wined3d: Move the GL info structure into the adapter.
[wine/hacks.git] / dlls / ntdll / signal_i386.c
blobdf62a30637c7cd40556f4e5875139be2145ccbee
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_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
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_data->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_data->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_CONTROL))
762 FIXME( "setting partial context (%x) not supported\n", flags );
763 else if (flags & CONTEXT_SEGMENTS)
764 __wine_call_from_32_restore_regs( context );
765 else
767 CONTEXT newcontext = *context;
768 newcontext.SegDs = wine_get_ds();
769 newcontext.SegEs = wine_get_es();
770 newcontext.SegFs = wine_get_fs();
771 newcontext.SegGs = wine_get_gs();
772 __wine_call_from_32_restore_regs( &newcontext );
778 /***********************************************************************
779 * is_privileged_instr
781 * Check if the fault location is a privileged instruction.
782 * Based on the instruction emulation code in dlls/kernel/instr.c.
784 static inline DWORD is_privileged_instr( CONTEXT86 *context )
786 const BYTE *instr;
787 unsigned int prefix_count = 0;
789 if (!wine_ldt_is_system( context->SegCs )) return 0;
790 instr = (BYTE *)context->Eip;
792 for (;;) switch(*instr)
794 /* instruction prefixes */
795 case 0x2e: /* %cs: */
796 case 0x36: /* %ss: */
797 case 0x3e: /* %ds: */
798 case 0x26: /* %es: */
799 case 0x64: /* %fs: */
800 case 0x65: /* %gs: */
801 case 0x66: /* opcode size */
802 case 0x67: /* addr size */
803 case 0xf0: /* lock */
804 case 0xf2: /* repne */
805 case 0xf3: /* repe */
806 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
807 instr++;
808 continue;
810 case 0x0f: /* extended instruction */
811 switch(instr[1])
813 case 0x20: /* mov crX, reg */
814 case 0x21: /* mov drX, reg */
815 case 0x22: /* mov reg, crX */
816 case 0x23: /* mov reg drX */
817 return EXCEPTION_PRIV_INSTRUCTION;
819 return 0;
820 case 0x6c: /* insb (%dx) */
821 case 0x6d: /* insl (%dx) */
822 case 0x6e: /* outsb (%dx) */
823 case 0x6f: /* outsl (%dx) */
824 case 0xcd: /* int $xx */
825 case 0xe4: /* inb al,XX */
826 case 0xe5: /* in (e)ax,XX */
827 case 0xe6: /* outb XX,al */
828 case 0xe7: /* out XX,(e)ax */
829 case 0xec: /* inb (%dx),%al */
830 case 0xed: /* inl (%dx),%eax */
831 case 0xee: /* outb %al,(%dx) */
832 case 0xef: /* outl %eax,(%dx) */
833 case 0xf4: /* hlt */
834 case 0xfa: /* cli */
835 case 0xfb: /* sti */
836 return EXCEPTION_PRIV_INSTRUCTION;
837 default:
838 return 0;
843 #include "pshpack1.h"
844 struct atl_thunk
846 DWORD movl; /* movl this,4(%esp) */
847 DWORD this;
848 BYTE jmp; /* jmp func */
849 int func;
851 #include "poppack.h"
853 /**********************************************************************
854 * check_atl_thunk
856 * Check if code destination is an ATL thunk, and emulate it if so.
858 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
860 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
861 BOOL ret = FALSE;
863 __TRY
865 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
867 *((DWORD *)context->Esp + 1) = thunk->this;
868 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
869 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
870 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
871 ret = TRUE;
874 __EXCEPT_PAGE_FAULT
876 return FALSE;
878 __ENDTRY
879 return ret;
883 /***********************************************************************
884 * setup_exception
886 * Setup a proper stack frame for the raise function, and modify the
887 * sigcontext so that the return from the signal handler will call
888 * the raise function.
890 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
892 struct stack_layout
894 void *ret_addr; /* return address from raise_func */
895 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
896 CONTEXT *context_ptr; /* second arg for raise_func */
897 CONTEXT context;
898 EXCEPTION_RECORD rec;
899 DWORD ebp;
900 DWORD eip;
901 } *stack;
903 WORD fs, gs;
905 stack = init_handler( sigcontext, &fs, &gs );
907 /* stack sanity checks */
909 if ((char *)stack >= (char *)get_signal_stack() &&
910 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
912 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
913 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
914 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
915 NtCurrentTeb()->Tib.StackBase );
916 server_abort_thread(1);
919 if (stack - 1 > stack || /* check for overflow in subtraction */
920 (char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit ||
921 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
923 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)stack;
924 if (diff < 4096)
926 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p\n",
927 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
928 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
929 NtCurrentTeb()->Tib.StackBase );
930 server_abort_thread(1);
932 else WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
933 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
934 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
935 NtCurrentTeb()->Tib.StackBase );
938 stack--; /* push the stack_layout structure */
939 #ifdef HAVE_VALGRIND_MEMCHECK_H
940 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
941 #endif
942 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
943 stack->rec_ptr = &stack->rec;
944 stack->context_ptr = &stack->context;
946 stack->rec.ExceptionRecord = NULL;
947 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
948 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
949 stack->rec.NumberParameters = 0;
951 save_context( &stack->context, sigcontext, fs, gs );
953 /* now modify the sigcontext to return to the raise function */
954 ESP_sig(sigcontext) = (DWORD)stack;
955 EIP_sig(sigcontext) = (DWORD)func;
956 /* clear single-step and align check flag */
957 EFL_sig(sigcontext) &= ~(0x100|0x40000);
958 CS_sig(sigcontext) = wine_get_cs();
959 DS_sig(sigcontext) = wine_get_ds();
960 ES_sig(sigcontext) = wine_get_es();
961 FS_sig(sigcontext) = wine_get_fs();
962 GS_sig(sigcontext) = wine_get_gs();
963 SS_sig(sigcontext) = wine_get_ss();
965 return stack->rec_ptr;
969 /***********************************************************************
970 * get_exception_context
972 * Get a pointer to the context built by setup_exception.
974 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
976 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
980 /**********************************************************************
981 * get_fpu_code
983 * Get the FPU exception code from the FPU status.
985 static inline DWORD get_fpu_code( const CONTEXT *context )
987 DWORD status = context->FloatSave.StatusWord;
989 if (status & 0x01) /* IE */
991 if (status & 0x40) /* SF */
992 return EXCEPTION_FLT_STACK_CHECK;
993 else
994 return EXCEPTION_FLT_INVALID_OPERATION;
996 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
997 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
998 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
999 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1000 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1001 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1005 /**********************************************************************
1006 * raise_segv_exception
1008 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1010 switch(rec->ExceptionCode)
1012 case EXCEPTION_ACCESS_VIOLATION:
1013 if (rec->NumberParameters == 2)
1015 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1016 goto done;
1017 rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] );
1019 break;
1020 case EXCEPTION_DATATYPE_MISALIGNMENT:
1021 /* FIXME: pass through exception handler first? */
1022 if (context->EFlags & 0x00040000)
1024 /* Disable AC flag, return */
1025 context->EFlags &= ~0x00040000;
1026 goto done;
1028 break;
1030 __regs_RtlRaiseException( rec, context );
1031 done:
1032 NtSetContextThread( GetCurrentThread(), context );
1036 /**********************************************************************
1037 * raise_trap_exception
1039 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1041 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1043 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1045 /* when single stepping can't tell whether this is a hw bp or a
1046 * single step interrupt. try to avoid as much overhead as possible
1047 * and only do a server call if there is any hw bp enabled. */
1049 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1051 /* (possible) hardware breakpoint, fetch the debug registers */
1052 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1053 NtGetContextThread(GetCurrentThread(), context);
1054 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1057 context->EFlags &= ~0x100; /* clear single-step flag */
1060 __regs_RtlRaiseException( rec, context );
1061 NtSetContextThread( GetCurrentThread(), context );
1065 /**********************************************************************
1066 * raise_exception
1068 * Generic raise function for exceptions that don't need special treatment.
1070 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1072 __regs_RtlRaiseException( rec, context );
1073 NtSetContextThread( GetCurrentThread(), context );
1077 #ifdef __HAVE_VM86
1078 /**********************************************************************
1079 * raise_vm86_sti_exception
1081 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1083 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1084 NtCurrentTeb()->vm86_pending |= VIP_MASK;
1086 if (ntdll_get_thread_data()->vm86_ptr)
1088 if (((char*)context->Eip >= (char*)vm86_return) &&
1089 ((char*)context->Eip <= (char*)vm86_return_end) &&
1090 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1091 /* exiting from VM86, can't throw */
1092 goto done;
1094 merge_vm86_pending_flags( rec );
1096 else if (NtCurrentTeb()->dpmi_vif &&
1097 !wine_ldt_is_system(context->SegCs) &&
1098 !wine_ldt_is_system(context->SegSs))
1100 /* Executing DPMI code and virtual interrupts are enabled. */
1101 NtCurrentTeb()->vm86_pending = 0;
1102 __regs_RtlRaiseException( rec, context );
1104 done:
1105 NtSetContextThread( GetCurrentThread(), context );
1109 /**********************************************************************
1110 * usr2_handler
1112 * Handler for SIGUSR2.
1113 * We use it to signal that the running __wine_enter_vm86() should
1114 * immediately set VIP_MASK, causing pending events to be handled
1115 * as early as possible.
1117 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1119 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1120 rec->ExceptionCode = EXCEPTION_VM86_STI;
1122 #endif /* __HAVE_VM86 */
1125 /**********************************************************************
1126 * segv_handler
1128 * Handler for SIGSEGV and related errors.
1130 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1132 SIGCONTEXT *context = sigcontext;
1133 EXCEPTION_RECORD *rec = setup_exception( context, raise_segv_exception );
1135 switch(get_trap_code(context))
1137 case TRAP_x86_OFLOW: /* Overflow exception */
1138 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1139 break;
1140 case TRAP_x86_BOUND: /* Bound range exception */
1141 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1142 break;
1143 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1144 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1145 break;
1146 case TRAP_x86_STKFLT: /* Stack fault */
1147 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1148 break;
1149 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1150 case TRAP_x86_PROTFLT: /* General protection fault */
1151 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1153 WORD err = get_error_code(context);
1154 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1155 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1156 rec->NumberParameters = 2;
1157 rec->ExceptionInformation[0] = 0;
1158 /* if error contains a LDT selector, use that as fault address */
1159 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1160 rec->ExceptionInformation[1] = err & ~7;
1161 else
1162 rec->ExceptionInformation[1] = 0xffffffff;
1164 break;
1165 case TRAP_x86_PAGEFLT: /* Page fault */
1166 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1167 rec->NumberParameters = 2;
1168 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1169 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1170 break;
1171 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1172 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1173 break;
1174 default:
1175 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1176 /* fall through */
1177 case TRAP_x86_NMI: /* NMI interrupt */
1178 case TRAP_x86_DNA: /* Device not available exception */
1179 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1180 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1181 case TRAP_x86_MCHK: /* Machine check exception */
1182 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1183 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1184 break;
1189 /**********************************************************************
1190 * trap_handler
1192 * Handler for SIGTRAP.
1194 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1196 SIGCONTEXT *context = sigcontext;
1197 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1199 switch(get_trap_code(context))
1201 case TRAP_x86_TRCTRAP: /* Single-step exception */
1202 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1203 break;
1204 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1205 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1206 /* fall through */
1207 default:
1208 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1209 break;
1214 /**********************************************************************
1215 * fpe_handler
1217 * Handler for SIGFPE.
1219 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1221 CONTEXT *win_context;
1222 SIGCONTEXT *context = sigcontext;
1223 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1225 win_context = get_exception_context( rec );
1227 switch(get_trap_code(context))
1229 case TRAP_x86_DIVIDE: /* Division by zero exception */
1230 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1231 break;
1232 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1233 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1234 break;
1235 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1236 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1237 rec->ExceptionCode = get_fpu_code( win_context );
1238 break;
1239 case TRAP_x86_CACHEFLT: /* SIMD exception */
1240 /* TODO:
1241 * Behaviour only tested for divide-by-zero exceptions
1242 * Check for other SIMD exceptions as well */
1243 if(siginfo->si_code != FPE_FLTDIV)
1244 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1245 siginfo->si_code);
1247 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1248 rec->NumberParameters = 1;
1249 /* no idea what meaning is actually behind this but that's what native does */
1250 rec->ExceptionInformation[0] = 0;
1251 break;
1252 default:
1253 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1254 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1255 break;
1260 /**********************************************************************
1261 * int_handler
1263 * Handler for SIGINT.
1265 * FIXME: should not be calling external functions on the signal stack.
1267 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1269 WORD fs, gs;
1270 init_handler( sigcontext, &fs, &gs );
1271 if (!dispatch_signal(SIGINT))
1273 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1274 rec->ExceptionCode = CONTROL_C_EXIT;
1278 /**********************************************************************
1279 * abrt_handler
1281 * Handler for SIGABRT.
1283 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1285 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1286 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1287 rec->ExceptionFlags = EH_NONCONTINUABLE;
1291 /**********************************************************************
1292 * term_handler
1294 * Handler for SIGTERM.
1296 static void term_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1298 WORD fs, gs;
1299 init_handler( sigcontext, &fs, &gs );
1300 server_abort_thread(0);
1304 /**********************************************************************
1305 * usr1_handler
1307 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1309 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1311 CONTEXT context;
1312 WORD fs, gs;
1314 init_handler( sigcontext, &fs, &gs );
1315 save_context( &context, sigcontext, fs, gs );
1316 wait_suspend( &context );
1317 restore_context( &context, sigcontext );
1321 /**********************************************************************
1322 * get_signal_stack_total_size
1324 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1325 * Must be a power of two.
1327 size_t get_signal_stack_total_size(void)
1329 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
1331 if (!signal_stack_size)
1333 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1334 /* find the first power of two not smaller than min_size */
1335 while (size < min_size) size *= 2;
1336 signal_stack_mask = size - 1;
1337 signal_stack_size = size - teb_size;
1339 return signal_stack_size + teb_size;
1343 /***********************************************************************
1344 * __wine_set_signal_handler (NTDLL.@)
1346 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1348 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1349 if (handlers[sig] != NULL) return -2;
1350 handlers[sig] = wsh;
1351 return 0;
1355 /**********************************************************************
1356 * SIGNAL_Init
1358 BOOL SIGNAL_Init(void)
1360 struct sigaction sig_act;
1362 #ifdef HAVE_SIGALTSTACK
1363 stack_t ss;
1365 #ifdef __APPLE__
1366 int mib[2], val = 1;
1368 mib[0] = CTL_KERN;
1369 mib[1] = KERN_THALTSTACK;
1370 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1371 #endif
1373 ss.ss_sp = get_signal_stack();
1374 ss.ss_size = signal_stack_size;
1375 ss.ss_flags = 0;
1376 if (sigaltstack(&ss, NULL) == -1)
1378 perror( "sigaltstack" );
1379 return FALSE;
1381 #endif /* HAVE_SIGALTSTACK */
1383 sig_act.sa_mask = server_block_set;
1384 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1385 #ifdef SA_ONSTACK
1386 sig_act.sa_flags |= SA_ONSTACK;
1387 #endif
1389 sig_act.sa_sigaction = int_handler;
1390 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1391 sig_act.sa_sigaction = fpe_handler;
1392 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1393 sig_act.sa_sigaction = abrt_handler;
1394 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1395 sig_act.sa_sigaction = term_handler;
1396 if (sigaction( SIGTERM, &sig_act, NULL ) == -1) goto error;
1397 sig_act.sa_sigaction = usr1_handler;
1398 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1400 sig_act.sa_sigaction = segv_handler;
1401 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1402 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1403 #ifdef SIGBUS
1404 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1405 #endif
1407 #ifdef SIGTRAP
1408 sig_act.sa_sigaction = trap_handler;
1409 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1410 #endif
1412 #ifdef __HAVE_VM86
1413 sig_act.sa_sigaction = usr2_handler;
1414 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1415 #endif
1417 return TRUE;
1419 error:
1420 perror("sigaction");
1421 return FALSE;
1425 #ifdef __HAVE_VM86
1426 /**********************************************************************
1427 * __wine_enter_vm86 (NTDLL.@)
1429 * Enter vm86 mode with the specified register context.
1431 void __wine_enter_vm86( CONTEXT *context )
1433 EXCEPTION_RECORD rec;
1434 int res;
1435 struct vm86plus_struct vm86;
1437 memset( &vm86, 0, sizeof(vm86) );
1438 for (;;)
1440 restore_vm86_context( context, &vm86 );
1442 ntdll_get_thread_data()->vm86_ptr = &vm86;
1443 merge_vm86_pending_flags( &rec );
1445 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1446 if (res < 0)
1448 errno = -res;
1449 return;
1452 save_vm86_context( context, &vm86 );
1454 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1455 rec.ExceptionRecord = NULL;
1456 rec.ExceptionAddress = (LPVOID)context->Eip;
1457 rec.NumberParameters = 0;
1459 switch(VM86_TYPE(res))
1461 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1462 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1463 break;
1464 case VM86_TRAP: /* return due to DOS-debugger request */
1465 switch(VM86_ARG(res))
1467 case TRAP_x86_TRCTRAP: /* Single-step exception */
1468 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1469 break;
1470 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1471 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1472 /* fall through */
1473 default:
1474 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1475 break;
1477 break;
1478 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1479 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1480 rec.NumberParameters = 1;
1481 rec.ExceptionInformation[0] = VM86_ARG(res);
1482 break;
1483 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1484 context->EFlags |= VIF_MASK;
1485 context->EFlags &= ~VIP_MASK;
1486 NtCurrentTeb()->vm86_pending = 0;
1487 rec.ExceptionCode = EXCEPTION_VM86_STI;
1488 break;
1489 case VM86_PICRETURN: /* return due to pending PIC request */
1490 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1491 break;
1492 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1493 default:
1494 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1495 continue;
1497 __regs_RtlRaiseException( &rec, context );
1501 #else /* __HAVE_VM86 */
1502 /**********************************************************************
1503 * __wine_enter_vm86 (NTDLL.@)
1505 void __wine_enter_vm86( CONTEXT *context )
1507 MESSAGE("vm86 mode not supported on this platform\n");
1509 #endif /* __HAVE_VM86 */
1511 /**********************************************************************
1512 * DbgBreakPoint (NTDLL.@)
1514 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1516 /**********************************************************************
1517 * DbgUserBreakPoint (NTDLL.@)
1519 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1522 /**********************************************************************
1523 * EXC_CallHandler (internal)
1525 * Some exception handlers depend on EBP to have a fixed position relative to
1526 * the exception frame.
1527 * Shrinker depends on (*1) doing what it does,
1528 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1529 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1530 * function calling the handler having only 5 parameters (*4).
1532 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1533 " pushl %ebp\n"
1534 " movl %esp, %ebp\n"
1535 " pushl %ebx\n"
1536 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1537 " pushl 24(%ebp)\n"
1538 " pushl 20(%ebp)\n"
1539 " pushl 16(%ebp)\n"
1540 " pushl 12(%ebp)\n"
1541 " pushl 8(%ebp)\n"
1542 " call " __ASM_NAME("call_exception_handler") "\n"
1543 " popl %ebx\n"
1544 " leave\n"
1545 " ret\n"
1547 __ASM_GLOBAL_FUNC(call_exception_handler,
1548 " pushl %ebp\n"
1549 " movl %esp, %ebp\n"
1550 " subl $12,%esp\n"
1551 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1552 " pushl %edx\n" /* handler be handled by... */
1553 " .byte 0x64\n"
1554 " pushl (0)\n" /* nested_handler (passed in edx). */
1555 " .byte 0x64\n"
1556 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1557 " pushl 20(%ebp)\n"
1558 " pushl 16(%ebp)\n"
1559 " pushl 12(%ebp)\n"
1560 " pushl 8(%ebp)\n"
1561 " movl 24(%ebp), %ecx\n" /* (*1) */
1562 " call *%ecx\n" /* call handler. (*2) */
1563 " .byte 0x64\n"
1564 " movl (0), %esp\n" /* restore previous... (*3) */
1565 " .byte 0x64\n"
1566 " popl (0)\n" /* exception frame. */
1567 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1568 " popl %ebp\n"
1569 " ret $20\n" /* (*4) */
1571 #endif /* __i386__ */