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 __ASM_FUNC("vm86_return") "\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 __ASM_FUNC("vm86_return_end") "\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
;
254 /* FIXME: This section is just here so it can compile, it's most likely
255 * completely wrong. */
259 unsigned short sc_gs
, __gsh
;
260 unsigned short sc_fs
, __fsh
;
261 unsigned short sc_es
, __esh
;
262 unsigned short sc_ds
, __dsh
;
263 unsigned long sc_edi
;
264 unsigned long sc_esi
;
265 unsigned long sc_ebp
;
266 unsigned long sc_esp
;
267 unsigned long sc_ebx
;
268 unsigned long sc_edx
;
269 unsigned long sc_ecx
;
270 unsigned long sc_eax
;
271 unsigned long sc_trapno
;
272 unsigned long sc_err
;
273 unsigned long sc_eip
;
274 unsigned short sc_cs
, __csh
;
275 unsigned long sc_eflags
;
276 unsigned long esp_at_signal
;
277 unsigned short sc_ss
, __ssh
;
279 unsigned long oldmask
;
283 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
284 #define HANDLER_CONTEXT (&__context)
286 #endif /* __CYGWIN__ */
288 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
289 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
291 #define EAX_sig(context) ((context)->sc_eax)
292 #define EBX_sig(context) ((context)->sc_ebx)
293 #define ECX_sig(context) ((context)->sc_ecx)
294 #define EDX_sig(context) ((context)->sc_edx)
295 #define ESI_sig(context) ((context)->sc_esi)
296 #define EDI_sig(context) ((context)->sc_edi)
297 #define EBP_sig(context) ((context)->sc_ebp)
299 #define CS_sig(context) ((context)->sc_cs)
300 #define DS_sig(context) ((context)->sc_ds)
301 #define ES_sig(context) ((context)->sc_es)
302 #define SS_sig(context) ((context)->sc_ss)
304 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
305 * saved by the exception handling. duh.
306 * Actually they are in -current (have been for a while), and that
307 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
308 * If you're running a system from the -stable branch older than that,
309 * like a 3.3-RELEASE, grab the patch from the ports tree:
310 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
311 * (If its not yet there when you look, go here:
312 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
315 #define FS_sig(context) ((context)->sc_fs)
316 #define GS_sig(context) ((context)->sc_gs)
320 #define FS_sig(context) ((context)->sc_fs)
321 #define GS_sig(context) ((context)->sc_gs)
322 #define CR2_sig(context) ((context)->cr2)
323 #define TRAP_sig(context) ((context)->sc_trapno)
324 #define ERROR_sig(context) ((context)->sc_err)
325 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
329 #define EFL_sig(context) ((context)->sc_eflags)
331 #define EFL_sig(context) ((context)->sc_efl)
332 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
333 #define CR2_sig(context) ((context)->sc_err)
334 #define TRAP_sig(context) ((context)->sc_trapno)
337 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
338 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
340 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
342 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
348 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
349 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
350 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
351 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
352 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
353 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
354 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
356 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
357 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
358 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
359 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
361 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
362 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
364 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
366 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
368 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
370 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
373 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
376 #endif /* svr4 || SCO_DS */
379 /* exception code definitions (already defined by FreeBSD/NetBSD) */
380 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
381 #define T_DIVIDE 0 /* Division by zero exception */
382 #define T_TRCTRAP 1 /* Single-step exception */
383 #define T_NMI 2 /* NMI interrupt */
384 #define T_BPTFLT 3 /* Breakpoint exception */
385 #define T_OFLOW 4 /* Overflow exception */
386 #define T_BOUND 5 /* Bound range exception */
387 #define T_PRIVINFLT 6 /* Invalid opcode exception */
388 #define T_DNA 7 /* Device not available exception */
389 #define T_DOUBLEFLT 8 /* Double fault exception */
390 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
391 #define T_TSSFLT 10 /* Invalid TSS exception */
392 #define T_SEGNPFLT 11 /* Segment not present exception */
393 #define T_STKFLT 12 /* Stack fault */
394 #define T_PROTFLT 13 /* General protection fault */
395 #define T_PAGEFLT 14 /* Page fault */
396 #define T_RESERVED 15 /* Unknown exception */
397 #define T_ARITHTRAP 16 /* Floating point exception */
398 #define T_ALIGNFLT 17 /* Alignment check exception */
399 #define T_MCHK 18 /* Machine check exception */
400 #define T_CACHEFLT 19 /* Cache flush exception */
402 #if defined(__NetBSD__)
403 #define T_MCHK 19 /* Machine check exception */
406 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
408 #include "wine/exception.h"
410 #include "stackframe.h"
414 #include "syslevel.h"
415 #include "wine/debug.h"
417 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
419 typedef int (*wine_signal_handler
)(unsigned sig
);
421 static wine_signal_handler handlers
[256];
423 static sigset_t all_sigs
;
426 /***********************************************************************
429 inline static int dispatch_signal(unsigned sig
)
431 if (handlers
[sig
] == NULL
) return 0;
432 return handlers
[sig
](sig
);
436 /***********************************************************************
439 * Get the trap code for a signal.
441 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
444 return TRAP_sig(sigcontext
);
446 return T_UNKNOWN
; /* unknown trap code */
450 /***********************************************************************
453 * Get the error code for a signal.
455 static inline int get_error_code( const SIGCONTEXT
*sigcontext
)
458 return ERROR_sig(sigcontext
);
464 /***********************************************************************
467 * Get the CR2 value for a signal.
469 static inline void *get_cr2_value( const SIGCONTEXT
*sigcontext
)
472 return (void *)CR2_sig(sigcontext
);
480 /***********************************************************************
483 * Set the register values from a vm86 structure.
485 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
487 context
->Eax
= vm86
->regs
.eax
;
488 context
->Ebx
= vm86
->regs
.ebx
;
489 context
->Ecx
= vm86
->regs
.ecx
;
490 context
->Edx
= vm86
->regs
.edx
;
491 context
->Esi
= vm86
->regs
.esi
;
492 context
->Edi
= vm86
->regs
.edi
;
493 context
->Esp
= vm86
->regs
.esp
;
494 context
->Ebp
= vm86
->regs
.ebp
;
495 context
->Eip
= vm86
->regs
.eip
;
496 context
->SegCs
= vm86
->regs
.cs
;
497 context
->SegDs
= vm86
->regs
.ds
;
498 context
->SegEs
= vm86
->regs
.es
;
499 context
->SegFs
= vm86
->regs
.fs
;
500 context
->SegGs
= vm86
->regs
.gs
;
501 context
->SegSs
= vm86
->regs
.ss
;
502 context
->EFlags
= vm86
->regs
.eflags
;
506 /***********************************************************************
507 * restore_vm86_context
509 * Build a vm86 structure from the register values.
511 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
513 vm86
->regs
.eax
= context
->Eax
;
514 vm86
->regs
.ebx
= context
->Ebx
;
515 vm86
->regs
.ecx
= context
->Ecx
;
516 vm86
->regs
.edx
= context
->Edx
;
517 vm86
->regs
.esi
= context
->Esi
;
518 vm86
->regs
.edi
= context
->Edi
;
519 vm86
->regs
.esp
= context
->Esp
;
520 vm86
->regs
.ebp
= context
->Ebp
;
521 vm86
->regs
.eip
= context
->Eip
;
522 vm86
->regs
.cs
= context
->SegCs
;
523 vm86
->regs
.ds
= context
->SegDs
;
524 vm86
->regs
.es
= context
->SegEs
;
525 vm86
->regs
.fs
= context
->SegFs
;
526 vm86
->regs
.gs
= context
->SegGs
;
527 vm86
->regs
.ss
= context
->SegSs
;
528 vm86
->regs
.eflags
= context
->EFlags
;
530 #endif /* __HAVE_VM86 */
533 /***********************************************************************
536 * Set the register values from a sigcontext.
538 static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
541 /* get %fs at time of the fault */
543 fs
= FS_sig(sigcontext
);
549 /* now restore a proper %fs for the fault handler */
550 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext
))) /* 16-bit mode */
552 fs
= SYSLEVEL_Win16CurrentTeb
;
555 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
557 /* fetch the saved %fs on the stack */
558 fs
= *(unsigned int *)ESP_sig(sigcontext
);
559 if (EAX_sig(sigcontext
) == VM86_EAX
) {
560 struct vm86plus_struct
*vm86
;
562 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
563 * (but we could also get if from teb->vm86_ptr) */
564 vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
565 /* get context from vm86 struct */
566 save_vm86_context( context
, vm86
);
570 #endif /* __HAVE_VM86 */
574 context
->Eax
= EAX_sig(sigcontext
);
575 context
->Ebx
= EBX_sig(sigcontext
);
576 context
->Ecx
= ECX_sig(sigcontext
);
577 context
->Edx
= EDX_sig(sigcontext
);
578 context
->Esi
= ESI_sig(sigcontext
);
579 context
->Edi
= EDI_sig(sigcontext
);
580 context
->Ebp
= EBP_sig(sigcontext
);
581 context
->EFlags
= EFL_sig(sigcontext
);
582 context
->Eip
= EIP_sig(sigcontext
);
583 context
->Esp
= ESP_sig(sigcontext
);
584 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
585 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
586 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
587 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
589 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
591 context
->SegGs
= wine_get_gs();
596 /***********************************************************************
599 * Build a sigcontext from the register values.
601 static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
604 /* check if exception occurred in vm86 mode */
605 if ((void *)EIP_sig(sigcontext
) == vm86_return
&&
606 IS_SELECTOR_SYSTEM(CS_sig(sigcontext
)) &&
607 EAX_sig(sigcontext
) == VM86_EAX
)
609 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
610 * (but we could also get it from teb->vm86_ptr) */
611 struct vm86plus_struct
*vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
612 restore_vm86_context( context
, vm86
);
615 #endif /* __HAVE_VM86 */
617 EAX_sig(sigcontext
) = context
->Eax
;
618 EBX_sig(sigcontext
) = context
->Ebx
;
619 ECX_sig(sigcontext
) = context
->Ecx
;
620 EDX_sig(sigcontext
) = context
->Edx
;
621 ESI_sig(sigcontext
) = context
->Esi
;
622 EDI_sig(sigcontext
) = context
->Edi
;
623 EBP_sig(sigcontext
) = context
->Ebp
;
624 EFL_sig(sigcontext
) = context
->EFlags
;
625 EIP_sig(sigcontext
) = context
->Eip
;
626 ESP_sig(sigcontext
) = context
->Esp
;
627 CS_sig(sigcontext
) = context
->SegCs
;
628 DS_sig(sigcontext
) = context
->SegDs
;
629 ES_sig(sigcontext
) = context
->SegEs
;
630 SS_sig(sigcontext
) = context
->SegSs
;
632 FS_sig(sigcontext
) = context
->SegFs
;
634 wine_set_fs( context
->SegFs
);
637 GS_sig(sigcontext
) = context
->SegGs
;
639 wine_set_gs( context
->SegGs
);
644 /***********************************************************************
647 * Set the FPU context from a sigcontext.
649 inline static void save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
652 if (FPU_sig(sigcontext
))
654 context
->FloatSave
= *FPU_sig(sigcontext
);
659 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
660 #endif /* __GNUC__ */
664 /***********************************************************************
667 * Restore the FPU context to a sigcontext.
669 inline static void restore_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
671 /* reset the current interrupt status */
672 context
->FloatSave
.StatusWord
&= context
->FloatSave
.ControlWord
| 0xffffff80;
674 if (FPU_sig(sigcontext
))
676 *FPU_sig(sigcontext
) = context
->FloatSave
;
681 /* avoid nested exceptions */
682 __asm__
__volatile__( "frstor %0; fwait" : : "m" (context
->FloatSave
) );
683 #endif /* __GNUC__ */
687 /**********************************************************************
690 * Get the FPU exception code from the FPU status.
692 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
694 DWORD status
= context
->FloatSave
.StatusWord
;
696 if (status
& 0x01) /* IE */
698 if (status
& 0x40) /* SF */
699 return EXCEPTION_FLT_STACK_CHECK
;
701 return EXCEPTION_FLT_INVALID_OPERATION
;
703 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
704 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
705 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
706 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
707 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
708 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
712 /***********************************************************************
715 * Unblock signals. Called from EXC_RtlRaiseException.
717 void SIGNAL_Unblock( void )
719 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
723 /**********************************************************************
726 * Implementation of SIGSEGV handler.
728 static void do_segv( CONTEXT
*context
, int trap_code
, void *cr2
, int err_code
)
730 EXCEPTION_RECORD rec
;
731 DWORD page_fault_code
= EXCEPTION_ACCESS_VIOLATION
;
734 /* we want the page-fault case to be fast */
735 if (trap_code
== T_PAGEFLT
)
736 if (!(page_fault_code
= VIRTUAL_HandleFault( cr2
))) return;
739 rec
.ExceptionRecord
= NULL
;
740 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
741 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
742 rec
.NumberParameters
= 0;
746 case T_OFLOW
: /* Overflow exception */
747 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
749 case T_BOUND
: /* Bound range exception */
750 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
752 case T_PRIVINFLT
: /* Invalid opcode exception */
753 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
755 case T_STKFLT
: /* Stack fault */
756 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
758 case T_SEGNPFLT
: /* Segment not present exception */
759 case T_PROTFLT
: /* General protection fault */
760 case T_UNKNOWN
: /* Unknown fault code */
761 if (INSTR_EmulateInstruction( context
)) return;
762 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
764 case T_PAGEFLT
: /* Page fault */
766 rec
.NumberParameters
= 2;
767 rec
.ExceptionInformation
[0] = (err_code
& 2) != 0;
768 rec
.ExceptionInformation
[1] = (DWORD
)cr2
;
770 rec
.ExceptionCode
= page_fault_code
;
772 case T_ALIGNFLT
: /* Alignment check exception */
773 /* FIXME: pass through exception handler first? */
774 if (context
->EFlags
& 0x00040000)
776 /* Disable AC flag, return */
777 context
->EFlags
&= ~0x00040000;
780 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
783 ERR( "Got unexpected trap %d\n", trap_code
);
785 case T_NMI
: /* NMI interrupt */
786 case T_DNA
: /* Device not available exception */
787 case T_DOUBLEFLT
: /* Double fault exception */
788 case T_TSSFLT
: /* Invalid TSS exception */
789 case T_RESERVED
: /* Unknown exception */
790 case T_MCHK
: /* Machine check exception */
792 case T_CACHEFLT
: /* Cache flush exception */
794 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
797 EXC_RtlRaiseException( &rec
, context
);
801 /**********************************************************************
804 * Implementation of SIGTRAP handler.
806 static void do_trap( CONTEXT
*context
, int trap_code
)
808 EXCEPTION_RECORD rec
;
810 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
811 rec
.ExceptionRecord
= NULL
;
812 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
813 rec
.NumberParameters
= 0;
817 case T_TRCTRAP
: /* Single-step exception */
818 if (context
->EFlags
& 0x100)
820 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
821 context
->EFlags
&= ~0x100; /* clear single-step flag */
825 /* likely we get this because of a kill(SIGTRAP) on ourself,
826 * so send a bp exception instead of a single step exception
828 TRACE("Spurious single step trap => breakpoint simulation\n");
829 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
832 case T_BPTFLT
: /* Breakpoint exception */
833 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
836 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
839 EXC_RtlRaiseException( &rec
, context
);
843 /**********************************************************************
846 * Implementation of SIGFPE handler
848 static void do_fpe( CONTEXT
*context
, int trap_code
)
850 EXCEPTION_RECORD rec
;
854 case T_DIVIDE
: /* Division by zero exception */
855 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
857 case T_FPOPFLT
: /* Coprocessor segment overrun */
858 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
860 case T_ARITHTRAP
: /* Floating point exception */
861 case T_UNKNOWN
: /* Unknown fault code */
862 rec
.ExceptionCode
= get_fpu_code( context
);
865 ERR( "Got unexpected trap %d\n", trap_code
);
866 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
869 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
870 rec
.ExceptionRecord
= NULL
;
871 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
872 rec
.NumberParameters
= 0;
873 EXC_RtlRaiseException( &rec
, context
);
878 /**********************************************************************
881 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
883 static void set_vm86_pend( CONTEXT
*context
)
885 EXCEPTION_RECORD rec
;
886 TEB
*teb
= NtCurrentTeb();
887 struct vm86plus_struct
*vm86
= (struct vm86plus_struct
*)(teb
->vm86_ptr
);
889 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
890 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
891 rec
.ExceptionRecord
= NULL
;
892 rec
.NumberParameters
= 1;
893 rec
.ExceptionInformation
[0] = 0;
895 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
896 teb
->vm86_pending
|= VIP_MASK
;
897 /* see if we were in VM86 mode */
898 if (context
->EFlags
& 0x00020000)
900 /* seems so, also set flag in signal context */
901 if (context
->EFlags
& VIP_MASK
) return;
902 context
->EFlags
|= VIP_MASK
;
903 vm86
->regs
.eflags
|= VIP_MASK
; /* no exception recursion */
904 if (context
->EFlags
& VIF_MASK
) {
905 /* VIF is set, throw exception */
906 teb
->vm86_pending
= 0;
907 teb
->vm86_ptr
= NULL
;
908 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
909 EXC_RtlRaiseException( &rec
, context
);
910 teb
->vm86_ptr
= vm86
;
915 /* not in VM86, but possibly setting up for it */
916 if (vm86
->regs
.eflags
& VIP_MASK
) return;
917 vm86
->regs
.eflags
|= VIP_MASK
;
918 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
919 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
920 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
921 /* exiting from VM86, can't throw */
924 if (vm86
->regs
.eflags
& VIF_MASK
) {
925 /* VIF is set, throw exception */
927 teb
->vm86_pending
= 0;
928 teb
->vm86_ptr
= NULL
;
929 save_vm86_context( &vcontext
, vm86
);
930 rec
.ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
931 EXC_RtlRaiseException( &rec
, &vcontext
);
932 teb
->vm86_ptr
= vm86
;
933 restore_vm86_context( &vcontext
, vm86
);
939 /**********************************************************************
942 * Handler for SIGUSR2.
943 * We use it to signal that the running __wine_enter_vm86() should
944 * immediately set VIP_MASK, causing pending events to be handled
945 * as early as possible.
947 static HANDLER_DEF(usr2_handler
)
951 save_context( &context
, HANDLER_CONTEXT
);
952 set_vm86_pend( &context
);
953 restore_context( &context
, HANDLER_CONTEXT
);
957 /**********************************************************************
960 * Handler for SIGALRM.
961 * Increases the alarm counter and sets the vm86 pending flag.
963 static HANDLER_DEF(alrm_handler
)
967 save_context( &context
, HANDLER_CONTEXT
);
968 NtCurrentTeb()->alarms
++;
969 set_vm86_pend( &context
);
970 restore_context( &context
, HANDLER_CONTEXT
);
972 #endif /* __HAVE_VM86 */
975 /**********************************************************************
978 * Handler for SIGSEGV and related errors.
980 static HANDLER_DEF(segv_handler
)
983 save_context( &context
, HANDLER_CONTEXT
);
984 do_segv( &context
, get_trap_code(HANDLER_CONTEXT
),
985 get_cr2_value(HANDLER_CONTEXT
), get_error_code(HANDLER_CONTEXT
) );
986 restore_context( &context
, HANDLER_CONTEXT
);
990 /**********************************************************************
993 * Handler for SIGTRAP.
995 static HANDLER_DEF(trap_handler
)
998 save_context( &context
, HANDLER_CONTEXT
);
999 do_trap( &context
, get_trap_code(HANDLER_CONTEXT
) );
1000 restore_context( &context
, HANDLER_CONTEXT
);
1004 /**********************************************************************
1007 * Handler for SIGFPE.
1009 static HANDLER_DEF(fpe_handler
)
1012 save_fpu( &context
, HANDLER_CONTEXT
);
1013 save_context( &context
, HANDLER_CONTEXT
);
1014 do_fpe( &context
, get_trap_code(HANDLER_CONTEXT
) );
1015 restore_context( &context
, HANDLER_CONTEXT
);
1016 restore_fpu( &context
, HANDLER_CONTEXT
);
1020 /**********************************************************************
1023 * Handler for SIGINT.
1025 static HANDLER_DEF(int_handler
)
1027 if (!dispatch_signal(SIGINT
))
1029 EXCEPTION_RECORD rec
;
1032 save_context( &context
, HANDLER_CONTEXT
);
1033 rec
.ExceptionCode
= CONTROL_C_EXIT
;
1034 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1035 rec
.ExceptionRecord
= NULL
;
1036 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
1037 rec
.NumberParameters
= 0;
1038 EXC_RtlRaiseException( &rec
, &context
);
1039 restore_context( &context
, HANDLER_CONTEXT
);
1044 /***********************************************************************
1047 * Set a signal handler
1049 static int set_handler( int sig
, int have_sigaltstack
, void (*func
)() )
1051 struct sigaction sig_act
;
1054 if (!have_sigaltstack
&& NtCurrentTeb()->signal_stack
)
1056 struct kernel_sigaction sig_act
;
1057 sig_act
.ksa_handler
= func
;
1058 sig_act
.ksa_flags
= SA_RESTART
;
1059 sig_act
.ksa_mask
= (1 << (SIGINT
-1)) |
1061 /* point to the top of the stack */
1062 sig_act
.ksa_restorer
= (char *)NtCurrentTeb()->signal_stack
+ SIGNAL_STACK_SIZE
;
1063 return wine_sigaction( sig
, &sig_act
, NULL
);
1066 sig_act
.sa_handler
= func
;
1067 sigemptyset( &sig_act
.sa_mask
);
1068 sigaddset( &sig_act
.sa_mask
, SIGINT
);
1069 sigaddset( &sig_act
.sa_mask
, SIGALRM
);
1072 sig_act
.sa_flags
= SA_RESTART
;
1073 #elif defined (__svr4__) || defined(_SCO_DS)
1074 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1076 sig_act
.sa_flags
= 0;
1080 if (have_sigaltstack
) sig_act
.sa_flags
|= SA_ONSTACK
;
1082 return sigaction( sig
, &sig_act
, NULL
);
1086 /***********************************************************************
1087 * __wine_set_signal_handler (NTDLL.@)
1089 int __wine_set_signal_handler(unsigned sig
, wine_signal_handler wsh
)
1091 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1092 if (handlers
[sig
] != NULL
) return -2;
1093 handlers
[sig
] = wsh
;
1098 /**********************************************************************
1101 BOOL
SIGNAL_Init(void)
1103 int have_sigaltstack
= 0;
1105 #ifdef HAVE_SIGALTSTACK
1106 struct sigaltstack ss
;
1107 if ((ss
.ss_sp
= NtCurrentTeb()->signal_stack
))
1109 ss
.ss_size
= SIGNAL_STACK_SIZE
;
1111 if (!sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1113 /* sigaltstack may fail because the kernel is too old, or
1114 because glibc is brain-dead. In the latter case a
1115 direct system call should succeed. */
1116 else if (!wine_sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1119 #endif /* HAVE_SIGALTSTACK */
1121 sigfillset( &all_sigs
);
1123 if (set_handler( SIGINT
, have_sigaltstack
, (void (*)())int_handler
) == -1) goto error
;
1124 if (set_handler( SIGFPE
, have_sigaltstack
, (void (*)())fpe_handler
) == -1) goto error
;
1125 if (set_handler( SIGSEGV
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1126 if (set_handler( SIGILL
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1128 if (set_handler( SIGBUS
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1131 if (set_handler( SIGTRAP
, have_sigaltstack
, (void (*)())trap_handler
) == -1) goto error
;
1135 if (set_handler( SIGALRM
, have_sigaltstack
, (void (*)())alrm_handler
) == -1) goto error
;
1136 if (set_handler( SIGUSR2
, have_sigaltstack
, (void (*)())usr2_handler
) == -1) goto error
;
1142 perror("sigaction");
1147 /**********************************************************************
1150 void SIGNAL_Reset(void)
1154 /* block the async signals */
1155 sigemptyset( &block_set
);
1156 sigaddset( &block_set
, SIGALRM
);
1157 sigaddset( &block_set
, SIGIO
);
1158 sigaddset( &block_set
, SIGHUP
);
1159 sigaddset( &block_set
, SIGUSR2
);
1160 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
1162 /* restore default handlers */
1163 signal( SIGINT
, SIG_DFL
);
1164 signal( SIGFPE
, SIG_DFL
);
1165 signal( SIGSEGV
, SIG_DFL
);
1166 signal( SIGILL
, SIG_DFL
);
1168 signal( SIGBUS
, SIG_DFL
);
1171 signal( SIGTRAP
, SIG_DFL
);
1177 /**********************************************************************
1180 * Enter vm86 mode with the specified register context.
1182 void __wine_enter_vm86( CONTEXT
*context
)
1184 EXCEPTION_RECORD rec
;
1185 TEB
*teb
= NtCurrentTeb();
1187 struct vm86plus_struct vm86
;
1189 memset( &vm86
, 0, sizeof(vm86
) );
1192 restore_vm86_context( context
, &vm86
);
1193 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1194 * so save it on entry, just in case */
1195 teb
->vm86_pending
|= (context
->EFlags
& VIP_MASK
);
1196 /* Work around race conditions with signal handler
1197 * (avoiding sigprocmask for performance reasons) */
1198 teb
->vm86_ptr
= &vm86
;
1199 vm86
.regs
.eflags
|= teb
->vm86_pending
;
1200 /* Check for VIF|VIP here, since vm86_enter doesn't */
1201 if ((vm86
.regs
.eflags
& (VIF_MASK
|VIP_MASK
)) == (VIF_MASK
|VIP_MASK
)) {
1202 teb
->vm86_ptr
= NULL
;
1203 teb
->vm86_pending
= 0;
1204 context
->EFlags
|= VIP_MASK
;
1205 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1206 rec
.ExceptionInformation
[0] = 0;
1212 res
= vm86_enter( &teb
->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1218 } while (VM86_TYPE(res
) == VM86_SIGNAL
);
1220 save_vm86_context( context
, &vm86
);
1221 context
->EFlags
|= teb
->vm86_pending
;
1223 switch(VM86_TYPE(res
))
1225 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1226 do_segv( context
, T_PROTFLT
, 0, 0 );
1228 case VM86_TRAP
: /* return due to DOS-debugger request */
1229 do_trap( context
, VM86_ARG(res
) );
1231 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1232 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1234 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1235 teb
->vm86_pending
= 0;
1236 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1238 case VM86_PICRETURN
: /* return due to pending PIC request */
1239 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1242 ERR( "unhandled result from vm86 mode %x\n", res
);
1245 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1247 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1248 rec
.ExceptionRecord
= NULL
;
1249 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1250 rec
.NumberParameters
= 1;
1251 EXC_RtlRaiseException( &rec
, context
);
1255 #else /* __HAVE_VM86 */
1256 void __wine_enter_vm86( CONTEXT
*context
)
1258 MESSAGE("vm86 mode not supported on this platform\n");
1260 #endif /* __HAVE_VM86 */
1262 /**********************************************************************
1263 * DbgBreakPoint (NTDLL.@)
1265 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret");
1267 /**********************************************************************
1268 * DbgUserBreakPoint (NTDLL.@)
1270 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret");
1272 #endif /* __i386__ */