kernel32/tests: Detect and handle a redirected stdin.
[wine.git] / dlls / ntdll / signal_i386.c
blob4038436a426601cfdfe77b839e6c1bf651f0f239
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 VIF_FLAG 0x00080000
181 #define VIP_FLAG 0x00100000
183 int vm86_enter( void **vm86_ptr );
184 void vm86_return(void);
185 void vm86_return_end(void);
186 __ASM_GLOBAL_FUNC(vm86_enter,
187 "pushl %ebp\n\t"
188 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
189 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
190 "movl %esp,%ebp\n\t"
191 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
192 "pushl %ebx\n\t"
193 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
194 "movl $166,%eax\n\t" /*SYS_vm86*/
195 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
196 "movl (%ecx),%ecx\n\t"
197 "movl $1,%ebx\n\t" /*VM86_ENTER*/
198 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
199 "pushl %fs\n\t"
200 "pushl %gs\n\t"
201 "int $0x80\n"
202 ".globl " __ASM_NAME("vm86_return") "\n\t"
203 __ASM_FUNC("vm86_return") "\n"
204 __ASM_NAME("vm86_return") ":\n\t"
205 "popl %gs\n\t"
206 "popl %fs\n\t"
207 "popl %ecx\n\t"
208 "popl %ebx\n\t"
209 __ASM_CFI(".cfi_same_value %ebx\n\t")
210 "popl %ebp\n\t"
211 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
212 __ASM_CFI(".cfi_same_value %ebp\n\t")
213 "testl %eax,%eax\n\t"
214 "jl 0f\n\t"
215 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
216 "je " __ASM_NAME("vm86_enter") "\n\t"
217 "0:\n\t"
218 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
219 "movl $0,(%ecx)\n\t"
220 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
221 __ASM_FUNC("vm86_return_end") "\n"
222 __ASM_NAME("vm86_return_end") ":\n\t"
223 "ret" )
225 #ifdef HAVE_SYS_VM86_H
226 # define __HAVE_VM86
227 #endif
229 #ifdef __ANDROID__
230 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
231 void rt_sigreturn(void);
232 __ASM_GLOBAL_FUNC( rt_sigreturn,
233 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
234 "int $0x80" );
235 #endif
237 #elif defined (__BSDI__)
239 #include <machine/frame.h>
240 typedef struct trapframe ucontext_t;
242 #define EAX_sig(context) ((context)->tf_eax)
243 #define EBX_sig(context) ((context)->tf_ebx)
244 #define ECX_sig(context) ((context)->tf_ecx)
245 #define EDX_sig(context) ((context)->tf_edx)
246 #define ESI_sig(context) ((context)->tf_esi)
247 #define EDI_sig(context) ((context)->tf_edi)
248 #define EBP_sig(context) ((context)->tf_ebp)
250 #define CS_sig(context) ((context)->tf_cs)
251 #define DS_sig(context) ((context)->tf_ds)
252 #define ES_sig(context) ((context)->tf_es)
253 #define SS_sig(context) ((context)->tf_ss)
255 #define EFL_sig(context) ((context)->tf_eflags)
257 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
258 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
260 #define FPU_sig(context) NULL /* FIXME */
261 #define FPUX_sig(context) NULL /* FIXME */
263 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
265 #include <machine/trap.h>
267 #define EAX_sig(context) ((context)->uc_mcontext.mc_eax)
268 #define EBX_sig(context) ((context)->uc_mcontext.mc_ebx)
269 #define ECX_sig(context) ((context)->uc_mcontext.mc_ecx)
270 #define EDX_sig(context) ((context)->uc_mcontext.mc_edx)
271 #define ESI_sig(context) ((context)->uc_mcontext.mc_esi)
272 #define EDI_sig(context) ((context)->uc_mcontext.mc_edi)
273 #define EBP_sig(context) ((context)->uc_mcontext.mc_ebp)
275 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
276 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
277 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
278 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
279 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
280 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
282 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
283 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
284 #define EFL_sig(context) ((context)->uc_mcontext.mc_eflags)
286 #define EIP_sig(context) ((context)->uc_mcontext.mc_eip)
287 #define ESP_sig(context) ((context)->uc_mcontext.mc_esp)
289 #define FPU_sig(context) NULL /* FIXME */
290 #define FPUX_sig(context) NULL /* FIXME */
292 #elif defined (__OpenBSD__)
294 #define EAX_sig(context) ((context)->sc_eax)
295 #define EBX_sig(context) ((context)->sc_ebx)
296 #define ECX_sig(context) ((context)->sc_ecx)
297 #define EDX_sig(context) ((context)->sc_edx)
298 #define ESI_sig(context) ((context)->sc_esi)
299 #define EDI_sig(context) ((context)->sc_edi)
300 #define EBP_sig(context) ((context)->sc_ebp)
302 #define CS_sig(context) ((context)->sc_cs)
303 #define DS_sig(context) ((context)->sc_ds)
304 #define ES_sig(context) ((context)->sc_es)
305 #define FS_sig(context) ((context)->sc_fs)
306 #define GS_sig(context) ((context)->sc_gs)
307 #define SS_sig(context) ((context)->sc_ss)
309 #define TRAP_sig(context) ((context)->sc_trapno)
310 #define ERROR_sig(context) ((context)->sc_err)
311 #define EFL_sig(context) ((context)->sc_eflags)
313 #define EIP_sig(context) ((context)->sc_eip)
314 #define ESP_sig(context) ((context)->sc_esp)
316 #define FPU_sig(context) NULL /* FIXME */
317 #define FPUX_sig(context) NULL /* FIXME */
319 #define T_MCHK T_MACHK
320 #define T_XMMFLT T_XFTRAP
322 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
324 #ifdef _SCO_DS
325 #include <sys/regset.h>
326 #define gregs regs
327 #endif
329 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
330 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
331 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
332 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
333 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
334 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
335 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
337 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
338 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
339 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
340 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
342 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
343 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
345 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
347 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
348 #ifdef UESP
349 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
350 #elif defined(R_ESP)
351 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
352 #else
353 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
354 #endif
355 #ifdef ERR
356 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
357 #endif
358 #ifdef TRAPNO
359 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
360 #endif
362 #define FPU_sig(context) NULL /* FIXME */
363 #define FPUX_sig(context) NULL /* FIXME */
365 #elif defined (__APPLE__)
367 /* work around silly renaming of struct members in OS X 10.5 */
368 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
369 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
370 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
371 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
372 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
373 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
374 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
375 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
376 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
377 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
378 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
379 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
380 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
381 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
382 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
383 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
384 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
385 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
386 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
387 #define FPU_sig(context) NULL
388 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
389 #else
390 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
391 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
392 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
393 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
394 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
395 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
396 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
397 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
398 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
399 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
400 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
401 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
402 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
403 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
404 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
405 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
406 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
407 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
408 #define FPU_sig(context) NULL
409 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
410 #endif
412 #elif defined(__NetBSD__)
414 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
415 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
416 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
417 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
418 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
419 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
420 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
421 #define ESP_sig(context) _UC_MACHINE_SP(context)
423 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
424 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
425 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
426 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
427 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
428 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
430 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
431 #define EIP_sig(context) _UC_MACHINE_PC(context)
432 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
433 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
435 #define FPU_sig(context) NULL
436 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
438 #define T_MCHK T_MCA
439 #define T_XMMFLT T_XMM
441 #elif defined(__GNU__)
443 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
444 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
445 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
446 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
447 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
448 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
449 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
450 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
452 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
453 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
454 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
455 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
456 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
457 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
459 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
460 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
461 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
462 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
464 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
465 #define FPUX_sig(context) NULL
467 #else
468 #error You must define the signal context functions for your platform
469 #endif /* linux */
471 WINE_DEFAULT_DEBUG_CHANNEL(seh);
473 typedef int (*wine_signal_handler)(unsigned int sig);
475 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
476 static size_t signal_stack_mask;
477 static size_t signal_stack_size;
479 static wine_signal_handler handlers[256];
481 static BOOL fpux_support; /* whether the CPU supports extended fpu context */
483 enum i386_trap_code
485 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
486 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
487 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
488 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
489 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
490 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
491 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
492 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
493 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
494 TRAP_x86_DNA = T_DNA, /* Device not available exception */
495 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
496 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
497 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
498 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
499 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
500 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
501 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
502 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
503 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
504 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
505 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
506 #else
507 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
508 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
509 TRAP_x86_NMI = 2, /* NMI interrupt */
510 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
511 TRAP_x86_OFLOW = 4, /* Overflow exception */
512 TRAP_x86_BOUND = 5, /* Bound range exception */
513 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
514 TRAP_x86_DNA = 7, /* Device not available exception */
515 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
516 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
517 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
518 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
519 TRAP_x86_STKFLT = 12, /* Stack fault */
520 TRAP_x86_PROTFLT = 13, /* General protection fault */
521 TRAP_x86_PAGEFLT = 14, /* Page fault */
522 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
523 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
524 TRAP_x86_MCHK = 18, /* Machine check exception */
525 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
526 otherwise Cache flush exception (via SIGSEV) */
527 #endif
530 struct x86_thread_data
532 DWORD fs; /* 1d4 TEB selector */
533 DWORD gs; /* 1d8 libc selector; update winebuild if you move this! */
534 DWORD dr0; /* 1dc debug registers */
535 DWORD dr1; /* 1e0 */
536 DWORD dr2; /* 1e4 */
537 DWORD dr3; /* 1e8 */
538 DWORD dr6; /* 1ec */
539 DWORD dr7; /* 1f0 */
540 void *exit_frame; /* 1f4 exit frame pointer */
541 #ifdef __HAVE_VM86
542 void *vm86_ptr; /* 1f8 data for vm86 mode */
543 WINE_VM86_TEB_INFO vm86; /* 1fc vm86 private data */
544 #endif
545 /* the ntdll_thread_data structure follows here */
548 C_ASSERT( offsetof( TEB, SystemReserved2 ) + offsetof( struct x86_thread_data, gs ) == 0x1d8 );
549 #ifdef __HAVE_VM86
550 C_ASSERT( offsetof( TEB, SystemReserved2 ) + offsetof( struct x86_thread_data, vm86 ) ==
551 offsetof( TEB, GdiTebBatch ) + offsetof( struct ntdll_thread_data, __vm86 ));
552 #endif
554 static inline struct x86_thread_data *x86_thread_data(void)
556 return (struct x86_thread_data *)NtCurrentTeb()->SystemReserved2;
559 /* Exception record for handling exceptions happening inside exception handlers */
560 typedef struct
562 EXCEPTION_REGISTRATION_RECORD frame;
563 EXCEPTION_REGISTRATION_RECORD *prevFrame;
564 } EXC_NESTED_FRAME;
566 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
567 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
568 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler );
570 /***********************************************************************
571 * dispatch_signal
573 static inline int dispatch_signal(unsigned int sig)
575 if (handlers[sig] == NULL) return 0;
576 return handlers[sig](sig);
580 /***********************************************************************
581 * get_trap_code
583 * Get the trap code for a signal.
585 static inline enum i386_trap_code get_trap_code( const ucontext_t *sigcontext )
587 #ifdef TRAP_sig
588 return TRAP_sig(sigcontext);
589 #else
590 return TRAP_x86_UNKNOWN; /* unknown trap code */
591 #endif
594 /***********************************************************************
595 * get_error_code
597 * Get the error code for a signal.
599 static inline WORD get_error_code( const ucontext_t *sigcontext )
601 #ifdef ERROR_sig
602 return ERROR_sig(sigcontext);
603 #else
604 return 0;
605 #endif
608 /***********************************************************************
609 * get_signal_stack
611 * Get the base of the signal stack for the current thread.
613 static inline void *get_signal_stack(void)
615 return (char *)NtCurrentTeb() + 4096;
619 /***********************************************************************
620 * get_current_teb
622 * Get the current teb based on the stack pointer.
624 static inline TEB *get_current_teb(void)
626 unsigned long esp;
627 __asm__("movl %%esp,%0" : "=g" (esp) );
628 return (TEB *)(esp & ~signal_stack_mask);
632 /*******************************************************************
633 * is_valid_frame
635 static inline BOOL is_valid_frame( void *frame )
637 if ((ULONG_PTR)frame & 3) return FALSE;
638 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
639 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
642 /*******************************************************************
643 * raise_handler
645 * Handler for exceptions happening inside a handler.
647 static DWORD raise_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
648 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
650 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
651 return ExceptionContinueSearch;
652 /* We shouldn't get here so we store faulty frame in dispatcher */
653 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
654 return ExceptionNestedException;
658 /*******************************************************************
659 * unwind_handler
661 * Handler for exceptions happening inside an unwind handler.
663 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
664 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
666 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
667 return ExceptionContinueSearch;
668 /* We shouldn't get here so we store faulty frame in dispatcher */
669 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
670 return ExceptionCollidedUnwind;
674 /**********************************************************************
675 * call_stack_handlers
677 * Call the stack handlers chain.
679 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
681 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
682 DWORD res;
684 frame = NtCurrentTeb()->Tib.ExceptionList;
685 nested_frame = NULL;
686 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
688 /* Check frame address */
689 if (!is_valid_frame( frame ))
691 rec->ExceptionFlags |= EH_STACK_INVALID;
692 break;
695 /* Call handler */
696 TRACE( "calling handler at %p code=%x flags=%x\n",
697 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
698 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, raise_handler );
699 TRACE( "handler at %p returned %x\n", frame->Handler, res );
701 if (frame == nested_frame)
703 /* no longer nested */
704 nested_frame = NULL;
705 rec->ExceptionFlags &= ~EH_NESTED_CALL;
708 switch(res)
710 case ExceptionContinueExecution:
711 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
712 return STATUS_NONCONTINUABLE_EXCEPTION;
713 case ExceptionContinueSearch:
714 break;
715 case ExceptionNestedException:
716 if (nested_frame < dispatch) nested_frame = dispatch;
717 rec->ExceptionFlags |= EH_NESTED_CALL;
718 break;
719 default:
720 return STATUS_INVALID_DISPOSITION;
722 frame = frame->Prev;
724 return STATUS_UNHANDLED_EXCEPTION;
728 /*******************************************************************
729 * raise_exception
731 * Implementation of NtRaiseException.
733 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
735 NTSTATUS status;
737 if (first_chance)
739 DWORD c;
741 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
742 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
743 context->Eip, GetCurrentThreadId() );
744 for (c = 0; c < rec->NumberParameters; c++)
745 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
746 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
748 if (rec->ExceptionInformation[1] >> 16)
749 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
750 rec->ExceptionAddress,
751 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
752 else
753 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
754 rec->ExceptionAddress,
755 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
757 else
759 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
760 context->Eax, context->Ebx, context->Ecx,
761 context->Edx, context->Esi, context->Edi );
762 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
763 context->Ebp, context->Esp, context->SegCs, context->SegDs,
764 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
766 status = send_debug_event( rec, TRUE, context );
767 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
768 return STATUS_SUCCESS;
770 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
771 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Eip--;
773 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
774 return STATUS_SUCCESS;
776 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
777 return status;
780 /* last chance exception */
782 status = send_debug_event( rec, FALSE, context );
783 if (status != DBG_CONTINUE)
785 if (rec->ExceptionFlags & EH_STACK_INVALID)
786 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
787 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
788 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
789 else
790 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
791 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
792 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
794 return STATUS_SUCCESS;
798 #ifdef __HAVE_VM86
799 /***********************************************************************
800 * save_vm86_context
802 * Set the register values from a vm86 structure.
804 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
806 context->ContextFlags = CONTEXT_FULL;
807 context->Eax = vm86->regs.eax;
808 context->Ebx = vm86->regs.ebx;
809 context->Ecx = vm86->regs.ecx;
810 context->Edx = vm86->regs.edx;
811 context->Esi = vm86->regs.esi;
812 context->Edi = vm86->regs.edi;
813 context->Esp = vm86->regs.esp;
814 context->Ebp = vm86->regs.ebp;
815 context->Eip = vm86->regs.eip;
816 context->SegCs = vm86->regs.cs;
817 context->SegDs = vm86->regs.ds;
818 context->SegEs = vm86->regs.es;
819 context->SegFs = vm86->regs.fs;
820 context->SegGs = vm86->regs.gs;
821 context->SegSs = vm86->regs.ss;
822 context->EFlags = vm86->regs.eflags;
826 /***********************************************************************
827 * restore_vm86_context
829 * Build a vm86 structure from the register values.
831 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
833 vm86->regs.eax = context->Eax;
834 vm86->regs.ebx = context->Ebx;
835 vm86->regs.ecx = context->Ecx;
836 vm86->regs.edx = context->Edx;
837 vm86->regs.esi = context->Esi;
838 vm86->regs.edi = context->Edi;
839 vm86->regs.esp = context->Esp;
840 vm86->regs.ebp = context->Ebp;
841 vm86->regs.eip = context->Eip;
842 vm86->regs.cs = context->SegCs;
843 vm86->regs.ds = context->SegDs;
844 vm86->regs.es = context->SegEs;
845 vm86->regs.fs = context->SegFs;
846 vm86->regs.gs = context->SegGs;
847 vm86->regs.ss = context->SegSs;
848 vm86->regs.eflags = context->EFlags;
852 /**********************************************************************
853 * merge_vm86_pending_flags
855 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
856 * raises exception if there are pending events and VIF flag
857 * has been turned on.
859 * Called from __wine_enter_vm86 because vm86_enter
860 * doesn't check for pending events.
862 * Called from raise_vm86_sti_exception to check for
863 * pending events in a signal safe way.
865 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
867 BOOL check_pending = TRUE;
868 struct vm86plus_struct *vm86 =
869 (struct vm86plus_struct*)x86_thread_data()->vm86_ptr;
872 * In order to prevent a race when SIGUSR2 occurs while
873 * we are returning from exception handler, pending events
874 * will be rechecked after each raised exception.
876 while (check_pending && get_vm86_teb_info()->vm86_pending)
878 check_pending = FALSE;
879 x86_thread_data()->vm86_ptr = NULL;
882 * If VIF is set, throw exception.
883 * Note that SIGUSR2 may turn VIF flag off so
884 * VIF check must occur only when TEB.vm86_ptr is NULL.
886 if (vm86->regs.eflags & VIF_FLAG)
888 CONTEXT vcontext;
889 save_vm86_context( &vcontext, vm86 );
891 rec->ExceptionCode = EXCEPTION_VM86_STI;
892 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
893 rec->ExceptionRecord = NULL;
894 rec->NumberParameters = 0;
895 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
897 vcontext.EFlags &= ~VIP_FLAG;
898 get_vm86_teb_info()->vm86_pending = 0;
899 raise_exception( rec, &vcontext, TRUE );
901 restore_vm86_context( &vcontext, vm86 );
902 check_pending = TRUE;
905 x86_thread_data()->vm86_ptr = vm86;
909 * Merge VIP flags in a signal safe way. This requires
910 * that the following operation compiles into atomic
911 * instruction.
913 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
915 #endif /* __HAVE_VM86 */
918 #ifdef __sun
920 /* We have to workaround two Solaris breakages:
921 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
922 * code crash badly.
923 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
924 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
927 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
928 __ASM_GLOBAL_FUNC( sigaction_syscall,
929 "movl $0x62,%eax\n\t"
930 "int $0x91\n\t"
931 "ret" )
933 /* assume the same libc handler is used for all signals */
934 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
936 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
938 struct x86_thread_data *thread_data;
940 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
942 thread_data = (struct x86_thread_data *)get_current_teb()->SystemReserved2;
943 wine_set_fs( thread_data->fs );
944 wine_set_gs( thread_data->gs );
946 libc_sigacthandler( signal, siginfo, sigcontext );
949 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
951 struct sigaction real_act;
953 if (sigaction( sig, new, old ) == -1) return -1;
955 /* retrieve the real handler and flags with a direct syscall */
956 sigaction_syscall( sig, NULL, &real_act );
957 libc_sigacthandler = real_act.sa_sigaction;
958 real_act.sa_sigaction = wine_sigacthandler;
959 sigaction_syscall( sig, &real_act, NULL );
960 return 0;
962 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
964 #endif
966 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
968 extern void clear_alignment_flag(void);
969 __ASM_GLOBAL_FUNC( clear_alignment_flag,
970 "pushfl\n\t"
971 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
972 "andl $~0x40000,(%esp)\n\t"
973 "popfl\n\t"
974 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
975 "ret" )
978 /***********************************************************************
979 * init_handler
981 * Handler initialization when the full context is not needed.
982 * Return the stack pointer to use for pushing the exception data.
984 static inline void *init_handler( const ucontext_t *sigcontext, WORD *fs, WORD *gs )
986 TEB *teb = get_current_teb();
988 clear_alignment_flag();
990 /* get %fs and %gs at time of the fault */
991 #ifdef FS_sig
992 *fs = LOWORD(FS_sig(sigcontext));
993 #else
994 *fs = wine_get_fs();
995 #endif
996 #ifdef GS_sig
997 *gs = LOWORD(GS_sig(sigcontext));
998 #else
999 *gs = wine_get_gs();
1000 #endif
1002 #ifndef __sun /* see above for Solaris handling */
1004 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
1005 wine_set_fs( thread_data->fs );
1006 wine_set_gs( thread_data->gs );
1008 #endif
1010 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
1011 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
1014 * Win16 or DOS protected mode. Note that during switch
1015 * from 16-bit mode to linear mode, CS may be set to system
1016 * segment before FS is restored. Fortunately, in this case
1017 * SS is still non-system segment. This is why both CS and SS
1018 * are checked.
1020 return teb->WOW32Reserved;
1022 return (void *)(ESP_sig(sigcontext) & ~3);
1026 /***********************************************************************
1027 * save_fpu
1029 * Save the thread FPU context.
1031 static inline void save_fpu( CONTEXT *context )
1033 #ifdef __GNUC__
1034 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1035 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
1036 #endif
1040 /***********************************************************************
1041 * restore_fpu
1043 * Restore the FPU context to a sigcontext.
1045 static inline void restore_fpu( const CONTEXT *context )
1047 FLOATING_SAVE_AREA float_status = context->FloatSave;
1048 /* reset the current interrupt status */
1049 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
1050 #ifdef __GNUC__
1051 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
1052 #endif /* __GNUC__ */
1056 /***********************************************************************
1057 * restore_fpux
1059 * Restore the FPU extended context to a sigcontext.
1061 static inline void restore_fpux( const CONTEXT *context )
1063 #ifdef __GNUC__
1064 /* we have to enforce alignment by hand */
1065 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
1066 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
1068 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
1069 /* reset the current interrupt status */
1070 state->StatusWord &= state->ControlWord | 0xff80;
1071 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
1072 #endif
1076 /***********************************************************************
1077 * fpux_to_fpu
1079 * Build a standard FPU context from an extended one.
1081 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
1083 unsigned int i, tag, stack_top;
1085 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
1086 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
1087 fpu->ErrorOffset = fpux->ErrorOffset;
1088 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
1089 fpu->DataOffset = fpux->DataOffset;
1090 fpu->DataSelector = fpux->DataSelector;
1091 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
1093 stack_top = (fpux->StatusWord >> 11) & 7;
1094 fpu->TagWord = 0xffff0000;
1095 for (i = 0; i < 8; i++)
1097 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
1098 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
1099 else
1101 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
1102 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
1104 tag = 2; /* special */
1106 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
1108 if (reg->Low) tag = 2; /* special */
1109 else tag = 1; /* zero */
1111 else
1113 if (reg->Low >> 63) tag = 0; /* valid */
1114 else tag = 2; /* special */
1117 fpu->TagWord |= tag << (2 * i);
1122 /***********************************************************************
1123 * save_context
1125 * Build a context structure from the signal info.
1127 static inline void save_context( CONTEXT *context, const ucontext_t *sigcontext, WORD fs, WORD gs )
1129 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1130 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1132 memset(context, 0, sizeof(*context));
1133 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
1134 context->Eax = EAX_sig(sigcontext);
1135 context->Ebx = EBX_sig(sigcontext);
1136 context->Ecx = ECX_sig(sigcontext);
1137 context->Edx = EDX_sig(sigcontext);
1138 context->Esi = ESI_sig(sigcontext);
1139 context->Edi = EDI_sig(sigcontext);
1140 context->Ebp = EBP_sig(sigcontext);
1141 context->EFlags = EFL_sig(sigcontext);
1142 context->Eip = EIP_sig(sigcontext);
1143 context->Esp = ESP_sig(sigcontext);
1144 context->SegCs = LOWORD(CS_sig(sigcontext));
1145 context->SegDs = LOWORD(DS_sig(sigcontext));
1146 context->SegEs = LOWORD(ES_sig(sigcontext));
1147 context->SegFs = fs;
1148 context->SegGs = gs;
1149 context->SegSs = LOWORD(SS_sig(sigcontext));
1150 context->Dr0 = x86_thread_data()->dr0;
1151 context->Dr1 = x86_thread_data()->dr1;
1152 context->Dr2 = x86_thread_data()->dr2;
1153 context->Dr3 = x86_thread_data()->dr3;
1154 context->Dr6 = x86_thread_data()->dr6;
1155 context->Dr7 = x86_thread_data()->dr7;
1157 if (fpu)
1159 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1160 context->FloatSave = *fpu;
1162 if (fpux)
1164 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
1165 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
1166 fpux_support = TRUE;
1167 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
1169 if (!fpu && !fpux) save_fpu( context );
1173 /***********************************************************************
1174 * restore_context
1176 * Restore the signal info from the context.
1178 static inline void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1180 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1181 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1183 x86_thread_data()->dr0 = context->Dr0;
1184 x86_thread_data()->dr1 = context->Dr1;
1185 x86_thread_data()->dr2 = context->Dr2;
1186 x86_thread_data()->dr3 = context->Dr3;
1187 x86_thread_data()->dr6 = context->Dr6;
1188 x86_thread_data()->dr7 = context->Dr7;
1189 EAX_sig(sigcontext) = context->Eax;
1190 EBX_sig(sigcontext) = context->Ebx;
1191 ECX_sig(sigcontext) = context->Ecx;
1192 EDX_sig(sigcontext) = context->Edx;
1193 ESI_sig(sigcontext) = context->Esi;
1194 EDI_sig(sigcontext) = context->Edi;
1195 EBP_sig(sigcontext) = context->Ebp;
1196 EFL_sig(sigcontext) = context->EFlags;
1197 EIP_sig(sigcontext) = context->Eip;
1198 ESP_sig(sigcontext) = context->Esp;
1199 CS_sig(sigcontext) = context->SegCs;
1200 DS_sig(sigcontext) = context->SegDs;
1201 ES_sig(sigcontext) = context->SegEs;
1202 SS_sig(sigcontext) = context->SegSs;
1203 #ifdef GS_sig
1204 GS_sig(sigcontext) = context->SegGs;
1205 #else
1206 wine_set_gs( context->SegGs );
1207 #endif
1208 #ifdef FS_sig
1209 FS_sig(sigcontext) = context->SegFs;
1210 #else
1211 wine_set_fs( context->SegFs );
1212 #endif
1214 if (fpu) *fpu = context->FloatSave;
1215 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
1216 if (!fpu && !fpux) restore_fpu( context );
1220 /***********************************************************************
1221 * RtlCaptureContext (NTDLL.@)
1223 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
1224 "pushl %eax\n\t"
1225 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1226 "movl 8(%esp),%eax\n\t" /* context */
1227 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1228 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1229 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1230 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1231 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1232 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1233 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1234 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1235 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1236 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1237 "movl 0(%ebp),%edx\n\t"
1238 "movl %edx,0xb4(%eax)\n\t" /* context->Ebp */
1239 "movl 4(%ebp),%edx\n\t"
1240 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1241 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1242 "pushfl\n\t"
1243 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1244 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1245 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1246 "leal 8(%ebp),%edx\n\t"
1247 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1248 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1249 "popl 0xb0(%eax)\n\t" /* context->Eax */
1250 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1251 "ret $4" )
1253 /***********************************************************************
1254 * set_full_cpu_context
1256 * Set the new CPU context.
1258 extern void set_full_cpu_context( const CONTEXT *context );
1259 __ASM_GLOBAL_FUNC( set_full_cpu_context,
1260 "movl 4(%esp),%ecx\n\t"
1261 "movw 0x8c(%ecx),%gs\n\t" /* SegGs */
1262 "movw 0x90(%ecx),%fs\n\t" /* SegFs */
1263 "movw 0x94(%ecx),%es\n\t" /* SegEs */
1264 "movl 0x9c(%ecx),%edi\n\t" /* Edi */
1265 "movl 0xa0(%ecx),%esi\n\t" /* Esi */
1266 "movl 0xa4(%ecx),%ebx\n\t" /* Ebx */
1267 "movl 0xb4(%ecx),%ebp\n\t" /* Ebp */
1268 "movw %ss,%ax\n\t"
1269 "cmpw 0xc8(%ecx),%ax\n\t" /* SegSs */
1270 "jne 1f\n\t"
1271 /* As soon as we have switched stacks the context structure could
1272 * be invalid (when signal handlers are executed for example). Copy
1273 * values on the target stack before changing ESP. */
1274 "movl 0xc4(%ecx),%eax\n\t" /* Esp */
1275 "leal -4*4(%eax),%eax\n\t"
1276 "movl 0xc0(%ecx),%edx\n\t" /* EFlags */
1277 "movl %edx,3*4(%eax)\n\t"
1278 "movl 0xbc(%ecx),%edx\n\t" /* SegCs */
1279 "movl %edx,2*4(%eax)\n\t"
1280 "movl 0xb8(%ecx),%edx\n\t" /* Eip */
1281 "movl %edx,1*4(%eax)\n\t"
1282 "movl 0xb0(%ecx),%edx\n\t" /* Eax */
1283 "movl %edx,0*4(%eax)\n\t"
1284 "pushl 0x98(%ecx)\n\t" /* SegDs */
1285 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1286 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1287 "popl %ds\n\t"
1288 "movl %eax,%esp\n\t"
1289 "popl %eax\n\t"
1290 "iret\n"
1291 /* Restore the context when the stack segment changes. We can't use
1292 * the same code as above because we do not know if the stack segment
1293 * is 16 or 32 bit, and 'movl' will throw an exception when we try to
1294 * access memory above the limit. */
1295 "1:\n\t"
1296 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1297 "movl 0xb0(%ecx),%eax\n\t" /* Eax */
1298 "movw 0xc8(%ecx),%ss\n\t" /* SegSs */
1299 "movl 0xc4(%ecx),%esp\n\t" /* Esp */
1300 "pushl 0xc0(%ecx)\n\t" /* EFlags */
1301 "pushl 0xbc(%ecx)\n\t" /* SegCs */
1302 "pushl 0xb8(%ecx)\n\t" /* Eip */
1303 "pushl 0x98(%ecx)\n\t" /* SegDs */
1304 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1305 "popl %ds\n\t"
1306 "iret" )
1309 /***********************************************************************
1310 * set_cpu_context
1312 * Set the new CPU context. Used by NtSetContextThread.
1314 static void set_cpu_context( const CONTEXT *context )
1316 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1318 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
1319 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
1321 if (flags & CONTEXT_DEBUG_REGISTERS)
1323 x86_thread_data()->dr0 = context->Dr0;
1324 x86_thread_data()->dr1 = context->Dr1;
1325 x86_thread_data()->dr2 = context->Dr2;
1326 x86_thread_data()->dr3 = context->Dr3;
1327 x86_thread_data()->dr6 = context->Dr6;
1328 x86_thread_data()->dr7 = context->Dr7;
1330 if (flags & CONTEXT_FULL)
1332 if (!(flags & CONTEXT_CONTROL))
1333 FIXME( "setting partial context (%x) not supported\n", flags );
1334 else if (flags & CONTEXT_SEGMENTS)
1335 set_full_cpu_context( context );
1336 else
1338 CONTEXT newcontext = *context;
1339 newcontext.SegDs = wine_get_ds();
1340 newcontext.SegEs = wine_get_es();
1341 newcontext.SegFs = wine_get_fs();
1342 newcontext.SegGs = wine_get_gs();
1343 set_full_cpu_context( &newcontext );
1349 /***********************************************************************
1350 * context_to_server
1352 * Convert a register context to the server format.
1354 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1356 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1358 memset( to, 0, sizeof(*to) );
1359 to->cpu = CPU_x86;
1361 if (flags & CONTEXT_CONTROL)
1363 to->flags |= SERVER_CTX_CONTROL;
1364 to->ctl.i386_regs.ebp = from->Ebp;
1365 to->ctl.i386_regs.esp = from->Esp;
1366 to->ctl.i386_regs.eip = from->Eip;
1367 to->ctl.i386_regs.cs = from->SegCs;
1368 to->ctl.i386_regs.ss = from->SegSs;
1369 to->ctl.i386_regs.eflags = from->EFlags;
1371 if (flags & CONTEXT_INTEGER)
1373 to->flags |= SERVER_CTX_INTEGER;
1374 to->integer.i386_regs.eax = from->Eax;
1375 to->integer.i386_regs.ebx = from->Ebx;
1376 to->integer.i386_regs.ecx = from->Ecx;
1377 to->integer.i386_regs.edx = from->Edx;
1378 to->integer.i386_regs.esi = from->Esi;
1379 to->integer.i386_regs.edi = from->Edi;
1381 if (flags & CONTEXT_SEGMENTS)
1383 to->flags |= SERVER_CTX_SEGMENTS;
1384 to->seg.i386_regs.ds = from->SegDs;
1385 to->seg.i386_regs.es = from->SegEs;
1386 to->seg.i386_regs.fs = from->SegFs;
1387 to->seg.i386_regs.gs = from->SegGs;
1389 if (flags & CONTEXT_FLOATING_POINT)
1391 to->flags |= SERVER_CTX_FLOATING_POINT;
1392 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1393 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1394 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1395 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1396 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1397 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1398 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1399 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1400 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1402 if (flags & CONTEXT_DEBUG_REGISTERS)
1404 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1405 to->debug.i386_regs.dr0 = from->Dr0;
1406 to->debug.i386_regs.dr1 = from->Dr1;
1407 to->debug.i386_regs.dr2 = from->Dr2;
1408 to->debug.i386_regs.dr3 = from->Dr3;
1409 to->debug.i386_regs.dr6 = from->Dr6;
1410 to->debug.i386_regs.dr7 = from->Dr7;
1412 if (flags & CONTEXT_EXTENDED_REGISTERS)
1414 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1415 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1417 return STATUS_SUCCESS;
1421 /***********************************************************************
1422 * context_from_server
1424 * Convert a register context from the server format.
1426 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1428 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1430 to->ContextFlags = CONTEXT_i386;
1431 if (from->flags & SERVER_CTX_CONTROL)
1433 to->ContextFlags |= CONTEXT_CONTROL;
1434 to->Ebp = from->ctl.i386_regs.ebp;
1435 to->Esp = from->ctl.i386_regs.esp;
1436 to->Eip = from->ctl.i386_regs.eip;
1437 to->SegCs = from->ctl.i386_regs.cs;
1438 to->SegSs = from->ctl.i386_regs.ss;
1439 to->EFlags = from->ctl.i386_regs.eflags;
1441 if (from->flags & SERVER_CTX_INTEGER)
1443 to->ContextFlags |= CONTEXT_INTEGER;
1444 to->Eax = from->integer.i386_regs.eax;
1445 to->Ebx = from->integer.i386_regs.ebx;
1446 to->Ecx = from->integer.i386_regs.ecx;
1447 to->Edx = from->integer.i386_regs.edx;
1448 to->Esi = from->integer.i386_regs.esi;
1449 to->Edi = from->integer.i386_regs.edi;
1451 if (from->flags & SERVER_CTX_SEGMENTS)
1453 to->ContextFlags |= CONTEXT_SEGMENTS;
1454 to->SegDs = from->seg.i386_regs.ds;
1455 to->SegEs = from->seg.i386_regs.es;
1456 to->SegFs = from->seg.i386_regs.fs;
1457 to->SegGs = from->seg.i386_regs.gs;
1459 if (from->flags & SERVER_CTX_FLOATING_POINT)
1461 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1462 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1463 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1464 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1465 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1466 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1467 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1468 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1469 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1470 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1472 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1474 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1475 to->Dr0 = from->debug.i386_regs.dr0;
1476 to->Dr1 = from->debug.i386_regs.dr1;
1477 to->Dr2 = from->debug.i386_regs.dr2;
1478 to->Dr3 = from->debug.i386_regs.dr3;
1479 to->Dr6 = from->debug.i386_regs.dr6;
1480 to->Dr7 = from->debug.i386_regs.dr7;
1482 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1484 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1485 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1487 return STATUS_SUCCESS;
1491 /***********************************************************************
1492 * NtSetContextThread (NTDLL.@)
1493 * ZwSetContextThread (NTDLL.@)
1495 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
1497 NTSTATUS ret = STATUS_SUCCESS;
1498 BOOL self = (handle == GetCurrentThread());
1500 /* debug registers require a server call */
1501 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
1502 self = (x86_thread_data()->dr0 == context->Dr0 &&
1503 x86_thread_data()->dr1 == context->Dr1 &&
1504 x86_thread_data()->dr2 == context->Dr2 &&
1505 x86_thread_data()->dr3 == context->Dr3 &&
1506 x86_thread_data()->dr6 == context->Dr6 &&
1507 x86_thread_data()->dr7 == context->Dr7);
1509 if (!self) ret = set_thread_context( handle, context, &self );
1511 if (self && ret == STATUS_SUCCESS) set_cpu_context( context );
1512 return ret;
1516 /***********************************************************************
1517 * NtGetContextThread (NTDLL.@)
1518 * ZwGetContextThread (NTDLL.@)
1520 * Note: we use a small assembly wrapper to save the necessary registers
1521 * in case we are fetching the context of the current thread.
1523 NTSTATUS CDECL DECLSPEC_HIDDEN __regs_NtGetContextThread( DWORD edi, DWORD esi, DWORD ebx, DWORD eflags,
1524 DWORD ebp, DWORD retaddr, HANDLE handle,
1525 CONTEXT *context )
1527 NTSTATUS ret;
1528 DWORD needed_flags = context->ContextFlags & ~CONTEXT_i386;
1529 BOOL self = (handle == GetCurrentThread());
1531 /* debug registers require a server call */
1532 if (needed_flags & CONTEXT_DEBUG_REGISTERS) self = FALSE;
1534 if (!self)
1536 if ((ret = get_thread_context( handle, context, &self ))) return ret;
1537 needed_flags &= ~context->ContextFlags;
1540 if (self)
1542 if (needed_flags & CONTEXT_INTEGER)
1544 context->Eax = 0;
1545 context->Ebx = ebx;
1546 context->Ecx = 0;
1547 context->Edx = 0;
1548 context->Esi = esi;
1549 context->Edi = edi;
1550 context->ContextFlags |= CONTEXT_INTEGER;
1552 if (needed_flags & CONTEXT_CONTROL)
1554 context->Ebp = ebp;
1555 context->Esp = (DWORD)&retaddr;
1556 context->Eip = *(&edi - 1);
1557 context->SegCs = wine_get_cs();
1558 context->SegSs = wine_get_ss();
1559 context->EFlags = eflags;
1560 context->ContextFlags |= CONTEXT_CONTROL;
1562 if (needed_flags & CONTEXT_SEGMENTS)
1564 context->SegDs = wine_get_ds();
1565 context->SegEs = wine_get_es();
1566 context->SegFs = wine_get_fs();
1567 context->SegGs = wine_get_gs();
1568 context->ContextFlags |= CONTEXT_SEGMENTS;
1570 if (needed_flags & CONTEXT_FLOATING_POINT) save_fpu( context );
1571 /* FIXME: extended floating point */
1572 /* update the cached version of the debug registers */
1573 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1575 x86_thread_data()->dr0 = context->Dr0;
1576 x86_thread_data()->dr1 = context->Dr1;
1577 x86_thread_data()->dr2 = context->Dr2;
1578 x86_thread_data()->dr3 = context->Dr3;
1579 x86_thread_data()->dr6 = context->Dr6;
1580 x86_thread_data()->dr7 = context->Dr7;
1584 if (context->ContextFlags & (CONTEXT_INTEGER & ~CONTEXT_i386))
1585 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle,
1586 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi );
1587 if (context->ContextFlags & (CONTEXT_CONTROL & ~CONTEXT_i386))
1588 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle,
1589 context->Ebp, context->Esp, context->Eip, context->SegCs, context->SegSs, context->EFlags );
1590 if (context->ContextFlags & (CONTEXT_SEGMENTS & ~CONTEXT_i386))
1591 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle,
1592 context->SegDs, context->SegEs, context->SegFs, context->SegGs );
1593 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1594 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle,
1595 context->Dr0, context->Dr1, context->Dr2, context->Dr3, context->Dr6, context->Dr7 );
1597 return STATUS_SUCCESS;
1599 __ASM_STDCALL_FUNC( NtGetContextThread, 8,
1600 "pushl %ebp\n\t"
1601 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1602 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1603 "movl %esp,%ebp\n\t"
1604 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1605 "pushfl\n\t"
1606 "pushl %ebx\n\t"
1607 __ASM_CFI(".cfi_rel_offset %ebx,-8\n\t")
1608 "pushl %esi\n\t"
1609 __ASM_CFI(".cfi_rel_offset %esi,-12\n\t")
1610 "pushl %edi\n\t"
1611 __ASM_CFI(".cfi_rel_offset %edi,-16\n\t")
1612 "call " __ASM_NAME("__regs_NtGetContextThread") "\n\t"
1613 "leave\n\t"
1614 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1615 __ASM_CFI(".cfi_same_value %ebp\n\t")
1616 "ret $8" )
1619 /***********************************************************************
1620 * is_privileged_instr
1622 * Check if the fault location is a privileged instruction.
1623 * Based on the instruction emulation code in dlls/kernel/instr.c.
1625 static inline DWORD is_privileged_instr( CONTEXT *context )
1627 const BYTE *instr;
1628 unsigned int prefix_count = 0;
1630 if (!wine_ldt_is_system( context->SegCs )) return 0;
1631 instr = (BYTE *)context->Eip;
1633 for (;;) switch(*instr)
1635 /* instruction prefixes */
1636 case 0x2e: /* %cs: */
1637 case 0x36: /* %ss: */
1638 case 0x3e: /* %ds: */
1639 case 0x26: /* %es: */
1640 case 0x64: /* %fs: */
1641 case 0x65: /* %gs: */
1642 case 0x66: /* opcode size */
1643 case 0x67: /* addr size */
1644 case 0xf0: /* lock */
1645 case 0xf2: /* repne */
1646 case 0xf3: /* repe */
1647 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1648 instr++;
1649 continue;
1651 case 0x0f: /* extended instruction */
1652 switch(instr[1])
1654 case 0x20: /* mov crX, reg */
1655 case 0x21: /* mov drX, reg */
1656 case 0x22: /* mov reg, crX */
1657 case 0x23: /* mov reg drX */
1658 return EXCEPTION_PRIV_INSTRUCTION;
1660 return 0;
1661 case 0x6c: /* insb (%dx) */
1662 case 0x6d: /* insl (%dx) */
1663 case 0x6e: /* outsb (%dx) */
1664 case 0x6f: /* outsl (%dx) */
1665 case 0xcd: /* int $xx */
1666 case 0xe4: /* inb al,XX */
1667 case 0xe5: /* in (e)ax,XX */
1668 case 0xe6: /* outb XX,al */
1669 case 0xe7: /* out XX,(e)ax */
1670 case 0xec: /* inb (%dx),%al */
1671 case 0xed: /* inl (%dx),%eax */
1672 case 0xee: /* outb %al,(%dx) */
1673 case 0xef: /* outl %eax,(%dx) */
1674 case 0xf4: /* hlt */
1675 case 0xfa: /* cli */
1676 case 0xfb: /* sti */
1677 return EXCEPTION_PRIV_INSTRUCTION;
1678 default:
1679 return 0;
1684 /***********************************************************************
1685 * handle_interrupt
1687 * Handle an interrupt.
1689 static inline BOOL handle_interrupt( unsigned int interrupt, EXCEPTION_RECORD *rec, CONTEXT *context )
1691 switch(interrupt)
1693 case 0x2d:
1694 context->Eip += 3;
1695 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1696 rec->ExceptionAddress = (void *)context->Eip;
1697 rec->NumberParameters = is_wow64 ? 1 : 3;
1698 rec->ExceptionInformation[0] = context->Eax;
1699 rec->ExceptionInformation[1] = context->Ecx;
1700 rec->ExceptionInformation[2] = context->Edx;
1701 return TRUE;
1702 default:
1703 return FALSE;
1708 /***********************************************************************
1709 * check_invalid_gs
1711 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1713 static inline BOOL check_invalid_gs( CONTEXT *context )
1715 unsigned int prefix_count = 0;
1716 const BYTE *instr = (BYTE *)context->Eip;
1717 WORD system_gs = x86_thread_data()->gs;
1719 if (context->SegGs == system_gs) return FALSE;
1720 if (!wine_ldt_is_system( context->SegCs )) return FALSE;
1721 /* only handle faults in system libraries */
1722 if (virtual_is_valid_code_address( instr, 1 )) return FALSE;
1724 for (;;) switch(*instr)
1726 /* instruction prefixes */
1727 case 0x2e: /* %cs: */
1728 case 0x36: /* %ss: */
1729 case 0x3e: /* %ds: */
1730 case 0x26: /* %es: */
1731 case 0x64: /* %fs: */
1732 case 0x66: /* opcode size */
1733 case 0x67: /* addr size */
1734 case 0xf0: /* lock */
1735 case 0xf2: /* repne */
1736 case 0xf3: /* repe */
1737 if (++prefix_count >= 15) return FALSE;
1738 instr++;
1739 continue;
1740 case 0x65: /* %gs: */
1741 TRACE( "%04x/%04x at %p, fixing up\n", context->SegGs, system_gs, instr );
1742 context->SegGs = system_gs;
1743 return TRUE;
1744 default:
1745 return FALSE;
1750 #include "pshpack1.h"
1751 union atl_thunk
1753 struct
1755 DWORD movl; /* movl this,4(%esp) */
1756 DWORD this;
1757 BYTE jmp; /* jmp func */
1758 int func;
1759 } t1;
1760 struct
1762 BYTE movl; /* movl this,ecx */
1763 DWORD this;
1764 BYTE jmp; /* jmp func */
1765 int func;
1766 } t2;
1767 struct
1769 BYTE movl1; /* movl this,edx */
1770 DWORD this;
1771 BYTE movl2; /* movl func,ecx */
1772 DWORD func;
1773 WORD jmp; /* jmp ecx */
1774 } t3;
1775 struct
1777 BYTE movl1; /* movl this,ecx */
1778 DWORD this;
1779 BYTE movl2; /* movl func,eax */
1780 DWORD func;
1781 WORD jmp; /* jmp eax */
1782 } t4;
1783 struct
1785 DWORD inst1; /* pop ecx
1786 * pop eax
1787 * push ecx
1788 * jmp 4(%eax) */
1789 WORD inst2;
1790 } t5;
1792 #include "poppack.h"
1794 /**********************************************************************
1795 * check_atl_thunk
1797 * Check if code destination is an ATL thunk, and emulate it if so.
1799 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1801 const union atl_thunk *thunk = (const union atl_thunk *)rec->ExceptionInformation[1];
1802 union atl_thunk thunk_copy;
1803 SIZE_T thunk_len;
1805 thunk_len = virtual_uninterrupted_read_memory( thunk, &thunk_copy, sizeof(*thunk) );
1806 if (!thunk_len) return FALSE;
1808 if (thunk_len >= sizeof(thunk_copy.t1) && thunk_copy.t1.movl == 0x042444c7 &&
1809 thunk_copy.t1.jmp == 0xe9)
1811 if (!virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1,
1812 &thunk_copy.t1.this, sizeof(DWORD) ))
1814 context->Eip = (DWORD_PTR)(&thunk->t1.func + 1) + thunk_copy.t1.func;
1815 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1816 thunk, context->Eip, thunk_copy.t1.this );
1817 return TRUE;
1820 else if (thunk_len >= sizeof(thunk_copy.t2) && thunk_copy.t2.movl == 0xb9 &&
1821 thunk_copy.t2.jmp == 0xe9)
1823 context->Ecx = thunk_copy.t2.this;
1824 context->Eip = (DWORD_PTR)(&thunk->t2.func + 1) + thunk_copy.t2.func;
1825 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1826 thunk, context->Eip, context->Ecx );
1827 return TRUE;
1829 else if (thunk_len >= sizeof(thunk_copy.t3) && thunk_copy.t3.movl1 == 0xba &&
1830 thunk_copy.t3.movl2 == 0xb9 &&
1831 thunk_copy.t3.jmp == 0xe1ff)
1833 context->Edx = thunk_copy.t3.this;
1834 context->Ecx = thunk_copy.t3.func;
1835 context->Eip = thunk_copy.t3.func;
1836 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1837 thunk, context->Eip, context->Ecx, context->Edx );
1838 return TRUE;
1840 else if (thunk_len >= sizeof(thunk_copy.t4) && thunk_copy.t4.movl1 == 0xb9 &&
1841 thunk_copy.t4.movl2 == 0xb8 &&
1842 thunk_copy.t4.jmp == 0xe0ff)
1844 context->Ecx = thunk_copy.t4.this;
1845 context->Eax = thunk_copy.t4.func;
1846 context->Eip = thunk_copy.t4.func;
1847 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1848 thunk, context->Eip, context->Eax, context->Ecx );
1849 return TRUE;
1851 else if (thunk_len >= sizeof(thunk_copy.t5) && thunk_copy.t5.inst1 == 0xff515859 &&
1852 thunk_copy.t5.inst2 == 0x0460)
1854 DWORD func, stack[2];
1855 if (virtual_uninterrupted_read_memory( (DWORD *)context->Esp,
1856 stack, sizeof(stack) ) == sizeof(stack) &&
1857 virtual_uninterrupted_read_memory( (DWORD *)stack[1] + 1,
1858 &func, sizeof(DWORD) ) == sizeof(DWORD) &&
1859 !virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1, &stack[0], sizeof(stack[0]) ))
1861 context->Ecx = stack[0];
1862 context->Eax = stack[1];
1863 context->Esp = context->Esp + sizeof(DWORD);
1864 context->Eip = func;
1865 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1866 thunk, context->Eip, context->Eax, context->Ecx, context->Esp );
1867 return TRUE;
1871 return FALSE;
1875 /***********************************************************************
1876 * setup_exception_record
1878 * Setup the exception record and context on the thread stack.
1880 static EXCEPTION_RECORD *setup_exception_record( ucontext_t *sigcontext, void *stack_ptr,
1881 WORD fs, WORD gs, raise_func func )
1883 struct stack_layout
1885 void *ret_addr; /* return address from raise_func */
1886 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1887 CONTEXT *context_ptr; /* second arg for raise_func */
1888 CONTEXT context;
1889 EXCEPTION_RECORD rec;
1890 DWORD ebp;
1891 DWORD eip;
1892 } *stack = stack_ptr;
1893 DWORD exception_code = 0;
1895 /* stack sanity checks */
1897 if ((char *)stack >= (char *)get_signal_stack() &&
1898 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1900 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1901 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1902 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1903 NtCurrentTeb()->Tib.StackBase );
1904 abort_thread(1);
1907 if (stack - 1 > stack || /* check for overflow in subtraction */
1908 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1909 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1911 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1912 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1913 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1914 NtCurrentTeb()->Tib.StackBase );
1916 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1918 /* stack overflow on last page, unrecoverable */
1919 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1920 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1921 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1922 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1923 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1924 abort_thread(1);
1926 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1928 /* stack access below stack limit, may be recoverable */
1929 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1930 else
1932 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1933 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1934 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1935 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1936 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1937 abort_thread(1);
1941 stack--; /* push the stack_layout structure */
1942 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1943 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1944 #elif defined(VALGRIND_MAKE_WRITABLE)
1945 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1946 #endif
1947 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1948 stack->rec_ptr = &stack->rec;
1949 stack->context_ptr = &stack->context;
1951 stack->rec.ExceptionRecord = NULL;
1952 stack->rec.ExceptionCode = exception_code;
1953 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1954 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1955 stack->rec.NumberParameters = 0;
1957 save_context( &stack->context, sigcontext, fs, gs );
1959 /* now modify the sigcontext to return to the raise function */
1960 ESP_sig(sigcontext) = (DWORD)stack;
1961 EIP_sig(sigcontext) = (DWORD)func;
1962 /* clear single-step, direction, and align check flag */
1963 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1964 CS_sig(sigcontext) = wine_get_cs();
1965 DS_sig(sigcontext) = wine_get_ds();
1966 ES_sig(sigcontext) = wine_get_es();
1967 FS_sig(sigcontext) = wine_get_fs();
1968 GS_sig(sigcontext) = wine_get_gs();
1969 SS_sig(sigcontext) = wine_get_ss();
1971 return stack->rec_ptr;
1975 /***********************************************************************
1976 * setup_exception
1978 * Setup a proper stack frame for the raise function, and modify the
1979 * sigcontext so that the return from the signal handler will call
1980 * the raise function.
1982 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1984 WORD fs, gs;
1985 void *stack = init_handler( sigcontext, &fs, &gs );
1987 return setup_exception_record( sigcontext, stack, fs, gs, func );
1991 /***********************************************************************
1992 * get_exception_context
1994 * Get a pointer to the context built by setup_exception.
1996 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1998 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
2002 /**********************************************************************
2003 * get_fpu_code
2005 * Get the FPU exception code from the FPU status.
2007 static inline DWORD get_fpu_code( const CONTEXT *context )
2009 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
2011 if (status & 0x01) /* IE */
2013 if (status & 0x40) /* SF */
2014 return EXCEPTION_FLT_STACK_CHECK;
2015 else
2016 return EXCEPTION_FLT_INVALID_OPERATION;
2018 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
2019 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
2020 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
2021 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
2022 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
2023 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
2027 /**********************************************************************
2028 * raise_segv_exception
2030 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2032 NTSTATUS status;
2034 switch(rec->ExceptionCode)
2036 case EXCEPTION_ACCESS_VIOLATION:
2037 if (rec->NumberParameters == 2)
2039 if (rec->ExceptionInformation[1] == 0xffffffff && check_invalid_gs( context ))
2040 goto done;
2041 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
2042 rec->ExceptionInformation[0], FALSE )))
2043 goto done;
2044 if (rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
2045 rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT)
2047 ULONG flags;
2048 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
2049 &flags, sizeof(flags), NULL );
2051 if (!(flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION) && check_atl_thunk( rec, context ))
2052 goto done;
2054 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
2055 if (!(flags & MEM_EXECUTE_OPTION_DISABLE))
2056 rec->ExceptionInformation[0] = EXCEPTION_READ_FAULT;
2059 break;
2060 case EXCEPTION_DATATYPE_MISALIGNMENT:
2061 /* FIXME: pass through exception handler first? */
2062 if (context->EFlags & 0x00040000)
2064 /* Disable AC flag, return */
2065 context->EFlags &= ~0x00040000;
2066 goto done;
2068 break;
2069 case EXCEPTION_BREAKPOINT:
2070 if (!is_wow64)
2072 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
2073 * service is usually not recognized when called from usermode. */
2074 switch (rec->ExceptionInformation[0])
2076 case 1: /* BREAKPOINT_PRINT */
2077 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2078 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2079 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2080 goto done;
2083 break;
2085 status = NtRaiseException( rec, context, TRUE );
2086 raise_status( status, rec );
2087 done:
2088 set_cpu_context( context );
2092 /**********************************************************************
2093 * raise_trap_exception
2095 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2097 NTSTATUS status;
2099 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
2101 /* when single stepping can't tell whether this is a hw bp or a
2102 * single step interrupt. try to avoid as much overhead as possible
2103 * and only do a server call if there is any hw bp enabled. */
2105 if( !(context->EFlags & 0x100) || (x86_thread_data()->dr7 & 0xff) )
2107 /* (possible) hardware breakpoint, fetch the debug registers */
2108 DWORD saved_flags = context->ContextFlags;
2109 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
2110 NtGetContextThread(GetCurrentThread(), context);
2111 context->ContextFlags |= saved_flags; /* restore flags */
2114 context->EFlags &= ~0x100; /* clear single-step flag */
2117 status = NtRaiseException( rec, context, TRUE );
2118 raise_status( status, rec );
2122 /**********************************************************************
2123 * raise_generic_exception
2125 * Generic raise function for exceptions that don't need special treatment.
2127 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2129 NTSTATUS status;
2131 status = NtRaiseException( rec, context, TRUE );
2132 raise_status( status, rec );
2136 #ifdef __HAVE_VM86
2137 /**********************************************************************
2138 * raise_vm86_sti_exception
2140 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2142 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
2143 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
2145 if (x86_thread_data()->vm86_ptr)
2147 if (((char*)context->Eip >= (char*)vm86_return) &&
2148 ((char*)context->Eip <= (char*)vm86_return_end) &&
2149 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
2150 /* exiting from VM86, can't throw */
2151 goto done;
2153 merge_vm86_pending_flags( rec );
2155 else if (get_vm86_teb_info()->dpmi_vif &&
2156 !wine_ldt_is_system(context->SegCs) &&
2157 !wine_ldt_is_system(context->SegSs))
2159 /* Executing DPMI code and virtual interrupts are enabled. */
2160 get_vm86_teb_info()->vm86_pending = 0;
2161 NtRaiseException( rec, context, TRUE );
2163 done:
2164 set_cpu_context( context );
2168 /**********************************************************************
2169 * usr2_handler
2171 * Handler for SIGUSR2.
2172 * We use it to signal that the running __wine_enter_vm86() should
2173 * immediately set VIP_FLAG, causing pending events to be handled
2174 * as early as possible.
2176 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2178 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
2179 rec->ExceptionCode = EXCEPTION_VM86_STI;
2181 #endif /* __HAVE_VM86 */
2184 /**********************************************************************
2185 * segv_handler
2187 * Handler for SIGSEGV and related errors.
2189 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2191 WORD fs, gs;
2192 EXCEPTION_RECORD *rec;
2193 ucontext_t *context = sigcontext;
2194 void *stack = init_handler( sigcontext, &fs, &gs );
2196 /* check for exceptions on the signal stack caused by write watches */
2197 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
2198 (char *)stack >= (char *)get_signal_stack() &&
2199 (char *)stack < (char *)get_signal_stack() + signal_stack_size &&
2200 !virtual_handle_fault( siginfo->si_addr, (get_error_code(context) >> 1) & 0x09, TRUE ))
2202 return;
2205 /* check for page fault inside the thread stack */
2206 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
2207 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
2208 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
2209 virtual_handle_stack_fault( siginfo->si_addr ))
2211 /* check if this was the last guard page */
2212 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
2214 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2215 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2217 return;
2220 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2221 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
2223 switch(get_trap_code(context))
2225 case TRAP_x86_OFLOW: /* Overflow exception */
2226 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2227 break;
2228 case TRAP_x86_BOUND: /* Bound range exception */
2229 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2230 break;
2231 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2232 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2233 break;
2234 case TRAP_x86_STKFLT: /* Stack fault */
2235 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2236 break;
2237 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2238 case TRAP_x86_PROTFLT: /* General protection fault */
2239 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2241 CONTEXT *win_context = get_exception_context( rec );
2242 WORD err = get_error_code(context);
2243 if (!err && (rec->ExceptionCode = is_privileged_instr( win_context ))) break;
2244 if ((err & 7) == 2 && handle_interrupt( err >> 3, rec, win_context )) break;
2245 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2246 rec->NumberParameters = 2;
2247 rec->ExceptionInformation[0] = 0;
2248 /* if error contains a LDT selector, use that as fault address */
2249 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
2250 rec->ExceptionInformation[1] = err & ~7;
2251 else
2252 rec->ExceptionInformation[1] = 0xffffffff;
2254 break;
2255 case TRAP_x86_PAGEFLT: /* Page fault */
2256 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2257 rec->NumberParameters = 2;
2258 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
2259 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2260 break;
2261 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2262 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2263 break;
2264 default:
2265 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2266 /* fall through */
2267 case TRAP_x86_NMI: /* NMI interrupt */
2268 case TRAP_x86_DNA: /* Device not available exception */
2269 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2270 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2271 case TRAP_x86_MCHK: /* Machine check exception */
2272 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2273 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2274 break;
2279 /**********************************************************************
2280 * trap_handler
2282 * Handler for SIGTRAP.
2284 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2286 ucontext_t *context = sigcontext;
2287 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
2289 switch(get_trap_code(context))
2291 case TRAP_x86_TRCTRAP: /* Single-step exception */
2292 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2293 break;
2294 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2295 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2296 /* fall through */
2297 default:
2298 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2299 rec->NumberParameters = is_wow64 ? 1 : 3;
2300 rec->ExceptionInformation[0] = 0;
2301 rec->ExceptionInformation[1] = 0; /* FIXME */
2302 rec->ExceptionInformation[2] = 0; /* FIXME */
2303 break;
2308 /**********************************************************************
2309 * fpe_handler
2311 * Handler for SIGFPE.
2313 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2315 CONTEXT *win_context;
2316 ucontext_t *context = sigcontext;
2317 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
2319 win_context = get_exception_context( rec );
2321 switch(get_trap_code(context))
2323 case TRAP_x86_DIVIDE: /* Division by zero exception */
2324 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2325 break;
2326 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
2327 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2328 break;
2329 case TRAP_x86_ARITHTRAP: /* Floating point exception */
2330 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2331 rec->ExceptionCode = get_fpu_code( win_context );
2332 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
2333 break;
2334 case TRAP_x86_CACHEFLT: /* SIMD exception */
2335 /* TODO:
2336 * Behaviour only tested for divide-by-zero exceptions
2337 * Check for other SIMD exceptions as well */
2338 if(siginfo->si_code != FPE_FLTDIV && siginfo->si_code != FPE_FLTINV)
2339 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2340 siginfo->si_code);
2342 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
2343 rec->NumberParameters = 1;
2344 /* no idea what meaning is actually behind this but that's what native does */
2345 rec->ExceptionInformation[0] = 0;
2346 break;
2347 default:
2348 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2349 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2350 break;
2355 /**********************************************************************
2356 * int_handler
2358 * Handler for SIGINT.
2360 * FIXME: should not be calling external functions on the signal stack.
2362 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2364 WORD fs, gs;
2365 init_handler( sigcontext, &fs, &gs );
2366 if (!dispatch_signal(SIGINT))
2368 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2369 rec->ExceptionCode = CONTROL_C_EXIT;
2373 /**********************************************************************
2374 * abrt_handler
2376 * Handler for SIGABRT.
2378 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2380 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2381 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2382 rec->ExceptionFlags = EH_NONCONTINUABLE;
2386 /**********************************************************************
2387 * quit_handler
2389 * Handler for SIGQUIT.
2391 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2393 WORD fs, gs;
2394 init_handler( sigcontext, &fs, &gs );
2395 abort_thread(0);
2399 /**********************************************************************
2400 * usr1_handler
2402 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2404 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2406 CONTEXT context;
2407 WORD fs, gs;
2409 init_handler( sigcontext, &fs, &gs );
2410 save_context( &context, sigcontext, fs, gs );
2411 wait_suspend( &context );
2412 restore_context( &context, sigcontext );
2416 /***********************************************************************
2417 * __wine_set_signal_handler (NTDLL.@)
2419 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2421 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2422 if (handlers[sig] != NULL) return -2;
2423 handlers[sig] = wsh;
2424 return 0;
2428 /***********************************************************************
2429 * locking for LDT routines
2431 static RTL_CRITICAL_SECTION ldt_section;
2432 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
2434 0, 0, &ldt_section,
2435 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
2436 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
2438 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
2439 static sigset_t ldt_sigset;
2441 static void ldt_lock(void)
2443 sigset_t sigset;
2445 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
2446 RtlEnterCriticalSection( &ldt_section );
2447 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
2450 static void ldt_unlock(void)
2452 if (ldt_section.RecursionCount == 1)
2454 sigset_t sigset = ldt_sigset;
2455 RtlLeaveCriticalSection( &ldt_section );
2456 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
2458 else RtlLeaveCriticalSection( &ldt_section );
2462 /**********************************************************************
2463 * signal_alloc_thread
2465 NTSTATUS signal_alloc_thread( TEB **teb )
2467 static size_t sigstack_zero_bits;
2468 struct x86_thread_data *thread_data;
2469 SIZE_T size;
2470 void *addr = NULL;
2471 NTSTATUS status;
2473 if (!sigstack_zero_bits)
2475 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2476 /* find the first power of two not smaller than min_size */
2477 sigstack_zero_bits = 12;
2478 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2479 signal_stack_mask = (1 << sigstack_zero_bits) - 1;
2480 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2483 size = signal_stack_mask + 1;
2484 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
2485 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2487 *teb = addr;
2488 (*teb)->Tib.Self = &(*teb)->Tib;
2489 (*teb)->Tib.ExceptionList = (void *)~0UL;
2490 thread_data = (struct x86_thread_data *)(*teb)->SystemReserved2;
2491 if (!(thread_data->fs = wine_ldt_alloc_fs()))
2493 size = 0;
2494 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
2495 status = STATUS_TOO_MANY_THREADS;
2498 return status;
2502 /**********************************************************************
2503 * signal_free_thread
2505 void signal_free_thread( TEB *teb )
2507 SIZE_T size;
2508 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2510 if (thread_data) wine_ldt_free_fs( thread_data->fs );
2511 if (teb->DeallocationStack)
2513 size = 0;
2514 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2516 size = 0;
2517 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2521 /**********************************************************************
2522 * signal_init_thread
2524 void signal_init_thread( TEB *teb )
2526 const WORD fpu_cw = 0x27f;
2527 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2528 LDT_ENTRY fs_entry;
2529 stack_t ss;
2531 #ifdef __APPLE__
2532 int mib[2], val = 1;
2534 mib[0] = CTL_KERN;
2535 mib[1] = KERN_THALTSTACK;
2536 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2537 #endif
2539 ss.ss_sp = (char *)teb + teb_size;
2540 ss.ss_size = signal_stack_size;
2541 ss.ss_flags = 0;
2542 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2544 wine_ldt_set_base( &fs_entry, teb );
2545 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2546 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2547 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2548 thread_data->gs = wine_get_gs();
2550 #ifdef __GNUC__
2551 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2552 #else
2553 FIXME("FPU setup not implemented for this platform.\n");
2554 #endif
2557 /**********************************************************************
2558 * signal_init_process
2560 void signal_init_process( CONTEXT *context, LPTHREAD_START_ROUTINE entry )
2562 struct sigaction sig_act;
2564 sig_act.sa_mask = server_block_set;
2565 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2566 #ifdef SA_ONSTACK
2567 sig_act.sa_flags |= SA_ONSTACK;
2568 #endif
2569 #ifdef __ANDROID__
2570 sig_act.sa_flags |= SA_RESTORER;
2571 sig_act.sa_restorer = rt_sigreturn;
2572 #endif
2573 sig_act.sa_sigaction = int_handler;
2574 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2575 sig_act.sa_sigaction = fpe_handler;
2576 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2577 sig_act.sa_sigaction = abrt_handler;
2578 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2579 sig_act.sa_sigaction = quit_handler;
2580 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2581 sig_act.sa_sigaction = usr1_handler;
2582 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2584 sig_act.sa_sigaction = segv_handler;
2585 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2586 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2587 #ifdef SIGBUS
2588 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2589 #endif
2591 #ifdef SIGTRAP
2592 sig_act.sa_sigaction = trap_handler;
2593 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2594 #endif
2596 #ifdef __HAVE_VM86
2597 sig_act.sa_sigaction = usr2_handler;
2598 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2599 #endif
2601 wine_ldt_init_locking( ldt_lock, ldt_unlock );
2603 /* build the initial context */
2604 context->ContextFlags = CONTEXT_FULL;
2605 context->SegCs = wine_get_cs();
2606 context->SegDs = wine_get_ds();
2607 context->SegEs = wine_get_es();
2608 context->SegFs = wine_get_fs();
2609 context->SegGs = wine_get_gs();
2610 context->SegSs = wine_get_ss();
2611 context->Eax = (DWORD)entry;
2612 context->Ebx = (DWORD)NtCurrentTeb()->Peb;
2613 context->Esp = (DWORD)NtCurrentTeb()->Tib.StackBase - 16;
2614 context->Eip = (DWORD)call_thread_entry_point;
2615 ((void **)context->Esp)[1] = kernel32_start_process;
2616 ((void **)context->Esp)[2] = entry;
2617 return;
2619 error:
2620 perror("sigaction");
2621 exit(1);
2625 #ifdef __HAVE_VM86
2626 /**********************************************************************
2627 * __wine_enter_vm86 (NTDLL.@)
2629 * Enter vm86 mode with the specified register context.
2631 void __wine_enter_vm86( CONTEXT *context )
2633 EXCEPTION_RECORD rec;
2634 int res;
2635 struct vm86plus_struct vm86;
2637 memset( &vm86, 0, sizeof(vm86) );
2638 for (;;)
2640 restore_vm86_context( context, &vm86 );
2642 x86_thread_data()->vm86_ptr = &vm86;
2643 merge_vm86_pending_flags( &rec );
2645 res = vm86_enter( &x86_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
2646 if (res < 0)
2648 errno = -res;
2649 return;
2652 save_vm86_context( context, &vm86 );
2654 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2655 rec.ExceptionRecord = NULL;
2656 rec.ExceptionAddress = (LPVOID)context->Eip;
2657 rec.NumberParameters = 0;
2659 switch(VM86_TYPE(res))
2661 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
2662 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
2663 break;
2664 case VM86_TRAP: /* return due to DOS-debugger request */
2665 switch(VM86_ARG(res))
2667 case TRAP_x86_TRCTRAP: /* Single-step exception */
2668 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
2669 break;
2670 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2671 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
2672 /* fall through */
2673 default:
2674 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
2675 break;
2677 break;
2678 case VM86_INTx: /* int3/int x instruction (ARG = x) */
2679 rec.ExceptionCode = EXCEPTION_VM86_INTx;
2680 rec.NumberParameters = 1;
2681 rec.ExceptionInformation[0] = VM86_ARG(res);
2682 break;
2683 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
2684 context->EFlags |= VIF_FLAG;
2685 context->EFlags &= ~VIP_FLAG;
2686 get_vm86_teb_info()->vm86_pending = 0;
2687 rec.ExceptionCode = EXCEPTION_VM86_STI;
2688 break;
2689 case VM86_PICRETURN: /* return due to pending PIC request */
2690 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
2691 break;
2692 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
2693 default:
2694 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
2695 continue;
2697 raise_exception( &rec, context, TRUE );
2701 #else /* __HAVE_VM86 */
2702 /**********************************************************************
2703 * __wine_enter_vm86 (NTDLL.@)
2705 void __wine_enter_vm86( CONTEXT *context )
2707 MESSAGE("vm86 mode not supported on this platform\n");
2709 #endif /* __HAVE_VM86 */
2712 /*******************************************************************
2713 * RtlUnwind (NTDLL.@)
2715 void WINAPI DECLSPEC_HIDDEN __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2716 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2718 EXCEPTION_RECORD record;
2719 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2720 DWORD res;
2722 context->Eax = (DWORD)retval;
2724 /* build an exception record, if we do not have one */
2725 if (!pRecord)
2727 record.ExceptionCode = STATUS_UNWIND;
2728 record.ExceptionFlags = 0;
2729 record.ExceptionRecord = NULL;
2730 record.ExceptionAddress = (void *)context->Eip;
2731 record.NumberParameters = 0;
2732 pRecord = &record;
2735 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2737 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2738 TRACE( "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
2739 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi );
2740 TRACE( "ebp=%08x esp=%08x eip=%08x cs=%04x ds=%04x fs=%04x gs=%04x flags=%08x\n",
2741 context->Ebp, context->Esp, context->Eip, LOWORD(context->SegCs), LOWORD(context->SegDs),
2742 LOWORD(context->SegFs), LOWORD(context->SegGs), context->EFlags );
2744 /* get chain of exception frames */
2745 frame = NtCurrentTeb()->Tib.ExceptionList;
2746 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2748 /* Check frame address */
2749 if (pEndFrame && (frame > pEndFrame))
2750 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2752 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, pRecord );
2754 /* Call handler */
2755 TRACE( "calling handler at %p code=%x flags=%x\n",
2756 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2757 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2758 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2760 switch(res)
2762 case ExceptionContinueSearch:
2763 break;
2764 case ExceptionCollidedUnwind:
2765 frame = dispatch;
2766 break;
2767 default:
2768 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2769 break;
2771 frame = __wine_pop_frame( frame );
2774 NtSetContextThread( GetCurrentThread(), context );
2776 __ASM_STDCALL_FUNC( RtlUnwind, 16,
2777 "pushl %ebp\n\t"
2778 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2779 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2780 "movl %esp,%ebp\n\t"
2781 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2782 "leal -(0x2cc+8)(%esp),%esp\n\t" /* sizeof(CONTEXT) + alignment */
2783 "pushl %esp\n\t" /* context */
2784 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2785 "leal 24(%ebp),%eax\n\t"
2786 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2787 "pushl %esp\n\t"
2788 "pushl 20(%ebp)\n\t"
2789 "pushl 16(%ebp)\n\t"
2790 "pushl 12(%ebp)\n\t"
2791 "pushl 8(%ebp)\n\t"
2792 "call " __ASM_NAME("__regs_RtlUnwind") __ASM_STDCALL(20) "\n\t"
2793 "leave\n\t"
2794 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2795 __ASM_CFI(".cfi_same_value %ebp\n\t")
2796 "ret $16" ) /* actually never returns */
2799 /*******************************************************************
2800 * NtRaiseException (NTDLL.@)
2802 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2804 NTSTATUS status = raise_exception( rec, context, first_chance );
2805 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2806 return status;
2810 /***********************************************************************
2811 * RtlRaiseException (NTDLL.@)
2813 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
2814 "pushl %ebp\n\t"
2815 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2816 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2817 "movl %esp,%ebp\n\t"
2818 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2819 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
2820 "pushl %esp\n\t" /* context */
2821 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2822 "movl 4(%ebp),%eax\n\t" /* return address */
2823 "movl 8(%ebp),%ecx\n\t" /* rec */
2824 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
2825 "leal 12(%ebp),%eax\n\t"
2826 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2827 "movl %esp,%eax\n\t"
2828 "pushl $1\n\t"
2829 "pushl %eax\n\t"
2830 "pushl %ecx\n\t"
2831 "call " __ASM_NAME("NtRaiseException") __ASM_STDCALL(12) "\n\t"
2832 "pushl %eax\n\t"
2833 "call " __ASM_NAME("RtlRaiseStatus") __ASM_STDCALL(4) "\n\t"
2834 "leave\n\t"
2835 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2836 __ASM_CFI(".cfi_same_value %ebp\n\t")
2837 "ret $4" ) /* actually never returns */
2840 /*************************************************************************
2841 * RtlCaptureStackBackTrace (NTDLL.@)
2843 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2845 CONTEXT context;
2846 ULONG i;
2847 ULONG *frame;
2849 RtlCaptureContext( &context );
2850 if (hash) *hash = 0;
2851 frame = (ULONG *)context.Ebp;
2853 while (skip--)
2855 if (!is_valid_frame( frame )) return 0;
2856 frame = (ULONG *)*frame;
2859 for (i = 0; i < count; i++)
2861 if (!is_valid_frame( frame )) break;
2862 buffer[i] = (void *)frame[1];
2863 if (hash) *hash += frame[1];
2864 frame = (ULONG *)*frame;
2866 return i;
2870 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
2871 __ASM_GLOBAL_FUNC( call_thread_entry_point,
2872 "pushl %ebp\n\t"
2873 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2874 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2875 "movl %esp,%ebp\n\t"
2876 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2877 "pushl %ebx\n\t"
2878 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2879 "pushl %esi\n\t"
2880 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2881 "pushl %edi\n\t"
2882 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2883 "pushl %ebp\n\t"
2884 "pushl 12(%ebp)\n\t"
2885 "pushl 8(%ebp)\n\t"
2886 "call " __ASM_NAME("call_thread_func") );
2888 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
2889 __ASM_GLOBAL_FUNC( call_thread_exit_func,
2890 "movl 4(%esp),%eax\n\t"
2891 "movl 8(%esp),%ecx\n\t"
2892 "movl 12(%esp),%ebp\n\t"
2893 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2894 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2895 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2896 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2897 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2898 "leal -20(%ebp),%esp\n\t"
2899 "pushl %eax\n\t"
2900 "call *%ecx" );
2902 /* wrapper for apps that don't declare the thread function correctly */
2903 extern void DECLSPEC_NORETURN call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry, void *arg );
2904 __ASM_GLOBAL_FUNC(call_thread_func_wrapper,
2905 "pushl %ebp\n\t"
2906 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2907 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2908 "movl %esp,%ebp\n\t"
2909 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2910 "subl $4,%esp\n\t"
2911 "pushl 12(%ebp)\n\t"
2912 "call *8(%ebp)\n\t"
2913 "leal -4(%ebp),%esp\n\t"
2914 "pushl %eax\n\t"
2915 "call " __ASM_NAME("exit_thread") "\n\t"
2916 "int $3" )
2918 /***********************************************************************
2919 * call_thread_func
2921 void DECLSPEC_HIDDEN call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
2923 x86_thread_data()->exit_frame = frame;
2924 __TRY
2926 call_thread_func_wrapper( entry, arg );
2928 __EXCEPT(unhandled_exception_filter)
2930 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2932 __ENDTRY
2933 abort(); /* should not be reached */
2936 /***********************************************************************
2937 * RtlExitUserThread (NTDLL.@)
2939 void WINAPI RtlExitUserThread( ULONG status )
2941 if (!x86_thread_data()->exit_frame) exit_thread( status );
2942 call_thread_exit_func( status, exit_thread, x86_thread_data()->exit_frame );
2945 /***********************************************************************
2946 * abort_thread
2948 void abort_thread( int status )
2950 if (!x86_thread_data()->exit_frame) terminate_thread( status );
2951 call_thread_exit_func( status, terminate_thread, x86_thread_data()->exit_frame );
2954 /**********************************************************************
2955 * DbgBreakPoint (NTDLL.@)
2957 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2959 /**********************************************************************
2960 * DbgUserBreakPoint (NTDLL.@)
2962 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2964 /**********************************************************************
2965 * NtCurrentTeb (NTDLL.@)
2967 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2970 /**************************************************************************
2971 * _chkstk (NTDLL.@)
2973 __ASM_STDCALL_FUNC( _chkstk, 0,
2974 "negl %eax\n\t"
2975 "addl %esp,%eax\n\t"
2976 "xchgl %esp,%eax\n\t"
2977 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2978 "movl %eax,0(%esp)\n\t"
2979 "ret" )
2981 /**************************************************************************
2982 * _alloca_probe (NTDLL.@)
2984 __ASM_STDCALL_FUNC( _alloca_probe, 0,
2985 "negl %eax\n\t"
2986 "addl %esp,%eax\n\t"
2987 "xchgl %esp,%eax\n\t"
2988 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2989 "movl %eax,0(%esp)\n\t"
2990 "ret" )
2993 /**********************************************************************
2994 * EXC_CallHandler (internal)
2996 * Some exception handlers depend on EBP to have a fixed position relative to
2997 * the exception frame.
2998 * Shrinker depends on (*1) doing what it does,
2999 * (*2) being the exact instruction it is and (*3) beginning with 0x64
3000 * (i.e. the %fs prefix to the movl instruction). It also depends on the
3001 * function calling the handler having only 5 parameters (*4).
3003 __ASM_GLOBAL_FUNC( EXC_CallHandler,
3004 "pushl %ebp\n\t"
3005 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3006 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3007 "movl %esp,%ebp\n\t"
3008 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3009 "pushl %ebx\n\t"
3010 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
3011 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
3012 "pushl 24(%ebp)\n\t"
3013 "pushl 20(%ebp)\n\t"
3014 "pushl 16(%ebp)\n\t"
3015 "pushl 12(%ebp)\n\t"
3016 "pushl 8(%ebp)\n\t"
3017 "call " __ASM_NAME("call_exception_handler") "\n\t"
3018 "popl %ebx\n\t"
3019 __ASM_CFI(".cfi_same_value %ebx\n\t")
3020 "leave\n"
3021 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3022 __ASM_CFI(".cfi_same_value %ebp\n\t")
3023 "ret" )
3024 __ASM_GLOBAL_FUNC(call_exception_handler,
3025 "pushl %ebp\n\t"
3026 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3027 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3028 "movl %esp,%ebp\n\t"
3029 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3030 "subl $12,%esp\n\t"
3031 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
3032 "pushl %edx\n\t" /* handler be handled by... */
3033 ".byte 0x64\n\t"
3034 "pushl (0)\n\t" /* nested_handler (passed in edx). */
3035 ".byte 0x64\n\t"
3036 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
3037 "pushl 20(%ebp)\n\t"
3038 "pushl 16(%ebp)\n\t"
3039 "pushl 12(%ebp)\n\t"
3040 "pushl 8(%ebp)\n\t"
3041 "movl 24(%ebp), %ecx\n\t" /* (*1) */
3042 "call *%ecx\n\t" /* call handler. (*2) */
3043 ".byte 0x64\n\t"
3044 "movl (0), %esp\n\t" /* restore previous... (*3) */
3045 ".byte 0x64\n\t"
3046 "popl (0)\n\t" /* exception frame. */
3047 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
3048 "popl %ebp\n\t"
3049 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3050 __ASM_CFI(".cfi_same_value %ebp\n\t")
3051 "ret $20" ) /* (*4) */
3053 #endif /* __i386__ */