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"
31 #include <sys/types.h>
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
60 #include "wine/library.h"
61 #include "ntdll_misc.h"
63 #ifdef HAVE_VALGRIND_MEMCHECK_H
64 #include <valgrind/memcheck.h>
67 /***********************************************************************
68 * signal context platform-specific definitions
73 typedef ucontext_t SIGCONTEXT;
75 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
76 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
77 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
78 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
79 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
80 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
81 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
82 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
84 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
85 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
86 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
87 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
88 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
89 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
91 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
92 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
93 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
94 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
96 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
98 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
100 int vm86_enter( void **vm86_ptr );
101 void vm86_return(void);
102 void vm86_return_end(void);
103 __ASM_GLOBAL_FUNC(vm86_enter,
105 "movl %esp, %ebp\n\t"
106 "movl $166,%eax\n\t" /*SYS_vm86*/
107 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
108 "movl (%ecx),%ecx\n\t"
110 "movl $1,%ebx\n\t" /*VM86_ENTER*/
111 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
115 ".globl " __ASM_NAME("vm86_return") "\n\t"
116 __ASM_FUNC("vm86_return") "\n"
117 __ASM_NAME("vm86_return") ":\n\t"
123 "testl %eax,%eax\n\t"
125 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
126 "je " __ASM_NAME("vm86_enter") "\n\t"
128 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
130 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
131 __ASM_FUNC("vm86_return_end") "\n"
132 __ASM_NAME("vm86_return_end") ":\n\t"
135 #ifdef HAVE_SYS_VM86_H
143 #include <machine/frame.h>
144 typedef struct trapframe SIGCONTEXT;
146 #define EAX_sig(context) ((context)->tf_eax)
147 #define EBX_sig(context) ((context)->tf_ebx)
148 #define ECX_sig(context) ((context)->tf_ecx)
149 #define EDX_sig(context) ((context)->tf_edx)
150 #define ESI_sig(context) ((context)->tf_esi)
151 #define EDI_sig(context) ((context)->tf_edi)
152 #define EBP_sig(context) ((context)->tf_ebp)
154 #define CS_sig(context) ((context)->tf_cs)
155 #define DS_sig(context) ((context)->tf_ds)
156 #define ES_sig(context) ((context)->tf_es)
157 #define SS_sig(context) ((context)->tf_ss)
159 #define EFL_sig(context) ((context)->tf_eflags)
161 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
162 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
166 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
168 typedef struct sigcontext SIGCONTEXT;
170 #define EAX_sig(context) ((context)->sc_eax)
171 #define EBX_sig(context) ((context)->sc_ebx)
172 #define ECX_sig(context) ((context)->sc_ecx)
173 #define EDX_sig(context) ((context)->sc_edx)
174 #define ESI_sig(context) ((context)->sc_esi)
175 #define EDI_sig(context) ((context)->sc_edi)
176 #define EBP_sig(context) ((context)->sc_ebp)
178 #define CS_sig(context) ((context)->sc_cs)
179 #define DS_sig(context) ((context)->sc_ds)
180 #define ES_sig(context) ((context)->sc_es)
181 #define FS_sig(context) ((context)->sc_fs)
182 #define GS_sig(context) ((context)->sc_gs)
183 #define SS_sig(context) ((context)->sc_ss)
185 #define TRAP_sig(context) ((context)->sc_trapno)
186 #define ERROR_sig(context) ((context)->sc_err)
187 #define EFL_sig(context) ((context)->sc_eflags)
189 #define EIP_sig(context) ((context)->sc_eip)
190 #define ESP_sig(context) ((context)->sc_esp)
194 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
197 #include <sys/regset.h>
201 #include <sys/ucontext.h>
203 typedef struct ucontext SIGCONTEXT;
209 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
210 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
211 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
212 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
213 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
214 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
215 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
217 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
218 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
219 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
220 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
222 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
223 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
225 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
227 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
229 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
231 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
233 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
236 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
239 #endif /* svr4 || SCO_DS */
242 # include <sys/ucontext.h>
244 typedef ucontext_t SIGCONTEXT;
246 /* work around silly renaming of struct members in OS X 10.5 */
247 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
248 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
249 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
250 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
251 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
252 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
253 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
254 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
255 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
256 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
257 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
258 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
259 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
260 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
261 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
262 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
263 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
264 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
265 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
267 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
268 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
269 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
270 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
271 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
272 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
273 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
274 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
275 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
276 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
277 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
278 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
279 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
280 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
281 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
282 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
283 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
284 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
287 #endif /* __APPLE__ */
289 #include "wine/exception.h"
290 #include "wine/debug.h"
292 WINE_DEFAULT_DEBUG_CHANNEL(seh);
294 typedef int (*wine_signal_handler)(unsigned int sig);
296 static size_t signal_stack_mask;
297 static size_t signal_stack_size;
299 static wine_signal_handler handlers[256];
301 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
305 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
306 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
307 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
308 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
309 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
310 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
311 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
312 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
313 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
314 TRAP_x86_DNA = T_DNA, /* Device not available exception */
315 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
316 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
317 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
318 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
319 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
320 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
321 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
322 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
323 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
324 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
325 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
327 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
328 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
329 TRAP_x86_NMI = 2, /* NMI interrupt */
330 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
331 TRAP_x86_OFLOW = 4, /* Overflow exception */
332 TRAP_x86_BOUND = 5, /* Bound range exception */
333 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
334 TRAP_x86_DNA = 7, /* Device not available exception */
335 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
336 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
337 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
338 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
339 TRAP_x86_STKFLT = 12, /* Stack fault */
340 TRAP_x86_PROTFLT = 13, /* General protection fault */
341 TRAP_x86_PAGEFLT = 14, /* Page fault */
342 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
343 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
344 TRAP_x86_MCHK = 18, /* Machine check exception */
345 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
346 otherwise Cache flush exception (via SIGSEV) */
351 /***********************************************************************
354 inline static int dispatch_signal(unsigned int sig)
356 if (handlers[sig] == NULL) return 0;
357 return handlers[sig](sig);
361 /***********************************************************************
364 * Get the trap code for a signal.
366 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
369 return TRAP_sig(sigcontext);
371 return TRAP_x86_UNKNOWN; /* unknown trap code */
375 /***********************************************************************
378 * Get the error code for a signal.
380 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
383 return ERROR_sig(sigcontext);
389 /***********************************************************************
392 * Get the base of the signal stack for the current thread.
394 static inline void *get_signal_stack(void)
396 return (char *)NtCurrentTeb() + 4096;
400 /***********************************************************************
403 * Get the current teb based on the stack pointer.
405 static inline TEB *get_current_teb(void)
408 __asm__("movl %%esp,%0" : "=g" (esp) );
409 return (TEB *)(esp & ~signal_stack_mask);
414 /***********************************************************************
417 * Set the register values from a vm86 structure.
419 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
421 context->ContextFlags = CONTEXT_FULL;
422 context->Eax = vm86->regs.eax;
423 context->Ebx = vm86->regs.ebx;
424 context->Ecx = vm86->regs.ecx;
425 context->Edx = vm86->regs.edx;
426 context->Esi = vm86->regs.esi;
427 context->Edi = vm86->regs.edi;
428 context->Esp = vm86->regs.esp;
429 context->Ebp = vm86->regs.ebp;
430 context->Eip = vm86->regs.eip;
431 context->SegCs = vm86->regs.cs;
432 context->SegDs = vm86->regs.ds;
433 context->SegEs = vm86->regs.es;
434 context->SegFs = vm86->regs.fs;
435 context->SegGs = vm86->regs.gs;
436 context->SegSs = vm86->regs.ss;
437 context->EFlags = vm86->regs.eflags;
441 /***********************************************************************
442 * restore_vm86_context
444 * Build a vm86 structure from the register values.
446 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
448 vm86->regs.eax = context->Eax;
449 vm86->regs.ebx = context->Ebx;
450 vm86->regs.ecx = context->Ecx;
451 vm86->regs.edx = context->Edx;
452 vm86->regs.esi = context->Esi;
453 vm86->regs.edi = context->Edi;
454 vm86->regs.esp = context->Esp;
455 vm86->regs.ebp = context->Ebp;
456 vm86->regs.eip = context->Eip;
457 vm86->regs.cs = context->SegCs;
458 vm86->regs.ds = context->SegDs;
459 vm86->regs.es = context->SegEs;
460 vm86->regs.fs = context->SegFs;
461 vm86->regs.gs = context->SegGs;
462 vm86->regs.ss = context->SegSs;
463 vm86->regs.eflags = context->EFlags;
467 /**********************************************************************
468 * merge_vm86_pending_flags
470 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
471 * raises exception if there are pending events and VIF flag
472 * has been turned on.
474 * Called from __wine_enter_vm86 because vm86_enter
475 * doesn't check for pending events.
477 * Called from raise_vm86_sti_exception to check for
478 * pending events in a signal safe way.
480 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
482 BOOL check_pending = TRUE;
483 struct vm86plus_struct *vm86 =
484 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
487 * In order to prevent a race when SIGUSR2 occurs while
488 * we are returning from exception handler, pending events
489 * will be rechecked after each raised exception.
491 while (check_pending && NtCurrentTeb()->vm86_pending)
493 check_pending = FALSE;
494 ntdll_get_thread_data()->vm86_ptr = NULL;
497 * If VIF is set, throw exception.
498 * Note that SIGUSR2 may turn VIF flag off so
499 * VIF check must occur only when TEB.vm86_ptr is NULL.
501 if (vm86->regs.eflags & VIF_MASK)
504 save_vm86_context( &vcontext, vm86 );
506 rec->ExceptionCode = EXCEPTION_VM86_STI;
507 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
508 rec->ExceptionRecord = NULL;
509 rec->NumberParameters = 0;
510 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
512 vcontext.EFlags &= ~VIP_MASK;
513 NtCurrentTeb()->vm86_pending = 0;
514 __regs_RtlRaiseException( rec, &vcontext );
516 restore_vm86_context( &vcontext, vm86 );
517 check_pending = TRUE;
520 ntdll_get_thread_data()->vm86_ptr = vm86;
524 * Merge VIP flags in a signal safe way. This requires
525 * that the following operation compiles into atomic
528 vm86->regs.eflags |= NtCurrentTeb()->vm86_pending;
530 #endif /* __HAVE_VM86 */
533 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
536 /***********************************************************************
539 * Handler initialization when the full context is not needed.
541 inline static void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
543 void *stack = (void *)(ESP_sig(sigcontext) & ~3);
544 TEB *teb = get_current_teb();
545 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
547 /* get %fs and %gs at time of the fault */
549 *fs = LOWORD(FS_sig(sigcontext));
554 *gs = LOWORD(GS_sig(sigcontext));
559 wine_set_fs( thread_regs->fs );
561 /* now restore a proper %gs for the fault handler */
562 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
563 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
566 * Win16 or DOS protected mode. Note that during switch
567 * from 16-bit mode to linear mode, CS may be set to system
568 * segment before FS is restored. Fortunately, in this case
569 * SS is still non-system segment. This is why both CS and SS
572 wine_set_gs( thread_regs->gs );
573 stack = teb->WOW32Reserved;
576 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
578 unsigned int *int_stack = stack;
579 /* fetch the saved %gs from the stack */
580 wine_set_gs( int_stack[0] );
583 else /* 32-bit mode */
586 wine_set_gs( GS_sig(sigcontext) );
593 /***********************************************************************
596 * Save the thread FPU context.
598 inline static void save_fpu( CONTEXT *context )
601 context->ContextFlags |= CONTEXT_FLOATING_POINT;
602 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
607 /***********************************************************************
610 * Restore the FPU context to a sigcontext.
612 inline static void restore_fpu( const CONTEXT *context )
614 FLOATING_SAVE_AREA float_status = context->FloatSave;
615 /* reset the current interrupt status */
616 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
618 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
619 #endif /* __GNUC__ */
623 /***********************************************************************
626 * Build a context structure from the signal info.
628 inline static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
630 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
632 memset(context, 0, sizeof(*context));
633 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
634 context->Eax = EAX_sig(sigcontext);
635 context->Ebx = EBX_sig(sigcontext);
636 context->Ecx = ECX_sig(sigcontext);
637 context->Edx = EDX_sig(sigcontext);
638 context->Esi = ESI_sig(sigcontext);
639 context->Edi = EDI_sig(sigcontext);
640 context->Ebp = EBP_sig(sigcontext);
641 context->EFlags = EFL_sig(sigcontext);
642 context->Eip = EIP_sig(sigcontext);
643 context->Esp = ESP_sig(sigcontext);
644 context->SegCs = LOWORD(CS_sig(sigcontext));
645 context->SegDs = LOWORD(DS_sig(sigcontext));
646 context->SegEs = LOWORD(ES_sig(sigcontext));
649 context->SegSs = LOWORD(SS_sig(sigcontext));
650 context->Dr0 = regs->dr0;
651 context->Dr1 = regs->dr1;
652 context->Dr2 = regs->dr2;
653 context->Dr3 = regs->dr3;
654 context->Dr6 = regs->dr6;
655 context->Dr7 = regs->dr7;
658 if (FPU_sig(sigcontext))
660 context->ContextFlags |= CONTEXT_FLOATING_POINT;
661 context->FloatSave = *FPU_sig(sigcontext);
671 /***********************************************************************
674 * Restore the signal info from the context.
676 inline static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
678 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
680 regs->dr0 = context->Dr0;
681 regs->dr1 = context->Dr1;
682 regs->dr2 = context->Dr2;
683 regs->dr3 = context->Dr3;
684 regs->dr6 = context->Dr6;
685 regs->dr7 = context->Dr7;
686 EAX_sig(sigcontext) = context->Eax;
687 EBX_sig(sigcontext) = context->Ebx;
688 ECX_sig(sigcontext) = context->Ecx;
689 EDX_sig(sigcontext) = context->Edx;
690 ESI_sig(sigcontext) = context->Esi;
691 EDI_sig(sigcontext) = context->Edi;
692 EBP_sig(sigcontext) = context->Ebp;
693 EFL_sig(sigcontext) = context->EFlags;
694 EIP_sig(sigcontext) = context->Eip;
695 ESP_sig(sigcontext) = context->Esp;
696 CS_sig(sigcontext) = context->SegCs;
697 DS_sig(sigcontext) = context->SegDs;
698 ES_sig(sigcontext) = context->SegEs;
699 SS_sig(sigcontext) = context->SegSs;
701 GS_sig(sigcontext) = context->SegGs;
703 wine_set_gs( context->SegGs );
706 FS_sig(sigcontext) = context->SegFs;
708 wine_set_fs( context->SegFs );
712 if (FPU_sig(sigcontext))
714 *FPU_sig(sigcontext) = context->FloatSave;
719 restore_fpu( context );
724 /***********************************************************************
727 * Register function to get the context of the current thread.
729 void WINAPI __regs_get_cpu_context( CONTEXT *context, CONTEXT *regs )
734 DEFINE_REGS_ENTRYPOINT( get_cpu_context, 4, 4 );
737 /***********************************************************************
740 * Set the new CPU context. Used by NtSetContextThread.
742 void set_cpu_context( const CONTEXT *context )
744 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
746 if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
748 if (flags & CONTEXT_DEBUG_REGISTERS)
750 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
751 regs->dr0 = context->Dr0;
752 regs->dr1 = context->Dr1;
753 regs->dr2 = context->Dr2;
754 regs->dr3 = context->Dr3;
755 regs->dr6 = context->Dr6;
756 regs->dr7 = context->Dr7;
758 if (flags & CONTEXT_FULL)
760 if ((flags & CONTEXT_FULL) != (CONTEXT_FULL & ~CONTEXT_i386))
761 FIXME( "setting partial context (%x) not supported\n", flags );
763 __wine_call_from_32_restore_regs( context );
768 /***********************************************************************
769 * is_privileged_instr
771 * Check if the fault location is a privileged instruction.
772 * Based on the instruction emulation code in dlls/kernel/instr.c.
774 static inline int is_privileged_instr( CONTEXT86 *context )
777 unsigned int prefix_count = 0;
779 if (!wine_ldt_is_system( context->SegCs )) return 0;
780 instr = (BYTE *)context->Eip;
782 for (;;) switch(*instr)
784 /* instruction prefixes */
785 case 0x2e: /* %cs: */
786 case 0x36: /* %ss: */
787 case 0x3e: /* %ds: */
788 case 0x26: /* %es: */
789 case 0x64: /* %fs: */
790 case 0x65: /* %gs: */
791 case 0x66: /* opcode size */
792 case 0x67: /* addr size */
793 case 0xf0: /* lock */
794 case 0xf2: /* repne */
795 case 0xf3: /* repe */
796 if (++prefix_count >= 15) return 0;
800 case 0x0f: /* extended instruction */
803 case 0x20: /* mov crX, reg */
804 case 0x21: /* mov drX, reg */
805 case 0x22: /* mov reg, crX */
806 case 0x23: /* mov reg drX */
810 case 0x6c: /* insb (%dx) */
811 case 0x6d: /* insl (%dx) */
812 case 0x6e: /* outsb (%dx) */
813 case 0x6f: /* outsl (%dx) */
814 case 0xcd: /* int $xx */
815 case 0xe4: /* inb al,XX */
816 case 0xe5: /* in (e)ax,XX */
817 case 0xe6: /* outb XX,al */
818 case 0xe7: /* out XX,(e)ax */
819 case 0xec: /* inb (%dx),%al */
820 case 0xed: /* inl (%dx),%eax */
821 case 0xee: /* outb %al,(%dx) */
822 case 0xef: /* outl %eax,(%dx) */
833 #include "pshpack1.h"
836 DWORD movl; /* movl this,4(%esp) */
838 BYTE jmp; /* jmp func */
843 /**********************************************************************
846 * Check if code destination is an ATL thunk, and emulate it if so.
848 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
850 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
855 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
857 *((DWORD *)context->Esp + 1) = thunk->this;
858 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
859 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
860 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
873 /***********************************************************************
876 * Setup a proper stack frame for the raise function, and modify the
877 * sigcontext so that the return from the signal handler will call
878 * the raise function.
880 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
884 void *ret_addr; /* return address from raise_func */
885 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
886 CONTEXT *context_ptr; /* second arg for raise_func */
888 EXCEPTION_RECORD rec;
895 stack = init_handler( sigcontext, &fs, &gs );
897 /* stack sanity checks */
899 if ((char *)stack >= (char *)get_signal_stack() &&
900 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
902 ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
903 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
904 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
905 NtCurrentTeb()->Tib.StackBase );
906 server_abort_thread(1);
909 if (stack - 1 > stack || /* check for overflow in subtraction */
910 (char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit ||
911 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
913 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)stack;
916 ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p\n",
917 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
918 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
919 NtCurrentTeb()->Tib.StackBase );
920 server_abort_thread(1);
922 else WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
923 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
924 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
925 NtCurrentTeb()->Tib.StackBase );
928 stack--; /* push the stack_layout structure */
929 #ifdef HAVE_VALGRIND_MEMCHECK_H
930 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
932 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
933 stack->rec_ptr = &stack->rec;
934 stack->context_ptr = &stack->context;
936 stack->rec.ExceptionRecord = NULL;
937 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
938 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
939 stack->rec.NumberParameters = 0;
941 save_context( &stack->context, sigcontext, fs, gs );
943 /* now modify the sigcontext to return to the raise function */
944 ESP_sig(sigcontext) = (DWORD)stack;
945 EIP_sig(sigcontext) = (DWORD)func;
946 /* clear single-step and align check flag */
947 EFL_sig(sigcontext) &= ~(0x100|0x40000);
948 CS_sig(sigcontext) = wine_get_cs();
949 DS_sig(sigcontext) = wine_get_ds();
950 ES_sig(sigcontext) = wine_get_es();
951 FS_sig(sigcontext) = wine_get_fs();
952 GS_sig(sigcontext) = wine_get_gs();
953 SS_sig(sigcontext) = wine_get_ss();
955 return stack->rec_ptr;
959 /***********************************************************************
960 * get_exception_context
962 * Get a pointer to the context built by setup_exception.
964 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
966 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
970 /**********************************************************************
973 * Get the FPU exception code from the FPU status.
975 static inline DWORD get_fpu_code( const CONTEXT *context )
977 DWORD status = context->FloatSave.StatusWord;
979 if (status & 0x01) /* IE */
981 if (status & 0x40) /* SF */
982 return EXCEPTION_FLT_STACK_CHECK;
984 return EXCEPTION_FLT_INVALID_OPERATION;
986 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
987 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
988 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
989 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
990 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
991 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
995 /**********************************************************************
996 * raise_segv_exception
998 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1000 switch(rec->ExceptionCode)
1002 case EXCEPTION_ACCESS_VIOLATION:
1003 if (rec->NumberParameters == 2)
1005 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1007 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1010 case EXCEPTION_DATATYPE_MISALIGNMENT:
1011 /* FIXME: pass through exception handler first? */
1012 if (context->EFlags & 0x00040000)
1014 /* Disable AC flag, return */
1015 context->EFlags &= ~0x00040000;
1020 __regs_RtlRaiseException( rec, context );
1022 NtSetContextThread( GetCurrentThread(), context );
1026 /**********************************************************************
1027 * raise_trap_exception
1029 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1031 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1033 if (context->EFlags & 0x100)
1035 context->EFlags &= ~0x100; /* clear single-step flag */
1037 else /* hardware breakpoint, fetch the debug registers */
1039 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1040 NtGetContextThread(GetCurrentThread(), context);
1042 * - a bp from a debug register
1043 * - a single step interrupt at popf instruction, which just has
1045 * - someone did a kill(SIGTRAP) on us, and we shall return
1046 * a breakpoint, not a single step exception
1048 if ( !(context->Dr6 & 0xf) && !(context->Dr6 & 0x4000) )
1049 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1050 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1054 __regs_RtlRaiseException( rec, context );
1055 NtSetContextThread( GetCurrentThread(), context );
1059 /**********************************************************************
1062 * Generic raise function for exceptions that don't need special treatment.
1064 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1066 __regs_RtlRaiseException( rec, context );
1067 NtSetContextThread( GetCurrentThread(), context );
1072 /**********************************************************************
1073 * raise_vm86_sti_exception
1075 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1077 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1078 NtCurrentTeb()->vm86_pending |= VIP_MASK;
1080 if (ntdll_get_thread_data()->vm86_ptr)
1082 if (((char*)context->Eip >= (char*)vm86_return) &&
1083 ((char*)context->Eip <= (char*)vm86_return_end) &&
1084 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1085 /* exiting from VM86, can't throw */
1088 merge_vm86_pending_flags( rec );
1090 else if (NtCurrentTeb()->dpmi_vif &&
1091 !wine_ldt_is_system(context->SegCs) &&
1092 !wine_ldt_is_system(context->SegSs))
1094 /* Executing DPMI code and virtual interrupts are enabled. */
1095 NtCurrentTeb()->vm86_pending = 0;
1096 __regs_RtlRaiseException( rec, context );
1099 NtSetContextThread( GetCurrentThread(), context );
1103 /**********************************************************************
1106 * Handler for SIGUSR2.
1107 * We use it to signal that the running __wine_enter_vm86() should
1108 * immediately set VIP_MASK, causing pending events to be handled
1109 * as early as possible.
1111 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1113 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1114 rec->ExceptionCode = EXCEPTION_VM86_STI;
1116 #endif /* __HAVE_VM86 */
1119 /**********************************************************************
1122 * Handler for SIGSEGV and related errors.
1124 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1126 SIGCONTEXT *context = sigcontext;
1127 EXCEPTION_RECORD *rec = setup_exception( context, raise_segv_exception );
1129 switch(get_trap_code(context))
1131 case TRAP_x86_OFLOW: /* Overflow exception */
1132 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1134 case TRAP_x86_BOUND: /* Bound range exception */
1135 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1137 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1138 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1140 case TRAP_x86_STKFLT: /* Stack fault */
1141 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1143 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1144 case TRAP_x86_PROTFLT: /* General protection fault */
1145 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1146 if (!get_error_code(context) && is_privileged_instr( get_exception_context(rec) ))
1147 rec->ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1150 WORD err = get_error_code(context);
1151 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1152 rec->NumberParameters = 2;
1153 rec->ExceptionInformation[0] = 0;
1154 /* if error contains a LDT selector, use that as fault address */
1155 rec->ExceptionInformation[1] = (err & 7) == 4 ? (err & ~7) : 0xffffffff;
1158 case TRAP_x86_PAGEFLT: /* Page fault */
1159 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1160 rec->NumberParameters = 2;
1161 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1162 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1164 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1165 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1168 ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1170 case TRAP_x86_NMI: /* NMI interrupt */
1171 case TRAP_x86_DNA: /* Device not available exception */
1172 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1173 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1174 case TRAP_x86_MCHK: /* Machine check exception */
1175 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1176 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1182 /**********************************************************************
1185 * Handler for SIGTRAP.
1187 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1189 SIGCONTEXT *context = sigcontext;
1190 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1192 switch(get_trap_code(context))
1194 case TRAP_x86_TRCTRAP: /* Single-step exception */
1195 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1197 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1198 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1201 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1207 /**********************************************************************
1210 * Handler for SIGFPE.
1212 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1214 CONTEXT *win_context;
1215 SIGCONTEXT *context = sigcontext;
1216 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1218 win_context = get_exception_context( rec );
1220 switch(get_trap_code(context))
1222 case TRAP_x86_DIVIDE: /* Division by zero exception */
1223 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1225 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1226 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1228 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1229 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1230 rec->ExceptionCode = get_fpu_code( win_context );
1232 case TRAP_x86_CACHEFLT: /* SIMD exception */
1234 * Behaviour only tested for divide-by-zero exceptions
1235 * Check for other SIMD exceptions as well */
1236 if(siginfo->si_code != FPE_FLTDIV)
1237 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1240 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1241 rec->NumberParameters = 1;
1242 /* no idea what meaning is actually behind this but thats what native does */
1243 rec->ExceptionInformation[0] = 0;
1246 ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1247 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1253 /**********************************************************************
1256 * Handler for SIGINT.
1258 * FIXME: should not be calling external functions on the signal stack.
1260 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1263 init_handler( sigcontext, &fs, &gs );
1264 if (!dispatch_signal(SIGINT))
1266 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1267 rec->ExceptionCode = CONTROL_C_EXIT;
1271 /**********************************************************************
1274 * Handler for SIGABRT.
1276 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1278 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1279 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1280 rec->ExceptionFlags = EH_NONCONTINUABLE;
1284 /**********************************************************************
1287 * Handler for SIGTERM.
1289 static void term_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1292 init_handler( sigcontext, &fs, &gs );
1293 server_abort_thread(0);
1297 /**********************************************************************
1300 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1302 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1307 init_handler( sigcontext, &fs, &gs );
1308 save_context( &context, sigcontext, fs, gs );
1309 wait_suspend( &context );
1310 restore_context( &context, sigcontext );
1314 /**********************************************************************
1315 * get_signal_stack_total_size
1317 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1318 * Must be a power of two.
1320 size_t get_signal_stack_total_size(void)
1322 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1324 if (!signal_stack_size)
1326 size_t size = 4096, min_size = teb_size + max( MINSIGSTKSZ, 4096 );
1327 /* find the first power of two not smaller than min_size */
1328 while (size < min_size) size *= 2;
1329 signal_stack_mask = size - 1;
1330 signal_stack_size = size - teb_size;
1332 return signal_stack_size + teb_size;
1336 /***********************************************************************
1337 * __wine_set_signal_handler (NTDLL.@)
1339 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1341 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1342 if (handlers[sig] != NULL) return -2;
1343 handlers[sig] = wsh;
1348 /**********************************************************************
1351 BOOL SIGNAL_Init(void)
1353 struct sigaction sig_act;
1355 #ifdef HAVE_SIGALTSTACK
1359 int mib[2], val = 1;
1362 mib[1] = KERN_THALTSTACK;
1363 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1366 ss.ss_sp = get_signal_stack();
1367 ss.ss_size = signal_stack_size;
1369 if (sigaltstack(&ss, NULL) == -1)
1371 perror( "sigaltstack" );
1374 #endif /* HAVE_SIGALTSTACK */
1376 sig_act.sa_mask = server_block_set;
1377 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1379 sig_act.sa_flags |= SA_ONSTACK;
1382 sig_act.sa_sigaction = int_handler;
1383 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1384 sig_act.sa_sigaction = fpe_handler;
1385 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1386 sig_act.sa_sigaction = abrt_handler;
1387 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1388 sig_act.sa_sigaction = term_handler;
1389 if (sigaction( SIGTERM, &sig_act, NULL ) == -1) goto error;
1390 sig_act.sa_sigaction = usr1_handler;
1391 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1393 sig_act.sa_sigaction = segv_handler;
1394 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1395 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1397 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1401 sig_act.sa_sigaction = trap_handler;
1402 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1406 sig_act.sa_sigaction = usr2_handler;
1407 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1413 perror("sigaction");
1419 /**********************************************************************
1420 * __wine_enter_vm86 (NTDLL.@)
1422 * Enter vm86 mode with the specified register context.
1424 void __wine_enter_vm86( CONTEXT *context )
1426 EXCEPTION_RECORD rec;
1428 struct vm86plus_struct vm86;
1430 memset( &vm86, 0, sizeof(vm86) );
1433 restore_vm86_context( context, &vm86 );
1435 ntdll_get_thread_data()->vm86_ptr = &vm86;
1436 merge_vm86_pending_flags( &rec );
1438 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1445 save_vm86_context( context, &vm86 );
1447 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1448 rec.ExceptionRecord = NULL;
1449 rec.ExceptionAddress = (LPVOID)context->Eip;
1450 rec.NumberParameters = 0;
1452 switch(VM86_TYPE(res))
1454 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1455 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1456 raise_segv_exception( &rec, context );
1458 case VM86_TRAP: /* return due to DOS-debugger request */
1459 switch(VM86_ARG(res))
1461 case TRAP_x86_TRCTRAP: /* Single-step exception */
1462 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1464 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1465 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1468 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1472 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1473 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1474 rec.NumberParameters = 1;
1475 rec.ExceptionInformation[0] = VM86_ARG(res);
1477 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1478 context->EFlags |= VIF_MASK;
1479 context->EFlags &= ~VIP_MASK;
1480 NtCurrentTeb()->vm86_pending = 0;
1481 rec.ExceptionCode = EXCEPTION_VM86_STI;
1483 case VM86_PICRETURN: /* return due to pending PIC request */
1484 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1486 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1488 ERR( "unhandled result from vm86 mode %x\n", res );
1491 __regs_RtlRaiseException( &rec, context );
1495 #else /* __HAVE_VM86 */
1496 /**********************************************************************
1497 * __wine_enter_vm86 (NTDLL.@)
1499 void __wine_enter_vm86( CONTEXT *context )
1501 MESSAGE("vm86 mode not supported on this platform\n");
1503 #endif /* __HAVE_VM86 */
1505 /**********************************************************************
1506 * DbgBreakPoint (NTDLL.@)
1508 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1510 /**********************************************************************
1511 * DbgUserBreakPoint (NTDLL.@)
1513 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1516 /**********************************************************************
1517 * EXC_CallHandler (internal)
1519 * Some exception handlers depend on EBP to have a fixed position relative to
1520 * the exception frame.
1521 * Shrinker depends on (*1) doing what it does,
1522 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1523 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1524 * function calling the handler having only 5 parameters (*4).
1526 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1528 " movl %esp, %ebp\n"
1530 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1536 " call " __ASM_NAME("call_exception_handler") "\n"
1541 __ASM_GLOBAL_FUNC(call_exception_handler,
1543 " movl %esp, %ebp\n"
1545 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1546 " pushl %edx\n" /* handler be handled by... */
1548 " pushl (0)\n" /* nested_handler (passed in edx). */
1550 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1555 " movl 24(%ebp), %ecx\n" /* (*1) */
1556 " call *%ecx\n" /* call handler. (*2) */
1558 " movl (0), %esp\n" /* restore previous... (*3) */
1560 " popl (0)\n" /* exception frame. */
1561 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1563 " ret $20\n" /* (*4) */
1565 #endif /* __i386__ */