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