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"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
46 #ifdef HAVE_SYS_VM86_H
47 # include <sys/vm86.h>
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
58 #include "wine/library.h"
59 #include "ntdll_misc.h"
60 #include "selectors.h"
62 /***********************************************************************
63 * signal context platform-specific definitions
69 unsigned short sc_gs, __gsh;
70 unsigned short sc_fs, __fsh;
71 unsigned short sc_es, __esh;
72 unsigned short sc_ds, __dsh;
81 unsigned long sc_trapno;
84 unsigned short sc_cs, __csh;
85 unsigned long sc_eflags;
86 unsigned long esp_at_signal;
87 unsigned short sc_ss, __ssh;
89 unsigned long oldmask;
93 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
94 #define HANDLER_CONTEXT (&__context)
96 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
97 struct kernel_sigaction
99 void (*ksa_handler)();
100 unsigned long ksa_mask;
101 unsigned long ksa_flags;
105 /* Similar to the sigaction function in libc, except it leaves alone the
106 restorer field, which is used to specify the signal stack address */
107 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
108 struct kernel_sigaction *old )
110 __asm__ __volatile__( "pushl %%ebx\n\t"
115 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
116 if (sig>=0) return 0;
121 #ifdef HAVE_SIGALTSTACK
122 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
123 static inline int wine_sigaltstack( const struct sigaltstack *new,
124 struct sigaltstack *old )
127 __asm__ __volatile__( "pushl %%ebx\n\t"
132 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
133 if (ret >= 0) return 0;
139 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
141 int vm86_enter( void **vm86_ptr );
142 void vm86_return(void);
143 void vm86_return_end(void);
144 __ASM_GLOBAL_FUNC(vm86_enter,
146 "movl %esp, %ebp\n\t"
147 "movl $166,%eax\n\t" /*SYS_vm86*/
148 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
149 "movl (%ecx),%ecx\n\t"
151 "movl $1,%ebx\n\t" /*VM86_ENTER*/
152 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
156 ".globl " __ASM_NAME("vm86_return") "\n\t"
157 __ASM_FUNC("vm86_return") "\n"
158 __ASM_NAME("vm86_return") ":\n\t"
164 "testl %eax,%eax\n\t"
166 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
167 "je " __ASM_NAME("vm86_enter") "\n\t"
169 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
171 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
172 __ASM_FUNC("vm86_return_end") "\n"
173 __ASM_NAME("vm86_return_end") ":\n\t"
176 #ifdef HAVE_SYS_VM86_H
184 #define EAX_sig(context) ((context)->tf_eax)
185 #define EBX_sig(context) ((context)->tf_ebx)
186 #define ECX_sig(context) ((context)->tf_ecx)
187 #define EDX_sig(context) ((context)->tf_edx)
188 #define ESI_sig(context) ((context)->tf_esi)
189 #define EDI_sig(context) ((context)->tf_edi)
190 #define EBP_sig(context) ((context)->tf_ebp)
192 #define CS_sig(context) ((context)->tf_cs)
193 #define DS_sig(context) ((context)->tf_ds)
194 #define ES_sig(context) ((context)->tf_es)
195 #define SS_sig(context) ((context)->tf_ss)
197 #include <machine/frame.h>
198 typedef struct trapframe SIGCONTEXT;
200 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
201 #define HANDLER_CONTEXT __context
203 #define EFL_sig(context) ((context)->tf_eflags)
205 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
206 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
210 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
212 typedef struct sigcontext SIGCONTEXT;
214 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
215 #define HANDLER_CONTEXT __context
219 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
222 #include <sys/regset.h>
226 #include <sys/ucontext.h>
228 typedef struct ucontext SIGCONTEXT;
230 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
231 #define HANDLER_CONTEXT __context
233 #endif /* svr4 || SCO_DS */
239 unsigned long ContextFlags;
240 FLOATING_SAVE_AREA sc_float;
245 unsigned long sc_edi;
246 unsigned long sc_esi;
247 unsigned long sc_eax;
248 unsigned long sc_ebx;
249 unsigned long sc_ecx;
250 unsigned long sc_edx;
251 unsigned long sc_ebp;
252 unsigned long sc_eip;
254 unsigned long sc_eflags;
255 unsigned long sc_esp;
263 /* FIXME: This section is just here so it can compile, it's most likely
264 * completely wrong. */
268 unsigned short sc_gs, __gsh;
269 unsigned short sc_fs, __fsh;
270 unsigned short sc_es, __esh;
271 unsigned short sc_ds, __dsh;
272 unsigned long sc_edi;
273 unsigned long sc_esi;
274 unsigned long sc_ebp;
275 unsigned long sc_esp;
276 unsigned long sc_ebx;
277 unsigned long sc_edx;
278 unsigned long sc_ecx;
279 unsigned long sc_eax;
280 unsigned long sc_trapno;
281 unsigned long sc_err;
282 unsigned long sc_eip;
283 unsigned short sc_cs, __csh;
284 unsigned long sc_eflags;
285 unsigned long esp_at_signal;
286 unsigned short sc_ss, __ssh;
288 unsigned long oldmask;
292 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
293 #define HANDLER_CONTEXT (&__context)
295 #endif /* __CYGWIN__ */
297 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
298 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
300 #define EAX_sig(context) ((context)->sc_eax)
301 #define EBX_sig(context) ((context)->sc_ebx)
302 #define ECX_sig(context) ((context)->sc_ecx)
303 #define EDX_sig(context) ((context)->sc_edx)
304 #define ESI_sig(context) ((context)->sc_esi)
305 #define EDI_sig(context) ((context)->sc_edi)
306 #define EBP_sig(context) ((context)->sc_ebp)
308 #define CS_sig(context) ((context)->sc_cs)
309 #define DS_sig(context) ((context)->sc_ds)
310 #define ES_sig(context) ((context)->sc_es)
311 #define FS_sig(context) ((context)->sc_fs)
312 #define GS_sig(context) ((context)->sc_gs)
313 #define SS_sig(context) ((context)->sc_ss)
315 #define TRAP_sig(context) ((context)->sc_trapno)
318 #define ERROR_sig(context) ((context)->sc_err)
322 #define ERROR_sig(context) ((context)->sc_err)
323 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
324 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->cr2)
328 #define EFL_sig(context) ((context)->sc_efl)
329 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
330 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->sc_err)
332 #define EFL_sig(context) ((context)->sc_eflags)
335 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
336 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
338 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
340 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
346 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
347 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
348 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
349 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
350 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
351 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
352 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
354 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
355 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
356 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
357 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
359 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
360 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
362 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
364 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
366 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
368 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
371 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
374 #define FAULT_ADDRESS (__siginfo->si_addr)
376 #endif /* svr4 || SCO_DS */
379 /* exception code definitions (already defined by FreeBSD/NetBSD) */
380 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
381 #define T_DIVIDE 0 /* Division by zero exception */
382 #define T_TRCTRAP 1 /* Single-step exception */
383 #define T_NMI 2 /* NMI interrupt */
384 #define T_BPTFLT 3 /* Breakpoint exception */
385 #define T_OFLOW 4 /* Overflow exception */
386 #define T_BOUND 5 /* Bound range exception */
387 #define T_PRIVINFLT 6 /* Invalid opcode exception */
388 #define T_DNA 7 /* Device not available exception */
389 #define T_DOUBLEFLT 8 /* Double fault exception */
390 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
391 #define T_TSSFLT 10 /* Invalid TSS exception */
392 #define T_SEGNPFLT 11 /* Segment not present exception */
393 #define T_STKFLT 12 /* Stack fault */
394 #define T_PROTFLT 13 /* General protection fault */
395 #define T_PAGEFLT 14 /* Page fault */
396 #define T_RESERVED 15 /* Unknown exception */
397 #define T_ARITHTRAP 16 /* Floating point exception */
398 #define T_ALIGNFLT 17 /* Alignment check exception */
399 #define T_MCHK 18 /* Machine check exception */
400 #define T_CACHEFLT 19 /* Cache flush exception */
402 #if defined(__NetBSD__)
403 #define T_MCHK 19 /* Machine check exception */
406 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
408 #include "wine/exception.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 /* Global variable to save the thread %fs register while in 16-bit code (FIXME) */
421 static unsigned int signal_fs;
423 /***********************************************************************
426 inline static int dispatch_signal(unsigned int sig)
428 if (handlers[sig] == NULL) return 0;
429 return handlers[sig](sig);
433 /***********************************************************************
436 * Get the trap code for a signal.
438 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
441 return TRAP_sig(sigcontext);
443 return T_UNKNOWN; /* unknown trap code */
447 /***********************************************************************
450 * Get the error code for a signal.
452 static inline int get_error_code( const SIGCONTEXT *sigcontext )
455 return ERROR_sig(sigcontext);
462 /***********************************************************************
465 * Set the register values from a vm86 structure.
467 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
469 context->Eax = vm86->regs.eax;
470 context->Ebx = vm86->regs.ebx;
471 context->Ecx = vm86->regs.ecx;
472 context->Edx = vm86->regs.edx;
473 context->Esi = vm86->regs.esi;
474 context->Edi = vm86->regs.edi;
475 context->Esp = vm86->regs.esp;
476 context->Ebp = vm86->regs.ebp;
477 context->Eip = vm86->regs.eip;
478 context->SegCs = vm86->regs.cs;
479 context->SegDs = vm86->regs.ds;
480 context->SegEs = vm86->regs.es;
481 context->SegFs = vm86->regs.fs;
482 context->SegGs = vm86->regs.gs;
483 context->SegSs = vm86->regs.ss;
484 context->EFlags = vm86->regs.eflags;
488 /***********************************************************************
489 * restore_vm86_context
491 * Build a vm86 structure from the register values.
493 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
495 vm86->regs.eax = context->Eax;
496 vm86->regs.ebx = context->Ebx;
497 vm86->regs.ecx = context->Ecx;
498 vm86->regs.edx = context->Edx;
499 vm86->regs.esi = context->Esi;
500 vm86->regs.edi = context->Edi;
501 vm86->regs.esp = context->Esp;
502 vm86->regs.ebp = context->Ebp;
503 vm86->regs.eip = context->Eip;
504 vm86->regs.cs = context->SegCs;
505 vm86->regs.ds = context->SegDs;
506 vm86->regs.es = context->SegEs;
507 vm86->regs.fs = context->SegFs;
508 vm86->regs.gs = context->SegGs;
509 vm86->regs.ss = context->SegSs;
510 vm86->regs.eflags = context->EFlags;
512 #endif /* __HAVE_VM86 */
515 /***********************************************************************
518 * Set the register values from a sigcontext.
520 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
522 /* get %fs and %gs at time of the fault */
524 context->SegFs = LOWORD(FS_sig(sigcontext));
526 context->SegFs = wine_get_fs();
529 context->SegGs = LOWORD(GS_sig(sigcontext));
531 context->SegGs = wine_get_gs();
534 /* now restore a proper %fs for the fault handler */
535 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) ||
536 !IS_SELECTOR_SYSTEM(SS_sig(sigcontext))) /* 16-bit mode */
539 * Win16 or DOS protected mode. Note that during switch
540 * from 16-bit mode to linear mode, CS may be set to system
541 * segment before FS is restored. Fortunately, in this case
542 * SS is still non-system segment. This is why both CS and SS
545 wine_set_fs( signal_fs );
546 wine_set_gs( NtCurrentTeb()->gs_sel );
549 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
551 unsigned int *stack = (unsigned int *)ESP_sig(sigcontext);
553 /* fetch the saved %fs on the stack */
554 wine_set_fs( stack[0] );
555 wine_set_gs( stack[1] );
556 if (EAX_sig(sigcontext) == VM86_EAX) {
557 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
558 * (but we could also get if from teb->vm86_ptr) */
559 struct vm86plus_struct *vm86 = (struct vm86plus_struct *)stack[2];
560 /* get context from vm86 struct */
561 save_vm86_context( context, vm86 );
565 #endif /* __HAVE_VM86 */
566 else /* 32-bit mode */
569 wine_set_fs( FS_sig(sigcontext) );
572 wine_set_gs( GS_sig(sigcontext) );
576 context->Eax = EAX_sig(sigcontext);
577 context->Ebx = EBX_sig(sigcontext);
578 context->Ecx = ECX_sig(sigcontext);
579 context->Edx = EDX_sig(sigcontext);
580 context->Esi = ESI_sig(sigcontext);
581 context->Edi = EDI_sig(sigcontext);
582 context->Ebp = EBP_sig(sigcontext);
583 context->EFlags = EFL_sig(sigcontext);
584 context->Eip = EIP_sig(sigcontext);
585 context->Esp = ESP_sig(sigcontext);
586 context->SegCs = LOWORD(CS_sig(sigcontext));
587 context->SegDs = LOWORD(DS_sig(sigcontext));
588 context->SegEs = LOWORD(ES_sig(sigcontext));
589 context->SegSs = LOWORD(SS_sig(sigcontext));
593 /***********************************************************************
596 * Build a sigcontext from the register values.
598 static void restore_context( CONTEXT *context, SIGCONTEXT *sigcontext )
601 BOOL check_pending = TRUE;
603 /* check if exception occurred in vm86 mode */
604 if ((void *)EIP_sig(sigcontext) == vm86_return &&
605 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) &&
606 EAX_sig(sigcontext) == VM86_EAX)
608 unsigned int *stack = (unsigned int *)ESP_sig(sigcontext);
609 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
610 * (but we could also get it from teb->vm86_ptr) */
611 struct vm86plus_struct *vm86 = (struct vm86plus_struct *)stack[2];
612 restore_vm86_context( context, vm86 );
616 while (NtCurrentTeb()->dpmi_vif &&
617 !IS_SELECTOR_SYSTEM(context->SegCs) &&
618 !IS_SELECTOR_SYSTEM(context->SegSs) &&
622 * Executing DPMI code and virtual interrupts are enabled.
623 * We must block signals so that we can be safely check
624 * for pending asynchronous events. Return from signal handler
625 * will unblock signals again so it is is safe to do so.
628 check_pending = FALSE;
630 if (NtCurrentTeb()->vm86_pending)
632 EXCEPTION_RECORD rec;
634 rec.ExceptionAddress = (LPVOID)context->Eip;
635 rec.ExceptionCode = EXCEPTION_VM86_STI;
636 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
637 rec.ExceptionRecord = NULL;
638 rec.NumberParameters = 1;
639 rec.ExceptionInformation[0] = 0;
641 NtCurrentTeb()->vm86_pending = 0;
642 EXC_RtlRaiseException( &rec, context );
645 * EXC_RtlRaiseException has unblocked all signals
646 * and we must retry check for pending asynchronous
647 * events in order to prevent races.
649 check_pending = TRUE;
652 #endif /* __HAVE_VM86 */
654 EAX_sig(sigcontext) = context->Eax;
655 EBX_sig(sigcontext) = context->Ebx;
656 ECX_sig(sigcontext) = context->Ecx;
657 EDX_sig(sigcontext) = context->Edx;
658 ESI_sig(sigcontext) = context->Esi;
659 EDI_sig(sigcontext) = context->Edi;
660 EBP_sig(sigcontext) = context->Ebp;
661 EFL_sig(sigcontext) = context->EFlags;
662 EIP_sig(sigcontext) = context->Eip;
663 ESP_sig(sigcontext) = context->Esp;
664 CS_sig(sigcontext) = context->SegCs;
665 DS_sig(sigcontext) = context->SegDs;
666 ES_sig(sigcontext) = context->SegEs;
667 SS_sig(sigcontext) = context->SegSs;
669 FS_sig(sigcontext) = context->SegFs;
671 wine_set_fs( context->SegFs );
674 GS_sig(sigcontext) = context->SegGs;
676 wine_set_gs( context->SegGs );
681 /***********************************************************************
684 * Handler initialization when the full context is not needed.
686 static void init_handler( const SIGCONTEXT *sigcontext )
688 /* restore a proper %fs for the fault handler */
689 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) ||
690 !IS_SELECTOR_SYSTEM(SS_sig(sigcontext))) /* 16-bit mode */
692 wine_set_fs( signal_fs );
693 wine_set_gs( NtCurrentTeb()->gs_sel );
696 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
698 /* fetch the saved %fs on the stack */
699 wine_set_fs( *(unsigned int *)ESP_sig(sigcontext) );
700 wine_set_gs( NtCurrentTeb()->gs_sel );
702 #endif /* __HAVE_VM86 */
703 else /* 32-bit mode, get %fs at time of the fault */
706 wine_set_fs( FS_sig(sigcontext) );
709 wine_set_gs( GS_sig(sigcontext) );
715 /***********************************************************************
718 * Set the FPU context from a sigcontext.
720 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
723 if (FPU_sig(sigcontext))
725 context->FloatSave = *FPU_sig(sigcontext);
730 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
731 #endif /* __GNUC__ */
735 /***********************************************************************
738 * Restore the FPU context to a sigcontext.
740 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
742 /* reset the current interrupt status */
743 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
745 if (FPU_sig(sigcontext))
747 *FPU_sig(sigcontext) = context->FloatSave;
752 /* avoid nested exceptions */
753 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
754 #endif /* __GNUC__ */
758 /**********************************************************************
761 * Get the FPU exception code from the FPU status.
763 static inline DWORD get_fpu_code( const CONTEXT *context )
765 DWORD status = context->FloatSave.StatusWord;
767 if (status & 0x01) /* IE */
769 if (status & 0x40) /* SF */
770 return EXCEPTION_FLT_STACK_CHECK;
772 return EXCEPTION_FLT_INVALID_OPERATION;
774 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
775 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
776 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
777 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
778 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
779 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
783 /**********************************************************************
786 * Implementation of SIGSEGV handler.
788 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
790 EXCEPTION_RECORD rec;
792 rec.ExceptionRecord = NULL;
793 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
794 rec.ExceptionAddress = (LPVOID)context->Eip;
795 rec.NumberParameters = 0;
799 case T_OFLOW: /* Overflow exception */
800 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
802 case T_BOUND: /* Bound range exception */
803 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
805 case T_PRIVINFLT: /* Invalid opcode exception */
806 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
808 case T_STKFLT: /* Stack fault */
809 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
811 case T_SEGNPFLT: /* Segment not present exception */
812 case T_PROTFLT: /* General protection fault */
813 case T_UNKNOWN: /* Unknown fault code */
814 rec.ExceptionCode = err_code ? EXCEPTION_ACCESS_VIOLATION : EXCEPTION_PRIV_INSTRUCTION;
816 case T_PAGEFLT: /* Page fault */
818 if (!(rec.ExceptionCode = VIRTUAL_HandleFault( cr2 ))) return;
819 rec.NumberParameters = 2;
820 rec.ExceptionInformation[0] = (err_code & 2) != 0;
821 rec.ExceptionInformation[1] = (DWORD)cr2;
823 rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
826 case T_ALIGNFLT: /* Alignment check exception */
827 /* FIXME: pass through exception handler first? */
828 if (context->EFlags & 0x00040000)
830 /* Disable AC flag, return */
831 context->EFlags &= ~0x00040000;
834 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
837 ERR( "Got unexpected trap %d\n", trap_code );
839 case T_NMI: /* NMI interrupt */
840 case T_DNA: /* Device not available exception */
841 case T_DOUBLEFLT: /* Double fault exception */
842 case T_TSSFLT: /* Invalid TSS exception */
843 case T_RESERVED: /* Unknown exception */
844 case T_MCHK: /* Machine check exception */
846 case T_CACHEFLT: /* Cache flush exception */
848 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
851 EXC_RtlRaiseException( &rec, context );
855 /**********************************************************************
858 * Implementation of SIGTRAP handler.
860 static void do_trap( CONTEXT *context, int trap_code )
862 EXCEPTION_RECORD rec;
863 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
865 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
866 rec.ExceptionRecord = NULL;
867 rec.ExceptionAddress = (LPVOID)context->Eip;
868 rec.NumberParameters = 0;
872 case T_TRCTRAP: /* Single-step exception */
873 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
874 if (context->EFlags & 0x100)
876 context->EFlags &= ~0x100; /* clear single-step flag */
878 else /* hardware breakpoint, fetch the debug registers */
880 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
881 NtGetContextThread(GetCurrentThread(), context);
882 /* do we really have a bp from a debug register ?
883 * if not, then someone did a kill(SIGTRAP) on us, and we
884 * shall return a breakpoint, not a single step exception
886 if (!(context->Dr6 & 0xf))
887 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
890 case T_BPTFLT: /* Breakpoint exception */
891 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
894 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
904 EXC_RtlRaiseException( &rec, context );
906 if (dr0 != context->Dr0 || dr1 != context->Dr1 || dr2 != context->Dr2 ||
907 dr3 != context->Dr3 || dr6 != context->Dr6 || dr7 != context->Dr7)
909 /* the debug registers have changed, set the new values */
910 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
911 NtSetContextThread(GetCurrentThread(), context);
916 /**********************************************************************
919 * Implementation of SIGFPE handler
921 static void do_fpe( CONTEXT *context, int trap_code )
923 EXCEPTION_RECORD rec;
927 case T_DIVIDE: /* Division by zero exception */
928 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
930 case T_FPOPFLT: /* Coprocessor segment overrun */
931 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
933 case T_ARITHTRAP: /* Floating point exception */
934 case T_UNKNOWN: /* Unknown fault code */
935 rec.ExceptionCode = get_fpu_code( context );
938 ERR( "Got unexpected trap %d\n", trap_code );
939 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
942 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
943 rec.ExceptionRecord = NULL;
944 rec.ExceptionAddress = (LPVOID)context->Eip;
945 rec.NumberParameters = 0;
946 EXC_RtlRaiseException( &rec, context );
951 /**********************************************************************
954 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
956 static void set_vm86_pend( CONTEXT *context )
958 EXCEPTION_RECORD rec;
959 TEB *teb = NtCurrentTeb();
960 struct vm86plus_struct *vm86 = (struct vm86plus_struct*)(teb->vm86_ptr);
962 rec.ExceptionCode = EXCEPTION_VM86_STI;
963 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
964 rec.ExceptionRecord = NULL;
965 rec.NumberParameters = 1;
966 rec.ExceptionInformation[0] = 0;
968 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
969 teb->vm86_pending |= VIP_MASK;
970 /* see if we were in VM86 mode */
971 if (context->EFlags & 0x00020000)
973 /* seems so, also set flag in signal context */
974 if (context->EFlags & VIP_MASK) return;
975 context->EFlags |= VIP_MASK;
976 vm86->regs.eflags |= VIP_MASK; /* no exception recursion */
977 if (context->EFlags & VIF_MASK) {
978 /* VIF is set, throw exception */
979 teb->vm86_pending = 0;
980 teb->vm86_ptr = NULL;
981 rec.ExceptionAddress = (LPVOID)context->Eip;
982 EXC_RtlRaiseException( &rec, context );
984 * FIXME: EXC_RtlRaiseException has unblocked all signals.
985 * If we receive nested SIGUSR2 here, VM86 event
986 * handling may lock up!
988 teb->vm86_ptr = vm86;
993 /* not in VM86, but possibly setting up for it */
994 if (vm86->regs.eflags & VIP_MASK) return;
995 vm86->regs.eflags |= VIP_MASK;
996 if (((char*)context->Eip >= (char*)vm86_return) &&
997 ((char*)context->Eip <= (char*)vm86_return_end) &&
998 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
999 /* exiting from VM86, can't throw */
1002 if (vm86->regs.eflags & VIF_MASK) {
1003 /* VIF is set, throw exception */
1005 teb->vm86_pending = 0;
1006 teb->vm86_ptr = NULL;
1007 save_vm86_context( &vcontext, vm86 );
1008 rec.ExceptionAddress = (LPVOID)vcontext.Eip;
1009 EXC_RtlRaiseException( &rec, &vcontext );
1011 * FIXME: EXC_RtlRaiseException has unblocked all signals.
1012 * If we receive nested SIGUSR2 here, VM86 event
1013 * handling may lock up!
1015 teb->vm86_ptr = vm86;
1016 restore_vm86_context( &vcontext, vm86 );
1022 /**********************************************************************
1025 * Handler for SIGUSR2.
1026 * We use it to signal that the running __wine_enter_vm86() should
1027 * immediately set VIP_MASK, causing pending events to be handled
1028 * as early as possible.
1030 static HANDLER_DEF(usr2_handler)
1034 save_context( &context, HANDLER_CONTEXT );
1035 set_vm86_pend( &context );
1036 restore_context( &context, HANDLER_CONTEXT );
1038 #endif /* __HAVE_VM86 */
1041 /**********************************************************************
1044 * Handler for SIGSEGV and related errors.
1046 static HANDLER_DEF(segv_handler)
1051 save_context( &context, HANDLER_CONTEXT );
1052 #ifdef FAULT_ADDRESS
1053 cr2 = FAULT_ADDRESS;
1057 do_segv( &context, get_trap_code(HANDLER_CONTEXT), cr2, get_error_code(HANDLER_CONTEXT) );
1058 restore_context( &context, HANDLER_CONTEXT );
1062 /**********************************************************************
1065 * Handler for SIGTRAP.
1067 static HANDLER_DEF(trap_handler)
1070 save_context( &context, HANDLER_CONTEXT );
1071 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
1072 restore_context( &context, HANDLER_CONTEXT );
1076 /**********************************************************************
1079 * Handler for SIGFPE.
1081 static HANDLER_DEF(fpe_handler)
1084 save_fpu( &context, HANDLER_CONTEXT );
1085 save_context( &context, HANDLER_CONTEXT );
1086 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
1087 restore_context( &context, HANDLER_CONTEXT );
1088 restore_fpu( &context, HANDLER_CONTEXT );
1092 /**********************************************************************
1095 * Handler for SIGINT.
1097 static HANDLER_DEF(int_handler)
1099 init_handler( HANDLER_CONTEXT );
1100 if (!dispatch_signal(SIGINT))
1102 EXCEPTION_RECORD rec;
1105 save_context( &context, HANDLER_CONTEXT );
1106 rec.ExceptionCode = CONTROL_C_EXIT;
1107 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1108 rec.ExceptionRecord = NULL;
1109 rec.ExceptionAddress = (LPVOID)context.Eip;
1110 rec.NumberParameters = 0;
1111 EXC_RtlRaiseException( &rec, &context );
1112 restore_context( &context, HANDLER_CONTEXT );
1116 /**********************************************************************
1119 * Handler for SIGABRT.
1121 static HANDLER_DEF(abrt_handler)
1123 EXCEPTION_RECORD rec;
1126 save_context( &context, HANDLER_CONTEXT );
1127 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
1128 rec.ExceptionFlags = EH_NONCONTINUABLE;
1129 rec.ExceptionRecord = NULL;
1130 rec.ExceptionAddress = (LPVOID)context.Eip;
1131 rec.NumberParameters = 0;
1132 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
1133 restore_context( &context, HANDLER_CONTEXT );
1137 /**********************************************************************
1140 * Handler for SIGTERM.
1142 static HANDLER_DEF(term_handler)
1144 init_handler( HANDLER_CONTEXT );
1145 SYSDEPS_AbortThread(0);
1149 /**********************************************************************
1152 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1154 static HANDLER_DEF(usr1_handler)
1156 LARGE_INTEGER timeout;
1158 init_handler( HANDLER_CONTEXT );
1159 /* wait with 0 timeout, will only return once the thread is no longer suspended */
1160 timeout.QuadPart = 0;
1161 NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout );
1165 /***********************************************************************
1168 * Set a signal handler
1170 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
1172 struct sigaction sig_act;
1175 if (!have_sigaltstack)
1177 struct kernel_sigaction sig_act;
1178 sig_act.ksa_handler = func;
1179 sig_act.ksa_flags = SA_RESTART;
1180 sig_act.ksa_mask = (1 << (SIGINT-1)) |
1182 /* point to the top of the signal stack */
1183 sig_act.ksa_restorer = (char *)NtCurrentTeb()->DeallocationStack + SIGNAL_STACK_SIZE;
1184 return wine_sigaction( sig, &sig_act, NULL );
1187 sig_act.sa_handler = func;
1188 sigemptyset( &sig_act.sa_mask );
1189 sigaddset( &sig_act.sa_mask, SIGINT );
1190 sigaddset( &sig_act.sa_mask, SIGUSR2 );
1192 #if defined(linux) || defined(__NetBSD__)
1193 sig_act.sa_flags = SA_RESTART;
1194 #elif defined (__svr4__) || defined(_SCO_DS)
1195 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1197 sig_act.sa_flags = 0;
1201 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1203 return sigaction( sig, &sig_act, NULL );
1207 /***********************************************************************
1208 * __wine_set_signal_handler (NTDLL.@)
1210 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1212 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
1213 if (handlers[sig] != NULL) return -2;
1214 handlers[sig] = wsh;
1219 /***********************************************************************
1220 * __wine_set_signal_fs (NTDLL.@)
1222 void __wine_set_signal_fs( unsigned int fs )
1228 /**********************************************************************
1231 BOOL SIGNAL_Init(void)
1233 int have_sigaltstack = 0;
1235 #ifdef HAVE_SIGALTSTACK
1236 struct sigaltstack ss;
1237 ss.ss_sp = NtCurrentTeb()->DeallocationStack;
1238 ss.ss_size = SIGNAL_STACK_SIZE;
1240 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1242 /* sigaltstack may fail because the kernel is too old, or
1243 because glibc is brain-dead. In the latter case a
1244 direct system call should succeed. */
1245 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1247 #endif /* HAVE_SIGALTSTACK */
1249 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1250 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1251 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1252 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1253 if (set_handler( SIGABRT, have_sigaltstack, (void (*)())abrt_handler ) == -1) goto error;
1254 if (set_handler( SIGTERM, have_sigaltstack, (void (*)())term_handler ) == -1) goto error;
1255 if (set_handler( SIGUSR1, have_sigaltstack, (void (*)())usr1_handler ) == -1) goto error;
1257 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1260 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1264 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1270 perror("sigaction");
1275 /**********************************************************************
1278 * Block the async signals.
1280 void SIGNAL_Block(void)
1284 sigemptyset( &block_set );
1285 sigaddset( &block_set, SIGIO );
1286 sigaddset( &block_set, SIGHUP );
1287 sigaddset( &block_set, SIGUSR1 );
1288 sigaddset( &block_set, SIGUSR2 );
1289 sigprocmask( SIG_BLOCK, &block_set, NULL );
1293 /***********************************************************************
1296 * Unblock signals. Called from EXC_RtlRaiseException.
1298 void SIGNAL_Unblock(void)
1302 sigfillset( &all_sigs );
1303 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
1307 /**********************************************************************
1310 * Restore the default handlers.
1312 void SIGNAL_Reset(void)
1314 signal( SIGINT, SIG_DFL );
1315 signal( SIGFPE, SIG_DFL );
1316 signal( SIGSEGV, SIG_DFL );
1317 signal( SIGILL, SIG_DFL );
1318 signal( SIGABRT, SIG_DFL );
1319 signal( SIGTERM, SIG_DFL );
1321 signal( SIGBUS, SIG_DFL );
1324 signal( SIGTRAP, SIG_DFL );
1330 /**********************************************************************
1331 * __wine_enter_vm86 (NTDLL.@)
1333 * Enter vm86 mode with the specified register context.
1335 void __wine_enter_vm86( CONTEXT *context )
1337 EXCEPTION_RECORD rec;
1338 TEB *teb = NtCurrentTeb();
1340 struct vm86plus_struct vm86;
1342 memset( &vm86, 0, sizeof(vm86) );
1345 restore_vm86_context( context, &vm86 );
1346 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1347 * so save it on entry, just in case */
1348 teb->vm86_pending |= (context->EFlags & VIP_MASK);
1349 /* Work around race conditions with signal handler
1350 * (avoiding sigprocmask for performance reasons) */
1351 teb->vm86_ptr = &vm86;
1352 vm86.regs.eflags |= teb->vm86_pending;
1353 /* Check for VIF|VIP here, since vm86_enter doesn't */
1354 if ((vm86.regs.eflags & (VIF_MASK|VIP_MASK)) == (VIF_MASK|VIP_MASK)) {
1355 teb->vm86_ptr = NULL;
1356 teb->vm86_pending = 0;
1357 context->EFlags |= VIP_MASK;
1358 rec.ExceptionCode = EXCEPTION_VM86_STI;
1359 rec.ExceptionInformation[0] = 0;
1365 res = vm86_enter( &teb->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1371 } while (VM86_TYPE(res) == VM86_SIGNAL);
1373 save_vm86_context( context, &vm86 );
1374 context->EFlags |= teb->vm86_pending;
1376 switch(VM86_TYPE(res))
1378 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1379 do_segv( context, T_PROTFLT, 0, 0 );
1381 case VM86_TRAP: /* return due to DOS-debugger request */
1382 do_trap( context, VM86_ARG(res) );
1384 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1385 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1387 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1388 teb->vm86_pending = 0;
1389 rec.ExceptionCode = EXCEPTION_VM86_STI;
1391 case VM86_PICRETURN: /* return due to pending PIC request */
1392 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1395 ERR( "unhandled result from vm86 mode %x\n", res );
1398 rec.ExceptionInformation[0] = VM86_ARG(res);
1400 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1401 rec.ExceptionRecord = NULL;
1402 rec.ExceptionAddress = (LPVOID)context->Eip;
1403 rec.NumberParameters = 1;
1404 EXC_RtlRaiseException( &rec, context );
1408 #else /* __HAVE_VM86 */
1409 /**********************************************************************
1410 * __wine_enter_vm86 (NTDLL.@)
1412 void __wine_enter_vm86( CONTEXT *context )
1414 MESSAGE("vm86 mode not supported on this platform\n");
1416 #endif /* __HAVE_VM86 */
1418 /**********************************************************************
1419 * DbgBreakPoint (NTDLL.@)
1421 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1423 /**********************************************************************
1424 * DbgUserBreakPoint (NTDLL.@)
1426 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1428 #endif /* __i386__ */