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