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
24 #include "wine/port.h"
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
40 # ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
45 #ifdef HAVE_SYS_VM86_H
46 # include <sys/vm86.h>
49 #ifdef HAVE_SYS_SIGNAL_H
50 # include <sys/signal.h>
55 #include "wine/library.h"
56 #include "ntdll_misc.h"
57 #include "selectors.h"
59 /***********************************************************************
60 * signal context platform-specific definitions
66 unsigned short sc_gs, __gsh;
67 unsigned short sc_fs, __fsh;
68 unsigned short sc_es, __esh;
69 unsigned short sc_ds, __dsh;
78 unsigned long sc_trapno;
81 unsigned short sc_cs, __csh;
82 unsigned long sc_eflags;
83 unsigned long esp_at_signal;
84 unsigned short sc_ss, __ssh;
86 unsigned long oldmask;
90 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
91 #define HANDLER_CONTEXT (&__context)
93 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
94 struct kernel_sigaction
96 void (*ksa_handler)();
97 unsigned long ksa_mask;
98 unsigned long ksa_flags;
102 /* Similar to the sigaction function in libc, except it leaves alone the
103 restorer field, which is used to specify the signal stack address */
104 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
105 struct kernel_sigaction *old )
107 __asm__ __volatile__( "pushl %%ebx\n\t"
112 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
113 if (sig>=0) return 0;
118 #ifdef HAVE_SIGALTSTACK
119 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
120 static inline int wine_sigaltstack( const struct sigaltstack *new,
121 struct sigaltstack *old )
124 __asm__ __volatile__( "pushl %%ebx\n\t"
129 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
130 if (ret >= 0) return 0;
136 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
138 int vm86_enter( void **vm86_ptr );
139 void vm86_return(void);
140 void vm86_return_end(void);
141 __ASM_GLOBAL_FUNC(vm86_enter,
143 "movl %esp, %ebp\n\t"
144 "movl $166,%eax\n\t" /*SYS_vm86*/
145 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
146 "movl (%ecx),%ecx\n\t"
148 "movl $1,%ebx\n\t" /*VM86_ENTER*/
149 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
153 ".globl " __ASM_NAME("vm86_return") "\n\t"
154 __ASM_FUNC("vm86_return") "\n"
155 __ASM_NAME("vm86_return") ":\n\t"
161 "testl %eax,%eax\n\t"
163 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
164 "je " __ASM_NAME("vm86_enter") "\n\t"
166 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
168 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
169 __ASM_FUNC("vm86_return_end") "\n"
170 __ASM_NAME("vm86_return_end") ":\n\t"
173 #ifdef HAVE_SYS_VM86_H
181 #define EAX_sig(context) ((context)->tf_eax)
182 #define EBX_sig(context) ((context)->tf_ebx)
183 #define ECX_sig(context) ((context)->tf_ecx)
184 #define EDX_sig(context) ((context)->tf_edx)
185 #define ESI_sig(context) ((context)->tf_esi)
186 #define EDI_sig(context) ((context)->tf_edi)
187 #define EBP_sig(context) ((context)->tf_ebp)
189 #define CS_sig(context) ((context)->tf_cs)
190 #define DS_sig(context) ((context)->tf_ds)
191 #define ES_sig(context) ((context)->tf_es)
192 #define SS_sig(context) ((context)->tf_ss)
194 #include <machine/frame.h>
195 typedef struct trapframe SIGCONTEXT;
197 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
198 #define HANDLER_CONTEXT __context
200 #define EFL_sig(context) ((context)->tf_eflags)
202 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
203 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
207 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
209 typedef struct sigcontext SIGCONTEXT;
211 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
212 #define HANDLER_CONTEXT __context
216 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
219 #include <sys/regset.h>
223 #include <sys/ucontext.h>
225 typedef struct ucontext SIGCONTEXT;
227 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
228 #define HANDLER_CONTEXT __context
230 #endif /* svr4 || SCO_DS */
236 unsigned long ContextFlags;
237 FLOATING_SAVE_AREA sc_float;
242 unsigned long sc_edi;
243 unsigned long sc_esi;
244 unsigned long sc_eax;
245 unsigned long sc_ebx;
246 unsigned long sc_ecx;
247 unsigned long sc_edx;
248 unsigned long sc_ebp;
249 unsigned long sc_eip;
251 unsigned long sc_eflags;
252 unsigned long sc_esp;
260 /* FIXME: This section is just here so it can compile, it's most likely
261 * completely wrong. */
265 unsigned short sc_gs, __gsh;
266 unsigned short sc_fs, __fsh;
267 unsigned short sc_es, __esh;
268 unsigned short sc_ds, __dsh;
269 unsigned long sc_edi;
270 unsigned long sc_esi;
271 unsigned long sc_ebp;
272 unsigned long sc_esp;
273 unsigned long sc_ebx;
274 unsigned long sc_edx;
275 unsigned long sc_ecx;
276 unsigned long sc_eax;
277 unsigned long sc_trapno;
278 unsigned long sc_err;
279 unsigned long sc_eip;
280 unsigned short sc_cs, __csh;
281 unsigned long sc_eflags;
282 unsigned long esp_at_signal;
283 unsigned short sc_ss, __ssh;
285 unsigned long oldmask;
289 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
290 #define HANDLER_CONTEXT (&__context)
292 #endif /* __CYGWIN__ */
294 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
295 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
297 #define EAX_sig(context) ((context)->sc_eax)
298 #define EBX_sig(context) ((context)->sc_ebx)
299 #define ECX_sig(context) ((context)->sc_ecx)
300 #define EDX_sig(context) ((context)->sc_edx)
301 #define ESI_sig(context) ((context)->sc_esi)
302 #define EDI_sig(context) ((context)->sc_edi)
303 #define EBP_sig(context) ((context)->sc_ebp)
305 #define CS_sig(context) ((context)->sc_cs)
306 #define DS_sig(context) ((context)->sc_ds)
307 #define ES_sig(context) ((context)->sc_es)
308 #define FS_sig(context) ((context)->sc_fs)
309 #define GS_sig(context) ((context)->sc_gs)
310 #define SS_sig(context) ((context)->sc_ss)
312 #define TRAP_sig(context) ((context)->sc_trapno)
315 #define ERROR_sig(context) ((context)->sc_err)
319 #define ERROR_sig(context) ((context)->sc_err)
320 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
321 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->cr2)
325 #define EFL_sig(context) ((context)->sc_efl)
326 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
327 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->sc_err)
329 #define EFL_sig(context) ((context)->sc_eflags)
332 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
333 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
335 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
337 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
343 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
344 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
345 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
346 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
347 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
348 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
349 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
351 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
352 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
353 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
354 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
356 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
357 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
359 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
361 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
363 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
365 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
368 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
371 #define FAULT_ADDRESS (__siginfo->si_addr)
373 #endif /* svr4 || SCO_DS */
376 /* exception code definitions (already defined by FreeBSD/NetBSD) */
377 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
378 #define T_DIVIDE 0 /* Division by zero exception */
379 #define T_TRCTRAP 1 /* Single-step exception */
380 #define T_NMI 2 /* NMI interrupt */
381 #define T_BPTFLT 3 /* Breakpoint exception */
382 #define T_OFLOW 4 /* Overflow exception */
383 #define T_BOUND 5 /* Bound range exception */
384 #define T_PRIVINFLT 6 /* Invalid opcode exception */
385 #define T_DNA 7 /* Device not available exception */
386 #define T_DOUBLEFLT 8 /* Double fault exception */
387 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
388 #define T_TSSFLT 10 /* Invalid TSS exception */
389 #define T_SEGNPFLT 11 /* Segment not present exception */
390 #define T_STKFLT 12 /* Stack fault */
391 #define T_PROTFLT 13 /* General protection fault */
392 #define T_PAGEFLT 14 /* Page fault */
393 #define T_RESERVED 15 /* Unknown exception */
394 #define T_ARITHTRAP 16 /* Floating point exception */
395 #define T_ALIGNFLT 17 /* Alignment check exception */
396 #define T_MCHK 18 /* Machine check exception */
397 #define T_CACHEFLT 19 /* Cache flush exception */
399 #if defined(__NetBSD__)
400 #define T_MCHK 19 /* Machine check exception */
403 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
405 #include "wine/exception.h"
406 #include "stackframe.h"
409 #include "syslevel.h"
410 #include "wine/debug.h"
412 WINE_DEFAULT_DEBUG_CHANNEL(seh);
414 typedef int (*wine_signal_handler)(unsigned int sig);
416 static wine_signal_handler handlers[256];
418 extern void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
420 /***********************************************************************
423 inline static int dispatch_signal(unsigned int sig)
425 if (handlers[sig] == NULL) return 0;
426 return handlers[sig](sig);
430 /***********************************************************************
433 * Get the trap code for a signal.
435 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
438 return TRAP_sig(sigcontext);
440 return T_UNKNOWN; /* unknown trap code */
444 /***********************************************************************
447 * Get the error code for a signal.
449 static inline int get_error_code( const SIGCONTEXT *sigcontext )
452 return ERROR_sig(sigcontext);
459 /***********************************************************************
462 * Set the register values from a vm86 structure.
464 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
466 context->Eax = vm86->regs.eax;
467 context->Ebx = vm86->regs.ebx;
468 context->Ecx = vm86->regs.ecx;
469 context->Edx = vm86->regs.edx;
470 context->Esi = vm86->regs.esi;
471 context->Edi = vm86->regs.edi;
472 context->Esp = vm86->regs.esp;
473 context->Ebp = vm86->regs.ebp;
474 context->Eip = vm86->regs.eip;
475 context->SegCs = vm86->regs.cs;
476 context->SegDs = vm86->regs.ds;
477 context->SegEs = vm86->regs.es;
478 context->SegFs = vm86->regs.fs;
479 context->SegGs = vm86->regs.gs;
480 context->SegSs = vm86->regs.ss;
481 context->EFlags = vm86->regs.eflags;
485 /***********************************************************************
486 * restore_vm86_context
488 * Build a vm86 structure from the register values.
490 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
492 vm86->regs.eax = context->Eax;
493 vm86->regs.ebx = context->Ebx;
494 vm86->regs.ecx = context->Ecx;
495 vm86->regs.edx = context->Edx;
496 vm86->regs.esi = context->Esi;
497 vm86->regs.edi = context->Edi;
498 vm86->regs.esp = context->Esp;
499 vm86->regs.ebp = context->Ebp;
500 vm86->regs.eip = context->Eip;
501 vm86->regs.cs = context->SegCs;
502 vm86->regs.ds = context->SegDs;
503 vm86->regs.es = context->SegEs;
504 vm86->regs.fs = context->SegFs;
505 vm86->regs.gs = context->SegGs;
506 vm86->regs.ss = context->SegSs;
507 vm86->regs.eflags = context->EFlags;
509 #endif /* __HAVE_VM86 */
512 /***********************************************************************
515 * Set the register values from a sigcontext.
517 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
519 /* get %fs and %gs at time of the fault */
521 context->SegFs = LOWORD(FS_sig(sigcontext));
523 context->SegFs = wine_get_fs();
526 context->SegGs = LOWORD(GS_sig(sigcontext));
528 context->SegGs = wine_get_gs();
531 /* now restore a proper %fs for the fault handler */
532 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) ||
533 !IS_SELECTOR_SYSTEM(SS_sig(sigcontext))) /* 16-bit mode */
536 * Win16 or DOS protected mode. Note that during switch
537 * from 16-bit mode to linear mode, CS may be set to system
538 * segment before FS is restored. Fortunately, in this case
539 * SS is still non-system segment. This is why both CS and SS
542 wine_set_fs( SYSLEVEL_Win16CurrentTeb );
543 wine_set_gs( NtCurrentTeb()->gs_sel );
546 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
548 unsigned int *stack = (unsigned int *)ESP_sig(sigcontext);
550 /* fetch the saved %fs on the stack */
551 wine_set_fs( stack[0] );
552 wine_set_gs( stack[1] );
553 if (EAX_sig(sigcontext) == VM86_EAX) {
554 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
555 * (but we could also get if from teb->vm86_ptr) */
556 struct vm86plus_struct *vm86 = (struct vm86plus_struct *)stack[2];
557 /* get context from vm86 struct */
558 save_vm86_context( context, vm86 );
562 #endif /* __HAVE_VM86 */
563 else /* 32-bit mode */
566 wine_set_fs( FS_sig(sigcontext) );
569 wine_set_gs( GS_sig(sigcontext) );
573 context->Eax = EAX_sig(sigcontext);
574 context->Ebx = EBX_sig(sigcontext);
575 context->Ecx = ECX_sig(sigcontext);
576 context->Edx = EDX_sig(sigcontext);
577 context->Esi = ESI_sig(sigcontext);
578 context->Edi = EDI_sig(sigcontext);
579 context->Ebp = EBP_sig(sigcontext);
580 context->EFlags = EFL_sig(sigcontext);
581 context->Eip = EIP_sig(sigcontext);
582 context->Esp = ESP_sig(sigcontext);
583 context->SegCs = LOWORD(CS_sig(sigcontext));
584 context->SegDs = LOWORD(DS_sig(sigcontext));
585 context->SegEs = LOWORD(ES_sig(sigcontext));
586 context->SegSs = LOWORD(SS_sig(sigcontext));
590 /***********************************************************************
593 * Build a sigcontext from the register values.
595 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
598 /* check if exception occurred in vm86 mode */
599 if ((void *)EIP_sig(sigcontext) == vm86_return &&
600 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) &&
601 EAX_sig(sigcontext) == VM86_EAX)
603 unsigned int *stack = (unsigned int *)ESP_sig(sigcontext);
604 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
605 * (but we could also get it from teb->vm86_ptr) */
606 struct vm86plus_struct *vm86 = (struct vm86plus_struct *)stack[2];
607 restore_vm86_context( context, vm86 );
610 #endif /* __HAVE_VM86 */
612 EAX_sig(sigcontext) = context->Eax;
613 EBX_sig(sigcontext) = context->Ebx;
614 ECX_sig(sigcontext) = context->Ecx;
615 EDX_sig(sigcontext) = context->Edx;
616 ESI_sig(sigcontext) = context->Esi;
617 EDI_sig(sigcontext) = context->Edi;
618 EBP_sig(sigcontext) = context->Ebp;
619 EFL_sig(sigcontext) = context->EFlags;
620 EIP_sig(sigcontext) = context->Eip;
621 ESP_sig(sigcontext) = context->Esp;
622 CS_sig(sigcontext) = context->SegCs;
623 DS_sig(sigcontext) = context->SegDs;
624 ES_sig(sigcontext) = context->SegEs;
625 SS_sig(sigcontext) = context->SegSs;
627 FS_sig(sigcontext) = context->SegFs;
629 wine_set_fs( context->SegFs );
632 GS_sig(sigcontext) = context->SegGs;
634 wine_set_gs( context->SegGs );
639 /***********************************************************************
642 * Handler initialization when the full context is not needed.
644 static void init_handler( const SIGCONTEXT *sigcontext )
646 /* restore a proper %fs for the fault handler */
647 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) ||
648 !IS_SELECTOR_SYSTEM(SS_sig(sigcontext))) /* 16-bit mode */
650 wine_set_fs( SYSLEVEL_Win16CurrentTeb );
651 wine_set_gs( NtCurrentTeb()->gs_sel );
654 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
656 /* fetch the saved %fs on the stack */
657 wine_set_fs( *(unsigned int *)ESP_sig(sigcontext) );
658 wine_set_gs( NtCurrentTeb()->gs_sel );
660 #endif /* __HAVE_VM86 */
661 else /* 32-bit mode, get %fs at time of the fault */
664 wine_set_fs( FS_sig(sigcontext) );
667 wine_set_gs( GS_sig(sigcontext) );
673 /***********************************************************************
676 * Set the FPU context from a sigcontext.
678 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
681 if (FPU_sig(sigcontext))
683 context->FloatSave = *FPU_sig(sigcontext);
688 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
689 #endif /* __GNUC__ */
693 /***********************************************************************
696 * Restore the FPU context to a sigcontext.
698 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
700 /* reset the current interrupt status */
701 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
703 if (FPU_sig(sigcontext))
705 *FPU_sig(sigcontext) = context->FloatSave;
710 /* avoid nested exceptions */
711 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
712 #endif /* __GNUC__ */
716 /**********************************************************************
719 * Get the FPU exception code from the FPU status.
721 static inline DWORD get_fpu_code( const CONTEXT *context )
723 DWORD status = context->FloatSave.StatusWord;
725 if (status & 0x01) /* IE */
727 if (status & 0x40) /* SF */
728 return EXCEPTION_FLT_STACK_CHECK;
730 return EXCEPTION_FLT_INVALID_OPERATION;
732 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
733 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
734 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
735 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
736 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
737 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
741 /**********************************************************************
744 * Implementation of SIGSEGV handler.
746 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
748 EXCEPTION_RECORD rec;
749 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
752 /* we want the page-fault case to be fast */
753 if (trap_code == T_PAGEFLT)
754 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
757 rec.ExceptionRecord = NULL;
758 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
759 rec.ExceptionAddress = (LPVOID)context->Eip;
760 rec.NumberParameters = 0;
764 case T_OFLOW: /* Overflow exception */
765 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
767 case T_BOUND: /* Bound range exception */
768 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
770 case T_PRIVINFLT: /* Invalid opcode exception */
771 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
773 case T_STKFLT: /* Stack fault */
774 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
776 case T_SEGNPFLT: /* Segment not present exception */
777 case T_PROTFLT: /* General protection fault */
778 case T_UNKNOWN: /* Unknown fault code */
779 if (!(rec.ExceptionCode = INSTR_EmulateInstruction( context ))) return;
781 case T_PAGEFLT: /* Page fault */
783 rec.NumberParameters = 2;
784 rec.ExceptionInformation[0] = (err_code & 2) != 0;
785 rec.ExceptionInformation[1] = (DWORD)cr2;
787 rec.ExceptionCode = page_fault_code;
789 case T_ALIGNFLT: /* Alignment check exception */
790 /* FIXME: pass through exception handler first? */
791 if (context->EFlags & 0x00040000)
793 /* Disable AC flag, return */
794 context->EFlags &= ~0x00040000;
797 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
800 ERR( "Got unexpected trap %d\n", trap_code );
802 case T_NMI: /* NMI interrupt */
803 case T_DNA: /* Device not available exception */
804 case T_DOUBLEFLT: /* Double fault exception */
805 case T_TSSFLT: /* Invalid TSS exception */
806 case T_RESERVED: /* Unknown exception */
807 case T_MCHK: /* Machine check exception */
809 case T_CACHEFLT: /* Cache flush exception */
811 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
814 EXC_RtlRaiseException( &rec, context );
818 /**********************************************************************
821 * Implementation of SIGTRAP handler.
823 static void do_trap( CONTEXT *context, int trap_code )
825 EXCEPTION_RECORD rec;
826 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
828 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
829 rec.ExceptionRecord = NULL;
830 rec.ExceptionAddress = (LPVOID)context->Eip;
831 rec.NumberParameters = 0;
835 case T_TRCTRAP: /* Single-step exception */
836 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
837 if (context->EFlags & 0x100)
839 context->EFlags &= ~0x100; /* clear single-step flag */
841 else /* hardware breakpoint, fetch the debug registers */
843 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
844 NtGetContextThread(GetCurrentThread(), context);
845 /* do we really have a bp from a debug register ?
846 * if not, then someone did a kill(SIGTRAP) on us, and we
847 * shall return a breakpoint, not a single step exception
849 if (!(context->Dr6 & 0xf))
850 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
853 case T_BPTFLT: /* Breakpoint exception */
854 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
857 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
867 EXC_RtlRaiseException( &rec, context );
869 if (dr0 != context->Dr0 || dr1 != context->Dr1 || dr2 != context->Dr2 ||
870 dr3 != context->Dr3 || dr6 != context->Dr6 || dr7 != context->Dr7)
872 /* the debug registers have changed, set the new values */
873 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
874 NtSetContextThread(GetCurrentThread(), context);
879 /**********************************************************************
882 * Implementation of SIGFPE handler
884 static void do_fpe( CONTEXT *context, int trap_code )
886 EXCEPTION_RECORD rec;
890 case T_DIVIDE: /* Division by zero exception */
891 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
893 case T_FPOPFLT: /* Coprocessor segment overrun */
894 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
896 case T_ARITHTRAP: /* Floating point exception */
897 case T_UNKNOWN: /* Unknown fault code */
898 rec.ExceptionCode = get_fpu_code( context );
901 ERR( "Got unexpected trap %d\n", trap_code );
902 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
905 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
906 rec.ExceptionRecord = NULL;
907 rec.ExceptionAddress = (LPVOID)context->Eip;
908 rec.NumberParameters = 0;
909 EXC_RtlRaiseException( &rec, context );
914 /**********************************************************************
917 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
919 static void set_vm86_pend( CONTEXT *context )
921 EXCEPTION_RECORD rec;
922 TEB *teb = NtCurrentTeb();
923 struct vm86plus_struct *vm86 = (struct vm86plus_struct*)(teb->vm86_ptr);
925 rec.ExceptionCode = EXCEPTION_VM86_STI;
926 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
927 rec.ExceptionRecord = NULL;
928 rec.NumberParameters = 1;
929 rec.ExceptionInformation[0] = 0;
931 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
932 teb->vm86_pending |= VIP_MASK;
933 /* see if we were in VM86 mode */
934 if (context->EFlags & 0x00020000)
936 /* seems so, also set flag in signal context */
937 if (context->EFlags & VIP_MASK) return;
938 context->EFlags |= VIP_MASK;
939 vm86->regs.eflags |= VIP_MASK; /* no exception recursion */
940 if (context->EFlags & VIF_MASK) {
941 /* VIF is set, throw exception */
942 teb->vm86_pending = 0;
943 teb->vm86_ptr = NULL;
944 rec.ExceptionAddress = (LPVOID)context->Eip;
945 EXC_RtlRaiseException( &rec, context );
947 * FIXME: EXC_RtlRaiseException has unblocked all signals.
948 * If we receive nested SIGUSR2 here, VM86 event
949 * handling may lock up!
951 teb->vm86_ptr = vm86;
956 /* not in VM86, but possibly setting up for it */
957 if (vm86->regs.eflags & VIP_MASK) return;
958 vm86->regs.eflags |= VIP_MASK;
959 if (((char*)context->Eip >= (char*)vm86_return) &&
960 ((char*)context->Eip <= (char*)vm86_return_end) &&
961 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
962 /* exiting from VM86, can't throw */
965 if (vm86->regs.eflags & VIF_MASK) {
966 /* VIF is set, throw exception */
968 teb->vm86_pending = 0;
969 teb->vm86_ptr = NULL;
970 save_vm86_context( &vcontext, vm86 );
971 rec.ExceptionAddress = (LPVOID)vcontext.Eip;
972 EXC_RtlRaiseException( &rec, &vcontext );
974 * FIXME: EXC_RtlRaiseException has unblocked all signals.
975 * If we receive nested SIGUSR2 here, VM86 event
976 * handling may lock up!
978 teb->vm86_ptr = vm86;
979 restore_vm86_context( &vcontext, vm86 );
982 else if(teb->dpmi_vif &&
983 !IS_SELECTOR_SYSTEM(context->SegCs) &&
984 !IS_SELECTOR_SYSTEM(context->SegSs))
986 /* Executing DPMI code and virtual interrupts are enabled. */
987 teb->vm86_pending = 0;
988 rec.ExceptionAddress = (LPVOID)context->Eip;
989 EXC_RtlRaiseException( &rec, context );
991 * EXC_RtlRaiseException has unblocked all signals and this
992 * signal handler is about to return to either DOS relay or
993 * IRQ handler. Because both of these will check pending
994 * interrupts again, it is not a problem if we receive
995 * a nested SIGUSR2 here and ignore it.
1001 /**********************************************************************
1004 * Handler for SIGUSR2.
1005 * We use it to signal that the running __wine_enter_vm86() should
1006 * immediately set VIP_MASK, causing pending events to be handled
1007 * as early as possible.
1009 static HANDLER_DEF(usr2_handler)
1013 save_context( &context, HANDLER_CONTEXT );
1014 set_vm86_pend( &context );
1015 restore_context( &context, HANDLER_CONTEXT );
1017 #endif /* __HAVE_VM86 */
1020 /**********************************************************************
1023 * Handler for SIGSEGV and related errors.
1025 static HANDLER_DEF(segv_handler)
1030 save_context( &context, HANDLER_CONTEXT );
1031 #ifdef FAULT_ADDRESS
1032 cr2 = FAULT_ADDRESS;
1036 do_segv( &context, get_trap_code(HANDLER_CONTEXT), cr2, get_error_code(HANDLER_CONTEXT) );
1037 restore_context( &context, HANDLER_CONTEXT );
1041 /**********************************************************************
1044 * Handler for SIGTRAP.
1046 static HANDLER_DEF(trap_handler)
1049 save_context( &context, HANDLER_CONTEXT );
1050 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
1051 restore_context( &context, HANDLER_CONTEXT );
1055 /**********************************************************************
1058 * Handler for SIGFPE.
1060 static HANDLER_DEF(fpe_handler)
1063 save_fpu( &context, HANDLER_CONTEXT );
1064 save_context( &context, HANDLER_CONTEXT );
1065 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
1066 restore_context( &context, HANDLER_CONTEXT );
1067 restore_fpu( &context, HANDLER_CONTEXT );
1071 /**********************************************************************
1074 * Handler for SIGINT.
1076 static HANDLER_DEF(int_handler)
1078 init_handler( HANDLER_CONTEXT );
1079 if (!dispatch_signal(SIGINT))
1081 EXCEPTION_RECORD rec;
1084 save_context( &context, HANDLER_CONTEXT );
1085 rec.ExceptionCode = CONTROL_C_EXIT;
1086 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1087 rec.ExceptionRecord = NULL;
1088 rec.ExceptionAddress = (LPVOID)context.Eip;
1089 rec.NumberParameters = 0;
1090 EXC_RtlRaiseException( &rec, &context );
1091 restore_context( &context, HANDLER_CONTEXT );
1095 /**********************************************************************
1098 * Handler for SIGABRT.
1100 static HANDLER_DEF(abrt_handler)
1102 EXCEPTION_RECORD rec;
1105 save_context( &context, HANDLER_CONTEXT );
1106 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
1107 rec.ExceptionFlags = EH_NONCONTINUABLE;
1108 rec.ExceptionRecord = NULL;
1109 rec.ExceptionAddress = (LPVOID)context.Eip;
1110 rec.NumberParameters = 0;
1111 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
1112 restore_context( &context, HANDLER_CONTEXT );
1116 /**********************************************************************
1119 * Handler for SIGTERM.
1121 static HANDLER_DEF(term_handler)
1123 init_handler( HANDLER_CONTEXT );
1124 SYSDEPS_AbortThread(0);
1128 /**********************************************************************
1131 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1133 static HANDLER_DEF(usr1_handler)
1135 LARGE_INTEGER timeout;
1137 init_handler( HANDLER_CONTEXT );
1138 /* wait with 0 timeout, will only return once the thread is no longer suspended */
1139 timeout.QuadPart = 0;
1140 NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout );
1144 /***********************************************************************
1147 * Set a signal handler
1149 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
1151 struct sigaction sig_act;
1154 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
1156 struct kernel_sigaction sig_act;
1157 sig_act.ksa_handler = func;
1158 sig_act.ksa_flags = SA_RESTART;
1159 sig_act.ksa_mask = (1 << (SIGINT-1)) |
1161 /* point to the top of the stack */
1162 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
1163 return wine_sigaction( sig, &sig_act, NULL );
1166 sig_act.sa_handler = func;
1167 sigemptyset( &sig_act.sa_mask );
1168 sigaddset( &sig_act.sa_mask, SIGINT );
1169 sigaddset( &sig_act.sa_mask, SIGUSR2 );
1171 #if defined(linux) || defined(__NetBSD__)
1172 sig_act.sa_flags = SA_RESTART;
1173 #elif defined (__svr4__) || defined(_SCO_DS)
1174 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1176 sig_act.sa_flags = 0;
1180 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1182 return sigaction( sig, &sig_act, NULL );
1186 /***********************************************************************
1187 * __wine_set_signal_handler (NTDLL.@)
1189 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1191 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
1192 if (handlers[sig] != NULL) return -2;
1193 handlers[sig] = wsh;
1198 /**********************************************************************
1201 BOOL SIGNAL_Init(void)
1203 int have_sigaltstack = 0;
1205 #ifdef HAVE_SIGALTSTACK
1206 struct sigaltstack ss;
1207 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
1209 ss.ss_size = SIGNAL_STACK_SIZE;
1211 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1213 /* sigaltstack may fail because the kernel is too old, or
1214 because glibc is brain-dead. In the latter case a
1215 direct system call should succeed. */
1216 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1219 #endif /* HAVE_SIGALTSTACK */
1221 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1222 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1223 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1224 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1225 if (set_handler( SIGABRT, have_sigaltstack, (void (*)())abrt_handler ) == -1) goto error;
1226 if (set_handler( SIGTERM, have_sigaltstack, (void (*)())term_handler ) == -1) goto error;
1227 if (set_handler( SIGUSR1, have_sigaltstack, (void (*)())usr1_handler ) == -1) goto error;
1229 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1232 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1236 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1242 perror("sigaction");
1247 /**********************************************************************
1250 * Block the async signals.
1252 void SIGNAL_Block(void)
1256 sigemptyset( &block_set );
1257 sigaddset( &block_set, SIGIO );
1258 sigaddset( &block_set, SIGHUP );
1259 sigaddset( &block_set, SIGUSR1 );
1260 sigaddset( &block_set, SIGUSR2 );
1261 sigprocmask( SIG_BLOCK, &block_set, NULL );
1265 /***********************************************************************
1268 * Unblock signals. Called from EXC_RtlRaiseException.
1270 void SIGNAL_Unblock(void)
1274 sigfillset( &all_sigs );
1275 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
1279 /**********************************************************************
1282 * Restore the default handlers.
1284 void SIGNAL_Reset(void)
1286 signal( SIGINT, SIG_DFL );
1287 signal( SIGFPE, SIG_DFL );
1288 signal( SIGSEGV, SIG_DFL );
1289 signal( SIGILL, SIG_DFL );
1290 signal( SIGABRT, SIG_DFL );
1291 signal( SIGTERM, SIG_DFL );
1293 signal( SIGBUS, SIG_DFL );
1296 signal( SIGTRAP, SIG_DFL );
1302 /**********************************************************************
1303 * __wine_enter_vm86 (NTDLL.@)
1305 * Enter vm86 mode with the specified register context.
1307 void __wine_enter_vm86( CONTEXT *context )
1309 EXCEPTION_RECORD rec;
1310 TEB *teb = NtCurrentTeb();
1312 struct vm86plus_struct vm86;
1314 memset( &vm86, 0, sizeof(vm86) );
1317 restore_vm86_context( context, &vm86 );
1318 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1319 * so save it on entry, just in case */
1320 teb->vm86_pending |= (context->EFlags & VIP_MASK);
1321 /* Work around race conditions with signal handler
1322 * (avoiding sigprocmask for performance reasons) */
1323 teb->vm86_ptr = &vm86;
1324 vm86.regs.eflags |= teb->vm86_pending;
1325 /* Check for VIF|VIP here, since vm86_enter doesn't */
1326 if ((vm86.regs.eflags & (VIF_MASK|VIP_MASK)) == (VIF_MASK|VIP_MASK)) {
1327 teb->vm86_ptr = NULL;
1328 teb->vm86_pending = 0;
1329 context->EFlags |= VIP_MASK;
1330 rec.ExceptionCode = EXCEPTION_VM86_STI;
1331 rec.ExceptionInformation[0] = 0;
1337 res = vm86_enter( &teb->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1343 } while (VM86_TYPE(res) == VM86_SIGNAL);
1345 save_vm86_context( context, &vm86 );
1346 context->EFlags |= teb->vm86_pending;
1348 switch(VM86_TYPE(res))
1350 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1351 do_segv( context, T_PROTFLT, 0, 0 );
1353 case VM86_TRAP: /* return due to DOS-debugger request */
1354 do_trap( context, VM86_ARG(res) );
1356 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1357 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1359 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1360 teb->vm86_pending = 0;
1361 rec.ExceptionCode = EXCEPTION_VM86_STI;
1363 case VM86_PICRETURN: /* return due to pending PIC request */
1364 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1367 ERR( "unhandled result from vm86 mode %x\n", res );
1370 rec.ExceptionInformation[0] = VM86_ARG(res);
1372 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1373 rec.ExceptionRecord = NULL;
1374 rec.ExceptionAddress = (LPVOID)context->Eip;
1375 rec.NumberParameters = 1;
1376 EXC_RtlRaiseException( &rec, context );
1380 #else /* __HAVE_VM86 */
1381 /**********************************************************************
1382 * __wine_enter_vm86 (NTDLL.@)
1384 void __wine_enter_vm86( CONTEXT *context )
1386 MESSAGE("vm86 mode not supported on this platform\n");
1388 #endif /* __HAVE_VM86 */
1390 /**********************************************************************
1391 * DbgBreakPoint (NTDLL.@)
1393 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1395 /**********************************************************************
1396 * DbgUserBreakPoint (NTDLL.@)
1398 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1400 #endif /* __i386__ */