push 556f62bab943c37bb6cbad95a6ecb3d73e04a93f
[wine/hacks.git] / dlls / ntdll / signal_i386.c
blob502195f1c22c9e72b41873cd4fb00bbfa2c45bb4
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__)
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 /* __FreeBSD__ */
233 #ifdef __OpenBSD__
235 typedef struct sigcontext SIGCONTEXT;
237 #define EAX_sig(context) ((context)->sc_eax)
238 #define EBX_sig(context) ((context)->sc_ebx)
239 #define ECX_sig(context) ((context)->sc_ecx)
240 #define EDX_sig(context) ((context)->sc_edx)
241 #define ESI_sig(context) ((context)->sc_esi)
242 #define EDI_sig(context) ((context)->sc_edi)
243 #define EBP_sig(context) ((context)->sc_ebp)
245 #define CS_sig(context) ((context)->sc_cs)
246 #define DS_sig(context) ((context)->sc_ds)
247 #define ES_sig(context) ((context)->sc_es)
248 #define FS_sig(context) ((context)->sc_fs)
249 #define GS_sig(context) ((context)->sc_gs)
250 #define SS_sig(context) ((context)->sc_ss)
252 #define TRAP_sig(context) ((context)->sc_trapno)
253 #define ERROR_sig(context) ((context)->sc_err)
254 #define EFL_sig(context) ((context)->sc_eflags)
256 #define EIP_sig(context) ((context)->sc_eip)
257 #define ESP_sig(context) ((context)->sc_esp)
259 #define FPU_sig(context) NULL /* FIXME */
260 #define FPUX_sig(context) NULL /* FIXME */
262 #define T_MCHK T_MACHK
263 #define T_XMMFLT T_XFTRAP
265 #endif /* __OpenBSD__ */
267 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
269 #ifdef _SCO_DS
270 #include <sys/regset.h>
271 #endif
272 #include <sys/ucontext.h>
273 typedef struct ucontext SIGCONTEXT;
275 #ifdef _SCO_DS
276 #define gregs regs
277 #endif
279 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
280 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
281 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
282 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
283 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
284 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
285 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
287 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
288 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
289 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
290 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
292 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
293 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
295 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
297 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
298 #ifdef UESP
299 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
300 #elif defined(R_ESP)
301 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
302 #else
303 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
304 #endif
305 #ifdef ERR
306 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
307 #endif
308 #ifdef TRAPNO
309 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
310 #endif
312 #define FPU_sig(context) NULL /* FIXME */
313 #define FPUX_sig(context) NULL /* FIXME */
315 #endif /* svr4 || SCO_DS */
317 #ifdef __APPLE__
318 # include <sys/ucontext.h>
320 typedef ucontext_t SIGCONTEXT;
322 /* work around silly renaming of struct members in OS X 10.5 */
323 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
324 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
325 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
326 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
327 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
328 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
329 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
330 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
331 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
332 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
333 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
334 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
335 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
336 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
337 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
338 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
339 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
340 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
341 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
342 #define FPU_sig(context) NULL
343 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
344 #else
345 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
346 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
347 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
348 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
349 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
350 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
351 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
352 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
353 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
354 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
355 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
356 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
357 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
358 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
359 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
360 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
361 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
362 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
363 #define FPU_sig(context) NULL
364 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
365 #endif
367 #endif /* __APPLE__ */
369 #if defined(__NetBSD__)
370 # include <sys/ucontext.h>
371 # include <sys/types.h>
372 # include <signal.h>
374 typedef ucontext_t SIGCONTEXT;
376 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
377 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
378 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
379 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
380 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
381 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
382 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
383 #define ESP_sig(context) _UC_MACHINE_SP(context)
385 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
386 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
387 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
388 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
389 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
390 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
392 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
393 #define EIP_sig(context) _UC_MACHINE_PC(context)
394 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
395 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
397 #define FPU_sig(context) NULL
398 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
400 #define T_MCHK T_MCA
401 #define T_XMMFLT T_XMM
403 #endif /* __NetBSD__ */
405 WINE_DEFAULT_DEBUG_CHANNEL(seh);
407 typedef int (*wine_signal_handler)(unsigned int sig);
409 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
410 static size_t signal_stack_mask;
411 static size_t signal_stack_size;
413 static wine_signal_handler handlers[256];
415 static int fpux_support; /* whether the CPU support extended fpu context */
417 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
419 enum i386_trap_code
421 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
422 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
423 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
424 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
425 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
426 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
427 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
428 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
429 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
430 TRAP_x86_DNA = T_DNA, /* Device not available exception */
431 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
432 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
433 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
434 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
435 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
436 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
437 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
438 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
439 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
440 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
441 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
442 #else
443 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
444 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
445 TRAP_x86_NMI = 2, /* NMI interrupt */
446 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
447 TRAP_x86_OFLOW = 4, /* Overflow exception */
448 TRAP_x86_BOUND = 5, /* Bound range exception */
449 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
450 TRAP_x86_DNA = 7, /* Device not available exception */
451 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
452 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
453 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
454 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
455 TRAP_x86_STKFLT = 12, /* Stack fault */
456 TRAP_x86_PROTFLT = 13, /* General protection fault */
457 TRAP_x86_PAGEFLT = 14, /* Page fault */
458 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
459 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
460 TRAP_x86_MCHK = 18, /* Machine check exception */
461 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
462 otherwise Cache flush exception (via SIGSEV) */
463 #endif
467 /***********************************************************************
468 * dispatch_signal
470 static inline int dispatch_signal(unsigned int sig)
472 if (handlers[sig] == NULL) return 0;
473 return handlers[sig](sig);
477 /***********************************************************************
478 * get_trap_code
480 * Get the trap code for a signal.
482 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
484 #ifdef TRAP_sig
485 return TRAP_sig(sigcontext);
486 #else
487 return TRAP_x86_UNKNOWN; /* unknown trap code */
488 #endif
491 /***********************************************************************
492 * get_error_code
494 * Get the error code for a signal.
496 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
498 #ifdef ERROR_sig
499 return ERROR_sig(sigcontext);
500 #else
501 return 0;
502 #endif
505 /***********************************************************************
506 * get_signal_stack
508 * Get the base of the signal stack for the current thread.
510 static inline void *get_signal_stack(void)
512 return (char *)NtCurrentTeb() + 4096;
516 /***********************************************************************
517 * get_current_teb
519 * Get the current teb based on the stack pointer.
521 static inline TEB *get_current_teb(void)
523 unsigned long esp;
524 __asm__("movl %%esp,%0" : "=g" (esp) );
525 return (TEB *)(esp & ~signal_stack_mask);
529 #ifdef __HAVE_VM86
530 /***********************************************************************
531 * save_vm86_context
533 * Set the register values from a vm86 structure.
535 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
537 context->ContextFlags = CONTEXT_FULL;
538 context->Eax = vm86->regs.eax;
539 context->Ebx = vm86->regs.ebx;
540 context->Ecx = vm86->regs.ecx;
541 context->Edx = vm86->regs.edx;
542 context->Esi = vm86->regs.esi;
543 context->Edi = vm86->regs.edi;
544 context->Esp = vm86->regs.esp;
545 context->Ebp = vm86->regs.ebp;
546 context->Eip = vm86->regs.eip;
547 context->SegCs = vm86->regs.cs;
548 context->SegDs = vm86->regs.ds;
549 context->SegEs = vm86->regs.es;
550 context->SegFs = vm86->regs.fs;
551 context->SegGs = vm86->regs.gs;
552 context->SegSs = vm86->regs.ss;
553 context->EFlags = vm86->regs.eflags;
557 /***********************************************************************
558 * restore_vm86_context
560 * Build a vm86 structure from the register values.
562 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
564 vm86->regs.eax = context->Eax;
565 vm86->regs.ebx = context->Ebx;
566 vm86->regs.ecx = context->Ecx;
567 vm86->regs.edx = context->Edx;
568 vm86->regs.esi = context->Esi;
569 vm86->regs.edi = context->Edi;
570 vm86->regs.esp = context->Esp;
571 vm86->regs.ebp = context->Ebp;
572 vm86->regs.eip = context->Eip;
573 vm86->regs.cs = context->SegCs;
574 vm86->regs.ds = context->SegDs;
575 vm86->regs.es = context->SegEs;
576 vm86->regs.fs = context->SegFs;
577 vm86->regs.gs = context->SegGs;
578 vm86->regs.ss = context->SegSs;
579 vm86->regs.eflags = context->EFlags;
583 /**********************************************************************
584 * merge_vm86_pending_flags
586 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
587 * raises exception if there are pending events and VIF flag
588 * has been turned on.
590 * Called from __wine_enter_vm86 because vm86_enter
591 * doesn't check for pending events.
593 * Called from raise_vm86_sti_exception to check for
594 * pending events in a signal safe way.
596 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
598 BOOL check_pending = TRUE;
599 struct vm86plus_struct *vm86 =
600 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
603 * In order to prevent a race when SIGUSR2 occurs while
604 * we are returning from exception handler, pending events
605 * will be rechecked after each raised exception.
607 while (check_pending && get_vm86_teb_info()->vm86_pending)
609 check_pending = FALSE;
610 ntdll_get_thread_data()->vm86_ptr = NULL;
613 * If VIF is set, throw exception.
614 * Note that SIGUSR2 may turn VIF flag off so
615 * VIF check must occur only when TEB.vm86_ptr is NULL.
617 if (vm86->regs.eflags & VIF_FLAG)
619 CONTEXT vcontext;
620 save_vm86_context( &vcontext, vm86 );
622 rec->ExceptionCode = EXCEPTION_VM86_STI;
623 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
624 rec->ExceptionRecord = NULL;
625 rec->NumberParameters = 0;
626 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
628 vcontext.EFlags &= ~VIP_FLAG;
629 get_vm86_teb_info()->vm86_pending = 0;
630 __regs_RtlRaiseException( rec, &vcontext );
632 restore_vm86_context( &vcontext, vm86 );
633 check_pending = TRUE;
636 ntdll_get_thread_data()->vm86_ptr = vm86;
640 * Merge VIP flags in a signal safe way. This requires
641 * that the following operation compiles into atomic
642 * instruction.
644 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
646 #endif /* __HAVE_VM86 */
649 #ifdef __sun
651 /* We have to workaround two Solaris breakages:
652 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
653 * code crash badly.
654 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
655 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
658 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
659 __ASM_GLOBAL_FUNC( sigaction_syscall,
660 "movl $0x62,%eax\n\t"
661 "int $0x91\n\t"
662 "ret" )
664 /* assume the same libc handler is used for all signals */
665 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
667 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
669 struct ntdll_thread_data *thread_data;
671 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
673 thread_data = (struct ntdll_thread_data *)get_current_teb()->SystemReserved2;
674 wine_set_fs( thread_data->fs );
675 wine_set_gs( thread_data->gs );
677 libc_sigacthandler( signal, siginfo, sigcontext );
680 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
682 struct sigaction real_act;
684 if (sigaction( sig, new, old ) == -1) return -1;
686 /* retrieve the real handler and flags with a direct syscall */
687 sigaction_syscall( sig, NULL, &real_act );
688 libc_sigacthandler = real_act.sa_sigaction;
689 real_act.sa_sigaction = wine_sigacthandler;
690 sigaction_syscall( sig, &real_act, NULL );
691 return 0;
693 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
695 #endif
697 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
700 /***********************************************************************
701 * init_handler
703 * Handler initialization when the full context is not needed.
704 * Return the stack pointer to use for pushing the exception data.
706 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
708 TEB *teb = get_current_teb();
710 /* get %fs and %gs at time of the fault */
711 #ifdef FS_sig
712 *fs = LOWORD(FS_sig(sigcontext));
713 #else
714 *fs = wine_get_fs();
715 #endif
716 #ifdef GS_sig
717 *gs = LOWORD(GS_sig(sigcontext));
718 #else
719 *gs = wine_get_gs();
720 #endif
722 #ifndef __sun /* see above for Solaris handling */
724 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
725 wine_set_fs( thread_data->fs );
726 wine_set_gs( thread_data->gs );
728 #endif
730 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
731 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
734 * Win16 or DOS protected mode. Note that during switch
735 * from 16-bit mode to linear mode, CS may be set to system
736 * segment before FS is restored. Fortunately, in this case
737 * SS is still non-system segment. This is why both CS and SS
738 * are checked.
740 return teb->WOW32Reserved;
742 return (void *)(ESP_sig(sigcontext) & ~3);
746 /***********************************************************************
747 * save_fpu
749 * Save the thread FPU context.
751 static inline void save_fpu( CONTEXT *context )
753 #ifdef __GNUC__
754 context->ContextFlags |= CONTEXT_FLOATING_POINT;
755 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
756 #endif
760 /***********************************************************************
761 * save_fpux
763 * Save the thread FPU extended context.
765 static inline void save_fpux( CONTEXT *context )
767 #ifdef __GNUC__
768 /* we have to enforce alignment by hand */
769 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
770 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
772 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
773 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
774 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
775 #endif
779 /***********************************************************************
780 * restore_fpu
782 * Restore the FPU context to a sigcontext.
784 static inline void restore_fpu( const CONTEXT *context )
786 FLOATING_SAVE_AREA float_status = context->FloatSave;
787 /* reset the current interrupt status */
788 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
789 #ifdef __GNUC__
790 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
791 #endif /* __GNUC__ */
795 /***********************************************************************
796 * restore_fpux
798 * Restore the FPU extended context to a sigcontext.
800 static inline void restore_fpux( const CONTEXT *context )
802 #ifdef __GNUC__
803 /* we have to enforce alignment by hand */
804 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
805 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
807 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
808 /* reset the current interrupt status */
809 state->StatusWord &= state->ControlWord | 0xff80;
810 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
811 #endif
815 /***********************************************************************
816 * fpux_to_fpu
818 * Build a standard FPU context from an extended one.
820 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
822 unsigned int i, tag, stack_top;
824 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
825 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
826 fpu->ErrorOffset = fpux->ErrorOffset;
827 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
828 fpu->DataOffset = fpux->DataOffset;
829 fpu->DataSelector = fpux->DataSelector;
830 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
832 stack_top = (fpux->StatusWord >> 11) & 7;
833 fpu->TagWord = 0xffff0000;
834 for (i = 0; i < 8; i++)
836 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
837 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
838 else
840 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
841 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
843 tag = 2; /* special */
845 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
847 if (reg->Low) tag = 2; /* special */
848 else tag = 1; /* zero */
850 else
852 if (reg->Low >> 63) tag = 0; /* valid */
853 else tag = 2; /* special */
856 fpu->TagWord |= tag << (2 * i);
861 /***********************************************************************
862 * save_context
864 * Build a context structure from the signal info.
866 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
868 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
869 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
870 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
872 memset(context, 0, sizeof(*context));
873 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
874 context->Eax = EAX_sig(sigcontext);
875 context->Ebx = EBX_sig(sigcontext);
876 context->Ecx = ECX_sig(sigcontext);
877 context->Edx = EDX_sig(sigcontext);
878 context->Esi = ESI_sig(sigcontext);
879 context->Edi = EDI_sig(sigcontext);
880 context->Ebp = EBP_sig(sigcontext);
881 context->EFlags = EFL_sig(sigcontext);
882 context->Eip = EIP_sig(sigcontext);
883 context->Esp = ESP_sig(sigcontext);
884 context->SegCs = LOWORD(CS_sig(sigcontext));
885 context->SegDs = LOWORD(DS_sig(sigcontext));
886 context->SegEs = LOWORD(ES_sig(sigcontext));
887 context->SegFs = fs;
888 context->SegGs = gs;
889 context->SegSs = LOWORD(SS_sig(sigcontext));
890 context->Dr0 = regs->dr0;
891 context->Dr1 = regs->dr1;
892 context->Dr2 = regs->dr2;
893 context->Dr3 = regs->dr3;
894 context->Dr6 = regs->dr6;
895 context->Dr7 = regs->dr7;
897 if (fpu)
899 context->ContextFlags |= CONTEXT_FLOATING_POINT;
900 context->FloatSave = *fpu;
902 if (fpux)
904 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
905 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
906 fpux_support = 1;
907 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
909 if (!fpu && !fpux) save_fpu( context );
913 /***********************************************************************
914 * restore_context
916 * Restore the signal info from the context.
918 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
920 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
921 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
922 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
924 regs->dr0 = context->Dr0;
925 regs->dr1 = context->Dr1;
926 regs->dr2 = context->Dr2;
927 regs->dr3 = context->Dr3;
928 regs->dr6 = context->Dr6;
929 regs->dr7 = context->Dr7;
930 EAX_sig(sigcontext) = context->Eax;
931 EBX_sig(sigcontext) = context->Ebx;
932 ECX_sig(sigcontext) = context->Ecx;
933 EDX_sig(sigcontext) = context->Edx;
934 ESI_sig(sigcontext) = context->Esi;
935 EDI_sig(sigcontext) = context->Edi;
936 EBP_sig(sigcontext) = context->Ebp;
937 EFL_sig(sigcontext) = context->EFlags;
938 EIP_sig(sigcontext) = context->Eip;
939 ESP_sig(sigcontext) = context->Esp;
940 CS_sig(sigcontext) = context->SegCs;
941 DS_sig(sigcontext) = context->SegDs;
942 ES_sig(sigcontext) = context->SegEs;
943 SS_sig(sigcontext) = context->SegSs;
944 #ifdef GS_sig
945 GS_sig(sigcontext) = context->SegGs;
946 #else
947 wine_set_gs( context->SegGs );
948 #endif
949 #ifdef FS_sig
950 FS_sig(sigcontext) = context->SegFs;
951 #else
952 wine_set_fs( context->SegFs );
953 #endif
955 if (fpu) *fpu = context->FloatSave;
956 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
957 if (!fpu && !fpux) restore_fpu( context );
961 /***********************************************************************
962 * RtlCaptureContext (NTDLL.@)
964 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
966 *context = *regs;
967 if (fpux_support) save_fpux( context );
968 else save_fpu( context );
970 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
973 /***********************************************************************
974 * set_cpu_context
976 * Set the new CPU context. Used by NtSetContextThread.
978 void set_cpu_context( const CONTEXT *context )
980 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
982 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
983 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
985 if (flags & CONTEXT_DEBUG_REGISTERS)
987 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
988 regs->dr0 = context->Dr0;
989 regs->dr1 = context->Dr1;
990 regs->dr2 = context->Dr2;
991 regs->dr3 = context->Dr3;
992 regs->dr6 = context->Dr6;
993 regs->dr7 = context->Dr7;
995 if (flags & CONTEXT_FULL)
997 if (!(flags & CONTEXT_CONTROL))
998 FIXME( "setting partial context (%x) not supported\n", flags );
999 else if (flags & CONTEXT_SEGMENTS)
1000 __wine_call_from_32_restore_regs( context );
1001 else
1003 CONTEXT newcontext = *context;
1004 newcontext.SegDs = wine_get_ds();
1005 newcontext.SegEs = wine_get_es();
1006 newcontext.SegFs = wine_get_fs();
1007 newcontext.SegGs = wine_get_gs();
1008 __wine_call_from_32_restore_regs( &newcontext );
1014 /***********************************************************************
1015 * copy_context
1017 * Copy a register context according to the flags.
1019 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1021 flags &= ~CONTEXT_i386; /* get rid of CPU id */
1022 if (flags & CONTEXT_INTEGER)
1024 to->Eax = from->Eax;
1025 to->Ebx = from->Ebx;
1026 to->Ecx = from->Ecx;
1027 to->Edx = from->Edx;
1028 to->Esi = from->Esi;
1029 to->Edi = from->Edi;
1031 if (flags & CONTEXT_CONTROL)
1033 to->Ebp = from->Ebp;
1034 to->Esp = from->Esp;
1035 to->Eip = from->Eip;
1036 to->SegCs = from->SegCs;
1037 to->SegSs = from->SegSs;
1038 to->EFlags = from->EFlags;
1040 if (flags & CONTEXT_SEGMENTS)
1042 to->SegDs = from->SegDs;
1043 to->SegEs = from->SegEs;
1044 to->SegFs = from->SegFs;
1045 to->SegGs = from->SegGs;
1047 if (flags & CONTEXT_DEBUG_REGISTERS)
1049 to->Dr0 = from->Dr0;
1050 to->Dr1 = from->Dr1;
1051 to->Dr2 = from->Dr2;
1052 to->Dr3 = from->Dr3;
1053 to->Dr6 = from->Dr6;
1054 to->Dr7 = from->Dr7;
1056 if (flags & CONTEXT_FLOATING_POINT)
1058 to->FloatSave = from->FloatSave;
1060 if (flags & CONTEXT_EXTENDED_REGISTERS)
1062 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
1067 /***********************************************************************
1068 * is_privileged_instr
1070 * Check if the fault location is a privileged instruction.
1071 * Based on the instruction emulation code in dlls/kernel/instr.c.
1073 static inline DWORD is_privileged_instr( CONTEXT86 *context )
1075 const BYTE *instr;
1076 unsigned int prefix_count = 0;
1078 if (!wine_ldt_is_system( context->SegCs )) return 0;
1079 instr = (BYTE *)context->Eip;
1081 for (;;) switch(*instr)
1083 /* instruction prefixes */
1084 case 0x2e: /* %cs: */
1085 case 0x36: /* %ss: */
1086 case 0x3e: /* %ds: */
1087 case 0x26: /* %es: */
1088 case 0x64: /* %fs: */
1089 case 0x65: /* %gs: */
1090 case 0x66: /* opcode size */
1091 case 0x67: /* addr size */
1092 case 0xf0: /* lock */
1093 case 0xf2: /* repne */
1094 case 0xf3: /* repe */
1095 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1096 instr++;
1097 continue;
1099 case 0x0f: /* extended instruction */
1100 switch(instr[1])
1102 case 0x20: /* mov crX, reg */
1103 case 0x21: /* mov drX, reg */
1104 case 0x22: /* mov reg, crX */
1105 case 0x23: /* mov reg drX */
1106 return EXCEPTION_PRIV_INSTRUCTION;
1108 return 0;
1109 case 0x6c: /* insb (%dx) */
1110 case 0x6d: /* insl (%dx) */
1111 case 0x6e: /* outsb (%dx) */
1112 case 0x6f: /* outsl (%dx) */
1113 case 0xcd: /* int $xx */
1114 case 0xe4: /* inb al,XX */
1115 case 0xe5: /* in (e)ax,XX */
1116 case 0xe6: /* outb XX,al */
1117 case 0xe7: /* out XX,(e)ax */
1118 case 0xec: /* inb (%dx),%al */
1119 case 0xed: /* inl (%dx),%eax */
1120 case 0xee: /* outb %al,(%dx) */
1121 case 0xef: /* outl %eax,(%dx) */
1122 case 0xf4: /* hlt */
1123 case 0xfa: /* cli */
1124 case 0xfb: /* sti */
1125 return EXCEPTION_PRIV_INSTRUCTION;
1126 default:
1127 return 0;
1132 #include "pshpack1.h"
1133 struct atl_thunk
1135 DWORD movl; /* movl this,4(%esp) */
1136 DWORD this;
1137 BYTE jmp; /* jmp func */
1138 int func;
1140 #include "poppack.h"
1142 /**********************************************************************
1143 * check_atl_thunk
1145 * Check if code destination is an ATL thunk, and emulate it if so.
1147 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1149 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1150 BOOL ret = FALSE;
1152 __TRY
1154 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1156 *((DWORD *)context->Esp + 1) = thunk->this;
1157 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1158 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1159 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1160 ret = TRUE;
1163 __EXCEPT_PAGE_FAULT
1165 return FALSE;
1167 __ENDTRY
1168 return ret;
1172 /***********************************************************************
1173 * setup_exception_record
1175 * Setup the exception record and context on the thread stack.
1177 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1178 WORD fs, WORD gs, raise_func func )
1180 struct stack_layout
1182 void *ret_addr; /* return address from raise_func */
1183 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1184 CONTEXT *context_ptr; /* second arg for raise_func */
1185 CONTEXT context;
1186 EXCEPTION_RECORD rec;
1187 DWORD ebp;
1188 DWORD eip;
1189 } *stack = stack_ptr;
1190 DWORD exception_code = 0;
1192 /* stack sanity checks */
1194 if ((char *)stack >= (char *)get_signal_stack() &&
1195 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1197 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1198 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1199 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1200 NtCurrentTeb()->Tib.StackBase );
1201 abort_thread(1);
1204 if (stack - 1 > stack || /* check for overflow in subtraction */
1205 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1206 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1208 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1209 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1210 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1211 NtCurrentTeb()->Tib.StackBase );
1213 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1215 /* stack overflow on last page, unrecoverable */
1216 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1217 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1218 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1219 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1220 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1221 abort_thread(1);
1223 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1225 /* stack access below stack limit, may be recoverable */
1226 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1227 else
1229 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1230 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1231 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1232 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1233 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1234 abort_thread(1);
1238 stack--; /* push the stack_layout structure */
1239 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1240 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1241 #elif defined(VALGRIND_MAKE_WRITABLE)
1242 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1243 #endif
1244 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1245 stack->rec_ptr = &stack->rec;
1246 stack->context_ptr = &stack->context;
1248 stack->rec.ExceptionRecord = NULL;
1249 stack->rec.ExceptionCode = exception_code;
1250 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1251 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1252 stack->rec.NumberParameters = 0;
1254 save_context( &stack->context, sigcontext, fs, gs );
1256 /* now modify the sigcontext to return to the raise function */
1257 ESP_sig(sigcontext) = (DWORD)stack;
1258 EIP_sig(sigcontext) = (DWORD)func;
1259 /* clear single-step, direction, and align check flag */
1260 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1261 CS_sig(sigcontext) = wine_get_cs();
1262 DS_sig(sigcontext) = wine_get_ds();
1263 ES_sig(sigcontext) = wine_get_es();
1264 FS_sig(sigcontext) = wine_get_fs();
1265 GS_sig(sigcontext) = wine_get_gs();
1266 SS_sig(sigcontext) = wine_get_ss();
1268 return stack->rec_ptr;
1272 /***********************************************************************
1273 * setup_exception
1275 * Setup a proper stack frame for the raise function, and modify the
1276 * sigcontext so that the return from the signal handler will call
1277 * the raise function.
1279 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1281 WORD fs, gs;
1282 void *stack = init_handler( sigcontext, &fs, &gs );
1284 return setup_exception_record( sigcontext, stack, fs, gs, func );
1288 /***********************************************************************
1289 * get_exception_context
1291 * Get a pointer to the context built by setup_exception.
1293 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1295 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1299 /**********************************************************************
1300 * get_fpu_code
1302 * Get the FPU exception code from the FPU status.
1304 static inline DWORD get_fpu_code( const CONTEXT *context )
1306 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1308 if (status & 0x01) /* IE */
1310 if (status & 0x40) /* SF */
1311 return EXCEPTION_FLT_STACK_CHECK;
1312 else
1313 return EXCEPTION_FLT_INVALID_OPERATION;
1315 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1316 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1317 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1318 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1319 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1320 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1324 /**********************************************************************
1325 * raise_segv_exception
1327 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1329 switch(rec->ExceptionCode)
1331 case EXCEPTION_ACCESS_VIOLATION:
1332 if (rec->NumberParameters == 2)
1334 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1335 goto done;
1336 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1337 rec->ExceptionInformation[0] )))
1338 goto done;
1340 break;
1341 case EXCEPTION_DATATYPE_MISALIGNMENT:
1342 /* FIXME: pass through exception handler first? */
1343 if (context->EFlags & 0x00040000)
1345 /* Disable AC flag, return */
1346 context->EFlags &= ~0x00040000;
1347 goto done;
1349 break;
1351 __regs_RtlRaiseException( rec, context );
1352 done:
1353 NtSetContextThread( GetCurrentThread(), context );
1357 /**********************************************************************
1358 * raise_trap_exception
1360 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1362 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1364 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1366 /* when single stepping can't tell whether this is a hw bp or a
1367 * single step interrupt. try to avoid as much overhead as possible
1368 * and only do a server call if there is any hw bp enabled. */
1370 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1372 /* (possible) hardware breakpoint, fetch the debug registers */
1373 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1374 NtGetContextThread(GetCurrentThread(), context);
1375 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1378 context->EFlags &= ~0x100; /* clear single-step flag */
1381 __regs_RtlRaiseException( rec, context );
1382 NtSetContextThread( GetCurrentThread(), context );
1386 /**********************************************************************
1387 * raise_exception
1389 * Generic raise function for exceptions that don't need special treatment.
1391 static void WINAPI raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1393 __regs_RtlRaiseException( rec, context );
1394 NtSetContextThread( GetCurrentThread(), context );
1398 #ifdef __HAVE_VM86
1399 /**********************************************************************
1400 * raise_vm86_sti_exception
1402 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1404 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1405 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1407 if (ntdll_get_thread_data()->vm86_ptr)
1409 if (((char*)context->Eip >= (char*)vm86_return) &&
1410 ((char*)context->Eip <= (char*)vm86_return_end) &&
1411 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1412 /* exiting from VM86, can't throw */
1413 goto done;
1415 merge_vm86_pending_flags( rec );
1417 else if (get_vm86_teb_info()->dpmi_vif &&
1418 !wine_ldt_is_system(context->SegCs) &&
1419 !wine_ldt_is_system(context->SegSs))
1421 /* Executing DPMI code and virtual interrupts are enabled. */
1422 get_vm86_teb_info()->vm86_pending = 0;
1423 __regs_RtlRaiseException( rec, context );
1425 done:
1426 NtSetContextThread( GetCurrentThread(), context );
1430 /**********************************************************************
1431 * usr2_handler
1433 * Handler for SIGUSR2.
1434 * We use it to signal that the running __wine_enter_vm86() should
1435 * immediately set VIP_FLAG, causing pending events to be handled
1436 * as early as possible.
1438 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1440 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1441 rec->ExceptionCode = EXCEPTION_VM86_STI;
1443 #endif /* __HAVE_VM86 */
1446 /**********************************************************************
1447 * segv_handler
1449 * Handler for SIGSEGV and related errors.
1451 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1453 WORD fs, gs;
1454 EXCEPTION_RECORD *rec;
1455 SIGCONTEXT *context = sigcontext;
1456 void *stack = init_handler( sigcontext, &fs, &gs );
1458 /* check for page fault inside the thread stack */
1459 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1460 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1461 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1462 virtual_handle_stack_fault( siginfo->si_addr ))
1464 /* check if this was the last guard page */
1465 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1467 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1468 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1470 return;
1473 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1474 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1476 switch(get_trap_code(context))
1478 case TRAP_x86_OFLOW: /* Overflow exception */
1479 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1480 break;
1481 case TRAP_x86_BOUND: /* Bound range exception */
1482 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1483 break;
1484 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1485 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1486 break;
1487 case TRAP_x86_STKFLT: /* Stack fault */
1488 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1489 break;
1490 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1491 case TRAP_x86_PROTFLT: /* General protection fault */
1492 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1494 WORD err = get_error_code(context);
1495 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1496 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1497 rec->NumberParameters = 2;
1498 rec->ExceptionInformation[0] = 0;
1499 /* if error contains a LDT selector, use that as fault address */
1500 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1501 rec->ExceptionInformation[1] = err & ~7;
1502 else
1503 rec->ExceptionInformation[1] = 0xffffffff;
1505 break;
1506 case TRAP_x86_PAGEFLT: /* Page fault */
1507 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1508 rec->NumberParameters = 2;
1509 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1510 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1511 break;
1512 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1513 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1514 break;
1515 default:
1516 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1517 /* fall through */
1518 case TRAP_x86_NMI: /* NMI interrupt */
1519 case TRAP_x86_DNA: /* Device not available exception */
1520 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1521 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1522 case TRAP_x86_MCHK: /* Machine check exception */
1523 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1524 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1525 break;
1530 /**********************************************************************
1531 * trap_handler
1533 * Handler for SIGTRAP.
1535 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1537 SIGCONTEXT *context = sigcontext;
1538 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1540 switch(get_trap_code(context))
1542 case TRAP_x86_TRCTRAP: /* Single-step exception */
1543 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1544 break;
1545 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1546 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1547 /* fall through */
1548 default:
1549 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1550 break;
1555 /**********************************************************************
1556 * fpe_handler
1558 * Handler for SIGFPE.
1560 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1562 CONTEXT *win_context;
1563 SIGCONTEXT *context = sigcontext;
1564 EXCEPTION_RECORD *rec = setup_exception( context, raise_exception );
1566 win_context = get_exception_context( rec );
1568 switch(get_trap_code(context))
1570 case TRAP_x86_DIVIDE: /* Division by zero exception */
1571 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1572 break;
1573 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1574 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1575 break;
1576 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1577 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1578 rec->ExceptionCode = get_fpu_code( win_context );
1579 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
1580 break;
1581 case TRAP_x86_CACHEFLT: /* SIMD exception */
1582 /* TODO:
1583 * Behaviour only tested for divide-by-zero exceptions
1584 * Check for other SIMD exceptions as well */
1585 if(siginfo->si_code != FPE_FLTDIV)
1586 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1587 siginfo->si_code);
1589 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1590 rec->NumberParameters = 1;
1591 /* no idea what meaning is actually behind this but that's what native does */
1592 rec->ExceptionInformation[0] = 0;
1593 break;
1594 default:
1595 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1596 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1597 break;
1602 /**********************************************************************
1603 * int_handler
1605 * Handler for SIGINT.
1607 * FIXME: should not be calling external functions on the signal stack.
1609 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1611 WORD fs, gs;
1612 init_handler( sigcontext, &fs, &gs );
1613 if (!dispatch_signal(SIGINT))
1615 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1616 rec->ExceptionCode = CONTROL_C_EXIT;
1620 /**********************************************************************
1621 * abrt_handler
1623 * Handler for SIGABRT.
1625 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1627 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_exception );
1628 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1629 rec->ExceptionFlags = EH_NONCONTINUABLE;
1633 /**********************************************************************
1634 * quit_handler
1636 * Handler for SIGQUIT.
1638 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1640 WORD fs, gs;
1641 init_handler( sigcontext, &fs, &gs );
1642 abort_thread(0);
1646 /**********************************************************************
1647 * usr1_handler
1649 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1651 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1653 CONTEXT context;
1654 WORD fs, gs;
1656 init_handler( sigcontext, &fs, &gs );
1657 save_context( &context, sigcontext, fs, gs );
1658 wait_suspend( &context );
1659 restore_context( &context, sigcontext );
1663 /**********************************************************************
1664 * get_signal_stack_total_size
1666 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1667 * Must be a power of two.
1669 size_t get_signal_stack_total_size(void)
1671 if (!signal_stack_size)
1673 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1674 /* find the first power of two not smaller than min_size */
1675 while (size < min_size) size *= 2;
1676 signal_stack_mask = size - 1;
1677 signal_stack_size = size - teb_size;
1679 return signal_stack_size + teb_size;
1683 /***********************************************************************
1684 * __wine_set_signal_handler (NTDLL.@)
1686 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1688 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
1689 if (handlers[sig] != NULL) return -2;
1690 handlers[sig] = wsh;
1691 return 0;
1695 /**********************************************************************
1696 * signal_init_thread
1698 void signal_init_thread( TEB *teb )
1700 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
1701 LDT_ENTRY fs_entry;
1702 stack_t ss;
1704 #ifdef __APPLE__
1705 int mib[2], val = 1;
1707 mib[0] = CTL_KERN;
1708 mib[1] = KERN_THALTSTACK;
1709 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
1710 #endif
1712 ss.ss_sp = (char *)teb + teb_size;
1713 ss.ss_size = signal_stack_size;
1714 ss.ss_flags = 0;
1715 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
1717 wine_ldt_set_base( &fs_entry, teb );
1718 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
1719 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
1720 wine_ldt_init_fs( thread_data->fs, &fs_entry );
1721 thread_data->gs = wine_get_gs();
1724 /**********************************************************************
1725 * signal_init_process
1727 void signal_init_process(void)
1729 struct sigaction sig_act;
1731 sig_act.sa_mask = server_block_set;
1732 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1733 #ifdef SA_ONSTACK
1734 sig_act.sa_flags |= SA_ONSTACK;
1735 #endif
1737 sig_act.sa_sigaction = int_handler;
1738 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
1739 sig_act.sa_sigaction = fpe_handler;
1740 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
1741 sig_act.sa_sigaction = abrt_handler;
1742 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
1743 sig_act.sa_sigaction = quit_handler;
1744 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
1745 sig_act.sa_sigaction = usr1_handler;
1746 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
1748 sig_act.sa_sigaction = segv_handler;
1749 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
1750 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
1751 #ifdef SIGBUS
1752 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
1753 #endif
1755 #ifdef SIGTRAP
1756 sig_act.sa_sigaction = trap_handler;
1757 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
1758 #endif
1760 #ifdef __HAVE_VM86
1761 sig_act.sa_sigaction = usr2_handler;
1762 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
1763 #endif
1765 return;
1767 error:
1768 perror("sigaction");
1769 exit(1);
1773 #ifdef __HAVE_VM86
1774 /**********************************************************************
1775 * __wine_enter_vm86 (NTDLL.@)
1777 * Enter vm86 mode with the specified register context.
1779 void __wine_enter_vm86( CONTEXT *context )
1781 EXCEPTION_RECORD rec;
1782 int res;
1783 struct vm86plus_struct vm86;
1785 memset( &vm86, 0, sizeof(vm86) );
1786 for (;;)
1788 restore_vm86_context( context, &vm86 );
1790 ntdll_get_thread_data()->vm86_ptr = &vm86;
1791 merge_vm86_pending_flags( &rec );
1793 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1794 if (res < 0)
1796 errno = -res;
1797 return;
1800 save_vm86_context( context, &vm86 );
1802 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1803 rec.ExceptionRecord = NULL;
1804 rec.ExceptionAddress = (LPVOID)context->Eip;
1805 rec.NumberParameters = 0;
1807 switch(VM86_TYPE(res))
1809 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1810 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1811 break;
1812 case VM86_TRAP: /* return due to DOS-debugger request */
1813 switch(VM86_ARG(res))
1815 case TRAP_x86_TRCTRAP: /* Single-step exception */
1816 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1817 break;
1818 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1819 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
1820 /* fall through */
1821 default:
1822 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1823 break;
1825 break;
1826 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1827 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1828 rec.NumberParameters = 1;
1829 rec.ExceptionInformation[0] = VM86_ARG(res);
1830 break;
1831 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1832 context->EFlags |= VIF_FLAG;
1833 context->EFlags &= ~VIP_FLAG;
1834 get_vm86_teb_info()->vm86_pending = 0;
1835 rec.ExceptionCode = EXCEPTION_VM86_STI;
1836 break;
1837 case VM86_PICRETURN: /* return due to pending PIC request */
1838 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1839 break;
1840 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1841 default:
1842 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
1843 continue;
1845 __regs_RtlRaiseException( &rec, context );
1849 #else /* __HAVE_VM86 */
1850 /**********************************************************************
1851 * __wine_enter_vm86 (NTDLL.@)
1853 void __wine_enter_vm86( CONTEXT *context )
1855 MESSAGE("vm86 mode not supported on this platform\n");
1857 #endif /* __HAVE_VM86 */
1859 /**********************************************************************
1860 * DbgBreakPoint (NTDLL.@)
1862 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret")
1864 /**********************************************************************
1865 * DbgUserBreakPoint (NTDLL.@)
1867 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret")
1869 /**********************************************************************
1870 * NtCurrentTeb (NTDLL.@)
1872 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1875 /**********************************************************************
1876 * EXC_CallHandler (internal)
1878 * Some exception handlers depend on EBP to have a fixed position relative to
1879 * the exception frame.
1880 * Shrinker depends on (*1) doing what it does,
1881 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1882 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1883 * function calling the handler having only 5 parameters (*4).
1885 __ASM_GLOBAL_FUNC( EXC_CallHandler,
1886 " pushl %ebp\n"
1887 " movl %esp, %ebp\n"
1888 " pushl %ebx\n"
1889 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1890 " pushl 24(%ebp)\n"
1891 " pushl 20(%ebp)\n"
1892 " pushl 16(%ebp)\n"
1893 " pushl 12(%ebp)\n"
1894 " pushl 8(%ebp)\n"
1895 " call " __ASM_NAME("call_exception_handler") "\n"
1896 " popl %ebx\n"
1897 " leave\n"
1898 " ret\n"
1900 __ASM_GLOBAL_FUNC(call_exception_handler,
1901 " pushl %ebp\n"
1902 " movl %esp, %ebp\n"
1903 " subl $12,%esp\n"
1904 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1905 " pushl %edx\n" /* handler be handled by... */
1906 " .byte 0x64\n"
1907 " pushl (0)\n" /* nested_handler (passed in edx). */
1908 " .byte 0x64\n"
1909 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1910 " pushl 20(%ebp)\n"
1911 " pushl 16(%ebp)\n"
1912 " pushl 12(%ebp)\n"
1913 " pushl 8(%ebp)\n"
1914 " movl 24(%ebp), %ecx\n" /* (*1) */
1915 " call *%ecx\n" /* call handler. (*2) */
1916 " .byte 0x64\n"
1917 " movl (0), %esp\n" /* restore previous... (*3) */
1918 " .byte 0x64\n"
1919 " popl (0)\n" /* exception frame. */
1920 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1921 " popl %ebp\n"
1922 " ret $20\n" /* (*4) */
1924 #endif /* __i386__ */