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
28 #include "wine/port.h"
35 #include <sys/types.h>
40 #ifdef HAVE_SYS_PARAM_H
41 # include <sys/param.h>
46 # ifdef HAVE_SYS_SYSCALL_H
47 # include <sys/syscall.h>
50 #ifdef HAVE_SYS_SIGNAL_H
51 # include <sys/signal.h>
53 #ifdef HAVE_SYS_UCONTEXT_H
54 # include <sys/ucontext.h>
58 #define WIN32_NO_STATUS
63 #include "wine/exception.h"
64 #include "unix_private.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
69 #undef ERR /* Solaris needs to define this */
71 /***********************************************************************
72 * signal context platform-specific definitions
77 #ifndef HAVE_SYS_UCONTEXT_H
81 REG_GS
, REG_FS
, REG_ES
, REG_DS
, REG_EDI
, REG_ESI
, REG_EBP
, REG_ESP
,
82 REG_EBX
, REG_EDX
, REG_ECX
, REG_EAX
, REG_TRAPNO
, REG_ERR
, REG_EIP
,
83 REG_CS
, REG_EFL
, REG_UESP
, REG_SS
, NGREG
87 typedef greg_t gregset_t
[NGREG
];
91 unsigned short significand
[4];
92 unsigned short exponent
;
102 unsigned long dataoff
;
103 unsigned long datasel
;
104 struct _libc_fpreg _st
[8];
105 unsigned long status
;
108 typedef struct _libc_fpstate
* fpregset_t
;
114 unsigned long oldmask
;
118 typedef struct ucontext
120 unsigned long uc_flags
;
121 struct ucontext
*uc_link
;
123 mcontext_t uc_mcontext
;
126 #endif /* HAVE_SYS_UCONTEXT_H */
128 #ifndef FP_XSTATE_MAGIC1
129 #define FP_XSTATE_MAGIC1 0x46505853
132 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
133 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
134 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
135 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
136 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
137 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
138 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
139 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
141 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
142 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
143 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
144 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
145 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
146 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
148 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
149 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
150 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
151 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
153 #define FPU_sig(context) ((FLOATING_SAVE_AREA*)((context)->uc_mcontext.fpregs))
154 #define FPUX_sig(context) (FPU_sig(context) && !((context)->uc_mcontext.fpregs->status >> 16) ? (XSAVE_FORMAT *)(FPU_sig(context) + 1) : NULL)
155 #define XState_sig(fpu) (((unsigned int *)fpu->Reserved4)[12] == FP_XSTATE_MAGIC1 ? (XSTATE *)(fpu + 1) : NULL)
158 /* custom signal restorer since we may have unmapped the one in vdso, and bionic doesn't check for that */
159 void rt_sigreturn(void);
160 __ASM_GLOBAL_FUNC( rt_sigreturn
,
161 "movl $173,%eax\n\t" /* NR_rt_sigreturn */
167 unsigned int entry_number
;
170 unsigned int seg_32bit
: 1;
171 unsigned int contents
: 2;
172 unsigned int read_exec_only
: 1;
173 unsigned int limit_in_pages
: 1;
174 unsigned int seg_not_present
: 1;
175 unsigned int usable
: 1;
176 unsigned int garbage
: 25;
179 static inline int modify_ldt( int func
, struct modify_ldt_s
*ptr
, unsigned long count
)
181 return syscall( 123 /* SYS_modify_ldt */, func
, ptr
, count
);
184 static inline int set_thread_area( struct modify_ldt_s
*ptr
)
186 return syscall( 243 /* SYS_set_thread_area */, ptr
);
189 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
191 #include <machine/trap.h>
192 #include <machine/segments.h>
193 #include <machine/sysarch.h>
195 #define EAX_sig(context) ((context)->uc_mcontext.mc_eax)
196 #define EBX_sig(context) ((context)->uc_mcontext.mc_ebx)
197 #define ECX_sig(context) ((context)->uc_mcontext.mc_ecx)
198 #define EDX_sig(context) ((context)->uc_mcontext.mc_edx)
199 #define ESI_sig(context) ((context)->uc_mcontext.mc_esi)
200 #define EDI_sig(context) ((context)->uc_mcontext.mc_edi)
201 #define EBP_sig(context) ((context)->uc_mcontext.mc_ebp)
203 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
204 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
205 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
206 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
207 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
208 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
210 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
211 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
212 #define EFL_sig(context) ((context)->uc_mcontext.mc_eflags)
214 #define EIP_sig(context) ((context)->uc_mcontext.mc_eip)
215 #define ESP_sig(context) ((context)->uc_mcontext.mc_esp)
217 #define FPU_sig(context) NULL /* FIXME */
218 #define FPUX_sig(context) NULL /* FIXME */
219 #define XState_sig(context) NULL /* FIXME */
221 #elif defined (__OpenBSD__)
223 #include <machine/segments.h>
224 #include <machine/sysarch.h>
226 #define EAX_sig(context) ((context)->sc_eax)
227 #define EBX_sig(context) ((context)->sc_ebx)
228 #define ECX_sig(context) ((context)->sc_ecx)
229 #define EDX_sig(context) ((context)->sc_edx)
230 #define ESI_sig(context) ((context)->sc_esi)
231 #define EDI_sig(context) ((context)->sc_edi)
232 #define EBP_sig(context) ((context)->sc_ebp)
234 #define CS_sig(context) ((context)->sc_cs)
235 #define DS_sig(context) ((context)->sc_ds)
236 #define ES_sig(context) ((context)->sc_es)
237 #define FS_sig(context) ((context)->sc_fs)
238 #define GS_sig(context) ((context)->sc_gs)
239 #define SS_sig(context) ((context)->sc_ss)
241 #define TRAP_sig(context) ((context)->sc_trapno)
242 #define ERROR_sig(context) ((context)->sc_err)
243 #define EFL_sig(context) ((context)->sc_eflags)
245 #define EIP_sig(context) ((context)->sc_eip)
246 #define ESP_sig(context) ((context)->sc_esp)
248 #define FPU_sig(context) NULL /* FIXME */
249 #define FPUX_sig(context) NULL /* FIXME */
250 #define XState_sig(context) NULL /* FIXME */
252 #define T_MCHK T_MACHK
253 #define T_XMMFLT T_XFTRAP
255 #elif defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
257 #if defined(_SCO_DS) || defined(__sun)
258 #include <sys/regset.h>
265 #define EAX_sig(context) ((context)->uc_mcontext.gregs[EAX])
266 #define EBX_sig(context) ((context)->uc_mcontext.gregs[EBX])
267 #define ECX_sig(context) ((context)->uc_mcontext.gregs[ECX])
268 #define EDX_sig(context) ((context)->uc_mcontext.gregs[EDX])
269 #define ESI_sig(context) ((context)->uc_mcontext.gregs[ESI])
270 #define EDI_sig(context) ((context)->uc_mcontext.gregs[EDI])
271 #define EBP_sig(context) ((context)->uc_mcontext.gregs[EBP])
273 #define CS_sig(context) ((context)->uc_mcontext.gregs[CS])
274 #define DS_sig(context) ((context)->uc_mcontext.gregs[DS])
275 #define ES_sig(context) ((context)->uc_mcontext.gregs[ES])
276 #define SS_sig(context) ((context)->uc_mcontext.gregs[SS])
278 #define FS_sig(context) ((context)->uc_mcontext.gregs[FS])
279 #define GS_sig(context) ((context)->uc_mcontext.gregs[GS])
281 #define EFL_sig(context) ((context)->uc_mcontext.gregs[EFL])
283 #define EIP_sig(context) ((context)->uc_mcontext.gregs[EIP])
285 #define ESP_sig(context) ((context)->uc_mcontext.gregs[UESP])
287 #define ESP_sig(context) ((context)->uc_mcontext.gregs[R_ESP])
289 #define ESP_sig(context) ((context)->uc_mcontext.gregs[ESP])
291 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[ERR])
292 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[TRAPNO])
294 #define FPU_sig(context) NULL /* FIXME */
295 #define FPUX_sig(context) NULL /* FIXME */
296 #define XState_sig(context) NULL /* FIXME */
298 #elif defined (__APPLE__)
300 #include <i386/user_ldt.h>
302 /* work around silly renaming of struct members in OS X 10.5 */
303 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
304 #define EAX_sig(context) ((context)->uc_mcontext->__ss.__eax)
305 #define EBX_sig(context) ((context)->uc_mcontext->__ss.__ebx)
306 #define ECX_sig(context) ((context)->uc_mcontext->__ss.__ecx)
307 #define EDX_sig(context) ((context)->uc_mcontext->__ss.__edx)
308 #define ESI_sig(context) ((context)->uc_mcontext->__ss.__esi)
309 #define EDI_sig(context) ((context)->uc_mcontext->__ss.__edi)
310 #define EBP_sig(context) ((context)->uc_mcontext->__ss.__ebp)
311 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
312 #define DS_sig(context) ((context)->uc_mcontext->__ss.__ds)
313 #define ES_sig(context) ((context)->uc_mcontext->__ss.__es)
314 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
315 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
316 #define SS_sig(context) ((context)->uc_mcontext->__ss.__ss)
317 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__eflags)
318 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__eip))
319 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__esp))
320 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
321 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
322 #define FPU_sig(context) NULL
323 #define FPUX_sig(context) ((XSAVE_FORMAT *)&(context)->uc_mcontext->__fs.__fpu_fcw)
324 #define XState_sig(context) NULL /* FIXME */
326 #define EAX_sig(context) ((context)->uc_mcontext->ss.eax)
327 #define EBX_sig(context) ((context)->uc_mcontext->ss.ebx)
328 #define ECX_sig(context) ((context)->uc_mcontext->ss.ecx)
329 #define EDX_sig(context) ((context)->uc_mcontext->ss.edx)
330 #define ESI_sig(context) ((context)->uc_mcontext->ss.esi)
331 #define EDI_sig(context) ((context)->uc_mcontext->ss.edi)
332 #define EBP_sig(context) ((context)->uc_mcontext->ss.ebp)
333 #define CS_sig(context) ((context)->uc_mcontext->ss.cs)
334 #define DS_sig(context) ((context)->uc_mcontext->ss.ds)
335 #define ES_sig(context) ((context)->uc_mcontext->ss.es)
336 #define FS_sig(context) ((context)->uc_mcontext->ss.fs)
337 #define GS_sig(context) ((context)->uc_mcontext->ss.gs)
338 #define SS_sig(context) ((context)->uc_mcontext->ss.ss)
339 #define EFL_sig(context) ((context)->uc_mcontext->ss.eflags)
340 #define EIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.eip))
341 #define ESP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->ss.esp))
342 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
343 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
344 #define FPU_sig(context) NULL
345 #define FPUX_sig(context) ((XSAVE_FORMAT *)&(context)->uc_mcontext->fs.fpu_fcw)
346 #define XState_sig(context) NULL /* FIXME */
349 #elif defined(__NetBSD__)
351 #include <machine/segments.h>
352 #include <machine/sysarch.h>
354 #define EAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EAX])
355 #define EBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBX])
356 #define ECX_sig(context) ((context)->uc_mcontext.__gregs[_REG_ECX])
357 #define EDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDX])
358 #define ESI_sig(context) ((context)->uc_mcontext.__gregs[_REG_ESI])
359 #define EDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_EDI])
360 #define EBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EBP])
361 #define ESP_sig(context) _UC_MACHINE_SP(context)
363 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
364 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
365 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
366 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
367 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
368 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
370 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_EFL])
371 #define EIP_sig(context) _UC_MACHINE_PC(context)
372 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
373 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
375 #define FPU_sig(context) NULL
376 #define FPUX_sig(context) ((XSAVE_FORMAT *)&((context)->uc_mcontext.__fpregs))
377 #define XState_sig(context) NULL /* FIXME */
380 #define T_XMMFLT T_XMM
382 #elif defined(__GNU__)
384 #include <mach/i386/mach_i386.h>
385 #include <mach/mach_traps.h>
387 #define EAX_sig(context) ((context)->uc_mcontext.gregs[REG_EAX])
388 #define EBX_sig(context) ((context)->uc_mcontext.gregs[REG_EBX])
389 #define ECX_sig(context) ((context)->uc_mcontext.gregs[REG_ECX])
390 #define EDX_sig(context) ((context)->uc_mcontext.gregs[REG_EDX])
391 #define ESI_sig(context) ((context)->uc_mcontext.gregs[REG_ESI])
392 #define EDI_sig(context) ((context)->uc_mcontext.gregs[REG_EDI])
393 #define EBP_sig(context) ((context)->uc_mcontext.gregs[REG_EBP])
394 #define ESP_sig(context) ((context)->uc_mcontext.gregs[REG_ESP])
396 #define CS_sig(context) ((context)->uc_mcontext.gregs[REG_CS])
397 #define DS_sig(context) ((context)->uc_mcontext.gregs[REG_DS])
398 #define ES_sig(context) ((context)->uc_mcontext.gregs[REG_ES])
399 #define SS_sig(context) ((context)->uc_mcontext.gregs[REG_SS])
400 #define FS_sig(context) ((context)->uc_mcontext.gregs[REG_FS])
401 #define GS_sig(context) ((context)->uc_mcontext.gregs[REG_GS])
403 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
404 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
405 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
406 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
408 #define FPU_sig(context) ((FLOATING_SAVE_AREA *)&(context)->uc_mcontext.fpregs.fp_reg_set.fpchip_state)
409 #define FPUX_sig(context) NULL
410 #define XState_sig(context) NULL /* FIXME */
413 #error You must define the signal context functions for your platform
416 static ULONG first_ldt_entry
= 32;
420 #if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
421 TRAP_x86_DIVIDE
= T_DIVIDE
, /* Division by zero exception */
422 TRAP_x86_TRCTRAP
= T_TRCTRAP
, /* Single-step exception */
423 TRAP_x86_NMI
= T_NMI
, /* NMI interrupt */
424 TRAP_x86_BPTFLT
= T_BPTFLT
, /* Breakpoint exception */
425 TRAP_x86_OFLOW
= T_OFLOW
, /* Overflow exception */
426 TRAP_x86_BOUND
= T_BOUND
, /* Bound range exception */
427 TRAP_x86_PRIVINFLT
= T_PRIVINFLT
, /* Invalid opcode exception */
428 TRAP_x86_DNA
= T_DNA
, /* Device not available exception */
429 TRAP_x86_DOUBLEFLT
= T_DOUBLEFLT
, /* Double fault exception */
430 TRAP_x86_FPOPFLT
= T_FPOPFLT
, /* Coprocessor segment overrun */
431 TRAP_x86_TSSFLT
= T_TSSFLT
, /* Invalid TSS exception */
432 TRAP_x86_SEGNPFLT
= T_SEGNPFLT
, /* Segment not present exception */
433 TRAP_x86_STKFLT
= T_STKFLT
, /* Stack fault */
434 TRAP_x86_PROTFLT
= T_PROTFLT
, /* General protection fault */
435 TRAP_x86_PAGEFLT
= T_PAGEFLT
, /* Page fault */
436 TRAP_x86_ARITHTRAP
= T_ARITHTRAP
, /* Floating point exception */
437 TRAP_x86_ALIGNFLT
= T_ALIGNFLT
, /* Alignment check exception */
438 TRAP_x86_MCHK
= T_MCHK
, /* Machine check exception */
439 TRAP_x86_CACHEFLT
= T_XMMFLT
/* Cache flush exception */
441 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
442 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
443 TRAP_x86_NMI
= 2, /* NMI interrupt */
444 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
445 TRAP_x86_OFLOW
= 4, /* Overflow exception */
446 TRAP_x86_BOUND
= 5, /* Bound range exception */
447 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
448 TRAP_x86_DNA
= 7, /* Device not available exception */
449 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
450 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
451 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
452 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
453 TRAP_x86_STKFLT
= 12, /* Stack fault */
454 TRAP_x86_PROTFLT
= 13, /* General protection fault */
455 TRAP_x86_PAGEFLT
= 14, /* Page fault */
456 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
457 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
458 TRAP_x86_MCHK
= 18, /* Machine check exception */
459 TRAP_x86_CACHEFLT
= 19 /* SIMD exception (via SIGFPE) if CPU is SSE capable
460 otherwise Cache flush exception (via SIGSEV) */
469 FLOATING_SAVE_AREA fsave
;
474 ULONG64 compaction_mask
;
480 C_ASSERT( sizeof(struct syscall_xsave
) == 0x2c0 );
484 DWORD eflags
; /* 00 */
502 C_ASSERT( sizeof(struct syscall_frame
) == 0x34 );
504 struct x86_thread_data
506 DWORD fs
; /* 1d4 TEB selector */
507 DWORD gs
; /* 1d8 libc selector; update winebuild if you move this! */
508 DWORD dr0
; /* 1dc debug registers */
514 void *exit_frame
; /* 1f4 exit frame pointer */
515 struct syscall_frame
*syscall_frame
; /* 1f8 frame pointer on syscall entry */
518 C_ASSERT( sizeof(struct x86_thread_data
) <= sizeof(((struct ntdll_thread_data
*)0)->cpu_data
) );
519 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, gs
) == 0x1d8 );
520 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, exit_frame
) == 0x1f4 );
521 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, syscall_frame
) == 0x1f8 );
523 static void *syscall_dispatcher
;
525 static inline struct x86_thread_data
*x86_thread_data(void)
527 return (struct x86_thread_data
*)ntdll_get_thread_data()->cpu_data
;
530 void *get_syscall_frame(void)
532 return x86_thread_data()->syscall_frame
;
535 void set_syscall_frame(void *frame
)
537 x86_thread_data()->syscall_frame
= frame
;
540 static struct syscall_xsave
*get_syscall_xsave( struct syscall_frame
*frame
)
542 return (struct syscall_xsave
*)((ULONG_PTR
)((struct syscall_xsave
*)frame
- 1) & ~63);
545 static inline WORD
get_cs(void) { WORD res
; __asm__( "movw %%cs,%0" : "=r" (res
) ); return res
; }
546 static inline WORD
get_ds(void) { WORD res
; __asm__( "movw %%ds,%0" : "=r" (res
) ); return res
; }
547 static inline WORD
get_fs(void) { WORD res
; __asm__( "movw %%fs,%0" : "=r" (res
) ); return res
; }
548 static inline WORD
get_gs(void) { WORD res
; __asm__( "movw %%gs,%0" : "=r" (res
) ); return res
; }
549 static inline void set_fs( WORD val
) { __asm__( "mov %0,%%fs" :: "r" (val
)); }
550 static inline void set_gs( WORD val
) { __asm__( "mov %0,%%gs" :: "r" (val
)); }
553 /***********************************************************************
556 NTSTATUS CDECL
unwind_builtin_dll( ULONG type
, struct _DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*context
)
558 return STATUS_UNSUCCESSFUL
;
562 /***********************************************************************
565 static inline int is_gdt_sel( WORD sel
)
571 /***********************************************************************
574 static inline int ldt_is_system( WORD sel
)
576 return is_gdt_sel( sel
) || ((sel
>> 3) < first_ldt_entry
);
580 /***********************************************************************
583 * Get the current teb based on the stack pointer.
585 static inline TEB
*get_current_teb(void)
588 __asm__("movl %%esp,%0" : "=g" (esp
) );
589 return (TEB
*)((esp
& ~signal_stack_mask
) + teb_offset
);
595 /* We have to workaround two Solaris breakages:
596 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
598 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
599 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
602 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
603 __ASM_GLOBAL_FUNC( sigaction_syscall
,
604 "movl $0x62,%eax\n\t"
608 /* assume the same libc handler is used for all signals */
609 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
611 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
613 struct x86_thread_data
*thread_data
;
615 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
617 thread_data
= (struct x86_thread_data
*)get_current_teb()->GdiTebBatch
;
618 set_fs( thread_data
->fs
);
619 set_gs( thread_data
->gs
);
621 libc_sigacthandler( signal
, siginfo
, sigcontext
);
624 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
626 struct sigaction real_act
;
628 if (sigaction( sig
, new, old
) == -1) return -1;
630 /* retrieve the real handler and flags with a direct syscall */
631 sigaction_syscall( sig
, NULL
, &real_act
);
632 libc_sigacthandler
= real_act
.sa_sigaction
;
633 real_act
.sa_sigaction
= wine_sigacthandler
;
634 sigaction_syscall( sig
, &real_act
, NULL
);
637 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
641 extern void clear_alignment_flag(void) DECLSPEC_HIDDEN
;
642 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
644 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
645 "andl $~0x40000,(%esp)\n\t"
647 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
651 /***********************************************************************
654 * Handler initialization when the full context is not needed.
655 * Return the stack pointer to use for pushing the exception data.
657 static inline void *init_handler( const ucontext_t
*sigcontext
)
659 TEB
*teb
= get_current_teb();
661 clear_alignment_flag();
663 #ifndef __sun /* see above for Solaris handling */
665 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
666 set_fs( thread_data
->fs
);
667 set_gs( thread_data
->gs
);
671 if (!ldt_is_system(CS_sig(sigcontext
)) || !ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
674 * Win16 or DOS protected mode. Note that during switch
675 * from 16-bit mode to linear mode, CS may be set to system
676 * segment before FS is restored. Fortunately, in this case
677 * SS is still non-system segment. This is why both CS and SS
680 return teb
->SystemReserved1
[0];
682 return (void *)(ESP_sig(sigcontext
) & ~3);
686 /***********************************************************************
689 * Save the thread FPU context.
691 static inline void save_fpu( CONTEXT
*context
)
705 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
706 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
708 /* Reset unmasked exceptions status to avoid firing an exception. */
709 memcpy(&float_status
, &context
->FloatSave
, sizeof(float_status
));
710 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
712 __asm__
__volatile__( "fldenv %0" : : "m" (float_status
) );
716 /***********************************************************************
719 * Restore the x87 FPU context
721 static inline void restore_fpu( const CONTEXT
*context
)
723 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
724 /* reset the current interrupt status */
725 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
726 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
730 /***********************************************************************
733 * Build a standard FPU context from an extended one.
735 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XSAVE_FORMAT
*fpux
)
737 unsigned int i
, tag
, stack_top
;
739 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
740 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
741 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
742 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
743 fpu
->DataOffset
= fpux
->DataOffset
;
744 fpu
->DataSelector
= fpux
->DataSelector
;
745 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
747 stack_top
= (fpux
->StatusWord
>> 11) & 7;
748 fpu
->TagWord
= 0xffff0000;
749 for (i
= 0; i
< 8; i
++)
751 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
752 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
755 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
756 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
758 tag
= 2; /* special */
760 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
762 if (reg
->Low
) tag
= 2; /* special */
763 else tag
= 1; /* zero */
767 if (reg
->Low
>> 63) tag
= 0; /* valid */
768 else tag
= 2; /* special */
771 fpu
->TagWord
|= tag
<< (2 * i
);
776 /***********************************************************************
779 * Fill extended FPU context from standard one.
781 static void fpu_to_fpux( XSAVE_FORMAT
*fpux
, const FLOATING_SAVE_AREA
*fpu
)
785 fpux
->ControlWord
= fpu
->ControlWord
;
786 fpux
->StatusWord
= fpu
->StatusWord
;
787 fpux
->ErrorOffset
= fpu
->ErrorOffset
;
788 fpux
->ErrorSelector
= fpu
->ErrorSelector
;
789 fpux
->ErrorOpcode
= fpu
->ErrorSelector
>> 16;
790 fpux
->DataOffset
= fpu
->DataOffset
;
791 fpux
->DataSelector
= fpu
->DataSelector
;
793 for (i
= 0; i
< 8; i
++)
795 if (((fpu
->TagWord
>> (i
* 2)) & 3) != 3)
796 fpux
->TagWord
|= 1 << i
;
797 memcpy( &fpux
->FloatRegisters
[i
], &fpu
->RegisterArea
[10 * i
], 10 );
802 /***********************************************************************
805 * Build a context structure from the signal info.
807 static inline void save_context( struct xcontext
*xcontext
, const ucontext_t
*sigcontext
)
809 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
810 XSAVE_FORMAT
*fpux
= FPUX_sig(sigcontext
);
811 CONTEXT
*context
= &xcontext
->c
;
813 memset(context
, 0, sizeof(*context
));
814 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
815 context
->Eax
= EAX_sig(sigcontext
);
816 context
->Ebx
= EBX_sig(sigcontext
);
817 context
->Ecx
= ECX_sig(sigcontext
);
818 context
->Edx
= EDX_sig(sigcontext
);
819 context
->Esi
= ESI_sig(sigcontext
);
820 context
->Edi
= EDI_sig(sigcontext
);
821 context
->Ebp
= EBP_sig(sigcontext
);
822 context
->EFlags
= EFL_sig(sigcontext
);
823 context
->Eip
= EIP_sig(sigcontext
);
824 context
->Esp
= ESP_sig(sigcontext
);
825 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
826 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
827 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
828 context
->SegFs
= LOWORD(FS_sig(sigcontext
));
829 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
830 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
831 context
->Dr0
= x86_thread_data()->dr0
;
832 context
->Dr1
= x86_thread_data()->dr1
;
833 context
->Dr2
= x86_thread_data()->dr2
;
834 context
->Dr3
= x86_thread_data()->dr3
;
835 context
->Dr6
= x86_thread_data()->dr6
;
836 context
->Dr7
= x86_thread_data()->dr7
;
840 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
841 context
->FloatSave
= *fpu
;
847 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
848 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
849 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
850 if ((cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_AVX
) && (xs
= XState_sig(fpux
)))
852 context_init_xstate( context
, xs
);
853 xcontext
->host_compaction_mask
= xs
->CompactionMask
;
856 if (!fpu
&& !fpux
) save_fpu( context
);
860 /***********************************************************************
863 * Restore the signal info from the context.
865 static inline void restore_context( const struct xcontext
*xcontext
, ucontext_t
*sigcontext
)
867 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
868 XSAVE_FORMAT
*fpux
= FPUX_sig(sigcontext
);
869 const CONTEXT
*context
= &xcontext
->c
;
871 x86_thread_data()->dr0
= context
->Dr0
;
872 x86_thread_data()->dr1
= context
->Dr1
;
873 x86_thread_data()->dr2
= context
->Dr2
;
874 x86_thread_data()->dr3
= context
->Dr3
;
875 x86_thread_data()->dr6
= context
->Dr6
;
876 x86_thread_data()->dr7
= context
->Dr7
;
877 EAX_sig(sigcontext
) = context
->Eax
;
878 EBX_sig(sigcontext
) = context
->Ebx
;
879 ECX_sig(sigcontext
) = context
->Ecx
;
880 EDX_sig(sigcontext
) = context
->Edx
;
881 ESI_sig(sigcontext
) = context
->Esi
;
882 EDI_sig(sigcontext
) = context
->Edi
;
883 EBP_sig(sigcontext
) = context
->Ebp
;
884 EFL_sig(sigcontext
) = context
->EFlags
;
885 EIP_sig(sigcontext
) = context
->Eip
;
886 ESP_sig(sigcontext
) = context
->Esp
;
887 CS_sig(sigcontext
) = context
->SegCs
;
888 DS_sig(sigcontext
) = context
->SegDs
;
889 ES_sig(sigcontext
) = context
->SegEs
;
890 FS_sig(sigcontext
) = context
->SegFs
;
891 GS_sig(sigcontext
) = context
->SegGs
;
892 SS_sig(sigcontext
) = context
->SegSs
;
894 if (fpu
) *fpu
= context
->FloatSave
;
897 XSTATE
*src_xs
, *dst_xs
;
899 memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
901 if ((dst_xs
= XState_sig(fpux
)) && (src_xs
= xstate_from_context( context
)))
903 memcpy( &dst_xs
->YmmContext
, &src_xs
->YmmContext
, sizeof(dst_xs
->YmmContext
) );
904 dst_xs
->Mask
|= src_xs
->Mask
;
905 dst_xs
->CompactionMask
= xcontext
->host_compaction_mask
;
908 if (!fpu
&& !fpux
) restore_fpu( context
);
912 /***********************************************************************
913 * set_full_cpu_context
915 * Set the new CPU context.
917 extern void set_full_cpu_context(void) DECLSPEC_HIDDEN
;
918 __ASM_GLOBAL_FUNC( set_full_cpu_context
,
919 "movl %fs:0x1f8,%ecx\n\t"
920 "movl $0,%fs:0x1f8\n\t" /* x86_thread_data()->syscall_frame = NULL */
921 "movw 0x16(%ecx),%gs\n\t" /* SegGs */
922 "movw 0x14(%ecx),%fs\n\t" /* SegFs */
923 "movw 0x12(%ecx),%es\n\t" /* SegEs */
924 "movl 0x1c(%ecx),%ebx\n\t" /* Ebx */
925 "movl 0x28(%ecx),%edi\n\t" /* Edi */
926 "movl 0x2c(%ecx),%esi\n\t" /* Esi */
927 "movl 0x30(%ecx),%ebp\n\t" /* Ebp */
929 "cmpw 0x0e(%ecx),%ax\n\t" /* SegSs */
931 /* As soon as we have switched stacks the context structure could
932 * be invalid (when signal handlers are executed for example). Copy
933 * values on the target stack before changing ESP. */
934 "movl 0x08(%ecx),%eax\n\t" /* Esp */
935 "leal -4*4(%eax),%eax\n\t"
936 "movl (%ecx),%edx\n\t" /* EFlags */
937 "movl %edx,3*4(%eax)\n\t"
938 "movl 0x0c(%ecx),%edx\n\t" /* SegCs */
939 "movl %edx,2*4(%eax)\n\t"
940 "movl 0x04(%ecx),%edx\n\t" /* Eip */
941 "movl %edx,1*4(%eax)\n\t"
942 "movl 0x18(%ecx),%edx\n\t" /* Eax */
943 "movl %edx,0*4(%eax)\n\t"
944 "pushl 0x10(%ecx)\n\t" /* SegDs */
945 "movl 0x24(%ecx),%edx\n\t" /* Edx */
946 "movl 0x20(%ecx),%ecx\n\t" /* Ecx */
951 /* Restore the context when the stack segment changes. We can't use
952 * the same code as above because we do not know if the stack segment
953 * is 16 or 32 bit, and 'movl' will throw an exception when we try to
954 * access memory above the limit. */
956 "movl 0x24(%ecx),%edx\n\t" /* Edx */
957 "movl 0x18(%ecx),%eax\n\t" /* Eax */
958 "movw 0x0e(%ecx),%ss\n\t" /* SegSs */
959 "movl 0x08(%ecx),%esp\n\t" /* Esp */
960 "pushl 0x00(%ecx)\n\t" /* EFlags */
961 "pushl 0x0c(%ecx)\n\t" /* SegCs */
962 "pushl 0x04(%ecx)\n\t" /* Eip */
963 "pushl 0x10(%ecx)\n\t" /* SegDs */
964 "movl 0x20(%ecx),%ecx\n\t" /* Ecx */
969 /***********************************************************************
970 * signal_restore_full_cpu_context
972 * Restore full context from syscall frame
974 void signal_restore_full_cpu_context(void)
976 struct syscall_xsave
*xsave
= get_syscall_xsave( get_syscall_frame() );
978 if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_XSAVE
)
980 __asm__
volatile( "xrstor %0" : : "m"(*xsave
), "a" (7), "d" (0) );
982 else if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_FXSR
)
984 __asm__
volatile( "fxrstor %0" : : "m"(xsave
->u
.xsave
) );
988 __asm__
volatile( "frstor %0; fwait" : : "m" (xsave
->u
.fsave
) );
990 set_full_cpu_context();
994 /***********************************************************************
995 * NtSetContextThread (NTDLL.@)
996 * ZwSetContextThread (NTDLL.@)
998 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
1000 NTSTATUS ret
= STATUS_SUCCESS
;
1001 struct syscall_frame
*frame
= x86_thread_data()->syscall_frame
;
1002 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1003 BOOL self
= (handle
== GetCurrentThread());
1006 /* debug registers require a server call */
1007 if (self
&& (flags
& CONTEXT_DEBUG_REGISTERS
))
1008 self
= (x86_thread_data()->dr0
== context
->Dr0
&&
1009 x86_thread_data()->dr1
== context
->Dr1
&&
1010 x86_thread_data()->dr2
== context
->Dr2
&&
1011 x86_thread_data()->dr3
== context
->Dr3
&&
1012 x86_thread_data()->dr6
== context
->Dr6
&&
1013 x86_thread_data()->dr7
== context
->Dr7
);
1017 ret
= set_thread_context( handle
, context
, &self
, IMAGE_FILE_MACHINE_I386
);
1018 if (ret
|| !self
) return ret
;
1019 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1021 x86_thread_data()->dr0
= context
->Dr0
;
1022 x86_thread_data()->dr1
= context
->Dr1
;
1023 x86_thread_data()->dr2
= context
->Dr2
;
1024 x86_thread_data()->dr3
= context
->Dr3
;
1025 x86_thread_data()->dr6
= context
->Dr6
;
1026 x86_thread_data()->dr7
= context
->Dr7
;
1030 if (flags
& CONTEXT_INTEGER
)
1032 frame
->eax
= context
->Eax
;
1033 frame
->ebx
= context
->Ebx
;
1034 frame
->ecx
= context
->Ecx
;
1035 frame
->edx
= context
->Edx
;
1036 frame
->esi
= context
->Esi
;
1037 frame
->edi
= context
->Edi
;
1039 if (flags
& CONTEXT_CONTROL
)
1041 frame
->esp
= context
->Esp
;
1042 frame
->ebp
= context
->Ebp
;
1043 frame
->eip
= context
->Eip
;
1044 frame
->eflags
= context
->EFlags
;
1045 frame
->cs
= context
->SegCs
;
1046 frame
->ss
= context
->SegSs
;
1048 if (flags
& CONTEXT_SEGMENTS
)
1050 frame
->ds
= context
->SegDs
;
1051 frame
->es
= context
->SegEs
;
1052 frame
->fs
= context
->SegFs
;
1053 frame
->gs
= context
->SegGs
;
1055 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
1057 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1058 memcpy( &xsave
->u
.xsave
, context
->ExtendedRegisters
, sizeof(xsave
->u
.xsave
) );
1059 /* reset the current interrupt status */
1060 xsave
->u
.xsave
.StatusWord
&= xsave
->u
.xsave
.ControlWord
| 0xff80;
1061 xsave
->xstate
.mask
|= XSTATE_MASK_LEGACY
;
1063 else if (flags
& CONTEXT_FLOATING_POINT
)
1065 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1066 if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_FXSR
)
1068 fpu_to_fpux( &xsave
->u
.xsave
, &context
->FloatSave
);
1072 xsave
->u
.fsave
= context
->FloatSave
;
1074 xsave
->xstate
.mask
|= XSTATE_MASK_LEGACY_FLOATING_POINT
;
1076 if ((cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_AVX
) && (xs
= xstate_from_context( context
)))
1078 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1079 CONTEXT_EX
*context_ex
= (CONTEXT_EX
*)(context
+ 1);
1081 if (context_ex
->XState
.Length
< offsetof(XSTATE
, YmmContext
)
1082 || context_ex
->XState
.Length
> sizeof(XSTATE
))
1083 return STATUS_INVALID_PARAMETER
;
1085 if (xs
->Mask
& XSTATE_MASK_GSSE
)
1087 if (context_ex
->XState
.Length
< sizeof(XSTATE
))
1088 return STATUS_BUFFER_OVERFLOW
;
1090 xsave
->xstate
.mask
|= XSTATE_MASK_GSSE
;
1091 memcpy( &xsave
->xstate
.ymm_high
, &xs
->YmmContext
, sizeof(xsave
->xstate
.ymm_high
) );
1094 xsave
->xstate
.mask
&= ~XSTATE_MASK_GSSE
;
1097 return STATUS_SUCCESS
;
1101 /***********************************************************************
1102 * NtGetContextThread (NTDLL.@)
1103 * ZwGetContextThread (NTDLL.@)
1105 * Note: we use a small assembly wrapper to save the necessary registers
1106 * in case we are fetching the context of the current thread.
1108 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
1110 struct syscall_frame
*frame
= x86_thread_data()->syscall_frame
;
1111 DWORD needed_flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1112 BOOL self
= (handle
== GetCurrentThread());
1115 /* debug registers require a server call */
1116 if (needed_flags
& CONTEXT_DEBUG_REGISTERS
) self
= FALSE
;
1120 if ((ret
= get_thread_context( handle
, context
, &self
, IMAGE_FILE_MACHINE_I386
))) return ret
;
1121 needed_flags
&= ~context
->ContextFlags
;
1126 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1128 if (needed_flags
& CONTEXT_INTEGER
)
1130 context
->Eax
= frame
->eax
;
1131 context
->Ebx
= frame
->ebx
;
1132 context
->Ecx
= frame
->ecx
;
1133 context
->Edx
= frame
->edx
;
1134 context
->Esi
= frame
->esi
;
1135 context
->Edi
= frame
->edi
;
1136 context
->ContextFlags
|= CONTEXT_INTEGER
;
1138 if (needed_flags
& CONTEXT_CONTROL
)
1140 context
->Esp
= frame
->esp
;
1141 context
->Ebp
= frame
->ebp
;
1142 context
->Eip
= frame
->eip
;
1143 context
->EFlags
= frame
->eflags
;
1144 context
->SegCs
= frame
->cs
;
1145 context
->SegSs
= frame
->ss
;
1146 context
->ContextFlags
|= CONTEXT_CONTROL
;
1148 if (needed_flags
& CONTEXT_SEGMENTS
)
1150 context
->SegDs
= frame
->ds
;
1151 context
->SegEs
= frame
->es
;
1152 context
->SegFs
= frame
->fs
;
1153 context
->SegGs
= frame
->gs
;
1154 context
->ContextFlags
|= CONTEXT_SEGMENTS
;
1156 if (needed_flags
& CONTEXT_FLOATING_POINT
)
1158 if (!(cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_FXSR
))
1160 context
->FloatSave
= xsave
->u
.fsave
;
1162 else if (!xstate_compaction_enabled
||
1163 (xsave
->xstate
.mask
& XSTATE_MASK_LEGACY_FLOATING_POINT
))
1165 fpux_to_fpu( &context
->FloatSave
, &xsave
->u
.xsave
);
1169 memset( &context
->FloatSave
, 0, sizeof(context
->FloatSave
) );
1170 context
->FloatSave
.ControlWord
= 0x37f;
1172 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1174 if (needed_flags
& CONTEXT_EXTENDED_REGISTERS
)
1176 XSAVE_FORMAT
*xs
= (XSAVE_FORMAT
*)context
->ExtendedRegisters
;
1178 if (!xstate_compaction_enabled
||
1179 (xsave
->xstate
.mask
& XSTATE_MASK_LEGACY_FLOATING_POINT
))
1181 memcpy( xs
, &xsave
->u
.xsave
, FIELD_OFFSET( XSAVE_FORMAT
, MxCsr
));
1182 memcpy( xs
->FloatRegisters
, xsave
->u
.xsave
.FloatRegisters
,
1183 sizeof( xs
->FloatRegisters
));
1187 memset( xs
, 0, FIELD_OFFSET( XSAVE_FORMAT
, MxCsr
));
1188 memset( xs
->FloatRegisters
, 0, sizeof( xs
->FloatRegisters
));
1189 xs
->ControlWord
= 0x37f;
1192 if (!xstate_compaction_enabled
|| (xsave
->xstate
.mask
& XSTATE_MASK_LEGACY_SSE
))
1194 memcpy( xs
->XmmRegisters
, xsave
->u
.xsave
.XmmRegisters
, sizeof( xs
->XmmRegisters
));
1195 xs
->MxCsr
= xsave
->u
.xsave
.MxCsr
;
1196 xs
->MxCsr_Mask
= xsave
->u
.xsave
.MxCsr_Mask
;
1200 memset( xs
->XmmRegisters
, 0, sizeof( xs
->XmmRegisters
));
1202 xs
->MxCsr_Mask
= 0x2ffff;
1205 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1207 /* update the cached version of the debug registers */
1208 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1210 x86_thread_data()->dr0
= context
->Dr0
;
1211 x86_thread_data()->dr1
= context
->Dr1
;
1212 x86_thread_data()->dr2
= context
->Dr2
;
1213 x86_thread_data()->dr3
= context
->Dr3
;
1214 x86_thread_data()->dr6
= context
->Dr6
;
1215 x86_thread_data()->dr7
= context
->Dr7
;
1217 if ((cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_AVX
) && (xstate
= xstate_from_context( context
)))
1219 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1220 CONTEXT_EX
*context_ex
= (CONTEXT_EX
*)(context
+ 1);
1223 if (context_ex
->XState
.Length
< offsetof(XSTATE
, YmmContext
)
1224 || context_ex
->XState
.Length
> sizeof(XSTATE
))
1225 return STATUS_INVALID_PARAMETER
;
1227 mask
= (xstate_compaction_enabled
? xstate
->CompactionMask
: xstate
->Mask
) & XSTATE_MASK_GSSE
;
1228 xstate
->Mask
= xsave
->xstate
.mask
& mask
;
1229 xstate
->CompactionMask
= xstate_compaction_enabled
? (0x8000000000000000 | mask
) : 0;
1230 memset( xstate
->Reserved
, 0, sizeof(xstate
->Reserved
) );
1233 if (context_ex
->XState
.Length
< sizeof(XSTATE
))
1234 return STATUS_BUFFER_OVERFLOW
;
1236 memcpy( &xstate
->YmmContext
, xsave
->xstate
.ymm_high
, sizeof(xsave
->xstate
.ymm_high
) );
1241 if (context
->ContextFlags
& (CONTEXT_INTEGER
& ~CONTEXT_i386
))
1242 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle
,
1243 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
1244 if (context
->ContextFlags
& (CONTEXT_CONTROL
& ~CONTEXT_i386
))
1245 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle
,
1246 context
->Ebp
, context
->Esp
, context
->Eip
, context
->SegCs
, context
->SegSs
, context
->EFlags
);
1247 if (context
->ContextFlags
& (CONTEXT_SEGMENTS
& ~CONTEXT_i386
))
1248 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle
,
1249 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
);
1250 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1251 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle
,
1252 context
->Dr0
, context
->Dr1
, context
->Dr2
, context
->Dr3
, context
->Dr6
, context
->Dr7
);
1254 return STATUS_SUCCESS
;
1258 /***********************************************************************
1259 * set_thread_wow64_context
1261 NTSTATUS
set_thread_wow64_context( HANDLE handle
, const void *ctx
, ULONG size
)
1263 return STATUS_INVALID_INFO_CLASS
;
1267 /***********************************************************************
1268 * get_thread_wow64_context
1270 NTSTATUS
get_thread_wow64_context( HANDLE handle
, void *ctx
, ULONG size
)
1272 return STATUS_INVALID_INFO_CLASS
;
1276 /***********************************************************************
1277 * is_privileged_instr
1279 * Check if the fault location is a privileged instruction.
1280 * Based on the instruction emulation code in dlls/kernel/instr.c.
1282 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1285 unsigned int i
, len
, prefix_count
= 0;
1287 if (!ldt_is_system( context
->SegCs
)) return 0;
1288 len
= virtual_uninterrupted_read_memory( (BYTE
*)context
->Eip
, instr
, sizeof(instr
) );
1290 for (i
= 0; i
< len
; i
++) switch (instr
[i
])
1292 /* instruction prefixes */
1293 case 0x2e: /* %cs: */
1294 case 0x36: /* %ss: */
1295 case 0x3e: /* %ds: */
1296 case 0x26: /* %es: */
1297 case 0x64: /* %fs: */
1298 case 0x65: /* %gs: */
1299 case 0x66: /* opcode size */
1300 case 0x67: /* addr size */
1301 case 0xf0: /* lock */
1302 case 0xf2: /* repne */
1303 case 0xf3: /* repe */
1304 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1307 case 0x0f: /* extended instruction */
1308 if (i
== len
- 1) return 0;
1309 switch(instr
[i
+ 1])
1311 case 0x20: /* mov crX, reg */
1312 case 0x21: /* mov drX, reg */
1313 case 0x22: /* mov reg, crX */
1314 case 0x23: /* mov reg drX */
1315 return EXCEPTION_PRIV_INSTRUCTION
;
1318 case 0x6c: /* insb (%dx) */
1319 case 0x6d: /* insl (%dx) */
1320 case 0x6e: /* outsb (%dx) */
1321 case 0x6f: /* outsl (%dx) */
1322 case 0xcd: /* int $xx */
1323 case 0xe4: /* inb al,XX */
1324 case 0xe5: /* in (e)ax,XX */
1325 case 0xe6: /* outb XX,al */
1326 case 0xe7: /* out XX,(e)ax */
1327 case 0xec: /* inb (%dx),%al */
1328 case 0xed: /* inl (%dx),%eax */
1329 case 0xee: /* outb %al,(%dx) */
1330 case 0xef: /* outl %eax,(%dx) */
1331 case 0xf4: /* hlt */
1332 case 0xfa: /* cli */
1333 case 0xfb: /* sti */
1334 return EXCEPTION_PRIV_INSTRUCTION
;
1342 /***********************************************************************
1345 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1347 static inline BOOL
check_invalid_gs( ucontext_t
*sigcontext
, CONTEXT
*context
)
1349 unsigned int prefix_count
= 0;
1350 const BYTE
*instr
= (BYTE
*)context
->Eip
;
1351 WORD system_gs
= x86_thread_data()->gs
;
1353 if (context
->SegGs
== system_gs
) return FALSE
;
1354 if (!ldt_is_system( context
->SegCs
)) return FALSE
;
1355 /* only handle faults in system libraries */
1356 if (virtual_is_valid_code_address( instr
, 1 )) return FALSE
;
1358 for (;;) switch(*instr
)
1360 /* instruction prefixes */
1361 case 0x2e: /* %cs: */
1362 case 0x36: /* %ss: */
1363 case 0x3e: /* %ds: */
1364 case 0x26: /* %es: */
1365 case 0x64: /* %fs: */
1366 case 0x66: /* opcode size */
1367 case 0x67: /* addr size */
1368 case 0xf0: /* lock */
1369 case 0xf2: /* repne */
1370 case 0xf3: /* repe */
1371 if (++prefix_count
>= 15) return FALSE
;
1374 case 0x65: /* %gs: */
1375 TRACE( "%04x/%04x at %p, fixing up\n", context
->SegGs
, system_gs
, instr
);
1376 GS_sig(sigcontext
) = system_gs
;
1384 #include "pshpack1.h"
1389 DWORD movl
; /* movl this,4(%esp) */
1391 BYTE jmp
; /* jmp func */
1396 BYTE movl
; /* movl this,ecx */
1398 BYTE jmp
; /* jmp func */
1403 BYTE movl1
; /* movl this,edx */
1405 BYTE movl2
; /* movl func,ecx */
1407 WORD jmp
; /* jmp ecx */
1411 BYTE movl1
; /* movl this,ecx */
1413 BYTE movl2
; /* movl func,eax */
1415 WORD jmp
; /* jmp eax */
1419 DWORD inst1
; /* pop ecx
1426 #include "poppack.h"
1428 /**********************************************************************
1431 * Check if code destination is an ATL thunk, and emulate it if so.
1433 static BOOL
check_atl_thunk( ucontext_t
*sigcontext
, EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1435 const union atl_thunk
*thunk
= (const union atl_thunk
*)rec
->ExceptionInformation
[1];
1436 union atl_thunk thunk_copy
;
1439 thunk_len
= virtual_uninterrupted_read_memory( thunk
, &thunk_copy
, sizeof(*thunk
) );
1440 if (!thunk_len
) return FALSE
;
1442 if (thunk_len
>= sizeof(thunk_copy
.t1
) && thunk_copy
.t1
.movl
== 0x042444c7 &&
1443 thunk_copy
.t1
.jmp
== 0xe9)
1445 if (!virtual_uninterrupted_write_memory( (DWORD
*)context
->Esp
+ 1,
1446 &thunk_copy
.t1
.this, sizeof(DWORD
) ))
1448 EIP_sig(sigcontext
) = (DWORD_PTR
)(&thunk
->t1
.func
+ 1) + thunk_copy
.t1
.func
;
1449 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1450 thunk
, EIP_sig(sigcontext
), thunk_copy
.t1
.this );
1454 else if (thunk_len
>= sizeof(thunk_copy
.t2
) && thunk_copy
.t2
.movl
== 0xb9 &&
1455 thunk_copy
.t2
.jmp
== 0xe9)
1457 ECX_sig(sigcontext
) = thunk_copy
.t2
.this;
1458 EIP_sig(sigcontext
) = (DWORD_PTR
)(&thunk
->t2
.func
+ 1) + thunk_copy
.t2
.func
;
1459 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1460 thunk
, EIP_sig(sigcontext
), ECX_sig(sigcontext
) );
1463 else if (thunk_len
>= sizeof(thunk_copy
.t3
) && thunk_copy
.t3
.movl1
== 0xba &&
1464 thunk_copy
.t3
.movl2
== 0xb9 &&
1465 thunk_copy
.t3
.jmp
== 0xe1ff)
1467 EDX_sig(sigcontext
) = thunk_copy
.t3
.this;
1468 ECX_sig(sigcontext
) = thunk_copy
.t3
.func
;
1469 EIP_sig(sigcontext
) = thunk_copy
.t3
.func
;
1470 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1471 thunk
, EIP_sig(sigcontext
), ECX_sig(sigcontext
), EDX_sig(sigcontext
) );
1474 else if (thunk_len
>= sizeof(thunk_copy
.t4
) && thunk_copy
.t4
.movl1
== 0xb9 &&
1475 thunk_copy
.t4
.movl2
== 0xb8 &&
1476 thunk_copy
.t4
.jmp
== 0xe0ff)
1478 ECX_sig(sigcontext
) = thunk_copy
.t4
.this;
1479 EAX_sig(sigcontext
) = thunk_copy
.t4
.func
;
1480 EIP_sig(sigcontext
) = thunk_copy
.t4
.func
;
1481 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1482 thunk
, EIP_sig(sigcontext
), EAX_sig(sigcontext
), ECX_sig(sigcontext
) );
1485 else if (thunk_len
>= sizeof(thunk_copy
.t5
) && thunk_copy
.t5
.inst1
== 0xff515859 &&
1486 thunk_copy
.t5
.inst2
== 0x0460)
1489 if (virtual_uninterrupted_read_memory( (DWORD
*)context
->Esp
, sp
, sizeof(sp
) ) == sizeof(sp
) &&
1490 virtual_uninterrupted_read_memory( (DWORD
*)sp
[1] + 1, &func
, sizeof(DWORD
) ) == sizeof(DWORD
) &&
1491 !virtual_uninterrupted_write_memory( (DWORD
*)context
->Esp
+ 1, &sp
[0], sizeof(sp
[0]) ))
1493 ECX_sig(sigcontext
) = sp
[0];
1494 EAX_sig(sigcontext
) = sp
[1];
1495 ESP_sig(sigcontext
) += sizeof(DWORD
);
1496 EIP_sig(sigcontext
) = func
;
1497 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1498 thunk
, EIP_sig(sigcontext
), EAX_sig(sigcontext
),
1499 ECX_sig(sigcontext
), ESP_sig(sigcontext
) );
1508 /***********************************************************************
1509 * setup_exception_record
1511 * Setup the exception record and context on the thread stack.
1513 static void *setup_exception_record( ucontext_t
*sigcontext
, EXCEPTION_RECORD
*rec
, struct xcontext
*xcontext
)
1515 void *stack
= init_handler( sigcontext
);
1517 rec
->ExceptionAddress
= (void *)EIP_sig( sigcontext
);
1518 save_context( xcontext
, sigcontext
);
1522 /***********************************************************************
1523 * setup_raise_exception
1525 * Change context to setup a call to a raise exception function.
1527 static void setup_raise_exception( ucontext_t
*sigcontext
, void *stack_ptr
,
1528 EXCEPTION_RECORD
*rec
, struct xcontext
*xcontext
)
1530 CONTEXT
*context
= &xcontext
->c
;
1536 EXCEPTION_RECORD
*rec_ptr
; /* first arg for KiUserExceptionDispatcher */
1537 CONTEXT
*context_ptr
; /* second arg for KiUserExceptionDispatcher */
1539 CONTEXT_EX context_ex
;
1540 EXCEPTION_RECORD rec
;
1546 C_ASSERT( (offsetof(struct stack_layout
, xstate
) == sizeof(struct stack_layout
)) );
1548 NTSTATUS status
= send_debug_event( rec
, context
, TRUE
);
1550 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
1552 restore_context( xcontext
, sigcontext
);
1556 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
1557 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
1559 stack_size
= sizeof(*stack
);
1560 if ((src_xs
= xstate_from_context( context
)))
1562 stack_size
+= (ULONG_PTR
)stack_ptr
- (((ULONG_PTR
)stack_ptr
1563 - sizeof(XSTATE
)) & ~(ULONG_PTR
)63);
1566 stack
= virtual_setup_exception( stack_ptr
, stack_size
, rec
);
1568 stack
->context
= *context
;
1572 XSTATE
*dst_xs
= (XSTATE
*)stack
->xstate
;
1574 assert(!((ULONG_PTR
)dst_xs
& 63));
1575 context_init_xstate( &stack
->context
, stack
->xstate
);
1576 memset( dst_xs
, 0, offsetof(XSTATE
, YmmContext
) );
1577 dst_xs
->CompactionMask
= xstate_compaction_enabled
? 0x8000000000000004 : 0;
1578 if (src_xs
->Mask
& 4)
1581 memcpy( &dst_xs
->YmmContext
, &src_xs
->YmmContext
, sizeof(dst_xs
->YmmContext
) );
1585 stack
->rec_ptr
= &stack
->rec
;
1586 stack
->context_ptr
= &stack
->context
;
1587 ESP_sig(sigcontext
) = (DWORD
)stack
;
1588 EIP_sig(sigcontext
) = (DWORD
)pKiUserExceptionDispatcher
;
1589 /* clear single-step, direction, and align check flag */
1590 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1591 CS_sig(sigcontext
) = get_cs();
1592 DS_sig(sigcontext
) = get_ds();
1593 ES_sig(sigcontext
) = get_ds();
1594 FS_sig(sigcontext
) = get_fs();
1595 GS_sig(sigcontext
) = get_gs();
1596 SS_sig(sigcontext
) = get_ds();
1600 /***********************************************************************
1603 * Do the full setup to raise an exception from an exception record.
1605 static void setup_exception( ucontext_t
*sigcontext
, EXCEPTION_RECORD
*rec
)
1607 struct xcontext xcontext
;
1608 void *stack
= setup_exception_record( sigcontext
, rec
, &xcontext
);
1610 setup_raise_exception( sigcontext
, stack
, rec
, &xcontext
);
1613 /* stack layout when calling an user apc function.
1614 * FIXME: match Windows ABI. */
1615 struct apc_stack_layout
1625 struct apc_stack_layout
* WINAPI
setup_user_apc_dispatcher_stack( CONTEXT
*context
, struct apc_stack_layout
*stack
,
1626 void *arg1
, void *arg2
, void *arg3
,
1627 void *func
, NTSTATUS status
) DECLSPEC_HIDDEN
;
1628 struct apc_stack_layout
* WINAPI
setup_user_apc_dispatcher_stack( CONTEXT
*context
, struct apc_stack_layout
*stack
,
1629 void *arg1
, void *arg2
, void *arg3
,
1630 void *func
, NTSTATUS status
)
1636 c
.ContextFlags
= CONTEXT_FULL
;
1637 NtGetContextThread( GetCurrentThread(), &c
);
1641 memmove( &stack
->context
, context
, sizeof(stack
->context
) );
1642 stack
->context_ptr
= &stack
->context
;
1650 C_ASSERT( sizeof(struct apc_stack_layout
) == 0x2e0 );
1651 C_ASSERT( offsetof(struct apc_stack_layout
, context
) == 20 );
1653 /***********************************************************************
1654 * call_user_apc_dispatcher
1656 __ASM_GLOBAL_FUNC( call_user_apc_dispatcher
,
1657 "movl 4(%esp),%esi\n\t" /* context_ptr */
1658 "movl 24(%esp),%edi\n\t" /* dispatcher */
1659 "leal 4(%esp),%ebp\n\t"
1660 "test %esi,%esi\n\t"
1662 "movl 0xc4(%esi),%eax\n\t" /* context_ptr->Esp */
1664 "1:\tmovl %fs:0x1f8,%eax\n\t" /* x86_thread_data()->syscall_frame */
1665 "leal 0x38(%eax),%eax\n\t" /* &x86_thread_data()->syscall_frame->ret_addr */
1666 "2:\tsubl $0x2e0,%eax\n\t" /* sizeof(struct apc_stack_layout) */
1667 "movl %ebp,%esp\n\t" /* pop return address */
1668 "cmpl %esp,%eax\n\t"
1669 "cmovbl %eax,%esp\n\t"
1670 "pushl 24(%ebp)\n\t" /* status */
1671 "pushl 16(%ebp)\n\t" /* func */
1672 "pushl 12(%ebp)\n\t" /* arg3 */
1673 "pushl 8(%ebp)\n\t" /* arg2 */
1674 "pushl 4(%ebp)\n\t" /* arg1 */
1677 "call " __ASM_STDCALL("setup_user_apc_dispatcher_stack",24) "\n\t"
1678 "movl $0,%fs:0x1f8\n\t" /* x86_thread_data()->syscall_frame = NULL */
1679 "movl %eax,%esp\n\t"
1680 "leal 20(%eax),%esi\n\t"
1681 "movl 0xb4(%esi),%ebp\n\t" /* context.Ebp */
1682 "pushl 0xb8(%esi)\n\t" /* context.Eip */
1686 /***********************************************************************
1687 * call_raise_user_exception_dispatcher
1689 void WINAPI
call_raise_user_exception_dispatcher( NTSTATUS (WINAPI
*dispatcher
)(void) )
1691 x86_thread_data()->syscall_frame
->eip
= (DWORD
)dispatcher
;
1695 /***********************************************************************
1696 * call_user_exception_dispatcher
1698 __ASM_GLOBAL_FUNC( call_user_exception_dispatcher
,
1699 "movl 4(%esp),%edx\n\t" /* rec */
1700 "movl 8(%esp),%ecx\n\t" /* context */
1701 "cmpl $0x80000003,(%edx)\n\t" /* rec->ExceptionCode */
1703 "decl 0xb8(%ecx)\n" /* context->Eip */
1704 "1:\tmovl %fs:0x1f8,%eax\n\t" /* x86_thread_data()->syscall_frame */
1705 "movl 0x1c(%eax),%ebx\n\t" /* frame->ebx */
1706 "movl 0x28(%eax),%edi\n\t" /* frame->edi */
1707 "movl 0x2c(%eax),%esi\n\t" /* frame->esi */
1708 "movl 0x30(%eax),%ebp\n\t" /* frame->ebp */
1709 "movl %edx,0x30(%eax)\n\t"
1710 "movl %ecx,0x34(%eax)\n\t"
1711 "movl 12(%esp),%edx\n\t" /* dispatcher */
1712 "movl $0,%fs:0x1f8\n\t"
1713 "leal 0x30(%eax),%esp\n\t"
1716 /**********************************************************************
1719 * Get the FPU exception code from the FPU status.
1721 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1723 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1725 if (status
& 0x01) /* IE */
1727 if (status
& 0x40) /* SF */
1728 return EXCEPTION_FLT_STACK_CHECK
;
1730 return EXCEPTION_FLT_INVALID_OPERATION
;
1732 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1733 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1734 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1735 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1736 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1737 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1741 /***********************************************************************
1744 * Handle an interrupt.
1746 static BOOL
handle_interrupt( unsigned int interrupt
, ucontext_t
*sigcontext
, void *stack
,
1747 EXCEPTION_RECORD
*rec
, struct xcontext
*xcontext
)
1749 CONTEXT
*context
= &xcontext
->c
;
1756 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
1757 * service is usually not recognized when called from usermode. */
1758 switch (context
->Eax
)
1760 case 1: /* BREAKPOINT_PRINT */
1761 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
1762 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
1763 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
1764 EIP_sig(sigcontext
) += 3;
1769 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1770 rec
->ExceptionAddress
= (void *)context
->Eip
;
1771 rec
->NumberParameters
= is_wow64
? 1 : 3;
1772 rec
->ExceptionInformation
[0] = context
->Eax
;
1773 rec
->ExceptionInformation
[1] = context
->Ecx
;
1774 rec
->ExceptionInformation
[2] = context
->Edx
;
1775 setup_raise_exception( sigcontext
, stack
, rec
, xcontext
);
1783 /***********************************************************************
1784 * handle_syscall_fault
1786 * Handle a page fault happening during a system call.
1788 static BOOL
handle_syscall_fault( ucontext_t
*sigcontext
, void *stack_ptr
,
1789 EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1791 struct syscall_frame
*frame
= x86_thread_data()->syscall_frame
;
1792 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)NtCurrentTeb()->Tib
.ExceptionList
;
1795 if (!frame
) return FALSE
;
1797 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
1798 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
1799 context
->Eip
, GetCurrentThreadId() );
1800 for (i
= 0; i
< rec
->NumberParameters
; i
++)
1801 TRACE( " info[%d]=%08lx\n", i
, rec
->ExceptionInformation
[i
] );
1802 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
1803 context
->Eax
, context
->Ebx
, context
->Ecx
,
1804 context
->Edx
, context
->Esi
, context
->Edi
);
1805 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
1806 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
1807 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
1809 if ((char *)wine_frame
< (char *)frame
)
1811 /* stack frame for calling __wine_longjmp */
1815 __wine_jmp_buf
*jmp
;
1819 TRACE( "returning to handler\n" );
1820 stack
= virtual_setup_exception( stack_ptr
, sizeof(*stack
), rec
);
1822 stack
->jmp
= &wine_frame
->jmp
;
1823 stack
->retaddr
= (void *)0xdeadbabe;
1824 ESP_sig(sigcontext
) = (DWORD
)stack
;
1825 EIP_sig(sigcontext
) = (DWORD
)__wine_longjmp
;
1829 TRACE( "returning to user mode ip=%08x ret=%08x\n", frame
->eip
, rec
->ExceptionCode
);
1830 EAX_sig(sigcontext
) = rec
->ExceptionCode
;
1831 EBX_sig(sigcontext
) = frame
->ebx
;
1832 ESI_sig(sigcontext
) = frame
->esi
;
1833 EDI_sig(sigcontext
) = frame
->edi
;
1834 EBP_sig(sigcontext
) = frame
->ebp
;
1835 ESP_sig(sigcontext
) = frame
->esp
;
1836 EIP_sig(sigcontext
) = frame
->eip
;
1837 x86_thread_data()->syscall_frame
= NULL
;
1843 /**********************************************************************
1846 * Handler for SIGSEGV and related errors.
1848 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1850 EXCEPTION_RECORD rec
= { 0 };
1851 struct xcontext xcontext
;
1852 ucontext_t
*ucontext
= sigcontext
;
1853 void *stack
= setup_exception_record( sigcontext
, &rec
, &xcontext
);
1855 switch (TRAP_sig(ucontext
))
1857 case TRAP_x86_OFLOW
: /* Overflow exception */
1858 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1860 case TRAP_x86_BOUND
: /* Bound range exception */
1861 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1863 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1864 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1866 case TRAP_x86_STKFLT
: /* Stack fault */
1867 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1869 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1870 case TRAP_x86_PROTFLT
: /* General protection fault */
1872 WORD err
= ERROR_sig(ucontext
);
1873 if (!err
&& (rec
.ExceptionCode
= is_privileged_instr( &xcontext
.c
))) break;
1874 if ((err
& 7) == 2 && handle_interrupt( err
>> 3, ucontext
, stack
, &rec
, &xcontext
)) return;
1875 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1876 rec
.NumberParameters
= 2;
1877 rec
.ExceptionInformation
[0] = 0;
1878 /* if error contains a LDT selector, use that as fault address */
1879 if ((err
& 7) == 4 && !ldt_is_system( err
| 7 )) rec
.ExceptionInformation
[1] = err
& ~7;
1882 rec
.ExceptionInformation
[1] = 0xffffffff;
1883 if (check_invalid_gs( ucontext
, &xcontext
.c
)) return;
1887 case TRAP_x86_PAGEFLT
: /* Page fault */
1888 rec
.NumberParameters
= 2;
1889 rec
.ExceptionInformation
[0] = (ERROR_sig(ucontext
) >> 1) & 0x09;
1890 rec
.ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
1891 rec
.ExceptionCode
= virtual_handle_fault( siginfo
->si_addr
, rec
.ExceptionInformation
[0], stack
);
1892 if (!rec
.ExceptionCode
) return;
1893 if (rec
.ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
&&
1894 rec
.ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
1897 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
1898 &flags
, sizeof(flags
), NULL
);
1899 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
) &&
1900 check_atl_thunk( ucontext
, &rec
, &xcontext
.c
))
1903 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1904 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
)) rec
.ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
1907 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
1908 /* FIXME: pass through exception handler first? */
1909 if (xcontext
.c
.EFlags
& 0x00040000)
1911 EFL_sig(ucontext
) &= ~0x00040000; /* disable AC flag */
1914 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1917 WINE_ERR( "Got unexpected trap %d\n", TRAP_sig(ucontext
) );
1919 case TRAP_x86_NMI
: /* NMI interrupt */
1920 case TRAP_x86_DNA
: /* Device not available exception */
1921 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
1922 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
1923 case TRAP_x86_MCHK
: /* Machine check exception */
1924 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
1925 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1928 if (handle_syscall_fault( ucontext
, stack
, &rec
, &xcontext
.c
)) return;
1929 setup_raise_exception( ucontext
, stack
, &rec
, &xcontext
);
1933 /**********************************************************************
1936 * Handler for SIGTRAP.
1938 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1940 EXCEPTION_RECORD rec
= { 0 };
1941 struct xcontext xcontext
;
1942 ucontext_t
*ucontext
= sigcontext
;
1943 void *stack
= setup_exception_record( sigcontext
, &rec
, &xcontext
);
1945 switch (TRAP_sig(ucontext
))
1947 case TRAP_x86_TRCTRAP
: /* Single-step exception */
1948 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1949 /* when single stepping can't tell whether this is a hw bp or a
1950 * single step interrupt. try to avoid as much overhead as possible
1951 * and only do a server call if there is any hw bp enabled. */
1952 if (!(xcontext
.c
.EFlags
& 0x100) || (xcontext
.c
.Dr7
& 0xff))
1954 /* (possible) hardware breakpoint, fetch the debug registers */
1955 DWORD saved_flags
= xcontext
.c
.ContextFlags
;
1956 xcontext
.c
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1957 NtGetContextThread( GetCurrentThread(), &xcontext
.c
);
1958 xcontext
.c
.ContextFlags
|= saved_flags
; /* restore flags */
1960 xcontext
.c
.EFlags
&= ~0x100; /* clear single-step flag */
1962 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
1963 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
1966 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
1967 rec
.NumberParameters
= is_wow64
? 1 : 3;
1968 rec
.ExceptionInformation
[0] = 0;
1969 rec
.ExceptionInformation
[1] = 0; /* FIXME */
1970 rec
.ExceptionInformation
[2] = 0; /* FIXME */
1973 setup_raise_exception( sigcontext
, stack
, &rec
, &xcontext
);
1977 /**********************************************************************
1980 * Handler for SIGFPE.
1982 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1984 EXCEPTION_RECORD rec
= { 0 };
1985 struct xcontext xcontext
;
1986 ucontext_t
*ucontext
= sigcontext
;
1987 void *stack
= setup_exception_record( sigcontext
, &rec
, &xcontext
);
1989 switch (TRAP_sig(ucontext
))
1991 case TRAP_x86_DIVIDE
: /* Division by zero exception */
1992 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1994 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
1995 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1997 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
1998 rec
.ExceptionCode
= get_fpu_code( &xcontext
.c
);
1999 rec
.ExceptionAddress
= (void *)xcontext
.c
.FloatSave
.ErrorOffset
;
2001 case TRAP_x86_CACHEFLT
: /* SIMD exception */
2003 * Behaviour only tested for divide-by-zero exceptions
2004 * Check for other SIMD exceptions as well */
2005 if(siginfo
->si_code
!= FPE_FLTDIV
&& siginfo
->si_code
!= FPE_FLTINV
)
2006 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
2009 rec
.ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
2010 rec
.NumberParameters
= 1;
2011 /* no idea what meaning is actually behind this but that's what native does */
2012 rec
.ExceptionInformation
[0] = 0;
2015 WINE_ERR( "Got unexpected trap %d\n", TRAP_sig(ucontext
) );
2016 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2019 setup_raise_exception( sigcontext
, stack
, &rec
, &xcontext
);
2023 /**********************************************************************
2026 * Handler for SIGINT.
2028 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2030 EXCEPTION_RECORD rec
= { CONTROL_C_EXIT
};
2032 setup_exception( sigcontext
, &rec
);
2035 /**********************************************************************
2038 * Handler for SIGABRT.
2040 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2042 EXCEPTION_RECORD rec
= { EXCEPTION_WINE_ASSERTION
, EH_NONCONTINUABLE
};
2044 setup_exception( sigcontext
, &rec
);
2048 /**********************************************************************
2051 * Handler for SIGQUIT.
2053 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2055 init_handler( sigcontext
);
2060 /**********************************************************************
2063 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2065 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2067 struct xcontext xcontext
;
2069 init_handler( sigcontext
);
2070 if (x86_thread_data()->syscall_frame
)
2072 DECLSPEC_ALIGN(64) XSTATE xs
;
2073 xcontext
.c
.ContextFlags
= CONTEXT_FULL
;
2074 context_init_xstate( &xcontext
.c
, &xs
);
2076 NtGetContextThread( GetCurrentThread(), &xcontext
.c
);
2077 wait_suspend( &xcontext
.c
);
2078 NtSetContextThread( GetCurrentThread(), &xcontext
.c
);
2082 save_context( &xcontext
, sigcontext
);
2083 wait_suspend( &xcontext
.c
);
2084 restore_context( &xcontext
, sigcontext
);
2089 /***********************************************************************
2093 #define LDT_SIZE 8192
2095 #define LDT_FLAGS_DATA 0x13 /* Data segment */
2096 #define LDT_FLAGS_CODE 0x1b /* Code segment */
2097 #define LDT_FLAGS_32BIT 0x40 /* Segment is 32-bit (code or stack) */
2098 #define LDT_FLAGS_ALLOCATED 0x80 /* Segment is allocated */
2102 void *base
[LDT_SIZE
];
2103 unsigned int limit
[LDT_SIZE
];
2104 unsigned char flags
[LDT_SIZE
];
2107 static WORD gdt_fs_sel
;
2108 static pthread_mutex_t ldt_mutex
= PTHREAD_MUTEX_INITIALIZER
;
2109 static const LDT_ENTRY null_entry
;
2111 static inline void *ldt_get_base( LDT_ENTRY ent
)
2113 return (void *)(ent
.BaseLow
|
2114 (ULONG_PTR
)ent
.HighWord
.Bits
.BaseMid
<< 16 |
2115 (ULONG_PTR
)ent
.HighWord
.Bits
.BaseHi
<< 24);
2118 static inline unsigned int ldt_get_limit( LDT_ENTRY ent
)
2120 unsigned int limit
= ent
.LimitLow
| (ent
.HighWord
.Bits
.LimitHi
<< 16);
2121 if (ent
.HighWord
.Bits
.Granularity
) limit
= (limit
<< 12) | 0xfff;
2125 static LDT_ENTRY
ldt_make_entry( void *base
, unsigned int limit
, unsigned char flags
)
2129 entry
.BaseLow
= (WORD
)(ULONG_PTR
)base
;
2130 entry
.HighWord
.Bits
.BaseMid
= (BYTE
)((ULONG_PTR
)base
>> 16);
2131 entry
.HighWord
.Bits
.BaseHi
= (BYTE
)((ULONG_PTR
)base
>> 24);
2132 if ((entry
.HighWord
.Bits
.Granularity
= (limit
>= 0x100000))) limit
>>= 12;
2133 entry
.LimitLow
= (WORD
)limit
;
2134 entry
.HighWord
.Bits
.LimitHi
= limit
>> 16;
2135 entry
.HighWord
.Bits
.Dpl
= 3;
2136 entry
.HighWord
.Bits
.Pres
= 1;
2137 entry
.HighWord
.Bits
.Type
= flags
;
2138 entry
.HighWord
.Bits
.Sys
= 0;
2139 entry
.HighWord
.Bits
.Reserved_0
= 0;
2140 entry
.HighWord
.Bits
.Default_Big
= (flags
& LDT_FLAGS_32BIT
) != 0;
2144 static void ldt_set_entry( WORD sel
, LDT_ENTRY entry
)
2146 int index
= sel
>> 3;
2149 struct modify_ldt_s ldt_info
= { index
};
2151 ldt_info
.base_addr
= ldt_get_base( entry
);
2152 ldt_info
.limit
= entry
.LimitLow
| (entry
.HighWord
.Bits
.LimitHi
<< 16);
2153 ldt_info
.seg_32bit
= entry
.HighWord
.Bits
.Default_Big
;
2154 ldt_info
.contents
= (entry
.HighWord
.Bits
.Type
>> 2) & 3;
2155 ldt_info
.read_exec_only
= !(entry
.HighWord
.Bits
.Type
& 2);
2156 ldt_info
.limit_in_pages
= entry
.HighWord
.Bits
.Granularity
;
2157 ldt_info
.seg_not_present
= !entry
.HighWord
.Bits
.Pres
;
2158 ldt_info
.usable
= entry
.HighWord
.Bits
.Sys
;
2159 if (modify_ldt( 0x11, &ldt_info
, sizeof(ldt_info
) ) < 0) perror( "modify_ldt" );
2160 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
2161 /* The kernel will only let us set LDTs with user priority level */
2162 if (entry
.HighWord
.Bits
.Pres
&& entry
.HighWord
.Bits
.Dpl
!= 3) entry
.HighWord
.Bits
.Dpl
= 3;
2163 if (i386_set_ldt(index
, (union descriptor
*)&entry
, 1) < 0)
2165 perror("i386_set_ldt");
2166 fprintf( stderr
, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
2169 #elif defined(__svr4__) || defined(_SCO_DS)
2173 ldt_mod
.bo
= (unsigned long)ldt_get_base( entry
);
2174 ldt_mod
.ls
= entry
.LimitLow
| (entry
.HighWord
.Bits
.LimitHi
<< 16);
2175 ldt_mod
.acc1
= entry
.HighWord
.Bytes
.Flags1
;
2176 ldt_mod
.acc2
= entry
.HighWord
.Bytes
.Flags2
>> 4;
2177 if (sysi86(SI86DSCR
, &ldt_mod
) == -1) perror("sysi86");
2178 #elif defined(__APPLE__)
2179 if (i386_set_ldt(index
, (union ldt_entry
*)&entry
, 1) < 0) perror("i386_set_ldt");
2180 #elif defined(__GNU__)
2181 if (i386_set_ldt(mach_thread_self(), sel
, (descriptor_list_t
)&entry
, 1) != KERN_SUCCESS
)
2182 perror("i386_set_ldt");
2184 fprintf( stderr
, "No LDT support on this platform\n" );
2188 __wine_ldt_copy
.base
[index
] = ldt_get_base( entry
);
2189 __wine_ldt_copy
.limit
[index
] = ldt_get_limit( entry
);
2190 __wine_ldt_copy
.flags
[index
] = (entry
.HighWord
.Bits
.Type
|
2191 (entry
.HighWord
.Bits
.Default_Big
? LDT_FLAGS_32BIT
: 0) |
2192 LDT_FLAGS_ALLOCATED
);
2195 static void ldt_set_fs( WORD sel
, TEB
*teb
)
2197 if (sel
== gdt_fs_sel
)
2200 struct modify_ldt_s ldt_info
= { sel
>> 3 };
2202 ldt_info
.base_addr
= teb
;
2203 ldt_info
.limit
= page_size
- 1;
2204 ldt_info
.seg_32bit
= 1;
2205 if (set_thread_area( &ldt_info
) < 0) perror( "set_thread_area" );
2206 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
2207 i386_set_fsbase( teb
);
2214 /**********************************************************************
2215 * get_thread_ldt_entry
2217 NTSTATUS
get_thread_ldt_entry( HANDLE handle
, void *data
, ULONG len
, ULONG
*ret_len
)
2219 THREAD_DESCRIPTOR_INFORMATION
*info
= data
;
2220 NTSTATUS status
= STATUS_SUCCESS
;
2222 if (len
< sizeof(*info
)) return STATUS_INFO_LENGTH_MISMATCH
;
2223 if (info
->Selector
>> 16) return STATUS_UNSUCCESSFUL
;
2225 if (is_gdt_sel( info
->Selector
))
2227 if (!(info
->Selector
& ~3))
2228 info
->Entry
= null_entry
;
2229 else if ((info
->Selector
| 3) == get_cs())
2230 info
->Entry
= ldt_make_entry( 0, ~0u, LDT_FLAGS_CODE
| LDT_FLAGS_32BIT
);
2231 else if ((info
->Selector
| 3) == get_ds())
2232 info
->Entry
= ldt_make_entry( 0, ~0u, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2233 else if ((info
->Selector
| 3) == get_fs())
2234 info
->Entry
= ldt_make_entry( NtCurrentTeb(), 0xfff, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2236 return STATUS_UNSUCCESSFUL
;
2240 SERVER_START_REQ( get_selector_entry
)
2242 req
->handle
= wine_server_obj_handle( handle
);
2243 req
->entry
= info
->Selector
>> 3;
2244 status
= wine_server_call( req
);
2248 info
->Entry
= ldt_make_entry( (void *)reply
->base
, reply
->limit
, reply
->flags
);
2250 status
= STATUS_UNSUCCESSFUL
;
2255 if (status
== STATUS_SUCCESS
&& ret_len
)
2256 /* yes, that's a bit strange, but it's the way it is */
2257 *ret_len
= sizeof(info
->Entry
);
2263 /******************************************************************************
2264 * NtSetLdtEntries (NTDLL.@)
2265 * ZwSetLdtEntries (NTDLL.@)
2267 NTSTATUS WINAPI
NtSetLdtEntries( ULONG sel1
, LDT_ENTRY entry1
, ULONG sel2
, LDT_ENTRY entry2
)
2271 if (sel1
>> 16 || sel2
>> 16) return STATUS_INVALID_LDT_DESCRIPTOR
;
2272 if (sel1
&& (sel1
>> 3) < first_ldt_entry
) return STATUS_INVALID_LDT_DESCRIPTOR
;
2273 if (sel2
&& (sel2
>> 3) < first_ldt_entry
) return STATUS_INVALID_LDT_DESCRIPTOR
;
2275 server_enter_uninterrupted_section( &ldt_mutex
, &sigset
);
2276 if (sel1
) ldt_set_entry( sel1
, entry1
);
2277 if (sel2
) ldt_set_entry( sel2
, entry2
);
2278 server_leave_uninterrupted_section( &ldt_mutex
, &sigset
);
2279 return STATUS_SUCCESS
;
2283 /**********************************************************************
2284 * signal_init_threading
2286 void signal_init_threading(void)
2289 /* the preloader may have allocated it already */
2290 gdt_fs_sel
= get_fs();
2291 if (!gdt_fs_sel
|| !is_gdt_sel( gdt_fs_sel
))
2293 struct modify_ldt_s ldt_info
= { -1 };
2295 ldt_info
.seg_32bit
= 1;
2296 ldt_info
.usable
= 1;
2297 if (set_thread_area( &ldt_info
) >= 0) gdt_fs_sel
= (ldt_info
.entry_number
<< 3) | 3;
2298 else gdt_fs_sel
= 0;
2300 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
2301 gdt_fs_sel
= GSEL( GUFS_SEL
, SEL_UPL
);
2306 /**********************************************************************
2307 * signal_alloc_thread
2309 NTSTATUS
signal_alloc_thread( TEB
*teb
)
2311 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
2315 static int first_thread
= 1;
2318 LDT_ENTRY entry
= ldt_make_entry( teb
, page_size
- 1, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2320 if (first_thread
) /* no locking for first thread */
2322 /* leave some space if libc is using the LDT for %gs */
2323 if (!is_gdt_sel( get_gs() )) first_ldt_entry
= 512;
2324 idx
= first_ldt_entry
;
2325 ldt_set_entry( (idx
<< 3) | 7, entry
);
2330 server_enter_uninterrupted_section( &ldt_mutex
, &sigset
);
2331 for (idx
= first_ldt_entry
; idx
< LDT_SIZE
; idx
++)
2333 if (__wine_ldt_copy
.flags
[idx
]) continue;
2334 ldt_set_entry( (idx
<< 3) | 7, entry
);
2337 server_leave_uninterrupted_section( &ldt_mutex
, &sigset
);
2338 if (idx
== LDT_SIZE
) return STATUS_TOO_MANY_THREADS
;
2340 thread_data
->fs
= (idx
<< 3) | 7;
2342 else thread_data
->fs
= gdt_fs_sel
;
2344 teb
->WOW32Reserved
= syscall_dispatcher
;
2345 return STATUS_SUCCESS
;
2349 /**********************************************************************
2350 * signal_free_thread
2352 void signal_free_thread( TEB
*teb
)
2354 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
2357 if (gdt_fs_sel
) return;
2359 server_enter_uninterrupted_section( &ldt_mutex
, &sigset
);
2360 __wine_ldt_copy
.flags
[thread_data
->fs
>> 3] = 0;
2361 server_leave_uninterrupted_section( &ldt_mutex
, &sigset
);
2365 /**********************************************************************
2366 * signal_init_thread
2368 void signal_init_thread( TEB
*teb
)
2370 const WORD fpu_cw
= 0x27f;
2371 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
2373 ldt_set_fs( thread_data
->fs
, teb
);
2374 thread_data
->gs
= get_gs();
2376 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2380 /**********************************************************************
2381 * signal_init_process
2383 void signal_init_process(void)
2385 struct sigaction sig_act
;
2387 sig_act
.sa_mask
= server_block_set
;
2388 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
| SA_ONSTACK
;
2390 sig_act
.sa_flags
|= SA_RESTORER
;
2391 sig_act
.sa_restorer
= rt_sigreturn
;
2393 sig_act
.sa_sigaction
= int_handler
;
2394 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2395 sig_act
.sa_sigaction
= fpe_handler
;
2396 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2397 sig_act
.sa_sigaction
= abrt_handler
;
2398 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2399 sig_act
.sa_sigaction
= quit_handler
;
2400 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2401 sig_act
.sa_sigaction
= usr1_handler
;
2402 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2403 sig_act
.sa_sigaction
= trap_handler
;
2404 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2405 sig_act
.sa_sigaction
= segv_handler
;
2406 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2407 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2408 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2412 perror("sigaction");
2417 /**********************************************************************
2418 * signal_init_syscalls
2420 void *signal_init_syscalls(void)
2422 extern void __wine_syscall_dispatcher_fxsave(void) DECLSPEC_HIDDEN
;
2423 extern void __wine_syscall_dispatcher_xsave(void) DECLSPEC_HIDDEN
;
2424 extern void __wine_syscall_dispatcher_xsavec(void) DECLSPEC_HIDDEN
;
2426 if (xstate_compaction_enabled
)
2427 syscall_dispatcher
= __wine_syscall_dispatcher_xsavec
;
2428 else if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_XSAVE
)
2429 syscall_dispatcher
= __wine_syscall_dispatcher_xsave
;
2430 else if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_FXSR
)
2431 syscall_dispatcher
= __wine_syscall_dispatcher_fxsave
;
2433 syscall_dispatcher
= __wine_syscall_dispatcher
;
2435 return NtCurrentTeb()->WOW32Reserved
= syscall_dispatcher
;
2439 /***********************************************************************
2440 * init_thread_context
2442 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
)
2444 context
->SegCs
= get_cs();
2445 context
->SegDs
= get_ds();
2446 context
->SegEs
= get_ds();
2447 context
->SegFs
= get_fs();
2448 context
->SegGs
= get_gs();
2449 context
->SegSs
= get_ds();
2450 context
->EFlags
= 0x202;
2451 context
->Eax
= (DWORD
)entry
;
2452 context
->Ebx
= (DWORD
)arg
;
2453 context
->Esp
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
- 16;
2454 context
->Eip
= (DWORD
)pRtlUserThreadStart
;
2455 context
->FloatSave
.ControlWord
= 0x27f;
2456 ((XSAVE_FORMAT
*)context
->ExtendedRegisters
)->ControlWord
= 0x27f;
2457 ((XSAVE_FORMAT
*)context
->ExtendedRegisters
)->MxCsr
= 0x1f80;
2458 if (NtCurrentTeb64())
2460 WOW64_CPURESERVED
*cpu
= ULongToPtr( NtCurrentTeb64()->TlsSlots
[WOW64_TLS_CPURESERVED
] );
2461 memcpy( cpu
+ 1, context
, sizeof(*context
) );
2466 /***********************************************************************
2467 * get_initial_context
2469 PCONTEXT DECLSPEC_HIDDEN
get_initial_context( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
2475 CONTEXT context
= { CONTEXT_ALL
};
2477 init_thread_context( &context
, entry
, arg
);
2478 wait_suspend( &context
);
2479 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Esp
& ~15) - 1;
2484 ctx
= (CONTEXT
*)((char *)NtCurrentTeb()->Tib
.StackBase
- 16) - 1;
2485 init_thread_context( ctx
, entry
, arg
);
2487 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
2488 ctx
->ContextFlags
= CONTEXT_FULL
| CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
2493 /***********************************************************************
2494 * signal_start_thread
2496 __ASM_GLOBAL_FUNC( signal_start_thread
,
2498 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2499 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2500 "movl %esp,%ebp\n\t"
2501 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2503 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2505 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2507 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2508 /* store exit frame */
2509 "movl %ebp,%fs:0x1f4\n\t" /* x86_thread_data()->exit_frame */
2510 /* switch to thread stack */
2511 "movl %fs:4,%eax\n\t" /* NtCurrentTeb()->StackBase */
2512 "leal -0x1004(%eax),%esp\n\t"
2514 "pushl 16(%ebp)\n\t" /* suspend */
2515 "pushl 12(%ebp)\n\t" /* arg */
2516 "pushl 8(%ebp)\n\t" /* entry */
2517 "call " __ASM_NAME("get_initial_context") "\n\t"
2518 "movl %eax,(%esp)\n\t" /* context */
2519 "movl 20(%ebp),%edx\n\t" /* thunk */
2520 "xorl %ebp,%ebp\n\t"
2525 /***********************************************************************
2526 * signal_exit_thread
2528 __ASM_GLOBAL_FUNC( signal_exit_thread
,
2529 "movl 8(%esp),%ecx\n\t"
2530 /* fetch exit frame */
2531 "movl %fs:0x1f4,%edx\n\t" /* x86_thread_data()->exit_frame */
2532 "testl %edx,%edx\n\t"
2535 /* switch to exit frame stack */
2536 "1:\tmovl 4(%esp),%eax\n\t"
2537 "movl $0,%fs:0x1f4\n\t"
2538 "movl %edx,%ebp\n\t"
2539 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2540 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2541 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2542 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2543 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2544 "leal -20(%ebp),%esp\n\t"
2548 /**********************************************************************
2549 * NtCurrentTeb (NTDLL.@)
2551 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2553 #endif /* __i386__ */