2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
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.
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.
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
24 #include "wine/port.h"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
46 #ifdef HAVE_SYS_VM86_H
47 # include <sys/vm86.h>
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
56 #include "wine/library.h"
57 #include "ntdll_misc.h"
59 /***********************************************************************
60 * signal context platform-specific definitions
66 unsigned short sc_gs
, __gsh
;
67 unsigned short sc_fs
, __fsh
;
68 unsigned short sc_es
, __esh
;
69 unsigned short sc_ds
, __dsh
;
78 unsigned long sc_trapno
;
81 unsigned short sc_cs
, __csh
;
82 unsigned long sc_eflags
;
83 unsigned long esp_at_signal
;
84 unsigned short sc_ss
, __ssh
;
86 unsigned long oldmask
;
88 } volatile SIGCONTEXT
;
90 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
91 #define HANDLER_CONTEXT (&__context)
93 /* this is the sigaction structure from the Linux 2.1.20 kernel. */
94 struct kernel_sigaction
96 void (*ksa_handler
)();
97 unsigned long ksa_mask
;
98 unsigned long ksa_flags
;
102 /* Similar to the sigaction function in libc, except it leaves alone the
103 restorer field, which is used to specify the signal stack address */
104 static inline int wine_sigaction( int sig
, struct kernel_sigaction
*new,
105 struct kernel_sigaction
*old
)
107 __asm__
__volatile__( "pushl %%ebx\n\t"
112 : "0" (SYS_sigaction
), "S" (sig
), "c" (new), "d" (old
) );
113 if (sig
>=0) return 0;
118 #ifdef HAVE_SIGALTSTACK
119 /* direct syscall for sigaltstack to work around glibc 2.0 brain-damage */
120 static inline int wine_sigaltstack( const struct sigaltstack
*new,
121 struct sigaltstack
*old
)
124 __asm__
__volatile__( "pushl %%ebx\n\t"
129 : "0" (SYS_sigaltstack
), "q" (new), "c" (old
) );
130 if (ret
>= 0) return 0;
136 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
138 int vm86_enter( void **vm86_ptr
);
139 void vm86_return(void);
140 void vm86_return_end(void);
141 __ASM_GLOBAL_FUNC(vm86_enter
,
143 "movl %esp, %ebp\n\t"
144 "movl $166,%eax\n\t" /*SYS_vm86*/
145 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
146 "movl (%ecx),%ecx\n\t"
148 "movl $1,%ebx\n\t" /*VM86_ENTER*/
149 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
153 ".globl " __ASM_NAME("vm86_return") "\n\t"
154 __ASM_FUNC("vm86_return") "\n"
155 __ASM_NAME("vm86_return") ":\n\t"
161 "testl %eax,%eax\n\t"
163 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
164 "je " __ASM_NAME("vm86_enter") "\n\t"
166 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
168 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
169 __ASM_FUNC("vm86_return_end") "\n"
170 __ASM_NAME("vm86_return_end") ":\n\t"
173 #ifdef HAVE_SYS_VM86_H
181 #define EAX_sig(context) ((context)->tf_eax)
182 #define EBX_sig(context) ((context)->tf_ebx)
183 #define ECX_sig(context) ((context)->tf_ecx)
184 #define EDX_sig(context) ((context)->tf_edx)
185 #define ESI_sig(context) ((context)->tf_esi)
186 #define EDI_sig(context) ((context)->tf_edi)
187 #define EBP_sig(context) ((context)->tf_ebp)
189 #define CS_sig(context) ((context)->tf_cs)
190 #define DS_sig(context) ((context)->tf_ds)
191 #define ES_sig(context) ((context)->tf_es)
192 #define SS_sig(context) ((context)->tf_ss)
194 #include <machine/frame.h>
195 typedef struct trapframe SIGCONTEXT
;
197 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
198 #define HANDLER_CONTEXT __context
200 #define EFL_sig(context) ((context)->tf_eflags)
202 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
203 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
207 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
209 typedef struct sigcontext SIGCONTEXT
;
211 #define HANDLER_DEF(name) void name( int __signal, int code, SIGCONTEXT *__context )
212 #define HANDLER_CONTEXT __context
216 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
219 #include <sys/regset.h>
223 #include <sys/ucontext.h>
225 typedef struct ucontext SIGCONTEXT
;
227 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
228 #define HANDLER_CONTEXT __context
230 #endif /* svr4 || SCO_DS */
236 unsigned long ContextFlags
;
237 FLOATING_SAVE_AREA sc_float
;
242 unsigned long sc_edi
;
243 unsigned long sc_esi
;
244 unsigned long sc_eax
;
245 unsigned long sc_ebx
;
246 unsigned long sc_ecx
;
247 unsigned long sc_edx
;
248 unsigned long sc_ebp
;
249 unsigned long sc_eip
;
251 unsigned long sc_eflags
;
252 unsigned long sc_esp
;
260 /* FIXME: This section is just here so it can compile, it's most likely
261 * completely wrong. */
265 unsigned short sc_gs
, __gsh
;
266 unsigned short sc_fs
, __fsh
;
267 unsigned short sc_es
, __esh
;
268 unsigned short sc_ds
, __dsh
;
269 unsigned long sc_edi
;
270 unsigned long sc_esi
;
271 unsigned long sc_ebp
;
272 unsigned long sc_esp
;
273 unsigned long sc_ebx
;
274 unsigned long sc_edx
;
275 unsigned long sc_ecx
;
276 unsigned long sc_eax
;
277 unsigned long sc_trapno
;
278 unsigned long sc_err
;
279 unsigned long sc_eip
;
280 unsigned short sc_cs
, __csh
;
281 unsigned long sc_eflags
;
282 unsigned long esp_at_signal
;
283 unsigned short sc_ss
, __ssh
;
285 unsigned long oldmask
;
289 #define HANDLER_DEF(name) void name( int __signal, SIGCONTEXT __context )
290 #define HANDLER_CONTEXT (&__context)
292 #endif /* __CYGWIN__ */
295 # include <sys/ucontext.h>
296 # include <sys/types.h>
299 typedef ucontext_t SIGCONTEXT
;
301 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
302 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
303 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
304 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
305 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
306 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
307 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
309 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
310 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
311 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
312 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
313 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
314 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
316 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
318 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
319 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
321 #define HANDLER_DEF(name) void name( int __signal, siginfo_t *__siginfo, SIGCONTEXT *__context )
322 #define HANDLER_CONTEXT (__context)
324 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
325 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
327 #define FAULT_ADDRESS (__siginfo->si_addr)
329 #endif /* __APPLE__ */
331 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) ||\
332 defined(__OpenBSD__) || defined(__EMX__) || defined(__CYGWIN__)
334 #define EAX_sig(context) ((context)->sc_eax)
335 #define EBX_sig(context) ((context)->sc_ebx)
336 #define ECX_sig(context) ((context)->sc_ecx)
337 #define EDX_sig(context) ((context)->sc_edx)
338 #define ESI_sig(context) ((context)->sc_esi)
339 #define EDI_sig(context) ((context)->sc_edi)
340 #define EBP_sig(context) ((context)->sc_ebp)
342 #define CS_sig(context) ((context)->sc_cs)
343 #define DS_sig(context) ((context)->sc_ds)
344 #define ES_sig(context) ((context)->sc_es)
345 #define FS_sig(context) ((context)->sc_fs)
346 #define GS_sig(context) ((context)->sc_gs)
347 #define SS_sig(context) ((context)->sc_ss)
349 #define TRAP_sig(context) ((context)->sc_trapno)
352 #define ERROR_sig(context) ((context)->sc_err)
356 #define ERROR_sig(context) ((context)->sc_err)
357 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->i387))
358 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->cr2)
362 #define EFL_sig(context) ((context)->sc_efl)
363 /* FreeBSD, see i386/i386/traps.c::trap_pfault va->err kludge */
364 #define FAULT_ADDRESS ((void *)HANDLER_CONTEXT->sc_err)
366 #define EFL_sig(context) ((context)->sc_eflags)
369 #define EIP_sig(context) (*((unsigned long*)&(context)->sc_eip))
370 #define ESP_sig(context) (*((unsigned long*)&(context)->sc_esp))
372 #endif /* linux || __NetBSD__ || __FreeBSD__ || __OpenBSD__ */
374 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
380 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
381 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
382 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
383 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
384 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
385 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
386 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
388 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
389 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
390 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
391 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
393 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
394 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
396 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
398 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
400 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
402 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
404 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
407 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
410 #define FAULT_ADDRESS (__siginfo->si_addr)
412 #endif /* svr4 || SCO_DS */
414 /* exception code definitions (already defined by FreeBSD/NetBSD) */
415 #if !defined(__FreeBSD__) && !defined(__NetBSD__) /* FIXME: other BSDs? */
416 #define T_DIVIDE 0 /* Division by zero exception */
417 #define T_TRCTRAP 1 /* Single-step exception */
418 #define T_NMI 2 /* NMI interrupt */
419 #define T_BPTFLT 3 /* Breakpoint exception */
420 #define T_OFLOW 4 /* Overflow exception */
421 #define T_BOUND 5 /* Bound range exception */
422 #define T_PRIVINFLT 6 /* Invalid opcode exception */
423 #define T_DNA 7 /* Device not available exception */
424 #define T_DOUBLEFLT 8 /* Double fault exception */
425 #define T_FPOPFLT 9 /* Coprocessor segment overrun */
426 #define T_TSSFLT 10 /* Invalid TSS exception */
427 #define T_SEGNPFLT 11 /* Segment not present exception */
428 #define T_STKFLT 12 /* Stack fault */
429 #define T_PROTFLT 13 /* General protection fault */
430 #define T_PAGEFLT 14 /* Page fault */
431 #define T_RESERVED 15 /* Unknown exception */
432 #define T_ARITHTRAP 16 /* Floating point exception */
433 #define T_ALIGNFLT 17 /* Alignment check exception */
434 #define T_MCHK 18 /* Machine check exception */
435 #define T_CACHEFLT 19 /* Cache flush exception */
437 #if defined(__NetBSD__)
438 #define T_MCHK 19 /* Machine check exception */
441 #define T_UNKNOWN (-1) /* Unknown fault (TRAP_sig not defined) */
443 #include "wine/exception.h"
444 #include "wine/debug.h"
446 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
448 typedef int (*wine_signal_handler
)(unsigned int sig
);
450 static size_t signal_stack_mask
;
451 static size_t signal_stack_size
;
453 static wine_signal_handler handlers
[256];
455 extern void DECLSPEC_NORETURN
__wine_call_from_32_restore_regs( const CONTEXT
*context
);
458 /***********************************************************************
461 inline static int dispatch_signal(unsigned int sig
)
463 if (handlers
[sig
] == NULL
) return 0;
464 return handlers
[sig
](sig
);
468 /***********************************************************************
471 * Get the trap code for a signal.
473 static inline int get_trap_code( const SIGCONTEXT
*sigcontext
)
476 return TRAP_sig(sigcontext
);
478 return T_UNKNOWN
; /* unknown trap code */
482 /***********************************************************************
485 * Get the error code for a signal.
487 static inline WORD
get_error_code( const SIGCONTEXT
*sigcontext
)
490 return ERROR_sig(sigcontext
);
496 /***********************************************************************
499 * Get the base of the signal stack for the current thread.
501 static inline void *get_signal_stack(void)
503 return (char *)NtCurrentTeb() + 4096;
507 /***********************************************************************
510 * Get the current teb based on the stack pointer.
512 static inline TEB
*get_current_teb(void)
515 __asm__("movl %%esp,%0" : "=g" (esp
) );
516 return (TEB
*)(esp
& ~signal_stack_mask
);
521 /***********************************************************************
524 * Set the register values from a vm86 structure.
526 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
528 context
->Eax
= vm86
->regs
.eax
;
529 context
->Ebx
= vm86
->regs
.ebx
;
530 context
->Ecx
= vm86
->regs
.ecx
;
531 context
->Edx
= vm86
->regs
.edx
;
532 context
->Esi
= vm86
->regs
.esi
;
533 context
->Edi
= vm86
->regs
.edi
;
534 context
->Esp
= vm86
->regs
.esp
;
535 context
->Ebp
= vm86
->regs
.ebp
;
536 context
->Eip
= vm86
->regs
.eip
;
537 context
->SegCs
= vm86
->regs
.cs
;
538 context
->SegDs
= vm86
->regs
.ds
;
539 context
->SegEs
= vm86
->regs
.es
;
540 context
->SegFs
= vm86
->regs
.fs
;
541 context
->SegGs
= vm86
->regs
.gs
;
542 context
->SegSs
= vm86
->regs
.ss
;
543 context
->EFlags
= vm86
->regs
.eflags
;
547 /***********************************************************************
548 * restore_vm86_context
550 * Build a vm86 structure from the register values.
552 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
554 vm86
->regs
.eax
= context
->Eax
;
555 vm86
->regs
.ebx
= context
->Ebx
;
556 vm86
->regs
.ecx
= context
->Ecx
;
557 vm86
->regs
.edx
= context
->Edx
;
558 vm86
->regs
.esi
= context
->Esi
;
559 vm86
->regs
.edi
= context
->Edi
;
560 vm86
->regs
.esp
= context
->Esp
;
561 vm86
->regs
.ebp
= context
->Ebp
;
562 vm86
->regs
.eip
= context
->Eip
;
563 vm86
->regs
.cs
= context
->SegCs
;
564 vm86
->regs
.ds
= context
->SegDs
;
565 vm86
->regs
.es
= context
->SegEs
;
566 vm86
->regs
.fs
= context
->SegFs
;
567 vm86
->regs
.gs
= context
->SegGs
;
568 vm86
->regs
.ss
= context
->SegSs
;
569 vm86
->regs
.eflags
= context
->EFlags
;
573 /**********************************************************************
574 * merge_vm86_pending_flags
576 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
577 * raises exception if there are pending events and VIF flag
578 * has been turned on.
580 * Called from __wine_enter_vm86 because vm86_enter
581 * doesn't check for pending events.
583 * Called from raise_vm86_sti_exception to check for
584 * pending events in a signal safe way.
586 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
588 BOOL check_pending
= TRUE
;
589 struct vm86plus_struct
*vm86
=
590 (struct vm86plus_struct
*)(ntdll_get_thread_data()->vm86_ptr
);
593 * In order to prevent a race when SIGUSR2 occurs while
594 * we are returning from exception handler, pending events
595 * will be rechecked after each raised exception.
597 while (check_pending
&& NtCurrentTeb()->vm86_pending
)
599 check_pending
= FALSE
;
600 ntdll_get_thread_data()->vm86_ptr
= NULL
;
603 * If VIF is set, throw exception.
604 * Note that SIGUSR2 may turn VIF flag off so
605 * VIF check must occur only when TEB.vm86_ptr is NULL.
607 if (vm86
->regs
.eflags
& VIF_MASK
)
610 save_vm86_context( &vcontext
, vm86
);
612 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
613 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
614 rec
->ExceptionRecord
= NULL
;
615 rec
->NumberParameters
= 0;
616 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
618 vcontext
.EFlags
&= ~VIP_MASK
;
619 NtCurrentTeb()->vm86_pending
= 0;
620 __regs_RtlRaiseException( rec
, &vcontext
);
622 restore_vm86_context( &vcontext
, vm86
);
623 check_pending
= TRUE
;
626 ntdll_get_thread_data()->vm86_ptr
= vm86
;
630 * Merge VIP flags in a signal safe way. This requires
631 * that the following operation compiles into atomic
634 vm86
->regs
.eflags
|= NtCurrentTeb()->vm86_pending
;
636 #endif /* __HAVE_VM86 */
639 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
642 /***********************************************************************
645 * Handler initialization when the full context is not needed.
647 inline static void *init_handler( const SIGCONTEXT
*sigcontext
, WORD
*fs
, WORD
*gs
)
649 void *stack
= (void *)ESP_sig(sigcontext
);
650 TEB
*teb
= get_current_teb();
651 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
653 /* get %fs and %gs at time of the fault */
655 *fs
= LOWORD(FS_sig(sigcontext
));
660 *gs
= LOWORD(GS_sig(sigcontext
));
665 wine_set_fs( thread_data
->teb_sel
);
667 /* now restore a proper %gs for the fault handler */
668 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
669 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
672 * Win16 or DOS protected mode. Note that during switch
673 * from 16-bit mode to linear mode, CS may be set to system
674 * segment before FS is restored. Fortunately, in this case
675 * SS is still non-system segment. This is why both CS and SS
678 wine_set_gs( teb
->gs_sel
);
679 stack
= teb
->WOW32Reserved
;
682 else if ((void *)EIP_sig(sigcontext
) == vm86_return
) /* vm86 mode */
684 unsigned int *int_stack
= stack
;
685 /* fetch the saved %gs from the stack */
686 wine_set_gs( int_stack
[0] );
689 else /* 32-bit mode */
692 wine_set_gs( GS_sig(sigcontext
) );
699 /***********************************************************************
702 * Set the FPU context from a sigcontext.
704 inline static void save_fpu( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
)
706 context
->ContextFlags
|= CONTEXT86_FLOATING_POINT
;
708 if (FPU_sig(sigcontext
))
710 context
->FloatSave
= *FPU_sig(sigcontext
);
715 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
716 #endif /* __GNUC__ */
720 /***********************************************************************
723 * Restore the FPU context to a sigcontext.
725 inline static void restore_fpu( const CONTEXT
*context
)
727 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
728 /* reset the current interrupt status */
729 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
731 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
732 #endif /* __GNUC__ */
736 /***********************************************************************
739 * Build a context structure from the signal info.
741 inline static void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
, WORD fs
, WORD gs
)
743 context
->ContextFlags
= CONTEXT_FULL
;
744 context
->Eax
= EAX_sig(sigcontext
);
745 context
->Ebx
= EBX_sig(sigcontext
);
746 context
->Ecx
= ECX_sig(sigcontext
);
747 context
->Edx
= EDX_sig(sigcontext
);
748 context
->Esi
= ESI_sig(sigcontext
);
749 context
->Edi
= EDI_sig(sigcontext
);
750 context
->Ebp
= EBP_sig(sigcontext
);
751 context
->EFlags
= EFL_sig(sigcontext
);
752 context
->Eip
= EIP_sig(sigcontext
);
753 context
->Esp
= ESP_sig(sigcontext
);
754 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
755 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
756 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
759 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
763 /***********************************************************************
766 * Restore the signal info from the context.
768 inline static void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
770 EAX_sig(sigcontext
) = context
->Eax
;
771 EBX_sig(sigcontext
) = context
->Ebx
;
772 ECX_sig(sigcontext
) = context
->Ecx
;
773 EDX_sig(sigcontext
) = context
->Edx
;
774 ESI_sig(sigcontext
) = context
->Esi
;
775 EDI_sig(sigcontext
) = context
->Edi
;
776 EBP_sig(sigcontext
) = context
->Ebp
;
777 EFL_sig(sigcontext
) = context
->EFlags
;
778 EIP_sig(sigcontext
) = context
->Eip
;
779 ESP_sig(sigcontext
) = context
->Esp
;
780 CS_sig(sigcontext
) = context
->SegCs
;
781 DS_sig(sigcontext
) = context
->SegDs
;
782 ES_sig(sigcontext
) = context
->SegEs
;
783 SS_sig(sigcontext
) = context
->SegSs
;
785 GS_sig(sigcontext
) = context
->SegGs
;
787 wine_set_gs( context
->SegGs
);
790 FS_sig(sigcontext
) = context
->SegFs
;
792 wine_set_fs( context
->SegFs
);
797 /***********************************************************************
800 * Set the new CPU context.
802 void set_cpu_context( const CONTEXT
*context
)
804 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
806 if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
808 if (flags
& CONTEXT_FULL
)
810 if ((flags
& CONTEXT_FULL
) != (CONTEXT_FULL
& ~CONTEXT_i386
))
811 FIXME( "setting partial context (%lx) not supported\n", flags
);
813 __wine_call_from_32_restore_regs( context
);
818 /***********************************************************************
819 * is_privileged_instr
821 * Check if the fault location is a privileged instruction.
822 * Based on the instruction emulation code in dlls/kernel/instr.c.
824 static inline int is_privileged_instr( CONTEXT86
*context
)
827 unsigned int prefix_count
= 0;
829 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
830 instr
= (BYTE
*)context
->Eip
;
832 for (;;) switch(*instr
)
834 /* instruction prefixes */
835 case 0x2e: /* %cs: */
836 case 0x36: /* %ss: */
837 case 0x3e: /* %ds: */
838 case 0x26: /* %es: */
839 case 0x64: /* %fs: */
840 case 0x65: /* %gs: */
841 case 0x66: /* opcode size */
842 case 0x67: /* addr size */
843 case 0xf0: /* lock */
844 case 0xf2: /* repne */
845 case 0xf3: /* repe */
846 if (++prefix_count
>= 15) return 0;
850 case 0x0f: /* extended instruction */
853 case 0x20: /* mov crX, reg */
854 case 0x21: /* mov drX, reg */
855 case 0x22: /* mov reg, crX */
856 case 0x23: /* mov reg drX */
860 case 0x6c: /* insb (%dx) */
861 case 0x6d: /* insl (%dx) */
862 case 0x6e: /* outsb (%dx) */
863 case 0x6f: /* outsl (%dx) */
864 case 0xcd: /* int $xx */
865 case 0xe4: /* inb al,XX */
866 case 0xe5: /* in (e)ax,XX */
867 case 0xe6: /* outb XX,al */
868 case 0xe7: /* out XX,(e)ax */
869 case 0xec: /* inb (%dx),%al */
870 case 0xed: /* inl (%dx),%eax */
871 case 0xee: /* outb %al,(%dx) */
872 case 0xef: /* outl %eax,(%dx) */
883 /***********************************************************************
886 * Setup a proper stack frame for the raise function, and modify the
887 * sigcontext so that the return from the signal handler will call
888 * the raise function.
890 static EXCEPTION_RECORD
*setup_exception( SIGCONTEXT
*sigcontext
, raise_func func
)
894 void *ret_addr
; /* return address from raise_func */
895 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
896 CONTEXT
*context_ptr
; /* second arg for raise_func */
898 EXCEPTION_RECORD rec
;
905 stack
= init_handler( sigcontext
, &fs
, &gs
);
907 /* stack sanity checks */
909 if ((char *)stack
>= (char *)get_signal_stack() &&
910 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
912 ERR( "nested exception on signal stack in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
913 GetCurrentThreadId(), EIP_sig(sigcontext
), ESP_sig(sigcontext
),
914 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
915 server_abort_thread(1);
918 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
919 (char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
||
920 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
922 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)stack
;
925 ERR( "stack overflow %u bytes in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
926 diff
, GetCurrentThreadId(), EIP_sig(sigcontext
), ESP_sig(sigcontext
),
927 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
928 server_abort_thread(1);
930 else WARN( "exception outside of stack limits in thread %04lx eip %08lx esp %08lx stack %p-%p\n",
931 GetCurrentThreadId(), EIP_sig(sigcontext
), ESP_sig(sigcontext
),
932 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
935 stack
--; /* push the stack_layout structure */
936 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
937 stack
->rec_ptr
= &stack
->rec
;
938 stack
->context_ptr
= &stack
->context
;
940 stack
->rec
.ExceptionRecord
= NULL
;
941 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
942 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
943 stack
->rec
.NumberParameters
= 0;
945 save_context( &stack
->context
, sigcontext
, fs
, gs
);
947 /* now modify the sigcontext to return to the raise function */
948 ESP_sig(sigcontext
) = (DWORD
)stack
;
949 EIP_sig(sigcontext
) = (DWORD
)func
;
950 EFL_sig(sigcontext
) &= ~0x100; /* clear single-step flag */
951 CS_sig(sigcontext
) = wine_get_cs();
952 DS_sig(sigcontext
) = wine_get_ds();
953 ES_sig(sigcontext
) = wine_get_es();
954 FS_sig(sigcontext
) = wine_get_fs();
955 GS_sig(sigcontext
) = wine_get_gs();
956 SS_sig(sigcontext
) = wine_get_ss();
958 return stack
->rec_ptr
;
962 /***********************************************************************
963 * get_exception_context
965 * Get a pointer to the context built by setup_exception.
967 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
969 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
973 /**********************************************************************
976 * Get the FPU exception code from the FPU status.
978 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
980 DWORD status
= context
->FloatSave
.StatusWord
;
982 if (status
& 0x01) /* IE */
984 if (status
& 0x40) /* SF */
985 return EXCEPTION_FLT_STACK_CHECK
;
987 return EXCEPTION_FLT_INVALID_OPERATION
;
989 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
990 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
991 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
992 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
993 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
994 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
998 /**********************************************************************
999 * raise_segv_exception
1001 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1003 switch(rec
->ExceptionCode
)
1005 case EXCEPTION_ACCESS_VIOLATION
:
1006 if (rec
->NumberParameters
== 2)
1007 rec
->ExceptionCode
= VIRTUAL_HandleFault( (void *)rec
->ExceptionInformation
[1] );
1009 case EXCEPTION_DATATYPE_MISALIGNMENT
:
1010 /* FIXME: pass through exception handler first? */
1011 if (context
->EFlags
& 0x00040000)
1013 /* Disable AC flag, return */
1014 context
->EFlags
&= ~0x00040000;
1019 __regs_RtlRaiseException( rec
, context
);
1021 NtSetContextThread( GetCurrentThread(), context
);
1025 /**********************************************************************
1026 * raise_trap_exception
1028 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1030 DWORD dr0
, dr1
, dr2
, dr3
, dr6
, dr7
;
1032 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
1034 if (context
->EFlags
& 0x100)
1036 context
->EFlags
&= ~0x100; /* clear single-step flag */
1038 else /* hardware breakpoint, fetch the debug registers */
1040 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1041 NtGetContextThread(GetCurrentThread(), context
);
1042 /* do we really have a bp from a debug register ?
1043 * if not, then someone did a kill(SIGTRAP) on us, and we
1044 * shall return a breakpoint, not a single step exception
1046 if (!(context
->Dr6
& 0xf)) rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1047 context
->ContextFlags
|= CONTEXT_FULL
; /* restore flags */
1058 __regs_RtlRaiseException( rec
, context
);
1060 context
->ContextFlags
= CONTEXT_FULL
;
1061 if (dr0
!= context
->Dr0
|| dr1
!= context
->Dr1
|| dr2
!= context
->Dr2
||
1062 dr3
!= context
->Dr3
|| dr6
!= context
->Dr6
|| dr7
!= context
->Dr7
)
1064 /* the debug registers have changed, set the new values */
1065 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1067 NtSetContextThread( GetCurrentThread(), context
);
1071 /**********************************************************************
1074 * Generic raise function for exceptions that don't need special treatment.
1076 static void WINAPI
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1078 __regs_RtlRaiseException( rec
, context
);
1079 NtSetContextThread( GetCurrentThread(), context
);
1084 /**********************************************************************
1085 * raise_vm86_sti_exception
1087 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1089 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1090 NtCurrentTeb()->vm86_pending
|= VIP_MASK
;
1092 if (ntdll_get_thread_data()->vm86_ptr
)
1094 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
1095 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
1096 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
1097 /* exiting from VM86, can't throw */
1100 merge_vm86_pending_flags( rec
);
1102 else if (NtCurrentTeb()->dpmi_vif
&&
1103 !wine_ldt_is_system(context
->SegCs
) &&
1104 !wine_ldt_is_system(context
->SegSs
))
1106 /* Executing DPMI code and virtual interrupts are enabled. */
1107 NtCurrentTeb()->vm86_pending
= 0;
1108 __regs_RtlRaiseException( rec
, context
);
1111 NtSetContextThread( GetCurrentThread(), context
);
1115 /**********************************************************************
1118 * Handler for SIGUSR2.
1119 * We use it to signal that the running __wine_enter_vm86() should
1120 * immediately set VIP_MASK, causing pending events to be handled
1121 * as early as possible.
1123 static HANDLER_DEF(usr2_handler
)
1125 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_vm86_sti_exception
);
1126 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
1128 #endif /* __HAVE_VM86 */
1131 /**********************************************************************
1134 * Handler for SIGSEGV and related errors.
1136 static HANDLER_DEF(segv_handler
)
1138 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_segv_exception
);
1140 switch(get_trap_code(HANDLER_CONTEXT
))
1142 case T_OFLOW
: /* Overflow exception */
1143 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1145 case T_BOUND
: /* Bound range exception */
1146 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1148 case T_PRIVINFLT
: /* Invalid opcode exception */
1149 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1151 case T_STKFLT
: /* Stack fault */
1152 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1154 case T_SEGNPFLT
: /* Segment not present exception */
1155 case T_PROTFLT
: /* General protection fault */
1156 case T_UNKNOWN
: /* Unknown fault code */
1157 if (!get_error_code(HANDLER_CONTEXT
) && is_privileged_instr( get_exception_context(rec
) ))
1158 rec
->ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
1161 WORD err
= get_error_code(HANDLER_CONTEXT
);
1162 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1163 rec
->NumberParameters
= 2;
1164 rec
->ExceptionInformation
[0] = 0;
1165 /* if error contains a LDT selector, use that as fault address */
1166 rec
->ExceptionInformation
[1] = (err
& 7) == 4 ? (err
& ~7) : 0xffffffff;
1169 case T_PAGEFLT
: /* Page fault */
1170 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1171 #ifdef FAULT_ADDRESS
1172 rec
->NumberParameters
= 2;
1173 rec
->ExceptionInformation
[0] = (get_error_code(HANDLER_CONTEXT
) & 2) != 0;
1174 rec
->ExceptionInformation
[1] = (ULONG_PTR
)FAULT_ADDRESS
;
1177 case T_ALIGNFLT
: /* Alignment check exception */
1178 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1181 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT
) );
1183 case T_NMI
: /* NMI interrupt */
1184 case T_DNA
: /* Device not available exception */
1185 case T_DOUBLEFLT
: /* Double fault exception */
1186 case T_TSSFLT
: /* Invalid TSS exception */
1187 case T_RESERVED
: /* Unknown exception */
1188 case T_MCHK
: /* Machine check exception */
1190 case T_CACHEFLT
: /* Cache flush exception */
1192 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1198 /**********************************************************************
1201 * Handler for SIGTRAP.
1203 static HANDLER_DEF(trap_handler
)
1205 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_trap_exception
);
1207 switch(get_trap_code(HANDLER_CONTEXT
))
1209 case T_TRCTRAP
: /* Single-step exception */
1210 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1212 case T_BPTFLT
: /* Breakpoint exception */
1213 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
1216 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1222 /**********************************************************************
1225 * Handler for SIGFPE.
1227 static HANDLER_DEF(fpe_handler
)
1229 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_exception
);
1232 context
= get_exception_context( rec
);
1233 save_fpu( context
, HANDLER_CONTEXT
);
1235 switch(get_trap_code(HANDLER_CONTEXT
))
1237 case T_DIVIDE
: /* Division by zero exception */
1238 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1240 case T_FPOPFLT
: /* Coprocessor segment overrun */
1241 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1243 case T_ARITHTRAP
: /* Floating point exception */
1244 case T_UNKNOWN
: /* Unknown fault code */
1245 rec
->ExceptionCode
= get_fpu_code( context
);
1248 ERR( "Got unexpected trap %d\n", get_trap_code(HANDLER_CONTEXT
) );
1249 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1255 /**********************************************************************
1258 * Handler for SIGINT.
1260 * FIXME: should not be calling external functions on the signal stack.
1262 static HANDLER_DEF(int_handler
)
1265 init_handler( HANDLER_CONTEXT
, &fs
, &gs
);
1266 if (!dispatch_signal(SIGINT
))
1268 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_exception
);
1269 rec
->ExceptionCode
= CONTROL_C_EXIT
;
1273 /**********************************************************************
1276 * Handler for SIGABRT.
1278 static HANDLER_DEF(abrt_handler
)
1280 EXCEPTION_RECORD
*rec
= setup_exception( HANDLER_CONTEXT
, raise_exception
);
1281 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
1282 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
1286 /**********************************************************************
1289 * Handler for SIGTERM.
1291 static HANDLER_DEF(term_handler
)
1294 init_handler( HANDLER_CONTEXT
, &fs
, &gs
);
1295 server_abort_thread(0);
1299 /**********************************************************************
1302 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1304 static HANDLER_DEF(usr1_handler
)
1309 init_handler( HANDLER_CONTEXT
, &fs
, &gs
);
1310 save_context( &context
, HANDLER_CONTEXT
, fs
, gs
);
1311 wait_suspend( &context
);
1312 restore_context( &context
, HANDLER_CONTEXT
);
1316 /**********************************************************************
1317 * get_signal_stack_total_size
1319 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1320 * Must be a power of two.
1322 size_t get_signal_stack_total_size(void)
1324 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
1326 if (!signal_stack_size
)
1328 size_t size
= 4096, min_size
= teb_size
+ max( MINSIGSTKSZ
, 4096 );
1329 /* find the first power of two not smaller than min_size */
1330 while (size
< min_size
) size
*= 2;
1331 signal_stack_mask
= size
- 1;
1332 signal_stack_size
= size
- teb_size
;
1334 return signal_stack_size
+ teb_size
;
1338 /***********************************************************************
1341 * Set a signal handler
1343 static int set_handler( int sig
, int have_sigaltstack
, void (*func
)() )
1345 struct sigaction sig_act
;
1348 if (!have_sigaltstack
)
1350 struct kernel_sigaction sig_act
;
1351 sig_act
.ksa_handler
= func
;
1352 sig_act
.ksa_flags
= SA_RESTART
;
1353 sig_act
.ksa_mask
= (1 << (SIGINT
-1)) |
1354 (1 << (SIGUSR1
-1)) |
1356 /* point to the top of the signal stack */
1357 sig_act
.ksa_restorer
= (char *)get_signal_stack() + signal_stack_size
;
1358 return wine_sigaction( sig
, &sig_act
, NULL
);
1361 sig_act
.sa_handler
= func
;
1362 sigemptyset( &sig_act
.sa_mask
);
1363 sigaddset( &sig_act
.sa_mask
, SIGINT
);
1364 sigaddset( &sig_act
.sa_mask
, SIGUSR1
);
1365 sigaddset( &sig_act
.sa_mask
, SIGUSR2
);
1367 #if defined(linux) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
1368 sig_act
.sa_flags
= SA_RESTART
;
1369 #elif defined (__svr4__) || defined(_SCO_DS) || defined(__APPLE__)
1370 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1372 sig_act
.sa_flags
= 0;
1376 if (have_sigaltstack
) sig_act
.sa_flags
|= SA_ONSTACK
;
1378 return sigaction( sig
, &sig_act
, NULL
);
1382 /***********************************************************************
1383 * __wine_set_signal_handler (NTDLL.@)
1385 int __wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1387 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1388 if (handlers
[sig
] != NULL
) return -2;
1389 handlers
[sig
] = wsh
;
1394 /**********************************************************************
1397 BOOL
SIGNAL_Init(void)
1399 int have_sigaltstack
= 0;
1401 #ifdef HAVE_SIGALTSTACK
1402 struct sigaltstack ss
;
1403 ss
.ss_sp
= get_signal_stack();
1404 ss
.ss_size
= signal_stack_size
;
1406 if (!sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1408 /* sigaltstack may fail because the kernel is too old, or
1409 because glibc is brain-dead. In the latter case a
1410 direct system call should succeed. */
1411 else if (!wine_sigaltstack(&ss
, NULL
)) have_sigaltstack
= 1;
1413 #endif /* HAVE_SIGALTSTACK */
1415 if (set_handler( SIGINT
, have_sigaltstack
, (void (*)())int_handler
) == -1) goto error
;
1416 if (set_handler( SIGFPE
, have_sigaltstack
, (void (*)())fpe_handler
) == -1) goto error
;
1417 if (set_handler( SIGSEGV
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1418 if (set_handler( SIGILL
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1419 if (set_handler( SIGABRT
, have_sigaltstack
, (void (*)())abrt_handler
) == -1) goto error
;
1420 if (set_handler( SIGTERM
, have_sigaltstack
, (void (*)())term_handler
) == -1) goto error
;
1421 if (set_handler( SIGUSR1
, have_sigaltstack
, (void (*)())usr1_handler
) == -1) goto error
;
1423 if (set_handler( SIGBUS
, have_sigaltstack
, (void (*)())segv_handler
) == -1) goto error
;
1426 if (set_handler( SIGTRAP
, have_sigaltstack
, (void (*)())trap_handler
) == -1) goto error
;
1430 if (set_handler( SIGUSR2
, have_sigaltstack
, (void (*)())usr2_handler
) == -1) goto error
;
1436 perror("sigaction");
1442 /**********************************************************************
1443 * __wine_enter_vm86 (NTDLL.@)
1445 * Enter vm86 mode with the specified register context.
1447 void __wine_enter_vm86( CONTEXT
*context
)
1449 EXCEPTION_RECORD rec
;
1451 struct vm86plus_struct vm86
;
1453 memset( &vm86
, 0, sizeof(vm86
) );
1456 restore_vm86_context( context
, &vm86
);
1458 ntdll_get_thread_data()->vm86_ptr
= &vm86
;
1459 merge_vm86_pending_flags( &rec
);
1461 res
= vm86_enter( &ntdll_get_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1468 save_vm86_context( context
, &vm86
);
1470 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1471 rec
.ExceptionRecord
= NULL
;
1472 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1473 rec
.NumberParameters
= 0;
1475 switch(VM86_TYPE(res
))
1477 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1478 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
1479 raise_segv_exception( &rec
, context
);
1481 case VM86_TRAP
: /* return due to DOS-debugger request */
1482 switch(VM86_ARG(res
))
1484 case T_TRCTRAP
: /* Single-step exception, single step flag is cleared by raise_trap_exception */
1485 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1487 case T_BPTFLT
: /* Breakpoint exception */
1488 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
1491 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
1494 raise_trap_exception( &rec
, context
);
1496 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1497 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1498 rec
.NumberParameters
= 1;
1499 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1501 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1502 context
->EFlags
|= VIF_MASK
;
1503 context
->EFlags
&= ~VIP_MASK
;
1504 NtCurrentTeb()->vm86_pending
= 0;
1505 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1507 case VM86_PICRETURN
: /* return due to pending PIC request */
1508 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1510 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
1512 ERR( "unhandled result from vm86 mode %x\n", res
);
1515 __regs_RtlRaiseException( &rec
, context
);
1519 #else /* __HAVE_VM86 */
1520 /**********************************************************************
1521 * __wine_enter_vm86 (NTDLL.@)
1523 void __wine_enter_vm86( CONTEXT
*context
)
1525 MESSAGE("vm86 mode not supported on this platform\n");
1527 #endif /* __HAVE_VM86 */
1529 /**********************************************************************
1530 * DbgBreakPoint (NTDLL.@)
1532 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret");
1534 /**********************************************************************
1535 * DbgUserBreakPoint (NTDLL.@)
1537 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret");
1540 /**********************************************************************
1541 * EXC_CallHandler (internal)
1543 * Some exception handlers depend on EBP to have a fixed position relative to
1544 * the exception frame.
1545 * Shrinker depends on (*1) doing what it does,
1546 * (*2) being the exact instruction it is and (*3) beginning with 0x64
1547 * (i.e. the %fs prefix to the movl instruction). It also depends on the
1548 * function calling the handler having only 5 parameters (*4).
1550 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
1552 " movl %esp, %ebp\n"
1554 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
1560 " call " __ASM_NAME("call_exception_handler") "\n"
1564 __ASM_GLOBAL_FUNC(call_exception_handler
,
1566 " movl %esp, %ebp\n"
1568 " pushl 12(%ebp)\n" /* make any exceptions in this... */
1569 " pushl %edx\n" /* handler be handled by... */
1571 " pushl (0)\n" /* nested_handler (passed in edx). */
1573 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
1578 " movl 24(%ebp), %ecx\n" /* (*1) */
1579 " call *%ecx\n" /* call handler. (*2) */
1581 " movl (0), %esp\n" /* restore previous... (*3) */
1583 " popl (0)\n" /* exception frame. */
1584 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
1586 " ret $20\n" /* (*4) */
1588 #endif /* __i386__ */