msi: Downgrade a few messages to WARN.
[wine.git] / dlls / ntdll / signal_i386.c
blobd9c6aba24597bb41ecdcd391e409369cb5482f5e
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 */
131 int vm86_enter( void **vm86_ptr );
132 void vm86_return(void);
133 void vm86_return_end(void);
134 __ASM_GLOBAL_FUNC(vm86_enter,
135 "pushl %ebp\n\t"
136 "movl %esp, %ebp\n\t"
137 "movl $166,%eax\n\t" /*SYS_vm86*/
138 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
139 "movl (%ecx),%ecx\n\t"
140 "pushl %ebx\n\t"
141 "movl $1,%ebx\n\t" /*VM86_ENTER*/
142 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
143 "pushl %fs\n\t"
144 "pushl %gs\n\t"
145 "int $0x80\n"
146 ".globl " __ASM_NAME("vm86_return") "\n\t"
147 __ASM_FUNC("vm86_return") "\n"
148 __ASM_NAME("vm86_return") ":\n\t"
149 "popl %gs\n\t"
150 "popl %fs\n\t"
151 "popl %ecx\n\t"
152 "popl %ebx\n\t"
153 "popl %ebp\n\t"
154 "testl %eax,%eax\n\t"
155 "jl 0f\n\t"
156 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
157 "je " __ASM_NAME("vm86_enter") "\n\t"
158 "0:\n\t"
159 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
160 "movl $0,(%ecx)\n\t"
161 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
162 __ASM_FUNC("vm86_return_end") "\n"
163 __ASM_NAME("vm86_return_end") ":\n\t"
164 "ret" )
166 #ifdef HAVE_SYS_VM86_H
167 # define __HAVE_VM86
168 #endif
170 #endif /* linux */
172 #ifdef BSDI
174 #include <machine/frame.h>
175 typedef struct trapframe SIGCONTEXT;
177 #define EAX_sig(context) ((context)->tf_eax)
178 #define EBX_sig(context) ((context)->tf_ebx)
179 #define ECX_sig(context) ((context)->tf_ecx)
180 #define EDX_sig(context) ((context)->tf_edx)
181 #define ESI_sig(context) ((context)->tf_esi)
182 #define EDI_sig(context) ((context)->tf_edi)
183 #define EBP_sig(context) ((context)->tf_ebp)
185 #define CS_sig(context) ((context)->tf_cs)
186 #define DS_sig(context) ((context)->tf_ds)
187 #define ES_sig(context) ((context)->tf_es)
188 #define SS_sig(context) ((context)->tf_ss)
190 #define EFL_sig(context) ((context)->tf_eflags)
192 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
193 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
195 #define FPU_sig(context) NULL /* FIXME */
196 #define FPUX_sig(context) NULL /* FIXME */
198 #endif /* bsdi */
200 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
202 typedef struct sigcontext SIGCONTEXT;
204 #define EAX_sig(context) ((context)->sc_eax)
205 #define EBX_sig(context) ((context)->sc_ebx)
206 #define ECX_sig(context) ((context)->sc_ecx)
207 #define EDX_sig(context) ((context)->sc_edx)
208 #define ESI_sig(context) ((context)->sc_esi)
209 #define EDI_sig(context) ((context)->sc_edi)
210 #define EBP_sig(context) ((context)->sc_ebp)
212 #define CS_sig(context) ((context)->sc_cs)
213 #define DS_sig(context) ((context)->sc_ds)
214 #define ES_sig(context) ((context)->sc_es)
215 #define FS_sig(context) ((context)->sc_fs)
216 #define GS_sig(context) ((context)->sc_gs)
217 #define SS_sig(context) ((context)->sc_ss)
219 #define TRAP_sig(context) ((context)->sc_trapno)
220 #define ERROR_sig(context) ((context)->sc_err)
221 #define EFL_sig(context) ((context)->sc_eflags)
223 #define EIP_sig(context) ((context)->sc_eip)
224 #define ESP_sig(context) ((context)->sc_esp)
226 #define FPU_sig(context) NULL /* FIXME */
227 #define FPUX_sig(context) NULL /* FIXME */
229 #endif /* *BSD */
231 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
233 #ifdef _SCO_DS
234 #include <sys/regset.h>
235 #endif
236 #include <sys/ucontext.h>
237 typedef struct ucontext SIGCONTEXT;
239 #ifdef _SCO_DS
240 #define gregs regs
241 #endif
243 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
244 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
245 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
246 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
247 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
248 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
249 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
251 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
252 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
253 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
254 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
256 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
257 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
259 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
261 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
262 #ifdef UESP
263 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
264 #elif defined(R_ESP)
265 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
266 #else
267 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
268 #endif
269 #ifdef ERR
270 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
271 #endif
272 #ifdef TRAPNO
273 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
274 #endif
276 #define FPU_sig(context) NULL /* FIXME */
277 #define FPUX_sig(context) NULL /* FIXME */
279 #endif /* svr4 || SCO_DS */
281 #ifdef __APPLE__
282 # include <sys/ucontext.h>
284 typedef ucontext_t SIGCONTEXT;
286 /* work around silly renaming of struct members in OS X 10.5 */
287 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
288 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
289 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
290 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
291 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
292 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
293 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
294 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
295 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
296 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
297 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
298 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
299 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
300 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
301 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
302 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
303 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
304 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
305 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
306 #define FPU_sig(context) NULL
307 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
308 #else
309 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
310 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
311 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
312 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
313 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
314 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
315 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
316 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
317 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
318 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
319 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
320 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
321 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
322 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
323 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
324 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
325 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
326 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
327 #define FPU_sig(context) NULL
328 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
329 #endif
331 #endif /* __APPLE__ */
333 WINE_DEFAULT_DEBUG_CHANNEL(seh);
335 typedef int (*wine_signal_handler)(unsigned int sig);
337 static size_t signal_stack_mask;
338 static size_t signal_stack_size;
340 static wine_signal_handler handlers[256];
342 static int fpux_support; /* whether the CPU support extended fpu context */
344 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
346 enum i386_trap_code
348 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
349 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
350 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
351 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
352 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
353 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
354 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
355 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
356 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
357 TRAP_x86_DNA = T_DNA, /* Device not available exception */
358 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
359 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
360 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
361 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
362 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
363 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
364 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
365 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
366 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
367 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
368 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
369 #else
370 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
371 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
372 TRAP_x86_NMI = 2, /* NMI interrupt */
373 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
374 TRAP_x86_OFLOW = 4, /* Overflow exception */
375 TRAP_x86_BOUND = 5, /* Bound range exception */
376 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
377 TRAP_x86_DNA = 7, /* Device not available exception */
378 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
379 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
380 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
381 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
382 TRAP_x86_STKFLT = 12, /* Stack fault */
383 TRAP_x86_PROTFLT = 13, /* General protection fault */
384 TRAP_x86_PAGEFLT = 14, /* Page fault */
385 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
386 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
387 TRAP_x86_MCHK = 18, /* Machine check exception */
388 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
389 otherwise Cache flush exception (via SIGSEV) */
390 #endif
394 /***********************************************************************
395 * dispatch_signal
397 static inline int dispatch_signal(unsigned int sig)
399 if (handlers[sig] == NULL) return 0;
400 return handlers[sig](sig);
404 /***********************************************************************
405 * get_trap_code
407 * Get the trap code for a signal.
409 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
411 #ifdef TRAP_sig
412 return TRAP_sig(sigcontext);
413 #else
414 return TRAP_x86_UNKNOWN; /* unknown trap code */
415 #endif
418 /***********************************************************************
419 * get_error_code
421 * Get the error code for a signal.
423 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
425 #ifdef ERROR_sig
426 return ERROR_sig(sigcontext);
427 #else
428 return 0;
429 #endif
432 /***********************************************************************
433 * get_signal_stack
435 * Get the base of the signal stack for the current thread.
437 static inline void *get_signal_stack(void)
439 return (char *)NtCurrentTeb() + 4096;
443 /***********************************************************************
444 * get_current_teb
446 * Get the current teb based on the stack pointer.
448 static inline TEB *get_current_teb(void)
450 unsigned long esp;
451 __asm__("movl %%esp,%0" : "=g" (esp) );
452 return (TEB *)(esp & ~signal_stack_mask);
456 #ifdef __HAVE_VM86
457 /***********************************************************************
458 * save_vm86_context
460 * Set the register values from a vm86 structure.
462 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
464 context->ContextFlags = CONTEXT_FULL;
465 context->Eax = vm86->regs.eax;
466 context->Ebx = vm86->regs.ebx;
467 context->Ecx = vm86->regs.ecx;
468 context->Edx = vm86->regs.edx;
469 context->Esi = vm86->regs.esi;
470 context->Edi = vm86->regs.edi;
471 context->Esp = vm86->regs.esp;
472 context->Ebp = vm86->regs.ebp;
473 context->Eip = vm86->regs.eip;
474 context->SegCs = vm86->regs.cs;
475 context->SegDs = vm86->regs.ds;
476 context->SegEs = vm86->regs.es;
477 context->SegFs = vm86->regs.fs;
478 context->SegGs = vm86->regs.gs;
479 context->SegSs = vm86->regs.ss;
480 context->EFlags = vm86->regs.eflags;
484 /***********************************************************************
485 * restore_vm86_context
487 * Build a vm86 structure from the register values.
489 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
491 vm86->regs.eax = context->Eax;
492 vm86->regs.ebx = context->Ebx;
493 vm86->regs.ecx = context->Ecx;
494 vm86->regs.edx = context->Edx;
495 vm86->regs.esi = context->Esi;
496 vm86->regs.edi = context->Edi;
497 vm86->regs.esp = context->Esp;
498 vm86->regs.ebp = context->Ebp;
499 vm86->regs.eip = context->Eip;
500 vm86->regs.cs = context->SegCs;
501 vm86->regs.ds = context->SegDs;
502 vm86->regs.es = context->SegEs;
503 vm86->regs.fs = context->SegFs;
504 vm86->regs.gs = context->SegGs;
505 vm86->regs.ss = context->SegSs;
506 vm86->regs.eflags = context->EFlags;
510 /**********************************************************************
511 * merge_vm86_pending_flags
513 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
514 * raises exception if there are pending events and VIF flag
515 * has been turned on.
517 * Called from __wine_enter_vm86 because vm86_enter
518 * doesn't check for pending events.
520 * Called from raise_vm86_sti_exception to check for
521 * pending events in a signal safe way.
523 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
525 BOOL check_pending = TRUE;
526 struct vm86plus_struct *vm86 =
527 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
530 * In order to prevent a race when SIGUSR2 occurs while
531 * we are returning from exception handler, pending events
532 * will be rechecked after each raised exception.
534 while (check_pending && get_vm86_teb_info()->vm86_pending)
536 check_pending = FALSE;
537 ntdll_get_thread_data()->vm86_ptr = NULL;
540 * If VIF is set, throw exception.
541 * Note that SIGUSR2 may turn VIF flag off so
542 * VIF check must occur only when TEB.vm86_ptr is NULL.
544 if (vm86->regs.eflags & VIF_MASK)
546 CONTEXT vcontext;
547 save_vm86_context( &vcontext, vm86 );
549 rec->ExceptionCode = EXCEPTION_VM86_STI;
550 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
551 rec->ExceptionRecord = NULL;
552 rec->NumberParameters = 0;
553 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
555 vcontext.EFlags &= ~VIP_MASK;
556 get_vm86_teb_info()->vm86_pending = 0;
557 __regs_RtlRaiseException( rec, &vcontext );
559 restore_vm86_context( &vcontext, vm86 );
560 check_pending = TRUE;
563 ntdll_get_thread_data()->vm86_ptr = vm86;
567 * Merge VIP flags in a signal safe way. This requires
568 * that the following operation compiles into atomic
569 * instruction.
571 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
573 #endif /* __HAVE_VM86 */
576 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
579 /***********************************************************************
580 * init_handler
582 * Handler initialization when the full context is not needed.
584 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
586 void *stack = (void *)(ESP_sig(sigcontext) & ~3);
587 TEB *teb = get_current_teb();
588 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
590 /* get %fs and %gs at time of the fault */
591 #ifdef FS_sig
592 *fs = LOWORD(FS_sig(sigcontext));
593 #else
594 *fs = wine_get_fs();
595 #endif
596 #ifdef GS_sig
597 *gs = LOWORD(GS_sig(sigcontext));
598 #else
599 *gs = wine_get_gs();
600 #endif
602 wine_set_fs( thread_data->fs );
604 /* now restore a proper %gs for the fault handler */
605 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
606 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
609 * Win16 or DOS protected mode. Note that during switch
610 * from 16-bit mode to linear mode, CS may be set to system
611 * segment before FS is restored. Fortunately, in this case
612 * SS is still non-system segment. This is why both CS and SS
613 * are checked.
615 wine_set_gs( thread_data->gs );
616 stack = teb->WOW32Reserved;
618 #ifdef __HAVE_VM86
619 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
621 unsigned int *int_stack = stack;
622 /* fetch the saved %gs from the stack */
623 wine_set_gs( int_stack[0] );
625 #endif
626 else /* 32-bit mode */
628 #ifdef GS_sig
629 wine_set_gs( GS_sig(sigcontext) );
630 #endif
632 return stack;
636 /***********************************************************************
637 * save_fpu
639 * Save the thread FPU context.
641 static inline void save_fpu( CONTEXT *context )
643 #ifdef __GNUC__
644 context->ContextFlags |= CONTEXT_FLOATING_POINT;
645 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
646 #endif
650 /***********************************************************************
651 * save_fpux
653 * Save the thread FPU extended context.
655 static inline void save_fpux( CONTEXT *context )
657 #ifdef __GNUC__
658 /* we have to enforce alignment by hand */
659 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
660 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
662 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
663 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
664 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
665 #endif
669 /***********************************************************************
670 * restore_fpu
672 * Restore the FPU context to a sigcontext.
674 static inline void restore_fpu( const CONTEXT *context )
676 FLOATING_SAVE_AREA float_status = context->FloatSave;
677 /* reset the current interrupt status */
678 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
679 #ifdef __GNUC__
680 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
681 #endif /* __GNUC__ */
685 /***********************************************************************
686 * restore_fpux
688 * Restore the FPU extended context to a sigcontext.
690 static inline void restore_fpux( const CONTEXT *context )
692 #ifdef __GNUC__
693 /* we have to enforce alignment by hand */
694 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
695 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
697 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
698 /* reset the current interrupt status */
699 state->StatusWord &= state->ControlWord | 0xff80;
700 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
701 #endif
705 /***********************************************************************
706 * fpux_to_fpu
708 * Build a standard FPU context from an extended one.
710 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
712 unsigned int i, tag, stack_top;
714 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
715 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
716 fpu->ErrorOffset = fpux->ErrorOffset;
717 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
718 fpu->DataOffset = fpux->DataOffset;
719 fpu->DataSelector = fpux->DataSelector;
720 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
722 stack_top = (fpux->StatusWord >> 11) & 7;
723 fpu->TagWord = 0xffff0000;
724 for (i = 0; i < 8; i++)
726 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
727 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
728 else
730 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
731 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
733 tag = 2; /* special */
735 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
737 if (reg->Low) tag = 2; /* special */
738 else tag = 1; /* zero */
740 else
742 if (reg->Low >> 63) tag = 0; /* valid */
743 else tag = 2; /* special */
746 fpu->TagWord |= tag << (2 * i);
751 /***********************************************************************
752 * save_context
754 * Build a context structure from the signal info.
756 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
758 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
759 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
760 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
762 memset(context, 0, sizeof(*context));
763 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
764 context->Eax = EAX_sig(sigcontext);
765 context->Ebx = EBX_sig(sigcontext);
766 context->Ecx = ECX_sig(sigcontext);
767 context->Edx = EDX_sig(sigcontext);
768 context->Esi = ESI_sig(sigcontext);
769 context->Edi = EDI_sig(sigcontext);
770 context->Ebp = EBP_sig(sigcontext);
771 context->EFlags = EFL_sig(sigcontext);
772 context->Eip = EIP_sig(sigcontext);
773 context->Esp = ESP_sig(sigcontext);
774 context->SegCs = LOWORD(CS_sig(sigcontext));
775 context->SegDs = LOWORD(DS_sig(sigcontext));
776 context->SegEs = LOWORD(ES_sig(sigcontext));
777 context->SegFs = fs;
778 context->SegGs = gs;
779 context->SegSs = LOWORD(SS_sig(sigcontext));
780 context->Dr0 = regs->dr0;
781 context->Dr1 = regs->dr1;
782 context->Dr2 = regs->dr2;
783 context->Dr3 = regs->dr3;
784 context->Dr6 = regs->dr6;
785 context->Dr7 = regs->dr7;
787 if (fpu)
789 context->ContextFlags |= CONTEXT_FLOATING_POINT;
790 context->FloatSave = *fpu;
792 if (fpux)
794 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
795 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
796 fpux_support = 1;
797 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
799 if (!fpu && !fpux) save_fpu( context );
803 /***********************************************************************
804 * restore_context
806 * Restore the signal info from the context.
808 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
810 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
811 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
812 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
814 regs->dr0 = context->Dr0;
815 regs->dr1 = context->Dr1;
816 regs->dr2 = context->Dr2;
817 regs->dr3 = context->Dr3;
818 regs->dr6 = context->Dr6;
819 regs->dr7 = context->Dr7;
820 EAX_sig(sigcontext) = context->Eax;
821 EBX_sig(sigcontext) = context->Ebx;
822 ECX_sig(sigcontext) = context->Ecx;
823 EDX_sig(sigcontext) = context->Edx;
824 ESI_sig(sigcontext) = context->Esi;
825 EDI_sig(sigcontext) = context->Edi;
826 EBP_sig(sigcontext) = context->Ebp;
827 EFL_sig(sigcontext) = context->EFlags;
828 EIP_sig(sigcontext) = context->Eip;
829 ESP_sig(sigcontext) = context->Esp;
830 CS_sig(sigcontext) = context->SegCs;
831 DS_sig(sigcontext) = context->SegDs;
832 ES_sig(sigcontext) = context->SegEs;
833 SS_sig(sigcontext) = context->SegSs;
834 #ifdef GS_sig
835 GS_sig(sigcontext) = context->SegGs;
836 #else
837 wine_set_gs( context->SegGs );
838 #endif
839 #ifdef FS_sig
840 FS_sig(sigcontext) = context->SegFs;
841 #else
842 wine_set_fs( context->SegFs );
843 #endif
845 if (fpu) *fpu = context->FloatSave;
846 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
847 if (!fpu && !fpux) restore_fpu( context );
851 /***********************************************************************
852 * get_cpu_context
854 * Register function to get the context of the current thread.
856 void WINAPI __regs_get_cpu_context( CONTEXT *context, CONTEXT *regs )
858 *context = *regs;
859 if (fpux_support) save_fpux( context );
860 else save_fpu( context );
862 DEFINE_REGS_ENTRYPOINT( get_cpu_context, 4, 4 )
865 /***********************************************************************
866 * set_cpu_context
868 * Set the new CPU context. Used by NtSetContextThread.
870 void set_cpu_context( const CONTEXT *context )
872 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
874 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
875 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
877 if (flags & CONTEXT_DEBUG_REGISTERS)
879 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
880 regs->dr0 = context->Dr0;
881 regs->dr1 = context->Dr1;
882 regs->dr2 = context->Dr2;
883 regs->dr3 = context->Dr3;
884 regs->dr6 = context->Dr6;
885 regs->dr7 = context->Dr7;
887 if (flags & CONTEXT_FULL)
889 if (!(flags & CONTEXT_CONTROL))
890 FIXME( "setting partial context (%x) not supported\n", flags );
891 else if (flags & CONTEXT_SEGMENTS)
892 __wine_call_from_32_restore_regs( context );
893 else
895 CONTEXT newcontext = *context;
896 newcontext.SegDs = wine_get_ds();
897 newcontext.SegEs = wine_get_es();
898 newcontext.SegFs = wine_get_fs();
899 newcontext.SegGs = wine_get_gs();
900 __wine_call_from_32_restore_regs( &newcontext );
906 /***********************************************************************
907 * is_privileged_instr
909 * Check if the fault location is a privileged instruction.
910 * Based on the instruction emulation code in dlls/kernel/instr.c.
912 static inline DWORD is_privileged_instr( CONTEXT86 *context )
914 const BYTE *instr;
915 unsigned int prefix_count = 0;
917 if (!wine_ldt_is_system( context->SegCs )) return 0;
918 instr = (BYTE *)context->Eip;
920 for (;;) switch(*instr)
922 /* instruction prefixes */
923 case 0x2e: /* %cs: */
924 case 0x36: /* %ss: */
925 case 0x3e: /* %ds: */
926 case 0x26: /* %es: */
927 case 0x64: /* %fs: */
928 case 0x65: /* %gs: */
929 case 0x66: /* opcode size */
930 case 0x67: /* addr size */
931 case 0xf0: /* lock */
932 case 0xf2: /* repne */
933 case 0xf3: /* repe */
934 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
935 instr++;
936 continue;
938 case 0x0f: /* extended instruction */
939 switch(instr[1])
941 case 0x20: /* mov crX, reg */
942 case 0x21: /* mov drX, reg */
943 case 0x22: /* mov reg, crX */
944 case 0x23: /* mov reg drX */
945 return EXCEPTION_PRIV_INSTRUCTION;
947 return 0;
948 case 0x6c: /* insb (%dx) */
949 case 0x6d: /* insl (%dx) */
950 case 0x6e: /* outsb (%dx) */
951 case 0x6f: /* outsl (%dx) */
952 case 0xcd: /* int $xx */
953 case 0xe4: /* inb al,XX */
954 case 0xe5: /* in (e)ax,XX */
955 case 0xe6: /* outb XX,al */
956 case 0xe7: /* out XX,(e)ax */
957 case 0xec: /* inb (%dx),%al */
958 case 0xed: /* inl (%dx),%eax */
959 case 0xee: /* outb %al,(%dx) */
960 case 0xef: /* outl %eax,(%dx) */
961 case 0xf4: /* hlt */
962 case 0xfa: /* cli */
963 case 0xfb: /* sti */
964 return EXCEPTION_PRIV_INSTRUCTION;
965 default:
966 return 0;
971 #include "pshpack1.h"
972 struct atl_thunk
974 DWORD movl; /* movl this,4(%esp) */
975 DWORD this;
976 BYTE jmp; /* jmp func */
977 int func;
979 #include "poppack.h"
981 /**********************************************************************
982 * check_atl_thunk
984 * Check if code destination is an ATL thunk, and emulate it if so.
986 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
988 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
989 BOOL ret = FALSE;
991 __TRY
993 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
995 *((DWORD *)context->Esp + 1) = thunk->this;
996 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
997 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
998 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
999 ret = TRUE;
1002 __EXCEPT_PAGE_FAULT
1004 return FALSE;
1006 __ENDTRY
1007 return ret;
1011 /***********************************************************************
1012 * setup_exception_record
1014 * Setup the exception record and context on the thread stack.
1016 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1017 WORD fs, WORD gs, raise_func func )
1019 struct stack_layout
1021 void *ret_addr; /* return address from raise_func */
1022 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1023 CONTEXT *context_ptr; /* second arg for raise_func */
1024 CONTEXT context;
1025 EXCEPTION_RECORD rec;
1026 DWORD ebp;
1027 DWORD eip;
1028 } *stack = stack_ptr;
1029 DWORD exception_code = 0;
1031 /* stack sanity checks */
1033 if ((char *)stack >= (char *)get_signal_stack() &&
1034 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1036 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1037 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1038 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1039 NtCurrentTeb()->Tib.StackBase );
1040 server_abort_thread(1);
1043 if (stack - 1 > stack || /* check for overflow in subtraction */
1044 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1045 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1047 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1048 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1049 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1050 NtCurrentTeb()->Tib.StackBase );
1052 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1054 /* stack overflow on last page, unrecoverable */
1055 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (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);
1062 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1064 /* stack access below stack limit, may be recoverable */
1065 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1066 else
1068 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1069 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1070 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1071 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1072 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1073 server_abort_thread(1);
1077 stack--; /* push the stack_layout structure */
1078 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1079 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1080 #elif defined(VALGRIND_MAKE_WRITABLE)
1081 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1082 #endif
1083 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1084 stack->rec_ptr = &stack->rec;
1085 stack->context_ptr = &stack->context;
1087 stack->rec.ExceptionRecord = NULL;
1088 stack->rec.ExceptionCode = exception_code;
1089 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1090 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1091 stack->rec.NumberParameters = 0;
1093 save_context( &stack->context, sigcontext, fs, gs );
1095 /* now modify the sigcontext to return to the raise function */
1096 ESP_sig(sigcontext) = (DWORD)stack;
1097 EIP_sig(sigcontext) = (DWORD)func;
1098 /* clear single-step, direction, and align check flag */
1099 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1100 CS_sig(sigcontext) = wine_get_cs();
1101 DS_sig(sigcontext) = wine_get_ds();
1102 ES_sig(sigcontext) = wine_get_es();
1103 FS_sig(sigcontext) = wine_get_fs();
1104 GS_sig(sigcontext) = wine_get_gs();
1105 SS_sig(sigcontext) = wine_get_ss();
1107 return stack->rec_ptr;
1111 /***********************************************************************
1112 * setup_exception
1114 * Setup a proper stack frame for the raise function, and modify the
1115 * sigcontext so that the return from the signal handler will call
1116 * the raise function.
1118 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1120 WORD fs, gs;
1121 void *stack = init_handler( sigcontext, &fs, &gs );
1123 return setup_exception_record( sigcontext, stack, fs, gs, func );
1127 /***********************************************************************
1128 * get_exception_context
1130 * Get a pointer to the context built by setup_exception.
1132 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1134 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1138 /**********************************************************************
1139 * get_fpu_code
1141 * Get the FPU exception code from the FPU status.
1143 static inline DWORD get_fpu_code( const CONTEXT *context )
1145 DWORD status = context->FloatSave.StatusWord;
1147 if (status & 0x01) /* IE */
1149 if (status & 0x40) /* SF */
1150 return EXCEPTION_FLT_STACK_CHECK;
1151 else
1152 return EXCEPTION_FLT_INVALID_OPERATION;
1154 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1155 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1156 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1157 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1158 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1159 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1163 /**********************************************************************
1164 * raise_segv_exception
1166 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1168 switch(rec->ExceptionCode)
1170 case EXCEPTION_ACCESS_VIOLATION:
1171 if (rec->NumberParameters == 2)
1173 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1174 goto done;
1175 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1177 break;
1178 case EXCEPTION_DATATYPE_MISALIGNMENT:
1179 /* FIXME: pass through exception handler first? */
1180 if (context->EFlags & 0x00040000)
1182 /* Disable AC flag, return */
1183 context->EFlags &= ~0x00040000;
1184 goto done;
1186 break;
1188 __regs_RtlRaiseException( rec, context );
1189 done:
1190 NtSetContextThread( GetCurrentThread(), context );
1194 /**********************************************************************
1195 * raise_trap_exception
1197 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1199 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1201 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1203 /* when single stepping can't tell whether this is a hw bp or a
1204 * single step interrupt. try to avoid as much overhead as possible
1205 * and only do a server call if there is any hw bp enabled. */
1207 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1209 /* (possible) hardware breakpoint, fetch the debug registers */
1210 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1211 NtGetContextThread(GetCurrentThread(), context);
1212 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1215 context->EFlags &= ~0x100; /* clear single-step flag */
1218 __regs_RtlRaiseException( rec, context );
1219 NtSetContextThread( GetCurrentThread(), context );
1223 /**********************************************************************
1224 * raise_exception
1226 * Generic raise function for exceptions that don't need special treatment.
1228 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1230 __regs_RtlRaiseException( rec, context );
1231 NtSetContextThread( GetCurrentThread(), context );
1235 #ifdef __HAVE_VM86
1236 /**********************************************************************
1237 * raise_vm86_sti_exception
1239 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1241 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1242 get_vm86_teb_info()->vm86_pending |= VIP_MASK;
1244 if (ntdll_get_thread_data()->vm86_ptr)
1246 if (((char*)context->Eip >= (char*)vm86_return) &&
1247 ((char*)context->Eip <= (char*)vm86_return_end) &&
1248 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1249 /* exiting from VM86, can't throw */
1250 goto done;
1252 merge_vm86_pending_flags( rec );
1254 else if (get_vm86_teb_info()->dpmi_vif &&
1255 !wine_ldt_is_system(context->SegCs) &&
1256 !wine_ldt_is_system(context->SegSs))
1258 /* Executing DPMI code and virtual interrupts are enabled. */
1259 get_vm86_teb_info()->vm86_pending = 0;
1260 __regs_RtlRaiseException( rec, context );
1262 done:
1263 NtSetContextThread( GetCurrentThread(), context );
1267 /**********************************************************************
1268 * usr2_handler
1270 * Handler for SIGUSR2.
1271 * We use it to signal that the running __wine_enter_vm86() should
1272 * immediately set VIP_MASK, causing pending events to be handled
1273 * as early as possible.
1275 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1277 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1278 rec->ExceptionCode = EXCEPTION_VM86_STI;
1280 #endif /* __HAVE_VM86 */
1283 /**********************************************************************
1284 * segv_handler
1286 * Handler for SIGSEGV and related errors.
1288 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1290 WORD fs, gs;
1291 EXCEPTION_RECORD *rec;
1292 SIGCONTEXT *context = sigcontext;
1293 void *stack = init_handler( sigcontext, &fs, &gs );
1295 /* check for page fault inside the thread stack */
1296 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1297 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1298 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1299 virtual_handle_stack_fault( siginfo->si_addr ))
1301 /* check if this was the last guard page */
1302 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1304 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1305 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1307 return;
1310 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1311 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1313 switch(get_trap_code(context))
1315 case TRAP_x86_OFLOW: /* Overflow exception */
1316 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1317 break;
1318 case TRAP_x86_BOUND: /* Bound range exception */
1319 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1320 break;
1321 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1322 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1323 break;
1324 case TRAP_x86_STKFLT: /* Stack fault */
1325 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1326 break;
1327 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1328 case TRAP_x86_PROTFLT: /* General protection fault */
1329 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1331 WORD err = get_error_code(context);
1332 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1333 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1334 rec->NumberParameters = 2;
1335 rec->ExceptionInformation[0] = 0;
1336 /* if error contains a LDT selector, use that as fault address */
1337 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1338 rec->ExceptionInformation[1] = err & ~7;
1339 else
1340 rec->ExceptionInformation[1] = 0xffffffff;
1342 break;
1343 case TRAP_x86_PAGEFLT: /* Page fault */
1344 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1345 rec->NumberParameters = 2;
1346 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1347 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1348 break;
1349 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1350 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1351 break;
1352 default:
1353 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1354 /* fall through */
1355 case TRAP_x86_NMI: /* NMI interrupt */
1356 case TRAP_x86_DNA: /* Device not available exception */
1357 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1358 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1359 case TRAP_x86_MCHK: /* Machine check exception */
1360 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1361 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1362 break;
1367 /**********************************************************************
1368 * trap_handler
1370 * Handler for SIGTRAP.
1372 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1374 SIGCONTEXT *context = sigcontext;
1375 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1377 switch(get_trap_code(context))
1379 case TRAP_x86_TRCTRAP: /* Single-step exception */
1380 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1381 break;
1382 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1383 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1384 /* fall through */
1385 default:
1386 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1387 break;
1392 /**********************************************************************
1393 * fpe_handler
1395 * Handler for SIGFPE.
1397 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1399 CONTEXT *win_context;
1400 SIGCONTEXT *context = sigcontext;
1401 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1403 win_context = get_exception_context( rec );
1405 switch(get_trap_code(context))
1407 case TRAP_x86_DIVIDE: /* Division by zero exception */
1408 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1409 break;
1410 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1411 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1412 break;
1413 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1414 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1415 rec->ExceptionCode = get_fpu_code( win_context );
1416 break;
1417 case TRAP_x86_CACHEFLT: /* SIMD exception */
1418 /* TODO:
1419 * Behaviour only tested for divide-by-zero exceptions
1420 * Check for other SIMD exceptions as well */
1421 if(siginfo->si_code != FPE_FLTDIV)
1422 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1423 siginfo->si_code);
1425 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1426 rec->NumberParameters = 1;
1427 /* no idea what meaning is actually behind this but that's what native does */
1428 rec->ExceptionInformation[0] = 0;
1429 break;
1430 default:
1431 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1432 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1433 break;
1438 /**********************************************************************
1439 * int_handler
1441 * Handler for SIGINT.
1443 * FIXME: should not be calling external functions on the signal stack.
1445 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1447 WORD fs, gs;
1448 init_handler( sigcontext, &fs, &gs );
1449 if (!dispatch_signal(SIGINT))
1451 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1452 rec->ExceptionCode = CONTROL_C_EXIT;
1456 /**********************************************************************
1457 * abrt_handler
1459 * Handler for SIGABRT.
1461 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1463 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1464 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1465 rec->ExceptionFlags = EH_NONCONTINUABLE;
1469 /**********************************************************************
1470 * quit_handler
1472 * Handler for SIGQUIT.
1474 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1476 WORD fs, gs;
1477 init_handler( sigcontext, &fs, &gs );
1478 server_abort_thread(0);
1482 /**********************************************************************
1483 * usr1_handler
1485 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1487 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1489 CONTEXT context;
1490 WORD fs, gs;
1492 init_handler( sigcontext, &fs, &gs );
1493 save_context( &context, sigcontext, fs, gs );
1494 wait_suspend( &context );
1495 restore_context( &context, sigcontext );
1499 /**********************************************************************
1500 * get_signal_stack_total_size
1502 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1503 * Must be a power of two.
1505 size_t get_signal_stack_total_size(void)
1507 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1509 if (!signal_stack_size)
1511 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1512 /* find the first power of two not smaller than min_size */
1513 while (size < min_size) size *= 2;
1514 signal_stack_mask = size - 1;
1515 signal_stack_size = size - teb_size;
1517 return signal_stack_size + teb_size;
1521 /***********************************************************************
1522 * __wine_set_signal_handler (NTDLL.@)
1524 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1526 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1527 if (handlers[sig] != NULL) return -2;
1528 handlers[sig] = wsh;
1529 return 0;
1533 /**********************************************************************
1534 * SIGNAL_Init
1536 BOOL SIGNAL_Init(void)
1538 struct sigaction sig_act;
1540 #ifdef HAVE_SIGALTSTACK
1541 stack_t ss;
1543 #ifdef __APPLE__
1544 int mib[2], val = 1;
1546 mib[0] = CTL_KERN;
1547 mib[1] = KERN_THALTSTACK;
1548 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1549 #endif
1551 ss.ss_sp = get_signal_stack();
1552 ss.ss_size = signal_stack_size;
1553 ss.ss_flags = 0;
1554 if (sigaltstack(&ss, NULL) == -1)
1556 perror( "sigaltstack" );
1557 return FALSE;
1559 #endif /* HAVE_SIGALTSTACK */
1561 sig_act.sa_mask = server_block_set;
1562 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1563 #ifdef SA_ONSTACK
1564 sig_act.sa_flags |= SA_ONSTACK;
1565 #endif
1567 sig_act.sa_sigaction = int_handler;
1568 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1569 sig_act.sa_sigaction = fpe_handler;
1570 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1571 sig_act.sa_sigaction = abrt_handler;
1572 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1573 sig_act.sa_sigaction = quit_handler;
1574 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1575 sig_act.sa_sigaction = usr1_handler;
1576 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1578 sig_act.sa_sigaction = segv_handler;
1579 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1580 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1581 #ifdef SIGBUS
1582 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1583 #endif
1585 #ifdef SIGTRAP
1586 sig_act.sa_sigaction = trap_handler;
1587 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1588 #endif
1590 #ifdef __HAVE_VM86
1591 sig_act.sa_sigaction = usr2_handler;
1592 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1593 #endif
1595 return TRUE;
1597 error:
1598 perror("sigaction");
1599 return FALSE;
1603 #ifdef __HAVE_VM86
1604 /**********************************************************************
1605 * __wine_enter_vm86 (NTDLL.@)
1607 * Enter vm86 mode with the specified register context.
1609 void __wine_enter_vm86( CONTEXT *context )
1611 EXCEPTION_RECORD rec;
1612 int res;
1613 struct vm86plus_struct vm86;
1615 memset( &vm86, 0, sizeof(vm86) );
1616 for (;;)
1618 restore_vm86_context( context, &vm86 );
1620 ntdll_get_thread_data()->vm86_ptr = &vm86;
1621 merge_vm86_pending_flags( &rec );
1623 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1624 if (res < 0)
1626 errno = -res;
1627 return;
1630 save_vm86_context( context, &vm86 );
1632 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1633 rec.ExceptionRecord = NULL;
1634 rec.ExceptionAddress = (LPVOID)context->Eip;
1635 rec.NumberParameters = 0;
1637 switch(VM86_TYPE(res))
1639 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1640 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1641 break;
1642 case VM86_TRAP: /* return due to DOS-debugger request */
1643 switch(VM86_ARG(res))
1645 case TRAP_x86_TRCTRAP: /* Single-step exception */
1646 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1647 break;
1648 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1649 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1650 /* fall through */
1651 default:
1652 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1653 break;
1655 break;
1656 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1657 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1658 rec.NumberParameters = 1;
1659 rec.ExceptionInformation[0] = VM86_ARG(res);
1660 break;
1661 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1662 context->EFlags |= VIF_MASK;
1663 context->EFlags &= ~VIP_MASK;
1664 get_vm86_teb_info()->vm86_pending = 0;
1665 rec.ExceptionCode = EXCEPTION_VM86_STI;
1666 break;
1667 case VM86_PICRETURN: /* return due to pending PIC request */
1668 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1669 break;
1670 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1671 default:
1672 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1673 continue;
1675 __regs_RtlRaiseException( &rec, context );
1679 #else /* __HAVE_VM86 */
1680 /**********************************************************************
1681 * __wine_enter_vm86 (NTDLL.@)
1683 void __wine_enter_vm86( CONTEXT *context )
1685 MESSAGE("vm86 mode not supported on this platform\n");
1687 #endif /* __HAVE_VM86 */
1689 /**********************************************************************
1690 * DbgBreakPoint (NTDLL.@)
1692 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1694 /**********************************************************************
1695 * DbgUserBreakPoint (NTDLL.@)
1697 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1700 /**********************************************************************
1701 * EXC_CallHandler (internal)
1703 * Some exception handlers depend on EBP to have a fixed position relative to
1704 * the exception frame.
1705 * Shrinker depends on (*1) doing what it does,
1706 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1707 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1708 * function calling the handler having only 5 parameters (*4).
1710 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1711 " pushl %ebp\n"
1712 " movl %esp, %ebp\n"
1713 " pushl %ebx\n"
1714 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1715 " pushl 24(%ebp)\n"
1716 " pushl 20(%ebp)\n"
1717 " pushl 16(%ebp)\n"
1718 " pushl 12(%ebp)\n"
1719 " pushl 8(%ebp)\n"
1720 " call " __ASM_NAME("call_exception_handler") "\n"
1721 " popl %ebx\n"
1722 " leave\n"
1723 " ret\n"
1725 __ASM_GLOBAL_FUNC(call_exception_handler,
1726 " pushl %ebp\n"
1727 " movl %esp, %ebp\n"
1728 " subl $12,%esp\n"
1729 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1730 " pushl %edx\n" /* handler be handled by... */
1731 " .byte 0x64\n"
1732 " pushl (0)\n" /* nested_handler (passed in edx). */
1733 " .byte 0x64\n"
1734 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1735 " pushl 20(%ebp)\n"
1736 " pushl 16(%ebp)\n"
1737 " pushl 12(%ebp)\n"
1738 " pushl 8(%ebp)\n"
1739 " movl 24(%ebp), %ecx\n" /* (*1) */
1740 " call *%ecx\n" /* call handler. (*2) */
1741 " .byte 0x64\n"
1742 " movl (0), %esp\n" /* restore previous... (*3) */
1743 " .byte 0x64\n"
1744 " popl (0)\n" /* exception frame. */
1745 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1746 " popl %ebp\n"
1747 " ret $20\n" /* (*4) */
1749 #endif /* __i386__ */