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 */
153 ".globl " __ASM_NAME("vm86_return") "\n\t"
154 __ASM_FUNC("vm86_return") "\n"
155 __ASM_NAME("vm86_return") ":\n\t"
161 "testl %eax,%eax\n\t"
163 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
164 "je " __ASM_NAME("vm86_enter") "\n\t"
166 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
168 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
169 __ASM_FUNC("vm86_return_end") "\n"
170 __ASM_NAME("vm86_return_end") ":\n\t"
173 #ifdef HAVE_SYS_VM86_H
181 #define EAX_sig(context) ((context)->tf_eax)
182 #define EBX_sig(context) ((context)->tf_ebx)
183 #define ECX_sig(context) ((context)->tf_ecx)
184 #define EDX_sig(context) ((context)->tf_edx)
185 #define ESI_sig(context) ((context)->tf_esi)
186 #define EDI_sig(context) ((context)->tf_edi)
187 #define EBP_sig(context) ((context)->tf_ebp)
189 #define CS_sig(context) ((context)->tf_cs)
190 #define DS_sig(context) ((context)->tf_ds)
191 #define ES_sig(context) ((context)->tf_es)
192 #define SS_sig(context) ((context)->tf_ss)
194 #include <machine/frame.h>
195 typedef struct trapframe SIGCONTEXT
;
197 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
198 #define HANDLER_CONTEXT __context
200 #define EFL_sig(context) ((context)->tf_eflags)
202 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
203 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
207 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
209 typedef struct sigcontext SIGCONTEXT
;
211 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
212 #define HANDLER_CONTEXT __context
216 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
219 #include <sys/regset.h>
223 #include <sys/ucontext.h>
225 typedef struct ucontext SIGCONTEXT
;
227 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
228 #define HANDLER_CONTEXT __context
230 #endif /* svr4 || SCO_DS */
236 unsigned long ContextFlags
;
237 FLOATING_SAVE_AREA sc_float
;
242 unsigned long sc_edi
;
243 unsigned long sc_esi
;
244 unsigned long sc_eax
;
245 unsigned long sc_ebx
;
246 unsigned long sc_ecx
;
247 unsigned long sc_edx
;
248 unsigned long sc_ebp
;
249 unsigned long sc_eip
;
251 unsigned long sc_eflags
;
252 unsigned long sc_esp
;
260 /* FIXME: This section is just here so it can compile, it's most likely
261 * completely wrong. */
265 unsigned short sc_gs
, __gsh
;
266 unsigned short sc_fs
, __fsh
;
267 unsigned short sc_es
, __esh
;
268 unsigned short sc_ds
, __dsh
;
269 unsigned long sc_edi
;
270 unsigned long sc_esi
;
271 unsigned long sc_ebp
;
272 unsigned long sc_esp
;
273 unsigned long sc_ebx
;
274 unsigned long sc_edx
;
275 unsigned long sc_ecx
;
276 unsigned long sc_eax
;
277 unsigned long sc_trapno
;
278 unsigned long sc_err
;
279 unsigned long sc_eip
;
280 unsigned short sc_cs
, __csh
;
281 unsigned long sc_eflags
;
282 unsigned long esp_at_signal
;
283 unsigned short sc_ss
, __ssh
;
285 unsigned long oldmask
;
289 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
290 #define HANDLER_CONTEXT (&__context)
292 #endif /* __CYGWIN__ */
294 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
295 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
297 #define EAX_sig(context) ((context)->sc_eax)
298 #define EBX_sig(context) ((context)->sc_ebx)
299 #define ECX_sig(context) ((context)->sc_ecx)
300 #define EDX_sig(context) ((context)->sc_edx)
301 #define ESI_sig(context) ((context)->sc_esi)
302 #define EDI_sig(context) ((context)->sc_edi)
303 #define EBP_sig(context) ((context)->sc_ebp)
305 #define CS_sig(context) ((context)->sc_cs)
306 #define DS_sig(context) ((context)->sc_ds)
307 #define ES_sig(context) ((context)->sc_es)
308 #define SS_sig(context) ((context)->sc_ss)
310 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
311 * saved by the exception handling. duh.
312 * Actually they are in -current (have been for a while), and that
313 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
314 * If you're running a system from the -stable branch older than that,
315 * like a 3.3-RELEASE, grab the patch from the ports tree:
316 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
317 * (If its not yet there when you look, go here:
318 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
321 #define FS_sig(context) ((context)->sc_fs)
322 #define GS_sig(context) ((context)->sc_gs)
326 #define FS_sig(context) ((context)->sc_fs)
327 #define GS_sig(context) ((context)->sc_gs)
328 #define CR2_sig(context) ((context)->cr2)
329 #define TRAP_sig(context) ((context)->sc_trapno)
330 #define ERROR_sig(context) ((context)->sc_err)
331 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
335 #define EFL_sig(context) ((context)->sc_eflags)
337 #define EFL_sig(context) ((context)->sc_efl)
338 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
339 #define CR2_sig(context) ((context)->sc_err)
340 #define TRAP_sig(context) ((context)->sc_trapno)
343 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
344 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
346 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
348 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
354 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
355 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
356 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
357 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
358 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
359 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
360 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
362 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
363 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
364 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
365 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
367 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
368 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
370 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
372 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
374 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
376 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
379 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
382 #endif /* svr4 || SCO_DS */
385 /* exception code definitions (already defined by FreeBSD/NetBSD) */
386 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
387 #define T_DIVIDE 0 /* Division by zero exception */
388 #define T_TRCTRAP 1 /* Single-step exception */
389 #define T_NMI 2 /* NMI interrupt */
390 #define T_BPTFLT 3 /* Breakpoint exception */
391 #define T_OFLOW 4 /* Overflow exception */
392 #define T_BOUND 5 /* Bound range exception */
393 #define T_PRIVINFLT 6 /* Invalid opcode exception */
394 #define T_DNA 7 /* Device not available exception */
395 #define T_DOUBLEFLT 8 /* Double fault exception */
396 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
397 #define T_TSSFLT 10 /* Invalid TSS exception */
398 #define T_SEGNPFLT 11 /* Segment not present exception */
399 #define T_STKFLT 12 /* Stack fault */
400 #define T_PROTFLT 13 /* General protection fault */
401 #define T_PAGEFLT 14 /* Page fault */
402 #define T_RESERVED 15 /* Unknown exception */
403 #define T_ARITHTRAP 16 /* Floating point exception */
404 #define T_ALIGNFLT 17 /* Alignment check exception */
405 #define T_MCHK 18 /* Machine check exception */
406 #define T_CACHEFLT 19 /* Cache flush exception */
408 #if defined(__NetBSD__)
409 #define T_MCHK 19 /* Machine check exception */
412 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
414 #include "wine/exception.h"
415 #include "stackframe.h"
418 #include "syslevel.h"
419 #include "wine/debug.h"
421 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
423 typedef int (*wine_signal_handler
)(unsigned int sig
);
425 static wine_signal_handler handlers
[256];
427 extern void WINAPI
EXC_RtlRaiseException( PEXCEPTION_RECORD
, PCONTEXT
);
429 /***********************************************************************
432 inline static int dispatch_signal(unsigned int sig
)
434 if (handlers
[sig
] == NULL
) return 0;
435 return handlers
[sig
](sig
);
439 /***********************************************************************
442 * Get the trap code for a signal.
444 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
447 return TRAP_sig(sigcontext
);
449 return T_UNKNOWN
; /* unknown trap code */
453 /***********************************************************************
456 * Get the error code for a signal.
458 static inline int get_error_code( const SIGCONTEXT
*sigcontext
)
461 return ERROR_sig(sigcontext
);
467 /***********************************************************************
470 * Get the CR2 value for a signal.
472 static inline void *get_cr2_value( const SIGCONTEXT
*sigcontext
)
475 return (void *)CR2_sig(sigcontext
);
483 /***********************************************************************
486 * Set the register values from a vm86 structure.
488 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
490 context
->Eax
= vm86
->regs
.eax
;
491 context
->Ebx
= vm86
->regs
.ebx
;
492 context
->Ecx
= vm86
->regs
.ecx
;
493 context
->Edx
= vm86
->regs
.edx
;
494 context
->Esi
= vm86
->regs
.esi
;
495 context
->Edi
= vm86
->regs
.edi
;
496 context
->Esp
= vm86
->regs
.esp
;
497 context
->Ebp
= vm86
->regs
.ebp
;
498 context
->Eip
= vm86
->regs
.eip
;
499 context
->SegCs
= vm86
->regs
.cs
;
500 context
->SegDs
= vm86
->regs
.ds
;
501 context
->SegEs
= vm86
->regs
.es
;
502 context
->SegFs
= vm86
->regs
.fs
;
503 context
->SegGs
= vm86
->regs
.gs
;
504 context
->SegSs
= vm86
->regs
.ss
;
505 context
->EFlags
= vm86
->regs
.eflags
;
509 /***********************************************************************
510 * restore_vm86_context
512 * Build a vm86 structure from the register values.
514 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
516 vm86
->regs
.eax
= context
->Eax
;
517 vm86
->regs
.ebx
= context
->Ebx
;
518 vm86
->regs
.ecx
= context
->Ecx
;
519 vm86
->regs
.edx
= context
->Edx
;
520 vm86
->regs
.esi
= context
->Esi
;
521 vm86
->regs
.edi
= context
->Edi
;
522 vm86
->regs
.esp
= context
->Esp
;
523 vm86
->regs
.ebp
= context
->Ebp
;
524 vm86
->regs
.eip
= context
->Eip
;
525 vm86
->regs
.cs
= context
->SegCs
;
526 vm86
->regs
.ds
= context
->SegDs
;
527 vm86
->regs
.es
= context
->SegEs
;
528 vm86
->regs
.fs
= context
->SegFs
;
529 vm86
->regs
.gs
= context
->SegGs
;
530 vm86
->regs
.ss
= context
->SegSs
;
531 vm86
->regs
.eflags
= context
->EFlags
;
533 #endif /* __HAVE_VM86 */
536 /***********************************************************************
539 * Set the register values from a sigcontext.
541 static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
543 /* get %fs and %gs at time of the fault */
545 context
->SegFs
= LOWORD(FS_sig(sigcontext
));
547 context
->SegFs
= wine_get_fs();
550 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
552 context
->SegGs
= wine_get_gs();
555 /* now restore a proper %fs for the fault handler */
556 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext
)) ||
557 !IS_SELECTOR_SYSTEM(SS_sig(sigcontext
))) /* 16-bit mode */
560 * Win16 or DOS protected mode. Note that during switch
561 * from 16-bit mode to linear mode, CS may be set to system
562 * segment before FS is restored. Fortunately, in this case
563 * SS is still non-system segment. This is why both CS and SS
566 wine_set_fs( SYSLEVEL_Win16CurrentTeb
);
567 wine_set_gs( NtCurrentTeb()->gs_sel
);
570 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
572 unsigned int *stack
= (unsigned int *)ESP_sig(sigcontext
);
574 /* fetch the saved %fs on the stack */
575 wine_set_fs( stack
[0] );
576 wine_set_gs( stack
[1] );
577 if (EAX_sig(sigcontext
) == VM86_EAX
) {
578 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
579 * (but we could also get if from teb->vm86_ptr) */
580 struct vm86plus_struct
*vm86
= (struct vm86plus_struct
*)stack
[2];
581 /* get context from vm86 struct */
582 save_vm86_context( context
, vm86
);
586 #endif /* __HAVE_VM86 */
587 else /* 32-bit mode */
590 wine_set_fs( FS_sig(sigcontext
) );
593 wine_set_gs( GS_sig(sigcontext
) );
597 context
->Eax
= EAX_sig(sigcontext
);
598 context
->Ebx
= EBX_sig(sigcontext
);
599 context
->Ecx
= ECX_sig(sigcontext
);
600 context
->Edx
= EDX_sig(sigcontext
);
601 context
->Esi
= ESI_sig(sigcontext
);
602 context
->Edi
= EDI_sig(sigcontext
);
603 context
->Ebp
= EBP_sig(sigcontext
);
604 context
->EFlags
= EFL_sig(sigcontext
);
605 context
->Eip
= EIP_sig(sigcontext
);
606 context
->Esp
= ESP_sig(sigcontext
);
607 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
608 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
609 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
610 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
614 /***********************************************************************
617 * Build a sigcontext from the register values.
619 static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
622 /* check if exception occurred in vm86 mode */
623 if ((void *)EIP_sig(sigcontext
) == vm86_return
&&
624 IS_SELECTOR_SYSTEM(CS_sig(sigcontext
)) &&
625 EAX_sig(sigcontext
) == VM86_EAX
)
627 unsigned int *stack
= (unsigned int *)ESP_sig(sigcontext
);
628 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
629 * (but we could also get it from teb->vm86_ptr) */
630 struct vm86plus_struct
*vm86
= (struct vm86plus_struct
*)stack
[2];
631 restore_vm86_context( context
, vm86
);
634 #endif /* __HAVE_VM86 */
636 EAX_sig(sigcontext
) = context
->Eax
;
637 EBX_sig(sigcontext
) = context
->Ebx
;
638 ECX_sig(sigcontext
) = context
->Ecx
;
639 EDX_sig(sigcontext
) = context
->Edx
;
640 ESI_sig(sigcontext
) = context
->Esi
;
641 EDI_sig(sigcontext
) = context
->Edi
;
642 EBP_sig(sigcontext
) = context
->Ebp
;
643 EFL_sig(sigcontext
) = context
->EFlags
;
644 EIP_sig(sigcontext
) = context
->Eip
;
645 ESP_sig(sigcontext
) = context
->Esp
;
646 CS_sig(sigcontext
) = context
->SegCs
;
647 DS_sig(sigcontext
) = context
->SegDs
;
648 ES_sig(sigcontext
) = context
->SegEs
;
649 SS_sig(sigcontext
) = context
->SegSs
;
651 FS_sig(sigcontext
) = context
->SegFs
;
653 wine_set_fs( context
->SegFs
);
656 GS_sig(sigcontext
) = context
->SegGs
;
658 wine_set_gs( context
->SegGs
);
663 /***********************************************************************
666 * Handler initialization when the full context is not needed.
668 static void init_handler( const SIGCONTEXT
*sigcontext
)
670 /* restore a proper %fs for the fault handler */
671 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext
)) ||
672 !IS_SELECTOR_SYSTEM(SS_sig(sigcontext
))) /* 16-bit mode */
674 wine_set_fs( SYSLEVEL_Win16CurrentTeb
);
675 wine_set_gs( NtCurrentTeb()->gs_sel
);
678 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
680 /* fetch the saved %fs on the stack */
681 wine_set_fs( *(unsigned int *)ESP_sig(sigcontext
) );
682 wine_set_gs( NtCurrentTeb()->gs_sel
);
684 #endif /* __HAVE_VM86 */
685 else /* 32-bit mode, get %fs at time of the fault */
688 wine_set_fs( FS_sig(sigcontext
) );
691 wine_set_gs( GS_sig(sigcontext
) );
697 /***********************************************************************
700 * Set the FPU context from a sigcontext.
702 inline static void save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
705 if (FPU_sig(sigcontext
))
707 context
->FloatSave
= *FPU_sig(sigcontext
);
712 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
713 #endif /* __GNUC__ */
717 /***********************************************************************
720 * Restore the FPU context to a sigcontext.
722 inline static void restore_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
724 /* reset the current interrupt status */
725 context
->FloatSave
.StatusWord
&= context
->FloatSave
.ControlWord
| 0xffffff80;
727 if (FPU_sig(sigcontext
))
729 *FPU_sig(sigcontext
) = context
->FloatSave
;
734 /* avoid nested exceptions */
735 __asm__
__volatile__( "frstor %0; fwait" : : "m" (context
->FloatSave
) );
736 #endif /* __GNUC__ */
740 /**********************************************************************
743 * Get the FPU exception code from the FPU status.
745 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
747 DWORD status
= context
->FloatSave
.StatusWord
;
749 if (status
& 0x01) /* IE */
751 if (status
& 0x40) /* SF */
752 return EXCEPTION_FLT_STACK_CHECK
;
754 return EXCEPTION_FLT_INVALID_OPERATION
;
756 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
757 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
758 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
759 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
760 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
761 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
765 /**********************************************************************
768 * Implementation of SIGSEGV handler.
770 static void do_segv( CONTEXT
*context
, int trap_code
, void *cr2
, int err_code
)
772 EXCEPTION_RECORD rec
;
773 DWORD page_fault_code
= EXCEPTION_ACCESS_VIOLATION
;
776 /* we want the page-fault case to be fast */
777 if (trap_code
== T_PAGEFLT
)
778 if (!(page_fault_code
= VIRTUAL_HandleFault( cr2
))) return;
781 rec
.ExceptionRecord
= NULL
;
782 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
783 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
784 rec
.NumberParameters
= 0;
788 case T_OFLOW
: /* Overflow exception */
789 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
791 case T_BOUND
: /* Bound range exception */
792 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
794 case T_PRIVINFLT
: /* Invalid opcode exception */
795 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
797 case T_STKFLT
: /* Stack fault */
798 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
800 case T_SEGNPFLT
: /* Segment not present exception */
801 case T_PROTFLT
: /* General protection fault */
802 case T_UNKNOWN
: /* Unknown fault code */
803 if (INSTR_EmulateInstruction( context
)) return;
804 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
806 case T_PAGEFLT
: /* Page fault */
808 rec
.NumberParameters
= 2;
809 rec
.ExceptionInformation
[0] = (err_code
& 2) != 0;
810 rec
.ExceptionInformation
[1] = (DWORD
)cr2
;
812 rec
.ExceptionCode
= page_fault_code
;
814 case T_ALIGNFLT
: /* Alignment check exception */
815 /* FIXME: pass through exception handler first? */
816 if (context
->EFlags
& 0x00040000)
818 /* Disable AC flag, return */
819 context
->EFlags
&= ~0x00040000;
822 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
825 ERR( "Got unexpected trap %d\n", trap_code
);
827 case T_NMI
: /* NMI interrupt */
828 case T_DNA
: /* Device not available exception */
829 case T_DOUBLEFLT
: /* Double fault exception */
830 case T_TSSFLT
: /* Invalid TSS exception */
831 case T_RESERVED
: /* Unknown exception */
832 case T_MCHK
: /* Machine check exception */
834 case T_CACHEFLT
: /* Cache flush exception */
836 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
839 EXC_RtlRaiseException( &rec
, context
);
843 /**********************************************************************
846 * Implementation of SIGTRAP handler.
848 static void do_trap( CONTEXT
*context
, int trap_code
)
850 EXCEPTION_RECORD rec
;
851 DWORD dr0
, dr1
, dr2
, dr3
, dr6
, dr7
;
853 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
854 rec
.ExceptionRecord
= NULL
;
855 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
856 rec
.NumberParameters
= 0;
860 case T_TRCTRAP
: /* Single-step exception */
861 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
862 if (context
->EFlags
& 0x100)
864 context
->EFlags
&= ~0x100; /* clear single-step flag */
866 else /* hardware breakpoint, fetch the debug registers */
868 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
869 GetThreadContext(GetCurrentThread(), context
);
872 case T_BPTFLT
: /* Breakpoint exception */
873 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
876 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
886 EXC_RtlRaiseException( &rec
, context
);
888 if (dr0
!= context
->Dr0
|| dr1
!= context
->Dr1
|| dr2
!= context
->Dr2
||
889 dr3
!= context
->Dr3
|| dr6
!= context
->Dr6
|| dr7
!= context
->Dr7
)
891 /* the debug registers have changed, set the new values */
892 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
893 SetThreadContext(GetCurrentThread(), context
);
898 /**********************************************************************
901 * Implementation of SIGFPE handler
903 static void do_fpe( CONTEXT
*context
, int trap_code
)
905 EXCEPTION_RECORD rec
;
909 case T_DIVIDE
: /* Division by zero exception */
910 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
912 case T_FPOPFLT
: /* Coprocessor segment overrun */
913 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
915 case T_ARITHTRAP
: /* Floating point exception */
916 case T_UNKNOWN
: /* Unknown fault code */
917 rec
.ExceptionCode
= get_fpu_code( context
);
920 ERR( "Got unexpected trap %d\n", trap_code
);
921 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
924 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
925 rec
.ExceptionRecord
= NULL
;
926 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
927 rec
.NumberParameters
= 0;
928 EXC_RtlRaiseException( &rec
, context
);
933 /**********************************************************************
936 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
938 static void set_vm86_pend( CONTEXT
*context
)
940 EXCEPTION_RECORD rec
;
941 TEB
*teb
= NtCurrentTeb();
942 struct vm86plus_struct
*vm86
= (struct vm86plus_struct
*)(teb
->vm86_ptr
);
944 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
945 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
946 rec
.ExceptionRecord
= NULL
;
947 rec
.NumberParameters
= 1;
948 rec
.ExceptionInformation
[0] = 0;
950 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
951 teb
->vm86_pending
|= VIP_MASK
;
952 /* see if we were in VM86 mode */
953 if (context
->EFlags
& 0x00020000)
955 /* seems so, also set flag in signal context */
956 if (context
->EFlags
& VIP_MASK
) return;
957 context
->EFlags
|= VIP_MASK
;
958 vm86
->regs
.eflags
|= VIP_MASK
; /* no exception recursion */
959 if (context
->EFlags
& VIF_MASK
) {
960 /* VIF is set, throw exception */
961 teb
->vm86_pending
= 0;
962 teb
->vm86_ptr
= NULL
;
963 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
964 EXC_RtlRaiseException( &rec
, context
);
965 teb
->vm86_ptr
= vm86
;
970 /* not in VM86, but possibly setting up for it */
971 if (vm86
->regs
.eflags
& VIP_MASK
) return;
972 vm86
->regs
.eflags
|= VIP_MASK
;
973 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
974 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
975 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
976 /* exiting from VM86, can't throw */
979 if (vm86
->regs
.eflags
& VIF_MASK
) {
980 /* VIF is set, throw exception */
982 teb
->vm86_pending
= 0;
983 teb
->vm86_ptr
= NULL
;
984 save_vm86_context( &vcontext
, vm86
);
985 rec
.ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
986 EXC_RtlRaiseException( &rec
, &vcontext
);
987 teb
->vm86_ptr
= vm86
;
988 restore_vm86_context( &vcontext
, vm86
);
994 /**********************************************************************
997 * Handler for SIGUSR2.
998 * We use it to signal that the running __wine_enter_vm86() should
999 * immediately set VIP_MASK, causing pending events to be handled
1000 * as early as possible.
1002 static HANDLER_DEF(usr2_handler
)
1006 save_context( &context
, HANDLER_CONTEXT
);
1007 set_vm86_pend( &context
);
1008 restore_context( &context
, HANDLER_CONTEXT
);
1012 /**********************************************************************
1015 * Handler for SIGALRM.
1016 * Increases the alarm counter and sets the vm86 pending flag.
1018 static HANDLER_DEF(alrm_handler
)
1022 save_context( &context
, HANDLER_CONTEXT
);
1023 NtCurrentTeb()->alarms
++;
1024 set_vm86_pend( &context
);
1025 restore_context( &context
, HANDLER_CONTEXT
);
1027 #endif /* __HAVE_VM86 */
1030 /**********************************************************************
1033 * Handler for SIGSEGV and related errors.
1035 static HANDLER_DEF(segv_handler
)
1038 save_context( &context
, HANDLER_CONTEXT
);
1039 do_segv( &context
, get_trap_code(HANDLER_CONTEXT
),
1040 get_cr2_value(HANDLER_CONTEXT
), get_error_code(HANDLER_CONTEXT
) );
1041 restore_context( &context
, HANDLER_CONTEXT
);
1045 /**********************************************************************
1048 * Handler for SIGTRAP.
1050 static HANDLER_DEF(trap_handler
)
1053 save_context( &context
, HANDLER_CONTEXT
);
1054 do_trap( &context
, get_trap_code(HANDLER_CONTEXT
) );
1055 restore_context( &context
, HANDLER_CONTEXT
);
1059 /**********************************************************************
1062 * Handler for SIGFPE.
1064 static HANDLER_DEF(fpe_handler
)
1067 save_fpu( &context
, HANDLER_CONTEXT
);
1068 save_context( &context
, HANDLER_CONTEXT
);
1069 do_fpe( &context
, get_trap_code(HANDLER_CONTEXT
) );
1070 restore_context( &context
, HANDLER_CONTEXT
);
1071 restore_fpu( &context
, HANDLER_CONTEXT
);
1075 /**********************************************************************
1078 * Handler for SIGINT.
1080 static HANDLER_DEF(int_handler
)
1082 init_handler( HANDLER_CONTEXT
);
1083 if (!dispatch_signal(SIGINT
))
1085 EXCEPTION_RECORD rec
;
1088 save_context( &context
, HANDLER_CONTEXT
);
1089 rec
.ExceptionCode
= CONTROL_C_EXIT
;
1090 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1091 rec
.ExceptionRecord
= NULL
;
1092 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
1093 rec
.NumberParameters
= 0;
1094 EXC_RtlRaiseException( &rec
, &context
);
1095 restore_context( &context
, HANDLER_CONTEXT
);
1099 /**********************************************************************
1102 * Handler for SIGABRT.
1104 static HANDLER_DEF(abrt_handler
)
1106 EXCEPTION_RECORD rec
;
1109 save_context( &context
, HANDLER_CONTEXT
);
1110 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
1111 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
1112 rec
.ExceptionRecord
= NULL
;
1113 rec
.ExceptionAddress
= (LPVOID
)context
.Eip
;
1114 rec
.NumberParameters
= 0;
1115 EXC_RtlRaiseException( &rec
, &context
); /* Should never return.. */
1116 restore_context( &context
, HANDLER_CONTEXT
);
1120 /**********************************************************************
1123 * Handler for SIGTERM.
1125 static HANDLER_DEF(term_handler
)
1127 init_handler( HANDLER_CONTEXT
);
1128 SYSDEPS_AbortThread(0);
1132 /**********************************************************************
1135 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1137 static HANDLER_DEF(usr1_handler
)
1139 init_handler( HANDLER_CONTEXT
);
1140 /* wait with 0 timeout, will only return once the thread is no longer suspended */
1141 WaitForMultipleObjectsEx( 0, NULL
, FALSE
, 0, FALSE
);
1145 /***********************************************************************
1148 * Set a signal handler
1150 static int set_handler( int sig
, int have_sigaltstack
, void (*func
)() )
1152 struct sigaction sig_act
;
1155 if (!have_sigaltstack
&& NtCurrentTeb()->signal_stack
)
1157 struct kernel_sigaction sig_act
;
1158 sig_act
.ksa_handler
= func
;
1159 sig_act
.ksa_flags
= SA_RESTART
;
1160 sig_act
.ksa_mask
= (1 << (SIGINT
-1)) |
1161 (1 << (SIGUSR2
-1)) |
1163 /* point to the top of the stack */
1164 sig_act
.ksa_restorer
= (char *)NtCurrentTeb()->signal_stack
+ SIGNAL_STACK_SIZE
;
1165 return wine_sigaction( sig
, &sig_act
, NULL
);
1168 sig_act
.sa_handler
= func
;
1169 sigemptyset( &sig_act
.sa_mask
);
1170 sigaddset( &sig_act
.sa_mask
, SIGINT
);
1171 sigaddset( &sig_act
.sa_mask
, SIGUSR2
);
1172 sigaddset( &sig_act
.sa_mask
, SIGALRM
);
1175 sig_act
.sa_flags
= SA_RESTART
;
1176 #elif defined (__svr4__) || defined(_SCO_DS)
1177 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1179 sig_act
.sa_flags
= 0;
1183 if (have_sigaltstack
) sig_act
.sa_flags
|= SA_ONSTACK
;
1185 return sigaction( sig
, &sig_act
, NULL
);
1189 /***********************************************************************
1190 * __wine_set_signal_handler (NTDLL.@)
1192 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1194 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1195 if (handlers
[sig
] != NULL
) return -2;
1196 handlers
[sig
] = wsh
;
1201 /**********************************************************************
1204 BOOL
SIGNAL_Init(void)
1206 int have_sigaltstack
= 0;
1208 #ifdef HAVE_SIGALTSTACK
1209 struct sigaltstack ss
;
1210 if ((ss
.ss_sp
= NtCurrentTeb()->signal_stack
))
1212 ss
.ss_size
= SIGNAL_STACK_SIZE
;
1214 if (!sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1216 /* sigaltstack may fail because the kernel is too old, or
1217 because glibc is brain-dead. In the latter case a
1218 direct system call should succeed. */
1219 else if (!wine_sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1222 #endif /* HAVE_SIGALTSTACK */
1224 if (set_handler( SIGINT
, have_sigaltstack
, (void (*)())int_handler
) == -1) goto error
;
1225 if (set_handler( SIGFPE
, have_sigaltstack
, (void (*)())fpe_handler
) == -1) goto error
;
1226 if (set_handler( SIGSEGV
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1227 if (set_handler( SIGILL
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1228 if (set_handler( SIGABRT
, have_sigaltstack
, (void (*)())abrt_handler
) == -1) goto error
;
1229 if (set_handler( SIGTERM
, have_sigaltstack
, (void (*)())term_handler
) == -1) goto error
;
1230 if (set_handler( SIGUSR1
, have_sigaltstack
, (void (*)())usr1_handler
) == -1) goto error
;
1232 if (set_handler( SIGBUS
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1235 if (set_handler( SIGTRAP
, have_sigaltstack
, (void (*)())trap_handler
) == -1) goto error
;
1239 if (set_handler( SIGALRM
, have_sigaltstack
, (void (*)())alrm_handler
) == -1) goto error
;
1240 if (set_handler( SIGUSR2
, have_sigaltstack
, (void (*)())usr2_handler
) == -1) goto error
;
1246 perror("sigaction");
1251 /**********************************************************************
1254 * Block the async signals.
1256 void SIGNAL_Block(void)
1260 sigemptyset( &block_set
);
1261 sigaddset( &block_set
, SIGALRM
);
1262 sigaddset( &block_set
, SIGIO
);
1263 sigaddset( &block_set
, SIGHUP
);
1264 sigaddset( &block_set
, SIGUSR1
);
1265 sigaddset( &block_set
, SIGUSR2
);
1266 sigprocmask( SIG_BLOCK
, &block_set
, NULL
);
1270 /***********************************************************************
1273 * Unblock signals. Called from EXC_RtlRaiseException.
1275 void SIGNAL_Unblock(void)
1279 sigfillset( &all_sigs
);
1280 sigprocmask( SIG_UNBLOCK
, &all_sigs
, NULL
);
1284 /**********************************************************************
1287 * Restore the default handlers.
1289 void SIGNAL_Reset(void)
1291 signal( SIGINT
, SIG_DFL
);
1292 signal( SIGFPE
, SIG_DFL
);
1293 signal( SIGSEGV
, SIG_DFL
);
1294 signal( SIGILL
, SIG_DFL
);
1295 signal( SIGABRT
, SIG_DFL
);
1296 signal( SIGTERM
, SIG_DFL
);
1298 signal( SIGBUS
, SIG_DFL
);
1301 signal( SIGTRAP
, SIG_DFL
);
1307 /**********************************************************************
1308 * __wine_enter_vm86 (NTDLL.@)
1310 * Enter vm86 mode with the specified register context.
1312 void __wine_enter_vm86( CONTEXT
*context
)
1314 EXCEPTION_RECORD rec
;
1315 TEB
*teb
= NtCurrentTeb();
1317 struct vm86plus_struct vm86
;
1319 memset( &vm86
, 0, sizeof(vm86
) );
1322 restore_vm86_context( context
, &vm86
);
1323 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1324 * so save it on entry, just in case */
1325 teb
->vm86_pending
|= (context
->EFlags
& VIP_MASK
);
1326 /* Work around race conditions with signal handler
1327 * (avoiding sigprocmask for performance reasons) */
1328 teb
->vm86_ptr
= &vm86
;
1329 vm86
.regs
.eflags
|= teb
->vm86_pending
;
1330 /* Check for VIF|VIP here, since vm86_enter doesn't */
1331 if ((vm86
.regs
.eflags
& (VIF_MASK
|VIP_MASK
)) == (VIF_MASK
|VIP_MASK
)) {
1332 teb
->vm86_ptr
= NULL
;
1333 teb
->vm86_pending
= 0;
1334 context
->EFlags
|= VIP_MASK
;
1335 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1336 rec
.ExceptionInformation
[0] = 0;
1342 res
= vm86_enter( &teb
->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1348 } while (VM86_TYPE(res
) == VM86_SIGNAL
);
1350 save_vm86_context( context
, &vm86
);
1351 context
->EFlags
|= teb
->vm86_pending
;
1353 switch(VM86_TYPE(res
))
1355 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1356 do_segv( context
, T_PROTFLT
, 0, 0 );
1358 case VM86_TRAP
: /* return due to DOS-debugger request */
1359 do_trap( context
, VM86_ARG(res
) );
1361 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1362 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1364 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1365 teb
->vm86_pending
= 0;
1366 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1368 case VM86_PICRETURN
: /* return due to pending PIC request */
1369 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1372 ERR( "unhandled result from vm86 mode %x\n", res
);
1375 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1377 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1378 rec
.ExceptionRecord
= NULL
;
1379 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1380 rec
.NumberParameters
= 1;
1381 EXC_RtlRaiseException( &rec
, context
);
1385 #else /* __HAVE_VM86 */
1386 /**********************************************************************
1387 * __wine_enter_vm86 (NTDLL.@)
1389 void __wine_enter_vm86( CONTEXT
*context
)
1391 MESSAGE("vm86 mode not supported on this platform\n");
1393 #endif /* __HAVE_VM86 */
1395 /**********************************************************************
1396 * DbgBreakPoint (NTDLL.@)
1398 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret");
1400 /**********************************************************************
1401 * DbgUserBreakPoint (NTDLL.@)
1403 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret");
1405 #endif /* __i386__ */