dplayx: Code to forward player creation
[wine/gsoc_dplay.git] / dlls / ntdll / signal_i386.c
blobf70448a7754f7ab210ac458520567a10d7e812a8
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
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYSCALL_H
40 # include <syscall.h>
41 #else
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
44 # endif
45 #endif
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
49 #endif
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
53 #endif
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
56 #endif
58 #include "ntstatus.h"
59 #define WIN32_NO_STATUS
60 #include "windef.h"
61 #include "wine/library.h"
62 #include "ntdll_misc.h"
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 #ifdef HAVE_VALGRIND_MEMCHECK_H
67 #include <valgrind/memcheck.h>
68 #endif
70 #undef ERR /* Solaris needs to define this */
72 /* not defined for x86, so copy the x86_64 definition */
73 typedef struct DECLSPEC_ALIGN(16) _M128A
75 ULONGLONG Low;
76 LONGLONG High;
77 } M128A;
79 typedef struct
81 WORD ControlWord;
82 WORD StatusWord;
83 BYTE TagWord;
84 BYTE Reserved1;
85 WORD ErrorOpcode;
86 DWORD ErrorOffset;
87 WORD ErrorSelector;
88 WORD Reserved2;
89 DWORD DataOffset;
90 WORD DataSelector;
91 WORD Reserved3;
92 DWORD MxCsr;
93 DWORD MxCsr_Mask;
94 M128A FloatRegisters[8];
95 M128A XmmRegisters[16];
96 BYTE Reserved4[96];
97 } XMM_SAVE_AREA32;
99 /***********************************************************************
100 * signal context platform-specific definitions
103 #ifdef linux
105 typedef ucontext_t SIGCONTEXT;
107 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
108 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
109 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
110 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
111 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
112 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
113 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
114 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
116 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
117 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
118 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
119 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
120 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
121 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
123 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
124 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
125 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
126 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
128 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
129 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
131 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
132 #define VIF_FLAG 0x00080000
133 #define VIP_FLAG 0x00100000
135 int vm86_enter( void **vm86_ptr );
136 void vm86_return(void);
137 void vm86_return_end(void);
138 __ASM_GLOBAL_FUNC(vm86_enter,
139 "pushl %ebp\n\t"
140 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
141 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
142 "movl %esp,%ebp\n\t"
143 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
144 "pushl %ebx\n\t"
145 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
146 "movl $166,%eax\n\t" /*SYS_vm86*/
147 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
148 "movl (%ecx),%ecx\n\t"
149 "movl $1,%ebx\n\t" /*VM86_ENTER*/
150 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
151 "pushl %fs\n\t"
152 "pushl %gs\n\t"
153 "int $0x80\n"
154 ".globl " __ASM_NAME("vm86_return") "\n\t"
155 __ASM_FUNC("vm86_return") "\n"
156 __ASM_NAME("vm86_return") ":\n\t"
157 "popl %gs\n\t"
158 "popl %fs\n\t"
159 "popl %ecx\n\t"
160 "popl %ebx\n\t"
161 __ASM_CFI(".cfi_same_value %ebx\n\t")
162 "popl %ebp\n\t"
163 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
164 __ASM_CFI(".cfi_same_value %ebp\n\t")
165 "testl %eax,%eax\n\t"
166 "jl 0f\n\t"
167 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
168 "je " __ASM_NAME("vm86_enter") "\n\t"
169 "0:\n\t"
170 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
171 "movl $0,(%ecx)\n\t"
172 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
173 __ASM_FUNC("vm86_return_end") "\n"
174 __ASM_NAME("vm86_return_end") ":\n\t"
175 "ret" )
177 #ifdef HAVE_SYS_VM86_H
178 # define __HAVE_VM86
179 #endif
181 #endif /* linux */
183 #ifdef BSDI
185 #include <machine/frame.h>
186 typedef struct trapframe SIGCONTEXT;
188 #define EAX_sig(context) ((context)->tf_eax)
189 #define EBX_sig(context) ((context)->tf_ebx)
190 #define ECX_sig(context) ((context)->tf_ecx)
191 #define EDX_sig(context) ((context)->tf_edx)
192 #define ESI_sig(context) ((context)->tf_esi)
193 #define EDI_sig(context) ((context)->tf_edi)
194 #define EBP_sig(context) ((context)->tf_ebp)
196 #define CS_sig(context) ((context)->tf_cs)
197 #define DS_sig(context) ((context)->tf_ds)
198 #define ES_sig(context) ((context)->tf_es)
199 #define SS_sig(context) ((context)->tf_ss)
201 #define EFL_sig(context) ((context)->tf_eflags)
203 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
204 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
206 #define FPU_sig(context) NULL /* FIXME */
207 #define FPUX_sig(context) NULL /* FIXME */
209 #endif /* bsdi */
211 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
213 typedef struct sigcontext SIGCONTEXT;
215 #define EAX_sig(context) ((context)->sc_eax)
216 #define EBX_sig(context) ((context)->sc_ebx)
217 #define ECX_sig(context) ((context)->sc_ecx)
218 #define EDX_sig(context) ((context)->sc_edx)
219 #define ESI_sig(context) ((context)->sc_esi)
220 #define EDI_sig(context) ((context)->sc_edi)
221 #define EBP_sig(context) ((context)->sc_ebp)
223 #define CS_sig(context) ((context)->sc_cs)
224 #define DS_sig(context) ((context)->sc_ds)
225 #define ES_sig(context) ((context)->sc_es)
226 #define FS_sig(context) ((context)->sc_fs)
227 #define GS_sig(context) ((context)->sc_gs)
228 #define SS_sig(context) ((context)->sc_ss)
230 #define TRAP_sig(context) ((context)->sc_trapno)
231 #define ERROR_sig(context) ((context)->sc_err)
232 #define EFL_sig(context) ((context)->sc_eflags)
234 #define EIP_sig(context) ((context)->sc_eip)
235 #define ESP_sig(context) ((context)->sc_esp)
237 #define FPU_sig(context) NULL /* FIXME */
238 #define FPUX_sig(context) NULL /* FIXME */
240 #endif /* __FreeBSD__ */
242 #ifdef __OpenBSD__
244 typedef struct sigcontext SIGCONTEXT;
246 #define EAX_sig(context) ((context)->sc_eax)
247 #define EBX_sig(context) ((context)->sc_ebx)
248 #define ECX_sig(context) ((context)->sc_ecx)
249 #define EDX_sig(context) ((context)->sc_edx)
250 #define ESI_sig(context) ((context)->sc_esi)
251 #define EDI_sig(context) ((context)->sc_edi)
252 #define EBP_sig(context) ((context)->sc_ebp)
254 #define CS_sig(context) ((context)->sc_cs)
255 #define DS_sig(context) ((context)->sc_ds)
256 #define ES_sig(context) ((context)->sc_es)
257 #define FS_sig(context) ((context)->sc_fs)
258 #define GS_sig(context) ((context)->sc_gs)
259 #define SS_sig(context) ((context)->sc_ss)
261 #define TRAP_sig(context) ((context)->sc_trapno)
262 #define ERROR_sig(context) ((context)->sc_err)
263 #define EFL_sig(context) ((context)->sc_eflags)
265 #define EIP_sig(context) ((context)->sc_eip)
266 #define ESP_sig(context) ((context)->sc_esp)
268 #define FPU_sig(context) NULL /* FIXME */
269 #define FPUX_sig(context) NULL /* FIXME */
271 #define T_MCHK T_MACHK
272 #define T_XMMFLT T_XFTRAP
274 #endif /* __OpenBSD__ */
276 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
278 #ifdef _SCO_DS
279 #include <sys/regset.h>
280 #endif
281 #include <sys/ucontext.h>
282 typedef struct ucontext SIGCONTEXT;
284 #ifdef _SCO_DS
285 #define gregs regs
286 #endif
288 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
289 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
290 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
291 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
292 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
293 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
294 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
296 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
297 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
298 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
299 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
301 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
302 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
304 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
306 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
307 #ifdef UESP
308 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
309 #elif defined(R_ESP)
310 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
311 #else
312 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
313 #endif
314 #ifdef ERR
315 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
316 #endif
317 #ifdef TRAPNO
318 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
319 #endif
321 #define FPU_sig(context) NULL /* FIXME */
322 #define FPUX_sig(context) NULL /* FIXME */
324 #endif /* svr4 || SCO_DS */
326 #ifdef __APPLE__
327 # include <sys/ucontext.h>
329 typedef ucontext_t SIGCONTEXT;
331 /* work around silly renaming of struct members in OS X 10.5 */
332 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
333 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
334 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
335 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
336 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
337 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
338 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
339 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
340 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
341 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
342 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
343 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
344 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
345 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
346 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
347 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
348 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
349 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
350 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
351 #define FPU_sig(context) NULL
352 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
353 #else
354 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
355 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
356 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
357 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
358 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
359 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
360 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
361 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
362 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
363 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
364 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
365 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
366 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
367 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
368 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
369 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
370 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
371 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
372 #define FPU_sig(context) NULL
373 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
374 #endif
376 #endif /* __APPLE__ */
378 #if defined(__NetBSD__)
379 # include <sys/ucontext.h>
380 # include <sys/types.h>
381 # include <signal.h>
383 typedef ucontext_t SIGCONTEXT;
385 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
386 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
387 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
388 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
389 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
390 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
391 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
392 #define ESP_sig(context) _UC_MACHINE_SP(context)
394 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
395 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
396 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
397 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
398 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
399 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
401 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
402 #define EIP_sig(context) _UC_MACHINE_PC(context)
403 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
404 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
406 #define FPU_sig(context) NULL
407 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
409 #define T_MCHK T_MCA
410 #define T_XMMFLT T_XMM
412 #endif /* __NetBSD__ */
414 WINE_DEFAULT_DEBUG_CHANNEL(seh);
416 typedef int (*wine_signal_handler)(unsigned int sig);
418 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
419 static size_t signal_stack_mask;
420 static size_t signal_stack_size;
422 static wine_signal_handler handlers[256];
424 static int fpux_support; /* whether the CPU support extended fpu context */
426 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
428 enum i386_trap_code
430 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
431 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
432 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
433 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
434 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
435 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
436 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
437 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
438 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
439 TRAP_x86_DNA = T_DNA, /* Device not available exception */
440 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
441 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
442 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
443 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
444 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
445 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
446 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
447 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
448 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
449 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
450 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
451 #else
452 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
453 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
454 TRAP_x86_NMI = 2, /* NMI interrupt */
455 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
456 TRAP_x86_OFLOW = 4, /* Overflow exception */
457 TRAP_x86_BOUND = 5, /* Bound range exception */
458 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
459 TRAP_x86_DNA = 7, /* Device not available exception */
460 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
461 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
462 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
463 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
464 TRAP_x86_STKFLT = 12, /* Stack fault */
465 TRAP_x86_PROTFLT = 13, /* General protection fault */
466 TRAP_x86_PAGEFLT = 14, /* Page fault */
467 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
468 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
469 TRAP_x86_MCHK = 18, /* Machine check exception */
470 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
471 otherwise Cache flush exception (via SIGSEV) */
472 #endif
475 /* Exception record for handling exceptions happening inside exception handlers */
476 typedef struct
478 EXCEPTION_REGISTRATION_RECORD frame;
479 EXCEPTION_REGISTRATION_RECORD *prevFrame;
480 } EXC_NESTED_FRAME;
482 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
483 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
484 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler );
486 /***********************************************************************
487 * dispatch_signal
489 static inline int dispatch_signal(unsigned int sig)
491 if (handlers[sig] == NULL) return 0;
492 return handlers[sig](sig);
496 /***********************************************************************
497 * get_trap_code
499 * Get the trap code for a signal.
501 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
503 #ifdef TRAP_sig
504 return TRAP_sig(sigcontext);
505 #else
506 return TRAP_x86_UNKNOWN; /* unknown trap code */
507 #endif
510 /***********************************************************************
511 * get_error_code
513 * Get the error code for a signal.
515 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
517 #ifdef ERROR_sig
518 return ERROR_sig(sigcontext);
519 #else
520 return 0;
521 #endif
524 /***********************************************************************
525 * get_signal_stack
527 * Get the base of the signal stack for the current thread.
529 static inline void *get_signal_stack(void)
531 return (char *)NtCurrentTeb() + 4096;
535 /***********************************************************************
536 * get_current_teb
538 * Get the current teb based on the stack pointer.
540 static inline TEB *get_current_teb(void)
542 unsigned long esp;
543 __asm__("movl %%esp,%0" : "=g" (esp) );
544 return (TEB *)(esp & ~signal_stack_mask);
548 /*******************************************************************
549 * raise_handler
551 * Handler for exceptions happening inside a handler.
553 static DWORD raise_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
554 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
556 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
557 return ExceptionContinueSearch;
558 /* We shouldn't get here so we store faulty frame in dispatcher */
559 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
560 return ExceptionNestedException;
564 /*******************************************************************
565 * unwind_handler
567 * Handler for exceptions happening inside an unwind handler.
569 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
570 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
572 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
573 return ExceptionContinueSearch;
574 /* We shouldn't get here so we store faulty frame in dispatcher */
575 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
576 return ExceptionCollidedUnwind;
580 /**********************************************************************
581 * call_stack_handlers
583 * Call the stack handlers chain.
585 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
587 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
588 DWORD res;
590 frame = NtCurrentTeb()->Tib.ExceptionList;
591 nested_frame = NULL;
592 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
594 /* Check frame address */
595 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
596 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
597 (ULONG_PTR)frame & 3)
599 rec->ExceptionFlags |= EH_STACK_INVALID;
600 break;
603 /* Call handler */
604 TRACE( "calling handler at %p code=%x flags=%x\n",
605 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
606 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, raise_handler );
607 TRACE( "handler at %p returned %x\n", frame->Handler, res );
609 if (frame == nested_frame)
611 /* no longer nested */
612 nested_frame = NULL;
613 rec->ExceptionFlags &= ~EH_NESTED_CALL;
616 switch(res)
618 case ExceptionContinueExecution:
619 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
620 return STATUS_NONCONTINUABLE_EXCEPTION;
621 case ExceptionContinueSearch:
622 break;
623 case ExceptionNestedException:
624 if (nested_frame < dispatch) nested_frame = dispatch;
625 rec->ExceptionFlags |= EH_NESTED_CALL;
626 break;
627 default:
628 return STATUS_INVALID_DISPOSITION;
630 frame = frame->Prev;
632 return STATUS_UNHANDLED_EXCEPTION;
636 /*******************************************************************
637 * raise_exception
639 * Implementation of NtRaiseException.
641 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
643 NTSTATUS status;
645 if (first_chance)
647 DWORD c;
649 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
650 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
651 context->Eip, GetCurrentThreadId() );
652 for (c = 0; c < rec->NumberParameters; c++)
653 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
654 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
656 if (rec->ExceptionInformation[1] >> 16)
657 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
658 rec->ExceptionAddress,
659 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
660 else
661 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
662 rec->ExceptionAddress,
663 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
665 else
667 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
668 context->Eax, context->Ebx, context->Ecx,
669 context->Edx, context->Esi, context->Edi );
670 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
671 context->Ebp, context->Esp, context->SegCs, context->SegDs,
672 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
674 status = send_debug_event( rec, TRUE, context );
675 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
676 return STATUS_SUCCESS;
678 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
679 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Eip--;
681 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
682 return STATUS_SUCCESS;
684 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
685 return status;
688 /* last chance exception */
690 status = send_debug_event( rec, FALSE, context );
691 if (status != DBG_CONTINUE)
693 if (rec->ExceptionFlags & EH_STACK_INVALID)
694 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
695 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
696 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
697 else
698 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
699 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
700 NtTerminateProcess( NtCurrentProcess(), 1 );
702 return STATUS_SUCCESS;
706 #ifdef __HAVE_VM86
707 /***********************************************************************
708 * save_vm86_context
710 * Set the register values from a vm86 structure.
712 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
714 context->ContextFlags = CONTEXT_FULL;
715 context->Eax = vm86->regs.eax;
716 context->Ebx = vm86->regs.ebx;
717 context->Ecx = vm86->regs.ecx;
718 context->Edx = vm86->regs.edx;
719 context->Esi = vm86->regs.esi;
720 context->Edi = vm86->regs.edi;
721 context->Esp = vm86->regs.esp;
722 context->Ebp = vm86->regs.ebp;
723 context->Eip = vm86->regs.eip;
724 context->SegCs = vm86->regs.cs;
725 context->SegDs = vm86->regs.ds;
726 context->SegEs = vm86->regs.es;
727 context->SegFs = vm86->regs.fs;
728 context->SegGs = vm86->regs.gs;
729 context->SegSs = vm86->regs.ss;
730 context->EFlags = vm86->regs.eflags;
734 /***********************************************************************
735 * restore_vm86_context
737 * Build a vm86 structure from the register values.
739 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
741 vm86->regs.eax = context->Eax;
742 vm86->regs.ebx = context->Ebx;
743 vm86->regs.ecx = context->Ecx;
744 vm86->regs.edx = context->Edx;
745 vm86->regs.esi = context->Esi;
746 vm86->regs.edi = context->Edi;
747 vm86->regs.esp = context->Esp;
748 vm86->regs.ebp = context->Ebp;
749 vm86->regs.eip = context->Eip;
750 vm86->regs.cs = context->SegCs;
751 vm86->regs.ds = context->SegDs;
752 vm86->regs.es = context->SegEs;
753 vm86->regs.fs = context->SegFs;
754 vm86->regs.gs = context->SegGs;
755 vm86->regs.ss = context->SegSs;
756 vm86->regs.eflags = context->EFlags;
760 /**********************************************************************
761 * merge_vm86_pending_flags
763 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
764 * raises exception if there are pending events and VIF flag
765 * has been turned on.
767 * Called from __wine_enter_vm86 because vm86_enter
768 * doesn't check for pending events.
770 * Called from raise_vm86_sti_exception to check for
771 * pending events in a signal safe way.
773 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
775 BOOL check_pending = TRUE;
776 struct vm86plus_struct *vm86 =
777 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
780 * In order to prevent a race when SIGUSR2 occurs while
781 * we are returning from exception handler, pending events
782 * will be rechecked after each raised exception.
784 while (check_pending && get_vm86_teb_info()->vm86_pending)
786 check_pending = FALSE;
787 ntdll_get_thread_data()->vm86_ptr = NULL;
790 * If VIF is set, throw exception.
791 * Note that SIGUSR2 may turn VIF flag off so
792 * VIF check must occur only when TEB.vm86_ptr is NULL.
794 if (vm86->regs.eflags & VIF_FLAG)
796 CONTEXT vcontext;
797 save_vm86_context( &vcontext, vm86 );
799 rec->ExceptionCode = EXCEPTION_VM86_STI;
800 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
801 rec->ExceptionRecord = NULL;
802 rec->NumberParameters = 0;
803 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
805 vcontext.EFlags &= ~VIP_FLAG;
806 get_vm86_teb_info()->vm86_pending = 0;
807 raise_exception( rec, &vcontext, TRUE );
809 restore_vm86_context( &vcontext, vm86 );
810 check_pending = TRUE;
813 ntdll_get_thread_data()->vm86_ptr = vm86;
817 * Merge VIP flags in a signal safe way. This requires
818 * that the following operation compiles into atomic
819 * instruction.
821 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
823 #endif /* __HAVE_VM86 */
826 #ifdef __sun
828 /* We have to workaround two Solaris breakages:
829 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
830 * code crash badly.
831 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
832 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
835 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
836 __ASM_GLOBAL_FUNC( sigaction_syscall,
837 "movl $0x62,%eax\n\t"
838 "int $0x91\n\t"
839 "ret" )
841 /* assume the same libc handler is used for all signals */
842 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
844 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
846 struct ntdll_thread_data *thread_data;
848 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
850 thread_data = (struct ntdll_thread_data *)get_current_teb()->SystemReserved2;
851 wine_set_fs( thread_data->fs );
852 wine_set_gs( thread_data->gs );
854 libc_sigacthandler( signal, siginfo, sigcontext );
857 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
859 struct sigaction real_act;
861 if (sigaction( sig, new, old ) == -1) return -1;
863 /* retrieve the real handler and flags with a direct syscall */
864 sigaction_syscall( sig, NULL, &real_act );
865 libc_sigacthandler = real_act.sa_sigaction;
866 real_act.sa_sigaction = wine_sigacthandler;
867 sigaction_syscall( sig, &real_act, NULL );
868 return 0;
870 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
872 #endif
874 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
877 /***********************************************************************
878 * init_handler
880 * Handler initialization when the full context is not needed.
881 * Return the stack pointer to use for pushing the exception data.
883 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
885 TEB *teb = get_current_teb();
887 /* get %fs and %gs at time of the fault */
888 #ifdef FS_sig
889 *fs = LOWORD(FS_sig(sigcontext));
890 #else
891 *fs = wine_get_fs();
892 #endif
893 #ifdef GS_sig
894 *gs = LOWORD(GS_sig(sigcontext));
895 #else
896 *gs = wine_get_gs();
897 #endif
899 #ifndef __sun /* see above for Solaris handling */
901 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
902 wine_set_fs( thread_data->fs );
903 wine_set_gs( thread_data->gs );
905 #endif
907 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
908 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
911 * Win16 or DOS protected mode. Note that during switch
912 * from 16-bit mode to linear mode, CS may be set to system
913 * segment before FS is restored. Fortunately, in this case
914 * SS is still non-system segment. This is why both CS and SS
915 * are checked.
917 return teb->WOW32Reserved;
919 return (void *)(ESP_sig(sigcontext) & ~3);
923 /***********************************************************************
924 * save_fpu
926 * Save the thread FPU context.
928 static inline void save_fpu( CONTEXT *context )
930 #ifdef __GNUC__
931 context->ContextFlags |= CONTEXT_FLOATING_POINT;
932 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
933 #endif
937 /***********************************************************************
938 * save_fpux
940 * Save the thread FPU extended context.
942 static inline void save_fpux( CONTEXT *context )
944 #ifdef __GNUC__
945 /* we have to enforce alignment by hand */
946 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
947 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
949 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
950 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
951 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
952 #endif
956 /***********************************************************************
957 * restore_fpu
959 * Restore the FPU context to a sigcontext.
961 static inline void restore_fpu( const CONTEXT *context )
963 FLOATING_SAVE_AREA float_status = context->FloatSave;
964 /* reset the current interrupt status */
965 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
966 #ifdef __GNUC__
967 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
968 #endif /* __GNUC__ */
972 /***********************************************************************
973 * restore_fpux
975 * Restore the FPU extended context to a sigcontext.
977 static inline void restore_fpux( const CONTEXT *context )
979 #ifdef __GNUC__
980 /* we have to enforce alignment by hand */
981 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
982 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
984 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
985 /* reset the current interrupt status */
986 state->StatusWord &= state->ControlWord | 0xff80;
987 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
988 #endif
992 /***********************************************************************
993 * fpux_to_fpu
995 * Build a standard FPU context from an extended one.
997 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
999 unsigned int i, tag, stack_top;
1001 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
1002 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
1003 fpu->ErrorOffset = fpux->ErrorOffset;
1004 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
1005 fpu->DataOffset = fpux->DataOffset;
1006 fpu->DataSelector = fpux->DataSelector;
1007 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
1009 stack_top = (fpux->StatusWord >> 11) & 7;
1010 fpu->TagWord = 0xffff0000;
1011 for (i = 0; i < 8; i++)
1013 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
1014 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
1015 else
1017 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
1018 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
1020 tag = 2; /* special */
1022 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
1024 if (reg->Low) tag = 2; /* special */
1025 else tag = 1; /* zero */
1027 else
1029 if (reg->Low >> 63) tag = 0; /* valid */
1030 else tag = 2; /* special */
1033 fpu->TagWord |= tag << (2 * i);
1038 /***********************************************************************
1039 * save_context
1041 * Build a context structure from the signal info.
1043 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
1045 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1046 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1047 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1049 memset(context, 0, sizeof(*context));
1050 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
1051 context->Eax = EAX_sig(sigcontext);
1052 context->Ebx = EBX_sig(sigcontext);
1053 context->Ecx = ECX_sig(sigcontext);
1054 context->Edx = EDX_sig(sigcontext);
1055 context->Esi = ESI_sig(sigcontext);
1056 context->Edi = EDI_sig(sigcontext);
1057 context->Ebp = EBP_sig(sigcontext);
1058 context->EFlags = EFL_sig(sigcontext);
1059 context->Eip = EIP_sig(sigcontext);
1060 context->Esp = ESP_sig(sigcontext);
1061 context->SegCs = LOWORD(CS_sig(sigcontext));
1062 context->SegDs = LOWORD(DS_sig(sigcontext));
1063 context->SegEs = LOWORD(ES_sig(sigcontext));
1064 context->SegFs = fs;
1065 context->SegGs = gs;
1066 context->SegSs = LOWORD(SS_sig(sigcontext));
1067 context->Dr0 = regs->dr0;
1068 context->Dr1 = regs->dr1;
1069 context->Dr2 = regs->dr2;
1070 context->Dr3 = regs->dr3;
1071 context->Dr6 = regs->dr6;
1072 context->Dr7 = regs->dr7;
1074 if (fpu)
1076 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1077 context->FloatSave = *fpu;
1079 if (fpux)
1081 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
1082 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
1083 fpux_support = 1;
1084 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
1086 if (!fpu && !fpux) save_fpu( context );
1090 /***********************************************************************
1091 * restore_context
1093 * Restore the signal info from the context.
1095 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
1097 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1098 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1099 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1101 regs->dr0 = context->Dr0;
1102 regs->dr1 = context->Dr1;
1103 regs->dr2 = context->Dr2;
1104 regs->dr3 = context->Dr3;
1105 regs->dr6 = context->Dr6;
1106 regs->dr7 = context->Dr7;
1107 EAX_sig(sigcontext) = context->Eax;
1108 EBX_sig(sigcontext) = context->Ebx;
1109 ECX_sig(sigcontext) = context->Ecx;
1110 EDX_sig(sigcontext) = context->Edx;
1111 ESI_sig(sigcontext) = context->Esi;
1112 EDI_sig(sigcontext) = context->Edi;
1113 EBP_sig(sigcontext) = context->Ebp;
1114 EFL_sig(sigcontext) = context->EFlags;
1115 EIP_sig(sigcontext) = context->Eip;
1116 ESP_sig(sigcontext) = context->Esp;
1117 CS_sig(sigcontext) = context->SegCs;
1118 DS_sig(sigcontext) = context->SegDs;
1119 ES_sig(sigcontext) = context->SegEs;
1120 SS_sig(sigcontext) = context->SegSs;
1121 #ifdef GS_sig
1122 GS_sig(sigcontext) = context->SegGs;
1123 #else
1124 wine_set_gs( context->SegGs );
1125 #endif
1126 #ifdef FS_sig
1127 FS_sig(sigcontext) = context->SegFs;
1128 #else
1129 wine_set_fs( context->SegFs );
1130 #endif
1132 if (fpu) *fpu = context->FloatSave;
1133 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
1134 if (!fpu && !fpux) restore_fpu( context );
1138 /***********************************************************************
1139 * RtlCaptureContext (NTDLL.@)
1141 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
1143 *context = *regs;
1144 if (fpux_support) save_fpux( context );
1145 else save_fpu( context );
1147 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
1150 /***********************************************************************
1151 * set_cpu_context
1153 * Set the new CPU context. Used by NtSetContextThread.
1155 void set_cpu_context( const CONTEXT *context )
1157 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1159 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
1160 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
1162 if (flags & CONTEXT_DEBUG_REGISTERS)
1164 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1165 regs->dr0 = context->Dr0;
1166 regs->dr1 = context->Dr1;
1167 regs->dr2 = context->Dr2;
1168 regs->dr3 = context->Dr3;
1169 regs->dr6 = context->Dr6;
1170 regs->dr7 = context->Dr7;
1172 if (flags & CONTEXT_FULL)
1174 if (!(flags & CONTEXT_CONTROL))
1175 FIXME( "setting partial context (%x) not supported\n", flags );
1176 else if (flags & CONTEXT_SEGMENTS)
1177 __wine_call_from_32_restore_regs( context );
1178 else
1180 CONTEXT newcontext = *context;
1181 newcontext.SegDs = wine_get_ds();
1182 newcontext.SegEs = wine_get_es();
1183 newcontext.SegFs = wine_get_fs();
1184 newcontext.SegGs = wine_get_gs();
1185 __wine_call_from_32_restore_regs( &newcontext );
1191 /***********************************************************************
1192 * copy_context
1194 * Copy a register context according to the flags.
1196 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1198 flags &= ~CONTEXT_i386; /* get rid of CPU id */
1199 if (flags & CONTEXT_INTEGER)
1201 to->Eax = from->Eax;
1202 to->Ebx = from->Ebx;
1203 to->Ecx = from->Ecx;
1204 to->Edx = from->Edx;
1205 to->Esi = from->Esi;
1206 to->Edi = from->Edi;
1208 if (flags & CONTEXT_CONTROL)
1210 to->Ebp = from->Ebp;
1211 to->Esp = from->Esp;
1212 to->Eip = from->Eip;
1213 to->SegCs = from->SegCs;
1214 to->SegSs = from->SegSs;
1215 to->EFlags = from->EFlags;
1217 if (flags & CONTEXT_SEGMENTS)
1219 to->SegDs = from->SegDs;
1220 to->SegEs = from->SegEs;
1221 to->SegFs = from->SegFs;
1222 to->SegGs = from->SegGs;
1224 if (flags & CONTEXT_DEBUG_REGISTERS)
1226 to->Dr0 = from->Dr0;
1227 to->Dr1 = from->Dr1;
1228 to->Dr2 = from->Dr2;
1229 to->Dr3 = from->Dr3;
1230 to->Dr6 = from->Dr6;
1231 to->Dr7 = from->Dr7;
1233 if (flags & CONTEXT_FLOATING_POINT)
1235 to->FloatSave = from->FloatSave;
1237 if (flags & CONTEXT_EXTENDED_REGISTERS)
1239 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
1244 /***********************************************************************
1245 * context_to_server
1247 * Convert a register context to the server format.
1249 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1251 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1253 memset( to, 0, sizeof(*to) );
1254 to->cpu = CPU_x86;
1256 if (flags & CONTEXT_CONTROL)
1258 to->flags |= SERVER_CTX_CONTROL;
1259 to->ctl.i386_regs.ebp = from->Ebp;
1260 to->ctl.i386_regs.esp = from->Esp;
1261 to->ctl.i386_regs.eip = from->Eip;
1262 to->ctl.i386_regs.cs = from->SegCs;
1263 to->ctl.i386_regs.ss = from->SegSs;
1264 to->ctl.i386_regs.eflags = from->EFlags;
1266 if (flags & CONTEXT_INTEGER)
1268 to->flags |= SERVER_CTX_INTEGER;
1269 to->integer.i386_regs.eax = from->Eax;
1270 to->integer.i386_regs.ebx = from->Ebx;
1271 to->integer.i386_regs.ecx = from->Ecx;
1272 to->integer.i386_regs.edx = from->Edx;
1273 to->integer.i386_regs.esi = from->Esi;
1274 to->integer.i386_regs.edi = from->Edi;
1276 if (flags & CONTEXT_SEGMENTS)
1278 to->flags |= SERVER_CTX_SEGMENTS;
1279 to->seg.i386_regs.ds = from->SegDs;
1280 to->seg.i386_regs.es = from->SegEs;
1281 to->seg.i386_regs.fs = from->SegFs;
1282 to->seg.i386_regs.gs = from->SegGs;
1284 if (flags & CONTEXT_FLOATING_POINT)
1286 to->flags |= SERVER_CTX_FLOATING_POINT;
1287 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1288 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1289 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1290 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1291 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1292 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1293 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1294 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1295 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1297 if (flags & CONTEXT_DEBUG_REGISTERS)
1299 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1300 to->debug.i386_regs.dr0 = from->Dr0;
1301 to->debug.i386_regs.dr1 = from->Dr1;
1302 to->debug.i386_regs.dr2 = from->Dr2;
1303 to->debug.i386_regs.dr3 = from->Dr3;
1304 to->debug.i386_regs.dr6 = from->Dr6;
1305 to->debug.i386_regs.dr7 = from->Dr7;
1307 if (flags & CONTEXT_EXTENDED_REGISTERS)
1309 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1310 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1312 return STATUS_SUCCESS;
1316 /***********************************************************************
1317 * context_from_server
1319 * Convert a register context from the server format.
1321 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1323 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1325 to->ContextFlags = CONTEXT_i386;
1326 if (from->flags & SERVER_CTX_CONTROL)
1328 to->ContextFlags |= CONTEXT_CONTROL;
1329 to->Ebp = from->ctl.i386_regs.ebp;
1330 to->Esp = from->ctl.i386_regs.esp;
1331 to->Eip = from->ctl.i386_regs.eip;
1332 to->SegCs = from->ctl.i386_regs.cs;
1333 to->SegSs = from->ctl.i386_regs.ss;
1334 to->EFlags = from->ctl.i386_regs.eflags;
1336 if (from->flags & SERVER_CTX_INTEGER)
1338 to->ContextFlags |= CONTEXT_INTEGER;
1339 to->Eax = from->integer.i386_regs.eax;
1340 to->Ebx = from->integer.i386_regs.ebx;
1341 to->Ecx = from->integer.i386_regs.ecx;
1342 to->Edx = from->integer.i386_regs.edx;
1343 to->Esi = from->integer.i386_regs.esi;
1344 to->Edi = from->integer.i386_regs.edi;
1346 if (from->flags & SERVER_CTX_SEGMENTS)
1348 to->ContextFlags |= CONTEXT_SEGMENTS;
1349 to->SegDs = from->seg.i386_regs.ds;
1350 to->SegEs = from->seg.i386_regs.es;
1351 to->SegFs = from->seg.i386_regs.fs;
1352 to->SegGs = from->seg.i386_regs.gs;
1354 if (from->flags & SERVER_CTX_FLOATING_POINT)
1356 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1357 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1358 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1359 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1360 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1361 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1362 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1363 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1364 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1365 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1367 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1369 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1370 to->Dr0 = from->debug.i386_regs.dr0;
1371 to->Dr1 = from->debug.i386_regs.dr1;
1372 to->Dr2 = from->debug.i386_regs.dr2;
1373 to->Dr3 = from->debug.i386_regs.dr3;
1374 to->Dr6 = from->debug.i386_regs.dr6;
1375 to->Dr7 = from->debug.i386_regs.dr7;
1377 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1379 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1380 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1382 return STATUS_SUCCESS;
1386 /***********************************************************************
1387 * is_privileged_instr
1389 * Check if the fault location is a privileged instruction.
1390 * Based on the instruction emulation code in dlls/kernel/instr.c.
1392 static inline DWORD is_privileged_instr( CONTEXT86 *context )
1394 const BYTE *instr;
1395 unsigned int prefix_count = 0;
1397 if (!wine_ldt_is_system( context->SegCs )) return 0;
1398 instr = (BYTE *)context->Eip;
1400 for (;;) switch(*instr)
1402 /* instruction prefixes */
1403 case 0x2e: /* %cs: */
1404 case 0x36: /* %ss: */
1405 case 0x3e: /* %ds: */
1406 case 0x26: /* %es: */
1407 case 0x64: /* %fs: */
1408 case 0x65: /* %gs: */
1409 case 0x66: /* opcode size */
1410 case 0x67: /* addr size */
1411 case 0xf0: /* lock */
1412 case 0xf2: /* repne */
1413 case 0xf3: /* repe */
1414 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1415 instr++;
1416 continue;
1418 case 0x0f: /* extended instruction */
1419 switch(instr[1])
1421 case 0x20: /* mov crX, reg */
1422 case 0x21: /* mov drX, reg */
1423 case 0x22: /* mov reg, crX */
1424 case 0x23: /* mov reg drX */
1425 return EXCEPTION_PRIV_INSTRUCTION;
1427 return 0;
1428 case 0x6c: /* insb (%dx) */
1429 case 0x6d: /* insl (%dx) */
1430 case 0x6e: /* outsb (%dx) */
1431 case 0x6f: /* outsl (%dx) */
1432 case 0xcd: /* int $xx */
1433 case 0xe4: /* inb al,XX */
1434 case 0xe5: /* in (e)ax,XX */
1435 case 0xe6: /* outb XX,al */
1436 case 0xe7: /* out XX,(e)ax */
1437 case 0xec: /* inb (%dx),%al */
1438 case 0xed: /* inl (%dx),%eax */
1439 case 0xee: /* outb %al,(%dx) */
1440 case 0xef: /* outl %eax,(%dx) */
1441 case 0xf4: /* hlt */
1442 case 0xfa: /* cli */
1443 case 0xfb: /* sti */
1444 return EXCEPTION_PRIV_INSTRUCTION;
1445 default:
1446 return 0;
1451 #include "pshpack1.h"
1452 struct atl_thunk
1454 DWORD movl; /* movl this,4(%esp) */
1455 DWORD this;
1456 BYTE jmp; /* jmp func */
1457 int func;
1459 #include "poppack.h"
1461 /**********************************************************************
1462 * check_atl_thunk
1464 * Check if code destination is an ATL thunk, and emulate it if so.
1466 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1468 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1469 BOOL ret = FALSE;
1471 __TRY
1473 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1475 *((DWORD *)context->Esp + 1) = thunk->this;
1476 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1477 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1478 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1479 ret = TRUE;
1482 __EXCEPT_PAGE_FAULT
1484 return FALSE;
1486 __ENDTRY
1487 return ret;
1491 /***********************************************************************
1492 * setup_exception_record
1494 * Setup the exception record and context on the thread stack.
1496 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1497 WORD fs, WORD gs, raise_func func )
1499 struct stack_layout
1501 void *ret_addr; /* return address from raise_func */
1502 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1503 CONTEXT *context_ptr; /* second arg for raise_func */
1504 CONTEXT context;
1505 EXCEPTION_RECORD rec;
1506 DWORD ebp;
1507 DWORD eip;
1508 } *stack = stack_ptr;
1509 DWORD exception_code = 0;
1511 /* stack sanity checks */
1513 if ((char *)stack >= (char *)get_signal_stack() &&
1514 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1516 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1517 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1518 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1519 NtCurrentTeb()->Tib.StackBase );
1520 abort_thread(1);
1523 if (stack - 1 > stack || /* check for overflow in subtraction */
1524 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1525 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1527 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1528 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1529 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1530 NtCurrentTeb()->Tib.StackBase );
1532 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1534 /* stack overflow on last page, unrecoverable */
1535 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1536 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1537 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1538 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1539 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1540 abort_thread(1);
1542 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1544 /* stack access below stack limit, may be recoverable */
1545 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1546 else
1548 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1549 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1550 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1551 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1552 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1553 abort_thread(1);
1557 stack--; /* push the stack_layout structure */
1558 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1559 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1560 #elif defined(VALGRIND_MAKE_WRITABLE)
1561 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1562 #endif
1563 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1564 stack->rec_ptr = &stack->rec;
1565 stack->context_ptr = &stack->context;
1567 stack->rec.ExceptionRecord = NULL;
1568 stack->rec.ExceptionCode = exception_code;
1569 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1570 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1571 stack->rec.NumberParameters = 0;
1573 save_context( &stack->context, sigcontext, fs, gs );
1575 /* now modify the sigcontext to return to the raise function */
1576 ESP_sig(sigcontext) = (DWORD)stack;
1577 EIP_sig(sigcontext) = (DWORD)func;
1578 /* clear single-step, direction, and align check flag */
1579 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1580 CS_sig(sigcontext) = wine_get_cs();
1581 DS_sig(sigcontext) = wine_get_ds();
1582 ES_sig(sigcontext) = wine_get_es();
1583 FS_sig(sigcontext) = wine_get_fs();
1584 GS_sig(sigcontext) = wine_get_gs();
1585 SS_sig(sigcontext) = wine_get_ss();
1587 return stack->rec_ptr;
1591 /***********************************************************************
1592 * setup_exception
1594 * Setup a proper stack frame for the raise function, and modify the
1595 * sigcontext so that the return from the signal handler will call
1596 * the raise function.
1598 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1600 WORD fs, gs;
1601 void *stack = init_handler( sigcontext, &fs, &gs );
1603 return setup_exception_record( sigcontext, stack, fs, gs, func );
1607 /***********************************************************************
1608 * get_exception_context
1610 * Get a pointer to the context built by setup_exception.
1612 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1614 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1618 /**********************************************************************
1619 * get_fpu_code
1621 * Get the FPU exception code from the FPU status.
1623 static inline DWORD get_fpu_code( const CONTEXT *context )
1625 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1627 if (status & 0x01) /* IE */
1629 if (status & 0x40) /* SF */
1630 return EXCEPTION_FLT_STACK_CHECK;
1631 else
1632 return EXCEPTION_FLT_INVALID_OPERATION;
1634 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1635 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1636 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1637 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1638 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1639 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1643 /**********************************************************************
1644 * raise_segv_exception
1646 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1648 NTSTATUS status;
1650 switch(rec->ExceptionCode)
1652 case EXCEPTION_ACCESS_VIOLATION:
1653 if (rec->NumberParameters == 2)
1655 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1656 goto done;
1657 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1658 rec->ExceptionInformation[0] )))
1659 goto done;
1661 break;
1662 case EXCEPTION_DATATYPE_MISALIGNMENT:
1663 /* FIXME: pass through exception handler first? */
1664 if (context->EFlags & 0x00040000)
1666 /* Disable AC flag, return */
1667 context->EFlags &= ~0x00040000;
1668 goto done;
1670 break;
1672 status = NtRaiseException( rec, context, TRUE );
1673 if (status) raise_status( status, rec );
1674 done:
1675 NtSetContextThread( GetCurrentThread(), context );
1679 /**********************************************************************
1680 * raise_trap_exception
1682 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1684 NTSTATUS status;
1686 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1688 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1690 /* when single stepping can't tell whether this is a hw bp or a
1691 * single step interrupt. try to avoid as much overhead as possible
1692 * and only do a server call if there is any hw bp enabled. */
1694 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1696 /* (possible) hardware breakpoint, fetch the debug registers */
1697 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1698 NtGetContextThread(GetCurrentThread(), context);
1699 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1702 context->EFlags &= ~0x100; /* clear single-step flag */
1705 status = NtRaiseException( rec, context, TRUE );
1706 if (status) raise_status( status, rec );
1710 /**********************************************************************
1711 * raise_generic_exception
1713 * Generic raise function for exceptions that don't need special treatment.
1715 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1717 NTSTATUS status;
1719 status = NtRaiseException( rec, context, TRUE );
1720 if (status) raise_status( status, rec );
1724 #ifdef __HAVE_VM86
1725 /**********************************************************************
1726 * raise_vm86_sti_exception
1728 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1730 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1731 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1733 if (ntdll_get_thread_data()->vm86_ptr)
1735 if (((char*)context->Eip >= (char*)vm86_return) &&
1736 ((char*)context->Eip <= (char*)vm86_return_end) &&
1737 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1738 /* exiting from VM86, can't throw */
1739 goto done;
1741 merge_vm86_pending_flags( rec );
1743 else if (get_vm86_teb_info()->dpmi_vif &&
1744 !wine_ldt_is_system(context->SegCs) &&
1745 !wine_ldt_is_system(context->SegSs))
1747 /* Executing DPMI code and virtual interrupts are enabled. */
1748 get_vm86_teb_info()->vm86_pending = 0;
1749 NtRaiseException( rec, context, TRUE );
1751 done:
1752 NtSetContextThread( GetCurrentThread(), context );
1756 /**********************************************************************
1757 * usr2_handler
1759 * Handler for SIGUSR2.
1760 * We use it to signal that the running __wine_enter_vm86() should
1761 * immediately set VIP_FLAG, causing pending events to be handled
1762 * as early as possible.
1764 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1766 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1767 rec->ExceptionCode = EXCEPTION_VM86_STI;
1769 #endif /* __HAVE_VM86 */
1772 /**********************************************************************
1773 * segv_handler
1775 * Handler for SIGSEGV and related errors.
1777 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1779 WORD fs, gs;
1780 EXCEPTION_RECORD *rec;
1781 SIGCONTEXT *context = sigcontext;
1782 void *stack = init_handler( sigcontext, &fs, &gs );
1784 /* check for page fault inside the thread stack */
1785 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1786 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1787 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1788 virtual_handle_stack_fault( siginfo->si_addr ))
1790 /* check if this was the last guard page */
1791 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1793 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1794 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1796 return;
1799 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1800 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1802 switch(get_trap_code(context))
1804 case TRAP_x86_OFLOW: /* Overflow exception */
1805 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1806 break;
1807 case TRAP_x86_BOUND: /* Bound range exception */
1808 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1809 break;
1810 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1811 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1812 break;
1813 case TRAP_x86_STKFLT: /* Stack fault */
1814 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1815 break;
1816 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1817 case TRAP_x86_PROTFLT: /* General protection fault */
1818 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1820 WORD err = get_error_code(context);
1821 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1822 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1823 rec->NumberParameters = 2;
1824 rec->ExceptionInformation[0] = 0;
1825 /* if error contains a LDT selector, use that as fault address */
1826 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1827 rec->ExceptionInformation[1] = err & ~7;
1828 else
1829 rec->ExceptionInformation[1] = 0xffffffff;
1831 break;
1832 case TRAP_x86_PAGEFLT: /* Page fault */
1833 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1834 rec->NumberParameters = 2;
1835 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1836 /* Send code 8 (EXCEPTION_EXECUTE_FAULT) only if data execution
1837 prevention is enabled */
1838 if (rec->ExceptionInformation[0] & 8)
1840 ULONG flags;
1841 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags,
1842 &flags, sizeof(flags), NULL );
1843 if (!(flags & MEM_EXECUTE_OPTION_DISABLE)) rec->ExceptionInformation[0] &= 1;
1845 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1846 break;
1847 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1848 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1849 break;
1850 default:
1851 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1852 /* fall through */
1853 case TRAP_x86_NMI: /* NMI interrupt */
1854 case TRAP_x86_DNA: /* Device not available exception */
1855 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1856 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1857 case TRAP_x86_MCHK: /* Machine check exception */
1858 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1859 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1860 break;
1865 /**********************************************************************
1866 * trap_handler
1868 * Handler for SIGTRAP.
1870 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1872 SIGCONTEXT *context = sigcontext;
1873 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1875 switch(get_trap_code(context))
1877 case TRAP_x86_TRCTRAP: /* Single-step exception */
1878 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1879 break;
1880 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1881 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1882 /* fall through */
1883 default:
1884 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1885 break;
1890 /**********************************************************************
1891 * fpe_handler
1893 * Handler for SIGFPE.
1895 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1897 CONTEXT *win_context;
1898 SIGCONTEXT *context = sigcontext;
1899 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
1901 win_context = get_exception_context( rec );
1903 switch(get_trap_code(context))
1905 case TRAP_x86_DIVIDE: /* Division by zero exception */
1906 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1907 break;
1908 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1909 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1910 break;
1911 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1912 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1913 rec->ExceptionCode = get_fpu_code( win_context );
1914 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
1915 break;
1916 case TRAP_x86_CACHEFLT: /* SIMD exception */
1917 /* TODO:
1918 * Behaviour only tested for divide-by-zero exceptions
1919 * Check for other SIMD exceptions as well */
1920 if(siginfo->si_code != FPE_FLTDIV)
1921 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1922 siginfo->si_code);
1924 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1925 rec->NumberParameters = 1;
1926 /* no idea what meaning is actually behind this but that's what native does */
1927 rec->ExceptionInformation[0] = 0;
1928 break;
1929 default:
1930 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1931 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1932 break;
1937 /**********************************************************************
1938 * int_handler
1940 * Handler for SIGINT.
1942 * FIXME: should not be calling external functions on the signal stack.
1944 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1946 WORD fs, gs;
1947 init_handler( sigcontext, &fs, &gs );
1948 if (!dispatch_signal(SIGINT))
1950 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
1951 rec->ExceptionCode = CONTROL_C_EXIT;
1955 /**********************************************************************
1956 * abrt_handler
1958 * Handler for SIGABRT.
1960 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1962 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
1963 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1964 rec->ExceptionFlags = EH_NONCONTINUABLE;
1968 /**********************************************************************
1969 * quit_handler
1971 * Handler for SIGQUIT.
1973 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1975 WORD fs, gs;
1976 init_handler( sigcontext, &fs, &gs );
1977 abort_thread(0);
1981 /**********************************************************************
1982 * usr1_handler
1984 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1986 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1988 CONTEXT context;
1989 WORD fs, gs;
1991 init_handler( sigcontext, &fs, &gs );
1992 save_context( &context, sigcontext, fs, gs );
1993 wait_suspend( &context );
1994 restore_context( &context, sigcontext );
1998 /**********************************************************************
1999 * get_signal_stack_total_size
2001 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
2002 * Must be a power of two.
2004 size_t get_signal_stack_total_size(void)
2006 if (!signal_stack_size)
2008 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2009 /* find the first power of two not smaller than min_size */
2010 while (size < min_size) size *= 2;
2011 signal_stack_mask = size - 1;
2012 signal_stack_size = size - teb_size;
2014 return signal_stack_size + teb_size;
2018 /***********************************************************************
2019 * __wine_set_signal_handler (NTDLL.@)
2021 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2023 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2024 if (handlers[sig] != NULL) return -2;
2025 handlers[sig] = wsh;
2026 return 0;
2030 /**********************************************************************
2031 * signal_init_thread
2033 void signal_init_thread( TEB *teb )
2035 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
2036 LDT_ENTRY fs_entry;
2037 stack_t ss;
2039 #ifdef __APPLE__
2040 int mib[2], val = 1;
2042 mib[0] = CTL_KERN;
2043 mib[1] = KERN_THALTSTACK;
2044 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2045 #endif
2047 ss.ss_sp = (char *)teb + teb_size;
2048 ss.ss_size = signal_stack_size;
2049 ss.ss_flags = 0;
2050 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2052 wine_ldt_set_base( &fs_entry, teb );
2053 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2054 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2055 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2056 thread_data->gs = wine_get_gs();
2059 /**********************************************************************
2060 * signal_init_process
2062 void signal_init_process(void)
2064 struct sigaction sig_act;
2066 sig_act.sa_mask = server_block_set;
2067 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2068 #ifdef SA_ONSTACK
2069 sig_act.sa_flags |= SA_ONSTACK;
2070 #endif
2072 sig_act.sa_sigaction = int_handler;
2073 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2074 sig_act.sa_sigaction = fpe_handler;
2075 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2076 sig_act.sa_sigaction = abrt_handler;
2077 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2078 sig_act.sa_sigaction = quit_handler;
2079 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2080 sig_act.sa_sigaction = usr1_handler;
2081 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2083 sig_act.sa_sigaction = segv_handler;
2084 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2085 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2086 #ifdef SIGBUS
2087 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2088 #endif
2090 #ifdef SIGTRAP
2091 sig_act.sa_sigaction = trap_handler;
2092 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2093 #endif
2095 #ifdef __HAVE_VM86
2096 sig_act.sa_sigaction = usr2_handler;
2097 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2098 #endif
2100 return;
2102 error:
2103 perror("sigaction");
2104 exit(1);
2108 #ifdef __HAVE_VM86
2109 /**********************************************************************
2110 * __wine_enter_vm86 (NTDLL.@)
2112 * Enter vm86 mode with the specified register context.
2114 void __wine_enter_vm86( CONTEXT *context )
2116 EXCEPTION_RECORD rec;
2117 int res;
2118 struct vm86plus_struct vm86;
2120 memset( &vm86, 0, sizeof(vm86) );
2121 for (;;)
2123 restore_vm86_context( context, &vm86 );
2125 ntdll_get_thread_data()->vm86_ptr = &vm86;
2126 merge_vm86_pending_flags( &rec );
2128 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
2129 if (res < 0)
2131 errno = -res;
2132 return;
2135 save_vm86_context( context, &vm86 );
2137 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2138 rec.ExceptionRecord = NULL;
2139 rec.ExceptionAddress = (LPVOID)context->Eip;
2140 rec.NumberParameters = 0;
2142 switch(VM86_TYPE(res))
2144 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
2145 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
2146 break;
2147 case VM86_TRAP: /* return due to DOS-debugger request */
2148 switch(VM86_ARG(res))
2150 case TRAP_x86_TRCTRAP: /* Single-step exception */
2151 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
2152 break;
2153 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2154 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
2155 /* fall through */
2156 default:
2157 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
2158 break;
2160 break;
2161 case VM86_INTx: /* int3/int x instruction (ARG = x) */
2162 rec.ExceptionCode = EXCEPTION_VM86_INTx;
2163 rec.NumberParameters = 1;
2164 rec.ExceptionInformation[0] = VM86_ARG(res);
2165 break;
2166 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
2167 context->EFlags |= VIF_FLAG;
2168 context->EFlags &= ~VIP_FLAG;
2169 get_vm86_teb_info()->vm86_pending = 0;
2170 rec.ExceptionCode = EXCEPTION_VM86_STI;
2171 break;
2172 case VM86_PICRETURN: /* return due to pending PIC request */
2173 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
2174 break;
2175 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
2176 default:
2177 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
2178 continue;
2180 raise_exception( &rec, context, TRUE );
2184 #else /* __HAVE_VM86 */
2185 /**********************************************************************
2186 * __wine_enter_vm86 (NTDLL.@)
2188 void __wine_enter_vm86( CONTEXT *context )
2190 MESSAGE("vm86 mode not supported on this platform\n");
2192 #endif /* __HAVE_VM86 */
2195 /*******************************************************************
2196 * RtlUnwind (NTDLL.@)
2198 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2199 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2201 EXCEPTION_RECORD record;
2202 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2203 DWORD res;
2205 context->Eax = (DWORD)retval;
2207 /* build an exception record, if we do not have one */
2208 if (!pRecord)
2210 record.ExceptionCode = STATUS_UNWIND;
2211 record.ExceptionFlags = 0;
2212 record.ExceptionRecord = NULL;
2213 record.ExceptionAddress = (void *)context->Eip;
2214 record.NumberParameters = 0;
2215 pRecord = &record;
2218 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2220 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2222 /* get chain of exception frames */
2223 frame = NtCurrentTeb()->Tib.ExceptionList;
2224 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2226 /* Check frame address */
2227 if (pEndFrame && (frame > pEndFrame))
2228 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2230 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
2231 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
2232 (UINT_PTR)frame & 3)
2233 raise_status( STATUS_BAD_STACK, pRecord );
2235 /* Call handler */
2236 TRACE( "calling handler at %p code=%x flags=%x\n",
2237 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2238 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2239 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2241 switch(res)
2243 case ExceptionContinueSearch:
2244 break;
2245 case ExceptionCollidedUnwind:
2246 frame = dispatch;
2247 break;
2248 default:
2249 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2250 break;
2252 frame = __wine_pop_frame( frame );
2255 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2258 /*******************************************************************
2259 * NtRaiseException (NTDLL.@)
2261 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2263 NTSTATUS status = raise_exception( rec, context, first_chance );
2264 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2265 return status;
2269 /***********************************************************************
2270 * RtlRaiseException (NTDLL.@)
2272 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2274 NTSTATUS status;
2276 rec->ExceptionAddress = (void *)context->Eip;
2277 status = raise_exception( rec, context, TRUE );
2278 if (status != STATUS_SUCCESS) raise_status( status, rec );
2280 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2283 /* wrapper for apps that don't declare the thread function correctly */
2284 extern void DECLSPEC_NORETURN call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg );
2285 __ASM_GLOBAL_FUNC(call_thread_func,
2286 "pushl %ebp\n\t"
2287 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2288 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2289 "movl %esp,%ebp\n\t"
2290 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2291 "subl $4,%esp\n\t"
2292 "pushl 12(%ebp)\n\t"
2293 "call *8(%ebp)\n\t"
2294 "leal -4(%ebp),%esp\n\t"
2295 "pushl %eax\n\t"
2296 "call " __ASM_NAME("exit_thread") "\n\t"
2297 "int $3" )
2299 /***********************************************************************
2300 * call_thread_entry_point
2302 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
2304 __TRY
2306 call_thread_func( entry, arg );
2308 __EXCEPT(unhandled_exception_filter)
2310 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2312 __ENDTRY
2313 abort(); /* should not be reached */
2316 /***********************************************************************
2317 * RtlExitUserThread (NTDLL.@)
2319 void WINAPI RtlExitUserThread( ULONG status )
2321 exit_thread( status );
2324 /***********************************************************************
2325 * abort_thread
2327 void abort_thread( int status )
2329 terminate_thread( status );
2332 /**********************************************************************
2333 * DbgBreakPoint (NTDLL.@)
2335 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2337 /**********************************************************************
2338 * DbgUserBreakPoint (NTDLL.@)
2340 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2342 /**********************************************************************
2343 * NtCurrentTeb (NTDLL.@)
2345 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2348 /**********************************************************************
2349 * EXC_CallHandler (internal)
2351 * Some exception handlers depend on EBP to have a fixed position relative to
2352 * the exception frame.
2353 * Shrinker depends on (*1) doing what it does,
2354 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2355 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2356 * function calling the handler having only 5 parameters (*4).
2358 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2359 "pushl %ebp\n\t"
2360 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2361 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2362 "movl %esp,%ebp\n\t"
2363 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2364 "pushl %ebx\n\t"
2365 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2366 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2367 "pushl 24(%ebp)\n\t"
2368 "pushl 20(%ebp)\n\t"
2369 "pushl 16(%ebp)\n\t"
2370 "pushl 12(%ebp)\n\t"
2371 "pushl 8(%ebp)\n\t"
2372 "call " __ASM_NAME("call_exception_handler") "\n\t"
2373 "popl %ebx\n\t"
2374 __ASM_CFI(".cfi_same_value %ebx\n\t")
2375 "leave\n"
2376 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2377 __ASM_CFI(".cfi_same_value %ebp\n\t")
2378 "ret" )
2379 __ASM_GLOBAL_FUNC(call_exception_handler,
2380 "pushl %ebp\n\t"
2381 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2382 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2383 "movl %esp,%ebp\n\t"
2384 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2385 "subl $12,%esp\n\t"
2386 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2387 "pushl %edx\n\t" /* handler be handled by... */
2388 ".byte 0x64\n\t"
2389 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2390 ".byte 0x64\n\t"
2391 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2392 "pushl 20(%ebp)\n\t"
2393 "pushl 16(%ebp)\n\t"
2394 "pushl 12(%ebp)\n\t"
2395 "pushl 8(%ebp)\n\t"
2396 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2397 "call *%ecx\n\t" /* call handler. (*2) */
2398 ".byte 0x64\n\t"
2399 "movl (0), %esp\n\t" /* restore previous... (*3) */
2400 ".byte 0x64\n\t"
2401 "popl (0)\n\t" /* exception frame. */
2402 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2403 "popl %ebp\n\t"
2404 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2405 __ASM_CFI(".cfi_same_value %ebp\n\t")
2406 "ret $20" ) /* (*4) */
2408 #endif /* __i386__ */