push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / ntdll / signal_i386.c
blobff251fa3d0aac478c7ed37daa7fd1d8d3b9cca15
1 /*
2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifdef __i386__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYSCALL_H
40 # include <syscall.h>
41 #else
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
44 # endif
45 #endif
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
49 #endif
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
53 #endif
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
56 #endif
58 #include "ntstatus.h"
59 #define WIN32_NO_STATUS
60 #include "windef.h"
61 #include "wine/library.h"
62 #include "ntdll_misc.h"
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 #ifdef HAVE_VALGRIND_MEMCHECK_H
67 #include <valgrind/memcheck.h>
68 #endif
70 #undef ERR /* Solaris needs to define this */
72 /* not defined for x86, so copy the x86_64 definition */
73 typedef struct DECLSPEC_ALIGN(16) _M128A
75 ULONGLONG Low;
76 LONGLONG High;
77 } M128A;
79 typedef struct
81 WORD ControlWord;
82 WORD StatusWord;
83 BYTE TagWord;
84 BYTE Reserved1;
85 WORD ErrorOpcode;
86 DWORD ErrorOffset;
87 WORD ErrorSelector;
88 WORD Reserved2;
89 DWORD DataOffset;
90 WORD DataSelector;
91 WORD Reserved3;
92 DWORD MxCsr;
93 DWORD MxCsr_Mask;
94 M128A FloatRegisters[8];
95 M128A XmmRegisters[16];
96 BYTE Reserved4[96];
97 } XMM_SAVE_AREA32;
99 /***********************************************************************
100 * signal context platform-specific definitions
103 #ifdef linux
105 typedef ucontext_t SIGCONTEXT;
107 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
108 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
109 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
110 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
111 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
112 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
113 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
114 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
116 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
117 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
118 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
119 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
120 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
121 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
123 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
124 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
125 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
126 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
128 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
129 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
131 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
132 #define VIF_FLAG 0x00080000
133 #define VIP_FLAG 0x00100000
135 int vm86_enter( void **vm86_ptr );
136 void vm86_return(void);
137 void vm86_return_end(void);
138 __ASM_GLOBAL_FUNC(vm86_enter,
139 "pushl %ebp\n\t"
140 "movl %esp, %ebp\n\t"
141 "movl $166,%eax\n\t" /*SYS_vm86*/
142 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
143 "movl (%ecx),%ecx\n\t"
144 "pushl %ebx\n\t"
145 "movl $1,%ebx\n\t" /*VM86_ENTER*/
146 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
147 "pushl %fs\n\t"
148 "pushl %gs\n\t"
149 "int $0x80\n"
150 ".globl " __ASM_NAME("vm86_return") "\n\t"
151 __ASM_FUNC("vm86_return") "\n"
152 __ASM_NAME("vm86_return") ":\n\t"
153 "popl %gs\n\t"
154 "popl %fs\n\t"
155 "popl %ecx\n\t"
156 "popl %ebx\n\t"
157 "popl %ebp\n\t"
158 "testl %eax,%eax\n\t"
159 "jl 0f\n\t"
160 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
161 "je " __ASM_NAME("vm86_enter") "\n\t"
162 "0:\n\t"
163 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
164 "movl $0,(%ecx)\n\t"
165 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
166 __ASM_FUNC("vm86_return_end") "\n"
167 __ASM_NAME("vm86_return_end") ":\n\t"
168 "ret" )
170 #ifdef HAVE_SYS_VM86_H
171 # define __HAVE_VM86
172 #endif
174 #endif /* linux */
176 #ifdef BSDI
178 #include <machine/frame.h>
179 typedef struct trapframe SIGCONTEXT;
181 #define EAX_sig(context) ((context)->tf_eax)
182 #define EBX_sig(context) ((context)->tf_ebx)
183 #define ECX_sig(context) ((context)->tf_ecx)
184 #define EDX_sig(context) ((context)->tf_edx)
185 #define ESI_sig(context) ((context)->tf_esi)
186 #define EDI_sig(context) ((context)->tf_edi)
187 #define EBP_sig(context) ((context)->tf_ebp)
189 #define CS_sig(context) ((context)->tf_cs)
190 #define DS_sig(context) ((context)->tf_ds)
191 #define ES_sig(context) ((context)->tf_es)
192 #define SS_sig(context) ((context)->tf_ss)
194 #define EFL_sig(context) ((context)->tf_eflags)
196 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
197 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
199 #define FPU_sig(context) NULL /* FIXME */
200 #define FPUX_sig(context) NULL /* FIXME */
202 #endif /* bsdi */
204 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
206 typedef struct sigcontext SIGCONTEXT;
208 #define EAX_sig(context) ((context)->sc_eax)
209 #define EBX_sig(context) ((context)->sc_ebx)
210 #define ECX_sig(context) ((context)->sc_ecx)
211 #define EDX_sig(context) ((context)->sc_edx)
212 #define ESI_sig(context) ((context)->sc_esi)
213 #define EDI_sig(context) ((context)->sc_edi)
214 #define EBP_sig(context) ((context)->sc_ebp)
216 #define CS_sig(context) ((context)->sc_cs)
217 #define DS_sig(context) ((context)->sc_ds)
218 #define ES_sig(context) ((context)->sc_es)
219 #define FS_sig(context) ((context)->sc_fs)
220 #define GS_sig(context) ((context)->sc_gs)
221 #define SS_sig(context) ((context)->sc_ss)
223 #define TRAP_sig(context) ((context)->sc_trapno)
224 #define ERROR_sig(context) ((context)->sc_err)
225 #define EFL_sig(context) ((context)->sc_eflags)
227 #define EIP_sig(context) ((context)->sc_eip)
228 #define ESP_sig(context) ((context)->sc_esp)
230 #define FPU_sig(context) NULL /* FIXME */
231 #define FPUX_sig(context) NULL /* FIXME */
233 #endif /* __FreeBSD__ */
235 #ifdef __OpenBSD__
237 typedef struct sigcontext SIGCONTEXT;
239 #define EAX_sig(context) ((context)->sc_eax)
240 #define EBX_sig(context) ((context)->sc_ebx)
241 #define ECX_sig(context) ((context)->sc_ecx)
242 #define EDX_sig(context) ((context)->sc_edx)
243 #define ESI_sig(context) ((context)->sc_esi)
244 #define EDI_sig(context) ((context)->sc_edi)
245 #define EBP_sig(context) ((context)->sc_ebp)
247 #define CS_sig(context) ((context)->sc_cs)
248 #define DS_sig(context) ((context)->sc_ds)
249 #define ES_sig(context) ((context)->sc_es)
250 #define FS_sig(context) ((context)->sc_fs)
251 #define GS_sig(context) ((context)->sc_gs)
252 #define SS_sig(context) ((context)->sc_ss)
254 #define TRAP_sig(context) ((context)->sc_trapno)
255 #define ERROR_sig(context) ((context)->sc_err)
256 #define EFL_sig(context) ((context)->sc_eflags)
258 #define EIP_sig(context) ((context)->sc_eip)
259 #define ESP_sig(context) ((context)->sc_esp)
261 #define FPU_sig(context) NULL /* FIXME */
262 #define FPUX_sig(context) NULL /* FIXME */
264 #define T_MCHK T_MACHK
265 #define T_XMMFLT T_XFTRAP
267 #endif /* __OpenBSD__ */
269 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
271 #ifdef _SCO_DS
272 #include <sys/regset.h>
273 #endif
274 #include <sys/ucontext.h>
275 typedef struct ucontext SIGCONTEXT;
277 #ifdef _SCO_DS
278 #define gregs regs
279 #endif
281 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
282 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
283 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
284 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
285 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
286 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
287 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
289 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
290 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
291 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
292 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
294 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
295 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
297 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
299 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
300 #ifdef UESP
301 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
302 #elif defined(R_ESP)
303 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
304 #else
305 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
306 #endif
307 #ifdef ERR
308 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
309 #endif
310 #ifdef TRAPNO
311 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
312 #endif
314 #define FPU_sig(context) NULL /* FIXME */
315 #define FPUX_sig(context) NULL /* FIXME */
317 #endif /* svr4 || SCO_DS */
319 #ifdef __APPLE__
320 # include <sys/ucontext.h>
322 typedef ucontext_t SIGCONTEXT;
324 /* work around silly renaming of struct members in OS X 10.5 */
325 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
326 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
327 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
328 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
329 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
330 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
331 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
332 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
333 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
334 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
335 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
336 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
337 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
338 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
339 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
340 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
341 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
342 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
343 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
344 #define FPU_sig(context) NULL
345 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
346 #else
347 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
348 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
349 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
350 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
351 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
352 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
353 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
354 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
355 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
356 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
357 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
358 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
359 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
360 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
361 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
362 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
363 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
364 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
365 #define FPU_sig(context) NULL
366 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
367 #endif
369 #endif /* __APPLE__ */
371 #if defined(__NetBSD__)
372 # include <sys/ucontext.h>
373 # include <sys/types.h>
374 # include <signal.h>
376 typedef ucontext_t SIGCONTEXT;
378 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
379 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
380 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
381 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
382 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
383 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
384 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
385 #define ESP_sig(context) _UC_MACHINE_SP(context)
387 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
388 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
389 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
390 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
391 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
392 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
394 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
395 #define EIP_sig(context) _UC_MACHINE_PC(context)
396 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
397 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
399 #define FPU_sig(context) NULL
400 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
402 #define T_MCHK T_MCA
403 #define T_XMMFLT T_XMM
405 #endif /* __NetBSD__ */
407 WINE_DEFAULT_DEBUG_CHANNEL(seh);
409 typedef int (*wine_signal_handler)(unsigned int sig);
411 static const size_t teb_size = 4096; /* we reserve one page for the TEB */
412 static size_t signal_stack_mask;
413 static size_t signal_stack_size;
415 static wine_signal_handler handlers[256];
417 static int fpux_support; /* whether the CPU support extended fpu context */
419 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( const CONTEXT *context );
421 enum i386_trap_code
423 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
424 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
425 TRAP_x86_DIVIDE = T_DIVIDE, /* Division by zero exception */
426 TRAP_x86_TRCTRAP = T_TRCTRAP, /* Single-step exception */
427 TRAP_x86_NMI = T_NMI, /* NMI interrupt */
428 TRAP_x86_BPTFLT = T_BPTFLT, /* Breakpoint exception */
429 TRAP_x86_OFLOW = T_OFLOW, /* Overflow exception */
430 TRAP_x86_BOUND = T_BOUND, /* Bound range exception */
431 TRAP_x86_PRIVINFLT = T_PRIVINFLT, /* Invalid opcode exception */
432 TRAP_x86_DNA = T_DNA, /* Device not available exception */
433 TRAP_x86_DOUBLEFLT = T_DOUBLEFLT, /* Double fault exception */
434 TRAP_x86_FPOPFLT = T_FPOPFLT, /* Coprocessor segment overrun */
435 TRAP_x86_TSSFLT = T_TSSFLT, /* Invalid TSS exception */
436 TRAP_x86_SEGNPFLT = T_SEGNPFLT, /* Segment not present exception */
437 TRAP_x86_STKFLT = T_STKFLT, /* Stack fault */
438 TRAP_x86_PROTFLT = T_PROTFLT, /* General protection fault */
439 TRAP_x86_PAGEFLT = T_PAGEFLT, /* Page fault */
440 TRAP_x86_ARITHTRAP = T_ARITHTRAP, /* Floating point exception */
441 TRAP_x86_ALIGNFLT = T_ALIGNFLT, /* Alignment check exception */
442 TRAP_x86_MCHK = T_MCHK, /* Machine check exception */
443 TRAP_x86_CACHEFLT = T_XMMFLT /* Cache flush exception */
444 #else
445 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
446 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
447 TRAP_x86_NMI = 2, /* NMI interrupt */
448 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
449 TRAP_x86_OFLOW = 4, /* Overflow exception */
450 TRAP_x86_BOUND = 5, /* Bound range exception */
451 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
452 TRAP_x86_DNA = 7, /* Device not available exception */
453 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
454 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
455 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
456 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
457 TRAP_x86_STKFLT = 12, /* Stack fault */
458 TRAP_x86_PROTFLT = 13, /* General protection fault */
459 TRAP_x86_PAGEFLT = 14, /* Page fault */
460 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
461 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
462 TRAP_x86_MCHK = 18, /* Machine check exception */
463 TRAP_x86_CACHEFLT = 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
464 otherwise Cache flush exception (via SIGSEV) */
465 #endif
468 /* Exception record for handling exceptions happening inside exception handlers */
469 typedef struct
471 EXCEPTION_REGISTRATION_RECORD frame;
472 EXCEPTION_REGISTRATION_RECORD *prevFrame;
473 } EXC_NESTED_FRAME;
475 extern DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
476 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
477 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler );
479 /***********************************************************************
480 * dispatch_signal
482 static inline int dispatch_signal(unsigned int sig)
484 if (handlers[sig] == NULL) return 0;
485 return handlers[sig](sig);
489 /***********************************************************************
490 * get_trap_code
492 * Get the trap code for a signal.
494 static inline enum i386_trap_code get_trap_code( const SIGCONTEXT *sigcontext )
496 #ifdef TRAP_sig
497 return TRAP_sig(sigcontext);
498 #else
499 return TRAP_x86_UNKNOWN; /* unknown trap code */
500 #endif
503 /***********************************************************************
504 * get_error_code
506 * Get the error code for a signal.
508 static inline WORD get_error_code( const SIGCONTEXT *sigcontext )
510 #ifdef ERROR_sig
511 return ERROR_sig(sigcontext);
512 #else
513 return 0;
514 #endif
517 /***********************************************************************
518 * get_signal_stack
520 * Get the base of the signal stack for the current thread.
522 static inline void *get_signal_stack(void)
524 return (char *)NtCurrentTeb() + 4096;
528 /***********************************************************************
529 * get_current_teb
531 * Get the current teb based on the stack pointer.
533 static inline TEB *get_current_teb(void)
535 unsigned long esp;
536 __asm__("movl %%esp,%0" : "=g" (esp) );
537 return (TEB *)(esp & ~signal_stack_mask);
541 /*******************************************************************
542 * raise_handler
544 * Handler for exceptions happening inside a handler.
546 static DWORD raise_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
547 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
549 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
550 return ExceptionContinueSearch;
551 /* We shouldn't get here so we store faulty frame in dispatcher */
552 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
553 return ExceptionNestedException;
557 /*******************************************************************
558 * unwind_handler
560 * Handler for exceptions happening inside an unwind handler.
562 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
563 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
565 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
566 return ExceptionContinueSearch;
567 /* We shouldn't get here so we store faulty frame in dispatcher */
568 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
569 return ExceptionCollidedUnwind;
573 /**********************************************************************
574 * call_stack_handlers
576 * Call the stack handlers chain.
578 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
580 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
581 DWORD res;
583 frame = NtCurrentTeb()->Tib.ExceptionList;
584 nested_frame = NULL;
585 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
587 /* Check frame address */
588 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
589 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
590 (ULONG_PTR)frame & 3)
592 rec->ExceptionFlags |= EH_STACK_INVALID;
593 break;
596 /* Call handler */
597 TRACE( "calling handler at %p code=%x flags=%x\n",
598 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
599 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, raise_handler );
600 TRACE( "handler at %p returned %x\n", frame->Handler, res );
602 if (frame == nested_frame)
604 /* no longer nested */
605 nested_frame = NULL;
606 rec->ExceptionFlags &= ~EH_NESTED_CALL;
609 switch(res)
611 case ExceptionContinueExecution:
612 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
613 return STATUS_NONCONTINUABLE_EXCEPTION;
614 case ExceptionContinueSearch:
615 break;
616 case ExceptionNestedException:
617 if (nested_frame < dispatch) nested_frame = dispatch;
618 rec->ExceptionFlags |= EH_NESTED_CALL;
619 break;
620 default:
621 return STATUS_INVALID_DISPOSITION;
623 frame = frame->Prev;
625 return STATUS_UNHANDLED_EXCEPTION;
629 /*******************************************************************
630 * raise_exception
632 * Implementation of NtRaiseException.
634 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
636 NTSTATUS status;
638 if (first_chance)
640 DWORD c;
642 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
643 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
644 context->Eip, GetCurrentThreadId() );
645 for (c = 0; c < rec->NumberParameters; c++)
646 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
647 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
649 if (rec->ExceptionInformation[1] >> 16)
650 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
651 rec->ExceptionAddress,
652 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
653 else
654 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
655 rec->ExceptionAddress,
656 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
658 else
660 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
661 context->Eax, context->Ebx, context->Ecx,
662 context->Edx, context->Esi, context->Edi );
663 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
664 context->Ebp, context->Esp, context->SegCs, context->SegDs,
665 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
667 status = send_debug_event( rec, TRUE, context );
668 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
669 return STATUS_SUCCESS;
671 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
672 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Eip--;
674 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
675 return STATUS_SUCCESS;
677 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
678 return status;
681 /* last chance exception */
683 status = send_debug_event( rec, FALSE, context );
684 if (status != DBG_CONTINUE)
686 if (rec->ExceptionFlags & EH_STACK_INVALID)
687 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
688 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
689 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
690 else
691 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
692 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
693 NtTerminateProcess( NtCurrentProcess(), 1 );
695 return STATUS_SUCCESS;
699 #ifdef __HAVE_VM86
700 /***********************************************************************
701 * save_vm86_context
703 * Set the register values from a vm86 structure.
705 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
707 context->ContextFlags = CONTEXT_FULL;
708 context->Eax = vm86->regs.eax;
709 context->Ebx = vm86->regs.ebx;
710 context->Ecx = vm86->regs.ecx;
711 context->Edx = vm86->regs.edx;
712 context->Esi = vm86->regs.esi;
713 context->Edi = vm86->regs.edi;
714 context->Esp = vm86->regs.esp;
715 context->Ebp = vm86->regs.ebp;
716 context->Eip = vm86->regs.eip;
717 context->SegCs = vm86->regs.cs;
718 context->SegDs = vm86->regs.ds;
719 context->SegEs = vm86->regs.es;
720 context->SegFs = vm86->regs.fs;
721 context->SegGs = vm86->regs.gs;
722 context->SegSs = vm86->regs.ss;
723 context->EFlags = vm86->regs.eflags;
727 /***********************************************************************
728 * restore_vm86_context
730 * Build a vm86 structure from the register values.
732 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
734 vm86->regs.eax = context->Eax;
735 vm86->regs.ebx = context->Ebx;
736 vm86->regs.ecx = context->Ecx;
737 vm86->regs.edx = context->Edx;
738 vm86->regs.esi = context->Esi;
739 vm86->regs.edi = context->Edi;
740 vm86->regs.esp = context->Esp;
741 vm86->regs.ebp = context->Ebp;
742 vm86->regs.eip = context->Eip;
743 vm86->regs.cs = context->SegCs;
744 vm86->regs.ds = context->SegDs;
745 vm86->regs.es = context->SegEs;
746 vm86->regs.fs = context->SegFs;
747 vm86->regs.gs = context->SegGs;
748 vm86->regs.ss = context->SegSs;
749 vm86->regs.eflags = context->EFlags;
753 /**********************************************************************
754 * merge_vm86_pending_flags
756 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
757 * raises exception if there are pending events and VIF flag
758 * has been turned on.
760 * Called from __wine_enter_vm86 because vm86_enter
761 * doesn't check for pending events.
763 * Called from raise_vm86_sti_exception to check for
764 * pending events in a signal safe way.
766 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
768 BOOL check_pending = TRUE;
769 struct vm86plus_struct *vm86 =
770 (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
773 * In order to prevent a race when SIGUSR2 occurs while
774 * we are returning from exception handler, pending events
775 * will be rechecked after each raised exception.
777 while (check_pending && get_vm86_teb_info()->vm86_pending)
779 check_pending = FALSE;
780 ntdll_get_thread_data()->vm86_ptr = NULL;
783 * If VIF is set, throw exception.
784 * Note that SIGUSR2 may turn VIF flag off so
785 * VIF check must occur only when TEB.vm86_ptr is NULL.
787 if (vm86->regs.eflags & VIF_FLAG)
789 CONTEXT vcontext;
790 save_vm86_context( &vcontext, vm86 );
792 rec->ExceptionCode = EXCEPTION_VM86_STI;
793 rec->ExceptionFlags = EXCEPTION_CONTINUABLE;
794 rec->ExceptionRecord = NULL;
795 rec->NumberParameters = 0;
796 rec->ExceptionAddress = (LPVOID)vcontext.Eip;
798 vcontext.EFlags &= ~VIP_FLAG;
799 get_vm86_teb_info()->vm86_pending = 0;
800 raise_exception( rec, &vcontext, TRUE );
802 restore_vm86_context( &vcontext, vm86 );
803 check_pending = TRUE;
806 ntdll_get_thread_data()->vm86_ptr = vm86;
810 * Merge VIP flags in a signal safe way. This requires
811 * that the following operation compiles into atomic
812 * instruction.
814 vm86->regs.eflags |= get_vm86_teb_info()->vm86_pending;
816 #endif /* __HAVE_VM86 */
819 #ifdef __sun
821 /* We have to workaround two Solaris breakages:
822 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
823 * code crash badly.
824 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
825 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
828 extern int sigaction_syscall( int sig, const struct sigaction *new, struct sigaction *old );
829 __ASM_GLOBAL_FUNC( sigaction_syscall,
830 "movl $0x62,%eax\n\t"
831 "int $0x91\n\t"
832 "ret" )
834 /* assume the same libc handler is used for all signals */
835 static void (*libc_sigacthandler)( int signal, siginfo_t *siginfo, void *context );
837 static void wine_sigacthandler( int signal, siginfo_t *siginfo, void *sigcontext )
839 struct ntdll_thread_data *thread_data;
841 __asm__ __volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
843 thread_data = (struct ntdll_thread_data *)get_current_teb()->SystemReserved2;
844 wine_set_fs( thread_data->fs );
845 wine_set_gs( thread_data->gs );
847 libc_sigacthandler( signal, siginfo, sigcontext );
850 static int solaris_sigaction( int sig, const struct sigaction *new, struct sigaction *old )
852 struct sigaction real_act;
854 if (sigaction( sig, new, old ) == -1) return -1;
856 /* retrieve the real handler and flags with a direct syscall */
857 sigaction_syscall( sig, NULL, &real_act );
858 libc_sigacthandler = real_act.sa_sigaction;
859 real_act.sa_sigaction = wine_sigacthandler;
860 sigaction_syscall( sig, &real_act, NULL );
861 return 0;
863 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
865 #endif
867 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
870 /***********************************************************************
871 * init_handler
873 * Handler initialization when the full context is not needed.
874 * Return the stack pointer to use for pushing the exception data.
876 static inline void *init_handler( const SIGCONTEXT *sigcontext, WORD *fs, WORD *gs )
878 TEB *teb = get_current_teb();
880 /* get %fs and %gs at time of the fault */
881 #ifdef FS_sig
882 *fs = LOWORD(FS_sig(sigcontext));
883 #else
884 *fs = wine_get_fs();
885 #endif
886 #ifdef GS_sig
887 *gs = LOWORD(GS_sig(sigcontext));
888 #else
889 *gs = wine_get_gs();
890 #endif
892 #ifndef __sun /* see above for Solaris handling */
894 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
895 wine_set_fs( thread_data->fs );
896 wine_set_gs( thread_data->gs );
898 #endif
900 if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
901 !wine_ldt_is_system(SS_sig(sigcontext))) /* 16-bit mode */
904 * Win16 or DOS protected mode. Note that during switch
905 * from 16-bit mode to linear mode, CS may be set to system
906 * segment before FS is restored. Fortunately, in this case
907 * SS is still non-system segment. This is why both CS and SS
908 * are checked.
910 return teb->WOW32Reserved;
912 return (void *)(ESP_sig(sigcontext) & ~3);
916 /***********************************************************************
917 * save_fpu
919 * Save the thread FPU context.
921 static inline void save_fpu( CONTEXT *context )
923 #ifdef __GNUC__
924 context->ContextFlags |= CONTEXT_FLOATING_POINT;
925 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
926 #endif
930 /***********************************************************************
931 * save_fpux
933 * Save the thread FPU extended context.
935 static inline void save_fpux( CONTEXT *context )
937 #ifdef __GNUC__
938 /* we have to enforce alignment by hand */
939 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
940 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
942 __asm__ __volatile__( "fxsave %0" : "=m" (*state) );
943 context->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
944 memcpy( context->ExtendedRegisters, state, sizeof(*state) );
945 #endif
949 /***********************************************************************
950 * restore_fpu
952 * Restore the FPU context to a sigcontext.
954 static inline void restore_fpu( const CONTEXT *context )
956 FLOATING_SAVE_AREA float_status = context->FloatSave;
957 /* reset the current interrupt status */
958 float_status.StatusWord &= float_status.ControlWord | 0xffffff80;
959 #ifdef __GNUC__
960 __asm__ __volatile__( "frstor %0; fwait" : : "m" (float_status) );
961 #endif /* __GNUC__ */
965 /***********************************************************************
966 * restore_fpux
968 * Restore the FPU extended context to a sigcontext.
970 static inline void restore_fpux( const CONTEXT *context )
972 #ifdef __GNUC__
973 /* we have to enforce alignment by hand */
974 char buffer[sizeof(XMM_SAVE_AREA32) + 16];
975 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
977 memcpy( state, context->ExtendedRegisters, sizeof(*state) );
978 /* reset the current interrupt status */
979 state->StatusWord &= state->ControlWord | 0xff80;
980 __asm__ __volatile__( "fxrstor %0" : : "m" (*state) );
981 #endif
985 /***********************************************************************
986 * fpux_to_fpu
988 * Build a standard FPU context from an extended one.
990 static void fpux_to_fpu( FLOATING_SAVE_AREA *fpu, const XMM_SAVE_AREA32 *fpux )
992 unsigned int i, tag, stack_top;
994 fpu->ControlWord = fpux->ControlWord | 0xffff0000;
995 fpu->StatusWord = fpux->StatusWord | 0xffff0000;
996 fpu->ErrorOffset = fpux->ErrorOffset;
997 fpu->ErrorSelector = fpux->ErrorSelector | (fpux->ErrorOpcode << 16);
998 fpu->DataOffset = fpux->DataOffset;
999 fpu->DataSelector = fpux->DataSelector;
1000 fpu->Cr0NpxState = fpux->StatusWord | 0xffff0000;
1002 stack_top = (fpux->StatusWord >> 11) & 7;
1003 fpu->TagWord = 0xffff0000;
1004 for (i = 0; i < 8; i++)
1006 memcpy( &fpu->RegisterArea[10 * i], &fpux->FloatRegisters[i], 10 );
1007 if (!(fpux->TagWord & (1 << i))) tag = 3; /* empty */
1008 else
1010 const M128A *reg = &fpux->FloatRegisters[(i - stack_top) & 7];
1011 if ((reg->High & 0x7fff) == 0x7fff) /* exponent all ones */
1013 tag = 2; /* special */
1015 else if (!(reg->High & 0x7fff)) /* exponent all zeroes */
1017 if (reg->Low) tag = 2; /* special */
1018 else tag = 1; /* zero */
1020 else
1022 if (reg->Low >> 63) tag = 0; /* valid */
1023 else tag = 2; /* special */
1026 fpu->TagWord |= tag << (2 * i);
1031 /***********************************************************************
1032 * save_context
1034 * Build a context structure from the signal info.
1036 static inline void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext, WORD fs, WORD gs )
1038 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1039 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1040 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1042 memset(context, 0, sizeof(*context));
1043 context->ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
1044 context->Eax = EAX_sig(sigcontext);
1045 context->Ebx = EBX_sig(sigcontext);
1046 context->Ecx = ECX_sig(sigcontext);
1047 context->Edx = EDX_sig(sigcontext);
1048 context->Esi = ESI_sig(sigcontext);
1049 context->Edi = EDI_sig(sigcontext);
1050 context->Ebp = EBP_sig(sigcontext);
1051 context->EFlags = EFL_sig(sigcontext);
1052 context->Eip = EIP_sig(sigcontext);
1053 context->Esp = ESP_sig(sigcontext);
1054 context->SegCs = LOWORD(CS_sig(sigcontext));
1055 context->SegDs = LOWORD(DS_sig(sigcontext));
1056 context->SegEs = LOWORD(ES_sig(sigcontext));
1057 context->SegFs = fs;
1058 context->SegGs = gs;
1059 context->SegSs = LOWORD(SS_sig(sigcontext));
1060 context->Dr0 = regs->dr0;
1061 context->Dr1 = regs->dr1;
1062 context->Dr2 = regs->dr2;
1063 context->Dr3 = regs->dr3;
1064 context->Dr6 = regs->dr6;
1065 context->Dr7 = regs->dr7;
1067 if (fpu)
1069 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1070 context->FloatSave = *fpu;
1072 if (fpux)
1074 context->ContextFlags |= CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS;
1075 memcpy( context->ExtendedRegisters, fpux, sizeof(*fpux) );
1076 fpux_support = 1;
1077 if (!fpu) fpux_to_fpu( &context->FloatSave, fpux );
1079 if (!fpu && !fpux) save_fpu( context );
1083 /***********************************************************************
1084 * restore_context
1086 * Restore the signal info from the context.
1088 static inline void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
1090 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1091 FLOATING_SAVE_AREA *fpu = FPU_sig(sigcontext);
1092 XMM_SAVE_AREA32 *fpux = FPUX_sig(sigcontext);
1094 regs->dr0 = context->Dr0;
1095 regs->dr1 = context->Dr1;
1096 regs->dr2 = context->Dr2;
1097 regs->dr3 = context->Dr3;
1098 regs->dr6 = context->Dr6;
1099 regs->dr7 = context->Dr7;
1100 EAX_sig(sigcontext) = context->Eax;
1101 EBX_sig(sigcontext) = context->Ebx;
1102 ECX_sig(sigcontext) = context->Ecx;
1103 EDX_sig(sigcontext) = context->Edx;
1104 ESI_sig(sigcontext) = context->Esi;
1105 EDI_sig(sigcontext) = context->Edi;
1106 EBP_sig(sigcontext) = context->Ebp;
1107 EFL_sig(sigcontext) = context->EFlags;
1108 EIP_sig(sigcontext) = context->Eip;
1109 ESP_sig(sigcontext) = context->Esp;
1110 CS_sig(sigcontext) = context->SegCs;
1111 DS_sig(sigcontext) = context->SegDs;
1112 ES_sig(sigcontext) = context->SegEs;
1113 SS_sig(sigcontext) = context->SegSs;
1114 #ifdef GS_sig
1115 GS_sig(sigcontext) = context->SegGs;
1116 #else
1117 wine_set_gs( context->SegGs );
1118 #endif
1119 #ifdef FS_sig
1120 FS_sig(sigcontext) = context->SegFs;
1121 #else
1122 wine_set_fs( context->SegFs );
1123 #endif
1125 if (fpu) *fpu = context->FloatSave;
1126 if (fpux) memcpy( fpux, context->ExtendedRegisters, sizeof(*fpux) );
1127 if (!fpu && !fpux) restore_fpu( context );
1131 /***********************************************************************
1132 * RtlCaptureContext (NTDLL.@)
1134 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
1136 *context = *regs;
1137 if (fpux_support) save_fpux( context );
1138 else save_fpu( context );
1140 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
1143 /***********************************************************************
1144 * set_cpu_context
1146 * Set the new CPU context. Used by NtSetContextThread.
1148 void set_cpu_context( const CONTEXT *context )
1150 DWORD flags = context->ContextFlags & ~CONTEXT_i386;
1152 if ((flags & CONTEXT_EXTENDED_REGISTERS) && fpux_support) restore_fpux( context );
1153 else if (flags & CONTEXT_FLOATING_POINT) restore_fpu( context );
1155 if (flags & CONTEXT_DEBUG_REGISTERS)
1157 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1158 regs->dr0 = context->Dr0;
1159 regs->dr1 = context->Dr1;
1160 regs->dr2 = context->Dr2;
1161 regs->dr3 = context->Dr3;
1162 regs->dr6 = context->Dr6;
1163 regs->dr7 = context->Dr7;
1165 if (flags & CONTEXT_FULL)
1167 if (!(flags & CONTEXT_CONTROL))
1168 FIXME( "setting partial context (%x) not supported\n", flags );
1169 else if (flags & CONTEXT_SEGMENTS)
1170 __wine_call_from_32_restore_regs( context );
1171 else
1173 CONTEXT newcontext = *context;
1174 newcontext.SegDs = wine_get_ds();
1175 newcontext.SegEs = wine_get_es();
1176 newcontext.SegFs = wine_get_fs();
1177 newcontext.SegGs = wine_get_gs();
1178 __wine_call_from_32_restore_regs( &newcontext );
1184 /***********************************************************************
1185 * copy_context
1187 * Copy a register context according to the flags.
1189 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1191 flags &= ~CONTEXT_i386; /* get rid of CPU id */
1192 if (flags & CONTEXT_INTEGER)
1194 to->Eax = from->Eax;
1195 to->Ebx = from->Ebx;
1196 to->Ecx = from->Ecx;
1197 to->Edx = from->Edx;
1198 to->Esi = from->Esi;
1199 to->Edi = from->Edi;
1201 if (flags & CONTEXT_CONTROL)
1203 to->Ebp = from->Ebp;
1204 to->Esp = from->Esp;
1205 to->Eip = from->Eip;
1206 to->SegCs = from->SegCs;
1207 to->SegSs = from->SegSs;
1208 to->EFlags = from->EFlags;
1210 if (flags & CONTEXT_SEGMENTS)
1212 to->SegDs = from->SegDs;
1213 to->SegEs = from->SegEs;
1214 to->SegFs = from->SegFs;
1215 to->SegGs = from->SegGs;
1217 if (flags & CONTEXT_DEBUG_REGISTERS)
1219 to->Dr0 = from->Dr0;
1220 to->Dr1 = from->Dr1;
1221 to->Dr2 = from->Dr2;
1222 to->Dr3 = from->Dr3;
1223 to->Dr6 = from->Dr6;
1224 to->Dr7 = from->Dr7;
1226 if (flags & CONTEXT_FLOATING_POINT)
1228 to->FloatSave = from->FloatSave;
1230 if (flags & CONTEXT_EXTENDED_REGISTERS)
1232 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
1237 /***********************************************************************
1238 * context_to_server
1240 * Convert a register context to the server format.
1242 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1244 DWORD flags = from->ContextFlags & ~CONTEXT_i386; /* get rid of CPU id */
1246 memset( to, 0, sizeof(*to) );
1247 to->cpu = CPU_x86;
1249 if (flags & CONTEXT_CONTROL)
1251 to->flags |= SERVER_CTX_CONTROL;
1252 to->ctl.i386_regs.ebp = from->Ebp;
1253 to->ctl.i386_regs.esp = from->Esp;
1254 to->ctl.i386_regs.eip = from->Eip;
1255 to->ctl.i386_regs.cs = from->SegCs;
1256 to->ctl.i386_regs.ss = from->SegSs;
1257 to->ctl.i386_regs.eflags = from->EFlags;
1259 if (flags & CONTEXT_INTEGER)
1261 to->flags |= SERVER_CTX_INTEGER;
1262 to->integer.i386_regs.eax = from->Eax;
1263 to->integer.i386_regs.ebx = from->Ebx;
1264 to->integer.i386_regs.ecx = from->Ecx;
1265 to->integer.i386_regs.edx = from->Edx;
1266 to->integer.i386_regs.esi = from->Esi;
1267 to->integer.i386_regs.edi = from->Edi;
1269 if (flags & CONTEXT_SEGMENTS)
1271 to->flags |= SERVER_CTX_SEGMENTS;
1272 to->seg.i386_regs.ds = from->SegDs;
1273 to->seg.i386_regs.es = from->SegEs;
1274 to->seg.i386_regs.fs = from->SegFs;
1275 to->seg.i386_regs.gs = from->SegGs;
1277 if (flags & CONTEXT_FLOATING_POINT)
1279 to->flags |= SERVER_CTX_FLOATING_POINT;
1280 to->fp.i386_regs.ctrl = from->FloatSave.ControlWord;
1281 to->fp.i386_regs.status = from->FloatSave.StatusWord;
1282 to->fp.i386_regs.tag = from->FloatSave.TagWord;
1283 to->fp.i386_regs.err_off = from->FloatSave.ErrorOffset;
1284 to->fp.i386_regs.err_sel = from->FloatSave.ErrorSelector;
1285 to->fp.i386_regs.data_off = from->FloatSave.DataOffset;
1286 to->fp.i386_regs.data_sel = from->FloatSave.DataSelector;
1287 to->fp.i386_regs.cr0npx = from->FloatSave.Cr0NpxState;
1288 memcpy( to->fp.i386_regs.regs, from->FloatSave.RegisterArea, sizeof(to->fp.i386_regs.regs) );
1290 if (flags & CONTEXT_DEBUG_REGISTERS)
1292 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1293 to->debug.i386_regs.dr0 = from->Dr0;
1294 to->debug.i386_regs.dr1 = from->Dr1;
1295 to->debug.i386_regs.dr2 = from->Dr2;
1296 to->debug.i386_regs.dr3 = from->Dr3;
1297 to->debug.i386_regs.dr6 = from->Dr6;
1298 to->debug.i386_regs.dr7 = from->Dr7;
1300 if (flags & CONTEXT_EXTENDED_REGISTERS)
1302 to->flags |= SERVER_CTX_EXTENDED_REGISTERS;
1303 memcpy( to->ext.i386_regs, from->ExtendedRegisters, sizeof(to->ext.i386_regs) );
1305 return STATUS_SUCCESS;
1309 /***********************************************************************
1310 * context_from_server
1312 * Convert a register context from the server format.
1314 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1316 if (from->cpu != CPU_x86) return STATUS_INVALID_PARAMETER;
1318 to->ContextFlags = CONTEXT_i386;
1319 if (from->flags & SERVER_CTX_CONTROL)
1321 to->ContextFlags |= CONTEXT_CONTROL;
1322 to->Ebp = from->ctl.i386_regs.ebp;
1323 to->Esp = from->ctl.i386_regs.esp;
1324 to->Eip = from->ctl.i386_regs.eip;
1325 to->SegCs = from->ctl.i386_regs.cs;
1326 to->SegSs = from->ctl.i386_regs.ss;
1327 to->EFlags = from->ctl.i386_regs.eflags;
1329 if (from->flags & SERVER_CTX_INTEGER)
1331 to->ContextFlags |= CONTEXT_INTEGER;
1332 to->Eax = from->integer.i386_regs.eax;
1333 to->Ebx = from->integer.i386_regs.ebx;
1334 to->Ecx = from->integer.i386_regs.ecx;
1335 to->Edx = from->integer.i386_regs.edx;
1336 to->Esi = from->integer.i386_regs.esi;
1337 to->Edi = from->integer.i386_regs.edi;
1339 if (from->flags & SERVER_CTX_SEGMENTS)
1341 to->ContextFlags |= CONTEXT_SEGMENTS;
1342 to->SegDs = from->seg.i386_regs.ds;
1343 to->SegEs = from->seg.i386_regs.es;
1344 to->SegFs = from->seg.i386_regs.fs;
1345 to->SegGs = from->seg.i386_regs.gs;
1347 if (from->flags & SERVER_CTX_FLOATING_POINT)
1349 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1350 to->FloatSave.ControlWord = from->fp.i386_regs.ctrl;
1351 to->FloatSave.StatusWord = from->fp.i386_regs.status;
1352 to->FloatSave.TagWord = from->fp.i386_regs.tag;
1353 to->FloatSave.ErrorOffset = from->fp.i386_regs.err_off;
1354 to->FloatSave.ErrorSelector = from->fp.i386_regs.err_sel;
1355 to->FloatSave.DataOffset = from->fp.i386_regs.data_off;
1356 to->FloatSave.DataSelector = from->fp.i386_regs.data_sel;
1357 to->FloatSave.Cr0NpxState = from->fp.i386_regs.cr0npx;
1358 memcpy( to->FloatSave.RegisterArea, from->fp.i386_regs.regs, sizeof(to->FloatSave.RegisterArea) );
1360 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1362 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1363 to->Dr0 = from->debug.i386_regs.dr0;
1364 to->Dr1 = from->debug.i386_regs.dr1;
1365 to->Dr2 = from->debug.i386_regs.dr2;
1366 to->Dr3 = from->debug.i386_regs.dr3;
1367 to->Dr6 = from->debug.i386_regs.dr6;
1368 to->Dr7 = from->debug.i386_regs.dr7;
1370 if (from->flags & SERVER_CTX_EXTENDED_REGISTERS)
1372 to->ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
1373 memcpy( to->ExtendedRegisters, from->ext.i386_regs, sizeof(to->ExtendedRegisters) );
1375 return STATUS_SUCCESS;
1379 /***********************************************************************
1380 * is_privileged_instr
1382 * Check if the fault location is a privileged instruction.
1383 * Based on the instruction emulation code in dlls/kernel/instr.c.
1385 static inline DWORD is_privileged_instr( CONTEXT86 *context )
1387 const BYTE *instr;
1388 unsigned int prefix_count = 0;
1390 if (!wine_ldt_is_system( context->SegCs )) return 0;
1391 instr = (BYTE *)context->Eip;
1393 for (;;) switch(*instr)
1395 /* instruction prefixes */
1396 case 0x2e: /* %cs: */
1397 case 0x36: /* %ss: */
1398 case 0x3e: /* %ds: */
1399 case 0x26: /* %es: */
1400 case 0x64: /* %fs: */
1401 case 0x65: /* %gs: */
1402 case 0x66: /* opcode size */
1403 case 0x67: /* addr size */
1404 case 0xf0: /* lock */
1405 case 0xf2: /* repne */
1406 case 0xf3: /* repe */
1407 if (++prefix_count >= 15) return EXCEPTION_ILLEGAL_INSTRUCTION;
1408 instr++;
1409 continue;
1411 case 0x0f: /* extended instruction */
1412 switch(instr[1])
1414 case 0x20: /* mov crX, reg */
1415 case 0x21: /* mov drX, reg */
1416 case 0x22: /* mov reg, crX */
1417 case 0x23: /* mov reg drX */
1418 return EXCEPTION_PRIV_INSTRUCTION;
1420 return 0;
1421 case 0x6c: /* insb (%dx) */
1422 case 0x6d: /* insl (%dx) */
1423 case 0x6e: /* outsb (%dx) */
1424 case 0x6f: /* outsl (%dx) */
1425 case 0xcd: /* int $xx */
1426 case 0xe4: /* inb al,XX */
1427 case 0xe5: /* in (e)ax,XX */
1428 case 0xe6: /* outb XX,al */
1429 case 0xe7: /* out XX,(e)ax */
1430 case 0xec: /* inb (%dx),%al */
1431 case 0xed: /* inl (%dx),%eax */
1432 case 0xee: /* outb %al,(%dx) */
1433 case 0xef: /* outl %eax,(%dx) */
1434 case 0xf4: /* hlt */
1435 case 0xfa: /* cli */
1436 case 0xfb: /* sti */
1437 return EXCEPTION_PRIV_INSTRUCTION;
1438 default:
1439 return 0;
1444 #include "pshpack1.h"
1445 struct atl_thunk
1447 DWORD movl; /* movl this,4(%esp) */
1448 DWORD this;
1449 BYTE jmp; /* jmp func */
1450 int func;
1452 #include "poppack.h"
1454 /**********************************************************************
1455 * check_atl_thunk
1457 * Check if code destination is an ATL thunk, and emulate it if so.
1459 static BOOL check_atl_thunk( EXCEPTION_RECORD *rec, CONTEXT *context )
1461 const struct atl_thunk *thunk = (const struct atl_thunk *)rec->ExceptionInformation[1];
1462 BOOL ret = FALSE;
1464 __TRY
1466 if (thunk->movl == 0x042444c7 && thunk->jmp == 0xe9)
1468 *((DWORD *)context->Esp + 1) = thunk->this;
1469 context->Eip = (DWORD_PTR)(&thunk->func + 1) + thunk->func;
1470 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1471 thunk, context->Eip, *((DWORD *)context->Esp + 1) );
1472 ret = TRUE;
1475 __EXCEPT_PAGE_FAULT
1477 return FALSE;
1479 __ENDTRY
1480 return ret;
1484 /***********************************************************************
1485 * setup_exception_record
1487 * Setup the exception record and context on the thread stack.
1489 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr,
1490 WORD fs, WORD gs, raise_func func )
1492 struct stack_layout
1494 void *ret_addr; /* return address from raise_func */
1495 EXCEPTION_RECORD *rec_ptr; /* first arg for raise_func */
1496 CONTEXT *context_ptr; /* second arg for raise_func */
1497 CONTEXT context;
1498 EXCEPTION_RECORD rec;
1499 DWORD ebp;
1500 DWORD eip;
1501 } *stack = stack_ptr;
1502 DWORD exception_code = 0;
1504 /* stack sanity checks */
1506 if ((char *)stack >= (char *)get_signal_stack() &&
1507 (char *)stack < (char *)get_signal_stack() + signal_stack_size)
1509 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1510 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1511 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1512 NtCurrentTeb()->Tib.StackBase );
1513 abort_thread(1);
1516 if (stack - 1 > stack || /* check for overflow in subtraction */
1517 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1518 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1520 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1521 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1522 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->Tib.StackLimit,
1523 NtCurrentTeb()->Tib.StackBase );
1525 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1527 /* stack overflow on last page, unrecoverable */
1528 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1529 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1530 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1531 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1532 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1533 abort_thread(1);
1535 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1537 /* stack access below stack limit, may be recoverable */
1538 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1539 else
1541 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1542 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1543 diff, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext),
1544 (unsigned int) ESP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1545 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1546 abort_thread(1);
1550 stack--; /* push the stack_layout structure */
1551 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1552 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1553 #elif defined(VALGRIND_MAKE_WRITABLE)
1554 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1555 #endif
1556 stack->ret_addr = (void *)0xdeadbabe; /* raise_func must not return */
1557 stack->rec_ptr = &stack->rec;
1558 stack->context_ptr = &stack->context;
1560 stack->rec.ExceptionRecord = NULL;
1561 stack->rec.ExceptionCode = exception_code;
1562 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1563 stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
1564 stack->rec.NumberParameters = 0;
1566 save_context( &stack->context, sigcontext, fs, gs );
1568 /* now modify the sigcontext to return to the raise function */
1569 ESP_sig(sigcontext) = (DWORD)stack;
1570 EIP_sig(sigcontext) = (DWORD)func;
1571 /* clear single-step, direction, and align check flag */
1572 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1573 CS_sig(sigcontext) = wine_get_cs();
1574 DS_sig(sigcontext) = wine_get_ds();
1575 ES_sig(sigcontext) = wine_get_es();
1576 FS_sig(sigcontext) = wine_get_fs();
1577 GS_sig(sigcontext) = wine_get_gs();
1578 SS_sig(sigcontext) = wine_get_ss();
1580 return stack->rec_ptr;
1584 /***********************************************************************
1585 * setup_exception
1587 * Setup a proper stack frame for the raise function, and modify the
1588 * sigcontext so that the return from the signal handler will call
1589 * the raise function.
1591 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
1593 WORD fs, gs;
1594 void *stack = init_handler( sigcontext, &fs, &gs );
1596 return setup_exception_record( sigcontext, stack, fs, gs, func );
1600 /***********************************************************************
1601 * get_exception_context
1603 * Get a pointer to the context built by setup_exception.
1605 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
1607 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
1611 /**********************************************************************
1612 * get_fpu_code
1614 * Get the FPU exception code from the FPU status.
1616 static inline DWORD get_fpu_code( const CONTEXT *context )
1618 DWORD status = context->FloatSave.StatusWord & ~(context->FloatSave.ControlWord & 0x3f);
1620 if (status & 0x01) /* IE */
1622 if (status & 0x40) /* SF */
1623 return EXCEPTION_FLT_STACK_CHECK;
1624 else
1625 return EXCEPTION_FLT_INVALID_OPERATION;
1627 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
1628 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
1629 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
1630 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
1631 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
1632 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
1636 /**********************************************************************
1637 * raise_segv_exception
1639 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1641 NTSTATUS status;
1643 switch(rec->ExceptionCode)
1645 case EXCEPTION_ACCESS_VIOLATION:
1646 if (rec->NumberParameters == 2)
1648 if (rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT && check_atl_thunk( rec, context ))
1649 goto done;
1650 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
1651 rec->ExceptionInformation[0] )))
1652 goto done;
1654 break;
1655 case EXCEPTION_DATATYPE_MISALIGNMENT:
1656 /* FIXME: pass through exception handler first? */
1657 if (context->EFlags & 0x00040000)
1659 /* Disable AC flag, return */
1660 context->EFlags &= ~0x00040000;
1661 goto done;
1663 break;
1665 status = NtRaiseException( rec, context, TRUE );
1666 if (status) raise_status( status, rec );
1667 done:
1668 NtSetContextThread( GetCurrentThread(), context );
1672 /**********************************************************************
1673 * raise_trap_exception
1675 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1677 NTSTATUS status;
1679 if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
1681 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1683 /* when single stepping can't tell whether this is a hw bp or a
1684 * single step interrupt. try to avoid as much overhead as possible
1685 * and only do a server call if there is any hw bp enabled. */
1687 if( !(context->EFlags & 0x100) || (regs->dr7 & 0xff) )
1689 /* (possible) hardware breakpoint, fetch the debug registers */
1690 context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
1691 NtGetContextThread(GetCurrentThread(), context);
1692 context->ContextFlags |= CONTEXT_FULL; /* restore flags */
1695 context->EFlags &= ~0x100; /* clear single-step flag */
1698 status = NtRaiseException( rec, context, TRUE );
1699 if (status) raise_status( status, rec );
1703 /**********************************************************************
1704 * raise_generic_exception
1706 * Generic raise function for exceptions that don't need special treatment.
1708 static void WINAPI raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1710 NTSTATUS status;
1712 status = NtRaiseException( rec, context, TRUE );
1713 if (status) raise_status( status, rec );
1717 #ifdef __HAVE_VM86
1718 /**********************************************************************
1719 * raise_vm86_sti_exception
1721 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
1723 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1724 get_vm86_teb_info()->vm86_pending |= VIP_FLAG;
1726 if (ntdll_get_thread_data()->vm86_ptr)
1728 if (((char*)context->Eip >= (char*)vm86_return) &&
1729 ((char*)context->Eip <= (char*)vm86_return_end) &&
1730 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
1731 /* exiting from VM86, can't throw */
1732 goto done;
1734 merge_vm86_pending_flags( rec );
1736 else if (get_vm86_teb_info()->dpmi_vif &&
1737 !wine_ldt_is_system(context->SegCs) &&
1738 !wine_ldt_is_system(context->SegSs))
1740 /* Executing DPMI code and virtual interrupts are enabled. */
1741 get_vm86_teb_info()->vm86_pending = 0;
1742 NtRaiseException( rec, context, TRUE );
1744 done:
1745 NtSetContextThread( GetCurrentThread(), context );
1749 /**********************************************************************
1750 * usr2_handler
1752 * Handler for SIGUSR2.
1753 * We use it to signal that the running __wine_enter_vm86() should
1754 * immediately set VIP_FLAG, causing pending events to be handled
1755 * as early as possible.
1757 static void usr2_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1759 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_vm86_sti_exception );
1760 rec->ExceptionCode = EXCEPTION_VM86_STI;
1762 #endif /* __HAVE_VM86 */
1765 /**********************************************************************
1766 * segv_handler
1768 * Handler for SIGSEGV and related errors.
1770 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1772 WORD fs, gs;
1773 EXCEPTION_RECORD *rec;
1774 SIGCONTEXT *context = sigcontext;
1775 void *stack = init_handler( sigcontext, &fs, &gs );
1777 /* check for page fault inside the thread stack */
1778 if (get_trap_code(context) == TRAP_x86_PAGEFLT &&
1779 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
1780 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
1781 virtual_handle_stack_fault( siginfo->si_addr ))
1783 /* check if this was the last guard page */
1784 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
1786 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1787 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1789 return;
1792 rec = setup_exception_record( context, stack, fs, gs, raise_segv_exception );
1793 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
1795 switch(get_trap_code(context))
1797 case TRAP_x86_OFLOW: /* Overflow exception */
1798 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1799 break;
1800 case TRAP_x86_BOUND: /* Bound range exception */
1801 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1802 break;
1803 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
1804 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1805 break;
1806 case TRAP_x86_STKFLT: /* Stack fault */
1807 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1808 break;
1809 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
1810 case TRAP_x86_PROTFLT: /* General protection fault */
1811 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1813 WORD err = get_error_code(context);
1814 if (!err && (rec->ExceptionCode = is_privileged_instr( get_exception_context(rec) ))) break;
1815 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1816 rec->NumberParameters = 2;
1817 rec->ExceptionInformation[0] = 0;
1818 /* if error contains a LDT selector, use that as fault address */
1819 if ((err & 7) == 4 && !wine_ldt_is_system( err | 7 ))
1820 rec->ExceptionInformation[1] = err & ~7;
1821 else
1822 rec->ExceptionInformation[1] = 0xffffffff;
1824 break;
1825 case TRAP_x86_PAGEFLT: /* Page fault */
1826 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1827 rec->NumberParameters = 2;
1828 rec->ExceptionInformation[0] = (get_error_code(context) >> 1) & 0x09;
1829 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
1830 break;
1831 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
1832 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1833 break;
1834 default:
1835 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1836 /* fall through */
1837 case TRAP_x86_NMI: /* NMI interrupt */
1838 case TRAP_x86_DNA: /* Device not available exception */
1839 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
1840 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
1841 case TRAP_x86_MCHK: /* Machine check exception */
1842 case TRAP_x86_CACHEFLT: /* Cache flush exception */
1843 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1844 break;
1849 /**********************************************************************
1850 * trap_handler
1852 * Handler for SIGTRAP.
1854 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1856 SIGCONTEXT *context = sigcontext;
1857 EXCEPTION_RECORD *rec = setup_exception( context, raise_trap_exception );
1859 switch(get_trap_code(context))
1861 case TRAP_x86_TRCTRAP: /* Single-step exception */
1862 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1863 break;
1864 case TRAP_x86_BPTFLT: /* Breakpoint exception */
1865 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
1866 /* fall through */
1867 default:
1868 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1869 break;
1874 /**********************************************************************
1875 * fpe_handler
1877 * Handler for SIGFPE.
1879 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1881 CONTEXT *win_context;
1882 SIGCONTEXT *context = sigcontext;
1883 EXCEPTION_RECORD *rec = setup_exception( context, raise_generic_exception );
1885 win_context = get_exception_context( rec );
1887 switch(get_trap_code(context))
1889 case TRAP_x86_DIVIDE: /* Division by zero exception */
1890 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1891 break;
1892 case TRAP_x86_FPOPFLT: /* Coprocessor segment overrun */
1893 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1894 break;
1895 case TRAP_x86_ARITHTRAP: /* Floating point exception */
1896 case TRAP_x86_UNKNOWN: /* Unknown fault code */
1897 rec->ExceptionCode = get_fpu_code( win_context );
1898 rec->ExceptionAddress = (LPVOID)win_context->FloatSave.ErrorOffset;
1899 break;
1900 case TRAP_x86_CACHEFLT: /* SIMD exception */
1901 /* TODO:
1902 * Behaviour only tested for divide-by-zero exceptions
1903 * Check for other SIMD exceptions as well */
1904 if(siginfo->si_code != FPE_FLTDIV)
1905 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1906 siginfo->si_code);
1908 rec->ExceptionCode = STATUS_FLOAT_MULTIPLE_TRAPS;
1909 rec->NumberParameters = 1;
1910 /* no idea what meaning is actually behind this but that's what native does */
1911 rec->ExceptionInformation[0] = 0;
1912 break;
1913 default:
1914 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context) );
1915 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1916 break;
1921 /**********************************************************************
1922 * int_handler
1924 * Handler for SIGINT.
1926 * FIXME: should not be calling external functions on the signal stack.
1928 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1930 WORD fs, gs;
1931 init_handler( sigcontext, &fs, &gs );
1932 if (!dispatch_signal(SIGINT))
1934 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
1935 rec->ExceptionCode = CONTROL_C_EXIT;
1939 /**********************************************************************
1940 * abrt_handler
1942 * Handler for SIGABRT.
1944 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1946 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
1947 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
1948 rec->ExceptionFlags = EH_NONCONTINUABLE;
1952 /**********************************************************************
1953 * quit_handler
1955 * Handler for SIGQUIT.
1957 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1959 WORD fs, gs;
1960 init_handler( sigcontext, &fs, &gs );
1961 abort_thread(0);
1965 /**********************************************************************
1966 * usr1_handler
1968 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1970 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
1972 CONTEXT context;
1973 WORD fs, gs;
1975 init_handler( sigcontext, &fs, &gs );
1976 save_context( &context, sigcontext, fs, gs );
1977 wait_suspend( &context );
1978 restore_context( &context, sigcontext );
1982 /**********************************************************************
1983 * get_signal_stack_total_size
1985 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1986 * Must be a power of two.
1988 size_t get_signal_stack_total_size(void)
1990 if (!signal_stack_size)
1992 size_t size = 8192, min_size = teb_size + max( MINSIGSTKSZ, 8192 );
1993 /* find the first power of two not smaller than min_size */
1994 while (size < min_size) size *= 2;
1995 signal_stack_mask = size - 1;
1996 signal_stack_size = size - teb_size;
1998 return signal_stack_size + teb_size;
2002 /***********************************************************************
2003 * __wine_set_signal_handler (NTDLL.@)
2005 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2007 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2008 if (handlers[sig] != NULL) return -2;
2009 handlers[sig] = wsh;
2010 return 0;
2014 /**********************************************************************
2015 * signal_init_thread
2017 void signal_init_thread( TEB *teb )
2019 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
2020 LDT_ENTRY fs_entry;
2021 stack_t ss;
2023 #ifdef __APPLE__
2024 int mib[2], val = 1;
2026 mib[0] = CTL_KERN;
2027 mib[1] = KERN_THALTSTACK;
2028 sysctl( mib, 2, NULL, NULL, &val, sizeof(val) );
2029 #endif
2031 ss.ss_sp = (char *)teb + teb_size;
2032 ss.ss_size = signal_stack_size;
2033 ss.ss_flags = 0;
2034 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2036 wine_ldt_set_base( &fs_entry, teb );
2037 wine_ldt_set_limit( &fs_entry, teb_size - 1 );
2038 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
2039 wine_ldt_init_fs( thread_data->fs, &fs_entry );
2040 thread_data->gs = wine_get_gs();
2043 /**********************************************************************
2044 * signal_init_process
2046 void signal_init_process(void)
2048 struct sigaction sig_act;
2050 sig_act.sa_mask = server_block_set;
2051 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
2052 #ifdef SA_ONSTACK
2053 sig_act.sa_flags |= SA_ONSTACK;
2054 #endif
2056 sig_act.sa_sigaction = int_handler;
2057 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2058 sig_act.sa_sigaction = fpe_handler;
2059 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2060 sig_act.sa_sigaction = abrt_handler;
2061 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2062 sig_act.sa_sigaction = quit_handler;
2063 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2064 sig_act.sa_sigaction = usr1_handler;
2065 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2067 sig_act.sa_sigaction = segv_handler;
2068 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2069 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2070 #ifdef SIGBUS
2071 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2072 #endif
2074 #ifdef SIGTRAP
2075 sig_act.sa_sigaction = trap_handler;
2076 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2077 #endif
2079 #ifdef __HAVE_VM86
2080 sig_act.sa_sigaction = usr2_handler;
2081 if (sigaction( SIGUSR2, &sig_act, NULL ) == -1) goto error;
2082 #endif
2084 return;
2086 error:
2087 perror("sigaction");
2088 exit(1);
2092 #ifdef __HAVE_VM86
2093 /**********************************************************************
2094 * __wine_enter_vm86 (NTDLL.@)
2096 * Enter vm86 mode with the specified register context.
2098 void __wine_enter_vm86( CONTEXT *context )
2100 EXCEPTION_RECORD rec;
2101 int res;
2102 struct vm86plus_struct vm86;
2104 memset( &vm86, 0, sizeof(vm86) );
2105 for (;;)
2107 restore_vm86_context( context, &vm86 );
2109 ntdll_get_thread_data()->vm86_ptr = &vm86;
2110 merge_vm86_pending_flags( &rec );
2112 res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
2113 if (res < 0)
2115 errno = -res;
2116 return;
2119 save_vm86_context( context, &vm86 );
2121 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2122 rec.ExceptionRecord = NULL;
2123 rec.ExceptionAddress = (LPVOID)context->Eip;
2124 rec.NumberParameters = 0;
2126 switch(VM86_TYPE(res))
2128 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
2129 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
2130 break;
2131 case VM86_TRAP: /* return due to DOS-debugger request */
2132 switch(VM86_ARG(res))
2134 case TRAP_x86_TRCTRAP: /* Single-step exception */
2135 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
2136 break;
2137 case TRAP_x86_BPTFLT: /* Breakpoint exception */
2138 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
2139 /* fall through */
2140 default:
2141 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
2142 break;
2144 break;
2145 case VM86_INTx: /* int3/int x instruction (ARG = x) */
2146 rec.ExceptionCode = EXCEPTION_VM86_INTx;
2147 rec.NumberParameters = 1;
2148 rec.ExceptionInformation[0] = VM86_ARG(res);
2149 break;
2150 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
2151 context->EFlags |= VIF_FLAG;
2152 context->EFlags &= ~VIP_FLAG;
2153 get_vm86_teb_info()->vm86_pending = 0;
2154 rec.ExceptionCode = EXCEPTION_VM86_STI;
2155 break;
2156 case VM86_PICRETURN: /* return due to pending PIC request */
2157 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
2158 break;
2159 case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
2160 default:
2161 WINE_ERR( "unhandled result from vm86 mode %x\n", res );
2162 continue;
2164 raise_exception( &rec, context, TRUE );
2168 #else /* __HAVE_VM86 */
2169 /**********************************************************************
2170 * __wine_enter_vm86 (NTDLL.@)
2172 void __wine_enter_vm86( CONTEXT *context )
2174 MESSAGE("vm86 mode not supported on this platform\n");
2176 #endif /* __HAVE_VM86 */
2179 /*******************************************************************
2180 * RtlUnwind (NTDLL.@)
2182 void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID targetIp,
2183 PEXCEPTION_RECORD pRecord, PVOID retval, CONTEXT *context )
2185 EXCEPTION_RECORD record;
2186 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
2187 DWORD res;
2189 context->Eax = (DWORD)retval;
2191 /* build an exception record, if we do not have one */
2192 if (!pRecord)
2194 record.ExceptionCode = STATUS_UNWIND;
2195 record.ExceptionFlags = 0;
2196 record.ExceptionRecord = NULL;
2197 record.ExceptionAddress = (void *)context->Eip;
2198 record.NumberParameters = 0;
2199 pRecord = &record;
2202 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
2204 TRACE( "code=%x flags=%x\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
2206 /* get chain of exception frames */
2207 frame = NtCurrentTeb()->Tib.ExceptionList;
2208 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
2210 /* Check frame address */
2211 if (pEndFrame && (frame > pEndFrame))
2212 raise_status( STATUS_INVALID_UNWIND_TARGET, pRecord );
2214 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
2215 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
2216 (UINT_PTR)frame & 3)
2217 raise_status( STATUS_BAD_STACK, pRecord );
2219 /* Call handler */
2220 TRACE( "calling handler at %p code=%x flags=%x\n",
2221 frame->Handler, pRecord->ExceptionCode, pRecord->ExceptionFlags );
2222 res = EXC_CallHandler( pRecord, frame, context, &dispatch, frame->Handler, unwind_handler );
2223 TRACE( "handler at %p returned %x\n", frame->Handler, res );
2225 switch(res)
2227 case ExceptionContinueSearch:
2228 break;
2229 case ExceptionCollidedUnwind:
2230 frame = dispatch;
2231 break;
2232 default:
2233 raise_status( STATUS_INVALID_DISPOSITION, pRecord );
2234 break;
2236 frame = __wine_pop_frame( frame );
2239 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2242 /*******************************************************************
2243 * NtRaiseException (NTDLL.@)
2245 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2247 NTSTATUS status = raise_exception( rec, context, first_chance );
2248 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2249 return status;
2253 /***********************************************************************
2254 * RtlRaiseException (NTDLL.@)
2256 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2258 NTSTATUS status;
2260 rec->ExceptionAddress = (void *)context->Eip;
2261 status = raise_exception( rec, context, TRUE );
2262 if (status != STATUS_SUCCESS) raise_status( status, rec );
2264 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2267 /* wrapper for apps that don't declare the thread function correctly */
2268 extern void DECLSPEC_NORETURN call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg );
2269 __ASM_GLOBAL_FUNC(call_thread_func,
2270 "pushl %ebp\n\t"
2271 "movl %esp,%ebp\n\t"
2272 "subl $4,%esp\n\t"
2273 "pushl 12(%ebp)\n\t"
2274 "call *8(%ebp)\n\t"
2275 "leal -4(%ebp),%esp\n\t"
2276 "pushl %eax\n\t"
2277 "call " __ASM_NAME("exit_thread") "\n\t"
2278 "int $3" )
2280 /***********************************************************************
2281 * call_thread_entry_point
2283 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
2285 __TRY
2287 call_thread_func( entry, arg );
2289 __EXCEPT(unhandled_exception_filter)
2291 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2293 __ENDTRY
2294 abort(); /* should not be reached */
2297 /***********************************************************************
2298 * RtlExitUserThread (NTDLL.@)
2300 void WINAPI RtlExitUserThread( ULONG status )
2302 exit_thread( status );
2305 /**********************************************************************
2306 * DbgBreakPoint (NTDLL.@)
2308 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
2310 /**********************************************************************
2311 * DbgUserBreakPoint (NTDLL.@)
2313 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
2315 /**********************************************************************
2316 * NtCurrentTeb (NTDLL.@)
2318 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2321 /**********************************************************************
2322 * EXC_CallHandler (internal)
2324 * Some exception handlers depend on EBP to have a fixed position relative to
2325 * the exception frame.
2326 * Shrinker depends on (*1) doing what it does,
2327 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2328 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2329 * function calling the handler having only 5 parameters (*4).
2331 __ASM_GLOBAL_FUNC( EXC_CallHandler,
2332 " pushl %ebp\n"
2333 " movl %esp, %ebp\n"
2334 " pushl %ebx\n"
2335 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
2336 " pushl 24(%ebp)\n"
2337 " pushl 20(%ebp)\n"
2338 " pushl 16(%ebp)\n"
2339 " pushl 12(%ebp)\n"
2340 " pushl 8(%ebp)\n"
2341 " call " __ASM_NAME("call_exception_handler") "\n"
2342 " popl %ebx\n"
2343 " leave\n"
2344 " ret\n"
2346 __ASM_GLOBAL_FUNC(call_exception_handler,
2347 " pushl %ebp\n"
2348 " movl %esp, %ebp\n"
2349 " subl $12,%esp\n"
2350 " pushl 12(%ebp)\n" /* make any exceptions in this... */
2351 " pushl %edx\n" /* handler be handled by... */
2352 " .byte 0x64\n"
2353 " pushl (0)\n" /* nested_handler (passed in edx). */
2354 " .byte 0x64\n"
2355 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
2356 " pushl 20(%ebp)\n"
2357 " pushl 16(%ebp)\n"
2358 " pushl 12(%ebp)\n"
2359 " pushl 8(%ebp)\n"
2360 " movl 24(%ebp), %ecx\n" /* (*1) */
2361 " call *%ecx\n" /* call handler. (*2) */
2362 " .byte 0x64\n"
2363 " movl (0), %esp\n" /* restore previous... (*3) */
2364 " .byte 0x64\n"
2365 " popl (0)\n" /* exception frame. */
2366 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
2367 " popl %ebp\n"
2368 " ret $20\n" /* (*4) */
2370 #endif /* __i386__ */