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>
59 #include "wine/library.h"
60 #include "ntdll_misc.h"
61 #include "wine/exception.h"
62 #include "wine/debug.h"
64 #ifdef HAVE_VALGRIND_MEMCHECK_H
65 #include <valgrind/memcheck.h>
68 #undef ERR /* Solaris needs to define this */
70 /* not defined for x86, so copy the x86_64 definition */
71 typedef struct DECLSPEC_ALIGN(16) _M128A
92 M128A FloatRegisters[8];
93 M128A XmmRegisters[16];
97 /***********************************************************************
98 * signal context platform-specific definitions
103 typedef ucontext_t SIGCONTEXT;
105 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
106 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
107 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
108 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
109 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
110 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
111 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
112 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
114 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
115 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
116 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
117 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
118 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
119 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
121 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
122 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
123 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
124 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
126 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
127 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
129 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
130 #define VIF_FLAG 0x00080000
131 #define VIP_FLAG 0x00100000
133 int vm86_enter( void **vm86_ptr );
134 void vm86_return(void);
135 void vm86_return_end(void);
136 __ASM_GLOBAL_FUNC(vm86_enter,
138 "movl %esp, %ebp\n\t"
139 "movl $166,%eax\n\t" /*SYS_vm86*/
140 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
141 "movl (%ecx),%ecx\n\t"
143 "movl $1,%ebx\n\t" /*VM86_ENTER*/
144 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
148 ".globl " __ASM_NAME("vm86_return") "\n\t"
149 __ASM_FUNC("vm86_return") "\n"
150 __ASM_NAME("vm86_return") ":\n\t"
156 "testl %eax,%eax\n\t"
158 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
159 "je " __ASM_NAME("vm86_enter") "\n\t"
161 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
163 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
164 __ASM_FUNC("vm86_return_end") "\n"
165 __ASM_NAME("vm86_return_end") ":\n\t"
168 #ifdef HAVE_SYS_VM86_H
176 #include <machine/frame.h>
177 typedef struct trapframe SIGCONTEXT;
179 #define EAX_sig(context) ((context)->tf_eax)
180 #define EBX_sig(context) ((context)->tf_ebx)
181 #define ECX_sig(context) ((context)->tf_ecx)
182 #define EDX_sig(context) ((context)->tf_edx)
183 #define ESI_sig(context) ((context)->tf_esi)
184 #define EDI_sig(context) ((context)->tf_edi)
185 #define EBP_sig(context) ((context)->tf_ebp)
187 #define CS_sig(context) ((context)->tf_cs)
188 #define DS_sig(context) ((context)->tf_ds)
189 #define ES_sig(context) ((context)->tf_es)
190 #define SS_sig(context) ((context)->tf_ss)
192 #define EFL_sig(context) ((context)->tf_eflags)
194 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
195 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
197 #define FPU_sig(context) NULL /* FIXME */
198 #define FPUX_sig(context) NULL /* FIXME */
202 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
204 typedef struct sigcontext SIGCONTEXT;
206 #define EAX_sig(context) ((context)->sc_eax)
207 #define EBX_sig(context) ((context)->sc_ebx)
208 #define ECX_sig(context) ((context)->sc_ecx)
209 #define EDX_sig(context) ((context)->sc_edx)
210 #define ESI_sig(context) ((context)->sc_esi)
211 #define EDI_sig(context) ((context)->sc_edi)
212 #define EBP_sig(context) ((context)->sc_ebp)
214 #define CS_sig(context) ((context)->sc_cs)
215 #define DS_sig(context) ((context)->sc_ds)
216 #define ES_sig(context) ((context)->sc_es)
217 #define FS_sig(context) ((context)->sc_fs)
218 #define GS_sig(context) ((context)->sc_gs)
219 #define SS_sig(context) ((context)->sc_ss)
221 #define TRAP_sig(context) ((context)->sc_trapno)
222 #define ERROR_sig(context) ((context)->sc_err)
223 #define EFL_sig(context) ((context)->sc_eflags)
225 #define EIP_sig(context) ((context)->sc_eip)
226 #define ESP_sig(context) ((context)->sc_esp)
228 #define FPU_sig(context) NULL /* FIXME */
229 #define FPUX_sig(context) NULL /* FIXME */
233 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
236 #include <sys/regset.h>
238 #include <sys/ucontext.h>
239 typedef struct ucontext SIGCONTEXT;
245 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
246 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
247 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
248 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
249 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
250 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
251 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
253 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
254 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
255 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
256 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
258 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
259 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
261 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
263 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
265 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
267 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
269 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
272 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
275 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
278 #define FPU_sig(context) NULL /* FIXME */
279 #define FPUX_sig(context) NULL /* FIXME */
281 #endif /* svr4 || SCO_DS */
284 # include <sys/ucontext.h>
286 typedef ucontext_t SIGCONTEXT;
288 /* work around silly renaming of struct members in OS X 10.5 */
289 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
290 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
291 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
292 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
293 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
294 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
295 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
296 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
297 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
298 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
299 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
300 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
301 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
302 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
303 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
304 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
305 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
306 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
307 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
308 #define FPU_sig(context) NULL
309 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
311 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
312 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
313 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
314 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
315 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
316 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
317 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
318 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
319 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
320 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
321 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
322 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
323 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
324 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
325 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
326 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
327 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
328 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
329 #define FPU_sig(context) NULL
330 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
333 #endif /* __APPLE__ */
335 WINE_DEFAULT_DEBUG_CHANNEL(seh);
337 typedef int (*wine_signal_handler)(unsigned int sig);
339 static size_t signal_stack_mask;
340 static size_t signal_stack_size;
342 static wine_signal_handler handlers[256];
344 static int fpux_support; /* whether the CPU support extended fpu context */
346 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
350 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
351 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
352 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
353 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
354 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
355 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
356 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
357 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
358 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
359 TRAP_x86_DNA = T_DNA, /* Device not available exception */
360 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
361 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
362 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
363 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
364 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
365 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
366 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
367 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
368 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
369 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
370 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
372 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
373 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
374 TRAP_x86_NMI = 2, /* NMI interrupt */
375 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
376 TRAP_x86_OFLOW = 4, /* Overflow exception */
377 TRAP_x86_BOUND = 5, /* Bound range exception */
378 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
379 TRAP_x86_DNA = 7, /* Device not available exception */
380 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
381 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
382 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
383 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
384 TRAP_x86_STKFLT = 12, /* Stack fault */
385 TRAP_x86_PROTFLT = 13, /* General protection fault */
386 TRAP_x86_PAGEFLT = 14, /* Page fault */
387 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
388 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
389 TRAP_x86_MCHK = 18, /* Machine check exception */
390 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
391 otherwise Cache flush exception (via SIGSEV) */
396 /***********************************************************************
399 static inline int dispatch_signal(unsigned int sig)
401 if (handlers[sig] == NULL) return 0;
402 return handlers[sig](sig);
406 /***********************************************************************
409 * Get the trap code for a signal.
411 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
414 return TRAP_sig(sigcontext);
416 return TRAP_x86_UNKNOWN; /* unknown trap code */
420 /***********************************************************************
423 * Get the error code for a signal.
425 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
428 return ERROR_sig(sigcontext);
434 /***********************************************************************
437 * Get the base of the signal stack for the current thread.
439 static inline void *get_signal_stack(void)
441 return (char *)NtCurrentTeb() + 4096;
445 /***********************************************************************
448 * Get the current teb based on the stack pointer.
450 static inline TEB *get_current_teb(void)
453 __asm__("movl %%esp,%0" : "=g" (esp) );
454 return (TEB *)(esp & ~signal_stack_mask);
459 /***********************************************************************
462 * Set the register values from a vm86 structure.
464 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
466 context->ContextFlags = CONTEXT_FULL;
467 context->Eax = vm86->regs.eax;
468 context->Ebx = vm86->regs.ebx;
469 context->Ecx = vm86->regs.ecx;
470 context->Edx = vm86->regs.edx;
471 context->Esi = vm86->regs.esi;
472 context->Edi = vm86->regs.edi;
473 context->Esp = vm86->regs.esp;
474 context->Ebp = vm86->regs.ebp;
475 context->Eip = vm86->regs.eip;
476 context->SegCs = vm86->regs.cs;
477 context->SegDs = vm86->regs.ds;
478 context->SegEs = vm86->regs.es;
479 context->SegFs = vm86->regs.fs;
480 context->SegGs = vm86->regs.gs;
481 context->SegSs = vm86->regs.ss;
482 context->EFlags = vm86->regs.eflags;
486 /***********************************************************************
487 * restore_vm86_context
489 * Build a vm86 structure from the register values.
491 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
493 vm86->regs.eax = context->Eax;
494 vm86->regs.ebx = context->Ebx;
495 vm86->regs.ecx = context->Ecx;
496 vm86->regs.edx = context->Edx;
497 vm86->regs.esi = context->Esi;
498 vm86->regs.edi = context->Edi;
499 vm86->regs.esp = context->Esp;
500 vm86->regs.ebp = context->Ebp;
501 vm86->regs.eip = context->Eip;
502 vm86->regs.cs = context->SegCs;
503 vm86->regs.ds = context->SegDs;
504 vm86->regs.es = context->SegEs;
505 vm86->regs.fs = context->SegFs;
506 vm86->regs.gs = context->SegGs;
507 vm86->regs.ss = context->SegSs;
508 vm86->regs.eflags = context->EFlags;
512 /**********************************************************************
513 * merge_vm86_pending_flags
515 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
516 * raises exception if there are pending events and VIF flag
517 * has been turned on.
519 * Called from __wine_enter_vm86 because vm86_enter
520 * doesn't check for pending events.
522 * Called from raise_vm86_sti_exception to check for
523 * pending events in a signal safe way.
525 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
527 BOOL check_pending = TRUE;
528 struct vm86plus_struct *vm86 =
529 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
532 * In order to prevent a race when SIGUSR2 occurs while
533 * we are returning from exception handler, pending events
534 * will be rechecked after each raised exception.
536 while (check_pending && get_vm86_teb_info()->vm86_pending)
538 check_pending = FALSE;
539 ntdll_get_thread_data()->vm86_ptr = NULL;
542 * If VIF is set, throw exception.
543 * Note that SIGUSR2 may turn VIF flag off so
544 * VIF check must occur only when TEB.vm86_ptr is NULL.
546 if (vm86->regs.eflags & VIF_FLAG)
549 save_vm86_context( &vcontext, vm86 );
551 rec->ExceptionCode = EXCEPTION_VM86_STI;
552 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
553 rec->ExceptionRecord = NULL;
554 rec->NumberParameters = 0;
555 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
557 vcontext.EFlags &= ~VIP_FLAG;
558 get_vm86_teb_info()->vm86_pending = 0;
559 __regs_RtlRaiseException( rec, &vcontext );
561 restore_vm86_context( &vcontext, vm86 );
562 check_pending = TRUE;
565 ntdll_get_thread_data()->vm86_ptr = vm86;
569 * Merge VIP flags in a signal safe way. This requires
570 * that the following operation compiles into atomic
573 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
575 #endif /* __HAVE_VM86 */
580 /* We have to workaround two Solaris breakages:
581 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
583 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
584 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
587 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
588 __ASM_GLOBAL_FUNC( sigaction_syscall,
591 "movl $0x62,%eax\n\t"
592 "add $[2f-1b],%edx\n\t"
597 /* assume the same libc handler is used for all signals */
598 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
600 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
602 struct ntdll_thread_data *thread_data;
604 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
606 thread_data = (struct ntdll_thread_data *)get_current_teb()->SystemReserved2;
607 wine_set_fs( thread_data->fs );
608 wine_set_gs( thread_data->gs );
610 libc_sigacthandler( signal, siginfo, sigcontext );
613 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
615 struct sigaction real_act;
617 if (sigaction( sig, new, old ) == -1) return -1;
619 /* retrieve the real handler and flags with a direct syscall */
620 sigaction_syscall( sig, NULL, &real_act );
621 libc_sigacthandler = real_act.sa_sigaction;
622 real_act.sa_sigaction = wine_sigacthandler;
623 sigaction_syscall( sig, &real_act, NULL );
626 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
630 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
633 /***********************************************************************
636 * Handler initialization when the full context is not needed.
637 * Return the stack pointer to use for pushing the exception data.
639 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
641 TEB *teb = get_current_teb();
642 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
644 /* get %fs and %gs at time of the fault */
646 *fs = LOWORD(FS_sig(sigcontext));
651 *gs = LOWORD(GS_sig(sigcontext));
656 #ifndef __sun /* see above for Solaris handling */
657 wine_set_fs( thread_data->fs );
658 wine_set_gs( thread_data->gs );
661 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
662 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
665 * Win16 or DOS protected mode. Note that during switch
666 * from 16-bit mode to linear mode, CS may be set to system
667 * segment before FS is restored. Fortunately, in this case
668 * SS is still non-system segment. This is why both CS and SS
671 return teb->WOW32Reserved;
673 return (void *)(ESP_sig(sigcontext) & ~3);
677 /***********************************************************************
680 * Save the thread FPU context.
682 static inline void save_fpu( CONTEXT *context )
685 context->ContextFlags |= CONTEXT_FLOATING_POINT;
686 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
691 /***********************************************************************
694 * Save the thread FPU extended context.
696 static inline void save_fpux( CONTEXT *context )
699 /* we have to enforce alignment by hand */
700 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
701 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
703 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
704 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
705 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
710 /***********************************************************************
713 * Restore the FPU context to a sigcontext.
715 static inline void restore_fpu( const CONTEXT *context )
717 FLOATING_SAVE_AREA float_status = context->FloatSave;
718 /* reset the current interrupt status */
719 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
721 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
722 #endif /* __GNUC__ */
726 /***********************************************************************
729 * Restore the FPU extended context to a sigcontext.
731 static inline void restore_fpux( const CONTEXT *context )
734 /* we have to enforce alignment by hand */
735 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
736 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
738 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
739 /* reset the current interrupt status */
740 state->StatusWord &= state->ControlWord | 0xff80;
741 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
746 /***********************************************************************
749 * Build a standard FPU context from an extended one.
751 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
753 unsigned int i, tag, stack_top;
755 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
756 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
757 fpu->ErrorOffset = fpux->ErrorOffset;
758 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
759 fpu->DataOffset = fpux->DataOffset;
760 fpu->DataSelector = fpux->DataSelector;
761 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
763 stack_top = (fpux->StatusWord >> 11) & 7;
764 fpu->TagWord = 0xffff0000;
765 for (i = 0; i < 8; i++)
767 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
768 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
771 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
772 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
774 tag = 2; /* special */
776 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
778 if (reg->Low) tag = 2; /* special */
779 else tag = 1; /* zero */
783 if (reg->Low >> 63) tag = 0; /* valid */
784 else tag = 2; /* special */
787 fpu->TagWord |= tag << (2 * i);
792 /***********************************************************************
795 * Build a context structure from the signal info.
797 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
799 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
800 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
801 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
803 memset(context, 0, sizeof(*context));
804 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
805 context->Eax = EAX_sig(sigcontext);
806 context->Ebx = EBX_sig(sigcontext);
807 context->Ecx = ECX_sig(sigcontext);
808 context->Edx = EDX_sig(sigcontext);
809 context->Esi = ESI_sig(sigcontext);
810 context->Edi = EDI_sig(sigcontext);
811 context->Ebp = EBP_sig(sigcontext);
812 context->EFlags = EFL_sig(sigcontext);
813 context->Eip = EIP_sig(sigcontext);
814 context->Esp = ESP_sig(sigcontext);
815 context->SegCs = LOWORD(CS_sig(sigcontext));
816 context->SegDs = LOWORD(DS_sig(sigcontext));
817 context->SegEs = LOWORD(ES_sig(sigcontext));
820 context->SegSs = LOWORD(SS_sig(sigcontext));
821 context->Dr0 = regs->dr0;
822 context->Dr1 = regs->dr1;
823 context->Dr2 = regs->dr2;
824 context->Dr3 = regs->dr3;
825 context->Dr6 = regs->dr6;
826 context->Dr7 = regs->dr7;
830 context->ContextFlags |= CONTEXT_FLOATING_POINT;
831 context->FloatSave = *fpu;
835 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
836 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
838 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
840 if (!fpu && !fpux) save_fpu( context );
844 /***********************************************************************
847 * Restore the signal info from the context.
849 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
851 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
852 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
853 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
855 regs->dr0 = context->Dr0;
856 regs->dr1 = context->Dr1;
857 regs->dr2 = context->Dr2;
858 regs->dr3 = context->Dr3;
859 regs->dr6 = context->Dr6;
860 regs->dr7 = context->Dr7;
861 EAX_sig(sigcontext) = context->Eax;
862 EBX_sig(sigcontext) = context->Ebx;
863 ECX_sig(sigcontext) = context->Ecx;
864 EDX_sig(sigcontext) = context->Edx;
865 ESI_sig(sigcontext) = context->Esi;
866 EDI_sig(sigcontext) = context->Edi;
867 EBP_sig(sigcontext) = context->Ebp;
868 EFL_sig(sigcontext) = context->EFlags;
869 EIP_sig(sigcontext) = context->Eip;
870 ESP_sig(sigcontext) = context->Esp;
871 CS_sig(sigcontext) = context->SegCs;
872 DS_sig(sigcontext) = context->SegDs;
873 ES_sig(sigcontext) = context->SegEs;
874 SS_sig(sigcontext) = context->SegSs;
876 GS_sig(sigcontext) = context->SegGs;
878 wine_set_gs( context->SegGs );
881 FS_sig(sigcontext) = context->SegFs;
883 wine_set_fs( context->SegFs );
886 if (fpu) *fpu = context->FloatSave;
887 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
888 if (!fpu && !fpux) restore_fpu( context );
892 /***********************************************************************
895 * Register function to get the context of the current thread.
897 void WINAPI __regs_get_cpu_context( CONTEXT *context, CONTEXT *regs )
900 if (fpux_support) save_fpux( context );
901 else save_fpu( context );
903 DEFINE_REGS_ENTRYPOINT( get_cpu_context, 4, 4 )
906 /***********************************************************************
909 * Set the new CPU context. Used by NtSetContextThread.
911 void set_cpu_context( const CONTEXT *context )
913 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
915 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
916 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
918 if (flags & CONTEXT_DEBUG_REGISTERS)
920 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
921 regs->dr0 = context->Dr0;
922 regs->dr1 = context->Dr1;
923 regs->dr2 = context->Dr2;
924 regs->dr3 = context->Dr3;
925 regs->dr6 = context->Dr6;
926 regs->dr7 = context->Dr7;
928 if (flags & CONTEXT_FULL)
930 if (!(flags & CONTEXT_CONTROL))
931 FIXME( "setting partial context (%x) not supported\n", flags );
932 else if (flags & CONTEXT_SEGMENTS)
933 __wine_call_from_32_restore_regs( context );
936 CONTEXT newcontext = *context;
937 newcontext.SegDs = wine_get_ds();
938 newcontext.SegEs = wine_get_es();
939 newcontext.SegFs = wine_get_fs();
940 newcontext.SegGs = wine_get_gs();
941 __wine_call_from_32_restore_regs( &newcontext );
947 /***********************************************************************
948 * is_privileged_instr
950 * Check if the fault location is a privileged instruction.
951 * Based on the instruction emulation code in dlls/kernel/instr.c.
953 static inline DWORD is_privileged_instr( CONTEXT86 *context )
956 unsigned int prefix_count = 0;
958 if (!wine_ldt_is_system( context->SegCs )) return 0;
959 instr = (BYTE *)context->Eip;
961 for (;;) switch(*instr)
963 /* instruction prefixes */
964 case 0x2e: /* %cs: */
965 case 0x36: /* %ss: */
966 case 0x3e: /* %ds: */
967 case 0x26: /* %es: */
968 case 0x64: /* %fs: */
969 case 0x65: /* %gs: */
970 case 0x66: /* opcode size */
971 case 0x67: /* addr size */
972 case 0xf0: /* lock */
973 case 0xf2: /* repne */
974 case 0xf3: /* repe */
975 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
979 case 0x0f: /* extended instruction */
982 case 0x20: /* mov crX, reg */
983 case 0x21: /* mov drX, reg */
984 case 0x22: /* mov reg, crX */
985 case 0x23: /* mov reg drX */
986 return EXCEPTION_PRIV_INSTRUCTION;
989 case 0x6c: /* insb (%dx) */
990 case 0x6d: /* insl (%dx) */
991 case 0x6e: /* outsb (%dx) */
992 case 0x6f: /* outsl (%dx) */
993 case 0xcd: /* int $xx */
994 case 0xe4: /* inb al,XX */
995 case 0xe5: /* in (e)ax,XX */
996 case 0xe6: /* outb XX,al */
997 case 0xe7: /* out XX,(e)ax */
998 case 0xec: /* inb (%dx),%al */
999 case 0xed: /* inl (%dx),%eax */
1000 case 0xee: /* outb %al,(%dx) */
1001 case 0xef: /* outl %eax,(%dx) */
1002 case 0xf4: /* hlt */
1003 case 0xfa: /* cli */
1004 case 0xfb: /* sti */
1005 return EXCEPTION_PRIV_INSTRUCTION;
1012 #include "pshpack1.h"
1015 DWORD movl; /* movl this,4(%esp) */
1017 BYTE jmp; /* jmp func */
1020 #include "poppack.h"
1022 /**********************************************************************
1025 * Check if code destination is an ATL thunk, and emulate it if so.
1027 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1029 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1034 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1036 *((DWORD *)context->Esp + 1) = thunk->this;
1037 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1038 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1039 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1052 /***********************************************************************
1053 * setup_exception_record
1055 * Setup the exception record and context on the thread stack.
1057 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1058 WORD fs, WORD gs, raise_func func )
1062 void *ret_addr; /* return address from raise_func */
1063 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1064 CONTEXT *context_ptr; /* second arg for raise_func */
1066 EXCEPTION_RECORD rec;
1069 } *stack = stack_ptr;
1070 DWORD exception_code = 0;
1072 /* stack sanity checks */
1074 if ((char *)stack >= (char *)get_signal_stack() &&
1075 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1077 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1078 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1079 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1080 NtCurrentTeb()->Tib.StackBase );
1081 server_abort_thread(1);
1084 if (stack - 1 > stack || /* check for overflow in subtraction */
1085 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1086 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1088 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1089 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1090 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1091 NtCurrentTeb()->Tib.StackBase );
1093 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1095 /* stack overflow on last page, unrecoverable */
1096 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1097 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1098 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1099 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1100 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1101 server_abort_thread(1);
1103 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1105 /* stack access below stack limit, may be recoverable */
1106 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1109 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1110 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1111 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1112 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1113 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1114 server_abort_thread(1);
1118 stack--; /* push the stack_layout structure */
1119 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1120 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1121 #elif defined(VALGRIND_MAKE_WRITABLE)
1122 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1124 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1125 stack->rec_ptr = &stack->rec;
1126 stack->context_ptr = &stack->context;
1128 stack->rec.ExceptionRecord = NULL;
1129 stack->rec.ExceptionCode = exception_code;
1130 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1131 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1132 stack->rec.NumberParameters = 0;
1134 save_context( &stack->context, sigcontext, fs, gs );
1136 /* now modify the sigcontext to return to the raise function */
1137 ESP_sig(sigcontext) = (DWORD)stack;
1138 EIP_sig(sigcontext) = (DWORD)func;
1139 /* clear single-step, direction, and align check flag */
1140 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1141 CS_sig(sigcontext) = wine_get_cs();
1142 DS_sig(sigcontext) = wine_get_ds();
1143 ES_sig(sigcontext) = wine_get_es();
1144 FS_sig(sigcontext) = wine_get_fs();
1145 GS_sig(sigcontext) = wine_get_gs();
1146 SS_sig(sigcontext) = wine_get_ss();
1148 return stack->rec_ptr;
1152 /***********************************************************************
1155 * Setup a proper stack frame for the raise function, and modify the
1156 * sigcontext so that the return from the signal handler will call
1157 * the raise function.
1159 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1162 void *stack = init_handler( sigcontext, &fs, &gs );
1164 return setup_exception_record( sigcontext, stack, fs, gs, func );
1168 /***********************************************************************
1169 * get_exception_context
1171 * Get a pointer to the context built by setup_exception.
1173 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1175 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1179 /**********************************************************************
1182 * Get the FPU exception code from the FPU status.
1184 static inline DWORD get_fpu_code( const CONTEXT *context )
1186 DWORD status = context->FloatSave.StatusWord;
1188 if (status & 0x01) /* IE */
1190 if (status & 0x40) /* SF */
1191 return EXCEPTION_FLT_STACK_CHECK;
1193 return EXCEPTION_FLT_INVALID_OPERATION;
1195 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1196 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1197 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1198 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1199 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1200 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1204 /**********************************************************************
1205 * raise_segv_exception
1207 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1209 switch(rec->ExceptionCode)
1211 case EXCEPTION_ACCESS_VIOLATION:
1212 if (rec->NumberParameters == 2)
1214 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1216 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1219 case EXCEPTION_DATATYPE_MISALIGNMENT:
1220 /* FIXME: pass through exception handler first? */
1221 if (context->EFlags & 0x00040000)
1223 /* Disable AC flag, return */
1224 context->EFlags &= ~0x00040000;
1229 __regs_RtlRaiseException( rec, context );
1231 NtSetContextThread( GetCurrentThread(), context );
1235 /**********************************************************************
1236 * raise_trap_exception
1238 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1240 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1242 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1244 /* when single stepping can't tell whether this is a hw bp or a
1245 * single step interrupt. try to avoid as much overhead as possible
1246 * and only do a server call if there is any hw bp enabled. */
1248 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1250 /* (possible) hardware breakpoint, fetch the debug registers */
1251 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1252 NtGetContextThread(GetCurrentThread(), context);
1253 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1256 context->EFlags &= ~0x100; /* clear single-step flag */
1259 __regs_RtlRaiseException( rec, context );
1260 NtSetContextThread( GetCurrentThread(), context );
1264 /**********************************************************************
1267 * Generic raise function for exceptions that don't need special treatment.
1269 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1271 __regs_RtlRaiseException( rec, context );
1272 NtSetContextThread( GetCurrentThread(), context );
1277 /**********************************************************************
1278 * raise_vm86_sti_exception
1280 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1282 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1283 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1285 if (ntdll_get_thread_data()->vm86_ptr)
1287 if (((char*)context->Eip >= (char*)vm86_return) &&
1288 ((char*)context->Eip <= (char*)vm86_return_end) &&
1289 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1290 /* exiting from VM86, can't throw */
1293 merge_vm86_pending_flags( rec );
1295 else if (get_vm86_teb_info()->dpmi_vif &&
1296 !wine_ldt_is_system(context->SegCs) &&
1297 !wine_ldt_is_system(context->SegSs))
1299 /* Executing DPMI code and virtual interrupts are enabled. */
1300 get_vm86_teb_info()->vm86_pending = 0;
1301 __regs_RtlRaiseException( rec, context );
1304 NtSetContextThread( GetCurrentThread(), context );
1308 /**********************************************************************
1311 * Handler for SIGUSR2.
1312 * We use it to signal that the running __wine_enter_vm86() should
1313 * immediately set VIP_FLAG, causing pending events to be handled
1314 * as early as possible.
1316 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1318 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1319 rec->ExceptionCode = EXCEPTION_VM86_STI;
1321 #endif /* __HAVE_VM86 */
1324 /**********************************************************************
1327 * Handler for SIGSEGV and related errors.
1329 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1332 EXCEPTION_RECORD *rec;
1333 SIGCONTEXT *context = sigcontext;
1334 void *stack = init_handler( sigcontext, &fs, &gs );
1336 /* check for page fault inside the thread stack */
1337 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1338 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1339 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1340 virtual_handle_stack_fault( siginfo->si_addr ))
1342 /* check if this was the last guard page */
1343 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1345 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1346 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1351 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1352 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1354 switch(get_trap_code(context))
1356 case TRAP_x86_OFLOW: /* Overflow exception */
1357 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1359 case TRAP_x86_BOUND: /* Bound range exception */
1360 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1362 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1363 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1365 case TRAP_x86_STKFLT: /* Stack fault */
1366 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1368 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1369 case TRAP_x86_PROTFLT: /* General protection fault */
1370 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1372 WORD err = get_error_code(context);
1373 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1374 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1375 rec->NumberParameters = 2;
1376 rec->ExceptionInformation[0] = 0;
1377 /* if error contains a LDT selector, use that as fault address */
1378 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1379 rec->ExceptionInformation[1] = err & ~7;
1381 rec->ExceptionInformation[1] = 0xffffffff;
1384 case TRAP_x86_PAGEFLT: /* Page fault */
1385 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1386 rec->NumberParameters = 2;
1387 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1388 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1390 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1391 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1394 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1396 case TRAP_x86_NMI: /* NMI interrupt */
1397 case TRAP_x86_DNA: /* Device not available exception */
1398 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1399 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1400 case TRAP_x86_MCHK: /* Machine check exception */
1401 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1402 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1408 /**********************************************************************
1411 * Handler for SIGTRAP.
1413 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1415 SIGCONTEXT *context = sigcontext;
1416 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1418 switch(get_trap_code(context))
1420 case TRAP_x86_TRCTRAP: /* Single-step exception */
1421 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1423 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1424 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1427 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1433 /**********************************************************************
1436 * Handler for SIGFPE.
1438 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1440 CONTEXT *win_context;
1441 SIGCONTEXT *context = sigcontext;
1442 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1444 win_context = get_exception_context( rec );
1446 switch(get_trap_code(context))
1448 case TRAP_x86_DIVIDE: /* Division by zero exception */
1449 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1451 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1452 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1454 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1455 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1456 rec->ExceptionCode = get_fpu_code( win_context );
1458 case TRAP_x86_CACHEFLT: /* SIMD exception */
1460 * Behaviour only tested for divide-by-zero exceptions
1461 * Check for other SIMD exceptions as well */
1462 if(siginfo->si_code != FPE_FLTDIV)
1463 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1466 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1467 rec->NumberParameters = 1;
1468 /* no idea what meaning is actually behind this but that's what native does */
1469 rec->ExceptionInformation[0] = 0;
1472 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1473 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1479 /**********************************************************************
1482 * Handler for SIGINT.
1484 * FIXME: should not be calling external functions on the signal stack.
1486 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1489 init_handler( sigcontext, &fs, &gs );
1490 if (!dispatch_signal(SIGINT))
1492 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1493 rec->ExceptionCode = CONTROL_C_EXIT;
1497 /**********************************************************************
1500 * Handler for SIGABRT.
1502 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1504 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1505 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1506 rec->ExceptionFlags = EH_NONCONTINUABLE;
1510 /**********************************************************************
1513 * Handler for SIGQUIT.
1515 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1518 init_handler( sigcontext, &fs, &gs );
1519 server_abort_thread(0);
1523 /**********************************************************************
1526 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1528 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1533 init_handler( sigcontext, &fs, &gs );
1534 save_context( &context, sigcontext, fs, gs );
1535 wait_suspend( &context );
1536 restore_context( &context, sigcontext );
1540 /**********************************************************************
1541 * get_signal_stack_total_size
1543 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1544 * Must be a power of two.
1546 size_t get_signal_stack_total_size(void)
1548 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1550 if (!signal_stack_size)
1552 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1553 /* find the first power of two not smaller than min_size */
1554 while (size < min_size) size *= 2;
1555 signal_stack_mask = size - 1;
1556 signal_stack_size = size - teb_size;
1558 return signal_stack_size + teb_size;
1562 /***********************************************************************
1563 * __wine_set_signal_handler (NTDLL.@)
1565 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1567 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1568 if (handlers[sig] != NULL) return -2;
1569 handlers[sig] = wsh;
1574 /**********************************************************************
1575 * signal_init_thread
1577 void signal_init_thread(void)
1579 #ifdef HAVE_SIGALTSTACK
1583 int mib[2], val = 1;
1586 mib[1] = KERN_THALTSTACK;
1587 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1590 ss.ss_sp = get_signal_stack();
1591 ss.ss_size = signal_stack_size;
1593 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
1594 #endif /* HAVE_SIGALTSTACK */
1596 ntdll_get_thread_data()->gs = wine_get_gs();
1599 /**********************************************************************
1600 * signal_init_process
1602 void signal_init_process(void)
1604 struct sigaction sig_act;
1606 sig_act.sa_mask = server_block_set;
1607 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1609 sig_act.sa_flags |= SA_ONSTACK;
1612 sig_act.sa_sigaction = int_handler;
1613 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1614 sig_act.sa_sigaction = fpe_handler;
1615 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1616 sig_act.sa_sigaction = abrt_handler;
1617 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1618 sig_act.sa_sigaction = quit_handler;
1619 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1620 sig_act.sa_sigaction = usr1_handler;
1621 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1623 sig_act.sa_sigaction = segv_handler;
1624 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1625 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1627 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1631 sig_act.sa_sigaction = trap_handler;
1632 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1636 sig_act.sa_sigaction = usr2_handler;
1637 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1640 signal_init_thread();
1644 perror("sigaction");
1650 /**********************************************************************
1651 * __wine_enter_vm86 (NTDLL.@)
1653 * Enter vm86 mode with the specified register context.
1655 void __wine_enter_vm86( CONTEXT *context )
1657 EXCEPTION_RECORD rec;
1659 struct vm86plus_struct vm86;
1661 memset( &vm86, 0, sizeof(vm86) );
1664 restore_vm86_context( context, &vm86 );
1666 ntdll_get_thread_data()->vm86_ptr = &vm86;
1667 merge_vm86_pending_flags( &rec );
1669 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1676 save_vm86_context( context, &vm86 );
1678 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1679 rec.ExceptionRecord = NULL;
1680 rec.ExceptionAddress = (LPVOID)context->Eip;
1681 rec.NumberParameters = 0;
1683 switch(VM86_TYPE(res))
1685 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1686 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1688 case VM86_TRAP: /* return due to DOS-debugger request */
1689 switch(VM86_ARG(res))
1691 case TRAP_x86_TRCTRAP: /* Single-step exception */
1692 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1694 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1695 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1698 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1702 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1703 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1704 rec.NumberParameters = 1;
1705 rec.ExceptionInformation[0] = VM86_ARG(res);
1707 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1708 context->EFlags |= VIF_FLAG;
1709 context->EFlags &= ~VIP_FLAG;
1710 get_vm86_teb_info()->vm86_pending = 0;
1711 rec.ExceptionCode = EXCEPTION_VM86_STI;
1713 case VM86_PICRETURN: /* return due to pending PIC request */
1714 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1716 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1718 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1721 __regs_RtlRaiseException( &rec, context );
1725 #else /* __HAVE_VM86 */
1726 /**********************************************************************
1727 * __wine_enter_vm86 (NTDLL.@)
1729 void __wine_enter_vm86( CONTEXT *context )
1731 MESSAGE("vm86 mode not supported on this platform\n");
1733 #endif /* __HAVE_VM86 */
1735 /**********************************************************************
1736 * DbgBreakPoint (NTDLL.@)
1738 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1740 /**********************************************************************
1741 * DbgUserBreakPoint (NTDLL.@)
1743 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1746 /**********************************************************************
1747 * EXC_CallHandler (internal)
1749 * Some exception handlers depend on EBP to have a fixed position relative to
1750 * the exception frame.
1751 * Shrinker depends on (*1) doing what it does,
1752 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1753 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1754 * function calling the handler having only 5 parameters (*4).
1756 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1758 " movl %esp, %ebp\n"
1760 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1766 " call " __ASM_NAME("call_exception_handler") "\n"
1771 __ASM_GLOBAL_FUNC(call_exception_handler,
1773 " movl %esp, %ebp\n"
1775 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1776 " pushl %edx\n" /* handler be handled by... */
1778 " pushl (0)\n" /* nested_handler (passed in edx). */
1780 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1785 " movl 24(%ebp), %ecx\n" /* (*1) */
1786 " call *%ecx\n" /* call handler. (*2) */
1788 " movl (0), %esp\n" /* restore previous... (*3) */
1790 " popl (0)\n" /* exception frame. */
1791 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1793 " ret $20\n" /* (*4) */
1795 #endif /* __i386__ */