2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #ifdef HAVE_SYS_PARAM_H
32 # include <sys/param.h>
37 # ifdef HAVE_SYS_SYSCALL_H
38 # include <sys/syscall.h>
42 #ifdef HAVE_SYS_VM86_H
43 # include <sys/vm86.h>
46 #ifdef HAVE_SYS_SIGNAL_H
47 # include <sys/signal.h>
53 #include "selectors.h"
55 /***********************************************************************
56 * signal context platform-specific definitions
62 unsigned short sc_gs, __gsh;
63 unsigned short sc_fs, __fsh;
64 unsigned short sc_es, __esh;
65 unsigned short sc_ds, __dsh;
74 unsigned long sc_trapno;
77 unsigned short sc_cs, __csh;
78 unsigned long sc_eflags;
79 unsigned long esp_at_signal;
80 unsigned short sc_ss, __ssh;
82 unsigned long oldmask;
86 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
87 #define HANDLER_CONTEXT (&__context)
89 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
90 struct kernel_sigaction
92 void (*ksa_handler)();
93 unsigned long ksa_mask;
94 unsigned long ksa_flags;
98 /* Similar to the sigaction function in libc, except it leaves alone the
99 restorer field, which is used to specify the signal stack address */
100 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
101 struct kernel_sigaction *old )
103 __asm__ __volatile__( "pushl %%ebx\n\t"
108 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
109 if (sig>=0) return 0;
114 #ifdef HAVE_SIGALTSTACK
115 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
116 static inline int wine_sigaltstack( const struct sigaltstack *new,
117 struct sigaltstack *old )
120 __asm__ __volatile__( "pushl %%ebx\n\t"
125 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
126 if (ret >= 0) return 0;
132 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
134 int vm86_enter( void **vm86_ptr );
135 void vm86_return(void);
136 void vm86_return_end(void);
137 __ASM_GLOBAL_FUNC(vm86_enter,
139 "movl %esp, %ebp\n\t"
140 "movl $166,%eax\n\t" /*SYS_vm86*/
141 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
142 "movl (%ecx),%ecx\n\t"
144 "movl $1,%ebx\n\t" /*VM86_ENTER*/
145 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
148 ".globl " __ASM_NAME("vm86_return") "\n\t"
149 ".type " __ASM_NAME("vm86_return") ",@function\n"
150 __ASM_NAME("vm86_return") ":\n\t"
155 "testl %eax,%eax\n\t"
157 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
158 "je " __ASM_NAME("vm86_enter") "\n\t"
160 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
162 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
163 ".type " __ASM_NAME("vm86_return_end") ",@function\n"
164 __ASM_NAME("vm86_return_end") ":\n\t"
173 #define EAX_sig(context) ((context)->tf_eax)
174 #define EBX_sig(context) ((context)->tf_ebx)
175 #define ECX_sig(context) ((context)->tf_ecx)
176 #define EDX_sig(context) ((context)->tf_edx)
177 #define ESI_sig(context) ((context)->tf_esi)
178 #define EDI_sig(context) ((context)->tf_edi)
179 #define EBP_sig(context) ((context)->tf_ebp)
181 #define CS_sig(context) ((context)->tf_cs)
182 #define DS_sig(context) ((context)->tf_ds)
183 #define ES_sig(context) ((context)->tf_es)
184 #define SS_sig(context) ((context)->tf_ss)
186 #include <machine/frame.h>
187 typedef struct trapframe SIGCONTEXT;
189 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
190 #define HANDLER_CONTEXT __context
192 #define EFL_sig(context) ((context)->tf_eflags)
194 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
195 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
199 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
201 typedef struct sigcontext SIGCONTEXT;
203 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
204 #define HANDLER_CONTEXT __context
208 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
211 #include <sys/regset.h>
215 #include <sys/ucontext.h>
217 typedef struct ucontext SIGCONTEXT;
219 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
220 #define HANDLER_CONTEXT __context
222 #endif /* svr4 || SCO_DS */
228 unsigned long ContextFlags;
229 FLOATING_SAVE_AREA sc_float;
234 unsigned long sc_edi;
235 unsigned long sc_esi;
236 unsigned long sc_eax;
237 unsigned long sc_ebx;
238 unsigned long sc_ecx;
239 unsigned long sc_edx;
240 unsigned long sc_ebp;
241 unsigned long sc_eip;
243 unsigned long sc_eflags;
244 unsigned long sc_esp;
251 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
253 #define EAX_sig(context) ((context)->sc_eax)
254 #define EBX_sig(context) ((context)->sc_ebx)
255 #define ECX_sig(context) ((context)->sc_ecx)
256 #define EDX_sig(context) ((context)->sc_edx)
257 #define ESI_sig(context) ((context)->sc_esi)
258 #define EDI_sig(context) ((context)->sc_edi)
259 #define EBP_sig(context) ((context)->sc_ebp)
261 #define CS_sig(context) ((context)->sc_cs)
262 #define DS_sig(context) ((context)->sc_ds)
263 #define ES_sig(context) ((context)->sc_es)
264 #define SS_sig(context) ((context)->sc_ss)
266 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
267 * saved by the exception handling. duh.
268 * Actually they are in -current (have been for a while), and that
269 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
270 * If you're running a system from the -stable branch older than that,
271 * like a 3.3-RELEASE, grab the patch from the ports tree:
272 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
273 * (If its not yet there when you look, go here:
274 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
277 #define FS_sig(context) ((context)->sc_fs)
278 #define GS_sig(context) ((context)->sc_gs)
282 #define FS_sig(context) ((context)->sc_fs)
283 #define GS_sig(context) ((context)->sc_gs)
284 #define CR2_sig(context) ((context)->cr2)
285 #define TRAP_sig(context) ((context)->sc_trapno)
286 #define ERROR_sig(context) ((context)->sc_err)
287 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
291 #define EFL_sig(context) ((context)->sc_eflags)
293 #define EFL_sig(context) ((context)->sc_efl)
294 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
295 #define CR2_sig(context) ((context)->sc_err)
296 #define TRAP_sig(context) ((context)->sc_trapno)
299 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
300 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
302 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
304 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
310 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
311 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
312 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
313 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
314 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
315 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
316 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
318 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
319 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
320 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
321 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
323 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
324 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
326 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
328 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
330 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
332 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
335 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
338 #endif /* svr4 || SCO_DS */
341 /* exception code definitions (already defined by FreeBSD/NetBSD) */
342 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
343 #define T_DIVIDE 0 /* Division by zero exception */
344 #define T_TRCTRAP 1 /* Single-step exception */
345 #define T_NMI 2 /* NMI interrupt */
346 #define T_BPTFLT 3 /* Breakpoint exception */
347 #define T_OFLOW 4 /* Overflow exception */
348 #define T_BOUND 5 /* Bound range exception */
349 #define T_PRIVINFLT 6 /* Invalid opcode exception */
350 #define T_DNA 7 /* Device not available exception */
351 #define T_DOUBLEFLT 8 /* Double fault exception */
352 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
353 #define T_TSSFLT 10 /* Invalid TSS exception */
354 #define T_SEGNPFLT 11 /* Segment not present exception */
355 #define T_STKFLT 12 /* Stack fault */
356 #define T_PROTFLT 13 /* General protection fault */
357 #define T_PAGEFLT 14 /* Page fault */
358 #define T_RESERVED 15 /* Unknown exception */
359 #define T_ARITHTRAP 16 /* Floating point exception */
360 #define T_ALIGNFLT 17 /* Alignment check exception */
361 #define T_MCHK 18 /* Machine check exception */
362 #define T_CACHEFLT 19 /* Cache flush exception */
364 #if defined(__NetBSD__)
365 #define T_MCHK 19 /* Machine check exception */
368 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
370 #include "wine/exception.h"
372 #include "stackframe.h"
376 #include "syslevel.h"
377 #include "wine/debug.h"
379 WINE_DEFAULT_DEBUG_CHANNEL(seh);
381 static sigset_t all_sigs;
384 /***********************************************************************
387 * Get the trap code for a signal.
389 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
392 return TRAP_sig(sigcontext);
394 return T_UNKNOWN; /* unknown trap code */
398 /***********************************************************************
401 * Get the error code for a signal.
403 static inline int get_error_code( const SIGCONTEXT *sigcontext )
406 return ERROR_sig(sigcontext);
412 /***********************************************************************
415 * Get the CR2 value for a signal.
417 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
420 return (void *)CR2_sig(sigcontext);
428 /***********************************************************************
431 * Set the register values from a vm86 structure.
433 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
435 context->Eax = vm86->regs.eax;
436 context->Ebx = vm86->regs.ebx;
437 context->Ecx = vm86->regs.ecx;
438 context->Edx = vm86->regs.edx;
439 context->Esi = vm86->regs.esi;
440 context->Edi = vm86->regs.edi;
441 context->Esp = vm86->regs.esp;
442 context->Ebp = vm86->regs.ebp;
443 context->Eip = vm86->regs.eip;
444 context->SegCs = vm86->regs.cs;
445 context->SegDs = vm86->regs.ds;
446 context->SegEs = vm86->regs.es;
447 context->SegFs = vm86->regs.fs;
448 context->SegGs = vm86->regs.gs;
449 context->SegSs = vm86->regs.ss;
450 context->EFlags = vm86->regs.eflags;
454 /***********************************************************************
455 * restore_vm86_context
457 * Build a vm86 structure from the register values.
459 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
461 vm86->regs.eax = context->Eax;
462 vm86->regs.ebx = context->Ebx;
463 vm86->regs.ecx = context->Ecx;
464 vm86->regs.edx = context->Edx;
465 vm86->regs.esi = context->Esi;
466 vm86->regs.edi = context->Edi;
467 vm86->regs.esp = context->Esp;
468 vm86->regs.ebp = context->Ebp;
469 vm86->regs.eip = context->Eip;
470 vm86->regs.cs = context->SegCs;
471 vm86->regs.ds = context->SegDs;
472 vm86->regs.es = context->SegEs;
473 vm86->regs.fs = context->SegFs;
474 vm86->regs.gs = context->SegGs;
475 vm86->regs.ss = context->SegSs;
476 vm86->regs.eflags = context->EFlags;
478 #endif /* __HAVE_VM86 */
481 /***********************************************************************
484 * Set the register values from a sigcontext.
486 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
489 /* get %fs at time of the fault */
491 fs = FS_sig(sigcontext);
497 /* now restore a proper %fs for the fault handler */
498 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) /* 16-bit mode */
500 fs = SYSLEVEL_Win16CurrentTeb;
503 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
505 /* fetch the saved %fs on the stack */
506 fs = *(unsigned int *)ESP_sig(sigcontext);
507 if (EAX_sig(sigcontext) == VM86_EAX) {
508 struct vm86plus_struct *vm86;
510 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
511 * (but we could also get if from teb->vm86_ptr) */
512 vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
513 /* get context from vm86 struct */
514 save_vm86_context( context, vm86 );
518 #endif /* __HAVE_VM86 */
522 context->Eax = EAX_sig(sigcontext);
523 context->Ebx = EBX_sig(sigcontext);
524 context->Ecx = ECX_sig(sigcontext);
525 context->Edx = EDX_sig(sigcontext);
526 context->Esi = ESI_sig(sigcontext);
527 context->Edi = EDI_sig(sigcontext);
528 context->Ebp = EBP_sig(sigcontext);
529 context->EFlags = EFL_sig(sigcontext);
530 context->Eip = EIP_sig(sigcontext);
531 context->Esp = ESP_sig(sigcontext);
532 context->SegCs = LOWORD(CS_sig(sigcontext));
533 context->SegDs = LOWORD(DS_sig(sigcontext));
534 context->SegEs = LOWORD(ES_sig(sigcontext));
535 context->SegSs = LOWORD(SS_sig(sigcontext));
537 context->SegGs = LOWORD(GS_sig(sigcontext));
539 context->SegGs = __get_gs();
544 /***********************************************************************
547 * Build a sigcontext from the register values.
549 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
552 /* check if exception occurred in vm86 mode */
553 if ((void *)EIP_sig(sigcontext) == vm86_return &&
554 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) &&
555 EAX_sig(sigcontext) == VM86_EAX)
557 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
558 * (but we could also get it from teb->vm86_ptr) */
559 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
560 restore_vm86_context( context, vm86 );
563 #endif /* __HAVE_VM86 */
565 EAX_sig(sigcontext) = context->Eax;
566 EBX_sig(sigcontext) = context->Ebx;
567 ECX_sig(sigcontext) = context->Ecx;
568 EDX_sig(sigcontext) = context->Edx;
569 ESI_sig(sigcontext) = context->Esi;
570 EDI_sig(sigcontext) = context->Edi;
571 EBP_sig(sigcontext) = context->Ebp;
572 EFL_sig(sigcontext) = context->EFlags;
573 EIP_sig(sigcontext) = context->Eip;
574 ESP_sig(sigcontext) = context->Esp;
575 CS_sig(sigcontext) = context->SegCs;
576 DS_sig(sigcontext) = context->SegDs;
577 ES_sig(sigcontext) = context->SegEs;
578 SS_sig(sigcontext) = context->SegSs;
580 FS_sig(sigcontext) = context->SegFs;
582 __set_fs( context->SegFs );
585 GS_sig(sigcontext) = context->SegGs;
587 __set_gs( context->SegGs );
592 /***********************************************************************
595 * Set the FPU context from a sigcontext.
597 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
600 if (FPU_sig(sigcontext))
602 context->FloatSave = *FPU_sig(sigcontext);
607 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
608 #endif /* __GNUC__ */
612 /***********************************************************************
615 * Restore the FPU context to a sigcontext.
617 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
619 /* reset the current interrupt status */
620 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
622 if (FPU_sig(sigcontext))
624 *FPU_sig(sigcontext) = context->FloatSave;
629 /* avoid nested exceptions */
630 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
631 #endif /* __GNUC__ */
635 /**********************************************************************
638 * Get the FPU exception code from the FPU status.
640 static inline DWORD get_fpu_code( const CONTEXT *context )
642 DWORD status = context->FloatSave.StatusWord;
644 if (status & 0x01) /* IE */
646 if (status & 0x40) /* SF */
647 return EXCEPTION_FLT_STACK_CHECK;
649 return EXCEPTION_FLT_INVALID_OPERATION;
651 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
652 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
653 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
654 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
655 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
656 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
660 /***********************************************************************
663 * Unblock signals. Called from EXC_RtlRaiseException.
665 void SIGNAL_Unblock( void )
667 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
671 /**********************************************************************
674 * Implementation of SIGSEGV handler.
676 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
678 EXCEPTION_RECORD rec;
679 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
682 /* we want the page-fault case to be fast */
683 if (trap_code == T_PAGEFLT)
684 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
687 rec.ExceptionRecord = NULL;
688 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
689 rec.ExceptionAddress = (LPVOID)context->Eip;
690 rec.NumberParameters = 0;
694 case T_OFLOW: /* Overflow exception */
695 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
697 case T_BOUND: /* Bound range exception */
698 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
700 case T_PRIVINFLT: /* Invalid opcode exception */
701 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
703 case T_STKFLT: /* Stack fault */
704 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
706 case T_SEGNPFLT: /* Segment not present exception */
707 case T_PROTFLT: /* General protection fault */
708 case T_UNKNOWN: /* Unknown fault code */
709 if (INSTR_EmulateInstruction( context )) return;
710 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
712 case T_PAGEFLT: /* Page fault */
714 rec.NumberParameters = 2;
715 rec.ExceptionInformation[0] = (err_code & 2) != 0;
716 rec.ExceptionInformation[1] = (DWORD)cr2;
718 rec.ExceptionCode = page_fault_code;
720 case T_ALIGNFLT: /* Alignment check exception */
721 /* FIXME: pass through exception handler first? */
722 if (context->EFlags & 0x00040000)
724 /* Disable AC flag, return */
725 context->EFlags &= ~0x00040000;
728 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
731 ERR( "Got unexpected trap %d\n", trap_code );
733 case T_NMI: /* NMI interrupt */
734 case T_DNA: /* Device not available exception */
735 case T_DOUBLEFLT: /* Double fault exception */
736 case T_TSSFLT: /* Invalid TSS exception */
737 case T_RESERVED: /* Unknown exception */
738 case T_MCHK: /* Machine check exception */
740 case T_CACHEFLT: /* Cache flush exception */
742 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
745 EXC_RtlRaiseException( &rec, context );
749 /**********************************************************************
752 * Implementation of SIGTRAP handler.
754 static void do_trap( CONTEXT *context, int trap_code )
756 EXCEPTION_RECORD rec;
758 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
759 rec.ExceptionRecord = NULL;
760 rec.ExceptionAddress = (LPVOID)context->Eip;
761 rec.NumberParameters = 0;
765 case T_TRCTRAP: /* Single-step exception */
766 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
767 context->EFlags &= ~0x100; /* clear single-step flag */
769 case T_BPTFLT: /* Breakpoint exception */
770 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
773 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
776 EXC_RtlRaiseException( &rec, context );
780 /**********************************************************************
783 * Implementation of SIGFPE handler
785 static void do_fpe( CONTEXT *context, int trap_code )
787 EXCEPTION_RECORD rec;
791 case T_DIVIDE: /* Division by zero exception */
792 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
794 case T_FPOPFLT: /* Coprocessor segment overrun */
795 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
797 case T_ARITHTRAP: /* Floating point exception */
798 case T_UNKNOWN: /* Unknown fault code */
799 rec.ExceptionCode = get_fpu_code( context );
802 ERR( "Got unexpected trap %d\n", trap_code );
803 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
806 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
807 rec.ExceptionRecord = NULL;
808 rec.ExceptionAddress = (LPVOID)context->Eip;
809 rec.NumberParameters = 0;
810 EXC_RtlRaiseException( &rec, context );
815 /**********************************************************************
818 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
820 static void set_vm86_pend( CONTEXT *context )
822 EXCEPTION_RECORD rec;
823 TEB *teb = NtCurrentTeb();
824 struct vm86plus_struct *vm86 = (struct vm86plus_struct*)(teb->vm86_ptr);
826 rec.ExceptionCode = EXCEPTION_VM86_STI;
827 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
828 rec.ExceptionRecord = NULL;
829 rec.NumberParameters = 1;
830 rec.ExceptionInformation[0] = 0;
832 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
833 teb->vm86_pending |= VIP_MASK;
834 /* see if we were in VM86 mode */
835 if (context->EFlags & 0x00020000)
837 /* seems so, also set flag in signal context */
838 if (context->EFlags & VIP_MASK) return;
839 context->EFlags |= VIP_MASK;
840 vm86->regs.eflags |= VIP_MASK; /* no exception recursion */
841 if (context->EFlags & VIF_MASK) {
842 /* VIF is set, throw exception */
843 teb->vm86_pending = 0;
844 teb->vm86_ptr = NULL;
845 rec.ExceptionAddress = (LPVOID)context->Eip;
846 EXC_RtlRaiseException( &rec, context );
847 teb->vm86_ptr = vm86;
852 /* not in VM86, but possibly setting up for it */
853 if (vm86->regs.eflags & VIP_MASK) return;
854 vm86->regs.eflags |= VIP_MASK;
855 if (((char*)context->Eip >= (char*)vm86_return) &&
856 ((char*)context->Eip <= (char*)vm86_return_end) &&
857 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
858 /* exiting from VM86, can't throw */
861 if (vm86->regs.eflags & VIF_MASK) {
862 /* VIF is set, throw exception */
864 teb->vm86_pending = 0;
865 teb->vm86_ptr = NULL;
866 save_vm86_context( &vcontext, vm86 );
867 rec.ExceptionAddress = (LPVOID)vcontext.Eip;
868 EXC_RtlRaiseException( &rec, &vcontext );
869 teb->vm86_ptr = vm86;
870 restore_vm86_context( &vcontext, vm86 );
876 /**********************************************************************
879 * Handler for SIGUSR2.
880 * We use it to signal that the running __wine_enter_vm86() should
881 * immediately set VIP_MASK, causing pending events to be handled
882 * as early as possible.
884 static HANDLER_DEF(usr2_handler)
888 save_context( &context, HANDLER_CONTEXT );
889 set_vm86_pend( &context );
890 restore_context( &context, HANDLER_CONTEXT );
894 /**********************************************************************
897 * Handler for SIGALRM.
898 * Increases the alarm counter and sets the vm86 pending flag.
900 static HANDLER_DEF(alrm_handler)
904 save_context( &context, HANDLER_CONTEXT );
905 NtCurrentTeb()->alarms++;
906 set_vm86_pend( &context );
907 restore_context( &context, HANDLER_CONTEXT );
909 #endif /* __HAVE_VM86 */
912 /**********************************************************************
915 * Handler for SIGSEGV and related errors.
917 static HANDLER_DEF(segv_handler)
920 save_context( &context, HANDLER_CONTEXT );
921 do_segv( &context, get_trap_code(HANDLER_CONTEXT),
922 get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
923 restore_context( &context, HANDLER_CONTEXT );
927 /**********************************************************************
930 * Handler for SIGTRAP.
932 static HANDLER_DEF(trap_handler)
935 save_context( &context, HANDLER_CONTEXT );
936 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
937 restore_context( &context, HANDLER_CONTEXT );
941 /**********************************************************************
944 * Handler for SIGFPE.
946 static HANDLER_DEF(fpe_handler)
949 save_fpu( &context, HANDLER_CONTEXT );
950 save_context( &context, HANDLER_CONTEXT );
951 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
952 restore_context( &context, HANDLER_CONTEXT );
953 restore_fpu( &context, HANDLER_CONTEXT );
957 /**********************************************************************
960 * Handler for SIGINT.
962 static HANDLER_DEF(int_handler)
964 EXCEPTION_RECORD rec;
967 save_context( &context, HANDLER_CONTEXT );
968 rec.ExceptionCode = CONTROL_C_EXIT;
969 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
970 rec.ExceptionRecord = NULL;
971 rec.ExceptionAddress = (LPVOID)context.Eip;
972 rec.NumberParameters = 0;
973 EXC_RtlRaiseException( &rec, &context );
974 restore_context( &context, HANDLER_CONTEXT );
978 /***********************************************************************
981 * Set a signal handler
983 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
985 struct sigaction sig_act;
988 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
990 struct kernel_sigaction sig_act;
991 sig_act.ksa_handler = func;
992 sig_act.ksa_flags = SA_RESTART;
993 sig_act.ksa_mask = (1 << (SIGINT-1)) |
995 /* point to the top of the stack */
996 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
997 return wine_sigaction( sig, &sig_act, NULL );
1000 sig_act.sa_handler = func;
1001 sigemptyset( &sig_act.sa_mask );
1002 sigaddset( &sig_act.sa_mask, SIGINT );
1003 sigaddset( &sig_act.sa_mask, SIGALRM );
1006 sig_act.sa_flags = SA_RESTART;
1007 #elif defined (__svr4__) || defined(_SCO_DS)
1008 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1010 sig_act.sa_flags = 0;
1014 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1016 return sigaction( sig, &sig_act, NULL );
1020 /**********************************************************************
1023 BOOL SIGNAL_Init(void)
1025 int have_sigaltstack = 0;
1027 #ifdef HAVE_SIGALTSTACK
1028 struct sigaltstack ss;
1029 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
1031 ss.ss_size = SIGNAL_STACK_SIZE;
1033 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1035 /* sigaltstack may fail because the kernel is too old, or
1036 because glibc is brain-dead. In the latter case a
1037 direct system call should succeed. */
1038 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1041 #endif /* HAVE_SIGALTSTACK */
1043 sigfillset( &all_sigs );
1045 /* automatic child reaping to avoid zombies */
1046 signal( SIGCHLD, SIG_IGN );
1048 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1049 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1050 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1051 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1053 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1056 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1060 if (set_handler( SIGALRM, have_sigaltstack, (void (*)())alrm_handler ) == -1) goto error;
1061 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1067 perror("sigaction");
1072 /**********************************************************************
1075 void SIGNAL_Reset(void)
1079 /* block the async signals */
1080 sigemptyset( &block_set );
1081 sigaddset( &block_set, SIGALRM );
1082 sigaddset( &block_set, SIGIO );
1083 sigaddset( &block_set, SIGHUP );
1084 sigaddset( &block_set, SIGUSR2 );
1085 sigprocmask( SIG_BLOCK, &block_set, NULL );
1087 /* restore default handlers */
1088 signal( SIGINT, SIG_DFL );
1089 signal( SIGFPE, SIG_DFL );
1090 signal( SIGSEGV, SIG_DFL );
1091 signal( SIGILL, SIG_DFL );
1093 signal( SIGBUS, SIG_DFL );
1096 signal( SIGTRAP, SIG_DFL );
1102 /**********************************************************************
1105 * Enter vm86 mode with the specified register context.
1107 void __wine_enter_vm86( CONTEXT *context )
1109 EXCEPTION_RECORD rec;
1110 TEB *teb = NtCurrentTeb();
1112 struct vm86plus_struct vm86;
1114 memset( &vm86, 0, sizeof(vm86) );
1117 restore_vm86_context( context, &vm86 );
1118 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1119 * so save it on entry, just in case */
1120 teb->vm86_pending |= (context->EFlags & VIP_MASK);
1121 /* Work around race conditions with signal handler
1122 * (avoiding sigprocmask for performance reasons) */
1123 teb->vm86_ptr = &vm86;
1124 vm86.regs.eflags |= teb->vm86_pending;
1125 /* Check for VIF|VIP here, since vm86_enter doesn't */
1126 if ((vm86.regs.eflags & (VIF_MASK|VIP_MASK)) == (VIF_MASK|VIP_MASK)) {
1127 teb->vm86_ptr = NULL;
1128 teb->vm86_pending = 0;
1129 context->EFlags |= VIP_MASK;
1130 rec.ExceptionCode = EXCEPTION_VM86_STI;
1131 rec.ExceptionInformation[0] = 0;
1137 res = vm86_enter( &teb->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1143 } while (VM86_TYPE(res) == VM86_SIGNAL);
1145 save_vm86_context( context, &vm86 );
1146 context->EFlags |= teb->vm86_pending;
1148 switch(VM86_TYPE(res))
1150 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1151 do_segv( context, T_PROTFLT, 0, 0 );
1153 case VM86_TRAP: /* return due to DOS-debugger request */
1154 do_trap( context, VM86_ARG(res) );
1156 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1157 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1159 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1160 teb->vm86_pending = 0;
1161 rec.ExceptionCode = EXCEPTION_VM86_STI;
1163 case VM86_PICRETURN: /* return due to pending PIC request */
1164 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1167 ERR( "unhandled result from vm86 mode %x\n", res );
1170 rec.ExceptionInformation[0] = VM86_ARG(res);
1172 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1173 rec.ExceptionRecord = NULL;
1174 rec.ExceptionAddress = (LPVOID)context->Eip;
1175 rec.NumberParameters = 1;
1176 EXC_RtlRaiseException( &rec, context );
1180 #else /* __HAVE_VM86 */
1181 void __wine_enter_vm86( CONTEXT *context )
1183 MESSAGE("vm86 mode not supported on this platform\n");
1185 #endif /* __HAVE_VM86 */
1187 /**********************************************************************
1188 * DbgBreakPoint (NTDLL.@)
1190 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1192 /**********************************************************************
1193 * DbgUserBreakPoint (NTDLL.@)
1195 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1197 #endif /* __i386__ */