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 #define EAX_sig(context) ((context)->sc_eax)
98 #define EBX_sig(context) ((context)->sc_ebx)
99 #define ECX_sig(context) ((context)->sc_ecx)
100 #define EDX_sig(context) ((context)->sc_edx)
101 #define ESI_sig(context) ((context)->sc_esi)
102 #define EDI_sig(context) ((context)->sc_edi)
103 #define EBP_sig(context) ((context)->sc_ebp)
105 #define CS_sig(context) ((context)->sc_cs)
106 #define DS_sig(context) ((context)->sc_ds)
107 #define ES_sig(context) ((context)->sc_es)
108 #define FS_sig(context) ((context)->sc_fs)
109 #define GS_sig(context) ((context)->sc_gs)
110 #define SS_sig(context) ((context)->sc_ss)
112 #define TRAP_sig(context) ((context)->sc_trapno)
113 #define ERROR_sig(context) ((context)->sc_err)
114 #define EFL_sig(context) ((context)->sc_eflags)
116 #define EIP_sig(context) ((context)->sc_eip)
117 #define ESP_sig(context) ((context)->sc_esp)
119 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
121 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->cr2)
123 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
124 struct kernel_sigaction
126 void (*ksa_handler)();
127 unsigned long ksa_mask;
128 unsigned long ksa_flags;
132 #ifndef SYS_sigaction
133 # ifndef __NR_sigaction
134 # 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.
136 # define SYS_sigaction __NR_sigaction
139 /* Similar to the sigaction function in libc, except it leaves alone the
140 restorer field, which is used to specify the signal stack address */
141 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
142 struct kernel_sigaction *old )
144 __asm__ __volatile__( "pushl %%ebx\n\t"
149 : "0" (SYS_sigaction), "S" (sig), "c" (new), "d" (old) );
150 if (sig>=0) return 0;
155 #ifdef HAVE_SIGALTSTACK
156 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
157 static inline int wine_sigaltstack( const struct sigaltstack *new,
158 struct sigaltstack *old )
161 __asm__ __volatile__( "pushl %%ebx\n\t"
166 : "0" (SYS_sigaltstack), "q" (new), "c" (old) );
167 if (ret >= 0) return 0;
173 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
175 int vm86_enter( void **vm86_ptr );
176 void vm86_return(void);
177 void vm86_return_end(void);
178 __ASM_GLOBAL_FUNC(vm86_enter,
180 "movl %esp, %ebp\n\t"
181 "movl $166,%eax\n\t" /*SYS_vm86*/
182 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
183 "movl (%ecx),%ecx\n\t"
185 "movl $1,%ebx\n\t" /*VM86_ENTER*/
186 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
190 ".globl " __ASM_NAME("vm86_return") "\n\t"
191 __ASM_FUNC("vm86_return") "\n"
192 __ASM_NAME("vm86_return") ":\n\t"
198 "testl %eax,%eax\n\t"
200 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
201 "je " __ASM_NAME("vm86_enter") "\n\t"
203 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
205 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
206 __ASM_FUNC("vm86_return_end") "\n"
207 __ASM_NAME("vm86_return_end") ":\n\t"
210 #ifdef HAVE_SYS_VM86_H
218 #define EAX_sig(context) ((context)->tf_eax)
219 #define EBX_sig(context) ((context)->tf_ebx)
220 #define ECX_sig(context) ((context)->tf_ecx)
221 #define EDX_sig(context) ((context)->tf_edx)
222 #define ESI_sig(context) ((context)->tf_esi)
223 #define EDI_sig(context) ((context)->tf_edi)
224 #define EBP_sig(context) ((context)->tf_ebp)
226 #define CS_sig(context) ((context)->tf_cs)
227 #define DS_sig(context) ((context)->tf_ds)
228 #define ES_sig(context) ((context)->tf_es)
229 #define SS_sig(context) ((context)->tf_ss)
231 #include <machine/frame.h>
232 typedef struct trapframe SIGCONTEXT;
234 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
235 #define HANDLER_CONTEXT __context
237 #define EFL_sig(context) ((context)->tf_eflags)
239 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
240 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
244 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
246 typedef struct sigcontext SIGCONTEXT;
248 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
249 #define HANDLER_CONTEXT __context
251 #define EAX_sig(context) ((context)->sc_eax)
252 #define EBX_sig(context) ((context)->sc_ebx)
253 #define ECX_sig(context) ((context)->sc_ecx)
254 #define EDX_sig(context) ((context)->sc_edx)
255 #define ESI_sig(context) ((context)->sc_esi)
256 #define EDI_sig(context) ((context)->sc_edi)
257 #define EBP_sig(context) ((context)->sc_ebp)
259 #define CS_sig(context) ((context)->sc_cs)
260 #define DS_sig(context) ((context)->sc_ds)
261 #define ES_sig(context) ((context)->sc_es)
262 #define FS_sig(context) ((context)->sc_fs)
263 #define GS_sig(context) ((context)->sc_gs)
264 #define SS_sig(context) ((context)->sc_ss)
266 #define TRAP_sig(context) ((context)->sc_trapno)
267 #define ERROR_sig(context) ((context)->sc_err)
268 #define EFL_sig(context) ((context)->sc_eflags)
270 #define EIP_sig(context) ((context)->sc_eip)
271 #define ESP_sig(context) ((context)->sc_esp)
273 #define FAULT_ADDRESS (__siginfo->si_addr)
277 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
280 #include <sys/regset.h>
284 #include <sys/ucontext.h>
286 typedef struct ucontext SIGCONTEXT;
288 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
289 #define HANDLER_CONTEXT __context
295 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
296 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
297 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
298 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
299 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
300 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
301 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
303 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
304 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
305 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
306 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
308 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
309 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
311 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
313 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
315 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
317 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
319 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
322 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
325 #define FAULT_ADDRESS (__siginfo->si_addr)
327 #endif /* svr4 || SCO_DS */
333 unsigned long ContextFlags;
334 FLOATING_SAVE_AREA sc_float;
339 unsigned long sc_edi;
340 unsigned long sc_esi;
341 unsigned long sc_eax;
342 unsigned long sc_ebx;
343 unsigned long sc_ecx;
344 unsigned long sc_edx;
345 unsigned long sc_ebp;
346 unsigned long sc_eip;
348 unsigned long sc_eflags;
349 unsigned long sc_esp;
353 #define EAX_sig(context) ((context)->sc_eax)
354 #define EBX_sig(context) ((context)->sc_ebx)
355 #define ECX_sig(context) ((context)->sc_ecx)
356 #define EDX_sig(context) ((context)->sc_edx)
357 #define ESI_sig(context) ((context)->sc_esi)
358 #define EDI_sig(context) ((context)->sc_edi)
359 #define EBP_sig(context) ((context)->sc_ebp)
361 #define CS_sig(context) ((context)->sc_cs)
362 #define DS_sig(context) ((context)->sc_ds)
363 #define ES_sig(context) ((context)->sc_es)
364 #define FS_sig(context) ((context)->sc_fs)
365 #define GS_sig(context) ((context)->sc_gs)
366 #define SS_sig(context) ((context)->sc_ss)
368 #define EFL_sig(context) ((context)->sc_eflags)
370 #define EIP_sig(context) ((context)->sc_eip)
371 #define ESP_sig(context) ((context)->sc_esp)
377 /* FIXME: This section is just here so it can compile, it's most likely
378 * completely wrong. */
382 unsigned short sc_gs, __gsh;
383 unsigned short sc_fs, __fsh;
384 unsigned short sc_es, __esh;
385 unsigned short sc_ds, __dsh;
386 unsigned long sc_edi;
387 unsigned long sc_esi;
388 unsigned long sc_ebp;
389 unsigned long sc_esp;
390 unsigned long sc_ebx;
391 unsigned long sc_edx;
392 unsigned long sc_ecx;
393 unsigned long sc_eax;
394 unsigned long sc_trapno;
395 unsigned long sc_err;
396 unsigned long sc_eip;
397 unsigned short sc_cs, __csh;
398 unsigned long sc_eflags;
399 unsigned long esp_at_signal;
400 unsigned short sc_ss, __ssh;
402 unsigned long oldmask;
406 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
407 #define HANDLER_CONTEXT (&__context)
409 #define EAX_sig(context) ((context)->sc_eax)
410 #define EBX_sig(context) ((context)->sc_ebx)
411 #define ECX_sig(context) ((context)->sc_ecx)
412 #define EDX_sig(context) ((context)->sc_edx)
413 #define ESI_sig(context) ((context)->sc_esi)
414 #define EDI_sig(context) ((context)->sc_edi)
415 #define EBP_sig(context) ((context)->sc_ebp)
417 #define CS_sig(context) ((context)->sc_cs)
418 #define DS_sig(context) ((context)->sc_ds)
419 #define ES_sig(context) ((context)->sc_es)
420 #define FS_sig(context) ((context)->sc_fs)
421 #define GS_sig(context) ((context)->sc_gs)
422 #define SS_sig(context) ((context)->sc_ss)
424 #define TRAP_sig(context) ((context)->sc_trapno)
425 #define ERROR_sig(context) ((context)->sc_err)
426 #define EFL_sig(context) ((context)->sc_eflags)
428 #define EIP_sig(context) ((context)->sc_eip)
429 #define ESP_sig(context) ((context)->sc_esp)
431 #endif /* __CYGWIN__ */
434 # include <sys/ucontext.h>
435 # include <sys/types.h>
438 typedef ucontext_t SIGCONTEXT;
440 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
441 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
442 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
443 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
444 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
445 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
446 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
448 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
449 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
450 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
451 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
452 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
453 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
455 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
457 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
458 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
460 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
461 #define HANDLER_CONTEXT (__context)
463 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
464 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
466 #define FAULT_ADDRESS (__siginfo->si_addr)
468 #endif /* __APPLE__ */
470 #include "wine/exception.h"
471 #include "wine/debug.h"
473 WINE_DEFAULT_DEBUG_CHANNEL(seh);
475 typedef int (*wine_signal_handler)(unsigned int sig);
477 static size_t signal_stack_mask;
478 static size_t signal_stack_size;
480 static wine_signal_handler handlers[256];
482 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
486 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
487 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
488 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
489 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
490 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
491 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
492 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
493 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
494 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
495 TRAP_x86_DNA = T_DNA, /* Device not available exception */
496 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
497 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
498 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
499 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
500 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
501 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
502 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
503 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
504 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
505 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
506 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
508 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
509 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
510 TRAP_x86_NMI = 2, /* NMI interrupt */
511 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
512 TRAP_x86_OFLOW = 4, /* Overflow exception */
513 TRAP_x86_BOUND = 5, /* Bound range exception */
514 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
515 TRAP_x86_DNA = 7, /* Device not available exception */
516 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
517 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
518 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
519 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
520 TRAP_x86_STKFLT = 12, /* Stack fault */
521 TRAP_x86_PROTFLT = 13, /* General protection fault */
522 TRAP_x86_PAGEFLT = 14, /* Page fault */
523 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
524 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
525 TRAP_x86_MCHK = 18, /* Machine check exception */
526 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
531 /***********************************************************************
534 inline static int dispatch_signal(unsigned int sig)
536 if (handlers[sig] == NULL) return 0;
537 return handlers[sig](sig);
541 /***********************************************************************
544 * Get the trap code for a signal.
546 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
549 return TRAP_sig(sigcontext);
551 return TRAP_x86_UNKNOWN; /* unknown trap code */
555 /***********************************************************************
558 * Get the error code for a signal.
560 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
563 return ERROR_sig(sigcontext);
569 /***********************************************************************
572 * Get the base of the signal stack for the current thread.
574 static inline void *get_signal_stack(void)
576 return (char *)NtCurrentTeb() + 4096;
580 /***********************************************************************
583 * Get the current teb based on the stack pointer.
585 static inline TEB *get_current_teb(void)
588 __asm__("movl %%esp,%0" : "=g" (esp) );
589 return (TEB *)(esp & ~signal_stack_mask);
594 /***********************************************************************
597 * Set the register values from a vm86 structure.
599 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
601 context->ContextFlags = CONTEXT_FULL;
602 context->Eax = vm86->regs.eax;
603 context->Ebx = vm86->regs.ebx;
604 context->Ecx = vm86->regs.ecx;
605 context->Edx = vm86->regs.edx;
606 context->Esi = vm86->regs.esi;
607 context->Edi = vm86->regs.edi;
608 context->Esp = vm86->regs.esp;
609 context->Ebp = vm86->regs.ebp;
610 context->Eip = vm86->regs.eip;
611 context->SegCs = vm86->regs.cs;
612 context->SegDs = vm86->regs.ds;
613 context->SegEs = vm86->regs.es;
614 context->SegFs = vm86->regs.fs;
615 context->SegGs = vm86->regs.gs;
616 context->SegSs = vm86->regs.ss;
617 context->EFlags = vm86->regs.eflags;
621 /***********************************************************************
622 * restore_vm86_context
624 * Build a vm86 structure from the register values.
626 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
628 vm86->regs.eax = context->Eax;
629 vm86->regs.ebx = context->Ebx;
630 vm86->regs.ecx = context->Ecx;
631 vm86->regs.edx = context->Edx;
632 vm86->regs.esi = context->Esi;
633 vm86->regs.edi = context->Edi;
634 vm86->regs.esp = context->Esp;
635 vm86->regs.ebp = context->Ebp;
636 vm86->regs.eip = context->Eip;
637 vm86->regs.cs = context->SegCs;
638 vm86->regs.ds = context->SegDs;
639 vm86->regs.es = context->SegEs;
640 vm86->regs.fs = context->SegFs;
641 vm86->regs.gs = context->SegGs;
642 vm86->regs.ss = context->SegSs;
643 vm86->regs.eflags = context->EFlags;
647 /**********************************************************************
648 * merge_vm86_pending_flags
650 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
651 * raises exception if there are pending events and VIF flag
652 * has been turned on.
654 * Called from __wine_enter_vm86 because vm86_enter
655 * doesn't check for pending events.
657 * Called from raise_vm86_sti_exception to check for
658 * pending events in a signal safe way.
660 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
662 BOOL check_pending = TRUE;
663 struct vm86plus_struct *vm86 =
664 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
667 * In order to prevent a race when SIGUSR2 occurs while
668 * we are returning from exception handler, pending events
669 * will be rechecked after each raised exception.
671 while (check_pending && NtCurrentTeb()->vm86_pending)
673 check_pending = FALSE;
674 ntdll_get_thread_data()->vm86_ptr = NULL;
677 * If VIF is set, throw exception.
678 * Note that SIGUSR2 may turn VIF flag off so
679 * VIF check must occur only when TEB.vm86_ptr is NULL.
681 if (vm86->regs.eflags & VIF_MASK)
684 save_vm86_context( &vcontext, vm86 );
686 rec->ExceptionCode = EXCEPTION_VM86_STI;
687 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
688 rec->ExceptionRecord = NULL;
689 rec->NumberParameters = 0;
690 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
692 vcontext.EFlags &= ~VIP_MASK;
693 NtCurrentTeb()->vm86_pending = 0;
694 __regs_RtlRaiseException( rec, &vcontext );
696 restore_vm86_context( &vcontext, vm86 );
697 check_pending = TRUE;
700 ntdll_get_thread_data()->vm86_ptr = vm86;
704 * Merge VIP flags in a signal safe way. This requires
705 * that the following operation compiles into atomic
708 vm86->regs.eflags |= NtCurrentTeb()->vm86_pending;
710 #endif /* __HAVE_VM86 */
713 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
716 /***********************************************************************
719 * Handler initialization when the full context is not needed.
721 inline static void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
723 void *stack = (void *)ESP_sig(sigcontext);
724 TEB *teb = get_current_teb();
725 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
727 /* get %fs and %gs at time of the fault */
729 *fs = LOWORD(FS_sig(sigcontext));
734 *gs = LOWORD(GS_sig(sigcontext));
739 wine_set_fs( thread_regs->fs );
741 /* now restore a proper %gs for the fault handler */
742 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
743 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
746 * Win16 or DOS protected mode. Note that during switch
747 * from 16-bit mode to linear mode, CS may be set to system
748 * segment before FS is restored. Fortunately, in this case
749 * SS is still non-system segment. This is why both CS and SS
752 wine_set_gs( thread_regs->gs );
753 stack = teb->WOW32Reserved;
756 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
758 unsigned int *int_stack = stack;
759 /* fetch the saved %gs from the stack */
760 wine_set_gs( int_stack[0] );
763 else /* 32-bit mode */
766 wine_set_gs( GS_sig(sigcontext) );
773 /***********************************************************************
776 * Save the thread FPU context.
778 inline static void save_fpu( CONTEXT *context )
781 context->ContextFlags |= CONTEXT_FLOATING_POINT;
782 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
787 /***********************************************************************
790 * Restore the FPU context to a sigcontext.
792 inline static void restore_fpu( const CONTEXT *context )
794 FLOATING_SAVE_AREA float_status = context->FloatSave;
795 /* reset the current interrupt status */
796 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
798 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
799 #endif /* __GNUC__ */
803 /***********************************************************************
806 * Build a context structure from the signal info.
808 inline static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
810 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
812 memset(context, 0, sizeof(*context));
813 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
814 context->Eax = EAX_sig(sigcontext);
815 context->Ebx = EBX_sig(sigcontext);
816 context->Ecx = ECX_sig(sigcontext);
817 context->Edx = EDX_sig(sigcontext);
818 context->Esi = ESI_sig(sigcontext);
819 context->Edi = EDI_sig(sigcontext);
820 context->Ebp = EBP_sig(sigcontext);
821 context->EFlags = EFL_sig(sigcontext);
822 context->Eip = EIP_sig(sigcontext);
823 context->Esp = ESP_sig(sigcontext);
824 context->SegCs = LOWORD(CS_sig(sigcontext));
825 context->SegDs = LOWORD(DS_sig(sigcontext));
826 context->SegEs = LOWORD(ES_sig(sigcontext));
829 context->SegSs = LOWORD(SS_sig(sigcontext));
830 context->Dr0 = regs->dr0;
831 context->Dr1 = regs->dr1;
832 context->Dr2 = regs->dr2;
833 context->Dr3 = regs->dr3;
834 context->Dr6 = regs->dr6;
835 context->Dr7 = regs->dr7;
838 if (FPU_sig(sigcontext))
840 context->ContextFlags |= CONTEXT_FLOATING_POINT;
841 context->FloatSave = *FPU_sig(sigcontext);
851 /***********************************************************************
854 * Restore the signal info from the context.
856 inline static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
858 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
860 regs->dr0 = context->Dr0;
861 regs->dr1 = context->Dr1;
862 regs->dr2 = context->Dr2;
863 regs->dr3 = context->Dr3;
864 regs->dr6 = context->Dr6;
865 regs->dr7 = context->Dr7;
866 EAX_sig(sigcontext) = context->Eax;
867 EBX_sig(sigcontext) = context->Ebx;
868 ECX_sig(sigcontext) = context->Ecx;
869 EDX_sig(sigcontext) = context->Edx;
870 ESI_sig(sigcontext) = context->Esi;
871 EDI_sig(sigcontext) = context->Edi;
872 EBP_sig(sigcontext) = context->Ebp;
873 EFL_sig(sigcontext) = context->EFlags;
874 EIP_sig(sigcontext) = context->Eip;
875 ESP_sig(sigcontext) = context->Esp;
876 CS_sig(sigcontext) = context->SegCs;
877 DS_sig(sigcontext) = context->SegDs;
878 ES_sig(sigcontext) = context->SegEs;
879 SS_sig(sigcontext) = context->SegSs;
881 GS_sig(sigcontext) = context->SegGs;
883 wine_set_gs( context->SegGs );
886 FS_sig(sigcontext) = context->SegFs;
888 wine_set_fs( context->SegFs );
892 if (FPU_sig(sigcontext))
894 *FPU_sig(sigcontext) = context->FloatSave;
899 restore_fpu( context );
904 /***********************************************************************
907 * Register function to get the context of the current thread.
909 void WINAPI __regs_get_cpu_context( CONTEXT *context, CONTEXT *regs )
914 DEFINE_REGS_ENTRYPOINT( get_cpu_context, 4, 4 );
917 /***********************************************************************
920 * Set the new CPU context. Used by NtSetContextThread.
922 void set_cpu_context( const CONTEXT *context )
924 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
926 if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
928 if (flags & CONTEXT_DEBUG_REGISTERS)
930 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
931 regs->dr0 = context->Dr0;
932 regs->dr1 = context->Dr1;
933 regs->dr2 = context->Dr2;
934 regs->dr3 = context->Dr3;
935 regs->dr6 = context->Dr6;
936 regs->dr7 = context->Dr7;
938 if (flags & CONTEXT_FULL)
940 if ((flags & CONTEXT_FULL) != (CONTEXT_FULL & ~CONTEXT_i386))
941 FIXME( "setting partial context (%lx) not supported\n", flags );
943 __wine_call_from_32_restore_regs( context );
948 /***********************************************************************
949 * is_privileged_instr
951 * Check if the fault location is a privileged instruction.
952 * Based on the instruction emulation code in dlls/kernel/instr.c.
954 static inline int is_privileged_instr( CONTEXT86 *context )
957 unsigned int prefix_count = 0;
959 if (!wine_ldt_is_system( context->SegCs )) return 0;
960 instr = (BYTE *)context->Eip;
962 for (;;) switch(*instr)
964 /* instruction prefixes */
965 case 0x2e: /* %cs: */
966 case 0x36: /* %ss: */
967 case 0x3e: /* %ds: */
968 case 0x26: /* %es: */
969 case 0x64: /* %fs: */
970 case 0x65: /* %gs: */
971 case 0x66: /* opcode size */
972 case 0x67: /* addr size */
973 case 0xf0: /* lock */
974 case 0xf2: /* repne */
975 case 0xf3: /* repe */
976 if (++prefix_count >= 15) return 0;
980 case 0x0f: /* extended instruction */
983 case 0x20: /* mov crX, reg */
984 case 0x21: /* mov drX, reg */
985 case 0x22: /* mov reg, crX */
986 case 0x23: /* mov reg drX */
990 case 0x6c: /* insb (%dx) */
991 case 0x6d: /* insl (%dx) */
992 case 0x6e: /* outsb (%dx) */
993 case 0x6f: /* outsl (%dx) */
994 case 0xcd: /* int $xx */
995 case 0xe4: /* inb al,XX */
996 case 0xe5: /* in (e)ax,XX */
997 case 0xe6: /* outb XX,al */
998 case 0xe7: /* out XX,(e)ax */
999 case 0xec: /* inb (%dx),%al */
1000 case 0xed: /* inl (%dx),%eax */
1001 case 0xee: /* outb %al,(%dx) */
1002 case 0xef: /* outl %eax,(%dx) */
1003 case 0xf4: /* hlt */
1004 case 0xfa: /* cli */
1005 case 0xfb: /* sti */
1013 #include "pshpack1.h"
1016 DWORD movl; /* movl this,4(%esp) */
1018 BYTE jmp; /* jmp func */
1021 #include "poppack.h"
1023 /**********************************************************************
1026 * Check if code destination is an ATL thunk, and emulate it if so.
1028 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1030 struct atl_thunk *thunk = (struct atl_thunk *)rec->ExceptionInformation[1];
1032 if (thunk->movl != 0x042444c7 || thunk->jmp != 0xe9) return FALSE;
1033 *((DWORD *)context->Esp + 1) = thunk->this;
1034 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1035 TRACE( "emulating ATL thunk at %p, func=%08lx arg=%08lx\n",
1036 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1041 /***********************************************************************
1044 * Setup a proper stack frame for the raise function, and modify the
1045 * sigcontext so that the return from the signal handler will call
1046 * the raise function.
1048 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1052 void *ret_addr; /* return address from raise_func */
1053 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1054 CONTEXT *context_ptr; /* second arg for raise_func */
1056 EXCEPTION_RECORD rec;
1063 stack = init_handler( sigcontext, &fs, &gs );
1065 /* stack sanity checks */
1067 if ((char *)stack >= (char *)get_signal_stack() &&
1068 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1070 ERR( "nested exception on signal stack in thread %04lx eip %08x esp %08x stack %p-%p\n",
1071 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1072 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1073 NtCurrentTeb()->Tib.StackBase );
1074 server_abort_thread(1);
1077 if (stack - 1 > stack || /* check for overflow in subtraction */
1078 (char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit ||
1079 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1081 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)stack;
1084 ERR( "stack overflow %u bytes in thread %04lx eip %08x esp %08x stack %p-%p\n",
1085 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1086 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1087 NtCurrentTeb()->Tib.StackBase );
1088 server_abort_thread(1);
1090 else WARN( "exception outside of stack limits in thread %04lx eip %08x esp %08x stack %p-%p\n",
1091 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1092 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1093 NtCurrentTeb()->Tib.StackBase );
1096 stack--; /* push the stack_layout structure */
1097 #ifdef HAVE_VALGRIND_MEMCHECK_H
1098 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1100 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1101 stack->rec_ptr = &stack->rec;
1102 stack->context_ptr = &stack->context;
1104 stack->rec.ExceptionRecord = NULL;
1105 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1106 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1107 stack->rec.NumberParameters = 0;
1109 save_context( &stack->context, sigcontext, fs, gs );
1111 /* now modify the sigcontext to return to the raise function */
1112 ESP_sig(sigcontext) = (DWORD)stack;
1113 EIP_sig(sigcontext) = (DWORD)func;
1114 EFL_sig(sigcontext) &= ~0x100; /* clear single-step flag */
1115 CS_sig(sigcontext) = wine_get_cs();
1116 DS_sig(sigcontext) = wine_get_ds();
1117 ES_sig(sigcontext) = wine_get_es();
1118 FS_sig(sigcontext) = wine_get_fs();
1119 GS_sig(sigcontext) = wine_get_gs();
1120 SS_sig(sigcontext) = wine_get_ss();
1122 return stack->rec_ptr;
1126 /***********************************************************************
1127 * get_exception_context
1129 * Get a pointer to the context built by setup_exception.
1131 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1133 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1137 /**********************************************************************
1140 * Get the FPU exception code from the FPU status.
1142 static inline DWORD get_fpu_code( const CONTEXT *context )
1144 DWORD status = context->FloatSave.StatusWord;
1146 if (status & 0x01) /* IE */
1148 if (status & 0x40) /* SF */
1149 return EXCEPTION_FLT_STACK_CHECK;
1151 return EXCEPTION_FLT_INVALID_OPERATION;
1153 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1154 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1155 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1156 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1157 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1158 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1162 /**********************************************************************
1163 * raise_segv_exception
1165 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1167 switch(rec->ExceptionCode)
1169 case EXCEPTION_ACCESS_VIOLATION:
1170 if (rec->NumberParameters == 2)
1172 if ((rec->ExceptionInformation[0] == 8) && check_atl_thunk( rec, context )) goto done;
1173 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1176 case EXCEPTION_DATATYPE_MISALIGNMENT:
1177 /* FIXME: pass through exception handler first? */
1178 if (context->EFlags & 0x00040000)
1180 /* Disable AC flag, return */
1181 context->EFlags &= ~0x00040000;
1186 __regs_RtlRaiseException( rec, context );
1188 NtSetContextThread( GetCurrentThread(), context );
1192 /**********************************************************************
1193 * raise_trap_exception
1195 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1197 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1199 if (context->EFlags & 0x100)
1201 context->EFlags &= ~0x100; /* clear single-step flag */
1203 else /* hardware breakpoint, fetch the debug registers */
1205 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1206 NtGetContextThread(GetCurrentThread(), context);
1207 /* do we really have a bp from a debug register ?
1208 * if not, then someone did a kill(SIGTRAP) on us, and we
1209 * shall return a breakpoint, not a single step exception
1211 if (!(context->Dr6 & 0xf)) rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1212 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1216 __regs_RtlRaiseException( rec, context );
1217 NtSetContextThread( GetCurrentThread(), context );
1221 /**********************************************************************
1224 * Generic raise function for exceptions that don't need special treatment.
1226 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1228 __regs_RtlRaiseException( rec, context );
1229 NtSetContextThread( GetCurrentThread(), context );
1234 /**********************************************************************
1235 * raise_vm86_sti_exception
1237 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1239 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1240 NtCurrentTeb()->vm86_pending |= VIP_MASK;
1242 if (ntdll_get_thread_data()->vm86_ptr)
1244 if (((char*)context->Eip >= (char*)vm86_return) &&
1245 ((char*)context->Eip <= (char*)vm86_return_end) &&
1246 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1247 /* exiting from VM86, can't throw */
1250 merge_vm86_pending_flags( rec );
1252 else if (NtCurrentTeb()->dpmi_vif &&
1253 !wine_ldt_is_system(context->SegCs) &&
1254 !wine_ldt_is_system(context->SegSs))
1256 /* Executing DPMI code and virtual interrupts are enabled. */
1257 NtCurrentTeb()->vm86_pending = 0;
1258 __regs_RtlRaiseException( rec, context );
1261 NtSetContextThread( GetCurrentThread(), context );
1265 /**********************************************************************
1268 * Handler for SIGUSR2.
1269 * We use it to signal that the running __wine_enter_vm86() should
1270 * immediately set VIP_MASK, causing pending events to be handled
1271 * as early as possible.
1273 static HANDLER_DEF(usr2_handler)
1275 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_vm86_sti_exception );
1276 rec->ExceptionCode = EXCEPTION_VM86_STI;
1278 #endif /* __HAVE_VM86 */
1281 /**********************************************************************
1284 * Handler for SIGSEGV and related errors.
1286 static HANDLER_DEF(segv_handler)
1288 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_segv_exception );
1290 switch(get_trap_code(HANDLER_CONTEXT))
1292 case TRAP_x86_OFLOW: /* Overflow exception */
1293 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1295 case TRAP_x86_BOUND: /* Bound range exception */
1296 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1298 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1299 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1301 case TRAP_x86_STKFLT: /* Stack fault */
1302 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1304 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1305 case TRAP_x86_PROTFLT: /* General protection fault */
1306 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1307 if (!get_error_code(HANDLER_CONTEXT) && is_privileged_instr( get_exception_context(rec) ))
1308 rec->ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1311 WORD err = get_error_code(HANDLER_CONTEXT);
1312 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1313 rec->NumberParameters = 2;
1314 rec->ExceptionInformation[0] = 0;
1315 /* if error contains a LDT selector, use that as fault address */
1316 rec->ExceptionInformation[1] = (err & 7) == 4 ? (err & ~7) : 0xffffffff;
1319 case TRAP_x86_PAGEFLT: /* Page fault */
1320 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1321 #ifdef FAULT_ADDRESS
1322 rec->NumberParameters = 2;
1323 rec->ExceptionInformation[0] = (get_error_code(HANDLER_CONTEXT) >> 1) & 0x09;
1324 rec->ExceptionInformation[1] = (ULONG_PTR)FAULT_ADDRESS;
1327 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1328 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1331 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
1333 case TRAP_x86_NMI: /* NMI interrupt */
1334 case TRAP_x86_DNA: /* Device not available exception */
1335 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1336 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1337 case TRAP_x86_MCHK: /* Machine check exception */
1338 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1339 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1345 /**********************************************************************
1348 * Handler for SIGTRAP.
1350 static HANDLER_DEF(trap_handler)
1352 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_trap_exception );
1354 switch(get_trap_code(HANDLER_CONTEXT))
1356 case TRAP_x86_TRCTRAP: /* Single-step exception */
1357 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1359 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1360 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1363 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1369 /**********************************************************************
1372 * Handler for SIGFPE.
1374 static HANDLER_DEF(fpe_handler)
1376 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_exception );
1379 context = get_exception_context( rec );
1381 switch(get_trap_code(HANDLER_CONTEXT))
1383 case TRAP_x86_DIVIDE: /* Division by zero exception */
1384 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1386 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1387 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1389 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1390 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1391 rec->ExceptionCode = get_fpu_code( context );
1394 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
1395 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1401 /**********************************************************************
1404 * Handler for SIGINT.
1406 * FIXME: should not be calling external functions on the signal stack.
1408 static HANDLER_DEF(int_handler)
1411 init_handler( HANDLER_CONTEXT, &fs, &gs );
1412 if (!dispatch_signal(SIGINT))
1414 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_exception );
1415 rec->ExceptionCode = CONTROL_C_EXIT;
1419 /**********************************************************************
1422 * Handler for SIGABRT.
1424 static HANDLER_DEF(abrt_handler)
1426 EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_exception );
1427 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1428 rec->ExceptionFlags = EH_NONCONTINUABLE;
1432 /**********************************************************************
1435 * Handler for SIGTERM.
1437 static HANDLER_DEF(term_handler)
1440 init_handler( HANDLER_CONTEXT, &fs, &gs );
1441 server_abort_thread(0);
1445 /**********************************************************************
1448 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1450 static HANDLER_DEF(usr1_handler)
1455 init_handler( HANDLER_CONTEXT, &fs, &gs );
1456 save_context( &context, HANDLER_CONTEXT, fs, gs );
1457 wait_suspend( &context );
1458 restore_context( &context, HANDLER_CONTEXT );
1462 /**********************************************************************
1463 * get_signal_stack_total_size
1465 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1466 * Must be a power of two.
1468 size_t get_signal_stack_total_size(void)
1470 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1472 if (!signal_stack_size)
1474 size_t size = 4096, min_size = teb_size + max( MINSIGSTKSZ, 4096 );
1475 /* find the first power of two not smaller than min_size */
1476 while (size < min_size) size *= 2;
1477 signal_stack_mask = size - 1;
1478 signal_stack_size = size - teb_size;
1480 return signal_stack_size + teb_size;
1484 /***********************************************************************
1487 * Set a signal handler
1489 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
1491 struct sigaction sig_act;
1494 if (!have_sigaltstack)
1496 struct kernel_sigaction sig_act;
1497 sig_act.ksa_handler = func;
1498 sig_act.ksa_flags = SA_RESTART;
1499 sig_act.ksa_mask = (1 << (SIGINT-1)) |
1500 (1 << (SIGUSR1-1)) |
1502 /* point to the top of the signal stack */
1503 sig_act.ksa_restorer = (char *)get_signal_stack() + signal_stack_size;
1504 return wine_sigaction( sig, &sig_act, NULL );
1507 sig_act.sa_handler = func;
1508 sigemptyset( &sig_act.sa_mask );
1509 sigaddset( &sig_act.sa_mask, SIGINT );
1510 sigaddset( &sig_act.sa_mask, SIGUSR1 );
1511 sigaddset( &sig_act.sa_mask, SIGUSR2 );
1514 sig_act.sa_flags = SA_RESTART;
1515 #elif defined (__svr4__) || defined(_SCO_DS) || defined(__APPLE__) || \
1516 defined(__NetBSD__) || defined(__OpenBSD__) || \
1517 defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1518 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1520 sig_act.sa_flags = 0;
1524 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1526 return sigaction( sig, &sig_act, NULL );
1530 /***********************************************************************
1531 * __wine_set_signal_handler (NTDLL.@)
1533 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1535 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1536 if (handlers[sig] != NULL) return -2;
1537 handlers[sig] = wsh;
1542 /**********************************************************************
1545 BOOL SIGNAL_Init(void)
1547 int have_sigaltstack = 0;
1549 #ifdef HAVE_SIGALTSTACK
1550 struct sigaltstack ss;
1551 ss.ss_sp = get_signal_stack();
1552 ss.ss_size = signal_stack_size;
1554 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1556 /* sigaltstack may fail because the kernel is too old, or
1557 because glibc is brain-dead. In the latter case a
1558 direct system call should succeed. */
1559 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1561 #endif /* HAVE_SIGALTSTACK */
1563 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1564 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1565 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1566 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1567 if (set_handler( SIGABRT, have_sigaltstack, (void (*)())abrt_handler ) == -1) goto error;
1568 if (set_handler( SIGTERM, have_sigaltstack, (void (*)())term_handler ) == -1) goto error;
1569 if (set_handler( SIGUSR1, have_sigaltstack, (void (*)())usr1_handler ) == -1) goto error;
1571 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1574 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1578 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1584 perror("sigaction");
1590 /**********************************************************************
1591 * __wine_enter_vm86 (NTDLL.@)
1593 * Enter vm86 mode with the specified register context.
1595 void __wine_enter_vm86( CONTEXT *context )
1597 EXCEPTION_RECORD rec;
1599 struct vm86plus_struct vm86;
1601 memset( &vm86, 0, sizeof(vm86) );
1604 restore_vm86_context( context, &vm86 );
1606 ntdll_get_thread_data()->vm86_ptr = &vm86;
1607 merge_vm86_pending_flags( &rec );
1609 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1616 save_vm86_context( context, &vm86 );
1618 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1619 rec.ExceptionRecord = NULL;
1620 rec.ExceptionAddress = (LPVOID)context->Eip;
1621 rec.NumberParameters = 0;
1623 switch(VM86_TYPE(res))
1625 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1626 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1627 raise_segv_exception( &rec, context );
1629 case VM86_TRAP: /* return due to DOS-debugger request */
1630 switch(VM86_ARG(res))
1632 case TRAP_x86_TRCTRAP: /* Single-step exception */
1633 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1635 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1636 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1639 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1643 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1644 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1645 rec.NumberParameters = 1;
1646 rec.ExceptionInformation[0] = VM86_ARG(res);
1648 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1649 context->EFlags |= VIF_MASK;
1650 context->EFlags &= ~VIP_MASK;
1651 NtCurrentTeb()->vm86_pending = 0;
1652 rec.ExceptionCode = EXCEPTION_VM86_STI;
1654 case VM86_PICRETURN: /* return due to pending PIC request */
1655 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1657 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1659 ERR( "unhandled result from vm86 mode %x\n", res );
1662 __regs_RtlRaiseException( &rec, context );
1666 #else /* __HAVE_VM86 */
1667 /**********************************************************************
1668 * __wine_enter_vm86 (NTDLL.@)
1670 void __wine_enter_vm86( CONTEXT *context )
1672 MESSAGE("vm86 mode not supported on this platform\n");
1674 #endif /* __HAVE_VM86 */
1676 /**********************************************************************
1677 * DbgBreakPoint (NTDLL.@)
1679 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1681 /**********************************************************************
1682 * DbgUserBreakPoint (NTDLL.@)
1684 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1687 /**********************************************************************
1688 * EXC_CallHandler (internal)
1690 * Some exception handlers depend on EBP to have a fixed position relative to
1691 * the exception frame.
1692 * Shrinker depends on (*1) doing what it does,
1693 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1694 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1695 * function calling the handler having only 5 parameters (*4).
1697 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1699 " movl %esp, %ebp\n"
1701 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1707 " call " __ASM_NAME("call_exception_handler") "\n"
1711 __ASM_GLOBAL_FUNC(call_exception_handler,
1713 " movl %esp, %ebp\n"
1715 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1716 " pushl %edx\n" /* handler be handled by... */
1718 " pushl (0)\n" /* nested_handler (passed in edx). */
1720 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1725 " movl 24(%ebp), %ecx\n" /* (*1) */
1726 " call *%ecx\n" /* call handler. (*2) */
1728 " movl (0), %esp\n" /* restore previous... (*3) */
1730 " popl (0)\n" /* exception frame. */
1731 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1733 " ret $20\n" /* (*4) */
1735 #endif /* __i386__ */