push bf834a7eef2241618d351018da1587a7ae2466d1
[wine/hacks.git] / dlls / ntdll / signal_i386.c
blob154366436f86c5ef4b96e0622a3a9e5c1c4e4b95
1 /*
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
21 #ifdef __i386__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYSCALL_H
40 # include <syscall.h>
41 #else
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
44 # endif
45 #endif
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
49 #endif
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
53 #endif
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
56 #endif
58 #include "windef.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>
66 #endif
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
73 ULONGLONG Low;
74 LONGLONG High;
75 } M128A;
77 typedef struct
79 WORD ControlWord;
80 WORD StatusWord;
81 BYTE TagWord;
82 BYTE Reserved1;
83 WORD ErrorOpcode;
84 DWORD ErrorOffset;
85 WORD ErrorSelector;
86 WORD Reserved2;
87 DWORD DataOffset;
88 WORD DataSelector;
89 WORD Reserved3;
90 DWORD MxCsr;
91 DWORD MxCsr_Mask;
92 M128A FloatRegisters[8];
93 M128A XmmRegisters[16];
94 BYTE Reserved4[96];
95 } XMM_SAVE_AREA32;
97 /***********************************************************************
98 * signal context platform-specific definitions
101 #ifdef linux
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,
137 "pushl %ebp\n\t"
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"
142 "pushl %ebx\n\t"
143 "movl $1,%ebx\n\t" /*VM86_ENTER*/
144 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
145 "pushl %fs\n\t"
146 "pushl %gs\n\t"
147 "int $0x80\n"
148 ".globl " __ASM_NAME("vm86_return") "\n\t"
149 __ASM_FUNC("vm86_return") "\n"
150 __ASM_NAME("vm86_return") ":\n\t"
151 "popl %gs\n\t"
152 "popl %fs\n\t"
153 "popl %ecx\n\t"
154 "popl %ebx\n\t"
155 "popl %ebp\n\t"
156 "testl %eax,%eax\n\t"
157 "jl 0f\n\t"
158 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
159 "je " __ASM_NAME("vm86_enter") "\n\t"
160 "0:\n\t"
161 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
162 "movl $0,(%ecx)\n\t"
163 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
164 __ASM_FUNC("vm86_return_end") "\n"
165 __ASM_NAME("vm86_return_end") ":\n\t"
166 "ret" )
168 #ifdef HAVE_SYS_VM86_H
169 # define __HAVE_VM86
170 #endif
172 #endif /* linux */
174 #ifdef BSDI
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 */
200 #endif /* bsdi */
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 */
231 #endif /* *BSD */
233 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
235 #ifdef _SCO_DS
236 #include <sys/regset.h>
237 #endif
238 #include <sys/ucontext.h>
239 typedef struct ucontext SIGCONTEXT;
241 #ifdef _SCO_DS
242 #define gregs regs
243 #endif
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])
264 #ifdef UESP
265 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
266 #elif defined(R_ESP)
267 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
268 #else
269 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
270 #endif
271 #ifdef ERR
272 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
273 #endif
274 #ifdef TRAPNO
275 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
276 #endif
278 #define FPU_sig(context) NULL /* FIXME */
279 #define FPUX_sig(context) NULL /* FIXME */
281 #endif /* svr4 || SCO_DS */
283 #ifdef __APPLE__
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)
310 #else
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)
331 #endif
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 );
348 enum i386_trap_code
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 */
371 #else
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) */
392 #endif
396 /***********************************************************************
397 * dispatch_signal
399 static inline int dispatch_signal(unsigned int sig)
401 if (handlers[sig] == NULL) return 0;
402 return handlers[sig](sig);
406 /***********************************************************************
407 * get_trap_code
409 * Get the trap code for a signal.
411 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
413 #ifdef TRAP_sig
414 return TRAP_sig(sigcontext);
415 #else
416 return TRAP_x86_UNKNOWN; /* unknown trap code */
417 #endif
420 /***********************************************************************
421 * get_error_code
423 * Get the error code for a signal.
425 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
427 #ifdef ERROR_sig
428 return ERROR_sig(sigcontext);
429 #else
430 return 0;
431 #endif
434 /***********************************************************************
435 * get_signal_stack
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 /***********************************************************************
446 * get_current_teb
448 * Get the current teb based on the stack pointer.
450 static inline TEB *get_current_teb(void)
452 unsigned long esp;
453 __asm__("movl %%esp,%0" : "=g" (esp) );
454 return (TEB *)(esp & ~signal_stack_mask);
458 #ifdef __HAVE_VM86
459 /***********************************************************************
460 * save_vm86_context
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)
548 CONTEXT vcontext;
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
571 * instruction.
573 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
575 #endif /* __HAVE_VM86 */
578 #ifdef __sun
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
582 * code crash badly.
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,
589 "movl $0x62,%eax\n\t"
590 "int $0x91\n\t"
591 "ret" )
593 /* assume the same libc handler is used for all signals */
594 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
596 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
598 struct ntdll_thread_data *thread_data;
600 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
602 thread_data = get_current_teb()->SystemReserved2;
603 wine_set_fs( thread_data->fs );
604 wine_set_gs( thread_data->gs );
606 libc_sigacthandler( signal, siginfo, sigcontext );
609 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
611 struct sigaction real_act;
613 if (sigaction( sig, new, old ) == -1) return -1;
615 /* retrieve the real handler and flags with a direct syscall */
616 sigaction_syscall( sig, NULL, &real_act );
617 libc_sigacthandler = real_act.sa_sigaction;
618 real_act.sa_sigaction = wine_sigacthandler;
619 sigaction_syscall( sig, &real_act, NULL );
620 return 0;
622 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
624 #endif
626 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
629 /***********************************************************************
630 * init_handler
632 * Handler initialization when the full context is not needed.
633 * Return the stack pointer to use for pushing the exception data.
635 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
637 TEB *teb = get_current_teb();
638 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
640 /* get %fs and %gs at time of the fault */
641 #ifdef FS_sig
642 *fs = LOWORD(FS_sig(sigcontext));
643 #else
644 *fs = wine_get_fs();
645 #endif
646 #ifdef GS_sig
647 *gs = LOWORD(GS_sig(sigcontext));
648 #else
649 *gs = wine_get_gs();
650 #endif
652 #ifndef __sun /* see above for Solaris handling */
653 wine_set_fs( thread_data->fs );
654 wine_set_gs( thread_data->gs );
655 #endif
657 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
658 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
661 * Win16 or DOS protected mode. Note that during switch
662 * from 16-bit mode to linear mode, CS may be set to system
663 * segment before FS is restored. Fortunately, in this case
664 * SS is still non-system segment. This is why both CS and SS
665 * are checked.
667 return teb->WOW32Reserved;
669 return (void *)(ESP_sig(sigcontext) & ~3);
673 /***********************************************************************
674 * save_fpu
676 * Save the thread FPU context.
678 static inline void save_fpu( CONTEXT *context )
680 #ifdef __GNUC__
681 context->ContextFlags |= CONTEXT_FLOATING_POINT;
682 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
683 #endif
687 /***********************************************************************
688 * save_fpux
690 * Save the thread FPU extended context.
692 static inline void save_fpux( CONTEXT *context )
694 #ifdef __GNUC__
695 /* we have to enforce alignment by hand */
696 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
697 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
699 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
700 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
701 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
702 #endif
706 /***********************************************************************
707 * restore_fpu
709 * Restore the FPU context to a sigcontext.
711 static inline void restore_fpu( const CONTEXT *context )
713 FLOATING_SAVE_AREA float_status = context->FloatSave;
714 /* reset the current interrupt status */
715 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
716 #ifdef __GNUC__
717 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
718 #endif /* __GNUC__ */
722 /***********************************************************************
723 * restore_fpux
725 * Restore the FPU extended context to a sigcontext.
727 static inline void restore_fpux( const CONTEXT *context )
729 #ifdef __GNUC__
730 /* we have to enforce alignment by hand */
731 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
732 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
734 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
735 /* reset the current interrupt status */
736 state->StatusWord &= state->ControlWord | 0xff80;
737 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
738 #endif
742 /***********************************************************************
743 * fpux_to_fpu
745 * Build a standard FPU context from an extended one.
747 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
749 unsigned int i, tag, stack_top;
751 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
752 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
753 fpu->ErrorOffset = fpux->ErrorOffset;
754 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
755 fpu->DataOffset = fpux->DataOffset;
756 fpu->DataSelector = fpux->DataSelector;
757 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
759 stack_top = (fpux->StatusWord >> 11) & 7;
760 fpu->TagWord = 0xffff0000;
761 for (i = 0; i < 8; i++)
763 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
764 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
765 else
767 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
768 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
770 tag = 2; /* special */
772 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
774 if (reg->Low) tag = 2; /* special */
775 else tag = 1; /* zero */
777 else
779 if (reg->Low >> 63) tag = 0; /* valid */
780 else tag = 2; /* special */
783 fpu->TagWord |= tag << (2 * i);
788 /***********************************************************************
789 * save_context
791 * Build a context structure from the signal info.
793 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
795 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
796 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
797 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
799 memset(context, 0, sizeof(*context));
800 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
801 context->Eax = EAX_sig(sigcontext);
802 context->Ebx = EBX_sig(sigcontext);
803 context->Ecx = ECX_sig(sigcontext);
804 context->Edx = EDX_sig(sigcontext);
805 context->Esi = ESI_sig(sigcontext);
806 context->Edi = EDI_sig(sigcontext);
807 context->Ebp = EBP_sig(sigcontext);
808 context->EFlags = EFL_sig(sigcontext);
809 context->Eip = EIP_sig(sigcontext);
810 context->Esp = ESP_sig(sigcontext);
811 context->SegCs = LOWORD(CS_sig(sigcontext));
812 context->SegDs = LOWORD(DS_sig(sigcontext));
813 context->SegEs = LOWORD(ES_sig(sigcontext));
814 context->SegFs = fs;
815 context->SegGs = gs;
816 context->SegSs = LOWORD(SS_sig(sigcontext));
817 context->Dr0 = regs->dr0;
818 context->Dr1 = regs->dr1;
819 context->Dr2 = regs->dr2;
820 context->Dr3 = regs->dr3;
821 context->Dr6 = regs->dr6;
822 context->Dr7 = regs->dr7;
824 if (fpu)
826 context->ContextFlags |= CONTEXT_FLOATING_POINT;
827 context->FloatSave = *fpu;
829 if (fpux)
831 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
832 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
833 fpux_support = 1;
834 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
836 if (!fpu && !fpux) save_fpu( context );
840 /***********************************************************************
841 * restore_context
843 * Restore the signal info from the context.
845 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
847 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
848 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
849 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
851 regs->dr0 = context->Dr0;
852 regs->dr1 = context->Dr1;
853 regs->dr2 = context->Dr2;
854 regs->dr3 = context->Dr3;
855 regs->dr6 = context->Dr6;
856 regs->dr7 = context->Dr7;
857 EAX_sig(sigcontext) = context->Eax;
858 EBX_sig(sigcontext) = context->Ebx;
859 ECX_sig(sigcontext) = context->Ecx;
860 EDX_sig(sigcontext) = context->Edx;
861 ESI_sig(sigcontext) = context->Esi;
862 EDI_sig(sigcontext) = context->Edi;
863 EBP_sig(sigcontext) = context->Ebp;
864 EFL_sig(sigcontext) = context->EFlags;
865 EIP_sig(sigcontext) = context->Eip;
866 ESP_sig(sigcontext) = context->Esp;
867 CS_sig(sigcontext) = context->SegCs;
868 DS_sig(sigcontext) = context->SegDs;
869 ES_sig(sigcontext) = context->SegEs;
870 SS_sig(sigcontext) = context->SegSs;
871 #ifdef GS_sig
872 GS_sig(sigcontext) = context->SegGs;
873 #else
874 wine_set_gs( context->SegGs );
875 #endif
876 #ifdef FS_sig
877 FS_sig(sigcontext) = context->SegFs;
878 #else
879 wine_set_fs( context->SegFs );
880 #endif
882 if (fpu) *fpu = context->FloatSave;
883 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
884 if (!fpu && !fpux) restore_fpu( context );
888 /***********************************************************************
889 * RtlCaptureContext (NTDLL.@)
891 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
893 *context = *regs;
894 if (fpux_support) save_fpux( context );
895 else save_fpu( context );
897 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
900 /***********************************************************************
901 * set_cpu_context
903 * Set the new CPU context. Used by NtSetContextThread.
905 void set_cpu_context( const CONTEXT *context )
907 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
909 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
910 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
912 if (flags & CONTEXT_DEBUG_REGISTERS)
914 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
915 regs->dr0 = context->Dr0;
916 regs->dr1 = context->Dr1;
917 regs->dr2 = context->Dr2;
918 regs->dr3 = context->Dr3;
919 regs->dr6 = context->Dr6;
920 regs->dr7 = context->Dr7;
922 if (flags & CONTEXT_FULL)
924 if (!(flags & CONTEXT_CONTROL))
925 FIXME( "setting partial context (%x) not supported\n", flags );
926 else if (flags & CONTEXT_SEGMENTS)
927 __wine_call_from_32_restore_regs( context );
928 else
930 CONTEXT newcontext = *context;
931 newcontext.SegDs = wine_get_ds();
932 newcontext.SegEs = wine_get_es();
933 newcontext.SegFs = wine_get_fs();
934 newcontext.SegGs = wine_get_gs();
935 __wine_call_from_32_restore_regs( &newcontext );
941 /***********************************************************************
942 * is_privileged_instr
944 * Check if the fault location is a privileged instruction.
945 * Based on the instruction emulation code in dlls/kernel/instr.c.
947 static inline DWORD is_privileged_instr( CONTEXT86 *context )
949 const BYTE *instr;
950 unsigned int prefix_count = 0;
952 if (!wine_ldt_is_system( context->SegCs )) return 0;
953 instr = (BYTE *)context->Eip;
955 for (;;) switch(*instr)
957 /* instruction prefixes */
958 case 0x2e: /* %cs: */
959 case 0x36: /* %ss: */
960 case 0x3e: /* %ds: */
961 case 0x26: /* %es: */
962 case 0x64: /* %fs: */
963 case 0x65: /* %gs: */
964 case 0x66: /* opcode size */
965 case 0x67: /* addr size */
966 case 0xf0: /* lock */
967 case 0xf2: /* repne */
968 case 0xf3: /* repe */
969 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
970 instr++;
971 continue;
973 case 0x0f: /* extended instruction */
974 switch(instr[1])
976 case 0x20: /* mov crX, reg */
977 case 0x21: /* mov drX, reg */
978 case 0x22: /* mov reg, crX */
979 case 0x23: /* mov reg drX */
980 return EXCEPTION_PRIV_INSTRUCTION;
982 return 0;
983 case 0x6c: /* insb (%dx) */
984 case 0x6d: /* insl (%dx) */
985 case 0x6e: /* outsb (%dx) */
986 case 0x6f: /* outsl (%dx) */
987 case 0xcd: /* int $xx */
988 case 0xe4: /* inb al,XX */
989 case 0xe5: /* in (e)ax,XX */
990 case 0xe6: /* outb XX,al */
991 case 0xe7: /* out XX,(e)ax */
992 case 0xec: /* inb (%dx),%al */
993 case 0xed: /* inl (%dx),%eax */
994 case 0xee: /* outb %al,(%dx) */
995 case 0xef: /* outl %eax,(%dx) */
996 case 0xf4: /* hlt */
997 case 0xfa: /* cli */
998 case 0xfb: /* sti */
999 return EXCEPTION_PRIV_INSTRUCTION;
1000 default:
1001 return 0;
1006 #include "pshpack1.h"
1007 struct atl_thunk
1009 DWORD movl; /* movl this,4(%esp) */
1010 DWORD this;
1011 BYTE jmp; /* jmp func */
1012 int func;
1014 #include "poppack.h"
1016 /**********************************************************************
1017 * check_atl_thunk
1019 * Check if code destination is an ATL thunk, and emulate it if so.
1021 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1023 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1024 BOOL ret = FALSE;
1026 __TRY
1028 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1030 *((DWORD *)context->Esp + 1) = thunk->this;
1031 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1032 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1033 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1034 ret = TRUE;
1037 __EXCEPT_PAGE_FAULT
1039 return FALSE;
1041 __ENDTRY
1042 return ret;
1046 /***********************************************************************
1047 * setup_exception_record
1049 * Setup the exception record and context on the thread stack.
1051 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1052 WORD fs, WORD gs, raise_func func )
1054 struct stack_layout
1056 void *ret_addr; /* return address from raise_func */
1057 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1058 CONTEXT *context_ptr; /* second arg for raise_func */
1059 CONTEXT context;
1060 EXCEPTION_RECORD rec;
1061 DWORD ebp;
1062 DWORD eip;
1063 } *stack = stack_ptr;
1064 DWORD exception_code = 0;
1066 /* stack sanity checks */
1068 if ((char *)stack >= (char *)get_signal_stack() &&
1069 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1071 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1072 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1073 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1074 NtCurrentTeb()->Tib.StackBase );
1075 server_abort_thread(1);
1078 if (stack - 1 > stack || /* check for overflow in subtraction */
1079 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1080 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1082 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1083 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1084 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1085 NtCurrentTeb()->Tib.StackBase );
1087 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1089 /* stack overflow on last page, unrecoverable */
1090 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1091 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1092 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1093 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1094 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1095 server_abort_thread(1);
1097 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1099 /* stack access below stack limit, may be recoverable */
1100 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1101 else
1103 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1104 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1105 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1106 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1107 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1108 server_abort_thread(1);
1112 stack--; /* push the stack_layout structure */
1113 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1114 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1115 #elif defined(VALGRIND_MAKE_WRITABLE)
1116 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1117 #endif
1118 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1119 stack->rec_ptr = &stack->rec;
1120 stack->context_ptr = &stack->context;
1122 stack->rec.ExceptionRecord = NULL;
1123 stack->rec.ExceptionCode = exception_code;
1124 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1125 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1126 stack->rec.NumberParameters = 0;
1128 save_context( &stack->context, sigcontext, fs, gs );
1130 /* now modify the sigcontext to return to the raise function */
1131 ESP_sig(sigcontext) = (DWORD)stack;
1132 EIP_sig(sigcontext) = (DWORD)func;
1133 /* clear single-step, direction, and align check flag */
1134 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1135 CS_sig(sigcontext) = wine_get_cs();
1136 DS_sig(sigcontext) = wine_get_ds();
1137 ES_sig(sigcontext) = wine_get_es();
1138 FS_sig(sigcontext) = wine_get_fs();
1139 GS_sig(sigcontext) = wine_get_gs();
1140 SS_sig(sigcontext) = wine_get_ss();
1142 return stack->rec_ptr;
1146 /***********************************************************************
1147 * setup_exception
1149 * Setup a proper stack frame for the raise function, and modify the
1150 * sigcontext so that the return from the signal handler will call
1151 * the raise function.
1153 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1155 WORD fs, gs;
1156 void *stack = init_handler( sigcontext, &fs, &gs );
1158 return setup_exception_record( sigcontext, stack, fs, gs, func );
1162 /***********************************************************************
1163 * get_exception_context
1165 * Get a pointer to the context built by setup_exception.
1167 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1169 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1173 /**********************************************************************
1174 * get_fpu_code
1176 * Get the FPU exception code from the FPU status.
1178 static inline DWORD get_fpu_code( const CONTEXT *context )
1180 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1182 if (status & 0x01) /* IE */
1184 if (status & 0x40) /* SF */
1185 return EXCEPTION_FLT_STACK_CHECK;
1186 else
1187 return EXCEPTION_FLT_INVALID_OPERATION;
1189 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1190 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1191 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1192 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1193 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1194 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1198 /**********************************************************************
1199 * raise_segv_exception
1201 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1203 switch(rec->ExceptionCode)
1205 case EXCEPTION_ACCESS_VIOLATION:
1206 if (rec->NumberParameters == 2)
1208 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1209 goto done;
1210 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1211 rec->ExceptionInformation[0] )))
1212 goto done;
1214 break;
1215 case EXCEPTION_DATATYPE_MISALIGNMENT:
1216 /* FIXME: pass through exception handler first? */
1217 if (context->EFlags & 0x00040000)
1219 /* Disable AC flag, return */
1220 context->EFlags &= ~0x00040000;
1221 goto done;
1223 break;
1225 __regs_RtlRaiseException( rec, context );
1226 done:
1227 NtSetContextThread( GetCurrentThread(), context );
1231 /**********************************************************************
1232 * raise_trap_exception
1234 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1236 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1238 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1240 /* when single stepping can't tell whether this is a hw bp or a
1241 * single step interrupt. try to avoid as much overhead as possible
1242 * and only do a server call if there is any hw bp enabled. */
1244 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1246 /* (possible) hardware breakpoint, fetch the debug registers */
1247 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1248 NtGetContextThread(GetCurrentThread(), context);
1249 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1252 context->EFlags &= ~0x100; /* clear single-step flag */
1255 __regs_RtlRaiseException( rec, context );
1256 NtSetContextThread( GetCurrentThread(), context );
1260 /**********************************************************************
1261 * raise_exception
1263 * Generic raise function for exceptions that don't need special treatment.
1265 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1267 __regs_RtlRaiseException( rec, context );
1268 NtSetContextThread( GetCurrentThread(), context );
1272 #ifdef __HAVE_VM86
1273 /**********************************************************************
1274 * raise_vm86_sti_exception
1276 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1278 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1279 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1281 if (ntdll_get_thread_data()->vm86_ptr)
1283 if (((char*)context->Eip >= (char*)vm86_return) &&
1284 ((char*)context->Eip <= (char*)vm86_return_end) &&
1285 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1286 /* exiting from VM86, can't throw */
1287 goto done;
1289 merge_vm86_pending_flags( rec );
1291 else if (get_vm86_teb_info()->dpmi_vif &&
1292 !wine_ldt_is_system(context->SegCs) &&
1293 !wine_ldt_is_system(context->SegSs))
1295 /* Executing DPMI code and virtual interrupts are enabled. */
1296 get_vm86_teb_info()->vm86_pending = 0;
1297 __regs_RtlRaiseException( rec, context );
1299 done:
1300 NtSetContextThread( GetCurrentThread(), context );
1304 /**********************************************************************
1305 * usr2_handler
1307 * Handler for SIGUSR2.
1308 * We use it to signal that the running __wine_enter_vm86() should
1309 * immediately set VIP_FLAG, causing pending events to be handled
1310 * as early as possible.
1312 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1314 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1315 rec->ExceptionCode = EXCEPTION_VM86_STI;
1317 #endif /* __HAVE_VM86 */
1320 /**********************************************************************
1321 * segv_handler
1323 * Handler for SIGSEGV and related errors.
1325 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1327 WORD fs, gs;
1328 EXCEPTION_RECORD *rec;
1329 SIGCONTEXT *context = sigcontext;
1330 void *stack = init_handler( sigcontext, &fs, &gs );
1332 /* check for page fault inside the thread stack */
1333 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1334 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1335 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1336 virtual_handle_stack_fault( siginfo->si_addr ))
1338 /* check if this was the last guard page */
1339 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1341 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1342 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1344 return;
1347 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1348 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1350 switch(get_trap_code(context))
1352 case TRAP_x86_OFLOW: /* Overflow exception */
1353 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1354 break;
1355 case TRAP_x86_BOUND: /* Bound range exception */
1356 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1357 break;
1358 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1359 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1360 break;
1361 case TRAP_x86_STKFLT: /* Stack fault */
1362 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1363 break;
1364 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1365 case TRAP_x86_PROTFLT: /* General protection fault */
1366 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1368 WORD err = get_error_code(context);
1369 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1370 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1371 rec->NumberParameters = 2;
1372 rec->ExceptionInformation[0] = 0;
1373 /* if error contains a LDT selector, use that as fault address */
1374 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1375 rec->ExceptionInformation[1] = err & ~7;
1376 else
1377 rec->ExceptionInformation[1] = 0xffffffff;
1379 break;
1380 case TRAP_x86_PAGEFLT: /* Page fault */
1381 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1382 rec->NumberParameters = 2;
1383 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1384 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1385 break;
1386 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1387 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1388 break;
1389 default:
1390 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1391 /* fall through */
1392 case TRAP_x86_NMI: /* NMI interrupt */
1393 case TRAP_x86_DNA: /* Device not available exception */
1394 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1395 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1396 case TRAP_x86_MCHK: /* Machine check exception */
1397 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1398 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1399 break;
1404 /**********************************************************************
1405 * trap_handler
1407 * Handler for SIGTRAP.
1409 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1411 SIGCONTEXT *context = sigcontext;
1412 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1414 switch(get_trap_code(context))
1416 case TRAP_x86_TRCTRAP: /* Single-step exception */
1417 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1418 break;
1419 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1420 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1421 /* fall through */
1422 default:
1423 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1424 break;
1429 /**********************************************************************
1430 * fpe_handler
1432 * Handler for SIGFPE.
1434 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1436 CONTEXT *win_context;
1437 SIGCONTEXT *context = sigcontext;
1438 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1440 win_context = get_exception_context( rec );
1442 switch(get_trap_code(context))
1444 case TRAP_x86_DIVIDE: /* Division by zero exception */
1445 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1446 break;
1447 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1448 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1449 break;
1450 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1451 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1452 rec->ExceptionCode = get_fpu_code( win_context );
1453 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
1454 break;
1455 case TRAP_x86_CACHEFLT: /* SIMD exception */
1456 /* TODO:
1457 * Behaviour only tested for divide-by-zero exceptions
1458 * Check for other SIMD exceptions as well */
1459 if(siginfo->si_code != FPE_FLTDIV)
1460 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1461 siginfo->si_code);
1463 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1464 rec->NumberParameters = 1;
1465 /* no idea what meaning is actually behind this but that's what native does */
1466 rec->ExceptionInformation[0] = 0;
1467 break;
1468 default:
1469 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1470 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1471 break;
1476 /**********************************************************************
1477 * int_handler
1479 * Handler for SIGINT.
1481 * FIXME: should not be calling external functions on the signal stack.
1483 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1485 WORD fs, gs;
1486 init_handler( sigcontext, &fs, &gs );
1487 if (!dispatch_signal(SIGINT))
1489 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1490 rec->ExceptionCode = CONTROL_C_EXIT;
1494 /**********************************************************************
1495 * abrt_handler
1497 * Handler for SIGABRT.
1499 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1501 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1502 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1503 rec->ExceptionFlags = EH_NONCONTINUABLE;
1507 /**********************************************************************
1508 * quit_handler
1510 * Handler for SIGQUIT.
1512 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1514 WORD fs, gs;
1515 init_handler( sigcontext, &fs, &gs );
1516 server_abort_thread(0);
1520 /**********************************************************************
1521 * usr1_handler
1523 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1525 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1527 CONTEXT context;
1528 WORD fs, gs;
1530 init_handler( sigcontext, &fs, &gs );
1531 save_context( &context, sigcontext, fs, gs );
1532 wait_suspend( &context );
1533 restore_context( &context, sigcontext );
1537 /**********************************************************************
1538 * get_signal_stack_total_size
1540 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1541 * Must be a power of two.
1543 size_t get_signal_stack_total_size(void)
1545 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1547 if (!signal_stack_size)
1549 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1550 /* find the first power of two not smaller than min_size */
1551 while (size < min_size) size *= 2;
1552 signal_stack_mask = size - 1;
1553 signal_stack_size = size - teb_size;
1555 return signal_stack_size + teb_size;
1559 /***********************************************************************
1560 * __wine_set_signal_handler (NTDLL.@)
1562 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1564 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1565 if (handlers[sig] != NULL) return -2;
1566 handlers[sig] = wsh;
1567 return 0;
1571 /**********************************************************************
1572 * signal_init_thread
1574 void signal_init_thread(void)
1576 #ifdef HAVE_SIGALTSTACK
1577 stack_t ss;
1579 #ifdef __APPLE__
1580 int mib[2], val = 1;
1582 mib[0] = CTL_KERN;
1583 mib[1] = KERN_THALTSTACK;
1584 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1585 #endif
1587 ss.ss_sp = get_signal_stack();
1588 ss.ss_size = signal_stack_size;
1589 ss.ss_flags = 0;
1590 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
1591 #endif /* HAVE_SIGALTSTACK */
1593 ntdll_get_thread_data()->gs = wine_get_gs();
1596 /**********************************************************************
1597 * signal_init_process
1599 void signal_init_process(void)
1601 struct sigaction sig_act;
1603 sig_act.sa_mask = server_block_set;
1604 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1605 #ifdef SA_ONSTACK
1606 sig_act.sa_flags |= SA_ONSTACK;
1607 #endif
1609 sig_act.sa_sigaction = int_handler;
1610 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1611 sig_act.sa_sigaction = fpe_handler;
1612 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1613 sig_act.sa_sigaction = abrt_handler;
1614 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1615 sig_act.sa_sigaction = quit_handler;
1616 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1617 sig_act.sa_sigaction = usr1_handler;
1618 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1620 sig_act.sa_sigaction = segv_handler;
1621 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1622 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1623 #ifdef SIGBUS
1624 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1625 #endif
1627 #ifdef SIGTRAP
1628 sig_act.sa_sigaction = trap_handler;
1629 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1630 #endif
1632 #ifdef __HAVE_VM86
1633 sig_act.sa_sigaction = usr2_handler;
1634 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1635 #endif
1637 signal_init_thread();
1638 return;
1640 error:
1641 perror("sigaction");
1642 exit(1);
1646 #ifdef __HAVE_VM86
1647 /**********************************************************************
1648 * __wine_enter_vm86 (NTDLL.@)
1650 * Enter vm86 mode with the specified register context.
1652 void __wine_enter_vm86( CONTEXT *context )
1654 EXCEPTION_RECORD rec;
1655 int res;
1656 struct vm86plus_struct vm86;
1658 memset( &vm86, 0, sizeof(vm86) );
1659 for (;;)
1661 restore_vm86_context( context, &vm86 );
1663 ntdll_get_thread_data()->vm86_ptr = &vm86;
1664 merge_vm86_pending_flags( &rec );
1666 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1667 if (res < 0)
1669 errno = -res;
1670 return;
1673 save_vm86_context( context, &vm86 );
1675 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1676 rec.ExceptionRecord = NULL;
1677 rec.ExceptionAddress = (LPVOID)context->Eip;
1678 rec.NumberParameters = 0;
1680 switch(VM86_TYPE(res))
1682 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1683 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1684 break;
1685 case VM86_TRAP: /* return due to DOS-debugger request */
1686 switch(VM86_ARG(res))
1688 case TRAP_x86_TRCTRAP: /* Single-step exception */
1689 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1690 break;
1691 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1692 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1693 /* fall through */
1694 default:
1695 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1696 break;
1698 break;
1699 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1700 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1701 rec.NumberParameters = 1;
1702 rec.ExceptionInformation[0] = VM86_ARG(res);
1703 break;
1704 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1705 context->EFlags |= VIF_FLAG;
1706 context->EFlags &= ~VIP_FLAG;
1707 get_vm86_teb_info()->vm86_pending = 0;
1708 rec.ExceptionCode = EXCEPTION_VM86_STI;
1709 break;
1710 case VM86_PICRETURN: /* return due to pending PIC request */
1711 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1712 break;
1713 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1714 default:
1715 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1716 continue;
1718 __regs_RtlRaiseException( &rec, context );
1722 #else /* __HAVE_VM86 */
1723 /**********************************************************************
1724 * __wine_enter_vm86 (NTDLL.@)
1726 void __wine_enter_vm86( CONTEXT *context )
1728 MESSAGE("vm86 mode not supported on this platform\n");
1730 #endif /* __HAVE_VM86 */
1732 /**********************************************************************
1733 * DbgBreakPoint (NTDLL.@)
1735 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1737 /**********************************************************************
1738 * DbgUserBreakPoint (NTDLL.@)
1740 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1743 /**********************************************************************
1744 * EXC_CallHandler (internal)
1746 * Some exception handlers depend on EBP to have a fixed position relative to
1747 * the exception frame.
1748 * Shrinker depends on (*1) doing what it does,
1749 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1750 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1751 * function calling the handler having only 5 parameters (*4).
1753 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1754 " pushl %ebp\n"
1755 " movl %esp, %ebp\n"
1756 " pushl %ebx\n"
1757 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1758 " pushl 24(%ebp)\n"
1759 " pushl 20(%ebp)\n"
1760 " pushl 16(%ebp)\n"
1761 " pushl 12(%ebp)\n"
1762 " pushl 8(%ebp)\n"
1763 " call " __ASM_NAME("call_exception_handler") "\n"
1764 " popl %ebx\n"
1765 " leave\n"
1766 " ret\n"
1768 __ASM_GLOBAL_FUNC(call_exception_handler,
1769 " pushl %ebp\n"
1770 " movl %esp, %ebp\n"
1771 " subl $12,%esp\n"
1772 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1773 " pushl %edx\n" /* handler be handled by... */
1774 " .byte 0x64\n"
1775 " pushl (0)\n" /* nested_handler (passed in edx). */
1776 " .byte 0x64\n"
1777 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1778 " pushl 20(%ebp)\n"
1779 " pushl 16(%ebp)\n"
1780 " pushl 12(%ebp)\n"
1781 " pushl 8(%ebp)\n"
1782 " movl 24(%ebp), %ecx\n" /* (*1) */
1783 " call *%ecx\n" /* call handler. (*2) */
1784 " .byte 0x64\n"
1785 " movl (0), %esp\n" /* restore previous... (*3) */
1786 " .byte 0x64\n"
1787 " popl (0)\n" /* exception frame. */
1788 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1789 " popl %ebp\n"
1790 " ret $20\n" /* (*4) */
1792 #endif /* __i386__ */