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
106 typedef struct ucontext
108 unsigned long uc_flags
;
109 struct ucontext
*uc_link
;
111 struct sigcontext uc_mcontext
;
116 #define EAX_sig(context) ((context)->uc_mcontext.eax)
117 #define EBX_sig(context) ((context)->uc_mcontext.ebx)
118 #define ECX_sig(context) ((context)->uc_mcontext.ecx)
119 #define EDX_sig(context) ((context)->uc_mcontext.edx)
120 #define ESI_sig(context) ((context)->uc_mcontext.esi)
121 #define EDI_sig(context) ((context)->uc_mcontext.edi)
122 #define EBP_sig(context) ((context)->uc_mcontext.ebp)
123 #define ESP_sig(context) ((context)->uc_mcontext.esp)
125 #define CS_sig(context) ((context)->uc_mcontext.cs)
126 #define DS_sig(context) ((context)->uc_mcontext.ds)
127 #define ES_sig(context) ((context)->uc_mcontext.es)
128 #define SS_sig(context) ((context)->uc_mcontext.ss)
129 #define FS_sig(context) ((context)->uc_mcontext.fs)
130 #define GS_sig(context) ((context)->uc_mcontext.gs)
132 #define EFL_sig(context) ((context)->uc_mcontext.eflags)
133 #define EIP_sig(context) ((context)->uc_mcontext.eip)
134 #define TRAP_sig(context) ((context)->uc_mcontext.trapno)
135 #define ERROR_sig(context) ((context)->uc_mcontext.err)
137 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpstate))
138 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpstate->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
140 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
141 void rt_sigreturn(void);
142 __ASM_GLOBAL_FUNC( rt_sigreturn
,
143 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
146 #elif defined (__linux__)
148 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
149 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
150 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
151 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
152 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
153 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
154 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
155 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
157 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
158 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
159 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
160 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
161 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
162 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
164 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
165 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
166 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
167 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
169 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
170 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
172 #define VM86_EAX 0 /* the %eax value while vm86_enter is executing */
173 #define VIF_FLAG 0x00080000
174 #define VIP_FLAG 0x00100000
176 int vm86_enter( void **vm86_ptr
);
177 void vm86_return(void);
178 void vm86_return_end(void);
179 __ASM_GLOBAL_FUNC(vm86_enter
,
181 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
182 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
184 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
186 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
187 "movl $166,%eax\n\t" /*SYS_vm86*/
188 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
189 "movl (%ecx),%ecx\n\t"
190 "movl $1,%ebx\n\t" /*VM86_ENTER*/
191 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
195 ".globl " __ASM_NAME("vm86_return") "\n\t"
196 __ASM_FUNC("vm86_return") "\n"
197 __ASM_NAME("vm86_return") ":\n\t"
202 __ASM_CFI(".cfi_same_value %ebx\n\t")
204 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
205 __ASM_CFI(".cfi_same_value %ebp\n\t")
206 "testl %eax,%eax\n\t"
208 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
209 "je " __ASM_NAME("vm86_enter") "\n\t"
211 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
213 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
214 __ASM_FUNC("vm86_return_end") "\n"
215 __ASM_NAME("vm86_return_end") ":\n\t"
218 #ifdef HAVE_SYS_VM86_H
222 #elif defined (__BSDI__)
224 #include <machine/frame.h>
225 typedef struct trapframe ucontext_t
;
227 #define EAX_sig(context) ((context)->tf_eax)
228 #define EBX_sig(context) ((context)->tf_ebx)
229 #define ECX_sig(context) ((context)->tf_ecx)
230 #define EDX_sig(context) ((context)->tf_edx)
231 #define ESI_sig(context) ((context)->tf_esi)
232 #define EDI_sig(context) ((context)->tf_edi)
233 #define EBP_sig(context) ((context)->tf_ebp)
235 #define CS_sig(context) ((context)->tf_cs)
236 #define DS_sig(context) ((context)->tf_ds)
237 #define ES_sig(context) ((context)->tf_es)
238 #define SS_sig(context) ((context)->tf_ss)
240 #define EFL_sig(context) ((context)->tf_eflags)
242 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
243 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
245 #define FPU_sig(context) NULL /* FIXME */
246 #define FPUX_sig(context) NULL /* FIXME */
248 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
250 #include <machine/trap.h>
252 #define EAX_sig(context) ((context)->sc_eax)
253 #define EBX_sig(context) ((context)->sc_ebx)
254 #define ECX_sig(context) ((context)->sc_ecx)
255 #define EDX_sig(context) ((context)->sc_edx)
256 #define ESI_sig(context) ((context)->sc_esi)
257 #define EDI_sig(context) ((context)->sc_edi)
258 #define EBP_sig(context) ((context)->sc_ebp)
260 #define CS_sig(context) ((context)->sc_cs)
261 #define DS_sig(context) ((context)->sc_ds)
262 #define ES_sig(context) ((context)->sc_es)
263 #define FS_sig(context) ((context)->sc_fs)
264 #define GS_sig(context) ((context)->sc_gs)
265 #define SS_sig(context) ((context)->sc_ss)
267 #define TRAP_sig(context) ((context)->sc_trapno)
268 #define ERROR_sig(context) ((context)->sc_err)
269 #define EFL_sig(context) ((context)->sc_eflags)
271 #define EIP_sig(context) ((context)->sc_eip)
272 #define ESP_sig(context) ((context)->sc_esp)
274 #define FPU_sig(context) NULL /* FIXME */
275 #define FPUX_sig(context) NULL /* FIXME */
277 #elif defined (__OpenBSD__)
279 #define EAX_sig(context) ((context)->sc_eax)
280 #define EBX_sig(context) ((context)->sc_ebx)
281 #define ECX_sig(context) ((context)->sc_ecx)
282 #define EDX_sig(context) ((context)->sc_edx)
283 #define ESI_sig(context) ((context)->sc_esi)
284 #define EDI_sig(context) ((context)->sc_edi)
285 #define EBP_sig(context) ((context)->sc_ebp)
287 #define CS_sig(context) ((context)->sc_cs)
288 #define DS_sig(context) ((context)->sc_ds)
289 #define ES_sig(context) ((context)->sc_es)
290 #define FS_sig(context) ((context)->sc_fs)
291 #define GS_sig(context) ((context)->sc_gs)
292 #define SS_sig(context) ((context)->sc_ss)
294 #define TRAP_sig(context) ((context)->sc_trapno)
295 #define ERROR_sig(context) ((context)->sc_err)
296 #define EFL_sig(context) ((context)->sc_eflags)
298 #define EIP_sig(context) ((context)->sc_eip)
299 #define ESP_sig(context) ((context)->sc_esp)
301 #define FPU_sig(context) NULL /* FIXME */
302 #define FPUX_sig(context) NULL /* FIXME */
304 #define T_MCHK T_MACHK
305 #define T_XMMFLT T_XFTRAP
307 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
310 #include <sys/regset.h>
314 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
315 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
316 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
317 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
318 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
319 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
320 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
322 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
323 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
324 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
325 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
327 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
328 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
330 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
332 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
334 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
336 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
338 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
341 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
344 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
347 #define FPU_sig(context) NULL /* FIXME */
348 #define FPUX_sig(context) NULL /* FIXME */
350 #elif defined (__APPLE__)
352 /* work around silly renaming of struct members in OS X 10.5 */
353 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
354 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
355 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
356 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
357 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
358 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
359 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
360 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
361 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
362 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
363 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
364 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
365 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
366 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
367 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
368 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
369 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
370 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
371 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
372 #define FPU_sig(context) NULL
373 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
375 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
376 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
377 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
378 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
379 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
380 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
381 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
382 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
383 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
384 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
385 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
386 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
387 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
388 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
389 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
390 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
391 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
392 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
393 #define FPU_sig(context) NULL
394 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
397 #elif defined(__NetBSD__)
399 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
400 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
401 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
402 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
403 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
404 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
405 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
406 #define ESP_sig(context) _UC_MACHINE_SP(context)
408 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
409 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
410 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
411 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
412 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
413 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
415 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
416 #define EIP_sig(context) _UC_MACHINE_PC(context)
417 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
418 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
420 #define FPU_sig(context) NULL
421 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
424 #define T_XMMFLT T_XMM
426 #elif defined(__GNU__)
428 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
429 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
430 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
431 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
432 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
433 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
434 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
435 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
437 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
438 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
439 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
440 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
441 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
442 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
444 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
445 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
446 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
447 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
449 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
450 #define FPUX_sig(context) NULL
453 #error You must define the signal context functions for your platform
456 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
458 typedef int (*wine_signal_handler
)(unsigned int sig
);
460 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
461 static size_t signal_stack_mask
;
462 static size_t signal_stack_size
;
464 static wine_signal_handler handlers
[256];
466 static BOOL fpux_support
; /* whether the CPU supports extended fpu context */
468 extern void DECLSPEC_NORETURN
__wine_restore_regs( const CONTEXT
*context
);
472 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
473 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
474 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
475 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
476 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
477 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
478 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
479 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
480 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
481 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
482 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
483 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
484 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
485 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
486 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
487 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
488 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
489 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
490 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
491 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
492 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
494 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
495 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
496 TRAP_x86_NMI
= 2, /* NMI interrupt */
497 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
498 TRAP_x86_OFLOW
= 4, /* Overflow exception */
499 TRAP_x86_BOUND
= 5, /* Bound range exception */
500 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
501 TRAP_x86_DNA
= 7, /* Device not available exception */
502 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
503 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
504 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
505 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
506 TRAP_x86_STKFLT
= 12, /* Stack fault */
507 TRAP_x86_PROTFLT
= 13, /* General protection fault */
508 TRAP_x86_PAGEFLT
= 14, /* Page fault */
509 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
510 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
511 TRAP_x86_MCHK
= 18, /* Machine check exception */
512 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
513 otherwise Cache flush exception (via SIGSEV) */
517 /* Exception record for handling exceptions happening inside exception handlers */
520 EXCEPTION_REGISTRATION_RECORD frame
;
521 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
524 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
525 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
526 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
528 /***********************************************************************
531 static inline int dispatch_signal(unsigned int sig
)
533 if (handlers
[sig
] == NULL
) return 0;
534 return handlers
[sig
](sig
);
538 /***********************************************************************
541 * Get the trap code for a signal.
543 static inline enum i386_trap_code
get_trap_code( const ucontext_t
*sigcontext
)
546 return TRAP_sig(sigcontext
);
548 return TRAP_x86_UNKNOWN
; /* unknown trap code */
552 /***********************************************************************
555 * Get the error code for a signal.
557 static inline WORD
get_error_code( const ucontext_t
*sigcontext
)
560 return ERROR_sig(sigcontext
);
566 /***********************************************************************
569 * Get the base of the signal stack for the current thread.
571 static inline void *get_signal_stack(void)
573 return (char *)NtCurrentTeb() + 4096;
577 /***********************************************************************
580 * Get the current teb based on the stack pointer.
582 static inline TEB
*get_current_teb(void)
585 __asm__("movl %%esp,%0" : "=g" (esp
) );
586 return (TEB
*)(esp
& ~signal_stack_mask
);
590 /*******************************************************************
593 static inline BOOL
is_valid_frame( void *frame
)
595 if ((ULONG_PTR
)frame
& 3) return FALSE
;
596 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
597 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
600 /*******************************************************************
603 * Handler for exceptions happening inside a handler.
605 static DWORD
raise_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
606 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
608 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
609 return ExceptionContinueSearch
;
610 /* We shouldn't get here so we store faulty frame in dispatcher */
611 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
612 return ExceptionNestedException
;
616 /*******************************************************************
619 * Handler for exceptions happening inside an unwind handler.
621 static DWORD
unwind_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 ExceptionCollidedUnwind
;
632 /**********************************************************************
633 * call_stack_handlers
635 * Call the stack handlers chain.
637 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
639 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
642 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
644 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
646 /* Check frame address */
647 if (!is_valid_frame( frame
))
649 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
654 TRACE( "calling handler at %p code=%x flags=%x\n",
655 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
656 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, raise_handler
);
657 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
659 if (frame
== nested_frame
)
661 /* no longer nested */
663 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
668 case ExceptionContinueExecution
:
669 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
670 return STATUS_NONCONTINUABLE_EXCEPTION
;
671 case ExceptionContinueSearch
:
673 case ExceptionNestedException
:
674 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
675 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
678 return STATUS_INVALID_DISPOSITION
;
682 return STATUS_UNHANDLED_EXCEPTION
;
686 /*******************************************************************
689 * Implementation of NtRaiseException.
691 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
699 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
700 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
701 context
->Eip
, GetCurrentThreadId() );
702 for (c
= 0; c
< rec
->NumberParameters
; c
++)
703 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
704 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
706 if (rec
->ExceptionInformation
[1] >> 16)
707 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
708 rec
->ExceptionAddress
,
709 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
711 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
712 rec
->ExceptionAddress
,
713 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
717 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
718 context
->Eax
, context
->Ebx
, context
->Ecx
,
719 context
->Edx
, context
->Esi
, context
->Edi
);
720 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
721 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
722 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
724 status
= send_debug_event( rec
, TRUE
, context
);
725 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
726 return STATUS_SUCCESS
;
728 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
729 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
731 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
732 return STATUS_SUCCESS
;
734 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
738 /* last chance exception */
740 status
= send_debug_event( rec
, FALSE
, context
);
741 if (status
!= DBG_CONTINUE
)
743 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
744 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
745 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
746 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
748 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
749 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
750 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
752 return STATUS_SUCCESS
;
757 /***********************************************************************
760 * Set the register values from a vm86 structure.
762 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
764 context
->ContextFlags
= CONTEXT_FULL
;
765 context
->Eax
= vm86
->regs
.eax
;
766 context
->Ebx
= vm86
->regs
.ebx
;
767 context
->Ecx
= vm86
->regs
.ecx
;
768 context
->Edx
= vm86
->regs
.edx
;
769 context
->Esi
= vm86
->regs
.esi
;
770 context
->Edi
= vm86
->regs
.edi
;
771 context
->Esp
= vm86
->regs
.esp
;
772 context
->Ebp
= vm86
->regs
.ebp
;
773 context
->Eip
= vm86
->regs
.eip
;
774 context
->SegCs
= vm86
->regs
.cs
;
775 context
->SegDs
= vm86
->regs
.ds
;
776 context
->SegEs
= vm86
->regs
.es
;
777 context
->SegFs
= vm86
->regs
.fs
;
778 context
->SegGs
= vm86
->regs
.gs
;
779 context
->SegSs
= vm86
->regs
.ss
;
780 context
->EFlags
= vm86
->regs
.eflags
;
784 /***********************************************************************
785 * restore_vm86_context
787 * Build a vm86 structure from the register values.
789 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
791 vm86
->regs
.eax
= context
->Eax
;
792 vm86
->regs
.ebx
= context
->Ebx
;
793 vm86
->regs
.ecx
= context
->Ecx
;
794 vm86
->regs
.edx
= context
->Edx
;
795 vm86
->regs
.esi
= context
->Esi
;
796 vm86
->regs
.edi
= context
->Edi
;
797 vm86
->regs
.esp
= context
->Esp
;
798 vm86
->regs
.ebp
= context
->Ebp
;
799 vm86
->regs
.eip
= context
->Eip
;
800 vm86
->regs
.cs
= context
->SegCs
;
801 vm86
->regs
.ds
= context
->SegDs
;
802 vm86
->regs
.es
= context
->SegEs
;
803 vm86
->regs
.fs
= context
->SegFs
;
804 vm86
->regs
.gs
= context
->SegGs
;
805 vm86
->regs
.ss
= context
->SegSs
;
806 vm86
->regs
.eflags
= context
->EFlags
;
810 /**********************************************************************
811 * merge_vm86_pending_flags
813 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
814 * raises exception if there are pending events and VIF flag
815 * has been turned on.
817 * Called from __wine_enter_vm86 because vm86_enter
818 * doesn't check for pending events.
820 * Called from raise_vm86_sti_exception to check for
821 * pending events in a signal safe way.
823 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
825 BOOL check_pending
= TRUE
;
826 struct vm86plus_struct
*vm86
=
827 (struct vm86plus_struct
*)(ntdll_get_thread_data()->vm86_ptr
);
830 * In order to prevent a race when SIGUSR2 occurs while
831 * we are returning from exception handler, pending events
832 * will be rechecked after each raised exception.
834 while (check_pending
&& get_vm86_teb_info()->vm86_pending
)
836 check_pending
= FALSE
;
837 ntdll_get_thread_data()->vm86_ptr
= NULL
;
840 * If VIF is set, throw exception.
841 * Note that SIGUSR2 may turn VIF flag off so
842 * VIF check must occur only when TEB.vm86_ptr is NULL.
844 if (vm86
->regs
.eflags
& VIF_FLAG
)
847 save_vm86_context( &vcontext
, vm86
);
849 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
850 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
851 rec
->ExceptionRecord
= NULL
;
852 rec
->NumberParameters
= 0;
853 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
855 vcontext
.EFlags
&= ~VIP_FLAG
;
856 get_vm86_teb_info()->vm86_pending
= 0;
857 raise_exception( rec
, &vcontext
, TRUE
);
859 restore_vm86_context( &vcontext
, vm86
);
860 check_pending
= TRUE
;
863 ntdll_get_thread_data()->vm86_ptr
= vm86
;
867 * Merge VIP flags in a signal safe way. This requires
868 * that the following operation compiles into atomic
871 vm86
->regs
.eflags
|= get_vm86_teb_info()->vm86_pending
;
873 #endif /* __HAVE_VM86 */
878 /* We have to workaround two Solaris breakages:
879 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
881 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
882 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
885 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
886 __ASM_GLOBAL_FUNC( sigaction_syscall
,
887 "movl $0x62,%eax\n\t"
891 /* assume the same libc handler is used for all signals */
892 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
894 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
896 struct ntdll_thread_data
*thread_data
;
898 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
900 thread_data
= (struct ntdll_thread_data
*)get_current_teb()->SpareBytes1
;
901 wine_set_fs( thread_data
->fs
);
902 wine_set_gs( thread_data
->gs
);
904 libc_sigacthandler( signal
, siginfo
, sigcontext
);
907 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
909 struct sigaction real_act
;
911 if (sigaction( sig
, new, old
) == -1) return -1;
913 /* retrieve the real handler and flags with a direct syscall */
914 sigaction_syscall( sig
, NULL
, &real_act
);
915 libc_sigacthandler
= real_act
.sa_sigaction
;
916 real_act
.sa_sigaction
= wine_sigacthandler
;
917 sigaction_syscall( sig
, &real_act
, NULL
);
920 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
924 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
926 extern void clear_alignment_flag(void);
927 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
929 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
930 "andl $~0x40000,(%esp)\n\t"
932 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
936 /***********************************************************************
939 * Handler initialization when the full context is not needed.
940 * Return the stack pointer to use for pushing the exception data.
942 static inline void *init_handler( const ucontext_t
*sigcontext
, WORD
*fs
, WORD
*gs
)
944 TEB
*teb
= get_current_teb();
946 clear_alignment_flag();
948 /* get %fs and %gs at time of the fault */
950 *fs
= LOWORD(FS_sig(sigcontext
));
955 *gs
= LOWORD(GS_sig(sigcontext
));
960 #ifndef __sun /* see above for Solaris handling */
962 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
963 wine_set_fs( thread_data
->fs
);
964 wine_set_gs( thread_data
->gs
);
968 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
969 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
972 * Win16 or DOS protected mode. Note that during switch
973 * from 16-bit mode to linear mode, CS may be set to system
974 * segment before FS is restored. Fortunately, in this case
975 * SS is still non-system segment. This is why both CS and SS
978 return teb
->WOW32Reserved
;
980 return (void *)(ESP_sig(sigcontext
) & ~3);
984 /***********************************************************************
987 * Save the thread FPU context.
989 static inline void save_fpu( CONTEXT
*context
)
992 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
993 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
998 /***********************************************************************
1001 * Save the thread FPU extended context.
1003 static inline void save_fpux( CONTEXT
*context
)
1006 /* we have to enforce alignment by hand */
1007 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1008 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1010 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
1011 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1012 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
1017 /***********************************************************************
1020 * Restore the FPU context to a sigcontext.
1022 static inline void restore_fpu( const CONTEXT
*context
)
1024 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
1025 /* reset the current interrupt status */
1026 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
1028 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
1029 #endif /* __GNUC__ */
1033 /***********************************************************************
1036 * Restore the FPU extended context to a sigcontext.
1038 static inline void restore_fpux( const CONTEXT
*context
)
1041 /* we have to enforce alignment by hand */
1042 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1043 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1045 memcpy( state
, context
->ExtendedRegisters
, sizeof(*state
) );
1046 /* reset the current interrupt status */
1047 state
->StatusWord
&= state
->ControlWord
| 0xff80;
1048 __asm__
__volatile__( "fxrstor %0" : : "m" (*state
) );
1053 /***********************************************************************
1056 * Build a standard FPU context from an extended one.
1058 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XMM_SAVE_AREA32
*fpux
)
1060 unsigned int i
, tag
, stack_top
;
1062 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
1063 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
1064 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
1065 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
1066 fpu
->DataOffset
= fpux
->DataOffset
;
1067 fpu
->DataSelector
= fpux
->DataSelector
;
1068 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
1070 stack_top
= (fpux
->StatusWord
>> 11) & 7;
1071 fpu
->TagWord
= 0xffff0000;
1072 for (i
= 0; i
< 8; i
++)
1074 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
1075 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
1078 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
1079 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
1081 tag
= 2; /* special */
1083 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
1085 if (reg
->Low
) tag
= 2; /* special */
1086 else tag
= 1; /* zero */
1090 if (reg
->Low
>> 63) tag
= 0; /* valid */
1091 else tag
= 2; /* special */
1094 fpu
->TagWord
|= tag
<< (2 * i
);
1099 /***********************************************************************
1102 * Build a context structure from the signal info.
1104 static inline void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
, WORD fs
, WORD gs
)
1106 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1107 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1108 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1110 memset(context
, 0, sizeof(*context
));
1111 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
1112 context
->Eax
= EAX_sig(sigcontext
);
1113 context
->Ebx
= EBX_sig(sigcontext
);
1114 context
->Ecx
= ECX_sig(sigcontext
);
1115 context
->Edx
= EDX_sig(sigcontext
);
1116 context
->Esi
= ESI_sig(sigcontext
);
1117 context
->Edi
= EDI_sig(sigcontext
);
1118 context
->Ebp
= EBP_sig(sigcontext
);
1119 context
->EFlags
= EFL_sig(sigcontext
);
1120 context
->Eip
= EIP_sig(sigcontext
);
1121 context
->Esp
= ESP_sig(sigcontext
);
1122 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
1123 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
1124 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
1125 context
->SegFs
= fs
;
1126 context
->SegGs
= gs
;
1127 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
1128 context
->Dr0
= regs
->dr0
;
1129 context
->Dr1
= regs
->dr1
;
1130 context
->Dr2
= regs
->dr2
;
1131 context
->Dr3
= regs
->dr3
;
1132 context
->Dr6
= regs
->dr6
;
1133 context
->Dr7
= regs
->dr7
;
1137 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1138 context
->FloatSave
= *fpu
;
1142 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
1143 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
1144 fpux_support
= TRUE
;
1145 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
1147 if (!fpu
&& !fpux
) save_fpu( context
);
1151 /***********************************************************************
1154 * Restore the signal info from the context.
1156 static inline void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1158 struct ntdll_thread_data
* const regs
= ntdll_get_thread_data();
1159 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1160 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1162 regs
->dr0
= context
->Dr0
;
1163 regs
->dr1
= context
->Dr1
;
1164 regs
->dr2
= context
->Dr2
;
1165 regs
->dr3
= context
->Dr3
;
1166 regs
->dr6
= context
->Dr6
;
1167 regs
->dr7
= context
->Dr7
;
1168 EAX_sig(sigcontext
) = context
->Eax
;
1169 EBX_sig(sigcontext
) = context
->Ebx
;
1170 ECX_sig(sigcontext
) = context
->Ecx
;
1171 EDX_sig(sigcontext
) = context
->Edx
;
1172 ESI_sig(sigcontext
) = context
->Esi
;
1173 EDI_sig(sigcontext
) = context
->Edi
;
1174 EBP_sig(sigcontext
) = context
->Ebp
;
1175 EFL_sig(sigcontext
) = context
->EFlags
;
1176 EIP_sig(sigcontext
) = context
->Eip
;
1177 ESP_sig(sigcontext
) = context
->Esp
;
1178 CS_sig(sigcontext
) = context
->SegCs
;
1179 DS_sig(sigcontext
) = context
->SegDs
;
1180 ES_sig(sigcontext
) = context
->SegEs
;
1181 SS_sig(sigcontext
) = context
->SegSs
;
1183 GS_sig(sigcontext
) = context
->SegGs
;
1185 wine_set_gs( context
->SegGs
);
1188 FS_sig(sigcontext
) = context
->SegFs
;
1190 wine_set_fs( context
->SegFs
);
1193 if (fpu
) *fpu
= context
->FloatSave
;
1194 if (fpux
) memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
1195 if (!fpu
&& !fpux
) restore_fpu( context
);
1199 /***********************************************************************
1200 * RtlCaptureContext (NTDLL.@)
1202 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
1204 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1205 "movl 8(%esp),%eax\n\t" /* context */
1206 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1207 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1208 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1209 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1210 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1211 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1212 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1213 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1214 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1215 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1216 "movl %ebp,0xb4(%eax)\n\t" /* context->Ebp */
1217 "movl 4(%esp),%edx\n\t"
1218 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1219 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1221 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1222 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1223 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1224 "leal 8(%esp),%edx\n\t"
1225 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1226 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1227 "popl 0xb0(%eax)\n\t" /* context->Eax */
1228 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1232 /***********************************************************************
1235 * Set the new CPU context. Used by NtSetContextThread.
1237 void set_cpu_context( const CONTEXT
*context
)
1239 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1241 if ((flags
& CONTEXT_EXTENDED_REGISTERS
) && fpux_support
) restore_fpux( context
);
1242 else if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
1244 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1246 ntdll_get_thread_data()->dr0
= context
->Dr0
;
1247 ntdll_get_thread_data()->dr1
= context
->Dr1
;
1248 ntdll_get_thread_data()->dr2
= context
->Dr2
;
1249 ntdll_get_thread_data()->dr3
= context
->Dr3
;
1250 ntdll_get_thread_data()->dr6
= context
->Dr6
;
1251 ntdll_get_thread_data()->dr7
= context
->Dr7
;
1253 if (flags
& CONTEXT_FULL
)
1255 if (!(flags
& CONTEXT_CONTROL
))
1256 FIXME( "setting partial context (%x) not supported\n", flags
);
1257 else if (flags
& CONTEXT_SEGMENTS
)
1258 __wine_restore_regs( context
);
1261 CONTEXT newcontext
= *context
;
1262 newcontext
.SegDs
= wine_get_ds();
1263 newcontext
.SegEs
= wine_get_es();
1264 newcontext
.SegFs
= wine_get_fs();
1265 newcontext
.SegGs
= wine_get_gs();
1266 __wine_restore_regs( &newcontext
);
1272 /***********************************************************************
1273 * set_debug_registers
1275 static void set_debug_registers( const CONTEXT
*context
)
1277 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1278 context_t server_context
;
1280 if (!(flags
& CONTEXT_DEBUG_REGISTERS
)) return;
1281 if (ntdll_get_thread_data()->dr0
== context
->Dr0
&&
1282 ntdll_get_thread_data()->dr1
== context
->Dr1
&&
1283 ntdll_get_thread_data()->dr2
== context
->Dr2
&&
1284 ntdll_get_thread_data()->dr3
== context
->Dr3
&&
1285 ntdll_get_thread_data()->dr6
== context
->Dr6
&&
1286 ntdll_get_thread_data()->dr7
== context
->Dr7
) return;
1288 context_to_server( &server_context
, context
);
1290 SERVER_START_REQ( set_thread_context
)
1292 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
1294 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
1295 wine_server_call( req
);
1301 /***********************************************************************
1304 * Copy a register context according to the flags.
1306 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1308 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
1309 if (flags
& CONTEXT_INTEGER
)
1311 to
->Eax
= from
->Eax
;
1312 to
->Ebx
= from
->Ebx
;
1313 to
->Ecx
= from
->Ecx
;
1314 to
->Edx
= from
->Edx
;
1315 to
->Esi
= from
->Esi
;
1316 to
->Edi
= from
->Edi
;
1318 if (flags
& CONTEXT_CONTROL
)
1320 to
->Ebp
= from
->Ebp
;
1321 to
->Esp
= from
->Esp
;
1322 to
->Eip
= from
->Eip
;
1323 to
->SegCs
= from
->SegCs
;
1324 to
->SegSs
= from
->SegSs
;
1325 to
->EFlags
= from
->EFlags
;
1327 if (flags
& CONTEXT_SEGMENTS
)
1329 to
->SegDs
= from
->SegDs
;
1330 to
->SegEs
= from
->SegEs
;
1331 to
->SegFs
= from
->SegFs
;
1332 to
->SegGs
= from
->SegGs
;
1334 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1336 to
->Dr0
= from
->Dr0
;
1337 to
->Dr1
= from
->Dr1
;
1338 to
->Dr2
= from
->Dr2
;
1339 to
->Dr3
= from
->Dr3
;
1340 to
->Dr6
= from
->Dr6
;
1341 to
->Dr7
= from
->Dr7
;
1343 if (flags
& CONTEXT_FLOATING_POINT
)
1345 to
->FloatSave
= from
->FloatSave
;
1347 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1349 memcpy( to
->ExtendedRegisters
, from
->ExtendedRegisters
, sizeof(to
->ExtendedRegisters
) );
1354 /***********************************************************************
1357 * Convert a register context to the server format.
1359 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1361 DWORD flags
= from
->ContextFlags
& ~CONTEXT_i386
; /* get rid of CPU id */
1363 memset( to
, 0, sizeof(*to
) );
1366 if (flags
& CONTEXT_CONTROL
)
1368 to
->flags
|= SERVER_CTX_CONTROL
;
1369 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
1370 to
->ctl
.i386_regs
.esp
= from
->Esp
;
1371 to
->ctl
.i386_regs
.eip
= from
->Eip
;
1372 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
1373 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
1374 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
1376 if (flags
& CONTEXT_INTEGER
)
1378 to
->flags
|= SERVER_CTX_INTEGER
;
1379 to
->integer
.i386_regs
.eax
= from
->Eax
;
1380 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
1381 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
1382 to
->integer
.i386_regs
.edx
= from
->Edx
;
1383 to
->integer
.i386_regs
.esi
= from
->Esi
;
1384 to
->integer
.i386_regs
.edi
= from
->Edi
;
1386 if (flags
& CONTEXT_SEGMENTS
)
1388 to
->flags
|= SERVER_CTX_SEGMENTS
;
1389 to
->seg
.i386_regs
.ds
= from
->SegDs
;
1390 to
->seg
.i386_regs
.es
= from
->SegEs
;
1391 to
->seg
.i386_regs
.fs
= from
->SegFs
;
1392 to
->seg
.i386_regs
.gs
= from
->SegGs
;
1394 if (flags
& CONTEXT_FLOATING_POINT
)
1396 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1397 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
1398 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
1399 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
1400 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
1401 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
1402 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
1403 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
1404 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
1405 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
1407 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1409 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1410 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
1411 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
1412 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
1413 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
1414 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
1415 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
1417 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1419 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
1420 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
1422 return STATUS_SUCCESS
;
1426 /***********************************************************************
1427 * context_from_server
1429 * Convert a register context from the server format.
1431 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1433 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
1435 to
->ContextFlags
= CONTEXT_i386
;
1436 if (from
->flags
& SERVER_CTX_CONTROL
)
1438 to
->ContextFlags
|= CONTEXT_CONTROL
;
1439 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
1440 to
->Esp
= from
->ctl
.i386_regs
.esp
;
1441 to
->Eip
= from
->ctl
.i386_regs
.eip
;
1442 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
1443 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
1444 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
1446 if (from
->flags
& SERVER_CTX_INTEGER
)
1448 to
->ContextFlags
|= CONTEXT_INTEGER
;
1449 to
->Eax
= from
->integer
.i386_regs
.eax
;
1450 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
1451 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
1452 to
->Edx
= from
->integer
.i386_regs
.edx
;
1453 to
->Esi
= from
->integer
.i386_regs
.esi
;
1454 to
->Edi
= from
->integer
.i386_regs
.edi
;
1456 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1458 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1459 to
->SegDs
= from
->seg
.i386_regs
.ds
;
1460 to
->SegEs
= from
->seg
.i386_regs
.es
;
1461 to
->SegFs
= from
->seg
.i386_regs
.fs
;
1462 to
->SegGs
= from
->seg
.i386_regs
.gs
;
1464 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1466 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1467 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
1468 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
1469 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
1470 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
1471 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
1472 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
1473 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
1474 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
1475 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
1477 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1479 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1480 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
1481 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
1482 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
1483 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
1484 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
1485 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
1487 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
1489 to
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1490 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
1492 return STATUS_SUCCESS
;
1496 /***********************************************************************
1497 * is_privileged_instr
1499 * Check if the fault location is a privileged instruction.
1500 * Based on the instruction emulation code in dlls/kernel/instr.c.
1502 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1505 unsigned int prefix_count
= 0;
1507 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
1508 instr
= (BYTE
*)context
->Eip
;
1510 for (;;) switch(*instr
)
1512 /* instruction prefixes */
1513 case 0x2e: /* %cs: */
1514 case 0x36: /* %ss: */
1515 case 0x3e: /* %ds: */
1516 case 0x26: /* %es: */
1517 case 0x64: /* %fs: */
1518 case 0x65: /* %gs: */
1519 case 0x66: /* opcode size */
1520 case 0x67: /* addr size */
1521 case 0xf0: /* lock */
1522 case 0xf2: /* repne */
1523 case 0xf3: /* repe */
1524 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1528 case 0x0f: /* extended instruction */
1531 case 0x20: /* mov crX, reg */
1532 case 0x21: /* mov drX, reg */
1533 case 0x22: /* mov reg, crX */
1534 case 0x23: /* mov reg drX */
1535 return EXCEPTION_PRIV_INSTRUCTION
;
1538 case 0x6c: /* insb (%dx) */
1539 case 0x6d: /* insl (%dx) */
1540 case 0x6e: /* outsb (%dx) */
1541 case 0x6f: /* outsl (%dx) */
1542 case 0xcd: /* int $xx */
1543 case 0xe4: /* inb al,XX */
1544 case 0xe5: /* in (e)ax,XX */
1545 case 0xe6: /* outb XX,al */
1546 case 0xe7: /* out XX,(e)ax */
1547 case 0xec: /* inb (%dx),%al */
1548 case 0xed: /* inl (%dx),%eax */
1549 case 0xee: /* outb %al,(%dx) */
1550 case 0xef: /* outl %eax,(%dx) */
1551 case 0xf4: /* hlt */
1552 case 0xfa: /* cli */
1553 case 0xfb: /* sti */
1554 return EXCEPTION_PRIV_INSTRUCTION
;
1560 /***********************************************************************
1563 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1565 static inline BOOL
check_invalid_gs( CONTEXT
*context
)
1567 unsigned int prefix_count
= 0;
1568 const BYTE
*instr
= (BYTE
*)context
->Eip
;
1569 WORD system_gs
= ntdll_get_thread_data()->gs
;
1571 if (context
->SegGs
== system_gs
) return FALSE
;
1572 if (!wine_ldt_is_system( context
->SegCs
)) return FALSE
;
1573 /* only handle faults in system libraries */
1574 if (virtual_is_valid_code_address( instr
, 1 )) return FALSE
;
1576 for (;;) switch(*instr
)
1578 /* instruction prefixes */
1579 case 0x2e: /* %cs: */
1580 case 0x36: /* %ss: */
1581 case 0x3e: /* %ds: */
1582 case 0x26: /* %es: */
1583 case 0x64: /* %fs: */
1584 case 0x66: /* opcode size */
1585 case 0x67: /* addr size */
1586 case 0xf0: /* lock */
1587 case 0xf2: /* repne */
1588 case 0xf3: /* repe */
1589 if (++prefix_count
>= 15) return FALSE
;
1592 case 0x65: /* %gs: */
1593 TRACE( "%04x/%04x at %p, fixing up\n", context
->SegGs
, system_gs
, instr
);
1594 context
->SegGs
= system_gs
;
1602 #include "pshpack1.h"
1605 DWORD movl
; /* movl this,4(%esp) */
1607 BYTE jmp
; /* jmp func */
1610 #include "poppack.h"
1612 /**********************************************************************
1615 * Check if code destination is an ATL thunk, and emulate it if so.
1617 static BOOL
check_atl_thunk( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1619 const struct atl_thunk
*thunk
= (const struct atl_thunk
*)rec
->ExceptionInformation
[1];
1622 if (!virtual_is_valid_code_address( thunk
, sizeof(*thunk
) )) return FALSE
;
1626 if (thunk
->movl
== 0x042444c7 && thunk
->jmp
== 0xe9)
1628 *((DWORD
*)context
->Esp
+ 1) = thunk
->this;
1629 context
->Eip
= (DWORD_PTR
)(&thunk
->func
+ 1) + thunk
->func
;
1630 TRACE( "emulating ATL thunk at %p, func=%08x arg=%08x\n",
1631 thunk
, context
->Eip
, *((DWORD
*)context
->Esp
+ 1) );
1644 /***********************************************************************
1645 * setup_exception_record
1647 * Setup the exception record and context on the thread stack.
1649 static EXCEPTION_RECORD
*setup_exception_record( ucontext_t
*sigcontext
, void *stack_ptr
,
1650 WORD fs
, WORD gs
, raise_func func
)
1654 void *ret_addr
; /* return address from raise_func */
1655 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
1656 CONTEXT
*context_ptr
; /* second arg for raise_func */
1658 EXCEPTION_RECORD rec
;
1661 } *stack
= stack_ptr
;
1662 DWORD exception_code
= 0;
1664 /* stack sanity checks */
1666 if ((char *)stack
>= (char *)get_signal_stack() &&
1667 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1669 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1670 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1671 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1672 NtCurrentTeb()->Tib
.StackBase
);
1676 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1677 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1678 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1680 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1681 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1682 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1683 NtCurrentTeb()->Tib
.StackBase
);
1685 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1687 /* stack overflow on last page, unrecoverable */
1688 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1689 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1690 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1691 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1692 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1695 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1697 /* stack access below stack limit, may be recoverable */
1698 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1701 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1702 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1703 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1704 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1705 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1710 stack
--; /* push the stack_layout structure */
1711 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1712 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1713 #elif defined(VALGRIND_MAKE_WRITABLE)
1714 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1716 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
1717 stack
->rec_ptr
= &stack
->rec
;
1718 stack
->context_ptr
= &stack
->context
;
1720 stack
->rec
.ExceptionRecord
= NULL
;
1721 stack
->rec
.ExceptionCode
= exception_code
;
1722 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1723 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
1724 stack
->rec
.NumberParameters
= 0;
1726 save_context( &stack
->context
, sigcontext
, fs
, gs
);
1728 /* now modify the sigcontext to return to the raise function */
1729 ESP_sig(sigcontext
) = (DWORD
)stack
;
1730 EIP_sig(sigcontext
) = (DWORD
)func
;
1731 /* clear single-step, direction, and align check flag */
1732 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1733 CS_sig(sigcontext
) = wine_get_cs();
1734 DS_sig(sigcontext
) = wine_get_ds();
1735 ES_sig(sigcontext
) = wine_get_es();
1736 FS_sig(sigcontext
) = wine_get_fs();
1737 GS_sig(sigcontext
) = wine_get_gs();
1738 SS_sig(sigcontext
) = wine_get_ss();
1740 return stack
->rec_ptr
;
1744 /***********************************************************************
1747 * Setup a proper stack frame for the raise function, and modify the
1748 * sigcontext so that the return from the signal handler will call
1749 * the raise function.
1751 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
1754 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1756 return setup_exception_record( sigcontext
, stack
, fs
, gs
, func
);
1760 /***********************************************************************
1761 * get_exception_context
1763 * Get a pointer to the context built by setup_exception.
1765 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
1767 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
1771 /**********************************************************************
1774 * Get the FPU exception code from the FPU status.
1776 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1778 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1780 if (status
& 0x01) /* IE */
1782 if (status
& 0x40) /* SF */
1783 return EXCEPTION_FLT_STACK_CHECK
;
1785 return EXCEPTION_FLT_INVALID_OPERATION
;
1787 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1788 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1789 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1790 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1791 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1792 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1796 /**********************************************************************
1797 * raise_segv_exception
1799 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1803 switch(rec
->ExceptionCode
)
1805 case EXCEPTION_ACCESS_VIOLATION
:
1806 if (rec
->NumberParameters
== 2)
1808 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
&& check_atl_thunk( rec
, context
))
1810 if (rec
->ExceptionInformation
[1] == 0xffffffff && check_invalid_gs( context
))
1812 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
1813 rec
->ExceptionInformation
[0] )))
1815 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1816 if (rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
1819 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
1820 &flags
, sizeof(flags
), NULL
);
1821 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
))
1822 rec
->ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
1826 case EXCEPTION_DATATYPE_MISALIGNMENT
:
1827 /* FIXME: pass through exception handler first? */
1828 if (context
->EFlags
& 0x00040000)
1830 /* Disable AC flag, return */
1831 context
->EFlags
&= ~0x00040000;
1836 status
= NtRaiseException( rec
, context
, TRUE
);
1837 raise_status( status
, rec
);
1839 set_cpu_context( context
);
1843 /**********************************************************************
1844 * raise_trap_exception
1846 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1850 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
1852 /* when single stepping can't tell whether this is a hw bp or a
1853 * single step interrupt. try to avoid as much overhead as possible
1854 * and only do a server call if there is any hw bp enabled. */
1856 if( !(context
->EFlags
& 0x100) || (ntdll_get_thread_data()->dr7
& 0xff) )
1858 /* (possible) hardware breakpoint, fetch the debug registers */
1859 DWORD saved_flags
= context
->ContextFlags
;
1860 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1861 NtGetContextThread(GetCurrentThread(), context
);
1862 context
->ContextFlags
|= saved_flags
; /* restore flags */
1865 context
->EFlags
&= ~0x100; /* clear single-step flag */
1868 status
= NtRaiseException( rec
, context
, TRUE
);
1869 raise_status( status
, rec
);
1873 /**********************************************************************
1874 * raise_generic_exception
1876 * Generic raise function for exceptions that don't need special treatment.
1878 static void WINAPI
raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1882 status
= NtRaiseException( rec
, context
, TRUE
);
1883 raise_status( status
, rec
);
1888 /**********************************************************************
1889 * raise_vm86_sti_exception
1891 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1893 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
1894 get_vm86_teb_info()->vm86_pending
|= VIP_FLAG
;
1896 if (ntdll_get_thread_data()->vm86_ptr
)
1898 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
1899 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
1900 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
1901 /* exiting from VM86, can't throw */
1904 merge_vm86_pending_flags( rec
);
1906 else if (get_vm86_teb_info()->dpmi_vif
&&
1907 !wine_ldt_is_system(context
->SegCs
) &&
1908 !wine_ldt_is_system(context
->SegSs
))
1910 /* Executing DPMI code and virtual interrupts are enabled. */
1911 get_vm86_teb_info()->vm86_pending
= 0;
1912 NtRaiseException( rec
, context
, TRUE
);
1915 set_cpu_context( context
);
1919 /**********************************************************************
1922 * Handler for SIGUSR2.
1923 * We use it to signal that the running __wine_enter_vm86() should
1924 * immediately set VIP_FLAG, causing pending events to be handled
1925 * as early as possible.
1927 static void usr2_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1929 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_vm86_sti_exception
);
1930 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
1932 #endif /* __HAVE_VM86 */
1935 /**********************************************************************
1938 * Handler for SIGSEGV and related errors.
1940 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1943 EXCEPTION_RECORD
*rec
;
1944 ucontext_t
*context
= sigcontext
;
1945 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1947 /* check for page fault inside the thread stack */
1948 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
1949 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
1950 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
1951 virtual_handle_stack_fault( siginfo
->si_addr
))
1953 /* check if this was the last guard page */
1954 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
1956 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1957 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1962 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
1963 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
1965 switch(get_trap_code(context
))
1967 case TRAP_x86_OFLOW
: /* Overflow exception */
1968 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1970 case TRAP_x86_BOUND
: /* Bound range exception */
1971 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1973 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1974 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1976 case TRAP_x86_STKFLT
: /* Stack fault */
1977 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1979 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1980 case TRAP_x86_PROTFLT
: /* General protection fault */
1981 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1983 WORD err
= get_error_code(context
);
1984 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( get_exception_context(rec
) ))) break;
1985 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1986 rec
->NumberParameters
= 2;
1987 rec
->ExceptionInformation
[0] = 0;
1988 /* if error contains a LDT selector, use that as fault address */
1989 if ((err
& 7) == 4 && !wine_ldt_is_system( err
| 7 ))
1990 rec
->ExceptionInformation
[1] = err
& ~7;
1992 rec
->ExceptionInformation
[1] = 0xffffffff;
1995 case TRAP_x86_PAGEFLT
: /* Page fault */
1996 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1997 rec
->NumberParameters
= 2;
1998 rec
->ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
1999 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
2001 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
2002 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
2005 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2007 case TRAP_x86_NMI
: /* NMI interrupt */
2008 case TRAP_x86_DNA
: /* Device not available exception */
2009 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
2010 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
2011 case TRAP_x86_MCHK
: /* Machine check exception */
2012 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
2013 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2019 /**********************************************************************
2022 * Handler for SIGTRAP.
2024 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2026 ucontext_t
*context
= sigcontext
;
2027 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_trap_exception
);
2029 switch(get_trap_code(context
))
2031 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2032 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2034 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2035 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
2038 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
2044 /**********************************************************************
2047 * Handler for SIGFPE.
2049 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2051 CONTEXT
*win_context
;
2052 ucontext_t
*context
= sigcontext
;
2053 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_generic_exception
);
2055 win_context
= get_exception_context( rec
);
2057 switch(get_trap_code(context
))
2059 case TRAP_x86_DIVIDE
: /* Division by zero exception */
2060 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
2062 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
2063 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2065 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
2066 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2067 rec
->ExceptionCode
= get_fpu_code( win_context
);
2068 rec
->ExceptionAddress
= (LPVOID
)win_context
->FloatSave
.ErrorOffset
;
2070 case TRAP_x86_CACHEFLT
: /* SIMD exception */
2072 * Behaviour only tested for divide-by-zero exceptions
2073 * Check for other SIMD exceptions as well */
2074 if(siginfo
->si_code
!= FPE_FLTDIV
)
2075 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2078 rec
->ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
2079 rec
->NumberParameters
= 1;
2080 /* no idea what meaning is actually behind this but that's what native does */
2081 rec
->ExceptionInformation
[0] = 0;
2084 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2085 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2091 /**********************************************************************
2094 * Handler for SIGINT.
2096 * FIXME: should not be calling external functions on the signal stack.
2098 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2101 init_handler( sigcontext
, &fs
, &gs
);
2102 if (!dispatch_signal(SIGINT
))
2104 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2105 rec
->ExceptionCode
= CONTROL_C_EXIT
;
2109 /**********************************************************************
2112 * Handler for SIGABRT.
2114 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2116 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2117 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2118 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
2122 /**********************************************************************
2125 * Handler for SIGQUIT.
2127 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2130 init_handler( sigcontext
, &fs
, &gs
);
2135 /**********************************************************************
2138 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2140 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2145 init_handler( sigcontext
, &fs
, &gs
);
2146 save_context( &context
, sigcontext
, fs
, gs
);
2147 wait_suspend( &context
);
2148 restore_context( &context
, sigcontext
);
2152 /***********************************************************************
2153 * __wine_set_signal_handler (NTDLL.@)
2155 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2157 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
2158 if (handlers
[sig
] != NULL
) return -2;
2159 handlers
[sig
] = wsh
;
2164 /***********************************************************************
2165 * locking for LDT routines
2167 static RTL_CRITICAL_SECTION ldt_section
;
2168 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
2171 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
2172 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
2174 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
2175 static sigset_t ldt_sigset
;
2177 static void ldt_lock(void)
2181 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
2182 RtlEnterCriticalSection( &ldt_section
);
2183 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
2186 static void ldt_unlock(void)
2188 if (ldt_section
.RecursionCount
== 1)
2190 sigset_t sigset
= ldt_sigset
;
2191 RtlLeaveCriticalSection( &ldt_section
);
2192 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
2194 else RtlLeaveCriticalSection( &ldt_section
);
2198 /**********************************************************************
2199 * signal_alloc_thread
2201 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2203 static size_t sigstack_zero_bits
;
2204 struct ntdll_thread_data
*thread_data
;
2205 struct ntdll_thread_data
*parent_data
= NULL
;
2210 if (!sigstack_zero_bits
)
2212 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2213 /* find the first power of two not smaller than min_size */
2214 sigstack_zero_bits
= 12;
2215 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
2216 signal_stack_mask
= (1 << sigstack_zero_bits
) - 1;
2217 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
2219 else parent_data
= ntdll_get_thread_data();
2221 size
= signal_stack_mask
+ 1;
2222 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
2223 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
2226 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2227 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2228 thread_data
= (struct ntdll_thread_data
*)(*teb
)->SpareBytes1
;
2229 if (!(thread_data
->fs
= wine_ldt_alloc_fs()))
2232 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
2233 status
= STATUS_TOO_MANY_THREADS
;
2237 /* inherit debug registers from parent thread */
2238 thread_data
->dr0
= parent_data
->dr0
;
2239 thread_data
->dr1
= parent_data
->dr1
;
2240 thread_data
->dr2
= parent_data
->dr2
;
2241 thread_data
->dr3
= parent_data
->dr3
;
2242 thread_data
->dr6
= parent_data
->dr6
;
2243 thread_data
->dr7
= parent_data
->dr7
;
2251 /**********************************************************************
2252 * signal_free_thread
2254 void signal_free_thread( TEB
*teb
)
2257 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2259 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
2260 if (teb
->DeallocationStack
)
2263 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
2266 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2270 /**********************************************************************
2271 * signal_init_thread
2273 void signal_init_thread( TEB
*teb
)
2275 const WORD fpu_cw
= 0x27f;
2276 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
2281 int mib
[2], val
= 1;
2284 mib
[1] = KERN_THALTSTACK
;
2285 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
2288 ss
.ss_sp
= (char *)teb
+ teb_size
;
2289 ss
.ss_size
= signal_stack_size
;
2291 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2293 wine_ldt_set_base( &fs_entry
, teb
);
2294 wine_ldt_set_limit( &fs_entry
, teb_size
- 1 );
2295 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
2296 wine_ldt_init_fs( thread_data
->fs
, &fs_entry
);
2297 thread_data
->gs
= wine_get_gs();
2300 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2302 FIXME("FPU setup not implemented for this platform.\n");
2306 /**********************************************************************
2307 * signal_init_process
2309 void signal_init_process(void)
2311 struct sigaction sig_act
;
2313 sig_act
.sa_mask
= server_block_set
;
2314 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
2316 sig_act
.sa_flags
|= SA_ONSTACK
;
2319 sig_act
.sa_flags
|= SA_RESTORER
;
2320 sig_act
.sa_restorer
= rt_sigreturn
;
2322 sig_act
.sa_sigaction
= int_handler
;
2323 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2324 sig_act
.sa_sigaction
= fpe_handler
;
2325 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2326 sig_act
.sa_sigaction
= abrt_handler
;
2327 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2328 sig_act
.sa_sigaction
= quit_handler
;
2329 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2330 sig_act
.sa_sigaction
= usr1_handler
;
2331 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2333 sig_act
.sa_sigaction
= segv_handler
;
2334 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2335 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2337 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2341 sig_act
.sa_sigaction
= trap_handler
;
2342 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2346 sig_act
.sa_sigaction
= usr2_handler
;
2347 if (sigaction( SIGUSR2
, &sig_act
, NULL
) == -1) goto error
;
2350 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
2354 perror("sigaction");
2360 /**********************************************************************
2361 * __wine_enter_vm86 (NTDLL.@)
2363 * Enter vm86 mode with the specified register context.
2365 void __wine_enter_vm86( CONTEXT
*context
)
2367 EXCEPTION_RECORD rec
;
2369 struct vm86plus_struct vm86
;
2371 memset( &vm86
, 0, sizeof(vm86
) );
2374 restore_vm86_context( context
, &vm86
);
2376 ntdll_get_thread_data()->vm86_ptr
= &vm86
;
2377 merge_vm86_pending_flags( &rec
);
2379 res
= vm86_enter( &ntdll_get_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
2386 save_vm86_context( context
, &vm86
);
2388 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2389 rec
.ExceptionRecord
= NULL
;
2390 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
2391 rec
.NumberParameters
= 0;
2393 switch(VM86_TYPE(res
))
2395 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
2396 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
2398 case VM86_TRAP
: /* return due to DOS-debugger request */
2399 switch(VM86_ARG(res
))
2401 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2402 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2404 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2405 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
2408 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2412 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
2413 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
2414 rec
.NumberParameters
= 1;
2415 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
2417 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
2418 context
->EFlags
|= VIF_FLAG
;
2419 context
->EFlags
&= ~VIP_FLAG
;
2420 get_vm86_teb_info()->vm86_pending
= 0;
2421 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
2423 case VM86_PICRETURN
: /* return due to pending PIC request */
2424 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
2426 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
2428 WINE_ERR( "unhandled result from vm86 mode %x\n", res
);
2431 raise_exception( &rec
, context
, TRUE
);
2435 #else /* __HAVE_VM86 */
2436 /**********************************************************************
2437 * __wine_enter_vm86 (NTDLL.@)
2439 void __wine_enter_vm86( CONTEXT
*context
)
2441 MESSAGE("vm86 mode not supported on this platform\n");
2443 #endif /* __HAVE_VM86 */
2446 /*******************************************************************
2447 * RtlUnwind (NTDLL.@)
2449 void WINAPI
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
2450 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
2452 EXCEPTION_RECORD record
;
2453 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
2456 context
->Eax
= (DWORD
)retval
;
2458 /* build an exception record, if we do not have one */
2461 record
.ExceptionCode
= STATUS_UNWIND
;
2462 record
.ExceptionFlags
= 0;
2463 record
.ExceptionRecord
= NULL
;
2464 record
.ExceptionAddress
= (void *)context
->Eip
;
2465 record
.NumberParameters
= 0;
2469 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
2471 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2473 /* get chain of exception frames */
2474 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2475 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
2477 /* Check frame address */
2478 if (pEndFrame
&& (frame
> pEndFrame
))
2479 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
2481 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
2484 TRACE( "calling handler at %p code=%x flags=%x\n",
2485 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2486 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
2487 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
2491 case ExceptionContinueSearch
:
2493 case ExceptionCollidedUnwind
:
2497 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
2500 frame
= __wine_pop_frame( frame
);
2503 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 4 )
2506 /*******************************************************************
2507 * NtRaiseException (NTDLL.@)
2509 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2511 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
2512 if (status
== STATUS_SUCCESS
)
2514 set_debug_registers( context
);
2515 set_cpu_context( context
);
2521 /***********************************************************************
2522 * RtlRaiseException (NTDLL.@)
2524 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2528 rec
->ExceptionAddress
= (void *)context
->Eip
;
2529 status
= raise_exception( rec
, context
, TRUE
);
2530 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
2532 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 1 )
2535 /*************************************************************************
2536 * RtlCaptureStackBackTrace (NTDLL.@)
2538 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
2544 RtlCaptureContext( &context
);
2545 if (hash
) *hash
= 0;
2546 frame
= (ULONG
*)context
.Ebp
;
2550 if (!is_valid_frame( frame
)) return 0;
2551 frame
= (ULONG
*)*frame
;
2554 for (i
= 0; i
< count
; i
++)
2556 if (!is_valid_frame( frame
)) break;
2557 buffer
[i
] = (void *)frame
[1];
2558 if (hash
) *hash
+= frame
[1];
2559 frame
= (ULONG
*)*frame
;
2565 extern void DECLSPEC_NORETURN
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
);
2566 __ASM_GLOBAL_FUNC( call_thread_entry_point
,
2568 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2569 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2570 "movl %esp,%ebp\n\t"
2571 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2573 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2575 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2577 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2579 "pushl 12(%ebp)\n\t"
2581 "call " __ASM_NAME("call_thread_func") );
2583 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int), void *frame
);
2584 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
2585 "movl 4(%esp),%eax\n\t"
2586 "movl 8(%esp),%ecx\n\t"
2587 "movl 12(%esp),%ebp\n\t"
2588 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2589 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2590 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2591 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2592 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2593 "leal -20(%ebp),%esp\n\t"
2597 /* wrapper for apps that don't declare the thread function correctly */
2598 extern void DECLSPEC_NORETURN
call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry
, void *arg
);
2599 __ASM_GLOBAL_FUNC(call_thread_func_wrapper
,
2601 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2602 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2603 "movl %esp,%ebp\n\t"
2604 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2606 "pushl 12(%ebp)\n\t"
2608 "leal -4(%ebp),%esp\n\t"
2610 "call " __ASM_NAME("exit_thread") "\n\t"
2613 /***********************************************************************
2616 void call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
, void *frame
)
2618 ntdll_get_thread_data()->exit_frame
= frame
;
2621 call_thread_func_wrapper( entry
, arg
);
2623 __EXCEPT(unhandled_exception_filter
)
2625 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2628 abort(); /* should not be reached */
2631 /***********************************************************************
2632 * RtlExitUserThread (NTDLL.@)
2634 void WINAPI
RtlExitUserThread( ULONG status
)
2636 if (!ntdll_get_thread_data()->exit_frame
) exit_thread( status
);
2637 call_thread_exit_func( status
, exit_thread
, ntdll_get_thread_data()->exit_frame
);
2640 /***********************************************************************
2643 void abort_thread( int status
)
2645 if (!ntdll_get_thread_data()->exit_frame
) terminate_thread( status
);
2646 call_thread_exit_func( status
, terminate_thread
, ntdll_get_thread_data()->exit_frame
);
2649 /**********************************************************************
2650 * DbgBreakPoint (NTDLL.@)
2652 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
2654 /**********************************************************************
2655 * DbgUserBreakPoint (NTDLL.@)
2657 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
2659 /**********************************************************************
2660 * NtCurrentTeb (NTDLL.@)
2662 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2665 /**************************************************************************
2668 __ASM_STDCALL_FUNC( _chkstk
, 0,
2670 "addl %esp,%eax\n\t"
2671 "xchgl %esp,%eax\n\t"
2672 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2673 "movl %eax,0(%esp)\n\t"
2676 /**************************************************************************
2677 * _alloca_probe (NTDLL.@)
2679 __ASM_STDCALL_FUNC( _alloca_probe
, 0,
2681 "addl %esp,%eax\n\t"
2682 "xchgl %esp,%eax\n\t"
2683 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
2684 "movl %eax,0(%esp)\n\t"
2688 /**********************************************************************
2689 * EXC_CallHandler (internal)
2691 * Some exception handlers depend on EBP to have a fixed position relative to
2692 * the exception frame.
2693 * Shrinker depends on (*1) doing what it does,
2694 * (*2) being the exact instruction it is and (*3) beginning with 0x64
2695 * (i.e. the %fs prefix to the movl instruction). It also depends on the
2696 * function calling the handler having only 5 parameters (*4).
2698 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
2700 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2701 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2702 "movl %esp,%ebp\n\t"
2703 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2705 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2706 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
2707 "pushl 24(%ebp)\n\t"
2708 "pushl 20(%ebp)\n\t"
2709 "pushl 16(%ebp)\n\t"
2710 "pushl 12(%ebp)\n\t"
2712 "call " __ASM_NAME("call_exception_handler") "\n\t"
2714 __ASM_CFI(".cfi_same_value %ebx\n\t")
2716 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2717 __ASM_CFI(".cfi_same_value %ebp\n\t")
2719 __ASM_GLOBAL_FUNC(call_exception_handler
,
2721 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2722 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2723 "movl %esp,%ebp\n\t"
2724 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2726 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
2727 "pushl %edx\n\t" /* handler be handled by... */
2729 "pushl (0)\n\t" /* nested_handler (passed in edx). */
2731 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
2732 "pushl 20(%ebp)\n\t"
2733 "pushl 16(%ebp)\n\t"
2734 "pushl 12(%ebp)\n\t"
2736 "movl 24(%ebp), %ecx\n\t" /* (*1) */
2737 "call *%ecx\n\t" /* call handler. (*2) */
2739 "movl (0), %esp\n\t" /* restore previous... (*3) */
2741 "popl (0)\n\t" /* exception frame. */
2742 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
2744 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2745 __ASM_CFI(".cfi_same_value %ebp\n\t")
2746 "ret $20" ) /* (*4) */
2748 #endif /* __i386__ */