Added support for exception handling while in vm86 mode.
[wine.git] / dlls / ntdll / signal_i386.c
bloba39f5ad33f012a29b9ade9028a5f327a221e8451
1 /*
2 * i386 signal handling routines
3 *
4 * Copyright 1999 Alexandre Julliard
5 */
7 #ifdef __i386__
9 #include "config.h"
11 #include <errno.h>
12 #include <signal.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <unistd.h>
17 #ifdef HAVE_SYS_PARAM_H
18 # include <sys/param.h>
19 #endif
20 #ifdef HAVE_SYSCALL_H
21 # include <syscall.h>
22 #else
23 # ifdef HAVE_SYS_SYSCALL_H
24 # include <sys/syscall.h>
25 # endif
26 #endif
28 #ifdef HAVE_SYS_VM86_H
29 # include <sys/vm86.h>
30 #endif
32 #include "winnt.h"
33 #include "selectors.h"
35 /***********************************************************************
36 * signal context platform-specific definitions
39 #ifdef linux
40 typedef struct
42 unsigned short sc_gs, __gsh;
43 unsigned short sc_fs, __fsh;
44 unsigned short sc_es, __esh;
45 unsigned short sc_ds, __dsh;
46 unsigned long sc_edi;
47 unsigned long sc_esi;
48 unsigned long sc_ebp;
49 unsigned long sc_esp;
50 unsigned long sc_ebx;
51 unsigned long sc_edx;
52 unsigned long sc_ecx;
53 unsigned long sc_eax;
54 unsigned long sc_trapno;
55 unsigned long sc_err;
56 unsigned long sc_eip;
57 unsigned short sc_cs, __csh;
58 unsigned long sc_eflags;
59 unsigned long esp_at_signal;
60 unsigned short sc_ss, __ssh;
61 unsigned long i387;
62 unsigned long oldmask;
63 unsigned long cr2;
64 } SIGCONTEXT;
66 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
67 #define HANDLER_CONTEXT (&__context)
69 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
70 struct kernel_sigaction
72 void (*ksa_handler)();
73 unsigned long ksa_mask;
74 unsigned long ksa_flags;
75 void *ksa_restorer;
78 /* Similar to the sigaction function in libc, except it leaves alone the
79 restorer field, which is used to specify the signal stack address */
80 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
81 struct kernel_sigaction *old )
83 __asm__ __volatile__( "pushl %%ebx\n\t"
84 "movl %2,%%ebx\n\t"
85 "int $0x80\n\t"
86 "popl %%ebx"
87 : "=a" (sig)
88 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
89 if (sig>=0) return 0;
90 errno = -sig;
91 return -1;
94 #ifdef HAVE_SIGALTSTACK
95 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
96 static inline int wine_sigaltstack( const struct sigaltstack *new,
97 struct sigaltstack *old )
99 int ret;
100 __asm__ __volatile__( "pushl %%ebx\n\t"
101 "movl %2,%%ebx\n\t"
102 "int $0x80\n\t"
103 "popl %%ebx"
104 : "=a" (ret)
105 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
106 if (ret >= 0) return 0;
107 errno = -ret;
108 return -1;
110 #endif
112 int vm86_enter( struct vm86plus_struct *ptr );
113 void vm86_return();
114 __ASM_GLOBAL_FUNC(vm86_enter,
115 "pushl %ebp\n\t"
116 "movl %esp, %ebp\n\t"
117 "movl $166,%eax\n\t" /*SYS_vm86*/
118 "pushl %fs\n\t"
119 "movl 8(%ebp),%ecx\n\t"
120 "pushl %ebx\n\t"
121 "movl $1,%ebx\n\t" /*VM86_ENTER*/
122 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
123 "int $0x80\n"
124 ".globl " __ASM_NAME("vm86_return") "\n\t"
125 ".type " __ASM_NAME("vm86_return") ",@function\n"
126 __ASM_NAME("vm86_return") ":\n\t"
127 "popl %ecx\n\t"
128 "popl %ebx\n\t"
129 "popl %fs\n\t"
130 "popl %ebp\n\t"
131 "ret" );
133 #endif /* linux */
135 #ifdef BSDI
137 #define EAX_sig(context) ((context)->tf_eax)
138 #define EBX_sig(context) ((context)->tf_ebx)
139 #define ECX_sig(context) ((context)->tf_ecx)
140 #define EDX_sig(context) ((context)->tf_edx)
141 #define ESI_sig(context) ((context)->tf_esi)
142 #define EDI_sig(context) ((context)->tf_edi)
143 #define EBP_sig(context) ((context)->tf_ebp)
145 #define CS_sig(context) ((context)->tf_cs)
146 #define DS_sig(context) ((context)->tf_ds)
147 #define ES_sig(context) ((context)->tf_es)
148 #define SS_sig(context) ((context)->tf_ss)
150 #include <machine/frame.h>
151 typedef struct trapframe SIGCONTEXT;
153 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
154 #define HANDLER_CONTEXT __context
156 #define EFL_sig(context) ((context)->tf_eflags)
158 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
159 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
161 #endif /* bsdi */
163 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
165 typedef struct sigcontext SIGCONTEXT;
167 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
168 #define HANDLER_CONTEXT __context
170 #endif /* FreeBSD */
172 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
174 #ifdef _SCO_DS
175 #include <sys/regset.h>
176 #endif
177 /* Solaris kludge */
178 #undef ERR
179 #include <sys/ucontext.h>
180 #undef ERR
181 typedef struct ucontext SIGCONTEXT;
183 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
184 #define HANDLER_CONTEXT __context
186 #endif /* svr4 || SCO_DS */
188 #ifdef __EMX__
190 typedef struct
192 unsigned long ContextFlags;
193 FLOATING_SAVE_AREA sc_float;
194 unsigned long sc_gs;
195 unsigned long sc_fs;
196 unsigned long sc_es;
197 unsigned long sc_ds;
198 unsigned long sc_edi;
199 unsigned long sc_esi;
200 unsigned long sc_eax;
201 unsigned long sc_ebx;
202 unsigned long sc_ecx;
203 unsigned long sc_edx;
204 unsigned long sc_ebp;
205 unsigned long sc_eip;
206 unsigned long sc_cs;
207 unsigned long sc_eflags;
208 unsigned long sc_esp;
209 unsigned long sc_ss;
210 } SIGCONTEXT;
212 #endif /* __EMX__ */
215 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
217 #define EAX_sig(context) ((context)->sc_eax)
218 #define EBX_sig(context) ((context)->sc_ebx)
219 #define ECX_sig(context) ((context)->sc_ecx)
220 #define EDX_sig(context) ((context)->sc_edx)
221 #define ESI_sig(context) ((context)->sc_esi)
222 #define EDI_sig(context) ((context)->sc_edi)
223 #define EBP_sig(context) ((context)->sc_ebp)
225 #define CS_sig(context) ((context)->sc_cs)
226 #define DS_sig(context) ((context)->sc_ds)
227 #define ES_sig(context) ((context)->sc_es)
228 #define SS_sig(context) ((context)->sc_ss)
230 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
231 * saved by the exception handling. duh.
232 * Actually they are in -current (have been for a while), and that
233 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
234 * If you're running a system from the -stable branch older than that,
235 * like a 3.3-RELEASE, grab the patch from the ports tree:
236 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
237 * (If its not yet there when you look, go here:
238 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
240 #ifdef __FreeBSD__
241 #define FS_sig(context) ((context)->sc_fs)
242 #define GS_sig(context) ((context)->sc_gs)
243 #endif
245 #ifdef linux
246 #define FS_sig(context) ((context)->sc_fs)
247 #define GS_sig(context) ((context)->sc_gs)
248 #define CR2_sig(context) ((context)->cr2)
249 #define TRAP_sig(context) ((context)->sc_trapno)
250 #define ERROR_sig(context) ((context)->sc_err)
251 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
252 #endif
254 #ifndef __FreeBSD__
255 #define EFL_sig(context) ((context)->sc_eflags)
256 #else
257 #define EFL_sig(context) ((context)->sc_efl)
258 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
259 #define CR2_sig(context) ((context)->sc_err)
260 #define TRAP_sig(context) ((context)->sc_trapno)
261 #endif
263 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
264 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
266 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
268 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
270 #ifdef _SCO_DS
271 #define gregs regs
272 #endif
274 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
275 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
276 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
277 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
278 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
279 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
280 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
282 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
283 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
284 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
285 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
287 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
288 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
290 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
292 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
293 #ifdef R_ESP
294 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
295 #else
296 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
297 #endif
298 #ifdef TRAPNO
299 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
300 #endif
302 #endif /* svr4 || SCO_DS */
305 /* exception code definitions (already defined by FreeBSD) */
306 #ifndef __FreeBSD__ /* FIXME: other BSDs? */
307 #define T_DIVIDE 0 /* Division by zero exception */
308 #define T_TRCTRAP 1 /* Single-step exception */
309 #define T_NMI 2 /* NMI interrupt */
310 #define T_BPTFLT 3 /* Breakpoint exception */
311 #define T_OFLOW 4 /* Overflow exception */
312 #define T_BOUND 5 /* Bound range exception */
313 #define T_PRIVINFLT 6 /* Invalid opcode exception */
314 #define T_DNA 7 /* Device not available exception */
315 #define T_DOUBLEFLT 8 /* Double fault exception */
316 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
317 #define T_TSSFLT 10 /* Invalid TSS exception */
318 #define T_SEGNPFLT 11 /* Segment not present exception */
319 #define T_STKFLT 12 /* Stack fault */
320 #define T_PROTFLT 13 /* General protection fault */
321 #define T_PAGEFLT 14 /* Page fault */
322 #define T_RESERVED 15 /* Unknown exception */
323 #define T_ARITHTRAP 16 /* Floating point exception */
324 #define T_ALIGNFLT 17 /* Alignment check exception */
325 #define T_MCHK 18 /* Machine check exception */
326 #define T_CACHEFLT 19 /* Cache flush exception */
327 #endif
329 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
331 #include "wine/exception.h"
332 #include "winnt.h"
333 #include "stackframe.h"
334 #include "global.h"
335 #include "miscemu.h"
336 #include "ntddk.h"
337 #include "syslevel.h"
338 #include "debugtools.h"
340 DEFAULT_DEBUG_CHANNEL(seh);
343 /***********************************************************************
344 * get_trap_code
346 * Get the trap code for a signal.
348 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
350 #ifdef TRAP_sig
351 return TRAP_sig(sigcontext);
352 #else
353 return T_UNKNOWN; /* unknown trap code */
354 #endif
357 /***********************************************************************
358 * get_error_code
360 * Get the error code for a signal.
362 static inline int get_error_code( const SIGCONTEXT *sigcontext )
364 #ifdef ERROR_sig
365 return ERROR_sig(sigcontext);
366 #else
367 return 0;
368 #endif
371 /***********************************************************************
372 * get_cr2_value
374 * Get the CR2 value for a signal.
376 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
378 #ifdef CR2_sig
379 return (void *)CR2_sig(sigcontext);
380 #else
381 return NULL;
382 #endif
385 /***********************************************************************
386 * save_context
388 * Set the register values from a sigcontext.
390 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
392 WORD fs;
393 /* get %fs at time of the fault */
394 #ifdef FS_sig
395 fs = FS_sig(sigcontext);
396 #else
397 fs = __get_fs();
398 #endif
399 context->SegFs = fs;
401 /* now restore a proper %fs for the fault handler */
402 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) /* 16-bit mode */
404 fs = SYSLEVEL_Win16CurrentTeb;
406 #ifdef linux
407 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
409 /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
410 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)ESP_sig(sigcontext);
411 /* fetch the saved %fs on the stack */
412 fs = *((unsigned int *)ESP_sig(sigcontext) + 2);
413 __set_fs(fs);
414 /* get context from vm86 struct */
415 context->Eax = vm86->regs.eax;
416 context->Ebx = vm86->regs.ebx;
417 context->Ecx = vm86->regs.ecx;
418 context->Edx = vm86->regs.edx;
419 context->Esi = vm86->regs.esi;
420 context->Edi = vm86->regs.edi;
421 context->Esp = vm86->regs.esp;
422 context->Ebp = vm86->regs.ebp;
423 context->Eip = vm86->regs.eip;
424 context->SegCs = vm86->regs.cs;
425 context->SegDs = vm86->regs.ds;
426 context->SegEs = vm86->regs.es;
427 context->SegFs = vm86->regs.fs;
428 context->SegGs = vm86->regs.gs;
429 context->SegSs = vm86->regs.ss;
430 context->EFlags = vm86->regs.eflags;
431 return;
433 #endif /* linux */
435 if (!fs)
437 fs = SYSLEVEL_EmergencyTeb;
438 __set_fs(fs);
439 ERR("fallback to emergency TEB\n");
441 __set_fs(fs);
443 context->Eax = EAX_sig(sigcontext);
444 context->Ebx = EBX_sig(sigcontext);
445 context->Ecx = ECX_sig(sigcontext);
446 context->Edx = EDX_sig(sigcontext);
447 context->Esi = ESI_sig(sigcontext);
448 context->Edi = EDI_sig(sigcontext);
449 context->Ebp = EBP_sig(sigcontext);
450 context->EFlags = EFL_sig(sigcontext);
451 context->Eip = EIP_sig(sigcontext);
452 context->Esp = ESP_sig(sigcontext);
453 context->SegCs = LOWORD(CS_sig(sigcontext));
454 context->SegDs = LOWORD(DS_sig(sigcontext));
455 context->SegEs = LOWORD(ES_sig(sigcontext));
456 context->SegSs = LOWORD(SS_sig(sigcontext));
457 #ifdef GS_sig
458 context->SegGs = LOWORD(GS_sig(sigcontext));
459 #else
460 context->SegGs = __get_gs();
461 #endif
465 /***********************************************************************
466 * restore_context
468 * Build a sigcontext from the register values.
470 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
472 #ifdef linux
473 /* check if exception occurred in vm86 mode */
474 if ((void *)EIP_sig(sigcontext) == vm86_return &&
475 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)))
477 /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
478 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)ESP_sig(sigcontext);
479 vm86->regs.eax = context->Eax;
480 vm86->regs.ebx = context->Ebx;
481 vm86->regs.ecx = context->Ecx;
482 vm86->regs.edx = context->Edx;
483 vm86->regs.esi = context->Esi;
484 vm86->regs.edi = context->Edi;
485 vm86->regs.esp = context->Esp;
486 vm86->regs.ebp = context->Ebp;
487 vm86->regs.eip = context->Eip;
488 vm86->regs.cs = context->SegCs;
489 vm86->regs.ds = context->SegDs;
490 vm86->regs.es = context->SegEs;
491 vm86->regs.fs = context->SegFs;
492 vm86->regs.gs = context->SegGs;
493 vm86->regs.ss = context->SegSs;
494 vm86->regs.eflags = context->EFlags;
495 return;
497 #endif /* linux */
499 EAX_sig(sigcontext) = context->Eax;
500 EBX_sig(sigcontext) = context->Ebx;
501 ECX_sig(sigcontext) = context->Ecx;
502 EDX_sig(sigcontext) = context->Edx;
503 ESI_sig(sigcontext) = context->Esi;
504 EDI_sig(sigcontext) = context->Edi;
505 EBP_sig(sigcontext) = context->Ebp;
506 EFL_sig(sigcontext) = context->EFlags;
507 EIP_sig(sigcontext) = context->Eip;
508 ESP_sig(sigcontext) = context->Esp;
509 CS_sig(sigcontext) = context->SegCs;
510 DS_sig(sigcontext) = context->SegDs;
511 ES_sig(sigcontext) = context->SegEs;
512 SS_sig(sigcontext) = context->SegSs;
513 #ifdef FS_sig
514 FS_sig(sigcontext) = context->SegFs;
515 #else
516 __set_fs( context->SegFs );
517 #endif
518 #ifdef GS_sig
519 GS_sig(sigcontext) = context->SegGs;
520 #else
521 __set_gs( context->SegGs );
522 #endif
526 /***********************************************************************
527 * save_fpu
529 * Set the FPU context from a sigcontext.
531 static void inline save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
533 #ifdef FPU_sig
534 if (FPU_sig(sigcontext))
536 context->FloatSave = *FPU_sig(sigcontext);
537 return;
539 #endif /* FPU_sig */
540 #ifdef __GNUC__
541 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
542 #endif /* __GNUC__ */
546 /***********************************************************************
547 * restore_fpu
549 * Restore the FPU context to a sigcontext.
551 static void inline restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
553 /* reset the current interrupt status */
554 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
555 #ifdef FPU_sig
556 if (FPU_sig(sigcontext))
558 *FPU_sig(sigcontext) = context->FloatSave;
559 return;
561 #endif /* FPU_sig */
562 #ifdef __GNUC__
563 /* avoid nested exceptions */
564 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
565 #endif /* __GNUC__ */
569 /**********************************************************************
570 * get_fpu_code
572 * Get the FPU exception code from the FPU status.
574 static inline DWORD get_fpu_code( const CONTEXT *context )
576 DWORD status = context->FloatSave.StatusWord;
578 if (status & 0x01) /* IE */
580 if (status & 0x40) /* SF */
581 return EXCEPTION_FLT_STACK_CHECK;
582 else
583 return EXCEPTION_FLT_INVALID_OPERATION;
585 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
586 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
587 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
588 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
589 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
590 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
594 /**********************************************************************
595 * do_segv
597 * Implementation of SIGSEGV handler.
599 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
601 EXCEPTION_RECORD rec;
602 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
604 #ifdef CR2_sig
605 /* we want the page-fault case to be fast */
606 if (trap_code == T_PAGEFLT)
607 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
608 #endif
610 rec.ExceptionRecord = NULL;
611 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
612 rec.ExceptionAddress = (LPVOID)context->Eip;
613 rec.NumberParameters = 0;
615 switch(trap_code)
617 case T_OFLOW: /* Overflow exception */
618 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
619 break;
620 case T_BOUND: /* Bound range exception */
621 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
622 break;
623 case T_PRIVINFLT: /* Invalid opcode exception */
624 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
625 break;
626 case T_STKFLT: /* Stack fault */
627 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
628 break;
629 case T_SEGNPFLT: /* Segment not present exception */
630 case T_PROTFLT: /* General protection fault */
631 case T_UNKNOWN: /* Unknown fault code */
632 if (INSTR_EmulateInstruction( context )) return;
633 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
634 break;
635 case T_PAGEFLT: /* Page fault */
636 #ifdef CR2_sig
637 rec.NumberParameters = 2;
638 rec.ExceptionInformation[0] = (err_code & 2) != 0;
639 rec.ExceptionInformation[0] = 0;
640 rec.ExceptionInformation[1] = (DWORD)cr2;
641 #endif /* CR2_sig */
642 rec.ExceptionCode = page_fault_code;
643 break;
644 case T_ALIGNFLT: /* Alignment check exception */
645 /* FIXME: pass through exception handler first? */
646 if (context->EFlags & 0x00040000)
648 /* Disable AC flag, return */
649 context->EFlags &= ~0x00040000;
650 return;
652 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
653 break;
654 default:
655 ERR( "Got unexpected trap %d\n", trap_code );
656 /* fall through */
657 case T_NMI: /* NMI interrupt */
658 case T_DNA: /* Device not available exception */
659 case T_DOUBLEFLT: /* Double fault exception */
660 case T_TSSFLT: /* Invalid TSS exception */
661 case T_RESERVED: /* Unknown exception */
662 case T_MCHK: /* Machine check exception */
663 #ifdef T_CACHEFLT
664 case T_CACHEFLT: /* Cache flush exception */
665 #endif
666 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
667 break;
669 EXC_RtlRaiseException( &rec, context );
673 /**********************************************************************
674 * do_trap
676 * Implementation of SIGTRAP handler.
678 static void do_trap( CONTEXT *context, int trap_code )
680 EXCEPTION_RECORD rec;
682 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
683 rec.ExceptionRecord = NULL;
684 rec.ExceptionAddress = (LPVOID)context->Eip;
685 rec.NumberParameters = 0;
687 switch(trap_code)
689 case T_TRCTRAP: /* Single-step exception */
690 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
691 context->EFlags &= ~0x100; /* clear single-step flag */
692 break;
693 case T_BPTFLT: /* Breakpoint exception */
694 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
695 /* fall through */
696 default:
697 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
698 break;
700 EXC_RtlRaiseException( &rec, context );
704 /**********************************************************************
705 * do_fpe
707 * Implementation of SIGFPE handler
709 static void do_fpe( CONTEXT *context, int trap_code )
711 EXCEPTION_RECORD rec;
713 switch(trap_code)
715 case T_DIVIDE: /* Division by zero exception */
716 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
717 break;
718 case T_FPOPFLT: /* Coprocessor segment overrun */
719 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
720 break;
721 case T_ARITHTRAP: /* Floating point exception */
722 case T_UNKNOWN: /* Unknown fault code */
723 rec.ExceptionCode = get_fpu_code( context );
724 break;
725 default:
726 ERR( "Got unexpected trap %d\n", trap_code );
727 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
728 break;
730 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
731 rec.ExceptionRecord = NULL;
732 rec.ExceptionAddress = (LPVOID)context->Eip;
733 rec.NumberParameters = 0;
734 EXC_RtlRaiseException( &rec, context );
738 /**********************************************************************
739 * segv_handler
741 * Handler for SIGSEGV and related errors.
743 static HANDLER_DEF(segv_handler)
745 CONTEXT context;
746 save_context( &context, HANDLER_CONTEXT );
747 do_segv( &context, get_trap_code(HANDLER_CONTEXT),
748 get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
749 restore_context( &context, HANDLER_CONTEXT );
753 /**********************************************************************
754 * trap_handler
756 * Handler for SIGTRAP.
758 static HANDLER_DEF(trap_handler)
760 CONTEXT context;
761 save_context( &context, HANDLER_CONTEXT );
762 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
763 restore_context( &context, HANDLER_CONTEXT );
767 /**********************************************************************
768 * fpe_handler
770 * Handler for SIGFPE.
772 static HANDLER_DEF(fpe_handler)
774 CONTEXT context;
775 save_fpu( &context, HANDLER_CONTEXT );
776 save_context( &context, HANDLER_CONTEXT );
777 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
778 restore_context( &context, HANDLER_CONTEXT );
779 restore_fpu( &context, HANDLER_CONTEXT );
783 /**********************************************************************
784 * int_handler
786 * Handler for SIGINT.
788 static HANDLER_DEF(int_handler)
790 EXCEPTION_RECORD rec;
791 CONTEXT context;
793 save_context( &context, HANDLER_CONTEXT );
794 rec.ExceptionCode = CONTROL_C_EXIT;
795 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
796 rec.ExceptionRecord = NULL;
797 rec.ExceptionAddress = (LPVOID)context.Eip;
798 rec.NumberParameters = 0;
799 EXC_RtlRaiseException( &rec, &context );
800 restore_context( &context, HANDLER_CONTEXT );
804 /***********************************************************************
805 * set_handler
807 * Set a signal handler
809 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
811 struct sigaction sig_act;
813 #ifdef linux
814 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
816 struct kernel_sigaction sig_act;
817 sig_act.ksa_handler = func;
818 sig_act.ksa_flags = SA_RESTART | SA_NOMASK;
819 sig_act.ksa_mask = 0;
820 /* point to the top of the stack */
821 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
822 return wine_sigaction( sig, &sig_act, NULL );
824 #endif /* linux */
825 sig_act.sa_handler = func;
826 sigemptyset( &sig_act.sa_mask );
828 #ifdef linux
829 sig_act.sa_flags = SA_RESTART | SA_NOMASK;
830 #elif defined (__svr4__) || defined(_SCO_DS)
831 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
832 #else
833 sig_act.sa_flags = 0;
834 #endif
836 #ifdef SA_ONSTACK
837 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
838 #endif
839 return sigaction( sig, &sig_act, NULL );
843 /**********************************************************************
844 * SIGNAL_Init
846 BOOL SIGNAL_Init(void)
848 int have_sigaltstack = 0;
850 #ifdef HAVE_SIGALTSTACK
851 struct sigaltstack ss;
852 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
854 ss.ss_size = SIGNAL_STACK_SIZE;
855 ss.ss_flags = 0;
856 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
857 #ifdef linux
858 /* sigaltstack may fail because the kernel is too old, or
859 because glibc is brain-dead. In the latter case a
860 direct system call should succeed. */
861 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
862 #endif /* linux */
864 #endif /* HAVE_SIGALTSTACK */
866 /* automatic child reaping to avoid zombies */
867 signal( SIGCHLD, SIG_IGN );
869 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
870 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
871 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
872 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
873 #ifdef SIGBUS
874 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
875 #endif
876 #ifdef SIGTRAP
877 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
878 #endif
879 return TRUE;
881 error:
882 perror("sigaction");
883 return FALSE;
887 #ifdef linux
888 /**********************************************************************
889 * __wine_enter_vm86
891 * Enter vm86 mode with the specified register context.
893 void __wine_enter_vm86( CONTEXT *context )
895 EXCEPTION_RECORD rec;
896 int res;
897 struct vm86plus_struct vm86;
899 memset( &vm86, 0, sizeof(vm86) );
900 for (;;)
902 vm86.regs.eax = context->Eax;
903 vm86.regs.ebx = context->Ebx;
904 vm86.regs.ecx = context->Ecx;
905 vm86.regs.edx = context->Edx;
906 vm86.regs.esi = context->Esi;
907 vm86.regs.edi = context->Edi;
908 vm86.regs.esp = context->Esp;
909 vm86.regs.ebp = context->Ebp;
910 vm86.regs.eip = context->Eip;
911 vm86.regs.cs = context->SegCs;
912 vm86.regs.ds = context->SegDs;
913 vm86.regs.es = context->SegEs;
914 vm86.regs.fs = context->SegFs;
915 vm86.regs.gs = context->SegGs;
916 vm86.regs.ss = context->SegSs;
917 vm86.regs.eflags = context->EFlags;
921 res = vm86_enter( &vm86 );
922 if (res < 0)
924 errno = -res;
925 return;
927 } while (VM86_TYPE(res) == VM86_SIGNAL);
929 context->Eax = vm86.regs.eax;
930 context->Ebx = vm86.regs.ebx;
931 context->Ecx = vm86.regs.ecx;
932 context->Edx = vm86.regs.edx;
933 context->Esi = vm86.regs.esi;
934 context->Edi = vm86.regs.edi;
935 context->Esp = vm86.regs.esp;
936 context->Ebp = vm86.regs.ebp;
937 context->Eip = vm86.regs.eip;
938 context->SegCs = vm86.regs.cs;
939 context->SegDs = vm86.regs.ds;
940 context->SegEs = vm86.regs.es;
941 context->SegFs = vm86.regs.fs;
942 context->SegGs = vm86.regs.gs;
943 context->SegSs = vm86.regs.ss;
944 context->EFlags = vm86.regs.eflags;
946 switch(VM86_TYPE(res))
948 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
949 do_segv( context, T_PROTFLT, 0, 0 );
950 continue;
951 case VM86_TRAP: /* return due to DOS-debugger request */
952 do_trap( context, VM86_ARG(res) );
953 continue;
954 case VM86_INTx: /* int3/int x instruction (ARG = x) */
955 rec.ExceptionCode = EXCEPTION_VM86_INTx;
956 break;
957 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
958 rec.ExceptionCode = EXCEPTION_VM86_STI;
959 break;
960 case VM86_PICRETURN: /* return due to pending PIC request */
961 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
962 break;
963 default:
964 ERR( "unhandled result from vm86 mode %x\n", res );
965 continue;
967 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
968 rec.ExceptionRecord = NULL;
969 rec.ExceptionAddress = (LPVOID)context->Eip;
970 rec.NumberParameters = 1;
971 rec.ExceptionInformation[0] = VM86_ARG(res);
972 EXC_RtlRaiseException( &rec, context );
976 #else /* linux */
977 void __wine_enter_vm86( CONTEXT *context )
979 MESSAGE("vm86 mode not supported on this platform\n");
981 #endif /* linux */
983 /**********************************************************************
984 * DbgBreakPoint (NTDLL)
986 void WINAPI DbgBreakPoint(void);
987 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
989 /**********************************************************************
990 * DbgUserBreakPoint (NTDLL)
992 void WINAPI DbgUserBreakPoint(void);
993 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
995 #endif /* __i386__ */