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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
31 #include <sys/types.h>
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
59 #define WIN32_NO_STATUS
61 #include "wine/library.h"
62 #include "ntdll_misc.h"
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 #ifdef HAVE_VALGRIND_MEMCHECK_H
67 #include <valgrind/memcheck.h>
70 #undef ERR /* Solaris needs to define this */
72 /* not defined for x86, so copy the x86_64 definition */
73 typedef struct DECLSPEC_ALIGN(16) _M128A
94 M128A FloatRegisters
[8];
95 M128A XmmRegisters
[16];
99 /***********************************************************************
100 * signal context platform-specific definitions
105 typedef ucontext_t SIGCONTEXT
;
107 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
108 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
109 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
110 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
111 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
112 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
113 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
114 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
116 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
117 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
118 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
119 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
120 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
121 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
123 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
124 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
125 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
126 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
128 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
129 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
131 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
132 #define VIF_FLAG 0x00080000
133 #define VIP_FLAG 0x00100000
135 int vm86_enter( void **vm86_ptr
);
136 void vm86_return(void);
137 void vm86_return_end(void);
138 __ASM_GLOBAL_FUNC(vm86_enter
,
140 "movl %esp, %ebp\n\t"
141 "movl $166,%eax\n\t" /*SYS_vm86*/
142 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
143 "movl (%ecx),%ecx\n\t"
145 "movl $1,%ebx\n\t" /*VM86_ENTER*/
146 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
150 ".globl " __ASM_NAME("vm86_return") "\n\t"
151 __ASM_FUNC("vm86_return") "\n"
152 __ASM_NAME("vm86_return") ":\n\t"
158 "testl %eax,%eax\n\t"
160 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
161 "je " __ASM_NAME("vm86_enter") "\n\t"
163 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
165 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
166 __ASM_FUNC("vm86_return_end") "\n"
167 __ASM_NAME("vm86_return_end") ":\n\t"
170 #ifdef HAVE_SYS_VM86_H
178 #include <machine/frame.h>
179 typedef struct trapframe SIGCONTEXT
;
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 #define EFL_sig(context) ((context)->tf_eflags)
196 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
197 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
199 #define FPU_sig(context) NULL /* FIXME */
200 #define FPUX_sig(context) NULL /* FIXME */
204 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
206 typedef struct sigcontext SIGCONTEXT
;
208 #define EAX_sig(context) ((context)->sc_eax)
209 #define EBX_sig(context) ((context)->sc_ebx)
210 #define ECX_sig(context) ((context)->sc_ecx)
211 #define EDX_sig(context) ((context)->sc_edx)
212 #define ESI_sig(context) ((context)->sc_esi)
213 #define EDI_sig(context) ((context)->sc_edi)
214 #define EBP_sig(context) ((context)->sc_ebp)
216 #define CS_sig(context) ((context)->sc_cs)
217 #define DS_sig(context) ((context)->sc_ds)
218 #define ES_sig(context) ((context)->sc_es)
219 #define FS_sig(context) ((context)->sc_fs)
220 #define GS_sig(context) ((context)->sc_gs)
221 #define SS_sig(context) ((context)->sc_ss)
223 #define TRAP_sig(context) ((context)->sc_trapno)
224 #define ERROR_sig(context) ((context)->sc_err)
225 #define EFL_sig(context) ((context)->sc_eflags)
227 #define EIP_sig(context) ((context)->sc_eip)
228 #define ESP_sig(context) ((context)->sc_esp)
230 #define FPU_sig(context) NULL /* FIXME */
231 #define FPUX_sig(context) NULL /* FIXME */
233 #endif /* __FreeBSD__ */
237 typedef struct sigcontext SIGCONTEXT
;
239 #define EAX_sig(context) ((context)->sc_eax)
240 #define EBX_sig(context) ((context)->sc_ebx)
241 #define ECX_sig(context) ((context)->sc_ecx)
242 #define EDX_sig(context) ((context)->sc_edx)
243 #define ESI_sig(context) ((context)->sc_esi)
244 #define EDI_sig(context) ((context)->sc_edi)
245 #define EBP_sig(context) ((context)->sc_ebp)
247 #define CS_sig(context) ((context)->sc_cs)
248 #define DS_sig(context) ((context)->sc_ds)
249 #define ES_sig(context) ((context)->sc_es)
250 #define FS_sig(context) ((context)->sc_fs)
251 #define GS_sig(context) ((context)->sc_gs)
252 #define SS_sig(context) ((context)->sc_ss)
254 #define TRAP_sig(context) ((context)->sc_trapno)
255 #define ERROR_sig(context) ((context)->sc_err)
256 #define EFL_sig(context) ((context)->sc_eflags)
258 #define EIP_sig(context) ((context)->sc_eip)
259 #define ESP_sig(context) ((context)->sc_esp)
261 #define FPU_sig(context) NULL /* FIXME */
262 #define FPUX_sig(context) NULL /* FIXME */
264 #define T_MCHK T_MACHK
265 #define T_XMMFLT T_XFTRAP
267 #endif /* __OpenBSD__ */
269 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
272 #include <sys/regset.h>
274 #include <sys/ucontext.h>
275 typedef struct ucontext SIGCONTEXT
;
281 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
282 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
283 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
284 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
285 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
286 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
287 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
289 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
290 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
291 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
292 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
294 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
295 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
297 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
299 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
301 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
303 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
305 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
308 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
311 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
314 #define FPU_sig(context) NULL /* FIXME */
315 #define FPUX_sig(context) NULL /* FIXME */
317 #endif /* svr4 || SCO_DS */
320 # include <sys/ucontext.h>
322 typedef ucontext_t SIGCONTEXT
;
324 /* work around silly renaming of struct members in OS X 10.5 */
325 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
326 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
327 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
328 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
329 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
330 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
331 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
332 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
333 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
334 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
335 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
336 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
337 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
338 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
339 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
340 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
341 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
342 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
343 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
344 #define FPU_sig(context) NULL
345 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
347 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
348 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
349 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
350 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
351 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
352 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
353 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
354 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
355 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
356 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
357 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
358 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
359 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
360 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
361 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
362 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
363 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
364 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
365 #define FPU_sig(context) NULL
366 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
369 #endif /* __APPLE__ */
371 #if defined(__NetBSD__)
372 # include <sys/ucontext.h>
373 # include <sys/types.h>
376 typedef ucontext_t SIGCONTEXT
;
378 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
379 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
380 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
381 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
382 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
383 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
384 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
385 #define ESP_sig(context) _UC_MACHINE_SP(context)
387 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
388 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
389 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
390 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
391 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
392 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
394 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
395 #define EIP_sig(context) _UC_MACHINE_PC(context)
396 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
397 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
399 #define FPU_sig(context) NULL
400 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
403 #define T_XMMFLT T_XMM
405 #endif /* __NetBSD__ */
407 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
409 typedef int (*wine_signal_handler
)(unsigned int sig
);
411 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
412 static size_t signal_stack_mask
;
413 static size_t signal_stack_size
;
415 static wine_signal_handler handlers
[256];
417 static int fpux_support
; /* whether the CPU support extended fpu context */
419 extern void DECLSPEC_NORETURN
__wine_call_from_32_restore_regs( const CONTEXT
*context
);
423 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
424 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
425 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
426 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
427 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
428 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
429 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
430 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
431 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
432 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
433 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
434 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
435 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
436 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
437 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
438 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
439 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
440 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
441 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
442 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
443 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
445 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
446 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
447 TRAP_x86_NMI
= 2, /* NMI interrupt */
448 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
449 TRAP_x86_OFLOW
= 4, /* Overflow exception */
450 TRAP_x86_BOUND
= 5, /* Bound range exception */
451 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
452 TRAP_x86_DNA
= 7, /* Device not available exception */
453 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
454 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
455 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
456 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
457 TRAP_x86_STKFLT
= 12, /* Stack fault */
458 TRAP_x86_PROTFLT
= 13, /* General protection fault */
459 TRAP_x86_PAGEFLT
= 14, /* Page fault */
460 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
461 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
462 TRAP_x86_MCHK
= 18, /* Machine check exception */
463 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
464 otherwise Cache flush exception (via SIGSEV) */
469 /***********************************************************************
472 static inline int dispatch_signal(unsigned int sig
)
474 if (handlers
[sig
] == NULL
) return 0;
475 return handlers
[sig
](sig
);
479 /***********************************************************************
482 * Get the trap code for a signal.
484 static inline enum i386_trap_code
get_trap_code( const SIGCONTEXT
*sigcontext
)
487 return TRAP_sig(sigcontext
);
489 return TRAP_x86_UNKNOWN
; /* unknown trap code */
493 /***********************************************************************
496 * Get the error code for a signal.
498 static inline WORD
get_error_code( const SIGCONTEXT
*sigcontext
)
501 return ERROR_sig(sigcontext
);
507 /***********************************************************************
510 * Get the base of the signal stack for the current thread.
512 static inline void *get_signal_stack(void)
514 return (char *)NtCurrentTeb() + 4096;
518 /***********************************************************************
521 * Get the current teb based on the stack pointer.
523 static inline TEB
*get_current_teb(void)
526 __asm__("movl %%esp,%0" : "=g" (esp
) );
527 return (TEB
*)(esp
& ~signal_stack_mask
);
532 /***********************************************************************
535 * Set the register values from a vm86 structure.
537 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
539 context
->ContextFlags
= CONTEXT_FULL
;
540 context
->Eax
= vm86
->regs
.eax
;
541 context
->Ebx
= vm86
->regs
.ebx
;
542 context
->Ecx
= vm86
->regs
.ecx
;
543 context
->Edx
= vm86
->regs
.edx
;
544 context
->Esi
= vm86
->regs
.esi
;
545 context
->Edi
= vm86
->regs
.edi
;
546 context
->Esp
= vm86
->regs
.esp
;
547 context
->Ebp
= vm86
->regs
.ebp
;
548 context
->Eip
= vm86
->regs
.eip
;
549 context
->SegCs
= vm86
->regs
.cs
;
550 context
->SegDs
= vm86
->regs
.ds
;
551 context
->SegEs
= vm86
->regs
.es
;
552 context
->SegFs
= vm86
->regs
.fs
;
553 context
->SegGs
= vm86
->regs
.gs
;
554 context
->SegSs
= vm86
->regs
.ss
;
555 context
->EFlags
= vm86
->regs
.eflags
;
559 /***********************************************************************
560 * restore_vm86_context
562 * Build a vm86 structure from the register values.
564 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
566 vm86
->regs
.eax
= context
->Eax
;
567 vm86
->regs
.ebx
= context
->Ebx
;
568 vm86
->regs
.ecx
= context
->Ecx
;
569 vm86
->regs
.edx
= context
->Edx
;
570 vm86
->regs
.esi
= context
->Esi
;
571 vm86
->regs
.edi
= context
->Edi
;
572 vm86
->regs
.esp
= context
->Esp
;
573 vm86
->regs
.ebp
= context
->Ebp
;
574 vm86
->regs
.eip
= context
->Eip
;
575 vm86
->regs
.cs
= context
->SegCs
;
576 vm86
->regs
.ds
= context
->SegDs
;
577 vm86
->regs
.es
= context
->SegEs
;
578 vm86
->regs
.fs
= context
->SegFs
;
579 vm86
->regs
.gs
= context
->SegGs
;
580 vm86
->regs
.ss
= context
->SegSs
;
581 vm86
->regs
.eflags
= context
->EFlags
;
585 /**********************************************************************
586 * merge_vm86_pending_flags
588 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
589 * raises exception if there are pending events and VIF flag
590 * has been turned on.
592 * Called from __wine_enter_vm86 because vm86_enter
593 * doesn't check for pending events.
595 * Called from raise_vm86_sti_exception to check for
596 * pending events in a signal safe way.
598 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
600 BOOL check_pending
= TRUE
;
601 struct vm86plus_struct
*vm86
=
602 (struct vm86plus_struct
*)(ntdll_get_thread_data()->vm86_ptr
);
605 * In order to prevent a race when SIGUSR2 occurs while
606 * we are returning from exception handler, pending events
607 * will be rechecked after each raised exception.
609 while (check_pending
&& get_vm86_teb_info()->vm86_pending
)
611 check_pending
= FALSE
;
612 ntdll_get_thread_data()->vm86_ptr
= NULL
;
615 * If VIF is set, throw exception.
616 * Note that SIGUSR2 may turn VIF flag off so
617 * VIF check must occur only when TEB.vm86_ptr is NULL.
619 if (vm86
->regs
.eflags
& VIF_FLAG
)
622 save_vm86_context( &vcontext
, vm86
);
624 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
625 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
626 rec
->ExceptionRecord
= NULL
;
627 rec
->NumberParameters
= 0;
628 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
630 vcontext
.EFlags
&= ~VIP_FLAG
;
631 get_vm86_teb_info()->vm86_pending
= 0;
632 __regs_RtlRaiseException( rec
, &vcontext
);
634 restore_vm86_context( &vcontext
, vm86
);
635 check_pending
= TRUE
;
638 ntdll_get_thread_data()->vm86_ptr
= vm86
;
642 * Merge VIP flags in a signal safe way. This requires
643 * that the following operation compiles into atomic
646 vm86
->regs
.eflags
|= get_vm86_teb_info()->vm86_pending
;
648 #endif /* __HAVE_VM86 */
653 /* We have to workaround two Solaris breakages:
654 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
656 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
657 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
660 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
661 __ASM_GLOBAL_FUNC( sigaction_syscall
,
662 "movl $0x62,%eax\n\t"
666 /* assume the same libc handler is used for all signals */
667 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
669 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
671 struct ntdll_thread_data
*thread_data
;
673 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
675 thread_data
= (struct ntdll_thread_data
*)get_current_teb()->SystemReserved2
;
676 wine_set_fs( thread_data
->fs
);
677 wine_set_gs( thread_data
->gs
);
679 libc_sigacthandler( signal
, siginfo
, sigcontext
);
682 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
684 struct sigaction real_act
;
686 if (sigaction( sig
, new, old
) == -1) return -1;
688 /* retrieve the real handler and flags with a direct syscall */
689 sigaction_syscall( sig
, NULL
, &real_act
);
690 libc_sigacthandler
= real_act
.sa_sigaction
;
691 real_act
.sa_sigaction
= wine_sigacthandler
;
692 sigaction_syscall( sig
, &real_act
, NULL
);
695 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
699 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
702 /***********************************************************************
705 * Handler initialization when the full context is not needed.
706 * Return the stack pointer to use for pushing the exception data.
708 static inline void *init_handler( const SIGCONTEXT
*sigcontext
, WORD
*fs
, WORD
*gs
)
710 TEB
*teb
= get_current_teb();
712 /* get %fs and %gs at time of the fault */
714 *fs
= LOWORD(FS_sig(sigcontext
));
719 *gs
= LOWORD(GS_sig(sigcontext
));
724 #ifndef __sun /* see above for Solaris handling */
726 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
727 wine_set_fs( thread_data
->fs
);
728 wine_set_gs( thread_data
->gs
);
732 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
733 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
736 * Win16 or DOS protected mode. Note that during switch
737 * from 16-bit mode to linear mode, CS may be set to system
738 * segment before FS is restored. Fortunately, in this case
739 * SS is still non-system segment. This is why both CS and SS
742 return teb
->WOW32Reserved
;
744 return (void *)(ESP_sig(sigcontext
) & ~3);
748 /***********************************************************************
751 * Save the thread FPU context.
753 static inline void save_fpu( CONTEXT
*context
)
756 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
757 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
762 /***********************************************************************
765 * Save the thread FPU extended context.
767 static inline void save_fpux( CONTEXT
*context
)
770 /* we have to enforce alignment by hand */
771 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
772 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
774 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
775 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
776 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
781 /***********************************************************************
784 * Restore the FPU context to a sigcontext.
786 static inline void restore_fpu( const CONTEXT
*context
)
788 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
789 /* reset the current interrupt status */
790 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
792 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
793 #endif /* __GNUC__ */
797 /***********************************************************************
800 * Restore the FPU extended context to a sigcontext.
802 static inline void restore_fpux( const CONTEXT
*context
)
805 /* we have to enforce alignment by hand */
806 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
807 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
809 memcpy( state
, context
->ExtendedRegisters
, sizeof(*state
) );
810 /* reset the current interrupt status */
811 state
->StatusWord
&= state
->ControlWord
| 0xff80;
812 __asm__
__volatile__( "fxrstor %0" : : "m" (*state
) );
817 /***********************************************************************
820 * Build a standard FPU context from an extended one.
822 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XMM_SAVE_AREA32
*fpux
)
824 unsigned int i
, tag
, stack_top
;
826 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
827 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
828 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
829 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
830 fpu
->DataOffset
= fpux
->DataOffset
;
831 fpu
->DataSelector
= fpux
->DataSelector
;
832 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
834 stack_top
= (fpux
->StatusWord
>> 11) & 7;
835 fpu
->TagWord
= 0xffff0000;
836 for (i
= 0; i
< 8; i
++)
838 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
839 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
842 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
843 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
845 tag
= 2; /* special */
847 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
849 if (reg
->Low
) tag
= 2; /* special */
850 else tag
= 1; /* zero */
854 if (reg
->Low
>> 63) tag
= 0; /* valid */
855 else tag
= 2; /* special */
858 fpu
->TagWord
|= tag
<< (2 * i
);
863 /***********************************************************************
866 * Build a context structure from the signal info.
868 static inline void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
, WORD fs
, WORD gs
)
870 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
871 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
872 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
874 memset(context
, 0, sizeof(*context
));
875 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
876 context
->Eax
= EAX_sig(sigcontext
);
877 context
->Ebx
= EBX_sig(sigcontext
);
878 context
->Ecx
= ECX_sig(sigcontext
);
879 context
->Edx
= EDX_sig(sigcontext
);
880 context
->Esi
= ESI_sig(sigcontext
);
881 context
->Edi
= EDI_sig(sigcontext
);
882 context
->Ebp
= EBP_sig(sigcontext
);
883 context
->EFlags
= EFL_sig(sigcontext
);
884 context
->Eip
= EIP_sig(sigcontext
);
885 context
->Esp
= ESP_sig(sigcontext
);
886 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
887 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
888 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
891 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
892 context
->Dr0
= regs
->dr0
;
893 context
->Dr1
= regs
->dr1
;
894 context
->Dr2
= regs
->dr2
;
895 context
->Dr3
= regs
->dr3
;
896 context
->Dr6
= regs
->dr6
;
897 context
->Dr7
= regs
->dr7
;
901 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
902 context
->FloatSave
= *fpu
;
906 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
907 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
909 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
911 if (!fpu
&& !fpux
) save_fpu( context
);
915 /***********************************************************************
918 * Restore the signal info from the context.
920 static inline void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
922 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
923 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
924 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
926 regs
->dr0
= context
->Dr0
;
927 regs
->dr1
= context
->Dr1
;
928 regs
->dr2
= context
->Dr2
;
929 regs
->dr3
= context
->Dr3
;
930 regs
->dr6
= context
->Dr6
;
931 regs
->dr7
= context
->Dr7
;
932 EAX_sig(sigcontext
) = context
->Eax
;
933 EBX_sig(sigcontext
) = context
->Ebx
;
934 ECX_sig(sigcontext
) = context
->Ecx
;
935 EDX_sig(sigcontext
) = context
->Edx
;
936 ESI_sig(sigcontext
) = context
->Esi
;
937 EDI_sig(sigcontext
) = context
->Edi
;
938 EBP_sig(sigcontext
) = context
->Ebp
;
939 EFL_sig(sigcontext
) = context
->EFlags
;
940 EIP_sig(sigcontext
) = context
->Eip
;
941 ESP_sig(sigcontext
) = context
->Esp
;
942 CS_sig(sigcontext
) = context
->SegCs
;
943 DS_sig(sigcontext
) = context
->SegDs
;
944 ES_sig(sigcontext
) = context
->SegEs
;
945 SS_sig(sigcontext
) = context
->SegSs
;
947 GS_sig(sigcontext
) = context
->SegGs
;
949 wine_set_gs( context
->SegGs
);
952 FS_sig(sigcontext
) = context
->SegFs
;
954 wine_set_fs( context
->SegFs
);
957 if (fpu
) *fpu
= context
->FloatSave
;
958 if (fpux
) memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
959 if (!fpu
&& !fpux
) restore_fpu( context
);
963 /***********************************************************************
964 * RtlCaptureContext (NTDLL.@)
966 void WINAPI
__regs_RtlCaptureContext( CONTEXT
*context
, CONTEXT
*regs
)
969 if (fpux_support
) save_fpux( context
);
970 else save_fpu( context
);
972 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext
, 1 )
975 /***********************************************************************
978 * Set the new CPU context. Used by NtSetContextThread.
980 void set_cpu_context( const CONTEXT
*context
)
982 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
984 if ((flags
& CONTEXT_EXTENDED_REGISTERS
) && fpux_support
) restore_fpux( context
);
985 else if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
987 if (flags
& CONTEXT_DEBUG_REGISTERS
)
989 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
990 regs
->dr0
= context
->Dr0
;
991 regs
->dr1
= context
->Dr1
;
992 regs
->dr2
= context
->Dr2
;
993 regs
->dr3
= context
->Dr3
;
994 regs
->dr6
= context
->Dr6
;
995 regs
->dr7
= context
->Dr7
;
997 if (flags
& CONTEXT_FULL
)
999 if (!(flags
& CONTEXT_CONTROL
))
1000 FIXME( "setting partial context (%x) not supported\n", flags
);
1001 else if (flags
& CONTEXT_SEGMENTS
)
1002 __wine_call_from_32_restore_regs( context
);
1005 CONTEXT newcontext
= *context
;
1006 newcontext
.SegDs
= wine_get_ds();
1007 newcontext
.SegEs
= wine_get_es();
1008 newcontext
.SegFs
= wine_get_fs();
1009 newcontext
.SegGs
= wine_get_gs();
1010 __wine_call_from_32_restore_regs( &newcontext
);
1016 /***********************************************************************
1019 * Copy a register context according to the flags.
1021 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1023 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
1024 if (flags
& CONTEXT_INTEGER
)
1026 to
->Eax
= from
->Eax
;
1027 to
->Ebx
= from
->Ebx
;
1028 to
->Ecx
= from
->Ecx
;
1029 to
->Edx
= from
->Edx
;
1030 to
->Esi
= from
->Esi
;
1031 to
->Edi
= from
->Edi
;
1033 if (flags
& CONTEXT_CONTROL
)
1035 to
->Ebp
= from
->Ebp
;
1036 to
->Esp
= from
->Esp
;
1037 to
->Eip
= from
->Eip
;
1038 to
->SegCs
= from
->SegCs
;
1039 to
->SegSs
= from
->SegSs
;
1040 to
->EFlags
= from
->EFlags
;
1042 if (flags
& CONTEXT_SEGMENTS
)
1044 to
->SegDs
= from
->SegDs
;
1045 to
->SegEs
= from
->SegEs
;
1046 to
->SegFs
= from
->SegFs
;
1047 to
->SegGs
= from
->SegGs
;
1049 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1051 to
->Dr0
= from
->Dr0
;
1052 to
->Dr1
= from
->Dr1
;
1053 to
->Dr2
= from
->Dr2
;
1054 to
->Dr3
= from
->Dr3
;
1055 to
->Dr6
= from
->Dr6
;
1056 to
->Dr7
= from
->Dr7
;
1058 if (flags
& CONTEXT_FLOATING_POINT
)
1060 to
->FloatSave
= from
->FloatSave
;
1062 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1064 memcpy( to
->ExtendedRegisters
, from
->ExtendedRegisters
, sizeof(to
->ExtendedRegisters
) );
1069 /***********************************************************************
1072 * Convert a register context to the server format.
1074 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1076 DWORD flags
= from
->ContextFlags
& ~CONTEXT_i386
; /* get rid of CPU id */
1078 memset( to
, 0, sizeof(*to
) );
1081 if (flags
& CONTEXT_CONTROL
)
1083 to
->flags
|= SERVER_CTX_CONTROL
;
1084 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
1085 to
->ctl
.i386_regs
.esp
= from
->Esp
;
1086 to
->ctl
.i386_regs
.eip
= from
->Eip
;
1087 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
1088 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
1089 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
1091 if (flags
& CONTEXT_INTEGER
)
1093 to
->flags
|= SERVER_CTX_INTEGER
;
1094 to
->integer
.i386_regs
.eax
= from
->Eax
;
1095 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
1096 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
1097 to
->integer
.i386_regs
.edx
= from
->Edx
;
1098 to
->integer
.i386_regs
.esi
= from
->Esi
;
1099 to
->integer
.i386_regs
.edi
= from
->Edi
;
1101 if (flags
& CONTEXT_SEGMENTS
)
1103 to
->flags
|= SERVER_CTX_SEGMENTS
;
1104 to
->seg
.i386_regs
.ds
= from
->SegDs
;
1105 to
->seg
.i386_regs
.es
= from
->SegEs
;
1106 to
->seg
.i386_regs
.fs
= from
->SegFs
;
1107 to
->seg
.i386_regs
.gs
= from
->SegGs
;
1109 if (flags
& CONTEXT_FLOATING_POINT
)
1111 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1112 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
1113 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
1114 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
1115 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
1116 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
1117 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
1118 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
1119 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
1120 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
1122 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1124 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1125 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
1126 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
1127 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
1128 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
1129 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
1130 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
1132 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1134 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
1135 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
1137 return STATUS_SUCCESS
;
1141 /***********************************************************************
1142 * context_from_server
1144 * Convert a register context from the server format.
1146 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1148 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
1150 to
->ContextFlags
= CONTEXT_i386
;
1151 if (from
->flags
& SERVER_CTX_CONTROL
)
1153 to
->ContextFlags
|= CONTEXT_CONTROL
;
1154 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
1155 to
->Esp
= from
->ctl
.i386_regs
.esp
;
1156 to
->Eip
= from
->ctl
.i386_regs
.eip
;
1157 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
1158 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
1159 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
1161 if (from
->flags
& SERVER_CTX_INTEGER
)
1163 to
->ContextFlags
|= CONTEXT_INTEGER
;
1164 to
->Eax
= from
->integer
.i386_regs
.eax
;
1165 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
1166 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
1167 to
->Edx
= from
->integer
.i386_regs
.edx
;
1168 to
->Esi
= from
->integer
.i386_regs
.esi
;
1169 to
->Edi
= from
->integer
.i386_regs
.edi
;
1171 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1173 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1174 to
->SegDs
= from
->seg
.i386_regs
.ds
;
1175 to
->SegEs
= from
->seg
.i386_regs
.es
;
1176 to
->SegFs
= from
->seg
.i386_regs
.fs
;
1177 to
->SegGs
= from
->seg
.i386_regs
.gs
;
1179 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1181 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1182 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
1183 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
1184 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
1185 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
1186 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
1187 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
1188 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
1189 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
1190 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
1192 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1194 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1195 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
1196 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
1197 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
1198 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
1199 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
1200 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
1202 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
1204 to
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1205 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
1207 return STATUS_SUCCESS
;
1211 /***********************************************************************
1212 * is_privileged_instr
1214 * Check if the fault location is a privileged instruction.
1215 * Based on the instruction emulation code in dlls/kernel/instr.c.
1217 static inline DWORD
is_privileged_instr( CONTEXT86
*context
)
1220 unsigned int prefix_count
= 0;
1222 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
1223 instr
= (BYTE
*)context
->Eip
;
1225 for (;;) switch(*instr
)
1227 /* instruction prefixes */
1228 case 0x2e: /* %cs: */
1229 case 0x36: /* %ss: */
1230 case 0x3e: /* %ds: */
1231 case 0x26: /* %es: */
1232 case 0x64: /* %fs: */
1233 case 0x65: /* %gs: */
1234 case 0x66: /* opcode size */
1235 case 0x67: /* addr size */
1236 case 0xf0: /* lock */
1237 case 0xf2: /* repne */
1238 case 0xf3: /* repe */
1239 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1243 case 0x0f: /* extended instruction */
1246 case 0x20: /* mov crX, reg */
1247 case 0x21: /* mov drX, reg */
1248 case 0x22: /* mov reg, crX */
1249 case 0x23: /* mov reg drX */
1250 return EXCEPTION_PRIV_INSTRUCTION
;
1253 case 0x6c: /* insb (%dx) */
1254 case 0x6d: /* insl (%dx) */
1255 case 0x6e: /* outsb (%dx) */
1256 case 0x6f: /* outsl (%dx) */
1257 case 0xcd: /* int $xx */
1258 case 0xe4: /* inb al,XX */
1259 case 0xe5: /* in (e)ax,XX */
1260 case 0xe6: /* outb XX,al */
1261 case 0xe7: /* out XX,(e)ax */
1262 case 0xec: /* inb (%dx),%al */
1263 case 0xed: /* inl (%dx),%eax */
1264 case 0xee: /* outb %al,(%dx) */
1265 case 0xef: /* outl %eax,(%dx) */
1266 case 0xf4: /* hlt */
1267 case 0xfa: /* cli */
1268 case 0xfb: /* sti */
1269 return EXCEPTION_PRIV_INSTRUCTION
;
1276 #include "pshpack1.h"
1279 DWORD movl
; /* movl this,4(%esp) */
1281 BYTE jmp
; /* jmp func */
1284 #include "poppack.h"
1286 /**********************************************************************
1289 * Check if code destination is an ATL thunk, and emulate it if so.
1291 static BOOL
check_atl_thunk( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1293 const struct atl_thunk
*thunk
= (const struct atl_thunk
*)rec
->ExceptionInformation
[1];
1298 if (thunk
->movl
== 0x042444c7 && thunk
->jmp
== 0xe9)
1300 *((DWORD
*)context
->Esp
+ 1) = thunk
->this;
1301 context
->Eip
= (DWORD_PTR
)(&thunk
->func
+ 1) + thunk
->func
;
1302 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1303 thunk
, context
->Eip
, *((DWORD
*)context
->Esp
+ 1) );
1316 /***********************************************************************
1317 * setup_exception_record
1319 * Setup the exception record and context on the thread stack.
1321 static EXCEPTION_RECORD
*setup_exception_record( SIGCONTEXT
*sigcontext
, void *stack_ptr
,
1322 WORD fs
, WORD gs
, raise_func func
)
1326 void *ret_addr
; /* return address from raise_func */
1327 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
1328 CONTEXT
*context_ptr
; /* second arg for raise_func */
1330 EXCEPTION_RECORD rec
;
1333 } *stack
= stack_ptr
;
1334 DWORD exception_code
= 0;
1336 /* stack sanity checks */
1338 if ((char *)stack
>= (char *)get_signal_stack() &&
1339 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1341 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1342 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1343 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1344 NtCurrentTeb()->Tib
.StackBase
);
1348 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1349 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1350 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1352 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1353 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1354 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1355 NtCurrentTeb()->Tib
.StackBase
);
1357 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1359 /* stack overflow on last page, unrecoverable */
1360 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1361 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1362 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1363 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1364 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1367 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1369 /* stack access below stack limit, may be recoverable */
1370 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1373 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1374 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1375 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1376 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1377 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1382 stack
--; /* push the stack_layout structure */
1383 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1384 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1385 #elif defined(VALGRIND_MAKE_WRITABLE)
1386 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1388 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
1389 stack
->rec_ptr
= &stack
->rec
;
1390 stack
->context_ptr
= &stack
->context
;
1392 stack
->rec
.ExceptionRecord
= NULL
;
1393 stack
->rec
.ExceptionCode
= exception_code
;
1394 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1395 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
1396 stack
->rec
.NumberParameters
= 0;
1398 save_context( &stack
->context
, sigcontext
, fs
, gs
);
1400 /* now modify the sigcontext to return to the raise function */
1401 ESP_sig(sigcontext
) = (DWORD
)stack
;
1402 EIP_sig(sigcontext
) = (DWORD
)func
;
1403 /* clear single-step, direction, and align check flag */
1404 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1405 CS_sig(sigcontext
) = wine_get_cs();
1406 DS_sig(sigcontext
) = wine_get_ds();
1407 ES_sig(sigcontext
) = wine_get_es();
1408 FS_sig(sigcontext
) = wine_get_fs();
1409 GS_sig(sigcontext
) = wine_get_gs();
1410 SS_sig(sigcontext
) = wine_get_ss();
1412 return stack
->rec_ptr
;
1416 /***********************************************************************
1419 * Setup a proper stack frame for the raise function, and modify the
1420 * sigcontext so that the return from the signal handler will call
1421 * the raise function.
1423 static EXCEPTION_RECORD
*setup_exception( SIGCONTEXT
*sigcontext
, raise_func func
)
1426 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1428 return setup_exception_record( sigcontext
, stack
, fs
, gs
, func
);
1432 /***********************************************************************
1433 * get_exception_context
1435 * Get a pointer to the context built by setup_exception.
1437 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
1439 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
1443 /**********************************************************************
1446 * Get the FPU exception code from the FPU status.
1448 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1450 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1452 if (status
& 0x01) /* IE */
1454 if (status
& 0x40) /* SF */
1455 return EXCEPTION_FLT_STACK_CHECK
;
1457 return EXCEPTION_FLT_INVALID_OPERATION
;
1459 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1460 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1461 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1462 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1463 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1464 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1468 /**********************************************************************
1469 * raise_segv_exception
1471 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1473 switch(rec
->ExceptionCode
)
1475 case EXCEPTION_ACCESS_VIOLATION
:
1476 if (rec
->NumberParameters
== 2)
1478 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
&& check_atl_thunk( rec
, context
))
1480 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
1481 rec
->ExceptionInformation
[0] )))
1485 case EXCEPTION_DATATYPE_MISALIGNMENT
:
1486 /* FIXME: pass through exception handler first? */
1487 if (context
->EFlags
& 0x00040000)
1489 /* Disable AC flag, return */
1490 context
->EFlags
&= ~0x00040000;
1495 __regs_RtlRaiseException( rec
, context
);
1497 NtSetContextThread( GetCurrentThread(), context
);
1501 /**********************************************************************
1502 * raise_trap_exception
1504 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1506 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
1508 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
1510 /* when single stepping can't tell whether this is a hw bp or a
1511 * single step interrupt. try to avoid as much overhead as possible
1512 * and only do a server call if there is any hw bp enabled. */
1514 if( !(context
->EFlags
& 0x100) || (regs
->dr7
& 0xff) )
1516 /* (possible) hardware breakpoint, fetch the debug registers */
1517 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1518 NtGetContextThread(GetCurrentThread(), context
);
1519 context
->ContextFlags
|= CONTEXT_FULL
; /* restore flags */
1522 context
->EFlags
&= ~0x100; /* clear single-step flag */
1525 __regs_RtlRaiseException( rec
, context
);
1526 NtSetContextThread( GetCurrentThread(), context
);
1530 /**********************************************************************
1533 * Generic raise function for exceptions that don't need special treatment.
1535 static void WINAPI
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1537 __regs_RtlRaiseException( rec
, context
);
1538 NtSetContextThread( GetCurrentThread(), context
);
1543 /**********************************************************************
1544 * raise_vm86_sti_exception
1546 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1548 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1549 get_vm86_teb_info()->vm86_pending
|= VIP_FLAG
;
1551 if (ntdll_get_thread_data()->vm86_ptr
)
1553 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
1554 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
1555 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
1556 /* exiting from VM86, can't throw */
1559 merge_vm86_pending_flags( rec
);
1561 else if (get_vm86_teb_info()->dpmi_vif
&&
1562 !wine_ldt_is_system(context
->SegCs
) &&
1563 !wine_ldt_is_system(context
->SegSs
))
1565 /* Executing DPMI code and virtual interrupts are enabled. */
1566 get_vm86_teb_info()->vm86_pending
= 0;
1567 __regs_RtlRaiseException( rec
, context
);
1570 NtSetContextThread( GetCurrentThread(), context
);
1574 /**********************************************************************
1577 * Handler for SIGUSR2.
1578 * We use it to signal that the running __wine_enter_vm86() should
1579 * immediately set VIP_FLAG, causing pending events to be handled
1580 * as early as possible.
1582 static void usr2_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1584 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_vm86_sti_exception
);
1585 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
1587 #endif /* __HAVE_VM86 */
1590 /**********************************************************************
1593 * Handler for SIGSEGV and related errors.
1595 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1598 EXCEPTION_RECORD
*rec
;
1599 SIGCONTEXT
*context
= sigcontext
;
1600 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1602 /* check for page fault inside the thread stack */
1603 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
1604 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
1605 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
1606 virtual_handle_stack_fault( siginfo
->si_addr
))
1608 /* check if this was the last guard page */
1609 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
1611 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1612 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1617 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1618 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
1620 switch(get_trap_code(context
))
1622 case TRAP_x86_OFLOW
: /* Overflow exception */
1623 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1625 case TRAP_x86_BOUND
: /* Bound range exception */
1626 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1628 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1629 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1631 case TRAP_x86_STKFLT
: /* Stack fault */
1632 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1634 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1635 case TRAP_x86_PROTFLT
: /* General protection fault */
1636 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1638 WORD err
= get_error_code(context
);
1639 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( get_exception_context(rec
) ))) break;
1640 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1641 rec
->NumberParameters
= 2;
1642 rec
->ExceptionInformation
[0] = 0;
1643 /* if error contains a LDT selector, use that as fault address */
1644 if ((err
& 7) == 4 && !wine_ldt_is_system( err
| 7 ))
1645 rec
->ExceptionInformation
[1] = err
& ~7;
1647 rec
->ExceptionInformation
[1] = 0xffffffff;
1650 case TRAP_x86_PAGEFLT
: /* Page fault */
1651 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1652 rec
->NumberParameters
= 2;
1653 rec
->ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
1654 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
1656 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
1657 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1660 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
1662 case TRAP_x86_NMI
: /* NMI interrupt */
1663 case TRAP_x86_DNA
: /* Device not available exception */
1664 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
1665 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
1666 case TRAP_x86_MCHK
: /* Machine check exception */
1667 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
1668 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1674 /**********************************************************************
1677 * Handler for SIGTRAP.
1679 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1681 SIGCONTEXT
*context
= sigcontext
;
1682 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_trap_exception
);
1684 switch(get_trap_code(context
))
1686 case TRAP_x86_TRCTRAP
: /* Single-step exception */
1687 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1689 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
1690 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
1693 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1699 /**********************************************************************
1702 * Handler for SIGFPE.
1704 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1706 CONTEXT
*win_context
;
1707 SIGCONTEXT
*context
= sigcontext
;
1708 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_exception
);
1710 win_context
= get_exception_context( rec
);
1712 switch(get_trap_code(context
))
1714 case TRAP_x86_DIVIDE
: /* Division by zero exception */
1715 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1717 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
1718 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1720 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
1721 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1722 rec
->ExceptionCode
= get_fpu_code( win_context
);
1723 rec
->ExceptionAddress
= (LPVOID
)win_context
->FloatSave
.ErrorOffset
;
1725 case TRAP_x86_CACHEFLT
: /* SIMD exception */
1727 * Behaviour only tested for divide-by-zero exceptions
1728 * Check for other SIMD exceptions as well */
1729 if(siginfo
->si_code
!= FPE_FLTDIV
)
1730 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1733 rec
->ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
1734 rec
->NumberParameters
= 1;
1735 /* no idea what meaning is actually behind this but that's what native does */
1736 rec
->ExceptionInformation
[0] = 0;
1739 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
1740 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1746 /**********************************************************************
1749 * Handler for SIGINT.
1751 * FIXME: should not be calling external functions on the signal stack.
1753 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1756 init_handler( sigcontext
, &fs
, &gs
);
1757 if (!dispatch_signal(SIGINT
))
1759 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_exception
);
1760 rec
->ExceptionCode
= CONTROL_C_EXIT
;
1764 /**********************************************************************
1767 * Handler for SIGABRT.
1769 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1771 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_exception
);
1772 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
1773 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
1777 /**********************************************************************
1780 * Handler for SIGQUIT.
1782 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1785 init_handler( sigcontext
, &fs
, &gs
);
1790 /**********************************************************************
1793 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1795 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1800 init_handler( sigcontext
, &fs
, &gs
);
1801 save_context( &context
, sigcontext
, fs
, gs
);
1802 wait_suspend( &context
);
1803 restore_context( &context
, sigcontext
);
1807 /**********************************************************************
1808 * get_signal_stack_total_size
1810 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1811 * Must be a power of two.
1813 size_t get_signal_stack_total_size(void)
1815 if (!signal_stack_size
)
1817 size_t size
= 8192, min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
1818 /* find the first power of two not smaller than min_size */
1819 while (size
< min_size
) size
*= 2;
1820 signal_stack_mask
= size
- 1;
1821 signal_stack_size
= size
- teb_size
;
1823 return signal_stack_size
+ teb_size
;
1827 /***********************************************************************
1828 * __wine_set_signal_handler (NTDLL.@)
1830 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1832 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1833 if (handlers
[sig
] != NULL
) return -2;
1834 handlers
[sig
] = wsh
;
1839 /**********************************************************************
1840 * signal_init_thread
1842 void signal_init_thread( TEB
*teb
)
1844 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
1849 int mib
[2], val
= 1;
1852 mib
[1] = KERN_THALTSTACK
;
1853 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
1856 ss
.ss_sp
= (char *)teb
+ teb_size
;
1857 ss
.ss_size
= signal_stack_size
;
1859 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
1861 wine_ldt_set_base( &fs_entry
, teb
);
1862 wine_ldt_set_limit( &fs_entry
, teb_size
- 1 );
1863 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
1864 wine_ldt_init_fs( thread_data
->fs
, &fs_entry
);
1865 thread_data
->gs
= wine_get_gs();
1868 /**********************************************************************
1869 * signal_init_process
1871 void signal_init_process(void)
1873 struct sigaction sig_act
;
1875 sig_act
.sa_mask
= server_block_set
;
1876 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1878 sig_act
.sa_flags
|= SA_ONSTACK
;
1881 sig_act
.sa_sigaction
= int_handler
;
1882 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
1883 sig_act
.sa_sigaction
= fpe_handler
;
1884 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
1885 sig_act
.sa_sigaction
= abrt_handler
;
1886 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
1887 sig_act
.sa_sigaction
= quit_handler
;
1888 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
1889 sig_act
.sa_sigaction
= usr1_handler
;
1890 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
1892 sig_act
.sa_sigaction
= segv_handler
;
1893 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
1894 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
1896 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
1900 sig_act
.sa_sigaction
= trap_handler
;
1901 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
1905 sig_act
.sa_sigaction
= usr2_handler
;
1906 if (sigaction( SIGUSR2
, &sig_act
, NULL
) == -1) goto error
;
1912 perror("sigaction");
1918 /**********************************************************************
1919 * __wine_enter_vm86 (NTDLL.@)
1921 * Enter vm86 mode with the specified register context.
1923 void __wine_enter_vm86( CONTEXT
*context
)
1925 EXCEPTION_RECORD rec
;
1927 struct vm86plus_struct vm86
;
1929 memset( &vm86
, 0, sizeof(vm86
) );
1932 restore_vm86_context( context
, &vm86
);
1934 ntdll_get_thread_data()->vm86_ptr
= &vm86
;
1935 merge_vm86_pending_flags( &rec
);
1937 res
= vm86_enter( &ntdll_get_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
1944 save_vm86_context( context
, &vm86
);
1946 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1947 rec
.ExceptionRecord
= NULL
;
1948 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
1949 rec
.NumberParameters
= 0;
1951 switch(VM86_TYPE(res
))
1953 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
1954 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
1956 case VM86_TRAP
: /* return due to DOS-debugger request */
1957 switch(VM86_ARG(res
))
1959 case TRAP_x86_TRCTRAP
: /* Single-step exception */
1960 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1962 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
1963 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
1966 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
1970 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
1971 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
1972 rec
.NumberParameters
= 1;
1973 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
1975 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
1976 context
->EFlags
|= VIF_FLAG
;
1977 context
->EFlags
&= ~VIP_FLAG
;
1978 get_vm86_teb_info()->vm86_pending
= 0;
1979 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
1981 case VM86_PICRETURN
: /* return due to pending PIC request */
1982 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
1984 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
1986 WINE_ERR( "unhandled result from vm86 mode %x\n", res
);
1989 __regs_RtlRaiseException( &rec
, context
);
1993 #else /* __HAVE_VM86 */
1994 /**********************************************************************
1995 * __wine_enter_vm86 (NTDLL.@)
1997 void __wine_enter_vm86( CONTEXT
*context
)
1999 MESSAGE("vm86 mode not supported on this platform\n");
2001 #endif /* __HAVE_VM86 */
2003 /**********************************************************************
2004 * DbgBreakPoint (NTDLL.@)
2006 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "int $3; ret")
2008 /**********************************************************************
2009 * DbgUserBreakPoint (NTDLL.@)
2011 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "int $3; ret")
2013 /**********************************************************************
2014 * NtCurrentTeb (NTDLL.@)
2016 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2019 /**********************************************************************
2020 * EXC_CallHandler (internal)
2022 * Some exception handlers depend on EBP to have a fixed position relative to
2023 * the exception frame.
2024 * Shrinker depends on (*1) doing what it does,
2025 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2026 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2027 * function calling the handler having only 5 parameters (*4).
2029 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
2031 " movl %esp, %ebp\n"
2033 " movl 28(%ebp), %edx\n" /* ugly hack to pass the 6th param needed because of Shrinker */
2039 " call " __ASM_NAME("call_exception_handler") "\n"
2044 __ASM_GLOBAL_FUNC(call_exception_handler
,
2046 " movl %esp, %ebp\n"
2048 " pushl 12(%ebp)\n" /* make any exceptions in this... */
2049 " pushl %edx\n" /* handler be handled by... */
2051 " pushl (0)\n" /* nested_handler (passed in edx). */
2053 " movl %esp,(0)\n" /* push the new exception frame onto the exception stack. */
2058 " movl 24(%ebp), %ecx\n" /* (*1) */
2059 " call *%ecx\n" /* call handler. (*2) */
2061 " movl (0), %esp\n" /* restore previous... (*3) */
2063 " popl (0)\n" /* exception frame. */
2064 " movl %ebp, %esp\n" /* restore saved stack, in case it was corrupted */
2066 " ret $20\n" /* (*4) */
2068 #endif /* __i386__ */