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
24 #include "wine/port.h"
31 #include <sys/types.h>
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
60 #include "wine/library.h"
61 #include "ntdll_misc.h"
62 #include "wine/exception.h"
63 #include "wine/debug.h"
65 #ifdef HAVE_VALGRIND_MEMCHECK_H
66 #include <valgrind/memcheck.h>
69 #undef ERR /* Solaris needs to define this */
71 /***********************************************************************
72 * signal context platform-specific definitions
77 typedef ucontext_t SIGCONTEXT
;
79 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
80 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
81 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
82 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
83 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
84 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
85 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
86 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
88 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
89 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
90 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
91 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
92 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
93 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
95 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
96 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
97 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
98 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
100 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
102 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
104 int vm86_enter( void **vm86_ptr
);
105 void vm86_return(void);
106 void vm86_return_end(void);
107 __ASM_GLOBAL_FUNC(vm86_enter
,
109 "movl %esp, %ebp\n\t"
110 "movl $166,%eax\n\t" /*SYS_vm86*/
111 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
112 "movl (%ecx),%ecx\n\t"
114 "movl $1,%ebx\n\t" /*VM86_ENTER*/
115 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
119 ".globl " __ASM_NAME("vm86_return") "\n\t"
120 __ASM_FUNC("vm86_return") "\n"
121 __ASM_NAME("vm86_return") ":\n\t"
127 "testl %eax,%eax\n\t"
129 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
130 "je " __ASM_NAME("vm86_enter") "\n\t"
132 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
134 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
135 __ASM_FUNC("vm86_return_end") "\n"
136 __ASM_NAME("vm86_return_end") ":\n\t"
139 #ifdef HAVE_SYS_VM86_H
147 #include <machine/frame.h>
148 typedef struct trapframe SIGCONTEXT
;
150 #define EAX_sig(context) ((context)->tf_eax)
151 #define EBX_sig(context) ((context)->tf_ebx)
152 #define ECX_sig(context) ((context)->tf_ecx)
153 #define EDX_sig(context) ((context)->tf_edx)
154 #define ESI_sig(context) ((context)->tf_esi)
155 #define EDI_sig(context) ((context)->tf_edi)
156 #define EBP_sig(context) ((context)->tf_ebp)
158 #define CS_sig(context) ((context)->tf_cs)
159 #define DS_sig(context) ((context)->tf_ds)
160 #define ES_sig(context) ((context)->tf_es)
161 #define SS_sig(context) ((context)->tf_ss)
163 #define EFL_sig(context) ((context)->tf_eflags)
165 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
166 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
170 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
172 typedef struct sigcontext SIGCONTEXT
;
174 #define EAX_sig(context) ((context)->sc_eax)
175 #define EBX_sig(context) ((context)->sc_ebx)
176 #define ECX_sig(context) ((context)->sc_ecx)
177 #define EDX_sig(context) ((context)->sc_edx)
178 #define ESI_sig(context) ((context)->sc_esi)
179 #define EDI_sig(context) ((context)->sc_edi)
180 #define EBP_sig(context) ((context)->sc_ebp)
182 #define CS_sig(context) ((context)->sc_cs)
183 #define DS_sig(context) ((context)->sc_ds)
184 #define ES_sig(context) ((context)->sc_es)
185 #define FS_sig(context) ((context)->sc_fs)
186 #define GS_sig(context) ((context)->sc_gs)
187 #define SS_sig(context) ((context)->sc_ss)
189 #define TRAP_sig(context) ((context)->sc_trapno)
190 #define ERROR_sig(context) ((context)->sc_err)
191 #define EFL_sig(context) ((context)->sc_eflags)
193 #define EIP_sig(context) ((context)->sc_eip)
194 #define ESP_sig(context) ((context)->sc_esp)
198 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
201 #include <sys/regset.h>
203 #include <sys/ucontext.h>
204 typedef struct ucontext SIGCONTEXT
;
210 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
211 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
212 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
213 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
214 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
215 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
216 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
218 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
219 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
220 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
221 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
223 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
224 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
226 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
228 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
230 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
232 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
234 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
237 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
240 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
243 #endif /* svr4 || SCO_DS */
246 # include <sys/ucontext.h>
248 typedef ucontext_t SIGCONTEXT
;
250 /* work around silly renaming of struct members in OS X 10.5 */
251 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
252 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
253 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
254 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
255 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
256 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
257 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
258 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
259 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
260 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
261 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
262 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
263 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
264 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
265 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
266 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
267 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
268 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
269 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
271 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
272 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
273 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
274 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
275 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
276 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
277 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
278 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
279 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
280 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
281 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
282 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
283 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
284 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
285 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
286 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
287 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
288 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
291 #endif /* __APPLE__ */
293 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
295 typedef int (*wine_signal_handler
)(unsigned int sig
);
297 static size_t signal_stack_mask
;
298 static size_t signal_stack_size
;
300 static wine_signal_handler handlers
[256];
302 extern void DECLSPEC_NORETURN
__wine_call_from_32_restore_regs( const CONTEXT
*context
);
306 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
307 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
308 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
309 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
310 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
311 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
312 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
313 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
314 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
315 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
316 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
317 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
318 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
319 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
320 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
321 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
322 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
323 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
324 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
325 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
326 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
328 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
329 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
330 TRAP_x86_NMI
= 2, /* NMI interrupt */
331 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
332 TRAP_x86_OFLOW
= 4, /* Overflow exception */
333 TRAP_x86_BOUND
= 5, /* Bound range exception */
334 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
335 TRAP_x86_DNA
= 7, /* Device not available exception */
336 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
337 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
338 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
339 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
340 TRAP_x86_STKFLT
= 12, /* Stack fault */
341 TRAP_x86_PROTFLT
= 13, /* General protection fault */
342 TRAP_x86_PAGEFLT
= 14, /* Page fault */
343 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
344 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
345 TRAP_x86_MCHK
= 18, /* Machine check exception */
346 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
347 otherwise Cache flush exception (via SIGSEV) */
352 /***********************************************************************
355 static inline int dispatch_signal(unsigned int sig
)
357 if (handlers
[sig
] == NULL
) return 0;
358 return handlers
[sig
](sig
);
362 /***********************************************************************
365 * Get the trap code for a signal.
367 static inline enum i386_trap_code
get_trap_code( const SIGCONTEXT
*sigcontext
)
370 return TRAP_sig(sigcontext
);
372 return TRAP_x86_UNKNOWN
; /* unknown trap code */
376 /***********************************************************************
379 * Get the error code for a signal.
381 static inline WORD
get_error_code( const SIGCONTEXT
*sigcontext
)
384 return ERROR_sig(sigcontext
);
390 /***********************************************************************
393 * Get the base of the signal stack for the current thread.
395 static inline void *get_signal_stack(void)
397 return (char *)NtCurrentTeb() + 4096;
401 /***********************************************************************
404 * Get the current teb based on the stack pointer.
406 static inline TEB
*get_current_teb(void)
409 __asm__("movl %%esp,%0" : "=g" (esp
) );
410 return (TEB
*)(esp
& ~signal_stack_mask
);
415 /***********************************************************************
418 * Set the register values from a vm86 structure.
420 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
422 context
->ContextFlags
= CONTEXT_FULL
;
423 context
->Eax
= vm86
->regs
.eax
;
424 context
->Ebx
= vm86
->regs
.ebx
;
425 context
->Ecx
= vm86
->regs
.ecx
;
426 context
->Edx
= vm86
->regs
.edx
;
427 context
->Esi
= vm86
->regs
.esi
;
428 context
->Edi
= vm86
->regs
.edi
;
429 context
->Esp
= vm86
->regs
.esp
;
430 context
->Ebp
= vm86
->regs
.ebp
;
431 context
->Eip
= vm86
->regs
.eip
;
432 context
->SegCs
= vm86
->regs
.cs
;
433 context
->SegDs
= vm86
->regs
.ds
;
434 context
->SegEs
= vm86
->regs
.es
;
435 context
->SegFs
= vm86
->regs
.fs
;
436 context
->SegGs
= vm86
->regs
.gs
;
437 context
->SegSs
= vm86
->regs
.ss
;
438 context
->EFlags
= vm86
->regs
.eflags
;
442 /***********************************************************************
443 * restore_vm86_context
445 * Build a vm86 structure from the register values.
447 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
449 vm86
->regs
.eax
= context
->Eax
;
450 vm86
->regs
.ebx
= context
->Ebx
;
451 vm86
->regs
.ecx
= context
->Ecx
;
452 vm86
->regs
.edx
= context
->Edx
;
453 vm86
->regs
.esi
= context
->Esi
;
454 vm86
->regs
.edi
= context
->Edi
;
455 vm86
->regs
.esp
= context
->Esp
;
456 vm86
->regs
.ebp
= context
->Ebp
;
457 vm86
->regs
.eip
= context
->Eip
;
458 vm86
->regs
.cs
= context
->SegCs
;
459 vm86
->regs
.ds
= context
->SegDs
;
460 vm86
->regs
.es
= context
->SegEs
;
461 vm86
->regs
.fs
= context
->SegFs
;
462 vm86
->regs
.gs
= context
->SegGs
;
463 vm86
->regs
.ss
= context
->SegSs
;
464 vm86
->regs
.eflags
= context
->EFlags
;
468 /**********************************************************************
469 * merge_vm86_pending_flags
471 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
472 * raises exception if there are pending events and VIF flag
473 * has been turned on.
475 * Called from __wine_enter_vm86 because vm86_enter
476 * doesn't check for pending events.
478 * Called from raise_vm86_sti_exception to check for
479 * pending events in a signal safe way.
481 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
483 BOOL check_pending
= TRUE
;
484 struct vm86plus_struct
*vm86
=
485 (struct vm86plus_struct
*)(ntdll_get_thread_data()->vm86_ptr
);
488 * In order to prevent a race when SIGUSR2 occurs while
489 * we are returning from exception handler, pending events
490 * will be rechecked after each raised exception.
492 while (check_pending
&& NtCurrentTeb()->vm86_pending
)
494 check_pending
= FALSE
;
495 ntdll_get_thread_data()->vm86_ptr
= NULL
;
498 * If VIF is set, throw exception.
499 * Note that SIGUSR2 may turn VIF flag off so
500 * VIF check must occur only when TEB.vm86_ptr is NULL.
502 if (vm86
->regs
.eflags
& VIF_MASK
)
505 save_vm86_context( &vcontext
, vm86
);
507 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
508 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
509 rec
->ExceptionRecord
= NULL
;
510 rec
->NumberParameters
= 0;
511 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
513 vcontext
.EFlags
&= ~VIP_MASK
;
514 NtCurrentTeb()->vm86_pending
= 0;
515 __regs_RtlRaiseException( rec
, &vcontext
);
517 restore_vm86_context( &vcontext
, vm86
);
518 check_pending
= TRUE
;
521 ntdll_get_thread_data()->vm86_ptr
= vm86
;
525 * Merge VIP flags in a signal safe way. This requires
526 * that the following operation compiles into atomic
529 vm86
->regs
.eflags
|= NtCurrentTeb()->vm86_pending
;
531 #endif /* __HAVE_VM86 */
534 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
537 /***********************************************************************
540 * Handler initialization when the full context is not needed.
542 static inline void *init_handler( const SIGCONTEXT
*sigcontext
, WORD
*fs
, WORD
*gs
)
544 void *stack
= (void *)(ESP_sig(sigcontext
) & ~3);
545 TEB
*teb
= get_current_teb();
546 struct ntdll_thread_regs
*thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
548 /* get %fs and %gs at time of the fault */
550 *fs
= LOWORD(FS_sig(sigcontext
));
555 *gs
= LOWORD(GS_sig(sigcontext
));
560 wine_set_fs( thread_regs
->fs
);
562 /* now restore a proper %gs for the fault handler */
563 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
564 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
567 * Win16 or DOS protected mode. Note that during switch
568 * from 16-bit mode to linear mode, CS may be set to system
569 * segment before FS is restored. Fortunately, in this case
570 * SS is still non-system segment. This is why both CS and SS
573 wine_set_gs( thread_regs
->gs
);
574 stack
= teb
->WOW32Reserved
;
577 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
579 unsigned int *int_stack
= stack
;
580 /* fetch the saved %gs from the stack */
581 wine_set_gs( int_stack
[0] );
584 else /* 32-bit mode */
587 wine_set_gs( GS_sig(sigcontext
) );
594 /***********************************************************************
597 * Save the thread FPU context.
599 static inline void save_fpu( CONTEXT
*context
)
602 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
603 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
608 /***********************************************************************
611 * Restore the FPU context to a sigcontext.
613 static inline void restore_fpu( const CONTEXT
*context
)
615 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
616 /* reset the current interrupt status */
617 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
619 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
620 #endif /* __GNUC__ */
624 /***********************************************************************
627 * Build a context structure from the signal info.
629 static inline void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
, WORD fs
, WORD gs
)
631 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
633 memset(context
, 0, sizeof(*context
));
634 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
635 context
->Eax
= EAX_sig(sigcontext
);
636 context
->Ebx
= EBX_sig(sigcontext
);
637 context
->Ecx
= ECX_sig(sigcontext
);
638 context
->Edx
= EDX_sig(sigcontext
);
639 context
->Esi
= ESI_sig(sigcontext
);
640 context
->Edi
= EDI_sig(sigcontext
);
641 context
->Ebp
= EBP_sig(sigcontext
);
642 context
->EFlags
= EFL_sig(sigcontext
);
643 context
->Eip
= EIP_sig(sigcontext
);
644 context
->Esp
= ESP_sig(sigcontext
);
645 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
646 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
647 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
650 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
651 context
->Dr0
= regs
->dr0
;
652 context
->Dr1
= regs
->dr1
;
653 context
->Dr2
= regs
->dr2
;
654 context
->Dr3
= regs
->dr3
;
655 context
->Dr6
= regs
->dr6
;
656 context
->Dr7
= regs
->dr7
;
659 if (FPU_sig(sigcontext
))
661 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
662 context
->FloatSave
= *FPU_sig(sigcontext
);
672 /***********************************************************************
675 * Restore the signal info from the context.
677 static inline void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
679 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
681 regs
->dr0
= context
->Dr0
;
682 regs
->dr1
= context
->Dr1
;
683 regs
->dr2
= context
->Dr2
;
684 regs
->dr3
= context
->Dr3
;
685 regs
->dr6
= context
->Dr6
;
686 regs
->dr7
= context
->Dr7
;
687 EAX_sig(sigcontext
) = context
->Eax
;
688 EBX_sig(sigcontext
) = context
->Ebx
;
689 ECX_sig(sigcontext
) = context
->Ecx
;
690 EDX_sig(sigcontext
) = context
->Edx
;
691 ESI_sig(sigcontext
) = context
->Esi
;
692 EDI_sig(sigcontext
) = context
->Edi
;
693 EBP_sig(sigcontext
) = context
->Ebp
;
694 EFL_sig(sigcontext
) = context
->EFlags
;
695 EIP_sig(sigcontext
) = context
->Eip
;
696 ESP_sig(sigcontext
) = context
->Esp
;
697 CS_sig(sigcontext
) = context
->SegCs
;
698 DS_sig(sigcontext
) = context
->SegDs
;
699 ES_sig(sigcontext
) = context
->SegEs
;
700 SS_sig(sigcontext
) = context
->SegSs
;
702 GS_sig(sigcontext
) = context
->SegGs
;
704 wine_set_gs( context
->SegGs
);
707 FS_sig(sigcontext
) = context
->SegFs
;
709 wine_set_fs( context
->SegFs
);
713 if (FPU_sig(sigcontext
))
715 *FPU_sig(sigcontext
) = context
->FloatSave
;
720 restore_fpu( context
);
725 /***********************************************************************
728 * Register function to get the context of the current thread.
730 void WINAPI
__regs_get_cpu_context( CONTEXT
*context
, CONTEXT
*regs
)
735 DEFINE_REGS_ENTRYPOINT( get_cpu_context
, 4, 4 );
738 /***********************************************************************
741 * Set the new CPU context. Used by NtSetContextThread.
743 void set_cpu_context( const CONTEXT
*context
)
745 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
747 if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
749 if (flags
& CONTEXT_DEBUG_REGISTERS
)
751 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
752 regs
->dr0
= context
->Dr0
;
753 regs
->dr1
= context
->Dr1
;
754 regs
->dr2
= context
->Dr2
;
755 regs
->dr3
= context
->Dr3
;
756 regs
->dr6
= context
->Dr6
;
757 regs
->dr7
= context
->Dr7
;
759 if (flags
& CONTEXT_FULL
)
761 if ((flags
& CONTEXT_FULL
) != (CONTEXT_FULL
& ~CONTEXT_i386
))
762 FIXME( "setting partial context (%x) not supported\n", flags
);
764 __wine_call_from_32_restore_regs( context
);
769 /***********************************************************************
770 * is_privileged_instr
772 * Check if the fault location is a privileged instruction.
773 * Based on the instruction emulation code in dlls/kernel/instr.c.
775 static inline DWORD
is_privileged_instr( CONTEXT86
*context
)
778 unsigned int prefix_count
= 0;
780 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
781 instr
= (BYTE
*)context
->Eip
;
783 for (;;) switch(*instr
)
785 /* instruction prefixes */
786 case 0x2e: /* %cs: */
787 case 0x36: /* %ss: */
788 case 0x3e: /* %ds: */
789 case 0x26: /* %es: */
790 case 0x64: /* %fs: */
791 case 0x65: /* %gs: */
792 case 0x66: /* opcode size */
793 case 0x67: /* addr size */
794 case 0xf0: /* lock */
795 case 0xf2: /* repne */
796 case 0xf3: /* repe */
797 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
801 case 0x0f: /* extended instruction */
804 case 0x20: /* mov crX, reg */
805 case 0x21: /* mov drX, reg */
806 case 0x22: /* mov reg, crX */
807 case 0x23: /* mov reg drX */
808 return EXCEPTION_PRIV_INSTRUCTION
;
811 case 0x6c: /* insb (%dx) */
812 case 0x6d: /* insl (%dx) */
813 case 0x6e: /* outsb (%dx) */
814 case 0x6f: /* outsl (%dx) */
815 case 0xcd: /* int $xx */
816 case 0xe4: /* inb al,XX */
817 case 0xe5: /* in (e)ax,XX */
818 case 0xe6: /* outb XX,al */
819 case 0xe7: /* out XX,(e)ax */
820 case 0xec: /* inb (%dx),%al */
821 case 0xed: /* inl (%dx),%eax */
822 case 0xee: /* outb %al,(%dx) */
823 case 0xef: /* outl %eax,(%dx) */
827 return EXCEPTION_PRIV_INSTRUCTION
;
834 #include "pshpack1.h"
837 DWORD movl
; /* movl this,4(%esp) */
839 BYTE jmp
; /* jmp func */
844 /**********************************************************************
847 * Check if code destination is an ATL thunk, and emulate it if so.
849 static BOOL
check_atl_thunk( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
851 const struct atl_thunk
*thunk
= (const struct atl_thunk
*)rec
->ExceptionInformation
[1];
856 if (thunk
->movl
== 0x042444c7 && thunk
->jmp
== 0xe9)
858 *((DWORD
*)context
->Esp
+ 1) = thunk
->this;
859 context
->Eip
= (DWORD_PTR
)(&thunk
->func
+ 1) + thunk
->func
;
860 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
861 thunk
, context
->Eip
, *((DWORD
*)context
->Esp
+ 1) );
874 /***********************************************************************
877 * Setup a proper stack frame for the raise function, and modify the
878 * sigcontext so that the return from the signal handler will call
879 * the raise function.
881 static EXCEPTION_RECORD
*setup_exception( SIGCONTEXT
*sigcontext
, raise_func func
)
885 void *ret_addr
; /* return address from raise_func */
886 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
887 CONTEXT
*context_ptr
; /* second arg for raise_func */
889 EXCEPTION_RECORD rec
;
896 stack
= init_handler( sigcontext
, &fs
, &gs
);
898 /* stack sanity checks */
900 if ((char *)stack
>= (char *)get_signal_stack() &&
901 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
903 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
904 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
905 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
906 NtCurrentTeb()->Tib
.StackBase
);
907 server_abort_thread(1);
910 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
911 (char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
||
912 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
914 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)stack
;
917 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p\n",
918 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
919 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
920 NtCurrentTeb()->Tib
.StackBase
);
921 server_abort_thread(1);
923 else WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
924 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
925 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
926 NtCurrentTeb()->Tib
.StackBase
);
929 stack
--; /* push the stack_layout structure */
930 #ifdef HAVE_VALGRIND_MEMCHECK_H
931 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
933 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
934 stack
->rec_ptr
= &stack
->rec
;
935 stack
->context_ptr
= &stack
->context
;
937 stack
->rec
.ExceptionRecord
= NULL
;
938 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
939 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
940 stack
->rec
.NumberParameters
= 0;
942 save_context( &stack
->context
, sigcontext
, fs
, gs
);
944 /* now modify the sigcontext to return to the raise function */
945 ESP_sig(sigcontext
) = (DWORD
)stack
;
946 EIP_sig(sigcontext
) = (DWORD
)func
;
947 /* clear single-step and align check flag */
948 EFL_sig(sigcontext
) &= ~(0x100|0x40000);
949 CS_sig(sigcontext
) = wine_get_cs();
950 DS_sig(sigcontext
) = wine_get_ds();
951 ES_sig(sigcontext
) = wine_get_es();
952 FS_sig(sigcontext
) = wine_get_fs();
953 GS_sig(sigcontext
) = wine_get_gs();
954 SS_sig(sigcontext
) = wine_get_ss();
956 return stack
->rec_ptr
;
960 /***********************************************************************
961 * get_exception_context
963 * Get a pointer to the context built by setup_exception.
965 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
967 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
971 /**********************************************************************
974 * Get the FPU exception code from the FPU status.
976 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
978 DWORD status
= context
->FloatSave
.StatusWord
;
980 if (status
& 0x01) /* IE */
982 if (status
& 0x40) /* SF */
983 return EXCEPTION_FLT_STACK_CHECK
;
985 return EXCEPTION_FLT_INVALID_OPERATION
;
987 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
988 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
989 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
990 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
991 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
992 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
996 /**********************************************************************
997 * raise_segv_exception
999 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1001 switch(rec
->ExceptionCode
)
1003 case EXCEPTION_ACCESS_VIOLATION
:
1004 if (rec
->NumberParameters
== 2)
1006 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
&& check_atl_thunk( rec
, context
))
1008 rec
->ExceptionCode
= VIRTUAL_HandleFault( (void *)rec
->ExceptionInformation
[1] );
1011 case EXCEPTION_DATATYPE_MISALIGNMENT
:
1012 /* FIXME: pass through exception handler first? */
1013 if (context
->EFlags
& 0x00040000)
1015 /* Disable AC flag, return */
1016 context
->EFlags
&= ~0x00040000;
1021 __regs_RtlRaiseException( rec
, context
);
1023 NtSetContextThread( GetCurrentThread(), context
);
1027 /**********************************************************************
1028 * raise_trap_exception
1030 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1032 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
1034 if (context
->EFlags
& 0x100)
1036 context
->EFlags
&= ~0x100; /* clear single-step flag */
1038 else /* hardware breakpoint, fetch the debug registers */
1040 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1041 NtGetContextThread(GetCurrentThread(), context
);
1043 * - a bp from a debug register
1044 * - a single step interrupt at popf instruction, which just has
1046 * - someone did a kill(SIGTRAP) on us, and we shall return
1047 * a breakpoint, not a single step exception
1049 if ( !(context
->Dr6
& 0xf) && !(context
->Dr6
& 0x4000) )
1050 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1051 context
->ContextFlags
|= CONTEXT_FULL
; /* restore flags */
1055 __regs_RtlRaiseException( rec
, context
);
1056 NtSetContextThread( GetCurrentThread(), context
);
1060 /**********************************************************************
1063 * Generic raise function for exceptions that don't need special treatment.
1065 static void WINAPI
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1067 __regs_RtlRaiseException( rec
, context
);
1068 NtSetContextThread( GetCurrentThread(), context
);
1073 /**********************************************************************
1074 * raise_vm86_sti_exception
1076 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1078 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1079 NtCurrentTeb()->vm86_pending
|= VIP_MASK
;
1081 if (ntdll_get_thread_data()->vm86_ptr
)
1083 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
1084 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
1085 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
1086 /* exiting from VM86, can't throw */
1089 merge_vm86_pending_flags( rec
);
1091 else if (NtCurrentTeb()->dpmi_vif
&&
1092 !wine_ldt_is_system(context
->SegCs
) &&
1093 !wine_ldt_is_system(context
->SegSs
))
1095 /* Executing DPMI code and virtual interrupts are enabled. */
1096 NtCurrentTeb()->vm86_pending
= 0;
1097 __regs_RtlRaiseException( rec
, context
);
1100 NtSetContextThread( GetCurrentThread(), context
);
1104 /**********************************************************************
1107 * Handler for SIGUSR2.
1108 * We use it to signal that the running __wine_enter_vm86() should
1109 * immediately set VIP_MASK, causing pending events to be handled
1110 * as early as possible.
1112 static void usr2_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1114 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_vm86_sti_exception
);
1115 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
1117 #endif /* __HAVE_VM86 */
1120 /**********************************************************************
1123 * Handler for SIGSEGV and related errors.
1125 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1127 SIGCONTEXT
*context
= sigcontext
;
1128 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_segv_exception
);
1130 switch(get_trap_code(context
))
1132 case TRAP_x86_OFLOW
: /* Overflow exception */
1133 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1135 case TRAP_x86_BOUND
: /* Bound range exception */
1136 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1138 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1139 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1141 case TRAP_x86_STKFLT
: /* Stack fault */
1142 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1144 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1145 case TRAP_x86_PROTFLT
: /* General protection fault */
1146 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1148 WORD err
= get_error_code(context
);
1149 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( get_exception_context(rec
) ))) break;
1150 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1151 rec
->NumberParameters
= 2;
1152 rec
->ExceptionInformation
[0] = 0;
1153 /* if error contains a LDT selector, use that as fault address */
1154 if ((err
& 7) == 4 && !wine_ldt_is_system( err
| 7 ))
1155 rec
->ExceptionInformation
[1] = err
& ~7;
1157 rec
->ExceptionInformation
[1] = 0xffffffff;
1160 case TRAP_x86_PAGEFLT
: /* Page fault */
1161 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1162 rec
->NumberParameters
= 2;
1163 rec
->ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
1164 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
1166 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
1167 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1170 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
1172 case TRAP_x86_NMI
: /* NMI interrupt */
1173 case TRAP_x86_DNA
: /* Device not available exception */
1174 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
1175 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
1176 case TRAP_x86_MCHK
: /* Machine check exception */
1177 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
1178 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1184 /**********************************************************************
1187 * Handler for SIGTRAP.
1189 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1191 SIGCONTEXT
*context
= sigcontext
;
1192 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_trap_exception
);
1194 switch(get_trap_code(context
))
1196 case TRAP_x86_TRCTRAP
: /* Single-step exception */
1197 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1199 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
1200 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
1203 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1209 /**********************************************************************
1212 * Handler for SIGFPE.
1214 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1216 CONTEXT
*win_context
;
1217 SIGCONTEXT
*context
= sigcontext
;
1218 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_exception
);
1220 win_context
= get_exception_context( rec
);
1222 switch(get_trap_code(context
))
1224 case TRAP_x86_DIVIDE
: /* Division by zero exception */
1225 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1227 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
1228 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1230 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
1231 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1232 rec
->ExceptionCode
= get_fpu_code( win_context
);
1234 case TRAP_x86_CACHEFLT
: /* SIMD exception */
1236 * Behaviour only tested for divide-by-zero exceptions
1237 * Check for other SIMD exceptions as well */
1238 if(siginfo
->si_code
!= FPE_FLTDIV
)
1239 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1242 rec
->ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
1243 rec
->NumberParameters
= 1;
1244 /* no idea what meaning is actually behind this but thats what native does */
1245 rec
->ExceptionInformation
[0] = 0;
1248 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
1249 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1255 /**********************************************************************
1258 * Handler for SIGINT.
1260 * FIXME: should not be calling external functions on the signal stack.
1262 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1265 init_handler( sigcontext
, &fs
, &gs
);
1266 if (!dispatch_signal(SIGINT
))
1268 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_exception
);
1269 rec
->ExceptionCode
= CONTROL_C_EXIT
;
1273 /**********************************************************************
1276 * Handler for SIGABRT.
1278 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1280 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_exception
);
1281 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
1282 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
1286 /**********************************************************************
1289 * Handler for SIGTERM.
1291 static void term_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1294 init_handler( sigcontext
, &fs
, &gs
);
1295 server_abort_thread(0);
1299 /**********************************************************************
1302 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1304 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1309 init_handler( sigcontext
, &fs
, &gs
);
1310 save_context( &context
, sigcontext
, fs
, gs
);
1311 wait_suspend( &context
);
1312 restore_context( &context
, sigcontext
);
1316 /**********************************************************************
1317 * get_signal_stack_total_size
1319 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1320 * Must be a power of two.
1322 size_t get_signal_stack_total_size(void)
1324 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
1326 if (!signal_stack_size
)
1328 size_t size
= 4096, min_size
= teb_size
+ max( MINSIGSTKSZ
, 4096 );
1329 /* find the first power of two not smaller than min_size */
1330 while (size
< min_size
) size
*= 2;
1331 signal_stack_mask
= size
- 1;
1332 signal_stack_size
= size
- teb_size
;
1334 return signal_stack_size
+ teb_size
;
1338 /***********************************************************************
1339 * __wine_set_signal_handler (NTDLL.@)
1341 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1343 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1344 if (handlers
[sig
] != NULL
) return -2;
1345 handlers
[sig
] = wsh
;
1350 /**********************************************************************
1353 BOOL
SIGNAL_Init(void)
1355 struct sigaction sig_act
;
1357 #ifdef HAVE_SIGALTSTACK
1361 int mib
[2], val
= 1;
1364 mib
[1] = KERN_THALTSTACK
;
1365 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
1368 ss
.ss_sp
= get_signal_stack();
1369 ss
.ss_size
= signal_stack_size
;
1371 if (sigaltstack(&ss
, NULL
) == -1)
1373 perror( "sigaltstack" );
1376 #endif /* HAVE_SIGALTSTACK */
1378 sig_act
.sa_mask
= server_block_set
;
1379 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1381 sig_act
.sa_flags
|= SA_ONSTACK
;
1384 sig_act
.sa_sigaction
= int_handler
;
1385 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
1386 sig_act
.sa_sigaction
= fpe_handler
;
1387 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
1388 sig_act
.sa_sigaction
= abrt_handler
;
1389 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
1390 sig_act
.sa_sigaction
= term_handler
;
1391 if (sigaction( SIGTERM
, &sig_act
, NULL
) == -1) goto error
;
1392 sig_act
.sa_sigaction
= usr1_handler
;
1393 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
1395 sig_act
.sa_sigaction
= segv_handler
;
1396 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
1397 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
1399 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
1403 sig_act
.sa_sigaction
= trap_handler
;
1404 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
1408 sig_act
.sa_sigaction
= usr2_handler
;
1409 if (sigaction( SIGUSR2
, &sig_act
, NULL
) == -1) goto error
;
1415 perror("sigaction");
1421 /**********************************************************************
1422 * __wine_enter_vm86 (NTDLL.@)
1424 * Enter vm86 mode with the specified register context.
1426 void __wine_enter_vm86( CONTEXT
*context
)
1428 EXCEPTION_RECORD rec
;
1430 struct vm86plus_struct vm86
;
1432 memset( &vm86
, 0, sizeof(vm86
) );
1435 restore_vm86_context( context
, &vm86
);
1437 ntdll_get_thread_data()->vm86_ptr
= &vm86
;
1438 merge_vm86_pending_flags( &rec
);
1440 res
= vm86_enter( &ntdll_get_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1447 save_vm86_context( context
, &vm86
);
1449 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1450 rec
.ExceptionRecord
= NULL
;
1451 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1452 rec
.NumberParameters
= 0;
1454 switch(VM86_TYPE(res
))
1456 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1457 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
1458 raise_segv_exception( &rec
, context
);
1460 case VM86_TRAP
: /* return due to DOS-debugger request */
1461 switch(VM86_ARG(res
))
1463 case TRAP_x86_TRCTRAP
: /* Single-step exception */
1464 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1466 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
1467 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
1470 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
1474 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1475 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1476 rec
.NumberParameters
= 1;
1477 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1479 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1480 context
->EFlags
|= VIF_MASK
;
1481 context
->EFlags
&= ~VIP_MASK
;
1482 NtCurrentTeb()->vm86_pending
= 0;
1483 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1485 case VM86_PICRETURN
: /* return due to pending PIC request */
1486 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1488 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
1490 WINE_ERR( "unhandled result from vm86 mode %x\n", res
);
1493 __regs_RtlRaiseException( &rec
, context
);
1497 #else /* __HAVE_VM86 */
1498 /**********************************************************************
1499 * __wine_enter_vm86 (NTDLL.@)
1501 void __wine_enter_vm86( CONTEXT
*context
)
1503 MESSAGE("vm86 mode not supported on this platform\n");
1505 #endif /* __HAVE_VM86 */
1507 /**********************************************************************
1508 * DbgBreakPoint (NTDLL.@)
1510 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret")
1512 /**********************************************************************
1513 * DbgUserBreakPoint (NTDLL.@)
1515 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret")
1518 /**********************************************************************
1519 * EXC_CallHandler (internal)
1521 * Some exception handlers depend on EBP to have a fixed position relative to
1522 * the exception frame.
1523 * Shrinker depends on (*1) doing what it does,
1524 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1525 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1526 * function calling the handler having only 5 parameters (*4).
1528 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
1530 " movl %esp, %ebp\n"
1532 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1538 " call " __ASM_NAME("call_exception_handler") "\n"
1543 __ASM_GLOBAL_FUNC(call_exception_handler
,
1545 " movl %esp, %ebp\n"
1547 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1548 " pushl %edx\n" /* handler be handled by... */
1550 " pushl (0)\n" /* nested_handler (passed in edx). */
1552 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1557 " movl 24(%ebp), %ecx\n" /* (*1) */
1558 " call *%ecx\n" /* call handler. (*2) */
1560 " movl (0), %esp\n" /* restore previous... (*3) */
1562 " popl (0)\n" /* exception frame. */
1563 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1565 " ret $20\n" /* (*4) */
1567 #endif /* __i386__ */