Documentation fixes.
[wine] / dlls / ntdll / signal_i386.c
1 /*
2  * i386 signal handling routines
3  * 
4  * Copyright 1999 Alexandre Julliard
5  */
6
7 #ifdef __i386__
8
9 #include "config.h"
10
11 #include <errno.h>
12 #include <signal.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <unistd.h>
16
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
27
28 #ifdef HAVE_SYS_VM86_H
29 # include <sys/vm86.h>
30 #endif
31
32 #ifdef HAVE_SYS_SIGNAL_H
33 # include <sys/signal.h>
34 #endif
35
36 #include "winnt.h"
37 #include "selectors.h"
38
39 /***********************************************************************
40  * signal context platform-specific definitions
41  */
42
43 #ifdef linux
44 typedef struct
45 {
46     unsigned short sc_gs, __gsh;
47     unsigned short sc_fs, __fsh;
48     unsigned short sc_es, __esh;
49     unsigned short sc_ds, __dsh;
50     unsigned long sc_edi;
51     unsigned long sc_esi;
52     unsigned long sc_ebp;
53     unsigned long sc_esp;
54     unsigned long sc_ebx;
55     unsigned long sc_edx;
56     unsigned long sc_ecx;
57     unsigned long sc_eax;
58     unsigned long sc_trapno;
59     unsigned long sc_err;
60     unsigned long sc_eip;
61     unsigned short sc_cs, __csh;
62     unsigned long sc_eflags;
63     unsigned long esp_at_signal;
64     unsigned short sc_ss, __ssh;
65     unsigned long i387;
66     unsigned long oldmask;
67     unsigned long cr2;
68 } SIGCONTEXT;
69
70 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
71 #define HANDLER_CONTEXT (&__context)
72
73 /* this is the sigaction structure from the Linux 2.1.20 kernel.  */
74 struct kernel_sigaction
75 {
76     void (*ksa_handler)();
77     unsigned long ksa_mask;
78     unsigned long ksa_flags;
79     void *ksa_restorer;
80 };
81
82 /* Similar to the sigaction function in libc, except it leaves alone the
83    restorer field, which is used to specify the signal stack address */
84 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
85                                   struct kernel_sigaction *old )
86 {
87     __asm__ __volatile__( "pushl %%ebx\n\t"
88                           "movl %2,%%ebx\n\t"
89                           "int $0x80\n\t"
90                           "popl %%ebx"
91                           : "=a" (sig)
92                           : "0" (SYS_sigaction), "r" (sig), "c" (new), "d" (old) );
93     if (sig>=0) return 0;
94     errno = -sig;
95     return -1;
96 }
97
98 #ifdef HAVE_SIGALTSTACK
99 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
100 static inline int wine_sigaltstack( const struct sigaltstack *new,
101                                     struct sigaltstack *old )
102 {
103     int ret;
104     __asm__ __volatile__( "pushl %%ebx\n\t"
105                           "movl %2,%%ebx\n\t"
106                           "int $0x80\n\t"
107                           "popl %%ebx"
108                           : "=a" (ret)
109                           : "0" (SYS_sigaltstack), "r" (new), "c" (old) );
110     if (ret >= 0) return 0;
111     errno = -ret;
112     return -1;
113 }
114 #endif
115
116 int vm86_enter( struct vm86plus_struct *ptr );
117 void vm86_return();
118 __ASM_GLOBAL_FUNC(vm86_enter,
119                   "pushl %ebp\n\t"
120                   "movl %esp, %ebp\n\t"
121                   "movl $166,%eax\n\t"  /*SYS_vm86*/
122                   "movl 8(%ebp),%ecx\n\t"
123                   "pushl %ebx\n\t"
124                   "movl $1,%ebx\n\t"    /*VM86_ENTER*/
125                   "pushl %ecx\n\t"      /* put vm86plus_struct ptr somewhere we can find it */
126                   "pushl %fs\n\t"
127                   "int $0x80\n"
128                   ".globl " __ASM_NAME("vm86_return") "\n\t"
129                   ".type " __ASM_NAME("vm86_return") ",@function\n"
130                   __ASM_NAME("vm86_return") ":\n\t"
131                   "popl %fs\n\t"
132                   "popl %ecx\n\t"
133                   "popl %ebx\n\t"
134                   "popl %ebp\n\t"
135                   "ret" );
136
137 #define __HAVE_VM86
138
139 #endif  /* linux */
140
141 #ifdef BSDI
142
143 #define EAX_sig(context)     ((context)->tf_eax)
144 #define EBX_sig(context)     ((context)->tf_ebx)
145 #define ECX_sig(context)     ((context)->tf_ecx)
146 #define EDX_sig(context)     ((context)->tf_edx)
147 #define ESI_sig(context)     ((context)->tf_esi)
148 #define EDI_sig(context)     ((context)->tf_edi)
149 #define EBP_sig(context)     ((context)->tf_ebp)
150                             
151 #define CS_sig(context)      ((context)->tf_cs)
152 #define DS_sig(context)      ((context)->tf_ds)
153 #define ES_sig(context)      ((context)->tf_es)
154 #define SS_sig(context)      ((context)->tf_ss)
155
156 #include <machine/frame.h>
157 typedef struct trapframe SIGCONTEXT;
158
159 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
160 #define HANDLER_CONTEXT __context
161
162 #define EFL_sig(context)     ((context)->tf_eflags)
163
164 #define EIP_sig(context)     (*((unsigned long*)&(context)->tf_eip))
165 #define ESP_sig(context)     (*((unsigned long*)&(context)->tf_esp))
166
167 #endif /* bsdi */
168
169 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
170
171 typedef struct sigcontext SIGCONTEXT;
172
173 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
174 #define HANDLER_CONTEXT __context
175
176 #endif  /* FreeBSD */
177
178 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
179
180 #ifdef _SCO_DS
181 #include <sys/regset.h>
182 #endif
183 /* Solaris kludge */
184 #undef ERR
185 #include <sys/ucontext.h>
186 #undef ERR
187 typedef struct ucontext SIGCONTEXT;
188
189 #define HANDLER_DEF(name) void name( int __signal, void *__siginfo, SIGCONTEXT *__context )
190 #define HANDLER_CONTEXT __context
191
192 #endif  /* svr4 || SCO_DS */
193
194 #ifdef __EMX__
195
196 typedef struct
197 {
198     unsigned long ContextFlags;
199     FLOATING_SAVE_AREA sc_float;
200     unsigned long sc_gs;
201     unsigned long sc_fs;
202     unsigned long sc_es;
203     unsigned long sc_ds;
204     unsigned long sc_edi;
205     unsigned long sc_esi;
206     unsigned long sc_eax;
207     unsigned long sc_ebx;
208     unsigned long sc_ecx;
209     unsigned long sc_edx;
210     unsigned long sc_ebp;
211     unsigned long sc_eip;
212     unsigned long sc_cs;
213     unsigned long sc_eflags;
214     unsigned long sc_esp;
215     unsigned long sc_ss;
216 } SIGCONTEXT;
217
218 #endif  /* __EMX__ */
219
220
221 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMX__)
222
223 #define EAX_sig(context)     ((context)->sc_eax)
224 #define EBX_sig(context)     ((context)->sc_ebx)
225 #define ECX_sig(context)     ((context)->sc_ecx)
226 #define EDX_sig(context)     ((context)->sc_edx)
227 #define ESI_sig(context)     ((context)->sc_esi)
228 #define EDI_sig(context)     ((context)->sc_edi)
229 #define EBP_sig(context)     ((context)->sc_ebp)
230                             
231 #define CS_sig(context)      ((context)->sc_cs)
232 #define DS_sig(context)      ((context)->sc_ds)
233 #define ES_sig(context)      ((context)->sc_es)
234 #define SS_sig(context)      ((context)->sc_ss)
235                             
236 /* FS and GS are now in the sigcontext struct of FreeBSD, but not 
237  * saved by the exception handling. duh.
238  * Actually they are in -current (have been for a while), and that
239  * patch now finally has been MFC'd to -stable too (Nov 15 1999).
240  * If you're running a system from the -stable branch older than that,
241  * like a 3.3-RELEASE, grab the patch from the ports tree:
242  * ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/ports/emulators/wine/files/patch-3.3-sys-fsgs
243  * (If its not yet there when you look, go here:
244  * http://www.jelal.kn-bremen.de/freebsd/ports/emulators/wine/files/ )
245  */
246 #ifdef __FreeBSD__
247 #define FS_sig(context)      ((context)->sc_fs)
248 #define GS_sig(context)      ((context)->sc_gs)
249 #endif
250
251 #ifdef linux
252 #define FS_sig(context)      ((context)->sc_fs)
253 #define GS_sig(context)      ((context)->sc_gs)
254 #define CR2_sig(context)     ((context)->cr2)
255 #define TRAP_sig(context)    ((context)->sc_trapno)
256 #define ERROR_sig(context)   ((context)->sc_err)
257 #define FPU_sig(context)     ((FLOATING_SAVE_AREA*)((context)->i387))
258 #endif
259
260 #ifndef __FreeBSD__
261 #define EFL_sig(context)     ((context)->sc_eflags)
262 #else                       
263 #define EFL_sig(context)     ((context)->sc_efl)
264 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge  */
265 #define CR2_sig(context)     ((context)->sc_err)
266 #define TRAP_sig(context)    ((context)->sc_trapno)
267 #endif                      
268
269 #define EIP_sig(context)     (*((unsigned long*)&(context)->sc_eip))
270 #define ESP_sig(context)     (*((unsigned long*)&(context)->sc_esp))
271
272 #endif  /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
273
274 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
275
276 #ifdef _SCO_DS
277 #define gregs regs
278 #endif
279
280 #define EAX_sig(context)     ((context)->uc_mcontext.gregs[EAX])
281 #define EBX_sig(context)     ((context)->uc_mcontext.gregs[EBX])
282 #define ECX_sig(context)     ((context)->uc_mcontext.gregs[ECX])
283 #define EDX_sig(context)     ((context)->uc_mcontext.gregs[EDX])
284 #define ESI_sig(context)     ((context)->uc_mcontext.gregs[ESI])
285 #define EDI_sig(context)     ((context)->uc_mcontext.gregs[EDI])
286 #define EBP_sig(context)     ((context)->uc_mcontext.gregs[EBP])
287                             
288 #define CS_sig(context)      ((context)->uc_mcontext.gregs[CS])
289 #define DS_sig(context)      ((context)->uc_mcontext.gregs[DS])
290 #define ES_sig(context)      ((context)->uc_mcontext.gregs[ES])
291 #define SS_sig(context)      ((context)->uc_mcontext.gregs[SS])
292                             
293 #define FS_sig(context)      ((context)->uc_mcontext.gregs[FS])
294 #define GS_sig(context)      ((context)->uc_mcontext.gregs[GS])
295
296 #define EFL_sig(context)     ((context)->uc_mcontext.gregs[EFL])
297                             
298 #define EIP_sig(context)     ((context)->uc_mcontext.gregs[EIP])
299 #ifdef R_ESP
300 #define ESP_sig(context)     ((context)->uc_mcontext.gregs[R_ESP])
301 #else
302 #define ESP_sig(context)     ((context)->uc_mcontext.gregs[ESP])
303 #endif
304 #ifdef TRAPNO
305 #define TRAP_sig(context)     ((context)->uc_mcontext.gregs[TRAPNO])
306 #endif
307
308 #endif  /* svr4 || SCO_DS */
309
310
311 /* exception code definitions (already defined by FreeBSD/NetBSD) */
312 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
313 #define T_DIVIDE        0   /* Division by zero exception */
314 #define T_TRCTRAP       1   /* Single-step exception */
315 #define T_NMI           2   /* NMI interrupt */
316 #define T_BPTFLT        3   /* Breakpoint exception */
317 #define T_OFLOW         4   /* Overflow exception */
318 #define T_BOUND         5   /* Bound range exception */
319 #define T_PRIVINFLT     6   /* Invalid opcode exception */
320 #define T_DNA           7   /* Device not available exception */
321 #define T_DOUBLEFLT     8   /* Double fault exception */
322 #define T_FPOPFLT       9   /* Coprocessor segment overrun */
323 #define T_TSSFLT        10  /* Invalid TSS exception */
324 #define T_SEGNPFLT      11  /* Segment not present exception */
325 #define T_STKFLT        12  /* Stack fault */
326 #define T_PROTFLT       13  /* General protection fault */
327 #define T_PAGEFLT       14  /* Page fault */
328 #define T_RESERVED      15  /* Unknown exception */
329 #define T_ARITHTRAP     16  /* Floating point exception */
330 #define T_ALIGNFLT      17  /* Alignment check exception */
331 #define T_MCHK          18  /* Machine check exception */
332 #define T_CACHEFLT      19  /* Cache flush exception */
333 #endif
334 #if defined(__NetBSD__)
335 #define T_MCHK          19  /* Machine check exception */
336 #endif
337
338 #define T_UNKNOWN     (-1)  /* Unknown fault (TRAP_sig not defined) */
339
340 #include "wine/exception.h"
341 #include "winnt.h"
342 #include "stackframe.h"
343 #include "global.h"
344 #include "miscemu.h"
345 #include "ntddk.h"
346 #include "syslevel.h"
347 #include "debugtools.h"
348
349 DEFAULT_DEBUG_CHANNEL(seh);
350
351 static sigset_t all_sigs;
352
353
354 /***********************************************************************
355  *           get_trap_code
356  *
357  * Get the trap code for a signal.
358  */
359 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
360 {
361 #ifdef TRAP_sig
362     return TRAP_sig(sigcontext);
363 #else
364     return T_UNKNOWN;  /* unknown trap code */
365 #endif
366 }
367
368 /***********************************************************************
369  *           get_error_code
370  *
371  * Get the error code for a signal.
372  */
373 static inline int get_error_code( const SIGCONTEXT *sigcontext )
374 {
375 #ifdef ERROR_sig
376     return ERROR_sig(sigcontext);
377 #else
378     return 0;
379 #endif
380 }
381
382 /***********************************************************************
383  *           get_cr2_value
384  *
385  * Get the CR2 value for a signal.
386  */
387 static inline void *get_cr2_value( const SIGCONTEXT *sigcontext )
388 {
389 #ifdef CR2_sig
390     return (void *)CR2_sig(sigcontext);
391 #else
392     return NULL;
393 #endif
394 }
395
396
397 #ifdef __HAVE_VM86
398 /***********************************************************************
399  *           save_vm86_context
400  *
401  * Set the register values from a vm86 structure.
402  */
403 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
404 {
405     context->Eax    = vm86->regs.eax;
406     context->Ebx    = vm86->regs.ebx;
407     context->Ecx    = vm86->regs.ecx;
408     context->Edx    = vm86->regs.edx;
409     context->Esi    = vm86->regs.esi;
410     context->Edi    = vm86->regs.edi;
411     context->Esp    = vm86->regs.esp;
412     context->Ebp    = vm86->regs.ebp;
413     context->Eip    = vm86->regs.eip;
414     context->SegCs  = vm86->regs.cs;
415     context->SegDs  = vm86->regs.ds;
416     context->SegEs  = vm86->regs.es;
417     context->SegFs  = vm86->regs.fs;
418     context->SegGs  = vm86->regs.gs;
419     context->SegSs  = vm86->regs.ss;
420     context->EFlags = vm86->regs.eflags;
421 }
422
423
424 /***********************************************************************
425  *           restore_vm86_context
426  *
427  * Build a vm86 structure from the register values.
428  */
429 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
430 {
431     vm86->regs.eax    = context->Eax;
432     vm86->regs.ebx    = context->Ebx;
433     vm86->regs.ecx    = context->Ecx;
434     vm86->regs.edx    = context->Edx;
435     vm86->regs.esi    = context->Esi;
436     vm86->regs.edi    = context->Edi;
437     vm86->regs.esp    = context->Esp;
438     vm86->regs.ebp    = context->Ebp;
439     vm86->regs.eip    = context->Eip;
440     vm86->regs.cs     = context->SegCs;
441     vm86->regs.ds     = context->SegDs;
442     vm86->regs.es     = context->SegEs;
443     vm86->regs.fs     = context->SegFs;
444     vm86->regs.gs     = context->SegGs;
445     vm86->regs.ss     = context->SegSs;
446     vm86->regs.eflags = context->EFlags;
447 }
448 #endif /* __HAVE_VM86 */
449
450
451 /***********************************************************************
452  *           save_context
453  *
454  * Set the register values from a sigcontext. 
455  */
456 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
457 {
458     WORD fs;
459     /* get %fs at time of the fault */
460 #ifdef FS_sig
461     fs = FS_sig(sigcontext);
462 #else
463     fs = __get_fs();
464 #endif
465     context->SegFs = fs;
466
467     /* now restore a proper %fs for the fault handler */
468     if (!IS_SELECTOR_SYSTEM(CS_sig(sigcontext)))  /* 16-bit mode */
469     {
470         fs = SYSLEVEL_Win16CurrentTeb;
471     }
472 #ifdef __HAVE_VM86
473     else if ((void *)EIP_sig(sigcontext) == vm86_return)  /* vm86 mode */
474     {
475         /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
476         struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
477         /* fetch the saved %fs on the stack */
478         fs = *(unsigned int *)ESP_sig(sigcontext);
479         __set_fs(fs);
480         /* get context from vm86 struct */
481         save_vm86_context( context, vm86 );
482         return;
483     }
484 #endif  /* __HAVE_VM86 */
485
486     __set_fs(fs);
487
488     context->Eax    = EAX_sig(sigcontext);
489     context->Ebx    = EBX_sig(sigcontext);
490     context->Ecx    = ECX_sig(sigcontext);
491     context->Edx    = EDX_sig(sigcontext);
492     context->Esi    = ESI_sig(sigcontext);
493     context->Edi    = EDI_sig(sigcontext);
494     context->Ebp    = EBP_sig(sigcontext);
495     context->EFlags = EFL_sig(sigcontext);
496     context->Eip    = EIP_sig(sigcontext);
497     context->Esp    = ESP_sig(sigcontext);
498     context->SegCs  = LOWORD(CS_sig(sigcontext));
499     context->SegDs  = LOWORD(DS_sig(sigcontext));
500     context->SegEs  = LOWORD(ES_sig(sigcontext));
501     context->SegSs  = LOWORD(SS_sig(sigcontext));
502 #ifdef GS_sig
503     context->SegGs  = LOWORD(GS_sig(sigcontext));
504 #else
505     context->SegGs  = __get_gs();
506 #endif
507 }
508
509
510 /***********************************************************************
511  *           restore_context
512  *
513  * Build a sigcontext from the register values.
514  */
515 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
516 {
517 #ifdef __HAVE_VM86
518     /* check if exception occurred in vm86 mode */
519     if ((void *)EIP_sig(sigcontext) == vm86_return &&
520         IS_SELECTOR_SYSTEM(CS_sig(sigcontext)))
521     {
522         /* retrieve pointer to vm86plus struct that was stored in vm86_enter */
523         struct vm86plus_struct *vm86 = *(struct vm86plus_struct **)(ESP_sig(sigcontext) + sizeof(int));
524         restore_vm86_context( context, vm86 );
525         return;
526     }
527 #endif /* __HAVE_VM86 */
528
529     EAX_sig(sigcontext) = context->Eax;
530     EBX_sig(sigcontext) = context->Ebx;
531     ECX_sig(sigcontext) = context->Ecx;
532     EDX_sig(sigcontext) = context->Edx;
533     ESI_sig(sigcontext) = context->Esi;
534     EDI_sig(sigcontext) = context->Edi;
535     EBP_sig(sigcontext) = context->Ebp;
536     EFL_sig(sigcontext) = context->EFlags;
537     EIP_sig(sigcontext) = context->Eip;
538     ESP_sig(sigcontext) = context->Esp;
539     CS_sig(sigcontext)  = context->SegCs;
540     DS_sig(sigcontext)  = context->SegDs;
541     ES_sig(sigcontext)  = context->SegEs;
542     SS_sig(sigcontext)  = context->SegSs;
543 #ifdef FS_sig
544     FS_sig(sigcontext)  = context->SegFs;
545 #else
546     __set_fs( context->SegFs );
547 #endif
548 #ifdef GS_sig
549     GS_sig(sigcontext)  = context->SegGs;
550 #else
551     __set_gs( context->SegGs );
552 #endif
553 }
554
555
556 /***********************************************************************
557  *           save_fpu
558  *
559  * Set the FPU context from a sigcontext. 
560  */
561 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
562 {
563 #ifdef FPU_sig
564     if (FPU_sig(sigcontext))
565     {
566         context->FloatSave = *FPU_sig(sigcontext);
567         return;
568     }
569 #endif  /* FPU_sig */
570 #ifdef __GNUC__
571     __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
572 #endif  /* __GNUC__ */
573 }
574
575
576 /***********************************************************************
577  *           restore_fpu
578  *
579  * Restore the FPU context to a sigcontext. 
580  */
581 inline static void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
582 {
583     /* reset the current interrupt status */
584     context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
585 #ifdef FPU_sig
586     if (FPU_sig(sigcontext))
587     {
588         *FPU_sig(sigcontext) = context->FloatSave;
589         return;
590     }
591 #endif  /* FPU_sig */
592 #ifdef __GNUC__
593     /* avoid nested exceptions */
594     __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
595 #endif  /* __GNUC__ */
596 }
597
598
599 /**********************************************************************
600  *              get_fpu_code
601  *
602  * Get the FPU exception code from the FPU status.
603  */
604 static inline DWORD get_fpu_code( const CONTEXT *context )
605 {
606     DWORD status = context->FloatSave.StatusWord;
607
608     if (status & 0x01)  /* IE */
609     {
610         if (status & 0x40)  /* SF */
611             return EXCEPTION_FLT_STACK_CHECK;
612         else
613             return EXCEPTION_FLT_INVALID_OPERATION;
614     }
615     if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND;  /* DE flag */
616     if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO;    /* ZE flag */
617     if (status & 0x08) return EXCEPTION_FLT_OVERFLOW;          /* OE flag */
618     if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW;         /* UE flag */
619     if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT;    /* PE flag */
620     return EXCEPTION_FLT_INVALID_OPERATION;  /* generic error */
621 }
622
623
624 /***********************************************************************
625  *           SIGNAL_Unblock
626  *
627  * Unblock signals. Called from EXC_RtlRaiseException.
628  */
629 void SIGNAL_Unblock( void )
630 {
631     sigprocmask( SIG_UNBLOCK, &all_sigs, NULL );
632 }
633
634
635 /**********************************************************************
636  *              do_segv
637  *
638  * Implementation of SIGSEGV handler.
639  */
640 static void do_segv( CONTEXT *context, int trap_code, void *cr2, int err_code )
641 {
642     EXCEPTION_RECORD rec;
643     DWORD page_fault_code = EXCEPTION_ACCESS_VIOLATION;
644
645 #ifdef CR2_sig
646     /* we want the page-fault case to be fast */
647     if (trap_code == T_PAGEFLT)
648         if (!(page_fault_code = VIRTUAL_HandleFault( cr2 ))) return;
649 #endif
650
651     rec.ExceptionRecord  = NULL;
652     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
653     rec.ExceptionAddress = (LPVOID)context->Eip;
654     rec.NumberParameters = 0;
655
656     switch(trap_code)
657     {
658     case T_OFLOW:   /* Overflow exception */
659         rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
660         break;
661     case T_BOUND:   /* Bound range exception */
662         rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
663         break;
664     case T_PRIVINFLT:   /* Invalid opcode exception */
665         rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
666         break;
667     case T_STKFLT:  /* Stack fault */
668         rec.ExceptionCode = EXCEPTION_STACK_OVERFLOW;
669         break;
670     case T_SEGNPFLT:  /* Segment not present exception */
671     case T_PROTFLT:   /* General protection fault */
672     case T_UNKNOWN:   /* Unknown fault code */
673         if (INSTR_EmulateInstruction( context )) return;
674         rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
675         break;
676     case T_PAGEFLT:  /* Page fault */
677 #ifdef CR2_sig
678         rec.NumberParameters = 2;
679         rec.ExceptionInformation[0] = (err_code & 2) != 0;
680         rec.ExceptionInformation[1] = (DWORD)cr2;
681 #endif /* CR2_sig */
682         rec.ExceptionCode = page_fault_code;
683         break;
684     case T_ALIGNFLT:  /* Alignment check exception */
685         /* FIXME: pass through exception handler first? */
686         if (context->EFlags & 0x00040000)
687         {
688             /* Disable AC flag, return */
689             context->EFlags &= ~0x00040000;
690             return;
691         }
692         rec.ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
693         break;
694     default:
695         ERR( "Got unexpected trap %d\n", trap_code );
696         /* fall through */
697     case T_NMI:       /* NMI interrupt */
698     case T_DNA:       /* Device not available exception */
699     case T_DOUBLEFLT: /* Double fault exception */
700     case T_TSSFLT:    /* Invalid TSS exception */
701     case T_RESERVED:  /* Unknown exception */
702     case T_MCHK:      /* Machine check exception */
703 #ifdef T_CACHEFLT
704     case T_CACHEFLT:  /* Cache flush exception */
705 #endif
706         rec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
707         break;
708     }
709     EXC_RtlRaiseException( &rec, context );
710 }
711
712
713 /**********************************************************************
714  *              do_trap
715  *
716  * Implementation of SIGTRAP handler.
717  */
718 static void do_trap( CONTEXT *context, int trap_code )
719 {
720     EXCEPTION_RECORD rec;
721
722     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
723     rec.ExceptionRecord  = NULL;
724     rec.ExceptionAddress = (LPVOID)context->Eip;
725     rec.NumberParameters = 0;
726
727     switch(trap_code)
728     {
729     case T_TRCTRAP:  /* Single-step exception */
730         rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
731         context->EFlags &= ~0x100;  /* clear single-step flag */
732         break;
733     case T_BPTFLT:   /* Breakpoint exception */
734         rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1;  /* back up over the int3 instruction */
735         /* fall through */
736     default:
737         rec.ExceptionCode = EXCEPTION_BREAKPOINT;
738         break;
739     }
740     EXC_RtlRaiseException( &rec, context );
741 }
742
743
744 /**********************************************************************
745  *              do_fpe
746  *
747  * Implementation of SIGFPE handler
748  */
749 static void do_fpe( CONTEXT *context, int trap_code )
750 {
751     EXCEPTION_RECORD rec;
752
753     switch(trap_code)
754     {
755     case T_DIVIDE:   /* Division by zero exception */
756         rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
757         break;
758     case T_FPOPFLT:   /* Coprocessor segment overrun */
759         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
760         break;
761     case T_ARITHTRAP:  /* Floating point exception */
762     case T_UNKNOWN:    /* Unknown fault code */
763         rec.ExceptionCode = get_fpu_code( context );
764         break;
765     default:
766         ERR( "Got unexpected trap %d\n", trap_code );
767         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
768         break;
769     }
770     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
771     rec.ExceptionRecord  = NULL;
772     rec.ExceptionAddress = (LPVOID)context->Eip;
773     rec.NumberParameters = 0;
774     EXC_RtlRaiseException( &rec, context );
775 }
776
777
778 #ifdef __HAVE_VM86
779 /**********************************************************************
780  *              set_vm86_pend
781  *
782  * Handler for SIGUSR2, which we use to set the vm86 pending flag.
783  */
784 static void set_vm86_pend( CONTEXT *context )
785 {
786     EXCEPTION_RECORD rec;
787     TEB *teb = NtCurrentTeb();
788
789     rec.ExceptionCode           = EXCEPTION_VM86_STI;
790     rec.ExceptionFlags          = EXCEPTION_CONTINUABLE;
791     rec.ExceptionRecord         = NULL;
792     rec.NumberParameters        = 1;
793     rec.ExceptionInformation[0] = 0;
794
795     /* __wine_enter_vm86() merges the vm86_pending flag in safely */
796     teb->vm86_pending |= VIP_MASK;
797     /* see if we were in VM86 mode */
798     if (context->EFlags & 0x00020000)
799     {
800         /* seems so, also set flag in signal context */
801         if (context->EFlags & VIP_MASK) return;
802         context->EFlags |= VIP_MASK;
803         if (context->EFlags & VIF_MASK) {
804             /* VIF is set, throw exception */
805             teb->vm86_pending = 0;
806             rec.ExceptionAddress = (LPVOID)context->Eip;
807             EXC_RtlRaiseException( &rec, context );
808         }
809     }
810     else if (teb->vm86_ptr)
811     {
812         /* not in VM86, but possibly setting up for it */
813         struct vm86plus_struct *vm86 = (struct vm86plus_struct*)(teb->vm86_ptr);
814         if (vm86->regs.eflags & VIP_MASK) return;
815         vm86->regs.eflags |= VIP_MASK;
816         if (vm86->regs.eflags & VIF_MASK) {
817             /* VIF is set, throw exception */
818             CONTEXT vcontext;
819             teb->vm86_pending = 0;
820             save_vm86_context( &vcontext, vm86 );
821             rec.ExceptionAddress = (LPVOID)vcontext.Eip;
822             EXC_RtlRaiseException( &rec, &vcontext );
823             restore_vm86_context( &vcontext, vm86 );
824             if (teb->vm86_ctx) {
825                 /* must also save here */
826                 *(CONTEXT*)(teb->vm86_ctx) = vcontext;
827             }
828         }
829     }
830 }
831
832
833 /**********************************************************************
834  *              usr2_handler
835  *
836  * Handler for SIGUSR2.
837  * We use it to signal that the running __wine_enter_vm86() should
838  * immediately set VIP_MASK, causing pending events to be handled
839  * as early as possible.
840  */
841 static HANDLER_DEF(usr2_handler)
842 {
843     CONTEXT context;
844
845     save_context( &context, HANDLER_CONTEXT );
846     set_vm86_pend( &context );
847     restore_context( &context, HANDLER_CONTEXT );
848 }
849
850
851 /**********************************************************************
852  *              alrm_handler
853  *
854  * Handler for SIGALRM.
855  * Increases the alarm counter and sets the vm86 pending flag.
856  */
857 static HANDLER_DEF(alrm_handler)
858 {
859     CONTEXT context;
860
861     save_context( &context, HANDLER_CONTEXT );
862     NtCurrentTeb()->alarms++;
863     set_vm86_pend( &context );
864     restore_context( &context, HANDLER_CONTEXT );
865 }
866 #endif /* __HAVE_VM86 */
867
868
869 /**********************************************************************
870  *              segv_handler
871  *
872  * Handler for SIGSEGV and related errors.
873  */
874 static HANDLER_DEF(segv_handler)
875 {
876     CONTEXT context;
877     save_context( &context, HANDLER_CONTEXT );
878     do_segv( &context, get_trap_code(HANDLER_CONTEXT),
879              get_cr2_value(HANDLER_CONTEXT), get_error_code(HANDLER_CONTEXT) );
880     restore_context( &context, HANDLER_CONTEXT );
881 }
882
883
884 /**********************************************************************
885  *              trap_handler
886  *
887  * Handler for SIGTRAP.
888  */
889 static HANDLER_DEF(trap_handler)
890 {
891     CONTEXT context;
892     save_context( &context, HANDLER_CONTEXT );
893     do_trap( &context, get_trap_code(HANDLER_CONTEXT) );
894     restore_context( &context, HANDLER_CONTEXT );
895 }
896
897
898 /**********************************************************************
899  *              fpe_handler
900  *
901  * Handler for SIGFPE.
902  */
903 static HANDLER_DEF(fpe_handler)
904 {
905     CONTEXT context;
906     save_fpu( &context, HANDLER_CONTEXT );
907     save_context( &context, HANDLER_CONTEXT );
908     do_fpe( &context, get_trap_code(HANDLER_CONTEXT) );
909     restore_context( &context, HANDLER_CONTEXT );
910     restore_fpu( &context, HANDLER_CONTEXT );
911 }
912
913
914 /**********************************************************************
915  *              int_handler
916  *
917  * Handler for SIGINT.
918  */
919 static HANDLER_DEF(int_handler)
920 {
921     EXCEPTION_RECORD rec;
922     CONTEXT context;
923
924     save_context( &context, HANDLER_CONTEXT );
925     rec.ExceptionCode    = CONTROL_C_EXIT;
926     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
927     rec.ExceptionRecord  = NULL;
928     rec.ExceptionAddress = (LPVOID)context.Eip;
929     rec.NumberParameters = 0;
930     EXC_RtlRaiseException( &rec, &context );
931     restore_context( &context, HANDLER_CONTEXT );
932 }
933
934
935 /***********************************************************************
936  *           set_handler
937  *
938  * Set a signal handler
939  */
940 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
941 {
942     struct sigaction sig_act;
943
944 #ifdef linux
945     if (!have_sigaltstack && NtCurrentTeb()->signal_stack)
946     {
947         struct kernel_sigaction sig_act;
948         sig_act.ksa_handler = func;
949         sig_act.ksa_flags   = SA_RESTART;
950         sig_act.ksa_mask    = (1 << (SIGINT-1)) |
951                               (1 << (SIGALRM-1));
952         /* point to the top of the stack */
953         sig_act.ksa_restorer = (char *)NtCurrentTeb()->signal_stack + SIGNAL_STACK_SIZE;
954         return wine_sigaction( sig, &sig_act, NULL );
955     }
956 #endif  /* linux */
957     sig_act.sa_handler = func;
958     sigemptyset( &sig_act.sa_mask );
959     sigaddset( &sig_act.sa_mask, SIGINT );
960     sigaddset( &sig_act.sa_mask, SIGALRM );
961
962 #ifdef linux
963     sig_act.sa_flags = SA_RESTART;
964 #elif defined (__svr4__) || defined(_SCO_DS)
965     sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
966 #else
967     sig_act.sa_flags = 0;
968 #endif
969
970 #ifdef SA_ONSTACK
971     if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
972 #endif
973     return sigaction( sig, &sig_act, NULL );
974 }
975
976
977 /**********************************************************************
978  *              SIGNAL_Init
979  */
980 BOOL SIGNAL_Init(void)
981 {
982     int have_sigaltstack = 0;
983
984 #ifdef HAVE_SIGALTSTACK
985     struct sigaltstack ss;
986     if ((ss.ss_sp = NtCurrentTeb()->signal_stack))
987     {
988         ss.ss_size  = SIGNAL_STACK_SIZE;
989         ss.ss_flags = 0;
990         if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
991 #ifdef linux
992         /* sigaltstack may fail because the kernel is too old, or
993            because glibc is brain-dead. In the latter case a
994            direct system call should succeed. */
995         else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
996 #endif  /* linux */
997     }
998 #endif  /* HAVE_SIGALTSTACK */
999
1000     sigfillset( &all_sigs );
1001
1002     /* automatic child reaping to avoid zombies */
1003     signal( SIGCHLD, SIG_IGN );
1004
1005     if (set_handler( SIGINT,  have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1006     if (set_handler( SIGFPE,  have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1007     if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1008     if (set_handler( SIGILL,  have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1009 #ifdef SIGBUS
1010     if (set_handler( SIGBUS,  have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1011 #endif
1012 #ifdef SIGTRAP
1013     if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1014 #endif
1015
1016 #ifdef __HAVE_VM86
1017     if (set_handler( SIGALRM, have_sigaltstack, (void (*)())alrm_handler ) == -1) goto error;
1018     if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1019 #endif
1020
1021     return TRUE;
1022
1023  error:
1024     perror("sigaction");
1025     return FALSE;
1026 }
1027
1028
1029 #ifdef __HAVE_VM86
1030 /**********************************************************************
1031  *              __wine_enter_vm86
1032  *
1033  * Enter vm86 mode with the specified register context.
1034  */
1035 void __wine_enter_vm86( CONTEXT *context )
1036 {
1037     EXCEPTION_RECORD rec;
1038     TEB *teb = NtCurrentTeb();
1039     int res;
1040     struct vm86plus_struct vm86;
1041
1042     memset( &vm86, 0, sizeof(vm86) );
1043     for (;;)
1044     {
1045         restore_vm86_context( context, &vm86 );
1046         /* Linux doesn't preserve pending flag (VIP_MASK) on return,
1047          * so save it on entry, just in case */
1048         teb->vm86_pending |= (context->EFlags & VIP_MASK);
1049         /* Work around race conditions with signal handler
1050          * (avoiding sigprocmask for performance reasons) */
1051         teb->vm86_ptr = &vm86;
1052         vm86.regs.eflags |= teb->vm86_pending;
1053         /* Check for VIF|VIP here, since vm86_enter doesn't */
1054         if ((vm86.regs.eflags & (VIF_MASK|VIP_MASK)) == (VIF_MASK|VIP_MASK)) {
1055             teb->vm86_ptr = NULL;
1056             teb->vm86_pending = 0;
1057             context->EFlags |= VIP_MASK;
1058             rec.ExceptionCode = EXCEPTION_VM86_STI;
1059             rec.ExceptionInformation[0] = 0;
1060             goto cancel_vm86;
1061         }
1062
1063         do
1064         {
1065             res = vm86_enter( &vm86 );
1066             if (res < 0)
1067             {
1068                 errno = -res;
1069                 return;
1070             }
1071         } while (VM86_TYPE(res) == VM86_SIGNAL);
1072
1073         teb->vm86_ctx = context;
1074         save_vm86_context( context, &vm86 );
1075         teb->vm86_ptr = NULL;
1076         teb->vm86_ctx = NULL;
1077         context->EFlags |= teb->vm86_pending;
1078
1079         switch(VM86_TYPE(res))
1080         {
1081         case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1082             do_segv( context, T_PROTFLT, 0, 0 );
1083             continue;
1084         case VM86_TRAP: /* return due to DOS-debugger request */
1085             do_trap( context, VM86_ARG(res) );
1086             continue;
1087         case VM86_INTx: /* int3/int x instruction (ARG = x) */
1088             rec.ExceptionCode = EXCEPTION_VM86_INTx;
1089             break;
1090         case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1091             teb->vm86_pending = 0;
1092             rec.ExceptionCode = EXCEPTION_VM86_STI;
1093             break;
1094         case VM86_PICRETURN: /* return due to pending PIC request */
1095             rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1096             break;
1097         default:
1098             ERR( "unhandled result from vm86 mode %x\n", res );
1099             continue;
1100         }
1101         rec.ExceptionInformation[0] = VM86_ARG(res);
1102 cancel_vm86:
1103         rec.ExceptionFlags          = EXCEPTION_CONTINUABLE;
1104         rec.ExceptionRecord         = NULL;
1105         rec.ExceptionAddress        = (LPVOID)context->Eip;
1106         rec.NumberParameters        = 1;
1107         EXC_RtlRaiseException( &rec, context );
1108     }
1109 }
1110
1111 #else /* __HAVE_VM86 */
1112 void __wine_enter_vm86( CONTEXT *context )
1113 {
1114     MESSAGE("vm86 mode not supported on this platform\n");
1115 }
1116 #endif /* __HAVE_VM86 */
1117
1118 /**********************************************************************
1119  *              DbgBreakPoint   (NTDLL.@)
1120  */
1121 void WINAPI DbgBreakPoint(void);
1122 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1123
1124 /**********************************************************************
1125  *              DbgUserBreakPoint   (NTDLL.@)
1126  */
1127 void WINAPI DbgUserBreakPoint(void);
1128 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1129
1130 #endif  /* __i386__ */