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