kernel32: Add stub for GetSystemTimes.
[wine.git] / dlls / ntdll / signal_i386.c
blob07f68f18fed547f8b8b8ec2fbb72e1bd3b4415fd
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 "windef.h"
59 #include "wine/library.h"
60 #include "ntdll_misc.h"
61 #include "wine/exception.h"
62 #include "wine/debug.h"
64 #ifdef HAVE_VALGRIND_MEMCHECK_H
65 #include <valgrind/memcheck.h>
66 #endif
68 #undef ERR /* Solaris needs to define this */
70 /* not defined for x86, so copy the x86_64 definition */
71 typedef struct DECLSPEC_ALIGN(16) _M128A
73 ULONGLONG Low;
74 LONGLONG High;
75 } M128A;
77 typedef struct
79 WORD ControlWord;
80 WORD StatusWord;
81 BYTE TagWord;
82 BYTE Reserved1;
83 WORD ErrorOpcode;
84 DWORD ErrorOffset;
85 WORD ErrorSelector;
86 WORD Reserved2;
87 DWORD DataOffset;
88 WORD DataSelector;
89 WORD Reserved3;
90 DWORD MxCsr;
91 DWORD MxCsr_Mask;
92 M128A FloatRegisters[8];
93 M128A XmmRegisters[16];
94 BYTE Reserved4[96];
95 } XMM_SAVE_AREA32;
97 /***********************************************************************
98 * signal context platform-specific definitions
101 #ifdef linux
103 typedef ucontext_t SIGCONTEXT;
105 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
106 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
107 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
108 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
109 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
110 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
111 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
112 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
114 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
115 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
116 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
117 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
118 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
119 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
121 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
122 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
123 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
124 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
126 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
127 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
129 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
130 #define VIF_FLAG 0x00080000
131 #define VIP_FLAG 0x00100000
133 int vm86_enter( void **vm86_ptr );
134 void vm86_return(void);
135 void vm86_return_end(void);
136 __ASM_GLOBAL_FUNC(vm86_enter,
137 "pushl %ebp\n\t"
138 "movl %esp, %ebp\n\t"
139 "movl $166,%eax\n\t" /*SYS_vm86*/
140 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
141 "movl (%ecx),%ecx\n\t"
142 "pushl %ebx\n\t"
143 "movl $1,%ebx\n\t" /*VM86_ENTER*/
144 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
145 "pushl %fs\n\t"
146 "pushl %gs\n\t"
147 "int $0x80\n"
148 ".globl " __ASM_NAME("vm86_return") "\n\t"
149 __ASM_FUNC("vm86_return") "\n"
150 __ASM_NAME("vm86_return") ":\n\t"
151 "popl %gs\n\t"
152 "popl %fs\n\t"
153 "popl %ecx\n\t"
154 "popl %ebx\n\t"
155 "popl %ebp\n\t"
156 "testl %eax,%eax\n\t"
157 "jl 0f\n\t"
158 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
159 "je " __ASM_NAME("vm86_enter") "\n\t"
160 "0:\n\t"
161 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
162 "movl $0,(%ecx)\n\t"
163 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
164 __ASM_FUNC("vm86_return_end") "\n"
165 __ASM_NAME("vm86_return_end") ":\n\t"
166 "ret" )
168 #ifdef HAVE_SYS_VM86_H
169 # define __HAVE_VM86
170 #endif
172 #endif /* linux */
174 #ifdef BSDI
176 #include <machine/frame.h>
177 typedef struct trapframe SIGCONTEXT;
179 #define EAX_sig(context) ((context)->tf_eax)
180 #define EBX_sig(context) ((context)->tf_ebx)
181 #define ECX_sig(context) ((context)->tf_ecx)
182 #define EDX_sig(context) ((context)->tf_edx)
183 #define ESI_sig(context) ((context)->tf_esi)
184 #define EDI_sig(context) ((context)->tf_edi)
185 #define EBP_sig(context) ((context)->tf_ebp)
187 #define CS_sig(context) ((context)->tf_cs)
188 #define DS_sig(context) ((context)->tf_ds)
189 #define ES_sig(context) ((context)->tf_es)
190 #define SS_sig(context) ((context)->tf_ss)
192 #define EFL_sig(context) ((context)->tf_eflags)
194 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
195 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
197 #define FPU_sig(context) NULL /* FIXME */
198 #define FPUX_sig(context) NULL /* FIXME */
200 #endif /* bsdi */
202 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
204 typedef struct sigcontext SIGCONTEXT;
206 #define EAX_sig(context) ((context)->sc_eax)
207 #define EBX_sig(context) ((context)->sc_ebx)
208 #define ECX_sig(context) ((context)->sc_ecx)
209 #define EDX_sig(context) ((context)->sc_edx)
210 #define ESI_sig(context) ((context)->sc_esi)
211 #define EDI_sig(context) ((context)->sc_edi)
212 #define EBP_sig(context) ((context)->sc_ebp)
214 #define CS_sig(context) ((context)->sc_cs)
215 #define DS_sig(context) ((context)->sc_ds)
216 #define ES_sig(context) ((context)->sc_es)
217 #define FS_sig(context) ((context)->sc_fs)
218 #define GS_sig(context) ((context)->sc_gs)
219 #define SS_sig(context) ((context)->sc_ss)
221 #define TRAP_sig(context) ((context)->sc_trapno)
222 #define ERROR_sig(context) ((context)->sc_err)
223 #define EFL_sig(context) ((context)->sc_eflags)
225 #define EIP_sig(context) ((context)->sc_eip)
226 #define ESP_sig(context) ((context)->sc_esp)
228 #define FPU_sig(context) NULL /* FIXME */
229 #define FPUX_sig(context) NULL /* FIXME */
231 #endif /* *BSD */
233 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
235 #ifdef _SCO_DS
236 #include <sys/regset.h>
237 #endif
238 #include <sys/ucontext.h>
239 typedef struct ucontext SIGCONTEXT;
241 #ifdef _SCO_DS
242 #define gregs regs
243 #endif
245 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
246 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
247 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
248 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
249 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
250 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
251 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
253 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
254 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
255 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
256 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
258 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
259 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
261 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
263 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
264 #ifdef UESP
265 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
266 #elif defined(R_ESP)
267 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
268 #else
269 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
270 #endif
271 #ifdef ERR
272 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
273 #endif
274 #ifdef TRAPNO
275 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
276 #endif
278 #define FPU_sig(context) NULL /* FIXME */
279 #define FPUX_sig(context) NULL /* FIXME */
281 #endif /* svr4 || SCO_DS */
283 #ifdef __APPLE__
284 # include <sys/ucontext.h>
286 typedef ucontext_t SIGCONTEXT;
288 /* work around silly renaming of struct members in OS X 10.5 */
289 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
290 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
291 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
292 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
293 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
294 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
295 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
296 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
297 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
298 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
299 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
300 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
301 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
302 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
303 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
304 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
305 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
306 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
307 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
308 #define FPU_sig(context) NULL
309 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
310 #else
311 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
312 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
313 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
314 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
315 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
316 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
317 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
318 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
319 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
320 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
321 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
322 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
323 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
324 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
325 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
326 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
327 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
328 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
329 #define FPU_sig(context) NULL
330 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
331 #endif
333 #endif /* __APPLE__ */
335 #if defined(__NetBSD__)
336 # include <sys/ucontext.h>
337 # include <sys/types.h>
338 # include <signal.h>
340 typedef ucontext_t SIGCONTEXT;
342 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
343 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
344 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
345 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
346 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
347 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
348 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
349 #define ESP_sig(context) _UC_MACHINE_SP(context)
351 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
352 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
353 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
354 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
355 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
356 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
358 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
359 #define EIP_sig(context) _UC_MACHINE_PC(context)
360 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
361 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
363 #define FPU_sig(context) NULL
364 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
366 #define T_MCHK T_MCA
367 #define T_XMMFLT T_XMM
369 #endif /* __NetBSD__ */
371 WINE_DEFAULT_DEBUG_CHANNEL(seh);
373 typedef int (*wine_signal_handler)(unsigned int sig);
375 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
376 static size_t signal_stack_mask;
377 static size_t signal_stack_size;
379 static wine_signal_handler handlers[256];
381 static int fpux_support; /* whether the CPU support extended fpu context */
383 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
385 enum i386_trap_code
387 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
388 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
389 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
390 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
391 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
392 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
393 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
394 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
395 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
396 TRAP_x86_DNA = T_DNA, /* Device not available exception */
397 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
398 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
399 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
400 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
401 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
402 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
403 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
404 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
405 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
406 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
407 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
408 #else
409 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
410 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
411 TRAP_x86_NMI = 2, /* NMI interrupt */
412 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
413 TRAP_x86_OFLOW = 4, /* Overflow exception */
414 TRAP_x86_BOUND = 5, /* Bound range exception */
415 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
416 TRAP_x86_DNA = 7, /* Device not available exception */
417 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
418 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
419 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
420 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
421 TRAP_x86_STKFLT = 12, /* Stack fault */
422 TRAP_x86_PROTFLT = 13, /* General protection fault */
423 TRAP_x86_PAGEFLT = 14, /* Page fault */
424 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
425 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
426 TRAP_x86_MCHK = 18, /* Machine check exception */
427 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
428 otherwise Cache flush exception (via SIGSEV) */
429 #endif
433 /***********************************************************************
434 * dispatch_signal
436 static inline int dispatch_signal(unsigned int sig)
438 if (handlers[sig] == NULL) return 0;
439 return handlers[sig](sig);
443 /***********************************************************************
444 * get_trap_code
446 * Get the trap code for a signal.
448 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
450 #ifdef TRAP_sig
451 return TRAP_sig(sigcontext);
452 #else
453 return TRAP_x86_UNKNOWN; /* unknown trap code */
454 #endif
457 /***********************************************************************
458 * get_error_code
460 * Get the error code for a signal.
462 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
464 #ifdef ERROR_sig
465 return ERROR_sig(sigcontext);
466 #else
467 return 0;
468 #endif
471 /***********************************************************************
472 * get_signal_stack
474 * Get the base of the signal stack for the current thread.
476 static inline void *get_signal_stack(void)
478 return (char *)NtCurrentTeb() + 4096;
482 /***********************************************************************
483 * get_current_teb
485 * Get the current teb based on the stack pointer.
487 static inline TEB *get_current_teb(void)
489 unsigned long esp;
490 __asm__("movl %%esp,%0" : "=g" (esp) );
491 return (TEB *)(esp & ~signal_stack_mask);
495 #ifdef __HAVE_VM86
496 /***********************************************************************
497 * save_vm86_context
499 * Set the register values from a vm86 structure.
501 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
503 context->ContextFlags = CONTEXT_FULL;
504 context->Eax = vm86->regs.eax;
505 context->Ebx = vm86->regs.ebx;
506 context->Ecx = vm86->regs.ecx;
507 context->Edx = vm86->regs.edx;
508 context->Esi = vm86->regs.esi;
509 context->Edi = vm86->regs.edi;
510 context->Esp = vm86->regs.esp;
511 context->Ebp = vm86->regs.ebp;
512 context->Eip = vm86->regs.eip;
513 context->SegCs = vm86->regs.cs;
514 context->SegDs = vm86->regs.ds;
515 context->SegEs = vm86->regs.es;
516 context->SegFs = vm86->regs.fs;
517 context->SegGs = vm86->regs.gs;
518 context->SegSs = vm86->regs.ss;
519 context->EFlags = vm86->regs.eflags;
523 /***********************************************************************
524 * restore_vm86_context
526 * Build a vm86 structure from the register values.
528 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
530 vm86->regs.eax = context->Eax;
531 vm86->regs.ebx = context->Ebx;
532 vm86->regs.ecx = context->Ecx;
533 vm86->regs.edx = context->Edx;
534 vm86->regs.esi = context->Esi;
535 vm86->regs.edi = context->Edi;
536 vm86->regs.esp = context->Esp;
537 vm86->regs.ebp = context->Ebp;
538 vm86->regs.eip = context->Eip;
539 vm86->regs.cs = context->SegCs;
540 vm86->regs.ds = context->SegDs;
541 vm86->regs.es = context->SegEs;
542 vm86->regs.fs = context->SegFs;
543 vm86->regs.gs = context->SegGs;
544 vm86->regs.ss = context->SegSs;
545 vm86->regs.eflags = context->EFlags;
549 /**********************************************************************
550 * merge_vm86_pending_flags
552 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
553 * raises exception if there are pending events and VIF flag
554 * has been turned on.
556 * Called from __wine_enter_vm86 because vm86_enter
557 * doesn't check for pending events.
559 * Called from raise_vm86_sti_exception to check for
560 * pending events in a signal safe way.
562 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
564 BOOL check_pending = TRUE;
565 struct vm86plus_struct *vm86 =
566 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
569 * In order to prevent a race when SIGUSR2 occurs while
570 * we are returning from exception handler, pending events
571 * will be rechecked after each raised exception.
573 while (check_pending && get_vm86_teb_info()->vm86_pending)
575 check_pending = FALSE;
576 ntdll_get_thread_data()->vm86_ptr = NULL;
579 * If VIF is set, throw exception.
580 * Note that SIGUSR2 may turn VIF flag off so
581 * VIF check must occur only when TEB.vm86_ptr is NULL.
583 if (vm86->regs.eflags & VIF_FLAG)
585 CONTEXT vcontext;
586 save_vm86_context( &vcontext, vm86 );
588 rec->ExceptionCode = EXCEPTION_VM86_STI;
589 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
590 rec->ExceptionRecord = NULL;
591 rec->NumberParameters = 0;
592 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
594 vcontext.EFlags &= ~VIP_FLAG;
595 get_vm86_teb_info()->vm86_pending = 0;
596 __regs_RtlRaiseException( rec, &vcontext );
598 restore_vm86_context( &vcontext, vm86 );
599 check_pending = TRUE;
602 ntdll_get_thread_data()->vm86_ptr = vm86;
606 * Merge VIP flags in a signal safe way. This requires
607 * that the following operation compiles into atomic
608 * instruction.
610 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
612 #endif /* __HAVE_VM86 */
615 #ifdef __sun
617 /* We have to workaround two Solaris breakages:
618 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
619 * code crash badly.
620 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
621 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
624 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
625 __ASM_GLOBAL_FUNC( sigaction_syscall,
626 "movl $0x62,%eax\n\t"
627 "int $0x91\n\t"
628 "ret" )
630 /* assume the same libc handler is used for all signals */
631 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
633 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
635 struct ntdll_thread_data *thread_data;
637 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
639 thread_data = (struct ntdll_thread_data *)get_current_teb()->SystemReserved2;
640 wine_set_fs( thread_data->fs );
641 wine_set_gs( thread_data->gs );
643 libc_sigacthandler( signal, siginfo, sigcontext );
646 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
648 struct sigaction real_act;
650 if (sigaction( sig, new, old ) == -1) return -1;
652 /* retrieve the real handler and flags with a direct syscall */
653 sigaction_syscall( sig, NULL, &real_act );
654 libc_sigacthandler = real_act.sa_sigaction;
655 real_act.sa_sigaction = wine_sigacthandler;
656 sigaction_syscall( sig, &real_act, NULL );
657 return 0;
659 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
661 #endif
663 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
666 /***********************************************************************
667 * init_handler
669 * Handler initialization when the full context is not needed.
670 * Return the stack pointer to use for pushing the exception data.
672 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
674 TEB *teb = get_current_teb();
676 /* get %fs and %gs at time of the fault */
677 #ifdef FS_sig
678 *fs = LOWORD(FS_sig(sigcontext));
679 #else
680 *fs = wine_get_fs();
681 #endif
682 #ifdef GS_sig
683 *gs = LOWORD(GS_sig(sigcontext));
684 #else
685 *gs = wine_get_gs();
686 #endif
688 #ifndef __sun /* see above for Solaris handling */
690 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
691 wine_set_fs( thread_data->fs );
692 wine_set_gs( thread_data->gs );
694 #endif
696 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
697 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
700 * Win16 or DOS protected mode. Note that during switch
701 * from 16-bit mode to linear mode, CS may be set to system
702 * segment before FS is restored. Fortunately, in this case
703 * SS is still non-system segment. This is why both CS and SS
704 * are checked.
706 return teb->WOW32Reserved;
708 return (void *)(ESP_sig(sigcontext) & ~3);
712 /***********************************************************************
713 * save_fpu
715 * Save the thread FPU context.
717 static inline void save_fpu( CONTEXT *context )
719 #ifdef __GNUC__
720 context->ContextFlags |= CONTEXT_FLOATING_POINT;
721 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
722 #endif
726 /***********************************************************************
727 * save_fpux
729 * Save the thread FPU extended context.
731 static inline void save_fpux( CONTEXT *context )
733 #ifdef __GNUC__
734 /* we have to enforce alignment by hand */
735 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
736 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
738 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
739 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
740 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
741 #endif
745 /***********************************************************************
746 * restore_fpu
748 * Restore the FPU context to a sigcontext.
750 static inline void restore_fpu( const CONTEXT *context )
752 FLOATING_SAVE_AREA float_status = context->FloatSave;
753 /* reset the current interrupt status */
754 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
755 #ifdef __GNUC__
756 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
757 #endif /* __GNUC__ */
761 /***********************************************************************
762 * restore_fpux
764 * Restore the FPU extended context to a sigcontext.
766 static inline void restore_fpux( const CONTEXT *context )
768 #ifdef __GNUC__
769 /* we have to enforce alignment by hand */
770 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
771 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
773 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
774 /* reset the current interrupt status */
775 state->StatusWord &= state->ControlWord | 0xff80;
776 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
777 #endif
781 /***********************************************************************
782 * fpux_to_fpu
784 * Build a standard FPU context from an extended one.
786 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
788 unsigned int i, tag, stack_top;
790 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
791 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
792 fpu->ErrorOffset = fpux->ErrorOffset;
793 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
794 fpu->DataOffset = fpux->DataOffset;
795 fpu->DataSelector = fpux->DataSelector;
796 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
798 stack_top = (fpux->StatusWord >> 11) & 7;
799 fpu->TagWord = 0xffff0000;
800 for (i = 0; i < 8; i++)
802 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
803 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
804 else
806 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
807 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
809 tag = 2; /* special */
811 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
813 if (reg->Low) tag = 2; /* special */
814 else tag = 1; /* zero */
816 else
818 if (reg->Low >> 63) tag = 0; /* valid */
819 else tag = 2; /* special */
822 fpu->TagWord |= tag << (2 * i);
827 /***********************************************************************
828 * save_context
830 * Build a context structure from the signal info.
832 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
834 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
835 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
836 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
838 memset(context, 0, sizeof(*context));
839 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
840 context->Eax = EAX_sig(sigcontext);
841 context->Ebx = EBX_sig(sigcontext);
842 context->Ecx = ECX_sig(sigcontext);
843 context->Edx = EDX_sig(sigcontext);
844 context->Esi = ESI_sig(sigcontext);
845 context->Edi = EDI_sig(sigcontext);
846 context->Ebp = EBP_sig(sigcontext);
847 context->EFlags = EFL_sig(sigcontext);
848 context->Eip = EIP_sig(sigcontext);
849 context->Esp = ESP_sig(sigcontext);
850 context->SegCs = LOWORD(CS_sig(sigcontext));
851 context->SegDs = LOWORD(DS_sig(sigcontext));
852 context->SegEs = LOWORD(ES_sig(sigcontext));
853 context->SegFs = fs;
854 context->SegGs = gs;
855 context->SegSs = LOWORD(SS_sig(sigcontext));
856 context->Dr0 = regs->dr0;
857 context->Dr1 = regs->dr1;
858 context->Dr2 = regs->dr2;
859 context->Dr3 = regs->dr3;
860 context->Dr6 = regs->dr6;
861 context->Dr7 = regs->dr7;
863 if (fpu)
865 context->ContextFlags |= CONTEXT_FLOATING_POINT;
866 context->FloatSave = *fpu;
868 if (fpux)
870 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
871 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
872 fpux_support = 1;
873 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
875 if (!fpu && !fpux) save_fpu( context );
879 /***********************************************************************
880 * restore_context
882 * Restore the signal info from the context.
884 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
886 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
887 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
888 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
890 regs->dr0 = context->Dr0;
891 regs->dr1 = context->Dr1;
892 regs->dr2 = context->Dr2;
893 regs->dr3 = context->Dr3;
894 regs->dr6 = context->Dr6;
895 regs->dr7 = context->Dr7;
896 EAX_sig(sigcontext) = context->Eax;
897 EBX_sig(sigcontext) = context->Ebx;
898 ECX_sig(sigcontext) = context->Ecx;
899 EDX_sig(sigcontext) = context->Edx;
900 ESI_sig(sigcontext) = context->Esi;
901 EDI_sig(sigcontext) = context->Edi;
902 EBP_sig(sigcontext) = context->Ebp;
903 EFL_sig(sigcontext) = context->EFlags;
904 EIP_sig(sigcontext) = context->Eip;
905 ESP_sig(sigcontext) = context->Esp;
906 CS_sig(sigcontext) = context->SegCs;
907 DS_sig(sigcontext) = context->SegDs;
908 ES_sig(sigcontext) = context->SegEs;
909 SS_sig(sigcontext) = context->SegSs;
910 #ifdef GS_sig
911 GS_sig(sigcontext) = context->SegGs;
912 #else
913 wine_set_gs( context->SegGs );
914 #endif
915 #ifdef FS_sig
916 FS_sig(sigcontext) = context->SegFs;
917 #else
918 wine_set_fs( context->SegFs );
919 #endif
921 if (fpu) *fpu = context->FloatSave;
922 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
923 if (!fpu && !fpux) restore_fpu( context );
927 /***********************************************************************
928 * RtlCaptureContext (NTDLL.@)
930 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
932 *context = *regs;
933 if (fpux_support) save_fpux( context );
934 else save_fpu( context );
936 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
939 /***********************************************************************
940 * set_cpu_context
942 * Set the new CPU context. Used by NtSetContextThread.
944 void set_cpu_context( const CONTEXT *context )
946 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
948 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
949 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
951 if (flags & CONTEXT_DEBUG_REGISTERS)
953 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
954 regs->dr0 = context->Dr0;
955 regs->dr1 = context->Dr1;
956 regs->dr2 = context->Dr2;
957 regs->dr3 = context->Dr3;
958 regs->dr6 = context->Dr6;
959 regs->dr7 = context->Dr7;
961 if (flags & CONTEXT_FULL)
963 if (!(flags & CONTEXT_CONTROL))
964 FIXME( "setting partial context (%x) not supported\n", flags );
965 else if (flags & CONTEXT_SEGMENTS)
966 __wine_call_from_32_restore_regs( context );
967 else
969 CONTEXT newcontext = *context;
970 newcontext.SegDs = wine_get_ds();
971 newcontext.SegEs = wine_get_es();
972 newcontext.SegFs = wine_get_fs();
973 newcontext.SegGs = wine_get_gs();
974 __wine_call_from_32_restore_regs( &newcontext );
980 /***********************************************************************
981 * copy_context
983 * Copy a register context according to the flags.
985 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
987 flags &= ~CONTEXT_i386; /* get rid of CPU id */
988 if (flags & CONTEXT_INTEGER)
990 to->Eax = from->Eax;
991 to->Ebx = from->Ebx;
992 to->Ecx = from->Ecx;
993 to->Edx = from->Edx;
994 to->Esi = from->Esi;
995 to->Edi = from->Edi;
997 if (flags & CONTEXT_CONTROL)
999 to->Ebp = from->Ebp;
1000 to->Esp = from->Esp;
1001 to->Eip = from->Eip;
1002 to->SegCs = from->SegCs;
1003 to->SegSs = from->SegSs;
1004 to->EFlags = from->EFlags;
1006 if (flags & CONTEXT_SEGMENTS)
1008 to->SegDs = from->SegDs;
1009 to->SegEs = from->SegEs;
1010 to->SegFs = from->SegFs;
1011 to->SegGs = from->SegGs;
1013 if (flags & CONTEXT_DEBUG_REGISTERS)
1015 to->Dr0 = from->Dr0;
1016 to->Dr1 = from->Dr1;
1017 to->Dr2 = from->Dr2;
1018 to->Dr3 = from->Dr3;
1019 to->Dr6 = from->Dr6;
1020 to->Dr7 = from->Dr7;
1022 if (flags & CONTEXT_FLOATING_POINT)
1024 to->FloatSave = from->FloatSave;
1026 if (flags & CONTEXT_EXTENDED_REGISTERS)
1028 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
1033 /***********************************************************************
1034 * is_privileged_instr
1036 * Check if the fault location is a privileged instruction.
1037 * Based on the instruction emulation code in dlls/kernel/instr.c.
1039 static inline DWORD is_privileged_instr( CONTEXT86 *context )
1041 const BYTE *instr;
1042 unsigned int prefix_count = 0;
1044 if (!wine_ldt_is_system( context->SegCs )) return 0;
1045 instr = (BYTE *)context->Eip;
1047 for (;;) switch(*instr)
1049 /* instruction prefixes */
1050 case 0x2e: /* %cs: */
1051 case 0x36: /* %ss: */
1052 case 0x3e: /* %ds: */
1053 case 0x26: /* %es: */
1054 case 0x64: /* %fs: */
1055 case 0x65: /* %gs: */
1056 case 0x66: /* opcode size */
1057 case 0x67: /* addr size */
1058 case 0xf0: /* lock */
1059 case 0xf2: /* repne */
1060 case 0xf3: /* repe */
1061 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1062 instr++;
1063 continue;
1065 case 0x0f: /* extended instruction */
1066 switch(instr[1])
1068 case 0x20: /* mov crX, reg */
1069 case 0x21: /* mov drX, reg */
1070 case 0x22: /* mov reg, crX */
1071 case 0x23: /* mov reg drX */
1072 return EXCEPTION_PRIV_INSTRUCTION;
1074 return 0;
1075 case 0x6c: /* insb (%dx) */
1076 case 0x6d: /* insl (%dx) */
1077 case 0x6e: /* outsb (%dx) */
1078 case 0x6f: /* outsl (%dx) */
1079 case 0xcd: /* int $xx */
1080 case 0xe4: /* inb al,XX */
1081 case 0xe5: /* in (e)ax,XX */
1082 case 0xe6: /* outb XX,al */
1083 case 0xe7: /* out XX,(e)ax */
1084 case 0xec: /* inb (%dx),%al */
1085 case 0xed: /* inl (%dx),%eax */
1086 case 0xee: /* outb %al,(%dx) */
1087 case 0xef: /* outl %eax,(%dx) */
1088 case 0xf4: /* hlt */
1089 case 0xfa: /* cli */
1090 case 0xfb: /* sti */
1091 return EXCEPTION_PRIV_INSTRUCTION;
1092 default:
1093 return 0;
1098 #include "pshpack1.h"
1099 struct atl_thunk
1101 DWORD movl; /* movl this,4(%esp) */
1102 DWORD this;
1103 BYTE jmp; /* jmp func */
1104 int func;
1106 #include "poppack.h"
1108 /**********************************************************************
1109 * check_atl_thunk
1111 * Check if code destination is an ATL thunk, and emulate it if so.
1113 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1115 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1116 BOOL ret = FALSE;
1118 __TRY
1120 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1122 *((DWORD *)context->Esp + 1) = thunk->this;
1123 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1124 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1125 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1126 ret = TRUE;
1129 __EXCEPT_PAGE_FAULT
1131 return FALSE;
1133 __ENDTRY
1134 return ret;
1138 /***********************************************************************
1139 * setup_exception_record
1141 * Setup the exception record and context on the thread stack.
1143 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1144 WORD fs, WORD gs, raise_func func )
1146 struct stack_layout
1148 void *ret_addr; /* return address from raise_func */
1149 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1150 CONTEXT *context_ptr; /* second arg for raise_func */
1151 CONTEXT context;
1152 EXCEPTION_RECORD rec;
1153 DWORD ebp;
1154 DWORD eip;
1155 } *stack = stack_ptr;
1156 DWORD exception_code = 0;
1158 /* stack sanity checks */
1160 if ((char *)stack >= (char *)get_signal_stack() &&
1161 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1163 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1164 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1165 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1166 NtCurrentTeb()->Tib.StackBase );
1167 abort_thread(1);
1170 if (stack - 1 > stack || /* check for overflow in subtraction */
1171 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1172 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1174 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1175 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1176 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1177 NtCurrentTeb()->Tib.StackBase );
1179 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1181 /* stack overflow on last page, unrecoverable */
1182 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1183 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1184 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1185 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1186 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1187 abort_thread(1);
1189 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1191 /* stack access below stack limit, may be recoverable */
1192 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1193 else
1195 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1196 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1197 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1198 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1199 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1200 abort_thread(1);
1204 stack--; /* push the stack_layout structure */
1205 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1206 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1207 #elif defined(VALGRIND_MAKE_WRITABLE)
1208 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1209 #endif
1210 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1211 stack->rec_ptr = &stack->rec;
1212 stack->context_ptr = &stack->context;
1214 stack->rec.ExceptionRecord = NULL;
1215 stack->rec.ExceptionCode = exception_code;
1216 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1217 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1218 stack->rec.NumberParameters = 0;
1220 save_context( &stack->context, sigcontext, fs, gs );
1222 /* now modify the sigcontext to return to the raise function */
1223 ESP_sig(sigcontext) = (DWORD)stack;
1224 EIP_sig(sigcontext) = (DWORD)func;
1225 /* clear single-step, direction, and align check flag */
1226 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1227 CS_sig(sigcontext) = wine_get_cs();
1228 DS_sig(sigcontext) = wine_get_ds();
1229 ES_sig(sigcontext) = wine_get_es();
1230 FS_sig(sigcontext) = wine_get_fs();
1231 GS_sig(sigcontext) = wine_get_gs();
1232 SS_sig(sigcontext) = wine_get_ss();
1234 return stack->rec_ptr;
1238 /***********************************************************************
1239 * setup_exception
1241 * Setup a proper stack frame for the raise function, and modify the
1242 * sigcontext so that the return from the signal handler will call
1243 * the raise function.
1245 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1247 WORD fs, gs;
1248 void *stack = init_handler( sigcontext, &fs, &gs );
1250 return setup_exception_record( sigcontext, stack, fs, gs, func );
1254 /***********************************************************************
1255 * get_exception_context
1257 * Get a pointer to the context built by setup_exception.
1259 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1261 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1265 /**********************************************************************
1266 * get_fpu_code
1268 * Get the FPU exception code from the FPU status.
1270 static inline DWORD get_fpu_code( const CONTEXT *context )
1272 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1274 if (status & 0x01) /* IE */
1276 if (status & 0x40) /* SF */
1277 return EXCEPTION_FLT_STACK_CHECK;
1278 else
1279 return EXCEPTION_FLT_INVALID_OPERATION;
1281 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1282 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1283 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1284 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1285 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1286 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1290 /**********************************************************************
1291 * raise_segv_exception
1293 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1295 switch(rec->ExceptionCode)
1297 case EXCEPTION_ACCESS_VIOLATION:
1298 if (rec->NumberParameters == 2)
1300 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1301 goto done;
1302 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1303 rec->ExceptionInformation[0] )))
1304 goto done;
1306 break;
1307 case EXCEPTION_DATATYPE_MISALIGNMENT:
1308 /* FIXME: pass through exception handler first? */
1309 if (context->EFlags & 0x00040000)
1311 /* Disable AC flag, return */
1312 context->EFlags &= ~0x00040000;
1313 goto done;
1315 break;
1317 __regs_RtlRaiseException( rec, context );
1318 done:
1319 NtSetContextThread( GetCurrentThread(), context );
1323 /**********************************************************************
1324 * raise_trap_exception
1326 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1328 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1330 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1332 /* when single stepping can't tell whether this is a hw bp or a
1333 * single step interrupt. try to avoid as much overhead as possible
1334 * and only do a server call if there is any hw bp enabled. */
1336 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1338 /* (possible) hardware breakpoint, fetch the debug registers */
1339 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1340 NtGetContextThread(GetCurrentThread(), context);
1341 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1344 context->EFlags &= ~0x100; /* clear single-step flag */
1347 __regs_RtlRaiseException( rec, context );
1348 NtSetContextThread( GetCurrentThread(), context );
1352 /**********************************************************************
1353 * raise_exception
1355 * Generic raise function for exceptions that don't need special treatment.
1357 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1359 __regs_RtlRaiseException( rec, context );
1360 NtSetContextThread( GetCurrentThread(), context );
1364 #ifdef __HAVE_VM86
1365 /**********************************************************************
1366 * raise_vm86_sti_exception
1368 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1370 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1371 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1373 if (ntdll_get_thread_data()->vm86_ptr)
1375 if (((char*)context->Eip >= (char*)vm86_return) &&
1376 ((char*)context->Eip <= (char*)vm86_return_end) &&
1377 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1378 /* exiting from VM86, can't throw */
1379 goto done;
1381 merge_vm86_pending_flags( rec );
1383 else if (get_vm86_teb_info()->dpmi_vif &&
1384 !wine_ldt_is_system(context->SegCs) &&
1385 !wine_ldt_is_system(context->SegSs))
1387 /* Executing DPMI code and virtual interrupts are enabled. */
1388 get_vm86_teb_info()->vm86_pending = 0;
1389 __regs_RtlRaiseException( rec, context );
1391 done:
1392 NtSetContextThread( GetCurrentThread(), context );
1396 /**********************************************************************
1397 * usr2_handler
1399 * Handler for SIGUSR2.
1400 * We use it to signal that the running __wine_enter_vm86() should
1401 * immediately set VIP_FLAG, causing pending events to be handled
1402 * as early as possible.
1404 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1406 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1407 rec->ExceptionCode = EXCEPTION_VM86_STI;
1409 #endif /* __HAVE_VM86 */
1412 /**********************************************************************
1413 * segv_handler
1415 * Handler for SIGSEGV and related errors.
1417 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1419 WORD fs, gs;
1420 EXCEPTION_RECORD *rec;
1421 SIGCONTEXT *context = sigcontext;
1422 void *stack = init_handler( sigcontext, &fs, &gs );
1424 /* check for page fault inside the thread stack */
1425 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1426 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1427 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1428 virtual_handle_stack_fault( siginfo->si_addr ))
1430 /* check if this was the last guard page */
1431 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1433 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1434 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1436 return;
1439 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1440 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1442 switch(get_trap_code(context))
1444 case TRAP_x86_OFLOW: /* Overflow exception */
1445 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1446 break;
1447 case TRAP_x86_BOUND: /* Bound range exception */
1448 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1449 break;
1450 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1451 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1452 break;
1453 case TRAP_x86_STKFLT: /* Stack fault */
1454 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1455 break;
1456 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1457 case TRAP_x86_PROTFLT: /* General protection fault */
1458 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1460 WORD err = get_error_code(context);
1461 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1462 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1463 rec->NumberParameters = 2;
1464 rec->ExceptionInformation[0] = 0;
1465 /* if error contains a LDT selector, use that as fault address */
1466 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1467 rec->ExceptionInformation[1] = err & ~7;
1468 else
1469 rec->ExceptionInformation[1] = 0xffffffff;
1471 break;
1472 case TRAP_x86_PAGEFLT: /* Page fault */
1473 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1474 rec->NumberParameters = 2;
1475 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1476 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1477 break;
1478 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1479 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1480 break;
1481 default:
1482 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1483 /* fall through */
1484 case TRAP_x86_NMI: /* NMI interrupt */
1485 case TRAP_x86_DNA: /* Device not available exception */
1486 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1487 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1488 case TRAP_x86_MCHK: /* Machine check exception */
1489 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1490 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1491 break;
1496 /**********************************************************************
1497 * trap_handler
1499 * Handler for SIGTRAP.
1501 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1503 SIGCONTEXT *context = sigcontext;
1504 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1506 switch(get_trap_code(context))
1508 case TRAP_x86_TRCTRAP: /* Single-step exception */
1509 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1510 break;
1511 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1512 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1513 /* fall through */
1514 default:
1515 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1516 break;
1521 /**********************************************************************
1522 * fpe_handler
1524 * Handler for SIGFPE.
1526 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1528 CONTEXT *win_context;
1529 SIGCONTEXT *context = sigcontext;
1530 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1532 win_context = get_exception_context( rec );
1534 switch(get_trap_code(context))
1536 case TRAP_x86_DIVIDE: /* Division by zero exception */
1537 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1538 break;
1539 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1540 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1541 break;
1542 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1543 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1544 rec->ExceptionCode = get_fpu_code( win_context );
1545 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
1546 break;
1547 case TRAP_x86_CACHEFLT: /* SIMD exception */
1548 /* TODO:
1549 * Behaviour only tested for divide-by-zero exceptions
1550 * Check for other SIMD exceptions as well */
1551 if(siginfo->si_code != FPE_FLTDIV)
1552 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1553 siginfo->si_code);
1555 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1556 rec->NumberParameters = 1;
1557 /* no idea what meaning is actually behind this but that's what native does */
1558 rec->ExceptionInformation[0] = 0;
1559 break;
1560 default:
1561 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1562 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1563 break;
1568 /**********************************************************************
1569 * int_handler
1571 * Handler for SIGINT.
1573 * FIXME: should not be calling external functions on the signal stack.
1575 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1577 WORD fs, gs;
1578 init_handler( sigcontext, &fs, &gs );
1579 if (!dispatch_signal(SIGINT))
1581 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1582 rec->ExceptionCode = CONTROL_C_EXIT;
1586 /**********************************************************************
1587 * abrt_handler
1589 * Handler for SIGABRT.
1591 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1593 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1594 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1595 rec->ExceptionFlags = EH_NONCONTINUABLE;
1599 /**********************************************************************
1600 * quit_handler
1602 * Handler for SIGQUIT.
1604 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1606 WORD fs, gs;
1607 init_handler( sigcontext, &fs, &gs );
1608 abort_thread(0);
1612 /**********************************************************************
1613 * usr1_handler
1615 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1617 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1619 CONTEXT context;
1620 WORD fs, gs;
1622 init_handler( sigcontext, &fs, &gs );
1623 save_context( &context, sigcontext, fs, gs );
1624 wait_suspend( &context );
1625 restore_context( &context, sigcontext );
1629 /**********************************************************************
1630 * get_signal_stack_total_size
1632 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1633 * Must be a power of two.
1635 size_t get_signal_stack_total_size(void)
1637 if (!signal_stack_size)
1639 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1640 /* find the first power of two not smaller than min_size */
1641 while (size < min_size) size *= 2;
1642 signal_stack_mask = size - 1;
1643 signal_stack_size = size - teb_size;
1645 return signal_stack_size + teb_size;
1649 /***********************************************************************
1650 * __wine_set_signal_handler (NTDLL.@)
1652 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1654 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1655 if (handlers[sig] != NULL) return -2;
1656 handlers[sig] = wsh;
1657 return 0;
1661 /**********************************************************************
1662 * signal_init_thread
1664 void signal_init_thread( TEB *teb )
1666 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
1667 LDT_ENTRY fs_entry;
1668 stack_t ss;
1670 #ifdef __APPLE__
1671 int mib[2], val = 1;
1673 mib[0] = CTL_KERN;
1674 mib[1] = KERN_THALTSTACK;
1675 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1676 #endif
1678 ss.ss_sp = (char *)teb + teb_size;
1679 ss.ss_size = signal_stack_size;
1680 ss.ss_flags = 0;
1681 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
1683 wine_ldt_set_base( &fs_entry, teb );
1684 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
1685 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
1686 wine_ldt_init_fs( thread_data->fs, &fs_entry );
1687 thread_data->gs = wine_get_gs();
1690 /**********************************************************************
1691 * signal_init_process
1693 void signal_init_process(void)
1695 struct sigaction sig_act;
1697 sig_act.sa_mask = server_block_set;
1698 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1699 #ifdef SA_ONSTACK
1700 sig_act.sa_flags |= SA_ONSTACK;
1701 #endif
1703 sig_act.sa_sigaction = int_handler;
1704 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1705 sig_act.sa_sigaction = fpe_handler;
1706 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1707 sig_act.sa_sigaction = abrt_handler;
1708 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1709 sig_act.sa_sigaction = quit_handler;
1710 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1711 sig_act.sa_sigaction = usr1_handler;
1712 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1714 sig_act.sa_sigaction = segv_handler;
1715 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1716 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1717 #ifdef SIGBUS
1718 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1719 #endif
1721 #ifdef SIGTRAP
1722 sig_act.sa_sigaction = trap_handler;
1723 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1724 #endif
1726 #ifdef __HAVE_VM86
1727 sig_act.sa_sigaction = usr2_handler;
1728 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1729 #endif
1731 return;
1733 error:
1734 perror("sigaction");
1735 exit(1);
1739 #ifdef __HAVE_VM86
1740 /**********************************************************************
1741 * __wine_enter_vm86 (NTDLL.@)
1743 * Enter vm86 mode with the specified register context.
1745 void __wine_enter_vm86( CONTEXT *context )
1747 EXCEPTION_RECORD rec;
1748 int res;
1749 struct vm86plus_struct vm86;
1751 memset( &vm86, 0, sizeof(vm86) );
1752 for (;;)
1754 restore_vm86_context( context, &vm86 );
1756 ntdll_get_thread_data()->vm86_ptr = &vm86;
1757 merge_vm86_pending_flags( &rec );
1759 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1760 if (res < 0)
1762 errno = -res;
1763 return;
1766 save_vm86_context( context, &vm86 );
1768 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1769 rec.ExceptionRecord = NULL;
1770 rec.ExceptionAddress = (LPVOID)context->Eip;
1771 rec.NumberParameters = 0;
1773 switch(VM86_TYPE(res))
1775 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1776 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1777 break;
1778 case VM86_TRAP: /* return due to DOS-debugger request */
1779 switch(VM86_ARG(res))
1781 case TRAP_x86_TRCTRAP: /* Single-step exception */
1782 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1783 break;
1784 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1785 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1786 /* fall through */
1787 default:
1788 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1789 break;
1791 break;
1792 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1793 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1794 rec.NumberParameters = 1;
1795 rec.ExceptionInformation[0] = VM86_ARG(res);
1796 break;
1797 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1798 context->EFlags |= VIF_FLAG;
1799 context->EFlags &= ~VIP_FLAG;
1800 get_vm86_teb_info()->vm86_pending = 0;
1801 rec.ExceptionCode = EXCEPTION_VM86_STI;
1802 break;
1803 case VM86_PICRETURN: /* return due to pending PIC request */
1804 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1805 break;
1806 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1807 default:
1808 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1809 continue;
1811 __regs_RtlRaiseException( &rec, context );
1815 #else /* __HAVE_VM86 */
1816 /**********************************************************************
1817 * __wine_enter_vm86 (NTDLL.@)
1819 void __wine_enter_vm86( CONTEXT *context )
1821 MESSAGE("vm86 mode not supported on this platform\n");
1823 #endif /* __HAVE_VM86 */
1825 /**********************************************************************
1826 * DbgBreakPoint (NTDLL.@)
1828 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1830 /**********************************************************************
1831 * DbgUserBreakPoint (NTDLL.@)
1833 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1835 /**********************************************************************
1836 * NtCurrentTeb (NTDLL.@)
1838 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1841 /**********************************************************************
1842 * EXC_CallHandler (internal)
1844 * Some exception handlers depend on EBP to have a fixed position relative to
1845 * the exception frame.
1846 * Shrinker depends on (*1) doing what it does,
1847 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1848 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1849 * function calling the handler having only 5 parameters (*4).
1851 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1852 " pushl %ebp\n"
1853 " movl %esp, %ebp\n"
1854 " pushl %ebx\n"
1855 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1856 " pushl 24(%ebp)\n"
1857 " pushl 20(%ebp)\n"
1858 " pushl 16(%ebp)\n"
1859 " pushl 12(%ebp)\n"
1860 " pushl 8(%ebp)\n"
1861 " call " __ASM_NAME("call_exception_handler") "\n"
1862 " popl %ebx\n"
1863 " leave\n"
1864 " ret\n"
1866 __ASM_GLOBAL_FUNC(call_exception_handler,
1867 " pushl %ebp\n"
1868 " movl %esp, %ebp\n"
1869 " subl $12,%esp\n"
1870 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1871 " pushl %edx\n" /* handler be handled by... */
1872 " .byte 0x64\n"
1873 " pushl (0)\n" /* nested_handler (passed in edx). */
1874 " .byte 0x64\n"
1875 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1876 " pushl 20(%ebp)\n"
1877 " pushl 16(%ebp)\n"
1878 " pushl 12(%ebp)\n"
1879 " pushl 8(%ebp)\n"
1880 " movl 24(%ebp), %ecx\n" /* (*1) */
1881 " call *%ecx\n" /* call handler. (*2) */
1882 " .byte 0x64\n"
1883 " movl (0), %esp\n" /* restore previous... (*3) */
1884 " .byte 0x64\n"
1885 " popl (0)\n" /* exception frame. */
1886 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1887 " popl %ebp\n"
1888 " ret $20\n" /* (*4) */
1890 #endif /* __i386__ */