ntdll: Add an assembly wrapper to return correct values for the current thread in...
[wine.git] / dlls / ntdll / signal_i386.c
blob9275ad9cd25eafd8319464f8e7ed38b2fa1d6051
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 extern void DECLSPEC_NORETURN __wine_restore_regs( const CONTEXT *context );
485 enum i386_trap_code
487 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
488 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
489 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
490 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
491 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
492 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
493 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
494 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
495 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
496 TRAP_x86_DNA = T_DNA, /* Device not available exception */
497 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
498 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
499 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
500 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
501 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
502 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
503 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
504 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
505 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
506 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
507 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
508 #else
509 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
510 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
511 TRAP_x86_NMI = 2, /* NMI interrupt */
512 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
513 TRAP_x86_OFLOW = 4, /* Overflow exception */
514 TRAP_x86_BOUND = 5, /* Bound range exception */
515 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
516 TRAP_x86_DNA = 7, /* Device not available exception */
517 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
518 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
519 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
520 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
521 TRAP_x86_STKFLT = 12, /* Stack fault */
522 TRAP_x86_PROTFLT = 13, /* General protection fault */
523 TRAP_x86_PAGEFLT = 14, /* Page fault */
524 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
525 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
526 TRAP_x86_MCHK = 18, /* Machine check exception */
527 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
528 otherwise Cache flush exception (via SIGSEV) */
529 #endif
532 struct x86_thread_data
534 DWORD fs; /* 1d4 TEB selector */
535 DWORD gs; /* 1d8 libc selector; update winebuild if you move this! */
536 DWORD dr0; /* 1dc debug registers */
537 DWORD dr1; /* 1e0 */
538 DWORD dr2; /* 1e4 */
539 DWORD dr3; /* 1e8 */
540 DWORD dr6; /* 1ec */
541 DWORD dr7; /* 1f0 */
542 void *exit_frame; /* 1f4 exit frame pointer */
543 #ifdef __HAVE_VM86
544 void *vm86_ptr; /* 1f8 data for vm86 mode */
545 WINE_VM86_TEB_INFO vm86; /* 1fc vm86 private data */
546 #endif
547 /* the ntdll_thread_data structure follows here */
550 C_ASSERT( offsetof( TEB, SystemReserved2 ) + offsetof( struct x86_thread_data, gs ) == 0x1d8 );
551 #ifdef __HAVE_VM86
552 C_ASSERT( offsetof( TEB, SystemReserved2 ) + offsetof( struct x86_thread_data, vm86 ) ==
553 offsetof( TEB, GdiTebBatch ) + offsetof( struct ntdll_thread_data, __vm86 ));
554 #endif
556 static inline struct x86_thread_data *x86_thread_data(void)
558 return (struct x86_thread_data *)NtCurrentTeb()->SystemReserved2;
561 /* Exception record for handling exceptions happening inside exception handlers */
562 typedef struct
564 EXCEPTION_REGISTRATION_RECORD frame;
565 EXCEPTION_REGISTRATION_RECORD *prevFrame;
566 } EXC_NESTED_FRAME;
568 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
569 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
570 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler );
572 /***********************************************************************
573 * dispatch_signal
575 static inline int dispatch_signal(unsigned int sig)
577 if (handlers[sig] == NULL) return 0;
578 return handlers[sig](sig);
582 /***********************************************************************
583 * get_trap_code
585 * Get the trap code for a signal.
587 static inline enum i386_trap_code get_trap_code( const ucontext_t *sigcontext )
589 #ifdef TRAP_sig
590 return TRAP_sig(sigcontext);
591 #else
592 return TRAP_x86_UNKNOWN; /* unknown trap code */
593 #endif
596 /***********************************************************************
597 * get_error_code
599 * Get the error code for a signal.
601 static inline WORD get_error_code( const ucontext_t *sigcontext )
603 #ifdef ERROR_sig
604 return ERROR_sig(sigcontext);
605 #else
606 return 0;
607 #endif
610 /***********************************************************************
611 * get_signal_stack
613 * Get the base of the signal stack for the current thread.
615 static inline void *get_signal_stack(void)
617 return (char *)NtCurrentTeb() + 4096;
621 /***********************************************************************
622 * get_current_teb
624 * Get the current teb based on the stack pointer.
626 static inline TEB *get_current_teb(void)
628 unsigned long esp;
629 __asm__("movl %%esp,%0" : "=g" (esp) );
630 return (TEB *)(esp & ~signal_stack_mask);
634 /*******************************************************************
635 * is_valid_frame
637 static inline BOOL is_valid_frame( void *frame )
639 if ((ULONG_PTR)frame & 3) return FALSE;
640 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
641 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
644 /*******************************************************************
645 * raise_handler
647 * Handler for exceptions happening inside a handler.
649 static DWORD raise_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
650 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
652 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
653 return ExceptionContinueSearch;
654 /* We shouldn't get here so we store faulty frame in dispatcher */
655 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
656 return ExceptionNestedException;
660 /*******************************************************************
661 * unwind_handler
663 * Handler for exceptions happening inside an unwind handler.
665 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
666 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
668 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
669 return ExceptionContinueSearch;
670 /* We shouldn't get here so we store faulty frame in dispatcher */
671 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
672 return ExceptionCollidedUnwind;
676 /**********************************************************************
677 * call_stack_handlers
679 * Call the stack handlers chain.
681 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
683 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
684 DWORD res;
686 frame = NtCurrentTeb()->Tib.ExceptionList;
687 nested_frame = NULL;
688 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
690 /* Check frame address */
691 if (!is_valid_frame( frame ))
693 rec->ExceptionFlags |= EH_STACK_INVALID;
694 break;
697 /* Call handler */
698 TRACE( "calling handler at %p code=%x flags=%x\n",
699 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
700 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, raise_handler );
701 TRACE( "handler at %p returned %x\n", frame->Handler, res );
703 if (frame == nested_frame)
705 /* no longer nested */
706 nested_frame = NULL;
707 rec->ExceptionFlags &= ~EH_NESTED_CALL;
710 switch(res)
712 case ExceptionContinueExecution:
713 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
714 return STATUS_NONCONTINUABLE_EXCEPTION;
715 case ExceptionContinueSearch:
716 break;
717 case ExceptionNestedException:
718 if (nested_frame < dispatch) nested_frame = dispatch;
719 rec->ExceptionFlags |= EH_NESTED_CALL;
720 break;
721 default:
722 return STATUS_INVALID_DISPOSITION;
724 frame = frame->Prev;
726 return STATUS_UNHANDLED_EXCEPTION;
730 /*******************************************************************
731 * raise_exception
733 * Implementation of NtRaiseException.
735 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
737 NTSTATUS status;
739 if (first_chance)
741 DWORD c;
743 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
744 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
745 context->Eip, GetCurrentThreadId() );
746 for (c = 0; c < rec->NumberParameters; c++)
747 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
748 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
750 if (rec->ExceptionInformation[1] >> 16)
751 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
752 rec->ExceptionAddress,
753 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
754 else
755 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
756 rec->ExceptionAddress,
757 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
759 else
761 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
762 context->Eax, context->Ebx, context->Ecx,
763 context->Edx, context->Esi, context->Edi );
764 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
765 context->Ebp, context->Esp, context->SegCs, context->SegDs,
766 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
768 status = send_debug_event( rec, TRUE, context );
769 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
770 return STATUS_SUCCESS;
772 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
773 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Eip--;
775 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
776 return STATUS_SUCCESS;
778 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
779 return status;
782 /* last chance exception */
784 status = send_debug_event( rec, FALSE, context );
785 if (status != DBG_CONTINUE)
787 if (rec->ExceptionFlags & EH_STACK_INVALID)
788 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
789 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
790 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
791 else
792 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
793 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
794 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
796 return STATUS_SUCCESS;
800 #ifdef __HAVE_VM86
801 /***********************************************************************
802 * save_vm86_context
804 * Set the register values from a vm86 structure.
806 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
808 context->ContextFlags = CONTEXT_FULL;
809 context->Eax = vm86->regs.eax;
810 context->Ebx = vm86->regs.ebx;
811 context->Ecx = vm86->regs.ecx;
812 context->Edx = vm86->regs.edx;
813 context->Esi = vm86->regs.esi;
814 context->Edi = vm86->regs.edi;
815 context->Esp = vm86->regs.esp;
816 context->Ebp = vm86->regs.ebp;
817 context->Eip = vm86->regs.eip;
818 context->SegCs = vm86->regs.cs;
819 context->SegDs = vm86->regs.ds;
820 context->SegEs = vm86->regs.es;
821 context->SegFs = vm86->regs.fs;
822 context->SegGs = vm86->regs.gs;
823 context->SegSs = vm86->regs.ss;
824 context->EFlags = vm86->regs.eflags;
828 /***********************************************************************
829 * restore_vm86_context
831 * Build a vm86 structure from the register values.
833 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
835 vm86->regs.eax = context->Eax;
836 vm86->regs.ebx = context->Ebx;
837 vm86->regs.ecx = context->Ecx;
838 vm86->regs.edx = context->Edx;
839 vm86->regs.esi = context->Esi;
840 vm86->regs.edi = context->Edi;
841 vm86->regs.esp = context->Esp;
842 vm86->regs.ebp = context->Ebp;
843 vm86->regs.eip = context->Eip;
844 vm86->regs.cs = context->SegCs;
845 vm86->regs.ds = context->SegDs;
846 vm86->regs.es = context->SegEs;
847 vm86->regs.fs = context->SegFs;
848 vm86->regs.gs = context->SegGs;
849 vm86->regs.ss = context->SegSs;
850 vm86->regs.eflags = context->EFlags;
854 /**********************************************************************
855 * merge_vm86_pending_flags
857 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
858 * raises exception if there are pending events and VIF flag
859 * has been turned on.
861 * Called from __wine_enter_vm86 because vm86_enter
862 * doesn't check for pending events.
864 * Called from raise_vm86_sti_exception to check for
865 * pending events in a signal safe way.
867 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
869 BOOL check_pending = TRUE;
870 struct vm86plus_struct *vm86 =
871 (struct vm86plus_struct*)x86_thread_data()->vm86_ptr;
874 * In order to prevent a race when SIGUSR2 occurs while
875 * we are returning from exception handler, pending events
876 * will be rechecked after each raised exception.
878 while (check_pending && get_vm86_teb_info()->vm86_pending)
880 check_pending = FALSE;
881 x86_thread_data()->vm86_ptr = NULL;
884 * If VIF is set, throw exception.
885 * Note that SIGUSR2 may turn VIF flag off so
886 * VIF check must occur only when TEB.vm86_ptr is NULL.
888 if (vm86->regs.eflags & VIF_FLAG)
890 CONTEXT vcontext;
891 save_vm86_context( &vcontext, vm86 );
893 rec->ExceptionCode = EXCEPTION_VM86_STI;
894 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
895 rec->ExceptionRecord = NULL;
896 rec->NumberParameters = 0;
897 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
899 vcontext.EFlags &= ~VIP_FLAG;
900 get_vm86_teb_info()->vm86_pending = 0;
901 raise_exception( rec, &vcontext, TRUE );
903 restore_vm86_context( &vcontext, vm86 );
904 check_pending = TRUE;
907 x86_thread_data()->vm86_ptr = vm86;
911 * Merge VIP flags in a signal safe way. This requires
912 * that the following operation compiles into atomic
913 * instruction.
915 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
917 #endif /* __HAVE_VM86 */
920 #ifdef __sun
922 /* We have to workaround two Solaris breakages:
923 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
924 * code crash badly.
925 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
926 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
929 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
930 __ASM_GLOBAL_FUNC( sigaction_syscall,
931 "movl $0x62,%eax\n\t"
932 "int $0x91\n\t"
933 "ret" )
935 /* assume the same libc handler is used for all signals */
936 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
938 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
940 struct x86_thread_data *thread_data;
942 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
944 thread_data = (struct x86_thread_data *)get_current_teb()->SystemReserved2;
945 wine_set_fs( thread_data->fs );
946 wine_set_gs( thread_data->gs );
948 libc_sigacthandler( signal, siginfo, sigcontext );
951 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
953 struct sigaction real_act;
955 if (sigaction( sig, new, old ) == -1) return -1;
957 /* retrieve the real handler and flags with a direct syscall */
958 sigaction_syscall( sig, NULL, &real_act );
959 libc_sigacthandler = real_act.sa_sigaction;
960 real_act.sa_sigaction = wine_sigacthandler;
961 sigaction_syscall( sig, &real_act, NULL );
962 return 0;
964 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
966 #endif
968 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
970 extern void clear_alignment_flag(void);
971 __ASM_GLOBAL_FUNC( clear_alignment_flag,
972 "pushfl\n\t"
973 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
974 "andl $~0x40000,(%esp)\n\t"
975 "popfl\n\t"
976 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
977 "ret" )
980 /***********************************************************************
981 * init_handler
983 * Handler initialization when the full context is not needed.
984 * Return the stack pointer to use for pushing the exception data.
986 static inline void *init_handler( const ucontext_t *sigcontext, WORD *fs, WORD *gs )
988 TEB *teb = get_current_teb();
990 clear_alignment_flag();
992 /* get %fs and %gs at time of the fault */
993 #ifdef FS_sig
994 *fs = LOWORD(FS_sig(sigcontext));
995 #else
996 *fs = wine_get_fs();
997 #endif
998 #ifdef GS_sig
999 *gs = LOWORD(GS_sig(sigcontext));
1000 #else
1001 *gs = wine_get_gs();
1002 #endif
1004 #ifndef __sun /* see above for Solaris handling */
1006 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
1007 wine_set_fs( thread_data->fs );
1008 wine_set_gs( thread_data->gs );
1010 #endif
1012 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
1013 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
1016 * Win16 or DOS protected mode. Note that during switch
1017 * from 16-bit mode to linear mode, CS may be set to system
1018 * segment before FS is restored. Fortunately, in this case
1019 * SS is still non-system segment. This is why both CS and SS
1020 * are checked.
1022 return teb->WOW32Reserved;
1024 return (void *)(ESP_sig(sigcontext) & ~3);
1028 /***********************************************************************
1029 * save_fpu
1031 * Save the thread FPU context.
1033 static inline void save_fpu( CONTEXT *context )
1035 #ifdef __GNUC__
1036 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1037 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
1038 #endif
1042 /***********************************************************************
1043 * restore_fpu
1045 * Restore the FPU context to a sigcontext.
1047 static inline void restore_fpu( const CONTEXT *context )
1049 FLOATING_SAVE_AREA float_status = context->FloatSave;
1050 /* reset the current interrupt status */
1051 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
1052 #ifdef __GNUC__
1053 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
1054 #endif /* __GNUC__ */
1058 /***********************************************************************
1059 * restore_fpux
1061 * Restore the FPU extended context to a sigcontext.
1063 static inline void restore_fpux( const CONTEXT *context )
1065 #ifdef __GNUC__
1066 /* we have to enforce alignment by hand */
1067 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
1068 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
1070 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
1071 /* reset the current interrupt status */
1072 state->StatusWord &= state->ControlWord | 0xff80;
1073 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
1074 #endif
1078 /***********************************************************************
1079 * fpux_to_fpu
1081 * Build a standard FPU context from an extended one.
1083 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
1085 unsigned int i, tag, stack_top;
1087 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
1088 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
1089 fpu->ErrorOffset = fpux->ErrorOffset;
1090 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
1091 fpu->DataOffset = fpux->DataOffset;
1092 fpu->DataSelector = fpux->DataSelector;
1093 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
1095 stack_top = (fpux->StatusWord >> 11) & 7;
1096 fpu->TagWord = 0xffff0000;
1097 for (i = 0; i < 8; i++)
1099 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
1100 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
1101 else
1103 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
1104 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
1106 tag = 2; /* special */
1108 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
1110 if (reg->Low) tag = 2; /* special */
1111 else tag = 1; /* zero */
1113 else
1115 if (reg->Low >> 63) tag = 0; /* valid */
1116 else tag = 2; /* special */
1119 fpu->TagWord |= tag << (2 * i);
1124 /***********************************************************************
1125 * save_context
1127 * Build a context structure from the signal info.
1129 static inline void save_context( CONTEXT *context, const ucontext_t *sigcontext, WORD fs, WORD gs )
1131 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1132 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1134 memset(context, 0, sizeof(*context));
1135 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
1136 context->Eax = EAX_sig(sigcontext);
1137 context->Ebx = EBX_sig(sigcontext);
1138 context->Ecx = ECX_sig(sigcontext);
1139 context->Edx = EDX_sig(sigcontext);
1140 context->Esi = ESI_sig(sigcontext);
1141 context->Edi = EDI_sig(sigcontext);
1142 context->Ebp = EBP_sig(sigcontext);
1143 context->EFlags = EFL_sig(sigcontext);
1144 context->Eip = EIP_sig(sigcontext);
1145 context->Esp = ESP_sig(sigcontext);
1146 context->SegCs = LOWORD(CS_sig(sigcontext));
1147 context->SegDs = LOWORD(DS_sig(sigcontext));
1148 context->SegEs = LOWORD(ES_sig(sigcontext));
1149 context->SegFs = fs;
1150 context->SegGs = gs;
1151 context->SegSs = LOWORD(SS_sig(sigcontext));
1152 context->Dr0 = x86_thread_data()->dr0;
1153 context->Dr1 = x86_thread_data()->dr1;
1154 context->Dr2 = x86_thread_data()->dr2;
1155 context->Dr3 = x86_thread_data()->dr3;
1156 context->Dr6 = x86_thread_data()->dr6;
1157 context->Dr7 = x86_thread_data()->dr7;
1159 if (fpu)
1161 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1162 context->FloatSave = *fpu;
1164 if (fpux)
1166 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
1167 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
1168 fpux_support = TRUE;
1169 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
1171 if (!fpu && !fpux) save_fpu( context );
1175 /***********************************************************************
1176 * restore_context
1178 * Restore the signal info from the context.
1180 static inline void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1182 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1183 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1185 x86_thread_data()->dr0 = context->Dr0;
1186 x86_thread_data()->dr1 = context->Dr1;
1187 x86_thread_data()->dr2 = context->Dr2;
1188 x86_thread_data()->dr3 = context->Dr3;
1189 x86_thread_data()->dr6 = context->Dr6;
1190 x86_thread_data()->dr7 = context->Dr7;
1191 EAX_sig(sigcontext) = context->Eax;
1192 EBX_sig(sigcontext) = context->Ebx;
1193 ECX_sig(sigcontext) = context->Ecx;
1194 EDX_sig(sigcontext) = context->Edx;
1195 ESI_sig(sigcontext) = context->Esi;
1196 EDI_sig(sigcontext) = context->Edi;
1197 EBP_sig(sigcontext) = context->Ebp;
1198 EFL_sig(sigcontext) = context->EFlags;
1199 EIP_sig(sigcontext) = context->Eip;
1200 ESP_sig(sigcontext) = context->Esp;
1201 CS_sig(sigcontext) = context->SegCs;
1202 DS_sig(sigcontext) = context->SegDs;
1203 ES_sig(sigcontext) = context->SegEs;
1204 SS_sig(sigcontext) = context->SegSs;
1205 #ifdef GS_sig
1206 GS_sig(sigcontext) = context->SegGs;
1207 #else
1208 wine_set_gs( context->SegGs );
1209 #endif
1210 #ifdef FS_sig
1211 FS_sig(sigcontext) = context->SegFs;
1212 #else
1213 wine_set_fs( context->SegFs );
1214 #endif
1216 if (fpu) *fpu = context->FloatSave;
1217 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
1218 if (!fpu && !fpux) restore_fpu( context );
1222 /***********************************************************************
1223 * RtlCaptureContext (NTDLL.@)
1225 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
1226 "pushl %eax\n\t"
1227 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1228 "movl 8(%esp),%eax\n\t" /* context */
1229 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1230 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1231 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1232 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1233 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1234 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1235 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1236 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1237 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1238 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1239 "movl %ebp,0xb4(%eax)\n\t" /* context->Ebp */
1240 "movl 4(%esp),%edx\n\t"
1241 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1242 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1243 "pushfl\n\t"
1244 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1245 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1246 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1247 "leal 8(%esp),%edx\n\t"
1248 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1249 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1250 "popl 0xb0(%eax)\n\t" /* context->Eax */
1251 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1252 "ret $4" )
1255 /***********************************************************************
1256 * set_cpu_context
1258 * Set the new CPU context. Used by NtSetContextThread.
1260 static void set_cpu_context( const CONTEXT *context )
1262 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1264 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
1265 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
1267 if (flags & CONTEXT_DEBUG_REGISTERS)
1269 x86_thread_data()->dr0 = context->Dr0;
1270 x86_thread_data()->dr1 = context->Dr1;
1271 x86_thread_data()->dr2 = context->Dr2;
1272 x86_thread_data()->dr3 = context->Dr3;
1273 x86_thread_data()->dr6 = context->Dr6;
1274 x86_thread_data()->dr7 = context->Dr7;
1276 if (flags & CONTEXT_FULL)
1278 if (!(flags & CONTEXT_CONTROL))
1279 FIXME( "setting partial context (%x) not supported\n", flags );
1280 else if (flags & CONTEXT_SEGMENTS)
1281 __wine_restore_regs( context );
1282 else
1284 CONTEXT newcontext = *context;
1285 newcontext.SegDs = wine_get_ds();
1286 newcontext.SegEs = wine_get_es();
1287 newcontext.SegFs = wine_get_fs();
1288 newcontext.SegGs = wine_get_gs();
1289 __wine_restore_regs( &newcontext );
1295 /***********************************************************************
1296 * context_to_server
1298 * Convert a register context to the server format.
1300 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1302 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1304 memset( to, 0, sizeof(*to) );
1305 to->cpu = CPU_x86;
1307 if (flags & CONTEXT_CONTROL)
1309 to->flags |= SERVER_CTX_CONTROL;
1310 to->ctl.i386_regs.ebp = from->Ebp;
1311 to->ctl.i386_regs.esp = from->Esp;
1312 to->ctl.i386_regs.eip = from->Eip;
1313 to->ctl.i386_regs.cs = from->SegCs;
1314 to->ctl.i386_regs.ss = from->SegSs;
1315 to->ctl.i386_regs.eflags = from->EFlags;
1317 if (flags & CONTEXT_INTEGER)
1319 to->flags |= SERVER_CTX_INTEGER;
1320 to->integer.i386_regs.eax = from->Eax;
1321 to->integer.i386_regs.ebx = from->Ebx;
1322 to->integer.i386_regs.ecx = from->Ecx;
1323 to->integer.i386_regs.edx = from->Edx;
1324 to->integer.i386_regs.esi = from->Esi;
1325 to->integer.i386_regs.edi = from->Edi;
1327 if (flags & CONTEXT_SEGMENTS)
1329 to->flags |= SERVER_CTX_SEGMENTS;
1330 to->seg.i386_regs.ds = from->SegDs;
1331 to->seg.i386_regs.es = from->SegEs;
1332 to->seg.i386_regs.fs = from->SegFs;
1333 to->seg.i386_regs.gs = from->SegGs;
1335 if (flags & CONTEXT_FLOATING_POINT)
1337 to->flags |= SERVER_CTX_FLOATING_POINT;
1338 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1339 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1340 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1341 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1342 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1343 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1344 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1345 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1346 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1348 if (flags & CONTEXT_DEBUG_REGISTERS)
1350 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1351 to->debug.i386_regs.dr0 = from->Dr0;
1352 to->debug.i386_regs.dr1 = from->Dr1;
1353 to->debug.i386_regs.dr2 = from->Dr2;
1354 to->debug.i386_regs.dr3 = from->Dr3;
1355 to->debug.i386_regs.dr6 = from->Dr6;
1356 to->debug.i386_regs.dr7 = from->Dr7;
1358 if (flags & CONTEXT_EXTENDED_REGISTERS)
1360 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1361 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1363 return STATUS_SUCCESS;
1367 /***********************************************************************
1368 * context_from_server
1370 * Convert a register context from the server format.
1372 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1374 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1376 to->ContextFlags = CONTEXT_i386;
1377 if (from->flags & SERVER_CTX_CONTROL)
1379 to->ContextFlags |= CONTEXT_CONTROL;
1380 to->Ebp = from->ctl.i386_regs.ebp;
1381 to->Esp = from->ctl.i386_regs.esp;
1382 to->Eip = from->ctl.i386_regs.eip;
1383 to->SegCs = from->ctl.i386_regs.cs;
1384 to->SegSs = from->ctl.i386_regs.ss;
1385 to->EFlags = from->ctl.i386_regs.eflags;
1387 if (from->flags & SERVER_CTX_INTEGER)
1389 to->ContextFlags |= CONTEXT_INTEGER;
1390 to->Eax = from->integer.i386_regs.eax;
1391 to->Ebx = from->integer.i386_regs.ebx;
1392 to->Ecx = from->integer.i386_regs.ecx;
1393 to->Edx = from->integer.i386_regs.edx;
1394 to->Esi = from->integer.i386_regs.esi;
1395 to->Edi = from->integer.i386_regs.edi;
1397 if (from->flags & SERVER_CTX_SEGMENTS)
1399 to->ContextFlags |= CONTEXT_SEGMENTS;
1400 to->SegDs = from->seg.i386_regs.ds;
1401 to->SegEs = from->seg.i386_regs.es;
1402 to->SegFs = from->seg.i386_regs.fs;
1403 to->SegGs = from->seg.i386_regs.gs;
1405 if (from->flags & SERVER_CTX_FLOATING_POINT)
1407 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1408 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1409 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1410 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1411 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1412 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1413 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1414 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1415 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1416 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1418 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1420 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1421 to->Dr0 = from->debug.i386_regs.dr0;
1422 to->Dr1 = from->debug.i386_regs.dr1;
1423 to->Dr2 = from->debug.i386_regs.dr2;
1424 to->Dr3 = from->debug.i386_regs.dr3;
1425 to->Dr6 = from->debug.i386_regs.dr6;
1426 to->Dr7 = from->debug.i386_regs.dr7;
1428 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1430 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1431 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1433 return STATUS_SUCCESS;
1437 /***********************************************************************
1438 * NtSetContextThread (NTDLL.@)
1439 * ZwSetContextThread (NTDLL.@)
1441 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
1443 NTSTATUS ret = STATUS_SUCCESS;
1444 BOOL self = (handle == GetCurrentThread());
1446 /* debug registers require a server call */
1447 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
1448 self = (x86_thread_data()->dr0 == context->Dr0 &&
1449 x86_thread_data()->dr1 == context->Dr1 &&
1450 x86_thread_data()->dr2 == context->Dr2 &&
1451 x86_thread_data()->dr3 == context->Dr3 &&
1452 x86_thread_data()->dr6 == context->Dr6 &&
1453 x86_thread_data()->dr7 == context->Dr7);
1455 if (!self) ret = set_thread_context( handle, context, &self );
1457 if (self && ret == STATUS_SUCCESS) set_cpu_context( context );
1458 return ret;
1462 /***********************************************************************
1463 * NtGetContextThread (NTDLL.@)
1464 * ZwGetContextThread (NTDLL.@)
1466 * Note: we use a small assembly wrapper to save the necessary registers
1467 * in case we are fetching the context of the current thread.
1469 NTSTATUS CDECL __regs_NtGetContextThread( DWORD edi, DWORD esi, DWORD ebx, DWORD eflags,
1470 DWORD ebp, DWORD retaddr, HANDLE handle, CONTEXT *context )
1472 NTSTATUS ret;
1473 DWORD needed_flags = context->ContextFlags & ~CONTEXT_i386;
1474 BOOL self = (handle == GetCurrentThread());
1476 /* debug registers require a server call */
1477 if (needed_flags & CONTEXT_DEBUG_REGISTERS) self = FALSE;
1479 if (!self)
1481 if ((ret = get_thread_context( handle, context, &self ))) return ret;
1482 needed_flags &= ~context->ContextFlags;
1485 if (self)
1487 if (needed_flags & CONTEXT_INTEGER)
1489 context->Eax = 0;
1490 context->Ebx = ebx;
1491 context->Ecx = 0;
1492 context->Edx = 0;
1493 context->Esi = esi;
1494 context->Edi = edi;
1495 context->ContextFlags |= CONTEXT_INTEGER;
1497 if (needed_flags & CONTEXT_CONTROL)
1499 context->Ebp = ebp;
1500 context->Esp = (DWORD)&retaddr;
1501 context->Eip = *(&edi - 1);
1502 context->SegCs = wine_get_cs();
1503 context->SegSs = wine_get_ss();
1504 context->EFlags = eflags;
1505 context->ContextFlags |= CONTEXT_CONTROL;
1507 if (needed_flags & CONTEXT_SEGMENTS)
1509 context->SegDs = wine_get_ds();
1510 context->SegEs = wine_get_es();
1511 context->SegFs = wine_get_fs();
1512 context->SegGs = wine_get_gs();
1513 context->ContextFlags |= CONTEXT_SEGMENTS;
1515 if (needed_flags & CONTEXT_FLOATING_POINT) save_fpu( context );
1516 /* FIXME: extended floating point */
1517 /* update the cached version of the debug registers */
1518 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1520 x86_thread_data()->dr0 = context->Dr0;
1521 x86_thread_data()->dr1 = context->Dr1;
1522 x86_thread_data()->dr2 = context->Dr2;
1523 x86_thread_data()->dr3 = context->Dr3;
1524 x86_thread_data()->dr6 = context->Dr6;
1525 x86_thread_data()->dr7 = context->Dr7;
1529 if (context->ContextFlags & (CONTEXT_INTEGER & ~CONTEXT_i386))
1530 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle,
1531 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi );
1532 if (context->ContextFlags & (CONTEXT_CONTROL & ~CONTEXT_i386))
1533 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle,
1534 context->Ebp, context->Esp, context->Eip, context->SegCs, context->SegSs, context->EFlags );
1535 if (context->ContextFlags & (CONTEXT_SEGMENTS & ~CONTEXT_i386))
1536 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle,
1537 context->SegCs, context->SegDs, context->SegEs, context->SegFs );
1538 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1539 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle,
1540 context->Dr0, context->Dr1, context->Dr2, context->Dr3, context->Dr6, context->Dr7 );
1542 return STATUS_SUCCESS;
1544 __ASM_STDCALL_FUNC( NtGetContextThread, 8,
1545 "pushl %ebp\n\t"
1546 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1547 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1548 "movl %esp,%ebp\n\t"
1549 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1550 "pushfl\n\t"
1551 "pushl %ebx\n\t"
1552 __ASM_CFI(".cfi_rel_offset %ebx,-8\n\t")
1553 "pushl %esi\n\t"
1554 __ASM_CFI(".cfi_rel_offset %esi,-12\n\t")
1555 "pushl %edi\n\t"
1556 __ASM_CFI(".cfi_rel_offset %edi,-16\n\t")
1557 "call " __ASM_NAME("__regs_NtGetContextThread") "\n\t"
1558 "leave\n\t"
1559 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1560 __ASM_CFI(".cfi_same_value %ebp\n\t")
1561 "ret $8" )
1564 /***********************************************************************
1565 * is_privileged_instr
1567 * Check if the fault location is a privileged instruction.
1568 * Based on the instruction emulation code in dlls/kernel/instr.c.
1570 static inline DWORD is_privileged_instr( CONTEXT *context )
1572 const BYTE *instr;
1573 unsigned int prefix_count = 0;
1575 if (!wine_ldt_is_system( context->SegCs )) return 0;
1576 instr = (BYTE *)context->Eip;
1578 for (;;) switch(*instr)
1580 /* instruction prefixes */
1581 case 0x2e: /* %cs: */
1582 case 0x36: /* %ss: */
1583 case 0x3e: /* %ds: */
1584 case 0x26: /* %es: */
1585 case 0x64: /* %fs: */
1586 case 0x65: /* %gs: */
1587 case 0x66: /* opcode size */
1588 case 0x67: /* addr size */
1589 case 0xf0: /* lock */
1590 case 0xf2: /* repne */
1591 case 0xf3: /* repe */
1592 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1593 instr++;
1594 continue;
1596 case 0x0f: /* extended instruction */
1597 switch(instr[1])
1599 case 0x20: /* mov crX, reg */
1600 case 0x21: /* mov drX, reg */
1601 case 0x22: /* mov reg, crX */
1602 case 0x23: /* mov reg drX */
1603 return EXCEPTION_PRIV_INSTRUCTION;
1605 return 0;
1606 case 0x6c: /* insb (%dx) */
1607 case 0x6d: /* insl (%dx) */
1608 case 0x6e: /* outsb (%dx) */
1609 case 0x6f: /* outsl (%dx) */
1610 case 0xcd: /* int $xx */
1611 case 0xe4: /* inb al,XX */
1612 case 0xe5: /* in (e)ax,XX */
1613 case 0xe6: /* outb XX,al */
1614 case 0xe7: /* out XX,(e)ax */
1615 case 0xec: /* inb (%dx),%al */
1616 case 0xed: /* inl (%dx),%eax */
1617 case 0xee: /* outb %al,(%dx) */
1618 case 0xef: /* outl %eax,(%dx) */
1619 case 0xf4: /* hlt */
1620 case 0xfa: /* cli */
1621 case 0xfb: /* sti */
1622 return EXCEPTION_PRIV_INSTRUCTION;
1623 default:
1624 return 0;
1629 /***********************************************************************
1630 * handle_interrupt
1632 * Handle an interrupt.
1634 static inline BOOL handle_interrupt( unsigned int interrupt, EXCEPTION_RECORD *rec, CONTEXT *context )
1636 switch(interrupt)
1638 case 0x2d:
1639 context->Eip += 3;
1640 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1641 rec->ExceptionAddress = (void *)context->Eip;
1642 rec->NumberParameters = is_wow64 ? 1 : 3;
1643 rec->ExceptionInformation[0] = context->Eax;
1644 rec->ExceptionInformation[1] = context->Ecx;
1645 rec->ExceptionInformation[2] = context->Edx;
1646 return TRUE;
1647 default:
1648 return FALSE;
1653 /***********************************************************************
1654 * check_invalid_gs
1656 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1658 static inline BOOL check_invalid_gs( CONTEXT *context )
1660 unsigned int prefix_count = 0;
1661 const BYTE *instr = (BYTE *)context->Eip;
1662 WORD system_gs = x86_thread_data()->gs;
1664 if (context->SegGs == system_gs) return FALSE;
1665 if (!wine_ldt_is_system( context->SegCs )) return FALSE;
1666 /* only handle faults in system libraries */
1667 if (virtual_is_valid_code_address( instr, 1 )) return FALSE;
1669 for (;;) switch(*instr)
1671 /* instruction prefixes */
1672 case 0x2e: /* %cs: */
1673 case 0x36: /* %ss: */
1674 case 0x3e: /* %ds: */
1675 case 0x26: /* %es: */
1676 case 0x64: /* %fs: */
1677 case 0x66: /* opcode size */
1678 case 0x67: /* addr size */
1679 case 0xf0: /* lock */
1680 case 0xf2: /* repne */
1681 case 0xf3: /* repe */
1682 if (++prefix_count >= 15) return FALSE;
1683 instr++;
1684 continue;
1685 case 0x65: /* %gs: */
1686 TRACE( "%04x/%04x at %p, fixing up\n", context->SegGs, system_gs, instr );
1687 context->SegGs = system_gs;
1688 return TRUE;
1689 default:
1690 return FALSE;
1695 #include "pshpack1.h"
1696 union atl_thunk
1698 struct
1700 DWORD movl; /* movl this,4(%esp) */
1701 DWORD this;
1702 BYTE jmp; /* jmp func */
1703 int func;
1704 } t1;
1705 struct
1707 BYTE movl; /* movl this,ecx */
1708 DWORD this;
1709 BYTE jmp; /* jmp func */
1710 int func;
1711 } t2;
1712 struct
1714 BYTE movl1; /* movl this,edx */
1715 DWORD this;
1716 BYTE movl2; /* movl func,ecx */
1717 DWORD func;
1718 WORD jmp; /* jmp ecx */
1719 } t3;
1720 struct
1722 BYTE movl1; /* movl this,ecx */
1723 DWORD this;
1724 BYTE movl2; /* movl func,eax */
1725 DWORD func;
1726 WORD jmp; /* jmp eax */
1727 } t4;
1728 struct
1730 DWORD inst1; /* pop ecx
1731 * pop eax
1732 * push ecx
1733 * jmp 4(%eax) */
1734 WORD inst2;
1735 } t5;
1737 #include "poppack.h"
1739 /**********************************************************************
1740 * check_atl_thunk
1742 * Check if code destination is an ATL thunk, and emulate it if so.
1744 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1746 const union atl_thunk *thunk = (const union atl_thunk *)rec->ExceptionInformation[1];
1747 union atl_thunk thunk_copy;
1748 SIZE_T thunk_len;
1750 thunk_len = virtual_uninterrupted_read_memory( thunk, &thunk_copy, sizeof(*thunk) );
1751 if (!thunk_len) return FALSE;
1753 if (thunk_len >= sizeof(thunk_copy.t1) && thunk_copy.t1.movl == 0x042444c7 &&
1754 thunk_copy.t1.jmp == 0xe9)
1756 if (virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1,
1757 &thunk_copy.t1.this, sizeof(DWORD) ) == sizeof(DWORD))
1759 context->Eip = (DWORD_PTR)(&thunk->t1.func + 1) + thunk_copy.t1.func;
1760 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1761 thunk, context->Eip, thunk_copy.t1.this );
1762 return TRUE;
1765 else if (thunk_len >= sizeof(thunk_copy.t2) && thunk_copy.t2.movl == 0xb9 &&
1766 thunk_copy.t2.jmp == 0xe9)
1768 context->Ecx = thunk_copy.t2.this;
1769 context->Eip = (DWORD_PTR)(&thunk->t2.func + 1) + thunk_copy.t2.func;
1770 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1771 thunk, context->Eip, context->Ecx );
1772 return TRUE;
1774 else if (thunk_len >= sizeof(thunk_copy.t3) && thunk_copy.t3.movl1 == 0xba &&
1775 thunk_copy.t3.movl2 == 0xb9 &&
1776 thunk_copy.t3.jmp == 0xe1ff)
1778 context->Edx = thunk_copy.t3.this;
1779 context->Ecx = thunk_copy.t3.func;
1780 context->Eip = thunk_copy.t3.func;
1781 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1782 thunk, context->Eip, context->Ecx, context->Edx );
1783 return TRUE;
1785 else if (thunk_len >= sizeof(thunk_copy.t4) && thunk_copy.t4.movl1 == 0xb9 &&
1786 thunk_copy.t4.movl2 == 0xb8 &&
1787 thunk_copy.t4.jmp == 0xe0ff)
1789 context->Ecx = thunk_copy.t4.this;
1790 context->Eax = thunk_copy.t4.func;
1791 context->Eip = thunk_copy.t4.func;
1792 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1793 thunk, context->Eip, context->Eax, context->Ecx );
1794 return TRUE;
1796 else if (thunk_len >= sizeof(thunk_copy.t5) && thunk_copy.t5.inst1 == 0xff515859 &&
1797 thunk_copy.t5.inst2 == 0x0460)
1799 DWORD func, stack[2];
1800 if (virtual_uninterrupted_read_memory( (DWORD *)context->Esp,
1801 stack, sizeof(stack) ) == sizeof(stack) &&
1802 virtual_uninterrupted_read_memory( (DWORD *)stack[1] + 1,
1803 &func, sizeof(DWORD) ) == sizeof(DWORD) &&
1804 virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1,
1805 &stack[0], sizeof(stack[0]) ) == sizeof(stack[0]))
1807 context->Ecx = stack[0];
1808 context->Eax = stack[1];
1809 context->Esp = context->Esp + sizeof(DWORD);
1810 context->Eip = func;
1811 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1812 thunk, context->Eip, context->Eax, context->Ecx, context->Esp );
1813 return TRUE;
1817 return FALSE;
1821 /***********************************************************************
1822 * setup_exception_record
1824 * Setup the exception record and context on the thread stack.
1826 static EXCEPTION_RECORD *setup_exception_record( ucontext_t *sigcontext, void *stack_ptr,
1827 WORD fs, WORD gs, raise_func func )
1829 struct stack_layout
1831 void *ret_addr; /* return address from raise_func */
1832 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1833 CONTEXT *context_ptr; /* second arg for raise_func */
1834 CONTEXT context;
1835 EXCEPTION_RECORD rec;
1836 DWORD ebp;
1837 DWORD eip;
1838 } *stack = stack_ptr;
1839 DWORD exception_code = 0;
1841 /* stack sanity checks */
1843 if ((char *)stack >= (char *)get_signal_stack() &&
1844 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1846 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1847 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1848 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1849 NtCurrentTeb()->Tib.StackBase );
1850 abort_thread(1);
1853 if (stack - 1 > stack || /* check for overflow in subtraction */
1854 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1855 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1857 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1858 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1859 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1860 NtCurrentTeb()->Tib.StackBase );
1862 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1864 /* stack overflow on last page, unrecoverable */
1865 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1866 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1867 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1868 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1869 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1870 abort_thread(1);
1872 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1874 /* stack access below stack limit, may be recoverable */
1875 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1876 else
1878 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1879 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1880 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1881 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1882 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1883 abort_thread(1);
1887 stack--; /* push the stack_layout structure */
1888 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1889 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1890 #elif defined(VALGRIND_MAKE_WRITABLE)
1891 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1892 #endif
1893 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1894 stack->rec_ptr = &stack->rec;
1895 stack->context_ptr = &stack->context;
1897 stack->rec.ExceptionRecord = NULL;
1898 stack->rec.ExceptionCode = exception_code;
1899 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1900 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1901 stack->rec.NumberParameters = 0;
1903 save_context( &stack->context, sigcontext, fs, gs );
1905 /* now modify the sigcontext to return to the raise function */
1906 ESP_sig(sigcontext) = (DWORD)stack;
1907 EIP_sig(sigcontext) = (DWORD)func;
1908 /* clear single-step, direction, and align check flag */
1909 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1910 CS_sig(sigcontext) = wine_get_cs();
1911 DS_sig(sigcontext) = wine_get_ds();
1912 ES_sig(sigcontext) = wine_get_es();
1913 FS_sig(sigcontext) = wine_get_fs();
1914 GS_sig(sigcontext) = wine_get_gs();
1915 SS_sig(sigcontext) = wine_get_ss();
1917 return stack->rec_ptr;
1921 /***********************************************************************
1922 * setup_exception
1924 * Setup a proper stack frame for the raise function, and modify the
1925 * sigcontext so that the return from the signal handler will call
1926 * the raise function.
1928 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1930 WORD fs, gs;
1931 void *stack = init_handler( sigcontext, &fs, &gs );
1933 return setup_exception_record( sigcontext, stack, fs, gs, func );
1937 /***********************************************************************
1938 * get_exception_context
1940 * Get a pointer to the context built by setup_exception.
1942 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1944 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1948 /**********************************************************************
1949 * get_fpu_code
1951 * Get the FPU exception code from the FPU status.
1953 static inline DWORD get_fpu_code( const CONTEXT *context )
1955 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1957 if (status & 0x01) /* IE */
1959 if (status & 0x40) /* SF */
1960 return EXCEPTION_FLT_STACK_CHECK;
1961 else
1962 return EXCEPTION_FLT_INVALID_OPERATION;
1964 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1965 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1966 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1967 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1968 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1969 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1973 /**********************************************************************
1974 * raise_segv_exception
1976 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1978 NTSTATUS status;
1980 switch(rec->ExceptionCode)
1982 case EXCEPTION_ACCESS_VIOLATION:
1983 if (rec->NumberParameters == 2)
1985 if (rec->ExceptionInformation[1] == 0xffffffff && check_invalid_gs( context ))
1986 goto done;
1987 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1988 rec->ExceptionInformation[0], FALSE )))
1989 goto done;
1990 if (rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
1991 rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT)
1993 ULONG flags;
1994 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
1995 &flags, sizeof(flags), NULL );
1997 if (!(flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION) && check_atl_thunk( rec, context ))
1998 goto done;
2000 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
2001 if (!(flags & MEM_EXECUTE_OPTION_DISABLE))
2002 rec->ExceptionInformation[0] = EXCEPTION_READ_FAULT;
2005 break;
2006 case EXCEPTION_DATATYPE_MISALIGNMENT:
2007 /* FIXME: pass through exception handler first? */
2008 if (context->EFlags & 0x00040000)
2010 /* Disable AC flag, return */
2011 context->EFlags &= ~0x00040000;
2012 goto done;
2014 break;
2015 case EXCEPTION_BREAKPOINT:
2016 if (!is_wow64)
2018 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
2019 * service is usually not recognized when called from usermode. */
2020 switch (rec->ExceptionInformation[0])
2022 case 1: /* BREAKPOINT_PRINT */
2023 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2024 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2025 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2026 goto done;
2029 break;
2031 status = NtRaiseException( rec, context, TRUE );
2032 raise_status( status, rec );
2033 done:
2034 set_cpu_context( context );
2038 /**********************************************************************
2039 * raise_trap_exception
2041 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2043 NTSTATUS status;
2045 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
2047 /* when single stepping can't tell whether this is a hw bp or a
2048 * single step interrupt. try to avoid as much overhead as possible
2049 * and only do a server call if there is any hw bp enabled. */
2051 if( !(context->EFlags & 0x100) || (x86_thread_data()->dr7 & 0xff) )
2053 /* (possible) hardware breakpoint, fetch the debug registers */
2054 DWORD saved_flags = context->ContextFlags;
2055 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
2056 NtGetContextThread(GetCurrentThread(), context);
2057 context->ContextFlags |= saved_flags; /* restore flags */
2060 context->EFlags &= ~0x100; /* clear single-step flag */
2063 status = NtRaiseException( rec, context, TRUE );
2064 raise_status( status, rec );
2068 /**********************************************************************
2069 * raise_generic_exception
2071 * Generic raise function for exceptions that don't need special treatment.
2073 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2075 NTSTATUS status;
2077 status = NtRaiseException( rec, context, TRUE );
2078 raise_status( status, rec );
2082 #ifdef __HAVE_VM86
2083 /**********************************************************************
2084 * raise_vm86_sti_exception
2086 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2088 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
2089 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
2091 if (x86_thread_data()->vm86_ptr)
2093 if (((char*)context->Eip >= (char*)vm86_return) &&
2094 ((char*)context->Eip <= (char*)vm86_return_end) &&
2095 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
2096 /* exiting from VM86, can't throw */
2097 goto done;
2099 merge_vm86_pending_flags( rec );
2101 else if (get_vm86_teb_info()->dpmi_vif &&
2102 !wine_ldt_is_system(context->SegCs) &&
2103 !wine_ldt_is_system(context->SegSs))
2105 /* Executing DPMI code and virtual interrupts are enabled. */
2106 get_vm86_teb_info()->vm86_pending = 0;
2107 NtRaiseException( rec, context, TRUE );
2109 done:
2110 set_cpu_context( context );
2114 /**********************************************************************
2115 * usr2_handler
2117 * Handler for SIGUSR2.
2118 * We use it to signal that the running __wine_enter_vm86() should
2119 * immediately set VIP_FLAG, causing pending events to be handled
2120 * as early as possible.
2122 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2124 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
2125 rec->ExceptionCode = EXCEPTION_VM86_STI;
2127 #endif /* __HAVE_VM86 */
2130 /**********************************************************************
2131 * segv_handler
2133 * Handler for SIGSEGV and related errors.
2135 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2137 WORD fs, gs;
2138 EXCEPTION_RECORD *rec;
2139 ucontext_t *context = sigcontext;
2140 void *stack = init_handler( sigcontext, &fs, &gs );
2142 /* check for exceptions on the signal stack caused by write watches */
2143 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
2144 (char *)stack >= (char *)get_signal_stack() &&
2145 (char *)stack < (char *)get_signal_stack() + signal_stack_size &&
2146 !virtual_handle_fault( siginfo->si_addr, (get_error_code(context) >> 1) & 0x09, TRUE ))
2148 return;
2151 /* check for page fault inside the thread stack */
2152 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
2153 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
2154 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
2155 virtual_handle_stack_fault( siginfo->si_addr ))
2157 /* check if this was the last guard page */
2158 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
2160 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2161 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2163 return;
2166 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2167 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
2169 switch(get_trap_code(context))
2171 case TRAP_x86_OFLOW: /* Overflow exception */
2172 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2173 break;
2174 case TRAP_x86_BOUND: /* Bound range exception */
2175 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2176 break;
2177 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2178 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2179 break;
2180 case TRAP_x86_STKFLT: /* Stack fault */
2181 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2182 break;
2183 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2184 case TRAP_x86_PROTFLT: /* General protection fault */
2185 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2187 CONTEXT *win_context = get_exception_context( rec );
2188 WORD err = get_error_code(context);
2189 if (!err && (rec->ExceptionCode = is_privileged_instr( win_context ))) break;
2190 if ((err & 7) == 2 && handle_interrupt( err >> 3, rec, win_context )) break;
2191 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2192 rec->NumberParameters = 2;
2193 rec->ExceptionInformation[0] = 0;
2194 /* if error contains a LDT selector, use that as fault address */
2195 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
2196 rec->ExceptionInformation[1] = err & ~7;
2197 else
2198 rec->ExceptionInformation[1] = 0xffffffff;
2200 break;
2201 case TRAP_x86_PAGEFLT: /* Page fault */
2202 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2203 rec->NumberParameters = 2;
2204 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
2205 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2206 break;
2207 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2208 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2209 break;
2210 default:
2211 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2212 /* fall through */
2213 case TRAP_x86_NMI: /* NMI interrupt */
2214 case TRAP_x86_DNA: /* Device not available exception */
2215 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2216 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2217 case TRAP_x86_MCHK: /* Machine check exception */
2218 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2219 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2220 break;
2225 /**********************************************************************
2226 * trap_handler
2228 * Handler for SIGTRAP.
2230 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2232 ucontext_t *context = sigcontext;
2233 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
2235 switch(get_trap_code(context))
2237 case TRAP_x86_TRCTRAP: /* Single-step exception */
2238 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2239 break;
2240 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2241 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2242 /* fall through */
2243 default:
2244 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2245 rec->NumberParameters = is_wow64 ? 1 : 3;
2246 rec->ExceptionInformation[0] = 0;
2247 rec->ExceptionInformation[1] = 0; /* FIXME */
2248 rec->ExceptionInformation[2] = 0; /* FIXME */
2249 break;
2254 /**********************************************************************
2255 * fpe_handler
2257 * Handler for SIGFPE.
2259 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2261 CONTEXT *win_context;
2262 ucontext_t *context = sigcontext;
2263 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
2265 win_context = get_exception_context( rec );
2267 switch(get_trap_code(context))
2269 case TRAP_x86_DIVIDE: /* Division by zero exception */
2270 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2271 break;
2272 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
2273 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2274 break;
2275 case TRAP_x86_ARITHTRAP: /* Floating point exception */
2276 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2277 rec->ExceptionCode = get_fpu_code( win_context );
2278 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
2279 break;
2280 case TRAP_x86_CACHEFLT: /* SIMD exception */
2281 /* TODO:
2282 * Behaviour only tested for divide-by-zero exceptions
2283 * Check for other SIMD exceptions as well */
2284 if(siginfo->si_code != FPE_FLTDIV && siginfo->si_code != FPE_FLTINV)
2285 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2286 siginfo->si_code);
2288 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
2289 rec->NumberParameters = 1;
2290 /* no idea what meaning is actually behind this but that's what native does */
2291 rec->ExceptionInformation[0] = 0;
2292 break;
2293 default:
2294 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2295 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2296 break;
2301 /**********************************************************************
2302 * int_handler
2304 * Handler for SIGINT.
2306 * FIXME: should not be calling external functions on the signal stack.
2308 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2310 WORD fs, gs;
2311 init_handler( sigcontext, &fs, &gs );
2312 if (!dispatch_signal(SIGINT))
2314 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2315 rec->ExceptionCode = CONTROL_C_EXIT;
2319 /**********************************************************************
2320 * abrt_handler
2322 * Handler for SIGABRT.
2324 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2326 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2327 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2328 rec->ExceptionFlags = EH_NONCONTINUABLE;
2332 /**********************************************************************
2333 * quit_handler
2335 * Handler for SIGQUIT.
2337 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2339 WORD fs, gs;
2340 init_handler( sigcontext, &fs, &gs );
2341 abort_thread(0);
2345 /**********************************************************************
2346 * usr1_handler
2348 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2350 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2352 CONTEXT context;
2353 WORD fs, gs;
2355 init_handler( sigcontext, &fs, &gs );
2356 save_context( &context, sigcontext, fs, gs );
2357 wait_suspend( &context );
2358 restore_context( &context, sigcontext );
2362 /***********************************************************************
2363 * __wine_set_signal_handler (NTDLL.@)
2365 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2367 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2368 if (handlers[sig] != NULL) return -2;
2369 handlers[sig] = wsh;
2370 return 0;
2374 /***********************************************************************
2375 * locking for LDT routines
2377 static RTL_CRITICAL_SECTION ldt_section;
2378 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
2380 0, 0, &ldt_section,
2381 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
2382 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
2384 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
2385 static sigset_t ldt_sigset;
2387 static void ldt_lock(void)
2389 sigset_t sigset;
2391 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
2392 RtlEnterCriticalSection( &ldt_section );
2393 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
2396 static void ldt_unlock(void)
2398 if (ldt_section.RecursionCount == 1)
2400 sigset_t sigset = ldt_sigset;
2401 RtlLeaveCriticalSection( &ldt_section );
2402 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
2404 else RtlLeaveCriticalSection( &ldt_section );
2408 /**********************************************************************
2409 * signal_alloc_thread
2411 NTSTATUS signal_alloc_thread( TEB **teb )
2413 static size_t sigstack_zero_bits;
2414 struct x86_thread_data *thread_data;
2415 SIZE_T size;
2416 void *addr = NULL;
2417 NTSTATUS status;
2419 if (!sigstack_zero_bits)
2421 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2422 /* find the first power of two not smaller than min_size */
2423 sigstack_zero_bits = 12;
2424 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2425 signal_stack_mask = (1 << sigstack_zero_bits) - 1;
2426 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2429 size = signal_stack_mask + 1;
2430 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
2431 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2433 *teb = addr;
2434 (*teb)->Tib.Self = &(*teb)->Tib;
2435 (*teb)->Tib.ExceptionList = (void *)~0UL;
2436 thread_data = (struct x86_thread_data *)(*teb)->SystemReserved2;
2437 if (!(thread_data->fs = wine_ldt_alloc_fs()))
2439 size = 0;
2440 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
2441 status = STATUS_TOO_MANY_THREADS;
2444 return status;
2448 /**********************************************************************
2449 * signal_free_thread
2451 void signal_free_thread( TEB *teb )
2453 SIZE_T size;
2454 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2456 if (thread_data) wine_ldt_free_fs( thread_data->fs );
2457 if (teb->DeallocationStack)
2459 size = 0;
2460 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2462 size = 0;
2463 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2467 /**********************************************************************
2468 * signal_init_thread
2470 void signal_init_thread( TEB *teb )
2472 const WORD fpu_cw = 0x27f;
2473 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2474 LDT_ENTRY fs_entry;
2475 stack_t ss;
2477 #ifdef __APPLE__
2478 int mib[2], val = 1;
2480 mib[0] = CTL_KERN;
2481 mib[1] = KERN_THALTSTACK;
2482 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2483 #endif
2485 ss.ss_sp = (char *)teb + teb_size;
2486 ss.ss_size = signal_stack_size;
2487 ss.ss_flags = 0;
2488 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2490 wine_ldt_set_base( &fs_entry, teb );
2491 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2492 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2493 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2494 thread_data->gs = wine_get_gs();
2496 #ifdef __GNUC__
2497 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2498 #else
2499 FIXME("FPU setup not implemented for this platform.\n");
2500 #endif
2503 /**********************************************************************
2504 * signal_init_process
2506 void signal_init_process(void)
2508 struct sigaction sig_act;
2510 sig_act.sa_mask = server_block_set;
2511 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2512 #ifdef SA_ONSTACK
2513 sig_act.sa_flags |= SA_ONSTACK;
2514 #endif
2515 #ifdef __ANDROID__
2516 sig_act.sa_flags |= SA_RESTORER;
2517 sig_act.sa_restorer = rt_sigreturn;
2518 #endif
2519 sig_act.sa_sigaction = int_handler;
2520 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2521 sig_act.sa_sigaction = fpe_handler;
2522 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2523 sig_act.sa_sigaction = abrt_handler;
2524 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2525 sig_act.sa_sigaction = quit_handler;
2526 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2527 sig_act.sa_sigaction = usr1_handler;
2528 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2530 sig_act.sa_sigaction = segv_handler;
2531 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2532 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2533 #ifdef SIGBUS
2534 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2535 #endif
2537 #ifdef SIGTRAP
2538 sig_act.sa_sigaction = trap_handler;
2539 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2540 #endif
2542 #ifdef __HAVE_VM86
2543 sig_act.sa_sigaction = usr2_handler;
2544 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2545 #endif
2547 wine_ldt_init_locking( ldt_lock, ldt_unlock );
2548 return;
2550 error:
2551 perror("sigaction");
2552 exit(1);
2556 #ifdef __HAVE_VM86
2557 /**********************************************************************
2558 * __wine_enter_vm86 (NTDLL.@)
2560 * Enter vm86 mode with the specified register context.
2562 void __wine_enter_vm86( CONTEXT *context )
2564 EXCEPTION_RECORD rec;
2565 int res;
2566 struct vm86plus_struct vm86;
2568 memset( &vm86, 0, sizeof(vm86) );
2569 for (;;)
2571 restore_vm86_context( context, &vm86 );
2573 x86_thread_data()->vm86_ptr = &vm86;
2574 merge_vm86_pending_flags( &rec );
2576 res = vm86_enter( &x86_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
2577 if (res < 0)
2579 errno = -res;
2580 return;
2583 save_vm86_context( context, &vm86 );
2585 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2586 rec.ExceptionRecord = NULL;
2587 rec.ExceptionAddress = (LPVOID)context->Eip;
2588 rec.NumberParameters = 0;
2590 switch(VM86_TYPE(res))
2592 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
2593 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
2594 break;
2595 case VM86_TRAP: /* return due to DOS-debugger request */
2596 switch(VM86_ARG(res))
2598 case TRAP_x86_TRCTRAP: /* Single-step exception */
2599 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
2600 break;
2601 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2602 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
2603 /* fall through */
2604 default:
2605 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
2606 break;
2608 break;
2609 case VM86_INTx: /* int3/int x instruction (ARG = x) */
2610 rec.ExceptionCode = EXCEPTION_VM86_INTx;
2611 rec.NumberParameters = 1;
2612 rec.ExceptionInformation[0] = VM86_ARG(res);
2613 break;
2614 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
2615 context->EFlags |= VIF_FLAG;
2616 context->EFlags &= ~VIP_FLAG;
2617 get_vm86_teb_info()->vm86_pending = 0;
2618 rec.ExceptionCode = EXCEPTION_VM86_STI;
2619 break;
2620 case VM86_PICRETURN: /* return due to pending PIC request */
2621 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
2622 break;
2623 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
2624 default:
2625 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
2626 continue;
2628 raise_exception( &rec, context, TRUE );
2632 #else /* __HAVE_VM86 */
2633 /**********************************************************************
2634 * __wine_enter_vm86 (NTDLL.@)
2636 void __wine_enter_vm86( CONTEXT *context )
2638 MESSAGE("vm86 mode not supported on this platform\n");
2640 #endif /* __HAVE_VM86 */
2643 /*******************************************************************
2644 * RtlUnwind (NTDLL.@)
2646 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2647 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2649 EXCEPTION_RECORD record;
2650 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2651 DWORD res;
2653 context->Eax = (DWORD)retval;
2655 /* build an exception record, if we do not have one */
2656 if (!pRecord)
2658 record.ExceptionCode = STATUS_UNWIND;
2659 record.ExceptionFlags = 0;
2660 record.ExceptionRecord = NULL;
2661 record.ExceptionAddress = (void *)context->Eip;
2662 record.NumberParameters = 0;
2663 pRecord = &record;
2666 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2668 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2670 /* get chain of exception frames */
2671 frame = NtCurrentTeb()->Tib.ExceptionList;
2672 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2674 /* Check frame address */
2675 if (pEndFrame && (frame > pEndFrame))
2676 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2678 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, pRecord );
2680 /* Call handler */
2681 TRACE( "calling handler at %p code=%x flags=%x\n",
2682 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2683 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2684 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2686 switch(res)
2688 case ExceptionContinueSearch:
2689 break;
2690 case ExceptionCollidedUnwind:
2691 frame = dispatch;
2692 break;
2693 default:
2694 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2695 break;
2697 frame = __wine_pop_frame( frame );
2700 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2703 /*******************************************************************
2704 * NtRaiseException (NTDLL.@)
2706 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2708 NTSTATUS status = raise_exception( rec, context, first_chance );
2709 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2710 return status;
2714 /***********************************************************************
2715 * RtlRaiseException (NTDLL.@)
2717 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
2718 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
2719 __ASM_CFI(".cfi_adjust_cfa_offset 0x2cc\n\t")
2720 "pushl %esp\n\t" /* context */
2721 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2722 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2723 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2724 "movl 0x2cc(%esp),%eax\n\t" /* return address */
2725 "movl 0x2d0(%esp),%ecx\n\t" /* rec */
2726 "movl %eax,0xb8(%esp)\n\t" /* context->Eip */
2727 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
2728 "leal 0x2d4(%esp),%eax\n\t"
2729 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2730 "movl %esp,%eax\n\t"
2731 "pushl $1\n\t"
2732 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2733 "pushl %eax\n\t"
2734 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2735 "pushl %ecx\n\t"
2736 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2737 "call " __ASM_NAME("NtRaiseException") __ASM_STDCALL(12) "\n\t"
2738 __ASM_CFI(".cfi_adjust_cfa_offset -12\n\t")
2739 "pushl %eax\n\t"
2740 "call " __ASM_NAME("RtlRaiseStatus") __ASM_STDCALL(4) "\n\t"
2741 "ret $4" ) /* actually never returns */
2744 /*************************************************************************
2745 * RtlCaptureStackBackTrace (NTDLL.@)
2747 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2749 CONTEXT context;
2750 ULONG i;
2751 ULONG *frame;
2753 RtlCaptureContext( &context );
2754 if (hash) *hash = 0;
2755 frame = (ULONG *)context.Ebp;
2757 while (skip--)
2759 if (!is_valid_frame( frame )) return 0;
2760 frame = (ULONG *)*frame;
2763 for (i = 0; i < count; i++)
2765 if (!is_valid_frame( frame )) break;
2766 buffer[i] = (void *)frame[1];
2767 if (hash) *hash += frame[1];
2768 frame = (ULONG *)*frame;
2770 return i;
2774 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
2775 __ASM_GLOBAL_FUNC( call_thread_entry_point,
2776 "pushl %ebp\n\t"
2777 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2778 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2779 "movl %esp,%ebp\n\t"
2780 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2781 "pushl %ebx\n\t"
2782 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2783 "pushl %esi\n\t"
2784 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2785 "pushl %edi\n\t"
2786 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2787 "pushl %ebp\n\t"
2788 "pushl 12(%ebp)\n\t"
2789 "pushl 8(%ebp)\n\t"
2790 "call " __ASM_NAME("call_thread_func") );
2792 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
2793 __ASM_GLOBAL_FUNC( call_thread_exit_func,
2794 "movl 4(%esp),%eax\n\t"
2795 "movl 8(%esp),%ecx\n\t"
2796 "movl 12(%esp),%ebp\n\t"
2797 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2798 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2799 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2800 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2801 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2802 "leal -20(%ebp),%esp\n\t"
2803 "pushl %eax\n\t"
2804 "call *%ecx" );
2806 /* wrapper for apps that don't declare the thread function correctly */
2807 extern void DECLSPEC_NORETURN call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry, void *arg );
2808 __ASM_GLOBAL_FUNC(call_thread_func_wrapper,
2809 "pushl %ebp\n\t"
2810 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2811 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2812 "movl %esp,%ebp\n\t"
2813 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2814 "subl $4,%esp\n\t"
2815 "pushl 12(%ebp)\n\t"
2816 "call *8(%ebp)\n\t"
2817 "leal -4(%ebp),%esp\n\t"
2818 "pushl %eax\n\t"
2819 "call " __ASM_NAME("exit_thread") "\n\t"
2820 "int $3" )
2822 /***********************************************************************
2823 * call_thread_func
2825 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
2827 x86_thread_data()->exit_frame = frame;
2828 __TRY
2830 call_thread_func_wrapper( entry, arg );
2832 __EXCEPT(unhandled_exception_filter)
2834 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2836 __ENDTRY
2837 abort(); /* should not be reached */
2840 /***********************************************************************
2841 * RtlExitUserThread (NTDLL.@)
2843 void WINAPI RtlExitUserThread( ULONG status )
2845 if (!x86_thread_data()->exit_frame) exit_thread( status );
2846 call_thread_exit_func( status, exit_thread, x86_thread_data()->exit_frame );
2849 /***********************************************************************
2850 * abort_thread
2852 void abort_thread( int status )
2854 if (!x86_thread_data()->exit_frame) terminate_thread( status );
2855 call_thread_exit_func( status, terminate_thread, x86_thread_data()->exit_frame );
2858 /**********************************************************************
2859 * DbgBreakPoint (NTDLL.@)
2861 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2863 /**********************************************************************
2864 * DbgUserBreakPoint (NTDLL.@)
2866 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2868 /**********************************************************************
2869 * NtCurrentTeb (NTDLL.@)
2871 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2874 /**************************************************************************
2875 * _chkstk (NTDLL.@)
2877 __ASM_STDCALL_FUNC( _chkstk, 0,
2878 "negl %eax\n\t"
2879 "addl %esp,%eax\n\t"
2880 "xchgl %esp,%eax\n\t"
2881 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2882 "movl %eax,0(%esp)\n\t"
2883 "ret" )
2885 /**************************************************************************
2886 * _alloca_probe (NTDLL.@)
2888 __ASM_STDCALL_FUNC( _alloca_probe, 0,
2889 "negl %eax\n\t"
2890 "addl %esp,%eax\n\t"
2891 "xchgl %esp,%eax\n\t"
2892 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2893 "movl %eax,0(%esp)\n\t"
2894 "ret" )
2897 /**********************************************************************
2898 * EXC_CallHandler (internal)
2900 * Some exception handlers depend on EBP to have a fixed position relative to
2901 * the exception frame.
2902 * Shrinker depends on (*1) doing what it does,
2903 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2904 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2905 * function calling the handler having only 5 parameters (*4).
2907 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2908 "pushl %ebp\n\t"
2909 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2910 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2911 "movl %esp,%ebp\n\t"
2912 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2913 "pushl %ebx\n\t"
2914 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2915 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2916 "pushl 24(%ebp)\n\t"
2917 "pushl 20(%ebp)\n\t"
2918 "pushl 16(%ebp)\n\t"
2919 "pushl 12(%ebp)\n\t"
2920 "pushl 8(%ebp)\n\t"
2921 "call " __ASM_NAME("call_exception_handler") "\n\t"
2922 "popl %ebx\n\t"
2923 __ASM_CFI(".cfi_same_value %ebx\n\t")
2924 "leave\n"
2925 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2926 __ASM_CFI(".cfi_same_value %ebp\n\t")
2927 "ret" )
2928 __ASM_GLOBAL_FUNC(call_exception_handler,
2929 "pushl %ebp\n\t"
2930 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2931 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2932 "movl %esp,%ebp\n\t"
2933 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2934 "subl $12,%esp\n\t"
2935 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2936 "pushl %edx\n\t" /* handler be handled by... */
2937 ".byte 0x64\n\t"
2938 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2939 ".byte 0x64\n\t"
2940 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2941 "pushl 20(%ebp)\n\t"
2942 "pushl 16(%ebp)\n\t"
2943 "pushl 12(%ebp)\n\t"
2944 "pushl 8(%ebp)\n\t"
2945 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2946 "call *%ecx\n\t" /* call handler. (*2) */
2947 ".byte 0x64\n\t"
2948 "movl (0), %esp\n\t" /* restore previous... (*3) */
2949 ".byte 0x64\n\t"
2950 "popl (0)\n\t" /* exception frame. */
2951 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2952 "popl %ebp\n\t"
2953 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2954 __ASM_CFI(".cfi_same_value %ebp\n\t")
2955 "ret $20" ) /* (*4) */
2957 #endif /* __i386__ */