ntdll: Use the standard mcontext_t type for the signal context on Android.
[wine.git] / dlls / ntdll / signal_i386.c
blob447e97c76eda4a1373e94ffd416baf6bdfdf1a64
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
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYSCALL_H
39 # include <syscall.h>
40 #else
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
43 # endif
44 #endif
45 #ifdef HAVE_SYS_VM86_H
46 # include <sys/vm86.h>
47 #endif
48 #ifdef HAVE_SYS_SIGNAL_H
49 # include <sys/signal.h>
50 #endif
51 #ifdef HAVE_SYS_SYSCTL_H
52 # include <sys/sysctl.h>
53 #endif
54 #ifdef HAVE_SYS_UCONTEXT_H
55 # include <sys/ucontext.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 #ifndef HAVE_SYS_UCONTEXT_H
107 enum
109 REG_GS, REG_FS, REG_ES, REG_DS, REG_EDI, REG_ESI, REG_EBP, REG_ESP,
110 REG_EBX, REG_EDX, REG_ECX, REG_EAX, REG_TRAPNO, REG_ERR, REG_EIP,
111 REG_CS, REG_EFL, REG_UESP, REG_SS, NGREG
114 typedef int greg_t;
115 typedef greg_t gregset_t[NGREG];
117 struct _libc_fpreg
119 unsigned short significand[4];
120 unsigned short exponent;
123 struct _libc_fpstate
125 unsigned long cw;
126 unsigned long sw;
127 unsigned long tag;
128 unsigned long ipoff;
129 unsigned long cssel;
130 unsigned long dataoff;
131 unsigned long datasel;
132 struct _libc_fpreg _st[8];
133 unsigned long status;
136 typedef struct _libc_fpstate* fpregset_t;
138 typedef struct
140 gregset_t gregs;
141 fpregset_t fpregs;
142 unsigned long oldmask;
143 unsigned long cr2;
144 } mcontext_t;
146 typedef struct ucontext
148 unsigned long uc_flags;
149 struct ucontext *uc_link;
150 stack_t uc_stack;
151 mcontext_t uc_mcontext;
152 sigset_t uc_sigmask;
153 } ucontext_t;
154 #endif /* HAVE_SYS_UCONTEXT_H */
156 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
157 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
158 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
159 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
160 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
161 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
162 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
163 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
165 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
166 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
167 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
168 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
169 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
170 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
172 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
173 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
174 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
175 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
177 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
178 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
180 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
181 #define VIF_FLAG 0x00080000
182 #define VIP_FLAG 0x00100000
184 int vm86_enter( void **vm86_ptr );
185 void vm86_return(void);
186 void vm86_return_end(void);
187 __ASM_GLOBAL_FUNC(vm86_enter,
188 "pushl %ebp\n\t"
189 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
190 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
191 "movl %esp,%ebp\n\t"
192 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
193 "pushl %ebx\n\t"
194 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
195 "movl $166,%eax\n\t" /*SYS_vm86*/
196 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
197 "movl (%ecx),%ecx\n\t"
198 "movl $1,%ebx\n\t" /*VM86_ENTER*/
199 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
200 "pushl %fs\n\t"
201 "pushl %gs\n\t"
202 "int $0x80\n"
203 ".globl " __ASM_NAME("vm86_return") "\n\t"
204 __ASM_FUNC("vm86_return") "\n"
205 __ASM_NAME("vm86_return") ":\n\t"
206 "popl %gs\n\t"
207 "popl %fs\n\t"
208 "popl %ecx\n\t"
209 "popl %ebx\n\t"
210 __ASM_CFI(".cfi_same_value %ebx\n\t")
211 "popl %ebp\n\t"
212 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
213 __ASM_CFI(".cfi_same_value %ebp\n\t")
214 "testl %eax,%eax\n\t"
215 "jl 0f\n\t"
216 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
217 "je " __ASM_NAME("vm86_enter") "\n\t"
218 "0:\n\t"
219 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
220 "movl $0,(%ecx)\n\t"
221 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
222 __ASM_FUNC("vm86_return_end") "\n"
223 __ASM_NAME("vm86_return_end") ":\n\t"
224 "ret" )
226 #ifdef HAVE_SYS_VM86_H
227 # define __HAVE_VM86
228 #endif
230 #ifdef __ANDROID__
231 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
232 void rt_sigreturn(void);
233 __ASM_GLOBAL_FUNC( rt_sigreturn,
234 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
235 "int $0x80" );
236 #endif
238 #elif defined (__BSDI__)
240 #include <machine/frame.h>
241 typedef struct trapframe ucontext_t;
243 #define EAX_sig(context) ((context)->tf_eax)
244 #define EBX_sig(context) ((context)->tf_ebx)
245 #define ECX_sig(context) ((context)->tf_ecx)
246 #define EDX_sig(context) ((context)->tf_edx)
247 #define ESI_sig(context) ((context)->tf_esi)
248 #define EDI_sig(context) ((context)->tf_edi)
249 #define EBP_sig(context) ((context)->tf_ebp)
251 #define CS_sig(context) ((context)->tf_cs)
252 #define DS_sig(context) ((context)->tf_ds)
253 #define ES_sig(context) ((context)->tf_es)
254 #define SS_sig(context) ((context)->tf_ss)
256 #define EFL_sig(context) ((context)->tf_eflags)
258 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
259 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
261 #define FPU_sig(context) NULL /* FIXME */
262 #define FPUX_sig(context) NULL /* FIXME */
264 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
266 #include <machine/trap.h>
268 #define EAX_sig(context) ((context)->sc_eax)
269 #define EBX_sig(context) ((context)->sc_ebx)
270 #define ECX_sig(context) ((context)->sc_ecx)
271 #define EDX_sig(context) ((context)->sc_edx)
272 #define ESI_sig(context) ((context)->sc_esi)
273 #define EDI_sig(context) ((context)->sc_edi)
274 #define EBP_sig(context) ((context)->sc_ebp)
276 #define CS_sig(context) ((context)->sc_cs)
277 #define DS_sig(context) ((context)->sc_ds)
278 #define ES_sig(context) ((context)->sc_es)
279 #define FS_sig(context) ((context)->sc_fs)
280 #define GS_sig(context) ((context)->sc_gs)
281 #define SS_sig(context) ((context)->sc_ss)
283 #define TRAP_sig(context) ((context)->sc_trapno)
284 #define ERROR_sig(context) ((context)->sc_err)
285 #define EFL_sig(context) ((context)->sc_eflags)
287 #define EIP_sig(context) ((context)->sc_eip)
288 #define ESP_sig(context) ((context)->sc_esp)
290 #define FPU_sig(context) NULL /* FIXME */
291 #define FPUX_sig(context) NULL /* FIXME */
293 #elif defined (__OpenBSD__)
295 #define EAX_sig(context) ((context)->sc_eax)
296 #define EBX_sig(context) ((context)->sc_ebx)
297 #define ECX_sig(context) ((context)->sc_ecx)
298 #define EDX_sig(context) ((context)->sc_edx)
299 #define ESI_sig(context) ((context)->sc_esi)
300 #define EDI_sig(context) ((context)->sc_edi)
301 #define EBP_sig(context) ((context)->sc_ebp)
303 #define CS_sig(context) ((context)->sc_cs)
304 #define DS_sig(context) ((context)->sc_ds)
305 #define ES_sig(context) ((context)->sc_es)
306 #define FS_sig(context) ((context)->sc_fs)
307 #define GS_sig(context) ((context)->sc_gs)
308 #define SS_sig(context) ((context)->sc_ss)
310 #define TRAP_sig(context) ((context)->sc_trapno)
311 #define ERROR_sig(context) ((context)->sc_err)
312 #define EFL_sig(context) ((context)->sc_eflags)
314 #define EIP_sig(context) ((context)->sc_eip)
315 #define ESP_sig(context) ((context)->sc_esp)
317 #define FPU_sig(context) NULL /* FIXME */
318 #define FPUX_sig(context) NULL /* FIXME */
320 #define T_MCHK T_MACHK
321 #define T_XMMFLT T_XFTRAP
323 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
325 #ifdef _SCO_DS
326 #include <sys/regset.h>
327 #define gregs regs
328 #endif
330 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
331 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
332 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
333 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
334 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
335 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
336 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
338 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
339 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
340 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
341 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
343 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
344 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
346 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
348 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
349 #ifdef UESP
350 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
351 #elif defined(R_ESP)
352 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
353 #else
354 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
355 #endif
356 #ifdef ERR
357 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
358 #endif
359 #ifdef TRAPNO
360 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
361 #endif
363 #define FPU_sig(context) NULL /* FIXME */
364 #define FPUX_sig(context) NULL /* FIXME */
366 #elif defined (__APPLE__)
368 /* work around silly renaming of struct members in OS X 10.5 */
369 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
370 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
371 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
372 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
373 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
374 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
375 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
376 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
377 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
378 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
379 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
380 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
381 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
382 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
383 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
384 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
385 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
386 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
387 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
388 #define FPU_sig(context) NULL
389 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
390 #else
391 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
392 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
393 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
394 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
395 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
396 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
397 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
398 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
399 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
400 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
401 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
402 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
403 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
404 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
405 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
406 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
407 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
408 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
409 #define FPU_sig(context) NULL
410 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
411 #endif
413 #elif defined(__NetBSD__)
415 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
416 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
417 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
418 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
419 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
420 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
421 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
422 #define ESP_sig(context) _UC_MACHINE_SP(context)
424 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
425 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
426 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
427 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
428 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
429 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
431 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
432 #define EIP_sig(context) _UC_MACHINE_PC(context)
433 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
434 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
436 #define FPU_sig(context) NULL
437 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
439 #define T_MCHK T_MCA
440 #define T_XMMFLT T_XMM
442 #elif defined(__GNU__)
444 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
445 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
446 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
447 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
448 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
449 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
450 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
451 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
453 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
454 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
455 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
456 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
457 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
458 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
460 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
461 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
462 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
463 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
465 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
466 #define FPUX_sig(context) NULL
468 #else
469 #error You must define the signal context functions for your platform
470 #endif /* linux */
472 WINE_DEFAULT_DEBUG_CHANNEL(seh);
474 typedef int (*wine_signal_handler)(unsigned int sig);
476 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
477 static size_t signal_stack_mask;
478 static size_t signal_stack_size;
480 static wine_signal_handler handlers[256];
482 static BOOL fpux_support; /* whether the CPU supports extended fpu context */
484 extern void DECLSPEC_NORETURN __wine_restore_regs( const CONTEXT *context );
486 enum i386_trap_code
488 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
489 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
490 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
491 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
492 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
493 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
494 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
495 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
496 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
497 TRAP_x86_DNA = T_DNA, /* Device not available exception */
498 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
499 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
500 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
501 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
502 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
503 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
504 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
505 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
506 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
507 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
508 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
509 #else
510 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
511 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
512 TRAP_x86_NMI = 2, /* NMI interrupt */
513 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
514 TRAP_x86_OFLOW = 4, /* Overflow exception */
515 TRAP_x86_BOUND = 5, /* Bound range exception */
516 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
517 TRAP_x86_DNA = 7, /* Device not available exception */
518 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
519 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
520 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
521 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
522 TRAP_x86_STKFLT = 12, /* Stack fault */
523 TRAP_x86_PROTFLT = 13, /* General protection fault */
524 TRAP_x86_PAGEFLT = 14, /* Page fault */
525 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
526 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
527 TRAP_x86_MCHK = 18, /* Machine check exception */
528 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
529 otherwise Cache flush exception (via SIGSEV) */
530 #endif
533 /* Exception record for handling exceptions happening inside exception handlers */
534 typedef struct
536 EXCEPTION_REGISTRATION_RECORD frame;
537 EXCEPTION_REGISTRATION_RECORD *prevFrame;
538 } EXC_NESTED_FRAME;
540 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
541 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
542 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler );
544 /***********************************************************************
545 * dispatch_signal
547 static inline int dispatch_signal(unsigned int sig)
549 if (handlers[sig] == NULL) return 0;
550 return handlers[sig](sig);
554 /***********************************************************************
555 * get_trap_code
557 * Get the trap code for a signal.
559 static inline enum i386_trap_code get_trap_code( const ucontext_t *sigcontext )
561 #ifdef TRAP_sig
562 return TRAP_sig(sigcontext);
563 #else
564 return TRAP_x86_UNKNOWN; /* unknown trap code */
565 #endif
568 /***********************************************************************
569 * get_error_code
571 * Get the error code for a signal.
573 static inline WORD get_error_code( const ucontext_t *sigcontext )
575 #ifdef ERROR_sig
576 return ERROR_sig(sigcontext);
577 #else
578 return 0;
579 #endif
582 /***********************************************************************
583 * get_signal_stack
585 * Get the base of the signal stack for the current thread.
587 static inline void *get_signal_stack(void)
589 return (char *)NtCurrentTeb() + 4096;
593 /***********************************************************************
594 * get_current_teb
596 * Get the current teb based on the stack pointer.
598 static inline TEB *get_current_teb(void)
600 unsigned long esp;
601 __asm__("movl %%esp,%0" : "=g" (esp) );
602 return (TEB *)(esp & ~signal_stack_mask);
606 /*******************************************************************
607 * is_valid_frame
609 static inline BOOL is_valid_frame( void *frame )
611 if ((ULONG_PTR)frame & 3) return FALSE;
612 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
613 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
616 /*******************************************************************
617 * raise_handler
619 * Handler for exceptions happening inside a handler.
621 static DWORD raise_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
622 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
624 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
625 return ExceptionContinueSearch;
626 /* We shouldn't get here so we store faulty frame in dispatcher */
627 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
628 return ExceptionNestedException;
632 /*******************************************************************
633 * unwind_handler
635 * Handler for exceptions happening inside an unwind handler.
637 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
638 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
640 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
641 return ExceptionContinueSearch;
642 /* We shouldn't get here so we store faulty frame in dispatcher */
643 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
644 return ExceptionCollidedUnwind;
648 /**********************************************************************
649 * call_stack_handlers
651 * Call the stack handlers chain.
653 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
655 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
656 DWORD res;
658 frame = NtCurrentTeb()->Tib.ExceptionList;
659 nested_frame = NULL;
660 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
662 /* Check frame address */
663 if (!is_valid_frame( frame ))
665 rec->ExceptionFlags |= EH_STACK_INVALID;
666 break;
669 /* Call handler */
670 TRACE( "calling handler at %p code=%x flags=%x\n",
671 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
672 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, raise_handler );
673 TRACE( "handler at %p returned %x\n", frame->Handler, res );
675 if (frame == nested_frame)
677 /* no longer nested */
678 nested_frame = NULL;
679 rec->ExceptionFlags &= ~EH_NESTED_CALL;
682 switch(res)
684 case ExceptionContinueExecution:
685 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
686 return STATUS_NONCONTINUABLE_EXCEPTION;
687 case ExceptionContinueSearch:
688 break;
689 case ExceptionNestedException:
690 if (nested_frame < dispatch) nested_frame = dispatch;
691 rec->ExceptionFlags |= EH_NESTED_CALL;
692 break;
693 default:
694 return STATUS_INVALID_DISPOSITION;
696 frame = frame->Prev;
698 return STATUS_UNHANDLED_EXCEPTION;
702 /*******************************************************************
703 * raise_exception
705 * Implementation of NtRaiseException.
707 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
709 NTSTATUS status;
711 if (first_chance)
713 DWORD c;
715 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
716 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
717 context->Eip, GetCurrentThreadId() );
718 for (c = 0; c < rec->NumberParameters; c++)
719 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
720 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
722 if (rec->ExceptionInformation[1] >> 16)
723 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
724 rec->ExceptionAddress,
725 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
726 else
727 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
728 rec->ExceptionAddress,
729 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
731 else
733 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
734 context->Eax, context->Ebx, context->Ecx,
735 context->Edx, context->Esi, context->Edi );
736 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
737 context->Ebp, context->Esp, context->SegCs, context->SegDs,
738 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
740 status = send_debug_event( rec, TRUE, context );
741 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
742 return STATUS_SUCCESS;
744 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
745 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Eip--;
747 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
748 return STATUS_SUCCESS;
750 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
751 return status;
754 /* last chance exception */
756 status = send_debug_event( rec, FALSE, context );
757 if (status != DBG_CONTINUE)
759 if (rec->ExceptionFlags & EH_STACK_INVALID)
760 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
761 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
762 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
763 else
764 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
765 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
766 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
768 return STATUS_SUCCESS;
772 #ifdef __HAVE_VM86
773 /***********************************************************************
774 * save_vm86_context
776 * Set the register values from a vm86 structure.
778 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
780 context->ContextFlags = CONTEXT_FULL;
781 context->Eax = vm86->regs.eax;
782 context->Ebx = vm86->regs.ebx;
783 context->Ecx = vm86->regs.ecx;
784 context->Edx = vm86->regs.edx;
785 context->Esi = vm86->regs.esi;
786 context->Edi = vm86->regs.edi;
787 context->Esp = vm86->regs.esp;
788 context->Ebp = vm86->regs.ebp;
789 context->Eip = vm86->regs.eip;
790 context->SegCs = vm86->regs.cs;
791 context->SegDs = vm86->regs.ds;
792 context->SegEs = vm86->regs.es;
793 context->SegFs = vm86->regs.fs;
794 context->SegGs = vm86->regs.gs;
795 context->SegSs = vm86->regs.ss;
796 context->EFlags = vm86->regs.eflags;
800 /***********************************************************************
801 * restore_vm86_context
803 * Build a vm86 structure from the register values.
805 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
807 vm86->regs.eax = context->Eax;
808 vm86->regs.ebx = context->Ebx;
809 vm86->regs.ecx = context->Ecx;
810 vm86->regs.edx = context->Edx;
811 vm86->regs.esi = context->Esi;
812 vm86->regs.edi = context->Edi;
813 vm86->regs.esp = context->Esp;
814 vm86->regs.ebp = context->Ebp;
815 vm86->regs.eip = context->Eip;
816 vm86->regs.cs = context->SegCs;
817 vm86->regs.ds = context->SegDs;
818 vm86->regs.es = context->SegEs;
819 vm86->regs.fs = context->SegFs;
820 vm86->regs.gs = context->SegGs;
821 vm86->regs.ss = context->SegSs;
822 vm86->regs.eflags = context->EFlags;
826 /**********************************************************************
827 * merge_vm86_pending_flags
829 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
830 * raises exception if there are pending events and VIF flag
831 * has been turned on.
833 * Called from __wine_enter_vm86 because vm86_enter
834 * doesn't check for pending events.
836 * Called from raise_vm86_sti_exception to check for
837 * pending events in a signal safe way.
839 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
841 BOOL check_pending = TRUE;
842 struct vm86plus_struct *vm86 =
843 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
846 * In order to prevent a race when SIGUSR2 occurs while
847 * we are returning from exception handler, pending events
848 * will be rechecked after each raised exception.
850 while (check_pending && get_vm86_teb_info()->vm86_pending)
852 check_pending = FALSE;
853 ntdll_get_thread_data()->vm86_ptr = NULL;
856 * If VIF is set, throw exception.
857 * Note that SIGUSR2 may turn VIF flag off so
858 * VIF check must occur only when TEB.vm86_ptr is NULL.
860 if (vm86->regs.eflags & VIF_FLAG)
862 CONTEXT vcontext;
863 save_vm86_context( &vcontext, vm86 );
865 rec->ExceptionCode = EXCEPTION_VM86_STI;
866 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
867 rec->ExceptionRecord = NULL;
868 rec->NumberParameters = 0;
869 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
871 vcontext.EFlags &= ~VIP_FLAG;
872 get_vm86_teb_info()->vm86_pending = 0;
873 raise_exception( rec, &vcontext, TRUE );
875 restore_vm86_context( &vcontext, vm86 );
876 check_pending = TRUE;
879 ntdll_get_thread_data()->vm86_ptr = vm86;
883 * Merge VIP flags in a signal safe way. This requires
884 * that the following operation compiles into atomic
885 * instruction.
887 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
889 #endif /* __HAVE_VM86 */
892 #ifdef __sun
894 /* We have to workaround two Solaris breakages:
895 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
896 * code crash badly.
897 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
898 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
901 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
902 __ASM_GLOBAL_FUNC( sigaction_syscall,
903 "movl $0x62,%eax\n\t"
904 "int $0x91\n\t"
905 "ret" )
907 /* assume the same libc handler is used for all signals */
908 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
910 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
912 struct ntdll_thread_data *thread_data;
914 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
916 thread_data = (struct ntdll_thread_data *)get_current_teb()->SpareBytes1;
917 wine_set_fs( thread_data->fs );
918 wine_set_gs( thread_data->gs );
920 libc_sigacthandler( signal, siginfo, sigcontext );
923 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
925 struct sigaction real_act;
927 if (sigaction( sig, new, old ) == -1) return -1;
929 /* retrieve the real handler and flags with a direct syscall */
930 sigaction_syscall( sig, NULL, &real_act );
931 libc_sigacthandler = real_act.sa_sigaction;
932 real_act.sa_sigaction = wine_sigacthandler;
933 sigaction_syscall( sig, &real_act, NULL );
934 return 0;
936 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
938 #endif
940 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
942 extern void clear_alignment_flag(void);
943 __ASM_GLOBAL_FUNC( clear_alignment_flag,
944 "pushfl\n\t"
945 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
946 "andl $~0x40000,(%esp)\n\t"
947 "popfl\n\t"
948 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
949 "ret" )
952 /***********************************************************************
953 * init_handler
955 * Handler initialization when the full context is not needed.
956 * Return the stack pointer to use for pushing the exception data.
958 static inline void *init_handler( const ucontext_t *sigcontext, WORD *fs, WORD *gs )
960 TEB *teb = get_current_teb();
962 clear_alignment_flag();
964 /* get %fs and %gs at time of the fault */
965 #ifdef FS_sig
966 *fs = LOWORD(FS_sig(sigcontext));
967 #else
968 *fs = wine_get_fs();
969 #endif
970 #ifdef GS_sig
971 *gs = LOWORD(GS_sig(sigcontext));
972 #else
973 *gs = wine_get_gs();
974 #endif
976 #ifndef __sun /* see above for Solaris handling */
978 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
979 wine_set_fs( thread_data->fs );
980 wine_set_gs( thread_data->gs );
982 #endif
984 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
985 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
988 * Win16 or DOS protected mode. Note that during switch
989 * from 16-bit mode to linear mode, CS may be set to system
990 * segment before FS is restored. Fortunately, in this case
991 * SS is still non-system segment. This is why both CS and SS
992 * are checked.
994 return teb->WOW32Reserved;
996 return (void *)(ESP_sig(sigcontext) & ~3);
1000 /***********************************************************************
1001 * save_fpu
1003 * Save the thread FPU context.
1005 static inline void save_fpu( CONTEXT *context )
1007 #ifdef __GNUC__
1008 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1009 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
1010 #endif
1014 /***********************************************************************
1015 * save_fpux
1017 * Save the thread FPU extended context.
1019 static inline void save_fpux( CONTEXT *context )
1021 #ifdef __GNUC__
1022 /* we have to enforce alignment by hand */
1023 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
1024 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
1026 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
1027 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1028 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
1029 #endif
1033 /***********************************************************************
1034 * restore_fpu
1036 * Restore the FPU context to a sigcontext.
1038 static inline void restore_fpu( const CONTEXT *context )
1040 FLOATING_SAVE_AREA float_status = context->FloatSave;
1041 /* reset the current interrupt status */
1042 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
1043 #ifdef __GNUC__
1044 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
1045 #endif /* __GNUC__ */
1049 /***********************************************************************
1050 * restore_fpux
1052 * Restore the FPU extended context to a sigcontext.
1054 static inline void restore_fpux( const CONTEXT *context )
1056 #ifdef __GNUC__
1057 /* we have to enforce alignment by hand */
1058 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
1059 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
1061 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
1062 /* reset the current interrupt status */
1063 state->StatusWord &= state->ControlWord | 0xff80;
1064 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
1065 #endif
1069 /***********************************************************************
1070 * fpux_to_fpu
1072 * Build a standard FPU context from an extended one.
1074 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
1076 unsigned int i, tag, stack_top;
1078 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
1079 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
1080 fpu->ErrorOffset = fpux->ErrorOffset;
1081 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
1082 fpu->DataOffset = fpux->DataOffset;
1083 fpu->DataSelector = fpux->DataSelector;
1084 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
1086 stack_top = (fpux->StatusWord >> 11) & 7;
1087 fpu->TagWord = 0xffff0000;
1088 for (i = 0; i < 8; i++)
1090 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
1091 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
1092 else
1094 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
1095 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
1097 tag = 2; /* special */
1099 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
1101 if (reg->Low) tag = 2; /* special */
1102 else tag = 1; /* zero */
1104 else
1106 if (reg->Low >> 63) tag = 0; /* valid */
1107 else tag = 2; /* special */
1110 fpu->TagWord |= tag << (2 * i);
1115 /***********************************************************************
1116 * save_context
1118 * Build a context structure from the signal info.
1120 static inline void save_context( CONTEXT *context, const ucontext_t *sigcontext, WORD fs, WORD gs )
1122 struct ntdll_thread_data * const regs = ntdll_get_thread_data();
1123 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1124 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1126 memset(context, 0, sizeof(*context));
1127 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
1128 context->Eax = EAX_sig(sigcontext);
1129 context->Ebx = EBX_sig(sigcontext);
1130 context->Ecx = ECX_sig(sigcontext);
1131 context->Edx = EDX_sig(sigcontext);
1132 context->Esi = ESI_sig(sigcontext);
1133 context->Edi = EDI_sig(sigcontext);
1134 context->Ebp = EBP_sig(sigcontext);
1135 context->EFlags = EFL_sig(sigcontext);
1136 context->Eip = EIP_sig(sigcontext);
1137 context->Esp = ESP_sig(sigcontext);
1138 context->SegCs = LOWORD(CS_sig(sigcontext));
1139 context->SegDs = LOWORD(DS_sig(sigcontext));
1140 context->SegEs = LOWORD(ES_sig(sigcontext));
1141 context->SegFs = fs;
1142 context->SegGs = gs;
1143 context->SegSs = LOWORD(SS_sig(sigcontext));
1144 context->Dr0 = regs->dr0;
1145 context->Dr1 = regs->dr1;
1146 context->Dr2 = regs->dr2;
1147 context->Dr3 = regs->dr3;
1148 context->Dr6 = regs->dr6;
1149 context->Dr7 = regs->dr7;
1151 if (fpu)
1153 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1154 context->FloatSave = *fpu;
1156 if (fpux)
1158 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
1159 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
1160 fpux_support = TRUE;
1161 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
1163 if (!fpu && !fpux) save_fpu( context );
1167 /***********************************************************************
1168 * restore_context
1170 * Restore the signal info from the context.
1172 static inline void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1174 struct ntdll_thread_data * const regs = ntdll_get_thread_data();
1175 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1176 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1178 regs->dr0 = context->Dr0;
1179 regs->dr1 = context->Dr1;
1180 regs->dr2 = context->Dr2;
1181 regs->dr3 = context->Dr3;
1182 regs->dr6 = context->Dr6;
1183 regs->dr7 = context->Dr7;
1184 EAX_sig(sigcontext) = context->Eax;
1185 EBX_sig(sigcontext) = context->Ebx;
1186 ECX_sig(sigcontext) = context->Ecx;
1187 EDX_sig(sigcontext) = context->Edx;
1188 ESI_sig(sigcontext) = context->Esi;
1189 EDI_sig(sigcontext) = context->Edi;
1190 EBP_sig(sigcontext) = context->Ebp;
1191 EFL_sig(sigcontext) = context->EFlags;
1192 EIP_sig(sigcontext) = context->Eip;
1193 ESP_sig(sigcontext) = context->Esp;
1194 CS_sig(sigcontext) = context->SegCs;
1195 DS_sig(sigcontext) = context->SegDs;
1196 ES_sig(sigcontext) = context->SegEs;
1197 SS_sig(sigcontext) = context->SegSs;
1198 #ifdef GS_sig
1199 GS_sig(sigcontext) = context->SegGs;
1200 #else
1201 wine_set_gs( context->SegGs );
1202 #endif
1203 #ifdef FS_sig
1204 FS_sig(sigcontext) = context->SegFs;
1205 #else
1206 wine_set_fs( context->SegFs );
1207 #endif
1209 if (fpu) *fpu = context->FloatSave;
1210 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
1211 if (!fpu && !fpux) restore_fpu( context );
1215 /***********************************************************************
1216 * RtlCaptureContext (NTDLL.@)
1218 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
1219 "pushl %eax\n\t"
1220 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1221 "movl 8(%esp),%eax\n\t" /* context */
1222 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1223 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1224 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1225 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1226 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1227 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1228 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1229 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1230 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1231 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1232 "movl %ebp,0xb4(%eax)\n\t" /* context->Ebp */
1233 "movl 4(%esp),%edx\n\t"
1234 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1235 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1236 "pushfl\n\t"
1237 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1238 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1239 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1240 "leal 8(%esp),%edx\n\t"
1241 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1242 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1243 "popl 0xb0(%eax)\n\t" /* context->Eax */
1244 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1245 "ret $4" )
1248 /***********************************************************************
1249 * set_cpu_context
1251 * Set the new CPU context. Used by NtSetContextThread.
1253 void set_cpu_context( const CONTEXT *context )
1255 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1257 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
1258 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
1260 if (flags & CONTEXT_DEBUG_REGISTERS)
1262 ntdll_get_thread_data()->dr0 = context->Dr0;
1263 ntdll_get_thread_data()->dr1 = context->Dr1;
1264 ntdll_get_thread_data()->dr2 = context->Dr2;
1265 ntdll_get_thread_data()->dr3 = context->Dr3;
1266 ntdll_get_thread_data()->dr6 = context->Dr6;
1267 ntdll_get_thread_data()->dr7 = context->Dr7;
1269 if (flags & CONTEXT_FULL)
1271 if (!(flags & CONTEXT_CONTROL))
1272 FIXME( "setting partial context (%x) not supported\n", flags );
1273 else if (flags & CONTEXT_SEGMENTS)
1274 __wine_restore_regs( context );
1275 else
1277 CONTEXT newcontext = *context;
1278 newcontext.SegDs = wine_get_ds();
1279 newcontext.SegEs = wine_get_es();
1280 newcontext.SegFs = wine_get_fs();
1281 newcontext.SegGs = wine_get_gs();
1282 __wine_restore_regs( &newcontext );
1288 /***********************************************************************
1289 * set_debug_registers
1291 static void set_debug_registers( const CONTEXT *context )
1293 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1294 context_t server_context;
1296 if (!(flags & CONTEXT_DEBUG_REGISTERS)) return;
1297 if (ntdll_get_thread_data()->dr0 == context->Dr0 &&
1298 ntdll_get_thread_data()->dr1 == context->Dr1 &&
1299 ntdll_get_thread_data()->dr2 == context->Dr2 &&
1300 ntdll_get_thread_data()->dr3 == context->Dr3 &&
1301 ntdll_get_thread_data()->dr6 == context->Dr6 &&
1302 ntdll_get_thread_data()->dr7 == context->Dr7) return;
1304 context_to_server( &server_context, context );
1306 SERVER_START_REQ( set_thread_context )
1308 req->handle = wine_server_obj_handle( GetCurrentThread() );
1309 req->suspend = 0;
1310 wine_server_add_data( req, &server_context, sizeof(server_context) );
1311 wine_server_call( req );
1313 SERVER_END_REQ;
1317 /***********************************************************************
1318 * copy_context
1320 * Copy a register context according to the flags.
1322 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1324 flags &= ~CONTEXT_i386; /* get rid of CPU id */
1325 if (flags & CONTEXT_INTEGER)
1327 to->Eax = from->Eax;
1328 to->Ebx = from->Ebx;
1329 to->Ecx = from->Ecx;
1330 to->Edx = from->Edx;
1331 to->Esi = from->Esi;
1332 to->Edi = from->Edi;
1334 if (flags & CONTEXT_CONTROL)
1336 to->Ebp = from->Ebp;
1337 to->Esp = from->Esp;
1338 to->Eip = from->Eip;
1339 to->SegCs = from->SegCs;
1340 to->SegSs = from->SegSs;
1341 to->EFlags = from->EFlags;
1343 if (flags & CONTEXT_SEGMENTS)
1345 to->SegDs = from->SegDs;
1346 to->SegEs = from->SegEs;
1347 to->SegFs = from->SegFs;
1348 to->SegGs = from->SegGs;
1350 if (flags & CONTEXT_DEBUG_REGISTERS)
1352 to->Dr0 = from->Dr0;
1353 to->Dr1 = from->Dr1;
1354 to->Dr2 = from->Dr2;
1355 to->Dr3 = from->Dr3;
1356 to->Dr6 = from->Dr6;
1357 to->Dr7 = from->Dr7;
1359 if (flags & CONTEXT_FLOATING_POINT)
1361 to->FloatSave = from->FloatSave;
1363 if (flags & CONTEXT_EXTENDED_REGISTERS)
1365 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
1370 /***********************************************************************
1371 * context_to_server
1373 * Convert a register context to the server format.
1375 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1377 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1379 memset( to, 0, sizeof(*to) );
1380 to->cpu = CPU_x86;
1382 if (flags & CONTEXT_CONTROL)
1384 to->flags |= SERVER_CTX_CONTROL;
1385 to->ctl.i386_regs.ebp = from->Ebp;
1386 to->ctl.i386_regs.esp = from->Esp;
1387 to->ctl.i386_regs.eip = from->Eip;
1388 to->ctl.i386_regs.cs = from->SegCs;
1389 to->ctl.i386_regs.ss = from->SegSs;
1390 to->ctl.i386_regs.eflags = from->EFlags;
1392 if (flags & CONTEXT_INTEGER)
1394 to->flags |= SERVER_CTX_INTEGER;
1395 to->integer.i386_regs.eax = from->Eax;
1396 to->integer.i386_regs.ebx = from->Ebx;
1397 to->integer.i386_regs.ecx = from->Ecx;
1398 to->integer.i386_regs.edx = from->Edx;
1399 to->integer.i386_regs.esi = from->Esi;
1400 to->integer.i386_regs.edi = from->Edi;
1402 if (flags & CONTEXT_SEGMENTS)
1404 to->flags |= SERVER_CTX_SEGMENTS;
1405 to->seg.i386_regs.ds = from->SegDs;
1406 to->seg.i386_regs.es = from->SegEs;
1407 to->seg.i386_regs.fs = from->SegFs;
1408 to->seg.i386_regs.gs = from->SegGs;
1410 if (flags & CONTEXT_FLOATING_POINT)
1412 to->flags |= SERVER_CTX_FLOATING_POINT;
1413 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1414 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1415 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1416 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1417 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1418 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1419 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1420 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1421 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1423 if (flags & CONTEXT_DEBUG_REGISTERS)
1425 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1426 to->debug.i386_regs.dr0 = from->Dr0;
1427 to->debug.i386_regs.dr1 = from->Dr1;
1428 to->debug.i386_regs.dr2 = from->Dr2;
1429 to->debug.i386_regs.dr3 = from->Dr3;
1430 to->debug.i386_regs.dr6 = from->Dr6;
1431 to->debug.i386_regs.dr7 = from->Dr7;
1433 if (flags & CONTEXT_EXTENDED_REGISTERS)
1435 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1436 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1438 return STATUS_SUCCESS;
1442 /***********************************************************************
1443 * context_from_server
1445 * Convert a register context from the server format.
1447 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1449 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1451 to->ContextFlags = CONTEXT_i386;
1452 if (from->flags & SERVER_CTX_CONTROL)
1454 to->ContextFlags |= CONTEXT_CONTROL;
1455 to->Ebp = from->ctl.i386_regs.ebp;
1456 to->Esp = from->ctl.i386_regs.esp;
1457 to->Eip = from->ctl.i386_regs.eip;
1458 to->SegCs = from->ctl.i386_regs.cs;
1459 to->SegSs = from->ctl.i386_regs.ss;
1460 to->EFlags = from->ctl.i386_regs.eflags;
1462 if (from->flags & SERVER_CTX_INTEGER)
1464 to->ContextFlags |= CONTEXT_INTEGER;
1465 to->Eax = from->integer.i386_regs.eax;
1466 to->Ebx = from->integer.i386_regs.ebx;
1467 to->Ecx = from->integer.i386_regs.ecx;
1468 to->Edx = from->integer.i386_regs.edx;
1469 to->Esi = from->integer.i386_regs.esi;
1470 to->Edi = from->integer.i386_regs.edi;
1472 if (from->flags & SERVER_CTX_SEGMENTS)
1474 to->ContextFlags |= CONTEXT_SEGMENTS;
1475 to->SegDs = from->seg.i386_regs.ds;
1476 to->SegEs = from->seg.i386_regs.es;
1477 to->SegFs = from->seg.i386_regs.fs;
1478 to->SegGs = from->seg.i386_regs.gs;
1480 if (from->flags & SERVER_CTX_FLOATING_POINT)
1482 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1483 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1484 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1485 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1486 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1487 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1488 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1489 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1490 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1491 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1493 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1495 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1496 to->Dr0 = from->debug.i386_regs.dr0;
1497 to->Dr1 = from->debug.i386_regs.dr1;
1498 to->Dr2 = from->debug.i386_regs.dr2;
1499 to->Dr3 = from->debug.i386_regs.dr3;
1500 to->Dr6 = from->debug.i386_regs.dr6;
1501 to->Dr7 = from->debug.i386_regs.dr7;
1503 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1505 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1506 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1508 return STATUS_SUCCESS;
1512 /***********************************************************************
1513 * is_privileged_instr
1515 * Check if the fault location is a privileged instruction.
1516 * Based on the instruction emulation code in dlls/kernel/instr.c.
1518 static inline DWORD is_privileged_instr( CONTEXT *context )
1520 const BYTE *instr;
1521 unsigned int prefix_count = 0;
1523 if (!wine_ldt_is_system( context->SegCs )) return 0;
1524 instr = (BYTE *)context->Eip;
1526 for (;;) switch(*instr)
1528 /* instruction prefixes */
1529 case 0x2e: /* %cs: */
1530 case 0x36: /* %ss: */
1531 case 0x3e: /* %ds: */
1532 case 0x26: /* %es: */
1533 case 0x64: /* %fs: */
1534 case 0x65: /* %gs: */
1535 case 0x66: /* opcode size */
1536 case 0x67: /* addr size */
1537 case 0xf0: /* lock */
1538 case 0xf2: /* repne */
1539 case 0xf3: /* repe */
1540 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1541 instr++;
1542 continue;
1544 case 0x0f: /* extended instruction */
1545 switch(instr[1])
1547 case 0x20: /* mov crX, reg */
1548 case 0x21: /* mov drX, reg */
1549 case 0x22: /* mov reg, crX */
1550 case 0x23: /* mov reg drX */
1551 return EXCEPTION_PRIV_INSTRUCTION;
1553 return 0;
1554 case 0x6c: /* insb (%dx) */
1555 case 0x6d: /* insl (%dx) */
1556 case 0x6e: /* outsb (%dx) */
1557 case 0x6f: /* outsl (%dx) */
1558 case 0xcd: /* int $xx */
1559 case 0xe4: /* inb al,XX */
1560 case 0xe5: /* in (e)ax,XX */
1561 case 0xe6: /* outb XX,al */
1562 case 0xe7: /* out XX,(e)ax */
1563 case 0xec: /* inb (%dx),%al */
1564 case 0xed: /* inl (%dx),%eax */
1565 case 0xee: /* outb %al,(%dx) */
1566 case 0xef: /* outl %eax,(%dx) */
1567 case 0xf4: /* hlt */
1568 case 0xfa: /* cli */
1569 case 0xfb: /* sti */
1570 return EXCEPTION_PRIV_INSTRUCTION;
1571 default:
1572 return 0;
1576 /***********************************************************************
1577 * check_invalid_gs
1579 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1581 static inline BOOL check_invalid_gs( CONTEXT *context )
1583 unsigned int prefix_count = 0;
1584 const BYTE *instr = (BYTE *)context->Eip;
1585 WORD system_gs = ntdll_get_thread_data()->gs;
1587 if (context->SegGs == system_gs) return FALSE;
1588 if (!wine_ldt_is_system( context->SegCs )) return FALSE;
1589 /* only handle faults in system libraries */
1590 if (virtual_is_valid_code_address( instr, 1 )) return FALSE;
1592 for (;;) switch(*instr)
1594 /* instruction prefixes */
1595 case 0x2e: /* %cs: */
1596 case 0x36: /* %ss: */
1597 case 0x3e: /* %ds: */
1598 case 0x26: /* %es: */
1599 case 0x64: /* %fs: */
1600 case 0x66: /* opcode size */
1601 case 0x67: /* addr size */
1602 case 0xf0: /* lock */
1603 case 0xf2: /* repne */
1604 case 0xf3: /* repe */
1605 if (++prefix_count >= 15) return FALSE;
1606 instr++;
1607 continue;
1608 case 0x65: /* %gs: */
1609 TRACE( "%04x/%04x at %p, fixing up\n", context->SegGs, system_gs, instr );
1610 context->SegGs = system_gs;
1611 return TRUE;
1612 default:
1613 return FALSE;
1618 #include "pshpack1.h"
1619 struct atl_thunk
1621 DWORD movl; /* movl this,4(%esp) */
1622 DWORD this;
1623 BYTE jmp; /* jmp func */
1624 int func;
1626 #include "poppack.h"
1628 /**********************************************************************
1629 * check_atl_thunk
1631 * Check if code destination is an ATL thunk, and emulate it if so.
1633 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1635 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1636 BOOL ret = FALSE;
1638 if (!virtual_is_valid_code_address( thunk, sizeof(*thunk) )) return FALSE;
1640 __TRY
1642 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1644 *((DWORD *)context->Esp + 1) = thunk->this;
1645 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1646 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1647 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1648 ret = TRUE;
1651 __EXCEPT_PAGE_FAULT
1653 return FALSE;
1655 __ENDTRY
1656 return ret;
1660 /***********************************************************************
1661 * setup_exception_record
1663 * Setup the exception record and context on the thread stack.
1665 static EXCEPTION_RECORD *setup_exception_record( ucontext_t *sigcontext, void *stack_ptr,
1666 WORD fs, WORD gs, raise_func func )
1668 struct stack_layout
1670 void *ret_addr; /* return address from raise_func */
1671 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1672 CONTEXT *context_ptr; /* second arg for raise_func */
1673 CONTEXT context;
1674 EXCEPTION_RECORD rec;
1675 DWORD ebp;
1676 DWORD eip;
1677 } *stack = stack_ptr;
1678 DWORD exception_code = 0;
1680 /* stack sanity checks */
1682 if ((char *)stack >= (char *)get_signal_stack() &&
1683 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1685 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1686 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1687 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1688 NtCurrentTeb()->Tib.StackBase );
1689 abort_thread(1);
1692 if (stack - 1 > stack || /* check for overflow in subtraction */
1693 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1694 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1696 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1697 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1698 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1699 NtCurrentTeb()->Tib.StackBase );
1701 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1703 /* stack overflow on last page, unrecoverable */
1704 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1705 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1706 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1707 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1708 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1709 abort_thread(1);
1711 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1713 /* stack access below stack limit, may be recoverable */
1714 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1715 else
1717 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1718 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1719 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1720 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1721 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1722 abort_thread(1);
1726 stack--; /* push the stack_layout structure */
1727 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1728 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1729 #elif defined(VALGRIND_MAKE_WRITABLE)
1730 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1731 #endif
1732 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1733 stack->rec_ptr = &stack->rec;
1734 stack->context_ptr = &stack->context;
1736 stack->rec.ExceptionRecord = NULL;
1737 stack->rec.ExceptionCode = exception_code;
1738 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1739 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1740 stack->rec.NumberParameters = 0;
1742 save_context( &stack->context, sigcontext, fs, gs );
1744 /* now modify the sigcontext to return to the raise function */
1745 ESP_sig(sigcontext) = (DWORD)stack;
1746 EIP_sig(sigcontext) = (DWORD)func;
1747 /* clear single-step, direction, and align check flag */
1748 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1749 CS_sig(sigcontext) = wine_get_cs();
1750 DS_sig(sigcontext) = wine_get_ds();
1751 ES_sig(sigcontext) = wine_get_es();
1752 FS_sig(sigcontext) = wine_get_fs();
1753 GS_sig(sigcontext) = wine_get_gs();
1754 SS_sig(sigcontext) = wine_get_ss();
1756 return stack->rec_ptr;
1760 /***********************************************************************
1761 * setup_exception
1763 * Setup a proper stack frame for the raise function, and modify the
1764 * sigcontext so that the return from the signal handler will call
1765 * the raise function.
1767 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1769 WORD fs, gs;
1770 void *stack = init_handler( sigcontext, &fs, &gs );
1772 return setup_exception_record( sigcontext, stack, fs, gs, func );
1776 /***********************************************************************
1777 * get_exception_context
1779 * Get a pointer to the context built by setup_exception.
1781 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1783 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1787 /**********************************************************************
1788 * get_fpu_code
1790 * Get the FPU exception code from the FPU status.
1792 static inline DWORD get_fpu_code( const CONTEXT *context )
1794 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1796 if (status & 0x01) /* IE */
1798 if (status & 0x40) /* SF */
1799 return EXCEPTION_FLT_STACK_CHECK;
1800 else
1801 return EXCEPTION_FLT_INVALID_OPERATION;
1803 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1804 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1805 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1806 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1807 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1808 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1812 /**********************************************************************
1813 * raise_segv_exception
1815 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1817 NTSTATUS status;
1819 switch(rec->ExceptionCode)
1821 case EXCEPTION_ACCESS_VIOLATION:
1822 if (rec->NumberParameters == 2)
1824 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1825 goto done;
1826 if (rec->ExceptionInformation[1] == 0xffffffff && check_invalid_gs( context ))
1827 goto done;
1828 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1829 rec->ExceptionInformation[0] )))
1830 goto done;
1831 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1832 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT)
1834 ULONG flags;
1835 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
1836 &flags, sizeof(flags), NULL );
1837 if (!(flags & MEM_EXECUTE_OPTION_DISABLE))
1838 rec->ExceptionInformation[0] = EXCEPTION_READ_FAULT;
1841 break;
1842 case EXCEPTION_DATATYPE_MISALIGNMENT:
1843 /* FIXME: pass through exception handler first? */
1844 if (context->EFlags & 0x00040000)
1846 /* Disable AC flag, return */
1847 context->EFlags &= ~0x00040000;
1848 goto done;
1850 break;
1852 status = NtRaiseException( rec, context, TRUE );
1853 raise_status( status, rec );
1854 done:
1855 set_cpu_context( context );
1859 /**********************************************************************
1860 * raise_trap_exception
1862 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1864 NTSTATUS status;
1866 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1868 /* when single stepping can't tell whether this is a hw bp or a
1869 * single step interrupt. try to avoid as much overhead as possible
1870 * and only do a server call if there is any hw bp enabled. */
1872 if( !(context->EFlags & 0x100) || (ntdll_get_thread_data()->dr7 & 0xff) )
1874 /* (possible) hardware breakpoint, fetch the debug registers */
1875 DWORD saved_flags = context->ContextFlags;
1876 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1877 NtGetContextThread(GetCurrentThread(), context);
1878 context->ContextFlags |= saved_flags; /* restore flags */
1881 context->EFlags &= ~0x100; /* clear single-step flag */
1884 status = NtRaiseException( rec, context, TRUE );
1885 raise_status( status, rec );
1889 /**********************************************************************
1890 * raise_generic_exception
1892 * Generic raise function for exceptions that don't need special treatment.
1894 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1896 NTSTATUS status;
1898 status = NtRaiseException( rec, context, TRUE );
1899 raise_status( status, rec );
1903 #ifdef __HAVE_VM86
1904 /**********************************************************************
1905 * raise_vm86_sti_exception
1907 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1909 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1910 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1912 if (ntdll_get_thread_data()->vm86_ptr)
1914 if (((char*)context->Eip >= (char*)vm86_return) &&
1915 ((char*)context->Eip <= (char*)vm86_return_end) &&
1916 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1917 /* exiting from VM86, can't throw */
1918 goto done;
1920 merge_vm86_pending_flags( rec );
1922 else if (get_vm86_teb_info()->dpmi_vif &&
1923 !wine_ldt_is_system(context->SegCs) &&
1924 !wine_ldt_is_system(context->SegSs))
1926 /* Executing DPMI code and virtual interrupts are enabled. */
1927 get_vm86_teb_info()->vm86_pending = 0;
1928 NtRaiseException( rec, context, TRUE );
1930 done:
1931 set_cpu_context( context );
1935 /**********************************************************************
1936 * usr2_handler
1938 * Handler for SIGUSR2.
1939 * We use it to signal that the running __wine_enter_vm86() should
1940 * immediately set VIP_FLAG, causing pending events to be handled
1941 * as early as possible.
1943 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1945 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1946 rec->ExceptionCode = EXCEPTION_VM86_STI;
1948 #endif /* __HAVE_VM86 */
1951 /**********************************************************************
1952 * segv_handler
1954 * Handler for SIGSEGV and related errors.
1956 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1958 WORD fs, gs;
1959 EXCEPTION_RECORD *rec;
1960 ucontext_t *context = sigcontext;
1961 void *stack = init_handler( sigcontext, &fs, &gs );
1963 /* check for page fault inside the thread stack */
1964 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1965 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1966 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1967 virtual_handle_stack_fault( siginfo->si_addr ))
1969 /* check if this was the last guard page */
1970 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1972 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1973 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1975 return;
1978 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1979 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1981 switch(get_trap_code(context))
1983 case TRAP_x86_OFLOW: /* Overflow exception */
1984 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1985 break;
1986 case TRAP_x86_BOUND: /* Bound range exception */
1987 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1988 break;
1989 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1990 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1991 break;
1992 case TRAP_x86_STKFLT: /* Stack fault */
1993 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1994 break;
1995 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1996 case TRAP_x86_PROTFLT: /* General protection fault */
1997 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1999 WORD err = get_error_code(context);
2000 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
2001 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2002 rec->NumberParameters = 2;
2003 rec->ExceptionInformation[0] = 0;
2004 /* if error contains a LDT selector, use that as fault address */
2005 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
2006 rec->ExceptionInformation[1] = err & ~7;
2007 else
2008 rec->ExceptionInformation[1] = 0xffffffff;
2010 break;
2011 case TRAP_x86_PAGEFLT: /* Page fault */
2012 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2013 rec->NumberParameters = 2;
2014 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
2015 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2016 break;
2017 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2018 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2019 break;
2020 default:
2021 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2022 /* fall through */
2023 case TRAP_x86_NMI: /* NMI interrupt */
2024 case TRAP_x86_DNA: /* Device not available exception */
2025 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2026 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2027 case TRAP_x86_MCHK: /* Machine check exception */
2028 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2029 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2030 break;
2035 /**********************************************************************
2036 * trap_handler
2038 * Handler for SIGTRAP.
2040 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2042 ucontext_t *context = sigcontext;
2043 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
2045 switch(get_trap_code(context))
2047 case TRAP_x86_TRCTRAP: /* Single-step exception */
2048 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2049 break;
2050 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2051 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2052 /* fall through */
2053 default:
2054 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2055 break;
2060 /**********************************************************************
2061 * fpe_handler
2063 * Handler for SIGFPE.
2065 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2067 CONTEXT *win_context;
2068 ucontext_t *context = sigcontext;
2069 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
2071 win_context = get_exception_context( rec );
2073 switch(get_trap_code(context))
2075 case TRAP_x86_DIVIDE: /* Division by zero exception */
2076 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2077 break;
2078 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
2079 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2080 break;
2081 case TRAP_x86_ARITHTRAP: /* Floating point exception */
2082 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2083 rec->ExceptionCode = get_fpu_code( win_context );
2084 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
2085 break;
2086 case TRAP_x86_CACHEFLT: /* SIMD exception */
2087 /* TODO:
2088 * Behaviour only tested for divide-by-zero exceptions
2089 * Check for other SIMD exceptions as well */
2090 if(siginfo->si_code != FPE_FLTDIV)
2091 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2092 siginfo->si_code);
2094 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
2095 rec->NumberParameters = 1;
2096 /* no idea what meaning is actually behind this but that's what native does */
2097 rec->ExceptionInformation[0] = 0;
2098 break;
2099 default:
2100 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2101 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2102 break;
2107 /**********************************************************************
2108 * int_handler
2110 * Handler for SIGINT.
2112 * FIXME: should not be calling external functions on the signal stack.
2114 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2116 WORD fs, gs;
2117 init_handler( sigcontext, &fs, &gs );
2118 if (!dispatch_signal(SIGINT))
2120 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2121 rec->ExceptionCode = CONTROL_C_EXIT;
2125 /**********************************************************************
2126 * abrt_handler
2128 * Handler for SIGABRT.
2130 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2132 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2133 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2134 rec->ExceptionFlags = EH_NONCONTINUABLE;
2138 /**********************************************************************
2139 * quit_handler
2141 * Handler for SIGQUIT.
2143 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2145 WORD fs, gs;
2146 init_handler( sigcontext, &fs, &gs );
2147 abort_thread(0);
2151 /**********************************************************************
2152 * usr1_handler
2154 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2156 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2158 CONTEXT context;
2159 WORD fs, gs;
2161 init_handler( sigcontext, &fs, &gs );
2162 save_context( &context, sigcontext, fs, gs );
2163 wait_suspend( &context );
2164 restore_context( &context, sigcontext );
2168 /***********************************************************************
2169 * __wine_set_signal_handler (NTDLL.@)
2171 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2173 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2174 if (handlers[sig] != NULL) return -2;
2175 handlers[sig] = wsh;
2176 return 0;
2180 /***********************************************************************
2181 * locking for LDT routines
2183 static RTL_CRITICAL_SECTION ldt_section;
2184 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
2186 0, 0, &ldt_section,
2187 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
2188 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
2190 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
2191 static sigset_t ldt_sigset;
2193 static void ldt_lock(void)
2195 sigset_t sigset;
2197 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
2198 RtlEnterCriticalSection( &ldt_section );
2199 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
2202 static void ldt_unlock(void)
2204 if (ldt_section.RecursionCount == 1)
2206 sigset_t sigset = ldt_sigset;
2207 RtlLeaveCriticalSection( &ldt_section );
2208 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
2210 else RtlLeaveCriticalSection( &ldt_section );
2214 /**********************************************************************
2215 * signal_alloc_thread
2217 NTSTATUS signal_alloc_thread( TEB **teb )
2219 static size_t sigstack_zero_bits;
2220 struct ntdll_thread_data *thread_data;
2221 struct ntdll_thread_data *parent_data = NULL;
2222 SIZE_T size;
2223 void *addr = NULL;
2224 NTSTATUS status;
2226 if (!sigstack_zero_bits)
2228 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2229 /* find the first power of two not smaller than min_size */
2230 sigstack_zero_bits = 12;
2231 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2232 signal_stack_mask = (1 << sigstack_zero_bits) - 1;
2233 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2235 else parent_data = ntdll_get_thread_data();
2237 size = signal_stack_mask + 1;
2238 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
2239 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2241 *teb = addr;
2242 (*teb)->Tib.Self = &(*teb)->Tib;
2243 (*teb)->Tib.ExceptionList = (void *)~0UL;
2244 thread_data = (struct ntdll_thread_data *)(*teb)->SpareBytes1;
2245 if (!(thread_data->fs = wine_ldt_alloc_fs()))
2247 size = 0;
2248 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
2249 status = STATUS_TOO_MANY_THREADS;
2251 if (parent_data)
2253 /* inherit debug registers from parent thread */
2254 thread_data->dr0 = parent_data->dr0;
2255 thread_data->dr1 = parent_data->dr1;
2256 thread_data->dr2 = parent_data->dr2;
2257 thread_data->dr3 = parent_data->dr3;
2258 thread_data->dr6 = parent_data->dr6;
2259 thread_data->dr7 = parent_data->dr7;
2263 return status;
2267 /**********************************************************************
2268 * signal_free_thread
2270 void signal_free_thread( TEB *teb )
2272 SIZE_T size;
2273 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
2275 if (thread_data) wine_ldt_free_fs( thread_data->fs );
2276 if (teb->DeallocationStack)
2278 size = 0;
2279 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2281 size = 0;
2282 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2286 /**********************************************************************
2287 * signal_init_thread
2289 void signal_init_thread( TEB *teb )
2291 const WORD fpu_cw = 0x27f;
2292 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
2293 LDT_ENTRY fs_entry;
2294 stack_t ss;
2296 #ifdef __APPLE__
2297 int mib[2], val = 1;
2299 mib[0] = CTL_KERN;
2300 mib[1] = KERN_THALTSTACK;
2301 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2302 #endif
2304 ss.ss_sp = (char *)teb + teb_size;
2305 ss.ss_size = signal_stack_size;
2306 ss.ss_flags = 0;
2307 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2309 wine_ldt_set_base( &fs_entry, teb );
2310 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2311 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2312 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2313 thread_data->gs = wine_get_gs();
2315 #ifdef __GNUC__
2316 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2317 #else
2318 FIXME("FPU setup not implemented for this platform.\n");
2319 #endif
2322 /**********************************************************************
2323 * signal_init_process
2325 void signal_init_process(void)
2327 struct sigaction sig_act;
2329 sig_act.sa_mask = server_block_set;
2330 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2331 #ifdef SA_ONSTACK
2332 sig_act.sa_flags |= SA_ONSTACK;
2333 #endif
2334 #ifdef __ANDROID__
2335 sig_act.sa_flags |= SA_RESTORER;
2336 sig_act.sa_restorer = rt_sigreturn;
2337 #endif
2338 sig_act.sa_sigaction = int_handler;
2339 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2340 sig_act.sa_sigaction = fpe_handler;
2341 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2342 sig_act.sa_sigaction = abrt_handler;
2343 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2344 sig_act.sa_sigaction = quit_handler;
2345 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2346 sig_act.sa_sigaction = usr1_handler;
2347 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2349 sig_act.sa_sigaction = segv_handler;
2350 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2351 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2352 #ifdef SIGBUS
2353 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2354 #endif
2356 #ifdef SIGTRAP
2357 sig_act.sa_sigaction = trap_handler;
2358 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2359 #endif
2361 #ifdef __HAVE_VM86
2362 sig_act.sa_sigaction = usr2_handler;
2363 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2364 #endif
2366 wine_ldt_init_locking( ldt_lock, ldt_unlock );
2367 return;
2369 error:
2370 perror("sigaction");
2371 exit(1);
2375 #ifdef __HAVE_VM86
2376 /**********************************************************************
2377 * __wine_enter_vm86 (NTDLL.@)
2379 * Enter vm86 mode with the specified register context.
2381 void __wine_enter_vm86( CONTEXT *context )
2383 EXCEPTION_RECORD rec;
2384 int res;
2385 struct vm86plus_struct vm86;
2387 memset( &vm86, 0, sizeof(vm86) );
2388 for (;;)
2390 restore_vm86_context( context, &vm86 );
2392 ntdll_get_thread_data()->vm86_ptr = &vm86;
2393 merge_vm86_pending_flags( &rec );
2395 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
2396 if (res < 0)
2398 errno = -res;
2399 return;
2402 save_vm86_context( context, &vm86 );
2404 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2405 rec.ExceptionRecord = NULL;
2406 rec.ExceptionAddress = (LPVOID)context->Eip;
2407 rec.NumberParameters = 0;
2409 switch(VM86_TYPE(res))
2411 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
2412 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
2413 break;
2414 case VM86_TRAP: /* return due to DOS-debugger request */
2415 switch(VM86_ARG(res))
2417 case TRAP_x86_TRCTRAP: /* Single-step exception */
2418 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
2419 break;
2420 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2421 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
2422 /* fall through */
2423 default:
2424 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
2425 break;
2427 break;
2428 case VM86_INTx: /* int3/int x instruction (ARG = x) */
2429 rec.ExceptionCode = EXCEPTION_VM86_INTx;
2430 rec.NumberParameters = 1;
2431 rec.ExceptionInformation[0] = VM86_ARG(res);
2432 break;
2433 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
2434 context->EFlags |= VIF_FLAG;
2435 context->EFlags &= ~VIP_FLAG;
2436 get_vm86_teb_info()->vm86_pending = 0;
2437 rec.ExceptionCode = EXCEPTION_VM86_STI;
2438 break;
2439 case VM86_PICRETURN: /* return due to pending PIC request */
2440 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
2441 break;
2442 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
2443 default:
2444 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
2445 continue;
2447 raise_exception( &rec, context, TRUE );
2451 #else /* __HAVE_VM86 */
2452 /**********************************************************************
2453 * __wine_enter_vm86 (NTDLL.@)
2455 void __wine_enter_vm86( CONTEXT *context )
2457 MESSAGE("vm86 mode not supported on this platform\n");
2459 #endif /* __HAVE_VM86 */
2462 /*******************************************************************
2463 * RtlUnwind (NTDLL.@)
2465 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2466 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2468 EXCEPTION_RECORD record;
2469 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2470 DWORD res;
2472 context->Eax = (DWORD)retval;
2474 /* build an exception record, if we do not have one */
2475 if (!pRecord)
2477 record.ExceptionCode = STATUS_UNWIND;
2478 record.ExceptionFlags = 0;
2479 record.ExceptionRecord = NULL;
2480 record.ExceptionAddress = (void *)context->Eip;
2481 record.NumberParameters = 0;
2482 pRecord = &record;
2485 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2487 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2489 /* get chain of exception frames */
2490 frame = NtCurrentTeb()->Tib.ExceptionList;
2491 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2493 /* Check frame address */
2494 if (pEndFrame && (frame > pEndFrame))
2495 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2497 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, pRecord );
2499 /* Call handler */
2500 TRACE( "calling handler at %p code=%x flags=%x\n",
2501 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2502 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2503 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2505 switch(res)
2507 case ExceptionContinueSearch:
2508 break;
2509 case ExceptionCollidedUnwind:
2510 frame = dispatch;
2511 break;
2512 default:
2513 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2514 break;
2516 frame = __wine_pop_frame( frame );
2519 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2522 /*******************************************************************
2523 * NtRaiseException (NTDLL.@)
2525 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2527 NTSTATUS status = raise_exception( rec, context, first_chance );
2528 if (status == STATUS_SUCCESS)
2530 set_debug_registers( context );
2531 set_cpu_context( context );
2533 return status;
2537 /***********************************************************************
2538 * RtlRaiseException (NTDLL.@)
2540 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2542 NTSTATUS status;
2544 rec->ExceptionAddress = (void *)context->Eip;
2545 status = raise_exception( rec, context, TRUE );
2546 if (status != STATUS_SUCCESS) raise_status( status, rec );
2548 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2551 /*************************************************************************
2552 * RtlCaptureStackBackTrace (NTDLL.@)
2554 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2556 CONTEXT context;
2557 ULONG i;
2558 ULONG *frame;
2560 RtlCaptureContext( &context );
2561 if (hash) *hash = 0;
2562 frame = (ULONG *)context.Ebp;
2564 while (skip--)
2566 if (!is_valid_frame( frame )) return 0;
2567 frame = (ULONG *)*frame;
2570 for (i = 0; i < count; i++)
2572 if (!is_valid_frame( frame )) break;
2573 buffer[i] = (void *)frame[1];
2574 if (hash) *hash += frame[1];
2575 frame = (ULONG *)*frame;
2577 return i;
2581 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
2582 __ASM_GLOBAL_FUNC( call_thread_entry_point,
2583 "pushl %ebp\n\t"
2584 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2585 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2586 "movl %esp,%ebp\n\t"
2587 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2588 "pushl %ebx\n\t"
2589 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2590 "pushl %esi\n\t"
2591 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2592 "pushl %edi\n\t"
2593 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2594 "pushl %ebp\n\t"
2595 "pushl 12(%ebp)\n\t"
2596 "pushl 8(%ebp)\n\t"
2597 "call " __ASM_NAME("call_thread_func") );
2599 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
2600 __ASM_GLOBAL_FUNC( call_thread_exit_func,
2601 "movl 4(%esp),%eax\n\t"
2602 "movl 8(%esp),%ecx\n\t"
2603 "movl 12(%esp),%ebp\n\t"
2604 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2605 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2606 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2607 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2608 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2609 "leal -20(%ebp),%esp\n\t"
2610 "pushl %eax\n\t"
2611 "call *%ecx" );
2613 /* wrapper for apps that don't declare the thread function correctly */
2614 extern void DECLSPEC_NORETURN call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry, void *arg );
2615 __ASM_GLOBAL_FUNC(call_thread_func_wrapper,
2616 "pushl %ebp\n\t"
2617 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2618 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2619 "movl %esp,%ebp\n\t"
2620 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2621 "subl $4,%esp\n\t"
2622 "pushl 12(%ebp)\n\t"
2623 "call *8(%ebp)\n\t"
2624 "leal -4(%ebp),%esp\n\t"
2625 "pushl %eax\n\t"
2626 "call " __ASM_NAME("exit_thread") "\n\t"
2627 "int $3" )
2629 /***********************************************************************
2630 * call_thread_func
2632 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
2634 ntdll_get_thread_data()->exit_frame = frame;
2635 __TRY
2637 call_thread_func_wrapper( entry, arg );
2639 __EXCEPT(unhandled_exception_filter)
2641 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2643 __ENDTRY
2644 abort(); /* should not be reached */
2647 /***********************************************************************
2648 * RtlExitUserThread (NTDLL.@)
2650 void WINAPI RtlExitUserThread( ULONG status )
2652 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
2653 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
2656 /***********************************************************************
2657 * abort_thread
2659 void abort_thread( int status )
2661 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
2662 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
2665 /**********************************************************************
2666 * DbgBreakPoint (NTDLL.@)
2668 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2670 /**********************************************************************
2671 * DbgUserBreakPoint (NTDLL.@)
2673 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2675 /**********************************************************************
2676 * NtCurrentTeb (NTDLL.@)
2678 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2681 /**************************************************************************
2682 * _chkstk (NTDLL.@)
2684 __ASM_STDCALL_FUNC( _chkstk, 0,
2685 "negl %eax\n\t"
2686 "addl %esp,%eax\n\t"
2687 "xchgl %esp,%eax\n\t"
2688 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2689 "movl %eax,0(%esp)\n\t"
2690 "ret" )
2692 /**************************************************************************
2693 * _alloca_probe (NTDLL.@)
2695 __ASM_STDCALL_FUNC( _alloca_probe, 0,
2696 "negl %eax\n\t"
2697 "addl %esp,%eax\n\t"
2698 "xchgl %esp,%eax\n\t"
2699 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2700 "movl %eax,0(%esp)\n\t"
2701 "ret" )
2704 /**********************************************************************
2705 * EXC_CallHandler (internal)
2707 * Some exception handlers depend on EBP to have a fixed position relative to
2708 * the exception frame.
2709 * Shrinker depends on (*1) doing what it does,
2710 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2711 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2712 * function calling the handler having only 5 parameters (*4).
2714 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2715 "pushl %ebp\n\t"
2716 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2717 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2718 "movl %esp,%ebp\n\t"
2719 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2720 "pushl %ebx\n\t"
2721 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2722 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2723 "pushl 24(%ebp)\n\t"
2724 "pushl 20(%ebp)\n\t"
2725 "pushl 16(%ebp)\n\t"
2726 "pushl 12(%ebp)\n\t"
2727 "pushl 8(%ebp)\n\t"
2728 "call " __ASM_NAME("call_exception_handler") "\n\t"
2729 "popl %ebx\n\t"
2730 __ASM_CFI(".cfi_same_value %ebx\n\t")
2731 "leave\n"
2732 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2733 __ASM_CFI(".cfi_same_value %ebp\n\t")
2734 "ret" )
2735 __ASM_GLOBAL_FUNC(call_exception_handler,
2736 "pushl %ebp\n\t"
2737 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2738 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2739 "movl %esp,%ebp\n\t"
2740 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2741 "subl $12,%esp\n\t"
2742 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2743 "pushl %edx\n\t" /* handler be handled by... */
2744 ".byte 0x64\n\t"
2745 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2746 ".byte 0x64\n\t"
2747 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2748 "pushl 20(%ebp)\n\t"
2749 "pushl 16(%ebp)\n\t"
2750 "pushl 12(%ebp)\n\t"
2751 "pushl 8(%ebp)\n\t"
2752 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2753 "call *%ecx\n\t" /* call handler. (*2) */
2754 ".byte 0x64\n\t"
2755 "movl (0), %esp\n\t" /* restore previous... (*3) */
2756 ".byte 0x64\n\t"
2757 "popl (0)\n\t" /* exception frame. */
2758 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2759 "popl %ebp\n\t"
2760 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2761 __ASM_CFI(".cfi_same_value %ebp\n\t")
2762 "ret $20" ) /* (*4) */
2764 #endif /* __i386__ */