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