Added preliminary support for switching to vm86 mode with proper
[wine/multimedia.git] / dlls / ntdll / signal_i386.c
blob2cb33a0b175f4750709c66126d760261ed0a90bc
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 "selectors.h"
34 /***********************************************************************
35 * signal context platform-specific definitions
38 #ifdef linux
39 typedef struct
41 unsigned short sc_gs, __gsh;
42 unsigned short sc_fs, __fsh;
43 unsigned short sc_es, __esh;
44 unsigned short sc_ds, __dsh;
45 unsigned long sc_edi;
46 unsigned long sc_esi;
47 unsigned long sc_ebp;
48 unsigned long sc_esp;
49 unsigned long sc_ebx;
50 unsigned long sc_edx;
51 unsigned long sc_ecx;
52 unsigned long sc_eax;
53 unsigned long sc_trapno;
54 unsigned long sc_err;
55 unsigned long sc_eip;
56 unsigned short sc_cs, __csh;
57 unsigned long sc_eflags;
58 unsigned long esp_at_signal;
59 unsigned short sc_ss, __ssh;
60 unsigned long i387;
61 unsigned long oldmask;
62 unsigned long cr2;
63 } SIGCONTEXT;
65 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
66 #define HANDLER_CONTEXT (&__context)
68 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
69 struct kernel_sigaction
71 void (*ksa_handler)();
72 unsigned long ksa_mask;
73 unsigned long ksa_flags;
74 void *ksa_restorer;
77 /* Similar to the sigaction function in libc, except it leaves alone the
78 restorer field, which is used to specify the signal stack address */
79 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
80 struct kernel_sigaction *old )
82 __asm__ __volatile__( "pushl %%ebx\n\t"
83 "movl %2,%%ebx\n\t"
84 "int $0x80\n\t"
85 "popl %%ebx"
86 : "=a" (sig)
87 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
88 if (sig>=0) return 0;
89 errno = -sig;
90 return -1;
93 #ifdef HAVE_SIGALTSTACK
94 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
95 static inline int wine_sigaltstack( const struct sigaltstack *new,
96 struct sigaltstack *old )
98 int ret;
99 __asm__ __volatile__( "pushl %%ebx\n\t"
100 "movl %2,%%ebx\n\t"
101 "int $0x80\n\t"
102 "popl %%ebx"
103 : "=a" (ret)
104 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
105 if (ret >= 0) return 0;
106 errno = -ret;
107 return -1;
109 #endif
111 static inline int wine_vm86plus( int func, struct vm86plus_struct *ptr )
113 int res;
114 __asm__ __volatile__( "pushl %%fs\n\t"
115 "pushl %%ebx\n\t"
116 "movl %2,%%ebx\n\t"
117 "int $0x80\n\t"
118 "popl %%ebx\n\t"
119 "popl %%fs"
120 : "=a" (res)
121 : "0" (166 /*SYS_vm86*/), "g" (func), "c" (ptr) );
122 if (res >= 0) return res;
123 errno = -res;
124 return -1;
127 #endif /* linux */
129 #ifdef BSDI
131 #define EAX_sig(context) ((context)->tf_eax)
132 #define EBX_sig(context) ((context)->tf_ebx)
133 #define ECX_sig(context) ((context)->tf_ecx)
134 #define EDX_sig(context) ((context)->tf_edx)
135 #define ESI_sig(context) ((context)->tf_esi)
136 #define EDI_sig(context) ((context)->tf_edi)
137 #define EBP_sig(context) ((context)->tf_ebp)
139 #define CS_sig(context) ((context)->tf_cs)
140 #define DS_sig(context) ((context)->tf_ds)
141 #define ES_sig(context) ((context)->tf_es)
142 #define SS_sig(context) ((context)->tf_ss)
144 #include <machine/frame.h>
145 typedef struct trapframe SIGCONTEXT;
147 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
148 #define HANDLER_CONTEXT __context
150 #define EFL_sig(context) ((context)->tf_eflags)
152 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
153 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
155 #endif /* bsdi */
157 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
159 typedef struct sigcontext SIGCONTEXT;
161 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
162 #define HANDLER_CONTEXT __context
164 #endif /* FreeBSD */
166 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
168 #ifdef _SCO_DS
169 #include <sys/regset.h>
170 #endif
171 /* Solaris kludge */
172 #undef ERR
173 #include <sys/ucontext.h>
174 #undef ERR
175 typedef struct ucontext SIGCONTEXT;
177 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
178 #define HANDLER_CONTEXT __context
180 #endif /* svr4 || SCO_DS */
182 #ifdef __EMX__
184 typedef struct
186 unsigned long ContextFlags;
187 FLOATING_SAVE_AREA sc_float;
188 unsigned long sc_gs;
189 unsigned long sc_fs;
190 unsigned long sc_es;
191 unsigned long sc_ds;
192 unsigned long sc_edi;
193 unsigned long sc_esi;
194 unsigned long sc_eax;
195 unsigned long sc_ebx;
196 unsigned long sc_ecx;
197 unsigned long sc_edx;
198 unsigned long sc_ebp;
199 unsigned long sc_eip;
200 unsigned long sc_cs;
201 unsigned long sc_eflags;
202 unsigned long sc_esp;
203 unsigned long sc_ss;
204 } SIGCONTEXT;
206 #endif /* __EMX__ */
209 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
211 #define EAX_sig(context) ((context)->sc_eax)
212 #define EBX_sig(context) ((context)->sc_ebx)
213 #define ECX_sig(context) ((context)->sc_ecx)
214 #define EDX_sig(context) ((context)->sc_edx)
215 #define ESI_sig(context) ((context)->sc_esi)
216 #define EDI_sig(context) ((context)->sc_edi)
217 #define EBP_sig(context) ((context)->sc_ebp)
219 #define CS_sig(context) ((context)->sc_cs)
220 #define DS_sig(context) ((context)->sc_ds)
221 #define ES_sig(context) ((context)->sc_es)
222 #define SS_sig(context) ((context)->sc_ss)
224 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
225 * saved by the exception handling. duh.
226 * Actually they are in -current (have been for a while), and that
227 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
228 * If you're running a system from the -stable branch older than that,
229 * like a 3.3-RELEASE, grab the patch from the ports tree:
230 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
231 * (If its not yet there when you look, go here:
232 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
234 #ifdef __FreeBSD__
235 #define FS_sig(context) ((context)->sc_fs)
236 #define GS_sig(context) ((context)->sc_gs)
237 #endif
239 #ifdef linux
240 #define FS_sig(context) ((context)->sc_fs)
241 #define GS_sig(context) ((context)->sc_gs)
242 #define CR2_sig(context) ((context)->cr2)
243 #define TRAP_sig(context) ((context)->sc_trapno)
244 #define ERROR_sig(context) ((context)->sc_err)
245 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
246 #endif
248 #ifndef __FreeBSD__
249 #define EFL_sig(context) ((context)->sc_eflags)
250 #else
251 #define EFL_sig(context) ((context)->sc_efl)
252 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
253 #define CR2_sig(context) ((context)->sc_err)
254 #define TRAP_sig(context) ((context)->sc_trapno)
255 #endif
257 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
258 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
260 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
262 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
264 #ifdef _SCO_DS
265 #define gregs regs
266 #endif
268 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
269 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
270 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
271 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
272 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
273 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
274 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
276 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
277 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
278 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
279 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
281 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
282 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
284 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
286 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
287 #ifdef R_ESP
288 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
289 #else
290 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
291 #endif
292 #ifdef TRAPNO
293 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
294 #endif
296 #endif /* svr4 || SCO_DS */
299 /* exception code definitions (already defined by FreeBSD) */
300 #ifndef __FreeBSD__ /* FIXME: other BSDs? */
301 #define T_DIVIDE 0 /* Division by zero exception */
302 #define T_TRCTRAP 1 /* Single-step exception */
303 #define T_NMI 2 /* NMI interrupt */
304 #define T_BPTFLT 3 /* Breakpoint exception */
305 #define T_OFLOW 4 /* Overflow exception */
306 #define T_BOUND 5 /* Bound range exception */
307 #define T_PRIVINFLT 6 /* Invalid opcode exception */
308 #define T_DNA 7 /* Device not available exception */
309 #define T_DOUBLEFLT 8 /* Double fault exception */
310 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
311 #define T_TSSFLT 10 /* Invalid TSS exception */
312 #define T_SEGNPFLT 11 /* Segment not present exception */
313 #define T_STKFLT 12 /* Stack fault */
314 #define T_PROTFLT 13 /* General protection fault */
315 #define T_PAGEFLT 14 /* Page fault */
316 #define T_RESERVED 15 /* Unknown exception */
317 #define T_ARITHTRAP 16 /* Floating point exception */
318 #define T_ALIGNFLT 17 /* Alignment check exception */
319 #define T_MCHK 18 /* Machine check exception */
320 #define T_CACHEFLT 19 /* Cache flush exception */
321 #endif
323 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
325 #include "wine/exception.h"
326 #include "winnt.h"
327 #include "stackframe.h"
328 #include "global.h"
329 #include "miscemu.h"
330 #include "ntddk.h"
331 #include "syslevel.h"
332 #include "debugtools.h"
334 DEFAULT_DEBUG_CHANNEL(seh)
338 /***********************************************************************
339 * handler_init
341 * Initialization code for a signal handler.
342 * Restores the proper %fs value for the current thread.
344 static inline void handler_init( CONTEXT *context, const SIGCONTEXT *sigcontext )
346 WORD fs;
347 /* get %fs at time of the fault */
348 #ifdef FS_sig
349 fs = FS_sig(sigcontext);
350 #else
351 fs = __get_fs();
352 #endif
353 context->SegFs = fs;
354 /* now restore a proper %fs for the fault handler */
355 if ((EFL_sig(sigcontext) & 0x00020000) || /* vm86 mode */
356 !IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) /* 16-bit mode */
357 fs = SYSLEVEL_Win16CurrentTeb;
358 if (!fs) fs = SYSLEVEL_EmergencyTeb;
359 __set_fs(fs);
362 /***********************************************************************
363 * get_trap_code
365 * Get the trap code for a signal.
367 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
369 #ifdef TRAP_sig
370 return TRAP_sig(sigcontext);
371 #else
372 return T_UNKNOWN; /* unknown trap code */
373 #endif
376 /***********************************************************************
377 * get_error_code
379 * Get the error code for a signal.
381 static inline int get_error_code( const SIGCONTEXT *sigcontext )
383 #ifdef ERROR_sig
384 return ERROR_sig(sigcontext);
385 #else
386 return 0;
387 #endif
390 /***********************************************************************
391 * get_cr2_value
393 * Get the CR2 value for a signal.
395 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
397 #ifdef CR2_sig
398 return (void *)CR2_sig(sigcontext);
399 #else
400 return NULL;
401 #endif
404 /***********************************************************************
405 * save_context
407 * Set the register values from a sigcontext.
409 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
411 context->Eax = EAX_sig(sigcontext);
412 context->Ebx = EBX_sig(sigcontext);
413 context->Ecx = ECX_sig(sigcontext);
414 context->Edx = EDX_sig(sigcontext);
415 context->Esi = ESI_sig(sigcontext);
416 context->Edi = EDI_sig(sigcontext);
417 context->Ebp = EBP_sig(sigcontext);
418 context->EFlags = EFL_sig(sigcontext);
419 context->Eip = EIP_sig(sigcontext);
420 context->Esp = ESP_sig(sigcontext);
421 context->SegCs = LOWORD(CS_sig(sigcontext));
422 context->SegDs = LOWORD(DS_sig(sigcontext));
423 context->SegEs = LOWORD(ES_sig(sigcontext));
424 context->SegSs = LOWORD(SS_sig(sigcontext));
425 /* %fs already handled in handler_init */
426 #ifdef GS_sig
427 context->SegGs = LOWORD(GS_sig(sigcontext));
428 #else
429 context->SegGs = __get_gs();
430 #endif
434 /***********************************************************************
435 * restore_context
437 * Build a sigcontext from the register values.
439 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
441 EAX_sig(sigcontext) = context->Eax;
442 EBX_sig(sigcontext) = context->Ebx;
443 ECX_sig(sigcontext) = context->Ecx;
444 EDX_sig(sigcontext) = context->Edx;
445 ESI_sig(sigcontext) = context->Esi;
446 EDI_sig(sigcontext) = context->Edi;
447 EBP_sig(sigcontext) = context->Ebp;
448 EFL_sig(sigcontext) = context->EFlags;
449 EIP_sig(sigcontext) = context->Eip;
450 ESP_sig(sigcontext) = context->Esp;
451 CS_sig(sigcontext) = context->SegCs;
452 DS_sig(sigcontext) = context->SegDs;
453 ES_sig(sigcontext) = context->SegEs;
454 SS_sig(sigcontext) = context->SegSs;
455 #ifdef FS_sig
456 FS_sig(sigcontext) = context->SegFs;
457 #else
458 __set_fs( context->SegFs );
459 #endif
460 #ifdef GS_sig
461 GS_sig(sigcontext) = context->SegGs;
462 #else
463 __set_gs( context->SegGs );
464 #endif
468 /***********************************************************************
469 * save_fpu
471 * Set the FPU context from a sigcontext.
473 static void inline save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
475 #ifdef FPU_sig
476 if (FPU_sig(sigcontext))
478 context->FloatSave = *FPU_sig(sigcontext);
479 return;
481 #endif /* FPU_sig */
482 #ifdef __GNUC__
483 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
484 #endif /* __GNUC__ */
488 /***********************************************************************
489 * restore_fpu
491 * Restore the FPU context to a sigcontext.
493 static void inline restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
495 /* reset the current interrupt status */
496 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
497 #ifdef FPU_sig
498 if (FPU_sig(sigcontext))
500 *FPU_sig(sigcontext) = context->FloatSave;
501 return;
503 #endif /* FPU_sig */
504 #ifdef __GNUC__
505 /* avoid nested exceptions */
506 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
507 #endif /* __GNUC__ */
511 /**********************************************************************
512 * get_fpu_code
514 * Get the FPU exception code from the FPU status.
516 static inline DWORD get_fpu_code( const CONTEXT *context )
518 DWORD status = context->FloatSave.StatusWord;
520 if (status & 0x01) /* IE */
522 if (status & 0x40) /* SF */
523 return EXCEPTION_FLT_STACK_CHECK;
524 else
525 return EXCEPTION_FLT_INVALID_OPERATION;
527 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
528 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
529 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
530 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
531 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
532 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
536 /**********************************************************************
537 * do_segv
539 * Implementation of SIGSEGV handler.
541 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
543 EXCEPTION_RECORD rec;
544 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
546 #ifdef CR2_sig
547 /* we want the page-fault case to be fast */
548 if (trap_code == T_PAGEFLT)
549 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
550 #endif
552 rec.ExceptionRecord = NULL;
553 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
554 rec.ExceptionAddress = (LPVOID)context->Eip;
555 rec.NumberParameters = 0;
557 switch(trap_code)
559 case T_OFLOW: /* Overflow exception */
560 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
561 break;
562 case T_BOUND: /* Bound range exception */
563 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
564 break;
565 case T_PRIVINFLT: /* Invalid opcode exception */
566 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
567 break;
568 case T_STKFLT: /* Stack fault */
569 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
570 break;
571 case T_SEGNPFLT: /* Segment not present exception */
572 case T_PROTFLT: /* General protection fault */
573 case T_UNKNOWN: /* Unknown fault code */
574 if (INSTR_EmulateInstruction( context )) return;
575 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
576 break;
577 case T_PAGEFLT: /* Page fault */
578 #ifdef CR2_sig
579 rec.NumberParameters = 2;
580 rec.ExceptionInformation[0] = (err_code & 2) != 0;
581 rec.ExceptionInformation[0] = 0;
582 rec.ExceptionInformation[1] = (DWORD)cr2;
583 #endif /* CR2_sig */
584 rec.ExceptionCode = page_fault_code;
585 break;
586 case T_ALIGNFLT: /* Alignment check exception */
587 /* FIXME: pass through exception handler first? */
588 if (context->EFlags & 0x00040000)
590 /* Disable AC flag, return */
591 context->EFlags &= ~0x00040000;
592 return;
594 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
595 break;
596 default:
597 ERR( "Got unexpected trap %d\n", trap_code );
598 /* fall through */
599 case T_NMI: /* NMI interrupt */
600 case T_DNA: /* Device not available exception */
601 case T_DOUBLEFLT: /* Double fault exception */
602 case T_TSSFLT: /* Invalid TSS exception */
603 case T_RESERVED: /* Unknown exception */
604 case T_MCHK: /* Machine check exception */
605 #ifdef T_CACHEFLT
606 case T_CACHEFLT: /* Cache flush exception */
607 #endif
608 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
609 break;
611 EXC_RtlRaiseException( &rec, context );
615 /**********************************************************************
616 * do_trap
618 * Implementation of SIGTRAP handler.
620 static void do_trap( CONTEXT *context, int trap_code )
622 EXCEPTION_RECORD rec;
624 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
625 rec.ExceptionRecord = NULL;
626 rec.ExceptionAddress = (LPVOID)context->Eip;
627 rec.NumberParameters = 0;
629 switch(trap_code)
631 case T_TRCTRAP: /* Single-step exception */
632 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
633 context->EFlags &= ~0x100; /* clear single-step flag */
634 break;
635 case T_BPTFLT: /* Breakpoint exception */
636 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
637 /* fall through */
638 default:
639 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
640 break;
642 EXC_RtlRaiseException( &rec, context );
646 /**********************************************************************
647 * do_fpe
649 * Implementation of SIGFPE handler
651 static void do_fpe( CONTEXT *context, int trap_code )
653 EXCEPTION_RECORD rec;
655 switch(trap_code)
657 case T_DIVIDE: /* Division by zero exception */
658 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
659 break;
660 case T_FPOPFLT: /* Coprocessor segment overrun */
661 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
662 break;
663 case T_ARITHTRAP: /* Floating point exception */
664 case T_UNKNOWN: /* Unknown fault code */
665 rec.ExceptionCode = get_fpu_code( context );
666 break;
667 default:
668 ERR( "Got unexpected trap %d\n", trap_code );
669 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
670 break;
672 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
673 rec.ExceptionRecord = NULL;
674 rec.ExceptionAddress = (LPVOID)context->Eip;
675 rec.NumberParameters = 0;
676 EXC_RtlRaiseException( &rec, context );
680 /**********************************************************************
681 * segv_handler
683 * Handler for SIGSEGV and related errors.
685 static HANDLER_DEF(segv_handler)
687 CONTEXT context;
688 handler_init( &context, HANDLER_CONTEXT );
689 save_context( &context, HANDLER_CONTEXT );
690 do_segv( &context, get_trap_code(HANDLER_CONTEXT),
691 get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
692 restore_context( &context, HANDLER_CONTEXT );
696 /**********************************************************************
697 * trap_handler
699 * Handler for SIGTRAP.
701 static HANDLER_DEF(trap_handler)
703 CONTEXT context;
704 handler_init( &context, HANDLER_CONTEXT );
705 save_context( &context, HANDLER_CONTEXT );
706 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
707 restore_context( &context, HANDLER_CONTEXT );
711 /**********************************************************************
712 * fpe_handler
714 * Handler for SIGFPE.
716 static HANDLER_DEF(fpe_handler)
718 CONTEXT context;
719 handler_init( &context, HANDLER_CONTEXT );
720 save_fpu( &context, HANDLER_CONTEXT );
721 save_context( &context, HANDLER_CONTEXT );
722 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
723 restore_context( &context, HANDLER_CONTEXT );
724 restore_fpu( &context, HANDLER_CONTEXT );
728 /**********************************************************************
729 * int_handler
731 * Handler for SIGINT.
733 static HANDLER_DEF(int_handler)
735 EXCEPTION_RECORD rec;
736 CONTEXT context;
738 handler_init( &context, HANDLER_CONTEXT );
739 save_context( &context, HANDLER_CONTEXT );
740 rec.ExceptionCode = CONTROL_C_EXIT;
741 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
742 rec.ExceptionRecord = NULL;
743 rec.ExceptionAddress = (LPVOID)context.Eip;
744 rec.NumberParameters = 0;
745 EXC_RtlRaiseException( &rec, &context );
746 restore_context( &context, HANDLER_CONTEXT );
750 /***********************************************************************
751 * set_handler
753 * Set a signal handler
755 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
757 struct sigaction sig_act;
759 #ifdef linux
760 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
762 struct kernel_sigaction sig_act;
763 sig_act.ksa_handler = func;
764 sig_act.ksa_flags = SA_RESTART | SA_NOMASK;
765 sig_act.ksa_mask = 0;
766 /* point to the top of the stack */
767 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
768 return wine_sigaction( sig, &sig_act, NULL );
770 #endif /* linux */
771 sig_act.sa_handler = func;
772 sigemptyset( &sig_act.sa_mask );
774 #ifdef linux
775 sig_act.sa_flags = SA_RESTART | SA_NOMASK;
776 #elif defined (__svr4__) || defined(_SCO_DS)
777 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
778 #else
779 sig_act.sa_flags = 0;
780 #endif
782 #ifdef SA_ONSTACK
783 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
784 #endif
785 return sigaction( sig, &sig_act, NULL );
789 /**********************************************************************
790 * SIGNAL_Init
792 BOOL SIGNAL_Init(void)
794 int have_sigaltstack = 0;
796 #ifdef HAVE_SIGALTSTACK
797 struct sigaltstack ss;
798 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
800 ss.ss_size = SIGNAL_STACK_SIZE;
801 ss.ss_flags = 0;
802 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
803 #ifdef linux
804 /* sigaltstack may fail because the kernel is too old, or
805 because glibc is brain-dead. In the latter case a
806 direct system call should succeed. */
807 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
808 #endif /* linux */
810 #endif /* HAVE_SIGALTSTACK */
812 /* automatic child reaping to avoid zombies */
813 signal( SIGCHLD, SIG_IGN );
815 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
816 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
817 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
818 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
819 #ifdef SIGBUS
820 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
821 #endif
822 #ifdef SIGTRAP
823 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
824 #endif
825 return TRUE;
827 error:
828 perror("sigaction");
829 return FALSE;
833 #ifdef linux
834 /**********************************************************************
835 * __wine_enter_vm86
837 * Enter vm86 mode with the specified register context.
839 void __wine_enter_vm86( CONTEXT *context )
841 EXCEPTION_RECORD rec;
842 int res;
843 struct vm86plus_struct vm86;
845 for (;;)
847 vm86.regs.eax = context->Eax;
848 vm86.regs.ebx = context->Ebx;
849 vm86.regs.ecx = context->Ecx;
850 vm86.regs.edx = context->Edx;
851 vm86.regs.esi = context->Esi;
852 vm86.regs.edi = context->Edi;
853 vm86.regs.esp = context->Esp;
854 vm86.regs.ebp = context->Ebp;
855 vm86.regs.eip = context->Eip;
856 vm86.regs.cs = context->SegCs;
857 vm86.regs.ds = context->SegDs;
858 vm86.regs.es = context->SegEs;
859 vm86.regs.fs = context->SegFs;
860 vm86.regs.gs = context->SegGs;
861 vm86.regs.eflags = context->EFlags;
862 if (vm86.regs.eflags & IF_MASK) vm86.regs.eflags |= VIF_MASK;
864 res = wine_vm86plus( VM86_ENTER, &vm86 );
866 context->Eax = vm86.regs.eax;
867 context->Ebx = vm86.regs.ebx;
868 context->Ecx = vm86.regs.ecx;
869 context->Edx = vm86.regs.edx;
870 context->Esi = vm86.regs.esi;
871 context->Edi = vm86.regs.edi;
872 context->Esp = vm86.regs.esp;
873 context->Ebp = vm86.regs.ebp;
874 context->Eip = vm86.regs.eip;
875 context->SegCs = vm86.regs.cs;
876 context->SegDs = vm86.regs.ds;
877 context->SegEs = vm86.regs.es;
878 context->SegFs = vm86.regs.fs;
879 context->SegGs = vm86.regs.gs;
880 context->EFlags = vm86.regs.eflags;
882 switch(VM86_TYPE(res))
884 case VM86_SIGNAL: /* return due to signal */
885 switch(VM86_ARG(res))
887 case SIGILL:
888 case SIGSEGV:
889 #ifdef SIGBUS
890 case SIGBUS:
891 #endif
892 do_segv( context, T_UNKNOWN, 0, 0 );
893 continue;
894 case SIGTRAP:
895 do_trap( context, T_UNKNOWN );
896 continue;
897 case SIGFPE:
898 do_fpe( context, T_UNKNOWN );
899 continue;
901 rec.ExceptionCode = EXCEPTION_VM86_SIGNAL;
902 break;
903 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
904 do_segv( context, T_PROTFLT, 0, 0 );
905 continue;
906 case VM86_TRAP: /* return due to DOS-debugger request */
907 do_trap( context, T_UNKNOWN );
908 continue;
909 case VM86_INTx: /* int3/int x instruction (ARG = x) */
910 rec.ExceptionCode = EXCEPTION_VM86_INTx;
911 break;
912 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
913 rec.ExceptionCode = EXCEPTION_VM86_STI;
914 break;
915 case VM86_PICRETURN: /* return due to pending PIC request */
916 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
917 break;
918 default:
919 ERR( "unhandled result from vm86 mode %x\n", res );
920 continue;
922 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
923 rec.ExceptionRecord = NULL;
924 rec.ExceptionAddress = (LPVOID)context->Eip;
925 rec.NumberParameters = 1;
926 rec.ExceptionInformation[0] = VM86_ARG(res);
927 EXC_RtlRaiseException( &rec, context );
931 #else /* linux */
932 void __wine_enter_vm86( CONTEXT *context )
934 MESSAGE("vm86 mode not supported on this platform\n");
936 #endif /* linux */
938 /**********************************************************************
939 * DbgBreakPoint (NTDLL)
941 void WINAPI DbgBreakPoint(void);
942 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
944 /**********************************************************************
945 * DbgUserBreakPoint (NTDLL)
947 void WINAPI DbgUserBreakPoint(void);
948 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
950 #endif /* __i386__ */