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