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