Spelling fixes.
[wine.git] / dlls / ntdll / signal_i386.c
blob068de0459870af094d49d7661cb27e3318935b0b
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 #include "selectors.h"
30 /***********************************************************************
31 * signal context platform-specific definitions
34 #ifdef linux
35 typedef struct
37 unsigned short sc_gs, __gsh;
38 unsigned short sc_fs, __fsh;
39 unsigned short sc_es, __esh;
40 unsigned short sc_ds, __dsh;
41 unsigned long sc_edi;
42 unsigned long sc_esi;
43 unsigned long sc_ebp;
44 unsigned long sc_esp;
45 unsigned long sc_ebx;
46 unsigned long sc_edx;
47 unsigned long sc_ecx;
48 unsigned long sc_eax;
49 unsigned long sc_trapno;
50 unsigned long sc_err;
51 unsigned long sc_eip;
52 unsigned short sc_cs, __csh;
53 unsigned long sc_eflags;
54 unsigned long esp_at_signal;
55 unsigned short sc_ss, __ssh;
56 unsigned long i387;
57 unsigned long oldmask;
58 unsigned long cr2;
59 } SIGCONTEXT;
61 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
62 #define HANDLER_CONTEXT (&__context)
64 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
65 struct kernel_sigaction
67 void (*ksa_handler)();
68 unsigned long ksa_mask;
69 unsigned long ksa_flags;
70 void *ksa_restorer;
73 /* Similar to the sigaction function in libc, except it leaves alone the
74 restorer field, which is used to specify the signal stack address */
75 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
76 struct kernel_sigaction *old )
78 __asm__ __volatile__( "pushl %%ebx\n\t"
79 "movl %2,%%ebx\n\t"
80 "int $0x80\n\t"
81 "popl %%ebx"
82 : "=a" (sig)
83 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
84 if (sig>=0) return 0;
85 errno = -sig;
86 return -1;
89 #ifdef HAVE_SIGALTSTACK
90 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
91 static inline int wine_sigaltstack( const struct sigaltstack *new,
92 struct sigaltstack *old )
94 int ret;
95 __asm__ __volatile__( "pushl %%ebx\n\t"
96 "movl %2,%%ebx\n\t"
97 "int $0x80\n\t"
98 "popl %%ebx"
99 : "=a" (ret)
100 : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
101 if (ret >= 0) return 0;
102 errno = -ret;
103 return -1;
105 #endif
107 #endif /* linux */
109 #ifdef BSDI
111 #define EAX_sig(context) ((context)->tf_eax)
112 #define EBX_sig(context) ((context)->tf_ebx)
113 #define ECX_sig(context) ((context)->tf_ecx)
114 #define EDX_sig(context) ((context)->tf_edx)
115 #define ESI_sig(context) ((context)->tf_esi)
116 #define EDI_sig(context) ((context)->tf_edi)
117 #define EBP_sig(context) ((context)->tf_ebp)
119 #define CS_sig(context) ((context)->tf_cs)
120 #define DS_sig(context) ((context)->tf_ds)
121 #define ES_sig(context) ((context)->tf_es)
122 #define SS_sig(context) ((context)->tf_ss)
124 #include <machine/frame.h>
125 typedef struct trapframe SIGCONTEXT;
127 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
128 #define HANDLER_CONTEXT __context
130 #define EFL_sig(context) ((context)->tf_eflags)
132 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
133 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
135 #endif /* bsdi */
137 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
139 typedef struct sigcontext SIGCONTEXT;
141 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
142 #define HANDLER_CONTEXT __context
144 #endif /* FreeBSD */
146 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
148 #ifdef _SCO_DS
149 #include <sys/regset.h>
150 #endif
151 /* Solaris kludge */
152 #undef ERR
153 #include <sys/ucontext.h>
154 #undef ERR
155 typedef struct ucontext SIGCONTEXT;
157 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
158 #define HANDLER_CONTEXT __context
160 #endif /* svr4 || SCO_DS */
162 #ifdef __EMX__
164 typedef struct
166 unsigned long ContextFlags;
167 FLOATING_SAVE_AREA sc_float;
168 unsigned long sc_gs;
169 unsigned long sc_fs;
170 unsigned long sc_es;
171 unsigned long sc_ds;
172 unsigned long sc_edi;
173 unsigned long sc_esi;
174 unsigned long sc_eax;
175 unsigned long sc_ebx;
176 unsigned long sc_ecx;
177 unsigned long sc_edx;
178 unsigned long sc_ebp;
179 unsigned long sc_eip;
180 unsigned long sc_cs;
181 unsigned long sc_eflags;
182 unsigned long sc_esp;
183 unsigned long sc_ss;
184 } SIGCONTEXT;
186 #endif /* __EMX__ */
189 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
191 #define EAX_sig(context) ((context)->sc_eax)
192 #define EBX_sig(context) ((context)->sc_ebx)
193 #define ECX_sig(context) ((context)->sc_ecx)
194 #define EDX_sig(context) ((context)->sc_edx)
195 #define ESI_sig(context) ((context)->sc_esi)
196 #define EDI_sig(context) ((context)->sc_edi)
197 #define EBP_sig(context) ((context)->sc_ebp)
199 #define CS_sig(context) ((context)->sc_cs)
200 #define DS_sig(context) ((context)->sc_ds)
201 #define ES_sig(context) ((context)->sc_es)
202 #define SS_sig(context) ((context)->sc_ss)
204 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
205 * saved by the exception handling. duh.
206 * Actually they are in -current (have been for a while), and that
207 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
208 * If you're running a system from the -stable branch older than that,
209 * like a 3.3-RELEASE, grab the patch from the ports tree:
210 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
211 * (If its not yet there when you look, go here:
212 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
214 #ifdef __FreeBSD__
215 #define FS_sig(context) ((context)->sc_fs)
216 #define GS_sig(context) ((context)->sc_gs)
217 #endif
219 #ifdef linux
220 #define FS_sig(context) ((context)->sc_fs)
221 #define GS_sig(context) ((context)->sc_gs)
222 #define CR2_sig(context) ((context)->cr2)
223 #define TRAP_sig(context) ((context)->sc_trapno)
224 #define ERROR_sig(context) ((context)->sc_err)
225 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
226 #endif
228 #ifndef __FreeBSD__
229 #define EFL_sig(context) ((context)->sc_eflags)
230 #else
231 #define EFL_sig(context) ((context)->sc_efl)
232 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
233 #define CR2_sig(context) ((context)->sc_err)
234 #define TRAP_sig(context) ((context)->sc_trapno)
235 #endif
237 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
238 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
240 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
242 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
244 #ifdef _SCO_DS
245 #define gregs regs
246 #endif
248 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
249 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
250 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
251 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
252 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
253 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
254 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
256 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
257 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
258 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
259 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
261 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
262 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
264 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
266 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
267 #ifdef R_ESP
268 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
269 #else
270 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
271 #endif
272 #ifdef TRAPNO
273 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
274 #endif
276 #endif /* svr4 || SCO_DS */
279 /* exception code definitions (already defined by FreeBSD) */
280 #ifndef __FreeBSD__ /* FIXME: other BSDs? */
281 #define T_DIVIDE 0 /* Division by zero exception */
282 #define T_TRCTRAP 1 /* Single-step exception */
283 #define T_NMI 2 /* NMI interrupt */
284 #define T_BPTFLT 3 /* Breakpoint exception */
285 #define T_OFLOW 4 /* Overflow exception */
286 #define T_BOUND 5 /* Bound range exception */
287 #define T_PRIVINFLT 6 /* Invalid opcode exception */
288 #define T_DNA 7 /* Device not available exception */
289 #define T_DOUBLEFLT 8 /* Double fault exception */
290 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
291 #define T_TSSFLT 10 /* Invalid TSS exception */
292 #define T_SEGNPFLT 11 /* Segment not present exception */
293 #define T_STKFLT 12 /* Stack fault */
294 #define T_PROTFLT 13 /* General protection fault */
295 #define T_PAGEFLT 14 /* Page fault */
296 #define T_RESERVED 15 /* Unknown exception */
297 #define T_ARITHTRAP 16 /* Floating point exception */
298 #define T_ALIGNFLT 17 /* Alignment check exception */
299 #define T_MCHK 18 /* Machine check exception */
300 #define T_CACHEFLT 19 /* Cache flush exception */
301 #endif
303 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
305 #include "wine/exception.h"
306 #include "winnt.h"
307 #include "stackframe.h"
308 #include "global.h"
309 #include "miscemu.h"
310 #include "ntddk.h"
311 #include "syslevel.h"
312 #include "debugtools.h"
314 DEFAULT_DEBUG_CHANNEL(seh)
318 /***********************************************************************
319 * handler_init
321 * Initialization code for a signal handler.
322 * Restores the proper %fs value for the current thread.
324 static inline void handler_init( CONTEXT *context, const SIGCONTEXT *sigcontext )
326 WORD fs;
327 /* get %fs at time of the fault */
328 #ifdef FS_sig
329 fs = FS_sig(sigcontext);
330 #else
331 fs = __get_fs();
332 #endif
333 context->SegFs = fs;
334 /* now restore a proper %fs for the fault handler */
335 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) fs = SYSLEVEL_Win16CurrentTeb;
336 if (!fs) fs = SYSLEVEL_EmergencyTeb;
337 __set_fs(fs);
340 /***********************************************************************
341 * get_trap_code
343 * Get the trap code for a signal.
345 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
347 #ifdef TRAP_sig
348 return TRAP_sig(sigcontext);
349 #else
350 return T_UNKNOWN; /* unknown trap code */
351 #endif
354 /***********************************************************************
355 * save_context
357 * Set the register values from a sigcontext.
359 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
361 context->Eax = EAX_sig(sigcontext);
362 context->Ebx = EBX_sig(sigcontext);
363 context->Ecx = ECX_sig(sigcontext);
364 context->Edx = EDX_sig(sigcontext);
365 context->Esi = ESI_sig(sigcontext);
366 context->Edi = EDI_sig(sigcontext);
367 context->Ebp = EBP_sig(sigcontext);
368 context->EFlags = EFL_sig(sigcontext);
369 context->Eip = EIP_sig(sigcontext);
370 context->Esp = ESP_sig(sigcontext);
371 context->SegCs = LOWORD(CS_sig(sigcontext));
372 context->SegDs = LOWORD(DS_sig(sigcontext));
373 context->SegEs = LOWORD(ES_sig(sigcontext));
374 context->SegSs = LOWORD(SS_sig(sigcontext));
375 /* %fs already handled in handler_init */
376 #ifdef GS_sig
377 context->SegGs = LOWORD(GS_sig(sigcontext));
378 #else
379 context->SegGs = __get_gs();
380 #endif
381 if (ISV86(context)) V86BASE(context) = (DWORD)DOSMEM_MemoryBase(0);
385 /***********************************************************************
386 * restore_context
388 * Build a sigcontext from the register values.
390 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
392 EAX_sig(sigcontext) = context->Eax;
393 EBX_sig(sigcontext) = context->Ebx;
394 ECX_sig(sigcontext) = context->Ecx;
395 EDX_sig(sigcontext) = context->Edx;
396 ESI_sig(sigcontext) = context->Esi;
397 EDI_sig(sigcontext) = context->Edi;
398 EBP_sig(sigcontext) = context->Ebp;
399 EFL_sig(sigcontext) = context->EFlags;
400 EIP_sig(sigcontext) = context->Eip;
401 ESP_sig(sigcontext) = context->Esp;
402 CS_sig(sigcontext) = context->SegCs;
403 DS_sig(sigcontext) = context->SegDs;
404 ES_sig(sigcontext) = context->SegEs;
405 SS_sig(sigcontext) = context->SegSs;
406 #ifdef FS_sig
407 FS_sig(sigcontext) = context->SegFs;
408 #else
409 __set_fs( context->SegFs );
410 #endif
411 #ifdef GS_sig
412 GS_sig(sigcontext) = context->SegGs;
413 #else
414 __set_gs( context->SegGs );
415 #endif
419 /***********************************************************************
420 * save_fpu
422 * Set the FPU context from a sigcontext.
424 static void inline save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
426 #ifdef FPU_sig
427 if (FPU_sig(sigcontext))
429 context->FloatSave = *FPU_sig(sigcontext);
430 return;
432 #endif /* FPU_sig */
433 #ifdef __GNUC__
434 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
435 #endif /* __GNUC__ */
439 /***********************************************************************
440 * restore_fpu
442 * Restore the FPU context to a sigcontext.
444 static void inline restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
446 /* reset the current interrupt status */
447 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
448 #ifdef FPU_sig
449 if (FPU_sig(sigcontext))
451 *FPU_sig(sigcontext) = context->FloatSave;
452 return;
454 #endif /* FPU_sig */
455 #ifdef __GNUC__
456 /* avoid nested exceptions */
457 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
458 #endif /* __GNUC__ */
462 /**********************************************************************
463 * get_fpu_code
465 * Get the FPU exception code from the FPU status.
467 static inline DWORD get_fpu_code( const CONTEXT *context )
469 DWORD status = context->FloatSave.StatusWord;
471 if (status & 0x01) /* IE */
473 if (status & 0x40) /* SF */
474 return EXCEPTION_FLT_STACK_CHECK;
475 else
476 return EXCEPTION_FLT_INVALID_OPERATION;
478 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
479 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
480 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
481 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
482 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
483 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
487 /**********************************************************************
488 * segv_handler
490 * Handler for SIGSEGV and related errors.
492 static HANDLER_DEF(segv_handler)
494 EXCEPTION_RECORD rec;
495 CONTEXT context;
496 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
498 handler_init( &context, HANDLER_CONTEXT );
500 #ifdef CR2_sig
501 /* we want the page-fault case to be fast */
502 if (get_trap_code(HANDLER_CONTEXT) == T_PAGEFLT)
503 if (!(page_fault_code = VIRTUAL_HandleFault( (LPVOID)CR2_sig(HANDLER_CONTEXT) ))) return;
504 #endif
506 save_context( &context, HANDLER_CONTEXT );
507 rec.ExceptionRecord = NULL;
508 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
509 rec.ExceptionAddress = (LPVOID)context.Eip;
510 rec.NumberParameters = 0;
512 switch(get_trap_code(HANDLER_CONTEXT))
514 case T_OFLOW: /* Overflow exception */
515 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
516 break;
517 case T_BOUND: /* Bound range exception */
518 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
519 break;
520 case T_PRIVINFLT: /* Invalid opcode exception */
521 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
522 break;
523 case T_STKFLT: /* Stack fault */
524 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
525 break;
526 case T_SEGNPFLT: /* Segment not present exception */
527 case T_PROTFLT: /* General protection fault */
528 case T_UNKNOWN: /* Unknown fault code */
529 if (INSTR_EmulateInstruction( &context )) goto restore;
530 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
531 break;
532 case T_PAGEFLT: /* Page fault */
533 #ifdef CR2_sig
534 rec.NumberParameters = 2;
535 #ifdef ERROR_sig
536 rec.ExceptionInformation[0] = (ERROR_sig(HANDLER_CONTEXT) & 2) != 0;
537 #else
538 rec.ExceptionInformation[0] = 0;
539 #endif /* ERROR_sig */
540 rec.ExceptionInformation[1] = CR2_sig(HANDLER_CONTEXT);
541 #endif /* CR2_sig */
542 rec.ExceptionCode = page_fault_code;
543 break;
544 case T_ALIGNFLT: /* Alignment check exception */
545 /* FIXME: pass through exception handler first? */
546 if (context.EFlags & 0x00040000)
548 /* Disable AC flag, return */
549 context.EFlags &= ~0x00040000;
550 goto restore;
552 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
553 break;
554 default:
555 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
556 /* fall through */
557 case T_NMI: /* NMI interrupt */
558 case T_DNA: /* Device not available exception */
559 case T_DOUBLEFLT: /* Double fault exception */
560 case T_TSSFLT: /* Invalid TSS exception */
561 case T_RESERVED: /* Unknown exception */
562 case T_MCHK: /* Machine check exception */
563 #ifdef T_CACHEFLT
564 case T_CACHEFLT: /* Cache flush exception */
565 #endif
566 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
567 break;
570 EXC_RtlRaiseException( &rec, &context );
571 restore:
572 restore_context( &context, HANDLER_CONTEXT );
576 /**********************************************************************
577 * trap_handler
579 * Handler for SIGTRAP.
581 static HANDLER_DEF(trap_handler)
583 EXCEPTION_RECORD rec;
584 CONTEXT context;
586 handler_init( &context, HANDLER_CONTEXT );
588 save_context( &context, HANDLER_CONTEXT );
589 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
590 rec.ExceptionRecord = NULL;
591 rec.ExceptionAddress = (LPVOID)context.Eip;
592 rec.NumberParameters = 0;
594 switch(get_trap_code(HANDLER_CONTEXT))
596 case T_TRCTRAP: /* Single-step exception */
597 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
598 context.EFlags &= ~0x100; /* clear single-step flag */
599 break;
600 case T_BPTFLT: /* Breakpoint exception */
601 rec.ExceptionAddress = (char *) rec.ExceptionAddress - 1; /* back up over the int3 instruction */
602 /* fall through */
603 default:
604 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
605 break;
607 EXC_RtlRaiseException( &rec, &context );
608 restore_context( &context, HANDLER_CONTEXT );
612 /**********************************************************************
613 * fpe_handler
615 * Handler for SIGFPE.
617 static HANDLER_DEF(fpe_handler)
619 EXCEPTION_RECORD rec;
620 CONTEXT context;
622 handler_init( &context, HANDLER_CONTEXT );
624 save_fpu( &context, HANDLER_CONTEXT );
626 switch(get_trap_code(HANDLER_CONTEXT))
628 case T_DIVIDE: /* Division by zero exception */
629 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
630 break;
631 case T_FPOPFLT: /* Coprocessor segment overrun */
632 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
633 break;
634 case T_ARITHTRAP: /* Floating point exception */
635 case T_UNKNOWN: /* Unknown fault code */
636 rec.ExceptionCode = get_fpu_code( &context );
637 break;
638 default:
639 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
640 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
641 break;
643 save_context( &context, HANDLER_CONTEXT );
644 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
645 rec.ExceptionRecord = NULL;
646 rec.ExceptionAddress = (LPVOID)context.Eip;
647 rec.NumberParameters = 0;
648 EXC_RtlRaiseException( &rec, &context );
649 restore_context( &context, HANDLER_CONTEXT );
650 restore_fpu( &context, HANDLER_CONTEXT );
654 /**********************************************************************
655 * int_handler
657 * Handler for SIGINT.
659 static HANDLER_DEF(int_handler)
661 EXCEPTION_RECORD rec;
662 CONTEXT context;
664 handler_init( &context, HANDLER_CONTEXT );
665 save_context( &context, HANDLER_CONTEXT );
666 rec.ExceptionCode = CONTROL_C_EXIT;
667 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
668 rec.ExceptionRecord = NULL;
669 rec.ExceptionAddress = (LPVOID)context.Eip;
670 rec.NumberParameters = 0;
671 EXC_RtlRaiseException( &rec, &context );
672 restore_context( &context, HANDLER_CONTEXT );
676 /***********************************************************************
677 * set_handler
679 * Set a signal handler
681 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
683 struct sigaction sig_act;
685 #ifdef linux
686 if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
688 struct kernel_sigaction sig_act;
689 sig_act.ksa_handler = func;
690 sig_act.ksa_flags = SA_RESTART | SA_NOMASK;
691 sig_act.ksa_mask = 0;
692 /* point to the top of the stack */
693 sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
694 return wine_sigaction( sig, &sig_act, NULL );
696 #endif /* linux */
697 sig_act.sa_handler = func;
698 sigemptyset( &sig_act.sa_mask );
700 #ifdef linux
701 sig_act.sa_flags = SA_RESTART | SA_NOMASK;
702 #elif defined (__svr4__) || defined(_SCO_DS)
703 sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
704 #else
705 sig_act.sa_flags = 0;
706 #endif
708 #ifdef SA_ONSTACK
709 if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
710 #endif
711 return sigaction( sig, &sig_act, NULL );
715 /**********************************************************************
716 * SIGNAL_Init
718 BOOL SIGNAL_Init(void)
720 int have_sigaltstack = 0;
722 #ifdef HAVE_SIGALTSTACK
723 struct sigaltstack ss;
724 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
726 ss.ss_size = SIGNAL_STACK_SIZE;
727 ss.ss_flags = 0;
728 if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
729 #ifdef linux
730 /* sigaltstack may fail because the kernel is too old, or
731 because glibc is brain-dead. In the latter case a
732 direct system call should succeed. */
733 else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
734 #endif /* linux */
736 #endif /* HAVE_SIGALTSTACK */
738 /* automatic child reaping to avoid zombies */
739 signal( SIGCHLD, SIG_IGN );
741 if (set_handler( SIGINT, have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
742 if (set_handler( SIGFPE, have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
743 if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
744 if (set_handler( SIGILL, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
745 #ifdef SIGBUS
746 if (set_handler( SIGBUS, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
747 #endif
748 #ifdef SIGTRAP
749 if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
750 #endif
751 return TRUE;
753 error:
754 perror("sigaction");
755 return FALSE;
758 /**********************************************************************
759 * DbgBreakPoint (NTDLL)
761 void WINAPI DbgBreakPoint(void);
762 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
764 /**********************************************************************
765 * DbgUserBreakPoint (NTDLL)
767 void WINAPI DbgUserBreakPoint(void);
768 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
770 #endif /* __i386__ */