2 * Emulator signal handling
4 * Copyright 1995 Alexandre Julliard
15 #include <sys/timeb.h>
16 #include <sys/types.h>
21 #include "sig_context.h"
26 static const char * const SIGNAL_traps
[] =
28 "Division by zero exception", /* 0 */
29 "Debug exception", /* 1 */
30 "NMI interrupt", /* 2 */
31 "Breakpoint exception", /* 3 */
32 "Overflow exception", /* 4 */
33 "Bound range exception", /* 5 */
34 "Invalid opcode exception", /* 6 */
35 "Device not available exception", /* 7 */
36 "Double fault exception", /* 8 */
37 "Coprocessor segment overrun", /* 9 */
38 "Invalid TSS exception", /* 10 */
39 "Segment not present exception", /* 11 */
40 "Stack fault", /* 12 */
41 "General protection fault", /* 13 */
42 "Page fault", /* 14 */
43 "Unknown exception", /* 15 */
44 "Floating point exception", /* 16 */
45 "Alignment check exception", /* 17 */
46 "Machine check exception" /* 18 */
49 #define NB_TRAPS (sizeof(SIGNAL_traps) / sizeof(SIGNAL_traps[0]))
51 extern void SIGNAL_SetHandler( int sig
, void (*func
)(), int flags
);
52 extern BOOL32
INSTR_EmulateInstruction( SIGCONTEXT
*context
);
55 /**********************************************************************
58 * Handle Ctrl-C and such
60 static HANDLER_DEF(SIGNAL_break
)
64 wine_debug( signal
, HANDLER_CONTEXT
); /* Enter our debugger */
69 /**********************************************************************
74 static HANDLER_DEF(SIGNAL_trap
)
77 wine_debug( signal
, HANDLER_CONTEXT
); /* Enter our debugger */
81 /**********************************************************************
86 static HANDLER_DEF(SIGNAL_fault
)
88 const char *fault
= "Segmentation fault";
91 if (INSTR_EmulateInstruction( HANDLER_CONTEXT
)) return;
93 if (TRAP_sig( HANDLER_CONTEXT
) < NB_TRAPS
)
94 fault
= SIGNAL_traps
[TRAP_sig( HANDLER_CONTEXT
)];
96 if (IS_SELECTOR_SYSTEM(CS_sig(HANDLER_CONTEXT
)))
98 MSG("%s in 32-bit code (0x%08lx).\n", fault
, EIP_sig(HANDLER_CONTEXT
));
102 MSG("%s in 16-bit code (%04x:%04lx).\n", fault
,
103 (WORD
)CS_sig(HANDLER_CONTEXT
), EIP_sig(HANDLER_CONTEXT
) );
106 MSG("Fault address is 0x%08lx\n",CR2_sig(HANDLER_CONTEXT
));
108 wine_debug( signal
, HANDLER_CONTEXT
);
112 /***********************************************************************
115 * Set the register values from a sigcontext.
117 #ifdef UNUSED_FUNCTIONS
118 static void SIGNAL_SetSigContext( const SIGCONTEXT
*sigcontext
,
121 EAX_reg(context
) = EAX_sig(sigcontext
);
122 EBX_reg(context
) = EBX_sig(sigcontext
);
123 ECX_reg(context
) = ECX_sig(sigcontext
);
124 EDX_reg(context
) = EDX_sig(sigcontext
);
125 ESI_reg(context
) = ESI_sig(sigcontext
);
126 EDI_reg(context
) = EDI_sig(sigcontext
);
127 EBP_reg(context
) = EBP_sig(sigcontext
);
128 EFL_reg(context
) = EFL_sig(sigcontext
);
129 EIP_reg(context
) = EIP_sig(sigcontext
);
130 ESP_reg(context
) = ESP_sig(sigcontext
);
131 CS_reg(context
) = LOWORD(CS_sig(sigcontext
));
132 DS_reg(context
) = LOWORD(DS_sig(sigcontext
));
133 ES_reg(context
) = LOWORD(ES_sig(sigcontext
));
134 SS_reg(context
) = LOWORD(SS_sig(sigcontext
));
136 FS_reg(context
) = LOWORD(FS_sig(sigcontext
));
138 GET_FS( FS_reg(&DEBUG_context
) );
139 FS_reg(context
) &= 0xffff;
142 GS_reg(context
) = LOWORD(GS_sig(sigcontext
));
144 GET_GS( GS_reg(&DEBUG_context
) );
145 GS_reg(context
) &= 0xffff;
151 /***********************************************************************
152 * SIGNAL_GetSigContext
154 * Build a sigcontext from the register values.
156 #ifdef UNUSED_FUNCTIONS
157 static void SIGNAL_GetSigContext( SIGCONTEXT
*sigcontext
,
158 const CONTEXT
*context
)
160 EAX_sig(sigcontext
) = EAX_reg(context
);
161 EBX_sig(sigcontext
) = EBX_reg(context
);
162 ECX_sig(sigcontext
) = ECX_reg(context
);
163 EDX_sig(sigcontext
) = EDX_reg(context
);
164 ESI_sig(sigcontext
) = ESI_reg(context
);
165 EDI_sig(sigcontext
) = EDI_reg(context
);
166 EBP_sig(sigcontext
) = EBP_reg(context
);
167 EFL_sig(sigcontext
) = EFL_reg(context
);
168 EIP_sig(sigcontext
) = EIP_reg(context
);
169 ESP_sig(sigcontext
) = ESP_reg(context
);
170 CS_sig(sigcontext
) = CS_reg(context
);
171 DS_sig(sigcontext
) = DS_reg(context
);
172 ES_sig(sigcontext
) = ES_reg(context
);
173 SS_sig(sigcontext
) = SS_reg(context
);
175 FS_sig(sigcontext
) = FS_reg(context
);
177 SET_FS( FS_reg(&DEBUG_context
) );
180 GS_sig(sigcontext
) = GS_reg(context
);
182 SET_GS( GS_reg(&DEBUG_context
) );
188 /***********************************************************************
189 * SIGNAL_InfoRegisters
191 * Display registers information.
193 void SIGNAL_InfoRegisters( CONTEXT
*context
)
195 MSG(" CS:%04x SS:%04x DS:%04x ES:%04x FS:%04x GS:%04x",
196 (WORD
)CS_reg(context
), (WORD
)SS_reg(context
),
197 (WORD
)DS_reg(context
), (WORD
)ES_reg(context
),
198 (WORD
)FS_reg(context
), (WORD
)GS_reg(context
) );
199 MSG( "\n EIP:%08lx ESP:%08lx EBP:%08lx EFLAGS:%08lx\n",
200 EIP_reg(context
), ESP_reg(context
),
201 EBP_reg(context
), EFL_reg(context
) );
202 MSG( " EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n",
203 EAX_reg(context
), EBX_reg(context
),
204 ECX_reg(context
), EDX_reg(context
) );
205 MSG( " ESI:%08lx EDI:%08lx\n",
206 ESI_reg(context
), EDI_reg(context
) );
210 /**********************************************************************
211 * SIGNAL_InitEmulator
213 * Initialize emulator signals.
215 BOOL32
SIGNAL_InitEmulator(void)
217 SIGNAL_SetHandler( SIGINT
, (void (*)())SIGNAL_break
, 1);
218 SIGNAL_SetHandler( SIGSEGV
, (void (*)())SIGNAL_fault
, 1);
219 SIGNAL_SetHandler( SIGILL
, (void (*)())SIGNAL_fault
, 1);
220 SIGNAL_SetHandler( SIGFPE
, (void (*)())SIGNAL_fault
, 1);
221 SIGNAL_SetHandler( SIGTRAP
, (void (*)())SIGNAL_trap
, 1); /* debugger */
222 SIGNAL_SetHandler( SIGHUP
, (void (*)())SIGNAL_trap
, 1); /* forced break*/
224 SIGNAL_SetHandler( SIGBUS
, (void (*)())SIGNAL_fault
, 1);