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"
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
38 # ifdef HAVE_SYS_SYSCALL_H
39 # include <sys/syscall.h>
43 #ifdef HAVE_SYS_VM86_H
44 # include <sys/vm86.h>
47 #ifdef HAVE_SYS_SIGNAL_H
48 # include <sys/signal.h>
53 #include "wine/library.h"
55 #include "selectors.h"
57 /***********************************************************************
58 * signal context platform-specific definitions
64 unsigned short sc_gs
, __gsh
;
65 unsigned short sc_fs
, __fsh
;
66 unsigned short sc_es
, __esh
;
67 unsigned short sc_ds
, __dsh
;
76 unsigned long sc_trapno
;
79 unsigned short sc_cs
, __csh
;
80 unsigned long sc_eflags
;
81 unsigned long esp_at_signal
;
82 unsigned short sc_ss
, __ssh
;
84 unsigned long oldmask
;
88 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
89 #define HANDLER_CONTEXT (&__context)
91 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
92 struct kernel_sigaction
94 void (*ksa_handler
)();
95 unsigned long ksa_mask
;
96 unsigned long ksa_flags
;
100 /* Similar to the sigaction function in libc, except it leaves alone the
101 restorer field, which is used to specify the signal stack address */
102 static inline int wine_sigaction( int sig
, struct kernel_sigaction
*new,
103 struct kernel_sigaction
*old
)
105 __asm__
__volatile__( "pushl %%ebx\n\t"
110 : "0" (SYS_sigaction
), "r" (sig
), "c" (new), "d" (old
) );
111 if (sig
>=0) return 0;
116 #ifdef HAVE_SIGALTSTACK
117 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
118 static inline int wine_sigaltstack( const struct sigaltstack
*new,
119 struct sigaltstack
*old
)
122 __asm__
__volatile__( "pushl %%ebx\n\t"
127 : "0" (SYS_sigaltstack
), "r" (new), "c" (old
) );
128 if (ret
>= 0) return 0;
134 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
136 int vm86_enter( void **vm86_ptr
);
137 void vm86_return(void);
138 void vm86_return_end(void);
139 __ASM_GLOBAL_FUNC(vm86_enter
,
141 "movl %esp, %ebp\n\t"
142 "movl $166,%eax\n\t" /*SYS_vm86*/
143 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
144 "movl (%ecx),%ecx\n\t"
146 "movl $1,%ebx\n\t" /*VM86_ENTER*/
147 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
150 ".globl " __ASM_NAME("vm86_return") "\n\t"
151 ".type " __ASM_NAME("vm86_return") ",@function\n"
152 __ASM_NAME("vm86_return") ":\n\t"
157 "testl %eax,%eax\n\t"
159 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
160 "je " __ASM_NAME("vm86_enter") "\n\t"
162 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
164 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
165 ".type " __ASM_NAME("vm86_return_end") ",@function\n"
166 __ASM_NAME("vm86_return_end") ":\n\t"
175 #define EAX_sig(context) ((context)->tf_eax)
176 #define EBX_sig(context) ((context)->tf_ebx)
177 #define ECX_sig(context) ((context)->tf_ecx)
178 #define EDX_sig(context) ((context)->tf_edx)
179 #define ESI_sig(context) ((context)->tf_esi)
180 #define EDI_sig(context) ((context)->tf_edi)
181 #define EBP_sig(context) ((context)->tf_ebp)
183 #define CS_sig(context) ((context)->tf_cs)
184 #define DS_sig(context) ((context)->tf_ds)
185 #define ES_sig(context) ((context)->tf_es)
186 #define SS_sig(context) ((context)->tf_ss)
188 #include <machine/frame.h>
189 typedef struct trapframe SIGCONTEXT
;
191 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
192 #define HANDLER_CONTEXT __context
194 #define EFL_sig(context) ((context)->tf_eflags)
196 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
197 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
201 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
203 typedef struct sigcontext SIGCONTEXT
;
205 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
206 #define HANDLER_CONTEXT __context
210 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
213 #include <sys/regset.h>
217 #include <sys/ucontext.h>
219 typedef struct ucontext SIGCONTEXT
;
221 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
222 #define HANDLER_CONTEXT __context
224 #endif /* svr4 || SCO_DS */
230 unsigned long ContextFlags
;
231 FLOATING_SAVE_AREA sc_float
;
236 unsigned long sc_edi
;
237 unsigned long sc_esi
;
238 unsigned long sc_eax
;
239 unsigned long sc_ebx
;
240 unsigned long sc_ecx
;
241 unsigned long sc_edx
;
242 unsigned long sc_ebp
;
243 unsigned long sc_eip
;
245 unsigned long sc_eflags
;
246 unsigned long sc_esp
;
253 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
255 #define EAX_sig(context) ((context)->sc_eax)
256 #define EBX_sig(context) ((context)->sc_ebx)
257 #define ECX_sig(context) ((context)->sc_ecx)
258 #define EDX_sig(context) ((context)->sc_edx)
259 #define ESI_sig(context) ((context)->sc_esi)
260 #define EDI_sig(context) ((context)->sc_edi)
261 #define EBP_sig(context) ((context)->sc_ebp)
263 #define CS_sig(context) ((context)->sc_cs)
264 #define DS_sig(context) ((context)->sc_ds)
265 #define ES_sig(context) ((context)->sc_es)
266 #define SS_sig(context) ((context)->sc_ss)
268 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
269 * saved by the exception handling. duh.
270 * Actually they are in -current (have been for a while), and that
271 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
272 * If you're running a system from the -stable branch older than that,
273 * like a 3.3-RELEASE, grab the patch from the ports tree:
274 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
275 * (If its not yet there when you look, go here:
276 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
279 #define FS_sig(context) ((context)->sc_fs)
280 #define GS_sig(context) ((context)->sc_gs)
284 #define FS_sig(context) ((context)->sc_fs)
285 #define GS_sig(context) ((context)->sc_gs)
286 #define CR2_sig(context) ((context)->cr2)
287 #define TRAP_sig(context) ((context)->sc_trapno)
288 #define ERROR_sig(context) ((context)->sc_err)
289 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
293 #define EFL_sig(context) ((context)->sc_eflags)
295 #define EFL_sig(context) ((context)->sc_efl)
296 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
297 #define CR2_sig(context) ((context)->sc_err)
298 #define TRAP_sig(context) ((context)->sc_trapno)
301 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
302 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
304 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
306 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
312 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
313 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
314 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
315 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
316 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
317 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
318 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
320 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
321 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
322 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
323 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
325 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
326 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
328 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
330 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
332 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
334 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
337 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
340 #endif /* svr4 || SCO_DS */
343 /* exception code definitions (already defined by FreeBSD/NetBSD) */
344 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
345 #define T_DIVIDE 0 /* Division by zero exception */
346 #define T_TRCTRAP 1 /* Single-step exception */
347 #define T_NMI 2 /* NMI interrupt */
348 #define T_BPTFLT 3 /* Breakpoint exception */
349 #define T_OFLOW 4 /* Overflow exception */
350 #define T_BOUND 5 /* Bound range exception */
351 #define T_PRIVINFLT 6 /* Invalid opcode exception */
352 #define T_DNA 7 /* Device not available exception */
353 #define T_DOUBLEFLT 8 /* Double fault exception */
354 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
355 #define T_TSSFLT 10 /* Invalid TSS exception */
356 #define T_SEGNPFLT 11 /* Segment not present exception */
357 #define T_STKFLT 12 /* Stack fault */
358 #define T_PROTFLT 13 /* General protection fault */
359 #define T_PAGEFLT 14 /* Page fault */
360 #define T_RESERVED 15 /* Unknown exception */
361 #define T_ARITHTRAP 16 /* Floating point exception */
362 #define T_ALIGNFLT 17 /* Alignment check exception */
363 #define T_MCHK 18 /* Machine check exception */
364 #define T_CACHEFLT 19 /* Cache flush exception */
366 #if defined(__NetBSD__)
367 #define T_MCHK 19 /* Machine check exception */
370 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
372 #include "wine/exception.h"
374 #include "stackframe.h"
378 #include "syslevel.h"
379 #include "wine/debug.h"
381 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
383 static sigset_t all_sigs
;
386 /***********************************************************************
389 * Get the trap code for a signal.
391 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
394 return TRAP_sig(sigcontext
);
396 return T_UNKNOWN
; /* unknown trap code */
400 /***********************************************************************
403 * Get the error code for a signal.
405 static inline int get_error_code( const SIGCONTEXT
*sigcontext
)
408 return ERROR_sig(sigcontext
);
414 /***********************************************************************
417 * Get the CR2 value for a signal.
419 static inline void *get_cr2_value( const SIGCONTEXT
*sigcontext
)
422 return (void *)CR2_sig(sigcontext
);
430 /***********************************************************************
433 * Set the register values from a vm86 structure.
435 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
437 context
->Eax
= vm86
->regs
.eax
;
438 context
->Ebx
= vm86
->regs
.ebx
;
439 context
->Ecx
= vm86
->regs
.ecx
;
440 context
->Edx
= vm86
->regs
.edx
;
441 context
->Esi
= vm86
->regs
.esi
;
442 context
->Edi
= vm86
->regs
.edi
;
443 context
->Esp
= vm86
->regs
.esp
;
444 context
->Ebp
= vm86
->regs
.ebp
;
445 context
->Eip
= vm86
->regs
.eip
;
446 context
->SegCs
= vm86
->regs
.cs
;
447 context
->SegDs
= vm86
->regs
.ds
;
448 context
->SegEs
= vm86
->regs
.es
;
449 context
->SegFs
= vm86
->regs
.fs
;
450 context
->SegGs
= vm86
->regs
.gs
;
451 context
->SegSs
= vm86
->regs
.ss
;
452 context
->EFlags
= vm86
->regs
.eflags
;
456 /***********************************************************************
457 * restore_vm86_context
459 * Build a vm86 structure from the register values.
461 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
463 vm86
->regs
.eax
= context
->Eax
;
464 vm86
->regs
.ebx
= context
->Ebx
;
465 vm86
->regs
.ecx
= context
->Ecx
;
466 vm86
->regs
.edx
= context
->Edx
;
467 vm86
->regs
.esi
= context
->Esi
;
468 vm86
->regs
.edi
= context
->Edi
;
469 vm86
->regs
.esp
= context
->Esp
;
470 vm86
->regs
.ebp
= context
->Ebp
;
471 vm86
->regs
.eip
= context
->Eip
;
472 vm86
->regs
.cs
= context
->SegCs
;
473 vm86
->regs
.ds
= context
->SegDs
;
474 vm86
->regs
.es
= context
->SegEs
;
475 vm86
->regs
.fs
= context
->SegFs
;
476 vm86
->regs
.gs
= context
->SegGs
;
477 vm86
->regs
.ss
= context
->SegSs
;
478 vm86
->regs
.eflags
= context
->EFlags
;
480 #endif /* __HAVE_VM86 */
483 /***********************************************************************
486 * Set the register values from a sigcontext.
488 static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
491 /* get %fs at time of the fault */
493 fs
= FS_sig(sigcontext
);
499 /* now restore a proper %fs for the fault handler */
500 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext
))) /* 16-bit mode */
502 fs
= SYSLEVEL_Win16CurrentTeb
;
505 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
507 /* fetch the saved %fs on the stack */
508 fs
= *(unsigned int *)ESP_sig(sigcontext
);
509 if (EAX_sig(sigcontext
) == VM86_EAX
) {
510 struct vm86plus_struct
*vm86
;
512 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
513 * (but we could also get if from teb->vm86_ptr) */
514 vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
515 /* get context from vm86 struct */
516 save_vm86_context( context
, vm86
);
520 #endif /* __HAVE_VM86 */
524 context
->Eax
= EAX_sig(sigcontext
);
525 context
->Ebx
= EBX_sig(sigcontext
);
526 context
->Ecx
= ECX_sig(sigcontext
);
527 context
->Edx
= EDX_sig(sigcontext
);
528 context
->Esi
= ESI_sig(sigcontext
);
529 context
->Edi
= EDI_sig(sigcontext
);
530 context
->Ebp
= EBP_sig(sigcontext
);
531 context
->EFlags
= EFL_sig(sigcontext
);
532 context
->Eip
= EIP_sig(sigcontext
);
533 context
->Esp
= ESP_sig(sigcontext
);
534 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
535 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
536 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
537 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
539 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
541 context
->SegGs
= wine_get_gs();
546 /***********************************************************************
549 * Build a sigcontext from the register values.
551 static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
554 /* check if exception occurred in vm86 mode */
555 if ((void *)EIP_sig(sigcontext
) == vm86_return
&&
556 IS_SELECTOR_SYSTEM(CS_sig(sigcontext
)) &&
557 EAX_sig(sigcontext
) == VM86_EAX
)
559 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
560 * (but we could also get it from teb->vm86_ptr) */
561 struct vm86plus_struct
*vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
562 restore_vm86_context( context
, vm86
);
565 #endif /* __HAVE_VM86 */
567 EAX_sig(sigcontext
) = context
->Eax
;
568 EBX_sig(sigcontext
) = context
->Ebx
;
569 ECX_sig(sigcontext
) = context
->Ecx
;
570 EDX_sig(sigcontext
) = context
->Edx
;
571 ESI_sig(sigcontext
) = context
->Esi
;
572 EDI_sig(sigcontext
) = context
->Edi
;
573 EBP_sig(sigcontext
) = context
->Ebp
;
574 EFL_sig(sigcontext
) = context
->EFlags
;
575 EIP_sig(sigcontext
) = context
->Eip
;
576 ESP_sig(sigcontext
) = context
->Esp
;
577 CS_sig(sigcontext
) = context
->SegCs
;
578 DS_sig(sigcontext
) = context
->SegDs
;
579 ES_sig(sigcontext
) = context
->SegEs
;
580 SS_sig(sigcontext
) = context
->SegSs
;
582 FS_sig(sigcontext
) = context
->SegFs
;
584 wine_set_fs( context
->SegFs
);
587 GS_sig(sigcontext
) = context
->SegGs
;
589 wine_set_gs( context
->SegGs
);
594 /***********************************************************************
597 * Set the FPU context from a sigcontext.
599 inline static void save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
602 if (FPU_sig(sigcontext
))
604 context
->FloatSave
= *FPU_sig(sigcontext
);
609 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
610 #endif /* __GNUC__ */
614 /***********************************************************************
617 * Restore the FPU context to a sigcontext.
619 inline static void restore_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
621 /* reset the current interrupt status */
622 context
->FloatSave
.StatusWord
&= context
->FloatSave
.ControlWord
| 0xffffff80;
624 if (FPU_sig(sigcontext
))
626 *FPU_sig(sigcontext
) = context
->FloatSave
;
631 /* avoid nested exceptions */
632 __asm__
__volatile__( "frstor %0; fwait" : : "m" (context
->FloatSave
) );
633 #endif /* __GNUC__ */
637 /**********************************************************************
640 * Get the FPU exception code from the FPU status.
642 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
644 DWORD status
= context
->FloatSave
.StatusWord
;
646 if (status
& 0x01) /* IE */
648 if (status
& 0x40) /* SF */
649 return EXCEPTION_FLT_STACK_CHECK
;
651 return EXCEPTION_FLT_INVALID_OPERATION
;
653 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
654 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
655 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
656 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
657 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
658 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
662 /***********************************************************************
665 * Unblock signals. Called from EXC_RtlRaiseException.
667 void SIGNAL_Unblock( void )
669 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
673 /**********************************************************************
676 * Implementation of SIGSEGV handler.
678 static void do_segv( CONTEXT
*context
, int trap_code
, void *cr2
, int err_code
)
680 EXCEPTION_RECORD rec
;
681 DWORD page_fault_code
= EXCEPTION_ACCESS_VIOLATION
;
684 /* we want the page-fault case to be fast */
685 if (trap_code
== T_PAGEFLT
)
686 if (!(page_fault_code
= VIRTUAL_HandleFault( cr2
))) return;
689 rec
.ExceptionRecord
= NULL
;
690 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
691 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
692 rec
.NumberParameters
= 0;
696 case T_OFLOW
: /* Overflow exception */
697 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
699 case T_BOUND
: /* Bound range exception */
700 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
702 case T_PRIVINFLT
: /* Invalid opcode exception */
703 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
705 case T_STKFLT
: /* Stack fault */
706 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
708 case T_SEGNPFLT
: /* Segment not present exception */
709 case T_PROTFLT
: /* General protection fault */
710 case T_UNKNOWN
: /* Unknown fault code */
711 if (INSTR_EmulateInstruction( context
)) return;
712 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
714 case T_PAGEFLT
: /* Page fault */
716 rec
.NumberParameters
= 2;
717 rec
.ExceptionInformation
[0] = (err_code
& 2) != 0;
718 rec
.ExceptionInformation
[1] = (DWORD
)cr2
;
720 rec
.ExceptionCode
= page_fault_code
;
722 case T_ALIGNFLT
: /* Alignment check exception */
723 /* FIXME: pass through exception handler first? */
724 if (context
->EFlags
& 0x00040000)
726 /* Disable AC flag, return */
727 context
->EFlags
&= ~0x00040000;
730 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
733 ERR( "Got unexpected trap %d\n", trap_code
);
735 case T_NMI
: /* NMI interrupt */
736 case T_DNA
: /* Device not available exception */
737 case T_DOUBLEFLT
: /* Double fault exception */
738 case T_TSSFLT
: /* Invalid TSS exception */
739 case T_RESERVED
: /* Unknown exception */
740 case T_MCHK
: /* Machine check exception */
742 case T_CACHEFLT
: /* Cache flush exception */
744 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
747 EXC_RtlRaiseException( &rec
, context
);
751 /**********************************************************************
754 * Implementation of SIGTRAP handler.
756 static void do_trap( CONTEXT
*context
, int trap_code
)
758 EXCEPTION_RECORD rec
;
760 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
761 rec
.ExceptionRecord
= NULL
;
762 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
763 rec
.NumberParameters
= 0;
767 case T_TRCTRAP
: /* Single-step exception */
768 if (context
->EFlags
& 0x100)
770 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
771 context
->EFlags
&= ~0x100; /* clear single-step flag */
775 /* likely we get this because of a kill(SIGTRAP) on ourself,
776 * so send a bp exception instead of a single step exception
778 TRACE("Spurious single step trap => breakpoint simulation\n");
779 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
782 case T_BPTFLT
: /* Breakpoint exception */
783 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
786 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
789 EXC_RtlRaiseException( &rec
, context
);
793 /**********************************************************************
796 * Implementation of SIGFPE handler
798 static void do_fpe( CONTEXT
*context
, int trap_code
)
800 EXCEPTION_RECORD rec
;
804 case T_DIVIDE
: /* Division by zero exception */
805 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
807 case T_FPOPFLT
: /* Coprocessor segment overrun */
808 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
810 case T_ARITHTRAP
: /* Floating point exception */
811 case T_UNKNOWN
: /* Unknown fault code */
812 rec
.ExceptionCode
= get_fpu_code( context
);
815 ERR( "Got unexpected trap %d\n", trap_code
);
816 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
819 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
820 rec
.ExceptionRecord
= NULL
;
821 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
822 rec
.NumberParameters
= 0;
823 EXC_RtlRaiseException( &rec
, context
);
828 /**********************************************************************
831 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
833 static void set_vm86_pend( CONTEXT
*context
)
835 EXCEPTION_RECORD rec
;
836 TEB
*teb
= NtCurrentTeb();
837 struct vm86plus_struct
*vm86
= (struct vm86plus_struct
*)(teb
->vm86_ptr
);
839 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
840 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
841 rec
.ExceptionRecord
= NULL
;
842 rec
.NumberParameters
= 1;
843 rec
.ExceptionInformation
[0] = 0;
845 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
846 teb
->vm86_pending
|= VIP_MASK
;
847 /* see if we were in VM86 mode */
848 if (context
->EFlags
& 0x00020000)
850 /* seems so, also set flag in signal context */
851 if (context
->EFlags
& VIP_MASK
) return;
852 context
->EFlags
|= VIP_MASK
;
853 vm86
->regs
.eflags
|= VIP_MASK
; /* no exception recursion */
854 if (context
->EFlags
& VIF_MASK
) {
855 /* VIF is set, throw exception */
856 teb
->vm86_pending
= 0;
857 teb
->vm86_ptr
= NULL
;
858 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
859 EXC_RtlRaiseException( &rec
, context
);
860 teb
->vm86_ptr
= vm86
;
865 /* not in VM86, but possibly setting up for it */
866 if (vm86
->regs
.eflags
& VIP_MASK
) return;
867 vm86
->regs
.eflags
|= VIP_MASK
;
868 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
869 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
870 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
871 /* exiting from VM86, can't throw */
874 if (vm86
->regs
.eflags
& VIF_MASK
) {
875 /* VIF is set, throw exception */
877 teb
->vm86_pending
= 0;
878 teb
->vm86_ptr
= NULL
;
879 save_vm86_context( &vcontext
, vm86
);
880 rec
.ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
881 EXC_RtlRaiseException( &rec
, &vcontext
);
882 teb
->vm86_ptr
= vm86
;
883 restore_vm86_context( &vcontext
, vm86
);
889 /**********************************************************************
892 * Handler for SIGUSR2.
893 * We use it to signal that the running __wine_enter_vm86() should
894 * immediately set VIP_MASK, causing pending events to be handled
895 * as early as possible.
897 static HANDLER_DEF(usr2_handler
)
901 save_context( &context
, HANDLER_CONTEXT
);
902 set_vm86_pend( &context
);
903 restore_context( &context
, HANDLER_CONTEXT
);
907 /**********************************************************************
910 * Handler for SIGALRM.
911 * Increases the alarm counter and sets the vm86 pending flag.
913 static HANDLER_DEF(alrm_handler
)
917 save_context( &context
, HANDLER_CONTEXT
);
918 NtCurrentTeb()->alarms
++;
919 set_vm86_pend( &context
);
920 restore_context( &context
, HANDLER_CONTEXT
);
922 #endif /* __HAVE_VM86 */
925 /**********************************************************************
928 * Handler for SIGSEGV and related errors.
930 static HANDLER_DEF(segv_handler
)
933 save_context( &context
, HANDLER_CONTEXT
);
934 do_segv( &context
, get_trap_code(HANDLER_CONTEXT
),
935 get_cr2_value(HANDLER_CONTEXT
), get_error_code(HANDLER_CONTEXT
) );
936 restore_context( &context
, HANDLER_CONTEXT
);
940 /**********************************************************************
943 * Handler for SIGTRAP.
945 static HANDLER_DEF(trap_handler
)
948 save_context( &context
, HANDLER_CONTEXT
);
949 do_trap( &context
, get_trap_code(HANDLER_CONTEXT
) );
950 restore_context( &context
, HANDLER_CONTEXT
);
954 /**********************************************************************
957 * Handler for SIGFPE.
959 static HANDLER_DEF(fpe_handler
)
962 save_fpu( &context
, HANDLER_CONTEXT
);
963 save_context( &context
, HANDLER_CONTEXT
);
964 do_fpe( &context
, get_trap_code(HANDLER_CONTEXT
) );
965 restore_context( &context
, HANDLER_CONTEXT
);
966 restore_fpu( &context
, HANDLER_CONTEXT
);
970 /**********************************************************************
973 * Handler for SIGINT.
975 static HANDLER_DEF(int_handler
)
977 extern int CONSOLE_HandleCtrlC(void);
978 if (!CONSOLE_HandleCtrlC())
980 EXCEPTION_RECORD rec
;
983 save_context( &context
, HANDLER_CONTEXT
);
984 rec
.ExceptionCode
= CONTROL_C_EXIT
;
985 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
986 rec
.ExceptionRecord
= NULL
;
987 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
988 rec
.NumberParameters
= 0;
989 EXC_RtlRaiseException( &rec
, &context
);
990 restore_context( &context
, HANDLER_CONTEXT
);
995 /***********************************************************************
998 * Set a signal handler
1000 static int set_handler( int sig
, int have_sigaltstack
, void (*func
)() )
1002 struct sigaction sig_act
;
1005 if (!have_sigaltstack
&& NtCurrentTeb()->signal_stack
)
1007 struct kernel_sigaction sig_act
;
1008 sig_act
.ksa_handler
= func
;
1009 sig_act
.ksa_flags
= SA_RESTART
;
1010 sig_act
.ksa_mask
= (1 << (SIGINT
-1)) |
1012 /* point to the top of the stack */
1013 sig_act
.ksa_restorer
= (char *)NtCurrentTeb()->signal_stack
+ SIGNAL_STACK_SIZE
;
1014 return wine_sigaction( sig
, &sig_act
, NULL
);
1017 sig_act
.sa_handler
= func
;
1018 sigemptyset( &sig_act
.sa_mask
);
1019 sigaddset( &sig_act
.sa_mask
, SIGINT
);
1020 sigaddset( &sig_act
.sa_mask
, SIGALRM
);
1023 sig_act
.sa_flags
= SA_RESTART
;
1024 #elif defined (__svr4__) || defined(_SCO_DS)
1025 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1027 sig_act
.sa_flags
= 0;
1031 if (have_sigaltstack
) sig_act
.sa_flags
|= SA_ONSTACK
;
1033 return sigaction( sig
, &sig_act
, NULL
);
1037 /**********************************************************************
1040 BOOL
SIGNAL_Init(void)
1042 int have_sigaltstack
= 0;
1044 #ifdef HAVE_SIGALTSTACK
1045 struct sigaltstack ss
;
1046 if ((ss
.ss_sp
= NtCurrentTeb()->signal_stack
))
1048 ss
.ss_size
= SIGNAL_STACK_SIZE
;
1050 if (!sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1052 /* sigaltstack may fail because the kernel is too old, or
1053 because glibc is brain-dead. In the latter case a
1054 direct system call should succeed. */
1055 else if (!wine_sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1058 #endif /* HAVE_SIGALTSTACK */
1060 sigfillset( &all_sigs
);
1062 /* automatic child reaping to avoid zombies */
1063 signal( SIGCHLD
, SIG_IGN
);
1065 if (set_handler( SIGINT
, have_sigaltstack
, (void (*)())int_handler
) == -1) goto error
;
1066 if (set_handler( SIGFPE
, have_sigaltstack
, (void (*)())fpe_handler
) == -1) goto error
;
1067 if (set_handler( SIGSEGV
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1068 if (set_handler( SIGILL
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1070 if (set_handler( SIGBUS
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1073 if (set_handler( SIGTRAP
, have_sigaltstack
, (void (*)())trap_handler
) == -1) goto error
;
1077 if (set_handler( SIGALRM
, have_sigaltstack
, (void (*)())alrm_handler
) == -1) goto error
;
1078 if (set_handler( SIGUSR2
, have_sigaltstack
, (void (*)())usr2_handler
) == -1) goto error
;
1084 perror("sigaction");
1089 /**********************************************************************
1092 void SIGNAL_Reset(void)
1096 /* block the async signals */
1097 sigemptyset( &block_set
);
1098 sigaddset( &block_set
, SIGALRM
);
1099 sigaddset( &block_set
, SIGIO
);
1100 sigaddset( &block_set
, SIGHUP
);
1101 sigaddset( &block_set
, SIGUSR2
);
1102 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
1104 /* restore default handlers */
1105 signal( SIGINT
, SIG_DFL
);
1106 signal( SIGFPE
, SIG_DFL
);
1107 signal( SIGSEGV
, SIG_DFL
);
1108 signal( SIGILL
, SIG_DFL
);
1110 signal( SIGBUS
, SIG_DFL
);
1113 signal( SIGTRAP
, SIG_DFL
);
1119 /**********************************************************************
1122 * Enter vm86 mode with the specified register context.
1124 void __wine_enter_vm86( CONTEXT
*context
)
1126 EXCEPTION_RECORD rec
;
1127 TEB
*teb
= NtCurrentTeb();
1129 struct vm86plus_struct vm86
;
1131 memset( &vm86
, 0, sizeof(vm86
) );
1134 restore_vm86_context( context
, &vm86
);
1135 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1136 * so save it on entry, just in case */
1137 teb
->vm86_pending
|= (context
->EFlags
& VIP_MASK
);
1138 /* Work around race conditions with signal handler
1139 * (avoiding sigprocmask for performance reasons) */
1140 teb
->vm86_ptr
= &vm86
;
1141 vm86
.regs
.eflags
|= teb
->vm86_pending
;
1142 /* Check for VIF|VIP here, since vm86_enter doesn't */
1143 if ((vm86
.regs
.eflags
& (VIF_MASK
|VIP_MASK
)) == (VIF_MASK
|VIP_MASK
)) {
1144 teb
->vm86_ptr
= NULL
;
1145 teb
->vm86_pending
= 0;
1146 context
->EFlags
|= VIP_MASK
;
1147 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1148 rec
.ExceptionInformation
[0] = 0;
1154 res
= vm86_enter( &teb
->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1160 } while (VM86_TYPE(res
) == VM86_SIGNAL
);
1162 save_vm86_context( context
, &vm86
);
1163 context
->EFlags
|= teb
->vm86_pending
;
1165 switch(VM86_TYPE(res
))
1167 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1168 do_segv( context
, T_PROTFLT
, 0, 0 );
1170 case VM86_TRAP
: /* return due to DOS-debugger request */
1171 do_trap( context
, VM86_ARG(res
) );
1173 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1174 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1176 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1177 teb
->vm86_pending
= 0;
1178 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1180 case VM86_PICRETURN
: /* return due to pending PIC request */
1181 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1184 ERR( "unhandled result from vm86 mode %x\n", res
);
1187 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1189 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1190 rec
.ExceptionRecord
= NULL
;
1191 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1192 rec
.NumberParameters
= 1;
1193 EXC_RtlRaiseException( &rec
, context
);
1197 #else /* __HAVE_VM86 */
1198 void __wine_enter_vm86( CONTEXT
*context
)
1200 MESSAGE("vm86 mode not supported on this platform\n");
1202 #endif /* __HAVE_VM86 */
1204 /**********************************************************************
1205 * DbgBreakPoint (NTDLL.@)
1207 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret");
1209 /**********************************************************************
1210 * DbgUserBreakPoint (NTDLL.@)
1212 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret");
1214 #endif /* __i386__ */