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__)
120 typedef struct sigcontext SIGCONTEXT
;
122 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
123 #define HANDLER_CONTEXT __context
127 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
131 #include <sys/regset.h>
135 #include <sys/ucontext.h>
137 typedef struct ucontext SIGCONTEXT
;
139 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
140 #define HANDLER_CONTEXT __context
142 #endif /* svr4 || SCO_DS */
148 unsigned long ContextFlags
;
149 FLOATING_SAVE_AREA sc_float
;
154 unsigned long sc_edi
;
155 unsigned long sc_esi
;
156 unsigned long sc_eax
;
157 unsigned long sc_ebx
;
158 unsigned long sc_ecx
;
159 unsigned long sc_edx
;
160 unsigned long sc_ebp
;
161 unsigned long sc_eip
;
163 unsigned long sc_eflags
;
164 unsigned long sc_esp
;
171 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
173 #define EAX_sig(context) ((context)->sc_eax)
174 #define EBX_sig(context) ((context)->sc_ebx)
175 #define ECX_sig(context) ((context)->sc_ecx)
176 #define EDX_sig(context) ((context)->sc_edx)
177 #define ESI_sig(context) ((context)->sc_esi)
178 #define EDI_sig(context) ((context)->sc_edi)
179 #define EBP_sig(context) ((context)->sc_ebp)
181 #define CS_sig(context) ((context)->sc_cs)
182 #define DS_sig(context) ((context)->sc_ds)
183 #define ES_sig(context) ((context)->sc_es)
184 #define SS_sig(context) ((context)->sc_ss)
187 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
188 * saved by the exception handling. duh.
190 #define FS_sig(context) ((context)->sc_fs)
191 #define GS_sig(context) ((context)->sc_gs)
192 #define CR2_sig(context) ((context)->cr2)
193 #define TRAP_sig(context) ((context)->sc_trapno)
194 #define ERROR_sig(context) ((context)->sc_err)
195 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
199 #define EFL_sig(context) ((context)->sc_eflags)
201 #define EFL_sig(context) ((context)->sc_efl)
202 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
203 #define CR2_sig(context) ((context)->sc_err)
204 #define TRAP_sig(context) ((context)->sc_trapno)
207 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
208 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
210 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
212 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
218 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
219 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
220 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
221 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
222 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
223 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
224 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
226 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
227 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
228 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
229 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
231 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
232 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
234 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
236 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
238 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
240 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
243 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
246 #endif /* svr4 || SCO_DS */
249 /* exception code definitions (already defined by FreeBSD) */
250 #ifndef __FreeBSD__ /* FIXME: other BSDs? */
251 #define T_DIVIDE 0 /* Division by zero exception */
252 #define T_TRCTRAP 1 /* Single-step exception */
253 #define T_NMI 2 /* NMI interrupt */
254 #define T_BPTFLT 3 /* Breakpoint exception */
255 #define T_OFLOW 4 /* Overflow exception */
256 #define T_BOUND 5 /* Bound range exception */
257 #define T_PRIVINFLT 6 /* Invalid opcode exception */
258 #define T_DNA 7 /* Device not available exception */
259 #define T_DOUBLEFLT 8 /* Double fault exception */
260 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
261 #define T_TSSFLT 10 /* Invalid TSS exception */
262 #define T_SEGNPFLT 11 /* Segment not present exception */
263 #define T_STKFLT 12 /* Stack fault */
264 #define T_PROTFLT 13 /* General protection fault */
265 #define T_PAGEFLT 14 /* Page fault */
266 #define T_RESERVED 15 /* Unknown exception */
267 #define T_ARITHTRAP 16 /* Floating point exception */
268 #define T_ALIGNFLT 17 /* Alignment check exception */
269 #define T_MCHK 18 /* Machine check exception */
270 #define T_CACHEFLT 19 /* Cache flush exception */
273 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
276 #include "stackframe.h"
280 #include "syslevel.h"
281 #include "debugtools.h"
283 DEFAULT_DEBUG_CHANNEL(seh
)
286 extern void WINAPI
REGS_FUNC(RtlRaiseException
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
289 /***********************************************************************
292 * Initialization code for a signal handler.
293 * Restores the proper %fs value for the current thread.
295 static inline void handler_init( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
298 /* get %fs at time of the fault */
300 fs
= FS_sig(sigcontext
);
305 /* now restore a proper %fs for the fault handler */
306 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext
))) fs
= SYSLEVEL_Win16CurrentTeb
;
307 if (!fs
) fs
= SYSLEVEL_EmergencyTeb
;
311 /***********************************************************************
314 * Get the trap code for a signal.
316 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
319 return TRAP_sig(sigcontext
);
321 return T_UNKNOWN
; /* unknown trap code */
325 /***********************************************************************
328 * Set the register values from a sigcontext.
330 static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
332 context
->Eax
= EAX_sig(sigcontext
);
333 context
->Ebx
= EBX_sig(sigcontext
);
334 context
->Ecx
= ECX_sig(sigcontext
);
335 context
->Edx
= EDX_sig(sigcontext
);
336 context
->Esi
= ESI_sig(sigcontext
);
337 context
->Edi
= EDI_sig(sigcontext
);
338 context
->Ebp
= EBP_sig(sigcontext
);
339 context
->EFlags
= EFL_sig(sigcontext
);
340 context
->Eip
= EIP_sig(sigcontext
);
341 context
->Esp
= ESP_sig(sigcontext
);
342 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
343 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
344 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
345 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
346 /* %fs already handled in handler_init */
348 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
350 GET_GS( context
->SegGs
);
351 context
->SegGs
&= 0xffff;
353 if (ISV86(context
)) V86BASE(context
) = (DWORD
)DOSMEM_MemoryBase(0);
357 /***********************************************************************
360 * Build a sigcontext from the register values.
362 static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
364 EAX_sig(sigcontext
) = context
->Eax
;
365 EBX_sig(sigcontext
) = context
->Ebx
;
366 ECX_sig(sigcontext
) = context
->Ecx
;
367 EDX_sig(sigcontext
) = context
->Edx
;
368 ESI_sig(sigcontext
) = context
->Esi
;
369 EDI_sig(sigcontext
) = context
->Edi
;
370 EBP_sig(sigcontext
) = context
->Ebp
;
371 EFL_sig(sigcontext
) = context
->EFlags
;
372 EIP_sig(sigcontext
) = context
->Eip
;
373 ESP_sig(sigcontext
) = context
->Esp
;
374 CS_sig(sigcontext
) = context
->SegCs
;
375 DS_sig(sigcontext
) = context
->SegDs
;
376 ES_sig(sigcontext
) = context
->SegEs
;
377 SS_sig(sigcontext
) = context
->SegSs
;
379 FS_sig(sigcontext
) = context
->SegFs
;
381 SET_FS( context
->SegFs
);
384 GS_sig(sigcontext
) = context
->SegGs
;
386 SET_GS( context
->SegGs
);
391 /***********************************************************************
394 * Set the FPU context from a sigcontext.
396 static void inline save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
399 if (FPU_sig(sigcontext
))
401 context
->FloatSave
= *FPU_sig(sigcontext
);
406 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
407 #endif /* __GNUC__ */
411 /***********************************************************************
414 * Restore the FPU context to a sigcontext.
416 static void inline restore_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
419 if (FPU_sig(sigcontext
))
421 *FPU_sig(sigcontext
) = context
->FloatSave
;
426 /* avoid nested exceptions */
427 context
->FloatSave
.StatusWord
&= context
->FloatSave
.ControlWord
| 0xffffff80;
428 __asm__
__volatile__( "frstor %0; fwait" : : "m" (context
->FloatSave
) );
429 #endif /* __GNUC__ */
433 /**********************************************************************
436 * Get the FPU exception code from the FPU status.
438 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
440 DWORD status
= context
->FloatSave
.StatusWord
;
442 if (status
& 0x01) /* IE */
444 if (status
& 0x40) /* SF */
445 return EXCEPTION_FLT_STACK_CHECK
;
447 return EXCEPTION_FLT_INVALID_OPERATION
;
449 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
450 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
451 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
452 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
453 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
454 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
458 /**********************************************************************
461 * Handler for SIGSEGV and related errors.
463 static HANDLER_DEF(segv_handler
)
465 EXCEPTION_RECORD rec
;
468 handler_init( &context
, HANDLER_CONTEXT
);
471 /* we want the page-fault case to be fast */
472 if (get_trap_code(HANDLER_CONTEXT
) == T_PAGEFLT
)
473 if (VIRTUAL_HandleFault( (LPVOID
)CR2_sig(HANDLER_CONTEXT
) )) return;
476 save_context( &context
, HANDLER_CONTEXT
);
477 rec
.ExceptionRecord
= NULL
;
478 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
479 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
480 rec
.NumberParameters
= 0;
482 switch(get_trap_code(HANDLER_CONTEXT
))
484 case T_OFLOW
: /* Overflow exception */
485 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
487 case T_BOUND
: /* Bound range exception */
488 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
490 case T_PRIVINFLT
: /* Invalid opcode exception */
491 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
493 case T_STKFLT
: /* Stack fault */
494 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
496 case T_SEGNPFLT
: /* Segment not present exception */
497 case T_PROTFLT
: /* General protection fault */
498 case T_UNKNOWN
: /* Unknown fault code */
499 if (INSTR_EmulateInstruction( &context
)) goto restore
;
500 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
502 case T_PAGEFLT
: /* Page fault */
504 rec
.NumberParameters
= 2;
506 rec
.ExceptionInformation
[0] = (ERROR_sig(HANDLER_CONTEXT
) & 2) != 0;
508 rec
.ExceptionInformation
[0] = 0;
509 #endif /* ERROR_sig */
510 rec
.ExceptionInformation
[1] = CR2_sig(HANDLER_CONTEXT
);
512 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
514 case T_ALIGNFLT
: /* Alignment check exception */
515 /* FIXME: pass through exception handler first? */
516 if (context
.EFlags
& 0x00040000)
518 /* Disable AC flag, return */
519 context
.EFlags
&= ~0x00040000;
522 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
525 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT
) );
527 case T_NMI
: /* NMI interrupt */
528 case T_DNA
: /* Device not available exception */
529 case T_DOUBLEFLT
: /* Double fault exception */
530 case T_TSSFLT
: /* Invalid TSS exception */
531 case T_RESERVED
: /* Unknown exception */
532 case T_MCHK
: /* Machine check exception */
534 case T_CACHEFLT
: /* Cache flush exception */
536 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
540 REGS_FUNC(RtlRaiseException
)( &rec
, &context
);
542 restore_context( &context
, HANDLER_CONTEXT
);
546 /**********************************************************************
549 * Handler for SIGTRAP.
551 static HANDLER_DEF(trap_handler
)
553 EXCEPTION_RECORD rec
;
556 handler_init( &context
, HANDLER_CONTEXT
);
558 switch(get_trap_code(HANDLER_CONTEXT
))
560 case T_TRCTRAP
: /* Single-step exception */
561 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
563 case T_BPTFLT
: /* Breakpoint exception */
565 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
568 save_context( &context
, HANDLER_CONTEXT
);
569 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
570 rec
.ExceptionRecord
= NULL
;
571 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
572 rec
.NumberParameters
= 0;
573 REGS_FUNC(RtlRaiseException
)( &rec
, &context
);
574 restore_context( &context
, HANDLER_CONTEXT
);
578 /**********************************************************************
581 * Handler for SIGFPE.
583 static HANDLER_DEF(fpe_handler
)
585 EXCEPTION_RECORD rec
;
588 handler_init( &context
, HANDLER_CONTEXT
);
590 save_fpu( &context
, HANDLER_CONTEXT
);
592 switch(get_trap_code(HANDLER_CONTEXT
))
594 case T_DIVIDE
: /* Division by zero exception */
595 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
597 case T_FPOPFLT
: /* Coprocessor segment overrun */
598 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
600 case T_ARITHTRAP
: /* Floating point exception */
601 case T_UNKNOWN
: /* Unknown fault code */
602 rec
.ExceptionCode
= get_fpu_code( &context
);
605 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT
) );
606 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
609 save_context( &context
, HANDLER_CONTEXT
);
610 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
611 rec
.ExceptionRecord
= NULL
;
612 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
613 rec
.NumberParameters
= 0;
614 REGS_FUNC(RtlRaiseException
)( &rec
, &context
);
615 restore_context( &context
, HANDLER_CONTEXT
);
616 restore_fpu( &context
, HANDLER_CONTEXT
);
620 /**********************************************************************
623 * Handler for SIGINT.
625 static HANDLER_DEF(int_handler
)
627 EXCEPTION_RECORD rec
;
630 handler_init( &context
, HANDLER_CONTEXT
);
631 save_context( &context
, HANDLER_CONTEXT
);
632 rec
.ExceptionCode
= CONTROL_C_EXIT
;
633 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
634 rec
.ExceptionRecord
= NULL
;
635 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
636 rec
.NumberParameters
= 0;
637 REGS_FUNC(RtlRaiseException
)( &rec
, &context
);
638 restore_context( &context
, HANDLER_CONTEXT
);
642 /***********************************************************************
645 * Set a signal handler
647 static int set_handler( int sig
, void (*func
)() )
650 struct kernel_sigaction sig_act
;
651 sig_act
.sa_handler
= func
;
652 sig_act
.sa_flags
= SA_RESTART
| SA_NOMASK
;
654 /* point to the top of the stack */
655 sig_act
.sa_restorer
= (char *)NtCurrentTeb()->signal_stack
+ SIGNAL_STACK_SIZE
;
656 return wine_sigaction( sig
, &sig_act
, NULL
);
658 struct sigaction sig_act
;
659 sig_act
.sa_handler
= func
;
660 sigemptyset( &sig_act
.sa_mask
);
662 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
663 sig_act
.sa_flags
= SA_ONSTACK
;
664 # elif defined (__svr4__) || defined(_SCO_DS)
665 sig_act
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
| SA_RESTART
;
666 # elif defined(__EMX__)
667 sig_act
.sa_flags
= 0; /* FIXME: EMX has only SA_ACK and SA_SYSV */
669 sig_act
.sa_flags
= 0;
671 return sigaction( sig
, &sig_act
, NULL
);
676 /**********************************************************************
679 BOOL
SIGNAL_Init(void)
681 #ifdef HAVE_WORKING_SIGALTSTACK
682 struct sigaltstack ss
;
683 if ((ss
.ss_sp
= NtCurrentTeb()->signal_stack
))
685 ss
.ss_size
= SIGNAL_STACK_SIZE
;
687 if (sigaltstack(&ss
, NULL
) < 0)
689 perror("sigaltstack");
690 /* fall through on error and try it differently */
693 #endif /* HAVE_SIGALTSTACK */
695 /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead */
696 signal( SIGPIPE
, SIG_IGN
);
697 /* automatic child reaping to avoid zombies */
698 signal( SIGCHLD
, SIG_IGN
);
700 if (set_handler( SIGINT
, (void (*)())int_handler
) == -1) goto error
;
701 if (set_handler( SIGFPE
, (void (*)())fpe_handler
) == -1) goto error
;
702 if (set_handler( SIGSEGV
, (void (*)())segv_handler
) == -1) goto error
;
703 if (set_handler( SIGILL
, (void (*)())segv_handler
) == -1) goto error
;
705 if (set_handler( SIGBUS
, (void (*)())segv_handler
) == -1) goto error
;
708 if (set_handler( SIGTRAP
, (void (*)())trap_handler
) == -1) goto error
;
717 #endif /* __i386__ */