Fixed DebugBreakProcess (now generates EXCEPTION_BREAKPOINT instead of
[wine.git] / dlls / ntdll / signal_i386.c
blob199d845c1dd55dfdc3eead61cb2c51723bb4875d
1 /*
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
21 #ifdef __i386__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 #endif
35 #ifdef HAVE_SYSCALL_H
36 # include <syscall.h>
37 #else
38 # ifdef HAVE_SYS_SYSCALL_H
39 # include <sys/syscall.h>
40 # endif
41 #endif
43 #ifdef HAVE_SYS_VM86_H
44 # include <sys/vm86.h>
45 #endif
47 #ifdef HAVE_SYS_SIGNAL_H
48 # include <sys/signal.h>
49 #endif
51 #include "ntddk.h"
52 #include "winnt.h"
53 #include "wine/library.h"
55 #include "selectors.h"
57 /***********************************************************************
58 * signal context platform-specific definitions
61 #ifdef linux
62 typedef struct
64 unsigned short sc_gs, __gsh;
65 unsigned short sc_fs, __fsh;
66 unsigned short sc_es, __esh;
67 unsigned short sc_ds, __dsh;
68 unsigned long sc_edi;
69 unsigned long sc_esi;
70 unsigned long sc_ebp;
71 unsigned long sc_esp;
72 unsigned long sc_ebx;
73 unsigned long sc_edx;
74 unsigned long sc_ecx;
75 unsigned long sc_eax;
76 unsigned long sc_trapno;
77 unsigned long sc_err;
78 unsigned long sc_eip;
79 unsigned short sc_cs, __csh;
80 unsigned long sc_eflags;
81 unsigned long esp_at_signal;
82 unsigned short sc_ss, __ssh;
83 unsigned long i387;
84 unsigned long oldmask;
85 unsigned long cr2;
86 } SIGCONTEXT;
88 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
89 #define HANDLER_CONTEXT (&__context)
91 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
92 struct kernel_sigaction
94 void (*ksa_handler)();
95 unsigned long ksa_mask;
96 unsigned long ksa_flags;
97 void *ksa_restorer;
100 /* Similar to the sigaction function in libc, except it leaves alone the
101 restorer field, which is used to specify the signal stack address */
102 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
103 struct kernel_sigaction *old )
105 __asm__ __volatile__( "pushl %%ebx\n\t"
106 "movl %2,%%ebx\n\t"
107 "int $0x80\n\t"
108 "popl %%ebx"
109 : "=a" (sig)
110 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
111 if (sig>=0) return 0;
112 errno = -sig;
113 return -1;
116 #ifdef HAVE_SIGALTSTACK
117 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
118 static inline int wine_sigaltstack( const struct sigaltstack *new,
119 struct sigaltstack *old )
121 int ret;
122 __asm__ __volatile__( "pushl %%ebx\n\t"
123 "movl %2,%%ebx\n\t"
124 "int $0x80\n\t"
125 "popl %%ebx"
126 : "=a" (ret)
127 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
128 if (ret >= 0) return 0;
129 errno = -ret;
130 return -1;
132 #endif
134 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
136 int vm86_enter( void **vm86_ptr );
137 void vm86_return(void);
138 void vm86_return_end(void);
139 __ASM_GLOBAL_FUNC(vm86_enter,
140 "pushl %ebp\n\t"
141 "movl %esp, %ebp\n\t"
142 "movl $166,%eax\n\t" /*SYS_vm86*/
143 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
144 "movl (%ecx),%ecx\n\t"
145 "pushl %ebx\n\t"
146 "movl $1,%ebx\n\t" /*VM86_ENTER*/
147 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
148 "pushl %fs\n\t"
149 "int $0x80\n"
150 ".globl " __ASM_NAME("vm86_return") "\n\t"
151 ".type " __ASM_NAME("vm86_return") ",@function\n"
152 __ASM_NAME("vm86_return") ":\n\t"
153 "popl %fs\n\t"
154 "popl %ecx\n\t"
155 "popl %ebx\n\t"
156 "popl %ebp\n\t"
157 "testl %eax,%eax\n\t"
158 "jl 0f\n\t"
159 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
160 "je " __ASM_NAME("vm86_enter") "\n\t"
161 "0:\n\t"
162 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
163 "movl $0,(%ecx)\n\t"
164 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
165 ".type " __ASM_NAME("vm86_return_end") ",@function\n"
166 __ASM_NAME("vm86_return_end") ":\n\t"
167 "ret" );
169 #define __HAVE_VM86
171 #endif /* linux */
173 #ifdef BSDI
175 #define EAX_sig(context) ((context)->tf_eax)
176 #define EBX_sig(context) ((context)->tf_ebx)
177 #define ECX_sig(context) ((context)->tf_ecx)
178 #define EDX_sig(context) ((context)->tf_edx)
179 #define ESI_sig(context) ((context)->tf_esi)
180 #define EDI_sig(context) ((context)->tf_edi)
181 #define EBP_sig(context) ((context)->tf_ebp)
183 #define CS_sig(context) ((context)->tf_cs)
184 #define DS_sig(context) ((context)->tf_ds)
185 #define ES_sig(context) ((context)->tf_es)
186 #define SS_sig(context) ((context)->tf_ss)
188 #include <machine/frame.h>
189 typedef struct trapframe SIGCONTEXT;
191 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
192 #define HANDLER_CONTEXT __context
194 #define EFL_sig(context) ((context)->tf_eflags)
196 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
197 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
199 #endif /* bsdi */
201 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
203 typedef struct sigcontext SIGCONTEXT;
205 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
206 #define HANDLER_CONTEXT __context
208 #endif /* FreeBSD */
210 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
212 #ifdef _SCO_DS
213 #include <sys/regset.h>
214 #endif
215 /* Solaris kludge */
216 #undef ERR
217 #include <sys/ucontext.h>
218 #undef ERR
219 typedef struct ucontext SIGCONTEXT;
221 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
222 #define HANDLER_CONTEXT __context
224 #endif /* svr4 || SCO_DS */
226 #ifdef __EMX__
228 typedef struct
230 unsigned long ContextFlags;
231 FLOATING_SAVE_AREA sc_float;
232 unsigned long sc_gs;
233 unsigned long sc_fs;
234 unsigned long sc_es;
235 unsigned long sc_ds;
236 unsigned long sc_edi;
237 unsigned long sc_esi;
238 unsigned long sc_eax;
239 unsigned long sc_ebx;
240 unsigned long sc_ecx;
241 unsigned long sc_edx;
242 unsigned long sc_ebp;
243 unsigned long sc_eip;
244 unsigned long sc_cs;
245 unsigned long sc_eflags;
246 unsigned long sc_esp;
247 unsigned long sc_ss;
248 } SIGCONTEXT;
250 #endif /* __EMX__ */
253 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
255 #define EAX_sig(context) ((context)->sc_eax)
256 #define EBX_sig(context) ((context)->sc_ebx)
257 #define ECX_sig(context) ((context)->sc_ecx)
258 #define EDX_sig(context) ((context)->sc_edx)
259 #define ESI_sig(context) ((context)->sc_esi)
260 #define EDI_sig(context) ((context)->sc_edi)
261 #define EBP_sig(context) ((context)->sc_ebp)
263 #define CS_sig(context) ((context)->sc_cs)
264 #define DS_sig(context) ((context)->sc_ds)
265 #define ES_sig(context) ((context)->sc_es)
266 #define SS_sig(context) ((context)->sc_ss)
268 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
269 * saved by the exception handling. duh.
270 * Actually they are in -current (have been for a while), and that
271 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
272 * If you're running a system from the -stable branch older than that,
273 * like a 3.3-RELEASE, grab the patch from the ports tree:
274 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
275 * (If its not yet there when you look, go here:
276 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
278 #ifdef __FreeBSD__
279 #define FS_sig(context) ((context)->sc_fs)
280 #define GS_sig(context) ((context)->sc_gs)
281 #endif
283 #ifdef linux
284 #define FS_sig(context) ((context)->sc_fs)
285 #define GS_sig(context) ((context)->sc_gs)
286 #define CR2_sig(context) ((context)->cr2)
287 #define TRAP_sig(context) ((context)->sc_trapno)
288 #define ERROR_sig(context) ((context)->sc_err)
289 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
290 #endif
292 #ifndef __FreeBSD__
293 #define EFL_sig(context) ((context)->sc_eflags)
294 #else
295 #define EFL_sig(context) ((context)->sc_efl)
296 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
297 #define CR2_sig(context) ((context)->sc_err)
298 #define TRAP_sig(context) ((context)->sc_trapno)
299 #endif
301 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
302 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
304 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
306 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
308 #ifdef _SCO_DS
309 #define gregs regs
310 #endif
312 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
313 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
314 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
315 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
316 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
317 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
318 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
320 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
321 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
322 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
323 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
325 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
326 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
328 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
330 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
331 #ifdef R_ESP
332 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
333 #else
334 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
335 #endif
336 #ifdef TRAPNO
337 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
338 #endif
340 #endif /* svr4 || SCO_DS */
343 /* exception code definitions (already defined by FreeBSD/NetBSD) */
344 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
345 #define T_DIVIDE 0 /* Division by zero exception */
346 #define T_TRCTRAP 1 /* Single-step exception */
347 #define T_NMI 2 /* NMI interrupt */
348 #define T_BPTFLT 3 /* Breakpoint exception */
349 #define T_OFLOW 4 /* Overflow exception */
350 #define T_BOUND 5 /* Bound range exception */
351 #define T_PRIVINFLT 6 /* Invalid opcode exception */
352 #define T_DNA 7 /* Device not available exception */
353 #define T_DOUBLEFLT 8 /* Double fault exception */
354 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
355 #define T_TSSFLT 10 /* Invalid TSS exception */
356 #define T_SEGNPFLT 11 /* Segment not present exception */
357 #define T_STKFLT 12 /* Stack fault */
358 #define T_PROTFLT 13 /* General protection fault */
359 #define T_PAGEFLT 14 /* Page fault */
360 #define T_RESERVED 15 /* Unknown exception */
361 #define T_ARITHTRAP 16 /* Floating point exception */
362 #define T_ALIGNFLT 17 /* Alignment check exception */
363 #define T_MCHK 18 /* Machine check exception */
364 #define T_CACHEFLT 19 /* Cache flush exception */
365 #endif
366 #if defined(__NetBSD__)
367 #define T_MCHK 19 /* Machine check exception */
368 #endif
370 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
372 #include "wine/exception.h"
373 #include "winnt.h"
374 #include "stackframe.h"
375 #include "global.h"
376 #include "miscemu.h"
377 #include "ntddk.h"
378 #include "syslevel.h"
379 #include "wine/debug.h"
381 WINE_DEFAULT_DEBUG_CHANNEL(seh);
383 static sigset_t all_sigs;
386 /***********************************************************************
387 * get_trap_code
389 * Get the trap code for a signal.
391 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
393 #ifdef TRAP_sig
394 return TRAP_sig(sigcontext);
395 #else
396 return T_UNKNOWN; /* unknown trap code */
397 #endif
400 /***********************************************************************
401 * get_error_code
403 * Get the error code for a signal.
405 static inline int get_error_code( const SIGCONTEXT *sigcontext )
407 #ifdef ERROR_sig
408 return ERROR_sig(sigcontext);
409 #else
410 return 0;
411 #endif
414 /***********************************************************************
415 * get_cr2_value
417 * Get the CR2 value for a signal.
419 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
421 #ifdef CR2_sig
422 return (void *)CR2_sig(sigcontext);
423 #else
424 return NULL;
425 #endif
429 #ifdef __HAVE_VM86
430 /***********************************************************************
431 * save_vm86_context
433 * Set the register values from a vm86 structure.
435 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
437 context->Eax = vm86->regs.eax;
438 context->Ebx = vm86->regs.ebx;
439 context->Ecx = vm86->regs.ecx;
440 context->Edx = vm86->regs.edx;
441 context->Esi = vm86->regs.esi;
442 context->Edi = vm86->regs.edi;
443 context->Esp = vm86->regs.esp;
444 context->Ebp = vm86->regs.ebp;
445 context->Eip = vm86->regs.eip;
446 context->SegCs = vm86->regs.cs;
447 context->SegDs = vm86->regs.ds;
448 context->SegEs = vm86->regs.es;
449 context->SegFs = vm86->regs.fs;
450 context->SegGs = vm86->regs.gs;
451 context->SegSs = vm86->regs.ss;
452 context->EFlags = vm86->regs.eflags;
456 /***********************************************************************
457 * restore_vm86_context
459 * Build a vm86 structure from the register values.
461 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
463 vm86->regs.eax = context->Eax;
464 vm86->regs.ebx = context->Ebx;
465 vm86->regs.ecx = context->Ecx;
466 vm86->regs.edx = context->Edx;
467 vm86->regs.esi = context->Esi;
468 vm86->regs.edi = context->Edi;
469 vm86->regs.esp = context->Esp;
470 vm86->regs.ebp = context->Ebp;
471 vm86->regs.eip = context->Eip;
472 vm86->regs.cs = context->SegCs;
473 vm86->regs.ds = context->SegDs;
474 vm86->regs.es = context->SegEs;
475 vm86->regs.fs = context->SegFs;
476 vm86->regs.gs = context->SegGs;
477 vm86->regs.ss = context->SegSs;
478 vm86->regs.eflags = context->EFlags;
480 #endif /* __HAVE_VM86 */
483 /***********************************************************************
484 * save_context
486 * Set the register values from a sigcontext.
488 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
490 WORD fs;
491 /* get %fs at time of the fault */
492 #ifdef FS_sig
493 fs = FS_sig(sigcontext);
494 #else
495 fs = wine_get_fs();
496 #endif
497 context->SegFs = fs;
499 /* now restore a proper %fs for the fault handler */
500 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) /* 16-bit mode */
502 fs = SYSLEVEL_Win16CurrentTeb;
504 #ifdef __HAVE_VM86
505 else if ((void *)EIP_sig(sigcontext) == vm86_return) /* vm86 mode */
507 /* fetch the saved %fs on the stack */
508 fs = *(unsigned int *)ESP_sig(sigcontext);
509 if (EAX_sig(sigcontext) == VM86_EAX) {
510 struct vm86plus_struct *vm86;
511 wine_set_fs(fs);
512 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
513 * (but we could also get if from teb->vm86_ptr) */
514 vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
515 /* get context from vm86 struct */
516 save_vm86_context( context, vm86 );
517 return;
520 #endif /* __HAVE_VM86 */
522 wine_set_fs(fs);
524 context->Eax = EAX_sig(sigcontext);
525 context->Ebx = EBX_sig(sigcontext);
526 context->Ecx = ECX_sig(sigcontext);
527 context->Edx = EDX_sig(sigcontext);
528 context->Esi = ESI_sig(sigcontext);
529 context->Edi = EDI_sig(sigcontext);
530 context->Ebp = EBP_sig(sigcontext);
531 context->EFlags = EFL_sig(sigcontext);
532 context->Eip = EIP_sig(sigcontext);
533 context->Esp = ESP_sig(sigcontext);
534 context->SegCs = LOWORD(CS_sig(sigcontext));
535 context->SegDs = LOWORD(DS_sig(sigcontext));
536 context->SegEs = LOWORD(ES_sig(sigcontext));
537 context->SegSs = LOWORD(SS_sig(sigcontext));
538 #ifdef GS_sig
539 context->SegGs = LOWORD(GS_sig(sigcontext));
540 #else
541 context->SegGs = wine_get_gs();
542 #endif
546 /***********************************************************************
547 * restore_context
549 * Build a sigcontext from the register values.
551 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
553 #ifdef __HAVE_VM86
554 /* check if exception occurred in vm86 mode */
555 if ((void *)EIP_sig(sigcontext) == vm86_return &&
556 IS_SELECTOR_SYSTEM(CS_sig(sigcontext)) &&
557 EAX_sig(sigcontext) == VM86_EAX)
559 /* retrieve pointer to vm86plus struct that was stored in vm86_enter
560 * (but we could also get it from teb->vm86_ptr) */
561 struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
562 restore_vm86_context( context, vm86 );
563 return;
565 #endif /* __HAVE_VM86 */
567 EAX_sig(sigcontext) = context->Eax;
568 EBX_sig(sigcontext) = context->Ebx;
569 ECX_sig(sigcontext) = context->Ecx;
570 EDX_sig(sigcontext) = context->Edx;
571 ESI_sig(sigcontext) = context->Esi;
572 EDI_sig(sigcontext) = context->Edi;
573 EBP_sig(sigcontext) = context->Ebp;
574 EFL_sig(sigcontext) = context->EFlags;
575 EIP_sig(sigcontext) = context->Eip;
576 ESP_sig(sigcontext) = context->Esp;
577 CS_sig(sigcontext) = context->SegCs;
578 DS_sig(sigcontext) = context->SegDs;
579 ES_sig(sigcontext) = context->SegEs;
580 SS_sig(sigcontext) = context->SegSs;
581 #ifdef FS_sig
582 FS_sig(sigcontext) = context->SegFs;
583 #else
584 wine_set_fs( context->SegFs );
585 #endif
586 #ifdef GS_sig
587 GS_sig(sigcontext) = context->SegGs;
588 #else
589 wine_set_gs( context->SegGs );
590 #endif
594 /***********************************************************************
595 * save_fpu
597 * Set the FPU context from a sigcontext.
599 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
601 #ifdef FPU_sig
602 if (FPU_sig(sigcontext))
604 context->FloatSave = *FPU_sig(sigcontext);
605 return;
607 #endif /* FPU_sig */
608 #ifdef __GNUC__
609 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
610 #endif /* __GNUC__ */
614 /***********************************************************************
615 * restore_fpu
617 * Restore the FPU context to a sigcontext.
619 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
621 /* reset the current interrupt status */
622 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
623 #ifdef FPU_sig
624 if (FPU_sig(sigcontext))
626 *FPU_sig(sigcontext) = context->FloatSave;
627 return;
629 #endif /* FPU_sig */
630 #ifdef __GNUC__
631 /* avoid nested exceptions */
632 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
633 #endif /* __GNUC__ */
637 /**********************************************************************
638 * get_fpu_code
640 * Get the FPU exception code from the FPU status.
642 static inline DWORD get_fpu_code( const CONTEXT *context )
644 DWORD status = context->FloatSave.StatusWord;
646 if (status & 0x01) /* IE */
648 if (status & 0x40) /* SF */
649 return EXCEPTION_FLT_STACK_CHECK;
650 else
651 return EXCEPTION_FLT_INVALID_OPERATION;
653 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
654 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
655 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
656 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
657 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
658 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
662 /***********************************************************************
663 * SIGNAL_Unblock
665 * Unblock signals. Called from EXC_RtlRaiseException.
667 void SIGNAL_Unblock( void )
669 sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
673 /**********************************************************************
674 * do_segv
676 * Implementation of SIGSEGV handler.
678 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
680 EXCEPTION_RECORD rec;
681 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
683 #ifdef CR2_sig
684 /* we want the page-fault case to be fast */
685 if (trap_code == T_PAGEFLT)
686 if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
687 #endif
689 rec.ExceptionRecord = NULL;
690 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
691 rec.ExceptionAddress = (LPVOID)context->Eip;
692 rec.NumberParameters = 0;
694 switch(trap_code)
696 case T_OFLOW: /* Overflow exception */
697 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
698 break;
699 case T_BOUND: /* Bound range exception */
700 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
701 break;
702 case T_PRIVINFLT: /* Invalid opcode exception */
703 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
704 break;
705 case T_STKFLT: /* Stack fault */
706 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
707 break;
708 case T_SEGNPFLT: /* Segment not present exception */
709 case T_PROTFLT: /* General protection fault */
710 case T_UNKNOWN: /* Unknown fault code */
711 if (INSTR_EmulateInstruction( context )) return;
712 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
713 break;
714 case T_PAGEFLT: /* Page fault */
715 #ifdef CR2_sig
716 rec.NumberParameters = 2;
717 rec.ExceptionInformation[0] = (err_code & 2) != 0;
718 rec.ExceptionInformation[1] = (DWORD)cr2;
719 #endif /* CR2_sig */
720 rec.ExceptionCode = page_fault_code;
721 break;
722 case T_ALIGNFLT: /* Alignment check exception */
723 /* FIXME: pass through exception handler first? */
724 if (context->EFlags & 0x00040000)
726 /* Disable AC flag, return */
727 context->EFlags &= ~0x00040000;
728 return;
730 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
731 break;
732 default:
733 ERR( "Got unexpected trap %d\n", trap_code );
734 /* fall through */
735 case T_NMI: /* NMI interrupt */
736 case T_DNA: /* Device not available exception */
737 case T_DOUBLEFLT: /* Double fault exception */
738 case T_TSSFLT: /* Invalid TSS exception */
739 case T_RESERVED: /* Unknown exception */
740 case T_MCHK: /* Machine check exception */
741 #ifdef T_CACHEFLT
742 case T_CACHEFLT: /* Cache flush exception */
743 #endif
744 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
745 break;
747 EXC_RtlRaiseException( &rec, context );
751 /**********************************************************************
752 * do_trap
754 * Implementation of SIGTRAP handler.
756 static void do_trap( CONTEXT *context, int trap_code )
758 EXCEPTION_RECORD rec;
760 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
761 rec.ExceptionRecord = NULL;
762 rec.ExceptionAddress = (LPVOID)context->Eip;
763 rec.NumberParameters = 0;
765 switch(trap_code)
767 case T_TRCTRAP: /* Single-step exception */
768 if (context->EFlags & 0x100)
770 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
771 context->EFlags &= ~0x100; /* clear single-step flag */
773 else
775 /* likely we get this because of a kill(SIGTRAP) on ourself,
776 * so send a bp exception instead of a single step exception
778 TRACE("Spurious single step trap => breakpoint simulation\n");
779 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
781 break;
782 case T_BPTFLT: /* Breakpoint exception */
783 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1; /* back up over the int3 instruction */
784 /* fall through */
785 default:
786 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
787 break;
789 EXC_RtlRaiseException( &rec, context );
793 /**********************************************************************
794 * do_fpe
796 * Implementation of SIGFPE handler
798 static void do_fpe( CONTEXT *context, int trap_code )
800 EXCEPTION_RECORD rec;
802 switch(trap_code)
804 case T_DIVIDE: /* Division by zero exception */
805 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
806 break;
807 case T_FPOPFLT: /* Coprocessor segment overrun */
808 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
809 break;
810 case T_ARITHTRAP: /* Floating point exception */
811 case T_UNKNOWN: /* Unknown fault code */
812 rec.ExceptionCode = get_fpu_code( context );
813 break;
814 default:
815 ERR( "Got unexpected trap %d\n", trap_code );
816 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
817 break;
819 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
820 rec.ExceptionRecord = NULL;
821 rec.ExceptionAddress = (LPVOID)context->Eip;
822 rec.NumberParameters = 0;
823 EXC_RtlRaiseException( &rec, context );
827 #ifdef __HAVE_VM86
828 /**********************************************************************
829 * set_vm86_pend
831 * Handler for SIGUSR2, which we use to set the vm86 pending flag.
833 static void set_vm86_pend( CONTEXT *context )
835 EXCEPTION_RECORD rec;
836 TEB *teb = NtCurrentTeb();
837 struct vm86plus_struct *vm86 = (struct vm86plus_struct*)(teb->vm86_ptr);
839 rec.ExceptionCode = EXCEPTION_VM86_STI;
840 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
841 rec.ExceptionRecord = NULL;
842 rec.NumberParameters = 1;
843 rec.ExceptionInformation[0] = 0;
845 /* __wine_enter_vm86() merges the vm86_pending flag in safely */
846 teb->vm86_pending |= VIP_MASK;
847 /* see if we were in VM86 mode */
848 if (context->EFlags & 0x00020000)
850 /* seems so, also set flag in signal context */
851 if (context->EFlags & VIP_MASK) return;
852 context->EFlags |= VIP_MASK;
853 vm86->regs.eflags |= VIP_MASK; /* no exception recursion */
854 if (context->EFlags & VIF_MASK) {
855 /* VIF is set, throw exception */
856 teb->vm86_pending = 0;
857 teb->vm86_ptr = NULL;
858 rec.ExceptionAddress = (LPVOID)context->Eip;
859 EXC_RtlRaiseException( &rec, context );
860 teb->vm86_ptr = vm86;
863 else if (vm86)
865 /* not in VM86, but possibly setting up for it */
866 if (vm86->regs.eflags & VIP_MASK) return;
867 vm86->regs.eflags |= VIP_MASK;
868 if (((char*)context->Eip >= (char*)vm86_return) &&
869 ((char*)context->Eip <= (char*)vm86_return_end) &&
870 (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
871 /* exiting from VM86, can't throw */
872 return;
874 if (vm86->regs.eflags & VIF_MASK) {
875 /* VIF is set, throw exception */
876 CONTEXT vcontext;
877 teb->vm86_pending = 0;
878 teb->vm86_ptr = NULL;
879 save_vm86_context( &vcontext, vm86 );
880 rec.ExceptionAddress = (LPVOID)vcontext.Eip;
881 EXC_RtlRaiseException( &rec, &vcontext );
882 teb->vm86_ptr = vm86;
883 restore_vm86_context( &vcontext, vm86 );
889 /**********************************************************************
890 * usr2_handler
892 * Handler for SIGUSR2.
893 * We use it to signal that the running __wine_enter_vm86() should
894 * immediately set VIP_MASK, causing pending events to be handled
895 * as early as possible.
897 static HANDLER_DEF(usr2_handler)
899 CONTEXT context;
901 save_context( &context, HANDLER_CONTEXT );
902 set_vm86_pend( &context );
903 restore_context( &context, HANDLER_CONTEXT );
907 /**********************************************************************
908 * alrm_handler
910 * Handler for SIGALRM.
911 * Increases the alarm counter and sets the vm86 pending flag.
913 static HANDLER_DEF(alrm_handler)
915 CONTEXT context;
917 save_context( &context, HANDLER_CONTEXT );
918 NtCurrentTeb()->alarms++;
919 set_vm86_pend( &context );
920 restore_context( &context, HANDLER_CONTEXT );
922 #endif /* __HAVE_VM86 */
925 /**********************************************************************
926 * segv_handler
928 * Handler for SIGSEGV and related errors.
930 static HANDLER_DEF(segv_handler)
932 CONTEXT context;
933 save_context( &context, HANDLER_CONTEXT );
934 do_segv( &context, get_trap_code(HANDLER_CONTEXT),
935 get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
936 restore_context( &context, HANDLER_CONTEXT );
940 /**********************************************************************
941 * trap_handler
943 * Handler for SIGTRAP.
945 static HANDLER_DEF(trap_handler)
947 CONTEXT context;
948 save_context( &context, HANDLER_CONTEXT );
949 do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
950 restore_context( &context, HANDLER_CONTEXT );
954 /**********************************************************************
955 * fpe_handler
957 * Handler for SIGFPE.
959 static HANDLER_DEF(fpe_handler)
961 CONTEXT context;
962 save_fpu( &context, HANDLER_CONTEXT );
963 save_context( &context, HANDLER_CONTEXT );
964 do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
965 restore_context( &context, HANDLER_CONTEXT );
966 restore_fpu( &context, HANDLER_CONTEXT );
970 /**********************************************************************
971 * int_handler
973 * Handler for SIGINT.
975 static HANDLER_DEF(int_handler)
977 extern int CONSOLE_HandleCtrlC(void);
978 if (!CONSOLE_HandleCtrlC())
980 EXCEPTION_RECORD rec;
981 CONTEXT context;
983 save_context( &context, HANDLER_CONTEXT );
984 rec.ExceptionCode = CONTROL_C_EXIT;
985 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
986 rec.ExceptionRecord = NULL;
987 rec.ExceptionAddress = (LPVOID)context.Eip;
988 rec.NumberParameters = 0;
989 EXC_RtlRaiseException( &rec, &context );
990 restore_context( &context, HANDLER_CONTEXT );
995 /***********************************************************************
996 * set_handler
998 * Set a signal handler
1000 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
1002 struct sigaction sig_act;
1004 #ifdef linux
1005 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
1007 struct kernel_sigaction sig_act;
1008 sig_act.ksa_handler = func;
1009 sig_act.ksa_flags = SA_RESTART;
1010 sig_act.ksa_mask = (1 << (SIGINT-1)) |
1011 (1 << (SIGALRM-1));
1012 /* point to the top of the stack */
1013 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
1014 return wine_sigaction( sig, &sig_act, NULL );
1016 #endif /* linux */
1017 sig_act.sa_handler = func;
1018 sigemptyset( &sig_act.sa_mask );
1019 sigaddset( &sig_act.sa_mask, SIGINT );
1020 sigaddset( &sig_act.sa_mask, SIGALRM );
1022 #ifdef linux
1023 sig_act.sa_flags = SA_RESTART;
1024 #elif defined (__svr4__) || defined(_SCO_DS)
1025 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1026 #else
1027 sig_act.sa_flags = 0;
1028 #endif
1030 #ifdef SA_ONSTACK
1031 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1032 #endif
1033 return sigaction( sig, &sig_act, NULL );
1037 /**********************************************************************
1038 * SIGNAL_Init
1040 BOOL SIGNAL_Init(void)
1042 int have_sigaltstack = 0;
1044 #ifdef HAVE_SIGALTSTACK
1045 struct sigaltstack ss;
1046 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
1048 ss.ss_size = SIGNAL_STACK_SIZE;
1049 ss.ss_flags = 0;
1050 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1051 #ifdef linux
1052 /* sigaltstack may fail because the kernel is too old, or
1053 because glibc is brain-dead. In the latter case a
1054 direct system call should succeed. */
1055 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1056 #endif /* linux */
1058 #endif /* HAVE_SIGALTSTACK */
1060 sigfillset( &all_sigs );
1062 /* automatic child reaping to avoid zombies */
1063 signal( SIGCHLD, SIG_IGN );
1065 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1066 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1067 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1068 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1069 #ifdef SIGBUS
1070 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1071 #endif
1072 #ifdef SIGTRAP
1073 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1074 #endif
1076 #ifdef __HAVE_VM86
1077 if (set_handler( SIGALRM, have_sigaltstack, (void (*)())alrm_handler ) == -1) goto error;
1078 if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1079 #endif
1081 return TRUE;
1083 error:
1084 perror("sigaction");
1085 return FALSE;
1089 /**********************************************************************
1090 * SIGNAL_Reset
1092 void SIGNAL_Reset(void)
1094 sigset_t block_set;
1096 /* block the async signals */
1097 sigemptyset( &block_set );
1098 sigaddset( &block_set, SIGALRM );
1099 sigaddset( &block_set, SIGIO );
1100 sigaddset( &block_set, SIGHUP );
1101 sigaddset( &block_set, SIGUSR2 );
1102 sigprocmask( SIG_BLOCK, &block_set, NULL );
1104 /* restore default handlers */
1105 signal( SIGINT, SIG_DFL );
1106 signal( SIGFPE, SIG_DFL );
1107 signal( SIGSEGV, SIG_DFL );
1108 signal( SIGILL, SIG_DFL );
1109 #ifdef SIGBUS
1110 signal( SIGBUS, SIG_DFL );
1111 #endif
1112 #ifdef SIGTRAP
1113 signal( SIGTRAP, SIG_DFL );
1114 #endif
1118 #ifdef __HAVE_VM86
1119 /**********************************************************************
1120 * __wine_enter_vm86
1122 * Enter vm86 mode with the specified register context.
1124 void __wine_enter_vm86( CONTEXT *context )
1126 EXCEPTION_RECORD rec;
1127 TEB *teb = NtCurrentTeb();
1128 int res;
1129 struct vm86plus_struct vm86;
1131 memset( &vm86, 0, sizeof(vm86) );
1132 for (;;)
1134 restore_vm86_context( context, &vm86 );
1135 /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1136 * so save it on entry, just in case */
1137 teb->vm86_pending |= (context->EFlags & VIP_MASK);
1138 /* Work around race conditions with signal handler
1139 * (avoiding sigprocmask for performance reasons) */
1140 teb->vm86_ptr = &vm86;
1141 vm86.regs.eflags |= teb->vm86_pending;
1142 /* Check for VIF|VIP here, since vm86_enter doesn't */
1143 if ((vm86.regs.eflags & (VIF_MASK|VIP_MASK)) == (VIF_MASK|VIP_MASK)) {
1144 teb->vm86_ptr = NULL;
1145 teb->vm86_pending = 0;
1146 context->EFlags |= VIP_MASK;
1147 rec.ExceptionCode = EXCEPTION_VM86_STI;
1148 rec.ExceptionInformation[0] = 0;
1149 goto cancel_vm86;
1154 res = vm86_enter( &teb->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1155 if (res < 0)
1157 errno = -res;
1158 return;
1160 } while (VM86_TYPE(res) == VM86_SIGNAL);
1162 save_vm86_context( context, &vm86 );
1163 context->EFlags |= teb->vm86_pending;
1165 switch(VM86_TYPE(res))
1167 case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1168 do_segv( context, T_PROTFLT, 0, 0 );
1169 continue;
1170 case VM86_TRAP: /* return due to DOS-debugger request */
1171 do_trap( context, VM86_ARG(res) );
1172 continue;
1173 case VM86_INTx: /* int3/int x instruction (ARG = x) */
1174 rec.ExceptionCode = EXCEPTION_VM86_INTx;
1175 break;
1176 case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1177 teb->vm86_pending = 0;
1178 rec.ExceptionCode = EXCEPTION_VM86_STI;
1179 break;
1180 case VM86_PICRETURN: /* return due to pending PIC request */
1181 rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1182 break;
1183 default:
1184 ERR( "unhandled result from vm86 mode %x\n", res );
1185 continue;
1187 rec.ExceptionInformation[0] = VM86_ARG(res);
1188 cancel_vm86:
1189 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1190 rec.ExceptionRecord = NULL;
1191 rec.ExceptionAddress = (LPVOID)context->Eip;
1192 rec.NumberParameters = 1;
1193 EXC_RtlRaiseException( &rec, context );
1197 #else /* __HAVE_VM86 */
1198 void __wine_enter_vm86( CONTEXT *context )
1200 MESSAGE("vm86 mode not supported on this platform\n");
1202 #endif /* __HAVE_VM86 */
1204 /**********************************************************************
1205 * DbgBreakPoint (NTDLL.@)
1207 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1209 /**********************************************************************
1210 * DbgUserBreakPoint (NTDLL.@)
1212 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1214 #endif /* __i386__ */