2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
17 #ifdef HAVE_SYS_PARAM_H
18 # include <sys/param.h>
23 # ifdef HAVE_SYS_SYSCALL_H
24 # include <sys/syscall.h>
28 #ifdef HAVE_SYS_VM86_H
29 # include <sys/vm86.h>
32 #ifdef HAVE_SYS_SIGNAL_H
33 # include <sys/signal.h>
39 #include "selectors.h"
41 /***********************************************************************
42 * signal context platform-specific definitions
48 unsigned short sc_gs
, __gsh
;
49 unsigned short sc_fs
, __fsh
;
50 unsigned short sc_es
, __esh
;
51 unsigned short sc_ds
, __dsh
;
60 unsigned long sc_trapno
;
63 unsigned short sc_cs
, __csh
;
64 unsigned long sc_eflags
;
65 unsigned long esp_at_signal
;
66 unsigned short sc_ss
, __ssh
;
68 unsigned long oldmask
;
72 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
73 #define HANDLER_CONTEXT (&__context)
75 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
76 struct kernel_sigaction
78 void (*ksa_handler
)();
79 unsigned long ksa_mask
;
80 unsigned long ksa_flags
;
84 /* Similar to the sigaction function in libc, except it leaves alone the
85 restorer field, which is used to specify the signal stack address */
86 static inline int wine_sigaction( int sig
, struct kernel_sigaction
*new,
87 struct kernel_sigaction
*old
)
89 __asm__
__volatile__( "pushl %%ebx\n\t"
94 : "0" (SYS_sigaction
), "r" (sig
), "c" (new), "d" (old
) );
100 #ifdef HAVE_SIGALTSTACK
101 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
102 static inline int wine_sigaltstack( const struct sigaltstack
*new,
103 struct sigaltstack
*old
)
106 __asm__
__volatile__( "pushl %%ebx\n\t"
111 : "0" (SYS_sigaltstack
), "r" (new), "c" (old
) );
112 if (ret
>= 0) return 0;
118 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
120 int vm86_enter( void **vm86_ptr
);
121 void vm86_return(void);
122 void vm86_return_end(void);
123 __ASM_GLOBAL_FUNC(vm86_enter
,
125 "movl %esp, %ebp\n\t"
126 "movl $166,%eax\n\t" /*SYS_vm86*/
127 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
128 "movl (%ecx),%ecx\n\t"
130 "movl $1,%ebx\n\t" /*VM86_ENTER*/
131 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
134 ".globl " __ASM_NAME("vm86_return") "\n\t"
135 ".type " __ASM_NAME("vm86_return") ",@function\n"
136 __ASM_NAME("vm86_return") ":\n\t"
141 "testl %eax,%eax\n\t"
143 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
144 "je " __ASM_NAME("vm86_enter") "\n\t"
146 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
148 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
149 ".type " __ASM_NAME("vm86_return_end") ",@function\n"
150 __ASM_NAME("vm86_return_end") ":\n\t"
159 #define EAX_sig(context) ((context)->tf_eax)
160 #define EBX_sig(context) ((context)->tf_ebx)
161 #define ECX_sig(context) ((context)->tf_ecx)
162 #define EDX_sig(context) ((context)->tf_edx)
163 #define ESI_sig(context) ((context)->tf_esi)
164 #define EDI_sig(context) ((context)->tf_edi)
165 #define EBP_sig(context) ((context)->tf_ebp)
167 #define CS_sig(context) ((context)->tf_cs)
168 #define DS_sig(context) ((context)->tf_ds)
169 #define ES_sig(context) ((context)->tf_es)
170 #define SS_sig(context) ((context)->tf_ss)
172 #include <machine/frame.h>
173 typedef struct trapframe SIGCONTEXT
;
175 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
176 #define HANDLER_CONTEXT __context
178 #define EFL_sig(context) ((context)->tf_eflags)
180 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
181 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
185 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
187 typedef struct sigcontext SIGCONTEXT
;
189 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
190 #define HANDLER_CONTEXT __context
194 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
197 #include <sys/regset.h>
201 #include <sys/ucontext.h>
203 typedef struct ucontext SIGCONTEXT
;
205 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
206 #define HANDLER_CONTEXT __context
208 #endif /* svr4 || SCO_DS */
214 unsigned long ContextFlags
;
215 FLOATING_SAVE_AREA sc_float
;
220 unsigned long sc_edi
;
221 unsigned long sc_esi
;
222 unsigned long sc_eax
;
223 unsigned long sc_ebx
;
224 unsigned long sc_ecx
;
225 unsigned long sc_edx
;
226 unsigned long sc_ebp
;
227 unsigned long sc_eip
;
229 unsigned long sc_eflags
;
230 unsigned long sc_esp
;
237 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
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 SS_sig(context) ((context)->sc_ss)
252 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
253 * saved by the exception handling. duh.
254 * Actually they are in -current (have been for a while), and that
255 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
256 * If you're running a system from the -stable branch older than that,
257 * like a 3.3-RELEASE, grab the patch from the ports tree:
258 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
259 * (If its not yet there when you look, go here:
260 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
263 #define FS_sig(context) ((context)->sc_fs)
264 #define GS_sig(context) ((context)->sc_gs)
268 #define FS_sig(context) ((context)->sc_fs)
269 #define GS_sig(context) ((context)->sc_gs)
270 #define CR2_sig(context) ((context)->cr2)
271 #define TRAP_sig(context) ((context)->sc_trapno)
272 #define ERROR_sig(context) ((context)->sc_err)
273 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
277 #define EFL_sig(context) ((context)->sc_eflags)
279 #define EFL_sig(context) ((context)->sc_efl)
280 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
281 #define CR2_sig(context) ((context)->sc_err)
282 #define TRAP_sig(context) ((context)->sc_trapno)
285 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
286 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
288 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
290 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
296 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
297 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
298 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
299 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
300 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
301 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
302 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
304 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
305 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
306 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
307 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
309 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
310 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
312 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
314 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
316 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
318 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
321 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
324 #endif /* svr4 || SCO_DS */
327 /* exception code definitions (already defined by FreeBSD/NetBSD) */
328 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
329 #define T_DIVIDE 0 /* Division by zero exception */
330 #define T_TRCTRAP 1 /* Single-step exception */
331 #define T_NMI 2 /* NMI interrupt */
332 #define T_BPTFLT 3 /* Breakpoint exception */
333 #define T_OFLOW 4 /* Overflow exception */
334 #define T_BOUND 5 /* Bound range exception */
335 #define T_PRIVINFLT 6 /* Invalid opcode exception */
336 #define T_DNA 7 /* Device not available exception */
337 #define T_DOUBLEFLT 8 /* Double fault exception */
338 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
339 #define T_TSSFLT 10 /* Invalid TSS exception */
340 #define T_SEGNPFLT 11 /* Segment not present exception */
341 #define T_STKFLT 12 /* Stack fault */
342 #define T_PROTFLT 13 /* General protection fault */
343 #define T_PAGEFLT 14 /* Page fault */
344 #define T_RESERVED 15 /* Unknown exception */
345 #define T_ARITHTRAP 16 /* Floating point exception */
346 #define T_ALIGNFLT 17 /* Alignment check exception */
347 #define T_MCHK 18 /* Machine check exception */
348 #define T_CACHEFLT 19 /* Cache flush exception */
350 #if defined(__NetBSD__)
351 #define T_MCHK 19 /* Machine check exception */
354 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
356 #include "wine/exception.h"
358 #include "stackframe.h"
362 #include "syslevel.h"
363 #include "debugtools.h"
365 DEFAULT_DEBUG_CHANNEL(seh
);
367 static sigset_t all_sigs
;
370 /***********************************************************************
373 * Get the trap code for a signal.
375 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
378 return TRAP_sig(sigcontext
);
380 return T_UNKNOWN
; /* unknown trap code */
384 /***********************************************************************
387 * Get the error code for a signal.
389 static inline int get_error_code( const SIGCONTEXT
*sigcontext
)
392 return ERROR_sig(sigcontext
);
398 /***********************************************************************
401 * Get the CR2 value for a signal.
403 static inline void *get_cr2_value( const SIGCONTEXT
*sigcontext
)
406 return (void *)CR2_sig(sigcontext
);
414 /***********************************************************************
417 * Set the register values from a vm86 structure.
419 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
421 context
->Eax
= vm86
->regs
.eax
;
422 context
->Ebx
= vm86
->regs
.ebx
;
423 context
->Ecx
= vm86
->regs
.ecx
;
424 context
->Edx
= vm86
->regs
.edx
;
425 context
->Esi
= vm86
->regs
.esi
;
426 context
->Edi
= vm86
->regs
.edi
;
427 context
->Esp
= vm86
->regs
.esp
;
428 context
->Ebp
= vm86
->regs
.ebp
;
429 context
->Eip
= vm86
->regs
.eip
;
430 context
->SegCs
= vm86
->regs
.cs
;
431 context
->SegDs
= vm86
->regs
.ds
;
432 context
->SegEs
= vm86
->regs
.es
;
433 context
->SegFs
= vm86
->regs
.fs
;
434 context
->SegGs
= vm86
->regs
.gs
;
435 context
->SegSs
= vm86
->regs
.ss
;
436 context
->EFlags
= vm86
->regs
.eflags
;
440 /***********************************************************************
441 * restore_vm86_context
443 * Build a vm86 structure from the register values.
445 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
447 vm86
->regs
.eax
= context
->Eax
;
448 vm86
->regs
.ebx
= context
->Ebx
;
449 vm86
->regs
.ecx
= context
->Ecx
;
450 vm86
->regs
.edx
= context
->Edx
;
451 vm86
->regs
.esi
= context
->Esi
;
452 vm86
->regs
.edi
= context
->Edi
;
453 vm86
->regs
.esp
= context
->Esp
;
454 vm86
->regs
.ebp
= context
->Ebp
;
455 vm86
->regs
.eip
= context
->Eip
;
456 vm86
->regs
.cs
= context
->SegCs
;
457 vm86
->regs
.ds
= context
->SegDs
;
458 vm86
->regs
.es
= context
->SegEs
;
459 vm86
->regs
.fs
= context
->SegFs
;
460 vm86
->regs
.gs
= context
->SegGs
;
461 vm86
->regs
.ss
= context
->SegSs
;
462 vm86
->regs
.eflags
= context
->EFlags
;
464 #endif /* __HAVE_VM86 */
467 /***********************************************************************
470 * Set the register values from a sigcontext.
472 static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
475 /* get %fs at time of the fault */
477 fs
= FS_sig(sigcontext
);
483 /* now restore a proper %fs for the fault handler */
484 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext
))) /* 16-bit mode */
486 fs
= SYSLEVEL_Win16CurrentTeb
;
489 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
491 /* fetch the saved %fs on the stack */
492 fs
= *(unsigned int *)ESP_sig(sigcontext
);
493 if (EAX_sig(sigcontext
) == VM86_EAX
) {
494 struct vm86plus_struct
*vm86
;
496 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
497 * (but we could also get if from teb->vm86_ptr) */
498 vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
499 /* get context from vm86 struct */
500 save_vm86_context( context
, vm86
);
504 #endif /* __HAVE_VM86 */
508 context
->Eax
= EAX_sig(sigcontext
);
509 context
->Ebx
= EBX_sig(sigcontext
);
510 context
->Ecx
= ECX_sig(sigcontext
);
511 context
->Edx
= EDX_sig(sigcontext
);
512 context
->Esi
= ESI_sig(sigcontext
);
513 context
->Edi
= EDI_sig(sigcontext
);
514 context
->Ebp
= EBP_sig(sigcontext
);
515 context
->EFlags
= EFL_sig(sigcontext
);
516 context
->Eip
= EIP_sig(sigcontext
);
517 context
->Esp
= ESP_sig(sigcontext
);
518 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
519 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
520 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
521 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
523 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
525 context
->SegGs
= __get_gs();
530 /***********************************************************************
533 * Build a sigcontext from the register values.
535 static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
538 /* check if exception occurred in vm86 mode */
539 if ((void *)EIP_sig(sigcontext
) == vm86_return
&&
540 IS_SELECTOR_SYSTEM(CS_sig(sigcontext
)) &&
541 EAX_sig(sigcontext
) == VM86_EAX
)
543 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
544 * (but we could also get it from teb->vm86_ptr) */
545 struct vm86plus_struct
*vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
546 restore_vm86_context( context
, vm86
);
549 #endif /* __HAVE_VM86 */
551 EAX_sig(sigcontext
) = context
->Eax
;
552 EBX_sig(sigcontext
) = context
->Ebx
;
553 ECX_sig(sigcontext
) = context
->Ecx
;
554 EDX_sig(sigcontext
) = context
->Edx
;
555 ESI_sig(sigcontext
) = context
->Esi
;
556 EDI_sig(sigcontext
) = context
->Edi
;
557 EBP_sig(sigcontext
) = context
->Ebp
;
558 EFL_sig(sigcontext
) = context
->EFlags
;
559 EIP_sig(sigcontext
) = context
->Eip
;
560 ESP_sig(sigcontext
) = context
->Esp
;
561 CS_sig(sigcontext
) = context
->SegCs
;
562 DS_sig(sigcontext
) = context
->SegDs
;
563 ES_sig(sigcontext
) = context
->SegEs
;
564 SS_sig(sigcontext
) = context
->SegSs
;
566 FS_sig(sigcontext
) = context
->SegFs
;
568 __set_fs( context
->SegFs
);
571 GS_sig(sigcontext
) = context
->SegGs
;
573 __set_gs( context
->SegGs
);
578 /***********************************************************************
581 * Set the FPU context from a sigcontext.
583 inline static void save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
586 if (FPU_sig(sigcontext
))
588 context
->FloatSave
= *FPU_sig(sigcontext
);
593 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
594 #endif /* __GNUC__ */
598 /***********************************************************************
601 * Restore the FPU context to a sigcontext.
603 inline static void restore_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
605 /* reset the current interrupt status */
606 context
->FloatSave
.StatusWord
&= context
->FloatSave
.ControlWord
| 0xffffff80;
608 if (FPU_sig(sigcontext
))
610 *FPU_sig(sigcontext
) = context
->FloatSave
;
615 /* avoid nested exceptions */
616 __asm__
__volatile__( "frstor %0; fwait" : : "m" (context
->FloatSave
) );
617 #endif /* __GNUC__ */
621 /**********************************************************************
624 * Get the FPU exception code from the FPU status.
626 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
628 DWORD status
= context
->FloatSave
.StatusWord
;
630 if (status
& 0x01) /* IE */
632 if (status
& 0x40) /* SF */
633 return EXCEPTION_FLT_STACK_CHECK
;
635 return EXCEPTION_FLT_INVALID_OPERATION
;
637 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
638 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
639 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
640 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
641 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
642 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
646 /***********************************************************************
649 * Unblock signals. Called from EXC_RtlRaiseException.
651 void SIGNAL_Unblock( void )
653 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
657 /**********************************************************************
660 * Implementation of SIGSEGV handler.
662 static void do_segv( CONTEXT
*context
, int trap_code
, void *cr2
, int err_code
)
664 EXCEPTION_RECORD rec
;
665 DWORD page_fault_code
= EXCEPTION_ACCESS_VIOLATION
;
668 /* we want the page-fault case to be fast */
669 if (trap_code
== T_PAGEFLT
)
670 if (!(page_fault_code
= VIRTUAL_HandleFault( cr2
))) return;
673 rec
.ExceptionRecord
= NULL
;
674 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
675 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
676 rec
.NumberParameters
= 0;
680 case T_OFLOW
: /* Overflow exception */
681 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
683 case T_BOUND
: /* Bound range exception */
684 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
686 case T_PRIVINFLT
: /* Invalid opcode exception */
687 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
689 case T_STKFLT
: /* Stack fault */
690 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
692 case T_SEGNPFLT
: /* Segment not present exception */
693 case T_PROTFLT
: /* General protection fault */
694 case T_UNKNOWN
: /* Unknown fault code */
695 if (INSTR_EmulateInstruction( context
)) return;
696 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
698 case T_PAGEFLT
: /* Page fault */
700 rec
.NumberParameters
= 2;
701 rec
.ExceptionInformation
[0] = (err_code
& 2) != 0;
702 rec
.ExceptionInformation
[1] = (DWORD
)cr2
;
704 rec
.ExceptionCode
= page_fault_code
;
706 case T_ALIGNFLT
: /* Alignment check exception */
707 /* FIXME: pass through exception handler first? */
708 if (context
->EFlags
& 0x00040000)
710 /* Disable AC flag, return */
711 context
->EFlags
&= ~0x00040000;
714 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
717 ERR( "Got unexpected trap %d\n", trap_code
);
719 case T_NMI
: /* NMI interrupt */
720 case T_DNA
: /* Device not available exception */
721 case T_DOUBLEFLT
: /* Double fault exception */
722 case T_TSSFLT
: /* Invalid TSS exception */
723 case T_RESERVED
: /* Unknown exception */
724 case T_MCHK
: /* Machine check exception */
726 case T_CACHEFLT
: /* Cache flush exception */
728 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
731 EXC_RtlRaiseException( &rec
, context
);
735 /**********************************************************************
738 * Implementation of SIGTRAP handler.
740 static void do_trap( CONTEXT
*context
, int trap_code
)
742 EXCEPTION_RECORD rec
;
744 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
745 rec
.ExceptionRecord
= NULL
;
746 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
747 rec
.NumberParameters
= 0;
751 case T_TRCTRAP
: /* Single-step exception */
752 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
753 context
->EFlags
&= ~0x100; /* clear single-step flag */
755 case T_BPTFLT
: /* Breakpoint exception */
756 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
759 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
762 EXC_RtlRaiseException( &rec
, context
);
766 /**********************************************************************
769 * Implementation of SIGFPE handler
771 static void do_fpe( CONTEXT
*context
, int trap_code
)
773 EXCEPTION_RECORD rec
;
777 case T_DIVIDE
: /* Division by zero exception */
778 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
780 case T_FPOPFLT
: /* Coprocessor segment overrun */
781 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
783 case T_ARITHTRAP
: /* Floating point exception */
784 case T_UNKNOWN
: /* Unknown fault code */
785 rec
.ExceptionCode
= get_fpu_code( context
);
788 ERR( "Got unexpected trap %d\n", trap_code
);
789 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
792 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
793 rec
.ExceptionRecord
= NULL
;
794 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
795 rec
.NumberParameters
= 0;
796 EXC_RtlRaiseException( &rec
, context
);
801 /**********************************************************************
804 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
806 static void set_vm86_pend( CONTEXT
*context
)
808 EXCEPTION_RECORD rec
;
809 TEB
*teb
= NtCurrentTeb();
810 struct vm86plus_struct
*vm86
= (struct vm86plus_struct
*)(teb
->vm86_ptr
);
812 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
813 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
814 rec
.ExceptionRecord
= NULL
;
815 rec
.NumberParameters
= 1;
816 rec
.ExceptionInformation
[0] = 0;
818 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
819 teb
->vm86_pending
|= VIP_MASK
;
820 /* see if we were in VM86 mode */
821 if (context
->EFlags
& 0x00020000)
823 /* seems so, also set flag in signal context */
824 if (context
->EFlags
& VIP_MASK
) return;
825 context
->EFlags
|= VIP_MASK
;
826 vm86
->regs
.eflags
|= VIP_MASK
; /* no exception recursion */
827 if (context
->EFlags
& VIF_MASK
) {
828 /* VIF is set, throw exception */
829 teb
->vm86_pending
= 0;
830 teb
->vm86_ptr
= NULL
;
831 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
832 EXC_RtlRaiseException( &rec
, context
);
833 teb
->vm86_ptr
= vm86
;
838 /* not in VM86, but possibly setting up for it */
839 if (vm86
->regs
.eflags
& VIP_MASK
) return;
840 vm86
->regs
.eflags
|= VIP_MASK
;
841 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
842 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
843 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
844 /* exiting from VM86, can't throw */
847 if (vm86
->regs
.eflags
& VIF_MASK
) {
848 /* VIF is set, throw exception */
850 teb
->vm86_pending
= 0;
851 teb
->vm86_ptr
= NULL
;
852 save_vm86_context( &vcontext
, vm86
);
853 rec
.ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
854 EXC_RtlRaiseException( &rec
, &vcontext
);
855 teb
->vm86_ptr
= vm86
;
856 restore_vm86_context( &vcontext
, vm86
);
862 /**********************************************************************
865 * Handler for SIGUSR2.
866 * We use it to signal that the running __wine_enter_vm86() should
867 * immediately set VIP_MASK, causing pending events to be handled
868 * as early as possible.
870 static HANDLER_DEF(usr2_handler
)
874 save_context( &context
, HANDLER_CONTEXT
);
875 set_vm86_pend( &context
);
876 restore_context( &context
, HANDLER_CONTEXT
);
880 /**********************************************************************
883 * Handler for SIGALRM.
884 * Increases the alarm counter and sets the vm86 pending flag.
886 static HANDLER_DEF(alrm_handler
)
890 save_context( &context
, HANDLER_CONTEXT
);
891 NtCurrentTeb()->alarms
++;
892 set_vm86_pend( &context
);
893 restore_context( &context
, HANDLER_CONTEXT
);
895 #endif /* __HAVE_VM86 */
898 /**********************************************************************
901 * Handler for SIGSEGV and related errors.
903 static HANDLER_DEF(segv_handler
)
906 save_context( &context
, HANDLER_CONTEXT
);
907 do_segv( &context
, get_trap_code(HANDLER_CONTEXT
),
908 get_cr2_value(HANDLER_CONTEXT
), get_error_code(HANDLER_CONTEXT
) );
909 restore_context( &context
, HANDLER_CONTEXT
);
913 /**********************************************************************
916 * Handler for SIGTRAP.
918 static HANDLER_DEF(trap_handler
)
921 save_context( &context
, HANDLER_CONTEXT
);
922 do_trap( &context
, get_trap_code(HANDLER_CONTEXT
) );
923 restore_context( &context
, HANDLER_CONTEXT
);
927 /**********************************************************************
930 * Handler for SIGFPE.
932 static HANDLER_DEF(fpe_handler
)
935 save_fpu( &context
, HANDLER_CONTEXT
);
936 save_context( &context
, HANDLER_CONTEXT
);
937 do_fpe( &context
, get_trap_code(HANDLER_CONTEXT
) );
938 restore_context( &context
, HANDLER_CONTEXT
);
939 restore_fpu( &context
, HANDLER_CONTEXT
);
943 /**********************************************************************
946 * Handler for SIGINT.
948 static HANDLER_DEF(int_handler
)
950 EXCEPTION_RECORD rec
;
953 save_context( &context
, HANDLER_CONTEXT
);
954 rec
.ExceptionCode
= CONTROL_C_EXIT
;
955 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
956 rec
.ExceptionRecord
= NULL
;
957 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
958 rec
.NumberParameters
= 0;
959 EXC_RtlRaiseException( &rec
, &context
);
960 restore_context( &context
, HANDLER_CONTEXT
);
964 /***********************************************************************
967 * Set a signal handler
969 static int set_handler( int sig
, int have_sigaltstack
, void (*func
)() )
971 struct sigaction sig_act
;
974 if (!have_sigaltstack
&& NtCurrentTeb()->signal_stack
)
976 struct kernel_sigaction sig_act
;
977 sig_act
.ksa_handler
= func
;
978 sig_act
.ksa_flags
= SA_RESTART
;
979 sig_act
.ksa_mask
= (1 << (SIGINT
-1)) |
981 /* point to the top of the stack */
982 sig_act
.ksa_restorer
= (char *)NtCurrentTeb()->signal_stack
+ SIGNAL_STACK_SIZE
;
983 return wine_sigaction( sig
, &sig_act
, NULL
);
986 sig_act
.sa_handler
= func
;
987 sigemptyset( &sig_act
.sa_mask
);
988 sigaddset( &sig_act
.sa_mask
, SIGINT
);
989 sigaddset( &sig_act
.sa_mask
, SIGALRM
);
992 sig_act
.sa_flags
= SA_RESTART
;
993 #elif defined (__svr4__) || defined(_SCO_DS)
994 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
996 sig_act
.sa_flags
= 0;
1000 if (have_sigaltstack
) sig_act
.sa_flags
|= SA_ONSTACK
;
1002 return sigaction( sig
, &sig_act
, NULL
);
1006 /**********************************************************************
1009 BOOL
SIGNAL_Init(void)
1011 int have_sigaltstack
= 0;
1013 #ifdef HAVE_SIGALTSTACK
1014 struct sigaltstack ss
;
1015 if ((ss
.ss_sp
= NtCurrentTeb()->signal_stack
))
1017 ss
.ss_size
= SIGNAL_STACK_SIZE
;
1019 if (!sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1021 /* sigaltstack may fail because the kernel is too old, or
1022 because glibc is brain-dead. In the latter case a
1023 direct system call should succeed. */
1024 else if (!wine_sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1027 #endif /* HAVE_SIGALTSTACK */
1029 sigfillset( &all_sigs
);
1031 /* automatic child reaping to avoid zombies */
1032 signal( SIGCHLD
, SIG_IGN
);
1034 if (set_handler( SIGINT
, have_sigaltstack
, (void (*)())int_handler
) == -1) goto error
;
1035 if (set_handler( SIGFPE
, have_sigaltstack
, (void (*)())fpe_handler
) == -1) goto error
;
1036 if (set_handler( SIGSEGV
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1037 if (set_handler( SIGILL
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1039 if (set_handler( SIGBUS
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1042 if (set_handler( SIGTRAP
, have_sigaltstack
, (void (*)())trap_handler
) == -1) goto error
;
1046 if (set_handler( SIGALRM
, have_sigaltstack
, (void (*)())alrm_handler
) == -1) goto error
;
1047 if (set_handler( SIGUSR2
, have_sigaltstack
, (void (*)())usr2_handler
) == -1) goto error
;
1053 perror("sigaction");
1058 /**********************************************************************
1061 void SIGNAL_Reset(void)
1065 /* block the async signals */
1066 sigemptyset( &block_set
);
1067 sigaddset( &block_set
, SIGALRM
);
1068 sigaddset( &block_set
, SIGIO
);
1069 sigaddset( &block_set
, SIGHUP
);
1070 sigaddset( &block_set
, SIGUSR2
);
1071 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
1073 /* restore default handlers */
1074 signal( SIGINT
, SIG_DFL
);
1075 signal( SIGFPE
, SIG_DFL
);
1076 signal( SIGSEGV
, SIG_DFL
);
1077 signal( SIGILL
, SIG_DFL
);
1079 signal( SIGBUS
, SIG_DFL
);
1082 signal( SIGTRAP
, SIG_DFL
);
1088 /**********************************************************************
1091 * Enter vm86 mode with the specified register context.
1093 void __wine_enter_vm86( CONTEXT
*context
)
1095 EXCEPTION_RECORD rec
;
1096 TEB
*teb
= NtCurrentTeb();
1098 struct vm86plus_struct vm86
;
1100 memset( &vm86
, 0, sizeof(vm86
) );
1103 restore_vm86_context( context
, &vm86
);
1104 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1105 * so save it on entry, just in case */
1106 teb
->vm86_pending
|= (context
->EFlags
& VIP_MASK
);
1107 /* Work around race conditions with signal handler
1108 * (avoiding sigprocmask for performance reasons) */
1109 teb
->vm86_ptr
= &vm86
;
1110 vm86
.regs
.eflags
|= teb
->vm86_pending
;
1111 /* Check for VIF|VIP here, since vm86_enter doesn't */
1112 if ((vm86
.regs
.eflags
& (VIF_MASK
|VIP_MASK
)) == (VIF_MASK
|VIP_MASK
)) {
1113 teb
->vm86_ptr
= NULL
;
1114 teb
->vm86_pending
= 0;
1115 context
->EFlags
|= VIP_MASK
;
1116 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1117 rec
.ExceptionInformation
[0] = 0;
1123 res
= vm86_enter( &teb
->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1129 } while (VM86_TYPE(res
) == VM86_SIGNAL
);
1131 save_vm86_context( context
, &vm86
);
1132 context
->EFlags
|= teb
->vm86_pending
;
1134 switch(VM86_TYPE(res
))
1136 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1137 do_segv( context
, T_PROTFLT
, 0, 0 );
1139 case VM86_TRAP
: /* return due to DOS-debugger request */
1140 do_trap( context
, VM86_ARG(res
) );
1142 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1143 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1145 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1146 teb
->vm86_pending
= 0;
1147 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1149 case VM86_PICRETURN
: /* return due to pending PIC request */
1150 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1153 ERR( "unhandled result from vm86 mode %x\n", res
);
1156 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1158 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1159 rec
.ExceptionRecord
= NULL
;
1160 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1161 rec
.NumberParameters
= 1;
1162 EXC_RtlRaiseException( &rec
, context
);
1166 #else /* __HAVE_VM86 */
1167 void __wine_enter_vm86( CONTEXT
*context
)
1169 MESSAGE("vm86 mode not supported on this platform\n");
1171 #endif /* __HAVE_VM86 */
1173 /**********************************************************************
1174 * DbgBreakPoint (NTDLL.@)
1176 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret");
1178 /**********************************************************************
1179 * DbgUserBreakPoint (NTDLL.@)
1181 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret");
1183 #endif /* __i386__ */