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"
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 */
152 ".globl " __ASM_NAME("vm86_return") "\n\t"
153 __ASM_FUNC("vm86_return") "\n"
154 __ASM_NAME("vm86_return") ":\n\t"
159 "testl %eax,%eax\n\t"
161 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
162 "je " __ASM_NAME("vm86_enter") "\n\t"
164 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
166 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
167 __ASM_FUNC("vm86_return_end") "\n"
168 __ASM_NAME("vm86_return_end") ":\n\t"
171 #ifdef HAVE_SYS_VM86_H
179 #define EAX_sig(context) ((context)->tf_eax)
180 #define EBX_sig(context) ((context)->tf_ebx)
181 #define ECX_sig(context) ((context)->tf_ecx)
182 #define EDX_sig(context) ((context)->tf_edx)
183 #define ESI_sig(context) ((context)->tf_esi)
184 #define EDI_sig(context) ((context)->tf_edi)
185 #define EBP_sig(context) ((context)->tf_ebp)
187 #define CS_sig(context) ((context)->tf_cs)
188 #define DS_sig(context) ((context)->tf_ds)
189 #define ES_sig(context) ((context)->tf_es)
190 #define SS_sig(context) ((context)->tf_ss)
192 #include <machine/frame.h>
193 typedef struct trapframe SIGCONTEXT;
195 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
196 #define HANDLER_CONTEXT __context
198 #define EFL_sig(context) ((context)->tf_eflags)
200 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
201 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
205 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
207 typedef struct sigcontext SIGCONTEXT;
209 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
210 #define HANDLER_CONTEXT __context
214 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
217 #include <sys/regset.h>
221 #include <sys/ucontext.h>
223 typedef struct ucontext SIGCONTEXT;
225 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
226 #define HANDLER_CONTEXT __context
228 #endif /* svr4 || SCO_DS */
234 unsigned long ContextFlags;
235 FLOATING_SAVE_AREA sc_float;
240 unsigned long sc_edi;
241 unsigned long sc_esi;
242 unsigned long sc_eax;
243 unsigned long sc_ebx;
244 unsigned long sc_ecx;
245 unsigned long sc_edx;
246 unsigned long sc_ebp;
247 unsigned long sc_eip;
249 unsigned long sc_eflags;
250 unsigned long sc_esp;
258 /* FIXME: This section is just here so it can compile, it's most likely
259 * completely wrong. */
263 unsigned short sc_gs, __gsh;
264 unsigned short sc_fs, __fsh;
265 unsigned short sc_es, __esh;
266 unsigned short sc_ds, __dsh;
267 unsigned long sc_edi;
268 unsigned long sc_esi;
269 unsigned long sc_ebp;
270 unsigned long sc_esp;
271 unsigned long sc_ebx;
272 unsigned long sc_edx;
273 unsigned long sc_ecx;
274 unsigned long sc_eax;
275 unsigned long sc_trapno;
276 unsigned long sc_err;
277 unsigned long sc_eip;
278 unsigned short sc_cs, __csh;
279 unsigned long sc_eflags;
280 unsigned long esp_at_signal;
281 unsigned short sc_ss, __ssh;
283 unsigned long oldmask;
287 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
288 #define HANDLER_CONTEXT (&__context)
290 #endif /* __CYGWIN__ */
292 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
293 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
295 #define EAX_sig(context) ((context)->sc_eax)
296 #define EBX_sig(context) ((context)->sc_ebx)
297 #define ECX_sig(context) ((context)->sc_ecx)
298 #define EDX_sig(context) ((context)->sc_edx)
299 #define ESI_sig(context) ((context)->sc_esi)
300 #define EDI_sig(context) ((context)->sc_edi)
301 #define EBP_sig(context) ((context)->sc_ebp)
303 #define CS_sig(context) ((context)->sc_cs)
304 #define DS_sig(context) ((context)->sc_ds)
305 #define ES_sig(context) ((context)->sc_es)
306 #define SS_sig(context) ((context)->sc_ss)
308 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
309 * saved by the exception handling. duh.
310 * Actually they are in -current (have been for a while), and that
311 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
312 * If you're running a system from the -stable branch older than that,
313 * like a 3.3-RELEASE, grab the patch from the ports tree:
314 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
315 * (If its not yet there when you look, go here:
316 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
319 #define FS_sig(context) ((context)->sc_fs)
320 #define GS_sig(context) ((context)->sc_gs)
324 #define FS_sig(context) ((context)->sc_fs)
325 #define GS_sig(context) ((context)->sc_gs)
326 #define CR2_sig(context) ((context)->cr2)
327 #define TRAP_sig(context) ((context)->sc_trapno)
328 #define ERROR_sig(context) ((context)->sc_err)
329 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
333 #define EFL_sig(context) ((context)->sc_eflags)
335 #define EFL_sig(context) ((context)->sc_efl)
336 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
337 #define CR2_sig(context) ((context)->sc_err)
338 #define TRAP_sig(context) ((context)->sc_trapno)
341 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
342 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
344 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
346 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
352 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
353 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
354 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
355 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
356 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
357 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
358 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
360 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
361 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
362 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
363 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
365 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
366 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
368 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
370 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
372 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
374 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
377 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
380 #endif /* svr4 || SCO_DS */
383 /* exception code definitions (already defined by FreeBSD/NetBSD) */
384 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
385 #define T_DIVIDE 0 /* Division by zero exception */
386 #define T_TRCTRAP 1 /* Single-step exception */
387 #define T_NMI 2 /* NMI interrupt */
388 #define T_BPTFLT 3 /* Breakpoint exception */
389 #define T_OFLOW 4 /* Overflow exception */
390 #define T_BOUND 5 /* Bound range exception */
391 #define T_PRIVINFLT 6 /* Invalid opcode exception */
392 #define T_DNA 7 /* Device not available exception */
393 #define T_DOUBLEFLT 8 /* Double fault exception */
394 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
395 #define T_TSSFLT 10 /* Invalid TSS exception */
396 #define T_SEGNPFLT 11 /* Segment not present exception */
397 #define T_STKFLT 12 /* Stack fault */
398 #define T_PROTFLT 13 /* General protection fault */
399 #define T_PAGEFLT 14 /* Page fault */
400 #define T_RESERVED 15 /* Unknown exception */
401 #define T_ARITHTRAP 16 /* Floating point exception */
402 #define T_ALIGNFLT 17 /* Alignment check exception */
403 #define T_MCHK 18 /* Machine check exception */
404 #define T_CACHEFLT 19 /* Cache flush exception */
406 #if defined(__NetBSD__)
407 #define T_MCHK 19 /* Machine check exception */
410 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
412 #include "wine/exception.h"
413 #include "stackframe.h"
416 #include "syslevel.h"
417 #include "wine/debug.h"
419 WINE_DEFAULT_DEBUG_CHANNEL(seh);
421 typedef int (*wine_signal_handler)(unsigned int sig);
423 static wine_signal_handler handlers[256];
425 static sigset_t all_sigs;
428 /***********************************************************************
431 inline static int dispatch_signal(unsigned int sig)
433 if (handlers[sig] == NULL) return 0;
434 return handlers[sig](sig);
438 /***********************************************************************
441 * Get the trap code for a signal.
443 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
446 return TRAP_sig(sigcontext);
448 return T_UNKNOWN; /* unknown trap code */
452 /***********************************************************************
455 * Get the error code for a signal.
457 static inline int get_error_code( const SIGCONTEXT *sigcontext )
460 return ERROR_sig(sigcontext);
466 /***********************************************************************
469 * Get the CR2 value for a signal.
471 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
474 return (void *)CR2_sig(sigcontext);
482 /***********************************************************************
485 * Set the register values from a vm86 structure.
487 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
489 context->Eax = vm86->regs.eax;
490 context->Ebx = vm86->regs.ebx;
491 context->Ecx = vm86->regs.ecx;
492 context->Edx = vm86->regs.edx;
493 context->Esi = vm86->regs.esi;
494 context->Edi = vm86->regs.edi;
495 context->Esp = vm86->regs.esp;
496 context->Ebp = vm86->regs.ebp;
497 context->Eip = vm86->regs.eip;
498 context->SegCs = vm86->regs.cs;
499 context->SegDs = vm86->regs.ds;
500 context->SegEs = vm86->regs.es;
501 context->SegFs = vm86->regs.fs;
502 context->SegGs = vm86->regs.gs;
503 context->SegSs = vm86->regs.ss;
504 context->EFlags = vm86->regs.eflags;
508 /***********************************************************************
509 * restore_vm86_context
511 * Build a vm86 structure from the register values.
513 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
515 vm86->regs.eax = context->Eax;
516 vm86->regs.ebx = context->Ebx;
517 vm86->regs.ecx = context->Ecx;
518 vm86->regs.edx = context->Edx;
519 vm86->regs.esi = context->Esi;
520 vm86->regs.edi = context->Edi;
521 vm86->regs.esp = context->Esp;
522 vm86->regs.ebp = context->Ebp;
523 vm86->regs.eip = context->Eip;
524 vm86->regs.cs = context->SegCs;
525 vm86->regs.ds = context->SegDs;
526 vm86->regs.es = context->SegEs;
527 vm86->regs.fs = context->SegFs;
528 vm86->regs.gs = context->SegGs;
529 vm86->regs.ss = context->SegSs;
530 vm86->regs.eflags = context->EFlags;
532 #endif /* __HAVE_VM86 */
535 /***********************************************************************
538 * Set the register values from a sigcontext.
540 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
543 /* get %fs at time of the fault */
545 fs = FS_sig(sigcontext);
551 /* now restore a proper %fs for the fault handler */
552 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) ||
553 !IS_SELECTOR_SYSTEM(SS_sig(sigcontext))) /* 16-bit mode */
556 * Win16 or DOS protected mode. Note that during switch
557 * from 16-bit mode to linear mode, CS may be set to system
558 * segment before FS is restored. Fortunately, in this case
559 * SS is still non-system segment. This is why both CS and SS
562 fs = SYSLEVEL_Win16CurrentTeb;
565 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
567 /* fetch the saved %fs on the stack */
568 fs = *(unsigned int *)ESP_sig(sigcontext);
569 if (EAX_sig(sigcontext) == VM86_EAX) {
570 struct vm86plus_struct *vm86;
572 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
573 * (but we could also get if from teb->vm86_ptr) */
574 vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
575 /* get context from vm86 struct */
576 save_vm86_context( context, vm86 );
580 #endif /* __HAVE_VM86 */
584 context->Eax = EAX_sig(sigcontext);
585 context->Ebx = EBX_sig(sigcontext);
586 context->Ecx = ECX_sig(sigcontext);
587 context->Edx = EDX_sig(sigcontext);
588 context->Esi = ESI_sig(sigcontext);
589 context->Edi = EDI_sig(sigcontext);
590 context->Ebp = EBP_sig(sigcontext);
591 context->EFlags = EFL_sig(sigcontext);
592 context->Eip = EIP_sig(sigcontext);
593 context->Esp = ESP_sig(sigcontext);
594 context->SegCs = LOWORD(CS_sig(sigcontext));
595 context->SegDs = LOWORD(DS_sig(sigcontext));
596 context->SegEs = LOWORD(ES_sig(sigcontext));
597 context->SegSs = LOWORD(SS_sig(sigcontext));
599 context->SegGs = LOWORD(GS_sig(sigcontext));
601 context->SegGs = wine_get_gs();
606 /***********************************************************************
609 * Build a sigcontext from the register values.
611 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
614 /* check if exception occurred in vm86 mode */
615 if ((void *)EIP_sig(sigcontext) == vm86_return &&
616 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) &&
617 EAX_sig(sigcontext) == VM86_EAX)
619 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
620 * (but we could also get it from teb->vm86_ptr) */
621 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
622 restore_vm86_context( context, vm86 );
625 #endif /* __HAVE_VM86 */
627 EAX_sig(sigcontext) = context->Eax;
628 EBX_sig(sigcontext) = context->Ebx;
629 ECX_sig(sigcontext) = context->Ecx;
630 EDX_sig(sigcontext) = context->Edx;
631 ESI_sig(sigcontext) = context->Esi;
632 EDI_sig(sigcontext) = context->Edi;
633 EBP_sig(sigcontext) = context->Ebp;
634 EFL_sig(sigcontext) = context->EFlags;
635 EIP_sig(sigcontext) = context->Eip;
636 ESP_sig(sigcontext) = context->Esp;
637 CS_sig(sigcontext) = context->SegCs;
638 DS_sig(sigcontext) = context->SegDs;
639 ES_sig(sigcontext) = context->SegEs;
640 SS_sig(sigcontext) = context->SegSs;
642 FS_sig(sigcontext) = context->SegFs;
644 wine_set_fs( context->SegFs );
647 GS_sig(sigcontext) = context->SegGs;
649 wine_set_gs( context->SegGs );
654 /***********************************************************************
657 * Set the FPU context from a sigcontext.
659 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
662 if (FPU_sig(sigcontext))
664 context->FloatSave = *FPU_sig(sigcontext);
669 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
670 #endif /* __GNUC__ */
674 /***********************************************************************
677 * Restore the FPU context to a sigcontext.
679 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
681 /* reset the current interrupt status */
682 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
684 if (FPU_sig(sigcontext))
686 *FPU_sig(sigcontext) = context->FloatSave;
691 /* avoid nested exceptions */
692 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
693 #endif /* __GNUC__ */
697 /**********************************************************************
700 * Get the FPU exception code from the FPU status.
702 static inline DWORD get_fpu_code( const CONTEXT *context )
704 DWORD status = context->FloatSave.StatusWord;
706 if (status & 0x01) /* IE */
708 if (status & 0x40) /* SF */
709 return EXCEPTION_FLT_STACK_CHECK;
711 return EXCEPTION_FLT_INVALID_OPERATION;
713 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
714 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
715 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
716 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
717 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
718 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
722 /***********************************************************************
725 * Unblock signals. Called from EXC_RtlRaiseException.
727 void SIGNAL_Unblock( void )
729 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
733 /**********************************************************************
736 * Implementation of SIGSEGV handler.
738 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
740 EXCEPTION_RECORD rec;
741 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
744 /* we want the page-fault case to be fast */
745 if (trap_code == T_PAGEFLT)
746 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
749 rec.ExceptionRecord = NULL;
750 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
751 rec.ExceptionAddress = (LPVOID)context->Eip;
752 rec.NumberParameters = 0;
756 case T_OFLOW: /* Overflow exception */
757 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
759 case T_BOUND: /* Bound range exception */
760 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
762 case T_PRIVINFLT: /* Invalid opcode exception */
763 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
765 case T_STKFLT: /* Stack fault */
766 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
768 case T_SEGNPFLT: /* Segment not present exception */
769 case T_PROTFLT: /* General protection fault */
770 case T_UNKNOWN: /* Unknown fault code */
771 if (INSTR_EmulateInstruction( context )) return;
772 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
774 case T_PAGEFLT: /* Page fault */
776 rec.NumberParameters = 2;
777 rec.ExceptionInformation[0] = (err_code & 2) != 0;
778 rec.ExceptionInformation[1] = (DWORD)cr2;
780 rec.ExceptionCode = page_fault_code;
782 case T_ALIGNFLT: /* Alignment check exception */
783 /* FIXME: pass through exception handler first? */
784 if (context->EFlags & 0x00040000)
786 /* Disable AC flag, return */
787 context->EFlags &= ~0x00040000;
790 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
793 ERR( "Got unexpected trap %d\n", trap_code );
795 case T_NMI: /* NMI interrupt */
796 case T_DNA: /* Device not available exception */
797 case T_DOUBLEFLT: /* Double fault exception */
798 case T_TSSFLT: /* Invalid TSS exception */
799 case T_RESERVED: /* Unknown exception */
800 case T_MCHK: /* Machine check exception */
802 case T_CACHEFLT: /* Cache flush exception */
804 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
807 EXC_RtlRaiseException( &rec, context );
811 /**********************************************************************
814 * Implementation of SIGTRAP handler.
816 static void do_trap( CONTEXT *context, int trap_code )
818 EXCEPTION_RECORD rec;
820 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
821 rec.ExceptionRecord = NULL;
822 rec.ExceptionAddress = (LPVOID)context->Eip;
823 rec.NumberParameters = 0;
827 case T_TRCTRAP: /* Single-step exception */
828 if (context->EFlags & 0x100)
830 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
831 context->EFlags &= ~0x100; /* clear single-step flag */
835 /* likely we get this because of a kill(SIGTRAP) on ourself,
836 * so send a bp exception instead of a single step exception
838 TRACE("Spurious single step trap => breakpoint simulation\n");
839 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
842 case T_BPTFLT: /* Breakpoint exception */
843 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
846 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
849 EXC_RtlRaiseException( &rec, context );
853 /**********************************************************************
856 * Implementation of SIGFPE handler
858 static void do_fpe( CONTEXT *context, int trap_code )
860 EXCEPTION_RECORD rec;
864 case T_DIVIDE: /* Division by zero exception */
865 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
867 case T_FPOPFLT: /* Coprocessor segment overrun */
868 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
870 case T_ARITHTRAP: /* Floating point exception */
871 case T_UNKNOWN: /* Unknown fault code */
872 rec.ExceptionCode = get_fpu_code( context );
875 ERR( "Got unexpected trap %d\n", trap_code );
876 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
879 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
880 rec.ExceptionRecord = NULL;
881 rec.ExceptionAddress = (LPVOID)context->Eip;
882 rec.NumberParameters = 0;
883 EXC_RtlRaiseException( &rec, context );
888 /**********************************************************************
891 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
893 static void set_vm86_pend( CONTEXT *context )
895 EXCEPTION_RECORD rec;
896 TEB *teb = NtCurrentTeb();
897 struct vm86plus_struct *vm86 = (struct vm86plus_struct*)(teb->vm86_ptr);
899 rec.ExceptionCode = EXCEPTION_VM86_STI;
900 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
901 rec.ExceptionRecord = NULL;
902 rec.NumberParameters = 1;
903 rec.ExceptionInformation[0] = 0;
905 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
906 teb->vm86_pending |= VIP_MASK;
907 /* see if we were in VM86 mode */
908 if (context->EFlags & 0x00020000)
910 /* seems so, also set flag in signal context */
911 if (context->EFlags & VIP_MASK) return;
912 context->EFlags |= VIP_MASK;
913 vm86->regs.eflags |= VIP_MASK; /* no exception recursion */
914 if (context->EFlags & VIF_MASK) {
915 /* VIF is set, throw exception */
916 teb->vm86_pending = 0;
917 teb->vm86_ptr = NULL;
918 rec.ExceptionAddress = (LPVOID)context->Eip;
919 EXC_RtlRaiseException( &rec, context );
920 teb->vm86_ptr = vm86;
925 /* not in VM86, but possibly setting up for it */
926 if (vm86->regs.eflags & VIP_MASK) return;
927 vm86->regs.eflags |= VIP_MASK;
928 if (((char*)context->Eip >= (char*)vm86_return) &&
929 ((char*)context->Eip <= (char*)vm86_return_end) &&
930 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
931 /* exiting from VM86, can't throw */
934 if (vm86->regs.eflags & VIF_MASK) {
935 /* VIF is set, throw exception */
937 teb->vm86_pending = 0;
938 teb->vm86_ptr = NULL;
939 save_vm86_context( &vcontext, vm86 );
940 rec.ExceptionAddress = (LPVOID)vcontext.Eip;
941 EXC_RtlRaiseException( &rec, &vcontext );
942 teb->vm86_ptr = vm86;
943 restore_vm86_context( &vcontext, vm86 );
949 /**********************************************************************
952 * Handler for SIGUSR2.
953 * We use it to signal that the running __wine_enter_vm86() should
954 * immediately set VIP_MASK, causing pending events to be handled
955 * as early as possible.
957 static HANDLER_DEF(usr2_handler)
961 save_context( &context, HANDLER_CONTEXT );
962 set_vm86_pend( &context );
963 restore_context( &context, HANDLER_CONTEXT );
967 /**********************************************************************
970 * Handler for SIGALRM.
971 * Increases the alarm counter and sets the vm86 pending flag.
973 static HANDLER_DEF(alrm_handler)
977 save_context( &context, HANDLER_CONTEXT );
978 NtCurrentTeb()->alarms++;
979 set_vm86_pend( &context );
980 restore_context( &context, HANDLER_CONTEXT );
982 #endif /* __HAVE_VM86 */
985 /**********************************************************************
988 * Handler for SIGSEGV and related errors.
990 static HANDLER_DEF(segv_handler)
993 save_context( &context, HANDLER_CONTEXT );
994 do_segv( &context, get_trap_code(HANDLER_CONTEXT),
995 get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
996 restore_context( &context, HANDLER_CONTEXT );
1000 /**********************************************************************
1003 * Handler for SIGTRAP.
1005 static HANDLER_DEF(trap_handler)
1008 save_context( &context, HANDLER_CONTEXT );
1009 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
1010 restore_context( &context, HANDLER_CONTEXT );
1014 /**********************************************************************
1017 * Handler for SIGFPE.
1019 static HANDLER_DEF(fpe_handler)
1022 save_fpu( &context, HANDLER_CONTEXT );
1023 save_context( &context, HANDLER_CONTEXT );
1024 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
1025 restore_context( &context, HANDLER_CONTEXT );
1026 restore_fpu( &context, HANDLER_CONTEXT );
1030 /**********************************************************************
1033 * Handler for SIGINT.
1035 static HANDLER_DEF(int_handler)
1037 if (!dispatch_signal(SIGINT))
1039 EXCEPTION_RECORD rec;
1042 save_context( &context, HANDLER_CONTEXT );
1043 rec.ExceptionCode = CONTROL_C_EXIT;
1044 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1045 rec.ExceptionRecord = NULL;
1046 rec.ExceptionAddress = (LPVOID)context.Eip;
1047 rec.NumberParameters = 0;
1048 EXC_RtlRaiseException( &rec, &context );
1049 restore_context( &context, HANDLER_CONTEXT );
1053 /**********************************************************************
1056 * Handler for SIGABRT.
1058 static HANDLER_DEF(abrt_handler)
1060 EXCEPTION_RECORD rec;
1063 save_context( &context, HANDLER_CONTEXT );
1064 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
1065 rec.ExceptionFlags = EH_NONCONTINUABLE;
1066 rec.ExceptionRecord = NULL;
1067 rec.ExceptionAddress = (LPVOID)context.Eip;
1068 rec.NumberParameters = 0;
1069 EXC_RtlRaiseException( &rec, &context ); /* Should never return.. */
1070 restore_context( &context, HANDLER_CONTEXT );
1074 /***********************************************************************
1077 * Set a signal handler
1079 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
1081 struct sigaction sig_act;
1084 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
1086 struct kernel_sigaction sig_act;
1087 sig_act.ksa_handler = func;
1088 sig_act.ksa_flags = SA_RESTART;
1089 sig_act.ksa_mask = (1 << (SIGINT-1)) |
1090 (1 << (SIGUSR2-1)) |
1092 /* point to the top of the stack */
1093 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
1094 return wine_sigaction( sig, &sig_act, NULL );
1097 sig_act.sa_handler = func;
1098 sigemptyset( &sig_act.sa_mask );
1099 sigaddset( &sig_act.sa_mask, SIGINT );
1100 sigaddset( &sig_act.sa_mask, SIGUSR2 );
1101 sigaddset( &sig_act.sa_mask, SIGALRM );
1104 sig_act.sa_flags = SA_RESTART;
1105 #elif defined (__svr4__) || defined(_SCO_DS)
1106 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1108 sig_act.sa_flags = 0;
1112 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1114 return sigaction( sig, &sig_act, NULL );
1118 /***********************************************************************
1119 * __wine_set_signal_handler (NTDLL.@)
1121 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1123 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
1124 if (handlers[sig] != NULL) return -2;
1125 handlers[sig] = wsh;
1130 /**********************************************************************
1133 BOOL SIGNAL_Init(void)
1135 int have_sigaltstack = 0;
1137 #ifdef HAVE_SIGALTSTACK
1138 struct sigaltstack ss;
1139 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
1141 ss.ss_size = SIGNAL_STACK_SIZE;
1143 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1145 /* sigaltstack may fail because the kernel is too old, or
1146 because glibc is brain-dead. In the latter case a
1147 direct system call should succeed. */
1148 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1151 #endif /* HAVE_SIGALTSTACK */
1153 sigfillset( &all_sigs );
1155 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1156 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1157 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1158 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1159 if (set_handler( SIGABRT, have_sigaltstack, (void (*)())abrt_handler ) == -1) goto error;
1161 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1164 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1168 if (set_handler( SIGALRM, have_sigaltstack, (void (*)())alrm_handler ) == -1) goto error;
1169 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1175 perror("sigaction");
1180 /**********************************************************************
1183 void SIGNAL_Reset(void)
1187 /* block the async signals */
1188 sigemptyset( &block_set );
1189 sigaddset( &block_set, SIGALRM );
1190 sigaddset( &block_set, SIGIO );
1191 sigaddset( &block_set, SIGHUP );
1192 sigaddset( &block_set, SIGUSR2 );
1193 sigprocmask( SIG_BLOCK, &block_set, NULL );
1195 /* restore default handlers */
1196 signal( SIGINT, SIG_DFL );
1197 signal( SIGFPE, SIG_DFL );
1198 signal( SIGSEGV, SIG_DFL );
1199 signal( SIGILL, SIG_DFL );
1201 signal( SIGBUS, SIG_DFL );
1204 signal( SIGTRAP, SIG_DFL );
1210 /**********************************************************************
1211 * __wine_enter_vm86 (NTDLL.@)
1213 * Enter vm86 mode with the specified register context.
1215 void __wine_enter_vm86( CONTEXT *context )
1217 EXCEPTION_RECORD rec;
1218 TEB *teb = NtCurrentTeb();
1220 struct vm86plus_struct vm86;
1222 memset( &vm86, 0, sizeof(vm86) );
1225 restore_vm86_context( context, &vm86 );
1226 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1227 * so save it on entry, just in case */
1228 teb->vm86_pending |= (context->EFlags & VIP_MASK);
1229 /* Work around race conditions with signal handler
1230 * (avoiding sigprocmask for performance reasons) */
1231 teb->vm86_ptr = &vm86;
1232 vm86.regs.eflags |= teb->vm86_pending;
1233 /* Check for VIF|VIP here, since vm86_enter doesn't */
1234 if ((vm86.regs.eflags & (VIF_MASK|VIP_MASK)) == (VIF_MASK|VIP_MASK)) {
1235 teb->vm86_ptr = NULL;
1236 teb->vm86_pending = 0;
1237 context->EFlags |= VIP_MASK;
1238 rec.ExceptionCode = EXCEPTION_VM86_STI;
1239 rec.ExceptionInformation[0] = 0;
1245 res = vm86_enter( &teb->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1251 } while (VM86_TYPE(res) == VM86_SIGNAL);
1253 save_vm86_context( context, &vm86 );
1254 context->EFlags |= teb->vm86_pending;
1256 switch(VM86_TYPE(res))
1258 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1259 do_segv( context, T_PROTFLT, 0, 0 );
1261 case VM86_TRAP: /* return due to DOS-debugger request */
1262 do_trap( context, VM86_ARG(res) );
1264 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1265 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1267 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1268 teb->vm86_pending = 0;
1269 rec.ExceptionCode = EXCEPTION_VM86_STI;
1271 case VM86_PICRETURN: /* return due to pending PIC request */
1272 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1275 ERR( "unhandled result from vm86 mode %x\n", res );
1278 rec.ExceptionInformation[0] = VM86_ARG(res);
1280 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1281 rec.ExceptionRecord = NULL;
1282 rec.ExceptionAddress = (LPVOID)context->Eip;
1283 rec.NumberParameters = 1;
1284 EXC_RtlRaiseException( &rec, context );
1288 #else /* __HAVE_VM86 */
1289 /**********************************************************************
1290 * __wine_enter_vm86 (NTDLL.@)
1292 void __wine_enter_vm86( CONTEXT *context )
1294 MESSAGE("vm86 mode not supported on this platform\n");
1296 #endif /* __HAVE_VM86 */
1298 /**********************************************************************
1299 * DbgBreakPoint (NTDLL.@)
1301 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1303 /**********************************************************************
1304 * DbgUserBreakPoint (NTDLL.@)
1306 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1308 #endif /* __i386__ */