2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
17 #ifdef HAVE_SYS_PARAM_H
18 # include <sys/param.h>
23 # ifdef HAVE_SYS_SYSCALL_H
24 # include <sys/syscall.h>
28 #ifdef HAVE_SYS_VM86_H
29 # include <sys/vm86.h>
32 #ifdef HAVE_SYS_SIGNAL_H
33 # include <sys/signal.h>
37 #include "selectors.h"
39 /***********************************************************************
40 * signal context platform-specific definitions
46 unsigned short sc_gs, __gsh;
47 unsigned short sc_fs, __fsh;
48 unsigned short sc_es, __esh;
49 unsigned short sc_ds, __dsh;
58 unsigned long sc_trapno;
61 unsigned short sc_cs, __csh;
62 unsigned long sc_eflags;
63 unsigned long esp_at_signal;
64 unsigned short sc_ss, __ssh;
66 unsigned long oldmask;
70 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
71 #define HANDLER_CONTEXT (&__context)
73 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
74 struct kernel_sigaction
76 void (*ksa_handler)();
77 unsigned long ksa_mask;
78 unsigned long ksa_flags;
82 /* Similar to the sigaction function in libc, except it leaves alone the
83 restorer field, which is used to specify the signal stack address */
84 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
85 struct kernel_sigaction *old )
87 __asm__ __volatile__( "pushl %%ebx\n\t"
92 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
98 #ifdef HAVE_SIGALTSTACK
99 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
100 static inline int wine_sigaltstack( const struct sigaltstack *new,
101 struct sigaltstack *old )
104 __asm__ __volatile__( "pushl %%ebx\n\t"
109 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
110 if (ret >= 0) return 0;
116 int vm86_enter( struct vm86plus_struct *ptr );
118 __ASM_GLOBAL_FUNC(vm86_enter,
120 "movl %esp, %ebp\n\t"
121 "movl $166,%eax\n\t" /*SYS_vm86*/
122 "movl 8(%ebp),%ecx\n\t"
124 "movl $1,%ebx\n\t" /*VM86_ENTER*/
125 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
128 ".globl " __ASM_NAME("vm86_return") "\n\t"
129 ".type " __ASM_NAME("vm86_return") ",@function\n"
130 __ASM_NAME("vm86_return") ":\n\t"
141 #define EAX_sig(context) ((context)->tf_eax)
142 #define EBX_sig(context) ((context)->tf_ebx)
143 #define ECX_sig(context) ((context)->tf_ecx)
144 #define EDX_sig(context) ((context)->tf_edx)
145 #define ESI_sig(context) ((context)->tf_esi)
146 #define EDI_sig(context) ((context)->tf_edi)
147 #define EBP_sig(context) ((context)->tf_ebp)
149 #define CS_sig(context) ((context)->tf_cs)
150 #define DS_sig(context) ((context)->tf_ds)
151 #define ES_sig(context) ((context)->tf_es)
152 #define SS_sig(context) ((context)->tf_ss)
154 #include <machine/frame.h>
155 typedef struct trapframe SIGCONTEXT;
157 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
158 #define HANDLER_CONTEXT __context
160 #define EFL_sig(context) ((context)->tf_eflags)
162 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
163 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
167 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
169 typedef struct sigcontext SIGCONTEXT;
171 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
172 #define HANDLER_CONTEXT __context
176 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
179 #include <sys/regset.h>
183 #include <sys/ucontext.h>
185 typedef struct ucontext SIGCONTEXT;
187 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
188 #define HANDLER_CONTEXT __context
190 #endif /* svr4 || SCO_DS */
196 unsigned long ContextFlags;
197 FLOATING_SAVE_AREA sc_float;
202 unsigned long sc_edi;
203 unsigned long sc_esi;
204 unsigned long sc_eax;
205 unsigned long sc_ebx;
206 unsigned long sc_ecx;
207 unsigned long sc_edx;
208 unsigned long sc_ebp;
209 unsigned long sc_eip;
211 unsigned long sc_eflags;
212 unsigned long sc_esp;
219 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
221 #define EAX_sig(context) ((context)->sc_eax)
222 #define EBX_sig(context) ((context)->sc_ebx)
223 #define ECX_sig(context) ((context)->sc_ecx)
224 #define EDX_sig(context) ((context)->sc_edx)
225 #define ESI_sig(context) ((context)->sc_esi)
226 #define EDI_sig(context) ((context)->sc_edi)
227 #define EBP_sig(context) ((context)->sc_ebp)
229 #define CS_sig(context) ((context)->sc_cs)
230 #define DS_sig(context) ((context)->sc_ds)
231 #define ES_sig(context) ((context)->sc_es)
232 #define SS_sig(context) ((context)->sc_ss)
234 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
235 * saved by the exception handling. duh.
236 * Actually they are in -current (have been for a while), and that
237 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
238 * If you're running a system from the -stable branch older than that,
239 * like a 3.3-RELEASE, grab the patch from the ports tree:
240 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
241 * (If its not yet there when you look, go here:
242 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
245 #define FS_sig(context) ((context)->sc_fs)
246 #define GS_sig(context) ((context)->sc_gs)
250 #define FS_sig(context) ((context)->sc_fs)
251 #define GS_sig(context) ((context)->sc_gs)
252 #define CR2_sig(context) ((context)->cr2)
253 #define TRAP_sig(context) ((context)->sc_trapno)
254 #define ERROR_sig(context) ((context)->sc_err)
255 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
259 #define EFL_sig(context) ((context)->sc_eflags)
261 #define EFL_sig(context) ((context)->sc_efl)
262 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
263 #define CR2_sig(context) ((context)->sc_err)
264 #define TRAP_sig(context) ((context)->sc_trapno)
267 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
268 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
270 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
272 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
278 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
279 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
280 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
281 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
282 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
283 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
284 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
286 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
287 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
288 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
289 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
291 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
292 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
294 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
296 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
298 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
300 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
303 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
306 #endif /* svr4 || SCO_DS */
309 /* exception code definitions (already defined by FreeBSD/NetBSD) */
310 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
311 #define T_DIVIDE 0 /* Division by zero exception */
312 #define T_TRCTRAP 1 /* Single-step exception */
313 #define T_NMI 2 /* NMI interrupt */
314 #define T_BPTFLT 3 /* Breakpoint exception */
315 #define T_OFLOW 4 /* Overflow exception */
316 #define T_BOUND 5 /* Bound range exception */
317 #define T_PRIVINFLT 6 /* Invalid opcode exception */
318 #define T_DNA 7 /* Device not available exception */
319 #define T_DOUBLEFLT 8 /* Double fault exception */
320 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
321 #define T_TSSFLT 10 /* Invalid TSS exception */
322 #define T_SEGNPFLT 11 /* Segment not present exception */
323 #define T_STKFLT 12 /* Stack fault */
324 #define T_PROTFLT 13 /* General protection fault */
325 #define T_PAGEFLT 14 /* Page fault */
326 #define T_RESERVED 15 /* Unknown exception */
327 #define T_ARITHTRAP 16 /* Floating point exception */
328 #define T_ALIGNFLT 17 /* Alignment check exception */
329 #define T_MCHK 18 /* Machine check exception */
330 #define T_CACHEFLT 19 /* Cache flush exception */
332 #if defined(__NetBSD__)
333 #define T_MCHK 19 /* Machine check exception */
336 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
338 #include "wine/exception.h"
340 #include "stackframe.h"
344 #include "syslevel.h"
345 #include "debugtools.h"
347 DEFAULT_DEBUG_CHANNEL(seh);
350 /***********************************************************************
353 * Get the trap code for a signal.
355 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
358 return TRAP_sig(sigcontext);
360 return T_UNKNOWN; /* unknown trap code */
364 /***********************************************************************
367 * Get the error code for a signal.
369 static inline int get_error_code( const SIGCONTEXT *sigcontext )
372 return ERROR_sig(sigcontext);
378 /***********************************************************************
381 * Get the CR2 value for a signal.
383 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
386 return (void *)CR2_sig(sigcontext);
394 /***********************************************************************
397 * Set the register values from a vm86 structure.
399 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
401 context->Eax = vm86->regs.eax;
402 context->Ebx = vm86->regs.ebx;
403 context->Ecx = vm86->regs.ecx;
404 context->Edx = vm86->regs.edx;
405 context->Esi = vm86->regs.esi;
406 context->Edi = vm86->regs.edi;
407 context->Esp = vm86->regs.esp;
408 context->Ebp = vm86->regs.ebp;
409 context->Eip = vm86->regs.eip;
410 context->SegCs = vm86->regs.cs;
411 context->SegDs = vm86->regs.ds;
412 context->SegEs = vm86->regs.es;
413 context->SegFs = vm86->regs.fs;
414 context->SegGs = vm86->regs.gs;
415 context->SegSs = vm86->regs.ss;
416 context->EFlags = vm86->regs.eflags;
420 /***********************************************************************
421 * restore_vm86_context
423 * Build a vm86 structure from the register values.
425 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
427 vm86->regs.eax = context->Eax;
428 vm86->regs.ebx = context->Ebx;
429 vm86->regs.ecx = context->Ecx;
430 vm86->regs.edx = context->Edx;
431 vm86->regs.esi = context->Esi;
432 vm86->regs.edi = context->Edi;
433 vm86->regs.esp = context->Esp;
434 vm86->regs.ebp = context->Ebp;
435 vm86->regs.eip = context->Eip;
436 vm86->regs.cs = context->SegCs;
437 vm86->regs.ds = context->SegDs;
438 vm86->regs.es = context->SegEs;
439 vm86->regs.fs = context->SegFs;
440 vm86->regs.gs = context->SegGs;
441 vm86->regs.ss = context->SegSs;
442 vm86->regs.eflags = context->EFlags;
447 /***********************************************************************
450 * Set the register values from a sigcontext.
452 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
455 /* get %fs at time of the fault */
457 fs = FS_sig(sigcontext);
463 /* now restore a proper %fs for the fault handler */
464 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) /* 16-bit mode */
466 fs = SYSLEVEL_Win16CurrentTeb;
469 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
471 /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
472 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
473 /* fetch the saved %fs on the stack */
474 fs = *(unsigned int *)ESP_sig(sigcontext);
476 /* get context from vm86 struct */
477 save_vm86_context( context, vm86 );
484 context->Eax = EAX_sig(sigcontext);
485 context->Ebx = EBX_sig(sigcontext);
486 context->Ecx = ECX_sig(sigcontext);
487 context->Edx = EDX_sig(sigcontext);
488 context->Esi = ESI_sig(sigcontext);
489 context->Edi = EDI_sig(sigcontext);
490 context->Ebp = EBP_sig(sigcontext);
491 context->EFlags = EFL_sig(sigcontext);
492 context->Eip = EIP_sig(sigcontext);
493 context->Esp = ESP_sig(sigcontext);
494 context->SegCs = LOWORD(CS_sig(sigcontext));
495 context->SegDs = LOWORD(DS_sig(sigcontext));
496 context->SegEs = LOWORD(ES_sig(sigcontext));
497 context->SegSs = LOWORD(SS_sig(sigcontext));
499 context->SegGs = LOWORD(GS_sig(sigcontext));
501 context->SegGs = __get_gs();
506 /***********************************************************************
509 * Build a sigcontext from the register values.
511 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
514 /* check if exception occurred in vm86 mode */
515 if ((void *)EIP_sig(sigcontext) == vm86_return &&
516 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)))
518 /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
519 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
520 restore_vm86_context( context, vm86 );
525 EAX_sig(sigcontext) = context->Eax;
526 EBX_sig(sigcontext) = context->Ebx;
527 ECX_sig(sigcontext) = context->Ecx;
528 EDX_sig(sigcontext) = context->Edx;
529 ESI_sig(sigcontext) = context->Esi;
530 EDI_sig(sigcontext) = context->Edi;
531 EBP_sig(sigcontext) = context->Ebp;
532 EFL_sig(sigcontext) = context->EFlags;
533 EIP_sig(sigcontext) = context->Eip;
534 ESP_sig(sigcontext) = context->Esp;
535 CS_sig(sigcontext) = context->SegCs;
536 DS_sig(sigcontext) = context->SegDs;
537 ES_sig(sigcontext) = context->SegEs;
538 SS_sig(sigcontext) = context->SegSs;
540 FS_sig(sigcontext) = context->SegFs;
542 __set_fs( context->SegFs );
545 GS_sig(sigcontext) = context->SegGs;
547 __set_gs( context->SegGs );
552 /***********************************************************************
555 * Set the FPU context from a sigcontext.
557 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
560 if (FPU_sig(sigcontext))
562 context->FloatSave = *FPU_sig(sigcontext);
567 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
568 #endif /* __GNUC__ */
572 /***********************************************************************
575 * Restore the FPU context to a sigcontext.
577 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
579 /* reset the current interrupt status */
580 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
582 if (FPU_sig(sigcontext))
584 *FPU_sig(sigcontext) = context->FloatSave;
589 /* avoid nested exceptions */
590 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
591 #endif /* __GNUC__ */
595 /**********************************************************************
598 * Get the FPU exception code from the FPU status.
600 static inline DWORD get_fpu_code( const CONTEXT *context )
602 DWORD status = context->FloatSave.StatusWord;
604 if (status & 0x01) /* IE */
606 if (status & 0x40) /* SF */
607 return EXCEPTION_FLT_STACK_CHECK;
609 return EXCEPTION_FLT_INVALID_OPERATION;
611 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
612 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
613 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
614 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
615 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
616 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
620 /**********************************************************************
623 * Implementation of SIGSEGV handler.
625 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
627 EXCEPTION_RECORD rec;
628 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
631 /* we want the page-fault case to be fast */
632 if (trap_code == T_PAGEFLT)
633 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
636 rec.ExceptionRecord = NULL;
637 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
638 rec.ExceptionAddress = (LPVOID)context->Eip;
639 rec.NumberParameters = 0;
643 case T_OFLOW: /* Overflow exception */
644 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
646 case T_BOUND: /* Bound range exception */
647 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
649 case T_PRIVINFLT: /* Invalid opcode exception */
650 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
652 case T_STKFLT: /* Stack fault */
653 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
655 case T_SEGNPFLT: /* Segment not present exception */
656 case T_PROTFLT: /* General protection fault */
657 case T_UNKNOWN: /* Unknown fault code */
658 if (INSTR_EmulateInstruction( context )) return;
659 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
661 case T_PAGEFLT: /* Page fault */
663 rec.NumberParameters = 2;
664 rec.ExceptionInformation[0] = (err_code & 2) != 0;
665 rec.ExceptionInformation[1] = (DWORD)cr2;
667 rec.ExceptionCode = page_fault_code;
669 case T_ALIGNFLT: /* Alignment check exception */
670 /* FIXME: pass through exception handler first? */
671 if (context->EFlags & 0x00040000)
673 /* Disable AC flag, return */
674 context->EFlags &= ~0x00040000;
677 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
680 ERR( "Got unexpected trap %d\n", trap_code );
682 case T_NMI: /* NMI interrupt */
683 case T_DNA: /* Device not available exception */
684 case T_DOUBLEFLT: /* Double fault exception */
685 case T_TSSFLT: /* Invalid TSS exception */
686 case T_RESERVED: /* Unknown exception */
687 case T_MCHK: /* Machine check exception */
689 case T_CACHEFLT: /* Cache flush exception */
691 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
694 EXC_RtlRaiseException( &rec, context );
698 /**********************************************************************
701 * Implementation of SIGTRAP handler.
703 static void do_trap( CONTEXT *context, int trap_code )
705 EXCEPTION_RECORD rec;
707 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
708 rec.ExceptionRecord = NULL;
709 rec.ExceptionAddress = (LPVOID)context->Eip;
710 rec.NumberParameters = 0;
714 case T_TRCTRAP: /* Single-step exception */
715 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
716 context->EFlags &= ~0x100; /* clear single-step flag */
718 case T_BPTFLT: /* Breakpoint exception */
719 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
722 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
725 EXC_RtlRaiseException( &rec, context );
729 /**********************************************************************
732 * Implementation of SIGFPE handler
734 static void do_fpe( CONTEXT *context, int trap_code )
736 EXCEPTION_RECORD rec;
740 case T_DIVIDE: /* Division by zero exception */
741 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
743 case T_FPOPFLT: /* Coprocessor segment overrun */
744 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
746 case T_ARITHTRAP: /* Floating point exception */
747 case T_UNKNOWN: /* Unknown fault code */
748 rec.ExceptionCode = get_fpu_code( context );
751 ERR( "Got unexpected trap %d\n", trap_code );
752 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
755 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
756 rec.ExceptionRecord = NULL;
757 rec.ExceptionAddress = (LPVOID)context->Eip;
758 rec.NumberParameters = 0;
759 EXC_RtlRaiseException( &rec, context );
763 /**********************************************************************
766 * Handler for SIGSEGV and related errors.
768 static HANDLER_DEF(segv_handler)
771 save_context( &context, HANDLER_CONTEXT );
772 do_segv( &context, get_trap_code(HANDLER_CONTEXT),
773 get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
774 restore_context( &context, HANDLER_CONTEXT );
778 /**********************************************************************
781 * Handler for SIGTRAP.
783 static HANDLER_DEF(trap_handler)
786 save_context( &context, HANDLER_CONTEXT );
787 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
788 restore_context( &context, HANDLER_CONTEXT );
792 /**********************************************************************
795 * Handler for SIGFPE.
797 static HANDLER_DEF(fpe_handler)
800 save_fpu( &context, HANDLER_CONTEXT );
801 save_context( &context, HANDLER_CONTEXT );
802 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
803 restore_context( &context, HANDLER_CONTEXT );
804 restore_fpu( &context, HANDLER_CONTEXT );
808 /**********************************************************************
811 * Handler for SIGINT.
813 static HANDLER_DEF(int_handler)
815 EXCEPTION_RECORD rec;
818 save_context( &context, HANDLER_CONTEXT );
819 rec.ExceptionCode = CONTROL_C_EXIT;
820 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
821 rec.ExceptionRecord = NULL;
822 rec.ExceptionAddress = (LPVOID)context.Eip;
823 rec.NumberParameters = 0;
824 EXC_RtlRaiseException( &rec, &context );
825 restore_context( &context, HANDLER_CONTEXT );
829 /***********************************************************************
832 * Set a signal handler
834 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
836 struct sigaction sig_act;
839 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
841 struct kernel_sigaction sig_act;
842 sig_act.ksa_handler = func;
843 sig_act.ksa_flags = SA_RESTART | SA_NOMASK;
844 sig_act.ksa_mask = 0;
845 /* point to the top of the stack */
846 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
847 return wine_sigaction( sig, &sig_act, NULL );
850 sig_act.sa_handler = func;
851 sigemptyset( &sig_act.sa_mask );
854 sig_act.sa_flags = SA_RESTART | SA_NOMASK;
855 #elif defined (__svr4__) || defined(_SCO_DS)
856 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
858 sig_act.sa_flags = 0;
862 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
864 return sigaction( sig, &sig_act, NULL );
868 /**********************************************************************
871 BOOL SIGNAL_Init(void)
873 int have_sigaltstack = 0;
875 #ifdef HAVE_SIGALTSTACK
876 struct sigaltstack ss;
877 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
879 ss.ss_size = SIGNAL_STACK_SIZE;
881 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
883 /* sigaltstack may fail because the kernel is too old, or
884 because glibc is brain-dead. In the latter case a
885 direct system call should succeed. */
886 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
889 #endif /* HAVE_SIGALTSTACK */
891 /* automatic child reaping to avoid zombies */
892 signal( SIGCHLD, SIG_IGN );
894 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
895 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
896 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
897 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
899 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
902 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
913 /**********************************************************************
916 * Enter vm86 mode with the specified register context.
918 void __wine_enter_vm86( CONTEXT *context )
920 EXCEPTION_RECORD rec;
922 struct vm86plus_struct vm86;
924 memset( &vm86, 0, sizeof(vm86) );
927 restore_vm86_context( context, &vm86 );
931 res = vm86_enter( &vm86 );
937 } while (VM86_TYPE(res) == VM86_SIGNAL);
939 save_vm86_context( context, &vm86 );
941 switch(VM86_TYPE(res))
943 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
944 do_segv( context, T_PROTFLT, 0, 0 );
946 case VM86_TRAP: /* return due to DOS-debugger request */
947 do_trap( context, VM86_ARG(res) );
949 case VM86_INTx: /* int3/int x instruction (ARG = x) */
950 rec.ExceptionCode = EXCEPTION_VM86_INTx;
952 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
953 rec.ExceptionCode = EXCEPTION_VM86_STI;
955 case VM86_PICRETURN: /* return due to pending PIC request */
956 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
959 ERR( "unhandled result from vm86 mode %x\n", res );
962 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
963 rec.ExceptionRecord = NULL;
964 rec.ExceptionAddress = (LPVOID)context->Eip;
965 rec.NumberParameters = 1;
966 rec.ExceptionInformation[0] = VM86_ARG(res);
967 EXC_RtlRaiseException( &rec, context );
972 void __wine_enter_vm86( CONTEXT *context )
974 MESSAGE("vm86 mode not supported on this platform\n");
978 /**********************************************************************
979 * DbgBreakPoint (NTDLL)
981 void WINAPI DbgBreakPoint(void);
982 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
984 /**********************************************************************
985 * DbgUserBreakPoint (NTDLL)
987 void WINAPI DbgUserBreakPoint(void);
988 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
990 #endif /* __i386__ */