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>
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
45 #ifdef HAVE_SYS_VM86_H
46 # include <sys/vm86.h>
48 #ifdef HAVE_SYS_SIGNAL_H
49 # include <sys/signal.h>
51 #ifdef HAVE_SYS_SYSCTL_H
52 # include <sys/sysctl.h>
54 #ifdef HAVE_SYS_UCONTEXT_H
55 # include <sys/ucontext.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 #ifndef HAVE_SYS_UCONTEXT_H
109 REG_GS
, REG_FS
, REG_ES
, REG_DS
, REG_EDI
, REG_ESI
, REG_EBP
, REG_ESP
,
110 REG_EBX
, REG_EDX
, REG_ECX
, REG_EAX
, REG_TRAPNO
, REG_ERR
, REG_EIP
,
111 REG_CS
, REG_EFL
, REG_UESP
, REG_SS
, NGREG
115 typedef greg_t gregset_t
[NGREG
];
119 unsigned short significand
[4];
120 unsigned short exponent
;
130 unsigned long dataoff
;
131 unsigned long datasel
;
132 struct _libc_fpreg _st
[8];
133 unsigned long status
;
136 typedef struct _libc_fpstate
* fpregset_t
;
142 unsigned long oldmask
;
146 typedef struct ucontext
148 unsigned long uc_flags
;
149 struct ucontext
*uc_link
;
151 mcontext_t uc_mcontext
;
154 #endif /* HAVE_SYS_UCONTEXT_H */
156 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
157 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
158 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
159 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
160 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
161 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
162 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
163 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
165 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
166 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
167 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
168 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
169 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
170 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
172 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
173 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
174 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
175 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
177 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
178 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
180 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
181 #define VIF_FLAG 0x00080000
182 #define VIP_FLAG 0x00100000
184 int vm86_enter( void **vm86_ptr
);
185 void vm86_return(void);
186 void vm86_return_end(void);
187 __ASM_GLOBAL_FUNC(vm86_enter
,
189 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
190 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
192 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
194 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
195 "movl $166,%eax\n\t" /*SYS_vm86*/
196 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
197 "movl (%ecx),%ecx\n\t"
198 "movl $1,%ebx\n\t" /*VM86_ENTER*/
199 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
203 ".globl " __ASM_NAME("vm86_return") "\n\t"
204 __ASM_FUNC("vm86_return") "\n"
205 __ASM_NAME("vm86_return") ":\n\t"
210 __ASM_CFI(".cfi_same_value %ebx\n\t")
212 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
213 __ASM_CFI(".cfi_same_value %ebp\n\t")
214 "testl %eax,%eax\n\t"
216 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
217 "je " __ASM_NAME("vm86_enter") "\n\t"
219 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
221 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
222 __ASM_FUNC("vm86_return_end") "\n"
223 __ASM_NAME("vm86_return_end") ":\n\t"
226 #ifdef HAVE_SYS_VM86_H
231 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
232 void rt_sigreturn(void);
233 __ASM_GLOBAL_FUNC( rt_sigreturn
,
234 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
238 #elif defined (__BSDI__)
240 #include <machine/frame.h>
241 typedef struct trapframe ucontext_t
;
243 #define EAX_sig(context) ((context)->tf_eax)
244 #define EBX_sig(context) ((context)->tf_ebx)
245 #define ECX_sig(context) ((context)->tf_ecx)
246 #define EDX_sig(context) ((context)->tf_edx)
247 #define ESI_sig(context) ((context)->tf_esi)
248 #define EDI_sig(context) ((context)->tf_edi)
249 #define EBP_sig(context) ((context)->tf_ebp)
251 #define CS_sig(context) ((context)->tf_cs)
252 #define DS_sig(context) ((context)->tf_ds)
253 #define ES_sig(context) ((context)->tf_es)
254 #define SS_sig(context) ((context)->tf_ss)
256 #define EFL_sig(context) ((context)->tf_eflags)
258 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
259 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
261 #define FPU_sig(context) NULL /* FIXME */
262 #define FPUX_sig(context) NULL /* FIXME */
264 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
266 #include <machine/trap.h>
268 #define EAX_sig(context) ((context)->uc_mcontext.mc_eax)
269 #define EBX_sig(context) ((context)->uc_mcontext.mc_ebx)
270 #define ECX_sig(context) ((context)->uc_mcontext.mc_ecx)
271 #define EDX_sig(context) ((context)->uc_mcontext.mc_edx)
272 #define ESI_sig(context) ((context)->uc_mcontext.mc_esi)
273 #define EDI_sig(context) ((context)->uc_mcontext.mc_edi)
274 #define EBP_sig(context) ((context)->uc_mcontext.mc_ebp)
276 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
277 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
278 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
279 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
280 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
281 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
283 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
284 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
285 #define EFL_sig(context) ((context)->uc_mcontext.mc_eflags)
287 #define EIP_sig(context) ((context)->uc_mcontext.mc_eip)
288 #define ESP_sig(context) ((context)->uc_mcontext.mc_esp)
290 #define FPU_sig(context) NULL /* FIXME */
291 #define FPUX_sig(context) NULL /* FIXME */
293 #elif defined (__OpenBSD__)
295 #define EAX_sig(context) ((context)->sc_eax)
296 #define EBX_sig(context) ((context)->sc_ebx)
297 #define ECX_sig(context) ((context)->sc_ecx)
298 #define EDX_sig(context) ((context)->sc_edx)
299 #define ESI_sig(context) ((context)->sc_esi)
300 #define EDI_sig(context) ((context)->sc_edi)
301 #define EBP_sig(context) ((context)->sc_ebp)
303 #define CS_sig(context) ((context)->sc_cs)
304 #define DS_sig(context) ((context)->sc_ds)
305 #define ES_sig(context) ((context)->sc_es)
306 #define FS_sig(context) ((context)->sc_fs)
307 #define GS_sig(context) ((context)->sc_gs)
308 #define SS_sig(context) ((context)->sc_ss)
310 #define TRAP_sig(context) ((context)->sc_trapno)
311 #define ERROR_sig(context) ((context)->sc_err)
312 #define EFL_sig(context) ((context)->sc_eflags)
314 #define EIP_sig(context) ((context)->sc_eip)
315 #define ESP_sig(context) ((context)->sc_esp)
317 #define FPU_sig(context) NULL /* FIXME */
318 #define FPUX_sig(context) NULL /* FIXME */
320 #define T_MCHK T_MACHK
321 #define T_XMMFLT T_XFTRAP
323 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
326 #include <sys/regset.h>
330 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
331 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
332 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
333 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
334 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
335 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
336 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
338 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
339 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
340 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
341 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
343 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
344 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
346 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
348 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
350 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
352 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
354 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
357 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
360 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
363 #define FPU_sig(context) NULL /* FIXME */
364 #define FPUX_sig(context) NULL /* FIXME */
366 #elif defined (__APPLE__)
368 /* work around silly renaming of struct members in OS X 10.5 */
369 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
370 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
371 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
372 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
373 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
374 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
375 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
376 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
377 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
378 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
379 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
380 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
381 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
382 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
383 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
384 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
385 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
386 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
387 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
388 #define FPU_sig(context) NULL
389 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
391 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
392 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
393 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
394 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
395 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
396 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
397 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
398 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
399 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
400 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
401 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
402 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
403 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
404 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
405 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
406 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
407 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
408 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
409 #define FPU_sig(context) NULL
410 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
413 #elif defined(__NetBSD__)
415 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
416 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
417 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
418 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
419 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
420 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
421 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
422 #define ESP_sig(context) _UC_MACHINE_SP(context)
424 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
425 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
426 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
427 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
428 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
429 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
431 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
432 #define EIP_sig(context) _UC_MACHINE_PC(context)
433 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
434 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
436 #define FPU_sig(context) NULL
437 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
440 #define T_XMMFLT T_XMM
442 #elif defined(__GNU__)
444 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
445 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
446 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
447 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
448 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
449 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
450 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
451 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
453 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
454 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
455 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
456 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
457 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
458 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
460 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
461 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
462 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
463 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
465 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
466 #define FPUX_sig(context) NULL
469 #error You must define the signal context functions for your platform
472 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
474 typedef int (*wine_signal_handler
)(unsigned int sig
);
476 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
477 static size_t signal_stack_mask
;
478 static size_t signal_stack_size
;
480 static wine_signal_handler handlers
[256];
482 static BOOL fpux_support
; /* whether the CPU supports extended fpu context */
484 extern void DECLSPEC_NORETURN
__wine_restore_regs( const CONTEXT
*context
);
488 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
489 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
490 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
491 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
492 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
493 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
494 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
495 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
496 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
497 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
498 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
499 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
500 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
501 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
502 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
503 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
504 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
505 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
506 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
507 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
508 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
510 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
511 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
512 TRAP_x86_NMI
= 2, /* NMI interrupt */
513 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
514 TRAP_x86_OFLOW
= 4, /* Overflow exception */
515 TRAP_x86_BOUND
= 5, /* Bound range exception */
516 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
517 TRAP_x86_DNA
= 7, /* Device not available exception */
518 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
519 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
520 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
521 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
522 TRAP_x86_STKFLT
= 12, /* Stack fault */
523 TRAP_x86_PROTFLT
= 13, /* General protection fault */
524 TRAP_x86_PAGEFLT
= 14, /* Page fault */
525 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
526 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
527 TRAP_x86_MCHK
= 18, /* Machine check exception */
528 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
529 otherwise Cache flush exception (via SIGSEV) */
533 /* Exception record for handling exceptions happening inside exception handlers */
536 EXCEPTION_REGISTRATION_RECORD frame
;
537 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
540 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
541 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
542 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
544 /***********************************************************************
547 static inline int dispatch_signal(unsigned int sig
)
549 if (handlers
[sig
] == NULL
) return 0;
550 return handlers
[sig
](sig
);
554 /***********************************************************************
557 * Get the trap code for a signal.
559 static inline enum i386_trap_code
get_trap_code( const ucontext_t
*sigcontext
)
562 return TRAP_sig(sigcontext
);
564 return TRAP_x86_UNKNOWN
; /* unknown trap code */
568 /***********************************************************************
571 * Get the error code for a signal.
573 static inline WORD
get_error_code( const ucontext_t
*sigcontext
)
576 return ERROR_sig(sigcontext
);
582 /***********************************************************************
585 * Get the base of the signal stack for the current thread.
587 static inline void *get_signal_stack(void)
589 return (char *)NtCurrentTeb() + 4096;
593 /***********************************************************************
596 * Get the current teb based on the stack pointer.
598 static inline TEB
*get_current_teb(void)
601 __asm__("movl %%esp,%0" : "=g" (esp
) );
602 return (TEB
*)(esp
& ~signal_stack_mask
);
606 /*******************************************************************
609 static inline BOOL
is_valid_frame( void *frame
)
611 if ((ULONG_PTR
)frame
& 3) return FALSE
;
612 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
613 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
616 /*******************************************************************
619 * Handler for exceptions happening inside a handler.
621 static DWORD
raise_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
622 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
624 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
625 return ExceptionContinueSearch
;
626 /* We shouldn't get here so we store faulty frame in dispatcher */
627 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
628 return ExceptionNestedException
;
632 /*******************************************************************
635 * Handler for exceptions happening inside an unwind handler.
637 static DWORD
unwind_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
638 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
640 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
641 return ExceptionContinueSearch
;
642 /* We shouldn't get here so we store faulty frame in dispatcher */
643 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
644 return ExceptionCollidedUnwind
;
648 /**********************************************************************
649 * call_stack_handlers
651 * Call the stack handlers chain.
653 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
655 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
658 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
660 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
662 /* Check frame address */
663 if (!is_valid_frame( frame
))
665 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
670 TRACE( "calling handler at %p code=%x flags=%x\n",
671 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
672 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, raise_handler
);
673 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
675 if (frame
== nested_frame
)
677 /* no longer nested */
679 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
684 case ExceptionContinueExecution
:
685 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
686 return STATUS_NONCONTINUABLE_EXCEPTION
;
687 case ExceptionContinueSearch
:
689 case ExceptionNestedException
:
690 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
691 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
694 return STATUS_INVALID_DISPOSITION
;
698 return STATUS_UNHANDLED_EXCEPTION
;
702 /*******************************************************************
705 * Implementation of NtRaiseException.
707 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
715 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
716 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
717 context
->Eip
, GetCurrentThreadId() );
718 for (c
= 0; c
< rec
->NumberParameters
; c
++)
719 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
720 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
722 if (rec
->ExceptionInformation
[1] >> 16)
723 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
724 rec
->ExceptionAddress
,
725 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
727 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
728 rec
->ExceptionAddress
,
729 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
733 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
734 context
->Eax
, context
->Ebx
, context
->Ecx
,
735 context
->Edx
, context
->Esi
, context
->Edi
);
736 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
737 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
738 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
740 status
= send_debug_event( rec
, TRUE
, context
);
741 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
742 return STATUS_SUCCESS
;
744 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
745 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
747 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
748 return STATUS_SUCCESS
;
750 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
754 /* last chance exception */
756 status
= send_debug_event( rec
, FALSE
, context
);
757 if (status
!= DBG_CONTINUE
)
759 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
760 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
761 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
762 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
764 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
765 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
766 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
768 return STATUS_SUCCESS
;
773 /***********************************************************************
776 * Set the register values from a vm86 structure.
778 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
780 context
->ContextFlags
= CONTEXT_FULL
;
781 context
->Eax
= vm86
->regs
.eax
;
782 context
->Ebx
= vm86
->regs
.ebx
;
783 context
->Ecx
= vm86
->regs
.ecx
;
784 context
->Edx
= vm86
->regs
.edx
;
785 context
->Esi
= vm86
->regs
.esi
;
786 context
->Edi
= vm86
->regs
.edi
;
787 context
->Esp
= vm86
->regs
.esp
;
788 context
->Ebp
= vm86
->regs
.ebp
;
789 context
->Eip
= vm86
->regs
.eip
;
790 context
->SegCs
= vm86
->regs
.cs
;
791 context
->SegDs
= vm86
->regs
.ds
;
792 context
->SegEs
= vm86
->regs
.es
;
793 context
->SegFs
= vm86
->regs
.fs
;
794 context
->SegGs
= vm86
->regs
.gs
;
795 context
->SegSs
= vm86
->regs
.ss
;
796 context
->EFlags
= vm86
->regs
.eflags
;
800 /***********************************************************************
801 * restore_vm86_context
803 * Build a vm86 structure from the register values.
805 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
807 vm86
->regs
.eax
= context
->Eax
;
808 vm86
->regs
.ebx
= context
->Ebx
;
809 vm86
->regs
.ecx
= context
->Ecx
;
810 vm86
->regs
.edx
= context
->Edx
;
811 vm86
->regs
.esi
= context
->Esi
;
812 vm86
->regs
.edi
= context
->Edi
;
813 vm86
->regs
.esp
= context
->Esp
;
814 vm86
->regs
.ebp
= context
->Ebp
;
815 vm86
->regs
.eip
= context
->Eip
;
816 vm86
->regs
.cs
= context
->SegCs
;
817 vm86
->regs
.ds
= context
->SegDs
;
818 vm86
->regs
.es
= context
->SegEs
;
819 vm86
->regs
.fs
= context
->SegFs
;
820 vm86
->regs
.gs
= context
->SegGs
;
821 vm86
->regs
.ss
= context
->SegSs
;
822 vm86
->regs
.eflags
= context
->EFlags
;
826 /**********************************************************************
827 * merge_vm86_pending_flags
829 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
830 * raises exception if there are pending events and VIF flag
831 * has been turned on.
833 * Called from __wine_enter_vm86 because vm86_enter
834 * doesn't check for pending events.
836 * Called from raise_vm86_sti_exception to check for
837 * pending events in a signal safe way.
839 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
841 BOOL check_pending
= TRUE
;
842 struct vm86plus_struct
*vm86
=
843 (struct vm86plus_struct
*)(ntdll_get_thread_data()->vm86_ptr
);
846 * In order to prevent a race when SIGUSR2 occurs while
847 * we are returning from exception handler, pending events
848 * will be rechecked after each raised exception.
850 while (check_pending
&& get_vm86_teb_info()->vm86_pending
)
852 check_pending
= FALSE
;
853 ntdll_get_thread_data()->vm86_ptr
= NULL
;
856 * If VIF is set, throw exception.
857 * Note that SIGUSR2 may turn VIF flag off so
858 * VIF check must occur only when TEB.vm86_ptr is NULL.
860 if (vm86
->regs
.eflags
& VIF_FLAG
)
863 save_vm86_context( &vcontext
, vm86
);
865 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
866 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
867 rec
->ExceptionRecord
= NULL
;
868 rec
->NumberParameters
= 0;
869 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
871 vcontext
.EFlags
&= ~VIP_FLAG
;
872 get_vm86_teb_info()->vm86_pending
= 0;
873 raise_exception( rec
, &vcontext
, TRUE
);
875 restore_vm86_context( &vcontext
, vm86
);
876 check_pending
= TRUE
;
879 ntdll_get_thread_data()->vm86_ptr
= vm86
;
883 * Merge VIP flags in a signal safe way. This requires
884 * that the following operation compiles into atomic
887 vm86
->regs
.eflags
|= get_vm86_teb_info()->vm86_pending
;
889 #endif /* __HAVE_VM86 */
894 /* We have to workaround two Solaris breakages:
895 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
897 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
898 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
901 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
902 __ASM_GLOBAL_FUNC( sigaction_syscall
,
903 "movl $0x62,%eax\n\t"
907 /* assume the same libc handler is used for all signals */
908 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
910 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
912 struct ntdll_thread_data
*thread_data
;
914 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
916 thread_data
= (struct ntdll_thread_data
*)get_current_teb()->SpareBytes1
;
917 wine_set_fs( thread_data
->fs
);
918 wine_set_gs( thread_data
->gs
);
920 libc_sigacthandler( signal
, siginfo
, sigcontext
);
923 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
925 struct sigaction real_act
;
927 if (sigaction( sig
, new, old
) == -1) return -1;
929 /* retrieve the real handler and flags with a direct syscall */
930 sigaction_syscall( sig
, NULL
, &real_act
);
931 libc_sigacthandler
= real_act
.sa_sigaction
;
932 real_act
.sa_sigaction
= wine_sigacthandler
;
933 sigaction_syscall( sig
, &real_act
, NULL
);
936 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
940 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
942 extern void clear_alignment_flag(void);
943 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
945 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
946 "andl $~0x40000,(%esp)\n\t"
948 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
952 /***********************************************************************
955 * Handler initialization when the full context is not needed.
956 * Return the stack pointer to use for pushing the exception data.
958 static inline void *init_handler( const ucontext_t
*sigcontext
, WORD
*fs
, WORD
*gs
)
960 TEB
*teb
= get_current_teb();
962 clear_alignment_flag();
964 /* get %fs and %gs at time of the fault */
966 *fs
= LOWORD(FS_sig(sigcontext
));
971 *gs
= LOWORD(GS_sig(sigcontext
));
976 #ifndef __sun /* see above for Solaris handling */
978 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
979 wine_set_fs( thread_data
->fs
);
980 wine_set_gs( thread_data
->gs
);
984 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
985 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
988 * Win16 or DOS protected mode. Note that during switch
989 * from 16-bit mode to linear mode, CS may be set to system
990 * segment before FS is restored. Fortunately, in this case
991 * SS is still non-system segment. This is why both CS and SS
994 return teb
->WOW32Reserved
;
996 return (void *)(ESP_sig(sigcontext
) & ~3);
1000 /***********************************************************************
1003 * Save the thread FPU context.
1005 static inline void save_fpu( CONTEXT
*context
)
1008 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1009 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
1014 /***********************************************************************
1017 * Save the thread FPU extended context.
1019 static inline void save_fpux( CONTEXT
*context
)
1022 /* we have to enforce alignment by hand */
1023 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1024 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1026 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
1027 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1028 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
1033 /***********************************************************************
1036 * Restore the FPU context to a sigcontext.
1038 static inline void restore_fpu( const CONTEXT
*context
)
1040 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
1041 /* reset the current interrupt status */
1042 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
1044 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
1045 #endif /* __GNUC__ */
1049 /***********************************************************************
1052 * Restore the FPU extended context to a sigcontext.
1054 static inline void restore_fpux( const CONTEXT
*context
)
1057 /* we have to enforce alignment by hand */
1058 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1059 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1061 memcpy( state
, context
->ExtendedRegisters
, sizeof(*state
) );
1062 /* reset the current interrupt status */
1063 state
->StatusWord
&= state
->ControlWord
| 0xff80;
1064 __asm__
__volatile__( "fxrstor %0" : : "m" (*state
) );
1069 /***********************************************************************
1072 * Build a standard FPU context from an extended one.
1074 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XMM_SAVE_AREA32
*fpux
)
1076 unsigned int i
, tag
, stack_top
;
1078 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
1079 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
1080 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
1081 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
1082 fpu
->DataOffset
= fpux
->DataOffset
;
1083 fpu
->DataSelector
= fpux
->DataSelector
;
1084 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
1086 stack_top
= (fpux
->StatusWord
>> 11) & 7;
1087 fpu
->TagWord
= 0xffff0000;
1088 for (i
= 0; i
< 8; i
++)
1090 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
1091 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
1094 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
1095 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
1097 tag
= 2; /* special */
1099 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
1101 if (reg
->Low
) tag
= 2; /* special */
1102 else tag
= 1; /* zero */
1106 if (reg
->Low
>> 63) tag
= 0; /* valid */
1107 else tag
= 2; /* special */
1110 fpu
->TagWord
|= tag
<< (2 * i
);
1115 /***********************************************************************
1118 * Build a context structure from the signal info.
1120 static inline void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
, WORD fs
, WORD gs
)
1122 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1123 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1124 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1126 memset(context
, 0, sizeof(*context
));
1127 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
1128 context
->Eax
= EAX_sig(sigcontext
);
1129 context
->Ebx
= EBX_sig(sigcontext
);
1130 context
->Ecx
= ECX_sig(sigcontext
);
1131 context
->Edx
= EDX_sig(sigcontext
);
1132 context
->Esi
= ESI_sig(sigcontext
);
1133 context
->Edi
= EDI_sig(sigcontext
);
1134 context
->Ebp
= EBP_sig(sigcontext
);
1135 context
->EFlags
= EFL_sig(sigcontext
);
1136 context
->Eip
= EIP_sig(sigcontext
);
1137 context
->Esp
= ESP_sig(sigcontext
);
1138 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
1139 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
1140 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
1141 context
->SegFs
= fs
;
1142 context
->SegGs
= gs
;
1143 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
1144 context
->Dr0
= regs
->dr0
;
1145 context
->Dr1
= regs
->dr1
;
1146 context
->Dr2
= regs
->dr2
;
1147 context
->Dr3
= regs
->dr3
;
1148 context
->Dr6
= regs
->dr6
;
1149 context
->Dr7
= regs
->dr7
;
1153 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1154 context
->FloatSave
= *fpu
;
1158 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
1159 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
1160 fpux_support
= TRUE
;
1161 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
1163 if (!fpu
&& !fpux
) save_fpu( context
);
1167 /***********************************************************************
1170 * Restore the signal info from the context.
1172 static inline void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1174 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1175 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1176 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1178 regs
->dr0
= context
->Dr0
;
1179 regs
->dr1
= context
->Dr1
;
1180 regs
->dr2
= context
->Dr2
;
1181 regs
->dr3
= context
->Dr3
;
1182 regs
->dr6
= context
->Dr6
;
1183 regs
->dr7
= context
->Dr7
;
1184 EAX_sig(sigcontext
) = context
->Eax
;
1185 EBX_sig(sigcontext
) = context
->Ebx
;
1186 ECX_sig(sigcontext
) = context
->Ecx
;
1187 EDX_sig(sigcontext
) = context
->Edx
;
1188 ESI_sig(sigcontext
) = context
->Esi
;
1189 EDI_sig(sigcontext
) = context
->Edi
;
1190 EBP_sig(sigcontext
) = context
->Ebp
;
1191 EFL_sig(sigcontext
) = context
->EFlags
;
1192 EIP_sig(sigcontext
) = context
->Eip
;
1193 ESP_sig(sigcontext
) = context
->Esp
;
1194 CS_sig(sigcontext
) = context
->SegCs
;
1195 DS_sig(sigcontext
) = context
->SegDs
;
1196 ES_sig(sigcontext
) = context
->SegEs
;
1197 SS_sig(sigcontext
) = context
->SegSs
;
1199 GS_sig(sigcontext
) = context
->SegGs
;
1201 wine_set_gs( context
->SegGs
);
1204 FS_sig(sigcontext
) = context
->SegFs
;
1206 wine_set_fs( context
->SegFs
);
1209 if (fpu
) *fpu
= context
->FloatSave
;
1210 if (fpux
) memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
1211 if (!fpu
&& !fpux
) restore_fpu( context
);
1215 /***********************************************************************
1216 * RtlCaptureContext (NTDLL.@)
1218 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
1220 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1221 "movl 8(%esp),%eax\n\t" /* context */
1222 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1223 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1224 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1225 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1226 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1227 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1228 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1229 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1230 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1231 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1232 "movl %ebp,0xb4(%eax)\n\t" /* context->Ebp */
1233 "movl 4(%esp),%edx\n\t"
1234 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1235 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1237 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1238 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1239 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1240 "leal 8(%esp),%edx\n\t"
1241 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1242 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1243 "popl 0xb0(%eax)\n\t" /* context->Eax */
1244 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1248 /***********************************************************************
1251 * Set the new CPU context. Used by NtSetContextThread.
1253 void set_cpu_context( const CONTEXT
*context
)
1255 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1257 if ((flags
& CONTEXT_EXTENDED_REGISTERS
) && fpux_support
) restore_fpux( context
);
1258 else if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
1260 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1262 ntdll_get_thread_data()->dr0
= context
->Dr0
;
1263 ntdll_get_thread_data()->dr1
= context
->Dr1
;
1264 ntdll_get_thread_data()->dr2
= context
->Dr2
;
1265 ntdll_get_thread_data()->dr3
= context
->Dr3
;
1266 ntdll_get_thread_data()->dr6
= context
->Dr6
;
1267 ntdll_get_thread_data()->dr7
= context
->Dr7
;
1269 if (flags
& CONTEXT_FULL
)
1271 if (!(flags
& CONTEXT_CONTROL
))
1272 FIXME( "setting partial context (%x) not supported\n", flags
);
1273 else if (flags
& CONTEXT_SEGMENTS
)
1274 __wine_restore_regs( context
);
1277 CONTEXT newcontext
= *context
;
1278 newcontext
.SegDs
= wine_get_ds();
1279 newcontext
.SegEs
= wine_get_es();
1280 newcontext
.SegFs
= wine_get_fs();
1281 newcontext
.SegGs
= wine_get_gs();
1282 __wine_restore_regs( &newcontext
);
1288 /***********************************************************************
1289 * set_debug_registers
1291 static void set_debug_registers( const CONTEXT
*context
)
1293 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1294 context_t server_context
;
1296 if (!(flags
& CONTEXT_DEBUG_REGISTERS
)) return;
1297 if (ntdll_get_thread_data()->dr0
== context
->Dr0
&&
1298 ntdll_get_thread_data()->dr1
== context
->Dr1
&&
1299 ntdll_get_thread_data()->dr2
== context
->Dr2
&&
1300 ntdll_get_thread_data()->dr3
== context
->Dr3
&&
1301 ntdll_get_thread_data()->dr6
== context
->Dr6
&&
1302 ntdll_get_thread_data()->dr7
== context
->Dr7
) return;
1304 context_to_server( &server_context
, context
);
1306 SERVER_START_REQ( set_thread_context
)
1308 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
1310 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
1311 wine_server_call( req
);
1317 /***********************************************************************
1320 * Copy a register context according to the flags.
1322 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1324 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
1325 if (flags
& CONTEXT_INTEGER
)
1327 to
->Eax
= from
->Eax
;
1328 to
->Ebx
= from
->Ebx
;
1329 to
->Ecx
= from
->Ecx
;
1330 to
->Edx
= from
->Edx
;
1331 to
->Esi
= from
->Esi
;
1332 to
->Edi
= from
->Edi
;
1334 if (flags
& CONTEXT_CONTROL
)
1336 to
->Ebp
= from
->Ebp
;
1337 to
->Esp
= from
->Esp
;
1338 to
->Eip
= from
->Eip
;
1339 to
->SegCs
= from
->SegCs
;
1340 to
->SegSs
= from
->SegSs
;
1341 to
->EFlags
= from
->EFlags
;
1343 if (flags
& CONTEXT_SEGMENTS
)
1345 to
->SegDs
= from
->SegDs
;
1346 to
->SegEs
= from
->SegEs
;
1347 to
->SegFs
= from
->SegFs
;
1348 to
->SegGs
= from
->SegGs
;
1350 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1352 to
->Dr0
= from
->Dr0
;
1353 to
->Dr1
= from
->Dr1
;
1354 to
->Dr2
= from
->Dr2
;
1355 to
->Dr3
= from
->Dr3
;
1356 to
->Dr6
= from
->Dr6
;
1357 to
->Dr7
= from
->Dr7
;
1359 if (flags
& CONTEXT_FLOATING_POINT
)
1361 to
->FloatSave
= from
->FloatSave
;
1363 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1365 memcpy( to
->ExtendedRegisters
, from
->ExtendedRegisters
, sizeof(to
->ExtendedRegisters
) );
1370 /***********************************************************************
1373 * Convert a register context to the server format.
1375 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1377 DWORD flags
= from
->ContextFlags
& ~CONTEXT_i386
; /* get rid of CPU id */
1379 memset( to
, 0, sizeof(*to
) );
1382 if (flags
& CONTEXT_CONTROL
)
1384 to
->flags
|= SERVER_CTX_CONTROL
;
1385 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
1386 to
->ctl
.i386_regs
.esp
= from
->Esp
;
1387 to
->ctl
.i386_regs
.eip
= from
->Eip
;
1388 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
1389 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
1390 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
1392 if (flags
& CONTEXT_INTEGER
)
1394 to
->flags
|= SERVER_CTX_INTEGER
;
1395 to
->integer
.i386_regs
.eax
= from
->Eax
;
1396 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
1397 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
1398 to
->integer
.i386_regs
.edx
= from
->Edx
;
1399 to
->integer
.i386_regs
.esi
= from
->Esi
;
1400 to
->integer
.i386_regs
.edi
= from
->Edi
;
1402 if (flags
& CONTEXT_SEGMENTS
)
1404 to
->flags
|= SERVER_CTX_SEGMENTS
;
1405 to
->seg
.i386_regs
.ds
= from
->SegDs
;
1406 to
->seg
.i386_regs
.es
= from
->SegEs
;
1407 to
->seg
.i386_regs
.fs
= from
->SegFs
;
1408 to
->seg
.i386_regs
.gs
= from
->SegGs
;
1410 if (flags
& CONTEXT_FLOATING_POINT
)
1412 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1413 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
1414 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
1415 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
1416 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
1417 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
1418 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
1419 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
1420 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
1421 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
1423 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1425 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1426 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
1427 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
1428 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
1429 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
1430 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
1431 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
1433 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1435 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
1436 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
1438 return STATUS_SUCCESS
;
1442 /***********************************************************************
1443 * context_from_server
1445 * Convert a register context from the server format.
1447 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1449 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
1451 to
->ContextFlags
= CONTEXT_i386
;
1452 if (from
->flags
& SERVER_CTX_CONTROL
)
1454 to
->ContextFlags
|= CONTEXT_CONTROL
;
1455 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
1456 to
->Esp
= from
->ctl
.i386_regs
.esp
;
1457 to
->Eip
= from
->ctl
.i386_regs
.eip
;
1458 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
1459 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
1460 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
1462 if (from
->flags
& SERVER_CTX_INTEGER
)
1464 to
->ContextFlags
|= CONTEXT_INTEGER
;
1465 to
->Eax
= from
->integer
.i386_regs
.eax
;
1466 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
1467 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
1468 to
->Edx
= from
->integer
.i386_regs
.edx
;
1469 to
->Esi
= from
->integer
.i386_regs
.esi
;
1470 to
->Edi
= from
->integer
.i386_regs
.edi
;
1472 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1474 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1475 to
->SegDs
= from
->seg
.i386_regs
.ds
;
1476 to
->SegEs
= from
->seg
.i386_regs
.es
;
1477 to
->SegFs
= from
->seg
.i386_regs
.fs
;
1478 to
->SegGs
= from
->seg
.i386_regs
.gs
;
1480 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1482 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1483 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
1484 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
1485 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
1486 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
1487 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
1488 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
1489 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
1490 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
1491 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
1493 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1495 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1496 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
1497 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
1498 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
1499 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
1500 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
1501 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
1503 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
1505 to
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1506 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
1508 return STATUS_SUCCESS
;
1512 /***********************************************************************
1513 * is_privileged_instr
1515 * Check if the fault location is a privileged instruction.
1516 * Based on the instruction emulation code in dlls/kernel/instr.c.
1518 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1521 unsigned int prefix_count
= 0;
1523 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
1524 instr
= (BYTE
*)context
->Eip
;
1526 for (;;) switch(*instr
)
1528 /* instruction prefixes */
1529 case 0x2e: /* %cs: */
1530 case 0x36: /* %ss: */
1531 case 0x3e: /* %ds: */
1532 case 0x26: /* %es: */
1533 case 0x64: /* %fs: */
1534 case 0x65: /* %gs: */
1535 case 0x66: /* opcode size */
1536 case 0x67: /* addr size */
1537 case 0xf0: /* lock */
1538 case 0xf2: /* repne */
1539 case 0xf3: /* repe */
1540 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1544 case 0x0f: /* extended instruction */
1547 case 0x20: /* mov crX, reg */
1548 case 0x21: /* mov drX, reg */
1549 case 0x22: /* mov reg, crX */
1550 case 0x23: /* mov reg drX */
1551 return EXCEPTION_PRIV_INSTRUCTION
;
1554 case 0x6c: /* insb (%dx) */
1555 case 0x6d: /* insl (%dx) */
1556 case 0x6e: /* outsb (%dx) */
1557 case 0x6f: /* outsl (%dx) */
1558 case 0xcd: /* int $xx */
1559 case 0xe4: /* inb al,XX */
1560 case 0xe5: /* in (e)ax,XX */
1561 case 0xe6: /* outb XX,al */
1562 case 0xe7: /* out XX,(e)ax */
1563 case 0xec: /* inb (%dx),%al */
1564 case 0xed: /* inl (%dx),%eax */
1565 case 0xee: /* outb %al,(%dx) */
1566 case 0xef: /* outl %eax,(%dx) */
1567 case 0xf4: /* hlt */
1568 case 0xfa: /* cli */
1569 case 0xfb: /* sti */
1570 return EXCEPTION_PRIV_INSTRUCTION
;
1576 /***********************************************************************
1579 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1581 static inline BOOL
check_invalid_gs( CONTEXT
*context
)
1583 unsigned int prefix_count
= 0;
1584 const BYTE
*instr
= (BYTE
*)context
->Eip
;
1585 WORD system_gs
= ntdll_get_thread_data()->gs
;
1587 if (context
->SegGs
== system_gs
) return FALSE
;
1588 if (!wine_ldt_is_system( context
->SegCs
)) return FALSE
;
1589 /* only handle faults in system libraries */
1590 if (virtual_is_valid_code_address( instr
, 1 )) return FALSE
;
1592 for (;;) switch(*instr
)
1594 /* instruction prefixes */
1595 case 0x2e: /* %cs: */
1596 case 0x36: /* %ss: */
1597 case 0x3e: /* %ds: */
1598 case 0x26: /* %es: */
1599 case 0x64: /* %fs: */
1600 case 0x66: /* opcode size */
1601 case 0x67: /* addr size */
1602 case 0xf0: /* lock */
1603 case 0xf2: /* repne */
1604 case 0xf3: /* repe */
1605 if (++prefix_count
>= 15) return FALSE
;
1608 case 0x65: /* %gs: */
1609 TRACE( "%04x/%04x at %p, fixing up\n", context
->SegGs
, system_gs
, instr
);
1610 context
->SegGs
= system_gs
;
1618 #include "pshpack1.h"
1621 DWORD movl
; /* movl this,4(%esp) */
1623 BYTE jmp
; /* jmp func */
1626 #include "poppack.h"
1628 /**********************************************************************
1631 * Check if code destination is an ATL thunk, and emulate it if so.
1633 static BOOL
check_atl_thunk( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1635 const struct atl_thunk
*thunk
= (const struct atl_thunk
*)rec
->ExceptionInformation
[1];
1636 struct atl_thunk thunk_copy
;
1639 if (virtual_uninterrupted_read_memory( thunk
, &thunk_copy
, sizeof(*thunk
) ) != sizeof(*thunk
))
1642 if (thunk_copy
.movl
== 0x042444c7 && thunk_copy
.jmp
== 0xe9)
1644 if (virtual_uninterrupted_write_memory( (DWORD
*)context
->Esp
+ 1,
1645 &thunk_copy
.this, sizeof(DWORD
) ) == sizeof(DWORD
))
1647 context
->Eip
= (DWORD_PTR
)(&thunk
->func
+ 1) + thunk_copy
.func
;
1648 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1649 thunk
, context
->Eip
, thunk_copy
.this );
1658 /***********************************************************************
1659 * setup_exception_record
1661 * Setup the exception record and context on the thread stack.
1663 static EXCEPTION_RECORD
*setup_exception_record( ucontext_t
*sigcontext
, void *stack_ptr
,
1664 WORD fs
, WORD gs
, raise_func func
)
1668 void *ret_addr
; /* return address from raise_func */
1669 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
1670 CONTEXT
*context_ptr
; /* second arg for raise_func */
1672 EXCEPTION_RECORD rec
;
1675 } *stack
= stack_ptr
;
1676 DWORD exception_code
= 0;
1678 /* stack sanity checks */
1680 if ((char *)stack
>= (char *)get_signal_stack() &&
1681 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1683 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1684 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1685 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1686 NtCurrentTeb()->Tib
.StackBase
);
1690 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1691 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1692 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1694 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1695 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1696 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1697 NtCurrentTeb()->Tib
.StackBase
);
1699 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1701 /* stack overflow on last page, unrecoverable */
1702 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1703 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1704 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1705 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1706 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1709 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1711 /* stack access below stack limit, may be recoverable */
1712 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1715 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1716 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1717 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1718 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1719 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1724 stack
--; /* push the stack_layout structure */
1725 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1726 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1727 #elif defined(VALGRIND_MAKE_WRITABLE)
1728 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1730 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
1731 stack
->rec_ptr
= &stack
->rec
;
1732 stack
->context_ptr
= &stack
->context
;
1734 stack
->rec
.ExceptionRecord
= NULL
;
1735 stack
->rec
.ExceptionCode
= exception_code
;
1736 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1737 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
1738 stack
->rec
.NumberParameters
= 0;
1740 save_context( &stack
->context
, sigcontext
, fs
, gs
);
1742 /* now modify the sigcontext to return to the raise function */
1743 ESP_sig(sigcontext
) = (DWORD
)stack
;
1744 EIP_sig(sigcontext
) = (DWORD
)func
;
1745 /* clear single-step, direction, and align check flag */
1746 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1747 CS_sig(sigcontext
) = wine_get_cs();
1748 DS_sig(sigcontext
) = wine_get_ds();
1749 ES_sig(sigcontext
) = wine_get_es();
1750 FS_sig(sigcontext
) = wine_get_fs();
1751 GS_sig(sigcontext
) = wine_get_gs();
1752 SS_sig(sigcontext
) = wine_get_ss();
1754 return stack
->rec_ptr
;
1758 /***********************************************************************
1761 * Setup a proper stack frame for the raise function, and modify the
1762 * sigcontext so that the return from the signal handler will call
1763 * the raise function.
1765 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
1768 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1770 return setup_exception_record( sigcontext
, stack
, fs
, gs
, func
);
1774 /***********************************************************************
1775 * get_exception_context
1777 * Get a pointer to the context built by setup_exception.
1779 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
1781 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
1785 /**********************************************************************
1788 * Get the FPU exception code from the FPU status.
1790 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1792 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1794 if (status
& 0x01) /* IE */
1796 if (status
& 0x40) /* SF */
1797 return EXCEPTION_FLT_STACK_CHECK
;
1799 return EXCEPTION_FLT_INVALID_OPERATION
;
1801 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1802 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1803 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1804 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1805 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1806 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1810 /**********************************************************************
1811 * raise_segv_exception
1813 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1817 switch(rec
->ExceptionCode
)
1819 case EXCEPTION_ACCESS_VIOLATION
:
1820 if (rec
->NumberParameters
== 2)
1822 if (rec
->ExceptionInformation
[1] == 0xffffffff && check_invalid_gs( context
))
1824 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
1825 rec
->ExceptionInformation
[0] )))
1827 if (rec
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
&&
1828 rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
1831 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
1832 &flags
, sizeof(flags
), NULL
);
1834 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
) && check_atl_thunk( rec
, context
))
1837 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1838 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
))
1839 rec
->ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
1843 case EXCEPTION_DATATYPE_MISALIGNMENT
:
1844 /* FIXME: pass through exception handler first? */
1845 if (context
->EFlags
& 0x00040000)
1847 /* Disable AC flag, return */
1848 context
->EFlags
&= ~0x00040000;
1853 status
= NtRaiseException( rec
, context
, TRUE
);
1854 raise_status( status
, rec
);
1856 set_cpu_context( context
);
1860 /**********************************************************************
1861 * raise_trap_exception
1863 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1867 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
1869 /* when single stepping can't tell whether this is a hw bp or a
1870 * single step interrupt. try to avoid as much overhead as possible
1871 * and only do a server call if there is any hw bp enabled. */
1873 if( !(context
->EFlags
& 0x100) || (ntdll_get_thread_data()->dr7
& 0xff) )
1875 /* (possible) hardware breakpoint, fetch the debug registers */
1876 DWORD saved_flags
= context
->ContextFlags
;
1877 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1878 NtGetContextThread(GetCurrentThread(), context
);
1879 context
->ContextFlags
|= saved_flags
; /* restore flags */
1882 context
->EFlags
&= ~0x100; /* clear single-step flag */
1885 status
= NtRaiseException( rec
, context
, TRUE
);
1886 raise_status( status
, rec
);
1890 /**********************************************************************
1891 * raise_generic_exception
1893 * Generic raise function for exceptions that don't need special treatment.
1895 static void WINAPI
raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1899 status
= NtRaiseException( rec
, context
, TRUE
);
1900 raise_status( status
, rec
);
1905 /**********************************************************************
1906 * raise_vm86_sti_exception
1908 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1910 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1911 get_vm86_teb_info()->vm86_pending
|= VIP_FLAG
;
1913 if (ntdll_get_thread_data()->vm86_ptr
)
1915 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
1916 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
1917 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
1918 /* exiting from VM86, can't throw */
1921 merge_vm86_pending_flags( rec
);
1923 else if (get_vm86_teb_info()->dpmi_vif
&&
1924 !wine_ldt_is_system(context
->SegCs
) &&
1925 !wine_ldt_is_system(context
->SegSs
))
1927 /* Executing DPMI code and virtual interrupts are enabled. */
1928 get_vm86_teb_info()->vm86_pending
= 0;
1929 NtRaiseException( rec
, context
, TRUE
);
1932 set_cpu_context( context
);
1936 /**********************************************************************
1939 * Handler for SIGUSR2.
1940 * We use it to signal that the running __wine_enter_vm86() should
1941 * immediately set VIP_FLAG, causing pending events to be handled
1942 * as early as possible.
1944 static void usr2_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1946 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_vm86_sti_exception
);
1947 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
1949 #endif /* __HAVE_VM86 */
1952 /**********************************************************************
1955 * Handler for SIGSEGV and related errors.
1957 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1960 EXCEPTION_RECORD
*rec
;
1961 ucontext_t
*context
= sigcontext
;
1962 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1964 /* check for page fault inside the thread stack */
1965 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
1966 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
1967 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
1968 virtual_handle_stack_fault( siginfo
->si_addr
))
1970 /* check if this was the last guard page */
1971 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
1973 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1974 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1979 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1980 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
1982 switch(get_trap_code(context
))
1984 case TRAP_x86_OFLOW
: /* Overflow exception */
1985 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1987 case TRAP_x86_BOUND
: /* Bound range exception */
1988 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1990 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1991 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1993 case TRAP_x86_STKFLT
: /* Stack fault */
1994 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1996 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1997 case TRAP_x86_PROTFLT
: /* General protection fault */
1998 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2000 WORD err
= get_error_code(context
);
2001 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( get_exception_context(rec
) ))) break;
2002 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2003 rec
->NumberParameters
= 2;
2004 rec
->ExceptionInformation
[0] = 0;
2005 /* if error contains a LDT selector, use that as fault address */
2006 if ((err
& 7) == 4 && !wine_ldt_is_system( err
| 7 ))
2007 rec
->ExceptionInformation
[1] = err
& ~7;
2009 rec
->ExceptionInformation
[1] = 0xffffffff;
2012 case TRAP_x86_PAGEFLT
: /* Page fault */
2013 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2014 rec
->NumberParameters
= 2;
2015 rec
->ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
2016 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
2018 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
2019 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
2022 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2024 case TRAP_x86_NMI
: /* NMI interrupt */
2025 case TRAP_x86_DNA
: /* Device not available exception */
2026 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
2027 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
2028 case TRAP_x86_MCHK
: /* Machine check exception */
2029 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
2030 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2036 /**********************************************************************
2039 * Handler for SIGTRAP.
2041 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2043 ucontext_t
*context
= sigcontext
;
2044 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_trap_exception
);
2046 switch(get_trap_code(context
))
2048 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2049 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2051 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2052 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
2055 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
2061 /**********************************************************************
2064 * Handler for SIGFPE.
2066 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2068 CONTEXT
*win_context
;
2069 ucontext_t
*context
= sigcontext
;
2070 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_generic_exception
);
2072 win_context
= get_exception_context( rec
);
2074 switch(get_trap_code(context
))
2076 case TRAP_x86_DIVIDE
: /* Division by zero exception */
2077 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
2079 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
2080 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2082 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
2083 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2084 rec
->ExceptionCode
= get_fpu_code( win_context
);
2085 rec
->ExceptionAddress
= (LPVOID
)win_context
->FloatSave
.ErrorOffset
;
2087 case TRAP_x86_CACHEFLT
: /* SIMD exception */
2089 * Behaviour only tested for divide-by-zero exceptions
2090 * Check for other SIMD exceptions as well */
2091 if(siginfo
->si_code
!= FPE_FLTDIV
)
2092 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2095 rec
->ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
2096 rec
->NumberParameters
= 1;
2097 /* no idea what meaning is actually behind this but that's what native does */
2098 rec
->ExceptionInformation
[0] = 0;
2101 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2102 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2108 /**********************************************************************
2111 * Handler for SIGINT.
2113 * FIXME: should not be calling external functions on the signal stack.
2115 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2118 init_handler( sigcontext
, &fs
, &gs
);
2119 if (!dispatch_signal(SIGINT
))
2121 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2122 rec
->ExceptionCode
= CONTROL_C_EXIT
;
2126 /**********************************************************************
2129 * Handler for SIGABRT.
2131 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2133 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2134 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2135 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
2139 /**********************************************************************
2142 * Handler for SIGQUIT.
2144 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2147 init_handler( sigcontext
, &fs
, &gs
);
2152 /**********************************************************************
2155 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2157 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2162 init_handler( sigcontext
, &fs
, &gs
);
2163 save_context( &context
, sigcontext
, fs
, gs
);
2164 wait_suspend( &context
);
2165 restore_context( &context
, sigcontext
);
2169 /***********************************************************************
2170 * __wine_set_signal_handler (NTDLL.@)
2172 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2174 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
2175 if (handlers
[sig
] != NULL
) return -2;
2176 handlers
[sig
] = wsh
;
2181 /***********************************************************************
2182 * locking for LDT routines
2184 static RTL_CRITICAL_SECTION ldt_section
;
2185 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
2188 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
2189 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
2191 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
2192 static sigset_t ldt_sigset
;
2194 static void ldt_lock(void)
2198 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
2199 RtlEnterCriticalSection( &ldt_section
);
2200 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
2203 static void ldt_unlock(void)
2205 if (ldt_section
.RecursionCount
== 1)
2207 sigset_t sigset
= ldt_sigset
;
2208 RtlLeaveCriticalSection( &ldt_section
);
2209 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
2211 else RtlLeaveCriticalSection( &ldt_section
);
2215 /**********************************************************************
2216 * signal_alloc_thread
2218 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2220 static size_t sigstack_zero_bits
;
2221 struct ntdll_thread_data
*thread_data
;
2222 struct ntdll_thread_data
*parent_data
= NULL
;
2227 if (!sigstack_zero_bits
)
2229 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2230 /* find the first power of two not smaller than min_size */
2231 sigstack_zero_bits
= 12;
2232 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
2233 signal_stack_mask
= (1 << sigstack_zero_bits
) - 1;
2234 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
2236 else parent_data
= ntdll_get_thread_data();
2238 size
= signal_stack_mask
+ 1;
2239 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
2240 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
2243 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2244 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2245 thread_data
= (struct ntdll_thread_data
*)(*teb
)->SpareBytes1
;
2246 if (!(thread_data
->fs
= wine_ldt_alloc_fs()))
2249 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
2250 status
= STATUS_TOO_MANY_THREADS
;
2254 /* inherit debug registers from parent thread */
2255 thread_data
->dr0
= parent_data
->dr0
;
2256 thread_data
->dr1
= parent_data
->dr1
;
2257 thread_data
->dr2
= parent_data
->dr2
;
2258 thread_data
->dr3
= parent_data
->dr3
;
2259 thread_data
->dr6
= parent_data
->dr6
;
2260 thread_data
->dr7
= parent_data
->dr7
;
2268 /**********************************************************************
2269 * signal_free_thread
2271 void signal_free_thread( TEB
*teb
)
2274 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2276 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
2277 if (teb
->DeallocationStack
)
2280 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
2283 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2287 /**********************************************************************
2288 * signal_init_thread
2290 void signal_init_thread( TEB
*teb
)
2292 const WORD fpu_cw
= 0x27f;
2293 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2298 int mib
[2], val
= 1;
2301 mib
[1] = KERN_THALTSTACK
;
2302 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
2305 ss
.ss_sp
= (char *)teb
+ teb_size
;
2306 ss
.ss_size
= signal_stack_size
;
2308 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2310 wine_ldt_set_base( &fs_entry
, teb
);
2311 wine_ldt_set_limit( &fs_entry
, teb_size
- 1 );
2312 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
2313 wine_ldt_init_fs( thread_data
->fs
, &fs_entry
);
2314 thread_data
->gs
= wine_get_gs();
2317 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2319 FIXME("FPU setup not implemented for this platform.\n");
2323 /**********************************************************************
2324 * signal_init_process
2326 void signal_init_process(void)
2328 struct sigaction sig_act
;
2330 sig_act
.sa_mask
= server_block_set
;
2331 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
2333 sig_act
.sa_flags
|= SA_ONSTACK
;
2336 sig_act
.sa_flags
|= SA_RESTORER
;
2337 sig_act
.sa_restorer
= rt_sigreturn
;
2339 sig_act
.sa_sigaction
= int_handler
;
2340 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2341 sig_act
.sa_sigaction
= fpe_handler
;
2342 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2343 sig_act
.sa_sigaction
= abrt_handler
;
2344 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2345 sig_act
.sa_sigaction
= quit_handler
;
2346 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2347 sig_act
.sa_sigaction
= usr1_handler
;
2348 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2350 sig_act
.sa_sigaction
= segv_handler
;
2351 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2352 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2354 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2358 sig_act
.sa_sigaction
= trap_handler
;
2359 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2363 sig_act
.sa_sigaction
= usr2_handler
;
2364 if (sigaction( SIGUSR2
, &sig_act
, NULL
) == -1) goto error
;
2367 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
2371 perror("sigaction");
2377 /**********************************************************************
2378 * __wine_enter_vm86 (NTDLL.@)
2380 * Enter vm86 mode with the specified register context.
2382 void __wine_enter_vm86( CONTEXT
*context
)
2384 EXCEPTION_RECORD rec
;
2386 struct vm86plus_struct vm86
;
2388 memset( &vm86
, 0, sizeof(vm86
) );
2391 restore_vm86_context( context
, &vm86
);
2393 ntdll_get_thread_data()->vm86_ptr
= &vm86
;
2394 merge_vm86_pending_flags( &rec
);
2396 res
= vm86_enter( &ntdll_get_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
2403 save_vm86_context( context
, &vm86
);
2405 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2406 rec
.ExceptionRecord
= NULL
;
2407 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
2408 rec
.NumberParameters
= 0;
2410 switch(VM86_TYPE(res
))
2412 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
2413 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
2415 case VM86_TRAP
: /* return due to DOS-debugger request */
2416 switch(VM86_ARG(res
))
2418 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2419 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2421 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2422 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
2425 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2429 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
2430 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
2431 rec
.NumberParameters
= 1;
2432 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
2434 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
2435 context
->EFlags
|= VIF_FLAG
;
2436 context
->EFlags
&= ~VIP_FLAG
;
2437 get_vm86_teb_info()->vm86_pending
= 0;
2438 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
2440 case VM86_PICRETURN
: /* return due to pending PIC request */
2441 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
2443 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
2445 WINE_ERR( "unhandled result from vm86 mode %x\n", res
);
2448 raise_exception( &rec
, context
, TRUE
);
2452 #else /* __HAVE_VM86 */
2453 /**********************************************************************
2454 * __wine_enter_vm86 (NTDLL.@)
2456 void __wine_enter_vm86( CONTEXT
*context
)
2458 MESSAGE("vm86 mode not supported on this platform\n");
2460 #endif /* __HAVE_VM86 */
2463 /*******************************************************************
2464 * RtlUnwind (NTDLL.@)
2466 void WINAPI
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
2467 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
2469 EXCEPTION_RECORD record
;
2470 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
2473 context
->Eax
= (DWORD
)retval
;
2475 /* build an exception record, if we do not have one */
2478 record
.ExceptionCode
= STATUS_UNWIND
;
2479 record
.ExceptionFlags
= 0;
2480 record
.ExceptionRecord
= NULL
;
2481 record
.ExceptionAddress
= (void *)context
->Eip
;
2482 record
.NumberParameters
= 0;
2486 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
2488 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2490 /* get chain of exception frames */
2491 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2492 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
2494 /* Check frame address */
2495 if (pEndFrame
&& (frame
> pEndFrame
))
2496 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
2498 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
2501 TRACE( "calling handler at %p code=%x flags=%x\n",
2502 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2503 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
2504 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
2508 case ExceptionContinueSearch
:
2510 case ExceptionCollidedUnwind
:
2514 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
2517 frame
= __wine_pop_frame( frame
);
2520 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 4 )
2523 /*******************************************************************
2524 * NtRaiseException (NTDLL.@)
2526 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2528 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
2529 if (status
== STATUS_SUCCESS
)
2531 set_debug_registers( context
);
2532 set_cpu_context( context
);
2538 /***********************************************************************
2539 * RtlRaiseException (NTDLL.@)
2541 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2545 rec
->ExceptionAddress
= (void *)context
->Eip
;
2546 status
= raise_exception( rec
, context
, TRUE
);
2547 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
2549 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 1 )
2552 /*************************************************************************
2553 * RtlCaptureStackBackTrace (NTDLL.@)
2555 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
2561 RtlCaptureContext( &context
);
2562 if (hash
) *hash
= 0;
2563 frame
= (ULONG
*)context
.Ebp
;
2567 if (!is_valid_frame( frame
)) return 0;
2568 frame
= (ULONG
*)*frame
;
2571 for (i
= 0; i
< count
; i
++)
2573 if (!is_valid_frame( frame
)) break;
2574 buffer
[i
] = (void *)frame
[1];
2575 if (hash
) *hash
+= frame
[1];
2576 frame
= (ULONG
*)*frame
;
2582 extern void DECLSPEC_NORETURN
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
);
2583 __ASM_GLOBAL_FUNC( call_thread_entry_point
,
2585 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2586 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2587 "movl %esp,%ebp\n\t"
2588 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2590 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2592 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2594 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2596 "pushl 12(%ebp)\n\t"
2598 "call " __ASM_NAME("call_thread_func") );
2600 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int), void *frame
);
2601 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
2602 "movl 4(%esp),%eax\n\t"
2603 "movl 8(%esp),%ecx\n\t"
2604 "movl 12(%esp),%ebp\n\t"
2605 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2606 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2607 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2608 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2609 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2610 "leal -20(%ebp),%esp\n\t"
2614 /* wrapper for apps that don't declare the thread function correctly */
2615 extern void DECLSPEC_NORETURN
call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry
, void *arg
);
2616 __ASM_GLOBAL_FUNC(call_thread_func_wrapper
,
2618 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2619 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2620 "movl %esp,%ebp\n\t"
2621 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2623 "pushl 12(%ebp)\n\t"
2625 "leal -4(%ebp),%esp\n\t"
2627 "call " __ASM_NAME("exit_thread") "\n\t"
2630 /***********************************************************************
2633 void call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
, void *frame
)
2635 ntdll_get_thread_data()->exit_frame
= frame
;
2638 call_thread_func_wrapper( entry
, arg
);
2640 __EXCEPT(unhandled_exception_filter
)
2642 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2645 abort(); /* should not be reached */
2648 /***********************************************************************
2649 * RtlExitUserThread (NTDLL.@)
2651 void WINAPI
RtlExitUserThread( ULONG status
)
2653 if (!ntdll_get_thread_data()->exit_frame
) exit_thread( status
);
2654 call_thread_exit_func( status
, exit_thread
, ntdll_get_thread_data()->exit_frame
);
2657 /***********************************************************************
2660 void abort_thread( int status
)
2662 if (!ntdll_get_thread_data()->exit_frame
) terminate_thread( status
);
2663 call_thread_exit_func( status
, terminate_thread
, ntdll_get_thread_data()->exit_frame
);
2666 /**********************************************************************
2667 * DbgBreakPoint (NTDLL.@)
2669 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
2671 /**********************************************************************
2672 * DbgUserBreakPoint (NTDLL.@)
2674 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
2676 /**********************************************************************
2677 * NtCurrentTeb (NTDLL.@)
2679 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2682 /**************************************************************************
2685 __ASM_STDCALL_FUNC( _chkstk
, 0,
2687 "addl %esp,%eax\n\t"
2688 "xchgl %esp,%eax\n\t"
2689 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2690 "movl %eax,0(%esp)\n\t"
2693 /**************************************************************************
2694 * _alloca_probe (NTDLL.@)
2696 __ASM_STDCALL_FUNC( _alloca_probe
, 0,
2698 "addl %esp,%eax\n\t"
2699 "xchgl %esp,%eax\n\t"
2700 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2701 "movl %eax,0(%esp)\n\t"
2705 /**********************************************************************
2706 * EXC_CallHandler (internal)
2708 * Some exception handlers depend on EBP to have a fixed position relative to
2709 * the exception frame.
2710 * Shrinker depends on (*1) doing what it does,
2711 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2712 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2713 * function calling the handler having only 5 parameters (*4).
2715 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
2717 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2718 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2719 "movl %esp,%ebp\n\t"
2720 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2722 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2723 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2724 "pushl 24(%ebp)\n\t"
2725 "pushl 20(%ebp)\n\t"
2726 "pushl 16(%ebp)\n\t"
2727 "pushl 12(%ebp)\n\t"
2729 "call " __ASM_NAME("call_exception_handler") "\n\t"
2731 __ASM_CFI(".cfi_same_value %ebx\n\t")
2733 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2734 __ASM_CFI(".cfi_same_value %ebp\n\t")
2736 __ASM_GLOBAL_FUNC(call_exception_handler
,
2738 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2739 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2740 "movl %esp,%ebp\n\t"
2741 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2743 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2744 "pushl %edx\n\t" /* handler be handled by... */
2746 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2748 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2749 "pushl 20(%ebp)\n\t"
2750 "pushl 16(%ebp)\n\t"
2751 "pushl 12(%ebp)\n\t"
2753 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2754 "call *%ecx\n\t" /* call handler. (*2) */
2756 "movl (0), %esp\n\t" /* restore previous... (*3) */
2758 "popl (0)\n\t" /* exception frame. */
2759 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2761 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2762 __ASM_CFI(".cfi_same_value %ebp\n\t")
2763 "ret $20" ) /* (*4) */
2765 #endif /* __i386__ */