Added DebugBreak.
[wine] / dlls / ntdll / exception.c
1 /*
2  * NT exception handling routines
3  * 
4  * Copyright 1999 Turchanov Sergey
5  * Copyright 1999 Alexandre Julliard
6  */
7
8 #include <signal.h>
9 #include "winnt.h"
10 #include "ntddk.h"
11 #include "process.h"
12 #include "global.h"
13 #include "wine/exception.h"
14 #include "stackframe.h"
15 #include "sig_context.h"
16 #include "miscemu.h"
17 #include "debugtools.h"
18
19 DEFAULT_DEBUG_CHANNEL(seh)
20
21 /* Exception record for handling exceptions happening inside exception handlers */
22 typedef struct
23 {
24     EXCEPTION_FRAME frame;
25     EXCEPTION_FRAME *prevFrame;
26 } EXC_NESTED_FRAME;
27
28 #ifdef __i386__
29 # define GET_IP(context) ((LPVOID)EIP_reg(context))
30 #else
31 # define GET_IP(context) ((LPVOID)0)
32 #endif  /* __i386__ */
33
34 /* Default hook for built-in debugger */
35 static DWORD default_hook( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
36 {
37     if (!first)
38     {
39         DPRINTF( "stopping process due to unhandled exception %08lx.\n",
40                  rec->ExceptionCode );
41         raise( SIGSTOP );
42     }
43     return 0;  /* not handled */
44 }
45 static DEBUGHOOK debug_hook = default_hook;
46
47 /*******************************************************************
48  *         EXC_SetDebugEventHook
49  *         EXC_GetDebugEventHook
50  *
51  * Set/Get the hook for the built-in debugger.
52  *
53  * FIXME: the built-in debugger should use the normal debug events.
54  */
55 void EXC_SetDebugEventHook( DEBUGHOOK hook )
56 {
57     debug_hook = hook;
58 }
59 DEBUGHOOK EXC_GetDebugEventHook(void)
60 {
61     return debug_hook;
62 }
63
64 /*******************************************************************
65  *         EXC_RaiseHandler
66  *
67  * Handler for exceptions happening inside a handler.
68  */
69 static DWORD CALLBACK EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
70                                         CONTEXT *context, EXCEPTION_FRAME **dispatcher )
71 {
72     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
73         return ExceptionContinueSearch;
74     /* We shouldn't get here so we store faulty frame in dispatcher */
75     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
76     return ExceptionNestedException;
77 }
78
79
80 /*******************************************************************
81  *         EXC_UnwindHandler
82  *
83  * Handler for exceptions happening inside an unwind handler.
84  */
85 static DWORD CALLBACK EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
86                                          CONTEXT *context, EXCEPTION_FRAME **dispatcher )
87 {
88     if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
89         return ExceptionContinueSearch;
90     /* We shouldn't get here so we store faulty frame in dispatcher */
91     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
92     return ExceptionCollidedUnwind;
93 }
94
95
96 /*******************************************************************
97  *         EXC_CallHandler
98  *
99  * Call an exception handler, setting up an exception frame to catch exceptions
100  * happening during the handler execution.
101  */
102 static DWORD EXC_CallHandler( PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler,
103                               EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
104                               CONTEXT *context, EXCEPTION_FRAME **dispatcher )
105 {
106     EXC_NESTED_FRAME newframe;
107     DWORD ret;
108
109     newframe.frame.Handler = nested_handler;
110     newframe.prevFrame     = frame;
111     EXC_push_frame( &newframe.frame );
112     TRACE( "calling handler at %p code=%lx flags=%lx\n",
113            handler, record->ExceptionCode, record->ExceptionFlags );
114     ret = handler( record, frame, context, dispatcher );
115     TRACE( "handler returned %lx\n", ret );
116     EXC_pop_frame( &newframe.frame );
117     return ret;
118 }
119
120
121 /*******************************************************************
122  *         EXC_DefaultHandling
123  *
124  * Default handling for exceptions. Called when we didn't find a suitable handler.
125  */
126 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
127 {
128     if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
129         (DEBUG_SendExceptionEvent( rec, FALSE ) == DBG_CONTINUE))
130         return;  /* continue execution */
131
132     if (debug_hook( rec, context, FALSE ) == DBG_CONTINUE)
133         return;  /* continue execution */
134
135     if (rec->ExceptionFlags & EH_STACK_INVALID)
136         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
137     else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
138         ERR("Process attempted to continue execution after noncontinuable exception.\n");
139     else
140         ERR("Unhandled exception code %lx flags %lx addr %p\n",
141             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
142     TerminateProcess( GetCurrentProcess(), 1 );
143 }
144
145
146 /***********************************************************************
147  *            RtlRaiseException  (NTDLL.464)
148  */
149 void WINAPI REGS_FUNC(RtlRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *context )
150 {
151     PEXCEPTION_FRAME frame, dispatch, nested_frame;
152     EXCEPTION_RECORD newrec;
153     DWORD res;
154
155     TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
156
157     if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
158         (DEBUG_SendExceptionEvent( rec, TRUE ) == DBG_CONTINUE))
159         return;  /* continue execution */
160
161     if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
162         return;  /* continue execution */
163
164     frame = CURRENT()->except;
165     nested_frame = NULL;
166     while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
167     {
168         /* Check frame address */
169         if (((void*)frame < CURRENT()->stack_low) ||
170             ((void*)(frame+1) > CURRENT()->stack_top) ||
171             (int)frame & 3)
172         {
173             rec->ExceptionFlags |= EH_STACK_INVALID;
174             break;
175         }
176
177         /* Call handler */
178         res = EXC_CallHandler( frame->Handler, EXC_RaiseHandler, rec, frame, context, &dispatch );
179         if (frame == nested_frame)
180         {
181             /* no longer nested */
182             nested_frame = NULL;
183             rec->ExceptionFlags &= ~EH_NESTED_CALL;
184         }
185
186         switch(res)
187         {
188         case ExceptionContinueExecution:
189             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
190             newrec.ExceptionCode    = STATUS_NONCONTINUABLE_EXCEPTION;
191             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
192             newrec.ExceptionRecord  = rec;
193             newrec.NumberParameters = 0;
194             RtlRaiseException( &newrec );  /* never returns */
195             break;
196         case ExceptionContinueSearch:
197             break;
198         case ExceptionNestedException:
199             if (nested_frame < dispatch) nested_frame = dispatch;
200             rec->ExceptionFlags |= EH_NESTED_CALL;
201             break;
202         default:
203             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
204             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
205             newrec.ExceptionRecord  = rec;
206             newrec.NumberParameters = 0;
207             RtlRaiseException( &newrec );  /* never returns */
208             break;
209         }
210         frame = frame->Prev;
211     }
212     EXC_DefaultHandling( rec, context );
213 }
214
215
216 /*******************************************************************
217  *         RtlUnwind  (KERNEL32.590) (NTDLL.518)
218  */
219 void WINAPI REGS_FUNC(RtlUnwind)( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip, 
220                                   PEXCEPTION_RECORD pRecord, DWORD returnEax,
221                                   CONTEXT *context )
222 {
223     EXCEPTION_RECORD record, newrec;
224     PEXCEPTION_FRAME frame, dispatch;
225
226     EAX_reg(context) = returnEax;
227
228     /* build an exception record, if we do not have one */
229     if (!pRecord)
230     {
231         record.ExceptionCode    = STATUS_UNWIND;
232         record.ExceptionFlags   = 0;
233         record.ExceptionRecord  = NULL;
234         record.ExceptionAddress = GET_IP(context); 
235         record.NumberParameters = 0;
236         pRecord = &record;
237     }
238
239     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
240
241     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
242
243     /* get chain of exception frames */
244     frame = CURRENT()->except;
245     while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
246     {
247         /* Check frame address */
248         if (pEndFrame && (frame > pEndFrame))
249         {
250             newrec.ExceptionCode    = STATUS_INVALID_UNWIND_TARGET;
251             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
252             newrec.ExceptionRecord  = pRecord;
253             newrec.NumberParameters = 0;
254             RtlRaiseException( &newrec );  /* never returns */
255         }
256         if (((void*)frame < CURRENT()->stack_low) ||
257             ((void*)(frame+1) > CURRENT()->stack_top) ||
258             (int)frame & 3)
259         {
260             newrec.ExceptionCode    = STATUS_BAD_STACK;
261             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
262             newrec.ExceptionRecord  = pRecord;
263             newrec.NumberParameters = 0;
264             RtlRaiseException( &newrec );  /* never returns */
265         }
266
267         /* Call handler */
268         switch(EXC_CallHandler( frame->Handler, EXC_UnwindHandler, pRecord,
269                                 frame, context, &dispatch ))
270         {
271         case ExceptionContinueSearch:
272             break;
273         case ExceptionCollidedUnwind:
274             frame = dispatch;
275             break;
276         default:
277             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
278             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
279             newrec.ExceptionRecord  = pRecord;
280             newrec.NumberParameters = 0;
281             RtlRaiseException( &newrec );  /* never returns */
282             break;
283         }
284         frame = EXC_pop_frame( frame );
285     }
286 }
287
288
289 /*******************************************************************
290  *         NtRaiseException    (NTDLL.175)
291  *
292  * Real prototype:
293  *    DWORD WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first );
294  */
295 void WINAPI REGS_FUNC(NtRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *ctx,
296                                          BOOL first, CONTEXT *context )
297 {
298     REGS_FUNC(RtlRaiseException)( rec, ctx );
299     *context = *ctx;
300 }
301
302
303 /***********************************************************************
304  *            RtlRaiseStatus  (NTDLL.465)
305  *
306  * Raise an exception with ExceptionCode = status
307  */
308 void WINAPI RtlRaiseStatus( NTSTATUS status )
309 {
310     EXCEPTION_RECORD ExceptionRec;
311
312     ExceptionRec.ExceptionCode    = status;
313     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
314     ExceptionRec.ExceptionRecord  = NULL;
315     ExceptionRec.NumberParameters = 0;
316     RtlRaiseException( &ExceptionRec );
317 }
318
319
320 /***********************************************************************
321  *           DebugBreak   (KERNEL32.181)
322  */
323 void WINAPI REGS_FUNC(DebugBreak)( CONTEXT *context )
324 {
325     EXCEPTION_RECORD rec;
326
327     rec.ExceptionCode    = EXCEPTION_BREAKPOINT;
328     rec.ExceptionFlags   = EH_NONCONTINUABLE;
329     rec.ExceptionRecord  = NULL;
330     rec.NumberParameters = 0;
331     REGS_FUNC(RtlRaiseException)( &rec, context );
332 }
333
334
335 /***********************************************************************
336  *           DebugBreak16   (KERNEL.203)
337  */
338 void WINAPI DebugBreak16( CONTEXT *context )
339 {
340     REGS_FUNC(DebugBreak)( context );
341 }
342
343
344 /***********************************************************************
345  *           EXC_SaveContext
346  *
347  * Set the register values from a sigcontext. 
348  */
349 static void EXC_SaveContext( CONTEXT *context, const SIGCONTEXT *sigcontext )
350 {
351 #ifdef __i386__
352     EAX_reg(context) = EAX_sig(sigcontext);
353     EBX_reg(context) = EBX_sig(sigcontext);
354     ECX_reg(context) = ECX_sig(sigcontext);
355     EDX_reg(context) = EDX_sig(sigcontext);
356     ESI_reg(context) = ESI_sig(sigcontext);
357     EDI_reg(context) = EDI_sig(sigcontext);
358     EBP_reg(context) = EBP_sig(sigcontext);
359     EFL_reg(context) = EFL_sig(sigcontext);
360     EIP_reg(context) = EIP_sig(sigcontext);
361     ESP_reg(context) = ESP_sig(sigcontext);
362     CS_reg(context)  = LOWORD(CS_sig(sigcontext));
363     DS_reg(context)  = LOWORD(DS_sig(sigcontext));
364     ES_reg(context)  = LOWORD(ES_sig(sigcontext));
365     SS_reg(context)  = LOWORD(SS_sig(sigcontext));
366 #ifdef FS_sig
367     FS_reg(context)  = LOWORD(FS_sig(sigcontext));
368 #else
369     GET_FS( FS_reg(context) );
370     FS_reg(context) &= 0xffff;
371 #endif
372 #ifdef GS_sig
373     GS_reg(context)  = LOWORD(GS_sig(sigcontext));
374 #else
375     GET_GS( GS_reg(context) );
376     GS_reg(context) &= 0xffff;
377 #endif
378     if (ISV86(context)) V86BASE(context) = (DWORD)DOSMEM_MemoryBase(0);
379 #endif  /* __i386__ */
380 }
381
382
383 /***********************************************************************
384  *           EXC_RestoreContext
385  *
386  * Build a sigcontext from the register values.
387  */
388 static void EXC_RestoreContext( const CONTEXT *context, SIGCONTEXT *sigcontext )
389 {
390 #ifdef __i386__
391     EAX_sig(sigcontext) = EAX_reg(context);
392     EBX_sig(sigcontext) = EBX_reg(context);
393     ECX_sig(sigcontext) = ECX_reg(context);
394     EDX_sig(sigcontext) = EDX_reg(context);
395     ESI_sig(sigcontext) = ESI_reg(context);
396     EDI_sig(sigcontext) = EDI_reg(context);
397     EBP_sig(sigcontext) = EBP_reg(context);
398     EFL_sig(sigcontext) = EFL_reg(context);
399     EIP_sig(sigcontext) = EIP_reg(context);
400     ESP_sig(sigcontext) = ESP_reg(context);
401     CS_sig(sigcontext)  = CS_reg(context);
402     DS_sig(sigcontext)  = DS_reg(context);
403     ES_sig(sigcontext)  = ES_reg(context);
404     SS_sig(sigcontext)  = SS_reg(context);
405 #ifdef FS_sig
406     FS_sig(sigcontext)  = FS_reg(context);
407 #else
408     SET_FS( FS_reg(context) );
409 #endif
410 #ifdef GS_sig
411     GS_sig(sigcontext)  = GS_reg(context);
412 #else
413     SET_GS( GS_reg(context) );
414 #endif
415 #endif  /* __i386__ */
416 }
417
418
419 /**********************************************************************
420  *              EXC_segv
421  *
422  * Handler for SIGSEGV and related errors.
423  */
424 static HANDLER_DEF(EXC_segv)
425 {
426     EXCEPTION_RECORD rec;
427     CONTEXT context;
428
429     HANDLER_INIT();
430
431     rec.NumberParameters = 0;
432
433 #ifdef TRAP_sig
434     switch(TRAP_sig(HANDLER_CONTEXT))
435     {
436     case 4:   /* Overflow exception */
437         rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
438         break;
439     case 5:   /* Bound range exception */
440         rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
441         break;
442     case 6:   /* Invalid opcode exception */
443         rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
444         break;
445     case 12:  /* Stack fault */
446         rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
447         break;
448     case 11:  /* Segment not present exception */
449     case 13:  /* General protection fault */
450         if (INSTR_EmulateInstruction( HANDLER_CONTEXT )) return;
451         rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
452         break;
453     case 14:  /* Page fault */
454 #ifdef CR2_sig
455         if (VIRTUAL_HandleFault( (LPVOID)CR2_sig(HANDLER_CONTEXT) )) return;
456         rec.NumberParameters = 2;
457 #ifdef ERROR_sig
458         rec.ExceptionInformation[0] = (ERROR_sig(HANDLER_CONTEXT) & 2) != 0;
459 #else
460         rec.ExceptionInformation[0] = 0;
461 #endif /* ERROR_sig */
462         rec.ExceptionInformation[1] = CR2_sig(HANDLER_CONTEXT);
463 #endif /* CR2_sig */
464         rec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
465         break;
466     case 17:  /* Alignment check exception */
467         /* FIXME: pass through exception handler first? */
468         if (EFL_sig(HANDLER_CONTEXT) & 0x00040000)
469         {
470             /* Disable AC flag, return */
471             EFL_sig(HANDLER_CONTEXT) &= ~0x00040000;
472             return;
473         }
474         rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
475         break;
476     default:
477         ERR( "Got unexpected trap %ld\n", TRAP_sig(HANDLER_CONTEXT) );
478         /* fall through */
479     case 2:   /* NMI interrupt */
480     case 7:   /* Device not available exception */
481     case 8:   /* Double fault exception */
482     case 10:  /* Invalid TSS exception */
483     case 15:  /* Unknown exception */
484     case 18:  /* Machine check exception */
485     case 19:  /* Cache flush exception */
486         rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
487         break;
488     }
489 #else  /* TRAP_sig */
490 # ifdef __i386
491     if (INSTR_EmulateInstruction( HANDLER_CONTEXT )) return;
492 # endif
493     rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;  /* generic error */
494 #endif  /* TRAP_sig */
495
496     EXC_SaveContext( &context, HANDLER_CONTEXT );
497     rec.ExceptionRecord  = NULL;
498     rec.ExceptionFlags   = EH_NONCONTINUABLE;
499     rec.ExceptionAddress = GET_IP(&context);
500     REGS_FUNC(RtlRaiseException)( &rec, &context );
501     EXC_RestoreContext( &context, HANDLER_CONTEXT );
502 }
503
504
505 /**********************************************************************
506  *              EXC_trap
507  *
508  * Handler for SIGTRAP.
509  */
510 static HANDLER_DEF(EXC_trap)
511 {
512     EXCEPTION_RECORD rec;
513     CONTEXT context;
514
515     HANDLER_INIT();
516
517 #ifdef TRAP_sig
518     rec.ExceptionCode = (TRAP_sig(HANDLER_CONTEXT) == 1) ?
519                             EXCEPTION_SINGLE_STEP : EXCEPTION_BREAKPOINT;
520 #else
521     rec.ExceptionCode = EXCEPTION_BREAKPOINT;
522 #endif  /* TRAP_sig */
523
524     EXC_SaveContext( &context, HANDLER_CONTEXT );
525     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
526     rec.ExceptionRecord  = NULL;
527     rec.ExceptionAddress = GET_IP(&context);
528     rec.NumberParameters = 0;
529     REGS_FUNC(RtlRaiseException)( &rec, &context );
530     EXC_RestoreContext( &context, HANDLER_CONTEXT );
531 }
532
533
534 /***********************************************************************
535  *           EXC_SaveFPU
536  *
537  * Set the FPU context from a sigcontext. 
538  */
539 static void inline EXC_SaveFPU( CONTEXT *context, const SIGCONTEXT *sigcontext )
540 {
541 #ifdef __i386__
542 # ifdef FPU_sig
543     if (FPU_sig(sigcontext))
544     {
545         context->FloatSave = *FPU_sig(sigcontext);
546         return;
547     }
548 # endif  /* FPU_sig */
549 # ifdef __GNUC__
550     __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
551 # endif  /* __GNUC__ */
552 #endif  /* __i386__ */
553 }
554
555
556 /***********************************************************************
557  *           EXC_RestoreFPU
558  *
559  * Restore the FPU context to a sigcontext. 
560  */
561 static void inline EXC_RestoreFPU( CONTEXT *context, const SIGCONTEXT *sigcontext )
562 {
563 #ifdef __i386__
564 # ifdef FPU_sig
565     if (FPU_sig(sigcontext))
566     {
567         *FPU_sig(sigcontext) = context->FloatSave;
568         return;
569     }
570 # endif  /* FPU_sig */
571 # ifdef __GNUC__
572     /* avoid nested exceptions */
573     context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
574     __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
575 # endif  /* __GNUC__ */
576 #endif  /* __i386__ */
577 }
578
579
580 /**********************************************************************
581  *              EXC_GetFPUCode
582  *
583  * Get the FPU exception code from the FPU status.
584  */
585 static inline DWORD EXC_GetFPUCode( const CONTEXT *context )
586 {
587 #ifdef __i386__
588     DWORD status = context->FloatSave.StatusWord;
589
590     if (status & 0x01)  /* IE */
591     {
592         if (status & 0x40)  /* SF */
593             return EXCEPTION_FLT_STACK_CHECK;
594         else
595             return EXCEPTION_FLT_INVALID_OPERATION;
596     }
597     if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND;  /* DE flag */
598     if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO;    /* ZE flag */
599     if (status & 0x08) return EXCEPTION_FLT_OVERFLOW;          /* OE flag */
600     if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW;         /* UE flag */
601     if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT;    /* PE flag */
602 #endif  /* __i386__ */
603     return EXCEPTION_FLT_INVALID_OPERATION;  /* generic error */
604 }
605
606
607 /**********************************************************************
608  *              EXC_fpe
609  *
610  * Handler for SIGFPE.
611  */
612 static HANDLER_DEF(EXC_fpe)
613 {
614     EXCEPTION_RECORD rec;
615     CONTEXT context;
616
617     HANDLER_INIT();
618
619     EXC_SaveFPU( &context, HANDLER_CONTEXT );
620
621 #ifdef TRAP_sig
622     switch(TRAP_sig(HANDLER_CONTEXT))
623     {
624     case 0:   /* Division by zero exception */
625         rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
626         break;
627     case 9:   /* Coprocessor segment overrun */
628         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
629         break;
630     case 16:  /* Floating point exception */
631         rec.ExceptionCode = EXC_GetFPUCode( &context );
632         break;
633     default:
634         ERR( "Got unexpected trap %ld\n", TRAP_sig(HANDLER_CONTEXT) );
635         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
636         break;
637     }
638 #else  /* TRAP_sig */
639     rec.ExceptionCode = EXC_GetFPUCode( &context );
640 #endif  /* TRAP_sig */
641
642     EXC_SaveContext( &context, HANDLER_CONTEXT );
643     rec.ExceptionFlags   = EH_NONCONTINUABLE;
644     rec.ExceptionRecord  = NULL;
645     rec.ExceptionAddress = GET_IP(&context);
646     rec.NumberParameters = 0;
647     REGS_FUNC(RtlRaiseException)( &rec, &context );
648     EXC_RestoreContext( &context, HANDLER_CONTEXT );
649     EXC_RestoreFPU( &context, HANDLER_CONTEXT );
650 }
651
652
653 /**********************************************************************
654  *              EXC_int
655  *
656  * Handler for SIGINT.
657  */
658 static HANDLER_DEF(EXC_int)
659 {
660     EXCEPTION_RECORD rec;
661     CONTEXT context;
662
663     HANDLER_INIT();
664
665     EXC_SaveContext( &context, HANDLER_CONTEXT );
666     rec.ExceptionCode    = CONTROL_C_EXIT;
667     rec.ExceptionFlags   = EH_NONCONTINUABLE;
668     rec.ExceptionRecord  = NULL;
669     rec.ExceptionAddress = GET_IP(&context);
670     rec.NumberParameters = 0;
671     REGS_FUNC(RtlRaiseException)( &rec, &context );
672     EXC_RestoreContext( &context, HANDLER_CONTEXT );
673 }
674
675
676 /**********************************************************************
677  *          EXC_InitHandlers
678  *
679  * Initialize the signal handlers for exception handling.
680  */
681 void EXC_InitHandlers(void)
682 {
683     SIGNAL_SetHandler( SIGINT,  (void (*)())EXC_int,  1 );
684     SIGNAL_SetHandler( SIGFPE,  (void (*)())EXC_fpe,  1 );
685     SIGNAL_SetHandler( SIGSEGV, (void (*)())EXC_segv, 1 );
686     SIGNAL_SetHandler( SIGILL,  (void (*)())EXC_segv, 1 );
687 #ifdef SIGBUS
688     SIGNAL_SetHandler( SIGBUS,  (void (*)())EXC_segv, 1 );
689 #endif
690 #ifdef SIGTRAP
691     SIGNAL_SetHandler( SIGTRAP, (void (*)())EXC_trap, 1 );
692 #endif
693     return;
694 }