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 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
73 WINE_DECLARE_DEBUG_CHANNEL(relay
);
75 /* not defined for x86, so copy the x86_64 definition */
76 typedef struct DECLSPEC_ALIGN(16) _M128A
97 M128A FloatRegisters
[8];
98 M128A XmmRegisters
[16];
102 /***********************************************************************
103 * signal context platform-specific definitions
108 #ifndef HAVE_SYS_UCONTEXT_H
112 REG_GS
, REG_FS
, REG_ES
, REG_DS
, REG_EDI
, REG_ESI
, REG_EBP
, REG_ESP
,
113 REG_EBX
, REG_EDX
, REG_ECX
, REG_EAX
, REG_TRAPNO
, REG_ERR
, REG_EIP
,
114 REG_CS
, REG_EFL
, REG_UESP
, REG_SS
, NGREG
118 typedef greg_t gregset_t
[NGREG
];
122 unsigned short significand
[4];
123 unsigned short exponent
;
133 unsigned long dataoff
;
134 unsigned long datasel
;
135 struct _libc_fpreg _st
[8];
136 unsigned long status
;
139 typedef struct _libc_fpstate
* fpregset_t
;
145 unsigned long oldmask
;
149 typedef struct ucontext
151 unsigned long uc_flags
;
152 struct ucontext
*uc_link
;
154 mcontext_t uc_mcontext
;
157 #endif /* HAVE_SYS_UCONTEXT_H */
159 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
160 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
161 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
162 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
163 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
164 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
165 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
166 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
168 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
169 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
170 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
171 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
172 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
173 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
175 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
176 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
177 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
178 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
180 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
181 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
183 #define VIF_FLAG 0x00080000
184 #define VIP_FLAG 0x00100000
186 int vm86_enter( void **vm86_ptr
);
187 void vm86_return(void);
188 void vm86_return_end(void);
189 __ASM_GLOBAL_FUNC(vm86_enter
,
191 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
192 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
194 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
196 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
197 "movl $166,%eax\n\t" /*SYS_vm86*/
198 "movl 8(%ebp),%ecx\n\t" /* vm86_ptr */
199 "movl (%ecx),%ecx\n\t"
200 "movl $1,%ebx\n\t" /*VM86_ENTER*/
201 "pushl %ecx\n\t" /* put vm86plus_struct ptr somewhere we can find it */
205 ".globl " __ASM_NAME("vm86_return") "\n\t"
206 __ASM_FUNC("vm86_return") "\n"
207 __ASM_NAME("vm86_return") ":\n\t"
212 __ASM_CFI(".cfi_same_value %ebx\n\t")
214 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
215 __ASM_CFI(".cfi_same_value %ebp\n\t")
216 "testl %eax,%eax\n\t"
218 "cmpb $0,%al\n\t" /* VM86_SIGNAL */
219 "je " __ASM_NAME("vm86_enter") "\n\t"
221 "movl 4(%esp),%ecx\n\t" /* vm86_ptr */
223 ".globl " __ASM_NAME("vm86_return_end") "\n\t"
224 __ASM_FUNC("vm86_return_end") "\n"
225 __ASM_NAME("vm86_return_end") ":\n\t"
228 #ifdef HAVE_SYS_VM86_H
233 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
234 void rt_sigreturn(void);
235 __ASM_GLOBAL_FUNC( rt_sigreturn
,
236 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
240 #elif defined (__BSDI__)
242 #include <machine/frame.h>
243 typedef struct trapframe ucontext_t
;
245 #define EAX_sig(context) ((context)->tf_eax)
246 #define EBX_sig(context) ((context)->tf_ebx)
247 #define ECX_sig(context) ((context)->tf_ecx)
248 #define EDX_sig(context) ((context)->tf_edx)
249 #define ESI_sig(context) ((context)->tf_esi)
250 #define EDI_sig(context) ((context)->tf_edi)
251 #define EBP_sig(context) ((context)->tf_ebp)
253 #define CS_sig(context) ((context)->tf_cs)
254 #define DS_sig(context) ((context)->tf_ds)
255 #define ES_sig(context) ((context)->tf_es)
256 #define SS_sig(context) ((context)->tf_ss)
258 #define EFL_sig(context) ((context)->tf_eflags)
260 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
261 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
263 #define FPU_sig(context) NULL /* FIXME */
264 #define FPUX_sig(context) NULL /* FIXME */
266 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
268 #include <machine/trap.h>
270 #define EAX_sig(context) ((context)->uc_mcontext.mc_eax)
271 #define EBX_sig(context) ((context)->uc_mcontext.mc_ebx)
272 #define ECX_sig(context) ((context)->uc_mcontext.mc_ecx)
273 #define EDX_sig(context) ((context)->uc_mcontext.mc_edx)
274 #define ESI_sig(context) ((context)->uc_mcontext.mc_esi)
275 #define EDI_sig(context) ((context)->uc_mcontext.mc_edi)
276 #define EBP_sig(context) ((context)->uc_mcontext.mc_ebp)
278 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
279 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
280 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
281 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
282 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
283 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
285 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
286 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
287 #define EFL_sig(context) ((context)->uc_mcontext.mc_eflags)
289 #define EIP_sig(context) ((context)->uc_mcontext.mc_eip)
290 #define ESP_sig(context) ((context)->uc_mcontext.mc_esp)
292 #define FPU_sig(context) NULL /* FIXME */
293 #define FPUX_sig(context) NULL /* FIXME */
295 #elif defined (__OpenBSD__)
297 #define EAX_sig(context) ((context)->sc_eax)
298 #define EBX_sig(context) ((context)->sc_ebx)
299 #define ECX_sig(context) ((context)->sc_ecx)
300 #define EDX_sig(context) ((context)->sc_edx)
301 #define ESI_sig(context) ((context)->sc_esi)
302 #define EDI_sig(context) ((context)->sc_edi)
303 #define EBP_sig(context) ((context)->sc_ebp)
305 #define CS_sig(context) ((context)->sc_cs)
306 #define DS_sig(context) ((context)->sc_ds)
307 #define ES_sig(context) ((context)->sc_es)
308 #define FS_sig(context) ((context)->sc_fs)
309 #define GS_sig(context) ((context)->sc_gs)
310 #define SS_sig(context) ((context)->sc_ss)
312 #define TRAP_sig(context) ((context)->sc_trapno)
313 #define ERROR_sig(context) ((context)->sc_err)
314 #define EFL_sig(context) ((context)->sc_eflags)
316 #define EIP_sig(context) ((context)->sc_eip)
317 #define ESP_sig(context) ((context)->sc_esp)
319 #define FPU_sig(context) NULL /* FIXME */
320 #define FPUX_sig(context) NULL /* FIXME */
322 #define T_MCHK T_MACHK
323 #define T_XMMFLT T_XFTRAP
325 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
328 #include <sys/regset.h>
332 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
333 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
334 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
335 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
336 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
337 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
338 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
340 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
341 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
342 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
343 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
345 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
346 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
348 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
350 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
352 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
354 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
356 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
359 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
362 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
365 #define FPU_sig(context) NULL /* FIXME */
366 #define FPUX_sig(context) NULL /* FIXME */
368 #elif defined (__APPLE__)
370 /* work around silly renaming of struct members in OS X 10.5 */
371 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
372 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
373 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
374 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
375 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
376 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
377 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
378 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
379 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
380 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
381 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
382 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
383 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
384 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
385 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
386 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
387 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
388 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
389 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
390 #define FPU_sig(context) NULL
391 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
393 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
394 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
395 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
396 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
397 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
398 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
399 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
400 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
401 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
402 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
403 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
404 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
405 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
406 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
407 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
408 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
409 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
410 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
411 #define FPU_sig(context) NULL
412 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
415 #elif defined(__NetBSD__)
417 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
418 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
419 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
420 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
421 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
422 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
423 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
424 #define ESP_sig(context) _UC_MACHINE_SP(context)
426 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
427 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
428 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
429 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
430 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
431 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
433 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
434 #define EIP_sig(context) _UC_MACHINE_PC(context)
435 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
436 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
438 #define FPU_sig(context) NULL
439 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
442 #define T_XMMFLT T_XMM
444 #elif defined(__GNU__)
446 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
447 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
448 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
449 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
450 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
451 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
452 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
453 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
455 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
456 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
457 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
458 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
459 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
460 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
462 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
463 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
464 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
465 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
467 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
468 #define FPUX_sig(context) NULL
471 #error You must define the signal context functions for your platform
474 typedef int (*wine_signal_handler
)(unsigned int sig
);
476 static const size_t teb_size
= 4096; /* we reserve one page for the TEB */
477 static size_t signal_stack_mask
;
478 static size_t signal_stack_size
;
480 static wine_signal_handler handlers
[256];
482 static BOOL fpux_support
; /* whether the CPU supports extended fpu context */
486 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
487 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
488 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
489 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
490 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
491 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
492 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
493 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
494 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
495 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
496 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
497 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
498 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
499 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
500 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
501 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
502 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
503 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
504 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
505 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
506 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
508 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
509 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
510 TRAP_x86_NMI
= 2, /* NMI interrupt */
511 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
512 TRAP_x86_OFLOW
= 4, /* Overflow exception */
513 TRAP_x86_BOUND
= 5, /* Bound range exception */
514 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
515 TRAP_x86_DNA
= 7, /* Device not available exception */
516 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
517 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
518 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
519 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
520 TRAP_x86_STKFLT
= 12, /* Stack fault */
521 TRAP_x86_PROTFLT
= 13, /* General protection fault */
522 TRAP_x86_PAGEFLT
= 14, /* Page fault */
523 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
524 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
525 TRAP_x86_MCHK
= 18, /* Machine check exception */
526 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
527 otherwise Cache flush exception (via SIGSEV) */
531 struct x86_thread_data
533 DWORD fs
; /* 1d4 TEB selector */
534 DWORD gs
; /* 1d8 libc selector; update winebuild if you move this! */
535 DWORD dr0
; /* 1dc debug registers */
541 void *exit_frame
; /* 1f4 exit frame pointer */
543 void *vm86_ptr
; /* 1f8 data for vm86 mode */
544 WINE_VM86_TEB_INFO vm86
; /* 1fc vm86 private data */
546 /* the ntdll_thread_data structure follows here */
549 C_ASSERT( offsetof( TEB
, SystemReserved2
) + offsetof( struct x86_thread_data
, gs
) == 0x1d8 );
550 C_ASSERT( offsetof( TEB
, SystemReserved2
) + offsetof( struct x86_thread_data
, exit_frame
) == 0x1f4 );
552 C_ASSERT( offsetof( TEB
, SystemReserved2
) + offsetof( struct x86_thread_data
, vm86
) ==
553 offsetof( TEB
, GdiTebBatch
) + offsetof( struct ntdll_thread_data
, __vm86
));
556 static inline struct x86_thread_data
*x86_thread_data(void)
558 return (struct x86_thread_data
*)NtCurrentTeb()->SystemReserved2
;
561 /* Exception record for handling exceptions happening inside exception handlers */
564 EXCEPTION_REGISTRATION_RECORD frame
;
565 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
568 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
569 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
570 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
572 /***********************************************************************
575 static inline int dispatch_signal(unsigned int sig
)
577 if (handlers
[sig
] == NULL
) return 0;
578 return handlers
[sig
](sig
);
582 /***********************************************************************
585 * Get the trap code for a signal.
587 static inline enum i386_trap_code
get_trap_code( const ucontext_t
*sigcontext
)
590 return TRAP_sig(sigcontext
);
592 return TRAP_x86_UNKNOWN
; /* unknown trap code */
596 /***********************************************************************
599 * Get the error code for a signal.
601 static inline WORD
get_error_code( const ucontext_t
*sigcontext
)
604 return ERROR_sig(sigcontext
);
610 /***********************************************************************
613 * Get the base of the signal stack for the current thread.
615 static inline void *get_signal_stack(void)
617 return (char *)NtCurrentTeb() + 4096;
621 /***********************************************************************
624 * Get the current teb based on the stack pointer.
626 static inline TEB
*get_current_teb(void)
629 __asm__("movl %%esp,%0" : "=g" (esp
) );
630 return (TEB
*)(esp
& ~signal_stack_mask
);
634 /*******************************************************************
637 static inline BOOL
is_valid_frame( void *frame
)
639 if ((ULONG_PTR
)frame
& 3) return FALSE
;
640 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
641 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
644 /*******************************************************************
647 * Handler for exceptions happening inside a handler.
649 static DWORD
raise_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
650 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
652 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
653 return ExceptionContinueSearch
;
654 /* We shouldn't get here so we store faulty frame in dispatcher */
655 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
656 return ExceptionNestedException
;
660 /*******************************************************************
663 * Handler for exceptions happening inside an unwind handler.
665 static DWORD
unwind_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
666 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
668 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
669 return ExceptionContinueSearch
;
670 /* We shouldn't get here so we store faulty frame in dispatcher */
671 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
672 return ExceptionCollidedUnwind
;
676 /**********************************************************************
677 * call_stack_handlers
679 * Call the stack handlers chain.
681 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
683 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
686 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
688 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
690 /* Check frame address */
691 if (!is_valid_frame( frame
))
693 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
698 TRACE( "calling handler at %p code=%x flags=%x\n",
699 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
700 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, raise_handler
);
701 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
703 if (frame
== nested_frame
)
705 /* no longer nested */
707 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
712 case ExceptionContinueExecution
:
713 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
714 return STATUS_NONCONTINUABLE_EXCEPTION
;
715 case ExceptionContinueSearch
:
717 case ExceptionNestedException
:
718 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
719 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
722 return STATUS_INVALID_DISPOSITION
;
726 return STATUS_UNHANDLED_EXCEPTION
;
730 /*******************************************************************
733 * Implementation of NtRaiseException.
735 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
743 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
744 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
745 context
->Eip
, GetCurrentThreadId() );
746 for (c
= 0; c
< rec
->NumberParameters
; c
++)
747 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
748 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
750 if (rec
->ExceptionInformation
[1] >> 16)
751 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
752 rec
->ExceptionAddress
,
753 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
755 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
756 rec
->ExceptionAddress
,
757 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
761 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
762 context
->Eax
, context
->Ebx
, context
->Ecx
,
763 context
->Edx
, context
->Esi
, context
->Edi
);
764 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
765 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
766 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
768 status
= send_debug_event( rec
, TRUE
, context
);
769 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
770 return STATUS_SUCCESS
;
772 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
773 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
775 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
776 return STATUS_SUCCESS
;
778 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
782 /* last chance exception */
784 status
= send_debug_event( rec
, FALSE
, context
);
785 if (status
!= DBG_CONTINUE
)
787 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
788 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
789 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
790 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
792 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
793 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
794 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
796 return STATUS_SUCCESS
;
801 /***********************************************************************
804 * Set the register values from a vm86 structure.
806 static void save_vm86_context( CONTEXT
*context
, const struct vm86plus_struct
*vm86
)
808 context
->ContextFlags
= CONTEXT_FULL
;
809 context
->Eax
= vm86
->regs
.eax
;
810 context
->Ebx
= vm86
->regs
.ebx
;
811 context
->Ecx
= vm86
->regs
.ecx
;
812 context
->Edx
= vm86
->regs
.edx
;
813 context
->Esi
= vm86
->regs
.esi
;
814 context
->Edi
= vm86
->regs
.edi
;
815 context
->Esp
= vm86
->regs
.esp
;
816 context
->Ebp
= vm86
->regs
.ebp
;
817 context
->Eip
= vm86
->regs
.eip
;
818 context
->SegCs
= vm86
->regs
.cs
;
819 context
->SegDs
= vm86
->regs
.ds
;
820 context
->SegEs
= vm86
->regs
.es
;
821 context
->SegFs
= vm86
->regs
.fs
;
822 context
->SegGs
= vm86
->regs
.gs
;
823 context
->SegSs
= vm86
->regs
.ss
;
824 context
->EFlags
= vm86
->regs
.eflags
;
828 /***********************************************************************
829 * restore_vm86_context
831 * Build a vm86 structure from the register values.
833 static void restore_vm86_context( const CONTEXT
*context
, struct vm86plus_struct
*vm86
)
835 vm86
->regs
.eax
= context
->Eax
;
836 vm86
->regs
.ebx
= context
->Ebx
;
837 vm86
->regs
.ecx
= context
->Ecx
;
838 vm86
->regs
.edx
= context
->Edx
;
839 vm86
->regs
.esi
= context
->Esi
;
840 vm86
->regs
.edi
= context
->Edi
;
841 vm86
->regs
.esp
= context
->Esp
;
842 vm86
->regs
.ebp
= context
->Ebp
;
843 vm86
->regs
.eip
= context
->Eip
;
844 vm86
->regs
.cs
= context
->SegCs
;
845 vm86
->regs
.ds
= context
->SegDs
;
846 vm86
->regs
.es
= context
->SegEs
;
847 vm86
->regs
.fs
= context
->SegFs
;
848 vm86
->regs
.gs
= context
->SegGs
;
849 vm86
->regs
.ss
= context
->SegSs
;
850 vm86
->regs
.eflags
= context
->EFlags
;
854 /**********************************************************************
855 * merge_vm86_pending_flags
857 * Merges TEB.vm86_ptr and TEB.vm86_pending VIP flags and
858 * raises exception if there are pending events and VIF flag
859 * has been turned on.
861 * Called from __wine_enter_vm86 because vm86_enter
862 * doesn't check for pending events.
864 * Called from raise_vm86_sti_exception to check for
865 * pending events in a signal safe way.
867 static void merge_vm86_pending_flags( EXCEPTION_RECORD
*rec
)
869 BOOL check_pending
= TRUE
;
870 struct vm86plus_struct
*vm86
=
871 (struct vm86plus_struct
*)x86_thread_data()->vm86_ptr
;
874 * In order to prevent a race when SIGUSR2 occurs while
875 * we are returning from exception handler, pending events
876 * will be rechecked after each raised exception.
878 while (check_pending
&& get_vm86_teb_info()->vm86_pending
)
880 check_pending
= FALSE
;
881 x86_thread_data()->vm86_ptr
= NULL
;
884 * If VIF is set, throw exception.
885 * Note that SIGUSR2 may turn VIF flag off so
886 * VIF check must occur only when TEB.vm86_ptr is NULL.
888 if (vm86
->regs
.eflags
& VIF_FLAG
)
891 save_vm86_context( &vcontext
, vm86
);
893 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
894 rec
->ExceptionFlags
= EXCEPTION_CONTINUABLE
;
895 rec
->ExceptionRecord
= NULL
;
896 rec
->NumberParameters
= 0;
897 rec
->ExceptionAddress
= (LPVOID
)vcontext
.Eip
;
899 vcontext
.EFlags
&= ~VIP_FLAG
;
900 get_vm86_teb_info()->vm86_pending
= 0;
901 raise_exception( rec
, &vcontext
, TRUE
);
903 restore_vm86_context( &vcontext
, vm86
);
904 check_pending
= TRUE
;
907 x86_thread_data()->vm86_ptr
= vm86
;
911 * Merge VIP flags in a signal safe way. This requires
912 * that the following operation compiles into atomic
915 vm86
->regs
.eflags
|= get_vm86_teb_info()->vm86_pending
;
917 #endif /* __HAVE_VM86 */
922 /* We have to workaround two Solaris breakages:
923 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
925 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
926 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
929 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
930 __ASM_GLOBAL_FUNC( sigaction_syscall
,
931 "movl $0x62,%eax\n\t"
935 /* assume the same libc handler is used for all signals */
936 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
938 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
940 struct x86_thread_data
*thread_data
;
942 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
944 thread_data
= (struct x86_thread_data
*)get_current_teb()->SystemReserved2
;
945 wine_set_fs( thread_data
->fs
);
946 wine_set_gs( thread_data
->gs
);
948 libc_sigacthandler( signal
, siginfo
, sigcontext
);
951 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
953 struct sigaction real_act
;
955 if (sigaction( sig
, new, old
) == -1) return -1;
957 /* retrieve the real handler and flags with a direct syscall */
958 sigaction_syscall( sig
, NULL
, &real_act
);
959 libc_sigacthandler
= real_act
.sa_sigaction
;
960 real_act
.sa_sigaction
= wine_sigacthandler
;
961 sigaction_syscall( sig
, &real_act
, NULL
);
964 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
968 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
970 extern void clear_alignment_flag(void);
971 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
973 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
974 "andl $~0x40000,(%esp)\n\t"
976 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
980 /***********************************************************************
983 * Handler initialization when the full context is not needed.
984 * Return the stack pointer to use for pushing the exception data.
986 static inline void *init_handler( const ucontext_t
*sigcontext
, WORD
*fs
, WORD
*gs
)
988 TEB
*teb
= get_current_teb();
990 clear_alignment_flag();
992 /* get %fs and %gs at time of the fault */
994 *fs
= LOWORD(FS_sig(sigcontext
));
999 *gs
= LOWORD(GS_sig(sigcontext
));
1001 *gs
= wine_get_gs();
1004 #ifndef __sun /* see above for Solaris handling */
1006 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)teb
->SystemReserved2
;
1007 wine_set_fs( thread_data
->fs
);
1008 wine_set_gs( thread_data
->gs
);
1012 if (!wine_ldt_is_system(CS_sig(sigcontext
)) ||
1013 !wine_ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
1016 * Win16 or DOS protected mode. Note that during switch
1017 * from 16-bit mode to linear mode, CS may be set to system
1018 * segment before FS is restored. Fortunately, in this case
1019 * SS is still non-system segment. This is why both CS and SS
1022 return teb
->WOW32Reserved
;
1024 return (void *)(ESP_sig(sigcontext
) & ~3);
1028 /***********************************************************************
1031 * Save the thread FPU context.
1033 static inline void save_fpu( CONTEXT
*context
)
1036 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1037 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
1042 /***********************************************************************
1045 * Restore the FPU context to a sigcontext.
1047 static inline void restore_fpu( const CONTEXT
*context
)
1049 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
1050 /* reset the current interrupt status */
1051 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
1053 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
1054 #endif /* __GNUC__ */
1058 /***********************************************************************
1061 * Restore the FPU extended context to a sigcontext.
1063 static inline void restore_fpux( const CONTEXT
*context
)
1066 /* we have to enforce alignment by hand */
1067 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1068 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1070 memcpy( state
, context
->ExtendedRegisters
, sizeof(*state
) );
1071 /* reset the current interrupt status */
1072 state
->StatusWord
&= state
->ControlWord
| 0xff80;
1073 __asm__
__volatile__( "fxrstor %0" : : "m" (*state
) );
1078 /***********************************************************************
1081 * Build a standard FPU context from an extended one.
1083 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XMM_SAVE_AREA32
*fpux
)
1085 unsigned int i
, tag
, stack_top
;
1087 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
1088 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
1089 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
1090 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
1091 fpu
->DataOffset
= fpux
->DataOffset
;
1092 fpu
->DataSelector
= fpux
->DataSelector
;
1093 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
1095 stack_top
= (fpux
->StatusWord
>> 11) & 7;
1096 fpu
->TagWord
= 0xffff0000;
1097 for (i
= 0; i
< 8; i
++)
1099 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
1100 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
1103 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
1104 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
1106 tag
= 2; /* special */
1108 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
1110 if (reg
->Low
) tag
= 2; /* special */
1111 else tag
= 1; /* zero */
1115 if (reg
->Low
>> 63) tag
= 0; /* valid */
1116 else tag
= 2; /* special */
1119 fpu
->TagWord
|= tag
<< (2 * i
);
1124 /***********************************************************************
1127 * Build a context structure from the signal info.
1129 static inline void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
, WORD fs
, WORD gs
)
1131 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1132 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1134 memset(context
, 0, sizeof(*context
));
1135 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
1136 context
->Eax
= EAX_sig(sigcontext
);
1137 context
->Ebx
= EBX_sig(sigcontext
);
1138 context
->Ecx
= ECX_sig(sigcontext
);
1139 context
->Edx
= EDX_sig(sigcontext
);
1140 context
->Esi
= ESI_sig(sigcontext
);
1141 context
->Edi
= EDI_sig(sigcontext
);
1142 context
->Ebp
= EBP_sig(sigcontext
);
1143 context
->EFlags
= EFL_sig(sigcontext
);
1144 context
->Eip
= EIP_sig(sigcontext
);
1145 context
->Esp
= ESP_sig(sigcontext
);
1146 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
1147 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
1148 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
1149 context
->SegFs
= fs
;
1150 context
->SegGs
= gs
;
1151 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
1152 context
->Dr0
= x86_thread_data()->dr0
;
1153 context
->Dr1
= x86_thread_data()->dr1
;
1154 context
->Dr2
= x86_thread_data()->dr2
;
1155 context
->Dr3
= x86_thread_data()->dr3
;
1156 context
->Dr6
= x86_thread_data()->dr6
;
1157 context
->Dr7
= x86_thread_data()->dr7
;
1161 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1162 context
->FloatSave
= *fpu
;
1166 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
1167 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
1168 fpux_support
= TRUE
;
1169 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
1171 if (!fpu
&& !fpux
) save_fpu( context
);
1175 /***********************************************************************
1178 * Restore the signal info from the context.
1180 static inline void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1182 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1183 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1185 x86_thread_data()->dr0
= context
->Dr0
;
1186 x86_thread_data()->dr1
= context
->Dr1
;
1187 x86_thread_data()->dr2
= context
->Dr2
;
1188 x86_thread_data()->dr3
= context
->Dr3
;
1189 x86_thread_data()->dr6
= context
->Dr6
;
1190 x86_thread_data()->dr7
= context
->Dr7
;
1191 EAX_sig(sigcontext
) = context
->Eax
;
1192 EBX_sig(sigcontext
) = context
->Ebx
;
1193 ECX_sig(sigcontext
) = context
->Ecx
;
1194 EDX_sig(sigcontext
) = context
->Edx
;
1195 ESI_sig(sigcontext
) = context
->Esi
;
1196 EDI_sig(sigcontext
) = context
->Edi
;
1197 EBP_sig(sigcontext
) = context
->Ebp
;
1198 EFL_sig(sigcontext
) = context
->EFlags
;
1199 EIP_sig(sigcontext
) = context
->Eip
;
1200 ESP_sig(sigcontext
) = context
->Esp
;
1201 CS_sig(sigcontext
) = context
->SegCs
;
1202 DS_sig(sigcontext
) = context
->SegDs
;
1203 ES_sig(sigcontext
) = context
->SegEs
;
1204 SS_sig(sigcontext
) = context
->SegSs
;
1206 GS_sig(sigcontext
) = context
->SegGs
;
1208 wine_set_gs( context
->SegGs
);
1211 FS_sig(sigcontext
) = context
->SegFs
;
1213 wine_set_fs( context
->SegFs
);
1216 if (fpu
) *fpu
= context
->FloatSave
;
1217 if (fpux
) memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
1218 if (!fpu
&& !fpux
) restore_fpu( context
);
1222 /***********************************************************************
1223 * RtlCaptureContext (NTDLL.@)
1225 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
1227 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1228 "movl 8(%esp),%eax\n\t" /* context */
1229 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1230 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1231 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1232 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1233 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1234 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1235 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1236 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1237 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1238 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1239 "movl 0(%ebp),%edx\n\t"
1240 "movl %edx,0xb4(%eax)\n\t" /* context->Ebp */
1241 "movl 4(%ebp),%edx\n\t"
1242 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1243 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1245 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1246 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1247 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1248 "leal 8(%ebp),%edx\n\t"
1249 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1250 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1251 "popl 0xb0(%eax)\n\t" /* context->Eax */
1252 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1255 /***********************************************************************
1256 * set_full_cpu_context
1258 * Set the new CPU context.
1260 extern void set_full_cpu_context( const CONTEXT
*context
);
1261 __ASM_GLOBAL_FUNC( set_full_cpu_context
,
1262 "movl 4(%esp),%ecx\n\t"
1263 "movw 0x8c(%ecx),%gs\n\t" /* SegGs */
1264 "movw 0x90(%ecx),%fs\n\t" /* SegFs */
1265 "movw 0x94(%ecx),%es\n\t" /* SegEs */
1266 "movl 0x9c(%ecx),%edi\n\t" /* Edi */
1267 "movl 0xa0(%ecx),%esi\n\t" /* Esi */
1268 "movl 0xa4(%ecx),%ebx\n\t" /* Ebx */
1269 "movl 0xb4(%ecx),%ebp\n\t" /* Ebp */
1271 "cmpw 0xc8(%ecx),%ax\n\t" /* SegSs */
1273 /* As soon as we have switched stacks the context structure could
1274 * be invalid (when signal handlers are executed for example). Copy
1275 * values on the target stack before changing ESP. */
1276 "movl 0xc4(%ecx),%eax\n\t" /* Esp */
1277 "leal -4*4(%eax),%eax\n\t"
1278 "movl 0xc0(%ecx),%edx\n\t" /* EFlags */
1279 "movl %edx,3*4(%eax)\n\t"
1280 "movl 0xbc(%ecx),%edx\n\t" /* SegCs */
1281 "movl %edx,2*4(%eax)\n\t"
1282 "movl 0xb8(%ecx),%edx\n\t" /* Eip */
1283 "movl %edx,1*4(%eax)\n\t"
1284 "movl 0xb0(%ecx),%edx\n\t" /* Eax */
1285 "movl %edx,0*4(%eax)\n\t"
1286 "pushl 0x98(%ecx)\n\t" /* SegDs */
1287 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1288 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1290 "movl %eax,%esp\n\t"
1293 /* Restore the context when the stack segment changes. We can't use
1294 * the same code as above because we do not know if the stack segment
1295 * is 16 or 32 bit, and 'movl' will throw an exception when we try to
1296 * access memory above the limit. */
1298 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1299 "movl 0xb0(%ecx),%eax\n\t" /* Eax */
1300 "movw 0xc8(%ecx),%ss\n\t" /* SegSs */
1301 "movl 0xc4(%ecx),%esp\n\t" /* Esp */
1302 "pushl 0xc0(%ecx)\n\t" /* EFlags */
1303 "pushl 0xbc(%ecx)\n\t" /* SegCs */
1304 "pushl 0xb8(%ecx)\n\t" /* Eip */
1305 "pushl 0x98(%ecx)\n\t" /* SegDs */
1306 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1311 /***********************************************************************
1314 * Set the new CPU context. Used by NtSetContextThread.
1316 void DECLSPEC_HIDDEN
set_cpu_context( const CONTEXT
*context
)
1318 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1320 if ((flags
& CONTEXT_EXTENDED_REGISTERS
) && fpux_support
) restore_fpux( context
);
1321 else if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
1323 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1325 x86_thread_data()->dr0
= context
->Dr0
;
1326 x86_thread_data()->dr1
= context
->Dr1
;
1327 x86_thread_data()->dr2
= context
->Dr2
;
1328 x86_thread_data()->dr3
= context
->Dr3
;
1329 x86_thread_data()->dr6
= context
->Dr6
;
1330 x86_thread_data()->dr7
= context
->Dr7
;
1332 if (flags
& CONTEXT_FULL
)
1334 if (!(flags
& CONTEXT_CONTROL
))
1335 FIXME( "setting partial context (%x) not supported\n", flags
);
1336 else if (flags
& CONTEXT_SEGMENTS
)
1337 set_full_cpu_context( context
);
1340 CONTEXT newcontext
= *context
;
1341 newcontext
.SegDs
= wine_get_ds();
1342 newcontext
.SegEs
= wine_get_es();
1343 newcontext
.SegFs
= wine_get_fs();
1344 newcontext
.SegGs
= wine_get_gs();
1345 set_full_cpu_context( &newcontext
);
1351 /***********************************************************************
1354 * Convert a register context to the server format.
1356 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1358 DWORD flags
= from
->ContextFlags
& ~CONTEXT_i386
; /* get rid of CPU id */
1360 memset( to
, 0, sizeof(*to
) );
1363 if (flags
& CONTEXT_CONTROL
)
1365 to
->flags
|= SERVER_CTX_CONTROL
;
1366 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
1367 to
->ctl
.i386_regs
.esp
= from
->Esp
;
1368 to
->ctl
.i386_regs
.eip
= from
->Eip
;
1369 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
1370 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
1371 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
1373 if (flags
& CONTEXT_INTEGER
)
1375 to
->flags
|= SERVER_CTX_INTEGER
;
1376 to
->integer
.i386_regs
.eax
= from
->Eax
;
1377 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
1378 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
1379 to
->integer
.i386_regs
.edx
= from
->Edx
;
1380 to
->integer
.i386_regs
.esi
= from
->Esi
;
1381 to
->integer
.i386_regs
.edi
= from
->Edi
;
1383 if (flags
& CONTEXT_SEGMENTS
)
1385 to
->flags
|= SERVER_CTX_SEGMENTS
;
1386 to
->seg
.i386_regs
.ds
= from
->SegDs
;
1387 to
->seg
.i386_regs
.es
= from
->SegEs
;
1388 to
->seg
.i386_regs
.fs
= from
->SegFs
;
1389 to
->seg
.i386_regs
.gs
= from
->SegGs
;
1391 if (flags
& CONTEXT_FLOATING_POINT
)
1393 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1394 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
1395 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
1396 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
1397 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
1398 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
1399 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
1400 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
1401 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
1402 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
1404 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1406 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1407 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
1408 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
1409 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
1410 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
1411 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
1412 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
1414 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1416 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
1417 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
1419 return STATUS_SUCCESS
;
1423 /***********************************************************************
1424 * context_from_server
1426 * Convert a register context from the server format.
1428 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1430 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
1432 to
->ContextFlags
= CONTEXT_i386
;
1433 if (from
->flags
& SERVER_CTX_CONTROL
)
1435 to
->ContextFlags
|= CONTEXT_CONTROL
;
1436 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
1437 to
->Esp
= from
->ctl
.i386_regs
.esp
;
1438 to
->Eip
= from
->ctl
.i386_regs
.eip
;
1439 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
1440 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
1441 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
1443 if (from
->flags
& SERVER_CTX_INTEGER
)
1445 to
->ContextFlags
|= CONTEXT_INTEGER
;
1446 to
->Eax
= from
->integer
.i386_regs
.eax
;
1447 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
1448 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
1449 to
->Edx
= from
->integer
.i386_regs
.edx
;
1450 to
->Esi
= from
->integer
.i386_regs
.esi
;
1451 to
->Edi
= from
->integer
.i386_regs
.edi
;
1453 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1455 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1456 to
->SegDs
= from
->seg
.i386_regs
.ds
;
1457 to
->SegEs
= from
->seg
.i386_regs
.es
;
1458 to
->SegFs
= from
->seg
.i386_regs
.fs
;
1459 to
->SegGs
= from
->seg
.i386_regs
.gs
;
1461 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1463 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1464 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
1465 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
1466 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
1467 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
1468 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
1469 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
1470 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
1471 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
1472 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
1474 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1476 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1477 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
1478 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
1479 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
1480 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
1481 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
1482 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
1484 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
1486 to
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1487 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
1489 return STATUS_SUCCESS
;
1493 /***********************************************************************
1494 * NtSetContextThread (NTDLL.@)
1495 * ZwSetContextThread (NTDLL.@)
1497 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
1499 NTSTATUS ret
= STATUS_SUCCESS
;
1500 BOOL self
= (handle
== GetCurrentThread());
1502 /* debug registers require a server call */
1503 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
1504 self
= (x86_thread_data()->dr0
== context
->Dr0
&&
1505 x86_thread_data()->dr1
== context
->Dr1
&&
1506 x86_thread_data()->dr2
== context
->Dr2
&&
1507 x86_thread_data()->dr3
== context
->Dr3
&&
1508 x86_thread_data()->dr6
== context
->Dr6
&&
1509 x86_thread_data()->dr7
== context
->Dr7
);
1511 if (!self
) ret
= set_thread_context( handle
, context
, &self
);
1513 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
1518 /***********************************************************************
1519 * NtGetContextThread (NTDLL.@)
1520 * ZwGetContextThread (NTDLL.@)
1522 * Note: we use a small assembly wrapper to save the necessary registers
1523 * in case we are fetching the context of the current thread.
1525 NTSTATUS CDECL DECLSPEC_HIDDEN
__regs_NtGetContextThread( DWORD edi
, DWORD esi
, DWORD ebx
, DWORD eflags
,
1526 DWORD ebp
, DWORD retaddr
, HANDLE handle
,
1530 DWORD needed_flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1531 BOOL self
= (handle
== GetCurrentThread());
1533 /* debug registers require a server call */
1534 if (needed_flags
& CONTEXT_DEBUG_REGISTERS
) self
= FALSE
;
1538 if ((ret
= get_thread_context( handle
, context
, &self
))) return ret
;
1539 needed_flags
&= ~context
->ContextFlags
;
1544 if (needed_flags
& CONTEXT_INTEGER
)
1552 context
->ContextFlags
|= CONTEXT_INTEGER
;
1554 if (needed_flags
& CONTEXT_CONTROL
)
1557 context
->Esp
= (DWORD
)&retaddr
;
1558 context
->Eip
= *(&edi
- 1);
1559 context
->SegCs
= wine_get_cs();
1560 context
->SegSs
= wine_get_ss();
1561 context
->EFlags
= eflags
;
1562 context
->ContextFlags
|= CONTEXT_CONTROL
;
1564 if (needed_flags
& CONTEXT_SEGMENTS
)
1566 context
->SegDs
= wine_get_ds();
1567 context
->SegEs
= wine_get_es();
1568 context
->SegFs
= wine_get_fs();
1569 context
->SegGs
= wine_get_gs();
1570 context
->ContextFlags
|= CONTEXT_SEGMENTS
;
1572 if (needed_flags
& CONTEXT_FLOATING_POINT
) save_fpu( context
);
1573 /* FIXME: extended floating point */
1574 /* update the cached version of the debug registers */
1575 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1577 x86_thread_data()->dr0
= context
->Dr0
;
1578 x86_thread_data()->dr1
= context
->Dr1
;
1579 x86_thread_data()->dr2
= context
->Dr2
;
1580 x86_thread_data()->dr3
= context
->Dr3
;
1581 x86_thread_data()->dr6
= context
->Dr6
;
1582 x86_thread_data()->dr7
= context
->Dr7
;
1586 if (context
->ContextFlags
& (CONTEXT_INTEGER
& ~CONTEXT_i386
))
1587 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle
,
1588 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
1589 if (context
->ContextFlags
& (CONTEXT_CONTROL
& ~CONTEXT_i386
))
1590 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle
,
1591 context
->Ebp
, context
->Esp
, context
->Eip
, context
->SegCs
, context
->SegSs
, context
->EFlags
);
1592 if (context
->ContextFlags
& (CONTEXT_SEGMENTS
& ~CONTEXT_i386
))
1593 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle
,
1594 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
);
1595 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1596 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle
,
1597 context
->Dr0
, context
->Dr1
, context
->Dr2
, context
->Dr3
, context
->Dr6
, context
->Dr7
);
1599 return STATUS_SUCCESS
;
1601 __ASM_STDCALL_FUNC( NtGetContextThread
, 8,
1603 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1604 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1605 "movl %esp,%ebp\n\t"
1606 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1609 __ASM_CFI(".cfi_rel_offset %ebx,-8\n\t")
1611 __ASM_CFI(".cfi_rel_offset %esi,-12\n\t")
1613 __ASM_CFI(".cfi_rel_offset %edi,-16\n\t")
1614 "call " __ASM_NAME("__regs_NtGetContextThread") "\n\t"
1616 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1617 __ASM_CFI(".cfi_same_value %ebp\n\t")
1621 /***********************************************************************
1622 * is_privileged_instr
1624 * Check if the fault location is a privileged instruction.
1625 * Based on the instruction emulation code in dlls/kernel/instr.c.
1627 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1630 unsigned int prefix_count
= 0;
1632 if (!wine_ldt_is_system( context
->SegCs
)) return 0;
1633 instr
= (BYTE
*)context
->Eip
;
1635 for (;;) switch(*instr
)
1637 /* instruction prefixes */
1638 case 0x2e: /* %cs: */
1639 case 0x36: /* %ss: */
1640 case 0x3e: /* %ds: */
1641 case 0x26: /* %es: */
1642 case 0x64: /* %fs: */
1643 case 0x65: /* %gs: */
1644 case 0x66: /* opcode size */
1645 case 0x67: /* addr size */
1646 case 0xf0: /* lock */
1647 case 0xf2: /* repne */
1648 case 0xf3: /* repe */
1649 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1653 case 0x0f: /* extended instruction */
1656 case 0x20: /* mov crX, reg */
1657 case 0x21: /* mov drX, reg */
1658 case 0x22: /* mov reg, crX */
1659 case 0x23: /* mov reg drX */
1660 return EXCEPTION_PRIV_INSTRUCTION
;
1663 case 0x6c: /* insb (%dx) */
1664 case 0x6d: /* insl (%dx) */
1665 case 0x6e: /* outsb (%dx) */
1666 case 0x6f: /* outsl (%dx) */
1667 case 0xcd: /* int $xx */
1668 case 0xe4: /* inb al,XX */
1669 case 0xe5: /* in (e)ax,XX */
1670 case 0xe6: /* outb XX,al */
1671 case 0xe7: /* out XX,(e)ax */
1672 case 0xec: /* inb (%dx),%al */
1673 case 0xed: /* inl (%dx),%eax */
1674 case 0xee: /* outb %al,(%dx) */
1675 case 0xef: /* outl %eax,(%dx) */
1676 case 0xf4: /* hlt */
1677 case 0xfa: /* cli */
1678 case 0xfb: /* sti */
1679 return EXCEPTION_PRIV_INSTRUCTION
;
1686 /***********************************************************************
1689 * Handle an interrupt.
1691 static inline BOOL
handle_interrupt( unsigned int interrupt
, EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1697 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1698 rec
->ExceptionAddress
= (void *)context
->Eip
;
1699 rec
->NumberParameters
= is_wow64
? 1 : 3;
1700 rec
->ExceptionInformation
[0] = context
->Eax
;
1701 rec
->ExceptionInformation
[1] = context
->Ecx
;
1702 rec
->ExceptionInformation
[2] = context
->Edx
;
1710 /***********************************************************************
1713 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1715 static inline BOOL
check_invalid_gs( CONTEXT
*context
)
1717 unsigned int prefix_count
= 0;
1718 const BYTE
*instr
= (BYTE
*)context
->Eip
;
1719 WORD system_gs
= x86_thread_data()->gs
;
1721 if (context
->SegGs
== system_gs
) return FALSE
;
1722 if (!wine_ldt_is_system( context
->SegCs
)) return FALSE
;
1723 /* only handle faults in system libraries */
1724 if (virtual_is_valid_code_address( instr
, 1 )) return FALSE
;
1726 for (;;) switch(*instr
)
1728 /* instruction prefixes */
1729 case 0x2e: /* %cs: */
1730 case 0x36: /* %ss: */
1731 case 0x3e: /* %ds: */
1732 case 0x26: /* %es: */
1733 case 0x64: /* %fs: */
1734 case 0x66: /* opcode size */
1735 case 0x67: /* addr size */
1736 case 0xf0: /* lock */
1737 case 0xf2: /* repne */
1738 case 0xf3: /* repe */
1739 if (++prefix_count
>= 15) return FALSE
;
1742 case 0x65: /* %gs: */
1743 TRACE( "%04x/%04x at %p, fixing up\n", context
->SegGs
, system_gs
, instr
);
1744 context
->SegGs
= system_gs
;
1752 #include "pshpack1.h"
1757 DWORD movl
; /* movl this,4(%esp) */
1759 BYTE jmp
; /* jmp func */
1764 BYTE movl
; /* movl this,ecx */
1766 BYTE jmp
; /* jmp func */
1771 BYTE movl1
; /* movl this,edx */
1773 BYTE movl2
; /* movl func,ecx */
1775 WORD jmp
; /* jmp ecx */
1779 BYTE movl1
; /* movl this,ecx */
1781 BYTE movl2
; /* movl func,eax */
1783 WORD jmp
; /* jmp eax */
1787 DWORD inst1
; /* pop ecx
1794 #include "poppack.h"
1796 /**********************************************************************
1799 * Check if code destination is an ATL thunk, and emulate it if so.
1801 static BOOL
check_atl_thunk( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1803 const union atl_thunk
*thunk
= (const union atl_thunk
*)rec
->ExceptionInformation
[1];
1804 union atl_thunk thunk_copy
;
1807 thunk_len
= virtual_uninterrupted_read_memory( thunk
, &thunk_copy
, sizeof(*thunk
) );
1808 if (!thunk_len
) return FALSE
;
1810 if (thunk_len
>= sizeof(thunk_copy
.t1
) && thunk_copy
.t1
.movl
== 0x042444c7 &&
1811 thunk_copy
.t1
.jmp
== 0xe9)
1813 if (!virtual_uninterrupted_write_memory( (DWORD
*)context
->Esp
+ 1,
1814 &thunk_copy
.t1
.this, sizeof(DWORD
) ))
1816 context
->Eip
= (DWORD_PTR
)(&thunk
->t1
.func
+ 1) + thunk_copy
.t1
.func
;
1817 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1818 thunk
, context
->Eip
, thunk_copy
.t1
.this );
1822 else if (thunk_len
>= sizeof(thunk_copy
.t2
) && thunk_copy
.t2
.movl
== 0xb9 &&
1823 thunk_copy
.t2
.jmp
== 0xe9)
1825 context
->Ecx
= thunk_copy
.t2
.this;
1826 context
->Eip
= (DWORD_PTR
)(&thunk
->t2
.func
+ 1) + thunk_copy
.t2
.func
;
1827 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1828 thunk
, context
->Eip
, context
->Ecx
);
1831 else if (thunk_len
>= sizeof(thunk_copy
.t3
) && thunk_copy
.t3
.movl1
== 0xba &&
1832 thunk_copy
.t3
.movl2
== 0xb9 &&
1833 thunk_copy
.t3
.jmp
== 0xe1ff)
1835 context
->Edx
= thunk_copy
.t3
.this;
1836 context
->Ecx
= thunk_copy
.t3
.func
;
1837 context
->Eip
= thunk_copy
.t3
.func
;
1838 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1839 thunk
, context
->Eip
, context
->Ecx
, context
->Edx
);
1842 else if (thunk_len
>= sizeof(thunk_copy
.t4
) && thunk_copy
.t4
.movl1
== 0xb9 &&
1843 thunk_copy
.t4
.movl2
== 0xb8 &&
1844 thunk_copy
.t4
.jmp
== 0xe0ff)
1846 context
->Ecx
= thunk_copy
.t4
.this;
1847 context
->Eax
= thunk_copy
.t4
.func
;
1848 context
->Eip
= thunk_copy
.t4
.func
;
1849 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1850 thunk
, context
->Eip
, context
->Eax
, context
->Ecx
);
1853 else if (thunk_len
>= sizeof(thunk_copy
.t5
) && thunk_copy
.t5
.inst1
== 0xff515859 &&
1854 thunk_copy
.t5
.inst2
== 0x0460)
1856 DWORD func
, stack
[2];
1857 if (virtual_uninterrupted_read_memory( (DWORD
*)context
->Esp
,
1858 stack
, sizeof(stack
) ) == sizeof(stack
) &&
1859 virtual_uninterrupted_read_memory( (DWORD
*)stack
[1] + 1,
1860 &func
, sizeof(DWORD
) ) == sizeof(DWORD
) &&
1861 !virtual_uninterrupted_write_memory( (DWORD
*)context
->Esp
+ 1, &stack
[0], sizeof(stack
[0]) ))
1863 context
->Ecx
= stack
[0];
1864 context
->Eax
= stack
[1];
1865 context
->Esp
= context
->Esp
+ sizeof(DWORD
);
1866 context
->Eip
= func
;
1867 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1868 thunk
, context
->Eip
, context
->Eax
, context
->Ecx
, context
->Esp
);
1877 /***********************************************************************
1878 * setup_exception_record
1880 * Setup the exception record and context on the thread stack.
1882 static EXCEPTION_RECORD
*setup_exception_record( ucontext_t
*sigcontext
, void *stack_ptr
,
1883 WORD fs
, WORD gs
, raise_func func
)
1887 void *ret_addr
; /* return address from raise_func */
1888 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_func */
1889 CONTEXT
*context_ptr
; /* second arg for raise_func */
1891 EXCEPTION_RECORD rec
;
1894 } *stack
= stack_ptr
;
1895 DWORD exception_code
= 0;
1897 /* stack sanity checks */
1899 if ((char *)stack
>= (char *)get_signal_stack() &&
1900 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1902 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1903 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1904 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1905 NtCurrentTeb()->Tib
.StackBase
);
1909 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1910 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1911 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1913 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1914 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1915 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1916 NtCurrentTeb()->Tib
.StackBase
);
1918 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1920 /* stack overflow on last page, unrecoverable */
1921 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1922 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1923 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1924 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1925 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1928 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1930 /* stack access below stack limit, may be recoverable */
1931 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1934 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1935 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1936 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1937 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1938 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1943 stack
--; /* push the stack_layout structure */
1944 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1945 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1946 #elif defined(VALGRIND_MAKE_WRITABLE)
1947 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1949 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_func must not return */
1950 stack
->rec_ptr
= &stack
->rec
;
1951 stack
->context_ptr
= &stack
->context
;
1953 stack
->rec
.ExceptionRecord
= NULL
;
1954 stack
->rec
.ExceptionCode
= exception_code
;
1955 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1956 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
1957 stack
->rec
.NumberParameters
= 0;
1959 save_context( &stack
->context
, sigcontext
, fs
, gs
);
1961 /* now modify the sigcontext to return to the raise function */
1962 ESP_sig(sigcontext
) = (DWORD
)stack
;
1963 EIP_sig(sigcontext
) = (DWORD
)func
;
1964 /* clear single-step, direction, and align check flag */
1965 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1966 CS_sig(sigcontext
) = wine_get_cs();
1967 DS_sig(sigcontext
) = wine_get_ds();
1968 ES_sig(sigcontext
) = wine_get_es();
1969 FS_sig(sigcontext
) = wine_get_fs();
1970 GS_sig(sigcontext
) = wine_get_gs();
1971 SS_sig(sigcontext
) = wine_get_ss();
1973 return stack
->rec_ptr
;
1977 /***********************************************************************
1980 * Setup a proper stack frame for the raise function, and modify the
1981 * sigcontext so that the return from the signal handler will call
1982 * the raise function.
1984 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
1987 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1989 return setup_exception_record( sigcontext
, stack
, fs
, gs
, func
);
1993 /***********************************************************************
1994 * get_exception_context
1996 * Get a pointer to the context built by setup_exception.
1998 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
2000 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
2004 /**********************************************************************
2007 * Get the FPU exception code from the FPU status.
2009 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
2011 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
2013 if (status
& 0x01) /* IE */
2015 if (status
& 0x40) /* SF */
2016 return EXCEPTION_FLT_STACK_CHECK
;
2018 return EXCEPTION_FLT_INVALID_OPERATION
;
2020 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
2021 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
2022 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
2023 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
2024 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
2025 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
2029 /**********************************************************************
2030 * raise_segv_exception
2032 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2036 switch(rec
->ExceptionCode
)
2038 case EXCEPTION_ACCESS_VIOLATION
:
2039 if (rec
->NumberParameters
== 2)
2041 if (rec
->ExceptionInformation
[1] == 0xffffffff && check_invalid_gs( context
))
2043 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
2044 rec
->ExceptionInformation
[0], FALSE
)))
2046 if (rec
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
&&
2047 rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
2050 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
2051 &flags
, sizeof(flags
), NULL
);
2053 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
) && check_atl_thunk( rec
, context
))
2056 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
2057 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
))
2058 rec
->ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
2062 case EXCEPTION_DATATYPE_MISALIGNMENT
:
2063 /* FIXME: pass through exception handler first? */
2064 if (context
->EFlags
& 0x00040000)
2066 /* Disable AC flag, return */
2067 context
->EFlags
&= ~0x00040000;
2071 case EXCEPTION_BREAKPOINT
:
2074 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
2075 * service is usually not recognized when called from usermode. */
2076 switch (rec
->ExceptionInformation
[0])
2078 case 1: /* BREAKPOINT_PRINT */
2079 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2080 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2081 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2087 status
= NtRaiseException( rec
, context
, TRUE
);
2088 raise_status( status
, rec
);
2090 set_cpu_context( context
);
2094 /**********************************************************************
2095 * raise_trap_exception
2097 static void WINAPI
raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2101 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
2103 /* when single stepping can't tell whether this is a hw bp or a
2104 * single step interrupt. try to avoid as much overhead as possible
2105 * and only do a server call if there is any hw bp enabled. */
2107 if( !(context
->EFlags
& 0x100) || (x86_thread_data()->dr7
& 0xff) )
2109 /* (possible) hardware breakpoint, fetch the debug registers */
2110 DWORD saved_flags
= context
->ContextFlags
;
2111 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
2112 NtGetContextThread(GetCurrentThread(), context
);
2113 context
->ContextFlags
|= saved_flags
; /* restore flags */
2116 context
->EFlags
&= ~0x100; /* clear single-step flag */
2119 status
= NtRaiseException( rec
, context
, TRUE
);
2120 raise_status( status
, rec
);
2124 /**********************************************************************
2125 * raise_generic_exception
2127 * Generic raise function for exceptions that don't need special treatment.
2129 static void WINAPI
raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2133 status
= NtRaiseException( rec
, context
, TRUE
);
2134 raise_status( status
, rec
);
2139 /**********************************************************************
2140 * raise_vm86_sti_exception
2142 static void WINAPI
raise_vm86_sti_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2144 /* merge_vm86_pending_flags merges the vm86_pending flag in safely */
2145 get_vm86_teb_info()->vm86_pending
|= VIP_FLAG
;
2147 if (x86_thread_data()->vm86_ptr
)
2149 if (((char*)context
->Eip
>= (char*)vm86_return
) &&
2150 ((char*)context
->Eip
<= (char*)vm86_return_end
) &&
2151 (VM86_TYPE(context
->Eax
) != VM86_SIGNAL
)) {
2152 /* exiting from VM86, can't throw */
2155 merge_vm86_pending_flags( rec
);
2157 else if (get_vm86_teb_info()->dpmi_vif
&&
2158 !wine_ldt_is_system(context
->SegCs
) &&
2159 !wine_ldt_is_system(context
->SegSs
))
2161 /* Executing DPMI code and virtual interrupts are enabled. */
2162 get_vm86_teb_info()->vm86_pending
= 0;
2163 NtRaiseException( rec
, context
, TRUE
);
2166 set_cpu_context( context
);
2170 /**********************************************************************
2173 * Handler for SIGUSR2.
2174 * We use it to signal that the running __wine_enter_vm86() should
2175 * immediately set VIP_FLAG, causing pending events to be handled
2176 * as early as possible.
2178 static void usr2_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2180 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_vm86_sti_exception
);
2181 rec
->ExceptionCode
= EXCEPTION_VM86_STI
;
2183 #endif /* __HAVE_VM86 */
2186 /**********************************************************************
2189 * Handler for SIGSEGV and related errors.
2191 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2194 EXCEPTION_RECORD
*rec
;
2195 ucontext_t
*context
= sigcontext
;
2196 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
2198 /* check for exceptions on the signal stack caused by write watches */
2199 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
2200 (char *)stack
>= (char *)get_signal_stack() &&
2201 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
&&
2202 !virtual_handle_fault( siginfo
->si_addr
, (get_error_code(context
) >> 1) & 0x09, TRUE
))
2207 /* check for page fault inside the thread stack */
2208 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
2209 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
2210 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
2211 virtual_handle_stack_fault( siginfo
->si_addr
))
2213 /* check if this was the last guard page */
2214 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
2216 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
2217 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2222 rec
= setup_exception_record( context
, stack
, fs
, gs
, raise_segv_exception
);
2223 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
2225 switch(get_trap_code(context
))
2227 case TRAP_x86_OFLOW
: /* Overflow exception */
2228 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
2230 case TRAP_x86_BOUND
: /* Bound range exception */
2231 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
2233 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
2234 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2236 case TRAP_x86_STKFLT
: /* Stack fault */
2237 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2239 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
2240 case TRAP_x86_PROTFLT
: /* General protection fault */
2241 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2243 CONTEXT
*win_context
= get_exception_context( rec
);
2244 WORD err
= get_error_code(context
);
2245 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( win_context
))) break;
2246 if ((err
& 7) == 2 && handle_interrupt( err
>> 3, rec
, win_context
)) break;
2247 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2248 rec
->NumberParameters
= 2;
2249 rec
->ExceptionInformation
[0] = 0;
2250 /* if error contains a LDT selector, use that as fault address */
2251 if ((err
& 7) == 4 && !wine_ldt_is_system( err
| 7 ))
2252 rec
->ExceptionInformation
[1] = err
& ~7;
2254 rec
->ExceptionInformation
[1] = 0xffffffff;
2257 case TRAP_x86_PAGEFLT
: /* Page fault */
2258 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2259 rec
->NumberParameters
= 2;
2260 rec
->ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
2261 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
2263 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
2264 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
2267 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2269 case TRAP_x86_NMI
: /* NMI interrupt */
2270 case TRAP_x86_DNA
: /* Device not available exception */
2271 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
2272 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
2273 case TRAP_x86_MCHK
: /* Machine check exception */
2274 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
2275 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2281 /**********************************************************************
2284 * Handler for SIGTRAP.
2286 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2288 ucontext_t
*context
= sigcontext
;
2289 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_trap_exception
);
2291 switch(get_trap_code(context
))
2293 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2294 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2296 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2297 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
2300 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
2301 rec
->NumberParameters
= is_wow64
? 1 : 3;
2302 rec
->ExceptionInformation
[0] = 0;
2303 rec
->ExceptionInformation
[1] = 0; /* FIXME */
2304 rec
->ExceptionInformation
[2] = 0; /* FIXME */
2310 /**********************************************************************
2313 * Handler for SIGFPE.
2315 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2317 CONTEXT
*win_context
;
2318 ucontext_t
*context
= sigcontext
;
2319 EXCEPTION_RECORD
*rec
= setup_exception( context
, raise_generic_exception
);
2321 win_context
= get_exception_context( rec
);
2323 switch(get_trap_code(context
))
2325 case TRAP_x86_DIVIDE
: /* Division by zero exception */
2326 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
2328 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
2329 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2331 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
2332 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2333 rec
->ExceptionCode
= get_fpu_code( win_context
);
2334 rec
->ExceptionAddress
= (LPVOID
)win_context
->FloatSave
.ErrorOffset
;
2336 case TRAP_x86_CACHEFLT
: /* SIMD exception */
2338 * Behaviour only tested for divide-by-zero exceptions
2339 * Check for other SIMD exceptions as well */
2340 if(siginfo
->si_code
!= FPE_FLTDIV
&& siginfo
->si_code
!= FPE_FLTINV
)
2341 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2344 rec
->ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
2345 rec
->NumberParameters
= 1;
2346 /* no idea what meaning is actually behind this but that's what native does */
2347 rec
->ExceptionInformation
[0] = 0;
2350 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2351 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2357 /**********************************************************************
2360 * Handler for SIGINT.
2362 * FIXME: should not be calling external functions on the signal stack.
2364 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2367 init_handler( sigcontext
, &fs
, &gs
);
2368 if (!dispatch_signal(SIGINT
))
2370 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2371 rec
->ExceptionCode
= CONTROL_C_EXIT
;
2375 /**********************************************************************
2378 * Handler for SIGABRT.
2380 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2382 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2383 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2384 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
2388 /**********************************************************************
2391 * Handler for SIGQUIT.
2393 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2396 init_handler( sigcontext
, &fs
, &gs
);
2401 /**********************************************************************
2404 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2406 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2411 init_handler( sigcontext
, &fs
, &gs
);
2412 save_context( &context
, sigcontext
, fs
, gs
);
2413 wait_suspend( &context
);
2414 restore_context( &context
, sigcontext
);
2418 /***********************************************************************
2419 * __wine_set_signal_handler (NTDLL.@)
2421 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2423 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
2424 if (handlers
[sig
] != NULL
) return -2;
2425 handlers
[sig
] = wsh
;
2430 /***********************************************************************
2431 * locking for LDT routines
2433 static RTL_CRITICAL_SECTION ldt_section
;
2434 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
2437 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
2438 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
2440 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
2441 static sigset_t ldt_sigset
;
2443 static void ldt_lock(void)
2447 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
2448 RtlEnterCriticalSection( &ldt_section
);
2449 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
2452 static void ldt_unlock(void)
2454 if (ldt_section
.RecursionCount
== 1)
2456 sigset_t sigset
= ldt_sigset
;
2457 RtlLeaveCriticalSection( &ldt_section
);
2458 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
2460 else RtlLeaveCriticalSection( &ldt_section
);
2464 /**********************************************************************
2465 * signal_alloc_thread
2467 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2469 static size_t sigstack_zero_bits
;
2470 struct x86_thread_data
*thread_data
;
2475 if (!sigstack_zero_bits
)
2477 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2478 /* find the first power of two not smaller than min_size */
2479 sigstack_zero_bits
= 12;
2480 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
2481 signal_stack_mask
= (1 << sigstack_zero_bits
) - 1;
2482 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
2485 size
= signal_stack_mask
+ 1;
2486 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
2487 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
2490 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2491 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2492 thread_data
= (struct x86_thread_data
*)(*teb
)->SystemReserved2
;
2493 if (!(thread_data
->fs
= wine_ldt_alloc_fs()))
2496 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
2497 status
= STATUS_TOO_MANY_THREADS
;
2504 /**********************************************************************
2505 * signal_free_thread
2507 void signal_free_thread( TEB
*teb
)
2510 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)teb
->SystemReserved2
;
2512 wine_ldt_free_fs( thread_data
->fs
);
2513 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2517 /**********************************************************************
2518 * signal_init_thread
2520 void signal_init_thread( TEB
*teb
)
2522 const WORD fpu_cw
= 0x27f;
2523 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)teb
->SystemReserved2
;
2528 int mib
[2], val
= 1;
2531 mib
[1] = KERN_THALTSTACK
;
2532 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
2535 ss
.ss_sp
= (char *)teb
+ teb_size
;
2536 ss
.ss_size
= signal_stack_size
;
2538 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2540 wine_ldt_set_base( &fs_entry
, teb
);
2541 wine_ldt_set_limit( &fs_entry
, teb_size
- 1 );
2542 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
2543 wine_ldt_init_fs( thread_data
->fs
, &fs_entry
);
2544 thread_data
->gs
= wine_get_gs();
2547 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2549 FIXME("FPU setup not implemented for this platform.\n");
2553 /**********************************************************************
2554 * signal_init_process
2556 void signal_init_process(void)
2558 struct sigaction sig_act
;
2560 sig_act
.sa_mask
= server_block_set
;
2561 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
2563 sig_act
.sa_flags
|= SA_ONSTACK
;
2566 sig_act
.sa_flags
|= SA_RESTORER
;
2567 sig_act
.sa_restorer
= rt_sigreturn
;
2569 sig_act
.sa_sigaction
= int_handler
;
2570 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2571 sig_act
.sa_sigaction
= fpe_handler
;
2572 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2573 sig_act
.sa_sigaction
= abrt_handler
;
2574 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2575 sig_act
.sa_sigaction
= quit_handler
;
2576 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2577 sig_act
.sa_sigaction
= usr1_handler
;
2578 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2580 sig_act
.sa_sigaction
= segv_handler
;
2581 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2582 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2584 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2588 sig_act
.sa_sigaction
= trap_handler
;
2589 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2593 sig_act
.sa_sigaction
= usr2_handler
;
2594 if (sigaction( SIGUSR2
, &sig_act
, NULL
) == -1) goto error
;
2597 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
2601 perror("sigaction");
2608 /**********************************************************************
2609 * __wine_enter_vm86 (NTDLL.@)
2611 * Enter vm86 mode with the specified register context.
2613 void __wine_enter_vm86( CONTEXT
*context
)
2615 EXCEPTION_RECORD rec
;
2617 struct vm86plus_struct vm86
;
2619 memset( &vm86
, 0, sizeof(vm86
) );
2622 restore_vm86_context( context
, &vm86
);
2624 x86_thread_data()->vm86_ptr
= &vm86
;
2625 merge_vm86_pending_flags( &rec
);
2627 res
= vm86_enter( &x86_thread_data()->vm86_ptr
); /* uses and clears teb->vm86_ptr */
2634 save_vm86_context( context
, &vm86
);
2636 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2637 rec
.ExceptionRecord
= NULL
;
2638 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
2639 rec
.NumberParameters
= 0;
2641 switch(VM86_TYPE(res
))
2643 case VM86_UNKNOWN
: /* unhandled GP fault - IO-instruction or similar */
2644 rec
.ExceptionCode
= EXCEPTION_PRIV_INSTRUCTION
;
2646 case VM86_TRAP
: /* return due to DOS-debugger request */
2647 switch(VM86_ARG(res
))
2649 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2650 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2652 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2653 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
2656 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2660 case VM86_INTx
: /* int3/int x instruction (ARG = x) */
2661 rec
.ExceptionCode
= EXCEPTION_VM86_INTx
;
2662 rec
.NumberParameters
= 1;
2663 rec
.ExceptionInformation
[0] = VM86_ARG(res
);
2665 case VM86_STI
: /* sti/popf/iret instruction enabled virtual interrupts */
2666 context
->EFlags
|= VIF_FLAG
;
2667 context
->EFlags
&= ~VIP_FLAG
;
2668 get_vm86_teb_info()->vm86_pending
= 0;
2669 rec
.ExceptionCode
= EXCEPTION_VM86_STI
;
2671 case VM86_PICRETURN
: /* return due to pending PIC request */
2672 rec
.ExceptionCode
= EXCEPTION_VM86_PICRETURN
;
2674 case VM86_SIGNAL
: /* cannot happen because vm86_enter handles this case */
2676 WINE_ERR( "unhandled result from vm86 mode %x\n", res
);
2679 raise_exception( &rec
, context
, TRUE
);
2683 #else /* __HAVE_VM86 */
2684 /**********************************************************************
2685 * __wine_enter_vm86 (NTDLL.@)
2687 void __wine_enter_vm86( CONTEXT
*context
)
2689 MESSAGE("vm86 mode not supported on this platform\n");
2691 #endif /* __HAVE_VM86 */
2694 /*******************************************************************
2695 * RtlUnwind (NTDLL.@)
2697 void WINAPI DECLSPEC_HIDDEN
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
2698 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
2700 EXCEPTION_RECORD record
;
2701 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
2704 context
->Eax
= (DWORD
)retval
;
2706 /* build an exception record, if we do not have one */
2709 record
.ExceptionCode
= STATUS_UNWIND
;
2710 record
.ExceptionFlags
= 0;
2711 record
.ExceptionRecord
= NULL
;
2712 record
.ExceptionAddress
= (void *)context
->Eip
;
2713 record
.NumberParameters
= 0;
2717 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
2719 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2720 TRACE( "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
2721 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
2722 TRACE( "ebp=%08x esp=%08x eip=%08x cs=%04x ds=%04x fs=%04x gs=%04x flags=%08x\n",
2723 context
->Ebp
, context
->Esp
, context
->Eip
, LOWORD(context
->SegCs
), LOWORD(context
->SegDs
),
2724 LOWORD(context
->SegFs
), LOWORD(context
->SegGs
), context
->EFlags
);
2726 /* get chain of exception frames */
2727 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2728 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
2730 /* Check frame address */
2731 if (pEndFrame
&& (frame
> pEndFrame
))
2732 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
2734 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
2737 TRACE( "calling handler at %p code=%x flags=%x\n",
2738 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2739 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
2740 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
2744 case ExceptionContinueSearch
:
2746 case ExceptionCollidedUnwind
:
2750 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
2753 frame
= __wine_pop_frame( frame
);
2756 NtSetContextThread( GetCurrentThread(), context
);
2758 __ASM_STDCALL_FUNC( RtlUnwind
, 16,
2760 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2761 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2762 "movl %esp,%ebp\n\t"
2763 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2764 "leal -(0x2cc+8)(%esp),%esp\n\t" /* sizeof(CONTEXT) + alignment */
2765 "pushl %esp\n\t" /* context */
2766 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2767 "leal 24(%ebp),%eax\n\t"
2768 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2770 "pushl 20(%ebp)\n\t"
2771 "pushl 16(%ebp)\n\t"
2772 "pushl 12(%ebp)\n\t"
2774 "call " __ASM_NAME("__regs_RtlUnwind") __ASM_STDCALL(20) "\n\t"
2776 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2777 __ASM_CFI(".cfi_same_value %ebp\n\t")
2778 "ret $16" ) /* actually never returns */
2781 /*******************************************************************
2782 * NtRaiseException (NTDLL.@)
2784 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2786 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
2787 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
2792 /***********************************************************************
2793 * RtlRaiseException (NTDLL.@)
2795 __ASM_STDCALL_FUNC( RtlRaiseException
, 4,
2797 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2798 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2799 "movl %esp,%ebp\n\t"
2800 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2801 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
2802 "pushl %esp\n\t" /* context */
2803 "call " __ASM_NAME("RtlCaptureContext") __ASM_STDCALL(4) "\n\t"
2804 "movl 4(%ebp),%eax\n\t" /* return address */
2805 "movl 8(%ebp),%ecx\n\t" /* rec */
2806 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
2807 "leal 12(%ebp),%eax\n\t"
2808 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2809 "movl %esp,%eax\n\t"
2813 "call " __ASM_NAME("NtRaiseException") __ASM_STDCALL(12) "\n\t"
2815 "call " __ASM_NAME("RtlRaiseStatus") __ASM_STDCALL(4) "\n\t"
2817 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2818 __ASM_CFI(".cfi_same_value %ebp\n\t")
2819 "ret $4" ) /* actually never returns */
2822 /*************************************************************************
2823 * RtlCaptureStackBackTrace (NTDLL.@)
2825 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
2831 RtlCaptureContext( &context
);
2832 if (hash
) *hash
= 0;
2833 frame
= (ULONG
*)context
.Ebp
;
2837 if (!is_valid_frame( frame
)) return 0;
2838 frame
= (ULONG
*)*frame
;
2841 for (i
= 0; i
< count
; i
++)
2843 if (!is_valid_frame( frame
)) break;
2844 buffer
[i
] = (void *)frame
[1];
2845 if (hash
) *hash
+= frame
[1];
2846 frame
= (ULONG
*)*frame
;
2852 extern void DECLSPEC_NORETURN
start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
,
2854 __ASM_GLOBAL_FUNC( start_thread
,
2856 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2857 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2858 "movl %esp,%ebp\n\t"
2859 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2861 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2863 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2865 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2866 /* store exit frame */
2867 "movl %ebp,%fs:0x1f4\n\t" /* x86_thread_data()->exit_frame */
2868 /* switch to thread stack */
2869 "movl %fs:4,%eax\n\t" /* NtCurrentTeb()->StackBase */
2870 "leal -0x1000(%eax),%esp\n\t"
2872 "pushl 20(%ebp)\n\t" /* relay */
2873 "pushl 16(%ebp)\n\t" /* suspend */
2874 "pushl 12(%ebp)\n\t" /* arg */
2875 "pushl 8(%ebp)\n\t" /* entry */
2876 "xorl %ebp,%ebp\n\t"
2877 "call " __ASM_NAME("attach_thread") "\n\t"
2878 "movl %eax,%esi\n\t"
2879 "leal -12(%eax),%esp\n\t"
2880 /* clear the stack */
2881 "andl $~0xfff,%eax\n\t" /* round down to page size */
2882 "movl %eax,(%esp)\n\t"
2883 "call " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
2884 /* switch to the initial context */
2885 "movl %esi,(%esp)\n\t"
2886 "call " __ASM_NAME("set_cpu_context") )
2888 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int) );
2889 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
2890 /* fetch exit frame */
2891 "movl %fs:0x1f4,%edx\n\t" /* x86_thread_data()->exit_frame */
2892 "testl %edx,%edx\n\t"
2895 /* switch to exit frame stack */
2896 "1:\tmovl 4(%esp),%eax\n\t"
2897 "movl 8(%esp),%ecx\n\t"
2898 "movl $0,%fs:0x1f4\n\t"
2899 "movl %edx,%ebp\n\t"
2900 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2901 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2902 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2903 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2904 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2905 "leal -20(%ebp),%esp\n\t"
2909 extern void call_thread_entry(void) DECLSPEC_HIDDEN
;
2910 __ASM_GLOBAL_FUNC( call_thread_entry
,
2912 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2913 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2914 "movl %esp,%ebp\n\t"
2915 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2916 "pushl %ebx\n\t" /* arg */
2917 "pushl %eax\n\t" /* entry */
2918 "call " __ASM_NAME("call_thread_func") )
2920 /* wrapper for apps that don't declare the thread function correctly */
2921 extern DWORD
call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry
, void *arg
);
2922 __ASM_GLOBAL_FUNC(call_thread_func_wrapper
,
2924 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2925 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2926 "movl %esp,%ebp\n\t"
2927 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2929 "pushl 12(%ebp)\n\t"
2932 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2933 __ASM_CFI(".cfi_same_value %ebp\n\t")
2936 /***********************************************************************
2939 void DECLSPEC_HIDDEN
call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
)
2943 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
2944 RtlExitUserThread( call_thread_func_wrapper( entry
, arg
));
2946 __EXCEPT(unhandled_exception_filter
)
2948 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2951 abort(); /* should not be reached */
2955 /***********************************************************************
2956 * init_thread_context
2958 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
, void *relay
)
2960 context
->SegCs
= wine_get_cs();
2961 context
->SegDs
= wine_get_ds();
2962 context
->SegEs
= wine_get_es();
2963 context
->SegFs
= wine_get_fs();
2964 context
->SegGs
= wine_get_gs();
2965 context
->SegSs
= wine_get_ss();
2966 context
->EFlags
= 0x202;
2967 context
->Eax
= (DWORD
)entry
;
2968 context
->Ebx
= (DWORD
)arg
;
2969 context
->Esp
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
- 16;
2970 context
->Eip
= (DWORD
)relay
;
2971 context
->FloatSave
.ControlWord
= 0x27f;
2972 ((XMM_SAVE_AREA32
*)context
->ExtendedRegisters
)->ControlWord
= 0x27f;
2976 /***********************************************************************
2979 PCONTEXT DECLSPEC_HIDDEN
attach_thread( LPTHREAD_START_ROUTINE entry
, void *arg
,
2980 BOOL suspend
, void *relay
)
2986 CONTEXT context
= { CONTEXT_ALL
};
2988 init_thread_context( &context
, entry
, arg
, relay
);
2989 wait_suspend( &context
);
2990 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Esp
& ~15) - 1;
2995 ctx
= (CONTEXT
*)((char *)NtCurrentTeb()->Tib
.StackBase
- 16) - 1;
2996 init_thread_context( ctx
, entry
, arg
, relay
);
2998 ctx
->ContextFlags
= CONTEXT_FULL
;
3004 /***********************************************************************
3005 * signal_start_thread
3007 * Thread startup sequence:
3008 * signal_start_thread()
3010 * -> call_thread_entry()
3011 * -> call_thread_func()
3013 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
3015 start_thread( entry
, arg
, suspend
, call_thread_entry
);
3018 /**********************************************************************
3019 * signal_start_process
3021 * Process startup sequence:
3022 * signal_start_process()
3024 * -> kernel32_start_process()
3026 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
3028 start_thread( entry
, NtCurrentTeb()->Peb
, suspend
, kernel32_start_process
);
3032 /***********************************************************************
3033 * signal_exit_thread
3035 void signal_exit_thread( int status
)
3037 call_thread_exit_func( status
, exit_thread
);
3040 /***********************************************************************
3041 * signal_exit_process
3043 void signal_exit_process( int status
)
3045 call_thread_exit_func( status
, exit
);
3048 /**********************************************************************
3049 * DbgBreakPoint (NTDLL.@)
3051 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
3053 /**********************************************************************
3054 * DbgUserBreakPoint (NTDLL.@)
3056 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
3058 /**********************************************************************
3059 * NtCurrentTeb (NTDLL.@)
3061 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
3064 /**************************************************************************
3067 __ASM_STDCALL_FUNC( _chkstk
, 0,
3069 "addl %esp,%eax\n\t"
3070 "xchgl %esp,%eax\n\t"
3071 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
3072 "movl %eax,0(%esp)\n\t"
3075 /**************************************************************************
3076 * _alloca_probe (NTDLL.@)
3078 __ASM_STDCALL_FUNC( _alloca_probe
, 0,
3080 "addl %esp,%eax\n\t"
3081 "xchgl %esp,%eax\n\t"
3082 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
3083 "movl %eax,0(%esp)\n\t"
3087 /**********************************************************************
3088 * EXC_CallHandler (internal)
3090 * Some exception handlers depend on EBP to have a fixed position relative to
3091 * the exception frame.
3092 * Shrinker depends on (*1) doing what it does,
3093 * (*2) being the exact instruction it is and (*3) beginning with 0x64
3094 * (i.e. the %fs prefix to the movl instruction). It also depends on the
3095 * function calling the handler having only 5 parameters (*4).
3097 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
3099 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3100 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3101 "movl %esp,%ebp\n\t"
3102 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3104 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
3105 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
3106 "pushl 24(%ebp)\n\t"
3107 "pushl 20(%ebp)\n\t"
3108 "pushl 16(%ebp)\n\t"
3109 "pushl 12(%ebp)\n\t"
3111 "call " __ASM_NAME("call_exception_handler") "\n\t"
3113 __ASM_CFI(".cfi_same_value %ebx\n\t")
3115 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3116 __ASM_CFI(".cfi_same_value %ebp\n\t")
3118 __ASM_GLOBAL_FUNC(call_exception_handler
,
3120 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3121 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3122 "movl %esp,%ebp\n\t"
3123 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3125 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
3126 "pushl %edx\n\t" /* handler be handled by... */
3128 "pushl (0)\n\t" /* nested_handler (passed in edx). */
3130 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
3131 "pushl 20(%ebp)\n\t"
3132 "pushl 16(%ebp)\n\t"
3133 "pushl 12(%ebp)\n\t"
3135 "movl 24(%ebp), %ecx\n\t" /* (*1) */
3136 "call *%ecx\n\t" /* call handler. (*2) */
3138 "movl (0), %esp\n\t" /* restore previous... (*3) */
3140 "popl (0)\n\t" /* exception frame. */
3141 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
3143 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3144 __ASM_CFI(".cfi_same_value %ebp\n\t")
3145 "ret $20" ) /* (*4) */
3147 #endif /* __i386__ */