wined3d: Use wined3d_texture_get_sub_resource_target() in wined3d_surface_upload_data().
[wine.git] / dlls / ntdll / signal_i386.c
blobfcadcf9cba4ebaef41ca1eaccb2eca8e5c99c33e
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_SIGNAL_H
46 # include <sys/signal.h>
47 #endif
48 #ifdef HAVE_SYS_SYSCTL_H
49 # include <sys/sysctl.h>
50 #endif
51 #ifdef HAVE_SYS_UCONTEXT_H
52 # include <sys/ucontext.h>
53 #endif
55 #include "ntstatus.h"
56 #define WIN32_NO_STATUS
57 #include "windef.h"
58 #include "wine/library.h"
59 #include "ntdll_misc.h"
60 #include "wine/exception.h"
61 #include "wine/debug.h"
63 #ifdef HAVE_VALGRIND_MEMCHECK_H
64 #include <valgrind/memcheck.h>
65 #endif
67 #undef ERR /* Solaris needs to define this */
69 WINE_DEFAULT_DEBUG_CHANNEL(seh);
70 WINE_DECLARE_DEBUG_CHANNEL(relay);
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 #ifdef __ANDROID__
181 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
182 void rt_sigreturn(void);
183 __ASM_GLOBAL_FUNC( rt_sigreturn,
184 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
185 "int $0x80" );
186 #endif
188 #elif defined (__BSDI__)
190 #include <machine/frame.h>
191 typedef struct trapframe ucontext_t;
193 #define EAX_sig(context) ((context)->tf_eax)
194 #define EBX_sig(context) ((context)->tf_ebx)
195 #define ECX_sig(context) ((context)->tf_ecx)
196 #define EDX_sig(context) ((context)->tf_edx)
197 #define ESI_sig(context) ((context)->tf_esi)
198 #define EDI_sig(context) ((context)->tf_edi)
199 #define EBP_sig(context) ((context)->tf_ebp)
201 #define CS_sig(context) ((context)->tf_cs)
202 #define DS_sig(context) ((context)->tf_ds)
203 #define ES_sig(context) ((context)->tf_es)
204 #define SS_sig(context) ((context)->tf_ss)
206 #define EFL_sig(context) ((context)->tf_eflags)
208 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
209 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
211 #define FPU_sig(context) NULL /* FIXME */
212 #define FPUX_sig(context) NULL /* FIXME */
214 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
216 #include <machine/trap.h>
218 #define EAX_sig(context) ((context)->uc_mcontext.mc_eax)
219 #define EBX_sig(context) ((context)->uc_mcontext.mc_ebx)
220 #define ECX_sig(context) ((context)->uc_mcontext.mc_ecx)
221 #define EDX_sig(context) ((context)->uc_mcontext.mc_edx)
222 #define ESI_sig(context) ((context)->uc_mcontext.mc_esi)
223 #define EDI_sig(context) ((context)->uc_mcontext.mc_edi)
224 #define EBP_sig(context) ((context)->uc_mcontext.mc_ebp)
226 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
227 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
228 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
229 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
230 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
231 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
233 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
234 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
235 #define EFL_sig(context) ((context)->uc_mcontext.mc_eflags)
237 #define EIP_sig(context) ((context)->uc_mcontext.mc_eip)
238 #define ESP_sig(context) ((context)->uc_mcontext.mc_esp)
240 #define FPU_sig(context) NULL /* FIXME */
241 #define FPUX_sig(context) NULL /* FIXME */
243 #elif defined (__OpenBSD__)
245 #define EAX_sig(context) ((context)->sc_eax)
246 #define EBX_sig(context) ((context)->sc_ebx)
247 #define ECX_sig(context) ((context)->sc_ecx)
248 #define EDX_sig(context) ((context)->sc_edx)
249 #define ESI_sig(context) ((context)->sc_esi)
250 #define EDI_sig(context) ((context)->sc_edi)
251 #define EBP_sig(context) ((context)->sc_ebp)
253 #define CS_sig(context) ((context)->sc_cs)
254 #define DS_sig(context) ((context)->sc_ds)
255 #define ES_sig(context) ((context)->sc_es)
256 #define FS_sig(context) ((context)->sc_fs)
257 #define GS_sig(context) ((context)->sc_gs)
258 #define SS_sig(context) ((context)->sc_ss)
260 #define TRAP_sig(context) ((context)->sc_trapno)
261 #define ERROR_sig(context) ((context)->sc_err)
262 #define EFL_sig(context) ((context)->sc_eflags)
264 #define EIP_sig(context) ((context)->sc_eip)
265 #define ESP_sig(context) ((context)->sc_esp)
267 #define FPU_sig(context) NULL /* FIXME */
268 #define FPUX_sig(context) NULL /* FIXME */
270 #define T_MCHK T_MACHK
271 #define T_XMMFLT T_XFTRAP
273 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
275 #ifdef _SCO_DS
276 #include <sys/regset.h>
277 #define gregs regs
278 #endif
280 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
281 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
282 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
283 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
284 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
285 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
286 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
288 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
289 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
290 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
291 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
293 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
294 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
296 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
298 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
299 #ifdef UESP
300 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
301 #elif defined(R_ESP)
302 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
303 #else
304 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
305 #endif
306 #ifdef ERR
307 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
308 #endif
309 #ifdef TRAPNO
310 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
311 #endif
313 #define FPU_sig(context) NULL /* FIXME */
314 #define FPUX_sig(context) NULL /* FIXME */
316 #elif defined (__APPLE__)
318 /* work around silly renaming of struct members in OS X 10.5 */
319 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
320 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
321 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
322 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
323 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
324 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
325 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
326 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
327 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
328 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
329 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
330 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
331 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
332 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
333 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
334 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
335 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
336 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
337 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
338 #define FPU_sig(context) NULL
339 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
340 #else
341 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
342 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
343 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
344 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
345 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
346 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
347 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
348 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
349 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
350 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
351 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
352 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
353 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
354 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
355 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
356 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
357 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
358 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
359 #define FPU_sig(context) NULL
360 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
361 #endif
363 #elif defined(__NetBSD__)
365 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
366 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
367 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
368 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
369 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
370 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
371 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
372 #define ESP_sig(context) _UC_MACHINE_SP(context)
374 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
375 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
376 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
377 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
378 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
379 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
381 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
382 #define EIP_sig(context) _UC_MACHINE_PC(context)
383 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
384 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
386 #define FPU_sig(context) NULL
387 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
389 #define T_MCHK T_MCA
390 #define T_XMMFLT T_XMM
392 #elif defined(__GNU__)
394 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
395 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
396 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
397 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
398 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
399 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
400 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
401 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
403 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
404 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
405 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
406 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
407 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
408 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
410 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
411 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
412 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
413 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
415 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
416 #define FPUX_sig(context) NULL
418 #else
419 #error You must define the signal context functions for your platform
420 #endif /* linux */
422 typedef int (*wine_signal_handler)(unsigned int sig);
424 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
425 static size_t signal_stack_mask;
426 static size_t signal_stack_size;
428 static wine_signal_handler handlers[256];
430 static BOOL fpux_support; /* whether the CPU supports extended fpu context */
432 enum i386_trap_code
434 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
435 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
436 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
437 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
438 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
439 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
440 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
441 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
442 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
443 TRAP_x86_DNA = T_DNA, /* Device not available exception */
444 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
445 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
446 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
447 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
448 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
449 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
450 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
451 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
452 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
453 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
454 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
455 #else
456 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
457 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
458 TRAP_x86_NMI = 2, /* NMI interrupt */
459 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
460 TRAP_x86_OFLOW = 4, /* Overflow exception */
461 TRAP_x86_BOUND = 5, /* Bound range exception */
462 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
463 TRAP_x86_DNA = 7, /* Device not available exception */
464 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
465 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
466 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
467 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
468 TRAP_x86_STKFLT = 12, /* Stack fault */
469 TRAP_x86_PROTFLT = 13, /* General protection fault */
470 TRAP_x86_PAGEFLT = 14, /* Page fault */
471 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
472 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
473 TRAP_x86_MCHK = 18, /* Machine check exception */
474 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
475 otherwise Cache flush exception (via SIGSEV) */
476 #endif
479 struct x86_thread_data
481 DWORD fs; /* 1d4 TEB selector */
482 DWORD gs; /* 1d8 libc selector; update winebuild if you move this! */
483 DWORD dr0; /* 1dc debug registers */
484 DWORD dr1; /* 1e0 */
485 DWORD dr2; /* 1e4 */
486 DWORD dr3; /* 1e8 */
487 DWORD dr6; /* 1ec */
488 DWORD dr7; /* 1f0 */
489 void *exit_frame; /* 1f4 exit frame pointer */
490 /* the ntdll_thread_data structure follows here */
493 C_ASSERT( offsetof( TEB, SystemReserved2 ) + offsetof( struct x86_thread_data, gs ) == 0x1d8 );
494 C_ASSERT( offsetof( TEB, SystemReserved2 ) + offsetof( struct x86_thread_data, exit_frame ) == 0x1f4 );
496 static inline struct x86_thread_data *x86_thread_data(void)
498 return (struct x86_thread_data *)NtCurrentTeb()->SystemReserved2;
501 /* Exception record for handling exceptions happening inside exception handlers */
502 typedef struct
504 EXCEPTION_REGISTRATION_RECORD frame;
505 EXCEPTION_REGISTRATION_RECORD *prevFrame;
506 } EXC_NESTED_FRAME;
508 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
509 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
510 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler );
512 /***********************************************************************
513 * dispatch_signal
515 static inline int dispatch_signal(unsigned int sig)
517 if (handlers[sig] == NULL) return 0;
518 return handlers[sig](sig);
522 /***********************************************************************
523 * get_trap_code
525 * Get the trap code for a signal.
527 static inline enum i386_trap_code get_trap_code( const ucontext_t *sigcontext )
529 #ifdef TRAP_sig
530 return TRAP_sig(sigcontext);
531 #else
532 return TRAP_x86_UNKNOWN; /* unknown trap code */
533 #endif
536 /***********************************************************************
537 * get_error_code
539 * Get the error code for a signal.
541 static inline WORD get_error_code( const ucontext_t *sigcontext )
543 #ifdef ERROR_sig
544 return ERROR_sig(sigcontext);
545 #else
546 return 0;
547 #endif
550 /***********************************************************************
551 * get_signal_stack
553 * Get the base of the signal stack for the current thread.
555 static inline void *get_signal_stack(void)
557 return (char *)NtCurrentTeb() + 4096;
561 /***********************************************************************
562 * get_current_teb
564 * Get the current teb based on the stack pointer.
566 static inline TEB *get_current_teb(void)
568 unsigned long esp;
569 __asm__("movl %%esp,%0" : "=g" (esp) );
570 return (TEB *)(esp & ~signal_stack_mask);
574 /*******************************************************************
575 * is_valid_frame
577 static inline BOOL is_valid_frame( void *frame )
579 if ((ULONG_PTR)frame & 3) return FALSE;
580 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
581 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
584 /*******************************************************************
585 * raise_handler
587 * Handler for exceptions happening inside a handler.
589 static DWORD raise_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
590 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
592 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
593 return ExceptionContinueSearch;
594 /* We shouldn't get here so we store faulty frame in dispatcher */
595 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
596 return ExceptionNestedException;
600 /*******************************************************************
601 * unwind_handler
603 * Handler for exceptions happening inside an unwind handler.
605 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
606 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
608 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
609 return ExceptionContinueSearch;
610 /* We shouldn't get here so we store faulty frame in dispatcher */
611 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
612 return ExceptionCollidedUnwind;
616 /**********************************************************************
617 * call_stack_handlers
619 * Call the stack handlers chain.
621 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
623 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
624 DWORD res;
626 frame = NtCurrentTeb()->Tib.ExceptionList;
627 nested_frame = NULL;
628 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
630 /* Check frame address */
631 if (!is_valid_frame( frame ))
633 rec->ExceptionFlags |= EH_STACK_INVALID;
634 break;
637 /* Call handler */
638 TRACE( "calling handler at %p code=%x flags=%x\n",
639 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
640 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, raise_handler );
641 TRACE( "handler at %p returned %x\n", frame->Handler, res );
643 if (frame == nested_frame)
645 /* no longer nested */
646 nested_frame = NULL;
647 rec->ExceptionFlags &= ~EH_NESTED_CALL;
650 switch(res)
652 case ExceptionContinueExecution:
653 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
654 return STATUS_NONCONTINUABLE_EXCEPTION;
655 case ExceptionContinueSearch:
656 break;
657 case ExceptionNestedException:
658 if (nested_frame < dispatch) nested_frame = dispatch;
659 rec->ExceptionFlags |= EH_NESTED_CALL;
660 break;
661 default:
662 return STATUS_INVALID_DISPOSITION;
664 frame = frame->Prev;
666 return STATUS_UNHANDLED_EXCEPTION;
670 /*******************************************************************
671 * raise_exception
673 * Implementation of NtRaiseException.
675 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
677 NTSTATUS status;
679 if (first_chance)
681 DWORD c;
683 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
684 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
685 context->Eip, GetCurrentThreadId() );
686 for (c = 0; c < rec->NumberParameters; c++)
687 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
688 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
690 if (rec->ExceptionInformation[1] >> 16)
691 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
692 rec->ExceptionAddress,
693 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
694 else
695 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
696 rec->ExceptionAddress,
697 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
699 else
701 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
702 context->Eax, context->Ebx, context->Ecx,
703 context->Edx, context->Esi, context->Edi );
704 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
705 context->Ebp, context->Esp, context->SegCs, context->SegDs,
706 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
708 status = send_debug_event( rec, TRUE, context );
709 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
710 return STATUS_SUCCESS;
712 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
713 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Eip--;
715 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
716 return STATUS_SUCCESS;
718 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
719 return status;
722 /* last chance exception */
724 status = send_debug_event( rec, FALSE, context );
725 if (status != DBG_CONTINUE)
727 if (rec->ExceptionFlags & EH_STACK_INVALID)
728 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
729 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
730 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
731 else
732 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
733 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
734 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
736 return STATUS_SUCCESS;
740 #ifdef __sun
742 /* We have to workaround two Solaris breakages:
743 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
744 * code crash badly.
745 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
746 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
749 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
750 __ASM_GLOBAL_FUNC( sigaction_syscall,
751 "movl $0x62,%eax\n\t"
752 "int $0x91\n\t"
753 "ret" )
755 /* assume the same libc handler is used for all signals */
756 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
758 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
760 struct x86_thread_data *thread_data;
762 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
764 thread_data = (struct x86_thread_data *)get_current_teb()->SystemReserved2;
765 wine_set_fs( thread_data->fs );
766 wine_set_gs( thread_data->gs );
768 libc_sigacthandler( signal, siginfo, sigcontext );
771 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
773 struct sigaction real_act;
775 if (sigaction( sig, new, old ) == -1) return -1;
777 /* retrieve the real handler and flags with a direct syscall */
778 sigaction_syscall( sig, NULL, &real_act );
779 libc_sigacthandler = real_act.sa_sigaction;
780 real_act.sa_sigaction = wine_sigacthandler;
781 sigaction_syscall( sig, &real_act, NULL );
782 return 0;
784 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
786 #endif
788 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
790 extern void clear_alignment_flag(void);
791 __ASM_GLOBAL_FUNC( clear_alignment_flag,
792 "pushfl\n\t"
793 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
794 "andl $~0x40000,(%esp)\n\t"
795 "popfl\n\t"
796 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
797 "ret" )
800 /***********************************************************************
801 * init_handler
803 * Handler initialization when the full context is not needed.
804 * Return the stack pointer to use for pushing the exception data.
806 static inline void *init_handler( const ucontext_t *sigcontext, WORD *fs, WORD *gs )
808 TEB *teb = get_current_teb();
810 clear_alignment_flag();
812 /* get %fs and %gs at time of the fault */
813 #ifdef FS_sig
814 *fs = LOWORD(FS_sig(sigcontext));
815 #else
816 *fs = wine_get_fs();
817 #endif
818 #ifdef GS_sig
819 *gs = LOWORD(GS_sig(sigcontext));
820 #else
821 *gs = wine_get_gs();
822 #endif
824 #ifndef __sun /* see above for Solaris handling */
826 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
827 wine_set_fs( thread_data->fs );
828 wine_set_gs( thread_data->gs );
830 #endif
832 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
833 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
836 * Win16 or DOS protected mode. Note that during switch
837 * from 16-bit mode to linear mode, CS may be set to system
838 * segment before FS is restored. Fortunately, in this case
839 * SS is still non-system segment. This is why both CS and SS
840 * are checked.
842 return teb->WOW32Reserved;
844 return (void *)(ESP_sig(sigcontext) & ~3);
848 /***********************************************************************
849 * save_fpu
851 * Save the thread FPU context.
853 static inline void save_fpu( CONTEXT *context )
855 #ifdef __GNUC__
856 context->ContextFlags |= CONTEXT_FLOATING_POINT;
857 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
858 #endif
862 /***********************************************************************
863 * restore_fpu
865 * Restore the FPU context to a sigcontext.
867 static inline void restore_fpu( const CONTEXT *context )
869 FLOATING_SAVE_AREA float_status = context->FloatSave;
870 /* reset the current interrupt status */
871 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
872 #ifdef __GNUC__
873 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
874 #endif /* __GNUC__ */
878 /***********************************************************************
879 * restore_fpux
881 * Restore the FPU extended context to a sigcontext.
883 static inline void restore_fpux( const CONTEXT *context )
885 #ifdef __GNUC__
886 /* we have to enforce alignment by hand */
887 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
888 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
890 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
891 /* reset the current interrupt status */
892 state->StatusWord &= state->ControlWord | 0xff80;
893 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
894 #endif
898 /***********************************************************************
899 * fpux_to_fpu
901 * Build a standard FPU context from an extended one.
903 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
905 unsigned int i, tag, stack_top;
907 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
908 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
909 fpu->ErrorOffset = fpux->ErrorOffset;
910 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
911 fpu->DataOffset = fpux->DataOffset;
912 fpu->DataSelector = fpux->DataSelector;
913 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
915 stack_top = (fpux->StatusWord >> 11) & 7;
916 fpu->TagWord = 0xffff0000;
917 for (i = 0; i < 8; i++)
919 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
920 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
921 else
923 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
924 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
926 tag = 2; /* special */
928 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
930 if (reg->Low) tag = 2; /* special */
931 else tag = 1; /* zero */
933 else
935 if (reg->Low >> 63) tag = 0; /* valid */
936 else tag = 2; /* special */
939 fpu->TagWord |= tag << (2 * i);
944 /***********************************************************************
945 * save_context
947 * Build a context structure from the signal info.
949 static inline void save_context( CONTEXT *context, const ucontext_t *sigcontext, WORD fs, WORD gs )
951 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
952 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
954 memset(context, 0, sizeof(*context));
955 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
956 context->Eax = EAX_sig(sigcontext);
957 context->Ebx = EBX_sig(sigcontext);
958 context->Ecx = ECX_sig(sigcontext);
959 context->Edx = EDX_sig(sigcontext);
960 context->Esi = ESI_sig(sigcontext);
961 context->Edi = EDI_sig(sigcontext);
962 context->Ebp = EBP_sig(sigcontext);
963 context->EFlags = EFL_sig(sigcontext);
964 context->Eip = EIP_sig(sigcontext);
965 context->Esp = ESP_sig(sigcontext);
966 context->SegCs = LOWORD(CS_sig(sigcontext));
967 context->SegDs = LOWORD(DS_sig(sigcontext));
968 context->SegEs = LOWORD(ES_sig(sigcontext));
969 context->SegFs = fs;
970 context->SegGs = gs;
971 context->SegSs = LOWORD(SS_sig(sigcontext));
972 context->Dr0 = x86_thread_data()->dr0;
973 context->Dr1 = x86_thread_data()->dr1;
974 context->Dr2 = x86_thread_data()->dr2;
975 context->Dr3 = x86_thread_data()->dr3;
976 context->Dr6 = x86_thread_data()->dr6;
977 context->Dr7 = x86_thread_data()->dr7;
979 if (fpu)
981 context->ContextFlags |= CONTEXT_FLOATING_POINT;
982 context->FloatSave = *fpu;
984 if (fpux)
986 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
987 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
988 fpux_support = TRUE;
989 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
991 if (!fpu && !fpux) save_fpu( context );
995 /***********************************************************************
996 * restore_context
998 * Restore the signal info from the context.
1000 static inline void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1002 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1003 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1005 x86_thread_data()->dr0 = context->Dr0;
1006 x86_thread_data()->dr1 = context->Dr1;
1007 x86_thread_data()->dr2 = context->Dr2;
1008 x86_thread_data()->dr3 = context->Dr3;
1009 x86_thread_data()->dr6 = context->Dr6;
1010 x86_thread_data()->dr7 = context->Dr7;
1011 EAX_sig(sigcontext) = context->Eax;
1012 EBX_sig(sigcontext) = context->Ebx;
1013 ECX_sig(sigcontext) = context->Ecx;
1014 EDX_sig(sigcontext) = context->Edx;
1015 ESI_sig(sigcontext) = context->Esi;
1016 EDI_sig(sigcontext) = context->Edi;
1017 EBP_sig(sigcontext) = context->Ebp;
1018 EFL_sig(sigcontext) = context->EFlags;
1019 EIP_sig(sigcontext) = context->Eip;
1020 ESP_sig(sigcontext) = context->Esp;
1021 CS_sig(sigcontext) = context->SegCs;
1022 DS_sig(sigcontext) = context->SegDs;
1023 ES_sig(sigcontext) = context->SegEs;
1024 SS_sig(sigcontext) = context->SegSs;
1025 #ifdef GS_sig
1026 GS_sig(sigcontext) = context->SegGs;
1027 #else
1028 wine_set_gs( context->SegGs );
1029 #endif
1030 #ifdef FS_sig
1031 FS_sig(sigcontext) = context->SegFs;
1032 #else
1033 wine_set_fs( context->SegFs );
1034 #endif
1036 if (fpu) *fpu = context->FloatSave;
1037 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
1038 if (!fpu && !fpux) restore_fpu( context );
1042 /***********************************************************************
1043 * RtlCaptureContext (NTDLL.@)
1045 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
1046 "pushl %eax\n\t"
1047 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1048 "movl 8(%esp),%eax\n\t" /* context */
1049 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1050 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1051 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1052 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1053 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1054 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1055 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1056 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1057 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1058 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1059 "movl 0(%ebp),%edx\n\t"
1060 "movl %edx,0xb4(%eax)\n\t" /* context->Ebp */
1061 "movl 4(%ebp),%edx\n\t"
1062 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1063 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1064 "pushfl\n\t"
1065 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1066 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1067 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1068 "leal 8(%ebp),%edx\n\t"
1069 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1070 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1071 "popl 0xb0(%eax)\n\t" /* context->Eax */
1072 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1073 "ret $4" )
1075 /***********************************************************************
1076 * set_full_cpu_context
1078 * Set the new CPU context.
1080 extern void set_full_cpu_context( const CONTEXT *context );
1081 __ASM_GLOBAL_FUNC( set_full_cpu_context,
1082 "movl 4(%esp),%ecx\n\t"
1083 "movw 0x8c(%ecx),%gs\n\t" /* SegGs */
1084 "movw 0x90(%ecx),%fs\n\t" /* SegFs */
1085 "movw 0x94(%ecx),%es\n\t" /* SegEs */
1086 "movl 0x9c(%ecx),%edi\n\t" /* Edi */
1087 "movl 0xa0(%ecx),%esi\n\t" /* Esi */
1088 "movl 0xa4(%ecx),%ebx\n\t" /* Ebx */
1089 "movl 0xb4(%ecx),%ebp\n\t" /* Ebp */
1090 "movw %ss,%ax\n\t"
1091 "cmpw 0xc8(%ecx),%ax\n\t" /* SegSs */
1092 "jne 1f\n\t"
1093 /* As soon as we have switched stacks the context structure could
1094 * be invalid (when signal handlers are executed for example). Copy
1095 * values on the target stack before changing ESP. */
1096 "movl 0xc4(%ecx),%eax\n\t" /* Esp */
1097 "leal -4*4(%eax),%eax\n\t"
1098 "movl 0xc0(%ecx),%edx\n\t" /* EFlags */
1099 "movl %edx,3*4(%eax)\n\t"
1100 "movl 0xbc(%ecx),%edx\n\t" /* SegCs */
1101 "movl %edx,2*4(%eax)\n\t"
1102 "movl 0xb8(%ecx),%edx\n\t" /* Eip */
1103 "movl %edx,1*4(%eax)\n\t"
1104 "movl 0xb0(%ecx),%edx\n\t" /* Eax */
1105 "movl %edx,0*4(%eax)\n\t"
1106 "pushl 0x98(%ecx)\n\t" /* SegDs */
1107 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1108 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1109 "popl %ds\n\t"
1110 "movl %eax,%esp\n\t"
1111 "popl %eax\n\t"
1112 "iret\n"
1113 /* Restore the context when the stack segment changes. We can't use
1114 * the same code as above because we do not know if the stack segment
1115 * is 16 or 32 bit, and 'movl' will throw an exception when we try to
1116 * access memory above the limit. */
1117 "1:\n\t"
1118 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1119 "movl 0xb0(%ecx),%eax\n\t" /* Eax */
1120 "movw 0xc8(%ecx),%ss\n\t" /* SegSs */
1121 "movl 0xc4(%ecx),%esp\n\t" /* Esp */
1122 "pushl 0xc0(%ecx)\n\t" /* EFlags */
1123 "pushl 0xbc(%ecx)\n\t" /* SegCs */
1124 "pushl 0xb8(%ecx)\n\t" /* Eip */
1125 "pushl 0x98(%ecx)\n\t" /* SegDs */
1126 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1127 "popl %ds\n\t"
1128 "iret" )
1131 /***********************************************************************
1132 * set_cpu_context
1134 * Set the new CPU context. Used by NtSetContextThread.
1136 void DECLSPEC_HIDDEN set_cpu_context( const CONTEXT *context )
1138 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1140 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
1141 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
1143 if (flags & CONTEXT_DEBUG_REGISTERS)
1145 x86_thread_data()->dr0 = context->Dr0;
1146 x86_thread_data()->dr1 = context->Dr1;
1147 x86_thread_data()->dr2 = context->Dr2;
1148 x86_thread_data()->dr3 = context->Dr3;
1149 x86_thread_data()->dr6 = context->Dr6;
1150 x86_thread_data()->dr7 = context->Dr7;
1152 if (flags & CONTEXT_FULL)
1154 if (!(flags & CONTEXT_CONTROL))
1155 FIXME( "setting partial context (%x) not supported\n", flags );
1156 else if (flags & CONTEXT_SEGMENTS)
1157 set_full_cpu_context( context );
1158 else
1160 CONTEXT newcontext = *context;
1161 newcontext.SegDs = wine_get_ds();
1162 newcontext.SegEs = wine_get_es();
1163 newcontext.SegFs = wine_get_fs();
1164 newcontext.SegGs = wine_get_gs();
1165 set_full_cpu_context( &newcontext );
1171 /***********************************************************************
1172 * context_to_server
1174 * Convert a register context to the server format.
1176 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1178 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1180 memset( to, 0, sizeof(*to) );
1181 to->cpu = CPU_x86;
1183 if (flags & CONTEXT_CONTROL)
1185 to->flags |= SERVER_CTX_CONTROL;
1186 to->ctl.i386_regs.ebp = from->Ebp;
1187 to->ctl.i386_regs.esp = from->Esp;
1188 to->ctl.i386_regs.eip = from->Eip;
1189 to->ctl.i386_regs.cs = from->SegCs;
1190 to->ctl.i386_regs.ss = from->SegSs;
1191 to->ctl.i386_regs.eflags = from->EFlags;
1193 if (flags & CONTEXT_INTEGER)
1195 to->flags |= SERVER_CTX_INTEGER;
1196 to->integer.i386_regs.eax = from->Eax;
1197 to->integer.i386_regs.ebx = from->Ebx;
1198 to->integer.i386_regs.ecx = from->Ecx;
1199 to->integer.i386_regs.edx = from->Edx;
1200 to->integer.i386_regs.esi = from->Esi;
1201 to->integer.i386_regs.edi = from->Edi;
1203 if (flags & CONTEXT_SEGMENTS)
1205 to->flags |= SERVER_CTX_SEGMENTS;
1206 to->seg.i386_regs.ds = from->SegDs;
1207 to->seg.i386_regs.es = from->SegEs;
1208 to->seg.i386_regs.fs = from->SegFs;
1209 to->seg.i386_regs.gs = from->SegGs;
1211 if (flags & CONTEXT_FLOATING_POINT)
1213 to->flags |= SERVER_CTX_FLOATING_POINT;
1214 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1215 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1216 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1217 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1218 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1219 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1220 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1221 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1222 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1224 if (flags & CONTEXT_DEBUG_REGISTERS)
1226 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1227 to->debug.i386_regs.dr0 = from->Dr0;
1228 to->debug.i386_regs.dr1 = from->Dr1;
1229 to->debug.i386_regs.dr2 = from->Dr2;
1230 to->debug.i386_regs.dr3 = from->Dr3;
1231 to->debug.i386_regs.dr6 = from->Dr6;
1232 to->debug.i386_regs.dr7 = from->Dr7;
1234 if (flags & CONTEXT_EXTENDED_REGISTERS)
1236 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1237 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1239 return STATUS_SUCCESS;
1243 /***********************************************************************
1244 * context_from_server
1246 * Convert a register context from the server format.
1248 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1250 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1252 to->ContextFlags = CONTEXT_i386;
1253 if (from->flags & SERVER_CTX_CONTROL)
1255 to->ContextFlags |= CONTEXT_CONTROL;
1256 to->Ebp = from->ctl.i386_regs.ebp;
1257 to->Esp = from->ctl.i386_regs.esp;
1258 to->Eip = from->ctl.i386_regs.eip;
1259 to->SegCs = from->ctl.i386_regs.cs;
1260 to->SegSs = from->ctl.i386_regs.ss;
1261 to->EFlags = from->ctl.i386_regs.eflags;
1263 if (from->flags & SERVER_CTX_INTEGER)
1265 to->ContextFlags |= CONTEXT_INTEGER;
1266 to->Eax = from->integer.i386_regs.eax;
1267 to->Ebx = from->integer.i386_regs.ebx;
1268 to->Ecx = from->integer.i386_regs.ecx;
1269 to->Edx = from->integer.i386_regs.edx;
1270 to->Esi = from->integer.i386_regs.esi;
1271 to->Edi = from->integer.i386_regs.edi;
1273 if (from->flags & SERVER_CTX_SEGMENTS)
1275 to->ContextFlags |= CONTEXT_SEGMENTS;
1276 to->SegDs = from->seg.i386_regs.ds;
1277 to->SegEs = from->seg.i386_regs.es;
1278 to->SegFs = from->seg.i386_regs.fs;
1279 to->SegGs = from->seg.i386_regs.gs;
1281 if (from->flags & SERVER_CTX_FLOATING_POINT)
1283 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1284 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1285 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1286 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1287 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1288 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1289 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1290 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1291 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1292 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1294 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1296 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1297 to->Dr0 = from->debug.i386_regs.dr0;
1298 to->Dr1 = from->debug.i386_regs.dr1;
1299 to->Dr2 = from->debug.i386_regs.dr2;
1300 to->Dr3 = from->debug.i386_regs.dr3;
1301 to->Dr6 = from->debug.i386_regs.dr6;
1302 to->Dr7 = from->debug.i386_regs.dr7;
1304 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1306 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1307 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1309 return STATUS_SUCCESS;
1313 /***********************************************************************
1314 * NtSetContextThread (NTDLL.@)
1315 * ZwSetContextThread (NTDLL.@)
1317 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
1319 NTSTATUS ret = STATUS_SUCCESS;
1320 BOOL self = (handle == GetCurrentThread());
1322 /* debug registers require a server call */
1323 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
1324 self = (x86_thread_data()->dr0 == context->Dr0 &&
1325 x86_thread_data()->dr1 == context->Dr1 &&
1326 x86_thread_data()->dr2 == context->Dr2 &&
1327 x86_thread_data()->dr3 == context->Dr3 &&
1328 x86_thread_data()->dr6 == context->Dr6 &&
1329 x86_thread_data()->dr7 == context->Dr7);
1331 if (!self) ret = set_thread_context( handle, context, &self );
1333 if (self && ret == STATUS_SUCCESS) set_cpu_context( context );
1334 return ret;
1338 /***********************************************************************
1339 * NtGetContextThread (NTDLL.@)
1340 * ZwGetContextThread (NTDLL.@)
1342 * Note: we use a small assembly wrapper to save the necessary registers
1343 * in case we are fetching the context of the current thread.
1345 NTSTATUS CDECL DECLSPEC_HIDDEN __regs_NtGetContextThread( DWORD edi, DWORD esi, DWORD ebx, DWORD eflags,
1346 DWORD ebp, DWORD retaddr, HANDLE handle,
1347 CONTEXT *context )
1349 NTSTATUS ret;
1350 DWORD needed_flags = context->ContextFlags & ~CONTEXT_i386;
1351 BOOL self = (handle == GetCurrentThread());
1353 /* debug registers require a server call */
1354 if (needed_flags & CONTEXT_DEBUG_REGISTERS) self = FALSE;
1356 if (!self)
1358 if ((ret = get_thread_context( handle, context, &self ))) return ret;
1359 needed_flags &= ~context->ContextFlags;
1362 if (self)
1364 if (needed_flags & CONTEXT_INTEGER)
1366 context->Eax = 0;
1367 context->Ebx = ebx;
1368 context->Ecx = 0;
1369 context->Edx = 0;
1370 context->Esi = esi;
1371 context->Edi = edi;
1372 context->ContextFlags |= CONTEXT_INTEGER;
1374 if (needed_flags & CONTEXT_CONTROL)
1376 context->Ebp = ebp;
1377 context->Esp = (DWORD)&retaddr;
1378 context->Eip = *(&edi - 1);
1379 context->SegCs = wine_get_cs();
1380 context->SegSs = wine_get_ss();
1381 context->EFlags = eflags;
1382 context->ContextFlags |= CONTEXT_CONTROL;
1384 if (needed_flags & CONTEXT_SEGMENTS)
1386 context->SegDs = wine_get_ds();
1387 context->SegEs = wine_get_es();
1388 context->SegFs = wine_get_fs();
1389 context->SegGs = wine_get_gs();
1390 context->ContextFlags |= CONTEXT_SEGMENTS;
1392 if (needed_flags & CONTEXT_FLOATING_POINT) save_fpu( context );
1393 /* FIXME: extended floating point */
1394 /* update the cached version of the debug registers */
1395 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1397 x86_thread_data()->dr0 = context->Dr0;
1398 x86_thread_data()->dr1 = context->Dr1;
1399 x86_thread_data()->dr2 = context->Dr2;
1400 x86_thread_data()->dr3 = context->Dr3;
1401 x86_thread_data()->dr6 = context->Dr6;
1402 x86_thread_data()->dr7 = context->Dr7;
1406 if (context->ContextFlags & (CONTEXT_INTEGER & ~CONTEXT_i386))
1407 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle,
1408 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi );
1409 if (context->ContextFlags & (CONTEXT_CONTROL & ~CONTEXT_i386))
1410 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle,
1411 context->Ebp, context->Esp, context->Eip, context->SegCs, context->SegSs, context->EFlags );
1412 if (context->ContextFlags & (CONTEXT_SEGMENTS & ~CONTEXT_i386))
1413 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle,
1414 context->SegDs, context->SegEs, context->SegFs, context->SegGs );
1415 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1416 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle,
1417 context->Dr0, context->Dr1, context->Dr2, context->Dr3, context->Dr6, context->Dr7 );
1419 return STATUS_SUCCESS;
1421 __ASM_STDCALL_FUNC( NtGetContextThread, 8,
1422 "pushl %ebp\n\t"
1423 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1424 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1425 "movl %esp,%ebp\n\t"
1426 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1427 "pushfl\n\t"
1428 "pushl %ebx\n\t"
1429 __ASM_CFI(".cfi_rel_offset %ebx,-8\n\t")
1430 "pushl %esi\n\t"
1431 __ASM_CFI(".cfi_rel_offset %esi,-12\n\t")
1432 "pushl %edi\n\t"
1433 __ASM_CFI(".cfi_rel_offset %edi,-16\n\t")
1434 "call " __ASM_NAME("__regs_NtGetContextThread") "\n\t"
1435 "leave\n\t"
1436 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1437 __ASM_CFI(".cfi_same_value %ebp\n\t")
1438 "ret $8" )
1441 /***********************************************************************
1442 * is_privileged_instr
1444 * Check if the fault location is a privileged instruction.
1445 * Based on the instruction emulation code in dlls/kernel/instr.c.
1447 static inline DWORD is_privileged_instr( CONTEXT *context )
1449 const BYTE *instr;
1450 unsigned int prefix_count = 0;
1452 if (!wine_ldt_is_system( context->SegCs )) return 0;
1453 instr = (BYTE *)context->Eip;
1455 for (;;) switch(*instr)
1457 /* instruction prefixes */
1458 case 0x2e: /* %cs: */
1459 case 0x36: /* %ss: */
1460 case 0x3e: /* %ds: */
1461 case 0x26: /* %es: */
1462 case 0x64: /* %fs: */
1463 case 0x65: /* %gs: */
1464 case 0x66: /* opcode size */
1465 case 0x67: /* addr size */
1466 case 0xf0: /* lock */
1467 case 0xf2: /* repne */
1468 case 0xf3: /* repe */
1469 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1470 instr++;
1471 continue;
1473 case 0x0f: /* extended instruction */
1474 switch(instr[1])
1476 case 0x20: /* mov crX, reg */
1477 case 0x21: /* mov drX, reg */
1478 case 0x22: /* mov reg, crX */
1479 case 0x23: /* mov reg drX */
1480 return EXCEPTION_PRIV_INSTRUCTION;
1482 return 0;
1483 case 0x6c: /* insb (%dx) */
1484 case 0x6d: /* insl (%dx) */
1485 case 0x6e: /* outsb (%dx) */
1486 case 0x6f: /* outsl (%dx) */
1487 case 0xcd: /* int $xx */
1488 case 0xe4: /* inb al,XX */
1489 case 0xe5: /* in (e)ax,XX */
1490 case 0xe6: /* outb XX,al */
1491 case 0xe7: /* out XX,(e)ax */
1492 case 0xec: /* inb (%dx),%al */
1493 case 0xed: /* inl (%dx),%eax */
1494 case 0xee: /* outb %al,(%dx) */
1495 case 0xef: /* outl %eax,(%dx) */
1496 case 0xf4: /* hlt */
1497 case 0xfa: /* cli */
1498 case 0xfb: /* sti */
1499 return EXCEPTION_PRIV_INSTRUCTION;
1500 default:
1501 return 0;
1506 /***********************************************************************
1507 * handle_interrupt
1509 * Handle an interrupt.
1511 static inline BOOL handle_interrupt( unsigned int interrupt, EXCEPTION_RECORD *rec, CONTEXT *context )
1513 switch(interrupt)
1515 case 0x2d:
1516 context->Eip += 3;
1517 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1518 rec->ExceptionAddress = (void *)context->Eip;
1519 rec->NumberParameters = is_wow64 ? 1 : 3;
1520 rec->ExceptionInformation[0] = context->Eax;
1521 rec->ExceptionInformation[1] = context->Ecx;
1522 rec->ExceptionInformation[2] = context->Edx;
1523 return TRUE;
1524 default:
1525 return FALSE;
1530 /***********************************************************************
1531 * check_invalid_gs
1533 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1535 static inline BOOL check_invalid_gs( CONTEXT *context )
1537 unsigned int prefix_count = 0;
1538 const BYTE *instr = (BYTE *)context->Eip;
1539 WORD system_gs = x86_thread_data()->gs;
1541 if (context->SegGs == system_gs) return FALSE;
1542 if (!wine_ldt_is_system( context->SegCs )) return FALSE;
1543 /* only handle faults in system libraries */
1544 if (virtual_is_valid_code_address( instr, 1 )) return FALSE;
1546 for (;;) switch(*instr)
1548 /* instruction prefixes */
1549 case 0x2e: /* %cs: */
1550 case 0x36: /* %ss: */
1551 case 0x3e: /* %ds: */
1552 case 0x26: /* %es: */
1553 case 0x64: /* %fs: */
1554 case 0x66: /* opcode size */
1555 case 0x67: /* addr size */
1556 case 0xf0: /* lock */
1557 case 0xf2: /* repne */
1558 case 0xf3: /* repe */
1559 if (++prefix_count >= 15) return FALSE;
1560 instr++;
1561 continue;
1562 case 0x65: /* %gs: */
1563 TRACE( "%04x/%04x at %p, fixing up\n", context->SegGs, system_gs, instr );
1564 context->SegGs = system_gs;
1565 return TRUE;
1566 default:
1567 return FALSE;
1572 #include "pshpack1.h"
1573 union atl_thunk
1575 struct
1577 DWORD movl; /* movl this,4(%esp) */
1578 DWORD this;
1579 BYTE jmp; /* jmp func */
1580 int func;
1581 } t1;
1582 struct
1584 BYTE movl; /* movl this,ecx */
1585 DWORD this;
1586 BYTE jmp; /* jmp func */
1587 int func;
1588 } t2;
1589 struct
1591 BYTE movl1; /* movl this,edx */
1592 DWORD this;
1593 BYTE movl2; /* movl func,ecx */
1594 DWORD func;
1595 WORD jmp; /* jmp ecx */
1596 } t3;
1597 struct
1599 BYTE movl1; /* movl this,ecx */
1600 DWORD this;
1601 BYTE movl2; /* movl func,eax */
1602 DWORD func;
1603 WORD jmp; /* jmp eax */
1604 } t4;
1605 struct
1607 DWORD inst1; /* pop ecx
1608 * pop eax
1609 * push ecx
1610 * jmp 4(%eax) */
1611 WORD inst2;
1612 } t5;
1614 #include "poppack.h"
1616 /**********************************************************************
1617 * check_atl_thunk
1619 * Check if code destination is an ATL thunk, and emulate it if so.
1621 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1623 const union atl_thunk *thunk = (const union atl_thunk *)rec->ExceptionInformation[1];
1624 union atl_thunk thunk_copy;
1625 SIZE_T thunk_len;
1627 thunk_len = virtual_uninterrupted_read_memory( thunk, &thunk_copy, sizeof(*thunk) );
1628 if (!thunk_len) return FALSE;
1630 if (thunk_len >= sizeof(thunk_copy.t1) && thunk_copy.t1.movl == 0x042444c7 &&
1631 thunk_copy.t1.jmp == 0xe9)
1633 if (!virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1,
1634 &thunk_copy.t1.this, sizeof(DWORD) ))
1636 context->Eip = (DWORD_PTR)(&thunk->t1.func + 1) + thunk_copy.t1.func;
1637 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1638 thunk, context->Eip, thunk_copy.t1.this );
1639 return TRUE;
1642 else if (thunk_len >= sizeof(thunk_copy.t2) && thunk_copy.t2.movl == 0xb9 &&
1643 thunk_copy.t2.jmp == 0xe9)
1645 context->Ecx = thunk_copy.t2.this;
1646 context->Eip = (DWORD_PTR)(&thunk->t2.func + 1) + thunk_copy.t2.func;
1647 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1648 thunk, context->Eip, context->Ecx );
1649 return TRUE;
1651 else if (thunk_len >= sizeof(thunk_copy.t3) && thunk_copy.t3.movl1 == 0xba &&
1652 thunk_copy.t3.movl2 == 0xb9 &&
1653 thunk_copy.t3.jmp == 0xe1ff)
1655 context->Edx = thunk_copy.t3.this;
1656 context->Ecx = thunk_copy.t3.func;
1657 context->Eip = thunk_copy.t3.func;
1658 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1659 thunk, context->Eip, context->Ecx, context->Edx );
1660 return TRUE;
1662 else if (thunk_len >= sizeof(thunk_copy.t4) && thunk_copy.t4.movl1 == 0xb9 &&
1663 thunk_copy.t4.movl2 == 0xb8 &&
1664 thunk_copy.t4.jmp == 0xe0ff)
1666 context->Ecx = thunk_copy.t4.this;
1667 context->Eax = thunk_copy.t4.func;
1668 context->Eip = thunk_copy.t4.func;
1669 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1670 thunk, context->Eip, context->Eax, context->Ecx );
1671 return TRUE;
1673 else if (thunk_len >= sizeof(thunk_copy.t5) && thunk_copy.t5.inst1 == 0xff515859 &&
1674 thunk_copy.t5.inst2 == 0x0460)
1676 DWORD func, stack[2];
1677 if (virtual_uninterrupted_read_memory( (DWORD *)context->Esp,
1678 stack, sizeof(stack) ) == sizeof(stack) &&
1679 virtual_uninterrupted_read_memory( (DWORD *)stack[1] + 1,
1680 &func, sizeof(DWORD) ) == sizeof(DWORD) &&
1681 !virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1, &stack[0], sizeof(stack[0]) ))
1683 context->Ecx = stack[0];
1684 context->Eax = stack[1];
1685 context->Esp = context->Esp + sizeof(DWORD);
1686 context->Eip = func;
1687 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1688 thunk, context->Eip, context->Eax, context->Ecx, context->Esp );
1689 return TRUE;
1693 return FALSE;
1697 /***********************************************************************
1698 * setup_exception_record
1700 * Setup the exception record and context on the thread stack.
1702 static EXCEPTION_RECORD *setup_exception_record( ucontext_t *sigcontext, void *stack_ptr,
1703 WORD fs, WORD gs, raise_func func )
1705 struct stack_layout
1707 void *ret_addr; /* return address from raise_func */
1708 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1709 CONTEXT *context_ptr; /* second arg for raise_func */
1710 CONTEXT context;
1711 EXCEPTION_RECORD rec;
1712 DWORD ebp;
1713 DWORD eip;
1714 } *stack = stack_ptr;
1715 DWORD exception_code = 0;
1717 /* stack sanity checks */
1719 if ((char *)stack >= (char *)get_signal_stack() &&
1720 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1722 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1723 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1724 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1725 NtCurrentTeb()->Tib.StackBase );
1726 abort_thread(1);
1729 if (stack - 1 > stack || /* check for overflow in subtraction */
1730 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1731 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1733 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1734 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1735 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1736 NtCurrentTeb()->Tib.StackBase );
1738 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1740 /* stack overflow on last page, unrecoverable */
1741 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1742 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1743 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1744 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1745 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1746 abort_thread(1);
1748 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1750 /* stack access below stack limit, may be recoverable */
1751 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1752 else
1754 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1755 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1756 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1757 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1758 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1759 abort_thread(1);
1763 stack--; /* push the stack_layout structure */
1764 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1765 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1766 #elif defined(VALGRIND_MAKE_WRITABLE)
1767 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1768 #endif
1769 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1770 stack->rec_ptr = &stack->rec;
1771 stack->context_ptr = &stack->context;
1773 stack->rec.ExceptionRecord = NULL;
1774 stack->rec.ExceptionCode = exception_code;
1775 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1776 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1777 stack->rec.NumberParameters = 0;
1779 save_context( &stack->context, sigcontext, fs, gs );
1781 /* now modify the sigcontext to return to the raise function */
1782 ESP_sig(sigcontext) = (DWORD)stack;
1783 EIP_sig(sigcontext) = (DWORD)func;
1784 /* clear single-step, direction, and align check flag */
1785 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1786 CS_sig(sigcontext) = wine_get_cs();
1787 DS_sig(sigcontext) = wine_get_ds();
1788 ES_sig(sigcontext) = wine_get_es();
1789 FS_sig(sigcontext) = wine_get_fs();
1790 GS_sig(sigcontext) = wine_get_gs();
1791 SS_sig(sigcontext) = wine_get_ss();
1793 return stack->rec_ptr;
1797 /***********************************************************************
1798 * setup_exception
1800 * Setup a proper stack frame for the raise function, and modify the
1801 * sigcontext so that the return from the signal handler will call
1802 * the raise function.
1804 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1806 WORD fs, gs;
1807 void *stack = init_handler( sigcontext, &fs, &gs );
1809 return setup_exception_record( sigcontext, stack, fs, gs, func );
1813 /***********************************************************************
1814 * get_exception_context
1816 * Get a pointer to the context built by setup_exception.
1818 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1820 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1824 /**********************************************************************
1825 * get_fpu_code
1827 * Get the FPU exception code from the FPU status.
1829 static inline DWORD get_fpu_code( const CONTEXT *context )
1831 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1833 if (status & 0x01) /* IE */
1835 if (status & 0x40) /* SF */
1836 return EXCEPTION_FLT_STACK_CHECK;
1837 else
1838 return EXCEPTION_FLT_INVALID_OPERATION;
1840 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1841 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1842 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1843 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1844 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1845 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1849 /**********************************************************************
1850 * raise_segv_exception
1852 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1854 NTSTATUS status;
1856 switch(rec->ExceptionCode)
1858 case EXCEPTION_ACCESS_VIOLATION:
1859 if (rec->NumberParameters == 2)
1861 if (rec->ExceptionInformation[1] == 0xffffffff && check_invalid_gs( context ))
1862 goto done;
1863 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1864 rec->ExceptionInformation[0], FALSE )))
1865 goto done;
1866 if (rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
1867 rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT)
1869 ULONG flags;
1870 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
1871 &flags, sizeof(flags), NULL );
1873 if (!(flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION) && check_atl_thunk( rec, context ))
1874 goto done;
1876 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1877 if (!(flags & MEM_EXECUTE_OPTION_DISABLE))
1878 rec->ExceptionInformation[0] = EXCEPTION_READ_FAULT;
1881 break;
1882 case EXCEPTION_DATATYPE_MISALIGNMENT:
1883 /* FIXME: pass through exception handler first? */
1884 if (context->EFlags & 0x00040000)
1886 /* Disable AC flag, return */
1887 context->EFlags &= ~0x00040000;
1888 goto done;
1890 break;
1891 case EXCEPTION_BREAKPOINT:
1892 if (!is_wow64)
1894 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
1895 * service is usually not recognized when called from usermode. */
1896 switch (rec->ExceptionInformation[0])
1898 case 1: /* BREAKPOINT_PRINT */
1899 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
1900 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
1901 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
1902 goto done;
1905 break;
1907 status = NtRaiseException( rec, context, TRUE );
1908 raise_status( status, rec );
1909 done:
1910 set_cpu_context( context );
1914 /**********************************************************************
1915 * raise_trap_exception
1917 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1919 NTSTATUS status;
1921 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1923 /* when single stepping can't tell whether this is a hw bp or a
1924 * single step interrupt. try to avoid as much overhead as possible
1925 * and only do a server call if there is any hw bp enabled. */
1927 if( !(context->EFlags & 0x100) || (x86_thread_data()->dr7 & 0xff) )
1929 /* (possible) hardware breakpoint, fetch the debug registers */
1930 DWORD saved_flags = context->ContextFlags;
1931 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1932 NtGetContextThread(GetCurrentThread(), context);
1933 context->ContextFlags |= saved_flags; /* restore flags */
1936 context->EFlags &= ~0x100; /* clear single-step flag */
1939 status = NtRaiseException( rec, context, TRUE );
1940 raise_status( status, rec );
1944 /**********************************************************************
1945 * raise_generic_exception
1947 * Generic raise function for exceptions that don't need special treatment.
1949 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1951 NTSTATUS status;
1953 status = NtRaiseException( rec, context, TRUE );
1954 raise_status( status, rec );
1958 /**********************************************************************
1959 * segv_handler
1961 * Handler for SIGSEGV and related errors.
1963 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1965 WORD fs, gs;
1966 EXCEPTION_RECORD *rec;
1967 ucontext_t *context = sigcontext;
1968 void *stack = init_handler( sigcontext, &fs, &gs );
1970 /* check for exceptions on the signal stack caused by write watches */
1971 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1972 (char *)stack >= (char *)get_signal_stack() &&
1973 (char *)stack < (char *)get_signal_stack() + signal_stack_size &&
1974 !virtual_handle_fault( siginfo->si_addr, (get_error_code(context) >> 1) & 0x09, TRUE ))
1976 return;
1979 /* check for page fault inside the thread stack */
1980 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1981 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1982 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1983 virtual_handle_stack_fault( siginfo->si_addr ))
1985 /* check if this was the last guard page */
1986 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1988 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1989 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1991 return;
1994 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1995 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1997 switch(get_trap_code(context))
1999 case TRAP_x86_OFLOW: /* Overflow exception */
2000 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2001 break;
2002 case TRAP_x86_BOUND: /* Bound range exception */
2003 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2004 break;
2005 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2006 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2007 break;
2008 case TRAP_x86_STKFLT: /* Stack fault */
2009 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2010 break;
2011 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2012 case TRAP_x86_PROTFLT: /* General protection fault */
2013 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2015 CONTEXT *win_context = get_exception_context( rec );
2016 WORD err = get_error_code(context);
2017 if (!err && (rec->ExceptionCode = is_privileged_instr( win_context ))) break;
2018 if ((err & 7) == 2 && handle_interrupt( err >> 3, rec, win_context )) break;
2019 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2020 rec->NumberParameters = 2;
2021 rec->ExceptionInformation[0] = 0;
2022 /* if error contains a LDT selector, use that as fault address */
2023 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
2024 rec->ExceptionInformation[1] = err & ~7;
2025 else
2026 rec->ExceptionInformation[1] = 0xffffffff;
2028 break;
2029 case TRAP_x86_PAGEFLT: /* Page fault */
2030 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2031 rec->NumberParameters = 2;
2032 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
2033 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2034 break;
2035 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2036 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2037 break;
2038 default:
2039 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2040 /* fall through */
2041 case TRAP_x86_NMI: /* NMI interrupt */
2042 case TRAP_x86_DNA: /* Device not available exception */
2043 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2044 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2045 case TRAP_x86_MCHK: /* Machine check exception */
2046 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2047 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2048 break;
2053 /**********************************************************************
2054 * trap_handler
2056 * Handler for SIGTRAP.
2058 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2060 ucontext_t *context = sigcontext;
2061 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
2063 switch(get_trap_code(context))
2065 case TRAP_x86_TRCTRAP: /* Single-step exception */
2066 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2067 break;
2068 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2069 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2070 /* fall through */
2071 default:
2072 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2073 rec->NumberParameters = is_wow64 ? 1 : 3;
2074 rec->ExceptionInformation[0] = 0;
2075 rec->ExceptionInformation[1] = 0; /* FIXME */
2076 rec->ExceptionInformation[2] = 0; /* FIXME */
2077 break;
2082 /**********************************************************************
2083 * fpe_handler
2085 * Handler for SIGFPE.
2087 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2089 CONTEXT *win_context;
2090 ucontext_t *context = sigcontext;
2091 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
2093 win_context = get_exception_context( rec );
2095 switch(get_trap_code(context))
2097 case TRAP_x86_DIVIDE: /* Division by zero exception */
2098 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2099 break;
2100 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
2101 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2102 break;
2103 case TRAP_x86_ARITHTRAP: /* Floating point exception */
2104 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2105 rec->ExceptionCode = get_fpu_code( win_context );
2106 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
2107 break;
2108 case TRAP_x86_CACHEFLT: /* SIMD exception */
2109 /* TODO:
2110 * Behaviour only tested for divide-by-zero exceptions
2111 * Check for other SIMD exceptions as well */
2112 if(siginfo->si_code != FPE_FLTDIV && siginfo->si_code != FPE_FLTINV)
2113 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2114 siginfo->si_code);
2116 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
2117 rec->NumberParameters = 1;
2118 /* no idea what meaning is actually behind this but that's what native does */
2119 rec->ExceptionInformation[0] = 0;
2120 break;
2121 default:
2122 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2123 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2124 break;
2129 /**********************************************************************
2130 * int_handler
2132 * Handler for SIGINT.
2134 * FIXME: should not be calling external functions on the signal stack.
2136 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2138 WORD fs, gs;
2139 init_handler( sigcontext, &fs, &gs );
2140 if (!dispatch_signal(SIGINT))
2142 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2143 rec->ExceptionCode = CONTROL_C_EXIT;
2147 /**********************************************************************
2148 * abrt_handler
2150 * Handler for SIGABRT.
2152 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2154 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2155 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2156 rec->ExceptionFlags = EH_NONCONTINUABLE;
2160 /**********************************************************************
2161 * quit_handler
2163 * Handler for SIGQUIT.
2165 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2167 WORD fs, gs;
2168 init_handler( sigcontext, &fs, &gs );
2169 abort_thread(0);
2173 /**********************************************************************
2174 * usr1_handler
2176 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2178 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2180 CONTEXT context;
2181 WORD fs, gs;
2183 init_handler( sigcontext, &fs, &gs );
2184 save_context( &context, sigcontext, fs, gs );
2185 wait_suspend( &context );
2186 restore_context( &context, sigcontext );
2190 /***********************************************************************
2191 * __wine_set_signal_handler (NTDLL.@)
2193 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2195 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2196 if (handlers[sig] != NULL) return -2;
2197 handlers[sig] = wsh;
2198 return 0;
2202 /***********************************************************************
2203 * locking for LDT routines
2205 static RTL_CRITICAL_SECTION ldt_section;
2206 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
2208 0, 0, &ldt_section,
2209 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
2210 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
2212 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
2213 static sigset_t ldt_sigset;
2215 static void ldt_lock(void)
2217 sigset_t sigset;
2219 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
2220 RtlEnterCriticalSection( &ldt_section );
2221 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
2224 static void ldt_unlock(void)
2226 if (ldt_section.RecursionCount == 1)
2228 sigset_t sigset = ldt_sigset;
2229 RtlLeaveCriticalSection( &ldt_section );
2230 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
2232 else RtlLeaveCriticalSection( &ldt_section );
2236 /**********************************************************************
2237 * signal_alloc_thread
2239 NTSTATUS signal_alloc_thread( TEB **teb )
2241 static size_t sigstack_zero_bits;
2242 struct x86_thread_data *thread_data;
2243 SIZE_T size;
2244 void *addr = NULL;
2245 NTSTATUS status;
2247 if (!sigstack_zero_bits)
2249 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2250 /* find the first power of two not smaller than min_size */
2251 sigstack_zero_bits = 12;
2252 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2253 signal_stack_mask = (1 << sigstack_zero_bits) - 1;
2254 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2257 size = signal_stack_mask + 1;
2258 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
2259 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2261 *teb = addr;
2262 (*teb)->Tib.Self = &(*teb)->Tib;
2263 (*teb)->Tib.ExceptionList = (void *)~0UL;
2264 thread_data = (struct x86_thread_data *)(*teb)->SystemReserved2;
2265 if (!(thread_data->fs = wine_ldt_alloc_fs()))
2267 size = 0;
2268 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
2269 status = STATUS_TOO_MANY_THREADS;
2272 return status;
2276 /**********************************************************************
2277 * signal_free_thread
2279 void signal_free_thread( TEB *teb )
2281 SIZE_T size = 0;
2282 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2284 wine_ldt_free_fs( thread_data->fs );
2285 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2289 /**********************************************************************
2290 * signal_init_thread
2292 void signal_init_thread( TEB *teb )
2294 const WORD fpu_cw = 0x27f;
2295 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2296 LDT_ENTRY fs_entry;
2297 stack_t ss;
2299 #ifdef __APPLE__
2300 int mib[2], val = 1;
2302 mib[0] = CTL_KERN;
2303 mib[1] = KERN_THALTSTACK;
2304 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2305 #endif
2307 ss.ss_sp = (char *)teb + teb_size;
2308 ss.ss_size = signal_stack_size;
2309 ss.ss_flags = 0;
2310 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2312 wine_ldt_set_base( &fs_entry, teb );
2313 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2314 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2315 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2316 thread_data->gs = wine_get_gs();
2318 #ifdef __GNUC__
2319 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2320 #else
2321 FIXME("FPU setup not implemented for this platform.\n");
2322 #endif
2325 /**********************************************************************
2326 * signal_init_process
2328 void signal_init_process(void)
2330 struct sigaction sig_act;
2332 sig_act.sa_mask = server_block_set;
2333 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2334 #ifdef SA_ONSTACK
2335 sig_act.sa_flags |= SA_ONSTACK;
2336 #endif
2337 #ifdef __ANDROID__
2338 sig_act.sa_flags |= SA_RESTORER;
2339 sig_act.sa_restorer = rt_sigreturn;
2340 #endif
2341 sig_act.sa_sigaction = int_handler;
2342 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2343 sig_act.sa_sigaction = fpe_handler;
2344 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2345 sig_act.sa_sigaction = abrt_handler;
2346 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2347 sig_act.sa_sigaction = quit_handler;
2348 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2349 sig_act.sa_sigaction = usr1_handler;
2350 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2352 sig_act.sa_sigaction = segv_handler;
2353 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2354 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2355 #ifdef SIGBUS
2356 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2357 #endif
2359 #ifdef SIGTRAP
2360 sig_act.sa_sigaction = trap_handler;
2361 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2362 #endif
2364 wine_ldt_init_locking( ldt_lock, ldt_unlock );
2365 return;
2367 error:
2368 perror("sigaction");
2369 exit(1);
2373 /*******************************************************************
2374 * RtlUnwind (NTDLL.@)
2376 void WINAPI DECLSPEC_HIDDEN __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2377 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2379 EXCEPTION_RECORD record;
2380 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2381 DWORD res;
2383 context->Eax = (DWORD)retval;
2385 /* build an exception record, if we do not have one */
2386 if (!pRecord)
2388 record.ExceptionCode = STATUS_UNWIND;
2389 record.ExceptionFlags = 0;
2390 record.ExceptionRecord = NULL;
2391 record.ExceptionAddress = (void *)context->Eip;
2392 record.NumberParameters = 0;
2393 pRecord = &record;
2396 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2398 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2399 TRACE( "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
2400 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi );
2401 TRACE( "ebp=%08x esp=%08x eip=%08x cs=%04x ds=%04x fs=%04x gs=%04x flags=%08x\n",
2402 context->Ebp, context->Esp, context->Eip, LOWORD(context->SegCs), LOWORD(context->SegDs),
2403 LOWORD(context->SegFs), LOWORD(context->SegGs), context->EFlags );
2405 /* get chain of exception frames */
2406 frame = NtCurrentTeb()->Tib.ExceptionList;
2407 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2409 /* Check frame address */
2410 if (pEndFrame && (frame > pEndFrame))
2411 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2413 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, pRecord );
2415 /* Call handler */
2416 TRACE( "calling handler at %p code=%x flags=%x\n",
2417 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2418 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2419 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2421 switch(res)
2423 case ExceptionContinueSearch:
2424 break;
2425 case ExceptionCollidedUnwind:
2426 frame = dispatch;
2427 break;
2428 default:
2429 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2430 break;
2432 frame = __wine_pop_frame( frame );
2435 NtSetContextThread( GetCurrentThread(), context );
2437 __ASM_STDCALL_FUNC( RtlUnwind, 16,
2438 "pushl %ebp\n\t"
2439 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2440 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2441 "movl %esp,%ebp\n\t"
2442 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2443 "leal -(0x2cc+8)(%esp),%esp\n\t" /* sizeof(CONTEXT) + alignment */
2444 "pushl %esp\n\t" /* context */
2445 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2446 "leal 24(%ebp),%eax\n\t"
2447 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2448 "pushl %esp\n\t"
2449 "pushl 20(%ebp)\n\t"
2450 "pushl 16(%ebp)\n\t"
2451 "pushl 12(%ebp)\n\t"
2452 "pushl 8(%ebp)\n\t"
2453 "call " __ASM_NAME("__regs_RtlUnwind") __ASM_STDCALL(20) "\n\t"
2454 "leave\n\t"
2455 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2456 __ASM_CFI(".cfi_same_value %ebp\n\t")
2457 "ret $16" ) /* actually never returns */
2460 /*******************************************************************
2461 * NtRaiseException (NTDLL.@)
2463 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2465 NTSTATUS status = raise_exception( rec, context, first_chance );
2466 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2467 return status;
2471 /***********************************************************************
2472 * RtlRaiseException (NTDLL.@)
2474 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
2475 "pushl %ebp\n\t"
2476 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2477 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2478 "movl %esp,%ebp\n\t"
2479 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2480 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
2481 "pushl %esp\n\t" /* context */
2482 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2483 "movl 4(%ebp),%eax\n\t" /* return address */
2484 "movl 8(%ebp),%ecx\n\t" /* rec */
2485 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
2486 "leal 12(%ebp),%eax\n\t"
2487 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2488 "movl %esp,%eax\n\t"
2489 "pushl $1\n\t"
2490 "pushl %eax\n\t"
2491 "pushl %ecx\n\t"
2492 "call " __ASM_NAME("NtRaiseException") __ASM_STDCALL(12) "\n\t"
2493 "pushl %eax\n\t"
2494 "call " __ASM_NAME("RtlRaiseStatus") __ASM_STDCALL(4) "\n\t"
2495 "leave\n\t"
2496 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2497 __ASM_CFI(".cfi_same_value %ebp\n\t")
2498 "ret $4" ) /* actually never returns */
2501 /*************************************************************************
2502 * RtlCaptureStackBackTrace (NTDLL.@)
2504 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2506 CONTEXT context;
2507 ULONG i;
2508 ULONG *frame;
2510 RtlCaptureContext( &context );
2511 if (hash) *hash = 0;
2512 frame = (ULONG *)context.Ebp;
2514 while (skip--)
2516 if (!is_valid_frame( frame )) return 0;
2517 frame = (ULONG *)*frame;
2520 for (i = 0; i < count; i++)
2522 if (!is_valid_frame( frame )) break;
2523 buffer[i] = (void *)frame[1];
2524 if (hash) *hash += frame[1];
2525 frame = (ULONG *)*frame;
2527 return i;
2531 extern void DECLSPEC_NORETURN start_thread( LPTHREAD_START_ROUTINE entry, void *arg, BOOL suspend,
2532 void *relay );
2533 __ASM_GLOBAL_FUNC( start_thread,
2534 "pushl %ebp\n\t"
2535 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2536 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2537 "movl %esp,%ebp\n\t"
2538 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2539 "pushl %ebx\n\t"
2540 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2541 "pushl %esi\n\t"
2542 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2543 "pushl %edi\n\t"
2544 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2545 /* store exit frame */
2546 "movl %ebp,%fs:0x1f4\n\t" /* x86_thread_data()->exit_frame */
2547 /* switch to thread stack */
2548 "movl %fs:4,%eax\n\t" /* NtCurrentTeb()->StackBase */
2549 "leal -0x1000(%eax),%esp\n\t"
2550 /* attach dlls */
2551 "pushl 20(%ebp)\n\t" /* relay */
2552 "pushl 16(%ebp)\n\t" /* suspend */
2553 "pushl 12(%ebp)\n\t" /* arg */
2554 "pushl 8(%ebp)\n\t" /* entry */
2555 "xorl %ebp,%ebp\n\t"
2556 "call " __ASM_NAME("attach_thread") "\n\t"
2557 "movl %eax,%esi\n\t"
2558 "leal -12(%eax),%esp\n\t"
2559 /* clear the stack */
2560 "andl $~0xfff,%eax\n\t" /* round down to page size */
2561 "movl %eax,(%esp)\n\t"
2562 "call " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
2563 /* switch to the initial context */
2564 "movl %esi,(%esp)\n\t"
2565 "call " __ASM_NAME("set_cpu_context") )
2567 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int) );
2568 __ASM_GLOBAL_FUNC( call_thread_exit_func,
2569 "movl 8(%esp),%ecx\n\t"
2570 /* fetch exit frame */
2571 "movl %fs:0x1f4,%edx\n\t" /* x86_thread_data()->exit_frame */
2572 "testl %edx,%edx\n\t"
2573 "jnz 1f\n\t"
2574 "jmp *%ecx\n\t"
2575 /* switch to exit frame stack */
2576 "1:\tmovl 4(%esp),%eax\n\t"
2577 "movl $0,%fs:0x1f4\n\t"
2578 "movl %edx,%ebp\n\t"
2579 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2580 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2581 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2582 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2583 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2584 "leal -20(%ebp),%esp\n\t"
2585 "pushl %eax\n\t"
2586 "call *%ecx" )
2588 extern void call_thread_entry(void) DECLSPEC_HIDDEN;
2589 __ASM_GLOBAL_FUNC( call_thread_entry,
2590 "pushl %ebp\n\t"
2591 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2592 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2593 "movl %esp,%ebp\n\t"
2594 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2595 "pushl %ebx\n\t" /* arg */
2596 "pushl %eax\n\t" /* entry */
2597 "call " __ASM_NAME("call_thread_func") )
2599 /* wrapper for apps that don't declare the thread function correctly */
2600 extern DWORD call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry, void *arg );
2601 __ASM_GLOBAL_FUNC(call_thread_func_wrapper,
2602 "pushl %ebp\n\t"
2603 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2604 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2605 "movl %esp,%ebp\n\t"
2606 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2607 "subl $4,%esp\n\t"
2608 "pushl 12(%ebp)\n\t"
2609 "call *8(%ebp)\n\t"
2610 "leave\n\t"
2611 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2612 __ASM_CFI(".cfi_same_value %ebp\n\t")
2613 "ret" )
2615 /***********************************************************************
2616 * call_thread_func
2618 void DECLSPEC_HIDDEN call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg )
2620 __TRY
2622 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
2623 RtlExitUserThread( call_thread_func_wrapper( entry, arg ));
2625 __EXCEPT(unhandled_exception_filter)
2627 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2629 __ENDTRY
2630 abort(); /* should not be reached */
2634 /***********************************************************************
2635 * init_thread_context
2637 static void init_thread_context( CONTEXT *context, LPTHREAD_START_ROUTINE entry, void *arg, void *relay )
2639 context->SegCs = wine_get_cs();
2640 context->SegDs = wine_get_ds();
2641 context->SegEs = wine_get_es();
2642 context->SegFs = wine_get_fs();
2643 context->SegGs = wine_get_gs();
2644 context->SegSs = wine_get_ss();
2645 context->EFlags = 0x202;
2646 context->Eax = (DWORD)entry;
2647 context->Ebx = (DWORD)arg;
2648 context->Esp = (DWORD)NtCurrentTeb()->Tib.StackBase - 16;
2649 context->Eip = (DWORD)relay;
2650 context->FloatSave.ControlWord = 0x27f;
2651 ((XMM_SAVE_AREA32 *)context->ExtendedRegisters)->ControlWord = 0x27f;
2655 /***********************************************************************
2656 * attach_thread
2658 PCONTEXT DECLSPEC_HIDDEN attach_thread( LPTHREAD_START_ROUTINE entry, void *arg,
2659 BOOL suspend, void *relay )
2661 CONTEXT *ctx;
2663 if (suspend)
2665 CONTEXT context = { CONTEXT_ALL };
2667 init_thread_context( &context, entry, arg, relay );
2668 wait_suspend( &context );
2669 ctx = (CONTEXT *)((ULONG_PTR)context.Esp & ~15) - 1;
2670 *ctx = context;
2672 else
2674 ctx = (CONTEXT *)((char *)NtCurrentTeb()->Tib.StackBase - 16) - 1;
2675 init_thread_context( ctx, entry, arg, relay );
2677 ctx->ContextFlags = CONTEXT_FULL;
2678 attach_dlls( ctx );
2679 return ctx;
2683 /***********************************************************************
2684 * signal_start_thread
2686 * Thread startup sequence:
2687 * signal_start_thread()
2688 * -> start_thread()
2689 * -> call_thread_entry()
2690 * -> call_thread_func()
2692 void signal_start_thread( LPTHREAD_START_ROUTINE entry, void *arg, BOOL suspend )
2694 start_thread( entry, arg, suspend, call_thread_entry );
2697 /**********************************************************************
2698 * signal_start_process
2700 * Process startup sequence:
2701 * signal_start_process()
2702 * -> start_thread()
2703 * -> kernel32_start_process()
2705 void signal_start_process( LPTHREAD_START_ROUTINE entry, BOOL suspend )
2707 start_thread( entry, NtCurrentTeb()->Peb, suspend, kernel32_start_process );
2711 /***********************************************************************
2712 * signal_exit_thread
2714 void signal_exit_thread( int status )
2716 call_thread_exit_func( status, exit_thread );
2719 /***********************************************************************
2720 * signal_exit_process
2722 void signal_exit_process( int status )
2724 call_thread_exit_func( status, exit );
2727 /**********************************************************************
2728 * DbgBreakPoint (NTDLL.@)
2730 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2732 /**********************************************************************
2733 * DbgUserBreakPoint (NTDLL.@)
2735 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2737 /**********************************************************************
2738 * NtCurrentTeb (NTDLL.@)
2740 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2743 /**************************************************************************
2744 * _chkstk (NTDLL.@)
2746 __ASM_STDCALL_FUNC( _chkstk, 0,
2747 "negl %eax\n\t"
2748 "addl %esp,%eax\n\t"
2749 "xchgl %esp,%eax\n\t"
2750 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2751 "movl %eax,0(%esp)\n\t"
2752 "ret" )
2754 /**************************************************************************
2755 * _alloca_probe (NTDLL.@)
2757 __ASM_STDCALL_FUNC( _alloca_probe, 0,
2758 "negl %eax\n\t"
2759 "addl %esp,%eax\n\t"
2760 "xchgl %esp,%eax\n\t"
2761 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2762 "movl %eax,0(%esp)\n\t"
2763 "ret" )
2766 /**********************************************************************
2767 * EXC_CallHandler (internal)
2769 * Some exception handlers depend on EBP to have a fixed position relative to
2770 * the exception frame.
2771 * Shrinker depends on (*1) doing what it does,
2772 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2773 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2774 * function calling the handler having only 5 parameters (*4).
2776 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2777 "pushl %ebp\n\t"
2778 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2779 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2780 "movl %esp,%ebp\n\t"
2781 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2782 "pushl %ebx\n\t"
2783 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2784 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2785 "pushl 24(%ebp)\n\t"
2786 "pushl 20(%ebp)\n\t"
2787 "pushl 16(%ebp)\n\t"
2788 "pushl 12(%ebp)\n\t"
2789 "pushl 8(%ebp)\n\t"
2790 "call " __ASM_NAME("call_exception_handler") "\n\t"
2791 "popl %ebx\n\t"
2792 __ASM_CFI(".cfi_same_value %ebx\n\t")
2793 "leave\n"
2794 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2795 __ASM_CFI(".cfi_same_value %ebp\n\t")
2796 "ret" )
2797 __ASM_GLOBAL_FUNC(call_exception_handler,
2798 "pushl %ebp\n\t"
2799 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2800 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2801 "movl %esp,%ebp\n\t"
2802 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2803 "subl $12,%esp\n\t"
2804 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2805 "pushl %edx\n\t" /* handler be handled by... */
2806 ".byte 0x64\n\t"
2807 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2808 ".byte 0x64\n\t"
2809 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2810 "pushl 20(%ebp)\n\t"
2811 "pushl 16(%ebp)\n\t"
2812 "pushl 12(%ebp)\n\t"
2813 "pushl 8(%ebp)\n\t"
2814 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2815 "call *%ecx\n\t" /* call handler. (*2) */
2816 ".byte 0x64\n\t"
2817 "movl (0), %esp\n\t" /* restore previous... (*3) */
2818 ".byte 0x64\n\t"
2819 "popl (0)\n\t" /* exception frame. */
2820 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2821 "popl %ebp\n\t"
2822 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2823 __ASM_CFI(".cfi_same_value %ebp\n\t")
2824 "ret $20" ) /* (*4) */
2826 #endif /* __i386__ */