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 struct ucontext
107 unsigned long uc_flags
;
108 struct ucontext
*uc_link
;
110 struct sigcontext uc_mcontext
;
114 #define EAX_sig(context) ((context)->uc_mcontext.eax)
115 #define EBX_sig(context) ((context)->uc_mcontext.ebx)
116 #define ECX_sig(context) ((context)->uc_mcontext.ecx)
117 #define EDX_sig(context) ((context)->uc_mcontext.edx)
118 #define ESI_sig(context) ((context)->uc_mcontext.esi)
119 #define EDI_sig(context) ((context)->uc_mcontext.edi)
120 #define EBP_sig(context) ((context)->uc_mcontext.ebp)
121 #define ESP_sig(context) ((context)->uc_mcontext.esp)
123 #define CS_sig(context) ((context)->uc_mcontext.cs)
124 #define DS_sig(context) ((context)->uc_mcontext.ds)
125 #define ES_sig(context) ((context)->uc_mcontext.es)
126 #define SS_sig(context) ((context)->uc_mcontext.ss)
127 #define FS_sig(context) ((context)->uc_mcontext.fs)
128 #define GS_sig(context) ((context)->uc_mcontext.gs)
130 #define EFL_sig(context) ((context)->uc_mcontext.eflags)
131 #define EIP_sig(context) ((context)->uc_mcontext.eip)
132 #define TRAP_sig(context) ((context)->uc_mcontext.trapno)
133 #define ERROR_sig(context) ((context)->uc_mcontext.err)
135 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpstate))
136 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpstate->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
138 #elif defined (__linux__)
140 typedef ucontext_t SIGCONTEXT
;
142 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
143 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
144 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
145 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
146 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
147 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
148 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
149 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
151 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
152 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
153 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
154 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
155 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
156 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
158 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
159 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
160 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
161 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
163 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
164 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
166 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
167 #define VIF_FLAG 0x00080000
168 #define VIP_FLAG 0x00100000
170 int vm86_enter( void **vm86_ptr
);
171 void vm86_return(void);
172 void vm86_return_end(void);
173 __ASM_GLOBAL_FUNC(vm86_enter
,
175 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
176 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
178 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
180 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
181 "movl $166,%eax\n\t" /*SYS_vm86*/
182 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
183 "movl (%ecx),%ecx\n\t"
184 "movl $1,%ebx\n\t" /*VM86_ENTER*/
185 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
189 ".globl " __ASM_NAME("vm86_return") "\n\t"
190 __ASM_FUNC("vm86_return") "\n"
191 __ASM_NAME("vm86_return") ":\n\t"
196 __ASM_CFI(".cfi_same_value %ebx\n\t")
198 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
199 __ASM_CFI(".cfi_same_value %ebp\n\t")
200 "testl %eax,%eax\n\t"
202 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
203 "je " __ASM_NAME("vm86_enter") "\n\t"
205 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
207 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
208 __ASM_FUNC("vm86_return_end") "\n"
209 __ASM_NAME("vm86_return_end") ":\n\t"
212 #ifdef HAVE_SYS_VM86_H
216 #elif defined (__BSDI__)
218 #include <machine/frame.h>
219 typedef struct trapframe SIGCONTEXT
;
221 #define EAX_sig(context) ((context)->tf_eax)
222 #define EBX_sig(context) ((context)->tf_ebx)
223 #define ECX_sig(context) ((context)->tf_ecx)
224 #define EDX_sig(context) ((context)->tf_edx)
225 #define ESI_sig(context) ((context)->tf_esi)
226 #define EDI_sig(context) ((context)->tf_edi)
227 #define EBP_sig(context) ((context)->tf_ebp)
229 #define CS_sig(context) ((context)->tf_cs)
230 #define DS_sig(context) ((context)->tf_ds)
231 #define ES_sig(context) ((context)->tf_es)
232 #define SS_sig(context) ((context)->tf_ss)
234 #define EFL_sig(context) ((context)->tf_eflags)
236 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
237 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
239 #define FPU_sig(context) NULL /* FIXME */
240 #define FPUX_sig(context) NULL /* FIXME */
242 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
244 #include <machine/trap.h>
246 typedef struct sigcontext SIGCONTEXT
;
248 #define EAX_sig(context) ((context)->sc_eax)
249 #define EBX_sig(context) ((context)->sc_ebx)
250 #define ECX_sig(context) ((context)->sc_ecx)
251 #define EDX_sig(context) ((context)->sc_edx)
252 #define ESI_sig(context) ((context)->sc_esi)
253 #define EDI_sig(context) ((context)->sc_edi)
254 #define EBP_sig(context) ((context)->sc_ebp)
256 #define CS_sig(context) ((context)->sc_cs)
257 #define DS_sig(context) ((context)->sc_ds)
258 #define ES_sig(context) ((context)->sc_es)
259 #define FS_sig(context) ((context)->sc_fs)
260 #define GS_sig(context) ((context)->sc_gs)
261 #define SS_sig(context) ((context)->sc_ss)
263 #define TRAP_sig(context) ((context)->sc_trapno)
264 #define ERROR_sig(context) ((context)->sc_err)
265 #define EFL_sig(context) ((context)->sc_eflags)
267 #define EIP_sig(context) ((context)->sc_eip)
268 #define ESP_sig(context) ((context)->sc_esp)
270 #define FPU_sig(context) NULL /* FIXME */
271 #define FPUX_sig(context) NULL /* FIXME */
273 #elif defined (__OpenBSD__)
275 typedef struct sigcontext SIGCONTEXT
;
277 #define EAX_sig(context) ((context)->sc_eax)
278 #define EBX_sig(context) ((context)->sc_ebx)
279 #define ECX_sig(context) ((context)->sc_ecx)
280 #define EDX_sig(context) ((context)->sc_edx)
281 #define ESI_sig(context) ((context)->sc_esi)
282 #define EDI_sig(context) ((context)->sc_edi)
283 #define EBP_sig(context) ((context)->sc_ebp)
285 #define CS_sig(context) ((context)->sc_cs)
286 #define DS_sig(context) ((context)->sc_ds)
287 #define ES_sig(context) ((context)->sc_es)
288 #define FS_sig(context) ((context)->sc_fs)
289 #define GS_sig(context) ((context)->sc_gs)
290 #define SS_sig(context) ((context)->sc_ss)
292 #define TRAP_sig(context) ((context)->sc_trapno)
293 #define ERROR_sig(context) ((context)->sc_err)
294 #define EFL_sig(context) ((context)->sc_eflags)
296 #define EIP_sig(context) ((context)->sc_eip)
297 #define ESP_sig(context) ((context)->sc_esp)
299 #define FPU_sig(context) NULL /* FIXME */
300 #define FPUX_sig(context) NULL /* FIXME */
302 #define T_MCHK T_MACHK
303 #define T_XMMFLT T_XFTRAP
305 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
308 #include <sys/regset.h>
310 #include <sys/ucontext.h>
311 typedef struct ucontext SIGCONTEXT
;
317 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
318 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
319 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
320 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
321 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
322 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
323 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
325 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
326 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
327 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
328 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
330 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
331 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
333 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
335 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
337 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
339 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
341 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
344 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
347 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
350 #define FPU_sig(context) NULL /* FIXME */
351 #define FPUX_sig(context) NULL /* FIXME */
353 #elif defined (__APPLE__)
354 # include <sys/ucontext.h>
356 typedef ucontext_t SIGCONTEXT
;
358 /* work around silly renaming of struct members in OS X 10.5 */
359 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
360 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
361 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
362 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
363 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
364 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
365 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
366 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
367 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
368 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
369 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
370 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
371 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
372 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
373 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
374 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
375 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
376 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
377 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
378 #define FPU_sig(context) NULL
379 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
381 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
382 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
383 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
384 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
385 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
386 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
387 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
388 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
389 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
390 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
391 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
392 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
393 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
394 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
395 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
396 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
397 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
398 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
399 #define FPU_sig(context) NULL
400 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
403 #elif defined(__NetBSD__)
404 # include <sys/ucontext.h>
405 # include <sys/types.h>
408 typedef ucontext_t SIGCONTEXT
;
410 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
411 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
412 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
413 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
414 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
415 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
416 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
417 #define ESP_sig(context) _UC_MACHINE_SP(context)
419 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
420 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
421 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
422 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
423 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
424 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
426 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
427 #define EIP_sig(context) _UC_MACHINE_PC(context)
428 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
429 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
431 #define FPU_sig(context) NULL
432 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
435 #define T_XMMFLT T_XMM
438 #error You must define the signal context functions for your platform
441 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
443 typedef int (*wine_signal_handler
)(unsigned int sig
);
445 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
446 static size_t signal_stack_mask
;
447 static size_t signal_stack_size
;
449 static wine_signal_handler handlers
[256];
451 static BOOL fpux_support
; /* whether the CPU supports extended fpu context */
453 extern void DECLSPEC_NORETURN
__wine_restore_regs( const CONTEXT
*context
);
457 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
458 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
459 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
460 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
461 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
462 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
463 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
464 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
465 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
466 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
467 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
468 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
469 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
470 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
471 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
472 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
473 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
474 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
475 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
476 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
477 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
479 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
480 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
481 TRAP_x86_NMI
= 2, /* NMI interrupt */
482 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
483 TRAP_x86_OFLOW
= 4, /* Overflow exception */
484 TRAP_x86_BOUND
= 5, /* Bound range exception */
485 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
486 TRAP_x86_DNA
= 7, /* Device not available exception */
487 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
488 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
489 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
490 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
491 TRAP_x86_STKFLT
= 12, /* Stack fault */
492 TRAP_x86_PROTFLT
= 13, /* General protection fault */
493 TRAP_x86_PAGEFLT
= 14, /* Page fault */
494 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
495 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
496 TRAP_x86_MCHK
= 18, /* Machine check exception */
497 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
498 otherwise Cache flush exception (via SIGSEV) */
502 /* Exception record for handling exceptions happening inside exception handlers */
505 EXCEPTION_REGISTRATION_RECORD frame
;
506 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
509 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
510 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
511 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
513 /***********************************************************************
516 static inline int dispatch_signal(unsigned int sig
)
518 if (handlers
[sig
] == NULL
) return 0;
519 return handlers
[sig
](sig
);
523 /***********************************************************************
526 * Get the trap code for a signal.
528 static inline enum i386_trap_code
get_trap_code( const SIGCONTEXT
*sigcontext
)
531 return TRAP_sig(sigcontext
);
533 return TRAP_x86_UNKNOWN
; /* unknown trap code */
537 /***********************************************************************
540 * Get the error code for a signal.
542 static inline WORD
get_error_code( const SIGCONTEXT
*sigcontext
)
545 return ERROR_sig(sigcontext
);
551 /***********************************************************************
554 * Get the base of the signal stack for the current thread.
556 static inline void *get_signal_stack(void)
558 return (char *)NtCurrentTeb() + 4096;
562 /***********************************************************************
565 * Get the current teb based on the stack pointer.
567 static inline TEB
*get_current_teb(void)
570 __asm__("movl %%esp,%0" : "=g" (esp
) );
571 return (TEB
*)(esp
& ~signal_stack_mask
);
575 /*******************************************************************
578 static inline BOOL
is_valid_frame( void *frame
)
580 if ((ULONG_PTR
)frame
& 3) return FALSE
;
581 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
582 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
585 /*******************************************************************
588 * Handler for exceptions happening inside a handler.
590 static DWORD
raise_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
591 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
593 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
594 return ExceptionContinueSearch
;
595 /* We shouldn't get here so we store faulty frame in dispatcher */
596 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
597 return ExceptionNestedException
;
601 /*******************************************************************
604 * Handler for exceptions happening inside an unwind handler.
606 static DWORD
unwind_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
607 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
609 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
610 return ExceptionContinueSearch
;
611 /* We shouldn't get here so we store faulty frame in dispatcher */
612 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
613 return ExceptionCollidedUnwind
;
617 /**********************************************************************
618 * call_stack_handlers
620 * Call the stack handlers chain.
622 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
624 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
627 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
629 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
631 /* Check frame address */
632 if (!is_valid_frame( frame
))
634 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
639 TRACE( "calling handler at %p code=%x flags=%x\n",
640 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
641 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, raise_handler
);
642 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
644 if (frame
== nested_frame
)
646 /* no longer nested */
648 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
653 case ExceptionContinueExecution
:
654 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
655 return STATUS_NONCONTINUABLE_EXCEPTION
;
656 case ExceptionContinueSearch
:
658 case ExceptionNestedException
:
659 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
660 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
663 return STATUS_INVALID_DISPOSITION
;
667 return STATUS_UNHANDLED_EXCEPTION
;
671 /*******************************************************************
674 * Implementation of NtRaiseException.
676 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
684 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
685 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
686 context
->Eip
, GetCurrentThreadId() );
687 for (c
= 0; c
< rec
->NumberParameters
; c
++)
688 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
689 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
691 if (rec
->ExceptionInformation
[1] >> 16)
692 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
693 rec
->ExceptionAddress
,
694 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
696 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
697 rec
->ExceptionAddress
,
698 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
702 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
703 context
->Eax
, context
->Ebx
, context
->Ecx
,
704 context
->Edx
, context
->Esi
, context
->Edi
);
705 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
706 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
707 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
709 status
= send_debug_event( rec
, TRUE
, context
);
710 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
711 return STATUS_SUCCESS
;
713 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
714 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
716 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
717 return STATUS_SUCCESS
;
719 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
723 /* last chance exception */
725 status
= send_debug_event( rec
, FALSE
, context
);
726 if (status
!= DBG_CONTINUE
)
728 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
729 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
730 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
731 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
733 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
734 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
735 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
737 return STATUS_SUCCESS
;
742 /***********************************************************************
745 * Set the register values from a vm86 structure.
747 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
749 context
->ContextFlags
= CONTEXT_FULL
;
750 context
->Eax
= vm86
->regs
.eax
;
751 context
->Ebx
= vm86
->regs
.ebx
;
752 context
->Ecx
= vm86
->regs
.ecx
;
753 context
->Edx
= vm86
->regs
.edx
;
754 context
->Esi
= vm86
->regs
.esi
;
755 context
->Edi
= vm86
->regs
.edi
;
756 context
->Esp
= vm86
->regs
.esp
;
757 context
->Ebp
= vm86
->regs
.ebp
;
758 context
->Eip
= vm86
->regs
.eip
;
759 context
->SegCs
= vm86
->regs
.cs
;
760 context
->SegDs
= vm86
->regs
.ds
;
761 context
->SegEs
= vm86
->regs
.es
;
762 context
->SegFs
= vm86
->regs
.fs
;
763 context
->SegGs
= vm86
->regs
.gs
;
764 context
->SegSs
= vm86
->regs
.ss
;
765 context
->EFlags
= vm86
->regs
.eflags
;
769 /***********************************************************************
770 * restore_vm86_context
772 * Build a vm86 structure from the register values.
774 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
776 vm86
->regs
.eax
= context
->Eax
;
777 vm86
->regs
.ebx
= context
->Ebx
;
778 vm86
->regs
.ecx
= context
->Ecx
;
779 vm86
->regs
.edx
= context
->Edx
;
780 vm86
->regs
.esi
= context
->Esi
;
781 vm86
->regs
.edi
= context
->Edi
;
782 vm86
->regs
.esp
= context
->Esp
;
783 vm86
->regs
.ebp
= context
->Ebp
;
784 vm86
->regs
.eip
= context
->Eip
;
785 vm86
->regs
.cs
= context
->SegCs
;
786 vm86
->regs
.ds
= context
->SegDs
;
787 vm86
->regs
.es
= context
->SegEs
;
788 vm86
->regs
.fs
= context
->SegFs
;
789 vm86
->regs
.gs
= context
->SegGs
;
790 vm86
->regs
.ss
= context
->SegSs
;
791 vm86
->regs
.eflags
= context
->EFlags
;
795 /**********************************************************************
796 * merge_vm86_pending_flags
798 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
799 * raises exception if there are pending events and VIF flag
800 * has been turned on.
802 * Called from __wine_enter_vm86 because vm86_enter
803 * doesn't check for pending events.
805 * Called from raise_vm86_sti_exception to check for
806 * pending events in a signal safe way.
808 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
810 BOOL check_pending
= TRUE
;
811 struct vm86plus_struct
*vm86
=
812 (struct vm86plus_struct
*)(ntdll_get_thread_data()->vm86_ptr
);
815 * In order to prevent a race when SIGUSR2 occurs while
816 * we are returning from exception handler, pending events
817 * will be rechecked after each raised exception.
819 while (check_pending
&& get_vm86_teb_info()->vm86_pending
)
821 check_pending
= FALSE
;
822 ntdll_get_thread_data()->vm86_ptr
= NULL
;
825 * If VIF is set, throw exception.
826 * Note that SIGUSR2 may turn VIF flag off so
827 * VIF check must occur only when TEB.vm86_ptr is NULL.
829 if (vm86
->regs
.eflags
& VIF_FLAG
)
832 save_vm86_context( &vcontext
, vm86
);
834 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
835 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
836 rec
->ExceptionRecord
= NULL
;
837 rec
->NumberParameters
= 0;
838 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
840 vcontext
.EFlags
&= ~VIP_FLAG
;
841 get_vm86_teb_info()->vm86_pending
= 0;
842 raise_exception( rec
, &vcontext
, TRUE
);
844 restore_vm86_context( &vcontext
, vm86
);
845 check_pending
= TRUE
;
848 ntdll_get_thread_data()->vm86_ptr
= vm86
;
852 * Merge VIP flags in a signal safe way. This requires
853 * that the following operation compiles into atomic
856 vm86
->regs
.eflags
|= get_vm86_teb_info()->vm86_pending
;
858 #endif /* __HAVE_VM86 */
863 /* We have to workaround two Solaris breakages:
864 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
866 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
867 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
870 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
871 __ASM_GLOBAL_FUNC( sigaction_syscall
,
872 "movl $0x62,%eax\n\t"
876 /* assume the same libc handler is used for all signals */
877 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
879 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
881 struct ntdll_thread_data
*thread_data
;
883 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
885 thread_data
= (struct ntdll_thread_data
*)get_current_teb()->SpareBytes1
;
886 wine_set_fs( thread_data
->fs
);
887 wine_set_gs( thread_data
->gs
);
889 libc_sigacthandler( signal
, siginfo
, sigcontext
);
892 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
894 struct sigaction real_act
;
896 if (sigaction( sig
, new, old
) == -1) return -1;
898 /* retrieve the real handler and flags with a direct syscall */
899 sigaction_syscall( sig
, NULL
, &real_act
);
900 libc_sigacthandler
= real_act
.sa_sigaction
;
901 real_act
.sa_sigaction
= wine_sigacthandler
;
902 sigaction_syscall( sig
, &real_act
, NULL
);
905 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
909 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
911 extern void clear_alignment_flag(void);
912 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
914 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
915 "andl $~0x40000,(%esp)\n\t"
917 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
921 /***********************************************************************
924 * Handler initialization when the full context is not needed.
925 * Return the stack pointer to use for pushing the exception data.
927 static inline void *init_handler( const SIGCONTEXT
*sigcontext
, WORD
*fs
, WORD
*gs
)
929 TEB
*teb
= get_current_teb();
931 clear_alignment_flag();
933 /* get %fs and %gs at time of the fault */
935 *fs
= LOWORD(FS_sig(sigcontext
));
940 *gs
= LOWORD(GS_sig(sigcontext
));
945 #ifndef __sun /* see above for Solaris handling */
947 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
948 wine_set_fs( thread_data
->fs
);
949 wine_set_gs( thread_data
->gs
);
953 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
954 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
957 * Win16 or DOS protected mode. Note that during switch
958 * from 16-bit mode to linear mode, CS may be set to system
959 * segment before FS is restored. Fortunately, in this case
960 * SS is still non-system segment. This is why both CS and SS
963 return teb
->WOW32Reserved
;
965 return (void *)(ESP_sig(sigcontext
) & ~3);
969 /***********************************************************************
972 * Save the thread FPU context.
974 static inline void save_fpu( CONTEXT
*context
)
977 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
978 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
983 /***********************************************************************
986 * Save the thread FPU extended context.
988 static inline void save_fpux( CONTEXT
*context
)
991 /* we have to enforce alignment by hand */
992 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
993 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
995 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
996 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
997 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
1002 /***********************************************************************
1005 * Restore the FPU context to a sigcontext.
1007 static inline void restore_fpu( const CONTEXT
*context
)
1009 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
1010 /* reset the current interrupt status */
1011 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
1013 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
1014 #endif /* __GNUC__ */
1018 /***********************************************************************
1021 * Restore the FPU extended context to a sigcontext.
1023 static inline void restore_fpux( const CONTEXT
*context
)
1026 /* we have to enforce alignment by hand */
1027 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1028 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1030 memcpy( state
, context
->ExtendedRegisters
, sizeof(*state
) );
1031 /* reset the current interrupt status */
1032 state
->StatusWord
&= state
->ControlWord
| 0xff80;
1033 __asm__
__volatile__( "fxrstor %0" : : "m" (*state
) );
1038 /***********************************************************************
1041 * Build a standard FPU context from an extended one.
1043 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XMM_SAVE_AREA32
*fpux
)
1045 unsigned int i
, tag
, stack_top
;
1047 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
1048 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
1049 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
1050 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
1051 fpu
->DataOffset
= fpux
->DataOffset
;
1052 fpu
->DataSelector
= fpux
->DataSelector
;
1053 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
1055 stack_top
= (fpux
->StatusWord
>> 11) & 7;
1056 fpu
->TagWord
= 0xffff0000;
1057 for (i
= 0; i
< 8; i
++)
1059 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
1060 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
1063 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
1064 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
1066 tag
= 2; /* special */
1068 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
1070 if (reg
->Low
) tag
= 2; /* special */
1071 else tag
= 1; /* zero */
1075 if (reg
->Low
>> 63) tag
= 0; /* valid */
1076 else tag
= 2; /* special */
1079 fpu
->TagWord
|= tag
<< (2 * i
);
1084 /***********************************************************************
1087 * Build a context structure from the signal info.
1089 static inline void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
, WORD fs
, WORD gs
)
1091 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1092 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1093 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1095 memset(context
, 0, sizeof(*context
));
1096 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
1097 context
->Eax
= EAX_sig(sigcontext
);
1098 context
->Ebx
= EBX_sig(sigcontext
);
1099 context
->Ecx
= ECX_sig(sigcontext
);
1100 context
->Edx
= EDX_sig(sigcontext
);
1101 context
->Esi
= ESI_sig(sigcontext
);
1102 context
->Edi
= EDI_sig(sigcontext
);
1103 context
->Ebp
= EBP_sig(sigcontext
);
1104 context
->EFlags
= EFL_sig(sigcontext
);
1105 context
->Eip
= EIP_sig(sigcontext
);
1106 context
->Esp
= ESP_sig(sigcontext
);
1107 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
1108 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
1109 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
1110 context
->SegFs
= fs
;
1111 context
->SegGs
= gs
;
1112 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
1113 context
->Dr0
= regs
->dr0
;
1114 context
->Dr1
= regs
->dr1
;
1115 context
->Dr2
= regs
->dr2
;
1116 context
->Dr3
= regs
->dr3
;
1117 context
->Dr6
= regs
->dr6
;
1118 context
->Dr7
= regs
->dr7
;
1122 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1123 context
->FloatSave
= *fpu
;
1127 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
1128 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
1129 fpux_support
= TRUE
;
1130 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
1132 if (!fpu
&& !fpux
) save_fpu( context
);
1136 /***********************************************************************
1139 * Restore the signal info from the context.
1141 static inline void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
1143 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1144 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1145 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1147 regs
->dr0
= context
->Dr0
;
1148 regs
->dr1
= context
->Dr1
;
1149 regs
->dr2
= context
->Dr2
;
1150 regs
->dr3
= context
->Dr3
;
1151 regs
->dr6
= context
->Dr6
;
1152 regs
->dr7
= context
->Dr7
;
1153 EAX_sig(sigcontext
) = context
->Eax
;
1154 EBX_sig(sigcontext
) = context
->Ebx
;
1155 ECX_sig(sigcontext
) = context
->Ecx
;
1156 EDX_sig(sigcontext
) = context
->Edx
;
1157 ESI_sig(sigcontext
) = context
->Esi
;
1158 EDI_sig(sigcontext
) = context
->Edi
;
1159 EBP_sig(sigcontext
) = context
->Ebp
;
1160 EFL_sig(sigcontext
) = context
->EFlags
;
1161 EIP_sig(sigcontext
) = context
->Eip
;
1162 ESP_sig(sigcontext
) = context
->Esp
;
1163 CS_sig(sigcontext
) = context
->SegCs
;
1164 DS_sig(sigcontext
) = context
->SegDs
;
1165 ES_sig(sigcontext
) = context
->SegEs
;
1166 SS_sig(sigcontext
) = context
->SegSs
;
1168 GS_sig(sigcontext
) = context
->SegGs
;
1170 wine_set_gs( context
->SegGs
);
1173 FS_sig(sigcontext
) = context
->SegFs
;
1175 wine_set_fs( context
->SegFs
);
1178 if (fpu
) *fpu
= context
->FloatSave
;
1179 if (fpux
) memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
1180 if (!fpu
&& !fpux
) restore_fpu( context
);
1184 /***********************************************************************
1185 * RtlCaptureContext (NTDLL.@)
1187 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
1189 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1190 "movl 8(%esp),%eax\n\t" /* context */
1191 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1192 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1193 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1194 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1195 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1196 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1197 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1198 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1199 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1200 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1201 "movl %ebp,0xb4(%eax)\n\t" /* context->Ebp */
1202 "movl 4(%esp),%edx\n\t"
1203 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1204 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1206 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1207 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1208 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1209 "leal 8(%esp),%edx\n\t"
1210 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1211 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1212 "popl 0xb0(%eax)\n\t" /* context->Eax */
1213 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1217 /***********************************************************************
1220 * Set the new CPU context. Used by NtSetContextThread.
1222 void set_cpu_context( const CONTEXT
*context
)
1224 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1226 if ((flags
& CONTEXT_EXTENDED_REGISTERS
) && fpux_support
) restore_fpux( context
);
1227 else if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
1229 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1231 ntdll_get_thread_data()->dr0
= context
->Dr0
;
1232 ntdll_get_thread_data()->dr1
= context
->Dr1
;
1233 ntdll_get_thread_data()->dr2
= context
->Dr2
;
1234 ntdll_get_thread_data()->dr3
= context
->Dr3
;
1235 ntdll_get_thread_data()->dr6
= context
->Dr6
;
1236 ntdll_get_thread_data()->dr7
= context
->Dr7
;
1238 if (flags
& CONTEXT_FULL
)
1240 if (!(flags
& CONTEXT_CONTROL
))
1241 FIXME( "setting partial context (%x) not supported\n", flags
);
1242 else if (flags
& CONTEXT_SEGMENTS
)
1243 __wine_restore_regs( context
);
1246 CONTEXT newcontext
= *context
;
1247 newcontext
.SegDs
= wine_get_ds();
1248 newcontext
.SegEs
= wine_get_es();
1249 newcontext
.SegFs
= wine_get_fs();
1250 newcontext
.SegGs
= wine_get_gs();
1251 __wine_restore_regs( &newcontext
);
1257 /***********************************************************************
1258 * set_debug_registers
1260 static void set_debug_registers( const CONTEXT
*context
)
1262 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1263 context_t server_context
;
1265 if (!(flags
& CONTEXT_DEBUG_REGISTERS
)) return;
1266 if (ntdll_get_thread_data()->dr0
== context
->Dr0
&&
1267 ntdll_get_thread_data()->dr1
== context
->Dr1
&&
1268 ntdll_get_thread_data()->dr2
== context
->Dr2
&&
1269 ntdll_get_thread_data()->dr3
== context
->Dr3
&&
1270 ntdll_get_thread_data()->dr6
== context
->Dr6
&&
1271 ntdll_get_thread_data()->dr7
== context
->Dr7
) return;
1273 context_to_server( &server_context
, context
);
1275 SERVER_START_REQ( set_thread_context
)
1277 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
1279 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
1280 wine_server_call( req
);
1286 /***********************************************************************
1289 * Copy a register context according to the flags.
1291 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1293 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
1294 if (flags
& CONTEXT_INTEGER
)
1296 to
->Eax
= from
->Eax
;
1297 to
->Ebx
= from
->Ebx
;
1298 to
->Ecx
= from
->Ecx
;
1299 to
->Edx
= from
->Edx
;
1300 to
->Esi
= from
->Esi
;
1301 to
->Edi
= from
->Edi
;
1303 if (flags
& CONTEXT_CONTROL
)
1305 to
->Ebp
= from
->Ebp
;
1306 to
->Esp
= from
->Esp
;
1307 to
->Eip
= from
->Eip
;
1308 to
->SegCs
= from
->SegCs
;
1309 to
->SegSs
= from
->SegSs
;
1310 to
->EFlags
= from
->EFlags
;
1312 if (flags
& CONTEXT_SEGMENTS
)
1314 to
->SegDs
= from
->SegDs
;
1315 to
->SegEs
= from
->SegEs
;
1316 to
->SegFs
= from
->SegFs
;
1317 to
->SegGs
= from
->SegGs
;
1319 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1321 to
->Dr0
= from
->Dr0
;
1322 to
->Dr1
= from
->Dr1
;
1323 to
->Dr2
= from
->Dr2
;
1324 to
->Dr3
= from
->Dr3
;
1325 to
->Dr6
= from
->Dr6
;
1326 to
->Dr7
= from
->Dr7
;
1328 if (flags
& CONTEXT_FLOATING_POINT
)
1330 to
->FloatSave
= from
->FloatSave
;
1332 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1334 memcpy( to
->ExtendedRegisters
, from
->ExtendedRegisters
, sizeof(to
->ExtendedRegisters
) );
1339 /***********************************************************************
1342 * Convert a register context to the server format.
1344 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1346 DWORD flags
= from
->ContextFlags
& ~CONTEXT_i386
; /* get rid of CPU id */
1348 memset( to
, 0, sizeof(*to
) );
1351 if (flags
& CONTEXT_CONTROL
)
1353 to
->flags
|= SERVER_CTX_CONTROL
;
1354 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
1355 to
->ctl
.i386_regs
.esp
= from
->Esp
;
1356 to
->ctl
.i386_regs
.eip
= from
->Eip
;
1357 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
1358 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
1359 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
1361 if (flags
& CONTEXT_INTEGER
)
1363 to
->flags
|= SERVER_CTX_INTEGER
;
1364 to
->integer
.i386_regs
.eax
= from
->Eax
;
1365 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
1366 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
1367 to
->integer
.i386_regs
.edx
= from
->Edx
;
1368 to
->integer
.i386_regs
.esi
= from
->Esi
;
1369 to
->integer
.i386_regs
.edi
= from
->Edi
;
1371 if (flags
& CONTEXT_SEGMENTS
)
1373 to
->flags
|= SERVER_CTX_SEGMENTS
;
1374 to
->seg
.i386_regs
.ds
= from
->SegDs
;
1375 to
->seg
.i386_regs
.es
= from
->SegEs
;
1376 to
->seg
.i386_regs
.fs
= from
->SegFs
;
1377 to
->seg
.i386_regs
.gs
= from
->SegGs
;
1379 if (flags
& CONTEXT_FLOATING_POINT
)
1381 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1382 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
1383 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
1384 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
1385 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
1386 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
1387 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
1388 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
1389 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
1390 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
1392 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1394 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1395 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
1396 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
1397 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
1398 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
1399 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
1400 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
1402 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1404 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
1405 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
1407 return STATUS_SUCCESS
;
1411 /***********************************************************************
1412 * context_from_server
1414 * Convert a register context from the server format.
1416 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1418 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
1420 to
->ContextFlags
= CONTEXT_i386
;
1421 if (from
->flags
& SERVER_CTX_CONTROL
)
1423 to
->ContextFlags
|= CONTEXT_CONTROL
;
1424 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
1425 to
->Esp
= from
->ctl
.i386_regs
.esp
;
1426 to
->Eip
= from
->ctl
.i386_regs
.eip
;
1427 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
1428 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
1429 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
1431 if (from
->flags
& SERVER_CTX_INTEGER
)
1433 to
->ContextFlags
|= CONTEXT_INTEGER
;
1434 to
->Eax
= from
->integer
.i386_regs
.eax
;
1435 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
1436 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
1437 to
->Edx
= from
->integer
.i386_regs
.edx
;
1438 to
->Esi
= from
->integer
.i386_regs
.esi
;
1439 to
->Edi
= from
->integer
.i386_regs
.edi
;
1441 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1443 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1444 to
->SegDs
= from
->seg
.i386_regs
.ds
;
1445 to
->SegEs
= from
->seg
.i386_regs
.es
;
1446 to
->SegFs
= from
->seg
.i386_regs
.fs
;
1447 to
->SegGs
= from
->seg
.i386_regs
.gs
;
1449 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1451 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1452 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
1453 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
1454 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
1455 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
1456 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
1457 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
1458 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
1459 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
1460 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
1462 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1464 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1465 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
1466 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
1467 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
1468 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
1469 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
1470 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
1472 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
1474 to
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1475 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
1477 return STATUS_SUCCESS
;
1481 /***********************************************************************
1482 * is_privileged_instr
1484 * Check if the fault location is a privileged instruction.
1485 * Based on the instruction emulation code in dlls/kernel/instr.c.
1487 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1490 unsigned int prefix_count
= 0;
1492 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
1493 instr
= (BYTE
*)context
->Eip
;
1495 for (;;) switch(*instr
)
1497 /* instruction prefixes */
1498 case 0x2e: /* %cs: */
1499 case 0x36: /* %ss: */
1500 case 0x3e: /* %ds: */
1501 case 0x26: /* %es: */
1502 case 0x64: /* %fs: */
1503 case 0x65: /* %gs: */
1504 case 0x66: /* opcode size */
1505 case 0x67: /* addr size */
1506 case 0xf0: /* lock */
1507 case 0xf2: /* repne */
1508 case 0xf3: /* repe */
1509 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1513 case 0x0f: /* extended instruction */
1516 case 0x20: /* mov crX, reg */
1517 case 0x21: /* mov drX, reg */
1518 case 0x22: /* mov reg, crX */
1519 case 0x23: /* mov reg drX */
1520 return EXCEPTION_PRIV_INSTRUCTION
;
1523 case 0x6c: /* insb (%dx) */
1524 case 0x6d: /* insl (%dx) */
1525 case 0x6e: /* outsb (%dx) */
1526 case 0x6f: /* outsl (%dx) */
1527 case 0xcd: /* int $xx */
1528 case 0xe4: /* inb al,XX */
1529 case 0xe5: /* in (e)ax,XX */
1530 case 0xe6: /* outb XX,al */
1531 case 0xe7: /* out XX,(e)ax */
1532 case 0xec: /* inb (%dx),%al */
1533 case 0xed: /* inl (%dx),%eax */
1534 case 0xee: /* outb %al,(%dx) */
1535 case 0xef: /* outl %eax,(%dx) */
1536 case 0xf4: /* hlt */
1537 case 0xfa: /* cli */
1538 case 0xfb: /* sti */
1539 return EXCEPTION_PRIV_INSTRUCTION
;
1545 /***********************************************************************
1548 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1550 static inline BOOL
check_invalid_gs( CONTEXT
*context
)
1552 unsigned int prefix_count
= 0;
1553 const BYTE
*instr
= (BYTE
*)context
->Eip
;
1554 WORD system_gs
= ntdll_get_thread_data()->gs
;
1556 if (context
->SegGs
== system_gs
) return FALSE
;
1557 if (!wine_ldt_is_system( context
->SegCs
)) return FALSE
;
1558 /* only handle faults in system libraries */
1559 if (virtual_is_valid_code_address( instr
, 1 )) return FALSE
;
1561 for (;;) switch(*instr
)
1563 /* instruction prefixes */
1564 case 0x2e: /* %cs: */
1565 case 0x36: /* %ss: */
1566 case 0x3e: /* %ds: */
1567 case 0x26: /* %es: */
1568 case 0x64: /* %fs: */
1569 case 0x66: /* opcode size */
1570 case 0x67: /* addr size */
1571 case 0xf0: /* lock */
1572 case 0xf2: /* repne */
1573 case 0xf3: /* repe */
1574 if (++prefix_count
>= 15) return FALSE
;
1577 case 0x65: /* %gs: */
1578 TRACE( "%04x/%04x at %p, fixing up\n", context
->SegGs
, system_gs
, instr
);
1579 context
->SegGs
= system_gs
;
1587 #include "pshpack1.h"
1590 DWORD movl
; /* movl this,4(%esp) */
1592 BYTE jmp
; /* jmp func */
1595 #include "poppack.h"
1597 /**********************************************************************
1600 * Check if code destination is an ATL thunk, and emulate it if so.
1602 static BOOL
check_atl_thunk( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1604 const struct atl_thunk
*thunk
= (const struct atl_thunk
*)rec
->ExceptionInformation
[1];
1607 if (!virtual_is_valid_code_address( thunk
, sizeof(*thunk
) )) return FALSE
;
1611 if (thunk
->movl
== 0x042444c7 && thunk
->jmp
== 0xe9)
1613 *((DWORD
*)context
->Esp
+ 1) = thunk
->this;
1614 context
->Eip
= (DWORD_PTR
)(&thunk
->func
+ 1) + thunk
->func
;
1615 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1616 thunk
, context
->Eip
, *((DWORD
*)context
->Esp
+ 1) );
1629 /***********************************************************************
1630 * setup_exception_record
1632 * Setup the exception record and context on the thread stack.
1634 static EXCEPTION_RECORD
*setup_exception_record( SIGCONTEXT
*sigcontext
, void *stack_ptr
,
1635 WORD fs
, WORD gs
, raise_func func
)
1639 void *ret_addr
; /* return address from raise_func */
1640 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
1641 CONTEXT
*context_ptr
; /* second arg for raise_func */
1643 EXCEPTION_RECORD rec
;
1646 } *stack
= stack_ptr
;
1647 DWORD exception_code
= 0;
1649 /* stack sanity checks */
1651 if ((char *)stack
>= (char *)get_signal_stack() &&
1652 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1654 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1655 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1656 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1657 NtCurrentTeb()->Tib
.StackBase
);
1661 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1662 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1663 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1665 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1666 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1667 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1668 NtCurrentTeb()->Tib
.StackBase
);
1670 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1672 /* stack overflow on last page, unrecoverable */
1673 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1674 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1675 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1676 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1677 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1680 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1682 /* stack access below stack limit, may be recoverable */
1683 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1686 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1687 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1688 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1689 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1690 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1695 stack
--; /* push the stack_layout structure */
1696 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1697 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1698 #elif defined(VALGRIND_MAKE_WRITABLE)
1699 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1701 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
1702 stack
->rec_ptr
= &stack
->rec
;
1703 stack
->context_ptr
= &stack
->context
;
1705 stack
->rec
.ExceptionRecord
= NULL
;
1706 stack
->rec
.ExceptionCode
= exception_code
;
1707 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1708 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
1709 stack
->rec
.NumberParameters
= 0;
1711 save_context( &stack
->context
, sigcontext
, fs
, gs
);
1713 /* now modify the sigcontext to return to the raise function */
1714 ESP_sig(sigcontext
) = (DWORD
)stack
;
1715 EIP_sig(sigcontext
) = (DWORD
)func
;
1716 /* clear single-step, direction, and align check flag */
1717 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1718 CS_sig(sigcontext
) = wine_get_cs();
1719 DS_sig(sigcontext
) = wine_get_ds();
1720 ES_sig(sigcontext
) = wine_get_es();
1721 FS_sig(sigcontext
) = wine_get_fs();
1722 GS_sig(sigcontext
) = wine_get_gs();
1723 SS_sig(sigcontext
) = wine_get_ss();
1725 return stack
->rec_ptr
;
1729 /***********************************************************************
1732 * Setup a proper stack frame for the raise function, and modify the
1733 * sigcontext so that the return from the signal handler will call
1734 * the raise function.
1736 static EXCEPTION_RECORD
*setup_exception( SIGCONTEXT
*sigcontext
, raise_func func
)
1739 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1741 return setup_exception_record( sigcontext
, stack
, fs
, gs
, func
);
1745 /***********************************************************************
1746 * get_exception_context
1748 * Get a pointer to the context built by setup_exception.
1750 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
1752 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
1756 /**********************************************************************
1759 * Get the FPU exception code from the FPU status.
1761 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1763 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1765 if (status
& 0x01) /* IE */
1767 if (status
& 0x40) /* SF */
1768 return EXCEPTION_FLT_STACK_CHECK
;
1770 return EXCEPTION_FLT_INVALID_OPERATION
;
1772 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1773 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1774 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1775 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1776 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1777 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1781 /**********************************************************************
1782 * raise_segv_exception
1784 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1788 switch(rec
->ExceptionCode
)
1790 case EXCEPTION_ACCESS_VIOLATION
:
1791 if (rec
->NumberParameters
== 2)
1793 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
&& check_atl_thunk( rec
, context
))
1795 if (rec
->ExceptionInformation
[1] == 0xffffffff && check_invalid_gs( context
))
1797 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
1798 rec
->ExceptionInformation
[0] )))
1800 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1801 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
1804 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
1805 &flags
, sizeof(flags
), NULL
);
1806 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
))
1807 rec
->ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
1811 case EXCEPTION_DATATYPE_MISALIGNMENT
:
1812 /* FIXME: pass through exception handler first? */
1813 if (context
->EFlags
& 0x00040000)
1815 /* Disable AC flag, return */
1816 context
->EFlags
&= ~0x00040000;
1821 status
= NtRaiseException( rec
, context
, TRUE
);
1822 raise_status( status
, rec
);
1824 set_cpu_context( context
);
1828 /**********************************************************************
1829 * raise_trap_exception
1831 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1835 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
1837 /* when single stepping can't tell whether this is a hw bp or a
1838 * single step interrupt. try to avoid as much overhead as possible
1839 * and only do a server call if there is any hw bp enabled. */
1841 if( !(context
->EFlags
& 0x100) || (ntdll_get_thread_data()->dr7
& 0xff) )
1843 /* (possible) hardware breakpoint, fetch the debug registers */
1844 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1845 NtGetContextThread(GetCurrentThread(), context
);
1846 context
->ContextFlags
|= CONTEXT_FULL
; /* restore flags */
1849 context
->EFlags
&= ~0x100; /* clear single-step flag */
1852 status
= NtRaiseException( rec
, context
, TRUE
);
1853 raise_status( status
, rec
);
1857 /**********************************************************************
1858 * raise_generic_exception
1860 * Generic raise function for exceptions that don't need special treatment.
1862 static void WINAPI
raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1866 status
= NtRaiseException( rec
, context
, TRUE
);
1867 raise_status( status
, rec
);
1872 /**********************************************************************
1873 * raise_vm86_sti_exception
1875 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1877 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1878 get_vm86_teb_info()->vm86_pending
|= VIP_FLAG
;
1880 if (ntdll_get_thread_data()->vm86_ptr
)
1882 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
1883 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
1884 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
1885 /* exiting from VM86, can't throw */
1888 merge_vm86_pending_flags( rec
);
1890 else if (get_vm86_teb_info()->dpmi_vif
&&
1891 !wine_ldt_is_system(context
->SegCs
) &&
1892 !wine_ldt_is_system(context
->SegSs
))
1894 /* Executing DPMI code and virtual interrupts are enabled. */
1895 get_vm86_teb_info()->vm86_pending
= 0;
1896 NtRaiseException( rec
, context
, TRUE
);
1899 set_cpu_context( context
);
1903 /**********************************************************************
1906 * Handler for SIGUSR2.
1907 * We use it to signal that the running __wine_enter_vm86() should
1908 * immediately set VIP_FLAG, causing pending events to be handled
1909 * as early as possible.
1911 static void usr2_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1913 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_vm86_sti_exception
);
1914 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
1916 #endif /* __HAVE_VM86 */
1919 /**********************************************************************
1922 * Handler for SIGSEGV and related errors.
1924 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1927 EXCEPTION_RECORD
*rec
;
1928 SIGCONTEXT
*context
= sigcontext
;
1929 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1931 /* check for page fault inside the thread stack */
1932 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
1933 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
1934 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
1935 virtual_handle_stack_fault( siginfo
->si_addr
))
1937 /* check if this was the last guard page */
1938 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
1940 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1941 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1946 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1947 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
1949 switch(get_trap_code(context
))
1951 case TRAP_x86_OFLOW
: /* Overflow exception */
1952 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1954 case TRAP_x86_BOUND
: /* Bound range exception */
1955 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1957 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1958 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1960 case TRAP_x86_STKFLT
: /* Stack fault */
1961 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1963 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1964 case TRAP_x86_PROTFLT
: /* General protection fault */
1965 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1967 WORD err
= get_error_code(context
);
1968 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( get_exception_context(rec
) ))) break;
1969 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1970 rec
->NumberParameters
= 2;
1971 rec
->ExceptionInformation
[0] = 0;
1972 /* if error contains a LDT selector, use that as fault address */
1973 if ((err
& 7) == 4 && !wine_ldt_is_system( err
| 7 ))
1974 rec
->ExceptionInformation
[1] = err
& ~7;
1976 rec
->ExceptionInformation
[1] = 0xffffffff;
1979 case TRAP_x86_PAGEFLT
: /* Page fault */
1980 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1981 rec
->NumberParameters
= 2;
1982 rec
->ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
1983 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
1985 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
1986 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1989 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
1991 case TRAP_x86_NMI
: /* NMI interrupt */
1992 case TRAP_x86_DNA
: /* Device not available exception */
1993 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
1994 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
1995 case TRAP_x86_MCHK
: /* Machine check exception */
1996 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
1997 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2003 /**********************************************************************
2006 * Handler for SIGTRAP.
2008 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2010 SIGCONTEXT
*context
= sigcontext
;
2011 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_trap_exception
);
2013 switch(get_trap_code(context
))
2015 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2016 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2018 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2019 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
2022 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
2028 /**********************************************************************
2031 * Handler for SIGFPE.
2033 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2035 CONTEXT
*win_context
;
2036 SIGCONTEXT
*context
= sigcontext
;
2037 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_generic_exception
);
2039 win_context
= get_exception_context( rec
);
2041 switch(get_trap_code(context
))
2043 case TRAP_x86_DIVIDE
: /* Division by zero exception */
2044 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
2046 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
2047 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2049 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
2050 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2051 rec
->ExceptionCode
= get_fpu_code( win_context
);
2052 rec
->ExceptionAddress
= (LPVOID
)win_context
->FloatSave
.ErrorOffset
;
2054 case TRAP_x86_CACHEFLT
: /* SIMD exception */
2056 * Behaviour only tested for divide-by-zero exceptions
2057 * Check for other SIMD exceptions as well */
2058 if(siginfo
->si_code
!= FPE_FLTDIV
)
2059 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2062 rec
->ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
2063 rec
->NumberParameters
= 1;
2064 /* no idea what meaning is actually behind this but that's what native does */
2065 rec
->ExceptionInformation
[0] = 0;
2068 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2069 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2075 /**********************************************************************
2078 * Handler for SIGINT.
2080 * FIXME: should not be calling external functions on the signal stack.
2082 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2085 init_handler( sigcontext
, &fs
, &gs
);
2086 if (!dispatch_signal(SIGINT
))
2088 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2089 rec
->ExceptionCode
= CONTROL_C_EXIT
;
2093 /**********************************************************************
2096 * Handler for SIGABRT.
2098 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2100 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2101 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2102 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
2106 /**********************************************************************
2109 * Handler for SIGQUIT.
2111 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2114 init_handler( sigcontext
, &fs
, &gs
);
2119 /**********************************************************************
2122 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2124 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2129 init_handler( sigcontext
, &fs
, &gs
);
2130 save_context( &context
, sigcontext
, fs
, gs
);
2131 wait_suspend( &context
);
2132 restore_context( &context
, sigcontext
);
2136 /***********************************************************************
2137 * __wine_set_signal_handler (NTDLL.@)
2139 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2141 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
2142 if (handlers
[sig
] != NULL
) return -2;
2143 handlers
[sig
] = wsh
;
2148 /***********************************************************************
2149 * locking for LDT routines
2151 static RTL_CRITICAL_SECTION ldt_section
;
2152 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
2155 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
2156 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
2158 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
2159 static sigset_t ldt_sigset
;
2161 static void ldt_lock(void)
2165 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
2166 RtlEnterCriticalSection( &ldt_section
);
2167 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
2170 static void ldt_unlock(void)
2172 if (ldt_section
.RecursionCount
== 1)
2174 sigset_t sigset
= ldt_sigset
;
2175 RtlLeaveCriticalSection( &ldt_section
);
2176 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
2178 else RtlLeaveCriticalSection( &ldt_section
);
2182 /**********************************************************************
2183 * signal_alloc_thread
2185 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2187 static size_t sigstack_zero_bits
;
2188 struct ntdll_thread_data
*thread_data
;
2189 struct ntdll_thread_data
*parent_data
= NULL
;
2194 if (!sigstack_zero_bits
)
2196 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2197 /* find the first power of two not smaller than min_size */
2198 sigstack_zero_bits
= 12;
2199 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
2200 signal_stack_mask
= (1 << sigstack_zero_bits
) - 1;
2201 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
2203 else parent_data
= ntdll_get_thread_data();
2205 size
= signal_stack_mask
+ 1;
2206 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
2207 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
2210 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2211 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2212 thread_data
= (struct ntdll_thread_data
*)(*teb
)->SpareBytes1
;
2213 if (!(thread_data
->fs
= wine_ldt_alloc_fs()))
2216 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
2217 status
= STATUS_TOO_MANY_THREADS
;
2221 /* inherit debug registers from parent thread */
2222 thread_data
->dr0
= parent_data
->dr0
;
2223 thread_data
->dr1
= parent_data
->dr1
;
2224 thread_data
->dr2
= parent_data
->dr2
;
2225 thread_data
->dr3
= parent_data
->dr3
;
2226 thread_data
->dr6
= parent_data
->dr6
;
2227 thread_data
->dr7
= parent_data
->dr7
;
2235 /**********************************************************************
2236 * signal_free_thread
2238 void signal_free_thread( TEB
*teb
)
2241 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2243 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
2244 if (teb
->DeallocationStack
)
2247 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
2250 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2254 /**********************************************************************
2255 * signal_init_thread
2257 void signal_init_thread( TEB
*teb
)
2259 const WORD fpu_cw
= 0x27f;
2260 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2265 int mib
[2], val
= 1;
2268 mib
[1] = KERN_THALTSTACK
;
2269 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
2272 ss
.ss_sp
= (char *)teb
+ teb_size
;
2273 ss
.ss_size
= signal_stack_size
;
2275 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2277 wine_ldt_set_base( &fs_entry
, teb
);
2278 wine_ldt_set_limit( &fs_entry
, teb_size
- 1 );
2279 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
2280 wine_ldt_init_fs( thread_data
->fs
, &fs_entry
);
2281 thread_data
->gs
= wine_get_gs();
2284 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2286 FIXME("FPU setup not implemented for this platform.\n");
2290 /**********************************************************************
2291 * signal_init_process
2293 void signal_init_process(void)
2295 struct sigaction sig_act
;
2297 sig_act
.sa_mask
= server_block_set
;
2298 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
2300 sig_act
.sa_flags
|= SA_ONSTACK
;
2303 sig_act
.sa_sigaction
= int_handler
;
2304 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2305 sig_act
.sa_sigaction
= fpe_handler
;
2306 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2307 sig_act
.sa_sigaction
= abrt_handler
;
2308 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2309 sig_act
.sa_sigaction
= quit_handler
;
2310 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2311 sig_act
.sa_sigaction
= usr1_handler
;
2312 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2314 sig_act
.sa_sigaction
= segv_handler
;
2315 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2316 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2318 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2322 sig_act
.sa_sigaction
= trap_handler
;
2323 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2327 sig_act
.sa_sigaction
= usr2_handler
;
2328 if (sigaction( SIGUSR2
, &sig_act
, NULL
) == -1) goto error
;
2331 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
2335 perror("sigaction");
2341 /**********************************************************************
2342 * __wine_enter_vm86 (NTDLL.@)
2344 * Enter vm86 mode with the specified register context.
2346 void __wine_enter_vm86( CONTEXT
*context
)
2348 EXCEPTION_RECORD rec
;
2350 struct vm86plus_struct vm86
;
2352 memset( &vm86
, 0, sizeof(vm86
) );
2355 restore_vm86_context( context
, &vm86
);
2357 ntdll_get_thread_data()->vm86_ptr
= &vm86
;
2358 merge_vm86_pending_flags( &rec
);
2360 res
= vm86_enter( &ntdll_get_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
2367 save_vm86_context( context
, &vm86
);
2369 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2370 rec
.ExceptionRecord
= NULL
;
2371 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
2372 rec
.NumberParameters
= 0;
2374 switch(VM86_TYPE(res
))
2376 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
2377 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
2379 case VM86_TRAP
: /* return due to DOS-debugger request */
2380 switch(VM86_ARG(res
))
2382 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2383 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2385 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2386 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
2389 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2393 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
2394 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
2395 rec
.NumberParameters
= 1;
2396 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
2398 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
2399 context
->EFlags
|= VIF_FLAG
;
2400 context
->EFlags
&= ~VIP_FLAG
;
2401 get_vm86_teb_info()->vm86_pending
= 0;
2402 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
2404 case VM86_PICRETURN
: /* return due to pending PIC request */
2405 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
2407 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
2409 WINE_ERR( "unhandled result from vm86 mode %x\n", res
);
2412 raise_exception( &rec
, context
, TRUE
);
2416 #else /* __HAVE_VM86 */
2417 /**********************************************************************
2418 * __wine_enter_vm86 (NTDLL.@)
2420 void __wine_enter_vm86( CONTEXT
*context
)
2422 MESSAGE("vm86 mode not supported on this platform\n");
2424 #endif /* __HAVE_VM86 */
2427 /*******************************************************************
2428 * RtlUnwind (NTDLL.@)
2430 void WINAPI
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
2431 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
2433 EXCEPTION_RECORD record
;
2434 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
2437 context
->Eax
= (DWORD
)retval
;
2439 /* build an exception record, if we do not have one */
2442 record
.ExceptionCode
= STATUS_UNWIND
;
2443 record
.ExceptionFlags
= 0;
2444 record
.ExceptionRecord
= NULL
;
2445 record
.ExceptionAddress
= (void *)context
->Eip
;
2446 record
.NumberParameters
= 0;
2450 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
2452 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2454 /* get chain of exception frames */
2455 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2456 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
2458 /* Check frame address */
2459 if (pEndFrame
&& (frame
> pEndFrame
))
2460 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
2462 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
2465 TRACE( "calling handler at %p code=%x flags=%x\n",
2466 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2467 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
2468 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
2472 case ExceptionContinueSearch
:
2474 case ExceptionCollidedUnwind
:
2478 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
2481 frame
= __wine_pop_frame( frame
);
2484 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 4 )
2487 /*******************************************************************
2488 * NtRaiseException (NTDLL.@)
2490 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2492 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
2493 if (status
== STATUS_SUCCESS
)
2495 set_debug_registers( context
);
2496 set_cpu_context( context
);
2502 /***********************************************************************
2503 * RtlRaiseException (NTDLL.@)
2505 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2509 rec
->ExceptionAddress
= (void *)context
->Eip
;
2510 status
= raise_exception( rec
, context
, TRUE
);
2511 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
2513 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 1 )
2516 /*************************************************************************
2517 * RtlCaptureStackBackTrace (NTDLL.@)
2519 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
2525 RtlCaptureContext( &context
);
2526 if (hash
) *hash
= 0;
2527 frame
= (ULONG
*)context
.Ebp
;
2531 if (!is_valid_frame( frame
)) return 0;
2532 frame
= (ULONG
*)*frame
;
2535 for (i
= 0; i
< count
; i
++)
2537 if (!is_valid_frame( frame
)) break;
2538 buffer
[i
] = (void *)frame
[1];
2539 if (hash
) *hash
+= frame
[1];
2540 frame
= (ULONG
*)*frame
;
2546 extern void DECLSPEC_NORETURN
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
);
2547 __ASM_GLOBAL_FUNC( call_thread_entry_point
,
2549 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2550 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2551 "movl %esp,%ebp\n\t"
2552 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2554 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2556 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2558 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2560 "pushl 12(%ebp)\n\t"
2562 "call " __ASM_NAME("call_thread_func") );
2564 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int), void *frame
);
2565 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
2566 "movl 4(%esp),%eax\n\t"
2567 "movl 8(%esp),%ecx\n\t"
2568 "movl 12(%esp),%ebp\n\t"
2569 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2570 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2571 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2572 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2573 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2574 "leal -20(%ebp),%esp\n\t"
2578 /* wrapper for apps that don't declare the thread function correctly */
2579 extern void DECLSPEC_NORETURN
call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry
, void *arg
);
2580 __ASM_GLOBAL_FUNC(call_thread_func_wrapper
,
2582 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2583 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2584 "movl %esp,%ebp\n\t"
2585 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2587 "pushl 12(%ebp)\n\t"
2589 "leal -4(%ebp),%esp\n\t"
2591 "call " __ASM_NAME("exit_thread") "\n\t"
2594 /***********************************************************************
2597 void call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
, void *frame
)
2599 ntdll_get_thread_data()->exit_frame
= frame
;
2602 call_thread_func_wrapper( entry
, arg
);
2604 __EXCEPT(unhandled_exception_filter
)
2606 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2609 abort(); /* should not be reached */
2612 /***********************************************************************
2613 * RtlExitUserThread (NTDLL.@)
2615 void WINAPI
RtlExitUserThread( ULONG status
)
2617 if (!ntdll_get_thread_data()->exit_frame
) exit_thread( status
);
2618 call_thread_exit_func( status
, exit_thread
, ntdll_get_thread_data()->exit_frame
);
2621 /***********************************************************************
2624 void abort_thread( int status
)
2626 if (!ntdll_get_thread_data()->exit_frame
) terminate_thread( status
);
2627 call_thread_exit_func( status
, terminate_thread
, ntdll_get_thread_data()->exit_frame
);
2630 /**********************************************************************
2631 * DbgBreakPoint (NTDLL.@)
2633 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
2635 /**********************************************************************
2636 * DbgUserBreakPoint (NTDLL.@)
2638 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
2640 /**********************************************************************
2641 * NtCurrentTeb (NTDLL.@)
2643 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2646 /**************************************************************************
2649 __ASM_STDCALL_FUNC( _chkstk
, 0,
2651 "addl %esp,%eax\n\t"
2652 "xchgl %esp,%eax\n\t"
2653 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2654 "movl %eax,0(%esp)\n\t"
2657 /**************************************************************************
2658 * _alloca_probe (NTDLL.@)
2660 __ASM_STDCALL_FUNC( _alloca_probe
, 0,
2662 "addl %esp,%eax\n\t"
2663 "xchgl %esp,%eax\n\t"
2664 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2665 "movl %eax,0(%esp)\n\t"
2669 /**********************************************************************
2670 * EXC_CallHandler (internal)
2672 * Some exception handlers depend on EBP to have a fixed position relative to
2673 * the exception frame.
2674 * Shrinker depends on (*1) doing what it does,
2675 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2676 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2677 * function calling the handler having only 5 parameters (*4).
2679 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
2681 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2682 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2683 "movl %esp,%ebp\n\t"
2684 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2686 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2687 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2688 "pushl 24(%ebp)\n\t"
2689 "pushl 20(%ebp)\n\t"
2690 "pushl 16(%ebp)\n\t"
2691 "pushl 12(%ebp)\n\t"
2693 "call " __ASM_NAME("call_exception_handler") "\n\t"
2695 __ASM_CFI(".cfi_same_value %ebx\n\t")
2697 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2698 __ASM_CFI(".cfi_same_value %ebp\n\t")
2700 __ASM_GLOBAL_FUNC(call_exception_handler
,
2702 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2703 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2704 "movl %esp,%ebp\n\t"
2705 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2707 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2708 "pushl %edx\n\t" /* handler be handled by... */
2710 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2712 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2713 "pushl 20(%ebp)\n\t"
2714 "pushl 16(%ebp)\n\t"
2715 "pushl 12(%ebp)\n\t"
2717 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2718 "call *%ecx\n\t" /* call handler. (*2) */
2720 "movl (0), %esp\n\t" /* restore previous... (*3) */
2722 "popl (0)\n\t" /* exception frame. */
2723 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2725 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2726 __ASM_CFI(".cfi_same_value %ebp\n\t")
2727 "ret $20" ) /* (*4) */
2729 #endif /* __i386__ */