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