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"
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
40 # ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
45 #ifdef HAVE_SYS_VM86_H
46 # include <sys/vm86.h>
49 #ifdef HAVE_SYS_SIGNAL_H
50 # include <sys/signal.h>
55 #include "wine/library.h"
57 #include "selectors.h"
59 /***********************************************************************
60 * signal context platform-specific definitions
66 unsigned short sc_gs
, __gsh
;
67 unsigned short sc_fs
, __fsh
;
68 unsigned short sc_es
, __esh
;
69 unsigned short sc_ds
, __dsh
;
78 unsigned long sc_trapno
;
81 unsigned short sc_cs
, __csh
;
82 unsigned long sc_eflags
;
83 unsigned long esp_at_signal
;
84 unsigned short sc_ss
, __ssh
;
86 unsigned long oldmask
;
90 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
91 #define HANDLER_CONTEXT (&__context)
93 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
94 struct kernel_sigaction
96 void (*ksa_handler
)();
97 unsigned long ksa_mask
;
98 unsigned long ksa_flags
;
102 /* Similar to the sigaction function in libc, except it leaves alone the
103 restorer field, which is used to specify the signal stack address */
104 static inline int wine_sigaction( int sig
, struct kernel_sigaction
*new,
105 struct kernel_sigaction
*old
)
107 __asm__
__volatile__( "pushl %%ebx\n\t"
112 : "0" (SYS_sigaction
), "r" (sig
), "c" (new), "d" (old
) );
113 if (sig
>=0) return 0;
118 #ifdef HAVE_SIGALTSTACK
119 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
120 static inline int wine_sigaltstack( const struct sigaltstack
*new,
121 struct sigaltstack
*old
)
124 __asm__
__volatile__( "pushl %%ebx\n\t"
129 : "0" (SYS_sigaltstack
), "r" (new), "c" (old
) );
130 if (ret
>= 0) return 0;
136 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
138 int vm86_enter( void **vm86_ptr
);
139 void vm86_return(void);
140 void vm86_return_end(void);
141 __ASM_GLOBAL_FUNC(vm86_enter
,
143 "movl %esp, %ebp\n\t"
144 "movl $166,%eax\n\t" /*SYS_vm86*/
145 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
146 "movl (%ecx),%ecx\n\t"
148 "movl $1,%ebx\n\t" /*VM86_ENTER*/
149 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
152 ".globl " __ASM_NAME("vm86_return") "\n\t"
153 __ASM_FUNC("vm86_return") "\n"
154 __ASM_NAME("vm86_return") ":\n\t"
159 "testl %eax,%eax\n\t"
161 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
162 "je " __ASM_NAME("vm86_enter") "\n\t"
164 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
166 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
167 __ASM_FUNC("vm86_return_end") "\n"
168 __ASM_NAME("vm86_return_end") ":\n\t"
171 #ifdef HAVE_SYS_VM86_H
179 #define EAX_sig(context) ((context)->tf_eax)
180 #define EBX_sig(context) ((context)->tf_ebx)
181 #define ECX_sig(context) ((context)->tf_ecx)
182 #define EDX_sig(context) ((context)->tf_edx)
183 #define ESI_sig(context) ((context)->tf_esi)
184 #define EDI_sig(context) ((context)->tf_edi)
185 #define EBP_sig(context) ((context)->tf_ebp)
187 #define CS_sig(context) ((context)->tf_cs)
188 #define DS_sig(context) ((context)->tf_ds)
189 #define ES_sig(context) ((context)->tf_es)
190 #define SS_sig(context) ((context)->tf_ss)
192 #include <machine/frame.h>
193 typedef struct trapframe SIGCONTEXT
;
195 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
196 #define HANDLER_CONTEXT __context
198 #define EFL_sig(context) ((context)->tf_eflags)
200 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
201 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
205 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
207 typedef struct sigcontext SIGCONTEXT
;
209 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
210 #define HANDLER_CONTEXT __context
214 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
217 #include <sys/regset.h>
221 #include <sys/ucontext.h>
223 typedef struct ucontext SIGCONTEXT
;
225 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
226 #define HANDLER_CONTEXT __context
228 #endif /* svr4 || SCO_DS */
234 unsigned long ContextFlags
;
235 FLOATING_SAVE_AREA sc_float
;
240 unsigned long sc_edi
;
241 unsigned long sc_esi
;
242 unsigned long sc_eax
;
243 unsigned long sc_ebx
;
244 unsigned long sc_ecx
;
245 unsigned long sc_edx
;
246 unsigned long sc_ebp
;
247 unsigned long sc_eip
;
249 unsigned long sc_eflags
;
250 unsigned long sc_esp
;
258 /* FIXME: This section is just here so it can compile, it's most likely
259 * completely wrong. */
263 unsigned short sc_gs
, __gsh
;
264 unsigned short sc_fs
, __fsh
;
265 unsigned short sc_es
, __esh
;
266 unsigned short sc_ds
, __dsh
;
267 unsigned long sc_edi
;
268 unsigned long sc_esi
;
269 unsigned long sc_ebp
;
270 unsigned long sc_esp
;
271 unsigned long sc_ebx
;
272 unsigned long sc_edx
;
273 unsigned long sc_ecx
;
274 unsigned long sc_eax
;
275 unsigned long sc_trapno
;
276 unsigned long sc_err
;
277 unsigned long sc_eip
;
278 unsigned short sc_cs
, __csh
;
279 unsigned long sc_eflags
;
280 unsigned long esp_at_signal
;
281 unsigned short sc_ss
, __ssh
;
283 unsigned long oldmask
;
287 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
288 #define HANDLER_CONTEXT (&__context)
290 #endif /* __CYGWIN__ */
292 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
293 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
295 #define EAX_sig(context) ((context)->sc_eax)
296 #define EBX_sig(context) ((context)->sc_ebx)
297 #define ECX_sig(context) ((context)->sc_ecx)
298 #define EDX_sig(context) ((context)->sc_edx)
299 #define ESI_sig(context) ((context)->sc_esi)
300 #define EDI_sig(context) ((context)->sc_edi)
301 #define EBP_sig(context) ((context)->sc_ebp)
303 #define CS_sig(context) ((context)->sc_cs)
304 #define DS_sig(context) ((context)->sc_ds)
305 #define ES_sig(context) ((context)->sc_es)
306 #define SS_sig(context) ((context)->sc_ss)
308 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
309 * saved by the exception handling. duh.
310 * Actually they are in -current (have been for a while), and that
311 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
312 * If you're running a system from the -stable branch older than that,
313 * like a 3.3-RELEASE, grab the patch from the ports tree:
314 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
315 * (If its not yet there when you look, go here:
316 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
319 #define FS_sig(context) ((context)->sc_fs)
320 #define GS_sig(context) ((context)->sc_gs)
324 #define FS_sig(context) ((context)->sc_fs)
325 #define GS_sig(context) ((context)->sc_gs)
326 #define CR2_sig(context) ((context)->cr2)
327 #define TRAP_sig(context) ((context)->sc_trapno)
328 #define ERROR_sig(context) ((context)->sc_err)
329 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
333 #define EFL_sig(context) ((context)->sc_eflags)
335 #define EFL_sig(context) ((context)->sc_efl)
336 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
337 #define CR2_sig(context) ((context)->sc_err)
338 #define TRAP_sig(context) ((context)->sc_trapno)
341 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
342 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
344 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
346 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
352 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
353 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
354 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
355 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
356 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
357 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
358 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
360 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
361 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
362 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
363 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
365 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
366 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
368 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
370 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
372 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
374 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
377 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
380 #endif /* svr4 || SCO_DS */
383 /* exception code definitions (already defined by FreeBSD/NetBSD) */
384 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
385 #define T_DIVIDE 0 /* Division by zero exception */
386 #define T_TRCTRAP 1 /* Single-step exception */
387 #define T_NMI 2 /* NMI interrupt */
388 #define T_BPTFLT 3 /* Breakpoint exception */
389 #define T_OFLOW 4 /* Overflow exception */
390 #define T_BOUND 5 /* Bound range exception */
391 #define T_PRIVINFLT 6 /* Invalid opcode exception */
392 #define T_DNA 7 /* Device not available exception */
393 #define T_DOUBLEFLT 8 /* Double fault exception */
394 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
395 #define T_TSSFLT 10 /* Invalid TSS exception */
396 #define T_SEGNPFLT 11 /* Segment not present exception */
397 #define T_STKFLT 12 /* Stack fault */
398 #define T_PROTFLT 13 /* General protection fault */
399 #define T_PAGEFLT 14 /* Page fault */
400 #define T_RESERVED 15 /* Unknown exception */
401 #define T_ARITHTRAP 16 /* Floating point exception */
402 #define T_ALIGNFLT 17 /* Alignment check exception */
403 #define T_MCHK 18 /* Machine check exception */
404 #define T_CACHEFLT 19 /* Cache flush exception */
406 #if defined(__NetBSD__)
407 #define T_MCHK 19 /* Machine check exception */
410 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
412 #include "wine/exception.h"
413 #include "stackframe.h"
416 #include "syslevel.h"
417 #include "wine/debug.h"
419 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
421 typedef int (*wine_signal_handler
)(unsigned int sig
);
423 static wine_signal_handler handlers
[256];
425 static sigset_t all_sigs
;
428 /***********************************************************************
431 inline static int dispatch_signal(unsigned int sig
)
433 if (handlers
[sig
] == NULL
) return 0;
434 return handlers
[sig
](sig
);
438 /***********************************************************************
441 * Get the trap code for a signal.
443 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
446 return TRAP_sig(sigcontext
);
448 return T_UNKNOWN
; /* unknown trap code */
452 /***********************************************************************
455 * Get the error code for a signal.
457 static inline int get_error_code( const SIGCONTEXT
*sigcontext
)
460 return ERROR_sig(sigcontext
);
466 /***********************************************************************
469 * Get the CR2 value for a signal.
471 static inline void *get_cr2_value( const SIGCONTEXT
*sigcontext
)
474 return (void *)CR2_sig(sigcontext
);
482 /***********************************************************************
485 * Set the register values from a vm86 structure.
487 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
489 context
->Eax
= vm86
->regs
.eax
;
490 context
->Ebx
= vm86
->regs
.ebx
;
491 context
->Ecx
= vm86
->regs
.ecx
;
492 context
->Edx
= vm86
->regs
.edx
;
493 context
->Esi
= vm86
->regs
.esi
;
494 context
->Edi
= vm86
->regs
.edi
;
495 context
->Esp
= vm86
->regs
.esp
;
496 context
->Ebp
= vm86
->regs
.ebp
;
497 context
->Eip
= vm86
->regs
.eip
;
498 context
->SegCs
= vm86
->regs
.cs
;
499 context
->SegDs
= vm86
->regs
.ds
;
500 context
->SegEs
= vm86
->regs
.es
;
501 context
->SegFs
= vm86
->regs
.fs
;
502 context
->SegGs
= vm86
->regs
.gs
;
503 context
->SegSs
= vm86
->regs
.ss
;
504 context
->EFlags
= vm86
->regs
.eflags
;
508 /***********************************************************************
509 * restore_vm86_context
511 * Build a vm86 structure from the register values.
513 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
515 vm86
->regs
.eax
= context
->Eax
;
516 vm86
->regs
.ebx
= context
->Ebx
;
517 vm86
->regs
.ecx
= context
->Ecx
;
518 vm86
->regs
.edx
= context
->Edx
;
519 vm86
->regs
.esi
= context
->Esi
;
520 vm86
->regs
.edi
= context
->Edi
;
521 vm86
->regs
.esp
= context
->Esp
;
522 vm86
->regs
.ebp
= context
->Ebp
;
523 vm86
->regs
.eip
= context
->Eip
;
524 vm86
->regs
.cs
= context
->SegCs
;
525 vm86
->regs
.ds
= context
->SegDs
;
526 vm86
->regs
.es
= context
->SegEs
;
527 vm86
->regs
.fs
= context
->SegFs
;
528 vm86
->regs
.gs
= context
->SegGs
;
529 vm86
->regs
.ss
= context
->SegSs
;
530 vm86
->regs
.eflags
= context
->EFlags
;
532 #endif /* __HAVE_VM86 */
535 /***********************************************************************
538 * Set the register values from a sigcontext.
540 static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
543 /* get %fs at time of the fault */
545 fs
= FS_sig(sigcontext
);
551 /* now restore a proper %fs for the fault handler */
552 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext
))) /* 16-bit mode */
554 fs
= SYSLEVEL_Win16CurrentTeb
;
557 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
559 /* fetch the saved %fs on the stack */
560 fs
= *(unsigned int *)ESP_sig(sigcontext
);
561 if (EAX_sig(sigcontext
) == VM86_EAX
) {
562 struct vm86plus_struct
*vm86
;
564 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
565 * (but we could also get if from teb->vm86_ptr) */
566 vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
567 /* get context from vm86 struct */
568 save_vm86_context( context
, vm86
);
572 #endif /* __HAVE_VM86 */
576 context
->Eax
= EAX_sig(sigcontext
);
577 context
->Ebx
= EBX_sig(sigcontext
);
578 context
->Ecx
= ECX_sig(sigcontext
);
579 context
->Edx
= EDX_sig(sigcontext
);
580 context
->Esi
= ESI_sig(sigcontext
);
581 context
->Edi
= EDI_sig(sigcontext
);
582 context
->Ebp
= EBP_sig(sigcontext
);
583 context
->EFlags
= EFL_sig(sigcontext
);
584 context
->Eip
= EIP_sig(sigcontext
);
585 context
->Esp
= ESP_sig(sigcontext
);
586 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
587 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
588 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
589 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
591 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
593 context
->SegGs
= wine_get_gs();
598 /***********************************************************************
601 * Build a sigcontext from the register values.
603 static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
606 /* check if exception occurred in vm86 mode */
607 if ((void *)EIP_sig(sigcontext
) == vm86_return
&&
608 IS_SELECTOR_SYSTEM(CS_sig(sigcontext
)) &&
609 EAX_sig(sigcontext
) == VM86_EAX
)
611 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
612 * (but we could also get it from teb->vm86_ptr) */
613 struct vm86plus_struct
*vm86
= *(struct vm86plus_struct
**)(ESP_sig(sigcontext
) + sizeof(int));
614 restore_vm86_context( context
, vm86
);
617 #endif /* __HAVE_VM86 */
619 EAX_sig(sigcontext
) = context
->Eax
;
620 EBX_sig(sigcontext
) = context
->Ebx
;
621 ECX_sig(sigcontext
) = context
->Ecx
;
622 EDX_sig(sigcontext
) = context
->Edx
;
623 ESI_sig(sigcontext
) = context
->Esi
;
624 EDI_sig(sigcontext
) = context
->Edi
;
625 EBP_sig(sigcontext
) = context
->Ebp
;
626 EFL_sig(sigcontext
) = context
->EFlags
;
627 EIP_sig(sigcontext
) = context
->Eip
;
628 ESP_sig(sigcontext
) = context
->Esp
;
629 CS_sig(sigcontext
) = context
->SegCs
;
630 DS_sig(sigcontext
) = context
->SegDs
;
631 ES_sig(sigcontext
) = context
->SegEs
;
632 SS_sig(sigcontext
) = context
->SegSs
;
634 FS_sig(sigcontext
) = context
->SegFs
;
636 wine_set_fs( context
->SegFs
);
639 GS_sig(sigcontext
) = context
->SegGs
;
641 wine_set_gs( context
->SegGs
);
646 /***********************************************************************
649 * Set the FPU context from a sigcontext.
651 inline static void save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
654 if (FPU_sig(sigcontext
))
656 context
->FloatSave
= *FPU_sig(sigcontext
);
661 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
662 #endif /* __GNUC__ */
666 /***********************************************************************
669 * Restore the FPU context to a sigcontext.
671 inline static void restore_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
673 /* reset the current interrupt status */
674 context
->FloatSave
.StatusWord
&= context
->FloatSave
.ControlWord
| 0xffffff80;
676 if (FPU_sig(sigcontext
))
678 *FPU_sig(sigcontext
) = context
->FloatSave
;
683 /* avoid nested exceptions */
684 __asm__
__volatile__( "frstor %0; fwait" : : "m" (context
->FloatSave
) );
685 #endif /* __GNUC__ */
689 /**********************************************************************
692 * Get the FPU exception code from the FPU status.
694 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
696 DWORD status
= context
->FloatSave
.StatusWord
;
698 if (status
& 0x01) /* IE */
700 if (status
& 0x40) /* SF */
701 return EXCEPTION_FLT_STACK_CHECK
;
703 return EXCEPTION_FLT_INVALID_OPERATION
;
705 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
706 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
707 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
708 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
709 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
710 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
714 /***********************************************************************
717 * Unblock signals. Called from EXC_RtlRaiseException.
719 void SIGNAL_Unblock( void )
721 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
725 /**********************************************************************
728 * Implementation of SIGSEGV handler.
730 static void do_segv( CONTEXT
*context
, int trap_code
, void *cr2
, int err_code
)
732 EXCEPTION_RECORD rec
;
733 DWORD page_fault_code
= EXCEPTION_ACCESS_VIOLATION
;
736 /* we want the page-fault case to be fast */
737 if (trap_code
== T_PAGEFLT
)
738 if (!(page_fault_code
= VIRTUAL_HandleFault( cr2
))) return;
741 rec
.ExceptionRecord
= NULL
;
742 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
743 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
744 rec
.NumberParameters
= 0;
748 case T_OFLOW
: /* Overflow exception */
749 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
751 case T_BOUND
: /* Bound range exception */
752 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
754 case T_PRIVINFLT
: /* Invalid opcode exception */
755 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
757 case T_STKFLT
: /* Stack fault */
758 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
760 case T_SEGNPFLT
: /* Segment not present exception */
761 case T_PROTFLT
: /* General protection fault */
762 case T_UNKNOWN
: /* Unknown fault code */
763 if (INSTR_EmulateInstruction( context
)) return;
764 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
766 case T_PAGEFLT
: /* Page fault */
768 rec
.NumberParameters
= 2;
769 rec
.ExceptionInformation
[0] = (err_code
& 2) != 0;
770 rec
.ExceptionInformation
[1] = (DWORD
)cr2
;
772 rec
.ExceptionCode
= page_fault_code
;
774 case T_ALIGNFLT
: /* Alignment check exception */
775 /* FIXME: pass through exception handler first? */
776 if (context
->EFlags
& 0x00040000)
778 /* Disable AC flag, return */
779 context
->EFlags
&= ~0x00040000;
782 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
785 ERR( "Got unexpected trap %d\n", trap_code
);
787 case T_NMI
: /* NMI interrupt */
788 case T_DNA
: /* Device not available exception */
789 case T_DOUBLEFLT
: /* Double fault exception */
790 case T_TSSFLT
: /* Invalid TSS exception */
791 case T_RESERVED
: /* Unknown exception */
792 case T_MCHK
: /* Machine check exception */
794 case T_CACHEFLT
: /* Cache flush exception */
796 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
799 EXC_RtlRaiseException( &rec
, context
);
803 /**********************************************************************
806 * Implementation of SIGTRAP handler.
808 static void do_trap( CONTEXT
*context
, int trap_code
)
810 EXCEPTION_RECORD rec
;
812 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
813 rec
.ExceptionRecord
= NULL
;
814 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
815 rec
.NumberParameters
= 0;
819 case T_TRCTRAP
: /* Single-step exception */
820 if (context
->EFlags
& 0x100)
822 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
823 context
->EFlags
&= ~0x100; /* clear single-step flag */
827 /* likely we get this because of a kill(SIGTRAP) on ourself,
828 * so send a bp exception instead of a single step exception
830 TRACE("Spurious single step trap => breakpoint simulation\n");
831 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
834 case T_BPTFLT
: /* Breakpoint exception */
835 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
838 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
841 EXC_RtlRaiseException( &rec
, context
);
845 /**********************************************************************
848 * Implementation of SIGFPE handler
850 static void do_fpe( CONTEXT
*context
, int trap_code
)
852 EXCEPTION_RECORD rec
;
856 case T_DIVIDE
: /* Division by zero exception */
857 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
859 case T_FPOPFLT
: /* Coprocessor segment overrun */
860 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
862 case T_ARITHTRAP
: /* Floating point exception */
863 case T_UNKNOWN
: /* Unknown fault code */
864 rec
.ExceptionCode
= get_fpu_code( context
);
867 ERR( "Got unexpected trap %d\n", trap_code
);
868 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
871 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
872 rec
.ExceptionRecord
= NULL
;
873 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
874 rec
.NumberParameters
= 0;
875 EXC_RtlRaiseException( &rec
, context
);
880 /**********************************************************************
883 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
885 static void set_vm86_pend( CONTEXT
*context
)
887 EXCEPTION_RECORD rec
;
888 TEB
*teb
= NtCurrentTeb();
889 struct vm86plus_struct
*vm86
= (struct vm86plus_struct
*)(teb
->vm86_ptr
);
891 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
892 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
893 rec
.ExceptionRecord
= NULL
;
894 rec
.NumberParameters
= 1;
895 rec
.ExceptionInformation
[0] = 0;
897 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
898 teb
->vm86_pending
|= VIP_MASK
;
899 /* see if we were in VM86 mode */
900 if (context
->EFlags
& 0x00020000)
902 /* seems so, also set flag in signal context */
903 if (context
->EFlags
& VIP_MASK
) return;
904 context
->EFlags
|= VIP_MASK
;
905 vm86
->regs
.eflags
|= VIP_MASK
; /* no exception recursion */
906 if (context
->EFlags
& VIF_MASK
) {
907 /* VIF is set, throw exception */
908 teb
->vm86_pending
= 0;
909 teb
->vm86_ptr
= NULL
;
910 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
911 EXC_RtlRaiseException( &rec
, context
);
912 teb
->vm86_ptr
= vm86
;
917 /* not in VM86, but possibly setting up for it */
918 if (vm86
->regs
.eflags
& VIP_MASK
) return;
919 vm86
->regs
.eflags
|= VIP_MASK
;
920 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
921 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
922 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
923 /* exiting from VM86, can't throw */
926 if (vm86
->regs
.eflags
& VIF_MASK
) {
927 /* VIF is set, throw exception */
929 teb
->vm86_pending
= 0;
930 teb
->vm86_ptr
= NULL
;
931 save_vm86_context( &vcontext
, vm86
);
932 rec
.ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
933 EXC_RtlRaiseException( &rec
, &vcontext
);
934 teb
->vm86_ptr
= vm86
;
935 restore_vm86_context( &vcontext
, vm86
);
941 /**********************************************************************
944 * Handler for SIGUSR2.
945 * We use it to signal that the running __wine_enter_vm86() should
946 * immediately set VIP_MASK, causing pending events to be handled
947 * as early as possible.
949 static HANDLER_DEF(usr2_handler
)
953 save_context( &context
, HANDLER_CONTEXT
);
954 set_vm86_pend( &context
);
955 restore_context( &context
, HANDLER_CONTEXT
);
959 /**********************************************************************
962 * Handler for SIGALRM.
963 * Increases the alarm counter and sets the vm86 pending flag.
965 static HANDLER_DEF(alrm_handler
)
969 save_context( &context
, HANDLER_CONTEXT
);
970 NtCurrentTeb()->alarms
++;
971 set_vm86_pend( &context
);
972 restore_context( &context
, HANDLER_CONTEXT
);
974 #endif /* __HAVE_VM86 */
977 /**********************************************************************
980 * Handler for SIGSEGV and related errors.
982 static HANDLER_DEF(segv_handler
)
985 save_context( &context
, HANDLER_CONTEXT
);
986 do_segv( &context
, get_trap_code(HANDLER_CONTEXT
),
987 get_cr2_value(HANDLER_CONTEXT
), get_error_code(HANDLER_CONTEXT
) );
988 restore_context( &context
, HANDLER_CONTEXT
);
992 /**********************************************************************
995 * Handler for SIGTRAP.
997 static HANDLER_DEF(trap_handler
)
1000 save_context( &context
, HANDLER_CONTEXT
);
1001 do_trap( &context
, get_trap_code(HANDLER_CONTEXT
) );
1002 restore_context( &context
, HANDLER_CONTEXT
);
1006 /**********************************************************************
1009 * Handler for SIGFPE.
1011 static HANDLER_DEF(fpe_handler
)
1014 save_fpu( &context
, HANDLER_CONTEXT
);
1015 save_context( &context
, HANDLER_CONTEXT
);
1016 do_fpe( &context
, get_trap_code(HANDLER_CONTEXT
) );
1017 restore_context( &context
, HANDLER_CONTEXT
);
1018 restore_fpu( &context
, HANDLER_CONTEXT
);
1022 /**********************************************************************
1025 * Handler for SIGINT.
1027 static HANDLER_DEF(int_handler
)
1029 if (!dispatch_signal(SIGINT
))
1031 EXCEPTION_RECORD rec
;
1034 save_context( &context
, HANDLER_CONTEXT
);
1035 rec
.ExceptionCode
= CONTROL_C_EXIT
;
1036 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1037 rec
.ExceptionRecord
= NULL
;
1038 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
1039 rec
.NumberParameters
= 0;
1040 EXC_RtlRaiseException( &rec
, &context
);
1041 restore_context( &context
, HANDLER_CONTEXT
);
1045 /**********************************************************************
1048 * Handler for SIGABRT.
1050 static HANDLER_DEF(abrt_handler
)
1052 EXCEPTION_RECORD rec
;
1055 save_context( &context
, HANDLER_CONTEXT
);
1056 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
1057 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
1058 rec
.ExceptionRecord
= NULL
;
1059 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
1060 rec
.NumberParameters
= 0;
1061 EXC_RtlRaiseException( &rec
, &context
); /* Should never return.. */
1062 restore_context( &context
, HANDLER_CONTEXT
);
1066 /***********************************************************************
1069 * Set a signal handler
1071 static int set_handler( int sig
, int have_sigaltstack
, void (*func
)() )
1073 struct sigaction sig_act
;
1076 if (!have_sigaltstack
&& NtCurrentTeb()->signal_stack
)
1078 struct kernel_sigaction sig_act
;
1079 sig_act
.ksa_handler
= func
;
1080 sig_act
.ksa_flags
= SA_RESTART
;
1081 sig_act
.ksa_mask
= (1 << (SIGINT
-1)) |
1083 /* point to the top of the stack */
1084 sig_act
.ksa_restorer
= (char *)NtCurrentTeb()->signal_stack
+ SIGNAL_STACK_SIZE
;
1085 return wine_sigaction( sig
, &sig_act
, NULL
);
1088 sig_act
.sa_handler
= func
;
1089 sigemptyset( &sig_act
.sa_mask
);
1090 sigaddset( &sig_act
.sa_mask
, SIGINT
);
1091 sigaddset( &sig_act
.sa_mask
, SIGALRM
);
1094 sig_act
.sa_flags
= SA_RESTART
;
1095 #elif defined (__svr4__) || defined(_SCO_DS)
1096 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1098 sig_act
.sa_flags
= 0;
1102 if (have_sigaltstack
) sig_act
.sa_flags
|= SA_ONSTACK
;
1104 return sigaction( sig
, &sig_act
, NULL
);
1108 /***********************************************************************
1109 * __wine_set_signal_handler (NTDLL.@)
1111 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1113 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1114 if (handlers
[sig
] != NULL
) return -2;
1115 handlers
[sig
] = wsh
;
1120 /**********************************************************************
1123 BOOL
SIGNAL_Init(void)
1125 int have_sigaltstack
= 0;
1127 #ifdef HAVE_SIGALTSTACK
1128 struct sigaltstack ss
;
1129 if ((ss
.ss_sp
= NtCurrentTeb()->signal_stack
))
1131 ss
.ss_size
= SIGNAL_STACK_SIZE
;
1133 if (!sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1135 /* sigaltstack may fail because the kernel is too old, or
1136 because glibc is brain-dead. In the latter case a
1137 direct system call should succeed. */
1138 else if (!wine_sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1141 #endif /* HAVE_SIGALTSTACK */
1143 sigfillset( &all_sigs
);
1145 if (set_handler( SIGINT
, have_sigaltstack
, (void (*)())int_handler
) == -1) goto error
;
1146 if (set_handler( SIGFPE
, have_sigaltstack
, (void (*)())fpe_handler
) == -1) goto error
;
1147 if (set_handler( SIGSEGV
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1148 if (set_handler( SIGILL
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1149 if (set_handler( SIGABRT
, have_sigaltstack
, (void (*)())abrt_handler
) == -1) goto error
;
1151 if (set_handler( SIGBUS
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1154 if (set_handler( SIGTRAP
, have_sigaltstack
, (void (*)())trap_handler
) == -1) goto error
;
1158 if (set_handler( SIGALRM
, have_sigaltstack
, (void (*)())alrm_handler
) == -1) goto error
;
1159 if (set_handler( SIGUSR2
, have_sigaltstack
, (void (*)())usr2_handler
) == -1) goto error
;
1165 perror("sigaction");
1170 /**********************************************************************
1173 void SIGNAL_Reset(void)
1177 /* block the async signals */
1178 sigemptyset( &block_set
);
1179 sigaddset( &block_set
, SIGALRM
);
1180 sigaddset( &block_set
, SIGIO
);
1181 sigaddset( &block_set
, SIGHUP
);
1182 sigaddset( &block_set
, SIGUSR2
);
1183 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
1185 /* restore default handlers */
1186 signal( SIGINT
, SIG_DFL
);
1187 signal( SIGFPE
, SIG_DFL
);
1188 signal( SIGSEGV
, SIG_DFL
);
1189 signal( SIGILL
, SIG_DFL
);
1191 signal( SIGBUS
, SIG_DFL
);
1194 signal( SIGTRAP
, SIG_DFL
);
1200 /**********************************************************************
1201 * __wine_enter_vm86 (NTDLL.@)
1203 * Enter vm86 mode with the specified register context.
1205 void __wine_enter_vm86( CONTEXT
*context
)
1207 EXCEPTION_RECORD rec
;
1208 TEB
*teb
= NtCurrentTeb();
1210 struct vm86plus_struct vm86
;
1212 memset( &vm86
, 0, sizeof(vm86
) );
1215 restore_vm86_context( context
, &vm86
);
1216 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1217 * so save it on entry, just in case */
1218 teb
->vm86_pending
|= (context
->EFlags
& VIP_MASK
);
1219 /* Work around race conditions with signal handler
1220 * (avoiding sigprocmask for performance reasons) */
1221 teb
->vm86_ptr
= &vm86
;
1222 vm86
.regs
.eflags
|= teb
->vm86_pending
;
1223 /* Check for VIF|VIP here, since vm86_enter doesn't */
1224 if ((vm86
.regs
.eflags
& (VIF_MASK
|VIP_MASK
)) == (VIF_MASK
|VIP_MASK
)) {
1225 teb
->vm86_ptr
= NULL
;
1226 teb
->vm86_pending
= 0;
1227 context
->EFlags
|= VIP_MASK
;
1228 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1229 rec
.ExceptionInformation
[0] = 0;
1235 res
= vm86_enter( &teb
->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1241 } while (VM86_TYPE(res
) == VM86_SIGNAL
);
1243 save_vm86_context( context
, &vm86
);
1244 context
->EFlags
|= teb
->vm86_pending
;
1246 switch(VM86_TYPE(res
))
1248 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1249 do_segv( context
, T_PROTFLT
, 0, 0 );
1251 case VM86_TRAP
: /* return due to DOS-debugger request */
1252 do_trap( context
, VM86_ARG(res
) );
1254 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1255 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1257 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1258 teb
->vm86_pending
= 0;
1259 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1261 case VM86_PICRETURN
: /* return due to pending PIC request */
1262 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1265 ERR( "unhandled result from vm86 mode %x\n", res
);
1268 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1270 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1271 rec
.ExceptionRecord
= NULL
;
1272 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1273 rec
.NumberParameters
= 1;
1274 EXC_RtlRaiseException( &rec
, context
);
1278 #else /* __HAVE_VM86 */
1279 /**********************************************************************
1280 * __wine_enter_vm86 (NTDLL.@)
1282 void __wine_enter_vm86( CONTEXT
*context
)
1284 MESSAGE("vm86 mode not supported on this platform\n");
1286 #endif /* __HAVE_VM86 */
1288 /**********************************************************************
1289 * DbgBreakPoint (NTDLL.@)
1291 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret");
1293 /**********************************************************************
1294 * DbgUserBreakPoint (NTDLL.@)
1296 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret");
1298 #endif /* __i386__ */