ntdll: Add RtlAddGrowableFunctionTable stub.
[wine.git] / dlls / ntdll / signal_i386.c
blob0719c648304cc323e5d2c93bebd919d51573f8a5
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 * get_server_context_flags
1174 * Convert CPU-specific flags to generic server flags
1176 static unsigned int get_server_context_flags( DWORD flags )
1178 unsigned int ret = 0;
1180 flags &= ~CONTEXT_i386; /* get rid of CPU id */
1181 if (flags & CONTEXT_CONTROL) ret |= SERVER_CTX_CONTROL;
1182 if (flags & CONTEXT_INTEGER) ret |= SERVER_CTX_INTEGER;
1183 if (flags & CONTEXT_SEGMENTS) ret |= SERVER_CTX_SEGMENTS;
1184 if (flags & CONTEXT_FLOATING_POINT) ret |= SERVER_CTX_FLOATING_POINT;
1185 if (flags & CONTEXT_DEBUG_REGISTERS) ret |= SERVER_CTX_DEBUG_REGISTERS;
1186 if (flags & CONTEXT_EXTENDED_REGISTERS) ret |= SERVER_CTX_EXTENDED_REGISTERS;
1187 return ret;
1191 /***********************************************************************
1192 * context_to_server
1194 * Convert a register context to the server format.
1196 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1198 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1200 memset( to, 0, sizeof(*to) );
1201 to->cpu = CPU_x86;
1203 if (flags & CONTEXT_CONTROL)
1205 to->flags |= SERVER_CTX_CONTROL;
1206 to->ctl.i386_regs.ebp = from->Ebp;
1207 to->ctl.i386_regs.esp = from->Esp;
1208 to->ctl.i386_regs.eip = from->Eip;
1209 to->ctl.i386_regs.cs = from->SegCs;
1210 to->ctl.i386_regs.ss = from->SegSs;
1211 to->ctl.i386_regs.eflags = from->EFlags;
1213 if (flags & CONTEXT_INTEGER)
1215 to->flags |= SERVER_CTX_INTEGER;
1216 to->integer.i386_regs.eax = from->Eax;
1217 to->integer.i386_regs.ebx = from->Ebx;
1218 to->integer.i386_regs.ecx = from->Ecx;
1219 to->integer.i386_regs.edx = from->Edx;
1220 to->integer.i386_regs.esi = from->Esi;
1221 to->integer.i386_regs.edi = from->Edi;
1223 if (flags & CONTEXT_SEGMENTS)
1225 to->flags |= SERVER_CTX_SEGMENTS;
1226 to->seg.i386_regs.ds = from->SegDs;
1227 to->seg.i386_regs.es = from->SegEs;
1228 to->seg.i386_regs.fs = from->SegFs;
1229 to->seg.i386_regs.gs = from->SegGs;
1231 if (flags & CONTEXT_FLOATING_POINT)
1233 to->flags |= SERVER_CTX_FLOATING_POINT;
1234 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1235 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1236 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1237 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1238 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1239 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1240 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1241 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1242 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1244 if (flags & CONTEXT_DEBUG_REGISTERS)
1246 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1247 to->debug.i386_regs.dr0 = from->Dr0;
1248 to->debug.i386_regs.dr1 = from->Dr1;
1249 to->debug.i386_regs.dr2 = from->Dr2;
1250 to->debug.i386_regs.dr3 = from->Dr3;
1251 to->debug.i386_regs.dr6 = from->Dr6;
1252 to->debug.i386_regs.dr7 = from->Dr7;
1254 if (flags & CONTEXT_EXTENDED_REGISTERS)
1256 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1257 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1259 return STATUS_SUCCESS;
1263 /***********************************************************************
1264 * context_from_server
1266 * Convert a register context from the server format.
1268 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1270 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1272 to->ContextFlags = CONTEXT_i386;
1273 if (from->flags & SERVER_CTX_CONTROL)
1275 to->ContextFlags |= CONTEXT_CONTROL;
1276 to->Ebp = from->ctl.i386_regs.ebp;
1277 to->Esp = from->ctl.i386_regs.esp;
1278 to->Eip = from->ctl.i386_regs.eip;
1279 to->SegCs = from->ctl.i386_regs.cs;
1280 to->SegSs = from->ctl.i386_regs.ss;
1281 to->EFlags = from->ctl.i386_regs.eflags;
1283 if (from->flags & SERVER_CTX_INTEGER)
1285 to->ContextFlags |= CONTEXT_INTEGER;
1286 to->Eax = from->integer.i386_regs.eax;
1287 to->Ebx = from->integer.i386_regs.ebx;
1288 to->Ecx = from->integer.i386_regs.ecx;
1289 to->Edx = from->integer.i386_regs.edx;
1290 to->Esi = from->integer.i386_regs.esi;
1291 to->Edi = from->integer.i386_regs.edi;
1293 if (from->flags & SERVER_CTX_SEGMENTS)
1295 to->ContextFlags |= CONTEXT_SEGMENTS;
1296 to->SegDs = from->seg.i386_regs.ds;
1297 to->SegEs = from->seg.i386_regs.es;
1298 to->SegFs = from->seg.i386_regs.fs;
1299 to->SegGs = from->seg.i386_regs.gs;
1301 if (from->flags & SERVER_CTX_FLOATING_POINT)
1303 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1304 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1305 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1306 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1307 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1308 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1309 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1310 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1311 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1312 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1314 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1316 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1317 to->Dr0 = from->debug.i386_regs.dr0;
1318 to->Dr1 = from->debug.i386_regs.dr1;
1319 to->Dr2 = from->debug.i386_regs.dr2;
1320 to->Dr3 = from->debug.i386_regs.dr3;
1321 to->Dr6 = from->debug.i386_regs.dr6;
1322 to->Dr7 = from->debug.i386_regs.dr7;
1324 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1326 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1327 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1329 return STATUS_SUCCESS;
1333 /***********************************************************************
1334 * NtSetContextThread (NTDLL.@)
1335 * ZwSetContextThread (NTDLL.@)
1337 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
1339 NTSTATUS ret = STATUS_SUCCESS;
1340 BOOL self = (handle == GetCurrentThread());
1342 /* debug registers require a server call */
1343 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
1344 self = (x86_thread_data()->dr0 == context->Dr0 &&
1345 x86_thread_data()->dr1 == context->Dr1 &&
1346 x86_thread_data()->dr2 == context->Dr2 &&
1347 x86_thread_data()->dr3 == context->Dr3 &&
1348 x86_thread_data()->dr6 == context->Dr6 &&
1349 x86_thread_data()->dr7 == context->Dr7);
1351 if (!self)
1353 context_t server_context;
1354 context_to_server( &server_context, context );
1355 ret = set_thread_context( handle, &server_context, &self );
1358 if (self && ret == STATUS_SUCCESS) set_cpu_context( context );
1359 return ret;
1363 /***********************************************************************
1364 * NtGetContextThread (NTDLL.@)
1365 * ZwGetContextThread (NTDLL.@)
1367 * Note: we use a small assembly wrapper to save the necessary registers
1368 * in case we are fetching the context of the current thread.
1370 NTSTATUS CDECL DECLSPEC_HIDDEN __regs_NtGetContextThread( DWORD edi, DWORD esi, DWORD ebx, DWORD eflags,
1371 DWORD ebp, DWORD retaddr, HANDLE handle,
1372 CONTEXT *context )
1374 NTSTATUS ret;
1375 DWORD needed_flags = context->ContextFlags & ~CONTEXT_i386;
1376 BOOL self = (handle == GetCurrentThread());
1378 /* debug registers require a server call */
1379 if (needed_flags & CONTEXT_DEBUG_REGISTERS) self = FALSE;
1381 if (!self)
1383 context_t server_context;
1384 unsigned int server_flags = get_server_context_flags( context->ContextFlags );
1386 if ((ret = get_thread_context( handle, &server_context, server_flags, &self ))) return ret;
1387 if ((ret = context_from_server( context, &server_context ))) return ret;
1388 needed_flags &= ~context->ContextFlags;
1391 if (self)
1393 if (needed_flags & CONTEXT_INTEGER)
1395 context->Eax = 0;
1396 context->Ebx = ebx;
1397 context->Ecx = 0;
1398 context->Edx = 0;
1399 context->Esi = esi;
1400 context->Edi = edi;
1401 context->ContextFlags |= CONTEXT_INTEGER;
1403 if (needed_flags & CONTEXT_CONTROL)
1405 context->Ebp = ebp;
1406 context->Esp = (DWORD)&retaddr;
1407 context->Eip = *(&edi - 1);
1408 context->SegCs = wine_get_cs();
1409 context->SegSs = wine_get_ss();
1410 context->EFlags = eflags;
1411 context->ContextFlags |= CONTEXT_CONTROL;
1413 if (needed_flags & CONTEXT_SEGMENTS)
1415 context->SegDs = wine_get_ds();
1416 context->SegEs = wine_get_es();
1417 context->SegFs = wine_get_fs();
1418 context->SegGs = wine_get_gs();
1419 context->ContextFlags |= CONTEXT_SEGMENTS;
1421 if (needed_flags & CONTEXT_FLOATING_POINT) save_fpu( context );
1422 /* FIXME: extended floating point */
1423 /* update the cached version of the debug registers */
1424 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1426 x86_thread_data()->dr0 = context->Dr0;
1427 x86_thread_data()->dr1 = context->Dr1;
1428 x86_thread_data()->dr2 = context->Dr2;
1429 x86_thread_data()->dr3 = context->Dr3;
1430 x86_thread_data()->dr6 = context->Dr6;
1431 x86_thread_data()->dr7 = context->Dr7;
1435 if (context->ContextFlags & (CONTEXT_INTEGER & ~CONTEXT_i386))
1436 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle,
1437 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi );
1438 if (context->ContextFlags & (CONTEXT_CONTROL & ~CONTEXT_i386))
1439 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle,
1440 context->Ebp, context->Esp, context->Eip, context->SegCs, context->SegSs, context->EFlags );
1441 if (context->ContextFlags & (CONTEXT_SEGMENTS & ~CONTEXT_i386))
1442 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle,
1443 context->SegDs, context->SegEs, context->SegFs, context->SegGs );
1444 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1445 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle,
1446 context->Dr0, context->Dr1, context->Dr2, context->Dr3, context->Dr6, context->Dr7 );
1448 return STATUS_SUCCESS;
1450 __ASM_STDCALL_FUNC( NtGetContextThread, 8,
1451 "pushl %ebp\n\t"
1452 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1453 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1454 "movl %esp,%ebp\n\t"
1455 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1456 "pushfl\n\t"
1457 "pushl %ebx\n\t"
1458 __ASM_CFI(".cfi_rel_offset %ebx,-8\n\t")
1459 "pushl %esi\n\t"
1460 __ASM_CFI(".cfi_rel_offset %esi,-12\n\t")
1461 "pushl %edi\n\t"
1462 __ASM_CFI(".cfi_rel_offset %edi,-16\n\t")
1463 "call " __ASM_NAME("__regs_NtGetContextThread") "\n\t"
1464 "leave\n\t"
1465 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1466 __ASM_CFI(".cfi_same_value %ebp\n\t")
1467 "ret $8" )
1470 /***********************************************************************
1471 * is_privileged_instr
1473 * Check if the fault location is a privileged instruction.
1474 * Based on the instruction emulation code in dlls/kernel/instr.c.
1476 static inline DWORD is_privileged_instr( CONTEXT *context )
1478 const BYTE *instr;
1479 unsigned int prefix_count = 0;
1481 if (!wine_ldt_is_system( context->SegCs )) return 0;
1482 instr = (BYTE *)context->Eip;
1484 for (;;) switch(*instr)
1486 /* instruction prefixes */
1487 case 0x2e: /* %cs: */
1488 case 0x36: /* %ss: */
1489 case 0x3e: /* %ds: */
1490 case 0x26: /* %es: */
1491 case 0x64: /* %fs: */
1492 case 0x65: /* %gs: */
1493 case 0x66: /* opcode size */
1494 case 0x67: /* addr size */
1495 case 0xf0: /* lock */
1496 case 0xf2: /* repne */
1497 case 0xf3: /* repe */
1498 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1499 instr++;
1500 continue;
1502 case 0x0f: /* extended instruction */
1503 switch(instr[1])
1505 case 0x20: /* mov crX, reg */
1506 case 0x21: /* mov drX, reg */
1507 case 0x22: /* mov reg, crX */
1508 case 0x23: /* mov reg drX */
1509 return EXCEPTION_PRIV_INSTRUCTION;
1511 return 0;
1512 case 0x6c: /* insb (%dx) */
1513 case 0x6d: /* insl (%dx) */
1514 case 0x6e: /* outsb (%dx) */
1515 case 0x6f: /* outsl (%dx) */
1516 case 0xcd: /* int $xx */
1517 case 0xe4: /* inb al,XX */
1518 case 0xe5: /* in (e)ax,XX */
1519 case 0xe6: /* outb XX,al */
1520 case 0xe7: /* out XX,(e)ax */
1521 case 0xec: /* inb (%dx),%al */
1522 case 0xed: /* inl (%dx),%eax */
1523 case 0xee: /* outb %al,(%dx) */
1524 case 0xef: /* outl %eax,(%dx) */
1525 case 0xf4: /* hlt */
1526 case 0xfa: /* cli */
1527 case 0xfb: /* sti */
1528 return EXCEPTION_PRIV_INSTRUCTION;
1529 default:
1530 return 0;
1535 /***********************************************************************
1536 * handle_interrupt
1538 * Handle an interrupt.
1540 static inline BOOL handle_interrupt( unsigned int interrupt, EXCEPTION_RECORD *rec, CONTEXT *context )
1542 switch(interrupt)
1544 case 0x2d:
1545 context->Eip += 3;
1546 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1547 rec->ExceptionAddress = (void *)context->Eip;
1548 rec->NumberParameters = is_wow64 ? 1 : 3;
1549 rec->ExceptionInformation[0] = context->Eax;
1550 rec->ExceptionInformation[1] = context->Ecx;
1551 rec->ExceptionInformation[2] = context->Edx;
1552 return TRUE;
1553 default:
1554 return FALSE;
1559 /***********************************************************************
1560 * check_invalid_gs
1562 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1564 static inline BOOL check_invalid_gs( CONTEXT *context )
1566 unsigned int prefix_count = 0;
1567 const BYTE *instr = (BYTE *)context->Eip;
1568 WORD system_gs = x86_thread_data()->gs;
1570 if (context->SegGs == system_gs) return FALSE;
1571 if (!wine_ldt_is_system( context->SegCs )) return FALSE;
1572 /* only handle faults in system libraries */
1573 if (virtual_is_valid_code_address( instr, 1 )) return FALSE;
1575 for (;;) switch(*instr)
1577 /* instruction prefixes */
1578 case 0x2e: /* %cs: */
1579 case 0x36: /* %ss: */
1580 case 0x3e: /* %ds: */
1581 case 0x26: /* %es: */
1582 case 0x64: /* %fs: */
1583 case 0x66: /* opcode size */
1584 case 0x67: /* addr size */
1585 case 0xf0: /* lock */
1586 case 0xf2: /* repne */
1587 case 0xf3: /* repe */
1588 if (++prefix_count >= 15) return FALSE;
1589 instr++;
1590 continue;
1591 case 0x65: /* %gs: */
1592 TRACE( "%04x/%04x at %p, fixing up\n", context->SegGs, system_gs, instr );
1593 context->SegGs = system_gs;
1594 return TRUE;
1595 default:
1596 return FALSE;
1601 #include "pshpack1.h"
1602 union atl_thunk
1604 struct
1606 DWORD movl; /* movl this,4(%esp) */
1607 DWORD this;
1608 BYTE jmp; /* jmp func */
1609 int func;
1610 } t1;
1611 struct
1613 BYTE movl; /* movl this,ecx */
1614 DWORD this;
1615 BYTE jmp; /* jmp func */
1616 int func;
1617 } t2;
1618 struct
1620 BYTE movl1; /* movl this,edx */
1621 DWORD this;
1622 BYTE movl2; /* movl func,ecx */
1623 DWORD func;
1624 WORD jmp; /* jmp ecx */
1625 } t3;
1626 struct
1628 BYTE movl1; /* movl this,ecx */
1629 DWORD this;
1630 BYTE movl2; /* movl func,eax */
1631 DWORD func;
1632 WORD jmp; /* jmp eax */
1633 } t4;
1634 struct
1636 DWORD inst1; /* pop ecx
1637 * pop eax
1638 * push ecx
1639 * jmp 4(%eax) */
1640 WORD inst2;
1641 } t5;
1643 #include "poppack.h"
1645 /**********************************************************************
1646 * check_atl_thunk
1648 * Check if code destination is an ATL thunk, and emulate it if so.
1650 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1652 const union atl_thunk *thunk = (const union atl_thunk *)rec->ExceptionInformation[1];
1653 union atl_thunk thunk_copy;
1654 SIZE_T thunk_len;
1656 thunk_len = virtual_uninterrupted_read_memory( thunk, &thunk_copy, sizeof(*thunk) );
1657 if (!thunk_len) return FALSE;
1659 if (thunk_len >= sizeof(thunk_copy.t1) && thunk_copy.t1.movl == 0x042444c7 &&
1660 thunk_copy.t1.jmp == 0xe9)
1662 if (!virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1,
1663 &thunk_copy.t1.this, sizeof(DWORD) ))
1665 context->Eip = (DWORD_PTR)(&thunk->t1.func + 1) + thunk_copy.t1.func;
1666 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1667 thunk, context->Eip, thunk_copy.t1.this );
1668 return TRUE;
1671 else if (thunk_len >= sizeof(thunk_copy.t2) && thunk_copy.t2.movl == 0xb9 &&
1672 thunk_copy.t2.jmp == 0xe9)
1674 context->Ecx = thunk_copy.t2.this;
1675 context->Eip = (DWORD_PTR)(&thunk->t2.func + 1) + thunk_copy.t2.func;
1676 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1677 thunk, context->Eip, context->Ecx );
1678 return TRUE;
1680 else if (thunk_len >= sizeof(thunk_copy.t3) && thunk_copy.t3.movl1 == 0xba &&
1681 thunk_copy.t3.movl2 == 0xb9 &&
1682 thunk_copy.t3.jmp == 0xe1ff)
1684 context->Edx = thunk_copy.t3.this;
1685 context->Ecx = thunk_copy.t3.func;
1686 context->Eip = thunk_copy.t3.func;
1687 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1688 thunk, context->Eip, context->Ecx, context->Edx );
1689 return TRUE;
1691 else if (thunk_len >= sizeof(thunk_copy.t4) && thunk_copy.t4.movl1 == 0xb9 &&
1692 thunk_copy.t4.movl2 == 0xb8 &&
1693 thunk_copy.t4.jmp == 0xe0ff)
1695 context->Ecx = thunk_copy.t4.this;
1696 context->Eax = thunk_copy.t4.func;
1697 context->Eip = thunk_copy.t4.func;
1698 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1699 thunk, context->Eip, context->Eax, context->Ecx );
1700 return TRUE;
1702 else if (thunk_len >= sizeof(thunk_copy.t5) && thunk_copy.t5.inst1 == 0xff515859 &&
1703 thunk_copy.t5.inst2 == 0x0460)
1705 DWORD func, stack[2];
1706 if (virtual_uninterrupted_read_memory( (DWORD *)context->Esp,
1707 stack, sizeof(stack) ) == sizeof(stack) &&
1708 virtual_uninterrupted_read_memory( (DWORD *)stack[1] + 1,
1709 &func, sizeof(DWORD) ) == sizeof(DWORD) &&
1710 !virtual_uninterrupted_write_memory( (DWORD *)context->Esp + 1, &stack[0], sizeof(stack[0]) ))
1712 context->Ecx = stack[0];
1713 context->Eax = stack[1];
1714 context->Esp = context->Esp + sizeof(DWORD);
1715 context->Eip = func;
1716 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1717 thunk, context->Eip, context->Eax, context->Ecx, context->Esp );
1718 return TRUE;
1722 return FALSE;
1726 /***********************************************************************
1727 * setup_exception_record
1729 * Setup the exception record and context on the thread stack.
1731 static EXCEPTION_RECORD *setup_exception_record( ucontext_t *sigcontext, void *stack_ptr,
1732 WORD fs, WORD gs, raise_func func )
1734 struct stack_layout
1736 void *ret_addr; /* return address from raise_func */
1737 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1738 CONTEXT *context_ptr; /* second arg for raise_func */
1739 CONTEXT context;
1740 EXCEPTION_RECORD rec;
1741 DWORD ebp;
1742 DWORD eip;
1743 } *stack = stack_ptr;
1744 DWORD exception_code = 0;
1746 /* stack sanity checks */
1748 if ((char *)stack >= (char *)get_signal_stack() &&
1749 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1751 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1752 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1753 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1754 NtCurrentTeb()->Tib.StackBase );
1755 abort_thread(1);
1758 if (stack - 1 > stack || /* check for overflow in subtraction */
1759 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1760 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1762 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1763 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1764 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1765 NtCurrentTeb()->Tib.StackBase );
1767 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1769 /* stack overflow on last page, unrecoverable */
1770 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1771 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1772 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1773 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1774 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1775 abort_thread(1);
1777 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1779 /* stack access below stack limit, may be recoverable */
1780 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1781 else
1783 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1784 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1785 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1786 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1787 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1788 abort_thread(1);
1792 stack--; /* push the stack_layout structure */
1793 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1794 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1795 #elif defined(VALGRIND_MAKE_WRITABLE)
1796 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1797 #endif
1798 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1799 stack->rec_ptr = &stack->rec;
1800 stack->context_ptr = &stack->context;
1802 stack->rec.ExceptionRecord = NULL;
1803 stack->rec.ExceptionCode = exception_code;
1804 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1805 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1806 stack->rec.NumberParameters = 0;
1808 save_context( &stack->context, sigcontext, fs, gs );
1810 /* now modify the sigcontext to return to the raise function */
1811 ESP_sig(sigcontext) = (DWORD)stack;
1812 EIP_sig(sigcontext) = (DWORD)func;
1813 /* clear single-step, direction, and align check flag */
1814 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1815 CS_sig(sigcontext) = wine_get_cs();
1816 DS_sig(sigcontext) = wine_get_ds();
1817 ES_sig(sigcontext) = wine_get_es();
1818 FS_sig(sigcontext) = wine_get_fs();
1819 GS_sig(sigcontext) = wine_get_gs();
1820 SS_sig(sigcontext) = wine_get_ss();
1822 return stack->rec_ptr;
1826 /***********************************************************************
1827 * setup_exception
1829 * Setup a proper stack frame for the raise function, and modify the
1830 * sigcontext so that the return from the signal handler will call
1831 * the raise function.
1833 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1835 WORD fs, gs;
1836 void *stack = init_handler( sigcontext, &fs, &gs );
1838 return setup_exception_record( sigcontext, stack, fs, gs, func );
1842 /***********************************************************************
1843 * get_exception_context
1845 * Get a pointer to the context built by setup_exception.
1847 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1849 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1853 /**********************************************************************
1854 * get_fpu_code
1856 * Get the FPU exception code from the FPU status.
1858 static inline DWORD get_fpu_code( const CONTEXT *context )
1860 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1862 if (status & 0x01) /* IE */
1864 if (status & 0x40) /* SF */
1865 return EXCEPTION_FLT_STACK_CHECK;
1866 else
1867 return EXCEPTION_FLT_INVALID_OPERATION;
1869 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1870 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1871 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1872 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1873 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1874 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1878 /**********************************************************************
1879 * raise_segv_exception
1881 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1883 NTSTATUS status;
1885 switch(rec->ExceptionCode)
1887 case EXCEPTION_ACCESS_VIOLATION:
1888 if (rec->NumberParameters == 2)
1890 if (rec->ExceptionInformation[1] == 0xffffffff && check_invalid_gs( context ))
1891 goto done;
1892 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1893 rec->ExceptionInformation[0], FALSE )))
1894 goto done;
1895 if (rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
1896 rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT)
1898 ULONG flags;
1899 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
1900 &flags, sizeof(flags), NULL );
1902 if (!(flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION) && check_atl_thunk( rec, context ))
1903 goto done;
1905 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1906 if (!(flags & MEM_EXECUTE_OPTION_DISABLE))
1907 rec->ExceptionInformation[0] = EXCEPTION_READ_FAULT;
1910 break;
1911 case EXCEPTION_DATATYPE_MISALIGNMENT:
1912 /* FIXME: pass through exception handler first? */
1913 if (context->EFlags & 0x00040000)
1915 /* Disable AC flag, return */
1916 context->EFlags &= ~0x00040000;
1917 goto done;
1919 break;
1920 case EXCEPTION_BREAKPOINT:
1921 if (!is_wow64)
1923 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
1924 * service is usually not recognized when called from usermode. */
1925 switch (rec->ExceptionInformation[0])
1927 case 1: /* BREAKPOINT_PRINT */
1928 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
1929 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
1930 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
1931 goto done;
1934 break;
1936 status = NtRaiseException( rec, context, TRUE );
1937 raise_status( status, rec );
1938 done:
1939 set_cpu_context( context );
1943 /**********************************************************************
1944 * raise_trap_exception
1946 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1948 NTSTATUS status;
1950 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1952 /* when single stepping can't tell whether this is a hw bp or a
1953 * single step interrupt. try to avoid as much overhead as possible
1954 * and only do a server call if there is any hw bp enabled. */
1956 if( !(context->EFlags & 0x100) || (x86_thread_data()->dr7 & 0xff) )
1958 /* (possible) hardware breakpoint, fetch the debug registers */
1959 DWORD saved_flags = context->ContextFlags;
1960 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1961 NtGetContextThread(GetCurrentThread(), context);
1962 context->ContextFlags |= saved_flags; /* restore flags */
1965 context->EFlags &= ~0x100; /* clear single-step flag */
1968 status = NtRaiseException( rec, context, TRUE );
1969 raise_status( status, rec );
1973 /**********************************************************************
1974 * raise_generic_exception
1976 * Generic raise function for exceptions that don't need special treatment.
1978 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1980 NTSTATUS status;
1982 status = NtRaiseException( rec, context, TRUE );
1983 raise_status( status, rec );
1987 /**********************************************************************
1988 * segv_handler
1990 * Handler for SIGSEGV and related errors.
1992 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1994 WORD fs, gs;
1995 EXCEPTION_RECORD *rec;
1996 ucontext_t *context = sigcontext;
1997 void *stack = init_handler( sigcontext, &fs, &gs );
1999 /* check for exceptions on the signal stack caused by write watches */
2000 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
2001 (char *)stack >= (char *)get_signal_stack() &&
2002 (char *)stack < (char *)get_signal_stack() + signal_stack_size &&
2003 !virtual_handle_fault( siginfo->si_addr, (get_error_code(context) >> 1) & 0x09, TRUE ))
2005 return;
2008 /* check for page fault inside the thread stack */
2009 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
2010 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
2011 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
2012 virtual_handle_stack_fault( siginfo->si_addr ))
2014 /* check if this was the last guard page */
2015 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
2017 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2018 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2020 return;
2023 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
2024 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
2026 switch(get_trap_code(context))
2028 case TRAP_x86_OFLOW: /* Overflow exception */
2029 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2030 break;
2031 case TRAP_x86_BOUND: /* Bound range exception */
2032 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2033 break;
2034 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2035 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2036 break;
2037 case TRAP_x86_STKFLT: /* Stack fault */
2038 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2039 break;
2040 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2041 case TRAP_x86_PROTFLT: /* General protection fault */
2042 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2044 CONTEXT *win_context = get_exception_context( rec );
2045 WORD err = get_error_code(context);
2046 if (!err && (rec->ExceptionCode = is_privileged_instr( win_context ))) break;
2047 if ((err & 7) == 2 && handle_interrupt( err >> 3, rec, win_context )) break;
2048 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2049 rec->NumberParameters = 2;
2050 rec->ExceptionInformation[0] = 0;
2051 /* if error contains a LDT selector, use that as fault address */
2052 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
2053 rec->ExceptionInformation[1] = err & ~7;
2054 else
2055 rec->ExceptionInformation[1] = 0xffffffff;
2057 break;
2058 case TRAP_x86_PAGEFLT: /* Page fault */
2059 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2060 rec->NumberParameters = 2;
2061 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
2062 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2063 break;
2064 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2065 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2066 break;
2067 default:
2068 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2069 /* fall through */
2070 case TRAP_x86_NMI: /* NMI interrupt */
2071 case TRAP_x86_DNA: /* Device not available exception */
2072 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2073 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2074 case TRAP_x86_MCHK: /* Machine check exception */
2075 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2076 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2077 break;
2082 /**********************************************************************
2083 * trap_handler
2085 * Handler for SIGTRAP.
2087 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2089 ucontext_t *context = sigcontext;
2090 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
2092 switch(get_trap_code(context))
2094 case TRAP_x86_TRCTRAP: /* Single-step exception */
2095 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2096 break;
2097 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2098 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2099 /* fall through */
2100 default:
2101 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2102 rec->NumberParameters = is_wow64 ? 1 : 3;
2103 rec->ExceptionInformation[0] = 0;
2104 rec->ExceptionInformation[1] = 0; /* FIXME */
2105 rec->ExceptionInformation[2] = 0; /* FIXME */
2106 break;
2111 /**********************************************************************
2112 * fpe_handler
2114 * Handler for SIGFPE.
2116 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2118 CONTEXT *win_context;
2119 ucontext_t *context = sigcontext;
2120 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
2122 win_context = get_exception_context( rec );
2124 switch(get_trap_code(context))
2126 case TRAP_x86_DIVIDE: /* Division by zero exception */
2127 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2128 break;
2129 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
2130 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2131 break;
2132 case TRAP_x86_ARITHTRAP: /* Floating point exception */
2133 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2134 rec->ExceptionCode = get_fpu_code( win_context );
2135 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
2136 break;
2137 case TRAP_x86_CACHEFLT: /* SIMD exception */
2138 /* TODO:
2139 * Behaviour only tested for divide-by-zero exceptions
2140 * Check for other SIMD exceptions as well */
2141 if(siginfo->si_code != FPE_FLTDIV && siginfo->si_code != FPE_FLTINV)
2142 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2143 siginfo->si_code);
2145 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
2146 rec->NumberParameters = 1;
2147 /* no idea what meaning is actually behind this but that's what native does */
2148 rec->ExceptionInformation[0] = 0;
2149 break;
2150 default:
2151 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
2152 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2153 break;
2158 /**********************************************************************
2159 * int_handler
2161 * Handler for SIGINT.
2163 * FIXME: should not be calling external functions on the signal stack.
2165 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2167 WORD fs, gs;
2168 init_handler( sigcontext, &fs, &gs );
2169 if (!dispatch_signal(SIGINT))
2171 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2172 rec->ExceptionCode = CONTROL_C_EXIT;
2176 /**********************************************************************
2177 * abrt_handler
2179 * Handler for SIGABRT.
2181 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2183 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2184 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2185 rec->ExceptionFlags = EH_NONCONTINUABLE;
2189 /**********************************************************************
2190 * quit_handler
2192 * Handler for SIGQUIT.
2194 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2196 WORD fs, gs;
2197 init_handler( sigcontext, &fs, &gs );
2198 abort_thread(0);
2202 /**********************************************************************
2203 * usr1_handler
2205 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2207 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2209 CONTEXT context;
2210 WORD fs, gs;
2212 init_handler( sigcontext, &fs, &gs );
2213 save_context( &context, sigcontext, fs, gs );
2214 wait_suspend( &context );
2215 restore_context( &context, sigcontext );
2219 /***********************************************************************
2220 * __wine_set_signal_handler (NTDLL.@)
2222 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2224 if (sig >= ARRAY_SIZE(handlers)) return -1;
2225 if (handlers[sig] != NULL) return -2;
2226 handlers[sig] = wsh;
2227 return 0;
2231 /***********************************************************************
2232 * locking for LDT routines
2234 static RTL_CRITICAL_SECTION ldt_section;
2235 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
2237 0, 0, &ldt_section,
2238 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
2239 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
2241 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
2242 static sigset_t ldt_sigset;
2244 static void ldt_lock(void)
2246 sigset_t sigset;
2248 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
2249 RtlEnterCriticalSection( &ldt_section );
2250 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
2253 static void ldt_unlock(void)
2255 if (ldt_section.RecursionCount == 1)
2257 sigset_t sigset = ldt_sigset;
2258 RtlLeaveCriticalSection( &ldt_section );
2259 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
2261 else RtlLeaveCriticalSection( &ldt_section );
2265 /**********************************************************************
2266 * signal_alloc_thread
2268 NTSTATUS signal_alloc_thread( TEB **teb )
2270 static size_t sigstack_zero_bits;
2271 struct x86_thread_data *thread_data;
2272 SIZE_T size;
2273 void *addr = NULL;
2274 NTSTATUS status;
2276 if (!sigstack_zero_bits)
2278 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2279 /* find the first power of two not smaller than min_size */
2280 sigstack_zero_bits = 12;
2281 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2282 signal_stack_mask = (1 << sigstack_zero_bits) - 1;
2283 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2286 size = signal_stack_mask + 1;
2287 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
2288 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2290 *teb = addr;
2291 (*teb)->Tib.Self = &(*teb)->Tib;
2292 (*teb)->Tib.ExceptionList = (void *)~0UL;
2293 thread_data = (struct x86_thread_data *)(*teb)->SystemReserved2;
2294 if (!(thread_data->fs = wine_ldt_alloc_fs()))
2296 size = 0;
2297 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
2298 status = STATUS_TOO_MANY_THREADS;
2301 return status;
2305 /**********************************************************************
2306 * signal_free_thread
2308 void signal_free_thread( TEB *teb )
2310 SIZE_T size = 0;
2311 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2313 wine_ldt_free_fs( thread_data->fs );
2314 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2318 /**********************************************************************
2319 * signal_init_thread
2321 void signal_init_thread( TEB *teb )
2323 const WORD fpu_cw = 0x27f;
2324 struct x86_thread_data *thread_data = (struct x86_thread_data *)teb->SystemReserved2;
2325 LDT_ENTRY fs_entry;
2326 stack_t ss;
2328 #ifdef __APPLE__
2329 int mib[2], val = 1;
2331 mib[0] = CTL_KERN;
2332 mib[1] = KERN_THALTSTACK;
2333 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2334 #endif
2336 ss.ss_sp = (char *)teb + teb_size;
2337 ss.ss_size = signal_stack_size;
2338 ss.ss_flags = 0;
2339 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2341 wine_ldt_set_base( &fs_entry, teb );
2342 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2343 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2344 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2345 thread_data->gs = wine_get_gs();
2347 #ifdef __GNUC__
2348 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2349 #else
2350 FIXME("FPU setup not implemented for this platform.\n");
2351 #endif
2354 /**********************************************************************
2355 * signal_init_process
2357 void signal_init_process(void)
2359 struct sigaction sig_act;
2361 sig_act.sa_mask = server_block_set;
2362 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2363 #ifdef SA_ONSTACK
2364 sig_act.sa_flags |= SA_ONSTACK;
2365 #endif
2366 #ifdef __ANDROID__
2367 sig_act.sa_flags |= SA_RESTORER;
2368 sig_act.sa_restorer = rt_sigreturn;
2369 #endif
2370 sig_act.sa_sigaction = int_handler;
2371 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2372 sig_act.sa_sigaction = fpe_handler;
2373 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2374 sig_act.sa_sigaction = abrt_handler;
2375 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2376 sig_act.sa_sigaction = quit_handler;
2377 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2378 sig_act.sa_sigaction = usr1_handler;
2379 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2381 sig_act.sa_sigaction = segv_handler;
2382 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2383 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2384 #ifdef SIGBUS
2385 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2386 #endif
2388 #ifdef SIGTRAP
2389 sig_act.sa_sigaction = trap_handler;
2390 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2391 #endif
2393 wine_ldt_init_locking( ldt_lock, ldt_unlock );
2394 return;
2396 error:
2397 perror("sigaction");
2398 exit(1);
2402 /*******************************************************************
2403 * RtlUnwind (NTDLL.@)
2405 void WINAPI DECLSPEC_HIDDEN __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2406 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2408 EXCEPTION_RECORD record;
2409 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2410 DWORD res;
2412 context->Eax = (DWORD)retval;
2414 /* build an exception record, if we do not have one */
2415 if (!pRecord)
2417 record.ExceptionCode = STATUS_UNWIND;
2418 record.ExceptionFlags = 0;
2419 record.ExceptionRecord = NULL;
2420 record.ExceptionAddress = (void *)context->Eip;
2421 record.NumberParameters = 0;
2422 pRecord = &record;
2425 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2427 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2428 TRACE( "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
2429 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi );
2430 TRACE( "ebp=%08x esp=%08x eip=%08x cs=%04x ds=%04x fs=%04x gs=%04x flags=%08x\n",
2431 context->Ebp, context->Esp, context->Eip, LOWORD(context->SegCs), LOWORD(context->SegDs),
2432 LOWORD(context->SegFs), LOWORD(context->SegGs), context->EFlags );
2434 /* get chain of exception frames */
2435 frame = NtCurrentTeb()->Tib.ExceptionList;
2436 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2438 /* Check frame address */
2439 if (pEndFrame && (frame > pEndFrame))
2440 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2442 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, pRecord );
2444 /* Call handler */
2445 TRACE( "calling handler at %p code=%x flags=%x\n",
2446 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2447 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2448 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2450 switch(res)
2452 case ExceptionContinueSearch:
2453 break;
2454 case ExceptionCollidedUnwind:
2455 frame = dispatch;
2456 break;
2457 default:
2458 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2459 break;
2461 frame = __wine_pop_frame( frame );
2464 NtSetContextThread( GetCurrentThread(), context );
2466 __ASM_STDCALL_FUNC( RtlUnwind, 16,
2467 "pushl %ebp\n\t"
2468 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2469 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2470 "movl %esp,%ebp\n\t"
2471 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2472 "leal -(0x2cc+8)(%esp),%esp\n\t" /* sizeof(CONTEXT) + alignment */
2473 "pushl %eax\n\t"
2474 "leal 4(%esp),%eax\n\t" /* context */
2475 "xchgl %eax,(%esp)\n\t"
2476 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2477 "leal 24(%ebp),%eax\n\t"
2478 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2479 "pushl %esp\n\t"
2480 "pushl 20(%ebp)\n\t"
2481 "pushl 16(%ebp)\n\t"
2482 "pushl 12(%ebp)\n\t"
2483 "pushl 8(%ebp)\n\t"
2484 "call " __ASM_NAME("__regs_RtlUnwind") __ASM_STDCALL(20) "\n\t"
2485 "leave\n\t"
2486 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2487 __ASM_CFI(".cfi_same_value %ebp\n\t")
2488 "ret $16" ) /* actually never returns */
2491 /*******************************************************************
2492 * NtRaiseException (NTDLL.@)
2494 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2496 NTSTATUS status = raise_exception( rec, context, first_chance );
2497 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2498 return status;
2502 /***********************************************************************
2503 * RtlRaiseException (NTDLL.@)
2505 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
2506 "pushl %ebp\n\t"
2507 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2508 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2509 "movl %esp,%ebp\n\t"
2510 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2511 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
2512 "pushl %esp\n\t" /* context */
2513 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2514 "movl 4(%ebp),%eax\n\t" /* return address */
2515 "movl 8(%ebp),%ecx\n\t" /* rec */
2516 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
2517 "leal 12(%ebp),%eax\n\t"
2518 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2519 "movl %esp,%eax\n\t"
2520 "pushl $1\n\t"
2521 "pushl %eax\n\t"
2522 "pushl %ecx\n\t"
2523 "call " __ASM_NAME("NtRaiseException") __ASM_STDCALL(12) "\n\t"
2524 "pushl %eax\n\t"
2525 "call " __ASM_NAME("RtlRaiseStatus") __ASM_STDCALL(4) "\n\t"
2526 "leave\n\t"
2527 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2528 __ASM_CFI(".cfi_same_value %ebp\n\t")
2529 "ret $4" ) /* actually never returns */
2532 /*************************************************************************
2533 * RtlCaptureStackBackTrace (NTDLL.@)
2535 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2537 CONTEXT context;
2538 ULONG i;
2539 ULONG *frame;
2541 RtlCaptureContext( &context );
2542 if (hash) *hash = 0;
2543 frame = (ULONG *)context.Ebp;
2545 while (skip--)
2547 if (!is_valid_frame( frame )) return 0;
2548 frame = (ULONG *)*frame;
2551 for (i = 0; i < count; i++)
2553 if (!is_valid_frame( frame )) break;
2554 buffer[i] = (void *)frame[1];
2555 if (hash) *hash += frame[1];
2556 frame = (ULONG *)*frame;
2558 return i;
2562 extern void DECLSPEC_NORETURN start_thread( LPTHREAD_START_ROUTINE entry, void *arg, BOOL suspend,
2563 void *relay );
2564 __ASM_GLOBAL_FUNC( start_thread,
2565 "pushl %ebp\n\t"
2566 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2567 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2568 "movl %esp,%ebp\n\t"
2569 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2570 "pushl %ebx\n\t"
2571 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2572 "pushl %esi\n\t"
2573 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2574 "pushl %edi\n\t"
2575 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2576 /* store exit frame */
2577 "movl %ebp,%fs:0x1f4\n\t" /* x86_thread_data()->exit_frame */
2578 /* switch to thread stack */
2579 "movl %fs:4,%eax\n\t" /* NtCurrentTeb()->StackBase */
2580 "leal -0x1000(%eax),%esp\n\t"
2581 /* attach dlls */
2582 "pushl 20(%ebp)\n\t" /* relay */
2583 "pushl 16(%ebp)\n\t" /* suspend */
2584 "pushl 12(%ebp)\n\t" /* arg */
2585 "pushl 8(%ebp)\n\t" /* entry */
2586 "xorl %ebp,%ebp\n\t"
2587 "call " __ASM_NAME("attach_thread") "\n\t"
2588 "movl %eax,%esi\n\t"
2589 "leal -12(%eax),%esp\n\t"
2590 /* clear the stack */
2591 "andl $~0xfff,%eax\n\t" /* round down to page size */
2592 "movl %eax,(%esp)\n\t"
2593 "call " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
2594 /* switch to the initial context */
2595 "movl %esi,(%esp)\n\t"
2596 "call " __ASM_NAME("set_cpu_context") )
2598 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int) );
2599 __ASM_GLOBAL_FUNC( call_thread_exit_func,
2600 "movl 8(%esp),%ecx\n\t"
2601 /* fetch exit frame */
2602 "movl %fs:0x1f4,%edx\n\t" /* x86_thread_data()->exit_frame */
2603 "testl %edx,%edx\n\t"
2604 "jnz 1f\n\t"
2605 "jmp *%ecx\n\t"
2606 /* switch to exit frame stack */
2607 "1:\tmovl 4(%esp),%eax\n\t"
2608 "movl $0,%fs:0x1f4\n\t"
2609 "movl %edx,%ebp\n\t"
2610 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2611 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2612 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2613 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2614 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2615 "leal -20(%ebp),%esp\n\t"
2616 "pushl %eax\n\t"
2617 "call *%ecx" )
2619 extern void call_thread_entry(void) DECLSPEC_HIDDEN;
2620 __ASM_GLOBAL_FUNC( call_thread_entry,
2621 "pushl %ebp\n\t"
2622 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2623 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2624 "movl %esp,%ebp\n\t"
2625 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2626 "pushl %ebx\n\t" /* arg */
2627 "pushl %eax\n\t" /* entry */
2628 "call " __ASM_NAME("call_thread_func") )
2630 /* wrapper for apps that don't declare the thread function correctly */
2631 extern DWORD call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry, void *arg );
2632 __ASM_GLOBAL_FUNC(call_thread_func_wrapper,
2633 "pushl %ebp\n\t"
2634 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2635 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2636 "movl %esp,%ebp\n\t"
2637 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2638 "subl $4,%esp\n\t"
2639 "pushl 12(%ebp)\n\t"
2640 "call *8(%ebp)\n\t"
2641 "leave\n\t"
2642 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2643 __ASM_CFI(".cfi_same_value %ebp\n\t")
2644 "ret" )
2646 /***********************************************************************
2647 * call_thread_func
2649 void DECLSPEC_HIDDEN call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg )
2651 __TRY
2653 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
2654 RtlExitUserThread( call_thread_func_wrapper( entry, arg ));
2656 __EXCEPT(call_unhandled_exception_filter)
2658 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2660 __ENDTRY
2661 abort(); /* should not be reached */
2665 /***********************************************************************
2666 * init_thread_context
2668 static void init_thread_context( CONTEXT *context, LPTHREAD_START_ROUTINE entry, void *arg, void *relay )
2670 context->SegCs = wine_get_cs();
2671 context->SegDs = wine_get_ds();
2672 context->SegEs = wine_get_es();
2673 context->SegFs = wine_get_fs();
2674 context->SegGs = wine_get_gs();
2675 context->SegSs = wine_get_ss();
2676 context->EFlags = 0x202;
2677 context->Eax = (DWORD)entry;
2678 context->Ebx = (DWORD)arg;
2679 context->Esp = (DWORD)NtCurrentTeb()->Tib.StackBase - 16;
2680 context->Eip = (DWORD)relay;
2681 context->FloatSave.ControlWord = 0x27f;
2682 ((XMM_SAVE_AREA32 *)context->ExtendedRegisters)->ControlWord = 0x27f;
2686 /***********************************************************************
2687 * attach_thread
2689 PCONTEXT DECLSPEC_HIDDEN attach_thread( LPTHREAD_START_ROUTINE entry, void *arg,
2690 BOOL suspend, void *relay )
2692 CONTEXT *ctx;
2694 if (suspend)
2696 CONTEXT context = { CONTEXT_ALL };
2698 init_thread_context( &context, entry, arg, relay );
2699 wait_suspend( &context );
2700 ctx = (CONTEXT *)((ULONG_PTR)context.Esp & ~15) - 1;
2701 *ctx = context;
2703 else
2705 ctx = (CONTEXT *)((char *)NtCurrentTeb()->Tib.StackBase - 16) - 1;
2706 init_thread_context( ctx, entry, arg, relay );
2708 ctx->ContextFlags = CONTEXT_FULL;
2709 attach_dlls( ctx, (void **)&ctx->Eax );
2710 return ctx;
2714 /***********************************************************************
2715 * signal_start_thread
2717 * Thread startup sequence:
2718 * signal_start_thread()
2719 * -> start_thread()
2720 * -> call_thread_entry()
2721 * -> call_thread_func()
2723 void signal_start_thread( LPTHREAD_START_ROUTINE entry, void *arg, BOOL suspend )
2725 start_thread( entry, arg, suspend, call_thread_entry );
2728 /**********************************************************************
2729 * signal_start_process
2731 * Process startup sequence:
2732 * signal_start_process()
2733 * -> start_thread()
2734 * -> kernel32_start_process()
2736 void signal_start_process( LPTHREAD_START_ROUTINE entry, BOOL suspend )
2738 start_thread( entry, NtCurrentTeb()->Peb, suspend, kernel32_start_process );
2742 /***********************************************************************
2743 * signal_exit_thread
2745 void signal_exit_thread( int status )
2747 call_thread_exit_func( status, exit_thread );
2750 /***********************************************************************
2751 * signal_exit_process
2753 void signal_exit_process( int status )
2755 call_thread_exit_func( status, exit );
2758 /**********************************************************************
2759 * DbgBreakPoint (NTDLL.@)
2761 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2763 /**********************************************************************
2764 * DbgUserBreakPoint (NTDLL.@)
2766 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2768 /**********************************************************************
2769 * NtCurrentTeb (NTDLL.@)
2771 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2774 /**************************************************************************
2775 * _chkstk (NTDLL.@)
2777 __ASM_STDCALL_FUNC( _chkstk, 0,
2778 "negl %eax\n\t"
2779 "addl %esp,%eax\n\t"
2780 "xchgl %esp,%eax\n\t"
2781 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2782 "movl %eax,0(%esp)\n\t"
2783 "ret" )
2785 /**************************************************************************
2786 * _alloca_probe (NTDLL.@)
2788 __ASM_STDCALL_FUNC( _alloca_probe, 0,
2789 "negl %eax\n\t"
2790 "addl %esp,%eax\n\t"
2791 "xchgl %esp,%eax\n\t"
2792 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2793 "movl %eax,0(%esp)\n\t"
2794 "ret" )
2797 /**********************************************************************
2798 * EXC_CallHandler (internal)
2800 * Some exception handlers depend on EBP to have a fixed position relative to
2801 * the exception frame.
2802 * Shrinker depends on (*1) doing what it does,
2803 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2804 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2805 * function calling the handler having only 5 parameters (*4).
2807 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2808 "pushl %ebp\n\t"
2809 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2810 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2811 "movl %esp,%ebp\n\t"
2812 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2813 "pushl %ebx\n\t"
2814 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2815 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2816 "pushl 24(%ebp)\n\t"
2817 "pushl 20(%ebp)\n\t"
2818 "pushl 16(%ebp)\n\t"
2819 "pushl 12(%ebp)\n\t"
2820 "pushl 8(%ebp)\n\t"
2821 "call " __ASM_NAME("call_exception_handler") "\n\t"
2822 "popl %ebx\n\t"
2823 __ASM_CFI(".cfi_same_value %ebx\n\t")
2824 "leave\n"
2825 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2826 __ASM_CFI(".cfi_same_value %ebp\n\t")
2827 "ret" )
2828 __ASM_GLOBAL_FUNC(call_exception_handler,
2829 "pushl %ebp\n\t"
2830 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2831 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2832 "movl %esp,%ebp\n\t"
2833 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2834 "subl $12,%esp\n\t"
2835 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2836 "pushl %edx\n\t" /* handler be handled by... */
2837 ".byte 0x64\n\t"
2838 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2839 ".byte 0x64\n\t"
2840 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2841 "pushl 20(%ebp)\n\t"
2842 "pushl 16(%ebp)\n\t"
2843 "pushl 12(%ebp)\n\t"
2844 "pushl 8(%ebp)\n\t"
2845 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2846 "call *%ecx\n\t" /* call handler. (*2) */
2847 ".byte 0x64\n\t"
2848 "movl (0), %esp\n\t" /* restore previous... (*3) */
2849 ".byte 0x64\n\t"
2850 "popl (0)\n\t" /* exception frame. */
2851 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2852 "popl %ebp\n\t"
2853 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2854 __ASM_CFI(".cfi_same_value %ebp\n\t")
2855 "ret $20" ) /* (*4) */
2857 #endif /* __i386__ */