Started the implementation of MSACM.DLL and MSACM32.DLL.
[wine] / if1632 / signal.c
1 /*
2  * Emulator signal handling
3  *
4  * Copyright 1995 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <time.h>
12 #include <setjmp.h>
13
14 #include <sys/time.h>
15 #include <sys/timeb.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18
19 #include "debugger.h"
20 #include "options.h"
21 #include "sig_context.h"
22 #include "miscemu.h"
23 #include "thread.h"
24 #include "debug.h"
25
26 static const char * const SIGNAL_traps[] =
27 {
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 */
47 };
48
49 #define NB_TRAPS  (sizeof(SIGNAL_traps) / sizeof(SIGNAL_traps[0]))
50
51 extern void SIGNAL_SetHandler( int sig, void (*func)(), int flags );
52 extern BOOL32 INSTR_EmulateInstruction( SIGCONTEXT *context );
53
54
55 /**********************************************************************
56  *              SIGNAL_break
57  * 
58  * Handle Ctrl-C and such
59  */
60 static HANDLER_DEF(SIGNAL_break)
61 {
62     HANDLER_INIT();
63     if (Options.debug)
64         wine_debug( signal, HANDLER_CONTEXT );  /* Enter our debugger */
65     else exit(0);
66 }
67
68
69 /**********************************************************************
70  *              SIGNAL_trap
71  *
72  * SIGTRAP handler.
73  */
74 static HANDLER_DEF(SIGNAL_trap)
75 {
76     HANDLER_INIT();
77     wine_debug( signal, HANDLER_CONTEXT );  /* Enter our debugger */
78 }
79
80
81 /**********************************************************************
82  *              SIGNAL_fault
83  *
84  * Segfault handler.
85  */
86 static HANDLER_DEF(SIGNAL_fault)
87 {
88     const char *fault = "Segmentation fault";
89
90     HANDLER_INIT();
91     if (INSTR_EmulateInstruction( HANDLER_CONTEXT )) return;
92 #ifdef TRAP_sig
93     if (TRAP_sig( HANDLER_CONTEXT ) < NB_TRAPS)
94         fault = SIGNAL_traps[TRAP_sig( HANDLER_CONTEXT )];
95 #endif
96     if (IS_SELECTOR_SYSTEM(CS_sig(HANDLER_CONTEXT)))
97     {
98         MSG("%s in 32-bit code (0x%08lx).\n", fault, EIP_sig(HANDLER_CONTEXT));
99     }
100     else
101     {
102         MSG("%s in 16-bit code (%04x:%04lx).\n", fault,
103             (WORD)CS_sig(HANDLER_CONTEXT), EIP_sig(HANDLER_CONTEXT) );
104     }
105 #ifdef CR2_sig
106     MSG("Fault address is 0x%08lx\n",CR2_sig(HANDLER_CONTEXT));
107 #endif
108     wine_debug( signal, HANDLER_CONTEXT );
109 }
110
111
112 /***********************************************************************
113  *           SIGNAL_SetContext
114  *
115  * Set the register values from a sigcontext. 
116  */
117 #ifdef UNUSED_FUNCTIONS
118 static void SIGNAL_SetSigContext( const SIGCONTEXT *sigcontext,
119                                   CONTEXT *context )
120 {
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));
135 #ifdef FS_sig
136     FS_reg(context)  = LOWORD(FS_sig(sigcontext));
137 #else
138     GET_FS( FS_reg(&DEBUG_context) );
139     FS_reg(context) &= 0xffff;
140 #endif
141 #ifdef GS_sig
142     GS_reg(context)  = LOWORD(GS_sig(sigcontext));
143 #else
144     GET_GS( GS_reg(&DEBUG_context) );
145     GS_reg(context) &= 0xffff;
146 #endif
147 }
148 #endif
149
150
151 /***********************************************************************
152  *           SIGNAL_GetSigContext
153  *
154  * Build a sigcontext from the register values.
155  */
156 #ifdef UNUSED_FUNCTIONS
157 static void SIGNAL_GetSigContext( SIGCONTEXT *sigcontext,
158                                   const CONTEXT *context )
159 {
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);
174 #ifdef FS_sig
175     FS_sig(sigcontext)  = FS_reg(context);
176 #else
177     SET_FS( FS_reg(&DEBUG_context) );
178 #endif
179 #ifdef GS_sig
180     GS_sig(sigcontext)  = GS_reg(context);
181 #else
182     SET_GS( GS_reg(&DEBUG_context) );
183 #endif
184 }
185 #endif
186
187
188 /***********************************************************************
189  *           SIGNAL_InfoRegisters
190  *
191  * Display registers information. 
192  */
193 void SIGNAL_InfoRegisters( CONTEXT *context )
194 {
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) );
207 }
208
209
210 /**********************************************************************
211  *              SIGNAL_InitEmulator
212  *
213  * Initialize emulator signals.
214  */
215 BOOL32 SIGNAL_InitEmulator(void)
216 {
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*/
223 #ifdef SIGBUS
224     SIGNAL_SetHandler( SIGBUS,  (void (*)())SIGNAL_fault, 1);
225 #endif
226     return TRUE;
227 }