Implement asn.1 encoding/decoding of times, with tests.
[wine] / dlls / ntdll / signal_i386.c
1 /*
2  * i386 signal handling routines
3  *
4  * Copyright 1999 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifdef __i386__
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYSCALL_H
39 # include <syscall.h>
40 #else
41 # ifdef HAVE_SYS_SYSCALL_H
42 #  include <sys/syscall.h>
43 # endif
44 #endif
45
46 #ifdef HAVE_SYS_VM86_H
47 # include <sys/vm86.h>
48 #endif
49
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
52 #endif
53
54 #include "windef.h"
55 #include "winbase.h"
56 #include "thread.h"
57 #include "wine/library.h"
58 #include "ntdll_misc.h"
59
60 /***********************************************************************
61  * signal context platform-specific definitions
62  */
63
64 #ifdef linux
65 typedef struct
66 {
67     unsigned short sc_gs, __gsh;
68     unsigned short sc_fs, __fsh;
69     unsigned short sc_es, __esh;
70     unsigned short sc_ds, __dsh;
71     unsigned long sc_edi;
72     unsigned long sc_esi;
73     unsigned long sc_ebp;
74     unsigned long sc_esp;
75     unsigned long sc_ebx;
76     unsigned long sc_edx;
77     unsigned long sc_ecx;
78     unsigned long sc_eax;
79     unsigned long sc_trapno;
80     unsigned long sc_err;
81     unsigned long sc_eip;
82     unsigned short sc_cs, __csh;
83     unsigned long sc_eflags;
84     unsigned long esp_at_signal;
85     unsigned short sc_ss, __ssh;
86     unsigned long i387;
87     unsigned long oldmask;
88     unsigned long cr2;
89 } SIGCONTEXT;
90
91 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
92 #define HANDLER_CONTEXT (&__context)
93
94 /* this is the sigaction structure from the Linux 2.1.20 kernel.  */
95 struct kernel_sigaction
96 {
97     void (*ksa_handler)();
98     unsigned long ksa_mask;
99     unsigned long ksa_flags;
100     void *ksa_restorer;
101 };
102
103 /* Similar to the sigaction function in libc, except it leaves alone the
104    restorer field, which is used to specify the signal stack address */
105 static inline int wine_sigaction( int sig, struct kernel_sigaction *new,
106                                   struct kernel_sigaction *old )
107 {
108     __asm__ __volatile__( "pushl %%ebx\n\t"
109                           "movl %2,%%ebx\n\t"
110                           "int $0x80\n\t"
111                           "popl %%ebx"
112                           : "=a" (sig)
113                           : "0" (SYS_sigaction), "S" (sig), "c" (new), "d" (old) );
114     if (sig>=0) return 0;
115     errno = -sig;
116     return -1;
117 }
118
119 #ifdef HAVE_SIGALTSTACK
120 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
121 static inline int wine_sigaltstack( const struct sigaltstack *new,
122                                     struct sigaltstack *old )
123 {
124     int ret;
125     __asm__ __volatile__( "pushl %%ebx\n\t"
126                           "movl %2,%%ebx\n\t"
127                           "int $0x80\n\t"
128                           "popl %%ebx"
129                           : "=a" (ret)
130                           : "0" (SYS_sigaltstack), "q" (new), "c" (old) );
131     if (ret >= 0) return 0;
132     errno = -ret;
133     return -1;
134 }
135 #endif
136
137 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
138
139 int vm86_enter( void **vm86_ptr );
140 void vm86_return(void);
141 void vm86_return_end(void);
142 __ASM_GLOBAL_FUNC(vm86_enter,
143                   "pushl %ebp\n\t"
144                   "movl %esp, %ebp\n\t"
145                   "movl $166,%eax\n\t"  /*SYS_vm86*/
146                   "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
147                   "movl (%ecx),%ecx\n\t"
148                   "pushl %ebx\n\t"
149                   "movl $1,%ebx\n\t"    /*VM86_ENTER*/
150                   "pushl %ecx\n\t"      /* put vm86plus_struct ptr somewhere we can find it */
151                   "pushl %fs\n\t"
152                   "pushl %gs\n\t"
153                   "int $0x80\n"
154                   ".globl " __ASM_NAME("vm86_return") "\n\t"
155                   __ASM_FUNC("vm86_return") "\n"
156                   __ASM_NAME("vm86_return") ":\n\t"
157                   "popl %gs\n\t"
158                   "popl %fs\n\t"
159                   "popl %ecx\n\t"
160                   "popl %ebx\n\t"
161                   "popl %ebp\n\t"
162                   "testl %eax,%eax\n\t"
163                   "jl 0f\n\t"
164                   "cmpb $0,%al\n\t" /* VM86_SIGNAL */
165                   "je " __ASM_NAME("vm86_enter") "\n\t"
166                   "0:\n\t"
167                   "movl 4(%esp),%ecx\n\t"  /* vm86_ptr */
168                   "movl $0,(%ecx)\n\t"
169                   ".globl " __ASM_NAME("vm86_return_end") "\n\t"
170                   __ASM_FUNC("vm86_return_end") "\n"
171                   __ASM_NAME("vm86_return_end") ":\n\t"
172                   "ret" );
173
174 #ifdef HAVE_SYS_VM86_H
175 # define __HAVE_VM86
176 #endif
177
178 #endif  /* linux */
179
180 #ifdef BSDI
181
182 #define EAX_sig(context)     ((context)->tf_eax)
183 #define EBX_sig(context)     ((context)->tf_ebx)
184 #define ECX_sig(context)     ((context)->tf_ecx)
185 #define EDX_sig(context)     ((context)->tf_edx)
186 #define ESI_sig(context)     ((context)->tf_esi)
187 #define EDI_sig(context)     ((context)->tf_edi)
188 #define EBP_sig(context)     ((context)->tf_ebp)
189
190 #define CS_sig(context)      ((context)->tf_cs)
191 #define DS_sig(context)      ((context)->tf_ds)
192 #define ES_sig(context)      ((context)->tf_es)
193 #define SS_sig(context)      ((context)->tf_ss)
194
195 #include <machine/frame.h>
196 typedef struct trapframe SIGCONTEXT;
197
198 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
199 #define HANDLER_CONTEXT __context
200
201 #define EFL_sig(context)     ((context)->tf_eflags)
202
203 #define EIP_sig(context)     (*((unsigned long*)&(context)->tf_eip))
204 #define ESP_sig(context)     (*((unsigned long*)&(context)->tf_esp))
205
206 #endif /* bsdi */
207
208 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
209
210 typedef struct sigcontext SIGCONTEXT;
211
212 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
213 #define HANDLER_CONTEXT __context
214
215 #endif  /* *BSD */
216
217 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
218
219 #ifdef _SCO_DS
220 #include <sys/regset.h>
221 #endif
222 /* Solaris kludge */
223 #undef ERR
224 #include <sys/ucontext.h>
225 #undef ERR
226 typedef struct ucontext SIGCONTEXT;
227
228 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
229 #define HANDLER_CONTEXT __context
230
231 #endif  /* svr4 || SCO_DS */
232
233 #ifdef __EMX__
234
235 typedef struct
236 {
237     unsigned long ContextFlags;
238     FLOATING_SAVE_AREA sc_float;
239     unsigned long sc_gs;
240     unsigned long sc_fs;
241     unsigned long sc_es;
242     unsigned long sc_ds;
243     unsigned long sc_edi;
244     unsigned long sc_esi;
245     unsigned long sc_eax;
246     unsigned long sc_ebx;
247     unsigned long sc_ecx;
248     unsigned long sc_edx;
249     unsigned long sc_ebp;
250     unsigned long sc_eip;
251     unsigned long sc_cs;
252     unsigned long sc_eflags;
253     unsigned long sc_esp;
254     unsigned long sc_ss;
255 } SIGCONTEXT;
256
257 #endif  /* __EMX__ */
258
259 #ifdef __CYGWIN__
260
261 /* FIXME: This section is just here so it can compile, it's most likely
262  * completely wrong. */
263
264 typedef struct
265 {
266     unsigned short sc_gs, __gsh;
267     unsigned short sc_fs, __fsh;
268     unsigned short sc_es, __esh;
269     unsigned short sc_ds, __dsh;
270     unsigned long sc_edi;
271     unsigned long sc_esi;
272     unsigned long sc_ebp;
273     unsigned long sc_esp;
274     unsigned long sc_ebx;
275     unsigned long sc_edx;
276     unsigned long sc_ecx;
277     unsigned long sc_eax;
278     unsigned long sc_trapno;
279     unsigned long sc_err;
280     unsigned long sc_eip;
281     unsigned short sc_cs, __csh;
282     unsigned long sc_eflags;
283     unsigned long esp_at_signal;
284     unsigned short sc_ss, __ssh;
285     unsigned long i387;
286     unsigned long oldmask;
287     unsigned long cr2;
288 } SIGCONTEXT;
289
290 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
291 #define HANDLER_CONTEXT (&__context)
292
293 #endif /* __CYGWIN__ */
294
295 #ifdef __APPLE__
296 # include <sys/ucontext.h>
297 # include <sys/types.h>
298 # include <signal.h>
299
300 typedef siginfo_t siginfo;
301 typedef struct ucontext SIGCONTEXT;
302
303 # define HANDLER_DEF(name) void name( int __signal, siginfo *__siginfo, SIGCONTEXT *__context )
304 # define HANDLER_CONTEXT (__context)
305
306 #endif /* __APPLE__ */
307
308 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
309     defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
310
311 #define EAX_sig(context)     ((context)->sc_eax)
312 #define EBX_sig(context)     ((context)->sc_ebx)
313 #define ECX_sig(context)     ((context)->sc_ecx)
314 #define EDX_sig(context)     ((context)->sc_edx)
315 #define ESI_sig(context)     ((context)->sc_esi)
316 #define EDI_sig(context)     ((context)->sc_edi)
317 #define EBP_sig(context)     ((context)->sc_ebp)
318
319 #define CS_sig(context)      ((context)->sc_cs)
320 #define DS_sig(context)      ((context)->sc_ds)
321 #define ES_sig(context)      ((context)->sc_es)
322 #define FS_sig(context)      ((context)->sc_fs)
323 #define GS_sig(context)      ((context)->sc_gs)
324 #define SS_sig(context)      ((context)->sc_ss)
325
326 #define TRAP_sig(context)    ((context)->sc_trapno)
327
328 #ifdef __NetBSD__
329 #define ERROR_sig(context)   ((context)->sc_err)
330 #endif
331
332 #ifdef linux
333 #define ERROR_sig(context)   ((context)->sc_err)
334 #define FPU_sig(context)     ((FLOATING_SAVE_AREA*)((context)->i387))
335 #define FAULT_ADDRESS        ((void *)HANDLER_CONTEXT->cr2)
336 #endif
337
338 #ifdef __FreeBSD__
339 #define EFL_sig(context)     ((context)->sc_efl)
340 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge  */
341 #define FAULT_ADDRESS        ((void *)HANDLER_CONTEXT->sc_err)
342 #else
343 #define EFL_sig(context)     ((context)->sc_eflags)
344 #endif
345
346 #define EIP_sig(context)     (*((unsigned long*)&(context)->sc_eip))
347 #define ESP_sig(context)     (*((unsigned long*)&(context)->sc_esp))
348
349 #endif  /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
350
351 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
352
353 #ifdef _SCO_DS
354 #define gregs regs
355 #endif
356
357 #define EAX_sig(context)     ((context)->uc_mcontext.gregs[EAX])
358 #define EBX_sig(context)     ((context)->uc_mcontext.gregs[EBX])
359 #define ECX_sig(context)     ((context)->uc_mcontext.gregs[ECX])
360 #define EDX_sig(context)     ((context)->uc_mcontext.gregs[EDX])
361 #define ESI_sig(context)     ((context)->uc_mcontext.gregs[ESI])
362 #define EDI_sig(context)     ((context)->uc_mcontext.gregs[EDI])
363 #define EBP_sig(context)     ((context)->uc_mcontext.gregs[EBP])
364
365 #define CS_sig(context)      ((context)->uc_mcontext.gregs[CS])
366 #define DS_sig(context)      ((context)->uc_mcontext.gregs[DS])
367 #define ES_sig(context)      ((context)->uc_mcontext.gregs[ES])
368 #define SS_sig(context)      ((context)->uc_mcontext.gregs[SS])
369
370 #define FS_sig(context)      ((context)->uc_mcontext.gregs[FS])
371 #define GS_sig(context)      ((context)->uc_mcontext.gregs[GS])
372
373 #define EFL_sig(context)     ((context)->uc_mcontext.gregs[EFL])
374
375 #define EIP_sig(context)     ((context)->uc_mcontext.gregs[EIP])
376 #ifdef UESP
377 #define ESP_sig(context)     ((context)->uc_mcontext.gregs[UESP])
378 #elif defined(R_ESP)
379 #define ESP_sig(context)     ((context)->uc_mcontext.gregs[R_ESP])
380 #else
381 #define ESP_sig(context)     ((context)->uc_mcontext.gregs[ESP])
382 #endif
383 #ifdef TRAPNO
384 #define TRAP_sig(context)     ((context)->uc_mcontext.gregs[TRAPNO])
385 #endif
386
387 #define FAULT_ADDRESS         (__siginfo->si_addr)
388
389 #endif  /* svr4 || SCO_DS */
390
391 #ifdef __APPLE__
392 #define EAX_sig(context)     ((context)->uc_mcontext.sc_eax)
393 #define EBX_sig(context)     ((context)->uc_mcontext.sc_ebx)
394 #define ECX_sig(context)     ((context)->uc_mcontext.sc_ecx)
395 #define EDX_sig(context)     ((context)->uc_mcontext.sc_edx)
396 #define ESI_sig(context)     ((context)->uc_mcontext.sc_esi)
397 #define EDI_sig(context)     ((context)->uc_mcontext.sc_edi)
398 #define EBP_sig(context)     ((context)->uc_mcontext.sc_ebp)
399
400 #define CS_sig(context)      ((context)->uc_mcontext.sc_cs)
401 #define DS_sig(context)      ((context)->uc_mcontext.sc_ds)
402 #define ES_sig(context)      ((context)->uc_mcontext.sc_es)
403 #define FS_sig(context)      ((context)->uc_mcontext.sc_fs)
404 #define GS_sig(context)      ((context)->uc_mcontext.sc_gs)
405 #define SS_sig(context)      ((context)->uc_mcontext.sc_ss)
406
407 #define EFL_sig(context)     ((context)->uc_mcontext.sc_eflags)
408
409 #define EIP_sig(context)     (*((unsigned long*)&(context)->uc_mcontext.sc_eip))
410 #define ESP_sig(context)     (*((unsigned long*)&(context)->uc_mcontext.sc_esp))
411
412 #endif /* __APPLE__ */
413
414 /* exception code definitions (already defined by FreeBSD/NetBSD) */
415 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
416 #define T_DIVIDE        0   /* Division by zero exception */
417 #define T_TRCTRAP       1   /* Single-step exception */
418 #define T_NMI           2   /* NMI interrupt */
419 #define T_BPTFLT        3   /* Breakpoint exception */
420 #define T_OFLOW         4   /* Overflow exception */
421 #define T_BOUND         5   /* Bound range exception */
422 #define T_PRIVINFLT     6   /* Invalid opcode exception */
423 #define T_DNA           7   /* Device not available exception */
424 #define T_DOUBLEFLT     8   /* Double fault exception */
425 #define T_FPOPFLT       9   /* Coprocessor segment overrun */
426 #define T_TSSFLT        10  /* Invalid TSS exception */
427 #define T_SEGNPFLT      11  /* Segment not present exception */
428 #define T_STKFLT        12  /* Stack fault */
429 #define T_PROTFLT       13  /* General protection fault */
430 #define T_PAGEFLT       14  /* Page fault */
431 #define T_RESERVED      15  /* Unknown exception */
432 #define T_ARITHTRAP     16  /* Floating point exception */
433 #define T_ALIGNFLT      17  /* Alignment check exception */
434 #define T_MCHK          18  /* Machine check exception */
435 #define T_CACHEFLT      19  /* Cache flush exception */
436 #endif
437 #if defined(__NetBSD__)
438 #define T_MCHK          19  /* Machine check exception */
439 #endif
440
441 #define T_UNKNOWN     (-1)  /* Unknown fault (TRAP_sig not defined) */
442
443 #include "wine/exception.h"
444 #include "wine/debug.h"
445
446 WINE_DEFAULT_DEBUG_CHANNEL(seh);
447
448 typedef int (*wine_signal_handler)(unsigned int sig);
449
450 static wine_signal_handler handlers[256];
451
452 extern void DECLSPEC_NORETURN __wine_call_from_32_restore_regs( CONTEXT context );
453
454
455 /***********************************************************************
456  *           dispatch_signal
457  */
458 inline static int dispatch_signal(unsigned int sig)
459 {
460     if (handlers[sig] == NULL) return 0;
461     return handlers[sig](sig);
462 }
463
464
465 /***********************************************************************
466  *           get_trap_code
467  *
468  * Get the trap code for a signal.
469  */
470 static inline int get_trap_code( const SIGCONTEXT *sigcontext )
471 {
472 #ifdef TRAP_sig
473     return TRAP_sig(sigcontext);
474 #else
475     return T_UNKNOWN;  /* unknown trap code */
476 #endif
477 }
478
479 /***********************************************************************
480  *           get_error_code
481  *
482  * Get the error code for a signal.
483  */
484 static inline int get_error_code( const SIGCONTEXT *sigcontext )
485 {
486 #ifdef ERROR_sig
487     return ERROR_sig(sigcontext);
488 #else
489     return 0;
490 #endif
491 }
492
493 /***********************************************************************
494  *           get_signal_stack
495  *
496  * Get the base of the signal stack for the current thread.
497  */
498 static inline void *get_signal_stack(void)
499 {
500     return (char *)NtCurrentTeb() + 4096;
501 }
502
503
504 /***********************************************************************
505  *           get_current_teb
506  *
507  * Get the current teb based on the stack pointer.
508  */
509 static inline TEB *get_current_teb(void)
510 {
511     unsigned long esp;
512     __asm__("movl %%esp,%0" : "=g" (esp) );
513     return (TEB *)((esp & ~4095) - 4096);
514 }
515
516
517 #ifdef __HAVE_VM86
518 /***********************************************************************
519  *           save_vm86_context
520  *
521  * Set the register values from a vm86 structure.
522  */
523 static void save_vm86_context( CONTEXT *context, const struct vm86plus_struct *vm86 )
524 {
525     context->Eax    = vm86->regs.eax;
526     context->Ebx    = vm86->regs.ebx;
527     context->Ecx    = vm86->regs.ecx;
528     context->Edx    = vm86->regs.edx;
529     context->Esi    = vm86->regs.esi;
530     context->Edi    = vm86->regs.edi;
531     context->Esp    = vm86->regs.esp;
532     context->Ebp    = vm86->regs.ebp;
533     context->Eip    = vm86->regs.eip;
534     context->SegCs  = vm86->regs.cs;
535     context->SegDs  = vm86->regs.ds;
536     context->SegEs  = vm86->regs.es;
537     context->SegFs  = vm86->regs.fs;
538     context->SegGs  = vm86->regs.gs;
539     context->SegSs  = vm86->regs.ss;
540     context->EFlags = vm86->regs.eflags;
541 }
542
543
544 /***********************************************************************
545  *           restore_vm86_context
546  *
547  * Build a vm86 structure from the register values.
548  */
549 static void restore_vm86_context( const CONTEXT *context, struct vm86plus_struct *vm86 )
550 {
551     vm86->regs.eax    = context->Eax;
552     vm86->regs.ebx    = context->Ebx;
553     vm86->regs.ecx    = context->Ecx;
554     vm86->regs.edx    = context->Edx;
555     vm86->regs.esi    = context->Esi;
556     vm86->regs.edi    = context->Edi;
557     vm86->regs.esp    = context->Esp;
558     vm86->regs.ebp    = context->Ebp;
559     vm86->regs.eip    = context->Eip;
560     vm86->regs.cs     = context->SegCs;
561     vm86->regs.ds     = context->SegDs;
562     vm86->regs.es     = context->SegEs;
563     vm86->regs.fs     = context->SegFs;
564     vm86->regs.gs     = context->SegGs;
565     vm86->regs.ss     = context->SegSs;
566     vm86->regs.eflags = context->EFlags;
567 }
568
569
570 /**********************************************************************
571  *              merge_vm86_pending_flags
572  *
573  * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
574  * raises exception if there are pending events and VIF flag
575  * has been turned on.
576  *
577  * Called from __wine_enter_vm86 because vm86_enter
578  * doesn't check for pending events. 
579  *
580  * Called from raise_vm86_sti_exception to check for
581  * pending events in a signal safe way.
582  */
583 static void merge_vm86_pending_flags( EXCEPTION_RECORD *rec )
584 {
585     BOOL check_pending = TRUE;
586     struct vm86plus_struct *vm86 =
587         (struct vm86plus_struct*)(ntdll_get_thread_data()->vm86_ptr);
588
589     /*
590      * In order to prevent a race when SIGUSR2 occurs while
591      * we are returning from exception handler, pending events
592      * will be rechecked after each raised exception.
593      */
594     while (check_pending && NtCurrentTeb()->vm86_pending)
595     {
596         check_pending = FALSE;
597         ntdll_get_thread_data()->vm86_ptr = NULL;
598             
599         /*
600          * If VIF is set, throw exception.
601          * Note that SIGUSR2 may turn VIF flag off so
602          * VIF check must occur only when TEB.vm86_ptr is NULL.
603          */
604         if (vm86->regs.eflags & VIF_MASK)
605         {
606             CONTEXT vcontext;
607             save_vm86_context( &vcontext, vm86 );
608             
609             rec->ExceptionCode    = EXCEPTION_VM86_STI;
610             rec->ExceptionFlags   = EXCEPTION_CONTINUABLE;
611             rec->ExceptionRecord  = NULL;
612             rec->NumberParameters = 0;
613             rec->ExceptionAddress = (LPVOID)vcontext.Eip;
614
615             vcontext.EFlags &= ~VIP_MASK;
616             NtCurrentTeb()->vm86_pending = 0;
617             __regs_RtlRaiseException( rec, &vcontext );
618
619             restore_vm86_context( &vcontext, vm86 );
620             check_pending = TRUE;
621         }
622
623         ntdll_get_thread_data()->vm86_ptr = vm86;
624     }
625
626     /*
627      * Merge VIP flags in a signal safe way. This requires
628      * that the following operation compiles into atomic
629      * instruction.
630      */
631     vm86->regs.eflags |= NtCurrentTeb()->vm86_pending;
632 }
633 #endif /* __HAVE_VM86 */
634
635
636 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
637
638
639 /***********************************************************************
640  *           init_handler
641  *
642  * Handler initialization when the full context is not needed.
643  */
644 static void *init_handler( const SIGCONTEXT *sigcontext )
645 {
646     void *stack = (void *)ESP_sig(sigcontext);
647     TEB *teb = get_current_teb();
648     struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
649
650     wine_set_fs( thread_data->teb_sel );
651
652     /* now restore a proper %gs for the fault handler */
653     if (!wine_ldt_is_system(CS_sig(sigcontext)) ||
654         !wine_ldt_is_system(SS_sig(sigcontext)))  /* 16-bit mode */
655     {
656         /*
657          * Win16 or DOS protected mode. Note that during switch
658          * from 16-bit mode to linear mode, CS may be set to system
659          * segment before FS is restored. Fortunately, in this case
660          * SS is still non-system segment. This is why both CS and SS
661          * are checked.
662          */
663         wine_set_gs( teb->gs_sel );
664         stack = teb->WOW32Reserved;
665     }
666 #ifdef __HAVE_VM86
667     else if ((void *)EIP_sig(sigcontext) == vm86_return)  /* vm86 mode */
668     {
669         unsigned int *int_stack = stack;
670         /* fetch the saved %gs from the stack */
671         wine_set_gs( int_stack[0] );
672     }
673 #endif
674     else  /* 32-bit mode */
675     {
676 #ifdef GS_sig
677         wine_set_gs( GS_sig(sigcontext) );
678 #endif
679     }
680     return stack;
681 }
682
683
684 /***********************************************************************
685  *           save_fpu
686  *
687  * Set the FPU context from a sigcontext.
688  */
689 inline static void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
690 {
691 #ifdef FPU_sig
692     if (FPU_sig(sigcontext))
693     {
694         context->FloatSave = *FPU_sig(sigcontext);
695         return;
696     }
697 #endif  /* FPU_sig */
698 #ifdef __GNUC__
699     __asm__ __volatile__( "fnsave %0; fwait" : "=m" (context->FloatSave) );
700 #endif  /* __GNUC__ */
701 }
702
703
704 /***********************************************************************
705  *           restore_fpu
706  *
707  * Restore the FPU context to a sigcontext.
708  */
709 inline static void restore_fpu( CONTEXT *context )
710 {
711     /* reset the current interrupt status */
712     context->FloatSave.StatusWord &= context->FloatSave.ControlWord | 0xffffff80;
713 #ifdef __GNUC__
714     /* avoid nested exceptions */
715     __asm__ __volatile__( "frstor %0; fwait" : : "m" (context->FloatSave) );
716 #endif  /* __GNUC__ */
717 }
718
719
720 /***********************************************************************
721  *           setup_exception
722  *
723  * Setup a proper stack frame for the raise function, and modify the
724  * sigcontext so that the return from the signal handler will call
725  * the raise function.
726  */
727 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
728 {
729     struct stack_layout
730     {
731         void             *ret_addr;      /* return address from raise_func */
732         EXCEPTION_RECORD *rec_ptr;       /* first arg for raise_func */
733         CONTEXT          *context_ptr;   /* second arg for raise_func */
734         void             *dummy;         /* dummy ret addr for __wine_call_from_32_restore_regs */
735         CONTEXT           context;
736         EXCEPTION_RECORD  rec;
737         DWORD             ebp;
738         DWORD             eip;
739     } *stack;
740
741     WORD fs, gs;
742
743     /* get %fs and %gs at time of the fault */
744 #ifdef FS_sig
745     fs = LOWORD(FS_sig(sigcontext));
746 #else
747     fs = wine_get_fs();
748 #endif
749 #ifdef GS_sig
750     gs = LOWORD(GS_sig(sigcontext));
751 #else
752     gs = wine_get_gs();
753 #endif
754
755     stack = init_handler( sigcontext );
756
757     /* stack sanity checks */
758
759     if ((char *)stack >= (char *)get_signal_stack() &&
760         (char *)stack < (char *)get_signal_stack() + SIGNAL_STACK_SIZE)
761     {
762         ERR( "nested exception on signal stack in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
763              GetCurrentThreadId(), EIP_sig(sigcontext), ESP_sig(sigcontext),
764              NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
765         server_abort_thread(1);
766     }
767
768     if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit + 4096 ||
769         (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
770     {
771         UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit + 4096 - (char *)stack;
772         if (diff < 4096)
773         {
774             ERR( "stack overflow %u bytes in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
775                  diff, GetCurrentThreadId(), EIP_sig(sigcontext), ESP_sig(sigcontext),
776                  NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
777             server_abort_thread(1);
778         }
779         else WARN( "exception outside of stack limits in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
780                    GetCurrentThreadId(), EIP_sig(sigcontext), ESP_sig(sigcontext),
781                    NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
782     }
783
784     stack--;  /* push the stack_layout structure */
785     stack->ret_addr     = __wine_call_from_32_restore_regs;
786     stack->rec_ptr      = &stack->rec;
787     stack->context_ptr  = &stack->context;
788
789     stack->rec.ExceptionRecord  = NULL;
790     stack->rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
791     stack->rec.ExceptionAddress = (LPVOID)EIP_sig(sigcontext);
792     stack->rec.NumberParameters = 0;
793
794     stack->context.ContextFlags = CONTEXT_FULL;
795     stack->context.Eax          = EAX_sig(sigcontext);
796     stack->context.Ebx          = EBX_sig(sigcontext);
797     stack->context.Ecx          = ECX_sig(sigcontext);
798     stack->context.Edx          = EDX_sig(sigcontext);
799     stack->context.Esi          = ESI_sig(sigcontext);
800     stack->context.Edi          = EDI_sig(sigcontext);
801     stack->context.Ebp          = EBP_sig(sigcontext);
802     stack->context.EFlags       = EFL_sig(sigcontext);
803     stack->context.Eip          = EIP_sig(sigcontext);
804     stack->context.Esp          = ESP_sig(sigcontext);
805     stack->context.SegCs        = LOWORD(CS_sig(sigcontext));
806     stack->context.SegDs        = LOWORD(DS_sig(sigcontext));
807     stack->context.SegEs        = LOWORD(ES_sig(sigcontext));
808     stack->context.SegFs        = fs;
809     stack->context.SegGs        = gs;
810     stack->context.SegSs        = LOWORD(SS_sig(sigcontext));
811
812     /* now modify the sigcontext to return to the raise function */
813     ESP_sig(sigcontext) = (DWORD)stack;
814     EIP_sig(sigcontext) = (DWORD)func;
815     CS_sig(sigcontext)  = wine_get_cs();
816     DS_sig(sigcontext)  = wine_get_ds();
817     ES_sig(sigcontext)  = wine_get_es();
818     FS_sig(sigcontext)  = wine_get_fs();
819     GS_sig(sigcontext)  = wine_get_gs();
820     SS_sig(sigcontext)  = wine_get_ss();
821
822     return stack->rec_ptr;
823 }
824
825
826 /***********************************************************************
827  *           get_exception_context
828  *
829  * Get a pointer to the context built by setup_exception.
830  */
831 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
832 {
833     return (CONTEXT *)rec - 1;  /* cf. stack_layout structure */
834 }
835
836
837 /**********************************************************************
838  *              get_fpu_code
839  *
840  * Get the FPU exception code from the FPU status.
841  */
842 static inline DWORD get_fpu_code( const CONTEXT *context )
843 {
844     DWORD status = context->FloatSave.StatusWord;
845
846     if (status & 0x01)  /* IE */
847     {
848         if (status & 0x40)  /* SF */
849             return EXCEPTION_FLT_STACK_CHECK;
850         else
851             return EXCEPTION_FLT_INVALID_OPERATION;
852     }
853     if (status & 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND;  /* DE flag */
854     if (status & 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO;    /* ZE flag */
855     if (status & 0x08) return EXCEPTION_FLT_OVERFLOW;          /* OE flag */
856     if (status & 0x10) return EXCEPTION_FLT_UNDERFLOW;         /* UE flag */
857     if (status & 0x20) return EXCEPTION_FLT_INEXACT_RESULT;    /* PE flag */
858     return EXCEPTION_FLT_INVALID_OPERATION;  /* generic error */
859 }
860
861
862 /**********************************************************************
863  *              raise_segv_exception
864  */
865 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
866 {
867     switch(rec->ExceptionCode)
868     {
869     case EXCEPTION_ACCESS_VIOLATION:
870         if (rec->NumberParameters == 2)
871         {
872             if (!(rec->ExceptionCode = VIRTUAL_HandleFault( (void *)rec->ExceptionInformation[1] )))
873                 return;
874         }
875         break;
876     case EXCEPTION_DATATYPE_MISALIGNMENT:
877         /* FIXME: pass through exception handler first? */
878         if (context->EFlags & 0x00040000)
879         {
880             /* Disable AC flag, return */
881             context->EFlags &= ~0x00040000;
882             return;
883         }
884         break;
885     }
886     __regs_RtlRaiseException( rec, context );
887 }
888
889
890 /**********************************************************************
891  *              raise_trap_exception
892  */
893 static void WINAPI raise_trap_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
894 {
895     DWORD dr0, dr1, dr2, dr3, dr6, dr7;
896
897     if (rec->ExceptionCode == EXCEPTION_SINGLE_STEP)
898     {
899         if (context->EFlags & 0x100)
900         {
901             context->EFlags &= ~0x100;  /* clear single-step flag */
902         }
903         else  /* hardware breakpoint, fetch the debug registers */
904         {
905             context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
906             NtGetContextThread(GetCurrentThread(), context);
907             /* do we really have a bp from a debug register ?
908              * if not, then someone did a kill(SIGTRAP) on us, and we
909              * shall return a breakpoint, not a single step exception
910              */
911             if (!(context->Dr6 & 0xf)) rec->ExceptionCode = EXCEPTION_BREAKPOINT;
912         }
913     }
914
915     dr0 = context->Dr0;
916     dr1 = context->Dr1;
917     dr2 = context->Dr2;
918     dr3 = context->Dr3;
919     dr6 = context->Dr6;
920     dr7 = context->Dr7;
921
922     __regs_RtlRaiseException( rec, context );
923
924     if (dr0 != context->Dr0 || dr1 != context->Dr1 || dr2 != context->Dr2 ||
925         dr3 != context->Dr3 || dr6 != context->Dr6 || dr7 != context->Dr7)
926     {
927         /* the debug registers have changed, set the new values */
928         context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
929         NtSetContextThread(GetCurrentThread(), context);
930     }
931 }
932
933
934 /**********************************************************************
935  *              raise_fpu_exception
936  */
937 static void WINAPI raise_fpu_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
938 {
939     __regs_RtlRaiseException( rec, context );
940     restore_fpu( context );
941 }
942
943
944 #ifdef __HAVE_VM86
945 /**********************************************************************
946  *              raise_vm86_sti_exception
947  */
948 static void WINAPI raise_vm86_sti_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
949 {
950     /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
951     NtCurrentTeb()->vm86_pending |= VIP_MASK;
952
953     if (ntdll_get_thread_data()->vm86_ptr)
954     {
955         if (((char*)context->Eip >= (char*)vm86_return) &&
956             ((char*)context->Eip <= (char*)vm86_return_end) &&
957             (VM86_TYPE(context->Eax) != VM86_SIGNAL)) {
958             /* exiting from VM86, can't throw */
959             return;
960         }
961         merge_vm86_pending_flags( rec );
962     }
963     else if (NtCurrentTeb()->dpmi_vif &&
964              !wine_ldt_is_system(context->SegCs) &&
965              !wine_ldt_is_system(context->SegSs))
966     {
967         /* Executing DPMI code and virtual interrupts are enabled. */
968         NtCurrentTeb()->vm86_pending = 0;
969         __regs_RtlRaiseException( rec, context );
970     }
971 }
972
973
974 /**********************************************************************
975  *              usr2_handler
976  *
977  * Handler for SIGUSR2.
978  * We use it to signal that the running __wine_enter_vm86() should
979  * immediately set VIP_MASK, causing pending events to be handled
980  * as early as possible.
981  */
982 static HANDLER_DEF(usr2_handler)
983 {
984     EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_vm86_sti_exception );
985     rec->ExceptionCode = EXCEPTION_VM86_STI;
986 }
987 #endif /* __HAVE_VM86 */
988
989
990 /**********************************************************************
991  *              segv_handler
992  *
993  * Handler for SIGSEGV and related errors.
994  */
995 static HANDLER_DEF(segv_handler)
996 {
997     EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_segv_exception );
998
999     switch(get_trap_code(HANDLER_CONTEXT))
1000     {
1001     case T_OFLOW:   /* Overflow exception */
1002         rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
1003         break;
1004     case T_BOUND:   /* Bound range exception */
1005         rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
1006         break;
1007     case T_PRIVINFLT:   /* Invalid opcode exception */
1008         rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1009         break;
1010     case T_STKFLT:  /* Stack fault */
1011         rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
1012         break;
1013     case T_SEGNPFLT:  /* Segment not present exception */
1014     case T_PROTFLT:   /* General protection fault */
1015     case T_UNKNOWN:   /* Unknown fault code */
1016         rec->ExceptionCode = get_error_code(HANDLER_CONTEXT) ? EXCEPTION_ACCESS_VIOLATION
1017                                                              : EXCEPTION_PRIV_INSTRUCTION;
1018         break;
1019     case T_PAGEFLT:  /* Page fault */
1020         rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
1021 #ifdef FAULT_ADDRESS
1022         rec->NumberParameters = 2;
1023         rec->ExceptionInformation[0] = (get_error_code(HANDLER_CONTEXT) & 2) != 0;
1024         rec->ExceptionInformation[1] = (ULONG_PTR)FAULT_ADDRESS;
1025 #endif
1026         break;
1027     case T_ALIGNFLT:  /* Alignment check exception */
1028         rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
1029         break;
1030     default:
1031         ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
1032         /* fall through */
1033     case T_NMI:       /* NMI interrupt */
1034     case T_DNA:       /* Device not available exception */
1035     case T_DOUBLEFLT: /* Double fault exception */
1036     case T_TSSFLT:    /* Invalid TSS exception */
1037     case T_RESERVED:  /* Unknown exception */
1038     case T_MCHK:      /* Machine check exception */
1039 #ifdef T_CACHEFLT
1040     case T_CACHEFLT:  /* Cache flush exception */
1041 #endif
1042         rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
1043         break;
1044     }
1045 }
1046
1047
1048 /**********************************************************************
1049  *              trap_handler
1050  *
1051  * Handler for SIGTRAP.
1052  */
1053 static HANDLER_DEF(trap_handler)
1054 {
1055     EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_trap_exception );
1056
1057     switch(get_trap_code(HANDLER_CONTEXT))
1058     {
1059     case T_TRCTRAP:  /* Single-step exception */
1060         rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
1061         EFL_sig(HANDLER_CONTEXT) &= ~0x100;  /* clear single-step flag */
1062         break;
1063     case T_BPTFLT:   /* Breakpoint exception */
1064         rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1;  /* back up over the int3 instruction */
1065         /* fall through */
1066     default:
1067         rec->ExceptionCode = EXCEPTION_BREAKPOINT;
1068         break;
1069     }
1070 }
1071
1072
1073 /**********************************************************************
1074  *              fpe_handler
1075  *
1076  * Handler for SIGFPE.
1077  */
1078 static HANDLER_DEF(fpe_handler)
1079 {
1080     EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, raise_fpu_exception );
1081     CONTEXT *context;
1082
1083     context = get_exception_context( rec );
1084     save_fpu( context, HANDLER_CONTEXT );
1085
1086     switch(get_trap_code(HANDLER_CONTEXT))
1087     {
1088     case T_DIVIDE:   /* Division by zero exception */
1089         rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
1090         break;
1091     case T_FPOPFLT:   /* Coprocessor segment overrun */
1092         rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1093         break;
1094     case T_ARITHTRAP:  /* Floating point exception */
1095     case T_UNKNOWN:    /* Unknown fault code */
1096         rec->ExceptionCode = get_fpu_code( context );
1097         break;
1098     default:
1099         ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT) );
1100         rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
1101         break;
1102     }
1103 }
1104
1105
1106 /**********************************************************************
1107  *              int_handler
1108  *
1109  * Handler for SIGINT.
1110  */
1111 static HANDLER_DEF(int_handler)
1112 {
1113     init_handler( HANDLER_CONTEXT );
1114     if (!dispatch_signal(SIGINT))
1115     {
1116         EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, __regs_RtlRaiseException );
1117         rec->ExceptionCode = CONTROL_C_EXIT;
1118     }
1119 }
1120
1121 /**********************************************************************
1122  *              abrt_handler
1123  *
1124  * Handler for SIGABRT.
1125  */
1126 static HANDLER_DEF(abrt_handler)
1127 {
1128     EXCEPTION_RECORD *rec = setup_exception( HANDLER_CONTEXT, __regs_RtlRaiseException );
1129     rec->ExceptionCode  = EXCEPTION_WINE_ASSERTION;
1130     rec->ExceptionFlags = EH_NONCONTINUABLE;
1131 }
1132
1133
1134 /**********************************************************************
1135  *              term_handler
1136  *
1137  * Handler for SIGTERM.
1138  */
1139 static HANDLER_DEF(term_handler)
1140 {
1141     init_handler( HANDLER_CONTEXT );
1142     server_abort_thread(0);
1143 }
1144
1145
1146 /**********************************************************************
1147  *              usr1_handler
1148  *
1149  * Handler for SIGUSR1, used to signal a thread that it got suspended.
1150  */
1151 static HANDLER_DEF(usr1_handler)
1152 {
1153     LARGE_INTEGER timeout;
1154
1155     init_handler( HANDLER_CONTEXT );
1156     /* wait with 0 timeout, will only return once the thread is no longer suspended */
1157     timeout.QuadPart = 0;
1158     NTDLL_wait_for_multiple_objects( 0, NULL, 0, &timeout, 0 );
1159 }
1160
1161
1162 /***********************************************************************
1163  *           set_handler
1164  *
1165  * Set a signal handler
1166  */
1167 static int set_handler( int sig, int have_sigaltstack, void (*func)() )
1168 {
1169     struct sigaction sig_act;
1170
1171 #ifdef linux
1172     if (!have_sigaltstack)
1173     {
1174         struct kernel_sigaction sig_act;
1175         sig_act.ksa_handler = func;
1176         sig_act.ksa_flags   = SA_RESTART;
1177         sig_act.ksa_mask    = (1 << (SIGINT-1)) |
1178                               (1 << (SIGUSR2-1));
1179         /* point to the top of the signal stack */
1180         sig_act.ksa_restorer = (char *)get_signal_stack() + SIGNAL_STACK_SIZE;
1181         return wine_sigaction( sig, &sig_act, NULL );
1182     }
1183 #endif  /* linux */
1184     sig_act.sa_handler = func;
1185     sigemptyset( &sig_act.sa_mask );
1186     sigaddset( &sig_act.sa_mask, SIGINT );
1187     sigaddset( &sig_act.sa_mask, SIGUSR2 );
1188
1189 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
1190     sig_act.sa_flags = SA_RESTART;
1191 #elif defined (__svr4__) || defined(_SCO_DS)
1192     sig_act.sa_flags = SA_SIGINFO | SA_RESTART;
1193 #else
1194     sig_act.sa_flags = 0;
1195 #endif
1196
1197 #ifdef SA_ONSTACK
1198     if (have_sigaltstack) sig_act.sa_flags |= SA_ONSTACK;
1199 #endif
1200     return sigaction( sig, &sig_act, NULL );
1201 }
1202
1203
1204 /***********************************************************************
1205  *           __wine_set_signal_handler   (NTDLL.@)
1206  */
1207 int __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
1208 {
1209     if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
1210     if (handlers[sig] != NULL) return -2;
1211     handlers[sig] = wsh;
1212     return 0;
1213 }
1214
1215
1216 /**********************************************************************
1217  *              SIGNAL_Init
1218  */
1219 BOOL SIGNAL_Init(void)
1220 {
1221     int have_sigaltstack = 0;
1222
1223 #ifdef HAVE_SIGALTSTACK
1224     struct sigaltstack ss;
1225     ss.ss_sp    = get_signal_stack();
1226     ss.ss_size  = SIGNAL_STACK_SIZE;
1227     ss.ss_flags = 0;
1228     if (!sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1229 #ifdef linux
1230     /* sigaltstack may fail because the kernel is too old, or
1231        because glibc is brain-dead. In the latter case a
1232        direct system call should succeed. */
1233     else if (!wine_sigaltstack(&ss, NULL)) have_sigaltstack = 1;
1234 #endif  /* linux */
1235 #endif  /* HAVE_SIGALTSTACK */
1236
1237     if (set_handler( SIGINT,  have_sigaltstack, (void (*)())int_handler ) == -1) goto error;
1238     if (set_handler( SIGFPE,  have_sigaltstack, (void (*)())fpe_handler ) == -1) goto error;
1239     if (set_handler( SIGSEGV, have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1240     if (set_handler( SIGILL,  have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1241     if (set_handler( SIGABRT, have_sigaltstack, (void (*)())abrt_handler ) == -1) goto error;
1242     if (set_handler( SIGTERM, have_sigaltstack, (void (*)())term_handler ) == -1) goto error;
1243     if (set_handler( SIGUSR1, have_sigaltstack, (void (*)())usr1_handler ) == -1) goto error;
1244 #ifdef SIGBUS
1245     if (set_handler( SIGBUS,  have_sigaltstack, (void (*)())segv_handler ) == -1) goto error;
1246 #endif
1247 #ifdef SIGTRAP
1248     if (set_handler( SIGTRAP, have_sigaltstack, (void (*)())trap_handler ) == -1) goto error;
1249 #endif
1250
1251 #ifdef __HAVE_VM86
1252     if (set_handler( SIGUSR2, have_sigaltstack, (void (*)())usr2_handler ) == -1) goto error;
1253 #endif
1254
1255     return TRUE;
1256
1257  error:
1258     perror("sigaction");
1259     return FALSE;
1260 }
1261
1262
1263 #ifdef __HAVE_VM86
1264 /**********************************************************************
1265  *              __wine_enter_vm86   (NTDLL.@)
1266  *
1267  * Enter vm86 mode with the specified register context.
1268  */
1269 void __wine_enter_vm86( CONTEXT *context )
1270 {
1271     EXCEPTION_RECORD rec;
1272     int res;
1273     struct vm86plus_struct vm86;
1274
1275     memset( &vm86, 0, sizeof(vm86) );
1276     for (;;)
1277     {
1278         restore_vm86_context( context, &vm86 );
1279
1280         ntdll_get_thread_data()->vm86_ptr = &vm86;
1281         merge_vm86_pending_flags( &rec );
1282
1283         res = vm86_enter( &ntdll_get_thread_data()->vm86_ptr ); /* uses and clears teb->vm86_ptr */
1284         if (res < 0)
1285         {
1286             errno = -res;
1287             return;
1288         }
1289
1290         save_vm86_context( context, &vm86 );
1291
1292         rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
1293         rec.ExceptionRecord  = NULL;
1294         rec.ExceptionAddress = (LPVOID)context->Eip;
1295         rec.NumberParameters = 0;
1296
1297         switch(VM86_TYPE(res))
1298         {
1299         case VM86_UNKNOWN: /* unhandled GP fault - IO-instruction or similar */
1300             rec.ExceptionCode = EXCEPTION_PRIV_INSTRUCTION;
1301             raise_segv_exception( &rec, context );
1302             continue;
1303         case VM86_TRAP: /* return due to DOS-debugger request */
1304             switch(VM86_ARG(res))
1305             {
1306             case T_TRCTRAP:  /* Single-step exception, single step flag is cleared by raise_trap_exception */
1307                 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
1308                 break;
1309             case T_BPTFLT:   /* Breakpoint exception */
1310                 rec.ExceptionAddress = (char *)rec.ExceptionAddress - 1;  /* back up over the int3 instruction */
1311                 /* fall through */
1312             default:
1313                 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
1314                 break;
1315             }
1316             raise_trap_exception( &rec, context );
1317             continue;
1318         case VM86_INTx: /* int3/int x instruction (ARG = x) */
1319             rec.ExceptionCode = EXCEPTION_VM86_INTx;
1320             rec.NumberParameters = 1;
1321             rec.ExceptionInformation[0] = VM86_ARG(res);
1322             break;
1323         case VM86_STI: /* sti/popf/iret instruction enabled virtual interrupts */
1324             context->EFlags |= VIF_MASK;
1325             context->EFlags &= ~VIP_MASK;
1326             NtCurrentTeb()->vm86_pending = 0;
1327             rec.ExceptionCode = EXCEPTION_VM86_STI;
1328             break;
1329         case VM86_PICRETURN: /* return due to pending PIC request */
1330             rec.ExceptionCode = EXCEPTION_VM86_PICRETURN;
1331             break;
1332         case VM86_SIGNAL: /* cannot happen because vm86_enter handles this case */
1333         default:
1334             ERR( "unhandled result from vm86 mode %x\n", res );
1335             continue;
1336         }
1337         __regs_RtlRaiseException( &rec, context );
1338     }
1339 }
1340
1341 #else /* __HAVE_VM86 */
1342 /**********************************************************************
1343  *              __wine_enter_vm86   (NTDLL.@)
1344  */
1345 void __wine_enter_vm86( CONTEXT *context )
1346 {
1347     MESSAGE("vm86 mode not supported on this platform\n");
1348 }
1349 #endif /* __HAVE_VM86 */
1350
1351 /**********************************************************************
1352  *              DbgBreakPoint   (NTDLL.@)
1353  */
1354 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret");
1355
1356 /**********************************************************************
1357  *              DbgUserBreakPoint   (NTDLL.@)
1358  */
1359 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret");
1360
1361 #endif  /* __i386__ */