Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / ntdll / signal_i386.c
blob58fd167b8ad8f81651acaff036d2cf0fb2b7eb31
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 "thread.h"
60 #include "wine/library.h"
61 #include "ntdll_misc.h"
62 #include "wine/exception.h"
63 #include "wine/debug.h"
65 #ifdef HAVE_VALGRIND_MEMCHECK_H
66 #include <valgrind/memcheck.h>
67 #endif
69 #undef ERR /* Solaris needs to define this */
71 /***********************************************************************
72 * signal context platform-specific definitions
75 #ifdef linux
77 typedef ucontext_t SIGCONTEXT;
79 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
80 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
81 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
82 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
83 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
84 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
85 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
86 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
88 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
89 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
90 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
91 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
92 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
93 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
95 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
96 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
97 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
98 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
100 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
102 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
104 int vm86_enter( void **vm86_ptr );
105 void vm86_return(void);
106 void vm86_return_end(void);
107 __ASM_GLOBAL_FUNC(vm86_enter,
108 "pushl %ebp\n\t"
109 "movl %esp, %ebp\n\t"
110 "movl $166,%eax\n\t" /*SYS_vm86*/
111 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
112 "movl (%ecx),%ecx\n\t"
113 "pushl %ebx\n\t"
114 "movl $1,%ebx\n\t" /*VM86_ENTER*/
115 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
116 "pushl %fs\n\t"
117 "pushl %gs\n\t"
118 "int $0x80\n"
119 ".globl " __ASM_NAME("vm86_return") "\n\t"
120 __ASM_FUNC("vm86_return") "\n"
121 __ASM_NAME("vm86_return") ":\n\t"
122 "popl %gs\n\t"
123 "popl %fs\n\t"
124 "popl %ecx\n\t"
125 "popl %ebx\n\t"
126 "popl %ebp\n\t"
127 "testl %eax,%eax\n\t"
128 "jl 0f\n\t"
129 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
130 "je " __ASM_NAME("vm86_enter") "\n\t"
131 "0:\n\t"
132 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
133 "movl $0,(%ecx)\n\t"
134 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
135 __ASM_FUNC("vm86_return_end") "\n"
136 __ASM_NAME("vm86_return_end") ":\n\t"
137 "ret" )
139 #ifdef HAVE_SYS_VM86_H
140 # define __HAVE_VM86
141 #endif
143 #endif /* linux */
145 #ifdef BSDI
147 #include <machine/frame.h>
148 typedef struct trapframe SIGCONTEXT;
150 #define EAX_sig(context) ((context)->tf_eax)
151 #define EBX_sig(context) ((context)->tf_ebx)
152 #define ECX_sig(context) ((context)->tf_ecx)
153 #define EDX_sig(context) ((context)->tf_edx)
154 #define ESI_sig(context) ((context)->tf_esi)
155 #define EDI_sig(context) ((context)->tf_edi)
156 #define EBP_sig(context) ((context)->tf_ebp)
158 #define CS_sig(context) ((context)->tf_cs)
159 #define DS_sig(context) ((context)->tf_ds)
160 #define ES_sig(context) ((context)->tf_es)
161 #define SS_sig(context) ((context)->tf_ss)
163 #define EFL_sig(context) ((context)->tf_eflags)
165 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
166 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
168 #endif /* bsdi */
170 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
172 typedef struct sigcontext SIGCONTEXT;
174 #define EAX_sig(context) ((context)->sc_eax)
175 #define EBX_sig(context) ((context)->sc_ebx)
176 #define ECX_sig(context) ((context)->sc_ecx)
177 #define EDX_sig(context) ((context)->sc_edx)
178 #define ESI_sig(context) ((context)->sc_esi)
179 #define EDI_sig(context) ((context)->sc_edi)
180 #define EBP_sig(context) ((context)->sc_ebp)
182 #define CS_sig(context) ((context)->sc_cs)
183 #define DS_sig(context) ((context)->sc_ds)
184 #define ES_sig(context) ((context)->sc_es)
185 #define FS_sig(context) ((context)->sc_fs)
186 #define GS_sig(context) ((context)->sc_gs)
187 #define SS_sig(context) ((context)->sc_ss)
189 #define TRAP_sig(context) ((context)->sc_trapno)
190 #define ERROR_sig(context) ((context)->sc_err)
191 #define EFL_sig(context) ((context)->sc_eflags)
193 #define EIP_sig(context) ((context)->sc_eip)
194 #define ESP_sig(context) ((context)->sc_esp)
196 #endif /* *BSD */
198 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
200 #ifdef _SCO_DS
201 #include <sys/regset.h>
202 #endif
203 #include <sys/ucontext.h>
204 typedef struct ucontext SIGCONTEXT;
206 #ifdef _SCO_DS
207 #define gregs regs
208 #endif
210 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
211 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
212 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
213 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
214 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
215 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
216 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
218 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
219 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
220 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
221 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
223 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
224 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
226 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
228 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
229 #ifdef UESP
230 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
231 #elif defined(R_ESP)
232 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
233 #else
234 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
235 #endif
236 #ifdef ERR
237 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
238 #endif
239 #ifdef TRAPNO
240 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
241 #endif
243 #endif /* svr4 || SCO_DS */
245 #ifdef __APPLE__
246 # include <sys/ucontext.h>
248 typedef ucontext_t SIGCONTEXT;
250 /* work around silly renaming of struct members in OS X 10.5 */
251 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
252 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
253 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
254 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
255 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
256 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
257 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
258 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
259 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
260 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
261 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
262 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
263 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
264 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
265 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
266 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
267 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
268 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
269 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
270 #else
271 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
272 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
273 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
274 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
275 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
276 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
277 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
278 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
279 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
280 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
281 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
282 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
283 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
284 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
285 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
286 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
287 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
288 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
289 #endif
291 #endif /* __APPLE__ */
293 WINE_DEFAULT_DEBUG_CHANNEL(seh);
295 typedef int (*wine_signal_handler)(unsigned int sig);
297 static size_t signal_stack_mask;
298 static size_t signal_stack_size;
300 static wine_signal_handler handlers[256];
302 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
304 enum i386_trap_code
306 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
307 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
308 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
309 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
310 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
311 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
312 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
313 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
314 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
315 TRAP_x86_DNA = T_DNA, /* Device not available exception */
316 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
317 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
318 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
319 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
320 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
321 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
322 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
323 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
324 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
325 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
326 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
327 #else
328 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
329 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
330 TRAP_x86_NMI = 2, /* NMI interrupt */
331 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
332 TRAP_x86_OFLOW = 4, /* Overflow exception */
333 TRAP_x86_BOUND = 5, /* Bound range exception */
334 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
335 TRAP_x86_DNA = 7, /* Device not available exception */
336 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
337 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
338 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
339 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
340 TRAP_x86_STKFLT = 12, /* Stack fault */
341 TRAP_x86_PROTFLT = 13, /* General protection fault */
342 TRAP_x86_PAGEFLT = 14, /* Page fault */
343 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
344 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
345 TRAP_x86_MCHK = 18, /* Machine check exception */
346 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
347 otherwise Cache flush exception (via SIGSEV) */
348 #endif
352 /***********************************************************************
353 * dispatch_signal
355 static inline int dispatch_signal(unsigned int sig)
357 if (handlers[sig] == NULL) return 0;
358 return handlers[sig](sig);
362 /***********************************************************************
363 * get_trap_code
365 * Get the trap code for a signal.
367 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
369 #ifdef TRAP_sig
370 return TRAP_sig(sigcontext);
371 #else
372 return TRAP_x86_UNKNOWN; /* unknown trap code */
373 #endif
376 /***********************************************************************
377 * get_error_code
379 * Get the error code for a signal.
381 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
383 #ifdef ERROR_sig
384 return ERROR_sig(sigcontext);
385 #else
386 return 0;
387 #endif
390 /***********************************************************************
391 * get_signal_stack
393 * Get the base of the signal stack for the current thread.
395 static inline void *get_signal_stack(void)
397 return (char *)NtCurrentTeb() + 4096;
401 /***********************************************************************
402 * get_current_teb
404 * Get the current teb based on the stack pointer.
406 static inline TEB *get_current_teb(void)
408 unsigned long esp;
409 __asm__("movl %%esp,%0" : "=g" (esp) );
410 return (TEB *)(esp & ~signal_stack_mask);
414 #ifdef __HAVE_VM86
415 /***********************************************************************
416 * save_vm86_context
418 * Set the register values from a vm86 structure.
420 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
422 context->ContextFlags = CONTEXT_FULL;
423 context->Eax = vm86->regs.eax;
424 context->Ebx = vm86->regs.ebx;
425 context->Ecx = vm86->regs.ecx;
426 context->Edx = vm86->regs.edx;
427 context->Esi = vm86->regs.esi;
428 context->Edi = vm86->regs.edi;
429 context->Esp = vm86->regs.esp;
430 context->Ebp = vm86->regs.ebp;
431 context->Eip = vm86->regs.eip;
432 context->SegCs = vm86->regs.cs;
433 context->SegDs = vm86->regs.ds;
434 context->SegEs = vm86->regs.es;
435 context->SegFs = vm86->regs.fs;
436 context->SegGs = vm86->regs.gs;
437 context->SegSs = vm86->regs.ss;
438 context->EFlags = vm86->regs.eflags;
442 /***********************************************************************
443 * restore_vm86_context
445 * Build a vm86 structure from the register values.
447 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
449 vm86->regs.eax = context->Eax;
450 vm86->regs.ebx = context->Ebx;
451 vm86->regs.ecx = context->Ecx;
452 vm86->regs.edx = context->Edx;
453 vm86->regs.esi = context->Esi;
454 vm86->regs.edi = context->Edi;
455 vm86->regs.esp = context->Esp;
456 vm86->regs.ebp = context->Ebp;
457 vm86->regs.eip = context->Eip;
458 vm86->regs.cs = context->SegCs;
459 vm86->regs.ds = context->SegDs;
460 vm86->regs.es = context->SegEs;
461 vm86->regs.fs = context->SegFs;
462 vm86->regs.gs = context->SegGs;
463 vm86->regs.ss = context->SegSs;
464 vm86->regs.eflags = context->EFlags;
468 /**********************************************************************
469 * merge_vm86_pending_flags
471 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
472 * raises exception if there are pending events and VIF flag
473 * has been turned on.
475 * Called from __wine_enter_vm86 because vm86_enter
476 * doesn't check for pending events.
478 * Called from raise_vm86_sti_exception to check for
479 * pending events in a signal safe way.
481 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
483 BOOL check_pending = TRUE;
484 struct vm86plus_struct *vm86 =
485 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
488 * In order to prevent a race when SIGUSR2 occurs while
489 * we are returning from exception handler, pending events
490 * will be rechecked after each raised exception.
492 while (check_pending && NtCurrentTeb()->vm86_pending)
494 check_pending = FALSE;
495 ntdll_get_thread_data()->vm86_ptr = NULL;
498 * If VIF is set, throw exception.
499 * Note that SIGUSR2 may turn VIF flag off so
500 * VIF check must occur only when TEB.vm86_ptr is NULL.
502 if (vm86->regs.eflags & VIF_MASK)
504 CONTEXT vcontext;
505 save_vm86_context( &vcontext, vm86 );
507 rec->ExceptionCode = EXCEPTION_VM86_STI;
508 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
509 rec->ExceptionRecord = NULL;
510 rec->NumberParameters = 0;
511 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
513 vcontext.EFlags &= ~VIP_MASK;
514 NtCurrentTeb()->vm86_pending = 0;
515 __regs_RtlRaiseException( rec, &vcontext );
517 restore_vm86_context( &vcontext, vm86 );
518 check_pending = TRUE;
521 ntdll_get_thread_data()->vm86_ptr = vm86;
525 * Merge VIP flags in a signal safe way. This requires
526 * that the following operation compiles into atomic
527 * instruction.
529 vm86->regs.eflags |= NtCurrentTeb()->vm86_pending;
531 #endif /* __HAVE_VM86 */
534 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
537 /***********************************************************************
538 * init_handler
540 * Handler initialization when the full context is not needed.
542 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
544 void *stack = (void *)(ESP_sig(sigcontext) & ~3);
545 TEB *teb = get_current_teb();
546 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
548 /* get %fs and %gs at time of the fault */
549 #ifdef FS_sig
550 *fs = LOWORD(FS_sig(sigcontext));
551 #else
552 *fs = wine_get_fs();
553 #endif
554 #ifdef GS_sig
555 *gs = LOWORD(GS_sig(sigcontext));
556 #else
557 *gs = wine_get_gs();
558 #endif
560 wine_set_fs( thread_regs->fs );
562 /* now restore a proper %gs for the fault handler */
563 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
564 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
567 * Win16 or DOS protected mode. Note that during switch
568 * from 16-bit mode to linear mode, CS may be set to system
569 * segment before FS is restored. Fortunately, in this case
570 * SS is still non-system segment. This is why both CS and SS
571 * are checked.
573 wine_set_gs( thread_regs->gs );
574 stack = teb->WOW32Reserved;
576 #ifdef __HAVE_VM86
577 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
579 unsigned int *int_stack = stack;
580 /* fetch the saved %gs from the stack */
581 wine_set_gs( int_stack[0] );
583 #endif
584 else /* 32-bit mode */
586 #ifdef GS_sig
587 wine_set_gs( GS_sig(sigcontext) );
588 #endif
590 return stack;
594 /***********************************************************************
595 * save_fpu
597 * Save the thread FPU context.
599 static inline void save_fpu( CONTEXT *context )
601 #ifdef __GNUC__
602 context->ContextFlags |= CONTEXT_FLOATING_POINT;
603 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
604 #endif
608 /***********************************************************************
609 * restore_fpu
611 * Restore the FPU context to a sigcontext.
613 static inline void restore_fpu( const CONTEXT *context )
615 FLOATING_SAVE_AREA float_status = context->FloatSave;
616 /* reset the current interrupt status */
617 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
618 #ifdef __GNUC__
619 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
620 #endif /* __GNUC__ */
624 /***********************************************************************
625 * save_context
627 * Build a context structure from the signal info.
629 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
631 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
633 memset(context, 0, sizeof(*context));
634 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
635 context->Eax = EAX_sig(sigcontext);
636 context->Ebx = EBX_sig(sigcontext);
637 context->Ecx = ECX_sig(sigcontext);
638 context->Edx = EDX_sig(sigcontext);
639 context->Esi = ESI_sig(sigcontext);
640 context->Edi = EDI_sig(sigcontext);
641 context->Ebp = EBP_sig(sigcontext);
642 context->EFlags = EFL_sig(sigcontext);
643 context->Eip = EIP_sig(sigcontext);
644 context->Esp = ESP_sig(sigcontext);
645 context->SegCs = LOWORD(CS_sig(sigcontext));
646 context->SegDs = LOWORD(DS_sig(sigcontext));
647 context->SegEs = LOWORD(ES_sig(sigcontext));
648 context->SegFs = fs;
649 context->SegGs = gs;
650 context->SegSs = LOWORD(SS_sig(sigcontext));
651 context->Dr0 = regs->dr0;
652 context->Dr1 = regs->dr1;
653 context->Dr2 = regs->dr2;
654 context->Dr3 = regs->dr3;
655 context->Dr6 = regs->dr6;
656 context->Dr7 = regs->dr7;
658 #ifdef FPU_sig
659 if (FPU_sig(sigcontext))
661 context->ContextFlags |= CONTEXT_FLOATING_POINT;
662 context->FloatSave = *FPU_sig(sigcontext);
664 else
665 #endif
667 save_fpu( context );
672 /***********************************************************************
673 * restore_context
675 * Restore the signal info from the context.
677 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
679 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
681 regs->dr0 = context->Dr0;
682 regs->dr1 = context->Dr1;
683 regs->dr2 = context->Dr2;
684 regs->dr3 = context->Dr3;
685 regs->dr6 = context->Dr6;
686 regs->dr7 = context->Dr7;
687 EAX_sig(sigcontext) = context->Eax;
688 EBX_sig(sigcontext) = context->Ebx;
689 ECX_sig(sigcontext) = context->Ecx;
690 EDX_sig(sigcontext) = context->Edx;
691 ESI_sig(sigcontext) = context->Esi;
692 EDI_sig(sigcontext) = context->Edi;
693 EBP_sig(sigcontext) = context->Ebp;
694 EFL_sig(sigcontext) = context->EFlags;
695 EIP_sig(sigcontext) = context->Eip;
696 ESP_sig(sigcontext) = context->Esp;
697 CS_sig(sigcontext) = context->SegCs;
698 DS_sig(sigcontext) = context->SegDs;
699 ES_sig(sigcontext) = context->SegEs;
700 SS_sig(sigcontext) = context->SegSs;
701 #ifdef GS_sig
702 GS_sig(sigcontext) = context->SegGs;
703 #else
704 wine_set_gs( context->SegGs );
705 #endif
706 #ifdef FS_sig
707 FS_sig(sigcontext) = context->SegFs;
708 #else
709 wine_set_fs( context->SegFs );
710 #endif
712 #ifdef FPU_sig
713 if (FPU_sig(sigcontext))
715 *FPU_sig(sigcontext) = context->FloatSave;
717 else
718 #endif
720 restore_fpu( context );
725 /***********************************************************************
726 * get_cpu_context
728 * Register function to get the context of the current thread.
730 void WINAPI __regs_get_cpu_context( CONTEXT *context, CONTEXT *regs )
732 *context = *regs;
733 save_fpu( context );
735 DEFINE_REGS_ENTRYPOINT( get_cpu_context, 4, 4 );
738 /***********************************************************************
739 * set_cpu_context
741 * Set the new CPU context. Used by NtSetContextThread.
743 void set_cpu_context( const CONTEXT *context )
745 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
747 if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
749 if (flags & CONTEXT_DEBUG_REGISTERS)
751 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
752 regs->dr0 = context->Dr0;
753 regs->dr1 = context->Dr1;
754 regs->dr2 = context->Dr2;
755 regs->dr3 = context->Dr3;
756 regs->dr6 = context->Dr6;
757 regs->dr7 = context->Dr7;
759 if (flags & CONTEXT_FULL)
761 if ((flags & CONTEXT_FULL) != (CONTEXT_FULL & ~CONTEXT_i386))
762 FIXME( "setting partial context (%x) not supported\n", flags );
763 else
764 __wine_call_from_32_restore_regs( context );
769 /***********************************************************************
770 * is_privileged_instr
772 * Check if the fault location is a privileged instruction.
773 * Based on the instruction emulation code in dlls/kernel/instr.c.
775 static inline DWORD is_privileged_instr( CONTEXT86 *context )
777 const BYTE *instr;
778 unsigned int prefix_count = 0;
780 if (!wine_ldt_is_system( context->SegCs )) return 0;
781 instr = (BYTE *)context->Eip;
783 for (;;) switch(*instr)
785 /* instruction prefixes */
786 case 0x2e: /* %cs: */
787 case 0x36: /* %ss: */
788 case 0x3e: /* %ds: */
789 case 0x26: /* %es: */
790 case 0x64: /* %fs: */
791 case 0x65: /* %gs: */
792 case 0x66: /* opcode size */
793 case 0x67: /* addr size */
794 case 0xf0: /* lock */
795 case 0xf2: /* repne */
796 case 0xf3: /* repe */
797 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
798 instr++;
799 continue;
801 case 0x0f: /* extended instruction */
802 switch(instr[1])
804 case 0x20: /* mov crX, reg */
805 case 0x21: /* mov drX, reg */
806 case 0x22: /* mov reg, crX */
807 case 0x23: /* mov reg drX */
808 return EXCEPTION_PRIV_INSTRUCTION;
810 return 0;
811 case 0x6c: /* insb (%dx) */
812 case 0x6d: /* insl (%dx) */
813 case 0x6e: /* outsb (%dx) */
814 case 0x6f: /* outsl (%dx) */
815 case 0xcd: /* int $xx */
816 case 0xe4: /* inb al,XX */
817 case 0xe5: /* in (e)ax,XX */
818 case 0xe6: /* outb XX,al */
819 case 0xe7: /* out XX,(e)ax */
820 case 0xec: /* inb (%dx),%al */
821 case 0xed: /* inl (%dx),%eax */
822 case 0xee: /* outb %al,(%dx) */
823 case 0xef: /* outl %eax,(%dx) */
824 case 0xf4: /* hlt */
825 case 0xfa: /* cli */
826 case 0xfb: /* sti */
827 return EXCEPTION_PRIV_INSTRUCTION;
828 default:
829 return 0;
834 #include "pshpack1.h"
835 struct atl_thunk
837 DWORD movl; /* movl this,4(%esp) */
838 DWORD this;
839 BYTE jmp; /* jmp func */
840 int func;
842 #include "poppack.h"
844 /**********************************************************************
845 * check_atl_thunk
847 * Check if code destination is an ATL thunk, and emulate it if so.
849 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
851 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
852 BOOL ret = FALSE;
854 __TRY
856 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
858 *((DWORD *)context->Esp + 1) = thunk->this;
859 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
860 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
861 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
862 ret = TRUE;
865 __EXCEPT_PAGE_FAULT
867 return FALSE;
869 __ENDTRY
870 return ret;
874 /***********************************************************************
875 * setup_exception
877 * Setup a proper stack frame for the raise function, and modify the
878 * sigcontext so that the return from the signal handler will call
879 * the raise function.
881 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
883 struct stack_layout
885 void *ret_addr; /* return address from raise_func */
886 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
887 CONTEXT *context_ptr; /* second arg for raise_func */
888 CONTEXT context;
889 EXCEPTION_RECORD rec;
890 DWORD ebp;
891 DWORD eip;
892 } *stack;
894 WORD fs, gs;
896 stack = init_handler( sigcontext, &fs, &gs );
898 /* stack sanity checks */
900 if ((char *)stack >= (char *)get_signal_stack() &&
901 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
903 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
904 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
905 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
906 NtCurrentTeb()->Tib.StackBase );
907 server_abort_thread(1);
910 if (stack - 1 > stack || /* check for overflow in subtraction */
911 (char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit ||
912 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
914 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)stack;
915 if (diff < 4096)
917 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p\n",
918 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
919 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
920 NtCurrentTeb()->Tib.StackBase );
921 server_abort_thread(1);
923 else WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
924 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
925 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
926 NtCurrentTeb()->Tib.StackBase );
929 stack--; /* push the stack_layout structure */
930 #ifdef HAVE_VALGRIND_MEMCHECK_H
931 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
932 #endif
933 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
934 stack->rec_ptr = &stack->rec;
935 stack->context_ptr = &stack->context;
937 stack->rec.ExceptionRecord = NULL;
938 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
939 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
940 stack->rec.NumberParameters = 0;
942 save_context( &stack->context, sigcontext, fs, gs );
944 /* now modify the sigcontext to return to the raise function */
945 ESP_sig(sigcontext) = (DWORD)stack;
946 EIP_sig(sigcontext) = (DWORD)func;
947 /* clear single-step and align check flag */
948 EFL_sig(sigcontext) &= ~(0x100|0x40000);
949 CS_sig(sigcontext) = wine_get_cs();
950 DS_sig(sigcontext) = wine_get_ds();
951 ES_sig(sigcontext) = wine_get_es();
952 FS_sig(sigcontext) = wine_get_fs();
953 GS_sig(sigcontext) = wine_get_gs();
954 SS_sig(sigcontext) = wine_get_ss();
956 return stack->rec_ptr;
960 /***********************************************************************
961 * get_exception_context
963 * Get a pointer to the context built by setup_exception.
965 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
967 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
971 /**********************************************************************
972 * get_fpu_code
974 * Get the FPU exception code from the FPU status.
976 static inline DWORD get_fpu_code( const CONTEXT *context )
978 DWORD status = context->FloatSave.StatusWord;
980 if (status & 0x01) /* IE */
982 if (status & 0x40) /* SF */
983 return EXCEPTION_FLT_STACK_CHECK;
984 else
985 return EXCEPTION_FLT_INVALID_OPERATION;
987 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
988 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
989 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
990 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
991 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
992 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
996 /**********************************************************************
997 * raise_segv_exception
999 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1001 switch(rec->ExceptionCode)
1003 case EXCEPTION_ACCESS_VIOLATION:
1004 if (rec->NumberParameters == 2)
1006 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1007 goto done;
1008 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1010 break;
1011 case EXCEPTION_DATATYPE_MISALIGNMENT:
1012 /* FIXME: pass through exception handler first? */
1013 if (context->EFlags & 0x00040000)
1015 /* Disable AC flag, return */
1016 context->EFlags &= ~0x00040000;
1017 goto done;
1019 break;
1021 __regs_RtlRaiseException( rec, context );
1022 done:
1023 NtSetContextThread( GetCurrentThread(), context );
1027 /**********************************************************************
1028 * raise_trap_exception
1030 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1032 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1034 if (context->EFlags & 0x100)
1036 context->EFlags &= ~0x100; /* clear single-step flag */
1038 else /* hardware breakpoint, fetch the debug registers */
1040 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1041 NtGetContextThread(GetCurrentThread(), context);
1042 /* we have either:
1043 * - a bp from a debug register
1044 * - a single step interrupt at popf instruction, which just has
1045 * removed the TF.
1046 * - someone did a kill(SIGTRAP) on us, and we shall return
1047 * a breakpoint, not a single step exception
1049 if ( !(context->Dr6 & 0xf) && !(context->Dr6 & 0x4000) )
1050 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1051 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1055 __regs_RtlRaiseException( rec, context );
1056 NtSetContextThread( GetCurrentThread(), context );
1060 /**********************************************************************
1061 * raise_exception
1063 * Generic raise function for exceptions that don't need special treatment.
1065 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1067 __regs_RtlRaiseException( rec, context );
1068 NtSetContextThread( GetCurrentThread(), context );
1072 #ifdef __HAVE_VM86
1073 /**********************************************************************
1074 * raise_vm86_sti_exception
1076 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1078 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1079 NtCurrentTeb()->vm86_pending |= VIP_MASK;
1081 if (ntdll_get_thread_data()->vm86_ptr)
1083 if (((char*)context->Eip >= (char*)vm86_return) &&
1084 ((char*)context->Eip <= (char*)vm86_return_end) &&
1085 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1086 /* exiting from VM86, can't throw */
1087 goto done;
1089 merge_vm86_pending_flags( rec );
1091 else if (NtCurrentTeb()->dpmi_vif &&
1092 !wine_ldt_is_system(context->SegCs) &&
1093 !wine_ldt_is_system(context->SegSs))
1095 /* Executing DPMI code and virtual interrupts are enabled. */
1096 NtCurrentTeb()->vm86_pending = 0;
1097 __regs_RtlRaiseException( rec, context );
1099 done:
1100 NtSetContextThread( GetCurrentThread(), context );
1104 /**********************************************************************
1105 * usr2_handler
1107 * Handler for SIGUSR2.
1108 * We use it to signal that the running __wine_enter_vm86() should
1109 * immediately set VIP_MASK, causing pending events to be handled
1110 * as early as possible.
1112 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1114 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1115 rec->ExceptionCode = EXCEPTION_VM86_STI;
1117 #endif /* __HAVE_VM86 */
1120 /**********************************************************************
1121 * segv_handler
1123 * Handler for SIGSEGV and related errors.
1125 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1127 SIGCONTEXT *context = sigcontext;
1128 EXCEPTION_RECORD *rec;
1130 #ifdef __APPLE__
1131 if (get_trap_code(context) == 0) return;
1132 #endif
1134 rec = setup_exception( context, raise_segv_exception );
1136 switch(get_trap_code(context))
1138 case TRAP_x86_OFLOW: /* Overflow exception */
1139 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1140 break;
1141 case TRAP_x86_BOUND: /* Bound range exception */
1142 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1143 break;
1144 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1145 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1146 break;
1147 case TRAP_x86_STKFLT: /* Stack fault */
1148 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1149 break;
1150 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1151 case TRAP_x86_PROTFLT: /* General protection fault */
1152 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1154 WORD err = get_error_code(context);
1155 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1156 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1157 rec->NumberParameters = 2;
1158 rec->ExceptionInformation[0] = 0;
1159 /* if error contains a LDT selector, use that as fault address */
1160 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1161 rec->ExceptionInformation[1] = err & ~7;
1162 else
1163 rec->ExceptionInformation[1] = 0xffffffff;
1165 break;
1166 case TRAP_x86_PAGEFLT: /* Page fault */
1167 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1168 rec->NumberParameters = 2;
1169 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1170 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1171 break;
1172 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1173 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1174 break;
1175 default:
1176 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1177 /* fall through */
1178 case TRAP_x86_NMI: /* NMI interrupt */
1179 case TRAP_x86_DNA: /* Device not available exception */
1180 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1181 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1182 case TRAP_x86_MCHK: /* Machine check exception */
1183 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1184 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1185 break;
1190 /**********************************************************************
1191 * trap_handler
1193 * Handler for SIGTRAP.
1195 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1197 SIGCONTEXT *context = sigcontext;
1198 EXCEPTION_RECORD *rec;
1200 #ifdef __APPLE__
1201 /* MacOS single step flag handling is broken */
1202 if (wine_ldt_is_system(CS_sig(context)) &&
1203 (void *)EIP_sig(context) == raise_trap_exception &&
1204 !(EFL_sig(context) & 0x100))
1205 return;
1206 #endif
1208 rec = setup_exception( context, raise_trap_exception );
1210 switch(get_trap_code(context))
1212 case TRAP_x86_TRCTRAP: /* Single-step exception */
1213 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1214 break;
1215 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1216 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1217 /* fall through */
1218 default:
1219 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1220 break;
1225 /**********************************************************************
1226 * fpe_handler
1228 * Handler for SIGFPE.
1230 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1232 CONTEXT *win_context;
1233 SIGCONTEXT *context = sigcontext;
1234 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1236 win_context = get_exception_context( rec );
1238 switch(get_trap_code(context))
1240 case TRAP_x86_DIVIDE: /* Division by zero exception */
1241 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1242 break;
1243 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1244 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1245 break;
1246 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1247 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1248 rec->ExceptionCode = get_fpu_code( win_context );
1249 break;
1250 case TRAP_x86_CACHEFLT: /* SIMD exception */
1251 /* TODO:
1252 * Behaviour only tested for divide-by-zero exceptions
1253 * Check for other SIMD exceptions as well */
1254 if(siginfo->si_code != FPE_FLTDIV)
1255 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1256 siginfo->si_code);
1258 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1259 rec->NumberParameters = 1;
1260 /* no idea what meaning is actually behind this but thats what native does */
1261 rec->ExceptionInformation[0] = 0;
1262 break;
1263 default:
1264 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1265 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1266 break;
1271 /**********************************************************************
1272 * int_handler
1274 * Handler for SIGINT.
1276 * FIXME: should not be calling external functions on the signal stack.
1278 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1280 WORD fs, gs;
1281 init_handler( sigcontext, &fs, &gs );
1282 if (!dispatch_signal(SIGINT))
1284 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1285 rec->ExceptionCode = CONTROL_C_EXIT;
1289 /**********************************************************************
1290 * abrt_handler
1292 * Handler for SIGABRT.
1294 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1296 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1297 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1298 rec->ExceptionFlags = EH_NONCONTINUABLE;
1302 /**********************************************************************
1303 * term_handler
1305 * Handler for SIGTERM.
1307 static void term_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1309 WORD fs, gs;
1310 init_handler( sigcontext, &fs, &gs );
1311 server_abort_thread(0);
1315 /**********************************************************************
1316 * usr1_handler
1318 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1320 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1322 CONTEXT context;
1323 WORD fs, gs;
1325 init_handler( sigcontext, &fs, &gs );
1326 save_context( &context, sigcontext, fs, gs );
1327 wait_suspend( &context );
1328 restore_context( &context, sigcontext );
1332 /**********************************************************************
1333 * get_signal_stack_total_size
1335 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1336 * Must be a power of two.
1338 size_t get_signal_stack_total_size(void)
1340 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1342 if (!signal_stack_size)
1344 size_t size = 4096, min_size = teb_size + max( MINSIGSTKSZ, 4096 );
1345 /* find the first power of two not smaller than min_size */
1346 while (size < min_size) size *= 2;
1347 signal_stack_mask = size - 1;
1348 signal_stack_size = size - teb_size;
1350 return signal_stack_size + teb_size;
1354 /***********************************************************************
1355 * __wine_set_signal_handler (NTDLL.@)
1357 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1359 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1360 if (handlers[sig] != NULL) return -2;
1361 handlers[sig] = wsh;
1362 return 0;
1366 /**********************************************************************
1367 * SIGNAL_Init
1369 BOOL SIGNAL_Init(void)
1371 struct sigaction sig_act;
1373 #ifdef HAVE_SIGALTSTACK
1374 stack_t ss;
1376 #ifdef __APPLE__
1377 int mib[2], val = 1;
1379 mib[0] = CTL_KERN;
1380 mib[1] = KERN_THALTSTACK;
1381 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1382 #endif
1384 ss.ss_sp = get_signal_stack();
1385 ss.ss_size = signal_stack_size;
1386 ss.ss_flags = 0;
1387 if (sigaltstack(&ss, NULL) == -1)
1389 perror( "sigaltstack" );
1390 return FALSE;
1392 #endif /* HAVE_SIGALTSTACK */
1394 sig_act.sa_mask = server_block_set;
1395 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1396 #ifdef SA_ONSTACK
1397 sig_act.sa_flags |= SA_ONSTACK;
1398 #endif
1400 sig_act.sa_sigaction = int_handler;
1401 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1402 sig_act.sa_sigaction = fpe_handler;
1403 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1404 sig_act.sa_sigaction = abrt_handler;
1405 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1406 sig_act.sa_sigaction = term_handler;
1407 if (sigaction( SIGTERM, &sig_act, NULL ) == -1) goto error;
1408 sig_act.sa_sigaction = usr1_handler;
1409 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1411 sig_act.sa_sigaction = segv_handler;
1412 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1413 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1414 #ifdef SIGBUS
1415 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1416 #endif
1418 #ifdef SIGTRAP
1419 sig_act.sa_sigaction = trap_handler;
1420 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1421 #endif
1423 #ifdef __HAVE_VM86
1424 sig_act.sa_sigaction = usr2_handler;
1425 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1426 #endif
1428 return TRUE;
1430 error:
1431 perror("sigaction");
1432 return FALSE;
1436 #ifdef __HAVE_VM86
1437 /**********************************************************************
1438 * __wine_enter_vm86 (NTDLL.@)
1440 * Enter vm86 mode with the specified register context.
1442 void __wine_enter_vm86( CONTEXT *context )
1444 EXCEPTION_RECORD rec;
1445 int res;
1446 struct vm86plus_struct vm86;
1448 memset( &vm86, 0, sizeof(vm86) );
1449 for (;;)
1451 restore_vm86_context( context, &vm86 );
1453 ntdll_get_thread_data()->vm86_ptr = &vm86;
1454 merge_vm86_pending_flags( &rec );
1456 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1457 if (res < 0)
1459 errno = -res;
1460 return;
1463 save_vm86_context( context, &vm86 );
1465 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1466 rec.ExceptionRecord = NULL;
1467 rec.ExceptionAddress = (LPVOID)context->Eip;
1468 rec.NumberParameters = 0;
1470 switch(VM86_TYPE(res))
1472 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1473 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1474 raise_segv_exception( &rec, context );
1475 continue;
1476 case VM86_TRAP: /* return due to DOS-debugger request */
1477 switch(VM86_ARG(res))
1479 case TRAP_x86_TRCTRAP: /* Single-step exception */
1480 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1481 break;
1482 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1483 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1484 /* fall through */
1485 default:
1486 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1487 break;
1489 break;
1490 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1491 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1492 rec.NumberParameters = 1;
1493 rec.ExceptionInformation[0] = VM86_ARG(res);
1494 break;
1495 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1496 context->EFlags |= VIF_MASK;
1497 context->EFlags &= ~VIP_MASK;
1498 NtCurrentTeb()->vm86_pending = 0;
1499 rec.ExceptionCode = EXCEPTION_VM86_STI;
1500 break;
1501 case VM86_PICRETURN: /* return due to pending PIC request */
1502 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1503 break;
1504 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1505 default:
1506 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1507 continue;
1509 __regs_RtlRaiseException( &rec, context );
1513 #else /* __HAVE_VM86 */
1514 /**********************************************************************
1515 * __wine_enter_vm86 (NTDLL.@)
1517 void __wine_enter_vm86( CONTEXT *context )
1519 MESSAGE("vm86 mode not supported on this platform\n");
1521 #endif /* __HAVE_VM86 */
1523 /**********************************************************************
1524 * DbgBreakPoint (NTDLL.@)
1526 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1528 /**********************************************************************
1529 * DbgUserBreakPoint (NTDLL.@)
1531 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1534 /**********************************************************************
1535 * EXC_CallHandler (internal)
1537 * Some exception handlers depend on EBP to have a fixed position relative to
1538 * the exception frame.
1539 * Shrinker depends on (*1) doing what it does,
1540 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1541 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1542 * function calling the handler having only 5 parameters (*4).
1544 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1545 " pushl %ebp\n"
1546 " movl %esp, %ebp\n"
1547 " pushl %ebx\n"
1548 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1549 " pushl 24(%ebp)\n"
1550 " pushl 20(%ebp)\n"
1551 " pushl 16(%ebp)\n"
1552 " pushl 12(%ebp)\n"
1553 " pushl 8(%ebp)\n"
1554 " call " __ASM_NAME("call_exception_handler") "\n"
1555 " popl %ebx\n"
1556 " leave\n"
1557 " ret\n"
1559 __ASM_GLOBAL_FUNC(call_exception_handler,
1560 " pushl %ebp\n"
1561 " movl %esp, %ebp\n"
1562 " subl $12,%esp\n"
1563 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1564 " pushl %edx\n" /* handler be handled by... */
1565 " .byte 0x64\n"
1566 " pushl (0)\n" /* nested_handler (passed in edx). */
1567 " .byte 0x64\n"
1568 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1569 " pushl 20(%ebp)\n"
1570 " pushl 16(%ebp)\n"
1571 " pushl 12(%ebp)\n"
1572 " pushl 8(%ebp)\n"
1573 " movl 24(%ebp), %ecx\n" /* (*1) */
1574 " call *%ecx\n" /* call handler. (*2) */
1575 " .byte 0x64\n"
1576 " movl (0), %esp\n" /* restore previous... (*3) */
1577 " .byte 0x64\n"
1578 " popl (0)\n" /* exception frame. */
1579 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1580 " popl %ebp\n"
1581 " ret $20\n" /* (*4) */
1583 #endif /* __i386__ */