2 * i386 signal handling routines
4 * Copyright 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
31 #include <sys/types.h>
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
51 #ifdef HAVE_SYS_SIGNAL_H
52 # include <sys/signal.h>
54 #ifdef HAVE_SYS_SYSCTL_H
55 # include <sys/sysctl.h>
59 #define WIN32_NO_STATUS
61 #include "wine/library.h"
62 #include "ntdll_misc.h"
63 #include "wine/exception.h"
64 #include "wine/debug.h"
66 #ifdef HAVE_VALGRIND_MEMCHECK_H
67 #include <valgrind/memcheck.h>
70 #undef ERR /* Solaris needs to define this */
72 /* not defined for x86, so copy the x86_64 definition */
73 typedef struct DECLSPEC_ALIGN(16) _M128A
94 M128A FloatRegisters
[8];
95 M128A XmmRegisters
[16];
99 /***********************************************************************
100 * signal context platform-specific definitions
105 typedef ucontext_t SIGCONTEXT
;
107 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
108 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
109 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
110 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
111 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
112 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
113 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
114 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
116 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
117 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
118 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
119 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
120 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
121 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
123 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
124 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
125 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
126 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
128 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
129 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
131 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
132 #define VIF_FLAG 0x00080000
133 #define VIP_FLAG 0x00100000
135 int vm86_enter( void **vm86_ptr
);
136 void vm86_return(void);
137 void vm86_return_end(void);
138 __ASM_GLOBAL_FUNC(vm86_enter
,
140 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
141 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
143 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
145 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
146 "movl $166,%eax\n\t" /*SYS_vm86*/
147 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
148 "movl (%ecx),%ecx\n\t"
149 "movl $1,%ebx\n\t" /*VM86_ENTER*/
150 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
154 ".globl " __ASM_NAME("vm86_return") "\n\t"
155 __ASM_FUNC("vm86_return") "\n"
156 __ASM_NAME("vm86_return") ":\n\t"
161 __ASM_CFI(".cfi_same_value %ebx\n\t")
163 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
164 __ASM_CFI(".cfi_same_value %ebp\n\t")
165 "testl %eax,%eax\n\t"
167 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
168 "je " __ASM_NAME("vm86_enter") "\n\t"
170 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
172 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
173 __ASM_FUNC("vm86_return_end") "\n"
174 __ASM_NAME("vm86_return_end") ":\n\t"
177 #ifdef HAVE_SYS_VM86_H
185 #include <machine/frame.h>
186 typedef struct trapframe SIGCONTEXT
;
188 #define EAX_sig(context) ((context)->tf_eax)
189 #define EBX_sig(context) ((context)->tf_ebx)
190 #define ECX_sig(context) ((context)->tf_ecx)
191 #define EDX_sig(context) ((context)->tf_edx)
192 #define ESI_sig(context) ((context)->tf_esi)
193 #define EDI_sig(context) ((context)->tf_edi)
194 #define EBP_sig(context) ((context)->tf_ebp)
196 #define CS_sig(context) ((context)->tf_cs)
197 #define DS_sig(context) ((context)->tf_ds)
198 #define ES_sig(context) ((context)->tf_es)
199 #define SS_sig(context) ((context)->tf_ss)
201 #define EFL_sig(context) ((context)->tf_eflags)
203 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
204 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
206 #define FPU_sig(context) NULL /* FIXME */
207 #define FPUX_sig(context) NULL /* FIXME */
211 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
213 #include <machine/trap.h>
215 typedef struct sigcontext SIGCONTEXT
;
217 #define EAX_sig(context) ((context)->sc_eax)
218 #define EBX_sig(context) ((context)->sc_ebx)
219 #define ECX_sig(context) ((context)->sc_ecx)
220 #define EDX_sig(context) ((context)->sc_edx)
221 #define ESI_sig(context) ((context)->sc_esi)
222 #define EDI_sig(context) ((context)->sc_edi)
223 #define EBP_sig(context) ((context)->sc_ebp)
225 #define CS_sig(context) ((context)->sc_cs)
226 #define DS_sig(context) ((context)->sc_ds)
227 #define ES_sig(context) ((context)->sc_es)
228 #define FS_sig(context) ((context)->sc_fs)
229 #define GS_sig(context) ((context)->sc_gs)
230 #define SS_sig(context) ((context)->sc_ss)
232 #define TRAP_sig(context) ((context)->sc_trapno)
233 #define ERROR_sig(context) ((context)->sc_err)
234 #define EFL_sig(context) ((context)->sc_eflags)
236 #define EIP_sig(context) ((context)->sc_eip)
237 #define ESP_sig(context) ((context)->sc_esp)
239 #define FPU_sig(context) NULL /* FIXME */
240 #define FPUX_sig(context) NULL /* FIXME */
242 #endif /* __FreeBSD__ */
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 #define T_MCHK T_MACHK
274 #define T_XMMFLT T_XFTRAP
276 #endif /* __OpenBSD__ */
278 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
281 #include <sys/regset.h>
283 #include <sys/ucontext.h>
284 typedef struct ucontext SIGCONTEXT
;
290 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
291 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
292 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
293 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
294 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
295 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
296 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
298 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
299 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
300 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
301 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
303 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
304 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
306 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
308 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
310 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
312 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
314 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
317 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
320 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
323 #define FPU_sig(context) NULL /* FIXME */
324 #define FPUX_sig(context) NULL /* FIXME */
326 #endif /* svr4 || SCO_DS */
329 # include <sys/ucontext.h>
331 typedef ucontext_t SIGCONTEXT
;
333 /* work around silly renaming of struct members in OS X 10.5 */
334 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
335 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
336 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
337 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
338 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
339 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
340 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
341 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
342 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
343 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
344 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
345 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
346 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
347 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
348 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
349 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
350 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
351 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
352 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
353 #define FPU_sig(context) NULL
354 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
356 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
357 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
358 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
359 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
360 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
361 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
362 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
363 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
364 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
365 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
366 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
367 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
368 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
369 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
370 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
371 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
372 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
373 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
374 #define FPU_sig(context) NULL
375 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
378 #endif /* __APPLE__ */
380 #if defined(__NetBSD__)
381 # include <sys/ucontext.h>
382 # include <sys/types.h>
385 typedef ucontext_t SIGCONTEXT
;
387 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
388 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
389 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
390 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
391 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
392 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
393 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
394 #define ESP_sig(context) _UC_MACHINE_SP(context)
396 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
397 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
398 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
399 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
400 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
401 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
403 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
404 #define EIP_sig(context) _UC_MACHINE_PC(context)
405 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
406 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
408 #define FPU_sig(context) NULL
409 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
412 #define T_XMMFLT T_XMM
414 #endif /* __NetBSD__ */
416 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
418 typedef int (*wine_signal_handler
)(unsigned int sig
);
420 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
421 static size_t signal_stack_mask
;
422 static size_t signal_stack_size
;
424 static wine_signal_handler handlers
[256];
426 static int fpux_support
; /* whether the CPU support extended fpu context */
428 extern void DECLSPEC_NORETURN
__wine_restore_regs( const CONTEXT
*context
);
432 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
433 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
434 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
435 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
436 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
437 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
438 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
439 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
440 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
441 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
442 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
443 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
444 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
445 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
446 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
447 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
448 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
449 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
450 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
451 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
452 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
454 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
455 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
456 TRAP_x86_NMI
= 2, /* NMI interrupt */
457 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
458 TRAP_x86_OFLOW
= 4, /* Overflow exception */
459 TRAP_x86_BOUND
= 5, /* Bound range exception */
460 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
461 TRAP_x86_DNA
= 7, /* Device not available exception */
462 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
463 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
464 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
465 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
466 TRAP_x86_STKFLT
= 12, /* Stack fault */
467 TRAP_x86_PROTFLT
= 13, /* General protection fault */
468 TRAP_x86_PAGEFLT
= 14, /* Page fault */
469 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
470 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
471 TRAP_x86_MCHK
= 18, /* Machine check exception */
472 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
473 otherwise Cache flush exception (via SIGSEV) */
477 /* Exception record for handling exceptions happening inside exception handlers */
480 EXCEPTION_REGISTRATION_RECORD frame
;
481 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
484 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
485 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
486 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
488 /***********************************************************************
491 static inline int dispatch_signal(unsigned int sig
)
493 if (handlers
[sig
] == NULL
) return 0;
494 return handlers
[sig
](sig
);
498 /***********************************************************************
501 * Get the trap code for a signal.
503 static inline enum i386_trap_code
get_trap_code( const SIGCONTEXT
*sigcontext
)
506 return TRAP_sig(sigcontext
);
508 return TRAP_x86_UNKNOWN
; /* unknown trap code */
512 /***********************************************************************
515 * Get the error code for a signal.
517 static inline WORD
get_error_code( const SIGCONTEXT
*sigcontext
)
520 return ERROR_sig(sigcontext
);
526 /***********************************************************************
529 * Get the base of the signal stack for the current thread.
531 static inline void *get_signal_stack(void)
533 return (char *)NtCurrentTeb() + 4096;
537 /***********************************************************************
540 * Get the current teb based on the stack pointer.
542 static inline TEB
*get_current_teb(void)
545 __asm__("movl %%esp,%0" : "=g" (esp
) );
546 return (TEB
*)(esp
& ~signal_stack_mask
);
550 /*******************************************************************
553 static inline BOOL
is_valid_frame( void *frame
)
555 if ((ULONG_PTR
)frame
& 3) return FALSE
;
556 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
557 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
560 /*******************************************************************
563 * Handler for exceptions happening inside a handler.
565 static DWORD
raise_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
566 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
568 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
569 return ExceptionContinueSearch
;
570 /* We shouldn't get here so we store faulty frame in dispatcher */
571 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
572 return ExceptionNestedException
;
576 /*******************************************************************
579 * Handler for exceptions happening inside an unwind handler.
581 static DWORD
unwind_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
582 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
584 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
585 return ExceptionContinueSearch
;
586 /* We shouldn't get here so we store faulty frame in dispatcher */
587 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
588 return ExceptionCollidedUnwind
;
592 /**********************************************************************
593 * call_stack_handlers
595 * Call the stack handlers chain.
597 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
599 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
602 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
604 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
606 /* Check frame address */
607 if (!is_valid_frame( frame
))
609 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
614 TRACE( "calling handler at %p code=%x flags=%x\n",
615 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
616 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, raise_handler
);
617 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
619 if (frame
== nested_frame
)
621 /* no longer nested */
623 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
628 case ExceptionContinueExecution
:
629 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
630 return STATUS_NONCONTINUABLE_EXCEPTION
;
631 case ExceptionContinueSearch
:
633 case ExceptionNestedException
:
634 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
635 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
638 return STATUS_INVALID_DISPOSITION
;
642 return STATUS_UNHANDLED_EXCEPTION
;
646 /*******************************************************************
649 * Implementation of NtRaiseException.
651 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
659 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
660 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
661 context
->Eip
, GetCurrentThreadId() );
662 for (c
= 0; c
< rec
->NumberParameters
; c
++)
663 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
664 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
666 if (rec
->ExceptionInformation
[1] >> 16)
667 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
668 rec
->ExceptionAddress
,
669 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
671 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
672 rec
->ExceptionAddress
,
673 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
677 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
678 context
->Eax
, context
->Ebx
, context
->Ecx
,
679 context
->Edx
, context
->Esi
, context
->Edi
);
680 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
681 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
682 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
684 status
= send_debug_event( rec
, TRUE
, context
);
685 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
686 return STATUS_SUCCESS
;
688 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
689 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
691 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
692 return STATUS_SUCCESS
;
694 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
698 /* last chance exception */
700 status
= send_debug_event( rec
, FALSE
, context
);
701 if (status
!= DBG_CONTINUE
)
703 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
704 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
705 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
706 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
708 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
709 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
710 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
712 return STATUS_SUCCESS
;
717 /***********************************************************************
720 * Set the register values from a vm86 structure.
722 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
724 context
->ContextFlags
= CONTEXT_FULL
;
725 context
->Eax
= vm86
->regs
.eax
;
726 context
->Ebx
= vm86
->regs
.ebx
;
727 context
->Ecx
= vm86
->regs
.ecx
;
728 context
->Edx
= vm86
->regs
.edx
;
729 context
->Esi
= vm86
->regs
.esi
;
730 context
->Edi
= vm86
->regs
.edi
;
731 context
->Esp
= vm86
->regs
.esp
;
732 context
->Ebp
= vm86
->regs
.ebp
;
733 context
->Eip
= vm86
->regs
.eip
;
734 context
->SegCs
= vm86
->regs
.cs
;
735 context
->SegDs
= vm86
->regs
.ds
;
736 context
->SegEs
= vm86
->regs
.es
;
737 context
->SegFs
= vm86
->regs
.fs
;
738 context
->SegGs
= vm86
->regs
.gs
;
739 context
->SegSs
= vm86
->regs
.ss
;
740 context
->EFlags
= vm86
->regs
.eflags
;
744 /***********************************************************************
745 * restore_vm86_context
747 * Build a vm86 structure from the register values.
749 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
751 vm86
->regs
.eax
= context
->Eax
;
752 vm86
->regs
.ebx
= context
->Ebx
;
753 vm86
->regs
.ecx
= context
->Ecx
;
754 vm86
->regs
.edx
= context
->Edx
;
755 vm86
->regs
.esi
= context
->Esi
;
756 vm86
->regs
.edi
= context
->Edi
;
757 vm86
->regs
.esp
= context
->Esp
;
758 vm86
->regs
.ebp
= context
->Ebp
;
759 vm86
->regs
.eip
= context
->Eip
;
760 vm86
->regs
.cs
= context
->SegCs
;
761 vm86
->regs
.ds
= context
->SegDs
;
762 vm86
->regs
.es
= context
->SegEs
;
763 vm86
->regs
.fs
= context
->SegFs
;
764 vm86
->regs
.gs
= context
->SegGs
;
765 vm86
->regs
.ss
= context
->SegSs
;
766 vm86
->regs
.eflags
= context
->EFlags
;
770 /**********************************************************************
771 * merge_vm86_pending_flags
773 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
774 * raises exception if there are pending events and VIF flag
775 * has been turned on.
777 * Called from __wine_enter_vm86 because vm86_enter
778 * doesn't check for pending events.
780 * Called from raise_vm86_sti_exception to check for
781 * pending events in a signal safe way.
783 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
785 BOOL check_pending
= TRUE
;
786 struct vm86plus_struct
*vm86
=
787 (struct vm86plus_struct
*)(ntdll_get_thread_data()->vm86_ptr
);
790 * In order to prevent a race when SIGUSR2 occurs while
791 * we are returning from exception handler, pending events
792 * will be rechecked after each raised exception.
794 while (check_pending
&& get_vm86_teb_info()->vm86_pending
)
796 check_pending
= FALSE
;
797 ntdll_get_thread_data()->vm86_ptr
= NULL
;
800 * If VIF is set, throw exception.
801 * Note that SIGUSR2 may turn VIF flag off so
802 * VIF check must occur only when TEB.vm86_ptr is NULL.
804 if (vm86
->regs
.eflags
& VIF_FLAG
)
807 save_vm86_context( &vcontext
, vm86
);
809 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
810 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
811 rec
->ExceptionRecord
= NULL
;
812 rec
->NumberParameters
= 0;
813 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
815 vcontext
.EFlags
&= ~VIP_FLAG
;
816 get_vm86_teb_info()->vm86_pending
= 0;
817 raise_exception( rec
, &vcontext
, TRUE
);
819 restore_vm86_context( &vcontext
, vm86
);
820 check_pending
= TRUE
;
823 ntdll_get_thread_data()->vm86_ptr
= vm86
;
827 * Merge VIP flags in a signal safe way. This requires
828 * that the following operation compiles into atomic
831 vm86
->regs
.eflags
|= get_vm86_teb_info()->vm86_pending
;
833 #endif /* __HAVE_VM86 */
838 /* We have to workaround two Solaris breakages:
839 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
841 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
842 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
845 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
846 __ASM_GLOBAL_FUNC( sigaction_syscall
,
847 "movl $0x62,%eax\n\t"
851 /* assume the same libc handler is used for all signals */
852 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
854 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
856 struct ntdll_thread_data
*thread_data
;
858 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
860 thread_data
= (struct ntdll_thread_data
*)get_current_teb()->SpareBytes1
;
861 wine_set_fs( thread_data
->fs
);
862 wine_set_gs( thread_data
->gs
);
864 libc_sigacthandler( signal
, siginfo
, sigcontext
);
867 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
869 struct sigaction real_act
;
871 if (sigaction( sig
, new, old
) == -1) return -1;
873 /* retrieve the real handler and flags with a direct syscall */
874 sigaction_syscall( sig
, NULL
, &real_act
);
875 libc_sigacthandler
= real_act
.sa_sigaction
;
876 real_act
.sa_sigaction
= wine_sigacthandler
;
877 sigaction_syscall( sig
, &real_act
, NULL
);
880 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
884 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
886 extern void clear_alignment_flag(void);
887 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
889 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
890 "andl $~0x40000,(%esp)\n\t"
892 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
896 /***********************************************************************
899 * Handler initialization when the full context is not needed.
900 * Return the stack pointer to use for pushing the exception data.
902 static inline void *init_handler( const SIGCONTEXT
*sigcontext
, WORD
*fs
, WORD
*gs
)
904 TEB
*teb
= get_current_teb();
906 clear_alignment_flag();
908 /* get %fs and %gs at time of the fault */
910 *fs
= LOWORD(FS_sig(sigcontext
));
915 *gs
= LOWORD(GS_sig(sigcontext
));
920 #ifndef __sun /* see above for Solaris handling */
922 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
923 wine_set_fs( thread_data
->fs
);
924 wine_set_gs( thread_data
->gs
);
928 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
929 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
932 * Win16 or DOS protected mode. Note that during switch
933 * from 16-bit mode to linear mode, CS may be set to system
934 * segment before FS is restored. Fortunately, in this case
935 * SS is still non-system segment. This is why both CS and SS
938 return teb
->WOW32Reserved
;
940 return (void *)(ESP_sig(sigcontext
) & ~3);
944 /***********************************************************************
947 * Save the thread FPU context.
949 static inline void save_fpu( CONTEXT
*context
)
952 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
953 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
958 /***********************************************************************
961 * Save the thread FPU extended context.
963 static inline void save_fpux( CONTEXT
*context
)
966 /* we have to enforce alignment by hand */
967 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
968 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
970 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
971 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
972 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
977 /***********************************************************************
980 * Restore the FPU context to a sigcontext.
982 static inline void restore_fpu( const CONTEXT
*context
)
984 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
985 /* reset the current interrupt status */
986 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
988 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
989 #endif /* __GNUC__ */
993 /***********************************************************************
996 * Restore the FPU extended context to a sigcontext.
998 static inline void restore_fpux( const CONTEXT
*context
)
1001 /* we have to enforce alignment by hand */
1002 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1003 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1005 memcpy( state
, context
->ExtendedRegisters
, sizeof(*state
) );
1006 /* reset the current interrupt status */
1007 state
->StatusWord
&= state
->ControlWord
| 0xff80;
1008 __asm__
__volatile__( "fxrstor %0" : : "m" (*state
) );
1013 /***********************************************************************
1016 * Build a standard FPU context from an extended one.
1018 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XMM_SAVE_AREA32
*fpux
)
1020 unsigned int i
, tag
, stack_top
;
1022 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
1023 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
1024 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
1025 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
1026 fpu
->DataOffset
= fpux
->DataOffset
;
1027 fpu
->DataSelector
= fpux
->DataSelector
;
1028 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
1030 stack_top
= (fpux
->StatusWord
>> 11) & 7;
1031 fpu
->TagWord
= 0xffff0000;
1032 for (i
= 0; i
< 8; i
++)
1034 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
1035 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
1038 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
1039 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
1041 tag
= 2; /* special */
1043 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
1045 if (reg
->Low
) tag
= 2; /* special */
1046 else tag
= 1; /* zero */
1050 if (reg
->Low
>> 63) tag
= 0; /* valid */
1051 else tag
= 2; /* special */
1054 fpu
->TagWord
|= tag
<< (2 * i
);
1059 /***********************************************************************
1062 * Build a context structure from the signal info.
1064 static inline void save_context( CONTEXT
*context
, const SIGCONTEXT
*sigcontext
, WORD fs
, WORD gs
)
1066 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1067 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1068 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1070 memset(context
, 0, sizeof(*context
));
1071 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
1072 context
->Eax
= EAX_sig(sigcontext
);
1073 context
->Ebx
= EBX_sig(sigcontext
);
1074 context
->Ecx
= ECX_sig(sigcontext
);
1075 context
->Edx
= EDX_sig(sigcontext
);
1076 context
->Esi
= ESI_sig(sigcontext
);
1077 context
->Edi
= EDI_sig(sigcontext
);
1078 context
->Ebp
= EBP_sig(sigcontext
);
1079 context
->EFlags
= EFL_sig(sigcontext
);
1080 context
->Eip
= EIP_sig(sigcontext
);
1081 context
->Esp
= ESP_sig(sigcontext
);
1082 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
1083 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
1084 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
1085 context
->SegFs
= fs
;
1086 context
->SegGs
= gs
;
1087 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
1088 context
->Dr0
= regs
->dr0
;
1089 context
->Dr1
= regs
->dr1
;
1090 context
->Dr2
= regs
->dr2
;
1091 context
->Dr3
= regs
->dr3
;
1092 context
->Dr6
= regs
->dr6
;
1093 context
->Dr7
= regs
->dr7
;
1097 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1098 context
->FloatSave
= *fpu
;
1102 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
1103 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
1105 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
1107 if (!fpu
&& !fpux
) save_fpu( context
);
1111 /***********************************************************************
1114 * Restore the signal info from the context.
1116 static inline void restore_context( const CONTEXT
*context
, SIGCONTEXT
*sigcontext
)
1118 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1119 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1120 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1122 regs
->dr0
= context
->Dr0
;
1123 regs
->dr1
= context
->Dr1
;
1124 regs
->dr2
= context
->Dr2
;
1125 regs
->dr3
= context
->Dr3
;
1126 regs
->dr6
= context
->Dr6
;
1127 regs
->dr7
= context
->Dr7
;
1128 EAX_sig(sigcontext
) = context
->Eax
;
1129 EBX_sig(sigcontext
) = context
->Ebx
;
1130 ECX_sig(sigcontext
) = context
->Ecx
;
1131 EDX_sig(sigcontext
) = context
->Edx
;
1132 ESI_sig(sigcontext
) = context
->Esi
;
1133 EDI_sig(sigcontext
) = context
->Edi
;
1134 EBP_sig(sigcontext
) = context
->Ebp
;
1135 EFL_sig(sigcontext
) = context
->EFlags
;
1136 EIP_sig(sigcontext
) = context
->Eip
;
1137 ESP_sig(sigcontext
) = context
->Esp
;
1138 CS_sig(sigcontext
) = context
->SegCs
;
1139 DS_sig(sigcontext
) = context
->SegDs
;
1140 ES_sig(sigcontext
) = context
->SegEs
;
1141 SS_sig(sigcontext
) = context
->SegSs
;
1143 GS_sig(sigcontext
) = context
->SegGs
;
1145 wine_set_gs( context
->SegGs
);
1148 FS_sig(sigcontext
) = context
->SegFs
;
1150 wine_set_fs( context
->SegFs
);
1153 if (fpu
) *fpu
= context
->FloatSave
;
1154 if (fpux
) memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
1155 if (!fpu
&& !fpux
) restore_fpu( context
);
1159 /***********************************************************************
1160 * RtlCaptureContext (NTDLL.@)
1162 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
1164 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1165 "movl 8(%esp),%eax\n\t" /* context */
1166 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1167 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1168 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1169 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1170 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1171 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1172 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1173 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1174 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1175 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1176 "movl %ebp,0xb4(%eax)\n\t" /* context->Ebp */
1177 "movl 4(%esp),%edx\n\t"
1178 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1179 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1181 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1182 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1183 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1184 "leal 8(%esp),%edx\n\t"
1185 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1186 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1187 "popl 0xb0(%eax)\n\t" /* context->Eax */
1188 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1192 /***********************************************************************
1195 * Set the new CPU context. Used by NtSetContextThread.
1197 void set_cpu_context( const CONTEXT
*context
)
1199 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1201 if ((flags
& CONTEXT_EXTENDED_REGISTERS
) && fpux_support
) restore_fpux( context
);
1202 else if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
1204 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1206 ntdll_get_thread_data()->dr0
= context
->Dr0
;
1207 ntdll_get_thread_data()->dr1
= context
->Dr1
;
1208 ntdll_get_thread_data()->dr2
= context
->Dr2
;
1209 ntdll_get_thread_data()->dr3
= context
->Dr3
;
1210 ntdll_get_thread_data()->dr6
= context
->Dr6
;
1211 ntdll_get_thread_data()->dr7
= context
->Dr7
;
1213 if (flags
& CONTEXT_FULL
)
1215 if (!(flags
& CONTEXT_CONTROL
))
1216 FIXME( "setting partial context (%x) not supported\n", flags
);
1217 else if (flags
& CONTEXT_SEGMENTS
)
1218 __wine_restore_regs( context
);
1221 CONTEXT newcontext
= *context
;
1222 newcontext
.SegDs
= wine_get_ds();
1223 newcontext
.SegEs
= wine_get_es();
1224 newcontext
.SegFs
= wine_get_fs();
1225 newcontext
.SegGs
= wine_get_gs();
1226 __wine_restore_regs( &newcontext
);
1232 /***********************************************************************
1233 * set_debug_registers
1235 static void set_debug_registers( const CONTEXT
*context
)
1237 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1238 context_t server_context
;
1240 if (!(flags
& CONTEXT_DEBUG_REGISTERS
)) return;
1241 if (ntdll_get_thread_data()->dr0
== context
->Dr0
&&
1242 ntdll_get_thread_data()->dr1
== context
->Dr1
&&
1243 ntdll_get_thread_data()->dr2
== context
->Dr2
&&
1244 ntdll_get_thread_data()->dr3
== context
->Dr3
&&
1245 ntdll_get_thread_data()->dr6
== context
->Dr6
&&
1246 ntdll_get_thread_data()->dr7
== context
->Dr7
) return;
1248 context_to_server( &server_context
, context
);
1250 SERVER_START_REQ( set_thread_context
)
1252 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
1254 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
1255 wine_server_call( req
);
1261 /***********************************************************************
1264 * Copy a register context according to the flags.
1266 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1268 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
1269 if (flags
& CONTEXT_INTEGER
)
1271 to
->Eax
= from
->Eax
;
1272 to
->Ebx
= from
->Ebx
;
1273 to
->Ecx
= from
->Ecx
;
1274 to
->Edx
= from
->Edx
;
1275 to
->Esi
= from
->Esi
;
1276 to
->Edi
= from
->Edi
;
1278 if (flags
& CONTEXT_CONTROL
)
1280 to
->Ebp
= from
->Ebp
;
1281 to
->Esp
= from
->Esp
;
1282 to
->Eip
= from
->Eip
;
1283 to
->SegCs
= from
->SegCs
;
1284 to
->SegSs
= from
->SegSs
;
1285 to
->EFlags
= from
->EFlags
;
1287 if (flags
& CONTEXT_SEGMENTS
)
1289 to
->SegDs
= from
->SegDs
;
1290 to
->SegEs
= from
->SegEs
;
1291 to
->SegFs
= from
->SegFs
;
1292 to
->SegGs
= from
->SegGs
;
1294 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1296 to
->Dr0
= from
->Dr0
;
1297 to
->Dr1
= from
->Dr1
;
1298 to
->Dr2
= from
->Dr2
;
1299 to
->Dr3
= from
->Dr3
;
1300 to
->Dr6
= from
->Dr6
;
1301 to
->Dr7
= from
->Dr7
;
1303 if (flags
& CONTEXT_FLOATING_POINT
)
1305 to
->FloatSave
= from
->FloatSave
;
1307 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1309 memcpy( to
->ExtendedRegisters
, from
->ExtendedRegisters
, sizeof(to
->ExtendedRegisters
) );
1314 /***********************************************************************
1317 * Convert a register context to the server format.
1319 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1321 DWORD flags
= from
->ContextFlags
& ~CONTEXT_i386
; /* get rid of CPU id */
1323 memset( to
, 0, sizeof(*to
) );
1326 if (flags
& CONTEXT_CONTROL
)
1328 to
->flags
|= SERVER_CTX_CONTROL
;
1329 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
1330 to
->ctl
.i386_regs
.esp
= from
->Esp
;
1331 to
->ctl
.i386_regs
.eip
= from
->Eip
;
1332 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
1333 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
1334 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
1336 if (flags
& CONTEXT_INTEGER
)
1338 to
->flags
|= SERVER_CTX_INTEGER
;
1339 to
->integer
.i386_regs
.eax
= from
->Eax
;
1340 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
1341 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
1342 to
->integer
.i386_regs
.edx
= from
->Edx
;
1343 to
->integer
.i386_regs
.esi
= from
->Esi
;
1344 to
->integer
.i386_regs
.edi
= from
->Edi
;
1346 if (flags
& CONTEXT_SEGMENTS
)
1348 to
->flags
|= SERVER_CTX_SEGMENTS
;
1349 to
->seg
.i386_regs
.ds
= from
->SegDs
;
1350 to
->seg
.i386_regs
.es
= from
->SegEs
;
1351 to
->seg
.i386_regs
.fs
= from
->SegFs
;
1352 to
->seg
.i386_regs
.gs
= from
->SegGs
;
1354 if (flags
& CONTEXT_FLOATING_POINT
)
1356 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1357 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
1358 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
1359 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
1360 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
1361 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
1362 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
1363 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
1364 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
1365 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
1367 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1369 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1370 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
1371 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
1372 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
1373 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
1374 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
1375 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
1377 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1379 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
1380 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
1382 return STATUS_SUCCESS
;
1386 /***********************************************************************
1387 * context_from_server
1389 * Convert a register context from the server format.
1391 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1393 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
1395 to
->ContextFlags
= CONTEXT_i386
;
1396 if (from
->flags
& SERVER_CTX_CONTROL
)
1398 to
->ContextFlags
|= CONTEXT_CONTROL
;
1399 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
1400 to
->Esp
= from
->ctl
.i386_regs
.esp
;
1401 to
->Eip
= from
->ctl
.i386_regs
.eip
;
1402 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
1403 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
1404 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
1406 if (from
->flags
& SERVER_CTX_INTEGER
)
1408 to
->ContextFlags
|= CONTEXT_INTEGER
;
1409 to
->Eax
= from
->integer
.i386_regs
.eax
;
1410 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
1411 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
1412 to
->Edx
= from
->integer
.i386_regs
.edx
;
1413 to
->Esi
= from
->integer
.i386_regs
.esi
;
1414 to
->Edi
= from
->integer
.i386_regs
.edi
;
1416 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1418 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1419 to
->SegDs
= from
->seg
.i386_regs
.ds
;
1420 to
->SegEs
= from
->seg
.i386_regs
.es
;
1421 to
->SegFs
= from
->seg
.i386_regs
.fs
;
1422 to
->SegGs
= from
->seg
.i386_regs
.gs
;
1424 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1426 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1427 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
1428 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
1429 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
1430 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
1431 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
1432 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
1433 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
1434 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
1435 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
1437 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1439 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1440 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
1441 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
1442 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
1443 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
1444 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
1445 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
1447 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
1449 to
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1450 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
1452 return STATUS_SUCCESS
;
1456 /***********************************************************************
1457 * is_privileged_instr
1459 * Check if the fault location is a privileged instruction.
1460 * Based on the instruction emulation code in dlls/kernel/instr.c.
1462 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1465 unsigned int prefix_count
= 0;
1467 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
1468 instr
= (BYTE
*)context
->Eip
;
1470 for (;;) switch(*instr
)
1472 /* instruction prefixes */
1473 case 0x2e: /* %cs: */
1474 case 0x36: /* %ss: */
1475 case 0x3e: /* %ds: */
1476 case 0x26: /* %es: */
1477 case 0x64: /* %fs: */
1478 case 0x65: /* %gs: */
1479 case 0x66: /* opcode size */
1480 case 0x67: /* addr size */
1481 case 0xf0: /* lock */
1482 case 0xf2: /* repne */
1483 case 0xf3: /* repe */
1484 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1488 case 0x0f: /* extended instruction */
1491 case 0x20: /* mov crX, reg */
1492 case 0x21: /* mov drX, reg */
1493 case 0x22: /* mov reg, crX */
1494 case 0x23: /* mov reg drX */
1495 return EXCEPTION_PRIV_INSTRUCTION
;
1498 case 0x6c: /* insb (%dx) */
1499 case 0x6d: /* insl (%dx) */
1500 case 0x6e: /* outsb (%dx) */
1501 case 0x6f: /* outsl (%dx) */
1502 case 0xcd: /* int $xx */
1503 case 0xe4: /* inb al,XX */
1504 case 0xe5: /* in (e)ax,XX */
1505 case 0xe6: /* outb XX,al */
1506 case 0xe7: /* out XX,(e)ax */
1507 case 0xec: /* inb (%dx),%al */
1508 case 0xed: /* inl (%dx),%eax */
1509 case 0xee: /* outb %al,(%dx) */
1510 case 0xef: /* outl %eax,(%dx) */
1511 case 0xf4: /* hlt */
1512 case 0xfa: /* cli */
1513 case 0xfb: /* sti */
1514 return EXCEPTION_PRIV_INSTRUCTION
;
1521 #include "pshpack1.h"
1524 DWORD movl
; /* movl this,4(%esp) */
1526 BYTE jmp
; /* jmp func */
1529 #include "poppack.h"
1531 /**********************************************************************
1534 * Check if code destination is an ATL thunk, and emulate it if so.
1536 static BOOL
check_atl_thunk( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1538 const struct atl_thunk
*thunk
= (const struct atl_thunk
*)rec
->ExceptionInformation
[1];
1543 if (thunk
->movl
== 0x042444c7 && thunk
->jmp
== 0xe9)
1545 *((DWORD
*)context
->Esp
+ 1) = thunk
->this;
1546 context
->Eip
= (DWORD_PTR
)(&thunk
->func
+ 1) + thunk
->func
;
1547 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1548 thunk
, context
->Eip
, *((DWORD
*)context
->Esp
+ 1) );
1561 /***********************************************************************
1562 * setup_exception_record
1564 * Setup the exception record and context on the thread stack.
1566 static EXCEPTION_RECORD
*setup_exception_record( SIGCONTEXT
*sigcontext
, void *stack_ptr
,
1567 WORD fs
, WORD gs
, raise_func func
)
1571 void *ret_addr
; /* return address from raise_func */
1572 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
1573 CONTEXT
*context_ptr
; /* second arg for raise_func */
1575 EXCEPTION_RECORD rec
;
1578 } *stack
= stack_ptr
;
1579 DWORD exception_code
= 0;
1581 /* stack sanity checks */
1583 if ((char *)stack
>= (char *)get_signal_stack() &&
1584 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1586 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1587 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1588 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1589 NtCurrentTeb()->Tib
.StackBase
);
1593 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1594 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1595 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1597 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1598 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1599 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1600 NtCurrentTeb()->Tib
.StackBase
);
1602 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1604 /* stack overflow on last page, unrecoverable */
1605 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1606 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1607 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1608 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1609 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1612 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1614 /* stack access below stack limit, may be recoverable */
1615 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1618 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1619 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1620 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1621 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1622 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1627 stack
--; /* push the stack_layout structure */
1628 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1629 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1630 #elif defined(VALGRIND_MAKE_WRITABLE)
1631 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1633 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
1634 stack
->rec_ptr
= &stack
->rec
;
1635 stack
->context_ptr
= &stack
->context
;
1637 stack
->rec
.ExceptionRecord
= NULL
;
1638 stack
->rec
.ExceptionCode
= exception_code
;
1639 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1640 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
1641 stack
->rec
.NumberParameters
= 0;
1643 save_context( &stack
->context
, sigcontext
, fs
, gs
);
1645 /* now modify the sigcontext to return to the raise function */
1646 ESP_sig(sigcontext
) = (DWORD
)stack
;
1647 EIP_sig(sigcontext
) = (DWORD
)func
;
1648 /* clear single-step, direction, and align check flag */
1649 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1650 CS_sig(sigcontext
) = wine_get_cs();
1651 DS_sig(sigcontext
) = wine_get_ds();
1652 ES_sig(sigcontext
) = wine_get_es();
1653 FS_sig(sigcontext
) = wine_get_fs();
1654 GS_sig(sigcontext
) = wine_get_gs();
1655 SS_sig(sigcontext
) = wine_get_ss();
1657 return stack
->rec_ptr
;
1661 /***********************************************************************
1664 * Setup a proper stack frame for the raise function, and modify the
1665 * sigcontext so that the return from the signal handler will call
1666 * the raise function.
1668 static EXCEPTION_RECORD
*setup_exception( SIGCONTEXT
*sigcontext
, raise_func func
)
1671 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1673 return setup_exception_record( sigcontext
, stack
, fs
, gs
, func
);
1677 /***********************************************************************
1678 * get_exception_context
1680 * Get a pointer to the context built by setup_exception.
1682 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
1684 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
1688 /**********************************************************************
1691 * Get the FPU exception code from the FPU status.
1693 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1695 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1697 if (status
& 0x01) /* IE */
1699 if (status
& 0x40) /* SF */
1700 return EXCEPTION_FLT_STACK_CHECK
;
1702 return EXCEPTION_FLT_INVALID_OPERATION
;
1704 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1705 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1706 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1707 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1708 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1709 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1713 /**********************************************************************
1714 * raise_segv_exception
1716 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1720 switch(rec
->ExceptionCode
)
1722 case EXCEPTION_ACCESS_VIOLATION
:
1723 if (rec
->NumberParameters
== 2)
1725 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
&& check_atl_thunk( rec
, context
))
1727 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
1728 rec
->ExceptionInformation
[0] )))
1730 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1731 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
1734 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
1735 &flags
, sizeof(flags
), NULL
);
1736 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
))
1737 rec
->ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
1741 case EXCEPTION_DATATYPE_MISALIGNMENT
:
1742 /* FIXME: pass through exception handler first? */
1743 if (context
->EFlags
& 0x00040000)
1745 /* Disable AC flag, return */
1746 context
->EFlags
&= ~0x00040000;
1751 status
= NtRaiseException( rec
, context
, TRUE
);
1752 raise_status( status
, rec
);
1754 set_cpu_context( context
);
1758 /**********************************************************************
1759 * raise_trap_exception
1761 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1765 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
1767 /* when single stepping can't tell whether this is a hw bp or a
1768 * single step interrupt. try to avoid as much overhead as possible
1769 * and only do a server call if there is any hw bp enabled. */
1771 if( !(context
->EFlags
& 0x100) || (ntdll_get_thread_data()->dr7
& 0xff) )
1773 /* (possible) hardware breakpoint, fetch the debug registers */
1774 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1775 NtGetContextThread(GetCurrentThread(), context
);
1776 context
->ContextFlags
|= CONTEXT_FULL
; /* restore flags */
1779 context
->EFlags
&= ~0x100; /* clear single-step flag */
1782 status
= NtRaiseException( rec
, context
, TRUE
);
1783 raise_status( status
, rec
);
1787 /**********************************************************************
1788 * raise_generic_exception
1790 * Generic raise function for exceptions that don't need special treatment.
1792 static void WINAPI
raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1796 status
= NtRaiseException( rec
, context
, TRUE
);
1797 raise_status( status
, rec
);
1802 /**********************************************************************
1803 * raise_vm86_sti_exception
1805 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1807 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1808 get_vm86_teb_info()->vm86_pending
|= VIP_FLAG
;
1810 if (ntdll_get_thread_data()->vm86_ptr
)
1812 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
1813 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
1814 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
1815 /* exiting from VM86, can't throw */
1818 merge_vm86_pending_flags( rec
);
1820 else if (get_vm86_teb_info()->dpmi_vif
&&
1821 !wine_ldt_is_system(context
->SegCs
) &&
1822 !wine_ldt_is_system(context
->SegSs
))
1824 /* Executing DPMI code and virtual interrupts are enabled. */
1825 get_vm86_teb_info()->vm86_pending
= 0;
1826 NtRaiseException( rec
, context
, TRUE
);
1829 set_cpu_context( context
);
1833 /**********************************************************************
1836 * Handler for SIGUSR2.
1837 * We use it to signal that the running __wine_enter_vm86() should
1838 * immediately set VIP_FLAG, causing pending events to be handled
1839 * as early as possible.
1841 static void usr2_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1843 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_vm86_sti_exception
);
1844 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
1846 #endif /* __HAVE_VM86 */
1849 /**********************************************************************
1852 * Handler for SIGSEGV and related errors.
1854 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1857 EXCEPTION_RECORD
*rec
;
1858 SIGCONTEXT
*context
= sigcontext
;
1859 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1861 /* check for page fault inside the thread stack */
1862 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
1863 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
1864 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
1865 virtual_handle_stack_fault( siginfo
->si_addr
))
1867 /* check if this was the last guard page */
1868 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
1870 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1871 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1876 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1877 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
1879 switch(get_trap_code(context
))
1881 case TRAP_x86_OFLOW
: /* Overflow exception */
1882 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1884 case TRAP_x86_BOUND
: /* Bound range exception */
1885 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1887 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1888 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1890 case TRAP_x86_STKFLT
: /* Stack fault */
1891 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1893 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1894 case TRAP_x86_PROTFLT
: /* General protection fault */
1895 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1897 WORD err
= get_error_code(context
);
1898 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( get_exception_context(rec
) ))) break;
1899 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1900 rec
->NumberParameters
= 2;
1901 rec
->ExceptionInformation
[0] = 0;
1902 /* if error contains a LDT selector, use that as fault address */
1903 if ((err
& 7) == 4 && !wine_ldt_is_system( err
| 7 ))
1904 rec
->ExceptionInformation
[1] = err
& ~7;
1906 rec
->ExceptionInformation
[1] = 0xffffffff;
1909 case TRAP_x86_PAGEFLT
: /* Page fault */
1910 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1911 rec
->NumberParameters
= 2;
1912 rec
->ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
1913 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
1915 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
1916 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1919 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
1921 case TRAP_x86_NMI
: /* NMI interrupt */
1922 case TRAP_x86_DNA
: /* Device not available exception */
1923 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
1924 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
1925 case TRAP_x86_MCHK
: /* Machine check exception */
1926 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
1927 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1933 /**********************************************************************
1936 * Handler for SIGTRAP.
1938 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1940 SIGCONTEXT
*context
= sigcontext
;
1941 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_trap_exception
);
1943 switch(get_trap_code(context
))
1945 case TRAP_x86_TRCTRAP
: /* Single-step exception */
1946 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1948 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
1949 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
1952 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1958 /**********************************************************************
1961 * Handler for SIGFPE.
1963 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1965 CONTEXT
*win_context
;
1966 SIGCONTEXT
*context
= sigcontext
;
1967 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_generic_exception
);
1969 win_context
= get_exception_context( rec
);
1971 switch(get_trap_code(context
))
1973 case TRAP_x86_DIVIDE
: /* Division by zero exception */
1974 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1976 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
1977 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1979 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
1980 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1981 rec
->ExceptionCode
= get_fpu_code( win_context
);
1982 rec
->ExceptionAddress
= (LPVOID
)win_context
->FloatSave
.ErrorOffset
;
1984 case TRAP_x86_CACHEFLT
: /* SIMD exception */
1986 * Behaviour only tested for divide-by-zero exceptions
1987 * Check for other SIMD exceptions as well */
1988 if(siginfo
->si_code
!= FPE_FLTDIV
)
1989 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1992 rec
->ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
1993 rec
->NumberParameters
= 1;
1994 /* no idea what meaning is actually behind this but that's what native does */
1995 rec
->ExceptionInformation
[0] = 0;
1998 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
1999 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2005 /**********************************************************************
2008 * Handler for SIGINT.
2010 * FIXME: should not be calling external functions on the signal stack.
2012 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2015 init_handler( sigcontext
, &fs
, &gs
);
2016 if (!dispatch_signal(SIGINT
))
2018 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2019 rec
->ExceptionCode
= CONTROL_C_EXIT
;
2023 /**********************************************************************
2026 * Handler for SIGABRT.
2028 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2030 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2031 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2032 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
2036 /**********************************************************************
2039 * Handler for SIGQUIT.
2041 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2044 init_handler( sigcontext
, &fs
, &gs
);
2049 /**********************************************************************
2052 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2054 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2059 init_handler( sigcontext
, &fs
, &gs
);
2060 save_context( &context
, sigcontext
, fs
, gs
);
2061 wait_suspend( &context
);
2062 restore_context( &context
, sigcontext
);
2066 /***********************************************************************
2067 * __wine_set_signal_handler (NTDLL.@)
2069 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2071 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
2072 if (handlers
[sig
] != NULL
) return -2;
2073 handlers
[sig
] = wsh
;
2078 /***********************************************************************
2079 * locking for LDT routines
2081 static RTL_CRITICAL_SECTION ldt_section
;
2082 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
2085 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
2086 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
2088 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
2089 static sigset_t ldt_sigset
;
2091 static void ldt_lock(void)
2095 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
2096 RtlEnterCriticalSection( &ldt_section
);
2097 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
2100 static void ldt_unlock(void)
2102 if (ldt_section
.RecursionCount
== 1)
2104 sigset_t sigset
= ldt_sigset
;
2105 RtlLeaveCriticalSection( &ldt_section
);
2106 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
2108 else RtlLeaveCriticalSection( &ldt_section
);
2112 /**********************************************************************
2113 * signal_alloc_thread
2115 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2117 static size_t sigstack_zero_bits
;
2118 struct ntdll_thread_data
*thread_data
;
2119 struct ntdll_thread_data
*parent_data
= NULL
;
2124 if (!sigstack_zero_bits
)
2126 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2127 /* find the first power of two not smaller than min_size */
2128 sigstack_zero_bits
= 12;
2129 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
2130 signal_stack_mask
= (1 << sigstack_zero_bits
) - 1;
2131 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
2133 else parent_data
= ntdll_get_thread_data();
2135 size
= signal_stack_mask
+ 1;
2136 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
2137 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
2140 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2141 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2142 thread_data
= (struct ntdll_thread_data
*)(*teb
)->SpareBytes1
;
2143 if (!(thread_data
->fs
= wine_ldt_alloc_fs()))
2146 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
2147 status
= STATUS_TOO_MANY_THREADS
;
2151 /* inherit debug registers from parent thread */
2152 thread_data
->dr0
= parent_data
->dr0
;
2153 thread_data
->dr1
= parent_data
->dr1
;
2154 thread_data
->dr2
= parent_data
->dr2
;
2155 thread_data
->dr3
= parent_data
->dr3
;
2156 thread_data
->dr6
= parent_data
->dr6
;
2157 thread_data
->dr7
= parent_data
->dr7
;
2165 /**********************************************************************
2166 * signal_free_thread
2168 void signal_free_thread( TEB
*teb
)
2171 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2173 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
2174 if (teb
->DeallocationStack
)
2177 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
2180 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2184 /**********************************************************************
2185 * signal_init_thread
2187 void signal_init_thread( TEB
*teb
)
2189 const WORD fpu_cw
= 0x27f;
2190 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2195 int mib
[2], val
= 1;
2198 mib
[1] = KERN_THALTSTACK
;
2199 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
2202 ss
.ss_sp
= (char *)teb
+ teb_size
;
2203 ss
.ss_size
= signal_stack_size
;
2205 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2207 wine_ldt_set_base( &fs_entry
, teb
);
2208 wine_ldt_set_limit( &fs_entry
, teb_size
- 1 );
2209 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
2210 wine_ldt_init_fs( thread_data
->fs
, &fs_entry
);
2211 thread_data
->gs
= wine_get_gs();
2214 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2216 FIXME("FPU setup not implemented for this platform.\n");
2220 /**********************************************************************
2221 * signal_init_process
2223 void signal_init_process(void)
2225 struct sigaction sig_act
;
2227 sig_act
.sa_mask
= server_block_set
;
2228 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
2230 sig_act
.sa_flags
|= SA_ONSTACK
;
2233 sig_act
.sa_sigaction
= int_handler
;
2234 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2235 sig_act
.sa_sigaction
= fpe_handler
;
2236 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2237 sig_act
.sa_sigaction
= abrt_handler
;
2238 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2239 sig_act
.sa_sigaction
= quit_handler
;
2240 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2241 sig_act
.sa_sigaction
= usr1_handler
;
2242 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2244 sig_act
.sa_sigaction
= segv_handler
;
2245 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2246 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2248 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2252 sig_act
.sa_sigaction
= trap_handler
;
2253 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2257 sig_act
.sa_sigaction
= usr2_handler
;
2258 if (sigaction( SIGUSR2
, &sig_act
, NULL
) == -1) goto error
;
2261 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
2265 perror("sigaction");
2271 /**********************************************************************
2272 * __wine_enter_vm86 (NTDLL.@)
2274 * Enter vm86 mode with the specified register context.
2276 void __wine_enter_vm86( CONTEXT
*context
)
2278 EXCEPTION_RECORD rec
;
2280 struct vm86plus_struct vm86
;
2282 memset( &vm86
, 0, sizeof(vm86
) );
2285 restore_vm86_context( context
, &vm86
);
2287 ntdll_get_thread_data()->vm86_ptr
= &vm86
;
2288 merge_vm86_pending_flags( &rec
);
2290 res
= vm86_enter( &ntdll_get_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
2297 save_vm86_context( context
, &vm86
);
2299 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2300 rec
.ExceptionRecord
= NULL
;
2301 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
2302 rec
.NumberParameters
= 0;
2304 switch(VM86_TYPE(res
))
2306 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
2307 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
2309 case VM86_TRAP
: /* return due to DOS-debugger request */
2310 switch(VM86_ARG(res
))
2312 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2313 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2315 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2316 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
2319 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2323 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
2324 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
2325 rec
.NumberParameters
= 1;
2326 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
2328 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
2329 context
->EFlags
|= VIF_FLAG
;
2330 context
->EFlags
&= ~VIP_FLAG
;
2331 get_vm86_teb_info()->vm86_pending
= 0;
2332 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
2334 case VM86_PICRETURN
: /* return due to pending PIC request */
2335 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
2337 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
2339 WINE_ERR( "unhandled result from vm86 mode %x\n", res
);
2342 raise_exception( &rec
, context
, TRUE
);
2346 #else /* __HAVE_VM86 */
2347 /**********************************************************************
2348 * __wine_enter_vm86 (NTDLL.@)
2350 void __wine_enter_vm86( CONTEXT
*context
)
2352 MESSAGE("vm86 mode not supported on this platform\n");
2354 #endif /* __HAVE_VM86 */
2357 /*******************************************************************
2358 * RtlUnwind (NTDLL.@)
2360 void WINAPI
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
2361 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
2363 EXCEPTION_RECORD record
;
2364 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
2367 context
->Eax
= (DWORD
)retval
;
2369 /* build an exception record, if we do not have one */
2372 record
.ExceptionCode
= STATUS_UNWIND
;
2373 record
.ExceptionFlags
= 0;
2374 record
.ExceptionRecord
= NULL
;
2375 record
.ExceptionAddress
= (void *)context
->Eip
;
2376 record
.NumberParameters
= 0;
2380 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
2382 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2384 /* get chain of exception frames */
2385 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2386 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
2388 /* Check frame address */
2389 if (pEndFrame
&& (frame
> pEndFrame
))
2390 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
2392 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
2395 TRACE( "calling handler at %p code=%x flags=%x\n",
2396 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2397 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
2398 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
2402 case ExceptionContinueSearch
:
2404 case ExceptionCollidedUnwind
:
2408 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
2411 frame
= __wine_pop_frame( frame
);
2414 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 4 )
2417 /*******************************************************************
2418 * NtRaiseException (NTDLL.@)
2420 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2422 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
2423 if (status
== STATUS_SUCCESS
)
2425 set_debug_registers( context
);
2426 set_cpu_context( context
);
2432 /***********************************************************************
2433 * RtlRaiseException (NTDLL.@)
2435 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2439 rec
->ExceptionAddress
= (void *)context
->Eip
;
2440 status
= raise_exception( rec
, context
, TRUE
);
2441 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
2443 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 1 )
2446 /*************************************************************************
2447 * RtlCaptureStackBackTrace (NTDLL.@)
2449 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
2455 RtlCaptureContext( &context
);
2456 if (hash
) *hash
= 0;
2457 frame
= (ULONG
*)context
.Ebp
;
2461 if (!is_valid_frame( frame
)) return 0;
2462 frame
= (ULONG
*)*frame
;
2465 for (i
= 0; i
< count
; i
++)
2467 if (!is_valid_frame( frame
)) break;
2468 buffer
[i
] = (void *)frame
[1];
2469 if (hash
) *hash
+= frame
[1];
2470 frame
= (ULONG
*)*frame
;
2476 /* wrapper for apps that don't declare the thread function correctly */
2477 extern void DECLSPEC_NORETURN
call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
);
2478 __ASM_GLOBAL_FUNC(call_thread_func
,
2480 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2481 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2482 "movl %esp,%ebp\n\t"
2483 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2485 "pushl 12(%ebp)\n\t"
2487 "leal -4(%ebp),%esp\n\t"
2489 "call " __ASM_NAME("exit_thread") "\n\t"
2492 /***********************************************************************
2493 * call_thread_entry_point
2495 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
2499 call_thread_func( entry
, arg
);
2501 __EXCEPT(unhandled_exception_filter
)
2503 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2506 abort(); /* should not be reached */
2509 /***********************************************************************
2510 * RtlExitUserThread (NTDLL.@)
2512 void WINAPI
RtlExitUserThread( ULONG status
)
2514 exit_thread( status
);
2517 /***********************************************************************
2520 void abort_thread( int status
)
2522 terminate_thread( status
);
2525 /**********************************************************************
2526 * DbgBreakPoint (NTDLL.@)
2528 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
2530 /**********************************************************************
2531 * DbgUserBreakPoint (NTDLL.@)
2533 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
2535 /**********************************************************************
2536 * NtCurrentTeb (NTDLL.@)
2538 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2541 /**********************************************************************
2542 * EXC_CallHandler (internal)
2544 * Some exception handlers depend on EBP to have a fixed position relative to
2545 * the exception frame.
2546 * Shrinker depends on (*1) doing what it does,
2547 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2548 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2549 * function calling the handler having only 5 parameters (*4).
2551 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
2553 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2554 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2555 "movl %esp,%ebp\n\t"
2556 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2558 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2559 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2560 "pushl 24(%ebp)\n\t"
2561 "pushl 20(%ebp)\n\t"
2562 "pushl 16(%ebp)\n\t"
2563 "pushl 12(%ebp)\n\t"
2565 "call " __ASM_NAME("call_exception_handler") "\n\t"
2567 __ASM_CFI(".cfi_same_value %ebx\n\t")
2569 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2570 __ASM_CFI(".cfi_same_value %ebp\n\t")
2572 __ASM_GLOBAL_FUNC(call_exception_handler
,
2574 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2575 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2576 "movl %esp,%ebp\n\t"
2577 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2579 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2580 "pushl %edx\n\t" /* handler be handled by... */
2582 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2584 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2585 "pushl 20(%ebp)\n\t"
2586 "pushl 16(%ebp)\n\t"
2587 "pushl 12(%ebp)\n\t"
2589 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2590 "call *%ecx\n\t" /* call handler. (*2) */
2592 "movl (0), %esp\n\t" /* restore previous... (*3) */
2594 "popl (0)\n\t" /* exception frame. */
2595 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2597 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2598 __ASM_CFI(".cfi_same_value %ebp\n\t")
2599 "ret $20" ) /* (*4) */
2601 #endif /* __i386__ */