2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
17 #ifdef HAVE_SYS_PARAM_H
18 # include <sys/param.h>
23 # ifdef HAVE_SYS_SYSCALL_H
24 # include <sys/syscall.h>
28 #ifdef HAVE_SYS_VM86_H
29 # include <sys/vm86.h>
32 #ifdef HAVE_SYS_SIGNAL_H
33 # include <sys/signal.h>
39 #include "selectors.h"
41 /***********************************************************************
42 * signal context platform-specific definitions
48 unsigned short sc_gs, __gsh;
49 unsigned short sc_fs, __fsh;
50 unsigned short sc_es, __esh;
51 unsigned short sc_ds, __dsh;
60 unsigned long sc_trapno;
63 unsigned short sc_cs, __csh;
64 unsigned long sc_eflags;
65 unsigned long esp_at_signal;
66 unsigned short sc_ss, __ssh;
68 unsigned long oldmask;
72 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
73 #define HANDLER_CONTEXT (&__context)
75 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
76 struct kernel_sigaction
78 void (*ksa_handler)();
79 unsigned long ksa_mask;
80 unsigned long ksa_flags;
84 /* Similar to the sigaction function in libc, except it leaves alone the
85 restorer field, which is used to specify the signal stack address */
86 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
87 struct kernel_sigaction *old )
89 __asm__ __volatile__( "pushl %%ebx\n\t"
94 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
100 #ifdef HAVE_SIGALTSTACK
101 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
102 static inline int wine_sigaltstack( const struct sigaltstack *new,
103 struct sigaltstack *old )
106 __asm__ __volatile__( "pushl %%ebx\n\t"
111 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
112 if (ret >= 0) return 0;
118 int vm86_enter( struct vm86plus_struct *ptr );
120 __ASM_GLOBAL_FUNC(vm86_enter,
122 "movl %esp, %ebp\n\t"
123 "movl $166,%eax\n\t" /*SYS_vm86*/
124 "movl 8(%ebp),%ecx\n\t"
126 "movl $1,%ebx\n\t" /*VM86_ENTER*/
127 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
130 ".globl " __ASM_NAME("vm86_return") "\n\t"
131 ".type " __ASM_NAME("vm86_return") ",@function\n"
132 __ASM_NAME("vm86_return") ":\n\t"
145 #define EAX_sig(context) ((context)->tf_eax)
146 #define EBX_sig(context) ((context)->tf_ebx)
147 #define ECX_sig(context) ((context)->tf_ecx)
148 #define EDX_sig(context) ((context)->tf_edx)
149 #define ESI_sig(context) ((context)->tf_esi)
150 #define EDI_sig(context) ((context)->tf_edi)
151 #define EBP_sig(context) ((context)->tf_ebp)
153 #define CS_sig(context) ((context)->tf_cs)
154 #define DS_sig(context) ((context)->tf_ds)
155 #define ES_sig(context) ((context)->tf_es)
156 #define SS_sig(context) ((context)->tf_ss)
158 #include <machine/frame.h>
159 typedef struct trapframe SIGCONTEXT;
161 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
162 #define HANDLER_CONTEXT __context
164 #define EFL_sig(context) ((context)->tf_eflags)
166 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
167 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
171 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
173 typedef struct sigcontext SIGCONTEXT;
175 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
176 #define HANDLER_CONTEXT __context
180 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
183 #include <sys/regset.h>
187 #include <sys/ucontext.h>
189 typedef struct ucontext SIGCONTEXT;
191 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
192 #define HANDLER_CONTEXT __context
194 #endif /* svr4 || SCO_DS */
200 unsigned long ContextFlags;
201 FLOATING_SAVE_AREA sc_float;
206 unsigned long sc_edi;
207 unsigned long sc_esi;
208 unsigned long sc_eax;
209 unsigned long sc_ebx;
210 unsigned long sc_ecx;
211 unsigned long sc_edx;
212 unsigned long sc_ebp;
213 unsigned long sc_eip;
215 unsigned long sc_eflags;
216 unsigned long sc_esp;
223 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
225 #define EAX_sig(context) ((context)->sc_eax)
226 #define EBX_sig(context) ((context)->sc_ebx)
227 #define ECX_sig(context) ((context)->sc_ecx)
228 #define EDX_sig(context) ((context)->sc_edx)
229 #define ESI_sig(context) ((context)->sc_esi)
230 #define EDI_sig(context) ((context)->sc_edi)
231 #define EBP_sig(context) ((context)->sc_ebp)
233 #define CS_sig(context) ((context)->sc_cs)
234 #define DS_sig(context) ((context)->sc_ds)
235 #define ES_sig(context) ((context)->sc_es)
236 #define SS_sig(context) ((context)->sc_ss)
238 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
239 * saved by the exception handling. duh.
240 * Actually they are in -current (have been for a while), and that
241 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
242 * If you're running a system from the -stable branch older than that,
243 * like a 3.3-RELEASE, grab the patch from the ports tree:
244 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
245 * (If its not yet there when you look, go here:
246 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
249 #define FS_sig(context) ((context)->sc_fs)
250 #define GS_sig(context) ((context)->sc_gs)
254 #define FS_sig(context) ((context)->sc_fs)
255 #define GS_sig(context) ((context)->sc_gs)
256 #define CR2_sig(context) ((context)->cr2)
257 #define TRAP_sig(context) ((context)->sc_trapno)
258 #define ERROR_sig(context) ((context)->sc_err)
259 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
263 #define EFL_sig(context) ((context)->sc_eflags)
265 #define EFL_sig(context) ((context)->sc_efl)
266 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
267 #define CR2_sig(context) ((context)->sc_err)
268 #define TRAP_sig(context) ((context)->sc_trapno)
271 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
272 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
274 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
276 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
282 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
283 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
284 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
285 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
286 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
287 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
288 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
290 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
291 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
292 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
293 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
295 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
296 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
298 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
300 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
302 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
304 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
307 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
310 #endif /* svr4 || SCO_DS */
313 /* exception code definitions (already defined by FreeBSD/NetBSD) */
314 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
315 #define T_DIVIDE 0 /* Division by zero exception */
316 #define T_TRCTRAP 1 /* Single-step exception */
317 #define T_NMI 2 /* NMI interrupt */
318 #define T_BPTFLT 3 /* Breakpoint exception */
319 #define T_OFLOW 4 /* Overflow exception */
320 #define T_BOUND 5 /* Bound range exception */
321 #define T_PRIVINFLT 6 /* Invalid opcode exception */
322 #define T_DNA 7 /* Device not available exception */
323 #define T_DOUBLEFLT 8 /* Double fault exception */
324 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
325 #define T_TSSFLT 10 /* Invalid TSS exception */
326 #define T_SEGNPFLT 11 /* Segment not present exception */
327 #define T_STKFLT 12 /* Stack fault */
328 #define T_PROTFLT 13 /* General protection fault */
329 #define T_PAGEFLT 14 /* Page fault */
330 #define T_RESERVED 15 /* Unknown exception */
331 #define T_ARITHTRAP 16 /* Floating point exception */
332 #define T_ALIGNFLT 17 /* Alignment check exception */
333 #define T_MCHK 18 /* Machine check exception */
334 #define T_CACHEFLT 19 /* Cache flush exception */
336 #if defined(__NetBSD__)
337 #define T_MCHK 19 /* Machine check exception */
340 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
342 #include "wine/exception.h"
344 #include "stackframe.h"
348 #include "syslevel.h"
349 #include "debugtools.h"
351 DEFAULT_DEBUG_CHANNEL(seh);
353 static sigset_t all_sigs;
356 /***********************************************************************
359 * Get the trap code for a signal.
361 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
364 return TRAP_sig(sigcontext);
366 return T_UNKNOWN; /* unknown trap code */
370 /***********************************************************************
373 * Get the error code for a signal.
375 static inline int get_error_code( const SIGCONTEXT *sigcontext )
378 return ERROR_sig(sigcontext);
384 /***********************************************************************
387 * Get the CR2 value for a signal.
389 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
392 return (void *)CR2_sig(sigcontext);
400 /***********************************************************************
403 * Set the register values from a vm86 structure.
405 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
407 context->Eax = vm86->regs.eax;
408 context->Ebx = vm86->regs.ebx;
409 context->Ecx = vm86->regs.ecx;
410 context->Edx = vm86->regs.edx;
411 context->Esi = vm86->regs.esi;
412 context->Edi = vm86->regs.edi;
413 context->Esp = vm86->regs.esp;
414 context->Ebp = vm86->regs.ebp;
415 context->Eip = vm86->regs.eip;
416 context->SegCs = vm86->regs.cs;
417 context->SegDs = vm86->regs.ds;
418 context->SegEs = vm86->regs.es;
419 context->SegFs = vm86->regs.fs;
420 context->SegGs = vm86->regs.gs;
421 context->SegSs = vm86->regs.ss;
422 context->EFlags = vm86->regs.eflags;
426 /***********************************************************************
427 * restore_vm86_context
429 * Build a vm86 structure from the register values.
431 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
433 vm86->regs.eax = context->Eax;
434 vm86->regs.ebx = context->Ebx;
435 vm86->regs.ecx = context->Ecx;
436 vm86->regs.edx = context->Edx;
437 vm86->regs.esi = context->Esi;
438 vm86->regs.edi = context->Edi;
439 vm86->regs.esp = context->Esp;
440 vm86->regs.ebp = context->Ebp;
441 vm86->regs.eip = context->Eip;
442 vm86->regs.cs = context->SegCs;
443 vm86->regs.ds = context->SegDs;
444 vm86->regs.es = context->SegEs;
445 vm86->regs.fs = context->SegFs;
446 vm86->regs.gs = context->SegGs;
447 vm86->regs.ss = context->SegSs;
448 vm86->regs.eflags = context->EFlags;
450 #endif /* __HAVE_VM86 */
453 /***********************************************************************
456 * Set the register values from a sigcontext.
458 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
461 /* get %fs at time of the fault */
463 fs = FS_sig(sigcontext);
469 /* now restore a proper %fs for the fault handler */
470 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) /* 16-bit mode */
472 fs = SYSLEVEL_Win16CurrentTeb;
475 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
477 /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
478 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
479 /* fetch the saved %fs on the stack */
480 fs = *(unsigned int *)ESP_sig(sigcontext);
482 /* get context from vm86 struct */
483 save_vm86_context( context, vm86 );
486 #endif /* __HAVE_VM86 */
490 context->Eax = EAX_sig(sigcontext);
491 context->Ebx = EBX_sig(sigcontext);
492 context->Ecx = ECX_sig(sigcontext);
493 context->Edx = EDX_sig(sigcontext);
494 context->Esi = ESI_sig(sigcontext);
495 context->Edi = EDI_sig(sigcontext);
496 context->Ebp = EBP_sig(sigcontext);
497 context->EFlags = EFL_sig(sigcontext);
498 context->Eip = EIP_sig(sigcontext);
499 context->Esp = ESP_sig(sigcontext);
500 context->SegCs = LOWORD(CS_sig(sigcontext));
501 context->SegDs = LOWORD(DS_sig(sigcontext));
502 context->SegEs = LOWORD(ES_sig(sigcontext));
503 context->SegSs = LOWORD(SS_sig(sigcontext));
505 context->SegGs = LOWORD(GS_sig(sigcontext));
507 context->SegGs = __get_gs();
512 /***********************************************************************
515 * Build a sigcontext from the register values.
517 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
520 /* check if exception occurred in vm86 mode */
521 if ((void *)EIP_sig(sigcontext) == vm86_return &&
522 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)))
524 /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
525 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
526 restore_vm86_context( context, vm86 );
529 #endif /* __HAVE_VM86 */
531 EAX_sig(sigcontext) = context->Eax;
532 EBX_sig(sigcontext) = context->Ebx;
533 ECX_sig(sigcontext) = context->Ecx;
534 EDX_sig(sigcontext) = context->Edx;
535 ESI_sig(sigcontext) = context->Esi;
536 EDI_sig(sigcontext) = context->Edi;
537 EBP_sig(sigcontext) = context->Ebp;
538 EFL_sig(sigcontext) = context->EFlags;
539 EIP_sig(sigcontext) = context->Eip;
540 ESP_sig(sigcontext) = context->Esp;
541 CS_sig(sigcontext) = context->SegCs;
542 DS_sig(sigcontext) = context->SegDs;
543 ES_sig(sigcontext) = context->SegEs;
544 SS_sig(sigcontext) = context->SegSs;
546 FS_sig(sigcontext) = context->SegFs;
548 __set_fs( context->SegFs );
551 GS_sig(sigcontext) = context->SegGs;
553 __set_gs( context->SegGs );
558 /***********************************************************************
561 * Set the FPU context from a sigcontext.
563 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
566 if (FPU_sig(sigcontext))
568 context->FloatSave = *FPU_sig(sigcontext);
573 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
574 #endif /* __GNUC__ */
578 /***********************************************************************
581 * Restore the FPU context to a sigcontext.
583 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
585 /* reset the current interrupt status */
586 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
588 if (FPU_sig(sigcontext))
590 *FPU_sig(sigcontext) = context->FloatSave;
595 /* avoid nested exceptions */
596 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
597 #endif /* __GNUC__ */
601 /**********************************************************************
604 * Get the FPU exception code from the FPU status.
606 static inline DWORD get_fpu_code( const CONTEXT *context )
608 DWORD status = context->FloatSave.StatusWord;
610 if (status & 0x01) /* IE */
612 if (status & 0x40) /* SF */
613 return EXCEPTION_FLT_STACK_CHECK;
615 return EXCEPTION_FLT_INVALID_OPERATION;
617 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
618 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
619 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
620 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
621 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
622 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
626 /***********************************************************************
629 * Unblock signals. Called from EXC_RtlRaiseException.
631 void SIGNAL_Unblock( void )
633 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
637 /**********************************************************************
640 * Implementation of SIGSEGV handler.
642 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
644 EXCEPTION_RECORD rec;
645 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
648 /* we want the page-fault case to be fast */
649 if (trap_code == T_PAGEFLT)
650 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
653 rec.ExceptionRecord = NULL;
654 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
655 rec.ExceptionAddress = (LPVOID)context->Eip;
656 rec.NumberParameters = 0;
660 case T_OFLOW: /* Overflow exception */
661 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
663 case T_BOUND: /* Bound range exception */
664 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
666 case T_PRIVINFLT: /* Invalid opcode exception */
667 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
669 case T_STKFLT: /* Stack fault */
670 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
672 case T_SEGNPFLT: /* Segment not present exception */
673 case T_PROTFLT: /* General protection fault */
674 case T_UNKNOWN: /* Unknown fault code */
675 if (INSTR_EmulateInstruction( context )) return;
676 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
678 case T_PAGEFLT: /* Page fault */
680 rec.NumberParameters = 2;
681 rec.ExceptionInformation[0] = (err_code & 2) != 0;
682 rec.ExceptionInformation[1] = (DWORD)cr2;
684 rec.ExceptionCode = page_fault_code;
686 case T_ALIGNFLT: /* Alignment check exception */
687 /* FIXME: pass through exception handler first? */
688 if (context->EFlags & 0x00040000)
690 /* Disable AC flag, return */
691 context->EFlags &= ~0x00040000;
694 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
697 ERR( "Got unexpected trap %d\n", trap_code );
699 case T_NMI: /* NMI interrupt */
700 case T_DNA: /* Device not available exception */
701 case T_DOUBLEFLT: /* Double fault exception */
702 case T_TSSFLT: /* Invalid TSS exception */
703 case T_RESERVED: /* Unknown exception */
704 case T_MCHK: /* Machine check exception */
706 case T_CACHEFLT: /* Cache flush exception */
708 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
711 EXC_RtlRaiseException( &rec, context );
715 /**********************************************************************
718 * Implementation of SIGTRAP handler.
720 static void do_trap( CONTEXT *context, int trap_code )
722 EXCEPTION_RECORD rec;
724 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
725 rec.ExceptionRecord = NULL;
726 rec.ExceptionAddress = (LPVOID)context->Eip;
727 rec.NumberParameters = 0;
731 case T_TRCTRAP: /* Single-step exception */
732 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
733 context->EFlags &= ~0x100; /* clear single-step flag */
735 case T_BPTFLT: /* Breakpoint exception */
736 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
739 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
742 EXC_RtlRaiseException( &rec, context );
746 /**********************************************************************
749 * Implementation of SIGFPE handler
751 static void do_fpe( CONTEXT *context, int trap_code )
753 EXCEPTION_RECORD rec;
757 case T_DIVIDE: /* Division by zero exception */
758 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
760 case T_FPOPFLT: /* Coprocessor segment overrun */
761 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
763 case T_ARITHTRAP: /* Floating point exception */
764 case T_UNKNOWN: /* Unknown fault code */
765 rec.ExceptionCode = get_fpu_code( context );
768 ERR( "Got unexpected trap %d\n", trap_code );
769 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
772 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
773 rec.ExceptionRecord = NULL;
774 rec.ExceptionAddress = (LPVOID)context->Eip;
775 rec.NumberParameters = 0;
776 EXC_RtlRaiseException( &rec, context );
781 /**********************************************************************
784 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
786 static void set_vm86_pend( CONTEXT *context )
788 EXCEPTION_RECORD rec;
789 TEB *teb = NtCurrentTeb();
791 rec.ExceptionCode = EXCEPTION_VM86_STI;
792 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
793 rec.ExceptionRecord = NULL;
794 rec.NumberParameters = 1;
795 rec.ExceptionInformation[0] = 0;
797 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
798 teb->vm86_pending |= VIP_MASK;
799 /* see if we were in VM86 mode */
800 if (context->EFlags & 0x00020000)
802 /* seems so, also set flag in signal context */
803 if (context->EFlags & VIP_MASK) return;
804 context->EFlags |= VIP_MASK;
805 if (context->EFlags & VIF_MASK) {
806 /* VIF is set, throw exception */
807 teb->vm86_pending = 0;
808 rec.ExceptionAddress = (LPVOID)context->Eip;
809 EXC_RtlRaiseException( &rec, context );
812 else if (teb->vm86_ptr)
814 /* not in VM86, but possibly setting up for it */
815 struct vm86plus_struct *vm86 = (struct vm86plus_struct*)(teb->vm86_ptr);
816 if (vm86->regs.eflags & VIP_MASK) return;
817 vm86->regs.eflags |= VIP_MASK;
818 if (vm86->regs.eflags & VIF_MASK) {
819 /* VIF is set, throw exception */
821 teb->vm86_pending = 0;
822 save_vm86_context( &vcontext, vm86 );
823 rec.ExceptionAddress = (LPVOID)vcontext.Eip;
824 EXC_RtlRaiseException( &rec, &vcontext );
825 restore_vm86_context( &vcontext, vm86 );
827 /* must also save here */
828 *(CONTEXT*)(teb->vm86_ctx) = vcontext;
835 /**********************************************************************
838 * Handler for SIGUSR2.
839 * We use it to signal that the running __wine_enter_vm86() should
840 * immediately set VIP_MASK, causing pending events to be handled
841 * as early as possible.
843 static HANDLER_DEF(usr2_handler)
847 save_context( &context, HANDLER_CONTEXT );
848 set_vm86_pend( &context );
849 restore_context( &context, HANDLER_CONTEXT );
853 /**********************************************************************
856 * Handler for SIGALRM.
857 * Increases the alarm counter and sets the vm86 pending flag.
859 static HANDLER_DEF(alrm_handler)
863 save_context( &context, HANDLER_CONTEXT );
864 NtCurrentTeb()->alarms++;
865 set_vm86_pend( &context );
866 restore_context( &context, HANDLER_CONTEXT );
868 #endif /* __HAVE_VM86 */
871 /**********************************************************************
874 * Handler for SIGSEGV and related errors.
876 static HANDLER_DEF(segv_handler)
879 save_context( &context, HANDLER_CONTEXT );
880 do_segv( &context, get_trap_code(HANDLER_CONTEXT),
881 get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
882 restore_context( &context, HANDLER_CONTEXT );
886 /**********************************************************************
889 * Handler for SIGTRAP.
891 static HANDLER_DEF(trap_handler)
894 save_context( &context, HANDLER_CONTEXT );
895 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
896 restore_context( &context, HANDLER_CONTEXT );
900 /**********************************************************************
903 * Handler for SIGFPE.
905 static HANDLER_DEF(fpe_handler)
908 save_fpu( &context, HANDLER_CONTEXT );
909 save_context( &context, HANDLER_CONTEXT );
910 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
911 restore_context( &context, HANDLER_CONTEXT );
912 restore_fpu( &context, HANDLER_CONTEXT );
916 /**********************************************************************
919 * Handler for SIGINT.
921 static HANDLER_DEF(int_handler)
923 EXCEPTION_RECORD rec;
926 save_context( &context, HANDLER_CONTEXT );
927 rec.ExceptionCode = CONTROL_C_EXIT;
928 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
929 rec.ExceptionRecord = NULL;
930 rec.ExceptionAddress = (LPVOID)context.Eip;
931 rec.NumberParameters = 0;
932 EXC_RtlRaiseException( &rec, &context );
933 restore_context( &context, HANDLER_CONTEXT );
937 /***********************************************************************
940 * Set a signal handler
942 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
944 struct sigaction sig_act;
947 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
949 struct kernel_sigaction sig_act;
950 sig_act.ksa_handler = func;
951 sig_act.ksa_flags = SA_RESTART;
952 sig_act.ksa_mask = (1 << (SIGINT-1)) |
954 /* point to the top of the stack */
955 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
956 return wine_sigaction( sig, &sig_act, NULL );
959 sig_act.sa_handler = func;
960 sigemptyset( &sig_act.sa_mask );
961 sigaddset( &sig_act.sa_mask, SIGINT );
962 sigaddset( &sig_act.sa_mask, SIGALRM );
965 sig_act.sa_flags = SA_RESTART;
966 #elif defined (__svr4__) || defined(_SCO_DS)
967 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
969 sig_act.sa_flags = 0;
973 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
975 return sigaction( sig, &sig_act, NULL );
979 /**********************************************************************
982 BOOL SIGNAL_Init(void)
984 int have_sigaltstack = 0;
986 #ifdef HAVE_SIGALTSTACK
987 struct sigaltstack ss;
988 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
990 ss.ss_size = SIGNAL_STACK_SIZE;
992 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
994 /* sigaltstack may fail because the kernel is too old, or
995 because glibc is brain-dead. In the latter case a
996 direct system call should succeed. */
997 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1000 #endif /* HAVE_SIGALTSTACK */
1002 sigfillset( &all_sigs );
1004 /* automatic child reaping to avoid zombies */
1005 signal( SIGCHLD, SIG_IGN );
1007 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1008 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1009 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1010 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1012 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1015 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1019 if (set_handler( SIGALRM, have_sigaltstack, (void (*)())alrm_handler ) == -1) goto error;
1020 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1026 perror("sigaction");
1032 /**********************************************************************
1035 * Enter vm86 mode with the specified register context.
1037 void __wine_enter_vm86( CONTEXT *context )
1039 EXCEPTION_RECORD rec;
1040 TEB *teb = NtCurrentTeb();
1042 struct vm86plus_struct vm86;
1044 memset( &vm86, 0, sizeof(vm86) );
1047 restore_vm86_context( context, &vm86 );
1048 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1049 * so save it on entry, just in case */
1050 teb->vm86_pending |= (context->EFlags & VIP_MASK);
1051 /* Work around race conditions with signal handler
1052 * (avoiding sigprocmask for performance reasons) */
1053 teb->vm86_ptr = &vm86;
1054 vm86.regs.eflags |= teb->vm86_pending;
1055 /* Check for VIF|VIP here, since vm86_enter doesn't */
1056 if ((vm86.regs.eflags & (VIF_MASK|VIP_MASK)) == (VIF_MASK|VIP_MASK)) {
1057 teb->vm86_ptr = NULL;
1058 teb->vm86_pending = 0;
1059 context->EFlags |= VIP_MASK;
1060 rec.ExceptionCode = EXCEPTION_VM86_STI;
1061 rec.ExceptionInformation[0] = 0;
1067 res = vm86_enter( &vm86 );
1073 } while (VM86_TYPE(res) == VM86_SIGNAL);
1075 teb->vm86_ctx = context;
1076 save_vm86_context( context, &vm86 );
1077 teb->vm86_ptr = NULL;
1078 teb->vm86_ctx = NULL;
1079 context->EFlags |= teb->vm86_pending;
1081 switch(VM86_TYPE(res))
1083 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1084 do_segv( context, T_PROTFLT, 0, 0 );
1086 case VM86_TRAP: /* return due to DOS-debugger request */
1087 do_trap( context, VM86_ARG(res) );
1089 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1090 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1092 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1093 teb->vm86_pending = 0;
1094 rec.ExceptionCode = EXCEPTION_VM86_STI;
1096 case VM86_PICRETURN: /* return due to pending PIC request */
1097 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1100 ERR( "unhandled result from vm86 mode %x\n", res );
1103 rec.ExceptionInformation[0] = VM86_ARG(res);
1105 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1106 rec.ExceptionRecord = NULL;
1107 rec.ExceptionAddress = (LPVOID)context->Eip;
1108 rec.NumberParameters = 1;
1109 EXC_RtlRaiseException( &rec, context );
1113 #else /* __HAVE_VM86 */
1114 void __wine_enter_vm86( CONTEXT *context )
1116 MESSAGE("vm86 mode not supported on this platform\n");
1118 #endif /* __HAVE_VM86 */
1120 /**********************************************************************
1121 * DbgBreakPoint (NTDLL.@)
1123 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1125 /**********************************************************************
1126 * DbgUserBreakPoint (NTDLL.@)
1128 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1130 #endif /* __i386__ */