ntdll: Always restore the %gs register from the saved value in thread data.
[wine/hacks.git] / dlls / ntdll / signal_i386.c
blob4e0c5fad46738b3406fdaf4c73affb03c2b13a24
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 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
581 /***********************************************************************
582 * init_handler
584 * Handler initialization when the full context is not needed.
585 * Return the stack pointer to use for pushing the exception data.
587 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
589 TEB *teb = get_current_teb();
590 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
592 /* get %fs and %gs at time of the fault */
593 #ifdef FS_sig
594 *fs = LOWORD(FS_sig(sigcontext));
595 #else
596 *fs = wine_get_fs();
597 #endif
598 #ifdef GS_sig
599 *gs = LOWORD(GS_sig(sigcontext));
600 #else
601 *gs = wine_get_gs();
602 #endif
604 wine_set_fs( thread_data->fs );
605 wine_set_gs( thread_data->gs );
607 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
608 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
611 * Win16 or DOS protected mode. Note that during switch
612 * from 16-bit mode to linear mode, CS may be set to system
613 * segment before FS is restored. Fortunately, in this case
614 * SS is still non-system segment. This is why both CS and SS
615 * are checked.
617 return teb->WOW32Reserved;
619 return (void *)(ESP_sig(sigcontext) & ~3);
623 /***********************************************************************
624 * save_fpu
626 * Save the thread FPU context.
628 static inline void save_fpu( CONTEXT *context )
630 #ifdef __GNUC__
631 context->ContextFlags |= CONTEXT_FLOATING_POINT;
632 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
633 #endif
637 /***********************************************************************
638 * save_fpux
640 * Save the thread FPU extended context.
642 static inline void save_fpux( CONTEXT *context )
644 #ifdef __GNUC__
645 /* we have to enforce alignment by hand */
646 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
647 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
649 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
650 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
651 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
652 #endif
656 /***********************************************************************
657 * restore_fpu
659 * Restore the FPU context to a sigcontext.
661 static inline void restore_fpu( const CONTEXT *context )
663 FLOATING_SAVE_AREA float_status = context->FloatSave;
664 /* reset the current interrupt status */
665 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
666 #ifdef __GNUC__
667 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
668 #endif /* __GNUC__ */
672 /***********************************************************************
673 * restore_fpux
675 * Restore the FPU extended context to a sigcontext.
677 static inline void restore_fpux( const CONTEXT *context )
679 #ifdef __GNUC__
680 /* we have to enforce alignment by hand */
681 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
682 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
684 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
685 /* reset the current interrupt status */
686 state->StatusWord &= state->ControlWord | 0xff80;
687 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
688 #endif
692 /***********************************************************************
693 * fpux_to_fpu
695 * Build a standard FPU context from an extended one.
697 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
699 unsigned int i, tag, stack_top;
701 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
702 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
703 fpu->ErrorOffset = fpux->ErrorOffset;
704 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
705 fpu->DataOffset = fpux->DataOffset;
706 fpu->DataSelector = fpux->DataSelector;
707 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
709 stack_top = (fpux->StatusWord >> 11) & 7;
710 fpu->TagWord = 0xffff0000;
711 for (i = 0; i < 8; i++)
713 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
714 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
715 else
717 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
718 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
720 tag = 2; /* special */
722 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
724 if (reg->Low) tag = 2; /* special */
725 else tag = 1; /* zero */
727 else
729 if (reg->Low >> 63) tag = 0; /* valid */
730 else tag = 2; /* special */
733 fpu->TagWord |= tag << (2 * i);
738 /***********************************************************************
739 * save_context
741 * Build a context structure from the signal info.
743 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
745 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
746 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
747 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
749 memset(context, 0, sizeof(*context));
750 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
751 context->Eax = EAX_sig(sigcontext);
752 context->Ebx = EBX_sig(sigcontext);
753 context->Ecx = ECX_sig(sigcontext);
754 context->Edx = EDX_sig(sigcontext);
755 context->Esi = ESI_sig(sigcontext);
756 context->Edi = EDI_sig(sigcontext);
757 context->Ebp = EBP_sig(sigcontext);
758 context->EFlags = EFL_sig(sigcontext);
759 context->Eip = EIP_sig(sigcontext);
760 context->Esp = ESP_sig(sigcontext);
761 context->SegCs = LOWORD(CS_sig(sigcontext));
762 context->SegDs = LOWORD(DS_sig(sigcontext));
763 context->SegEs = LOWORD(ES_sig(sigcontext));
764 context->SegFs = fs;
765 context->SegGs = gs;
766 context->SegSs = LOWORD(SS_sig(sigcontext));
767 context->Dr0 = regs->dr0;
768 context->Dr1 = regs->dr1;
769 context->Dr2 = regs->dr2;
770 context->Dr3 = regs->dr3;
771 context->Dr6 = regs->dr6;
772 context->Dr7 = regs->dr7;
774 if (fpu)
776 context->ContextFlags |= CONTEXT_FLOATING_POINT;
777 context->FloatSave = *fpu;
779 if (fpux)
781 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
782 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
783 fpux_support = 1;
784 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
786 if (!fpu && !fpux) save_fpu( context );
790 /***********************************************************************
791 * restore_context
793 * Restore the signal info from the context.
795 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
797 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
798 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
799 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
801 regs->dr0 = context->Dr0;
802 regs->dr1 = context->Dr1;
803 regs->dr2 = context->Dr2;
804 regs->dr3 = context->Dr3;
805 regs->dr6 = context->Dr6;
806 regs->dr7 = context->Dr7;
807 EAX_sig(sigcontext) = context->Eax;
808 EBX_sig(sigcontext) = context->Ebx;
809 ECX_sig(sigcontext) = context->Ecx;
810 EDX_sig(sigcontext) = context->Edx;
811 ESI_sig(sigcontext) = context->Esi;
812 EDI_sig(sigcontext) = context->Edi;
813 EBP_sig(sigcontext) = context->Ebp;
814 EFL_sig(sigcontext) = context->EFlags;
815 EIP_sig(sigcontext) = context->Eip;
816 ESP_sig(sigcontext) = context->Esp;
817 CS_sig(sigcontext) = context->SegCs;
818 DS_sig(sigcontext) = context->SegDs;
819 ES_sig(sigcontext) = context->SegEs;
820 SS_sig(sigcontext) = context->SegSs;
821 #ifdef GS_sig
822 GS_sig(sigcontext) = context->SegGs;
823 #else
824 wine_set_gs( context->SegGs );
825 #endif
826 #ifdef FS_sig
827 FS_sig(sigcontext) = context->SegFs;
828 #else
829 wine_set_fs( context->SegFs );
830 #endif
832 if (fpu) *fpu = context->FloatSave;
833 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
834 if (!fpu && !fpux) restore_fpu( context );
838 /***********************************************************************
839 * get_cpu_context
841 * Register function to get the context of the current thread.
843 void WINAPI __regs_get_cpu_context( CONTEXT *context, CONTEXT *regs )
845 *context = *regs;
846 if (fpux_support) save_fpux( context );
847 else save_fpu( context );
849 DEFINE_REGS_ENTRYPOINT( get_cpu_context, 4, 4 )
852 /***********************************************************************
853 * set_cpu_context
855 * Set the new CPU context. Used by NtSetContextThread.
857 void set_cpu_context( const CONTEXT *context )
859 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
861 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
862 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
864 if (flags & CONTEXT_DEBUG_REGISTERS)
866 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
867 regs->dr0 = context->Dr0;
868 regs->dr1 = context->Dr1;
869 regs->dr2 = context->Dr2;
870 regs->dr3 = context->Dr3;
871 regs->dr6 = context->Dr6;
872 regs->dr7 = context->Dr7;
874 if (flags & CONTEXT_FULL)
876 if (!(flags & CONTEXT_CONTROL))
877 FIXME( "setting partial context (%x) not supported\n", flags );
878 else if (flags & CONTEXT_SEGMENTS)
879 __wine_call_from_32_restore_regs( context );
880 else
882 CONTEXT newcontext = *context;
883 newcontext.SegDs = wine_get_ds();
884 newcontext.SegEs = wine_get_es();
885 newcontext.SegFs = wine_get_fs();
886 newcontext.SegGs = wine_get_gs();
887 __wine_call_from_32_restore_regs( &newcontext );
893 /***********************************************************************
894 * is_privileged_instr
896 * Check if the fault location is a privileged instruction.
897 * Based on the instruction emulation code in dlls/kernel/instr.c.
899 static inline DWORD is_privileged_instr( CONTEXT86 *context )
901 const BYTE *instr;
902 unsigned int prefix_count = 0;
904 if (!wine_ldt_is_system( context->SegCs )) return 0;
905 instr = (BYTE *)context->Eip;
907 for (;;) switch(*instr)
909 /* instruction prefixes */
910 case 0x2e: /* %cs: */
911 case 0x36: /* %ss: */
912 case 0x3e: /* %ds: */
913 case 0x26: /* %es: */
914 case 0x64: /* %fs: */
915 case 0x65: /* %gs: */
916 case 0x66: /* opcode size */
917 case 0x67: /* addr size */
918 case 0xf0: /* lock */
919 case 0xf2: /* repne */
920 case 0xf3: /* repe */
921 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
922 instr++;
923 continue;
925 case 0x0f: /* extended instruction */
926 switch(instr[1])
928 case 0x20: /* mov crX, reg */
929 case 0x21: /* mov drX, reg */
930 case 0x22: /* mov reg, crX */
931 case 0x23: /* mov reg drX */
932 return EXCEPTION_PRIV_INSTRUCTION;
934 return 0;
935 case 0x6c: /* insb (%dx) */
936 case 0x6d: /* insl (%dx) */
937 case 0x6e: /* outsb (%dx) */
938 case 0x6f: /* outsl (%dx) */
939 case 0xcd: /* int $xx */
940 case 0xe4: /* inb al,XX */
941 case 0xe5: /* in (e)ax,XX */
942 case 0xe6: /* outb XX,al */
943 case 0xe7: /* out XX,(e)ax */
944 case 0xec: /* inb (%dx),%al */
945 case 0xed: /* inl (%dx),%eax */
946 case 0xee: /* outb %al,(%dx) */
947 case 0xef: /* outl %eax,(%dx) */
948 case 0xf4: /* hlt */
949 case 0xfa: /* cli */
950 case 0xfb: /* sti */
951 return EXCEPTION_PRIV_INSTRUCTION;
952 default:
953 return 0;
958 #include "pshpack1.h"
959 struct atl_thunk
961 DWORD movl; /* movl this,4(%esp) */
962 DWORD this;
963 BYTE jmp; /* jmp func */
964 int func;
966 #include "poppack.h"
968 /**********************************************************************
969 * check_atl_thunk
971 * Check if code destination is an ATL thunk, and emulate it if so.
973 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
975 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
976 BOOL ret = FALSE;
978 __TRY
980 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
982 *((DWORD *)context->Esp + 1) = thunk->this;
983 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
984 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
985 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
986 ret = TRUE;
989 __EXCEPT_PAGE_FAULT
991 return FALSE;
993 __ENDTRY
994 return ret;
998 /***********************************************************************
999 * setup_exception_record
1001 * Setup the exception record and context on the thread stack.
1003 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1004 WORD fs, WORD gs, raise_func func )
1006 struct stack_layout
1008 void *ret_addr; /* return address from raise_func */
1009 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1010 CONTEXT *context_ptr; /* second arg for raise_func */
1011 CONTEXT context;
1012 EXCEPTION_RECORD rec;
1013 DWORD ebp;
1014 DWORD eip;
1015 } *stack = stack_ptr;
1016 DWORD exception_code = 0;
1018 /* stack sanity checks */
1020 if ((char *)stack >= (char *)get_signal_stack() &&
1021 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1023 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1024 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1025 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1026 NtCurrentTeb()->Tib.StackBase );
1027 server_abort_thread(1);
1030 if (stack - 1 > stack || /* check for overflow in subtraction */
1031 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1032 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1034 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1035 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1036 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1037 NtCurrentTeb()->Tib.StackBase );
1039 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1041 /* stack overflow on last page, unrecoverable */
1042 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1043 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1044 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1045 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1046 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1047 server_abort_thread(1);
1049 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1051 /* stack access below stack limit, may be recoverable */
1052 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1053 else
1055 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1056 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1057 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1058 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1059 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1060 server_abort_thread(1);
1064 stack--; /* push the stack_layout structure */
1065 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1066 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1067 #elif defined(VALGRIND_MAKE_WRITABLE)
1068 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1069 #endif
1070 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1071 stack->rec_ptr = &stack->rec;
1072 stack->context_ptr = &stack->context;
1074 stack->rec.ExceptionRecord = NULL;
1075 stack->rec.ExceptionCode = exception_code;
1076 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1077 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1078 stack->rec.NumberParameters = 0;
1080 save_context( &stack->context, sigcontext, fs, gs );
1082 /* now modify the sigcontext to return to the raise function */
1083 ESP_sig(sigcontext) = (DWORD)stack;
1084 EIP_sig(sigcontext) = (DWORD)func;
1085 /* clear single-step, direction, and align check flag */
1086 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1087 CS_sig(sigcontext) = wine_get_cs();
1088 DS_sig(sigcontext) = wine_get_ds();
1089 ES_sig(sigcontext) = wine_get_es();
1090 FS_sig(sigcontext) = wine_get_fs();
1091 GS_sig(sigcontext) = wine_get_gs();
1092 SS_sig(sigcontext) = wine_get_ss();
1094 return stack->rec_ptr;
1098 /***********************************************************************
1099 * setup_exception
1101 * Setup a proper stack frame for the raise function, and modify the
1102 * sigcontext so that the return from the signal handler will call
1103 * the raise function.
1105 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1107 WORD fs, gs;
1108 void *stack = init_handler( sigcontext, &fs, &gs );
1110 return setup_exception_record( sigcontext, stack, fs, gs, func );
1114 /***********************************************************************
1115 * get_exception_context
1117 * Get a pointer to the context built by setup_exception.
1119 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1121 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1125 /**********************************************************************
1126 * get_fpu_code
1128 * Get the FPU exception code from the FPU status.
1130 static inline DWORD get_fpu_code( const CONTEXT *context )
1132 DWORD status = context->FloatSave.StatusWord;
1134 if (status & 0x01) /* IE */
1136 if (status & 0x40) /* SF */
1137 return EXCEPTION_FLT_STACK_CHECK;
1138 else
1139 return EXCEPTION_FLT_INVALID_OPERATION;
1141 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1142 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1143 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1144 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1145 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1146 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1150 /**********************************************************************
1151 * raise_segv_exception
1153 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1155 switch(rec->ExceptionCode)
1157 case EXCEPTION_ACCESS_VIOLATION:
1158 if (rec->NumberParameters == 2)
1160 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1161 goto done;
1162 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1164 break;
1165 case EXCEPTION_DATATYPE_MISALIGNMENT:
1166 /* FIXME: pass through exception handler first? */
1167 if (context->EFlags & 0x00040000)
1169 /* Disable AC flag, return */
1170 context->EFlags &= ~0x00040000;
1171 goto done;
1173 break;
1175 __regs_RtlRaiseException( rec, context );
1176 done:
1177 NtSetContextThread( GetCurrentThread(), context );
1181 /**********************************************************************
1182 * raise_trap_exception
1184 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1186 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1188 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1190 /* when single stepping can't tell whether this is a hw bp or a
1191 * single step interrupt. try to avoid as much overhead as possible
1192 * and only do a server call if there is any hw bp enabled. */
1194 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1196 /* (possible) hardware breakpoint, fetch the debug registers */
1197 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1198 NtGetContextThread(GetCurrentThread(), context);
1199 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1202 context->EFlags &= ~0x100; /* clear single-step flag */
1205 __regs_RtlRaiseException( rec, context );
1206 NtSetContextThread( GetCurrentThread(), context );
1210 /**********************************************************************
1211 * raise_exception
1213 * Generic raise function for exceptions that don't need special treatment.
1215 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1217 __regs_RtlRaiseException( rec, context );
1218 NtSetContextThread( GetCurrentThread(), context );
1222 #ifdef __HAVE_VM86
1223 /**********************************************************************
1224 * raise_vm86_sti_exception
1226 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1228 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1229 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1231 if (ntdll_get_thread_data()->vm86_ptr)
1233 if (((char*)context->Eip >= (char*)vm86_return) &&
1234 ((char*)context->Eip <= (char*)vm86_return_end) &&
1235 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1236 /* exiting from VM86, can't throw */
1237 goto done;
1239 merge_vm86_pending_flags( rec );
1241 else if (get_vm86_teb_info()->dpmi_vif &&
1242 !wine_ldt_is_system(context->SegCs) &&
1243 !wine_ldt_is_system(context->SegSs))
1245 /* Executing DPMI code and virtual interrupts are enabled. */
1246 get_vm86_teb_info()->vm86_pending = 0;
1247 __regs_RtlRaiseException( rec, context );
1249 done:
1250 NtSetContextThread( GetCurrentThread(), context );
1254 /**********************************************************************
1255 * usr2_handler
1257 * Handler for SIGUSR2.
1258 * We use it to signal that the running __wine_enter_vm86() should
1259 * immediately set VIP_FLAG, causing pending events to be handled
1260 * as early as possible.
1262 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1264 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1265 rec->ExceptionCode = EXCEPTION_VM86_STI;
1267 #endif /* __HAVE_VM86 */
1270 /**********************************************************************
1271 * segv_handler
1273 * Handler for SIGSEGV and related errors.
1275 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1277 WORD fs, gs;
1278 EXCEPTION_RECORD *rec;
1279 SIGCONTEXT *context = sigcontext;
1280 void *stack = init_handler( sigcontext, &fs, &gs );
1282 /* check for page fault inside the thread stack */
1283 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1284 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1285 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1286 virtual_handle_stack_fault( siginfo->si_addr ))
1288 /* check if this was the last guard page */
1289 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1291 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1292 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1294 return;
1297 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1298 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1300 switch(get_trap_code(context))
1302 case TRAP_x86_OFLOW: /* Overflow exception */
1303 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1304 break;
1305 case TRAP_x86_BOUND: /* Bound range exception */
1306 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1307 break;
1308 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1309 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1310 break;
1311 case TRAP_x86_STKFLT: /* Stack fault */
1312 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1313 break;
1314 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1315 case TRAP_x86_PROTFLT: /* General protection fault */
1316 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1318 WORD err = get_error_code(context);
1319 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1320 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1321 rec->NumberParameters = 2;
1322 rec->ExceptionInformation[0] = 0;
1323 /* if error contains a LDT selector, use that as fault address */
1324 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1325 rec->ExceptionInformation[1] = err & ~7;
1326 else
1327 rec->ExceptionInformation[1] = 0xffffffff;
1329 break;
1330 case TRAP_x86_PAGEFLT: /* Page fault */
1331 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1332 rec->NumberParameters = 2;
1333 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1334 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1335 break;
1336 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1337 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1338 break;
1339 default:
1340 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1341 /* fall through */
1342 case TRAP_x86_NMI: /* NMI interrupt */
1343 case TRAP_x86_DNA: /* Device not available exception */
1344 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1345 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1346 case TRAP_x86_MCHK: /* Machine check exception */
1347 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1348 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1349 break;
1354 /**********************************************************************
1355 * trap_handler
1357 * Handler for SIGTRAP.
1359 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1361 SIGCONTEXT *context = sigcontext;
1362 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1364 switch(get_trap_code(context))
1366 case TRAP_x86_TRCTRAP: /* Single-step exception */
1367 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1368 break;
1369 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1370 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1371 /* fall through */
1372 default:
1373 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1374 break;
1379 /**********************************************************************
1380 * fpe_handler
1382 * Handler for SIGFPE.
1384 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1386 CONTEXT *win_context;
1387 SIGCONTEXT *context = sigcontext;
1388 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1390 win_context = get_exception_context( rec );
1392 switch(get_trap_code(context))
1394 case TRAP_x86_DIVIDE: /* Division by zero exception */
1395 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1396 break;
1397 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1398 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1399 break;
1400 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1401 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1402 rec->ExceptionCode = get_fpu_code( win_context );
1403 break;
1404 case TRAP_x86_CACHEFLT: /* SIMD exception */
1405 /* TODO:
1406 * Behaviour only tested for divide-by-zero exceptions
1407 * Check for other SIMD exceptions as well */
1408 if(siginfo->si_code != FPE_FLTDIV)
1409 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1410 siginfo->si_code);
1412 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1413 rec->NumberParameters = 1;
1414 /* no idea what meaning is actually behind this but that's what native does */
1415 rec->ExceptionInformation[0] = 0;
1416 break;
1417 default:
1418 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1419 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1420 break;
1425 /**********************************************************************
1426 * int_handler
1428 * Handler for SIGINT.
1430 * FIXME: should not be calling external functions on the signal stack.
1432 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1434 WORD fs, gs;
1435 init_handler( sigcontext, &fs, &gs );
1436 if (!dispatch_signal(SIGINT))
1438 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1439 rec->ExceptionCode = CONTROL_C_EXIT;
1443 /**********************************************************************
1444 * abrt_handler
1446 * Handler for SIGABRT.
1448 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1450 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1451 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1452 rec->ExceptionFlags = EH_NONCONTINUABLE;
1456 /**********************************************************************
1457 * quit_handler
1459 * Handler for SIGQUIT.
1461 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1463 WORD fs, gs;
1464 init_handler( sigcontext, &fs, &gs );
1465 server_abort_thread(0);
1469 /**********************************************************************
1470 * usr1_handler
1472 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1474 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1476 CONTEXT context;
1477 WORD fs, gs;
1479 init_handler( sigcontext, &fs, &gs );
1480 save_context( &context, sigcontext, fs, gs );
1481 wait_suspend( &context );
1482 restore_context( &context, sigcontext );
1486 /**********************************************************************
1487 * get_signal_stack_total_size
1489 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1490 * Must be a power of two.
1492 size_t get_signal_stack_total_size(void)
1494 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1496 if (!signal_stack_size)
1498 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1499 /* find the first power of two not smaller than min_size */
1500 while (size < min_size) size *= 2;
1501 signal_stack_mask = size - 1;
1502 signal_stack_size = size - teb_size;
1504 return signal_stack_size + teb_size;
1508 /***********************************************************************
1509 * __wine_set_signal_handler (NTDLL.@)
1511 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1513 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1514 if (handlers[sig] != NULL) return -2;
1515 handlers[sig] = wsh;
1516 return 0;
1520 /**********************************************************************
1521 * signal_init_thread
1523 void signal_init_thread(void)
1525 #ifdef HAVE_SIGALTSTACK
1526 stack_t ss;
1528 #ifdef __APPLE__
1529 int mib[2], val = 1;
1531 mib[0] = CTL_KERN;
1532 mib[1] = KERN_THALTSTACK;
1533 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1534 #endif
1536 ss.ss_sp = get_signal_stack();
1537 ss.ss_size = signal_stack_size;
1538 ss.ss_flags = 0;
1539 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
1540 #endif /* HAVE_SIGALTSTACK */
1542 ntdll_get_thread_data()->gs = wine_get_gs();
1545 /**********************************************************************
1546 * signal_init_process
1548 void signal_init_process(void)
1550 struct sigaction sig_act;
1552 sig_act.sa_mask = server_block_set;
1553 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1554 #ifdef SA_ONSTACK
1555 sig_act.sa_flags |= SA_ONSTACK;
1556 #endif
1558 sig_act.sa_sigaction = int_handler;
1559 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1560 sig_act.sa_sigaction = fpe_handler;
1561 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1562 sig_act.sa_sigaction = abrt_handler;
1563 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1564 sig_act.sa_sigaction = quit_handler;
1565 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1566 sig_act.sa_sigaction = usr1_handler;
1567 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1569 sig_act.sa_sigaction = segv_handler;
1570 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1571 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1572 #ifdef SIGBUS
1573 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1574 #endif
1576 #ifdef SIGTRAP
1577 sig_act.sa_sigaction = trap_handler;
1578 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1579 #endif
1581 #ifdef __HAVE_VM86
1582 sig_act.sa_sigaction = usr2_handler;
1583 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1584 #endif
1586 signal_init_thread();
1587 return;
1589 error:
1590 perror("sigaction");
1591 exit(1);
1595 #ifdef __HAVE_VM86
1596 /**********************************************************************
1597 * __wine_enter_vm86 (NTDLL.@)
1599 * Enter vm86 mode with the specified register context.
1601 void __wine_enter_vm86( CONTEXT *context )
1603 EXCEPTION_RECORD rec;
1604 int res;
1605 struct vm86plus_struct vm86;
1607 memset( &vm86, 0, sizeof(vm86) );
1608 for (;;)
1610 restore_vm86_context( context, &vm86 );
1612 ntdll_get_thread_data()->vm86_ptr = &vm86;
1613 merge_vm86_pending_flags( &rec );
1615 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1616 if (res < 0)
1618 errno = -res;
1619 return;
1622 save_vm86_context( context, &vm86 );
1624 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1625 rec.ExceptionRecord = NULL;
1626 rec.ExceptionAddress = (LPVOID)context->Eip;
1627 rec.NumberParameters = 0;
1629 switch(VM86_TYPE(res))
1631 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1632 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1633 break;
1634 case VM86_TRAP: /* return due to DOS-debugger request */
1635 switch(VM86_ARG(res))
1637 case TRAP_x86_TRCTRAP: /* Single-step exception */
1638 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1639 break;
1640 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1641 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1642 /* fall through */
1643 default:
1644 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1645 break;
1647 break;
1648 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1649 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1650 rec.NumberParameters = 1;
1651 rec.ExceptionInformation[0] = VM86_ARG(res);
1652 break;
1653 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1654 context->EFlags |= VIF_FLAG;
1655 context->EFlags &= ~VIP_FLAG;
1656 get_vm86_teb_info()->vm86_pending = 0;
1657 rec.ExceptionCode = EXCEPTION_VM86_STI;
1658 break;
1659 case VM86_PICRETURN: /* return due to pending PIC request */
1660 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1661 break;
1662 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1663 default:
1664 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1665 continue;
1667 __regs_RtlRaiseException( &rec, context );
1671 #else /* __HAVE_VM86 */
1672 /**********************************************************************
1673 * __wine_enter_vm86 (NTDLL.@)
1675 void __wine_enter_vm86( CONTEXT *context )
1677 MESSAGE("vm86 mode not supported on this platform\n");
1679 #endif /* __HAVE_VM86 */
1681 /**********************************************************************
1682 * DbgBreakPoint (NTDLL.@)
1684 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1686 /**********************************************************************
1687 * DbgUserBreakPoint (NTDLL.@)
1689 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1692 /**********************************************************************
1693 * EXC_CallHandler (internal)
1695 * Some exception handlers depend on EBP to have a fixed position relative to
1696 * the exception frame.
1697 * Shrinker depends on (*1) doing what it does,
1698 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1699 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1700 * function calling the handler having only 5 parameters (*4).
1702 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1703 " pushl %ebp\n"
1704 " movl %esp, %ebp\n"
1705 " pushl %ebx\n"
1706 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1707 " pushl 24(%ebp)\n"
1708 " pushl 20(%ebp)\n"
1709 " pushl 16(%ebp)\n"
1710 " pushl 12(%ebp)\n"
1711 " pushl 8(%ebp)\n"
1712 " call " __ASM_NAME("call_exception_handler") "\n"
1713 " popl %ebx\n"
1714 " leave\n"
1715 " ret\n"
1717 __ASM_GLOBAL_FUNC(call_exception_handler,
1718 " pushl %ebp\n"
1719 " movl %esp, %ebp\n"
1720 " subl $12,%esp\n"
1721 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1722 " pushl %edx\n" /* handler be handled by... */
1723 " .byte 0x64\n"
1724 " pushl (0)\n" /* nested_handler (passed in edx). */
1725 " .byte 0x64\n"
1726 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1727 " pushl 20(%ebp)\n"
1728 " pushl 16(%ebp)\n"
1729 " pushl 12(%ebp)\n"
1730 " pushl 8(%ebp)\n"
1731 " movl 24(%ebp), %ecx\n" /* (*1) */
1732 " call *%ecx\n" /* call handler. (*2) */
1733 " .byte 0x64\n"
1734 " movl (0), %esp\n" /* restore previous... (*3) */
1735 " .byte 0x64\n"
1736 " popl (0)\n" /* exception frame. */
1737 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1738 " popl %ebp\n"
1739 " ret $20\n" /* (*4) */
1741 #endif /* __i386__ */