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_SIGNAL_H
46 # include <sys/signal.h>
48 #ifdef HAVE_SYS_SYSCTL_H
49 # include <sys/sysctl.h>
51 #ifdef HAVE_SYS_UCONTEXT_H
52 # include <sys/ucontext.h>
56 #define WIN32_NO_STATUS
58 #include "wine/library.h"
59 #include "ntdll_misc.h"
60 #include "wine/exception.h"
61 #include "wine/debug.h"
63 #ifdef HAVE_VALGRIND_MEMCHECK_H
64 #include <valgrind/memcheck.h>
67 #undef ERR /* Solaris needs to define this */
69 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
70 WINE_DECLARE_DEBUG_CHANNEL(relay
);
72 /* not defined for x86, so copy the x86_64 definition */
73 typedef struct DECLSPEC_ALIGN(16) _M128A
94 M128A FloatRegisters
[8];
95 M128A XmmRegisters
[16];
99 /***********************************************************************
100 * signal context platform-specific definitions
105 #ifndef HAVE_SYS_UCONTEXT_H
109 REG_GS
, REG_FS
, REG_ES
, REG_DS
, REG_EDI
, REG_ESI
, REG_EBP
, REG_ESP
,
110 REG_EBX
, REG_EDX
, REG_ECX
, REG_EAX
, REG_TRAPNO
, REG_ERR
, REG_EIP
,
111 REG_CS
, REG_EFL
, REG_UESP
, REG_SS
, NGREG
115 typedef greg_t gregset_t
[NGREG
];
119 unsigned short significand
[4];
120 unsigned short exponent
;
130 unsigned long dataoff
;
131 unsigned long datasel
;
132 struct _libc_fpreg _st
[8];
133 unsigned long status
;
136 typedef struct _libc_fpstate
* fpregset_t
;
142 unsigned long oldmask
;
146 typedef struct ucontext
148 unsigned long uc_flags
;
149 struct ucontext
*uc_link
;
151 mcontext_t uc_mcontext
;
154 #endif /* HAVE_SYS_UCONTEXT_H */
156 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
157 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
158 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
159 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
160 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
161 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
162 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
163 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
165 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
166 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
167 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
168 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
169 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
170 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
172 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
173 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
174 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
175 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
177 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
178 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XMM_SAVE_AREA32 *)(FPU_sig(context) + 1) : NULL)
181 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
182 void rt_sigreturn(void);
183 __ASM_GLOBAL_FUNC( rt_sigreturn
,
184 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
190 unsigned int entry_number
;
193 unsigned int seg_32bit
: 1;
194 unsigned int contents
: 2;
195 unsigned int read_exec_only
: 1;
196 unsigned int limit_in_pages
: 1;
197 unsigned int seg_not_present
: 1;
198 unsigned int usable
: 1;
199 unsigned int garbage
: 25;
202 static inline int modify_ldt( int func
, struct modify_ldt_s
*ptr
, unsigned long count
)
204 return syscall( 123 /* SYS_modify_ldt */, func
, ptr
, count
);
207 static inline int set_thread_area( struct modify_ldt_s
*ptr
)
209 return syscall( 243 /* SYS_set_thread_area */, ptr
);
212 #elif defined (__BSDI__)
214 #include <machine/frame.h>
215 typedef struct trapframe ucontext_t
;
217 #define EAX_sig(context) ((context)->tf_eax)
218 #define EBX_sig(context) ((context)->tf_ebx)
219 #define ECX_sig(context) ((context)->tf_ecx)
220 #define EDX_sig(context) ((context)->tf_edx)
221 #define ESI_sig(context) ((context)->tf_esi)
222 #define EDI_sig(context) ((context)->tf_edi)
223 #define EBP_sig(context) ((context)->tf_ebp)
225 #define CS_sig(context) ((context)->tf_cs)
226 #define DS_sig(context) ((context)->tf_ds)
227 #define ES_sig(context) ((context)->tf_es)
228 #define SS_sig(context) ((context)->tf_ss)
230 #define EFL_sig(context) ((context)->tf_eflags)
232 #define EIP_sig(context) (*((unsigned long*)&(context)->tf_eip))
233 #define ESP_sig(context) (*((unsigned long*)&(context)->tf_esp))
235 #define FPU_sig(context) NULL /* FIXME */
236 #define FPUX_sig(context) NULL /* FIXME */
238 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
240 #include <machine/trap.h>
241 #include <machine/segments.h>
242 #include <machine/sysarch.h>
244 #define EAX_sig(context) ((context)->uc_mcontext.mc_eax)
245 #define EBX_sig(context) ((context)->uc_mcontext.mc_ebx)
246 #define ECX_sig(context) ((context)->uc_mcontext.mc_ecx)
247 #define EDX_sig(context) ((context)->uc_mcontext.mc_edx)
248 #define ESI_sig(context) ((context)->uc_mcontext.mc_esi)
249 #define EDI_sig(context) ((context)->uc_mcontext.mc_edi)
250 #define EBP_sig(context) ((context)->uc_mcontext.mc_ebp)
252 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
253 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
254 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
255 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
256 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
257 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
259 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
260 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
261 #define EFL_sig(context) ((context)->uc_mcontext.mc_eflags)
263 #define EIP_sig(context) ((context)->uc_mcontext.mc_eip)
264 #define ESP_sig(context) ((context)->uc_mcontext.mc_esp)
266 #define FPU_sig(context) NULL /* FIXME */
267 #define FPUX_sig(context) NULL /* FIXME */
269 #elif defined (__OpenBSD__)
271 #include <machine/segments.h>
272 #include <machine/sysarch.h>
274 #define EAX_sig(context) ((context)->sc_eax)
275 #define EBX_sig(context) ((context)->sc_ebx)
276 #define ECX_sig(context) ((context)->sc_ecx)
277 #define EDX_sig(context) ((context)->sc_edx)
278 #define ESI_sig(context) ((context)->sc_esi)
279 #define EDI_sig(context) ((context)->sc_edi)
280 #define EBP_sig(context) ((context)->sc_ebp)
282 #define CS_sig(context) ((context)->sc_cs)
283 #define DS_sig(context) ((context)->sc_ds)
284 #define ES_sig(context) ((context)->sc_es)
285 #define FS_sig(context) ((context)->sc_fs)
286 #define GS_sig(context) ((context)->sc_gs)
287 #define SS_sig(context) ((context)->sc_ss)
289 #define TRAP_sig(context) ((context)->sc_trapno)
290 #define ERROR_sig(context) ((context)->sc_err)
291 #define EFL_sig(context) ((context)->sc_eflags)
293 #define EIP_sig(context) ((context)->sc_eip)
294 #define ESP_sig(context) ((context)->sc_esp)
296 #define FPU_sig(context) NULL /* FIXME */
297 #define FPUX_sig(context) NULL /* FIXME */
299 #define T_MCHK T_MACHK
300 #define T_XMMFLT T_XFTRAP
302 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
304 #if defined(_SCO_DS) || defined(__sun)
305 #include <sys/regset.h>
312 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
313 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
314 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
315 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
316 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
317 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
318 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
320 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
321 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
322 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
323 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
325 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
326 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
328 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
330 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
332 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
334 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
336 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
339 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
342 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
345 #define FPU_sig(context) NULL /* FIXME */
346 #define FPUX_sig(context) NULL /* FIXME */
348 #elif defined (__APPLE__)
350 #include <i386/user_ldt.h>
352 /* work around silly renaming of struct members in OS X 10.5 */
353 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
354 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
355 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
356 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
357 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
358 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
359 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
360 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
361 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
362 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
363 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
364 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
365 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
366 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
367 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
368 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
369 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
370 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
371 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
372 #define FPU_sig(context) NULL
373 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
375 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
376 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
377 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
378 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
379 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
380 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
381 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
382 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
383 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
384 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
385 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
386 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
387 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
388 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
389 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
390 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
391 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
392 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
393 #define FPU_sig(context) NULL
394 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->fs.fpu_fcw)
397 #elif defined(__NetBSD__)
399 #include <machine/segments.h>
400 #include <machine/sysarch.h>
402 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
403 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
404 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
405 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
406 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
407 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
408 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
409 #define ESP_sig(context) _UC_MACHINE_SP(context)
411 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
412 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
413 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
414 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
415 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
416 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
418 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
419 #define EIP_sig(context) _UC_MACHINE_PC(context)
420 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
421 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
423 #define FPU_sig(context) NULL
424 #define FPUX_sig(context) ((XMM_SAVE_AREA32 *)&((context)->uc_mcontext.__fpregs))
427 #define T_XMMFLT T_XMM
429 #elif defined(__GNU__)
431 #include <mach/i386/mach_i386.h>
432 #include <mach/mach_traps.h>
434 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
435 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
436 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
437 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
438 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
439 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
440 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
441 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
443 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
444 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
445 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
446 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
447 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
448 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
450 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
451 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
452 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
453 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
455 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
456 #define FPUX_sig(context) NULL
459 #error You must define the signal context functions for your platform
462 /* stack layout when calling an exception raise function */
465 void *ret_addr
; /* return address from raise_generic_exception */
466 EXCEPTION_RECORD
*rec_ptr
; /* first arg for raise_generic_exception */
467 CONTEXT
*context_ptr
; /* second arg for raise_generic_exception */
469 EXCEPTION_RECORD rec
;
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 ULONG first_ldt_entry
= 32;
482 static wine_signal_handler handlers
[256];
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 */
542 /* the ntdll_thread_data structure follows here */
545 C_ASSERT( offsetof( TEB
, SystemReserved2
) + offsetof( struct x86_thread_data
, gs
) == 0x1d8 );
546 C_ASSERT( offsetof( TEB
, SystemReserved2
) + offsetof( struct x86_thread_data
, exit_frame
) == 0x1f4 );
548 static inline struct x86_thread_data
*x86_thread_data(void)
550 return (struct x86_thread_data
*)NtCurrentTeb()->SystemReserved2
;
553 static inline WORD
get_cs(void) { WORD res
; __asm__( "movw %%cs,%0" : "=r" (res
) ); return res
; }
554 static inline WORD
get_ds(void) { WORD res
; __asm__( "movw %%ds,%0" : "=r" (res
) ); return res
; }
555 static inline WORD
get_fs(void) { WORD res
; __asm__( "movw %%fs,%0" : "=r" (res
) ); return res
; }
556 static inline WORD
get_gs(void) { WORD res
; __asm__( "movw %%gs,%0" : "=r" (res
) ); return res
; }
557 static inline void set_fs( WORD val
) { __asm__( "mov %0,%%fs" :: "r" (val
)); }
558 static inline void set_gs( WORD val
) { __asm__( "mov %0,%%gs" :: "r" (val
)); }
560 /* Exception record for handling exceptions happening inside exception handlers */
563 EXCEPTION_REGISTRATION_RECORD frame
;
564 EXCEPTION_REGISTRATION_RECORD
*prevFrame
;
567 extern DWORD
EXC_CallHandler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
568 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
,
569 PEXCEPTION_HANDLER handler
, PEXCEPTION_HANDLER nested_handler
);
571 /***********************************************************************
574 static inline int is_gdt_sel( WORD sel
)
579 /***********************************************************************
582 static inline int ldt_is_system( WORD sel
)
584 return is_gdt_sel( sel
) || ((sel
>> 3) < first_ldt_entry
);
587 /***********************************************************************
590 static inline int dispatch_signal(unsigned int sig
)
592 if (handlers
[sig
] == NULL
) return 0;
593 return handlers
[sig
](sig
);
597 /***********************************************************************
600 * Get the trap code for a signal.
602 static inline enum i386_trap_code
get_trap_code( const ucontext_t
*sigcontext
)
605 return TRAP_sig(sigcontext
);
607 return TRAP_x86_UNKNOWN
; /* unknown trap code */
611 /***********************************************************************
614 * Get the error code for a signal.
616 static inline WORD
get_error_code( const ucontext_t
*sigcontext
)
619 return ERROR_sig(sigcontext
);
625 /***********************************************************************
628 * Get the base of the signal stack for the current thread.
630 static inline void *get_signal_stack(void)
632 return (char *)NtCurrentTeb() + 4096;
636 /***********************************************************************
639 static inline int has_fpux(void)
641 return (cpu_info
.FeatureSet
& CPU_FEATURE_FXSR
);
645 /***********************************************************************
648 * Get the current teb based on the stack pointer.
650 static inline TEB
*get_current_teb(void)
653 __asm__("movl %%esp,%0" : "=g" (esp
) );
654 return (TEB
*)(esp
& ~signal_stack_mask
);
658 /*******************************************************************
661 static inline BOOL
is_valid_frame( void *frame
)
663 if ((ULONG_PTR
)frame
& 3) return FALSE
;
664 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
665 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
668 /*******************************************************************
671 * Handler for exceptions happening inside a handler.
673 static DWORD
raise_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
674 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
676 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
677 return ExceptionContinueSearch
;
678 /* We shouldn't get here so we store faulty frame in dispatcher */
679 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
680 return ExceptionNestedException
;
684 /*******************************************************************
687 * Handler for exceptions happening inside an unwind handler.
689 static DWORD
unwind_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
690 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
692 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
693 return ExceptionContinueSearch
;
694 /* We shouldn't get here so we store faulty frame in dispatcher */
695 *dispatcher
= ((EXC_NESTED_FRAME
*)frame
)->prevFrame
;
696 return ExceptionCollidedUnwind
;
700 /**********************************************************************
701 * call_stack_handlers
703 * Call the stack handlers chain.
705 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
707 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
710 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
712 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
714 /* Check frame address */
715 if (!is_valid_frame( frame
))
717 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
722 TRACE( "calling handler at %p code=%x flags=%x\n",
723 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
724 res
= EXC_CallHandler( rec
, frame
, context
, &dispatch
, frame
->Handler
, raise_handler
);
725 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
727 if (frame
== nested_frame
)
729 /* no longer nested */
731 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
736 case ExceptionContinueExecution
:
737 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
738 return STATUS_NONCONTINUABLE_EXCEPTION
;
739 case ExceptionContinueSearch
:
741 case ExceptionNestedException
:
742 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
743 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
746 return STATUS_INVALID_DISPOSITION
;
750 return STATUS_UNHANDLED_EXCEPTION
;
754 /*******************************************************************
757 * Implementation of NtRaiseException.
759 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
767 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
768 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
769 context
->Eip
, GetCurrentThreadId() );
770 for (c
= 0; c
< rec
->NumberParameters
; c
++)
771 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
772 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
774 if (rec
->ExceptionInformation
[1] >> 16)
775 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
776 rec
->ExceptionAddress
,
777 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
779 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
780 rec
->ExceptionAddress
,
781 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
785 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
786 context
->Eax
, context
->Ebx
, context
->Ecx
,
787 context
->Edx
, context
->Esi
, context
->Edi
);
788 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
789 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
790 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
793 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
794 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
796 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
) goto done
;
798 if ((status
= call_stack_handlers( rec
, context
)) == STATUS_SUCCESS
) goto done
;
799 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
802 /* last chance exception */
804 status
= send_debug_event( rec
, FALSE
, context
);
805 if (status
!= DBG_CONTINUE
)
807 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
808 WINE_ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
809 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
810 WINE_ERR("Process attempted to continue execution after noncontinuable exception.\n");
812 WINE_ERR("Unhandled exception code %x flags %x addr %p\n",
813 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
814 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
817 return NtSetContextThread( GetCurrentThread(), context
);
823 /* We have to workaround two Solaris breakages:
824 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
826 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
827 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
830 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
831 __ASM_GLOBAL_FUNC( sigaction_syscall
,
832 "movl $0x62,%eax\n\t"
836 /* assume the same libc handler is used for all signals */
837 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
839 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
841 struct x86_thread_data
*thread_data
;
843 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
845 thread_data
= (struct x86_thread_data
*)get_current_teb()->SystemReserved2
;
846 set_fs( thread_data
->fs
);
847 set_gs( thread_data
->gs
);
849 libc_sigacthandler( signal
, siginfo
, sigcontext
);
852 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
854 struct sigaction real_act
;
856 if (sigaction( sig
, new, old
) == -1) return -1;
858 /* retrieve the real handler and flags with a direct syscall */
859 sigaction_syscall( sig
, NULL
, &real_act
);
860 libc_sigacthandler
= real_act
.sa_sigaction
;
861 real_act
.sa_sigaction
= wine_sigacthandler
;
862 sigaction_syscall( sig
, &real_act
, NULL
);
865 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
869 extern void clear_alignment_flag(void);
870 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
872 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
873 "andl $~0x40000,(%esp)\n\t"
875 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
879 /***********************************************************************
882 * Handler initialization when the full context is not needed.
883 * Return the stack pointer to use for pushing the exception data.
885 static inline void *init_handler( const ucontext_t
*sigcontext
, WORD
*fs
, WORD
*gs
)
887 TEB
*teb
= get_current_teb();
889 clear_alignment_flag();
891 /* get %fs and %gs at time of the fault */
893 *fs
= LOWORD(FS_sig(sigcontext
));
898 *gs
= LOWORD(GS_sig(sigcontext
));
903 #ifndef __sun /* see above for Solaris handling */
905 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)teb
->SystemReserved2
;
906 set_fs( thread_data
->fs
);
907 set_gs( thread_data
->gs
);
911 if (!ldt_is_system(CS_sig(sigcontext
)) || !ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
914 * Win16 or DOS protected mode. Note that during switch
915 * from 16-bit mode to linear mode, CS may be set to system
916 * segment before FS is restored. Fortunately, in this case
917 * SS is still non-system segment. This is why both CS and SS
920 return teb
->WOW32Reserved
;
922 return (void *)(ESP_sig(sigcontext
) & ~3);
926 /***********************************************************************
929 * Save the thread FPU context.
931 static inline void save_fpu( CONTEXT
*context
)
946 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
947 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
949 /* Reset unmasked exceptions status to avoid firing an exception. */
950 memcpy(&float_status
, &context
->FloatSave
, sizeof(float_status
));
951 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
953 __asm__
__volatile__( "fldenv %0" : : "m" (float_status
) );
958 /***********************************************************************
961 * Save the thread FPU extended context.
963 static inline void save_fpux( CONTEXT
*context
)
966 /* we have to enforce alignment by hand */
967 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
968 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
970 if (!has_fpux()) return;
971 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
972 __asm__
__volatile__( "fxsave %0" : "=m" (*state
) );
973 memcpy( context
->ExtendedRegisters
, state
, sizeof(*state
) );
978 /***********************************************************************
981 * Restore the FPU context to a sigcontext.
983 static inline void restore_fpu( const CONTEXT
*context
)
985 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
986 /* reset the current interrupt status */
987 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
989 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
990 #endif /* __GNUC__ */
994 /***********************************************************************
997 * Restore the FPU extended context to a sigcontext.
999 static inline void restore_fpux( const CONTEXT
*context
)
1002 /* we have to enforce alignment by hand */
1003 char buffer
[sizeof(XMM_SAVE_AREA32
) + 16];
1004 XMM_SAVE_AREA32
*state
= (XMM_SAVE_AREA32
*)(((ULONG_PTR
)buffer
+ 15) & ~15);
1006 memcpy( state
, context
->ExtendedRegisters
, sizeof(*state
) );
1007 /* reset the current interrupt status */
1008 state
->StatusWord
&= state
->ControlWord
| 0xff80;
1009 __asm__
__volatile__( "fxrstor %0" : : "m" (*state
) );
1014 /***********************************************************************
1017 * Build a standard FPU context from an extended one.
1019 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XMM_SAVE_AREA32
*fpux
)
1021 unsigned int i
, tag
, stack_top
;
1023 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
1024 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
1025 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
1026 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
1027 fpu
->DataOffset
= fpux
->DataOffset
;
1028 fpu
->DataSelector
= fpux
->DataSelector
;
1029 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
1031 stack_top
= (fpux
->StatusWord
>> 11) & 7;
1032 fpu
->TagWord
= 0xffff0000;
1033 for (i
= 0; i
< 8; i
++)
1035 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
1036 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
1039 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
1040 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
1042 tag
= 2; /* special */
1044 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
1046 if (reg
->Low
) tag
= 2; /* special */
1047 else tag
= 1; /* zero */
1051 if (reg
->Low
>> 63) tag
= 0; /* valid */
1052 else tag
= 2; /* special */
1055 fpu
->TagWord
|= tag
<< (2 * i
);
1060 /***********************************************************************
1063 * Build a context structure from the signal info.
1065 static inline void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
, WORD fs
, WORD gs
)
1067 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1068 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1070 memset(context
, 0, sizeof(*context
));
1071 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
1072 context
->Eax
= EAX_sig(sigcontext
);
1073 context
->Ebx
= EBX_sig(sigcontext
);
1074 context
->Ecx
= ECX_sig(sigcontext
);
1075 context
->Edx
= EDX_sig(sigcontext
);
1076 context
->Esi
= ESI_sig(sigcontext
);
1077 context
->Edi
= EDI_sig(sigcontext
);
1078 context
->Ebp
= EBP_sig(sigcontext
);
1079 context
->EFlags
= EFL_sig(sigcontext
);
1080 context
->Eip
= EIP_sig(sigcontext
);
1081 context
->Esp
= ESP_sig(sigcontext
);
1082 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
1083 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
1084 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
1085 context
->SegFs
= fs
;
1086 context
->SegGs
= gs
;
1087 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
1088 context
->Dr0
= x86_thread_data()->dr0
;
1089 context
->Dr1
= x86_thread_data()->dr1
;
1090 context
->Dr2
= x86_thread_data()->dr2
;
1091 context
->Dr3
= x86_thread_data()->dr3
;
1092 context
->Dr6
= x86_thread_data()->dr6
;
1093 context
->Dr7
= x86_thread_data()->dr7
;
1097 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1098 context
->FloatSave
= *fpu
;
1102 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
1103 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
1104 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
1106 if (!fpu
&& !fpux
) save_fpu( context
);
1110 /***********************************************************************
1113 * Restore the signal info from the context.
1115 static inline void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1117 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
1118 XMM_SAVE_AREA32
*fpux
= FPUX_sig(sigcontext
);
1120 x86_thread_data()->dr0
= context
->Dr0
;
1121 x86_thread_data()->dr1
= context
->Dr1
;
1122 x86_thread_data()->dr2
= context
->Dr2
;
1123 x86_thread_data()->dr3
= context
->Dr3
;
1124 x86_thread_data()->dr6
= context
->Dr6
;
1125 x86_thread_data()->dr7
= context
->Dr7
;
1126 EAX_sig(sigcontext
) = context
->Eax
;
1127 EBX_sig(sigcontext
) = context
->Ebx
;
1128 ECX_sig(sigcontext
) = context
->Ecx
;
1129 EDX_sig(sigcontext
) = context
->Edx
;
1130 ESI_sig(sigcontext
) = context
->Esi
;
1131 EDI_sig(sigcontext
) = context
->Edi
;
1132 EBP_sig(sigcontext
) = context
->Ebp
;
1133 EFL_sig(sigcontext
) = context
->EFlags
;
1134 EIP_sig(sigcontext
) = context
->Eip
;
1135 ESP_sig(sigcontext
) = context
->Esp
;
1136 CS_sig(sigcontext
) = context
->SegCs
;
1137 DS_sig(sigcontext
) = context
->SegDs
;
1138 ES_sig(sigcontext
) = context
->SegEs
;
1139 SS_sig(sigcontext
) = context
->SegSs
;
1141 GS_sig(sigcontext
) = context
->SegGs
;
1143 set_gs( context
->SegGs
);
1146 FS_sig(sigcontext
) = context
->SegFs
;
1148 set_fs( context
->SegFs
);
1151 if (fpu
) *fpu
= context
->FloatSave
;
1152 if (fpux
) memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
1153 if (!fpu
&& !fpux
) restore_fpu( context
);
1157 /***********************************************************************
1158 * RtlCaptureContext (NTDLL.@)
1160 __ASM_STDCALL_FUNC( RtlCaptureContext
, 4,
1162 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1163 "movl 8(%esp),%eax\n\t" /* context */
1164 "movl $0x10007,(%eax)\n\t" /* context->ContextFlags */
1165 "movw %gs,0x8c(%eax)\n\t" /* context->SegGs */
1166 "movw %fs,0x90(%eax)\n\t" /* context->SegFs */
1167 "movw %es,0x94(%eax)\n\t" /* context->SegEs */
1168 "movw %ds,0x98(%eax)\n\t" /* context->SegDs */
1169 "movl %edi,0x9c(%eax)\n\t" /* context->Edi */
1170 "movl %esi,0xa0(%eax)\n\t" /* context->Esi */
1171 "movl %ebx,0xa4(%eax)\n\t" /* context->Ebx */
1172 "movl %edx,0xa8(%eax)\n\t" /* context->Edx */
1173 "movl %ecx,0xac(%eax)\n\t" /* context->Ecx */
1174 "movl 0(%ebp),%edx\n\t"
1175 "movl %edx,0xb4(%eax)\n\t" /* context->Ebp */
1176 "movl 4(%ebp),%edx\n\t"
1177 "movl %edx,0xb8(%eax)\n\t" /* context->Eip */
1178 "movw %cs,0xbc(%eax)\n\t" /* context->SegCs */
1180 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1181 "popl 0xc0(%eax)\n\t" /* context->EFlags */
1182 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1183 "leal 8(%ebp),%edx\n\t"
1184 "movl %edx,0xc4(%eax)\n\t" /* context->Esp */
1185 "movw %ss,0xc8(%eax)\n\t" /* context->SegSs */
1186 "popl 0xb0(%eax)\n\t" /* context->Eax */
1187 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1190 /***********************************************************************
1191 * set_full_cpu_context
1193 * Set the new CPU context.
1195 extern void set_full_cpu_context( const CONTEXT
*context
);
1196 __ASM_GLOBAL_FUNC( set_full_cpu_context
,
1197 "movl 4(%esp),%ecx\n\t"
1198 "movw 0x8c(%ecx),%gs\n\t" /* SegGs */
1199 "movw 0x90(%ecx),%fs\n\t" /* SegFs */
1200 "movw 0x94(%ecx),%es\n\t" /* SegEs */
1201 "movl 0x9c(%ecx),%edi\n\t" /* Edi */
1202 "movl 0xa0(%ecx),%esi\n\t" /* Esi */
1203 "movl 0xa4(%ecx),%ebx\n\t" /* Ebx */
1204 "movl 0xb4(%ecx),%ebp\n\t" /* Ebp */
1206 "cmpw 0xc8(%ecx),%ax\n\t" /* SegSs */
1208 /* As soon as we have switched stacks the context structure could
1209 * be invalid (when signal handlers are executed for example). Copy
1210 * values on the target stack before changing ESP. */
1211 "movl 0xc4(%ecx),%eax\n\t" /* Esp */
1212 "leal -4*4(%eax),%eax\n\t"
1213 "movl 0xc0(%ecx),%edx\n\t" /* EFlags */
1214 "movl %edx,3*4(%eax)\n\t"
1215 "movl 0xbc(%ecx),%edx\n\t" /* SegCs */
1216 "movl %edx,2*4(%eax)\n\t"
1217 "movl 0xb8(%ecx),%edx\n\t" /* Eip */
1218 "movl %edx,1*4(%eax)\n\t"
1219 "movl 0xb0(%ecx),%edx\n\t" /* Eax */
1220 "movl %edx,0*4(%eax)\n\t"
1221 "pushl 0x98(%ecx)\n\t" /* SegDs */
1222 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1223 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1225 "movl %eax,%esp\n\t"
1228 /* Restore the context when the stack segment changes. We can't use
1229 * the same code as above because we do not know if the stack segment
1230 * is 16 or 32 bit, and 'movl' will throw an exception when we try to
1231 * access memory above the limit. */
1233 "movl 0xa8(%ecx),%edx\n\t" /* Edx */
1234 "movl 0xb0(%ecx),%eax\n\t" /* Eax */
1235 "movw 0xc8(%ecx),%ss\n\t" /* SegSs */
1236 "movl 0xc4(%ecx),%esp\n\t" /* Esp */
1237 "pushl 0xc0(%ecx)\n\t" /* EFlags */
1238 "pushl 0xbc(%ecx)\n\t" /* SegCs */
1239 "pushl 0xb8(%ecx)\n\t" /* Eip */
1240 "pushl 0x98(%ecx)\n\t" /* SegDs */
1241 "movl 0xac(%ecx),%ecx\n\t" /* Ecx */
1246 /***********************************************************************
1249 * Set the new CPU context. Used by NtSetContextThread.
1251 void DECLSPEC_HIDDEN
set_cpu_context( const CONTEXT
*context
)
1253 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1255 if ((flags
& CONTEXT_EXTENDED_REGISTERS
) && has_fpux()) restore_fpux( context
);
1256 else if (flags
& CONTEXT_FLOATING_POINT
) restore_fpu( context
);
1258 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1260 x86_thread_data()->dr0
= context
->Dr0
;
1261 x86_thread_data()->dr1
= context
->Dr1
;
1262 x86_thread_data()->dr2
= context
->Dr2
;
1263 x86_thread_data()->dr3
= context
->Dr3
;
1264 x86_thread_data()->dr6
= context
->Dr6
;
1265 x86_thread_data()->dr7
= context
->Dr7
;
1267 if (flags
& CONTEXT_FULL
)
1269 if (!(flags
& CONTEXT_CONTROL
))
1270 FIXME( "setting partial context (%x) not supported\n", flags
);
1271 else if (flags
& CONTEXT_SEGMENTS
)
1272 set_full_cpu_context( context
);
1275 CONTEXT newcontext
= *context
;
1276 newcontext
.SegDs
= get_ds();
1277 newcontext
.SegEs
= get_ds();
1278 newcontext
.SegFs
= get_fs();
1279 newcontext
.SegGs
= get_gs();
1280 set_full_cpu_context( &newcontext
);
1286 /***********************************************************************
1287 * get_server_context_flags
1289 * Convert CPU-specific flags to generic server flags
1291 static unsigned int get_server_context_flags( DWORD flags
)
1293 unsigned int ret
= 0;
1295 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
1296 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
1297 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
1298 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
1299 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
1300 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
1301 if (flags
& CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
1306 /***********************************************************************
1309 * Convert a register context to the server format.
1311 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1313 DWORD flags
= from
->ContextFlags
& ~CONTEXT_i386
; /* get rid of CPU id */
1315 memset( to
, 0, sizeof(*to
) );
1318 if (flags
& CONTEXT_CONTROL
)
1320 to
->flags
|= SERVER_CTX_CONTROL
;
1321 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
1322 to
->ctl
.i386_regs
.esp
= from
->Esp
;
1323 to
->ctl
.i386_regs
.eip
= from
->Eip
;
1324 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
1325 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
1326 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
1328 if (flags
& CONTEXT_INTEGER
)
1330 to
->flags
|= SERVER_CTX_INTEGER
;
1331 to
->integer
.i386_regs
.eax
= from
->Eax
;
1332 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
1333 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
1334 to
->integer
.i386_regs
.edx
= from
->Edx
;
1335 to
->integer
.i386_regs
.esi
= from
->Esi
;
1336 to
->integer
.i386_regs
.edi
= from
->Edi
;
1338 if (flags
& CONTEXT_SEGMENTS
)
1340 to
->flags
|= SERVER_CTX_SEGMENTS
;
1341 to
->seg
.i386_regs
.ds
= from
->SegDs
;
1342 to
->seg
.i386_regs
.es
= from
->SegEs
;
1343 to
->seg
.i386_regs
.fs
= from
->SegFs
;
1344 to
->seg
.i386_regs
.gs
= from
->SegGs
;
1346 if (flags
& CONTEXT_FLOATING_POINT
)
1348 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1349 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
1350 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
1351 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
1352 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
1353 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
1354 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
1355 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
1356 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
1357 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
1359 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1361 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1362 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
1363 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
1364 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
1365 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
1366 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
1367 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
1369 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1371 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
1372 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
1374 return STATUS_SUCCESS
;
1378 /***********************************************************************
1379 * context_from_server
1381 * Convert a register context from the server format.
1383 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1385 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
1387 to
->ContextFlags
= CONTEXT_i386
;
1388 if (from
->flags
& SERVER_CTX_CONTROL
)
1390 to
->ContextFlags
|= CONTEXT_CONTROL
;
1391 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
1392 to
->Esp
= from
->ctl
.i386_regs
.esp
;
1393 to
->Eip
= from
->ctl
.i386_regs
.eip
;
1394 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
1395 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
1396 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
1398 if (from
->flags
& SERVER_CTX_INTEGER
)
1400 to
->ContextFlags
|= CONTEXT_INTEGER
;
1401 to
->Eax
= from
->integer
.i386_regs
.eax
;
1402 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
1403 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
1404 to
->Edx
= from
->integer
.i386_regs
.edx
;
1405 to
->Esi
= from
->integer
.i386_regs
.esi
;
1406 to
->Edi
= from
->integer
.i386_regs
.edi
;
1408 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1410 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1411 to
->SegDs
= from
->seg
.i386_regs
.ds
;
1412 to
->SegEs
= from
->seg
.i386_regs
.es
;
1413 to
->SegFs
= from
->seg
.i386_regs
.fs
;
1414 to
->SegGs
= from
->seg
.i386_regs
.gs
;
1416 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1418 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1419 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
1420 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
1421 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
1422 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
1423 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
1424 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
1425 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
1426 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
1427 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
1429 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1431 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1432 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
1433 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
1434 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
1435 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
1436 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
1437 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
1439 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
1441 to
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1442 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
1444 return STATUS_SUCCESS
;
1448 /***********************************************************************
1449 * NtSetContextThread (NTDLL.@)
1450 * ZwSetContextThread (NTDLL.@)
1452 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
1454 NTSTATUS ret
= STATUS_SUCCESS
;
1455 BOOL self
= (handle
== GetCurrentThread());
1457 /* debug registers require a server call */
1458 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
1459 self
= (x86_thread_data()->dr0
== context
->Dr0
&&
1460 x86_thread_data()->dr1
== context
->Dr1
&&
1461 x86_thread_data()->dr2
== context
->Dr2
&&
1462 x86_thread_data()->dr3
== context
->Dr3
&&
1463 x86_thread_data()->dr6
== context
->Dr6
&&
1464 x86_thread_data()->dr7
== context
->Dr7
);
1468 context_t server_context
;
1469 context_to_server( &server_context
, context
);
1470 ret
= set_thread_context( handle
, &server_context
, &self
);
1473 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
1478 /***********************************************************************
1479 * NtGetContextThread (NTDLL.@)
1480 * ZwGetContextThread (NTDLL.@)
1482 * Note: we use a small assembly wrapper to save the necessary registers
1483 * in case we are fetching the context of the current thread.
1485 NTSTATUS CDECL DECLSPEC_HIDDEN
__regs_NtGetContextThread( DWORD edi
, DWORD esi
, DWORD ebx
, DWORD eflags
,
1486 DWORD ebp
, DWORD retaddr
, HANDLE handle
,
1490 DWORD needed_flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1491 BOOL self
= (handle
== GetCurrentThread());
1493 /* debug registers require a server call */
1494 if (needed_flags
& CONTEXT_DEBUG_REGISTERS
) self
= FALSE
;
1498 context_t server_context
;
1499 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
1501 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
1502 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
1503 needed_flags
&= ~context
->ContextFlags
;
1508 if (needed_flags
& CONTEXT_INTEGER
)
1516 context
->ContextFlags
|= CONTEXT_INTEGER
;
1518 if (needed_flags
& CONTEXT_CONTROL
)
1521 context
->Esp
= (DWORD
)&retaddr
;
1522 context
->Eip
= *(&edi
- 1);
1523 context
->SegCs
= get_cs();
1524 context
->SegSs
= get_ds();
1525 context
->EFlags
= eflags
;
1526 context
->ContextFlags
|= CONTEXT_CONTROL
;
1528 if (needed_flags
& CONTEXT_SEGMENTS
)
1530 context
->SegDs
= get_ds();
1531 context
->SegEs
= get_ds();
1532 context
->SegFs
= get_fs();
1533 context
->SegGs
= get_gs();
1534 context
->ContextFlags
|= CONTEXT_SEGMENTS
;
1536 if (needed_flags
& CONTEXT_FLOATING_POINT
) save_fpu( context
);
1537 if (needed_flags
& CONTEXT_EXTENDED_REGISTERS
) save_fpux( context
);
1539 /* update the cached version of the debug registers */
1540 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1542 x86_thread_data()->dr0
= context
->Dr0
;
1543 x86_thread_data()->dr1
= context
->Dr1
;
1544 x86_thread_data()->dr2
= context
->Dr2
;
1545 x86_thread_data()->dr3
= context
->Dr3
;
1546 x86_thread_data()->dr6
= context
->Dr6
;
1547 x86_thread_data()->dr7
= context
->Dr7
;
1551 if (context
->ContextFlags
& (CONTEXT_INTEGER
& ~CONTEXT_i386
))
1552 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle
,
1553 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
1554 if (context
->ContextFlags
& (CONTEXT_CONTROL
& ~CONTEXT_i386
))
1555 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle
,
1556 context
->Ebp
, context
->Esp
, context
->Eip
, context
->SegCs
, context
->SegSs
, context
->EFlags
);
1557 if (context
->ContextFlags
& (CONTEXT_SEGMENTS
& ~CONTEXT_i386
))
1558 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle
,
1559 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
);
1560 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1561 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle
,
1562 context
->Dr0
, context
->Dr1
, context
->Dr2
, context
->Dr3
, context
->Dr6
, context
->Dr7
);
1564 return STATUS_SUCCESS
;
1566 __ASM_STDCALL_FUNC( NtGetContextThread
, 8,
1568 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1569 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1570 "movl %esp,%ebp\n\t"
1571 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1574 __ASM_CFI(".cfi_rel_offset %ebx,-8\n\t")
1576 __ASM_CFI(".cfi_rel_offset %esi,-12\n\t")
1578 __ASM_CFI(".cfi_rel_offset %edi,-16\n\t")
1579 "call " __ASM_NAME("__regs_NtGetContextThread") "\n\t"
1581 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1582 __ASM_CFI(".cfi_same_value %ebp\n\t")
1586 /***********************************************************************
1587 * is_privileged_instr
1589 * Check if the fault location is a privileged instruction.
1590 * Based on the instruction emulation code in dlls/kernel/instr.c.
1592 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1595 unsigned int i
, len
, prefix_count
= 0;
1597 if (!ldt_is_system( context
->SegCs
)) return 0;
1598 len
= virtual_uninterrupted_read_memory( (BYTE
*)context
->Eip
, instr
, sizeof(instr
) );
1600 for (i
= 0; i
< len
; i
++) switch (instr
[i
])
1602 /* instruction prefixes */
1603 case 0x2e: /* %cs: */
1604 case 0x36: /* %ss: */
1605 case 0x3e: /* %ds: */
1606 case 0x26: /* %es: */
1607 case 0x64: /* %fs: */
1608 case 0x65: /* %gs: */
1609 case 0x66: /* opcode size */
1610 case 0x67: /* addr size */
1611 case 0xf0: /* lock */
1612 case 0xf2: /* repne */
1613 case 0xf3: /* repe */
1614 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1617 case 0x0f: /* extended instruction */
1618 if (i
== len
- 1) return 0;
1619 switch(instr
[i
+ 1])
1621 case 0x20: /* mov crX, reg */
1622 case 0x21: /* mov drX, reg */
1623 case 0x22: /* mov reg, crX */
1624 case 0x23: /* mov reg drX */
1625 return EXCEPTION_PRIV_INSTRUCTION
;
1628 case 0x6c: /* insb (%dx) */
1629 case 0x6d: /* insl (%dx) */
1630 case 0x6e: /* outsb (%dx) */
1631 case 0x6f: /* outsl (%dx) */
1632 case 0xcd: /* int $xx */
1633 case 0xe4: /* inb al,XX */
1634 case 0xe5: /* in (e)ax,XX */
1635 case 0xe6: /* outb XX,al */
1636 case 0xe7: /* out XX,(e)ax */
1637 case 0xec: /* inb (%dx),%al */
1638 case 0xed: /* inl (%dx),%eax */
1639 case 0xee: /* outb %al,(%dx) */
1640 case 0xef: /* outl %eax,(%dx) */
1641 case 0xf4: /* hlt */
1642 case 0xfa: /* cli */
1643 case 0xfb: /* sti */
1644 return EXCEPTION_PRIV_INSTRUCTION
;
1652 /***********************************************************************
1655 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1657 static inline BOOL
check_invalid_gs( ucontext_t
*sigcontext
, CONTEXT
*context
)
1659 unsigned int prefix_count
= 0;
1660 const BYTE
*instr
= (BYTE
*)context
->Eip
;
1661 WORD system_gs
= x86_thread_data()->gs
;
1663 if (context
->SegGs
== system_gs
) return FALSE
;
1664 if (!ldt_is_system( context
->SegCs
)) return FALSE
;
1665 /* only handle faults in system libraries */
1666 if (virtual_is_valid_code_address( instr
, 1 )) return FALSE
;
1668 for (;;) switch(*instr
)
1670 /* instruction prefixes */
1671 case 0x2e: /* %cs: */
1672 case 0x36: /* %ss: */
1673 case 0x3e: /* %ds: */
1674 case 0x26: /* %es: */
1675 case 0x64: /* %fs: */
1676 case 0x66: /* opcode size */
1677 case 0x67: /* addr size */
1678 case 0xf0: /* lock */
1679 case 0xf2: /* repne */
1680 case 0xf3: /* repe */
1681 if (++prefix_count
>= 15) return FALSE
;
1684 case 0x65: /* %gs: */
1685 TRACE( "%04x/%04x at %p, fixing up\n", context
->SegGs
, system_gs
, instr
);
1687 GS_sig(sigcontext
) = system_gs
;
1696 #include "pshpack1.h"
1701 DWORD movl
; /* movl this,4(%esp) */
1703 BYTE jmp
; /* jmp func */
1708 BYTE movl
; /* movl this,ecx */
1710 BYTE jmp
; /* jmp func */
1715 BYTE movl1
; /* movl this,edx */
1717 BYTE movl2
; /* movl func,ecx */
1719 WORD jmp
; /* jmp ecx */
1723 BYTE movl1
; /* movl this,ecx */
1725 BYTE movl2
; /* movl func,eax */
1727 WORD jmp
; /* jmp eax */
1731 DWORD inst1
; /* pop ecx
1738 #include "poppack.h"
1740 /**********************************************************************
1743 * Check if code destination is an ATL thunk, and emulate it if so.
1745 static BOOL
check_atl_thunk( ucontext_t
*sigcontext
, struct stack_layout
*stack
)
1747 const union atl_thunk
*thunk
= (const union atl_thunk
*)stack
->rec
.ExceptionInformation
[1];
1748 union atl_thunk thunk_copy
;
1751 thunk_len
= virtual_uninterrupted_read_memory( thunk
, &thunk_copy
, sizeof(*thunk
) );
1752 if (!thunk_len
) return FALSE
;
1754 if (thunk_len
>= sizeof(thunk_copy
.t1
) && thunk_copy
.t1
.movl
== 0x042444c7 &&
1755 thunk_copy
.t1
.jmp
== 0xe9)
1757 if (!virtual_uninterrupted_write_memory( (DWORD
*)stack
->context
.Esp
+ 1,
1758 &thunk_copy
.t1
.this, sizeof(DWORD
) ))
1760 EIP_sig(sigcontext
) = (DWORD_PTR
)(&thunk
->t1
.func
+ 1) + thunk_copy
.t1
.func
;
1761 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1762 thunk
, EIP_sig(sigcontext
), thunk_copy
.t1
.this );
1766 else if (thunk_len
>= sizeof(thunk_copy
.t2
) && thunk_copy
.t2
.movl
== 0xb9 &&
1767 thunk_copy
.t2
.jmp
== 0xe9)
1769 ECX_sig(sigcontext
) = thunk_copy
.t2
.this;
1770 EIP_sig(sigcontext
) = (DWORD_PTR
)(&thunk
->t2
.func
+ 1) + thunk_copy
.t2
.func
;
1771 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1772 thunk
, EIP_sig(sigcontext
), ECX_sig(sigcontext
) );
1775 else if (thunk_len
>= sizeof(thunk_copy
.t3
) && thunk_copy
.t3
.movl1
== 0xba &&
1776 thunk_copy
.t3
.movl2
== 0xb9 &&
1777 thunk_copy
.t3
.jmp
== 0xe1ff)
1779 EDX_sig(sigcontext
) = thunk_copy
.t3
.this;
1780 ECX_sig(sigcontext
) = thunk_copy
.t3
.func
;
1781 EIP_sig(sigcontext
) = thunk_copy
.t3
.func
;
1782 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1783 thunk
, EIP_sig(sigcontext
), ECX_sig(sigcontext
), EDX_sig(sigcontext
) );
1786 else if (thunk_len
>= sizeof(thunk_copy
.t4
) && thunk_copy
.t4
.movl1
== 0xb9 &&
1787 thunk_copy
.t4
.movl2
== 0xb8 &&
1788 thunk_copy
.t4
.jmp
== 0xe0ff)
1790 ECX_sig(sigcontext
) = thunk_copy
.t4
.this;
1791 EAX_sig(sigcontext
) = thunk_copy
.t4
.func
;
1792 EIP_sig(sigcontext
) = thunk_copy
.t4
.func
;
1793 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1794 thunk
, EIP_sig(sigcontext
), EAX_sig(sigcontext
), ECX_sig(sigcontext
) );
1797 else if (thunk_len
>= sizeof(thunk_copy
.t5
) && thunk_copy
.t5
.inst1
== 0xff515859 &&
1798 thunk_copy
.t5
.inst2
== 0x0460)
1801 if (virtual_uninterrupted_read_memory( (DWORD
*)stack
->context
.Esp
, sp
, sizeof(sp
) ) == sizeof(sp
) &&
1802 virtual_uninterrupted_read_memory( (DWORD
*)sp
[1] + 1, &func
, sizeof(DWORD
) ) == sizeof(DWORD
) &&
1803 !virtual_uninterrupted_write_memory( (DWORD
*)stack
->context
.Esp
+ 1, &sp
[0], sizeof(sp
[0]) ))
1805 ECX_sig(sigcontext
) = sp
[0];
1806 EAX_sig(sigcontext
) = sp
[1];
1807 ESP_sig(sigcontext
) += sizeof(DWORD
);
1808 EIP_sig(sigcontext
) = func
;
1809 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1810 thunk
, EIP_sig(sigcontext
), EAX_sig(sigcontext
),
1811 ECX_sig(sigcontext
), ESP_sig(sigcontext
) );
1820 /***********************************************************************
1821 * setup_exception_record
1823 * Setup the exception record and context on the thread stack.
1825 static struct stack_layout
*setup_exception_record( ucontext_t
*sigcontext
, void *stack_ptr
,
1828 struct stack_layout
*stack
= stack_ptr
;
1829 DWORD exception_code
= 0;
1831 /* stack sanity checks */
1833 if ((char *)stack
>= (char *)get_signal_stack() &&
1834 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1836 WINE_ERR( "nested exception on signal stack in thread %04x eip %08x esp %08x stack %p-%p\n",
1837 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1838 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1839 NtCurrentTeb()->Tib
.StackBase
);
1843 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1844 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1845 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1847 WARN( "exception outside of stack limits in thread %04x eip %08x esp %08x stack %p-%p\n",
1848 GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1849 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->Tib
.StackLimit
,
1850 NtCurrentTeb()->Tib
.StackBase
);
1852 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1854 /* stack overflow on last page, unrecoverable */
1855 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1856 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1857 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1858 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1859 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1862 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1864 /* stack access below stack limit, may be recoverable */
1865 switch (virtual_handle_stack_fault( stack
- 1 ))
1867 case 0: /* not handled */
1869 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1870 WINE_ERR( "stack overflow %u bytes in thread %04x eip %08x esp %08x stack %p-%p-%p\n",
1871 diff
, GetCurrentThreadId(), (unsigned int) EIP_sig(sigcontext
),
1872 (unsigned int) ESP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1873 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1876 case -1: /* overflow */
1877 exception_code
= EXCEPTION_STACK_OVERFLOW
;
1882 stack
--; /* push the stack_layout structure */
1883 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1884 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1885 #elif defined(VALGRIND_MAKE_WRITABLE)
1886 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1888 stack
->rec
.ExceptionRecord
= NULL
;
1889 stack
->rec
.ExceptionCode
= exception_code
;
1890 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1891 stack
->rec
.ExceptionAddress
= (LPVOID
)EIP_sig(sigcontext
);
1892 stack
->rec
.NumberParameters
= 0;
1894 save_context( &stack
->context
, sigcontext
, fs
, gs
);
1899 /***********************************************************************
1902 * Setup a proper stack frame for the raise function, and modify the
1903 * sigcontext so that the return from the signal handler will call
1904 * the raise function.
1906 static struct stack_layout
*setup_exception( ucontext_t
*sigcontext
)
1909 void *stack
= init_handler( sigcontext
, &fs
, &gs
);
1911 return setup_exception_record( sigcontext
, stack
, fs
, gs
);
1915 /**********************************************************************
1916 * raise_generic_exception
1918 * Generic raise function for exceptions that don't need special treatment.
1920 static void WINAPI
raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1924 status
= raise_exception( rec
, context
, TRUE
);
1925 raise_status( status
, rec
);
1929 /***********************************************************************
1930 * setup_raise_exception
1932 * Change context to setup a call to a raise exception function.
1934 static void setup_raise_exception( ucontext_t
*sigcontext
, struct stack_layout
*stack
)
1936 NTSTATUS status
= send_debug_event( &stack
->rec
, TRUE
, &stack
->context
);
1938 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
1940 restore_context( &stack
->context
, sigcontext
);
1943 ESP_sig(sigcontext
) = (DWORD
)stack
;
1944 EIP_sig(sigcontext
) = (DWORD
)raise_generic_exception
;
1945 /* clear single-step, direction, and align check flag */
1946 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1947 CS_sig(sigcontext
) = get_cs();
1948 DS_sig(sigcontext
) = get_ds();
1949 ES_sig(sigcontext
) = get_ds();
1950 FS_sig(sigcontext
) = get_fs();
1951 GS_sig(sigcontext
) = get_gs();
1952 SS_sig(sigcontext
) = get_ds();
1953 stack
->ret_addr
= (void *)0xdeadbabe; /* raise_generic_exception must not return */
1954 stack
->rec_ptr
= &stack
->rec
; /* arguments for raise_generic_exception */
1955 stack
->context_ptr
= &stack
->context
;
1959 /**********************************************************************
1962 * Get the FPU exception code from the FPU status.
1964 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1966 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1968 if (status
& 0x01) /* IE */
1970 if (status
& 0x40) /* SF */
1971 return EXCEPTION_FLT_STACK_CHECK
;
1973 return EXCEPTION_FLT_INVALID_OPERATION
;
1975 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1976 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1977 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1978 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1979 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1980 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1984 /***********************************************************************
1987 * Handle an interrupt.
1989 static BOOL
handle_interrupt( unsigned int interrupt
, ucontext_t
*sigcontext
, struct stack_layout
*stack
)
1996 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
1997 * service is usually not recognized when called from usermode. */
1998 switch (stack
->context
.Eax
)
2000 case 1: /* BREAKPOINT_PRINT */
2001 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2002 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2003 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2004 EIP_sig(sigcontext
) += 3;
2008 stack
->context
.Eip
+= 3;
2009 stack
->rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2010 stack
->rec
.ExceptionAddress
= (void *)stack
->context
.Eip
;
2011 stack
->rec
.NumberParameters
= is_wow64
? 1 : 3;
2012 stack
->rec
.ExceptionInformation
[0] = stack
->context
.Eax
;
2013 stack
->rec
.ExceptionInformation
[1] = stack
->context
.Ecx
;
2014 stack
->rec
.ExceptionInformation
[2] = stack
->context
.Edx
;
2015 setup_raise_exception( sigcontext
, stack
);
2023 /**********************************************************************
2026 * Handler for SIGSEGV and related errors.
2028 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2031 struct stack_layout
*stack
;
2032 ucontext_t
*context
= sigcontext
;
2033 void *stack_ptr
= init_handler( sigcontext
, &fs
, &gs
);
2035 /* check for exceptions on the signal stack caused by write watches */
2036 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
&&
2037 (char *)stack_ptr
>= (char *)get_signal_stack() &&
2038 (char *)stack_ptr
< (char *)get_signal_stack() + signal_stack_size
&&
2039 !virtual_handle_fault( siginfo
->si_addr
, (get_error_code(context
) >> 1) & 0x09, TRUE
))
2044 /* check for page fault inside the thread stack */
2045 if (get_trap_code(context
) == TRAP_x86_PAGEFLT
)
2047 switch (virtual_handle_stack_fault( siginfo
->si_addr
))
2049 case 1: /* handled */
2051 case -1: /* overflow */
2052 stack
= setup_exception_record( context
, stack_ptr
, fs
, gs
);
2053 stack
->rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2058 stack
= setup_exception_record( context
, stack_ptr
, fs
, gs
);
2059 if (stack
->rec
.ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) goto done
;
2061 switch(get_trap_code(context
))
2063 case TRAP_x86_OFLOW
: /* Overflow exception */
2064 stack
->rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
2066 case TRAP_x86_BOUND
: /* Bound range exception */
2067 stack
->rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
2069 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
2070 stack
->rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2072 case TRAP_x86_STKFLT
: /* Stack fault */
2073 stack
->rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2075 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
2076 case TRAP_x86_PROTFLT
: /* General protection fault */
2077 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2079 WORD err
= get_error_code(context
);
2080 if (!err
&& (stack
->rec
.ExceptionCode
= is_privileged_instr( &stack
->context
))) break;
2081 if ((err
& 7) == 2 && handle_interrupt( err
>> 3, context
, stack
)) return;
2082 stack
->rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2083 stack
->rec
.NumberParameters
= 2;
2084 stack
->rec
.ExceptionInformation
[0] = 0;
2085 /* if error contains a LDT selector, use that as fault address */
2086 if ((err
& 7) == 4 && !ldt_is_system( err
| 7 ))
2087 stack
->rec
.ExceptionInformation
[1] = err
& ~7;
2090 stack
->rec
.ExceptionInformation
[1] = 0xffffffff;
2091 if (check_invalid_gs( context
, &stack
->context
)) return;
2095 case TRAP_x86_PAGEFLT
: /* Page fault */
2096 stack
->rec
.NumberParameters
= 2;
2097 stack
->rec
.ExceptionInformation
[0] = (get_error_code(context
) >> 1) & 0x09;
2098 stack
->rec
.ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
2099 stack
->rec
.ExceptionCode
= virtual_handle_fault( (void *)stack
->rec
.ExceptionInformation
[1],
2100 stack
->rec
.ExceptionInformation
[0], FALSE
);
2101 if (!stack
->rec
.ExceptionCode
) return;
2102 if (stack
->rec
.ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
&&
2103 stack
->rec
.ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
2106 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
2107 &flags
, sizeof(flags
), NULL
);
2108 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
) && check_atl_thunk( context
, stack
))
2111 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
2112 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
))
2113 stack
->rec
.ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
2116 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
2117 /* FIXME: pass through exception handler first? */
2118 if (stack
->context
.EFlags
& 0x00040000)
2120 EFL_sig(context
) &= ~0x00040000; /* disable AC flag */
2123 stack
->rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
2126 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2128 case TRAP_x86_NMI
: /* NMI interrupt */
2129 case TRAP_x86_DNA
: /* Device not available exception */
2130 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
2131 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
2132 case TRAP_x86_MCHK
: /* Machine check exception */
2133 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
2134 stack
->rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2138 setup_raise_exception( context
, stack
);
2142 /**********************************************************************
2145 * Handler for SIGTRAP.
2147 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2149 ucontext_t
*context
= sigcontext
;
2150 struct stack_layout
*stack
= setup_exception( context
);
2152 switch(get_trap_code(context
))
2154 case TRAP_x86_TRCTRAP
: /* Single-step exception */
2155 stack
->rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2156 /* when single stepping can't tell whether this is a hw bp or a
2157 * single step interrupt. try to avoid as much overhead as possible
2158 * and only do a server call if there is any hw bp enabled. */
2159 if (!(stack
->context
.EFlags
& 0x100) || (stack
->context
.Dr7
& 0xff))
2161 /* (possible) hardware breakpoint, fetch the debug registers */
2162 DWORD saved_flags
= stack
->context
.ContextFlags
;
2163 stack
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
2164 NtGetContextThread( GetCurrentThread(), &stack
->context
);
2165 stack
->context
.ContextFlags
|= saved_flags
; /* restore flags */
2167 stack
->context
.EFlags
&= ~0x100; /* clear single-step flag */
2169 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
2170 stack
->rec
.ExceptionAddress
= (char *)stack
->rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
2173 stack
->rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2174 stack
->rec
.NumberParameters
= is_wow64
? 1 : 3;
2175 stack
->rec
.ExceptionInformation
[0] = 0;
2176 stack
->rec
.ExceptionInformation
[1] = 0; /* FIXME */
2177 stack
->rec
.ExceptionInformation
[2] = 0; /* FIXME */
2180 setup_raise_exception( context
, stack
);
2184 /**********************************************************************
2187 * Handler for SIGFPE.
2189 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2191 ucontext_t
*context
= sigcontext
;
2192 struct stack_layout
*stack
= setup_exception( context
);
2194 switch(get_trap_code(context
))
2196 case TRAP_x86_DIVIDE
: /* Division by zero exception */
2197 stack
->rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
2199 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
2200 stack
->rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2202 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
2203 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2204 stack
->rec
.ExceptionCode
= get_fpu_code( &stack
->context
);
2205 stack
->rec
.ExceptionAddress
= (LPVOID
)stack
->context
.FloatSave
.ErrorOffset
;
2207 case TRAP_x86_CACHEFLT
: /* SIMD exception */
2209 * Behaviour only tested for divide-by-zero exceptions
2210 * Check for other SIMD exceptions as well */
2211 if(siginfo
->si_code
!= FPE_FLTDIV
&& siginfo
->si_code
!= FPE_FLTINV
)
2212 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2215 stack
->rec
.ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
2216 stack
->rec
.NumberParameters
= 1;
2217 /* no idea what meaning is actually behind this but that's what native does */
2218 stack
->rec
.ExceptionInformation
[0] = 0;
2221 WINE_ERR( "Got unexpected trap %d\n", get_trap_code(context
) );
2222 stack
->rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2226 setup_raise_exception( context
, stack
);
2230 /**********************************************************************
2233 * Handler for SIGINT.
2235 * FIXME: should not be calling external functions on the signal stack.
2237 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2240 void *stack_ptr
= init_handler( sigcontext
, &fs
, &gs
);
2241 if (!dispatch_signal(SIGINT
))
2243 struct stack_layout
*stack
= setup_exception_record( sigcontext
, stack_ptr
, fs
, gs
);
2244 stack
->rec
.ExceptionCode
= CONTROL_C_EXIT
;
2245 setup_raise_exception( sigcontext
, stack
);
2249 /**********************************************************************
2252 * Handler for SIGABRT.
2254 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2256 struct stack_layout
*stack
= setup_exception( sigcontext
);
2257 stack
->rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2258 stack
->rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
2259 setup_raise_exception( sigcontext
, stack
);
2263 /**********************************************************************
2266 * Handler for SIGQUIT.
2268 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2271 init_handler( sigcontext
, &fs
, &gs
);
2276 /**********************************************************************
2279 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2281 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2286 init_handler( sigcontext
, &fs
, &gs
);
2287 save_context( &context
, sigcontext
, fs
, gs
);
2288 wait_suspend( &context
);
2289 restore_context( &context
, sigcontext
);
2293 /***********************************************************************
2294 * __wine_set_signal_handler (NTDLL.@)
2296 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2298 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
2299 if (handlers
[sig
] != NULL
) return -2;
2300 handlers
[sig
] = wsh
;
2305 /***********************************************************************
2309 #define LDT_SIZE 8192
2311 #define LDT_FLAGS_DATA 0x13 /* Data segment */
2312 #define LDT_FLAGS_CODE 0x1b /* Code segment */
2313 #define LDT_FLAGS_32BIT 0x40 /* Segment is 32-bit (code or stack) */
2314 #define LDT_FLAGS_ALLOCATED 0x80 /* Segment is allocated */
2318 void *base
[LDT_SIZE
];
2319 unsigned int limit
[LDT_SIZE
];
2320 unsigned char flags
[LDT_SIZE
];
2323 static WORD gdt_fs_sel
;
2325 static RTL_CRITICAL_SECTION ldt_section
;
2326 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
2329 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
2330 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
2332 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
2333 static sigset_t ldt_sigset
;
2335 static const LDT_ENTRY null_entry
;
2337 static void ldt_lock(void)
2341 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
2342 RtlEnterCriticalSection( &ldt_section
);
2343 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
2346 static void ldt_unlock(void)
2348 if (ldt_section
.RecursionCount
== 1)
2350 sigset_t sigset
= ldt_sigset
;
2351 RtlLeaveCriticalSection( &ldt_section
);
2352 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
2354 else RtlLeaveCriticalSection( &ldt_section
);
2357 static inline void *ldt_get_base( LDT_ENTRY ent
)
2359 return (void *)(ent
.BaseLow
|
2360 (ULONG_PTR
)ent
.HighWord
.Bits
.BaseMid
<< 16 |
2361 (ULONG_PTR
)ent
.HighWord
.Bits
.BaseHi
<< 24);
2364 static inline unsigned int ldt_get_limit( LDT_ENTRY ent
)
2366 unsigned int limit
= ent
.LimitLow
| (ent
.HighWord
.Bits
.LimitHi
<< 16);
2367 if (ent
.HighWord
.Bits
.Granularity
) limit
= (limit
<< 12) | 0xfff;
2371 static LDT_ENTRY
ldt_make_entry( void *base
, unsigned int limit
, unsigned char flags
)
2375 entry
.BaseLow
= (WORD
)(ULONG_PTR
)base
;
2376 entry
.HighWord
.Bits
.BaseMid
= (BYTE
)((ULONG_PTR
)base
>> 16);
2377 entry
.HighWord
.Bits
.BaseHi
= (BYTE
)((ULONG_PTR
)base
>> 24);
2378 if ((entry
.HighWord
.Bits
.Granularity
= (limit
>= 0x100000))) limit
>>= 12;
2379 entry
.LimitLow
= (WORD
)limit
;
2380 entry
.HighWord
.Bits
.LimitHi
= limit
>> 16;
2381 entry
.HighWord
.Bits
.Dpl
= 3;
2382 entry
.HighWord
.Bits
.Pres
= 1;
2383 entry
.HighWord
.Bits
.Type
= flags
;
2384 entry
.HighWord
.Bits
.Sys
= 0;
2385 entry
.HighWord
.Bits
.Reserved_0
= 0;
2386 entry
.HighWord
.Bits
.Default_Big
= (flags
& LDT_FLAGS_32BIT
) != 0;
2390 static void ldt_set_entry( WORD sel
, LDT_ENTRY entry
)
2392 int index
= sel
>> 3;
2395 struct modify_ldt_s ldt_info
= { index
};
2397 ldt_info
.base_addr
= ldt_get_base( entry
);
2398 ldt_info
.limit
= entry
.LimitLow
| (entry
.HighWord
.Bits
.LimitHi
<< 16);
2399 ldt_info
.seg_32bit
= entry
.HighWord
.Bits
.Default_Big
;
2400 ldt_info
.contents
= (entry
.HighWord
.Bits
.Type
>> 2) & 3;
2401 ldt_info
.read_exec_only
= !(entry
.HighWord
.Bits
.Type
& 2);
2402 ldt_info
.limit_in_pages
= entry
.HighWord
.Bits
.Granularity
;
2403 ldt_info
.seg_not_present
= !entry
.HighWord
.Bits
.Pres
;
2404 ldt_info
.usable
= entry
.HighWord
.Bits
.Sys
;
2405 if (modify_ldt( 0x11, &ldt_info
, sizeof(ldt_info
) ) < 0) perror( "modify_ldt" );
2406 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
2407 /* The kernel will only let us set LDTs with user priority level */
2408 if (entry
.HighWord
.Bits
.Pres
&& entry
.HighWord
.Bits
.Dpl
!= 3) entry
.HighWord
.Bits
.Dpl
= 3;
2409 if (i386_set_ldt(index
, (union descriptor
*)&entry
, 1) < 0)
2411 perror("i386_set_ldt");
2412 fprintf( stderr
, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
2415 #elif defined(__svr4__) || defined(_SCO_DS)
2419 ldt_mod
.bo
= (unsigned long)ldt_get_base( entry
);
2420 ldt_mod
.ls
= entry
.LimitLow
| (entry
.HighWord
.Bits
.LimitHi
<< 16);
2421 ldt_mod
.acc1
= entry
.HighWord
.Bytes
.Flags1
;
2422 ldt_mod
.acc2
= entry
.HighWord
.Bytes
.Flags2
>> 4;
2423 if (sysi86(SI86DSCR
, &ldt_mod
) == -1) perror("sysi86");
2424 #elif defined(__APPLE__)
2425 if (i386_set_ldt(index
, (union ldt_entry
*)&entry
, 1) < 0) perror("i386_set_ldt");
2426 #elif defined(__GNU__)
2427 if (i386_set_ldt(mach_thread_self(), sel
, (descriptor_list_t
)&entry
, 1) != KERN_SUCCESS
)
2428 perror("i386_set_ldt");
2430 fprintf( stderr
, "No LDT support on this platform\n" );
2434 __wine_ldt_copy
.base
[index
] = ldt_get_base( entry
);
2435 __wine_ldt_copy
.limit
[index
] = ldt_get_limit( entry
);
2436 __wine_ldt_copy
.flags
[index
] = (entry
.HighWord
.Bits
.Type
|
2437 (entry
.HighWord
.Bits
.Default_Big
? LDT_FLAGS_32BIT
: 0) |
2438 LDT_FLAGS_ALLOCATED
);
2441 static void ldt_init(void)
2444 /* the preloader may have allocated it already */
2445 gdt_fs_sel
= get_fs();
2446 if (!gdt_fs_sel
|| !is_gdt_sel( gdt_fs_sel
))
2448 struct modify_ldt_s ldt_info
= { -1 };
2450 ldt_info
.seg_32bit
= 1;
2451 ldt_info
.usable
= 1;
2452 if (set_thread_area( &ldt_info
) >= 0) gdt_fs_sel
= (ldt_info
.entry_number
<< 3) | 3;
2453 else gdt_fs_sel
= 0;
2455 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
2456 gdt_fs_sel
= GSEL( GUFS_SEL
, SEL_UPL
);
2460 WORD
ldt_alloc_fs( TEB
*teb
, int first_thread
)
2465 if (gdt_fs_sel
) return gdt_fs_sel
;
2467 entry
= ldt_make_entry( teb
, teb_size
- 1, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2469 if (first_thread
) /* no locking for first thread */
2471 /* leave some space if libc is using the LDT for %gs */
2472 if (!is_gdt_sel( get_gs() )) first_ldt_entry
= 512;
2473 idx
= first_ldt_entry
;
2474 ldt_set_entry( (idx
<< 3) | 7, entry
);
2479 for (idx
= first_ldt_entry
; idx
< LDT_SIZE
; idx
++)
2481 if (__wine_ldt_copy
.flags
[idx
]) continue;
2482 ldt_set_entry( (idx
<< 3) | 7, entry
);
2486 if (idx
== LDT_SIZE
) return 0;
2488 return (idx
<< 3) | 7;
2491 static void ldt_free_fs( WORD sel
)
2493 if (sel
== gdt_fs_sel
) return;
2496 __wine_ldt_copy
.flags
[sel
>> 3] = 0;
2500 static void ldt_set_fs( WORD sel
, TEB
*teb
)
2502 if (sel
== gdt_fs_sel
)
2505 struct modify_ldt_s ldt_info
= { sel
>> 3 };
2507 ldt_info
.base_addr
= teb
;
2508 ldt_info
.limit
= teb_size
- 1;
2509 ldt_info
.seg_32bit
= 1;
2510 if (set_thread_area( &ldt_info
) < 0) perror( "set_thread_area" );
2511 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
2512 i386_set_fsbase( teb
);
2519 /**********************************************************************
2520 * get_thread_ldt_entry
2522 NTSTATUS
get_thread_ldt_entry( HANDLE handle
, void *data
, ULONG len
, ULONG
*ret_len
)
2524 THREAD_DESCRIPTOR_INFORMATION
*info
= data
;
2525 NTSTATUS status
= STATUS_SUCCESS
;
2527 if (len
< sizeof(*info
)) return STATUS_INFO_LENGTH_MISMATCH
;
2528 if (info
->Selector
>> 16) return STATUS_UNSUCCESSFUL
;
2530 if (is_gdt_sel( info
->Selector
))
2532 if (!(info
->Selector
& ~3))
2533 info
->Entry
= null_entry
;
2534 else if ((info
->Selector
| 3) == get_cs())
2535 info
->Entry
= ldt_make_entry( 0, ~0u, LDT_FLAGS_CODE
| LDT_FLAGS_32BIT
);
2536 else if ((info
->Selector
| 3) == get_ds())
2537 info
->Entry
= ldt_make_entry( 0, ~0u, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2538 else if ((info
->Selector
| 3) == get_fs())
2539 info
->Entry
= ldt_make_entry( NtCurrentTeb(), 0xfff, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2541 return STATUS_UNSUCCESSFUL
;
2545 SERVER_START_REQ( get_selector_entry
)
2547 req
->handle
= wine_server_obj_handle( handle
);
2548 req
->entry
= info
->Selector
>> 3;
2549 status
= wine_server_call( req
);
2553 info
->Entry
= ldt_make_entry( (void *)reply
->base
, reply
->limit
, reply
->flags
);
2555 status
= STATUS_UNSUCCESSFUL
;
2560 if (status
== STATUS_SUCCESS
&& ret_len
)
2561 /* yes, that's a bit strange, but it's the way it is */
2562 *ret_len
= sizeof(info
->Entry
);
2568 /******************************************************************************
2569 * NtSetLdtEntries (NTDLL.@)
2570 * ZwSetLdtEntries (NTDLL.@)
2572 NTSTATUS WINAPI
NtSetLdtEntries( ULONG sel1
, LDT_ENTRY entry1
, ULONG sel2
, LDT_ENTRY entry2
)
2574 if (sel1
>> 16 || sel2
>> 16) return STATUS_INVALID_LDT_DESCRIPTOR
;
2575 if (sel1
&& (sel1
>> 3) < first_ldt_entry
) return STATUS_INVALID_LDT_DESCRIPTOR
;
2576 if (sel2
&& (sel2
>> 3) < first_ldt_entry
) return STATUS_INVALID_LDT_DESCRIPTOR
;
2579 if (sel1
) ldt_set_entry( sel1
, entry1
);
2580 if (sel2
) ldt_set_entry( sel2
, entry2
);
2582 return STATUS_SUCCESS
;
2586 /**********************************************************************
2587 * signal_alloc_thread
2589 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2591 static size_t sigstack_alignment
;
2592 struct x86_thread_data
*thread_data
;
2596 int first_thread
= !sigstack_alignment
;
2598 if (!sigstack_alignment
)
2600 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2601 /* find the first power of two not smaller than min_size */
2602 sigstack_alignment
= 12;
2603 while ((1u << sigstack_alignment
) < min_size
) sigstack_alignment
++;
2604 signal_stack_mask
= (1 << sigstack_alignment
) - 1;
2605 signal_stack_size
= (1 << sigstack_alignment
) - teb_size
;
2609 size
= signal_stack_mask
+ 1;
2610 if (!(status
= virtual_alloc_aligned( &addr
, 0, &size
, MEM_COMMIT
| MEM_TOP_DOWN
,
2611 PAGE_READWRITE
, sigstack_alignment
)))
2614 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2615 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2616 thread_data
= (struct x86_thread_data
*)(*teb
)->SystemReserved2
;
2617 if (!(thread_data
->fs
= ldt_alloc_fs( *teb
, first_thread
)))
2620 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
2621 status
= STATUS_TOO_MANY_THREADS
;
2628 /**********************************************************************
2629 * signal_free_thread
2631 void signal_free_thread( TEB
*teb
)
2634 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)teb
->SystemReserved2
;
2636 ldt_free_fs( thread_data
->fs
);
2637 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2641 /**********************************************************************
2642 * signal_init_thread
2644 void signal_init_thread( TEB
*teb
)
2646 const WORD fpu_cw
= 0x27f;
2647 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)teb
->SystemReserved2
;
2651 int mib
[2], val
= 1;
2654 mib
[1] = KERN_THALTSTACK
;
2655 sysctl( mib
, 2, NULL
, NULL
, &val
, sizeof(val
) );
2658 ss
.ss_sp
= (char *)teb
+ teb_size
;
2659 ss
.ss_size
= signal_stack_size
;
2661 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2663 ldt_set_fs( thread_data
->fs
, teb
);
2664 thread_data
->gs
= get_gs();
2667 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2669 FIXME("FPU setup not implemented for this platform.\n");
2673 /**********************************************************************
2674 * signal_init_process
2676 void signal_init_process(void)
2678 struct sigaction sig_act
;
2680 sig_act
.sa_mask
= server_block_set
;
2681 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
2683 sig_act
.sa_flags
|= SA_ONSTACK
;
2686 sig_act
.sa_flags
|= SA_RESTORER
;
2687 sig_act
.sa_restorer
= rt_sigreturn
;
2689 sig_act
.sa_sigaction
= int_handler
;
2690 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2691 sig_act
.sa_sigaction
= fpe_handler
;
2692 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2693 sig_act
.sa_sigaction
= abrt_handler
;
2694 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2695 sig_act
.sa_sigaction
= quit_handler
;
2696 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2697 sig_act
.sa_sigaction
= usr1_handler
;
2698 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2700 sig_act
.sa_sigaction
= segv_handler
;
2701 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2702 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2704 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2708 sig_act
.sa_sigaction
= trap_handler
;
2709 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2714 perror("sigaction");
2719 /*******************************************************************
2720 * RtlUnwind (NTDLL.@)
2722 void WINAPI DECLSPEC_HIDDEN
__regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD
* pEndFrame
, PVOID targetIp
,
2723 PEXCEPTION_RECORD pRecord
, PVOID retval
, CONTEXT
*context
)
2725 EXCEPTION_RECORD record
;
2726 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
;
2729 context
->Eax
= (DWORD
)retval
;
2731 /* build an exception record, if we do not have one */
2734 record
.ExceptionCode
= STATUS_UNWIND
;
2735 record
.ExceptionFlags
= 0;
2736 record
.ExceptionRecord
= NULL
;
2737 record
.ExceptionAddress
= (void *)context
->Eip
;
2738 record
.NumberParameters
= 0;
2742 pRecord
->ExceptionFlags
|= EH_UNWINDING
| (pEndFrame
? 0 : EH_EXIT_UNWIND
);
2744 TRACE( "code=%x flags=%x\n", pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2745 TRACE( "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
2746 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
2747 TRACE( "ebp=%08x esp=%08x eip=%08x cs=%04x ds=%04x fs=%04x gs=%04x flags=%08x\n",
2748 context
->Ebp
, context
->Esp
, context
->Eip
, LOWORD(context
->SegCs
), LOWORD(context
->SegDs
),
2749 LOWORD(context
->SegFs
), LOWORD(context
->SegGs
), context
->EFlags
);
2751 /* get chain of exception frames */
2752 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2753 while ((frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL) && (frame
!= pEndFrame
))
2755 /* Check frame address */
2756 if (pEndFrame
&& (frame
> pEndFrame
))
2757 raise_status( STATUS_INVALID_UNWIND_TARGET
, pRecord
);
2759 if (!is_valid_frame( frame
)) raise_status( STATUS_BAD_STACK
, pRecord
);
2762 TRACE( "calling handler at %p code=%x flags=%x\n",
2763 frame
->Handler
, pRecord
->ExceptionCode
, pRecord
->ExceptionFlags
);
2764 res
= EXC_CallHandler( pRecord
, frame
, context
, &dispatch
, frame
->Handler
, unwind_handler
);
2765 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
2769 case ExceptionContinueSearch
:
2771 case ExceptionCollidedUnwind
:
2775 raise_status( STATUS_INVALID_DISPOSITION
, pRecord
);
2778 frame
= __wine_pop_frame( frame
);
2781 NtSetContextThread( GetCurrentThread(), context
);
2783 __ASM_STDCALL_FUNC( RtlUnwind
, 16,
2785 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2786 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2787 "movl %esp,%ebp\n\t"
2788 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2789 "leal -(0x2cc+8)(%esp),%esp\n\t" /* sizeof(CONTEXT) + alignment */
2791 "leal 4(%esp),%eax\n\t" /* context */
2792 "xchgl %eax,(%esp)\n\t"
2793 "call " __ASM_STDCALL("RtlCaptureContext",4) "\n\t"
2794 "leal 24(%ebp),%eax\n\t"
2795 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2797 "pushl 20(%ebp)\n\t"
2798 "pushl 16(%ebp)\n\t"
2799 "pushl 12(%ebp)\n\t"
2801 "call " __ASM_STDCALL("__regs_RtlUnwind",20) "\n\t"
2803 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2804 __ASM_CFI(".cfi_same_value %ebp\n\t")
2805 "ret $16" ) /* actually never returns */
2808 /*******************************************************************
2809 * NtRaiseException (NTDLL.@)
2811 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2815 NTSTATUS status
= send_debug_event( rec
, TRUE
, context
);
2816 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
2817 NtSetContextThread( GetCurrentThread(), context
);
2819 return raise_exception( rec
, context
, first_chance
);
2823 /*******************************************************************
2824 * raise_exception_full_context
2826 * Raise an exception with the full CPU context.
2828 void raise_exception_full_context( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2830 save_fpu( context
);
2831 save_fpux( context
);
2833 context
->Dr0
= x86_thread_data()->dr0
;
2834 context
->Dr1
= x86_thread_data()->dr1
;
2835 context
->Dr2
= x86_thread_data()->dr2
;
2836 context
->Dr3
= x86_thread_data()->dr3
;
2837 context
->Dr6
= x86_thread_data()->dr6
;
2838 context
->Dr7
= x86_thread_data()->dr7
;
2839 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
2841 RtlRaiseStatus( NtRaiseException( rec
, context
, TRUE
));
2845 /***********************************************************************
2846 * RtlRaiseException (NTDLL.@)
2848 __ASM_STDCALL_FUNC( RtlRaiseException
, 4,
2850 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2851 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2852 "movl %esp,%ebp\n\t"
2853 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2854 "leal -0x2cc(%esp),%esp\n\t" /* sizeof(CONTEXT) */
2855 "pushl %esp\n\t" /* context */
2856 "call " __ASM_STDCALL("RtlCaptureContext",4) "\n\t"
2857 "movl 4(%ebp),%eax\n\t" /* return address */
2858 "movl 8(%ebp),%ecx\n\t" /* rec */
2859 "movl %eax,12(%ecx)\n\t" /* rec->ExceptionAddress */
2860 "leal 12(%ebp),%eax\n\t"
2861 "movl %eax,0xc4(%esp)\n\t" /* context->Esp */
2862 "movl %esp,%eax\n\t"
2865 "call " __ASM_NAME("raise_exception_full_context") "\n\t"
2867 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2868 __ASM_CFI(".cfi_same_value %ebp\n\t")
2869 "ret $4" ) /* actually never returns */
2872 /*************************************************************************
2873 * RtlCaptureStackBackTrace (NTDLL.@)
2875 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
2881 RtlCaptureContext( &context
);
2882 if (hash
) *hash
= 0;
2883 frame
= (ULONG
*)context
.Ebp
;
2887 if (!is_valid_frame( frame
)) return 0;
2888 frame
= (ULONG
*)*frame
;
2891 for (i
= 0; i
< count
; i
++)
2893 if (!is_valid_frame( frame
)) break;
2894 buffer
[i
] = (void *)frame
[1];
2895 if (hash
) *hash
+= frame
[1];
2896 frame
= (ULONG
*)*frame
;
2902 extern void DECLSPEC_NORETURN
start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
,
2904 __ASM_GLOBAL_FUNC( start_thread
,
2906 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2907 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2908 "movl %esp,%ebp\n\t"
2909 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2911 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2913 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2915 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2916 /* store exit frame */
2917 "movl %ebp,%fs:0x1f4\n\t" /* x86_thread_data()->exit_frame */
2918 /* switch to thread stack */
2919 "movl %fs:4,%eax\n\t" /* NtCurrentTeb()->StackBase */
2920 "leal -0x1000(%eax),%esp\n\t"
2922 "pushl 20(%ebp)\n\t" /* relay */
2923 "pushl 16(%ebp)\n\t" /* suspend */
2924 "pushl 12(%ebp)\n\t" /* arg */
2925 "pushl 8(%ebp)\n\t" /* entry */
2926 "xorl %ebp,%ebp\n\t"
2927 "call " __ASM_NAME("attach_thread") "\n\t"
2928 "movl %eax,%esi\n\t"
2929 "leal -12(%eax),%esp\n\t"
2930 /* clear the stack */
2931 "andl $~0xfff,%eax\n\t" /* round down to page size */
2932 "movl %eax,(%esp)\n\t"
2933 "call " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
2934 /* switch to the initial context */
2935 "movl %esi,(%esp)\n\t"
2936 "call " __ASM_NAME("set_cpu_context") )
2938 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int) );
2939 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
2940 "movl 8(%esp),%ecx\n\t"
2941 /* fetch exit frame */
2942 "movl %fs:0x1f4,%edx\n\t" /* x86_thread_data()->exit_frame */
2943 "testl %edx,%edx\n\t"
2946 /* switch to exit frame stack */
2947 "1:\tmovl 4(%esp),%eax\n\t"
2948 "movl $0,%fs:0x1f4\n\t"
2949 "movl %edx,%ebp\n\t"
2950 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2951 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2952 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2953 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2954 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2955 "leal -20(%ebp),%esp\n\t"
2959 extern void call_thread_entry(void) DECLSPEC_HIDDEN
;
2960 __ASM_GLOBAL_FUNC( call_thread_entry
,
2962 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2963 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2964 "movl %esp,%ebp\n\t"
2965 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2966 "pushl %ebx\n\t" /* arg */
2967 "pushl %eax\n\t" /* entry */
2968 "call " __ASM_NAME("call_thread_func") )
2970 /* wrapper for apps that don't declare the thread function correctly */
2971 extern DWORD
call_thread_func_wrapper( LPTHREAD_START_ROUTINE entry
, void *arg
);
2972 __ASM_GLOBAL_FUNC(call_thread_func_wrapper
,
2974 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2975 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2976 "movl %esp,%ebp\n\t"
2977 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2979 "pushl 12(%ebp)\n\t"
2982 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2983 __ASM_CFI(".cfi_same_value %ebp\n\t")
2986 /***********************************************************************
2989 void DECLSPEC_HIDDEN
call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
)
2993 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
2994 RtlExitUserThread( call_thread_func_wrapper( entry
, arg
));
2996 __EXCEPT(call_unhandled_exception_filter
)
2998 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
3001 abort(); /* should not be reached */
3005 /***********************************************************************
3006 * init_thread_context
3008 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
, void *relay
)
3010 context
->SegCs
= get_cs();
3011 context
->SegDs
= get_ds();
3012 context
->SegEs
= get_ds();
3013 context
->SegFs
= get_fs();
3014 context
->SegGs
= get_gs();
3015 context
->SegSs
= get_ds();
3016 context
->EFlags
= 0x202;
3017 context
->Eax
= (DWORD
)entry
;
3018 context
->Ebx
= (DWORD
)arg
;
3019 context
->Esp
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
- 16;
3020 context
->Eip
= (DWORD
)relay
;
3021 context
->FloatSave
.ControlWord
= 0x27f;
3022 ((XMM_SAVE_AREA32
*)context
->ExtendedRegisters
)->ControlWord
= 0x27f;
3023 ((XMM_SAVE_AREA32
*)context
->ExtendedRegisters
)->MxCsr
= 0x1f80;
3027 /***********************************************************************
3030 PCONTEXT DECLSPEC_HIDDEN
attach_thread( LPTHREAD_START_ROUTINE entry
, void *arg
,
3031 BOOL suspend
, void *relay
)
3037 CONTEXT context
= { CONTEXT_ALL
};
3039 init_thread_context( &context
, entry
, arg
, relay
);
3040 wait_suspend( &context
);
3041 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Esp
& ~15) - 1;
3046 ctx
= (CONTEXT
*)((char *)NtCurrentTeb()->Tib
.StackBase
- 16) - 1;
3047 init_thread_context( ctx
, entry
, arg
, relay
);
3049 ctx
->ContextFlags
= CONTEXT_FULL
| CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
3050 LdrInitializeThunk( ctx
, (void **)&ctx
->Eax
, 0, 0 );
3055 /***********************************************************************
3056 * signal_start_thread
3058 * Thread startup sequence:
3059 * signal_start_thread()
3061 * -> call_thread_entry()
3062 * -> call_thread_func()
3064 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
3066 start_thread( entry
, arg
, suspend
, call_thread_entry
);
3069 /**********************************************************************
3070 * signal_start_process
3072 * Process startup sequence:
3073 * signal_start_process()
3075 * -> kernel32_start_process()
3077 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
3079 start_thread( entry
, NtCurrentTeb()->Peb
, suspend
, kernel32_start_process
);
3083 /***********************************************************************
3084 * signal_exit_thread
3086 void signal_exit_thread( int status
)
3088 call_thread_exit_func( status
, exit_thread
);
3091 /***********************************************************************
3092 * signal_exit_process
3094 void signal_exit_process( int status
)
3096 call_thread_exit_func( status
, exit
);
3099 /**********************************************************************
3100 * DbgBreakPoint (NTDLL.@)
3102 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
3104 /**********************************************************************
3105 * DbgUserBreakPoint (NTDLL.@)
3107 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
3109 /**********************************************************************
3110 * NtCurrentTeb (NTDLL.@)
3112 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
3115 /**************************************************************************
3118 __ASM_STDCALL_FUNC( _chkstk
, 0,
3120 "addl %esp,%eax\n\t"
3121 "xchgl %esp,%eax\n\t"
3122 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
3123 "movl %eax,0(%esp)\n\t"
3126 /**************************************************************************
3127 * _alloca_probe (NTDLL.@)
3129 __ASM_STDCALL_FUNC( _alloca_probe
, 0,
3131 "addl %esp,%eax\n\t"
3132 "xchgl %esp,%eax\n\t"
3133 "movl 0(%eax),%eax\n\t" /* copy return address from old location */
3134 "movl %eax,0(%esp)\n\t"
3138 /**********************************************************************
3139 * EXC_CallHandler (internal)
3141 * Some exception handlers depend on EBP to have a fixed position relative to
3142 * the exception frame.
3143 * Shrinker depends on (*1) doing what it does,
3144 * (*2) being the exact instruction it is and (*3) beginning with 0x64
3145 * (i.e. the %fs prefix to the movl instruction). It also depends on the
3146 * function calling the handler having only 5 parameters (*4).
3148 __ASM_GLOBAL_FUNC( EXC_CallHandler
,
3150 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3151 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3152 "movl %esp,%ebp\n\t"
3153 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3155 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
3156 "movl 28(%ebp), %edx\n\t" /* ugly hack to pass the 6th param needed because of Shrinker */
3157 "pushl 24(%ebp)\n\t"
3158 "pushl 20(%ebp)\n\t"
3159 "pushl 16(%ebp)\n\t"
3160 "pushl 12(%ebp)\n\t"
3162 "call " __ASM_NAME("call_exception_handler") "\n\t"
3164 __ASM_CFI(".cfi_same_value %ebx\n\t")
3166 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3167 __ASM_CFI(".cfi_same_value %ebp\n\t")
3169 __ASM_GLOBAL_FUNC(call_exception_handler
,
3171 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3172 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3173 "movl %esp,%ebp\n\t"
3174 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3176 "pushl 12(%ebp)\n\t" /* make any exceptions in this... */
3177 "pushl %edx\n\t" /* handler be handled by... */
3179 "pushl (0)\n\t" /* nested_handler (passed in edx). */
3181 "movl %esp,(0)\n\t" /* push the new exception frame onto the exception stack. */
3182 "pushl 20(%ebp)\n\t"
3183 "pushl 16(%ebp)\n\t"
3184 "pushl 12(%ebp)\n\t"
3186 "movl 24(%ebp), %ecx\n\t" /* (*1) */
3187 "call *%ecx\n\t" /* call handler. (*2) */
3189 "movl (0), %esp\n\t" /* restore previous... (*3) */
3191 "popl (0)\n\t" /* exception frame. */
3192 "movl %ebp, %esp\n\t" /* restore saved stack, in case it was corrupted */
3194 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3195 __ASM_CFI(".cfi_same_value %ebp\n\t")
3196 "ret $20" ) /* (*4) */
3198 #endif /* __i386__ */