Implemented guard pages and stack overflow exceptions.
[wine/testsucceed.git] / dlls / ntdll / signal_i386.c
blobe964f8f00bb9eb5923ee31e5c12815171b7e97cc
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 #undef sa_handler
66 struct kernel_sigaction
68 void (*sa_handler)();
69 unsigned long sa_mask;
70 unsigned long sa_flags;
71 void *sa_restorer;
74 /* Similar to the sigaction function in libc, except it leaves alone the
75 restorer field, which is used to specify the signal stack address */
76 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
77 struct kernel_sigaction *old )
79 __asm__ __volatile__( "pushl %%ebx\n\t"
80 "movl %2,%%ebx\n\t"
81 "int $0x80\n\t"
82 "popl %%ebx"
83 : "=a" (sig)
84 : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
85 if (sig>=0) return 0;
86 errno = -sig;
87 return -1;
90 #endif /* linux */
92 #ifdef BSDI
94 #define EAX_sig(context) ((context)->tf_eax)
95 #define EBX_sig(context) ((context)->tf_ebx)
96 #define ECX_sig(context) ((context)->tf_ecx)
97 #define EDX_sig(context) ((context)->tf_edx)
98 #define ESI_sig(context) ((context)->tf_esi)
99 #define EDI_sig(context) ((context)->tf_edi)
100 #define EBP_sig(context) ((context)->tf_ebp)
102 #define CS_sig(context) ((context)->tf_cs)
103 #define DS_sig(context) ((context)->tf_ds)
104 #define ES_sig(context) ((context)->tf_es)
105 #define SS_sig(context) ((context)->tf_ss)
107 #include <machine/frame.h>
108 typedef struct trapframe SIGCONTEXT;
110 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
111 #define HANDLER_CONTEXT __context
113 #define EFL_sig(context) ((context)->tf_eflags)
115 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
116 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
118 #endif /* bsdi */
120 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
122 typedef struct sigcontext SIGCONTEXT;
124 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
125 #define HANDLER_CONTEXT __context
127 #endif /* FreeBSD */
129 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
131 #ifdef _SCO_DS
132 #include <sys/regset.h>
133 #endif
134 /* Solaris kludge */
135 #undef ERR
136 #include <sys/ucontext.h>
137 #undef ERR
138 typedef struct ucontext SIGCONTEXT;
140 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
141 #define HANDLER_CONTEXT __context
143 #endif /* svr4 || SCO_DS */
145 #ifdef __EMX__
147 typedef struct
149 unsigned long ContextFlags;
150 FLOATING_SAVE_AREA sc_float;
151 unsigned long sc_gs;
152 unsigned long sc_fs;
153 unsigned long sc_es;
154 unsigned long sc_ds;
155 unsigned long sc_edi;
156 unsigned long sc_esi;
157 unsigned long sc_eax;
158 unsigned long sc_ebx;
159 unsigned long sc_ecx;
160 unsigned long sc_edx;
161 unsigned long sc_ebp;
162 unsigned long sc_eip;
163 unsigned long sc_cs;
164 unsigned long sc_eflags;
165 unsigned long sc_esp;
166 unsigned long sc_ss;
167 } SIGCONTEXT;
169 #endif /* __EMX__ */
172 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
174 #define EAX_sig(context) ((context)->sc_eax)
175 #define EBX_sig(context) ((context)->sc_ebx)
176 #define ECX_sig(context) ((context)->sc_ecx)
177 #define EDX_sig(context) ((context)->sc_edx)
178 #define ESI_sig(context) ((context)->sc_esi)
179 #define EDI_sig(context) ((context)->sc_edi)
180 #define EBP_sig(context) ((context)->sc_ebp)
182 #define CS_sig(context) ((context)->sc_cs)
183 #define DS_sig(context) ((context)->sc_ds)
184 #define ES_sig(context) ((context)->sc_es)
185 #define SS_sig(context) ((context)->sc_ss)
187 /* FS and GS are now in the sigcontext struct of FreeBSD, but not
188 * saved by the exception handling. duh.
189 * Actually they are in -current (have been for a while), and that
190 * patch now finally has been MFC'd to -stable too (Nov 15 1999).
191 * If you're running a system from the -stable branch older than that,
192 * like a 3.3-RELEASE, grab the patch from the ports tree:
193 * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
194 * (If its not yet there when you look, go here:
195 * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
197 #ifdef __FreeBSD__
198 #define FS_sig(context) ((context)->sc_fs)
199 #define GS_sig(context) ((context)->sc_gs)
200 #endif
202 #ifdef linux
203 #define FS_sig(context) ((context)->sc_fs)
204 #define GS_sig(context) ((context)->sc_gs)
205 #define CR2_sig(context) ((context)->cr2)
206 #define TRAP_sig(context) ((context)->sc_trapno)
207 #define ERROR_sig(context) ((context)->sc_err)
208 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
209 #endif
211 #ifndef __FreeBSD__
212 #define EFL_sig(context) ((context)->sc_eflags)
213 #else
214 #define EFL_sig(context) ((context)->sc_efl)
215 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
216 #define CR2_sig(context) ((context)->sc_err)
217 #define TRAP_sig(context) ((context)->sc_trapno)
218 #endif
220 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
221 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
223 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
225 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
227 #ifdef _SCO_DS
228 #define gregs regs
229 #endif
231 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
232 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
233 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
234 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
235 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
236 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
237 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
239 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
240 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
241 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
242 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
244 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
245 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
247 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
249 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
250 #ifdef R_ESP
251 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
252 #else
253 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
254 #endif
255 #ifdef TRAPNO
256 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
257 #endif
259 #endif /* svr4 || SCO_DS */
262 /* exception code definitions (already defined by FreeBSD) */
263 #ifndef __FreeBSD__ /* FIXME: other BSDs? */
264 #define T_DIVIDE 0 /* Division by zero exception */
265 #define T_TRCTRAP 1 /* Single-step exception */
266 #define T_NMI 2 /* NMI interrupt */
267 #define T_BPTFLT 3 /* Breakpoint exception */
268 #define T_OFLOW 4 /* Overflow exception */
269 #define T_BOUND 5 /* Bound range exception */
270 #define T_PRIVINFLT 6 /* Invalid opcode exception */
271 #define T_DNA 7 /* Device not available exception */
272 #define T_DOUBLEFLT 8 /* Double fault exception */
273 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
274 #define T_TSSFLT 10 /* Invalid TSS exception */
275 #define T_SEGNPFLT 11 /* Segment not present exception */
276 #define T_STKFLT 12 /* Stack fault */
277 #define T_PROTFLT 13 /* General protection fault */
278 #define T_PAGEFLT 14 /* Page fault */
279 #define T_RESERVED 15 /* Unknown exception */
280 #define T_ARITHTRAP 16 /* Floating point exception */
281 #define T_ALIGNFLT 17 /* Alignment check exception */
282 #define T_MCHK 18 /* Machine check exception */
283 #define T_CACHEFLT 19 /* Cache flush exception */
284 #endif
286 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
288 #include "wine/exception.h"
289 #include "winnt.h"
290 #include "stackframe.h"
291 #include "global.h"
292 #include "miscemu.h"
293 #include "ntddk.h"
294 #include "syslevel.h"
295 #include "debugtools.h"
297 DEFAULT_DEBUG_CHANNEL(seh)
301 /***********************************************************************
302 * handler_init
304 * Initialization code for a signal handler.
305 * Restores the proper %fs value for the current thread.
307 static inline void handler_init( CONTEXT *context, const SIGCONTEXT *sigcontext )
309 WORD fs;
310 /* get %fs at time of the fault */
311 #ifdef FS_sig
312 fs = FS_sig(sigcontext);
313 #else
314 fs = __get_fs();
315 #endif
316 context->SegFs = fs;
317 /* now restore a proper %fs for the fault handler */
318 if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext))) fs = SYSLEVEL_Win16CurrentTeb;
319 if (!fs) fs = SYSLEVEL_EmergencyTeb;
320 __set_fs(fs);
323 /***********************************************************************
324 * get_trap_code
326 * Get the trap code for a signal.
328 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
330 #ifdef TRAP_sig
331 return TRAP_sig(sigcontext);
332 #else
333 return T_UNKNOWN; /* unknown trap code */
334 #endif
337 /***********************************************************************
338 * save_context
340 * Set the register values from a sigcontext.
342 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
344 context->Eax = EAX_sig(sigcontext);
345 context->Ebx = EBX_sig(sigcontext);
346 context->Ecx = ECX_sig(sigcontext);
347 context->Edx = EDX_sig(sigcontext);
348 context->Esi = ESI_sig(sigcontext);
349 context->Edi = EDI_sig(sigcontext);
350 context->Ebp = EBP_sig(sigcontext);
351 context->EFlags = EFL_sig(sigcontext);
352 context->Eip = EIP_sig(sigcontext);
353 context->Esp = ESP_sig(sigcontext);
354 context->SegCs = LOWORD(CS_sig(sigcontext));
355 context->SegDs = LOWORD(DS_sig(sigcontext));
356 context->SegEs = LOWORD(ES_sig(sigcontext));
357 context->SegSs = LOWORD(SS_sig(sigcontext));
358 /* %fs already handled in handler_init */
359 #ifdef GS_sig
360 context->SegGs = LOWORD(GS_sig(sigcontext));
361 #else
362 context->SegGs = __get_gs();
363 #endif
364 if (ISV86(context)) V86BASE(context) = (DWORD)DOSMEM_MemoryBase(0);
368 /***********************************************************************
369 * restore_context
371 * Build a sigcontext from the register values.
373 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
375 EAX_sig(sigcontext) = context->Eax;
376 EBX_sig(sigcontext) = context->Ebx;
377 ECX_sig(sigcontext) = context->Ecx;
378 EDX_sig(sigcontext) = context->Edx;
379 ESI_sig(sigcontext) = context->Esi;
380 EDI_sig(sigcontext) = context->Edi;
381 EBP_sig(sigcontext) = context->Ebp;
382 EFL_sig(sigcontext) = context->EFlags;
383 EIP_sig(sigcontext) = context->Eip;
384 ESP_sig(sigcontext) = context->Esp;
385 CS_sig(sigcontext) = context->SegCs;
386 DS_sig(sigcontext) = context->SegDs;
387 ES_sig(sigcontext) = context->SegEs;
388 SS_sig(sigcontext) = context->SegSs;
389 #ifdef FS_sig
390 FS_sig(sigcontext) = context->SegFs;
391 #else
392 __set_fs( context->SegFs );
393 #endif
394 #ifdef GS_sig
395 GS_sig(sigcontext) = context->SegGs;
396 #else
397 __set_gs( context->SegGs );
398 #endif
402 /***********************************************************************
403 * save_fpu
405 * Set the FPU context from a sigcontext.
407 static void inline save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
409 #ifdef FPU_sig
410 if (FPU_sig(sigcontext))
412 context->FloatSave = *FPU_sig(sigcontext);
413 return;
415 #endif /* FPU_sig */
416 #ifdef __GNUC__
417 __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
418 #endif /* __GNUC__ */
422 /***********************************************************************
423 * restore_fpu
425 * Restore the FPU context to a sigcontext.
427 static void inline restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
429 #ifdef FPU_sig
430 if (FPU_sig(sigcontext))
432 *FPU_sig(sigcontext) = context->FloatSave;
433 return;
435 #endif /* FPU_sig */
436 #ifdef __GNUC__
437 /* avoid nested exceptions */
438 context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
439 __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
440 #endif /* __GNUC__ */
444 /**********************************************************************
445 * get_fpu_code
447 * Get the FPU exception code from the FPU status.
449 static inline DWORD get_fpu_code( const CONTEXT *context )
451 DWORD status = context->FloatSave.StatusWord;
453 if (status & 0x01) /* IE */
455 if (status & 0x40) /* SF */
456 return EXCEPTION_FLT_STACK_CHECK;
457 else
458 return EXCEPTION_FLT_INVALID_OPERATION;
460 if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND; /* DE flag */
461 if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO; /* ZE flag */
462 if (status & 0x08) return EXCEPTION_FLT_OVERFLOW; /* OE flag */
463 if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW; /* UE flag */
464 if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT; /* PE flag */
465 return EXCEPTION_FLT_INVALID_OPERATION; /* generic error */
469 /**********************************************************************
470 * segv_handler
472 * Handler for SIGSEGV and related errors.
474 static HANDLER_DEF(segv_handler)
476 EXCEPTION_RECORD rec;
477 CONTEXT context;
478 DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
480 handler_init( &context, HANDLER_CONTEXT );
482 #ifdef CR2_sig
483 /* we want the page-fault case to be fast */
484 if (get_trap_code(HANDLER_CONTEXT) == T_PAGEFLT)
485 if (!(page_fault_code = VIRTUAL_HandleFault( (LPVOID)CR2_sig(HANDLER_CONTEXT) ))) return;
486 #endif
488 save_context( &context, HANDLER_CONTEXT );
489 rec.ExceptionRecord = NULL;
490 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
491 rec.ExceptionAddress = (LPVOID)context.Eip;
492 rec.NumberParameters = 0;
494 switch(get_trap_code(HANDLER_CONTEXT))
496 case T_OFLOW: /* Overflow exception */
497 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
498 break;
499 case T_BOUND: /* Bound range exception */
500 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
501 break;
502 case T_PRIVINFLT: /* Invalid opcode exception */
503 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
504 break;
505 case T_STKFLT: /* Stack fault */
506 rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
507 break;
508 case T_SEGNPFLT: /* Segment not present exception */
509 case T_PROTFLT: /* General protection fault */
510 case T_UNKNOWN: /* Unknown fault code */
511 if (INSTR_EmulateInstruction( &context )) goto restore;
512 rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
513 break;
514 case T_PAGEFLT: /* Page fault */
515 #ifdef CR2_sig
516 rec.NumberParameters = 2;
517 #ifdef ERROR_sig
518 rec.ExceptionInformation[0] = (ERROR_sig(HANDLER_CONTEXT) & 2) != 0;
519 #else
520 rec.ExceptionInformation[0] = 0;
521 #endif /* ERROR_sig */
522 rec.ExceptionInformation[1] = CR2_sig(HANDLER_CONTEXT);
523 #endif /* CR2_sig */
524 rec.ExceptionCode = page_fault_code;
525 break;
526 case T_ALIGNFLT: /* Alignment check exception */
527 /* FIXME: pass through exception handler first? */
528 if (context.EFlags & 0x00040000)
530 /* Disable AC flag, return */
531 context.EFlags &= ~0x00040000;
532 goto restore;
534 rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
535 break;
536 default:
537 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
538 /* fall through */
539 case T_NMI: /* NMI interrupt */
540 case T_DNA: /* Device not available exception */
541 case T_DOUBLEFLT: /* Double fault exception */
542 case T_TSSFLT: /* Invalid TSS exception */
543 case T_RESERVED: /* Unknown exception */
544 case T_MCHK: /* Machine check exception */
545 #ifdef T_CACHEFLT
546 case T_CACHEFLT: /* Cache flush exception */
547 #endif
548 rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
549 break;
552 EXC_RtlRaiseException( &rec, &context );
553 restore:
554 restore_context( &context, HANDLER_CONTEXT );
558 /**********************************************************************
559 * trap_handler
561 * Handler for SIGTRAP.
563 static HANDLER_DEF(trap_handler)
565 EXCEPTION_RECORD rec;
566 CONTEXT context;
568 handler_init( &context, HANDLER_CONTEXT );
570 save_context( &context, HANDLER_CONTEXT );
571 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
572 rec.ExceptionRecord = NULL;
573 rec.ExceptionAddress = (LPVOID)context.Eip;
574 rec.NumberParameters = 0;
576 switch(get_trap_code(HANDLER_CONTEXT))
578 case T_TRCTRAP: /* Single-step exception */
579 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
580 context.EFlags &= ~0x100; /* clear single-step flag */
581 break;
582 case T_BPTFLT: /* Breakpoint exception */
583 rec.ExceptionAddress = (char *) rec.ExceptionAddress - 1; /* back up over the int3 instruction */
584 /* fall through */
585 default:
586 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
587 break;
589 EXC_RtlRaiseException( &rec, &context );
590 restore_context( &context, HANDLER_CONTEXT );
594 /**********************************************************************
595 * fpe_handler
597 * Handler for SIGFPE.
599 static HANDLER_DEF(fpe_handler)
601 EXCEPTION_RECORD rec;
602 CONTEXT context;
604 handler_init( &context, HANDLER_CONTEXT );
606 save_fpu( &context, HANDLER_CONTEXT );
608 switch(get_trap_code(HANDLER_CONTEXT))
610 case T_DIVIDE: /* Division by zero exception */
611 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
612 break;
613 case T_FPOPFLT: /* Coprocessor segment overrun */
614 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
615 break;
616 case T_ARITHTRAP: /* Floating point exception */
617 case T_UNKNOWN: /* Unknown fault code */
618 rec.ExceptionCode = get_fpu_code( &context );
619 break;
620 default:
621 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
622 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
623 break;
625 save_context( &context, HANDLER_CONTEXT );
626 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
627 rec.ExceptionRecord = NULL;
628 rec.ExceptionAddress = (LPVOID)context.Eip;
629 rec.NumberParameters = 0;
630 EXC_RtlRaiseException( &rec, &context );
631 restore_context( &context, HANDLER_CONTEXT );
632 restore_fpu( &context, HANDLER_CONTEXT );
636 /**********************************************************************
637 * int_handler
639 * Handler for SIGINT.
641 static HANDLER_DEF(int_handler)
643 EXCEPTION_RECORD rec;
644 CONTEXT context;
646 handler_init( &context, HANDLER_CONTEXT );
647 save_context( &context, HANDLER_CONTEXT );
648 rec.ExceptionCode = CONTROL_C_EXIT;
649 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
650 rec.ExceptionRecord = NULL;
651 rec.ExceptionAddress = (LPVOID)context.Eip;
652 rec.NumberParameters = 0;
653 EXC_RtlRaiseException( &rec, &context );
654 restore_context( &context, HANDLER_CONTEXT );
658 /***********************************************************************
659 * set_handler
661 * Set a signal handler
663 static int set_handler( int sig, void (*func)() )
665 #ifdef linux
666 struct kernel_sigaction sig_act;
667 sig_act.sa_handler = func;
668 sig_act.sa_flags = SA_RESTART | SA_NOMASK;
669 sig_act.sa_mask = 0;
670 /* point to the top of the stack */
671 sig_act.sa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
672 return wine_sigaction( sig, &sig_act, NULL );
673 #else /* linux */
674 struct sigaction sig_act;
675 sig_act.sa_handler = func;
676 sigemptyset( &sig_act.sa_mask );
678 # if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
679 sig_act.sa_flags = SA_ONSTACK;
680 # elif defined (__svr4__) || defined(_SCO_DS)
681 sig_act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
682 # elif defined(__EMX__)
683 sig_act.sa_flags = 0; /* FIXME: EMX has only SA_ACK and SA_SYSV */
684 # else
685 sig_act.sa_flags = 0;
686 # endif
687 return sigaction( sig, &sig_act, NULL );
688 #endif /* linux */
692 /**********************************************************************
693 * SIGNAL_Init
695 BOOL SIGNAL_Init(void)
697 #ifdef HAVE_WORKING_SIGALTSTACK
698 struct sigaltstack ss;
699 if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
701 ss.ss_size = SIGNAL_STACK_SIZE;
702 ss.ss_flags = 0;
703 if (sigaltstack(&ss, NULL) < 0)
705 perror("sigaltstack");
706 /* fall through on error and try it differently */
709 #endif /* HAVE_SIGALTSTACK */
711 /* ignore SIGPIPE so that WINSOCK can get a EPIPE error instead */
712 signal( SIGPIPE, SIG_IGN );
713 /* automatic child reaping to avoid zombies */
714 signal( SIGCHLD, SIG_IGN );
716 if (set_handler( SIGINT, (void (*)())int_handler ) == -1) goto error;
717 if (set_handler( SIGFPE, (void (*)())fpe_handler ) == -1) goto error;
718 if (set_handler( SIGSEGV, (void (*)())segv_handler ) == -1) goto error;
719 if (set_handler( SIGILL, (void (*)())segv_handler ) == -1) goto error;
720 #ifdef SIGBUS
721 if (set_handler( SIGBUS, (void (*)())segv_handler ) == -1) goto error;
722 #endif
723 #ifdef SIGTRAP
724 if (set_handler( SIGTRAP, (void (*)())trap_handler ) == -1) goto error;
725 #endif
726 return TRUE;
728 error:
729 perror("sigaction");
730 return FALSE;
733 /**********************************************************************
734 * DbgBreakPoint (NTDLL)
736 void WINAPI DbgBreakPoint(void);
737 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
739 /**********************************************************************
740 * DbgUserBreakPoint (NTDLL)
742 void WINAPI DbgUserBreakPoint(void);
743 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
745 #endif /* __i386__ */