2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
16 #ifdef HAVE_SYS_PARAM_H
17 # include <sys/param.h>
22 # ifdef HAVE_SYS_SYSCALL_H
23 # include <sys/syscall.h>
27 /***********************************************************************
28 * signal context platform-specific definitions
34 unsigned short sc_gs, __gsh;
35 unsigned short sc_fs, __fsh;
36 unsigned short sc_es, __esh;
37 unsigned short sc_ds, __dsh;
46 unsigned long sc_trapno;
49 unsigned short sc_cs, __csh;
50 unsigned long sc_eflags;
51 unsigned long esp_at_signal;
52 unsigned short sc_ss, __ssh;
54 unsigned long oldmask;
58 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
59 #define HANDLER_CONTEXT (&__context)
61 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
63 struct kernel_sigaction
66 unsigned long sa_mask;
67 unsigned long sa_flags;
71 /* Similar to the sigaction function in libc, except it leaves alone the
72 restorer field, which is used to specify the signal stack address */
73 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
74 struct kernel_sigaction *old )
76 __asm__ __volatile__( "pushl %%ebx\n\t"
81 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
91 #define EAX_sig(context) ((context)->tf_eax)
92 #define EBX_sig(context) ((context)->tf_ebx)
93 #define ECX_sig(context) ((context)->tf_ecx)
94 #define EDX_sig(context) ((context)->tf_edx)
95 #define ESI_sig(context) ((context)->tf_esi)
96 #define EDI_sig(context) ((context)->tf_edi)
97 #define EBP_sig(context) ((context)->tf_ebp)
99 #define CS_sig(context) ((context)->tf_cs)
100 #define DS_sig(context) ((context)->tf_ds)
101 #define ES_sig(context) ((context)->tf_es)
102 #define SS_sig(context) ((context)->tf_ss)
104 #include <machine/frame.h>
105 typedef struct trapframe SIGCONTEXT;
107 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
108 #define HANDLER_CONTEXT __context
110 #define EFL_sig(context) ((context)->tf_eflags)
112 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
113 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
117 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
119 typedef struct sigcontext SIGCONTEXT;
121 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
122 #define HANDLER_CONTEXT __context
126 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
129 #include <sys/regset.h>
133 #include <sys/ucontext.h>
135 typedef struct ucontext SIGCONTEXT;
137 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
138 #define HANDLER_CONTEXT __context
140 #endif /* svr4 || SCO_DS */
146 unsigned long ContextFlags;
147 FLOATING_SAVE_AREA sc_float;
152 unsigned long sc_edi;
153 unsigned long sc_esi;
154 unsigned long sc_eax;
155 unsigned long sc_ebx;
156 unsigned long sc_ecx;
157 unsigned long sc_edx;
158 unsigned long sc_ebp;
159 unsigned long sc_eip;
161 unsigned long sc_eflags;
162 unsigned long sc_esp;
169 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
171 #define EAX_sig(context) ((context)->sc_eax)
172 #define EBX_sig(context) ((context)->sc_ebx)
173 #define ECX_sig(context) ((context)->sc_ecx)
174 #define EDX_sig(context) ((context)->sc_edx)
175 #define ESI_sig(context) ((context)->sc_esi)
176 #define EDI_sig(context) ((context)->sc_edi)
177 #define EBP_sig(context) ((context)->sc_ebp)
179 #define CS_sig(context) ((context)->sc_cs)
180 #define DS_sig(context) ((context)->sc_ds)
181 #define ES_sig(context) ((context)->sc_es)
182 #define SS_sig(context) ((context)->sc_ss)
184 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
185 * saved by the exception handling. duh.
186 * Actually they are in -current (have been for a while), and that
187 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
188 * If you're running a system from the -stable branch older than that,
189 * like a 3.3-RELEASE, grab the patch from the ports tree:
190 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
191 * (If its not yet there when you look, go here:
192 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
195 #define FS_sig(context) ((context)->sc_fs)
196 #define GS_sig(context) ((context)->sc_gs)
200 #define FS_sig(context) ((context)->sc_fs)
201 #define GS_sig(context) ((context)->sc_gs)
202 #define CR2_sig(context) ((context)->cr2)
203 #define TRAP_sig(context) ((context)->sc_trapno)
204 #define ERROR_sig(context) ((context)->sc_err)
205 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
209 #define EFL_sig(context) ((context)->sc_eflags)
211 #define EFL_sig(context) ((context)->sc_efl)
212 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
213 #define CR2_sig(context) ((context)->sc_err)
214 #define TRAP_sig(context) ((context)->sc_trapno)
217 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
218 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
220 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
222 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
228 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
229 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
230 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
231 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
232 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
233 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
234 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
236 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
237 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
238 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
239 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
241 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
242 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
244 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
246 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
248 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
250 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
253 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
256 #endif /* svr4 || SCO_DS */
259 /* exception code definitions (already defined by FreeBSD) */
260 #ifndef __FreeBSD__ /* FIXME: other BSDs? */
261 #define T_DIVIDE 0 /* Division by zero exception */
262 #define T_TRCTRAP 1 /* Single-step exception */
263 #define T_NMI 2 /* NMI interrupt */
264 #define T_BPTFLT 3 /* Breakpoint exception */
265 #define T_OFLOW 4 /* Overflow exception */
266 #define T_BOUND 5 /* Bound range exception */
267 #define T_PRIVINFLT 6 /* Invalid opcode exception */
268 #define T_DNA 7 /* Device not available exception */
269 #define T_DOUBLEFLT 8 /* Double fault exception */
270 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
271 #define T_TSSFLT 10 /* Invalid TSS exception */
272 #define T_SEGNPFLT 11 /* Segment not present exception */
273 #define T_STKFLT 12 /* Stack fault */
274 #define T_PROTFLT 13 /* General protection fault */
275 #define T_PAGEFLT 14 /* Page fault */
276 #define T_RESERVED 15 /* Unknown exception */
277 #define T_ARITHTRAP 16 /* Floating point exception */
278 #define T_ALIGNFLT 17 /* Alignment check exception */
279 #define T_MCHK 18 /* Machine check exception */
280 #define T_CACHEFLT 19 /* Cache flush exception */
283 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
285 #include "wine/exception.h"
287 #include "stackframe.h"
291 #include "syslevel.h"
292 #include "debugtools.h"
294 DEFAULT_DEBUG_CHANNEL(seh)
298 /***********************************************************************
301 * Initialization code for a signal handler.
302 * Restores the proper %fs value for the current thread.
304 static inline void handler_init( CONTEXT *context, const SIGCONTEXT *sigcontext )
307 /* get %fs at time of the fault */
309 fs = FS_sig(sigcontext);
314 /* now restore a proper %fs for the fault handler */
315 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) fs = SYSLEVEL_Win16CurrentTeb;
316 if (!fs) fs = SYSLEVEL_EmergencyTeb;
320 /***********************************************************************
323 * Get the trap code for a signal.
325 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
328 return TRAP_sig(sigcontext);
330 return T_UNKNOWN; /* unknown trap code */
334 /***********************************************************************
337 * Set the register values from a sigcontext.
339 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
341 context->Eax = EAX_sig(sigcontext);
342 context->Ebx = EBX_sig(sigcontext);
343 context->Ecx = ECX_sig(sigcontext);
344 context->Edx = EDX_sig(sigcontext);
345 context->Esi = ESI_sig(sigcontext);
346 context->Edi = EDI_sig(sigcontext);
347 context->Ebp = EBP_sig(sigcontext);
348 context->EFlags = EFL_sig(sigcontext);
349 context->Eip = EIP_sig(sigcontext);
350 context->Esp = ESP_sig(sigcontext);
351 context->SegCs = LOWORD(CS_sig(sigcontext));
352 context->SegDs = LOWORD(DS_sig(sigcontext));
353 context->SegEs = LOWORD(ES_sig(sigcontext));
354 context->SegSs = LOWORD(SS_sig(sigcontext));
355 /* %fs already handled in handler_init */
357 context->SegGs = LOWORD(GS_sig(sigcontext));
359 GET_GS( context->SegGs );
360 context->SegGs &= 0xffff;
362 if (ISV86(context)) V86BASE(context) = (DWORD)DOSMEM_MemoryBase(0);
366 /***********************************************************************
369 * Build a sigcontext from the register values.
371 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
373 EAX_sig(sigcontext) = context->Eax;
374 EBX_sig(sigcontext) = context->Ebx;
375 ECX_sig(sigcontext) = context->Ecx;
376 EDX_sig(sigcontext) = context->Edx;
377 ESI_sig(sigcontext) = context->Esi;
378 EDI_sig(sigcontext) = context->Edi;
379 EBP_sig(sigcontext) = context->Ebp;
380 EFL_sig(sigcontext) = context->EFlags;
381 EIP_sig(sigcontext) = context->Eip;
382 ESP_sig(sigcontext) = context->Esp;
383 CS_sig(sigcontext) = context->SegCs;
384 DS_sig(sigcontext) = context->SegDs;
385 ES_sig(sigcontext) = context->SegEs;
386 SS_sig(sigcontext) = context->SegSs;
388 FS_sig(sigcontext) = context->SegFs;
390 SET_FS( context->SegFs );
393 GS_sig(sigcontext) = context->SegGs;
395 SET_GS( context->SegGs );
400 /***********************************************************************
403 * Set the FPU context from a sigcontext.
405 static void inline save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
408 if (FPU_sig(sigcontext))
410 context->FloatSave = *FPU_sig(sigcontext);
415 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
416 #endif /* __GNUC__ */
420 /***********************************************************************
423 * Restore the FPU context to a sigcontext.
425 static void inline restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
428 if (FPU_sig(sigcontext))
430 *FPU_sig(sigcontext) = context->FloatSave;
435 /* avoid nested exceptions */
436 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
437 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
438 #endif /* __GNUC__ */
442 /**********************************************************************
445 * Get the FPU exception code from the FPU status.
447 static inline DWORD get_fpu_code( const CONTEXT *context )
449 DWORD status = context->FloatSave.StatusWord;
451 if (status & 0x01) /* IE */
453 if (status & 0x40) /* SF */
454 return EXCEPTION_FLT_STACK_CHECK;
456 return EXCEPTION_FLT_INVALID_OPERATION;
458 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
459 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
460 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
461 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
462 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
463 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
467 /**********************************************************************
470 * Handler for SIGSEGV and related errors.
472 static HANDLER_DEF(segv_handler)
474 EXCEPTION_RECORD rec;
477 handler_init( &context, HANDLER_CONTEXT );
480 /* we want the page-fault case to be fast */
481 if (get_trap_code(HANDLER_CONTEXT) == T_PAGEFLT)
482 if (VIRTUAL_HandleFault( (LPVOID)CR2_sig(HANDLER_CONTEXT) )) return;
485 save_context( &context, HANDLER_CONTEXT );
486 rec.ExceptionRecord = NULL;
487 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
488 rec.ExceptionAddress = (LPVOID)context.Eip;
489 rec.NumberParameters = 0;
491 switch(get_trap_code(HANDLER_CONTEXT))
493 case T_OFLOW: /* Overflow exception */
494 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
496 case T_BOUND: /* Bound range exception */
497 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
499 case T_PRIVINFLT: /* Invalid opcode exception */
500 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
502 case T_STKFLT: /* Stack fault */
503 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
505 case T_SEGNPFLT: /* Segment not present exception */
506 case T_PROTFLT: /* General protection fault */
507 case T_UNKNOWN: /* Unknown fault code */
508 if (INSTR_EmulateInstruction( &context )) goto restore;
509 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
511 case T_PAGEFLT: /* Page fault */
513 rec.NumberParameters = 2;
515 rec.ExceptionInformation[0] = (ERROR_sig(HANDLER_CONTEXT) & 2) != 0;
517 rec.ExceptionInformation[0] = 0;
518 #endif /* ERROR_sig */
519 rec.ExceptionInformation[1] = CR2_sig(HANDLER_CONTEXT);
521 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
523 case T_ALIGNFLT: /* Alignment check exception */
524 /* FIXME: pass through exception handler first? */
525 if (context.EFlags & 0x00040000)
527 /* Disable AC flag, return */
528 context.EFlags &= ~0x00040000;
531 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
534 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
536 case T_NMI: /* NMI interrupt */
537 case T_DNA: /* Device not available exception */
538 case T_DOUBLEFLT: /* Double fault exception */
539 case T_TSSFLT: /* Invalid TSS exception */
540 case T_RESERVED: /* Unknown exception */
541 case T_MCHK: /* Machine check exception */
543 case T_CACHEFLT: /* Cache flush exception */
545 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
549 EXC_RtlRaiseException( &rec, &context );
551 restore_context( &context, HANDLER_CONTEXT );
555 /**********************************************************************
558 * Handler for SIGTRAP.
560 static HANDLER_DEF(trap_handler)
562 EXCEPTION_RECORD rec;
565 handler_init( &context, HANDLER_CONTEXT );
567 switch(get_trap_code(HANDLER_CONTEXT))
569 case T_TRCTRAP: /* Single-step exception */
570 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
572 case T_BPTFLT: /* Breakpoint exception */
574 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
577 save_context( &context, HANDLER_CONTEXT );
578 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
579 rec.ExceptionRecord = NULL;
580 rec.ExceptionAddress = (LPVOID)context.Eip;
581 rec.NumberParameters = 0;
582 EXC_RtlRaiseException( &rec, &context );
583 restore_context( &context, HANDLER_CONTEXT );
587 /**********************************************************************
590 * Handler for SIGFPE.
592 static HANDLER_DEF(fpe_handler)
594 EXCEPTION_RECORD rec;
597 handler_init( &context, HANDLER_CONTEXT );
599 save_fpu( &context, HANDLER_CONTEXT );
601 switch(get_trap_code(HANDLER_CONTEXT))
603 case T_DIVIDE: /* Division by zero exception */
604 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
606 case T_FPOPFLT: /* Coprocessor segment overrun */
607 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
609 case T_ARITHTRAP: /* Floating point exception */
610 case T_UNKNOWN: /* Unknown fault code */
611 rec.ExceptionCode = get_fpu_code( &context );
614 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
615 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
618 save_context( &context, HANDLER_CONTEXT );
619 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
620 rec.ExceptionRecord = NULL;
621 rec.ExceptionAddress = (LPVOID)context.Eip;
622 rec.NumberParameters = 0;
623 EXC_RtlRaiseException( &rec, &context );
624 restore_context( &context, HANDLER_CONTEXT );
625 restore_fpu( &context, HANDLER_CONTEXT );
629 /**********************************************************************
632 * Handler for SIGINT.
634 static HANDLER_DEF(int_handler)
636 EXCEPTION_RECORD rec;
639 handler_init( &context, HANDLER_CONTEXT );
640 save_context( &context, HANDLER_CONTEXT );
641 rec.ExceptionCode = CONTROL_C_EXIT;
642 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
643 rec.ExceptionRecord = NULL;
644 rec.ExceptionAddress = (LPVOID)context.Eip;
645 rec.NumberParameters = 0;
646 EXC_RtlRaiseException( &rec, &context );
647 restore_context( &context, HANDLER_CONTEXT );
651 /***********************************************************************
654 * Set a signal handler
656 static int set_handler( int sig, void (*func)() )
659 struct kernel_sigaction sig_act;
660 sig_act.sa_handler = func;
661 sig_act.sa_flags = SA_RESTART | SA_NOMASK;
663 /* point to the top of the stack */
664 sig_act.sa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
665 return wine_sigaction( sig, &sig_act, NULL );
667 struct sigaction sig_act;
668 sig_act.sa_handler = func;
669 sigemptyset( &sig_act.sa_mask );
671 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
672 sig_act.sa_flags = SA_ONSTACK;
673 # elif defined (__svr4__) || defined(_SCO_DS)
674 sig_act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
675 # elif defined(__EMX__)
676 sig_act.sa_flags = 0; /* FIXME: EMX has only SA_ACK and SA_SYSV */
678 sig_act.sa_flags = 0;
680 return sigaction( sig, &sig_act, NULL );
685 /**********************************************************************
688 BOOL SIGNAL_Init(void)
690 #ifdef HAVE_WORKING_SIGALTSTACK
691 struct sigaltstack ss;
692 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
694 ss.ss_size = SIGNAL_STACK_SIZE;
696 if (sigaltstack(&ss, NULL) < 0)
698 perror("sigaltstack");
699 /* fall through on error and try it differently */
702 #endif /* HAVE_SIGALTSTACK */
704 /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead */
705 signal( SIGPIPE, SIG_IGN );
706 /* automatic child reaping to avoid zombies */
707 signal( SIGCHLD, SIG_IGN );
709 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
710 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
711 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
712 if (set_handler( SIGILL, (void (*)())segv_handler ) == -1) goto error;
714 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
717 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
726 #endif /* __i386__ */