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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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>
56 #include "wine/library.h"
57 #include "ntdll_misc.h"
59 #ifdef HAVE_VALGRIND_MEMCHECK_H
60 #include <valgrind/memcheck.h>
63 /***********************************************************************
64 * signal context platform-specific definitions
70 unsigned short sc_gs, __gsh;
71 unsigned short sc_fs, __fsh;
72 unsigned short sc_es, __esh;
73 unsigned short sc_ds, __dsh;
82 unsigned long sc_trapno;
85 unsigned short sc_cs, __csh;
86 unsigned long sc_eflags;
87 unsigned long esp_at_signal;
88 unsigned short sc_ss, __ssh;
90 unsigned long oldmask;
92 } volatile SIGCONTEXT;
94 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
95 #define HANDLER_CONTEXT (&__context)
97 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
98 struct kernel_sigaction
100 void (*ksa_handler)();
101 unsigned long ksa_mask;
102 unsigned long ksa_flags;
106 #ifndef SYS_sigaction
107 # ifndef __NR_sigaction
108 # error The sigaction syscall is part of the Linux i386 ABI, but your headers does not define it. Please raise a bug with your distribution.
110 # define SYS_sigaction __NR_sigaction
113 /* Similar to the sigaction function in libc, except it leaves alone the
114 restorer field, which is used to specify the signal stack address */
115 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
116 struct kernel_sigaction *old )
118 __asm__ __volatile__( "pushl %%ebx\n\t"
123 : "0" (SYS_sigaction), "S" (sig), "c" (new), "d" (old) );
124 if (sig>=0) return 0;
129 #ifdef HAVE_SIGALTSTACK
130 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
131 static inline int wine_sigaltstack( const struct sigaltstack *new,
132 struct sigaltstack *old )
135 __asm__ __volatile__( "pushl %%ebx\n\t"
140 : "0" (SYS_sigaltstack), "q" (new), "c" (old) );
141 if (ret >= 0) return 0;
147 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
149 int vm86_enter( void **vm86_ptr );
150 void vm86_return(void);
151 void vm86_return_end(void);
152 __ASM_GLOBAL_FUNC(vm86_enter,
154 "movl %esp, %ebp\n\t"
155 "movl $166,%eax\n\t" /*SYS_vm86*/
156 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
157 "movl (%ecx),%ecx\n\t"
159 "movl $1,%ebx\n\t" /*VM86_ENTER*/
160 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
164 ".globl " __ASM_NAME("vm86_return") "\n\t"
165 __ASM_FUNC("vm86_return") "\n"
166 __ASM_NAME("vm86_return") ":\n\t"
172 "testl %eax,%eax\n\t"
174 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
175 "je " __ASM_NAME("vm86_enter") "\n\t"
177 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
179 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
180 __ASM_FUNC("vm86_return_end") "\n"
181 __ASM_NAME("vm86_return_end") ":\n\t"
184 #ifdef HAVE_SYS_VM86_H
192 #define EAX_sig(context) ((context)->tf_eax)
193 #define EBX_sig(context) ((context)->tf_ebx)
194 #define ECX_sig(context) ((context)->tf_ecx)
195 #define EDX_sig(context) ((context)->tf_edx)
196 #define ESI_sig(context) ((context)->tf_esi)
197 #define EDI_sig(context) ((context)->tf_edi)
198 #define EBP_sig(context) ((context)->tf_ebp)
200 #define CS_sig(context) ((context)->tf_cs)
201 #define DS_sig(context) ((context)->tf_ds)
202 #define ES_sig(context) ((context)->tf_es)
203 #define SS_sig(context) ((context)->tf_ss)
205 #include <machine/frame.h>
206 typedef struct trapframe SIGCONTEXT;
208 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
209 #define HANDLER_CONTEXT __context
211 #define EFL_sig(context) ((context)->tf_eflags)
213 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
214 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
218 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
220 typedef struct sigcontext SIGCONTEXT;
222 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
223 #define HANDLER_CONTEXT __context
227 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
230 #include <sys/regset.h>
234 #include <sys/ucontext.h>
236 typedef struct ucontext SIGCONTEXT;
238 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
239 #define HANDLER_CONTEXT __context
241 #endif /* svr4 || SCO_DS */
247 unsigned long ContextFlags;
248 FLOATING_SAVE_AREA sc_float;
253 unsigned long sc_edi;
254 unsigned long sc_esi;
255 unsigned long sc_eax;
256 unsigned long sc_ebx;
257 unsigned long sc_ecx;
258 unsigned long sc_edx;
259 unsigned long sc_ebp;
260 unsigned long sc_eip;
262 unsigned long sc_eflags;
263 unsigned long sc_esp;
271 /* FIXME: This section is just here so it can compile, it's most likely
272 * completely wrong. */
276 unsigned short sc_gs, __gsh;
277 unsigned short sc_fs, __fsh;
278 unsigned short sc_es, __esh;
279 unsigned short sc_ds, __dsh;
280 unsigned long sc_edi;
281 unsigned long sc_esi;
282 unsigned long sc_ebp;
283 unsigned long sc_esp;
284 unsigned long sc_ebx;
285 unsigned long sc_edx;
286 unsigned long sc_ecx;
287 unsigned long sc_eax;
288 unsigned long sc_trapno;
289 unsigned long sc_err;
290 unsigned long sc_eip;
291 unsigned short sc_cs, __csh;
292 unsigned long sc_eflags;
293 unsigned long esp_at_signal;
294 unsigned short sc_ss, __ssh;
296 unsigned long oldmask;
300 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
301 #define HANDLER_CONTEXT (&__context)
303 #endif /* __CYGWIN__ */
306 # include <sys/ucontext.h>
307 # include <sys/types.h>
310 typedef ucontext_t SIGCONTEXT;
312 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
313 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
314 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
315 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
316 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
317 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
318 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
320 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
321 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
322 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
323 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
324 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
325 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
327 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
329 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
330 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
332 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
333 #define HANDLER_CONTEXT (__context)
335 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
336 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
338 #define FAULT_ADDRESS (__siginfo->si_addr)
340 #endif /* __APPLE__ */
342 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) ||\
343 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
345 #define EAX_sig(context) ((context)->sc_eax)
346 #define EBX_sig(context) ((context)->sc_ebx)
347 #define ECX_sig(context) ((context)->sc_ecx)
348 #define EDX_sig(context) ((context)->sc_edx)
349 #define ESI_sig(context) ((context)->sc_esi)
350 #define EDI_sig(context) ((context)->sc_edi)
351 #define EBP_sig(context) ((context)->sc_ebp)
353 #define CS_sig(context) ((context)->sc_cs)
354 #define DS_sig(context) ((context)->sc_ds)
355 #define ES_sig(context) ((context)->sc_es)
356 #define FS_sig(context) ((context)->sc_fs)
357 #define GS_sig(context) ((context)->sc_gs)
358 #define SS_sig(context) ((context)->sc_ss)
360 #define TRAP_sig(context) ((context)->sc_trapno)
363 #define ERROR_sig(context) ((context)->sc_err)
367 #define ERROR_sig(context) ((context)->sc_err)
368 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
369 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->cr2)
372 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
373 #define EFL_sig(context) ((context)->sc_efl)
374 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
375 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->sc_err)
377 #define EFL_sig(context) ((context)->sc_eflags)
380 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
381 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
383 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
385 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
391 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
392 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
393 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
394 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
395 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
396 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
397 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
399 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
400 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
401 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
402 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
404 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
405 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
407 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
409 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
411 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
413 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
415 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
418 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
421 #define FAULT_ADDRESS (__siginfo->si_addr)
423 #endif /* svr4 || SCO_DS */
425 #include "wine/exception.h"
426 #include "wine/debug.h"
428 WINE_DEFAULT_DEBUG_CHANNEL(seh);
430 typedef int (*wine_signal_handler)(unsigned int sig);
432 static size_t signal_stack_mask;
433 static size_t signal_stack_size;
435 static wine_signal_handler handlers[256];
437 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
441 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
442 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
443 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
444 TRAP_x86_NMI = 2, /* NMI interrupt */
445 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
446 TRAP_x86_OFLOW = 4, /* Overflow exception */
447 TRAP_x86_BOUND = 5, /* Bound range exception */
448 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
449 TRAP_x86_DNA = 7, /* Device not available exception */
450 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
451 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
452 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
453 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
454 TRAP_x86_STKFLT = 12, /* Stack fault */
455 TRAP_x86_PROTFLT = 13, /* General protection fault */
456 TRAP_x86_PAGEFLT = 14, /* Page fault */
457 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
458 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
459 TRAP_x86_MCHK = 18, /* Machine check exception */
460 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
464 /***********************************************************************
467 inline static int dispatch_signal(unsigned int sig)
469 if (handlers[sig] == NULL) return 0;
470 return handlers[sig](sig);
474 /***********************************************************************
477 * Get the trap code for a signal.
479 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
482 return TRAP_sig(sigcontext);
484 return TRAP_x86_UNKNOWN; /* unknown trap code */
488 /***********************************************************************
491 * Get the error code for a signal.
493 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
496 return ERROR_sig(sigcontext);
502 /***********************************************************************
505 * Get the base of the signal stack for the current thread.
507 static inline void *get_signal_stack(void)
509 return (char *)NtCurrentTeb() + 4096;
513 /***********************************************************************
516 * Get the current teb based on the stack pointer.
518 static inline TEB *get_current_teb(void)
521 __asm__("movl %%esp,%0" : "=g" (esp) );
522 return (TEB *)(esp & ~signal_stack_mask);
527 /***********************************************************************
530 * Set the register values from a vm86 structure.
532 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
534 context->ContextFlags = CONTEXT_FULL;
535 context->Eax = vm86->regs.eax;
536 context->Ebx = vm86->regs.ebx;
537 context->Ecx = vm86->regs.ecx;
538 context->Edx = vm86->regs.edx;
539 context->Esi = vm86->regs.esi;
540 context->Edi = vm86->regs.edi;
541 context->Esp = vm86->regs.esp;
542 context->Ebp = vm86->regs.ebp;
543 context->Eip = vm86->regs.eip;
544 context->SegCs = vm86->regs.cs;
545 context->SegDs = vm86->regs.ds;
546 context->SegEs = vm86->regs.es;
547 context->SegFs = vm86->regs.fs;
548 context->SegGs = vm86->regs.gs;
549 context->SegSs = vm86->regs.ss;
550 context->EFlags = vm86->regs.eflags;
554 /***********************************************************************
555 * restore_vm86_context
557 * Build a vm86 structure from the register values.
559 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
561 vm86->regs.eax = context->Eax;
562 vm86->regs.ebx = context->Ebx;
563 vm86->regs.ecx = context->Ecx;
564 vm86->regs.edx = context->Edx;
565 vm86->regs.esi = context->Esi;
566 vm86->regs.edi = context->Edi;
567 vm86->regs.esp = context->Esp;
568 vm86->regs.ebp = context->Ebp;
569 vm86->regs.eip = context->Eip;
570 vm86->regs.cs = context->SegCs;
571 vm86->regs.ds = context->SegDs;
572 vm86->regs.es = context->SegEs;
573 vm86->regs.fs = context->SegFs;
574 vm86->regs.gs = context->SegGs;
575 vm86->regs.ss = context->SegSs;
576 vm86->regs.eflags = context->EFlags;
580 /**********************************************************************
581 * merge_vm86_pending_flags
583 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
584 * raises exception if there are pending events and VIF flag
585 * has been turned on.
587 * Called from __wine_enter_vm86 because vm86_enter
588 * doesn't check for pending events.
590 * Called from raise_vm86_sti_exception to check for
591 * pending events in a signal safe way.
593 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
595 BOOL check_pending = TRUE;
596 struct vm86plus_struct *vm86 =
597 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
600 * In order to prevent a race when SIGUSR2 occurs while
601 * we are returning from exception handler, pending events
602 * will be rechecked after each raised exception.
604 while (check_pending && NtCurrentTeb()->vm86_pending)
606 check_pending = FALSE;
607 ntdll_get_thread_data()->vm86_ptr = NULL;
610 * If VIF is set, throw exception.
611 * Note that SIGUSR2 may turn VIF flag off so
612 * VIF check must occur only when TEB.vm86_ptr is NULL.
614 if (vm86->regs.eflags & VIF_MASK)
617 save_vm86_context( &vcontext, vm86 );
619 rec->ExceptionCode = EXCEPTION_VM86_STI;
620 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
621 rec->ExceptionRecord = NULL;
622 rec->NumberParameters = 0;
623 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
625 vcontext.EFlags &= ~VIP_MASK;
626 NtCurrentTeb()->vm86_pending = 0;
627 __regs_RtlRaiseException( rec, &vcontext );
629 restore_vm86_context( &vcontext, vm86 );
630 check_pending = TRUE;
633 ntdll_get_thread_data()->vm86_ptr = vm86;
637 * Merge VIP flags in a signal safe way. This requires
638 * that the following operation compiles into atomic
641 vm86->regs.eflags |= NtCurrentTeb()->vm86_pending;
643 #endif /* __HAVE_VM86 */
646 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
649 /***********************************************************************
652 * Handler initialization when the full context is not needed.
654 inline static void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
656 void *stack = (void *)ESP_sig(sigcontext);
657 TEB *teb = get_current_teb();
658 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
660 /* get %fs and %gs at time of the fault */
662 *fs = LOWORD(FS_sig(sigcontext));
667 *gs = LOWORD(GS_sig(sigcontext));
672 wine_set_fs( thread_regs->fs );
674 /* now restore a proper %gs for the fault handler */
675 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
676 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
679 * Win16 or DOS protected mode. Note that during switch
680 * from 16-bit mode to linear mode, CS may be set to system
681 * segment before FS is restored. Fortunately, in this case
682 * SS is still non-system segment. This is why both CS and SS
685 wine_set_gs( thread_regs->gs );
686 stack = teb->WOW32Reserved;
689 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
691 unsigned int *int_stack = stack;
692 /* fetch the saved %gs from the stack */
693 wine_set_gs( int_stack[0] );
696 else /* 32-bit mode */
699 wine_set_gs( GS_sig(sigcontext) );
706 /***********************************************************************
709 * Save the thread FPU context.
711 inline static void save_fpu( CONTEXT *context )
714 context->ContextFlags |= CONTEXT_FLOATING_POINT;
715 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
720 /***********************************************************************
723 * Restore the FPU context to a sigcontext.
725 inline static void restore_fpu( const CONTEXT *context )
727 FLOATING_SAVE_AREA float_status = context->FloatSave;
728 /* reset the current interrupt status */
729 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
731 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
732 #endif /* __GNUC__ */
736 /***********************************************************************
739 * Build a context structure from the signal info.
741 inline static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
743 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
745 memset(context, 0, sizeof(*context));
746 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
747 context->Eax = EAX_sig(sigcontext);
748 context->Ebx = EBX_sig(sigcontext);
749 context->Ecx = ECX_sig(sigcontext);
750 context->Edx = EDX_sig(sigcontext);
751 context->Esi = ESI_sig(sigcontext);
752 context->Edi = EDI_sig(sigcontext);
753 context->Ebp = EBP_sig(sigcontext);
754 context->EFlags = EFL_sig(sigcontext);
755 context->Eip = EIP_sig(sigcontext);
756 context->Esp = ESP_sig(sigcontext);
757 context->SegCs = LOWORD(CS_sig(sigcontext));
758 context->SegDs = LOWORD(DS_sig(sigcontext));
759 context->SegEs = LOWORD(ES_sig(sigcontext));
762 context->SegSs = LOWORD(SS_sig(sigcontext));
763 context->Dr0 = regs->dr0;
764 context->Dr1 = regs->dr1;
765 context->Dr2 = regs->dr2;
766 context->Dr3 = regs->dr3;
767 context->Dr6 = regs->dr6;
768 context->Dr7 = regs->dr7;
771 if (FPU_sig(sigcontext))
773 context->ContextFlags |= CONTEXT_FLOATING_POINT;
774 context->FloatSave = *FPU_sig(sigcontext);
784 /***********************************************************************
787 * Restore the signal info from the context.
789 inline static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
791 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
793 regs->dr0 = context->Dr0;
794 regs->dr1 = context->Dr1;
795 regs->dr2 = context->Dr2;
796 regs->dr3 = context->Dr3;
797 regs->dr6 = context->Dr6;
798 regs->dr7 = context->Dr7;
799 EAX_sig(sigcontext) = context->Eax;
800 EBX_sig(sigcontext) = context->Ebx;
801 ECX_sig(sigcontext) = context->Ecx;
802 EDX_sig(sigcontext) = context->Edx;
803 ESI_sig(sigcontext) = context->Esi;
804 EDI_sig(sigcontext) = context->Edi;
805 EBP_sig(sigcontext) = context->Ebp;
806 EFL_sig(sigcontext) = context->EFlags;
807 EIP_sig(sigcontext) = context->Eip;
808 ESP_sig(sigcontext) = context->Esp;
809 CS_sig(sigcontext) = context->SegCs;
810 DS_sig(sigcontext) = context->SegDs;
811 ES_sig(sigcontext) = context->SegEs;
812 SS_sig(sigcontext) = context->SegSs;
814 GS_sig(sigcontext) = context->SegGs;
816 wine_set_gs( context->SegGs );
819 FS_sig(sigcontext) = context->SegFs;
821 wine_set_fs( context->SegFs );
825 if (FPU_sig(sigcontext))
827 *FPU_sig(sigcontext) = context->FloatSave;
832 restore_fpu( context );
837 /***********************************************************************
840 * Register function to get the context of the current thread.
842 void WINAPI __regs_get_cpu_context( CONTEXT *context, CONTEXT *regs )
847 DEFINE_REGS_ENTRYPOINT( get_cpu_context, 4, 4 );
850 /***********************************************************************
853 * Set the new CPU context. Used by NtSetContextThread.
855 void set_cpu_context( const CONTEXT *context )
857 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
859 if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
861 if (flags & CONTEXT_DEBUG_REGISTERS)
863 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
864 regs->dr0 = context->Dr0;
865 regs->dr1 = context->Dr1;
866 regs->dr2 = context->Dr2;
867 regs->dr3 = context->Dr3;
868 regs->dr6 = context->Dr6;
869 regs->dr7 = context->Dr7;
871 if (flags & CONTEXT_FULL)
873 if ((flags & CONTEXT_FULL) != (CONTEXT_FULL & ~CONTEXT_i386))
874 FIXME( "setting partial context (%lx) not supported\n", flags );
876 __wine_call_from_32_restore_regs( context );
881 /***********************************************************************
882 * is_privileged_instr
884 * Check if the fault location is a privileged instruction.
885 * Based on the instruction emulation code in dlls/kernel/instr.c.
887 static inline int is_privileged_instr( CONTEXT86 *context )
890 unsigned int prefix_count = 0;
892 if (!wine_ldt_is_system( context->SegCs )) return 0;
893 instr = (BYTE *)context->Eip;
895 for (;;) switch(*instr)
897 /* instruction prefixes */
898 case 0x2e: /* %cs: */
899 case 0x36: /* %ss: */
900 case 0x3e: /* %ds: */
901 case 0x26: /* %es: */
902 case 0x64: /* %fs: */
903 case 0x65: /* %gs: */
904 case 0x66: /* opcode size */
905 case 0x67: /* addr size */
906 case 0xf0: /* lock */
907 case 0xf2: /* repne */
908 case 0xf3: /* repe */
909 if (++prefix_count >= 15) return 0;
913 case 0x0f: /* extended instruction */
916 case 0x20: /* mov crX, reg */
917 case 0x21: /* mov drX, reg */
918 case 0x22: /* mov reg, crX */
919 case 0x23: /* mov reg drX */
923 case 0x6c: /* insb (%dx) */
924 case 0x6d: /* insl (%dx) */
925 case 0x6e: /* outsb (%dx) */
926 case 0x6f: /* outsl (%dx) */
927 case 0xcd: /* int $xx */
928 case 0xe4: /* inb al,XX */
929 case 0xe5: /* in (e)ax,XX */
930 case 0xe6: /* outb XX,al */
931 case 0xe7: /* out XX,(e)ax */
932 case 0xec: /* inb (%dx),%al */
933 case 0xed: /* inl (%dx),%eax */
934 case 0xee: /* outb %al,(%dx) */
935 case 0xef: /* outl %eax,(%dx) */
946 #include "pshpack1.h"
949 DWORD movl; /* movl this,4(%esp) */
951 BYTE jmp; /* jmp func */
956 /**********************************************************************
959 * Check if code destination is an ATL thunk, and emulate it if so.
961 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
963 struct atl_thunk *thunk = (struct atl_thunk *)rec->ExceptionInformation[1];
965 if (thunk->movl != 0x042444c7 || thunk->jmp != 0xe9) return FALSE;
966 *((DWORD *)context->Esp + 1) = thunk->this;
967 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
968 TRACE( "emulating ATL thunk at %p, func=%08lx arg=%08lx\n",
969 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
974 /***********************************************************************
977 * Setup a proper stack frame for the raise function, and modify the
978 * sigcontext so that the return from the signal handler will call
979 * the raise function.
981 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
985 void *ret_addr; /* return address from raise_func */
986 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
987 CONTEXT *context_ptr; /* second arg for raise_func */
989 EXCEPTION_RECORD rec;
996 stack = init_handler( sigcontext, &fs, &gs );
998 /* stack sanity checks */
1000 if ((char *)stack >= (char *)get_signal_stack() &&
1001 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1003 ERR( "nested exception on signal stack in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
1004 GetCurrentThreadId(), EIP_sig(sigcontext), ESP_sig(sigcontext),
1005 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1006 server_abort_thread(1);
1009 if (stack - 1 > stack || /* check for overflow in subtraction */
1010 (char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit ||
1011 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1013 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)stack;
1016 ERR( "stack overflow %u bytes in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
1017 diff, GetCurrentThreadId(), EIP_sig(sigcontext), ESP_sig(sigcontext),
1018 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1019 server_abort_thread(1);
1021 else WARN( "exception outside of stack limits in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
1022 GetCurrentThreadId(), EIP_sig(sigcontext), ESP_sig(sigcontext),
1023 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1026 stack--; /* push the stack_layout structure */
1027 #ifdef HAVE_VALGRIND_MEMCHECK_H
1028 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1030 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1031 stack->rec_ptr = &stack->rec;
1032 stack->context_ptr = &stack->context;
1034 stack->rec.ExceptionRecord = NULL;
1035 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1036 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1037 stack->rec.NumberParameters = 0;
1039 save_context( &stack->context, sigcontext, fs, gs );
1041 /* now modify the sigcontext to return to the raise function */
1042 ESP_sig(sigcontext) = (DWORD)stack;
1043 EIP_sig(sigcontext) = (DWORD)func;
1044 EFL_sig(sigcontext) &= ~0x100; /* clear single-step flag */
1045 CS_sig(sigcontext) = wine_get_cs();
1046 DS_sig(sigcontext) = wine_get_ds();
1047 ES_sig(sigcontext) = wine_get_es();
1048 FS_sig(sigcontext) = wine_get_fs();
1049 GS_sig(sigcontext) = wine_get_gs();
1050 SS_sig(sigcontext) = wine_get_ss();
1052 return stack->rec_ptr;
1056 /***********************************************************************
1057 * get_exception_context
1059 * Get a pointer to the context built by setup_exception.
1061 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1063 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1067 /**********************************************************************
1070 * Get the FPU exception code from the FPU status.
1072 static inline DWORD get_fpu_code( const CONTEXT *context )
1074 DWORD status = context->FloatSave.StatusWord;
1076 if (status & 0x01) /* IE */
1078 if (status & 0x40) /* SF */
1079 return EXCEPTION_FLT_STACK_CHECK;
1081 return EXCEPTION_FLT_INVALID_OPERATION;
1083 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1084 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1085 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1086 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1087 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1088 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1092 /**********************************************************************
1093 * raise_segv_exception
1095 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1097 switch(rec->ExceptionCode)
1099 case EXCEPTION_ACCESS_VIOLATION:
1100 if (rec->NumberParameters == 2)
1102 if ((rec->ExceptionInformation[0] == 8) && check_atl_thunk( rec, context )) goto done;
1103 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1106 case EXCEPTION_DATATYPE_MISALIGNMENT:
1107 /* FIXME: pass through exception handler first? */
1108 if (context->EFlags & 0x00040000)
1110 /* Disable AC flag, return */
1111 context->EFlags &= ~0x00040000;
1116 __regs_RtlRaiseException( rec, context );
1118 NtSetContextThread( GetCurrentThread(), context );
1122 /**********************************************************************
1123 * raise_trap_exception
1125 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1127 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1129 if (context->EFlags & 0x100)
1131 context->EFlags &= ~0x100; /* clear single-step flag */
1133 else /* hardware breakpoint, fetch the debug registers */
1135 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1136 NtGetContextThread(GetCurrentThread(), context);
1137 /* do we really have a bp from a debug register ?
1138 * if not, then someone did a kill(SIGTRAP) on us, and we
1139 * shall return a breakpoint, not a single step exception
1141 if (!(context->Dr6 & 0xf)) rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1142 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1146 __regs_RtlRaiseException( rec, context );
1147 NtSetContextThread( GetCurrentThread(), context );
1151 /**********************************************************************
1154 * Generic raise function for exceptions that don't need special treatment.
1156 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1158 __regs_RtlRaiseException( rec, context );
1159 NtSetContextThread( GetCurrentThread(), context );
1164 /**********************************************************************
1165 * raise_vm86_sti_exception
1167 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1169 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1170 NtCurrentTeb()->vm86_pending |= VIP_MASK;
1172 if (ntdll_get_thread_data()->vm86_ptr)
1174 if (((char*)context->Eip >= (char*)vm86_return) &&
1175 ((char*)context->Eip <= (char*)vm86_return_end) &&
1176 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1177 /* exiting from VM86, can't throw */
1180 merge_vm86_pending_flags( rec );
1182 else if (NtCurrentTeb()->dpmi_vif &&
1183 !wine_ldt_is_system(context->SegCs) &&
1184 !wine_ldt_is_system(context->SegSs))
1186 /* Executing DPMI code and virtual interrupts are enabled. */
1187 NtCurrentTeb()->vm86_pending = 0;
1188 __regs_RtlRaiseException( rec, context );
1191 NtSetContextThread( GetCurrentThread(), context );
1195 /**********************************************************************
1198 * Handler for SIGUSR2.
1199 * We use it to signal that the running __wine_enter_vm86() should
1200 * immediately set VIP_MASK, causing pending events to be handled
1201 * as early as possible.
1203 static HANDLER_DEF(usr2_handler)
1205 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_vm86_sti_exception );
1206 rec->ExceptionCode = EXCEPTION_VM86_STI;
1208 #endif /* __HAVE_VM86 */
1211 /**********************************************************************
1214 * Handler for SIGSEGV and related errors.
1216 static HANDLER_DEF(segv_handler)
1218 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_segv_exception );
1220 switch(get_trap_code(HANDLER_CONTEXT))
1222 case TRAP_x86_OFLOW: /* Overflow exception */
1223 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1225 case TRAP_x86_BOUND: /* Bound range exception */
1226 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1228 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1229 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1231 case TRAP_x86_STKFLT: /* Stack fault */
1232 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1234 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1235 case TRAP_x86_PROTFLT: /* General protection fault */
1236 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1237 if (!get_error_code(HANDLER_CONTEXT) && is_privileged_instr( get_exception_context(rec) ))
1238 rec->ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1241 WORD err = get_error_code(HANDLER_CONTEXT);
1242 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1243 rec->NumberParameters = 2;
1244 rec->ExceptionInformation[0] = 0;
1245 /* if error contains a LDT selector, use that as fault address */
1246 rec->ExceptionInformation[1] = (err & 7) == 4 ? (err & ~7) : 0xffffffff;
1249 case TRAP_x86_PAGEFLT: /* Page fault */
1250 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1251 #ifdef FAULT_ADDRESS
1252 rec->NumberParameters = 2;
1253 rec->ExceptionInformation[0] = (get_error_code(HANDLER_CONTEXT) >> 1) & 0x09;
1254 rec->ExceptionInformation[1] = (ULONG_PTR)FAULT_ADDRESS;
1257 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1258 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1261 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
1263 case TRAP_x86_NMI: /* NMI interrupt */
1264 case TRAP_x86_DNA: /* Device not available exception */
1265 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1266 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1267 case TRAP_x86_MCHK: /* Machine check exception */
1268 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1269 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1275 /**********************************************************************
1278 * Handler for SIGTRAP.
1280 static HANDLER_DEF(trap_handler)
1282 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_trap_exception );
1284 switch(get_trap_code(HANDLER_CONTEXT))
1286 case TRAP_x86_TRCTRAP: /* Single-step exception */
1287 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1289 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1290 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1293 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1299 /**********************************************************************
1302 * Handler for SIGFPE.
1304 static HANDLER_DEF(fpe_handler)
1306 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_exception );
1309 context = get_exception_context( rec );
1311 switch(get_trap_code(HANDLER_CONTEXT))
1313 case TRAP_x86_DIVIDE: /* Division by zero exception */
1314 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1316 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1317 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1319 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1320 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1321 rec->ExceptionCode = get_fpu_code( context );
1324 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
1325 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1331 /**********************************************************************
1334 * Handler for SIGINT.
1336 * FIXME: should not be calling external functions on the signal stack.
1338 static HANDLER_DEF(int_handler)
1341 init_handler( HANDLER_CONTEXT, &fs, &gs );
1342 if (!dispatch_signal(SIGINT))
1344 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_exception );
1345 rec->ExceptionCode = CONTROL_C_EXIT;
1349 /**********************************************************************
1352 * Handler for SIGABRT.
1354 static HANDLER_DEF(abrt_handler)
1356 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_exception );
1357 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1358 rec->ExceptionFlags = EH_NONCONTINUABLE;
1362 /**********************************************************************
1365 * Handler for SIGTERM.
1367 static HANDLER_DEF(term_handler)
1370 init_handler( HANDLER_CONTEXT, &fs, &gs );
1371 server_abort_thread(0);
1375 /**********************************************************************
1378 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1380 static HANDLER_DEF(usr1_handler)
1385 init_handler( HANDLER_CONTEXT, &fs, &gs );
1386 save_context( &context, HANDLER_CONTEXT, fs, gs );
1387 wait_suspend( &context );
1388 restore_context( &context, HANDLER_CONTEXT );
1392 /**********************************************************************
1393 * get_signal_stack_total_size
1395 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1396 * Must be a power of two.
1398 size_t get_signal_stack_total_size(void)
1400 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1402 if (!signal_stack_size)
1404 size_t size = 4096, min_size = teb_size + max( MINSIGSTKSZ, 4096 );
1405 /* find the first power of two not smaller than min_size */
1406 while (size < min_size) size *= 2;
1407 signal_stack_mask = size - 1;
1408 signal_stack_size = size - teb_size;
1410 return signal_stack_size + teb_size;
1414 /***********************************************************************
1417 * Set a signal handler
1419 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
1421 struct sigaction sig_act;
1424 if (!have_sigaltstack)
1426 struct kernel_sigaction sig_act;
1427 sig_act.ksa_handler = func;
1428 sig_act.ksa_flags = SA_RESTART;
1429 sig_act.ksa_mask = (1 << (SIGINT-1)) |
1430 (1 << (SIGUSR1-1)) |
1432 /* point to the top of the signal stack */
1433 sig_act.ksa_restorer = (char *)get_signal_stack() + signal_stack_size;
1434 return wine_sigaction( sig, &sig_act, NULL );
1437 sig_act.sa_handler = func;
1438 sigemptyset( &sig_act.sa_mask );
1439 sigaddset( &sig_act.sa_mask, SIGINT );
1440 sigaddset( &sig_act.sa_mask, SIGUSR1 );
1441 sigaddset( &sig_act.sa_mask, SIGUSR2 );
1443 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
1444 sig_act.sa_flags = SA_RESTART;
1445 #elif defined (__svr4__) || defined(_SCO_DS) || defined(__APPLE__)
1446 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1448 sig_act.sa_flags = 0;
1452 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1454 return sigaction( sig, &sig_act, NULL );
1458 /***********************************************************************
1459 * __wine_set_signal_handler (NTDLL.@)
1461 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1463 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1464 if (handlers[sig] != NULL) return -2;
1465 handlers[sig] = wsh;
1470 /**********************************************************************
1473 BOOL SIGNAL_Init(void)
1475 int have_sigaltstack = 0;
1477 #ifdef HAVE_SIGALTSTACK
1478 struct sigaltstack ss;
1479 ss.ss_sp = get_signal_stack();
1480 ss.ss_size = signal_stack_size;
1482 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1484 /* sigaltstack may fail because the kernel is too old, or
1485 because glibc is brain-dead. In the latter case a
1486 direct system call should succeed. */
1487 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1489 #endif /* HAVE_SIGALTSTACK */
1491 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1492 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1493 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1494 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1495 if (set_handler( SIGABRT, have_sigaltstack, (void (*)())abrt_handler ) == -1) goto error;
1496 if (set_handler( SIGTERM, have_sigaltstack, (void (*)())term_handler ) == -1) goto error;
1497 if (set_handler( SIGUSR1, have_sigaltstack, (void (*)())usr1_handler ) == -1) goto error;
1499 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1502 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1506 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1512 perror("sigaction");
1518 /**********************************************************************
1519 * __wine_enter_vm86 (NTDLL.@)
1521 * Enter vm86 mode with the specified register context.
1523 void __wine_enter_vm86( CONTEXT *context )
1525 EXCEPTION_RECORD rec;
1527 struct vm86plus_struct vm86;
1529 memset( &vm86, 0, sizeof(vm86) );
1532 restore_vm86_context( context, &vm86 );
1534 ntdll_get_thread_data()->vm86_ptr = &vm86;
1535 merge_vm86_pending_flags( &rec );
1537 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1544 save_vm86_context( context, &vm86 );
1546 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1547 rec.ExceptionRecord = NULL;
1548 rec.ExceptionAddress = (LPVOID)context->Eip;
1549 rec.NumberParameters = 0;
1551 switch(VM86_TYPE(res))
1553 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1554 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1555 raise_segv_exception( &rec, context );
1557 case VM86_TRAP: /* return due to DOS-debugger request */
1558 switch(VM86_ARG(res))
1560 case TRAP_x86_TRCTRAP: /* Single-step exception */
1561 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1563 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1564 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1567 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1571 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1572 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1573 rec.NumberParameters = 1;
1574 rec.ExceptionInformation[0] = VM86_ARG(res);
1576 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1577 context->EFlags |= VIF_MASK;
1578 context->EFlags &= ~VIP_MASK;
1579 NtCurrentTeb()->vm86_pending = 0;
1580 rec.ExceptionCode = EXCEPTION_VM86_STI;
1582 case VM86_PICRETURN: /* return due to pending PIC request */
1583 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1585 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1587 ERR( "unhandled result from vm86 mode %x\n", res );
1590 __regs_RtlRaiseException( &rec, context );
1594 #else /* __HAVE_VM86 */
1595 /**********************************************************************
1596 * __wine_enter_vm86 (NTDLL.@)
1598 void __wine_enter_vm86( CONTEXT *context )
1600 MESSAGE("vm86 mode not supported on this platform\n");
1602 #endif /* __HAVE_VM86 */
1604 /**********************************************************************
1605 * DbgBreakPoint (NTDLL.@)
1607 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1609 /**********************************************************************
1610 * DbgUserBreakPoint (NTDLL.@)
1612 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1615 /**********************************************************************
1616 * EXC_CallHandler (internal)
1618 * Some exception handlers depend on EBP to have a fixed position relative to
1619 * the exception frame.
1620 * Shrinker depends on (*1) doing what it does,
1621 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1622 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1623 * function calling the handler having only 5 parameters (*4).
1625 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1627 " movl %esp, %ebp\n"
1629 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1635 " call " __ASM_NAME("call_exception_handler") "\n"
1639 __ASM_GLOBAL_FUNC(call_exception_handler,
1641 " movl %esp, %ebp\n"
1643 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1644 " pushl %edx\n" /* handler be handled by... */
1646 " pushl (0)\n" /* nested_handler (passed in edx). */
1648 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1653 " movl 24(%ebp), %ecx\n" /* (*1) */
1654 " call *%ecx\n" /* call handler. (*2) */
1656 " movl (0), %esp\n" /* restore previous... (*3) */
1658 " popl (0)\n" /* exception frame. */
1659 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1661 " ret $20\n" /* (*4) */
1663 #endif /* __i386__ */