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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
46 #ifdef HAVE_SYS_VM86_H
47 # include <sys/vm86.h>
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
58 #include "wine/library.h"
59 #include "ntdll_misc.h"
61 /***********************************************************************
62 * signal context platform-specific definitions
68 unsigned short sc_gs
, __gsh
;
69 unsigned short sc_fs
, __fsh
;
70 unsigned short sc_es
, __esh
;
71 unsigned short sc_ds
, __dsh
;
80 unsigned long sc_trapno
;
83 unsigned short sc_cs
, __csh
;
84 unsigned long sc_eflags
;
85 unsigned long esp_at_signal
;
86 unsigned short sc_ss
, __ssh
;
88 unsigned long oldmask
;
92 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
93 #define HANDLER_CONTEXT (&__context)
95 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
96 struct kernel_sigaction
98 void (*ksa_handler
)();
99 unsigned long ksa_mask
;
100 unsigned long ksa_flags
;
104 /* Similar to the sigaction function in libc, except it leaves alone the
105 restorer field, which is used to specify the signal stack address */
106 static inline int wine_sigaction( int sig
, struct kernel_sigaction
*new,
107 struct kernel_sigaction
*old
)
109 __asm__
__volatile__( "pushl %%ebx\n\t"
114 : "0" (SYS_sigaction
), "r" (sig
), "c" (new), "d" (old
) );
115 if (sig
>=0) return 0;
120 #ifdef HAVE_SIGALTSTACK
121 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
122 static inline int wine_sigaltstack( const struct sigaltstack
*new,
123 struct sigaltstack
*old
)
126 __asm__
__volatile__( "pushl %%ebx\n\t"
131 : "0" (SYS_sigaltstack
), "r" (new), "c" (old
) );
132 if (ret
>= 0) return 0;
138 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
140 int vm86_enter( void **vm86_ptr
);
141 void vm86_return(void);
142 void vm86_return_end(void);
143 __ASM_GLOBAL_FUNC(vm86_enter
,
145 "movl %esp, %ebp\n\t"
146 "movl $166,%eax\n\t" /*SYS_vm86*/
147 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
148 "movl (%ecx),%ecx\n\t"
150 "movl $1,%ebx\n\t" /*VM86_ENTER*/
151 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
155 ".globl " __ASM_NAME("vm86_return") "\n\t"
156 __ASM_FUNC("vm86_return") "\n"
157 __ASM_NAME("vm86_return") ":\n\t"
163 "testl %eax,%eax\n\t"
165 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
166 "je " __ASM_NAME("vm86_enter") "\n\t"
168 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
170 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
171 __ASM_FUNC("vm86_return_end") "\n"
172 __ASM_NAME("vm86_return_end") ":\n\t"
175 #ifdef HAVE_SYS_VM86_H
183 #define EAX_sig(context) ((context)->tf_eax)
184 #define EBX_sig(context) ((context)->tf_ebx)
185 #define ECX_sig(context) ((context)->tf_ecx)
186 #define EDX_sig(context) ((context)->tf_edx)
187 #define ESI_sig(context) ((context)->tf_esi)
188 #define EDI_sig(context) ((context)->tf_edi)
189 #define EBP_sig(context) ((context)->tf_ebp)
191 #define CS_sig(context) ((context)->tf_cs)
192 #define DS_sig(context) ((context)->tf_ds)
193 #define ES_sig(context) ((context)->tf_es)
194 #define SS_sig(context) ((context)->tf_ss)
196 #include <machine/frame.h>
197 typedef struct trapframe SIGCONTEXT
;
199 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
200 #define HANDLER_CONTEXT __context
202 #define EFL_sig(context) ((context)->tf_eflags)
204 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
205 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
209 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
211 typedef struct sigcontext SIGCONTEXT
;
213 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
214 #define HANDLER_CONTEXT __context
218 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
221 #include <sys/regset.h>
225 #include <sys/ucontext.h>
227 typedef struct ucontext SIGCONTEXT
;
229 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
230 #define HANDLER_CONTEXT __context
232 #endif /* svr4 || SCO_DS */
238 unsigned long ContextFlags
;
239 FLOATING_SAVE_AREA sc_float
;
244 unsigned long sc_edi
;
245 unsigned long sc_esi
;
246 unsigned long sc_eax
;
247 unsigned long sc_ebx
;
248 unsigned long sc_ecx
;
249 unsigned long sc_edx
;
250 unsigned long sc_ebp
;
251 unsigned long sc_eip
;
253 unsigned long sc_eflags
;
254 unsigned long sc_esp
;
262 /* FIXME: This section is just here so it can compile, it's most likely
263 * completely wrong. */
267 unsigned short sc_gs
, __gsh
;
268 unsigned short sc_fs
, __fsh
;
269 unsigned short sc_es
, __esh
;
270 unsigned short sc_ds
, __dsh
;
271 unsigned long sc_edi
;
272 unsigned long sc_esi
;
273 unsigned long sc_ebp
;
274 unsigned long sc_esp
;
275 unsigned long sc_ebx
;
276 unsigned long sc_edx
;
277 unsigned long sc_ecx
;
278 unsigned long sc_eax
;
279 unsigned long sc_trapno
;
280 unsigned long sc_err
;
281 unsigned long sc_eip
;
282 unsigned short sc_cs
, __csh
;
283 unsigned long sc_eflags
;
284 unsigned long esp_at_signal
;
285 unsigned short sc_ss
, __ssh
;
287 unsigned long oldmask
;
291 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
292 #define HANDLER_CONTEXT (&__context)
294 #endif /* __CYGWIN__ */
297 # include <sys/ucontext.h>
298 # include <sys/types.h>
301 typedef siginfo_t siginfo
;
302 typedef struct ucontext SIGCONTEXT
;
304 # define HANDLER_DEF(name) void name( int __signal, siginfo *__siginfo, SIGCONTEXT *__context )
305 # define HANDLER_CONTEXT (__context)
307 #endif /* __APPLE__ */
309 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
310 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
312 #define EAX_sig(context) ((context)->sc_eax)
313 #define EBX_sig(context) ((context)->sc_ebx)
314 #define ECX_sig(context) ((context)->sc_ecx)
315 #define EDX_sig(context) ((context)->sc_edx)
316 #define ESI_sig(context) ((context)->sc_esi)
317 #define EDI_sig(context) ((context)->sc_edi)
318 #define EBP_sig(context) ((context)->sc_ebp)
320 #define CS_sig(context) ((context)->sc_cs)
321 #define DS_sig(context) ((context)->sc_ds)
322 #define ES_sig(context) ((context)->sc_es)
323 #define FS_sig(context) ((context)->sc_fs)
324 #define GS_sig(context) ((context)->sc_gs)
325 #define SS_sig(context) ((context)->sc_ss)
327 #define TRAP_sig(context) ((context)->sc_trapno)
330 #define ERROR_sig(context) ((context)->sc_err)
334 #define ERROR_sig(context) ((context)->sc_err)
335 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
336 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->cr2)
340 #define EFL_sig(context) ((context)->sc_efl)
341 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
342 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->sc_err)
344 #define EFL_sig(context) ((context)->sc_eflags)
347 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
348 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
350 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
352 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
358 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
359 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
360 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
361 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
362 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
363 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
364 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
366 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
367 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
368 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
369 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
371 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
372 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
374 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
376 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
378 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
380 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
382 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
385 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
388 #define FAULT_ADDRESS (__siginfo->si_addr)
390 #endif /* svr4 || SCO_DS */
393 #define EAX_sig(context) ((context)->uc_mcontext.sc_eax)
394 #define EBX_sig(context) ((context)->uc_mcontext.sc_ebx)
395 #define ECX_sig(context) ((context)->uc_mcontext.sc_ecx)
396 #define EDX_sig(context) ((context)->uc_mcontext.sc_edx)
397 #define ESI_sig(context) ((context)->uc_mcontext.sc_esi)
398 #define EDI_sig(context) ((context)->uc_mcontext.sc_edi)
399 #define EBP_sig(context) ((context)->uc_mcontext.sc_ebp)
401 #define CS_sig(context) ((context)->uc_mcontext.sc_cs)
402 #define DS_sig(context) ((context)->uc_mcontext.sc_ds)
403 #define ES_sig(context) ((context)->uc_mcontext.sc_es)
404 #define FS_sig(context) ((context)->uc_mcontext.sc_fs)
405 #define GS_sig(context) ((context)->uc_mcontext.sc_gs)
406 #define SS_sig(context) ((context)->uc_mcontext.sc_ss)
408 #define EFL_sig(context) ((context)->uc_mcontext.sc_eflags)
410 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.sc_eip))
411 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.sc_esp))
413 #endif /* __APPLE__ */
415 /* exception code definitions (already defined by FreeBSD/NetBSD) */
416 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
417 #define T_DIVIDE 0 /* Division by zero exception */
418 #define T_TRCTRAP 1 /* Single-step exception */
419 #define T_NMI 2 /* NMI interrupt */
420 #define T_BPTFLT 3 /* Breakpoint exception */
421 #define T_OFLOW 4 /* Overflow exception */
422 #define T_BOUND 5 /* Bound range exception */
423 #define T_PRIVINFLT 6 /* Invalid opcode exception */
424 #define T_DNA 7 /* Device not available exception */
425 #define T_DOUBLEFLT 8 /* Double fault exception */
426 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
427 #define T_TSSFLT 10 /* Invalid TSS exception */
428 #define T_SEGNPFLT 11 /* Segment not present exception */
429 #define T_STKFLT 12 /* Stack fault */
430 #define T_PROTFLT 13 /* General protection fault */
431 #define T_PAGEFLT 14 /* Page fault */
432 #define T_RESERVED 15 /* Unknown exception */
433 #define T_ARITHTRAP 16 /* Floating point exception */
434 #define T_ALIGNFLT 17 /* Alignment check exception */
435 #define T_MCHK 18 /* Machine check exception */
436 #define T_CACHEFLT 19 /* Cache flush exception */
438 #if defined(__NetBSD__)
439 #define T_MCHK 19 /* Machine check exception */
442 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
444 #include "wine/exception.h"
445 #include "wine/debug.h"
447 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
449 typedef int (*wine_signal_handler
)(unsigned int sig
);
451 static wine_signal_handler handlers
[256];
453 extern void WINAPI
EXC_RtlRaiseException( PEXCEPTION_RECORD
, PCONTEXT
);
454 extern void DECLSPEC_NORETURN
__wine_call_from_32_restore_regs( CONTEXT context
);
457 /***********************************************************************
460 inline static int dispatch_signal(unsigned int sig
)
462 if (handlers
[sig
] == NULL
) return 0;
463 return handlers
[sig
](sig
);
467 /***********************************************************************
470 * Get the trap code for a signal.
472 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
475 return TRAP_sig(sigcontext
);
477 return T_UNKNOWN
; /* unknown trap code */
481 /***********************************************************************
484 * Get the error code for a signal.
486 static inline int get_error_code( const SIGCONTEXT
*sigcontext
)
489 return ERROR_sig(sigcontext
);
495 /***********************************************************************
498 * Get the base of the signal stack for the current thread.
500 static inline void *get_signal_stack(void)
502 return (char *)NtCurrentTeb() + 4096;
506 /***********************************************************************
509 * Get the current teb based on the stack pointer.
511 static inline TEB
*get_current_teb(void)
514 __asm__("movl %%esp,%0" : "=g" (esp
) );
515 return (TEB
*)((esp
& ~4095) - 4096);
520 /***********************************************************************
523 * Set the register values from a vm86 structure.
525 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
527 context
->Eax
= vm86
->regs
.eax
;
528 context
->Ebx
= vm86
->regs
.ebx
;
529 context
->Ecx
= vm86
->regs
.ecx
;
530 context
->Edx
= vm86
->regs
.edx
;
531 context
->Esi
= vm86
->regs
.esi
;
532 context
->Edi
= vm86
->regs
.edi
;
533 context
->Esp
= vm86
->regs
.esp
;
534 context
->Ebp
= vm86
->regs
.ebp
;
535 context
->Eip
= vm86
->regs
.eip
;
536 context
->SegCs
= vm86
->regs
.cs
;
537 context
->SegDs
= vm86
->regs
.ds
;
538 context
->SegEs
= vm86
->regs
.es
;
539 context
->SegFs
= vm86
->regs
.fs
;
540 context
->SegGs
= vm86
->regs
.gs
;
541 context
->SegSs
= vm86
->regs
.ss
;
542 context
->EFlags
= vm86
->regs
.eflags
;
546 /***********************************************************************
547 * restore_vm86_context
549 * Build a vm86 structure from the register values.
551 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
553 vm86
->regs
.eax
= context
->Eax
;
554 vm86
->regs
.ebx
= context
->Ebx
;
555 vm86
->regs
.ecx
= context
->Ecx
;
556 vm86
->regs
.edx
= context
->Edx
;
557 vm86
->regs
.esi
= context
->Esi
;
558 vm86
->regs
.edi
= context
->Edi
;
559 vm86
->regs
.esp
= context
->Esp
;
560 vm86
->regs
.ebp
= context
->Ebp
;
561 vm86
->regs
.eip
= context
->Eip
;
562 vm86
->regs
.cs
= context
->SegCs
;
563 vm86
->regs
.ds
= context
->SegDs
;
564 vm86
->regs
.es
= context
->SegEs
;
565 vm86
->regs
.fs
= context
->SegFs
;
566 vm86
->regs
.gs
= context
->SegGs
;
567 vm86
->regs
.ss
= context
->SegSs
;
568 vm86
->regs
.eflags
= context
->EFlags
;
572 /**********************************************************************
573 * merge_vm86_pending_flags
575 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
576 * raises exception if there are pending events and VIF flag
577 * has been turned on.
579 * Called from __wine_enter_vm86 because vm86_enter
580 * doesn't check for pending events.
582 * Called from raise_vm86_sti_exception to check for
583 * pending events in a signal safe way.
585 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
587 BOOL check_pending
= TRUE
;
588 struct vm86plus_struct
*vm86
=
589 (struct vm86plus_struct
*)(NtCurrentTeb()->vm86_ptr
);
592 * In order to prevent a race when SIGUSR2 occurs while
593 * we are returning from exception handler, pending events
594 * will be rechecked after each raised exception.
596 while (check_pending
&& NtCurrentTeb()->vm86_pending
)
598 check_pending
= FALSE
;
599 NtCurrentTeb()->vm86_ptr
= NULL
;
602 * If VIF is set, throw exception.
603 * Note that SIGUSR2 may turn VIF flag off so
604 * VIF check must occur only when TEB.vm86_ptr is NULL.
606 if (vm86
->regs
.eflags
& VIF_MASK
)
609 save_vm86_context( &vcontext
, vm86
);
611 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
612 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
613 rec
->ExceptionRecord
= NULL
;
614 rec
->NumberParameters
= 0;
615 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
617 vcontext
.EFlags
&= ~VIP_MASK
;
618 NtCurrentTeb()->vm86_pending
= 0;
619 EXC_RtlRaiseException( rec
, &vcontext
);
621 restore_vm86_context( &vcontext
, vm86
);
622 check_pending
= TRUE
;
625 NtCurrentTeb()->vm86_ptr
= vm86
;
629 * Merge VIP flags in a signal safe way. This requires
630 * that the following operation compiles into atomic
633 vm86
->regs
.eflags
|= NtCurrentTeb()->vm86_pending
;
635 #endif /* __HAVE_VM86 */
638 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
641 /***********************************************************************
644 * Handler initialization when the full context is not needed.
646 static void *init_handler( const SIGCONTEXT
*sigcontext
)
648 void *stack
= (void *)ESP_sig(sigcontext
);
649 TEB
*teb
= get_current_teb();
651 wine_set_fs( teb
->teb_sel
);
653 /* now restore a proper %gs for the fault handler */
654 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
655 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
658 * Win16 or DOS protected mode. Note that during switch
659 * from 16-bit mode to linear mode, CS may be set to system
660 * segment before FS is restored. Fortunately, in this case
661 * SS is still non-system segment. This is why both CS and SS
664 wine_set_gs( teb
->gs_sel
);
665 stack
= (void *)teb
->cur_stack
;
668 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
670 unsigned int *int_stack
= stack
;
671 /* fetch the saved %gs from the stack */
672 wine_set_gs( int_stack
[0] );
675 else /* 32-bit mode */
678 wine_set_gs( GS_sig(sigcontext
) );
685 /***********************************************************************
688 * Set the FPU context from a sigcontext.
690 inline static void save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
693 if (FPU_sig(sigcontext
))
695 context
->FloatSave
= *FPU_sig(sigcontext
);
700 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
701 #endif /* __GNUC__ */
705 /***********************************************************************
708 * Restore the FPU context to a sigcontext.
710 inline static void restore_fpu( CONTEXT
*context
)
712 /* reset the current interrupt status */
713 context
->FloatSave
.StatusWord
&= context
->FloatSave
.ControlWord
| 0xffffff80;
715 /* avoid nested exceptions */
716 __asm__
__volatile__( "frstor %0; fwait" : : "m" (context
->FloatSave
) );
717 #endif /* __GNUC__ */
721 /***********************************************************************
724 * Setup a proper stack frame for the raise function, and modify the
725 * sigcontext so that the return from the signal handler will call
726 * the raise function.
728 static EXCEPTION_RECORD
*setup_exception( SIGCONTEXT
*sigcontext
, raise_func func
)
732 void *ret_addr
; /* return address from raise_func */
733 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
734 CONTEXT
*context_ptr
; /* second arg for raise_func */
735 void *dummy
; /* dummy ret addr for __wine_call_from_32_restore_regs */
737 EXCEPTION_RECORD rec
;
744 /* get %fs and %gs at time of the fault */
746 fs
= LOWORD(FS_sig(sigcontext
));
751 gs
= LOWORD(GS_sig(sigcontext
));
756 stack
= init_handler( sigcontext
);
758 /* stack sanity checks */
760 if ((char *)stack
>= (char *)get_signal_stack() &&
761 (char *)stack
< (char *)get_signal_stack() + SIGNAL_STACK_SIZE
)
763 ERR( "nested exception on signal stack in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
764 GetCurrentThreadId(), EIP_sig(sigcontext
), ESP_sig(sigcontext
),
765 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
766 server_abort_thread(1);
769 if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
+ 4096 ||
770 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
772 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
+ 4096 - (char *)stack
;
775 ERR( "stack overflow %u bytes in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
776 diff
, GetCurrentThreadId(), EIP_sig(sigcontext
), ESP_sig(sigcontext
),
777 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
778 server_abort_thread(1);
780 else WARN( "exception outside of stack limits in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
781 GetCurrentThreadId(), EIP_sig(sigcontext
), ESP_sig(sigcontext
),
782 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
785 stack
--; /* push the stack_layout structure */
786 stack
->ret_addr
= __wine_call_from_32_restore_regs
;
787 stack
->rec_ptr
= &stack
->rec
;
788 stack
->context_ptr
= &stack
->context
;
790 stack
->rec
.ExceptionRecord
= NULL
;
791 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
792 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
793 stack
->rec
.NumberParameters
= 0;
795 stack
->context
.ContextFlags
= CONTEXT_FULL
;
796 stack
->context
.Eax
= EAX_sig(sigcontext
);
797 stack
->context
.Ebx
= EBX_sig(sigcontext
);
798 stack
->context
.Ecx
= ECX_sig(sigcontext
);
799 stack
->context
.Edx
= EDX_sig(sigcontext
);
800 stack
->context
.Esi
= ESI_sig(sigcontext
);
801 stack
->context
.Edi
= EDI_sig(sigcontext
);
802 stack
->context
.Ebp
= EBP_sig(sigcontext
);
803 stack
->context
.EFlags
= EFL_sig(sigcontext
);
804 stack
->context
.Eip
= EIP_sig(sigcontext
);
805 stack
->context
.Esp
= ESP_sig(sigcontext
);
806 stack
->context
.SegCs
= LOWORD(CS_sig(sigcontext
));
807 stack
->context
.SegDs
= LOWORD(DS_sig(sigcontext
));
808 stack
->context
.SegEs
= LOWORD(ES_sig(sigcontext
));
809 stack
->context
.SegFs
= fs
;
810 stack
->context
.SegGs
= gs
;
811 stack
->context
.SegSs
= LOWORD(SS_sig(sigcontext
));
813 /* now modify the sigcontext to return to the raise function */
814 ESP_sig(sigcontext
) = (DWORD
)stack
;
815 EIP_sig(sigcontext
) = (DWORD
)func
;
816 CS_sig(sigcontext
) = wine_get_cs();
817 DS_sig(sigcontext
) = wine_get_ds();
818 ES_sig(sigcontext
) = wine_get_es();
819 FS_sig(sigcontext
) = wine_get_fs();
820 GS_sig(sigcontext
) = wine_get_gs();
821 SS_sig(sigcontext
) = wine_get_ss();
823 return stack
->rec_ptr
;
827 /***********************************************************************
828 * get_exception_context
830 * Get a pointer to the context built by setup_exception.
832 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
834 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
838 /**********************************************************************
841 * Get the FPU exception code from the FPU status.
843 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
845 DWORD status
= context
->FloatSave
.StatusWord
;
847 if (status
& 0x01) /* IE */
849 if (status
& 0x40) /* SF */
850 return EXCEPTION_FLT_STACK_CHECK
;
852 return EXCEPTION_FLT_INVALID_OPERATION
;
854 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
855 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
856 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
857 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
858 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
859 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
863 /**********************************************************************
864 * raise_segv_exception
866 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
868 switch(rec
->ExceptionCode
)
870 case EXCEPTION_ACCESS_VIOLATION
:
871 if (rec
->NumberParameters
== 2)
873 if (!(rec
->ExceptionCode
= VIRTUAL_HandleFault( (void *)rec
->ExceptionInformation
[1] )))
877 case EXCEPTION_DATATYPE_MISALIGNMENT
:
878 /* FIXME: pass through exception handler first? */
879 if (context
->EFlags
& 0x00040000)
881 /* Disable AC flag, return */
882 context
->EFlags
&= ~0x00040000;
887 EXC_RtlRaiseException( rec
, context
);
891 /**********************************************************************
892 * raise_trap_exception
894 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
896 DWORD dr0
, dr1
, dr2
, dr3
, dr6
, dr7
;
898 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
900 if (context
->EFlags
& 0x100)
902 context
->EFlags
&= ~0x100; /* clear single-step flag */
904 else /* hardware breakpoint, fetch the debug registers */
906 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
907 NtGetContextThread(GetCurrentThread(), context
);
908 /* do we really have a bp from a debug register ?
909 * if not, then someone did a kill(SIGTRAP) on us, and we
910 * shall return a breakpoint, not a single step exception
912 if (!(context
->Dr6
& 0xf)) rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
923 EXC_RtlRaiseException( rec
, context
);
925 if (dr0
!= context
->Dr0
|| dr1
!= context
->Dr1
|| dr2
!= context
->Dr2
||
926 dr3
!= context
->Dr3
|| dr6
!= context
->Dr6
|| dr7
!= context
->Dr7
)
928 /* the debug registers have changed, set the new values */
929 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
930 NtSetContextThread(GetCurrentThread(), context
);
935 /**********************************************************************
936 * raise_fpu_exception
938 static void WINAPI
raise_fpu_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
940 EXC_RtlRaiseException( rec
, context
);
941 restore_fpu( context
);
946 /**********************************************************************
947 * raise_vm86_sti_exception
949 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
951 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
952 NtCurrentTeb()->vm86_pending
|= VIP_MASK
;
954 if (NtCurrentTeb()->vm86_ptr
)
956 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
957 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
958 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
959 /* exiting from VM86, can't throw */
962 merge_vm86_pending_flags( rec
);
964 else if (NtCurrentTeb()->dpmi_vif
&&
965 !wine_ldt_is_system(context
->SegCs
) &&
966 !wine_ldt_is_system(context
->SegSs
))
968 /* Executing DPMI code and virtual interrupts are enabled. */
969 NtCurrentTeb()->vm86_pending
= 0;
970 EXC_RtlRaiseException( rec
, context
);
975 /**********************************************************************
978 * Handler for SIGUSR2.
979 * We use it to signal that the running __wine_enter_vm86() should
980 * immediately set VIP_MASK, causing pending events to be handled
981 * as early as possible.
983 static HANDLER_DEF(usr2_handler
)
985 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_vm86_sti_exception
);
986 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
988 #endif /* __HAVE_VM86 */
991 /**********************************************************************
994 * Handler for SIGSEGV and related errors.
996 static HANDLER_DEF(segv_handler
)
998 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_segv_exception
);
1000 switch(get_trap_code(HANDLER_CONTEXT
))
1002 case T_OFLOW
: /* Overflow exception */
1003 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1005 case T_BOUND
: /* Bound range exception */
1006 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1008 case T_PRIVINFLT
: /* Invalid opcode exception */
1009 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1011 case T_STKFLT
: /* Stack fault */
1012 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1014 case T_SEGNPFLT
: /* Segment not present exception */
1015 case T_PROTFLT
: /* General protection fault */
1016 case T_UNKNOWN
: /* Unknown fault code */
1017 rec
->ExceptionCode
= get_error_code(HANDLER_CONTEXT
) ? EXCEPTION_ACCESS_VIOLATION
1018 : EXCEPTION_PRIV_INSTRUCTION
;
1020 case T_PAGEFLT
: /* Page fault */
1021 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1022 #ifdef FAULT_ADDRESS
1023 rec
->NumberParameters
= 2;
1024 rec
->ExceptionInformation
[0] = (get_error_code(HANDLER_CONTEXT
) & 2) != 0;
1025 rec
->ExceptionInformation
[1] = (DWORD
)FAULT_ADDRESS
;
1028 case T_ALIGNFLT
: /* Alignment check exception */
1029 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1032 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT
) );
1034 case T_NMI
: /* NMI interrupt */
1035 case T_DNA
: /* Device not available exception */
1036 case T_DOUBLEFLT
: /* Double fault exception */
1037 case T_TSSFLT
: /* Invalid TSS exception */
1038 case T_RESERVED
: /* Unknown exception */
1039 case T_MCHK
: /* Machine check exception */
1041 case T_CACHEFLT
: /* Cache flush exception */
1043 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1049 /**********************************************************************
1052 * Handler for SIGTRAP.
1054 static HANDLER_DEF(trap_handler
)
1056 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_trap_exception
);
1058 switch(get_trap_code(HANDLER_CONTEXT
))
1060 case T_TRCTRAP
: /* Single-step exception */
1061 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1062 EFL_sig(HANDLER_CONTEXT
) &= ~0x100; /* clear single-step flag */
1064 case T_BPTFLT
: /* Breakpoint exception */
1065 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
1068 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1074 /**********************************************************************
1077 * Handler for SIGFPE.
1079 static HANDLER_DEF(fpe_handler
)
1081 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_fpu_exception
);
1084 context
= get_exception_context( rec
);
1085 save_fpu( context
, HANDLER_CONTEXT
);
1087 switch(get_trap_code(HANDLER_CONTEXT
))
1089 case T_DIVIDE
: /* Division by zero exception */
1090 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1092 case T_FPOPFLT
: /* Coprocessor segment overrun */
1093 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1095 case T_ARITHTRAP
: /* Floating point exception */
1096 case T_UNKNOWN
: /* Unknown fault code */
1097 rec
->ExceptionCode
= get_fpu_code( context
);
1100 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT
) );
1101 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1107 /**********************************************************************
1110 * Handler for SIGINT.
1112 static HANDLER_DEF(int_handler
)
1114 init_handler( HANDLER_CONTEXT
);
1115 if (!dispatch_signal(SIGINT
))
1117 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, EXC_RtlRaiseException
);
1118 rec
->ExceptionCode
= CONTROL_C_EXIT
;
1122 /**********************************************************************
1125 * Handler for SIGABRT.
1127 static HANDLER_DEF(abrt_handler
)
1129 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, EXC_RtlRaiseException
);
1130 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
1131 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
1135 /**********************************************************************
1138 * Handler for SIGTERM.
1140 static HANDLER_DEF(term_handler
)
1142 init_handler( HANDLER_CONTEXT
);
1143 server_abort_thread(0);
1147 /**********************************************************************
1150 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1152 static HANDLER_DEF(usr1_handler
)
1154 LARGE_INTEGER timeout
;
1156 init_handler( HANDLER_CONTEXT
);
1157 /* wait with 0 timeout, will only return once the thread is no longer suspended */
1158 timeout
.QuadPart
= 0;
1159 NTDLL_wait_for_multiple_objects( 0, NULL
, 0, &timeout
);
1163 /***********************************************************************
1166 * Set a signal handler
1168 static int set_handler( int sig
, int have_sigaltstack
, void (*func
)() )
1170 struct sigaction sig_act
;
1173 if (!have_sigaltstack
)
1175 struct kernel_sigaction sig_act
;
1176 sig_act
.ksa_handler
= func
;
1177 sig_act
.ksa_flags
= SA_RESTART
;
1178 sig_act
.ksa_mask
= (1 << (SIGINT
-1)) |
1180 /* point to the top of the signal stack */
1181 sig_act
.ksa_restorer
= (char *)get_signal_stack() + SIGNAL_STACK_SIZE
;
1182 return wine_sigaction( sig
, &sig_act
, NULL
);
1185 sig_act
.sa_handler
= func
;
1186 sigemptyset( &sig_act
.sa_mask
);
1187 sigaddset( &sig_act
.sa_mask
, SIGINT
);
1188 sigaddset( &sig_act
.sa_mask
, SIGUSR2
);
1190 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
1191 sig_act
.sa_flags
= SA_RESTART
;
1192 #elif defined (__svr4__) || defined(_SCO_DS)
1193 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1195 sig_act
.sa_flags
= 0;
1199 if (have_sigaltstack
) sig_act
.sa_flags
|= SA_ONSTACK
;
1201 return sigaction( sig
, &sig_act
, NULL
);
1205 /***********************************************************************
1206 * __wine_set_signal_handler (NTDLL.@)
1208 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1210 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1211 if (handlers
[sig
] != NULL
) return -2;
1212 handlers
[sig
] = wsh
;
1217 /**********************************************************************
1220 BOOL
SIGNAL_Init(void)
1222 int have_sigaltstack
= 0;
1224 #ifdef HAVE_SIGALTSTACK
1225 struct sigaltstack ss
;
1226 ss
.ss_sp
= get_signal_stack();
1227 ss
.ss_size
= SIGNAL_STACK_SIZE
;
1229 if (!sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1231 /* sigaltstack may fail because the kernel is too old, or
1232 because glibc is brain-dead. In the latter case a
1233 direct system call should succeed. */
1234 else if (!wine_sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1236 #endif /* HAVE_SIGALTSTACK */
1238 if (set_handler( SIGINT
, have_sigaltstack
, (void (*)())int_handler
) == -1) goto error
;
1239 if (set_handler( SIGFPE
, have_sigaltstack
, (void (*)())fpe_handler
) == -1) goto error
;
1240 if (set_handler( SIGSEGV
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1241 if (set_handler( SIGILL
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1242 if (set_handler( SIGABRT
, have_sigaltstack
, (void (*)())abrt_handler
) == -1) goto error
;
1243 if (set_handler( SIGTERM
, have_sigaltstack
, (void (*)())term_handler
) == -1) goto error
;
1244 if (set_handler( SIGUSR1
, have_sigaltstack
, (void (*)())usr1_handler
) == -1) goto error
;
1246 if (set_handler( SIGBUS
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1249 if (set_handler( SIGTRAP
, have_sigaltstack
, (void (*)())trap_handler
) == -1) goto error
;
1253 if (set_handler( SIGUSR2
, have_sigaltstack
, (void (*)())usr2_handler
) == -1) goto error
;
1259 perror("sigaction");
1265 /**********************************************************************
1266 * __wine_enter_vm86 (NTDLL.@)
1268 * Enter vm86 mode with the specified register context.
1270 void __wine_enter_vm86( CONTEXT
*context
)
1272 EXCEPTION_RECORD rec
;
1273 TEB
*teb
= NtCurrentTeb();
1275 struct vm86plus_struct vm86
;
1277 memset( &vm86
, 0, sizeof(vm86
) );
1280 restore_vm86_context( context
, &vm86
);
1282 teb
->vm86_ptr
= &vm86
;
1283 merge_vm86_pending_flags( &rec
);
1285 res
= vm86_enter( &teb
->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1292 save_vm86_context( context
, &vm86
);
1294 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1295 rec
.ExceptionRecord
= NULL
;
1296 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1297 rec
.NumberParameters
= 0;
1299 switch(VM86_TYPE(res
))
1301 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1302 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
1303 raise_segv_exception( &rec
, context
);
1305 case VM86_TRAP
: /* return due to DOS-debugger request */
1306 switch(VM86_ARG(res
))
1308 case T_TRCTRAP
: /* Single-step exception, single step flag is cleared by raise_trap_exception */
1309 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1311 case T_BPTFLT
: /* Breakpoint exception */
1312 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
1315 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
1318 raise_trap_exception( &rec
, context
);
1320 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1321 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1322 rec
.NumberParameters
= 1;
1323 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1325 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1326 context
->EFlags
|= VIF_MASK
;
1327 context
->EFlags
&= ~VIP_MASK
;
1328 teb
->vm86_pending
= 0;
1329 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1331 case VM86_PICRETURN
: /* return due to pending PIC request */
1332 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1334 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
1336 ERR( "unhandled result from vm86 mode %x\n", res
);
1339 EXC_RtlRaiseException( &rec
, context
);
1343 #else /* __HAVE_VM86 */
1344 /**********************************************************************
1345 * __wine_enter_vm86 (NTDLL.@)
1347 void __wine_enter_vm86( CONTEXT
*context
)
1349 MESSAGE("vm86 mode not supported on this platform\n");
1351 #endif /* __HAVE_VM86 */
1353 /**********************************************************************
1354 * DbgBreakPoint (NTDLL.@)
1356 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret");
1358 /**********************************************************************
1359 * DbgUserBreakPoint (NTDLL.@)
1361 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret");
1363 #endif /* __i386__ */