ntdll/tests: Clarify ok() conditions (PVS-Studio).
[wine/multimedia.git] / dlls / ntdll / signal_i386.c
blob13df4bb75204f69c0194762366138ce4ca1b1f0e
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)->uc_mcontext.mc_eax)
269 #define EBX_sig(context) ((context)->uc_mcontext.mc_ebx)
270 #define ECX_sig(context) ((context)->uc_mcontext.mc_ecx)
271 #define EDX_sig(context) ((context)->uc_mcontext.mc_edx)
272 #define ESI_sig(context) ((context)->uc_mcontext.mc_esi)
273 #define EDI_sig(context) ((context)->uc_mcontext.mc_edi)
274 #define EBP_sig(context) ((context)->uc_mcontext.mc_ebp)
276 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
277 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
278 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
279 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
280 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
281 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
283 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
284 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
285 #define EFL_sig(context) ((context)->uc_mcontext.mc_eflags)
287 #define EIP_sig(context) ((context)->uc_mcontext.mc_eip)
288 #define ESP_sig(context) ((context)->uc_mcontext.mc_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 union atl_thunk
1621 struct
1623 DWORD movl; /* movl this,4(%esp) */
1624 DWORD this;
1625 BYTE jmp; /* jmp func */
1626 int func;
1627 } t1;
1628 struct
1630 BYTE movl; /* movl this,ecx */
1631 DWORD this;
1632 BYTE jmp; /* jmp func */
1633 int func;
1634 } t2;
1635 struct
1637 BYTE movl1; /* movl this,edx */
1638 DWORD this;
1639 BYTE movl2; /* movl func,ecx */
1640 DWORD func;
1641 WORD jmp; /* jmp ecx */
1642 } t3;
1643 struct
1645 BYTE movl1; /* movl this,ecx */
1646 DWORD this;
1647 BYTE movl2; /* movl func,eax */
1648 DWORD func;
1649 WORD jmp; /* jmp eax */
1650 } t4;
1651 struct
1653 DWORD inst1; /* pop ecx
1654 * pop eax
1655 * push ecx
1656 * jmp 4(%eax) */
1657 WORD inst2;
1658 } t5;
1660 #include "poppack.h"
1662 /**********************************************************************
1663 * check_atl_thunk
1665 * Check if code destination is an ATL thunk, and emulate it if so.
1667 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1669 const union atl_thunk *thunk = (const union atl_thunk *)rec->ExceptionInformation[1];
1670 union atl_thunk thunk_copy;
1671 SIZE_T thunk_len;
1673 thunk_len = virtual_uninterrupted_read_memory( thunk, &thunk_copy, sizeof(*thunk) );
1674 if (!thunk_len) return FALSE;
1676 if (thunk_len >= sizeof(thunk_copy.t1) && thunk_copy.t1.movl == 0x042444c7 &&
1677 thunk_copy.t1.jmp == 0xe9)
1679 if (virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1,
1680 &thunk_copy.t1.this, sizeof(DWORD) ) == sizeof(DWORD))
1682 context->Eip = (DWORD_PTR)(&thunk->t1.func + 1) + thunk_copy.t1.func;
1683 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1684 thunk, context->Eip, thunk_copy.t1.this );
1685 return TRUE;
1688 else if (thunk_len >= sizeof(thunk_copy.t2) && thunk_copy.t2.movl == 0xb9 &&
1689 thunk_copy.t2.jmp == 0xe9)
1691 context->Ecx = thunk_copy.t2.this;
1692 context->Eip = (DWORD_PTR)(&thunk->t2.func + 1) + thunk_copy.t2.func;
1693 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1694 thunk, context->Eip, context->Ecx );
1695 return TRUE;
1697 else if (thunk_len >= sizeof(thunk_copy.t3) && thunk_copy.t3.movl1 == 0xba &&
1698 thunk_copy.t3.movl2 == 0xb9 &&
1699 thunk_copy.t3.jmp == 0xe1ff)
1701 context->Edx = thunk_copy.t3.this;
1702 context->Ecx = thunk_copy.t3.func;
1703 context->Eip = thunk_copy.t3.func;
1704 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1705 thunk, context->Eip, context->Ecx, context->Edx );
1706 return TRUE;
1708 else if (thunk_len >= sizeof(thunk_copy.t4) && thunk_copy.t4.movl1 == 0xb9 &&
1709 thunk_copy.t4.movl2 == 0xb8 &&
1710 thunk_copy.t4.jmp == 0xe0ff)
1712 context->Ecx = thunk_copy.t4.this;
1713 context->Eax = thunk_copy.t4.func;
1714 context->Eip = thunk_copy.t4.func;
1715 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1716 thunk, context->Eip, context->Eax, context->Ecx );
1717 return TRUE;
1719 else if (thunk_len >= sizeof(thunk_copy.t5) && thunk_copy.t5.inst1 == 0xff515859 &&
1720 thunk_copy.t5.inst2 == 0x0460)
1722 DWORD func, stack[2];
1723 if (virtual_uninterrupted_read_memory( (DWORD *)context->Esp,
1724 stack, sizeof(stack) ) == sizeof(stack) &&
1725 virtual_uninterrupted_read_memory( (DWORD *)stack[1] + 1,
1726 &func, sizeof(DWORD) ) == sizeof(DWORD) &&
1727 virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1,
1728 &stack[0], sizeof(stack[0]) ) == sizeof(stack[0]))
1730 context->Ecx = stack[0];
1731 context->Eax = stack[1];
1732 context->Esp = context->Esp + sizeof(DWORD);
1733 context->Eip = func;
1734 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1735 thunk, context->Eip, context->Eax, context->Ecx, context->Esp );
1736 return TRUE;
1740 return FALSE;
1744 /***********************************************************************
1745 * setup_exception_record
1747 * Setup the exception record and context on the thread stack.
1749 static EXCEPTION_RECORD *setup_exception_record( ucontext_t *sigcontext, void *stack_ptr,
1750 WORD fs, WORD gs, raise_func func )
1752 struct stack_layout
1754 void *ret_addr; /* return address from raise_func */
1755 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1756 CONTEXT *context_ptr; /* second arg for raise_func */
1757 CONTEXT context;
1758 EXCEPTION_RECORD rec;
1759 DWORD ebp;
1760 DWORD eip;
1761 } *stack = stack_ptr;
1762 DWORD exception_code = 0;
1764 /* stack sanity checks */
1766 if ((char *)stack >= (char *)get_signal_stack() &&
1767 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1769 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1770 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1771 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1772 NtCurrentTeb()->Tib.StackBase );
1773 abort_thread(1);
1776 if (stack - 1 > stack || /* check for overflow in subtraction */
1777 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1778 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1780 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1781 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1782 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1783 NtCurrentTeb()->Tib.StackBase );
1785 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1787 /* stack overflow on last page, unrecoverable */
1788 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1789 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1790 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1791 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1792 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1793 abort_thread(1);
1795 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1797 /* stack access below stack limit, may be recoverable */
1798 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1799 else
1801 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1802 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1803 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1804 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1805 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1806 abort_thread(1);
1810 stack--; /* push the stack_layout structure */
1811 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1812 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1813 #elif defined(VALGRIND_MAKE_WRITABLE)
1814 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1815 #endif
1816 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1817 stack->rec_ptr = &stack->rec;
1818 stack->context_ptr = &stack->context;
1820 stack->rec.ExceptionRecord = NULL;
1821 stack->rec.ExceptionCode = exception_code;
1822 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1823 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1824 stack->rec.NumberParameters = 0;
1826 save_context( &stack->context, sigcontext, fs, gs );
1828 /* now modify the sigcontext to return to the raise function */
1829 ESP_sig(sigcontext) = (DWORD)stack;
1830 EIP_sig(sigcontext) = (DWORD)func;
1831 /* clear single-step, direction, and align check flag */
1832 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1833 CS_sig(sigcontext) = wine_get_cs();
1834 DS_sig(sigcontext) = wine_get_ds();
1835 ES_sig(sigcontext) = wine_get_es();
1836 FS_sig(sigcontext) = wine_get_fs();
1837 GS_sig(sigcontext) = wine_get_gs();
1838 SS_sig(sigcontext) = wine_get_ss();
1840 return stack->rec_ptr;
1844 /***********************************************************************
1845 * setup_exception
1847 * Setup a proper stack frame for the raise function, and modify the
1848 * sigcontext so that the return from the signal handler will call
1849 * the raise function.
1851 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1853 WORD fs, gs;
1854 void *stack = init_handler( sigcontext, &fs, &gs );
1856 return setup_exception_record( sigcontext, stack, fs, gs, func );
1860 /***********************************************************************
1861 * get_exception_context
1863 * Get a pointer to the context built by setup_exception.
1865 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1867 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1871 /**********************************************************************
1872 * get_fpu_code
1874 * Get the FPU exception code from the FPU status.
1876 static inline DWORD get_fpu_code( const CONTEXT *context )
1878 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1880 if (status & 0x01) /* IE */
1882 if (status & 0x40) /* SF */
1883 return EXCEPTION_FLT_STACK_CHECK;
1884 else
1885 return EXCEPTION_FLT_INVALID_OPERATION;
1887 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1888 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1889 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1890 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1891 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1892 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1896 /**********************************************************************
1897 * raise_segv_exception
1899 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1901 NTSTATUS status;
1903 switch(rec->ExceptionCode)
1905 case EXCEPTION_ACCESS_VIOLATION:
1906 if (rec->NumberParameters == 2)
1908 if (rec->ExceptionInformation[1] == 0xffffffff && check_invalid_gs( context ))
1909 goto done;
1910 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1911 rec->ExceptionInformation[0] )))
1912 goto done;
1913 if (rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
1914 rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT)
1916 ULONG flags;
1917 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
1918 &flags, sizeof(flags), NULL );
1920 if (!(flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION) && check_atl_thunk( rec, context ))
1921 goto done;
1923 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1924 if (!(flags & MEM_EXECUTE_OPTION_DISABLE))
1925 rec->ExceptionInformation[0] = EXCEPTION_READ_FAULT;
1928 break;
1929 case EXCEPTION_DATATYPE_MISALIGNMENT:
1930 /* FIXME: pass through exception handler first? */
1931 if (context->EFlags & 0x00040000)
1933 /* Disable AC flag, return */
1934 context->EFlags &= ~0x00040000;
1935 goto done;
1937 break;
1939 status = NtRaiseException( rec, context, TRUE );
1940 raise_status( status, rec );
1941 done:
1942 set_cpu_context( context );
1946 /**********************************************************************
1947 * raise_trap_exception
1949 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1951 NTSTATUS status;
1953 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1955 /* when single stepping can't tell whether this is a hw bp or a
1956 * single step interrupt. try to avoid as much overhead as possible
1957 * and only do a server call if there is any hw bp enabled. */
1959 if( !(context->EFlags & 0x100) || (ntdll_get_thread_data()->dr7 & 0xff) )
1961 /* (possible) hardware breakpoint, fetch the debug registers */
1962 DWORD saved_flags = context->ContextFlags;
1963 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1964 NtGetContextThread(GetCurrentThread(), context);
1965 context->ContextFlags |= saved_flags; /* restore flags */
1968 context->EFlags &= ~0x100; /* clear single-step flag */
1971 status = NtRaiseException( rec, context, TRUE );
1972 raise_status( status, rec );
1976 /**********************************************************************
1977 * raise_generic_exception
1979 * Generic raise function for exceptions that don't need special treatment.
1981 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1983 NTSTATUS status;
1985 status = NtRaiseException( rec, context, TRUE );
1986 raise_status( status, rec );
1990 #ifdef __HAVE_VM86
1991 /**********************************************************************
1992 * raise_vm86_sti_exception
1994 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1996 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1997 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1999 if (ntdll_get_thread_data()->vm86_ptr)
2001 if (((char*)context->Eip >= (char*)vm86_return) &&
2002 ((char*)context->Eip <= (char*)vm86_return_end) &&
2003 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
2004 /* exiting from VM86, can't throw */
2005 goto done;
2007 merge_vm86_pending_flags( rec );
2009 else if (get_vm86_teb_info()->dpmi_vif &&
2010 !wine_ldt_is_system(context->SegCs) &&
2011 !wine_ldt_is_system(context->SegSs))
2013 /* Executing DPMI code and virtual interrupts are enabled. */
2014 get_vm86_teb_info()->vm86_pending = 0;
2015 NtRaiseException( rec, context, TRUE );
2017 done:
2018 set_cpu_context( context );
2022 /**********************************************************************
2023 * usr2_handler
2025 * Handler for SIGUSR2.
2026 * We use it to signal that the running __wine_enter_vm86() should
2027 * immediately set VIP_FLAG, causing pending events to be handled
2028 * as early as possible.
2030 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2032 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
2033 rec->ExceptionCode = EXCEPTION_VM86_STI;
2035 #endif /* __HAVE_VM86 */
2038 /**********************************************************************
2039 * segv_handler
2041 * Handler for SIGSEGV and related errors.
2043 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2045 WORD fs, gs;
2046 EXCEPTION_RECORD *rec;
2047 ucontext_t *context = sigcontext;
2048 void *stack = init_handler( sigcontext, &fs, &gs );
2050 /* check for page fault inside the thread stack */
2051 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
2052 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
2053 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
2054 virtual_handle_stack_fault( siginfo->si_addr ))
2056 /* check if this was the last guard page */
2057 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
2059 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2060 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2062 return;
2065 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2066 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
2068 switch(get_trap_code(context))
2070 case TRAP_x86_OFLOW: /* Overflow exception */
2071 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2072 break;
2073 case TRAP_x86_BOUND: /* Bound range exception */
2074 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2075 break;
2076 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2077 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2078 break;
2079 case TRAP_x86_STKFLT: /* Stack fault */
2080 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2081 break;
2082 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2083 case TRAP_x86_PROTFLT: /* General protection fault */
2084 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2086 WORD err = get_error_code(context);
2087 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
2088 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2089 rec->NumberParameters = 2;
2090 rec->ExceptionInformation[0] = 0;
2091 /* if error contains a LDT selector, use that as fault address */
2092 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
2093 rec->ExceptionInformation[1] = err & ~7;
2094 else
2095 rec->ExceptionInformation[1] = 0xffffffff;
2097 break;
2098 case TRAP_x86_PAGEFLT: /* Page fault */
2099 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2100 rec->NumberParameters = 2;
2101 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
2102 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2103 break;
2104 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2105 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2106 break;
2107 default:
2108 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2109 /* fall through */
2110 case TRAP_x86_NMI: /* NMI interrupt */
2111 case TRAP_x86_DNA: /* Device not available exception */
2112 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2113 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2114 case TRAP_x86_MCHK: /* Machine check exception */
2115 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2116 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2117 break;
2122 /**********************************************************************
2123 * trap_handler
2125 * Handler for SIGTRAP.
2127 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2129 ucontext_t *context = sigcontext;
2130 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
2132 switch(get_trap_code(context))
2134 case TRAP_x86_TRCTRAP: /* Single-step exception */
2135 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2136 break;
2137 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2138 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2139 /* fall through */
2140 default:
2141 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2142 break;
2147 /**********************************************************************
2148 * fpe_handler
2150 * Handler for SIGFPE.
2152 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2154 CONTEXT *win_context;
2155 ucontext_t *context = sigcontext;
2156 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
2158 win_context = get_exception_context( rec );
2160 switch(get_trap_code(context))
2162 case TRAP_x86_DIVIDE: /* Division by zero exception */
2163 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2164 break;
2165 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
2166 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2167 break;
2168 case TRAP_x86_ARITHTRAP: /* Floating point exception */
2169 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2170 rec->ExceptionCode = get_fpu_code( win_context );
2171 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
2172 break;
2173 case TRAP_x86_CACHEFLT: /* SIMD exception */
2174 /* TODO:
2175 * Behaviour only tested for divide-by-zero exceptions
2176 * Check for other SIMD exceptions as well */
2177 if(siginfo->si_code != FPE_FLTDIV)
2178 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2179 siginfo->si_code);
2181 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
2182 rec->NumberParameters = 1;
2183 /* no idea what meaning is actually behind this but that's what native does */
2184 rec->ExceptionInformation[0] = 0;
2185 break;
2186 default:
2187 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2188 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2189 break;
2194 /**********************************************************************
2195 * int_handler
2197 * Handler for SIGINT.
2199 * FIXME: should not be calling external functions on the signal stack.
2201 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2203 WORD fs, gs;
2204 init_handler( sigcontext, &fs, &gs );
2205 if (!dispatch_signal(SIGINT))
2207 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2208 rec->ExceptionCode = CONTROL_C_EXIT;
2212 /**********************************************************************
2213 * abrt_handler
2215 * Handler for SIGABRT.
2217 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2219 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2220 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2221 rec->ExceptionFlags = EH_NONCONTINUABLE;
2225 /**********************************************************************
2226 * quit_handler
2228 * Handler for SIGQUIT.
2230 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2232 WORD fs, gs;
2233 init_handler( sigcontext, &fs, &gs );
2234 abort_thread(0);
2238 /**********************************************************************
2239 * usr1_handler
2241 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2243 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2245 CONTEXT context;
2246 WORD fs, gs;
2248 init_handler( sigcontext, &fs, &gs );
2249 save_context( &context, sigcontext, fs, gs );
2250 wait_suspend( &context );
2251 restore_context( &context, sigcontext );
2255 /***********************************************************************
2256 * __wine_set_signal_handler (NTDLL.@)
2258 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2260 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2261 if (handlers[sig] != NULL) return -2;
2262 handlers[sig] = wsh;
2263 return 0;
2267 /***********************************************************************
2268 * locking for LDT routines
2270 static RTL_CRITICAL_SECTION ldt_section;
2271 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
2273 0, 0, &ldt_section,
2274 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
2275 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
2277 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
2278 static sigset_t ldt_sigset;
2280 static void ldt_lock(void)
2282 sigset_t sigset;
2284 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
2285 RtlEnterCriticalSection( &ldt_section );
2286 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
2289 static void ldt_unlock(void)
2291 if (ldt_section.RecursionCount == 1)
2293 sigset_t sigset = ldt_sigset;
2294 RtlLeaveCriticalSection( &ldt_section );
2295 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
2297 else RtlLeaveCriticalSection( &ldt_section );
2301 /**********************************************************************
2302 * signal_alloc_thread
2304 NTSTATUS signal_alloc_thread( TEB **teb )
2306 static size_t sigstack_zero_bits;
2307 struct ntdll_thread_data *thread_data;
2308 struct ntdll_thread_data *parent_data = NULL;
2309 SIZE_T size;
2310 void *addr = NULL;
2311 NTSTATUS status;
2313 if (!sigstack_zero_bits)
2315 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2316 /* find the first power of two not smaller than min_size */
2317 sigstack_zero_bits = 12;
2318 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2319 signal_stack_mask = (1 << sigstack_zero_bits) - 1;
2320 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2322 else parent_data = ntdll_get_thread_data();
2324 size = signal_stack_mask + 1;
2325 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
2326 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2328 *teb = addr;
2329 (*teb)->Tib.Self = &(*teb)->Tib;
2330 (*teb)->Tib.ExceptionList = (void *)~0UL;
2331 thread_data = (struct ntdll_thread_data *)(*teb)->SpareBytes1;
2332 if (!(thread_data->fs = wine_ldt_alloc_fs()))
2334 size = 0;
2335 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
2336 status = STATUS_TOO_MANY_THREADS;
2338 if (parent_data)
2340 /* inherit debug registers from parent thread */
2341 thread_data->dr0 = parent_data->dr0;
2342 thread_data->dr1 = parent_data->dr1;
2343 thread_data->dr2 = parent_data->dr2;
2344 thread_data->dr3 = parent_data->dr3;
2345 thread_data->dr6 = parent_data->dr6;
2346 thread_data->dr7 = parent_data->dr7;
2350 return status;
2354 /**********************************************************************
2355 * signal_free_thread
2357 void signal_free_thread( TEB *teb )
2359 SIZE_T size;
2360 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
2362 if (thread_data) wine_ldt_free_fs( thread_data->fs );
2363 if (teb->DeallocationStack)
2365 size = 0;
2366 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2368 size = 0;
2369 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2373 /**********************************************************************
2374 * signal_init_thread
2376 void signal_init_thread( TEB *teb )
2378 const WORD fpu_cw = 0x27f;
2379 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
2380 LDT_ENTRY fs_entry;
2381 stack_t ss;
2383 #ifdef __APPLE__
2384 int mib[2], val = 1;
2386 mib[0] = CTL_KERN;
2387 mib[1] = KERN_THALTSTACK;
2388 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2389 #endif
2391 ss.ss_sp = (char *)teb + teb_size;
2392 ss.ss_size = signal_stack_size;
2393 ss.ss_flags = 0;
2394 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2396 wine_ldt_set_base( &fs_entry, teb );
2397 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2398 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2399 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2400 thread_data->gs = wine_get_gs();
2402 #ifdef __GNUC__
2403 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2404 #else
2405 FIXME("FPU setup not implemented for this platform.\n");
2406 #endif
2409 /**********************************************************************
2410 * signal_init_process
2412 void signal_init_process(void)
2414 struct sigaction sig_act;
2416 sig_act.sa_mask = server_block_set;
2417 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2418 #ifdef SA_ONSTACK
2419 sig_act.sa_flags |= SA_ONSTACK;
2420 #endif
2421 #ifdef __ANDROID__
2422 sig_act.sa_flags |= SA_RESTORER;
2423 sig_act.sa_restorer = rt_sigreturn;
2424 #endif
2425 sig_act.sa_sigaction = int_handler;
2426 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2427 sig_act.sa_sigaction = fpe_handler;
2428 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2429 sig_act.sa_sigaction = abrt_handler;
2430 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2431 sig_act.sa_sigaction = quit_handler;
2432 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2433 sig_act.sa_sigaction = usr1_handler;
2434 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2436 sig_act.sa_sigaction = segv_handler;
2437 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2438 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2439 #ifdef SIGBUS
2440 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2441 #endif
2443 #ifdef SIGTRAP
2444 sig_act.sa_sigaction = trap_handler;
2445 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2446 #endif
2448 #ifdef __HAVE_VM86
2449 sig_act.sa_sigaction = usr2_handler;
2450 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2451 #endif
2453 wine_ldt_init_locking( ldt_lock, ldt_unlock );
2454 return;
2456 error:
2457 perror("sigaction");
2458 exit(1);
2462 #ifdef __HAVE_VM86
2463 /**********************************************************************
2464 * __wine_enter_vm86 (NTDLL.@)
2466 * Enter vm86 mode with the specified register context.
2468 void __wine_enter_vm86( CONTEXT *context )
2470 EXCEPTION_RECORD rec;
2471 int res;
2472 struct vm86plus_struct vm86;
2474 memset( &vm86, 0, sizeof(vm86) );
2475 for (;;)
2477 restore_vm86_context( context, &vm86 );
2479 ntdll_get_thread_data()->vm86_ptr = &vm86;
2480 merge_vm86_pending_flags( &rec );
2482 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
2483 if (res < 0)
2485 errno = -res;
2486 return;
2489 save_vm86_context( context, &vm86 );
2491 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2492 rec.ExceptionRecord = NULL;
2493 rec.ExceptionAddress = (LPVOID)context->Eip;
2494 rec.NumberParameters = 0;
2496 switch(VM86_TYPE(res))
2498 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
2499 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
2500 break;
2501 case VM86_TRAP: /* return due to DOS-debugger request */
2502 switch(VM86_ARG(res))
2504 case TRAP_x86_TRCTRAP: /* Single-step exception */
2505 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
2506 break;
2507 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2508 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
2509 /* fall through */
2510 default:
2511 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
2512 break;
2514 break;
2515 case VM86_INTx: /* int3/int x instruction (ARG = x) */
2516 rec.ExceptionCode = EXCEPTION_VM86_INTx;
2517 rec.NumberParameters = 1;
2518 rec.ExceptionInformation[0] = VM86_ARG(res);
2519 break;
2520 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
2521 context->EFlags |= VIF_FLAG;
2522 context->EFlags &= ~VIP_FLAG;
2523 get_vm86_teb_info()->vm86_pending = 0;
2524 rec.ExceptionCode = EXCEPTION_VM86_STI;
2525 break;
2526 case VM86_PICRETURN: /* return due to pending PIC request */
2527 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
2528 break;
2529 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
2530 default:
2531 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
2532 continue;
2534 raise_exception( &rec, context, TRUE );
2538 #else /* __HAVE_VM86 */
2539 /**********************************************************************
2540 * __wine_enter_vm86 (NTDLL.@)
2542 void __wine_enter_vm86( CONTEXT *context )
2544 MESSAGE("vm86 mode not supported on this platform\n");
2546 #endif /* __HAVE_VM86 */
2549 /*******************************************************************
2550 * RtlUnwind (NTDLL.@)
2552 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2553 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2555 EXCEPTION_RECORD record;
2556 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2557 DWORD res;
2559 context->Eax = (DWORD)retval;
2561 /* build an exception record, if we do not have one */
2562 if (!pRecord)
2564 record.ExceptionCode = STATUS_UNWIND;
2565 record.ExceptionFlags = 0;
2566 record.ExceptionRecord = NULL;
2567 record.ExceptionAddress = (void *)context->Eip;
2568 record.NumberParameters = 0;
2569 pRecord = &record;
2572 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2574 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2576 /* get chain of exception frames */
2577 frame = NtCurrentTeb()->Tib.ExceptionList;
2578 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2580 /* Check frame address */
2581 if (pEndFrame && (frame > pEndFrame))
2582 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2584 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, pRecord );
2586 /* Call handler */
2587 TRACE( "calling handler at %p code=%x flags=%x\n",
2588 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2589 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2590 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2592 switch(res)
2594 case ExceptionContinueSearch:
2595 break;
2596 case ExceptionCollidedUnwind:
2597 frame = dispatch;
2598 break;
2599 default:
2600 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2601 break;
2603 frame = __wine_pop_frame( frame );
2606 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2609 /*******************************************************************
2610 * NtRaiseException (NTDLL.@)
2612 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2614 NTSTATUS status = raise_exception( rec, context, first_chance );
2615 if (status == STATUS_SUCCESS)
2617 set_debug_registers( context );
2618 set_cpu_context( context );
2620 return status;
2624 /***********************************************************************
2625 * RtlRaiseException (NTDLL.@)
2627 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2629 NTSTATUS status;
2631 rec->ExceptionAddress = (void *)context->Eip;
2632 status = raise_exception( rec, context, TRUE );
2633 if (status != STATUS_SUCCESS) raise_status( status, rec );
2635 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2638 /*************************************************************************
2639 * RtlCaptureStackBackTrace (NTDLL.@)
2641 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2643 CONTEXT context;
2644 ULONG i;
2645 ULONG *frame;
2647 RtlCaptureContext( &context );
2648 if (hash) *hash = 0;
2649 frame = (ULONG *)context.Ebp;
2651 while (skip--)
2653 if (!is_valid_frame( frame )) return 0;
2654 frame = (ULONG *)*frame;
2657 for (i = 0; i < count; i++)
2659 if (!is_valid_frame( frame )) break;
2660 buffer[i] = (void *)frame[1];
2661 if (hash) *hash += frame[1];
2662 frame = (ULONG *)*frame;
2664 return i;
2668 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
2669 __ASM_GLOBAL_FUNC( call_thread_entry_point,
2670 "pushl %ebp\n\t"
2671 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2672 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2673 "movl %esp,%ebp\n\t"
2674 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2675 "pushl %ebx\n\t"
2676 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2677 "pushl %esi\n\t"
2678 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2679 "pushl %edi\n\t"
2680 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2681 "pushl %ebp\n\t"
2682 "pushl 12(%ebp)\n\t"
2683 "pushl 8(%ebp)\n\t"
2684 "call " __ASM_NAME("call_thread_func") );
2686 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
2687 __ASM_GLOBAL_FUNC( call_thread_exit_func,
2688 "movl 4(%esp),%eax\n\t"
2689 "movl 8(%esp),%ecx\n\t"
2690 "movl 12(%esp),%ebp\n\t"
2691 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2692 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2693 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2694 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2695 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2696 "leal -20(%ebp),%esp\n\t"
2697 "pushl %eax\n\t"
2698 "call *%ecx" );
2700 /* wrapper for apps that don't declare the thread function correctly */
2701 extern void DECLSPEC_NORETURN call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry, void *arg );
2702 __ASM_GLOBAL_FUNC(call_thread_func_wrapper,
2703 "pushl %ebp\n\t"
2704 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2705 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2706 "movl %esp,%ebp\n\t"
2707 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2708 "subl $4,%esp\n\t"
2709 "pushl 12(%ebp)\n\t"
2710 "call *8(%ebp)\n\t"
2711 "leal -4(%ebp),%esp\n\t"
2712 "pushl %eax\n\t"
2713 "call " __ASM_NAME("exit_thread") "\n\t"
2714 "int $3" )
2716 /***********************************************************************
2717 * call_thread_func
2719 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
2721 ntdll_get_thread_data()->exit_frame = frame;
2722 __TRY
2724 call_thread_func_wrapper( entry, arg );
2726 __EXCEPT(unhandled_exception_filter)
2728 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2730 __ENDTRY
2731 abort(); /* should not be reached */
2734 /***********************************************************************
2735 * RtlExitUserThread (NTDLL.@)
2737 void WINAPI RtlExitUserThread( ULONG status )
2739 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
2740 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
2743 /***********************************************************************
2744 * abort_thread
2746 void abort_thread( int status )
2748 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
2749 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
2752 /**********************************************************************
2753 * DbgBreakPoint (NTDLL.@)
2755 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2757 /**********************************************************************
2758 * DbgUserBreakPoint (NTDLL.@)
2760 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2762 /**********************************************************************
2763 * NtCurrentTeb (NTDLL.@)
2765 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2768 /**************************************************************************
2769 * _chkstk (NTDLL.@)
2771 __ASM_STDCALL_FUNC( _chkstk, 0,
2772 "negl %eax\n\t"
2773 "addl %esp,%eax\n\t"
2774 "xchgl %esp,%eax\n\t"
2775 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2776 "movl %eax,0(%esp)\n\t"
2777 "ret" )
2779 /**************************************************************************
2780 * _alloca_probe (NTDLL.@)
2782 __ASM_STDCALL_FUNC( _alloca_probe, 0,
2783 "negl %eax\n\t"
2784 "addl %esp,%eax\n\t"
2785 "xchgl %esp,%eax\n\t"
2786 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2787 "movl %eax,0(%esp)\n\t"
2788 "ret" )
2791 /**********************************************************************
2792 * EXC_CallHandler (internal)
2794 * Some exception handlers depend on EBP to have a fixed position relative to
2795 * the exception frame.
2796 * Shrinker depends on (*1) doing what it does,
2797 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2798 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2799 * function calling the handler having only 5 parameters (*4).
2801 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2802 "pushl %ebp\n\t"
2803 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2804 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2805 "movl %esp,%ebp\n\t"
2806 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2807 "pushl %ebx\n\t"
2808 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2809 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2810 "pushl 24(%ebp)\n\t"
2811 "pushl 20(%ebp)\n\t"
2812 "pushl 16(%ebp)\n\t"
2813 "pushl 12(%ebp)\n\t"
2814 "pushl 8(%ebp)\n\t"
2815 "call " __ASM_NAME("call_exception_handler") "\n\t"
2816 "popl %ebx\n\t"
2817 __ASM_CFI(".cfi_same_value %ebx\n\t")
2818 "leave\n"
2819 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2820 __ASM_CFI(".cfi_same_value %ebp\n\t")
2821 "ret" )
2822 __ASM_GLOBAL_FUNC(call_exception_handler,
2823 "pushl %ebp\n\t"
2824 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2825 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2826 "movl %esp,%ebp\n\t"
2827 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2828 "subl $12,%esp\n\t"
2829 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2830 "pushl %edx\n\t" /* handler be handled by... */
2831 ".byte 0x64\n\t"
2832 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2833 ".byte 0x64\n\t"
2834 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2835 "pushl 20(%ebp)\n\t"
2836 "pushl 16(%ebp)\n\t"
2837 "pushl 12(%ebp)\n\t"
2838 "pushl 8(%ebp)\n\t"
2839 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2840 "call *%ecx\n\t" /* call handler. (*2) */
2841 ".byte 0x64\n\t"
2842 "movl (0), %esp\n\t" /* restore previous... (*3) */
2843 ".byte 0x64\n\t"
2844 "popl (0)\n\t" /* exception frame. */
2845 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2846 "popl %ebp\n\t"
2847 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2848 __ASM_CFI(".cfi_same_value %ebp\n\t")
2849 "ret $20" ) /* (*4) */
2851 #endif /* __i386__ */