ntdll: Set the exception address to the program counter in RtlRaiseException.
[wine/multimedia.git] / dlls / ntdll / signal_i386.c
blobb36b51ebe80114cbbddc53d820bfae0a80e6036f
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 "ntstatus.h"
59 #define WIN32_NO_STATUS
60 #include "windef.h"
61 #include "wine/library.h"
62 #include "ntdll_misc.h"
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 #ifdef HAVE_VALGRIND_MEMCHECK_H
67 #include <valgrind/memcheck.h>
68 #endif
70 #undef ERR /* Solaris needs to define this */
72 /* not defined for x86, so copy the x86_64 definition */
73 typedef struct DECLSPEC_ALIGN(16) _M128A
75 ULONGLONG Low;
76 LONGLONG High;
77 } M128A;
79 typedef struct
81 WORD ControlWord;
82 WORD StatusWord;
83 BYTE TagWord;
84 BYTE Reserved1;
85 WORD ErrorOpcode;
86 DWORD ErrorOffset;
87 WORD ErrorSelector;
88 WORD Reserved2;
89 DWORD DataOffset;
90 WORD DataSelector;
91 WORD Reserved3;
92 DWORD MxCsr;
93 DWORD MxCsr_Mask;
94 M128A FloatRegisters[8];
95 M128A XmmRegisters[16];
96 BYTE Reserved4[96];
97 } XMM_SAVE_AREA32;
99 /***********************************************************************
100 * signal context platform-specific definitions
103 #ifdef linux
105 typedef ucontext_t SIGCONTEXT;
107 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
108 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
109 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
110 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
111 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
112 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
113 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
114 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
116 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
117 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
118 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
119 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
120 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
121 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
123 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
124 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
125 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
126 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
128 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
129 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
131 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
132 #define VIF_FLAG 0x00080000
133 #define VIP_FLAG 0x00100000
135 int vm86_enter( void **vm86_ptr );
136 void vm86_return(void);
137 void vm86_return_end(void);
138 __ASM_GLOBAL_FUNC(vm86_enter,
139 "pushl %ebp\n\t"
140 "movl %esp, %ebp\n\t"
141 "movl $166,%eax\n\t" /*SYS_vm86*/
142 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
143 "movl (%ecx),%ecx\n\t"
144 "pushl %ebx\n\t"
145 "movl $1,%ebx\n\t" /*VM86_ENTER*/
146 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
147 "pushl %fs\n\t"
148 "pushl %gs\n\t"
149 "int $0x80\n"
150 ".globl " __ASM_NAME("vm86_return") "\n\t"
151 __ASM_FUNC("vm86_return") "\n"
152 __ASM_NAME("vm86_return") ":\n\t"
153 "popl %gs\n\t"
154 "popl %fs\n\t"
155 "popl %ecx\n\t"
156 "popl %ebx\n\t"
157 "popl %ebp\n\t"
158 "testl %eax,%eax\n\t"
159 "jl 0f\n\t"
160 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
161 "je " __ASM_NAME("vm86_enter") "\n\t"
162 "0:\n\t"
163 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
164 "movl $0,(%ecx)\n\t"
165 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
166 __ASM_FUNC("vm86_return_end") "\n"
167 __ASM_NAME("vm86_return_end") ":\n\t"
168 "ret" )
170 #ifdef HAVE_SYS_VM86_H
171 # define __HAVE_VM86
172 #endif
174 #endif /* linux */
176 #ifdef BSDI
178 #include <machine/frame.h>
179 typedef struct trapframe SIGCONTEXT;
181 #define EAX_sig(context) ((context)->tf_eax)
182 #define EBX_sig(context) ((context)->tf_ebx)
183 #define ECX_sig(context) ((context)->tf_ecx)
184 #define EDX_sig(context) ((context)->tf_edx)
185 #define ESI_sig(context) ((context)->tf_esi)
186 #define EDI_sig(context) ((context)->tf_edi)
187 #define EBP_sig(context) ((context)->tf_ebp)
189 #define CS_sig(context) ((context)->tf_cs)
190 #define DS_sig(context) ((context)->tf_ds)
191 #define ES_sig(context) ((context)->tf_es)
192 #define SS_sig(context) ((context)->tf_ss)
194 #define EFL_sig(context) ((context)->tf_eflags)
196 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
197 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
199 #define FPU_sig(context) NULL /* FIXME */
200 #define FPUX_sig(context) NULL /* FIXME */
202 #endif /* bsdi */
204 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
206 typedef struct sigcontext SIGCONTEXT;
208 #define EAX_sig(context) ((context)->sc_eax)
209 #define EBX_sig(context) ((context)->sc_ebx)
210 #define ECX_sig(context) ((context)->sc_ecx)
211 #define EDX_sig(context) ((context)->sc_edx)
212 #define ESI_sig(context) ((context)->sc_esi)
213 #define EDI_sig(context) ((context)->sc_edi)
214 #define EBP_sig(context) ((context)->sc_ebp)
216 #define CS_sig(context) ((context)->sc_cs)
217 #define DS_sig(context) ((context)->sc_ds)
218 #define ES_sig(context) ((context)->sc_es)
219 #define FS_sig(context) ((context)->sc_fs)
220 #define GS_sig(context) ((context)->sc_gs)
221 #define SS_sig(context) ((context)->sc_ss)
223 #define TRAP_sig(context) ((context)->sc_trapno)
224 #define ERROR_sig(context) ((context)->sc_err)
225 #define EFL_sig(context) ((context)->sc_eflags)
227 #define EIP_sig(context) ((context)->sc_eip)
228 #define ESP_sig(context) ((context)->sc_esp)
230 #define FPU_sig(context) NULL /* FIXME */
231 #define FPUX_sig(context) NULL /* FIXME */
233 #endif /* __FreeBSD__ */
235 #ifdef __OpenBSD__
237 typedef struct sigcontext SIGCONTEXT;
239 #define EAX_sig(context) ((context)->sc_eax)
240 #define EBX_sig(context) ((context)->sc_ebx)
241 #define ECX_sig(context) ((context)->sc_ecx)
242 #define EDX_sig(context) ((context)->sc_edx)
243 #define ESI_sig(context) ((context)->sc_esi)
244 #define EDI_sig(context) ((context)->sc_edi)
245 #define EBP_sig(context) ((context)->sc_ebp)
247 #define CS_sig(context) ((context)->sc_cs)
248 #define DS_sig(context) ((context)->sc_ds)
249 #define ES_sig(context) ((context)->sc_es)
250 #define FS_sig(context) ((context)->sc_fs)
251 #define GS_sig(context) ((context)->sc_gs)
252 #define SS_sig(context) ((context)->sc_ss)
254 #define TRAP_sig(context) ((context)->sc_trapno)
255 #define ERROR_sig(context) ((context)->sc_err)
256 #define EFL_sig(context) ((context)->sc_eflags)
258 #define EIP_sig(context) ((context)->sc_eip)
259 #define ESP_sig(context) ((context)->sc_esp)
261 #define FPU_sig(context) NULL /* FIXME */
262 #define FPUX_sig(context) NULL /* FIXME */
264 #define T_MCHK T_MACHK
265 #define T_XMMFLT T_XFTRAP
267 #endif /* __OpenBSD__ */
269 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
271 #ifdef _SCO_DS
272 #include <sys/regset.h>
273 #endif
274 #include <sys/ucontext.h>
275 typedef struct ucontext SIGCONTEXT;
277 #ifdef _SCO_DS
278 #define gregs regs
279 #endif
281 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
282 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
283 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
284 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
285 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
286 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
287 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
289 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
290 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
291 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
292 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
294 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
295 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
297 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
299 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
300 #ifdef UESP
301 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
302 #elif defined(R_ESP)
303 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
304 #else
305 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
306 #endif
307 #ifdef ERR
308 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
309 #endif
310 #ifdef TRAPNO
311 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
312 #endif
314 #define FPU_sig(context) NULL /* FIXME */
315 #define FPUX_sig(context) NULL /* FIXME */
317 #endif /* svr4 || SCO_DS */
319 #ifdef __APPLE__
320 # include <sys/ucontext.h>
322 typedef ucontext_t SIGCONTEXT;
324 /* work around silly renaming of struct members in OS X 10.5 */
325 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
326 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
327 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
328 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
329 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
330 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
331 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
332 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
333 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
334 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
335 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
336 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
337 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
338 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
339 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
340 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
341 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
342 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
343 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
344 #define FPU_sig(context) NULL
345 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
346 #else
347 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
348 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
349 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
350 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
351 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
352 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
353 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
354 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
355 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
356 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
357 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
358 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
359 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
360 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
361 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
362 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
363 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
364 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
365 #define FPU_sig(context) NULL
366 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
367 #endif
369 #endif /* __APPLE__ */
371 #if defined(__NetBSD__)
372 # include <sys/ucontext.h>
373 # include <sys/types.h>
374 # include <signal.h>
376 typedef ucontext_t SIGCONTEXT;
378 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
379 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
380 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
381 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
382 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
383 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
384 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
385 #define ESP_sig(context) _UC_MACHINE_SP(context)
387 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
388 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
389 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
390 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
391 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
392 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
394 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
395 #define EIP_sig(context) _UC_MACHINE_PC(context)
396 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
397 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
399 #define FPU_sig(context) NULL
400 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
402 #define T_MCHK T_MCA
403 #define T_XMMFLT T_XMM
405 #endif /* __NetBSD__ */
407 WINE_DEFAULT_DEBUG_CHANNEL(seh);
409 typedef int (*wine_signal_handler)(unsigned int sig);
411 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
412 static size_t signal_stack_mask;
413 static size_t signal_stack_size;
415 static wine_signal_handler handlers[256];
417 static int fpux_support; /* whether the CPU support extended fpu context */
419 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
421 enum i386_trap_code
423 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
424 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
425 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
426 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
427 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
428 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
429 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
430 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
431 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
432 TRAP_x86_DNA = T_DNA, /* Device not available exception */
433 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
434 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
435 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
436 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
437 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
438 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
439 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
440 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
441 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
442 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
443 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
444 #else
445 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
446 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
447 TRAP_x86_NMI = 2, /* NMI interrupt */
448 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
449 TRAP_x86_OFLOW = 4, /* Overflow exception */
450 TRAP_x86_BOUND = 5, /* Bound range exception */
451 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
452 TRAP_x86_DNA = 7, /* Device not available exception */
453 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
454 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
455 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
456 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
457 TRAP_x86_STKFLT = 12, /* Stack fault */
458 TRAP_x86_PROTFLT = 13, /* General protection fault */
459 TRAP_x86_PAGEFLT = 14, /* Page fault */
460 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
461 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
462 TRAP_x86_MCHK = 18, /* Machine check exception */
463 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
464 otherwise Cache flush exception (via SIGSEV) */
465 #endif
469 /***********************************************************************
470 * dispatch_signal
472 static inline int dispatch_signal(unsigned int sig)
474 if (handlers[sig] == NULL) return 0;
475 return handlers[sig](sig);
479 /***********************************************************************
480 * get_trap_code
482 * Get the trap code for a signal.
484 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
486 #ifdef TRAP_sig
487 return TRAP_sig(sigcontext);
488 #else
489 return TRAP_x86_UNKNOWN; /* unknown trap code */
490 #endif
493 /***********************************************************************
494 * get_error_code
496 * Get the error code for a signal.
498 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
500 #ifdef ERROR_sig
501 return ERROR_sig(sigcontext);
502 #else
503 return 0;
504 #endif
507 /***********************************************************************
508 * get_signal_stack
510 * Get the base of the signal stack for the current thread.
512 static inline void *get_signal_stack(void)
514 return (char *)NtCurrentTeb() + 4096;
518 /***********************************************************************
519 * get_current_teb
521 * Get the current teb based on the stack pointer.
523 static inline TEB *get_current_teb(void)
525 unsigned long esp;
526 __asm__("movl %%esp,%0" : "=g" (esp) );
527 return (TEB *)(esp & ~signal_stack_mask);
531 #ifdef __HAVE_VM86
532 /***********************************************************************
533 * save_vm86_context
535 * Set the register values from a vm86 structure.
537 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
539 context->ContextFlags = CONTEXT_FULL;
540 context->Eax = vm86->regs.eax;
541 context->Ebx = vm86->regs.ebx;
542 context->Ecx = vm86->regs.ecx;
543 context->Edx = vm86->regs.edx;
544 context->Esi = vm86->regs.esi;
545 context->Edi = vm86->regs.edi;
546 context->Esp = vm86->regs.esp;
547 context->Ebp = vm86->regs.ebp;
548 context->Eip = vm86->regs.eip;
549 context->SegCs = vm86->regs.cs;
550 context->SegDs = vm86->regs.ds;
551 context->SegEs = vm86->regs.es;
552 context->SegFs = vm86->regs.fs;
553 context->SegGs = vm86->regs.gs;
554 context->SegSs = vm86->regs.ss;
555 context->EFlags = vm86->regs.eflags;
559 /***********************************************************************
560 * restore_vm86_context
562 * Build a vm86 structure from the register values.
564 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
566 vm86->regs.eax = context->Eax;
567 vm86->regs.ebx = context->Ebx;
568 vm86->regs.ecx = context->Ecx;
569 vm86->regs.edx = context->Edx;
570 vm86->regs.esi = context->Esi;
571 vm86->regs.edi = context->Edi;
572 vm86->regs.esp = context->Esp;
573 vm86->regs.ebp = context->Ebp;
574 vm86->regs.eip = context->Eip;
575 vm86->regs.cs = context->SegCs;
576 vm86->regs.ds = context->SegDs;
577 vm86->regs.es = context->SegEs;
578 vm86->regs.fs = context->SegFs;
579 vm86->regs.gs = context->SegGs;
580 vm86->regs.ss = context->SegSs;
581 vm86->regs.eflags = context->EFlags;
585 /**********************************************************************
586 * merge_vm86_pending_flags
588 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
589 * raises exception if there are pending events and VIF flag
590 * has been turned on.
592 * Called from __wine_enter_vm86 because vm86_enter
593 * doesn't check for pending events.
595 * Called from raise_vm86_sti_exception to check for
596 * pending events in a signal safe way.
598 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
600 BOOL check_pending = TRUE;
601 struct vm86plus_struct *vm86 =
602 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
605 * In order to prevent a race when SIGUSR2 occurs while
606 * we are returning from exception handler, pending events
607 * will be rechecked after each raised exception.
609 while (check_pending && get_vm86_teb_info()->vm86_pending)
611 check_pending = FALSE;
612 ntdll_get_thread_data()->vm86_ptr = NULL;
615 * If VIF is set, throw exception.
616 * Note that SIGUSR2 may turn VIF flag off so
617 * VIF check must occur only when TEB.vm86_ptr is NULL.
619 if (vm86->regs.eflags & VIF_FLAG)
621 CONTEXT vcontext;
622 save_vm86_context( &vcontext, vm86 );
624 rec->ExceptionCode = EXCEPTION_VM86_STI;
625 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
626 rec->ExceptionRecord = NULL;
627 rec->NumberParameters = 0;
628 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
630 vcontext.EFlags &= ~VIP_FLAG;
631 get_vm86_teb_info()->vm86_pending = 0;
632 raise_exception( rec, &vcontext, TRUE );
634 restore_vm86_context( &vcontext, vm86 );
635 check_pending = TRUE;
638 ntdll_get_thread_data()->vm86_ptr = vm86;
642 * Merge VIP flags in a signal safe way. This requires
643 * that the following operation compiles into atomic
644 * instruction.
646 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
648 #endif /* __HAVE_VM86 */
651 #ifdef __sun
653 /* We have to workaround two Solaris breakages:
654 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
655 * code crash badly.
656 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
657 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
660 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
661 __ASM_GLOBAL_FUNC( sigaction_syscall,
662 "movl $0x62,%eax\n\t"
663 "int $0x91\n\t"
664 "ret" )
666 /* assume the same libc handler is used for all signals */
667 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
669 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
671 struct ntdll_thread_data *thread_data;
673 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
675 thread_data = (struct ntdll_thread_data *)get_current_teb()->SystemReserved2;
676 wine_set_fs( thread_data->fs );
677 wine_set_gs( thread_data->gs );
679 libc_sigacthandler( signal, siginfo, sigcontext );
682 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
684 struct sigaction real_act;
686 if (sigaction( sig, new, old ) == -1) return -1;
688 /* retrieve the real handler and flags with a direct syscall */
689 sigaction_syscall( sig, NULL, &real_act );
690 libc_sigacthandler = real_act.sa_sigaction;
691 real_act.sa_sigaction = wine_sigacthandler;
692 sigaction_syscall( sig, &real_act, NULL );
693 return 0;
695 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
697 #endif
699 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
702 /***********************************************************************
703 * init_handler
705 * Handler initialization when the full context is not needed.
706 * Return the stack pointer to use for pushing the exception data.
708 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
710 TEB *teb = get_current_teb();
712 /* get %fs and %gs at time of the fault */
713 #ifdef FS_sig
714 *fs = LOWORD(FS_sig(sigcontext));
715 #else
716 *fs = wine_get_fs();
717 #endif
718 #ifdef GS_sig
719 *gs = LOWORD(GS_sig(sigcontext));
720 #else
721 *gs = wine_get_gs();
722 #endif
724 #ifndef __sun /* see above for Solaris handling */
726 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
727 wine_set_fs( thread_data->fs );
728 wine_set_gs( thread_data->gs );
730 #endif
732 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
733 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
736 * Win16 or DOS protected mode. Note that during switch
737 * from 16-bit mode to linear mode, CS may be set to system
738 * segment before FS is restored. Fortunately, in this case
739 * SS is still non-system segment. This is why both CS and SS
740 * are checked.
742 return teb->WOW32Reserved;
744 return (void *)(ESP_sig(sigcontext) & ~3);
748 /***********************************************************************
749 * save_fpu
751 * Save the thread FPU context.
753 static inline void save_fpu( CONTEXT *context )
755 #ifdef __GNUC__
756 context->ContextFlags |= CONTEXT_FLOATING_POINT;
757 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
758 #endif
762 /***********************************************************************
763 * save_fpux
765 * Save the thread FPU extended context.
767 static inline void save_fpux( CONTEXT *context )
769 #ifdef __GNUC__
770 /* we have to enforce alignment by hand */
771 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
772 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
774 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
775 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
776 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
777 #endif
781 /***********************************************************************
782 * restore_fpu
784 * Restore the FPU context to a sigcontext.
786 static inline void restore_fpu( const CONTEXT *context )
788 FLOATING_SAVE_AREA float_status = context->FloatSave;
789 /* reset the current interrupt status */
790 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
791 #ifdef __GNUC__
792 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
793 #endif /* __GNUC__ */
797 /***********************************************************************
798 * restore_fpux
800 * Restore the FPU extended context to a sigcontext.
802 static inline void restore_fpux( const CONTEXT *context )
804 #ifdef __GNUC__
805 /* we have to enforce alignment by hand */
806 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
807 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
809 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
810 /* reset the current interrupt status */
811 state->StatusWord &= state->ControlWord | 0xff80;
812 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
813 #endif
817 /***********************************************************************
818 * fpux_to_fpu
820 * Build a standard FPU context from an extended one.
822 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
824 unsigned int i, tag, stack_top;
826 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
827 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
828 fpu->ErrorOffset = fpux->ErrorOffset;
829 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
830 fpu->DataOffset = fpux->DataOffset;
831 fpu->DataSelector = fpux->DataSelector;
832 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
834 stack_top = (fpux->StatusWord >> 11) & 7;
835 fpu->TagWord = 0xffff0000;
836 for (i = 0; i < 8; i++)
838 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
839 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
840 else
842 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
843 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
845 tag = 2; /* special */
847 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
849 if (reg->Low) tag = 2; /* special */
850 else tag = 1; /* zero */
852 else
854 if (reg->Low >> 63) tag = 0; /* valid */
855 else tag = 2; /* special */
858 fpu->TagWord |= tag << (2 * i);
863 /***********************************************************************
864 * save_context
866 * Build a context structure from the signal info.
868 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
870 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
871 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
872 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
874 memset(context, 0, sizeof(*context));
875 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
876 context->Eax = EAX_sig(sigcontext);
877 context->Ebx = EBX_sig(sigcontext);
878 context->Ecx = ECX_sig(sigcontext);
879 context->Edx = EDX_sig(sigcontext);
880 context->Esi = ESI_sig(sigcontext);
881 context->Edi = EDI_sig(sigcontext);
882 context->Ebp = EBP_sig(sigcontext);
883 context->EFlags = EFL_sig(sigcontext);
884 context->Eip = EIP_sig(sigcontext);
885 context->Esp = ESP_sig(sigcontext);
886 context->SegCs = LOWORD(CS_sig(sigcontext));
887 context->SegDs = LOWORD(DS_sig(sigcontext));
888 context->SegEs = LOWORD(ES_sig(sigcontext));
889 context->SegFs = fs;
890 context->SegGs = gs;
891 context->SegSs = LOWORD(SS_sig(sigcontext));
892 context->Dr0 = regs->dr0;
893 context->Dr1 = regs->dr1;
894 context->Dr2 = regs->dr2;
895 context->Dr3 = regs->dr3;
896 context->Dr6 = regs->dr6;
897 context->Dr7 = regs->dr7;
899 if (fpu)
901 context->ContextFlags |= CONTEXT_FLOATING_POINT;
902 context->FloatSave = *fpu;
904 if (fpux)
906 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
907 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
908 fpux_support = 1;
909 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
911 if (!fpu && !fpux) save_fpu( context );
915 /***********************************************************************
916 * restore_context
918 * Restore the signal info from the context.
920 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
922 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
923 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
924 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
926 regs->dr0 = context->Dr0;
927 regs->dr1 = context->Dr1;
928 regs->dr2 = context->Dr2;
929 regs->dr3 = context->Dr3;
930 regs->dr6 = context->Dr6;
931 regs->dr7 = context->Dr7;
932 EAX_sig(sigcontext) = context->Eax;
933 EBX_sig(sigcontext) = context->Ebx;
934 ECX_sig(sigcontext) = context->Ecx;
935 EDX_sig(sigcontext) = context->Edx;
936 ESI_sig(sigcontext) = context->Esi;
937 EDI_sig(sigcontext) = context->Edi;
938 EBP_sig(sigcontext) = context->Ebp;
939 EFL_sig(sigcontext) = context->EFlags;
940 EIP_sig(sigcontext) = context->Eip;
941 ESP_sig(sigcontext) = context->Esp;
942 CS_sig(sigcontext) = context->SegCs;
943 DS_sig(sigcontext) = context->SegDs;
944 ES_sig(sigcontext) = context->SegEs;
945 SS_sig(sigcontext) = context->SegSs;
946 #ifdef GS_sig
947 GS_sig(sigcontext) = context->SegGs;
948 #else
949 wine_set_gs( context->SegGs );
950 #endif
951 #ifdef FS_sig
952 FS_sig(sigcontext) = context->SegFs;
953 #else
954 wine_set_fs( context->SegFs );
955 #endif
957 if (fpu) *fpu = context->FloatSave;
958 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
959 if (!fpu && !fpux) restore_fpu( context );
963 /***********************************************************************
964 * RtlCaptureContext (NTDLL.@)
966 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
968 *context = *regs;
969 if (fpux_support) save_fpux( context );
970 else save_fpu( context );
972 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
975 /***********************************************************************
976 * set_cpu_context
978 * Set the new CPU context. Used by NtSetContextThread.
980 void set_cpu_context( const CONTEXT *context )
982 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
984 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
985 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
987 if (flags & CONTEXT_DEBUG_REGISTERS)
989 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
990 regs->dr0 = context->Dr0;
991 regs->dr1 = context->Dr1;
992 regs->dr2 = context->Dr2;
993 regs->dr3 = context->Dr3;
994 regs->dr6 = context->Dr6;
995 regs->dr7 = context->Dr7;
997 if (flags & CONTEXT_FULL)
999 if (!(flags & CONTEXT_CONTROL))
1000 FIXME( "setting partial context (%x) not supported\n", flags );
1001 else if (flags & CONTEXT_SEGMENTS)
1002 __wine_call_from_32_restore_regs( context );
1003 else
1005 CONTEXT newcontext = *context;
1006 newcontext.SegDs = wine_get_ds();
1007 newcontext.SegEs = wine_get_es();
1008 newcontext.SegFs = wine_get_fs();
1009 newcontext.SegGs = wine_get_gs();
1010 __wine_call_from_32_restore_regs( &newcontext );
1016 /***********************************************************************
1017 * copy_context
1019 * Copy a register context according to the flags.
1021 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1023 flags &= ~CONTEXT_i386; /* get rid of CPU id */
1024 if (flags & CONTEXT_INTEGER)
1026 to->Eax = from->Eax;
1027 to->Ebx = from->Ebx;
1028 to->Ecx = from->Ecx;
1029 to->Edx = from->Edx;
1030 to->Esi = from->Esi;
1031 to->Edi = from->Edi;
1033 if (flags & CONTEXT_CONTROL)
1035 to->Ebp = from->Ebp;
1036 to->Esp = from->Esp;
1037 to->Eip = from->Eip;
1038 to->SegCs = from->SegCs;
1039 to->SegSs = from->SegSs;
1040 to->EFlags = from->EFlags;
1042 if (flags & CONTEXT_SEGMENTS)
1044 to->SegDs = from->SegDs;
1045 to->SegEs = from->SegEs;
1046 to->SegFs = from->SegFs;
1047 to->SegGs = from->SegGs;
1049 if (flags & CONTEXT_DEBUG_REGISTERS)
1051 to->Dr0 = from->Dr0;
1052 to->Dr1 = from->Dr1;
1053 to->Dr2 = from->Dr2;
1054 to->Dr3 = from->Dr3;
1055 to->Dr6 = from->Dr6;
1056 to->Dr7 = from->Dr7;
1058 if (flags & CONTEXT_FLOATING_POINT)
1060 to->FloatSave = from->FloatSave;
1062 if (flags & CONTEXT_EXTENDED_REGISTERS)
1064 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
1069 /***********************************************************************
1070 * context_to_server
1072 * Convert a register context to the server format.
1074 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1076 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1078 memset( to, 0, sizeof(*to) );
1079 to->cpu = CPU_x86;
1081 if (flags & CONTEXT_CONTROL)
1083 to->flags |= SERVER_CTX_CONTROL;
1084 to->ctl.i386_regs.ebp = from->Ebp;
1085 to->ctl.i386_regs.esp = from->Esp;
1086 to->ctl.i386_regs.eip = from->Eip;
1087 to->ctl.i386_regs.cs = from->SegCs;
1088 to->ctl.i386_regs.ss = from->SegSs;
1089 to->ctl.i386_regs.eflags = from->EFlags;
1091 if (flags & CONTEXT_INTEGER)
1093 to->flags |= SERVER_CTX_INTEGER;
1094 to->integer.i386_regs.eax = from->Eax;
1095 to->integer.i386_regs.ebx = from->Ebx;
1096 to->integer.i386_regs.ecx = from->Ecx;
1097 to->integer.i386_regs.edx = from->Edx;
1098 to->integer.i386_regs.esi = from->Esi;
1099 to->integer.i386_regs.edi = from->Edi;
1101 if (flags & CONTEXT_SEGMENTS)
1103 to->flags |= SERVER_CTX_SEGMENTS;
1104 to->seg.i386_regs.ds = from->SegDs;
1105 to->seg.i386_regs.es = from->SegEs;
1106 to->seg.i386_regs.fs = from->SegFs;
1107 to->seg.i386_regs.gs = from->SegGs;
1109 if (flags & CONTEXT_FLOATING_POINT)
1111 to->flags |= SERVER_CTX_FLOATING_POINT;
1112 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1113 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1114 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1115 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1116 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1117 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1118 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1119 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1120 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1122 if (flags & CONTEXT_DEBUG_REGISTERS)
1124 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1125 to->debug.i386_regs.dr0 = from->Dr0;
1126 to->debug.i386_regs.dr1 = from->Dr1;
1127 to->debug.i386_regs.dr2 = from->Dr2;
1128 to->debug.i386_regs.dr3 = from->Dr3;
1129 to->debug.i386_regs.dr6 = from->Dr6;
1130 to->debug.i386_regs.dr7 = from->Dr7;
1132 if (flags & CONTEXT_EXTENDED_REGISTERS)
1134 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1135 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1137 return STATUS_SUCCESS;
1141 /***********************************************************************
1142 * context_from_server
1144 * Convert a register context from the server format.
1146 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1148 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1150 to->ContextFlags = CONTEXT_i386;
1151 if (from->flags & SERVER_CTX_CONTROL)
1153 to->ContextFlags |= CONTEXT_CONTROL;
1154 to->Ebp = from->ctl.i386_regs.ebp;
1155 to->Esp = from->ctl.i386_regs.esp;
1156 to->Eip = from->ctl.i386_regs.eip;
1157 to->SegCs = from->ctl.i386_regs.cs;
1158 to->SegSs = from->ctl.i386_regs.ss;
1159 to->EFlags = from->ctl.i386_regs.eflags;
1161 if (from->flags & SERVER_CTX_INTEGER)
1163 to->ContextFlags |= CONTEXT_INTEGER;
1164 to->Eax = from->integer.i386_regs.eax;
1165 to->Ebx = from->integer.i386_regs.ebx;
1166 to->Ecx = from->integer.i386_regs.ecx;
1167 to->Edx = from->integer.i386_regs.edx;
1168 to->Esi = from->integer.i386_regs.esi;
1169 to->Edi = from->integer.i386_regs.edi;
1171 if (from->flags & SERVER_CTX_SEGMENTS)
1173 to->ContextFlags |= CONTEXT_SEGMENTS;
1174 to->SegDs = from->seg.i386_regs.ds;
1175 to->SegEs = from->seg.i386_regs.es;
1176 to->SegFs = from->seg.i386_regs.fs;
1177 to->SegGs = from->seg.i386_regs.gs;
1179 if (from->flags & SERVER_CTX_FLOATING_POINT)
1181 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1182 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1183 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1184 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1185 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1186 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1187 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1188 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1189 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1190 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1192 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1194 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1195 to->Dr0 = from->debug.i386_regs.dr0;
1196 to->Dr1 = from->debug.i386_regs.dr1;
1197 to->Dr2 = from->debug.i386_regs.dr2;
1198 to->Dr3 = from->debug.i386_regs.dr3;
1199 to->Dr6 = from->debug.i386_regs.dr6;
1200 to->Dr7 = from->debug.i386_regs.dr7;
1202 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1204 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1205 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1207 return STATUS_SUCCESS;
1211 /***********************************************************************
1212 * is_privileged_instr
1214 * Check if the fault location is a privileged instruction.
1215 * Based on the instruction emulation code in dlls/kernel/instr.c.
1217 static inline DWORD is_privileged_instr( CONTEXT86 *context )
1219 const BYTE *instr;
1220 unsigned int prefix_count = 0;
1222 if (!wine_ldt_is_system( context->SegCs )) return 0;
1223 instr = (BYTE *)context->Eip;
1225 for (;;) switch(*instr)
1227 /* instruction prefixes */
1228 case 0x2e: /* %cs: */
1229 case 0x36: /* %ss: */
1230 case 0x3e: /* %ds: */
1231 case 0x26: /* %es: */
1232 case 0x64: /* %fs: */
1233 case 0x65: /* %gs: */
1234 case 0x66: /* opcode size */
1235 case 0x67: /* addr size */
1236 case 0xf0: /* lock */
1237 case 0xf2: /* repne */
1238 case 0xf3: /* repe */
1239 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1240 instr++;
1241 continue;
1243 case 0x0f: /* extended instruction */
1244 switch(instr[1])
1246 case 0x20: /* mov crX, reg */
1247 case 0x21: /* mov drX, reg */
1248 case 0x22: /* mov reg, crX */
1249 case 0x23: /* mov reg drX */
1250 return EXCEPTION_PRIV_INSTRUCTION;
1252 return 0;
1253 case 0x6c: /* insb (%dx) */
1254 case 0x6d: /* insl (%dx) */
1255 case 0x6e: /* outsb (%dx) */
1256 case 0x6f: /* outsl (%dx) */
1257 case 0xcd: /* int $xx */
1258 case 0xe4: /* inb al,XX */
1259 case 0xe5: /* in (e)ax,XX */
1260 case 0xe6: /* outb XX,al */
1261 case 0xe7: /* out XX,(e)ax */
1262 case 0xec: /* inb (%dx),%al */
1263 case 0xed: /* inl (%dx),%eax */
1264 case 0xee: /* outb %al,(%dx) */
1265 case 0xef: /* outl %eax,(%dx) */
1266 case 0xf4: /* hlt */
1267 case 0xfa: /* cli */
1268 case 0xfb: /* sti */
1269 return EXCEPTION_PRIV_INSTRUCTION;
1270 default:
1271 return 0;
1276 #include "pshpack1.h"
1277 struct atl_thunk
1279 DWORD movl; /* movl this,4(%esp) */
1280 DWORD this;
1281 BYTE jmp; /* jmp func */
1282 int func;
1284 #include "poppack.h"
1286 /**********************************************************************
1287 * check_atl_thunk
1289 * Check if code destination is an ATL thunk, and emulate it if so.
1291 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1293 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1294 BOOL ret = FALSE;
1296 __TRY
1298 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1300 *((DWORD *)context->Esp + 1) = thunk->this;
1301 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1302 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1303 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1304 ret = TRUE;
1307 __EXCEPT_PAGE_FAULT
1309 return FALSE;
1311 __ENDTRY
1312 return ret;
1316 /***********************************************************************
1317 * setup_exception_record
1319 * Setup the exception record and context on the thread stack.
1321 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1322 WORD fs, WORD gs, raise_func func )
1324 struct stack_layout
1326 void *ret_addr; /* return address from raise_func */
1327 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1328 CONTEXT *context_ptr; /* second arg for raise_func */
1329 CONTEXT context;
1330 EXCEPTION_RECORD rec;
1331 DWORD ebp;
1332 DWORD eip;
1333 } *stack = stack_ptr;
1334 DWORD exception_code = 0;
1336 /* stack sanity checks */
1338 if ((char *)stack >= (char *)get_signal_stack() &&
1339 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1341 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1342 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1343 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1344 NtCurrentTeb()->Tib.StackBase );
1345 abort_thread(1);
1348 if (stack - 1 > stack || /* check for overflow in subtraction */
1349 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1350 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1352 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1353 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1354 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1355 NtCurrentTeb()->Tib.StackBase );
1357 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1359 /* stack overflow on last page, unrecoverable */
1360 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1361 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1362 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1363 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1364 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1365 abort_thread(1);
1367 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1369 /* stack access below stack limit, may be recoverable */
1370 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1371 else
1373 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1374 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1375 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1376 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1377 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1378 abort_thread(1);
1382 stack--; /* push the stack_layout structure */
1383 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1384 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1385 #elif defined(VALGRIND_MAKE_WRITABLE)
1386 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1387 #endif
1388 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1389 stack->rec_ptr = &stack->rec;
1390 stack->context_ptr = &stack->context;
1392 stack->rec.ExceptionRecord = NULL;
1393 stack->rec.ExceptionCode = exception_code;
1394 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1395 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1396 stack->rec.NumberParameters = 0;
1398 save_context( &stack->context, sigcontext, fs, gs );
1400 /* now modify the sigcontext to return to the raise function */
1401 ESP_sig(sigcontext) = (DWORD)stack;
1402 EIP_sig(sigcontext) = (DWORD)func;
1403 /* clear single-step, direction, and align check flag */
1404 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1405 CS_sig(sigcontext) = wine_get_cs();
1406 DS_sig(sigcontext) = wine_get_ds();
1407 ES_sig(sigcontext) = wine_get_es();
1408 FS_sig(sigcontext) = wine_get_fs();
1409 GS_sig(sigcontext) = wine_get_gs();
1410 SS_sig(sigcontext) = wine_get_ss();
1412 return stack->rec_ptr;
1416 /***********************************************************************
1417 * setup_exception
1419 * Setup a proper stack frame for the raise function, and modify the
1420 * sigcontext so that the return from the signal handler will call
1421 * the raise function.
1423 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1425 WORD fs, gs;
1426 void *stack = init_handler( sigcontext, &fs, &gs );
1428 return setup_exception_record( sigcontext, stack, fs, gs, func );
1432 /***********************************************************************
1433 * get_exception_context
1435 * Get a pointer to the context built by setup_exception.
1437 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1439 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1443 /**********************************************************************
1444 * get_fpu_code
1446 * Get the FPU exception code from the FPU status.
1448 static inline DWORD get_fpu_code( const CONTEXT *context )
1450 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1452 if (status & 0x01) /* IE */
1454 if (status & 0x40) /* SF */
1455 return EXCEPTION_FLT_STACK_CHECK;
1456 else
1457 return EXCEPTION_FLT_INVALID_OPERATION;
1459 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1460 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1461 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1462 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1463 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1464 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1468 /**********************************************************************
1469 * raise_segv_exception
1471 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1473 NTSTATUS status;
1475 switch(rec->ExceptionCode)
1477 case EXCEPTION_ACCESS_VIOLATION:
1478 if (rec->NumberParameters == 2)
1480 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1481 goto done;
1482 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1483 rec->ExceptionInformation[0] )))
1484 goto done;
1486 break;
1487 case EXCEPTION_DATATYPE_MISALIGNMENT:
1488 /* FIXME: pass through exception handler first? */
1489 if (context->EFlags & 0x00040000)
1491 /* Disable AC flag, return */
1492 context->EFlags &= ~0x00040000;
1493 goto done;
1495 break;
1497 status = NtRaiseException( rec, context, TRUE );
1498 if (status) raise_status( status, rec );
1499 done:
1500 NtSetContextThread( GetCurrentThread(), context );
1504 /**********************************************************************
1505 * raise_trap_exception
1507 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1509 NTSTATUS status;
1511 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1513 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1515 /* when single stepping can't tell whether this is a hw bp or a
1516 * single step interrupt. try to avoid as much overhead as possible
1517 * and only do a server call if there is any hw bp enabled. */
1519 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1521 /* (possible) hardware breakpoint, fetch the debug registers */
1522 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1523 NtGetContextThread(GetCurrentThread(), context);
1524 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1527 context->EFlags &= ~0x100; /* clear single-step flag */
1530 status = NtRaiseException( rec, context, TRUE );
1531 if (status) raise_status( status, rec );
1535 /**********************************************************************
1536 * raise_generic_exception
1538 * Generic raise function for exceptions that don't need special treatment.
1540 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1542 NTSTATUS status;
1544 status = NtRaiseException( rec, context, TRUE );
1545 if (status) raise_status( status, rec );
1549 #ifdef __HAVE_VM86
1550 /**********************************************************************
1551 * raise_vm86_sti_exception
1553 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1555 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1556 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1558 if (ntdll_get_thread_data()->vm86_ptr)
1560 if (((char*)context->Eip >= (char*)vm86_return) &&
1561 ((char*)context->Eip <= (char*)vm86_return_end) &&
1562 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1563 /* exiting from VM86, can't throw */
1564 goto done;
1566 merge_vm86_pending_flags( rec );
1568 else if (get_vm86_teb_info()->dpmi_vif &&
1569 !wine_ldt_is_system(context->SegCs) &&
1570 !wine_ldt_is_system(context->SegSs))
1572 /* Executing DPMI code and virtual interrupts are enabled. */
1573 get_vm86_teb_info()->vm86_pending = 0;
1574 NtRaiseException( rec, context, TRUE );
1576 done:
1577 NtSetContextThread( GetCurrentThread(), context );
1581 /**********************************************************************
1582 * usr2_handler
1584 * Handler for SIGUSR2.
1585 * We use it to signal that the running __wine_enter_vm86() should
1586 * immediately set VIP_FLAG, causing pending events to be handled
1587 * as early as possible.
1589 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1591 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1592 rec->ExceptionCode = EXCEPTION_VM86_STI;
1594 #endif /* __HAVE_VM86 */
1597 /**********************************************************************
1598 * segv_handler
1600 * Handler for SIGSEGV and related errors.
1602 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1604 WORD fs, gs;
1605 EXCEPTION_RECORD *rec;
1606 SIGCONTEXT *context = sigcontext;
1607 void *stack = init_handler( sigcontext, &fs, &gs );
1609 /* check for page fault inside the thread stack */
1610 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1611 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1612 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1613 virtual_handle_stack_fault( siginfo->si_addr ))
1615 /* check if this was the last guard page */
1616 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1618 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1619 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1621 return;
1624 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1625 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1627 switch(get_trap_code(context))
1629 case TRAP_x86_OFLOW: /* Overflow exception */
1630 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1631 break;
1632 case TRAP_x86_BOUND: /* Bound range exception */
1633 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1634 break;
1635 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1636 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1637 break;
1638 case TRAP_x86_STKFLT: /* Stack fault */
1639 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1640 break;
1641 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1642 case TRAP_x86_PROTFLT: /* General protection fault */
1643 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1645 WORD err = get_error_code(context);
1646 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1647 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1648 rec->NumberParameters = 2;
1649 rec->ExceptionInformation[0] = 0;
1650 /* if error contains a LDT selector, use that as fault address */
1651 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1652 rec->ExceptionInformation[1] = err & ~7;
1653 else
1654 rec->ExceptionInformation[1] = 0xffffffff;
1656 break;
1657 case TRAP_x86_PAGEFLT: /* Page fault */
1658 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1659 rec->NumberParameters = 2;
1660 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1661 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1662 break;
1663 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1664 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1665 break;
1666 default:
1667 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1668 /* fall through */
1669 case TRAP_x86_NMI: /* NMI interrupt */
1670 case TRAP_x86_DNA: /* Device not available exception */
1671 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1672 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1673 case TRAP_x86_MCHK: /* Machine check exception */
1674 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1675 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1676 break;
1681 /**********************************************************************
1682 * trap_handler
1684 * Handler for SIGTRAP.
1686 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1688 SIGCONTEXT *context = sigcontext;
1689 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1691 switch(get_trap_code(context))
1693 case TRAP_x86_TRCTRAP: /* Single-step exception */
1694 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1695 break;
1696 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1697 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1698 /* fall through */
1699 default:
1700 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1701 break;
1706 /**********************************************************************
1707 * fpe_handler
1709 * Handler for SIGFPE.
1711 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1713 CONTEXT *win_context;
1714 SIGCONTEXT *context = sigcontext;
1715 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
1717 win_context = get_exception_context( rec );
1719 switch(get_trap_code(context))
1721 case TRAP_x86_DIVIDE: /* Division by zero exception */
1722 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1723 break;
1724 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1725 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1726 break;
1727 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1728 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1729 rec->ExceptionCode = get_fpu_code( win_context );
1730 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
1731 break;
1732 case TRAP_x86_CACHEFLT: /* SIMD exception */
1733 /* TODO:
1734 * Behaviour only tested for divide-by-zero exceptions
1735 * Check for other SIMD exceptions as well */
1736 if(siginfo->si_code != FPE_FLTDIV)
1737 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1738 siginfo->si_code);
1740 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1741 rec->NumberParameters = 1;
1742 /* no idea what meaning is actually behind this but that's what native does */
1743 rec->ExceptionInformation[0] = 0;
1744 break;
1745 default:
1746 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1747 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1748 break;
1753 /**********************************************************************
1754 * int_handler
1756 * Handler for SIGINT.
1758 * FIXME: should not be calling external functions on the signal stack.
1760 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1762 WORD fs, gs;
1763 init_handler( sigcontext, &fs, &gs );
1764 if (!dispatch_signal(SIGINT))
1766 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
1767 rec->ExceptionCode = CONTROL_C_EXIT;
1771 /**********************************************************************
1772 * abrt_handler
1774 * Handler for SIGABRT.
1776 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1778 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
1779 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1780 rec->ExceptionFlags = EH_NONCONTINUABLE;
1784 /**********************************************************************
1785 * quit_handler
1787 * Handler for SIGQUIT.
1789 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1791 WORD fs, gs;
1792 init_handler( sigcontext, &fs, &gs );
1793 abort_thread(0);
1797 /**********************************************************************
1798 * usr1_handler
1800 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1802 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1804 CONTEXT context;
1805 WORD fs, gs;
1807 init_handler( sigcontext, &fs, &gs );
1808 save_context( &context, sigcontext, fs, gs );
1809 wait_suspend( &context );
1810 restore_context( &context, sigcontext );
1814 /**********************************************************************
1815 * get_signal_stack_total_size
1817 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1818 * Must be a power of two.
1820 size_t get_signal_stack_total_size(void)
1822 if (!signal_stack_size)
1824 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1825 /* find the first power of two not smaller than min_size */
1826 while (size < min_size) size *= 2;
1827 signal_stack_mask = size - 1;
1828 signal_stack_size = size - teb_size;
1830 return signal_stack_size + teb_size;
1834 /***********************************************************************
1835 * __wine_set_signal_handler (NTDLL.@)
1837 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1839 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1840 if (handlers[sig] != NULL) return -2;
1841 handlers[sig] = wsh;
1842 return 0;
1846 /**********************************************************************
1847 * signal_init_thread
1849 void signal_init_thread( TEB *teb )
1851 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
1852 LDT_ENTRY fs_entry;
1853 stack_t ss;
1855 #ifdef __APPLE__
1856 int mib[2], val = 1;
1858 mib[0] = CTL_KERN;
1859 mib[1] = KERN_THALTSTACK;
1860 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1861 #endif
1863 ss.ss_sp = (char *)teb + teb_size;
1864 ss.ss_size = signal_stack_size;
1865 ss.ss_flags = 0;
1866 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
1868 wine_ldt_set_base( &fs_entry, teb );
1869 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
1870 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
1871 wine_ldt_init_fs( thread_data->fs, &fs_entry );
1872 thread_data->gs = wine_get_gs();
1875 /**********************************************************************
1876 * signal_init_process
1878 void signal_init_process(void)
1880 struct sigaction sig_act;
1882 sig_act.sa_mask = server_block_set;
1883 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1884 #ifdef SA_ONSTACK
1885 sig_act.sa_flags |= SA_ONSTACK;
1886 #endif
1888 sig_act.sa_sigaction = int_handler;
1889 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1890 sig_act.sa_sigaction = fpe_handler;
1891 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1892 sig_act.sa_sigaction = abrt_handler;
1893 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1894 sig_act.sa_sigaction = quit_handler;
1895 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1896 sig_act.sa_sigaction = usr1_handler;
1897 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1899 sig_act.sa_sigaction = segv_handler;
1900 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1901 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1902 #ifdef SIGBUS
1903 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1904 #endif
1906 #ifdef SIGTRAP
1907 sig_act.sa_sigaction = trap_handler;
1908 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1909 #endif
1911 #ifdef __HAVE_VM86
1912 sig_act.sa_sigaction = usr2_handler;
1913 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1914 #endif
1916 return;
1918 error:
1919 perror("sigaction");
1920 exit(1);
1924 #ifdef __HAVE_VM86
1925 /**********************************************************************
1926 * __wine_enter_vm86 (NTDLL.@)
1928 * Enter vm86 mode with the specified register context.
1930 void __wine_enter_vm86( CONTEXT *context )
1932 EXCEPTION_RECORD rec;
1933 int res;
1934 struct vm86plus_struct vm86;
1936 memset( &vm86, 0, sizeof(vm86) );
1937 for (;;)
1939 restore_vm86_context( context, &vm86 );
1941 ntdll_get_thread_data()->vm86_ptr = &vm86;
1942 merge_vm86_pending_flags( &rec );
1944 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1945 if (res < 0)
1947 errno = -res;
1948 return;
1951 save_vm86_context( context, &vm86 );
1953 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1954 rec.ExceptionRecord = NULL;
1955 rec.ExceptionAddress = (LPVOID)context->Eip;
1956 rec.NumberParameters = 0;
1958 switch(VM86_TYPE(res))
1960 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1961 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1962 break;
1963 case VM86_TRAP: /* return due to DOS-debugger request */
1964 switch(VM86_ARG(res))
1966 case TRAP_x86_TRCTRAP: /* Single-step exception */
1967 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1968 break;
1969 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1970 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1971 /* fall through */
1972 default:
1973 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1974 break;
1976 break;
1977 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1978 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1979 rec.NumberParameters = 1;
1980 rec.ExceptionInformation[0] = VM86_ARG(res);
1981 break;
1982 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1983 context->EFlags |= VIF_FLAG;
1984 context->EFlags &= ~VIP_FLAG;
1985 get_vm86_teb_info()->vm86_pending = 0;
1986 rec.ExceptionCode = EXCEPTION_VM86_STI;
1987 break;
1988 case VM86_PICRETURN: /* return due to pending PIC request */
1989 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1990 break;
1991 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1992 default:
1993 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1994 continue;
1996 raise_exception( &rec, context, TRUE );
2000 #else /* __HAVE_VM86 */
2001 /**********************************************************************
2002 * __wine_enter_vm86 (NTDLL.@)
2004 void __wine_enter_vm86( CONTEXT *context )
2006 MESSAGE("vm86 mode not supported on this platform\n");
2008 #endif /* __HAVE_VM86 */
2011 /***********************************************************************
2012 * RtlRaiseException (NTDLL.@)
2014 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2016 NTSTATUS status;
2018 rec->ExceptionAddress = (void *)context->Eip;
2019 status = raise_exception( rec, context, TRUE );
2020 if (status != STATUS_SUCCESS) raise_status( status, rec );
2022 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2025 /**********************************************************************
2026 * DbgBreakPoint (NTDLL.@)
2028 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
2030 /**********************************************************************
2031 * DbgUserBreakPoint (NTDLL.@)
2033 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
2035 /**********************************************************************
2036 * NtCurrentTeb (NTDLL.@)
2038 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2041 /**********************************************************************
2042 * EXC_CallHandler (internal)
2044 * Some exception handlers depend on EBP to have a fixed position relative to
2045 * the exception frame.
2046 * Shrinker depends on (*1) doing what it does,
2047 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2048 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2049 * function calling the handler having only 5 parameters (*4).
2051 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2052 " pushl %ebp\n"
2053 " movl %esp, %ebp\n"
2054 " pushl %ebx\n"
2055 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
2056 " pushl 24(%ebp)\n"
2057 " pushl 20(%ebp)\n"
2058 " pushl 16(%ebp)\n"
2059 " pushl 12(%ebp)\n"
2060 " pushl 8(%ebp)\n"
2061 " call " __ASM_NAME("call_exception_handler") "\n"
2062 " popl %ebx\n"
2063 " leave\n"
2064 " ret\n"
2066 __ASM_GLOBAL_FUNC(call_exception_handler,
2067 " pushl %ebp\n"
2068 " movl %esp, %ebp\n"
2069 " subl $12,%esp\n"
2070 " pushl 12(%ebp)\n" /* make any exceptions in this... */
2071 " pushl %edx\n" /* handler be handled by... */
2072 " .byte 0x64\n"
2073 " pushl (0)\n" /* nested_handler (passed in edx). */
2074 " .byte 0x64\n"
2075 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
2076 " pushl 20(%ebp)\n"
2077 " pushl 16(%ebp)\n"
2078 " pushl 12(%ebp)\n"
2079 " pushl 8(%ebp)\n"
2080 " movl 24(%ebp), %ecx\n" /* (*1) */
2081 " call *%ecx\n" /* call handler. (*2) */
2082 " .byte 0x64\n"
2083 " movl (0), %esp\n" /* restore previous... (*3) */
2084 " .byte 0x64\n"
2085 " popl (0)\n" /* exception frame. */
2086 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
2087 " popl %ebp\n"
2088 " ret $20\n" /* (*4) */
2090 #endif /* __i386__ */