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 restore_flags
; /* 00 */
485 DWORD eflags
; /* 04 */
503 C_ASSERT( sizeof(struct syscall_frame
) == 0x38 );
505 struct x86_thread_data
507 DWORD fs
; /* 1d4 TEB selector */
508 DWORD gs
; /* 1d8 libc selector; update winebuild if you move this! */
509 DWORD dr0
; /* 1dc debug registers */
515 void *exit_frame
; /* 1f4 exit frame pointer */
516 struct syscall_frame
*syscall_frame
; /* 1f8 frame pointer on syscall entry */
519 C_ASSERT( sizeof(struct x86_thread_data
) <= sizeof(((struct ntdll_thread_data
*)0)->cpu_data
) );
520 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, gs
) == 0x1d8 );
521 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, exit_frame
) == 0x1f4 );
522 C_ASSERT( offsetof( TEB
, GdiTebBatch
) + offsetof( struct x86_thread_data
, syscall_frame
) == 0x1f8 );
524 /* flags to control the behavior of the syscall dispatcher */
525 #define SYSCALL_HAVE_XSAVE 1
526 #define SYSCALL_HAVE_XSAVEC 2
527 #define SYSCALL_HAVE_FXSAVE 4
529 static inline struct x86_thread_data
*x86_thread_data(void)
531 return (struct x86_thread_data
*)ntdll_get_thread_data()->cpu_data
;
534 static struct syscall_xsave
*get_syscall_xsave( struct syscall_frame
*frame
)
536 return (struct syscall_xsave
*)((ULONG_PTR
)((struct syscall_xsave
*)frame
- 1) & ~63);
539 static inline WORD
get_cs(void) { WORD res
; __asm__( "movw %%cs,%0" : "=r" (res
) ); return res
; }
540 static inline WORD
get_ds(void) { WORD res
; __asm__( "movw %%ds,%0" : "=r" (res
) ); return res
; }
541 static inline WORD
get_fs(void) { WORD res
; __asm__( "movw %%fs,%0" : "=r" (res
) ); return res
; }
542 static inline WORD
get_gs(void) { WORD res
; __asm__( "movw %%gs,%0" : "=r" (res
) ); return res
; }
543 static inline void set_fs( WORD val
) { __asm__( "mov %0,%%fs" :: "r" (val
)); }
544 static inline void set_gs( WORD val
) { __asm__( "mov %0,%%gs" :: "r" (val
)); }
547 /***********************************************************************
550 NTSTATUS CDECL
unwind_builtin_dll( ULONG type
, struct _DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*context
)
552 return STATUS_UNSUCCESSFUL
;
556 /***********************************************************************
559 static inline int is_gdt_sel( WORD sel
)
565 /***********************************************************************
568 static inline int ldt_is_system( WORD sel
)
570 return is_gdt_sel( sel
) || ((sel
>> 3) < first_ldt_entry
);
574 /***********************************************************************
577 * Get the current teb based on the stack pointer.
579 static inline TEB
*get_current_teb(void)
582 __asm__("movl %%esp,%0" : "=g" (esp
) );
583 return (TEB
*)((esp
& ~signal_stack_mask
) + teb_offset
);
589 /* We have to workaround two Solaris breakages:
590 * - Solaris doesn't restore %ds and %es before calling the signal handler so exceptions in 16-bit
592 * - Solaris inserts a libc trampoline to call our handler, but the trampoline expects that registers
593 * are setup correctly. So we need to insert our own trampoline below the libc trampoline to set %gs.
596 extern int sigaction_syscall( int sig
, const struct sigaction
*new, struct sigaction
*old
);
597 __ASM_GLOBAL_FUNC( sigaction_syscall
,
598 "movl $0x62,%eax\n\t"
602 /* assume the same libc handler is used for all signals */
603 static void (*libc_sigacthandler
)( int signal
, siginfo_t
*siginfo
, void *context
);
605 static void wine_sigacthandler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
607 struct x86_thread_data
*thread_data
;
609 __asm__
__volatile__("mov %ss,%ax; mov %ax,%ds; mov %ax,%es");
611 thread_data
= (struct x86_thread_data
*)get_current_teb()->GdiTebBatch
;
612 set_fs( thread_data
->fs
);
613 set_gs( thread_data
->gs
);
615 libc_sigacthandler( signal
, siginfo
, sigcontext
);
618 static int solaris_sigaction( int sig
, const struct sigaction
*new, struct sigaction
*old
)
620 struct sigaction real_act
;
622 if (sigaction( sig
, new, old
) == -1) return -1;
624 /* retrieve the real handler and flags with a direct syscall */
625 sigaction_syscall( sig
, NULL
, &real_act
);
626 libc_sigacthandler
= real_act
.sa_sigaction
;
627 real_act
.sa_sigaction
= wine_sigacthandler
;
628 sigaction_syscall( sig
, &real_act
, NULL
);
631 #define sigaction(sig,new,old) solaris_sigaction(sig,new,old)
635 extern void clear_alignment_flag(void) DECLSPEC_HIDDEN
;
636 __ASM_GLOBAL_FUNC( clear_alignment_flag
,
638 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
639 "andl $~0x40000,(%esp)\n\t"
641 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
645 /***********************************************************************
648 * Handler initialization when the full context is not needed.
649 * Return the stack pointer to use for pushing the exception data.
651 static inline void *init_handler( const ucontext_t
*sigcontext
)
653 TEB
*teb
= get_current_teb();
655 clear_alignment_flag();
657 #ifndef __sun /* see above for Solaris handling */
659 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
660 set_fs( thread_data
->fs
);
661 set_gs( thread_data
->gs
);
665 if (!ldt_is_system(CS_sig(sigcontext
)) || !ldt_is_system(SS_sig(sigcontext
))) /* 16-bit mode */
668 * Win16 or DOS protected mode. Note that during switch
669 * from 16-bit mode to linear mode, CS may be set to system
670 * segment before FS is restored. Fortunately, in this case
671 * SS is still non-system segment. This is why both CS and SS
674 return teb
->SystemReserved1
[0];
676 return (void *)(ESP_sig(sigcontext
) & ~3);
680 /***********************************************************************
683 * Save the thread FPU context.
685 static inline void save_fpu( CONTEXT
*context
)
699 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
700 __asm__
__volatile__( "fnsave %0; fwait" : "=m" (context
->FloatSave
) );
702 /* Reset unmasked exceptions status to avoid firing an exception. */
703 memcpy(&float_status
, &context
->FloatSave
, sizeof(float_status
));
704 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
706 __asm__
__volatile__( "fldenv %0" : : "m" (float_status
) );
710 /***********************************************************************
713 * Restore the x87 FPU context
715 static inline void restore_fpu( const CONTEXT
*context
)
717 FLOATING_SAVE_AREA float_status
= context
->FloatSave
;
718 /* reset the current interrupt status */
719 float_status
.StatusWord
&= float_status
.ControlWord
| 0xffffff80;
720 __asm__
__volatile__( "frstor %0; fwait" : : "m" (float_status
) );
724 /***********************************************************************
727 * Build a standard FPU context from an extended one.
729 static void fpux_to_fpu( FLOATING_SAVE_AREA
*fpu
, const XSAVE_FORMAT
*fpux
)
731 unsigned int i
, tag
, stack_top
;
733 fpu
->ControlWord
= fpux
->ControlWord
| 0xffff0000;
734 fpu
->StatusWord
= fpux
->StatusWord
| 0xffff0000;
735 fpu
->ErrorOffset
= fpux
->ErrorOffset
;
736 fpu
->ErrorSelector
= fpux
->ErrorSelector
| (fpux
->ErrorOpcode
<< 16);
737 fpu
->DataOffset
= fpux
->DataOffset
;
738 fpu
->DataSelector
= fpux
->DataSelector
;
739 fpu
->Cr0NpxState
= fpux
->StatusWord
| 0xffff0000;
741 stack_top
= (fpux
->StatusWord
>> 11) & 7;
742 fpu
->TagWord
= 0xffff0000;
743 for (i
= 0; i
< 8; i
++)
745 memcpy( &fpu
->RegisterArea
[10 * i
], &fpux
->FloatRegisters
[i
], 10 );
746 if (!(fpux
->TagWord
& (1 << i
))) tag
= 3; /* empty */
749 const M128A
*reg
= &fpux
->FloatRegisters
[(i
- stack_top
) & 7];
750 if ((reg
->High
& 0x7fff) == 0x7fff) /* exponent all ones */
752 tag
= 2; /* special */
754 else if (!(reg
->High
& 0x7fff)) /* exponent all zeroes */
756 if (reg
->Low
) tag
= 2; /* special */
757 else tag
= 1; /* zero */
761 if (reg
->Low
>> 63) tag
= 0; /* valid */
762 else tag
= 2; /* special */
765 fpu
->TagWord
|= tag
<< (2 * i
);
770 /***********************************************************************
773 * Fill extended FPU context from standard one.
775 static void fpu_to_fpux( XSAVE_FORMAT
*fpux
, const FLOATING_SAVE_AREA
*fpu
)
779 fpux
->ControlWord
= fpu
->ControlWord
;
780 fpux
->StatusWord
= fpu
->StatusWord
;
781 fpux
->ErrorOffset
= fpu
->ErrorOffset
;
782 fpux
->ErrorSelector
= fpu
->ErrorSelector
;
783 fpux
->ErrorOpcode
= fpu
->ErrorSelector
>> 16;
784 fpux
->DataOffset
= fpu
->DataOffset
;
785 fpux
->DataSelector
= fpu
->DataSelector
;
787 for (i
= 0; i
< 8; i
++)
789 if (((fpu
->TagWord
>> (i
* 2)) & 3) != 3)
790 fpux
->TagWord
|= 1 << i
;
791 memcpy( &fpux
->FloatRegisters
[i
], &fpu
->RegisterArea
[10 * i
], 10 );
796 /***********************************************************************
799 * Build a context structure from the signal info.
801 static inline void save_context( struct xcontext
*xcontext
, const ucontext_t
*sigcontext
)
803 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
804 XSAVE_FORMAT
*fpux
= FPUX_sig(sigcontext
);
805 CONTEXT
*context
= &xcontext
->c
;
807 memset(context
, 0, sizeof(*context
));
808 context
->ContextFlags
= CONTEXT_FULL
| CONTEXT_DEBUG_REGISTERS
;
809 context
->Eax
= EAX_sig(sigcontext
);
810 context
->Ebx
= EBX_sig(sigcontext
);
811 context
->Ecx
= ECX_sig(sigcontext
);
812 context
->Edx
= EDX_sig(sigcontext
);
813 context
->Esi
= ESI_sig(sigcontext
);
814 context
->Edi
= EDI_sig(sigcontext
);
815 context
->Ebp
= EBP_sig(sigcontext
);
816 context
->EFlags
= EFL_sig(sigcontext
);
817 context
->Eip
= EIP_sig(sigcontext
);
818 context
->Esp
= ESP_sig(sigcontext
);
819 context
->SegCs
= LOWORD(CS_sig(sigcontext
));
820 context
->SegDs
= LOWORD(DS_sig(sigcontext
));
821 context
->SegEs
= LOWORD(ES_sig(sigcontext
));
822 context
->SegFs
= LOWORD(FS_sig(sigcontext
));
823 context
->SegGs
= LOWORD(GS_sig(sigcontext
));
824 context
->SegSs
= LOWORD(SS_sig(sigcontext
));
825 context
->Dr0
= x86_thread_data()->dr0
;
826 context
->Dr1
= x86_thread_data()->dr1
;
827 context
->Dr2
= x86_thread_data()->dr2
;
828 context
->Dr3
= x86_thread_data()->dr3
;
829 context
->Dr6
= x86_thread_data()->dr6
;
830 context
->Dr7
= x86_thread_data()->dr7
;
834 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
835 context
->FloatSave
= *fpu
;
841 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
842 memcpy( context
->ExtendedRegisters
, fpux
, sizeof(*fpux
) );
843 if (!fpu
) fpux_to_fpu( &context
->FloatSave
, fpux
);
844 if ((cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_AVX
) && (xs
= XState_sig(fpux
)))
846 context_init_xstate( context
, xs
);
847 xcontext
->host_compaction_mask
= xs
->CompactionMask
;
850 if (!fpu
&& !fpux
) save_fpu( context
);
854 /***********************************************************************
857 * Restore the signal info from the context.
859 static inline void restore_context( const struct xcontext
*xcontext
, ucontext_t
*sigcontext
)
861 FLOATING_SAVE_AREA
*fpu
= FPU_sig(sigcontext
);
862 XSAVE_FORMAT
*fpux
= FPUX_sig(sigcontext
);
863 const CONTEXT
*context
= &xcontext
->c
;
865 x86_thread_data()->dr0
= context
->Dr0
;
866 x86_thread_data()->dr1
= context
->Dr1
;
867 x86_thread_data()->dr2
= context
->Dr2
;
868 x86_thread_data()->dr3
= context
->Dr3
;
869 x86_thread_data()->dr6
= context
->Dr6
;
870 x86_thread_data()->dr7
= context
->Dr7
;
871 EAX_sig(sigcontext
) = context
->Eax
;
872 EBX_sig(sigcontext
) = context
->Ebx
;
873 ECX_sig(sigcontext
) = context
->Ecx
;
874 EDX_sig(sigcontext
) = context
->Edx
;
875 ESI_sig(sigcontext
) = context
->Esi
;
876 EDI_sig(sigcontext
) = context
->Edi
;
877 EBP_sig(sigcontext
) = context
->Ebp
;
878 EFL_sig(sigcontext
) = context
->EFlags
;
879 EIP_sig(sigcontext
) = context
->Eip
;
880 ESP_sig(sigcontext
) = context
->Esp
;
881 CS_sig(sigcontext
) = context
->SegCs
;
882 DS_sig(sigcontext
) = context
->SegDs
;
883 ES_sig(sigcontext
) = context
->SegEs
;
884 FS_sig(sigcontext
) = context
->SegFs
;
885 GS_sig(sigcontext
) = context
->SegGs
;
886 SS_sig(sigcontext
) = context
->SegSs
;
888 if (fpu
) *fpu
= context
->FloatSave
;
891 XSTATE
*src_xs
, *dst_xs
;
893 memcpy( fpux
, context
->ExtendedRegisters
, sizeof(*fpux
) );
895 if ((dst_xs
= XState_sig(fpux
)) && (src_xs
= xstate_from_context( context
)))
897 memcpy( &dst_xs
->YmmContext
, &src_xs
->YmmContext
, sizeof(dst_xs
->YmmContext
) );
898 dst_xs
->Mask
|= src_xs
->Mask
;
899 dst_xs
->CompactionMask
= xcontext
->host_compaction_mask
;
902 if (!fpu
&& !fpux
) restore_fpu( context
);
906 /***********************************************************************
907 * signal_restore_full_cpu_context
909 * Restore full context from syscall frame
911 void signal_restore_full_cpu_context(void)
913 x86_thread_data()->syscall_frame
->restore_flags
|= CONTEXT_INTEGER
;
917 /***********************************************************************
918 * NtSetContextThread (NTDLL.@)
919 * ZwSetContextThread (NTDLL.@)
921 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
923 NTSTATUS ret
= STATUS_SUCCESS
;
924 struct syscall_frame
*frame
= x86_thread_data()->syscall_frame
;
925 DWORD flags
= context
->ContextFlags
& ~CONTEXT_i386
;
926 BOOL self
= (handle
== GetCurrentThread());
928 if ((flags
& CONTEXT_XSTATE
) && (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_AVX
))
930 CONTEXT_EX
*context_ex
= (CONTEXT_EX
*)(context
+ 1);
931 XSTATE
*xs
= (XSTATE
*)((char *)context_ex
+ context_ex
->XState
.Offset
);
933 if (context_ex
->XState
.Length
< offsetof(XSTATE
, YmmContext
) ||
934 context_ex
->XState
.Length
> sizeof(XSTATE
))
935 return STATUS_INVALID_PARAMETER
;
936 if ((xs
->Mask
& XSTATE_MASK_GSSE
) && (context_ex
->XState
.Length
< sizeof(XSTATE
)))
937 return STATUS_BUFFER_OVERFLOW
;
939 else flags
&= ~CONTEXT_XSTATE
;
941 /* debug registers require a server call */
942 if (self
&& (flags
& CONTEXT_DEBUG_REGISTERS
))
943 self
= (x86_thread_data()->dr0
== context
->Dr0
&&
944 x86_thread_data()->dr1
== context
->Dr1
&&
945 x86_thread_data()->dr2
== context
->Dr2
&&
946 x86_thread_data()->dr3
== context
->Dr3
&&
947 x86_thread_data()->dr6
== context
->Dr6
&&
948 x86_thread_data()->dr7
== context
->Dr7
);
952 ret
= set_thread_context( handle
, context
, &self
, IMAGE_FILE_MACHINE_I386
);
953 if (ret
|| !self
) return ret
;
954 if (flags
& CONTEXT_DEBUG_REGISTERS
)
956 x86_thread_data()->dr0
= context
->Dr0
;
957 x86_thread_data()->dr1
= context
->Dr1
;
958 x86_thread_data()->dr2
= context
->Dr2
;
959 x86_thread_data()->dr3
= context
->Dr3
;
960 x86_thread_data()->dr6
= context
->Dr6
;
961 x86_thread_data()->dr7
= context
->Dr7
;
965 if (flags
& CONTEXT_INTEGER
)
967 frame
->eax
= context
->Eax
;
968 frame
->ebx
= context
->Ebx
;
969 frame
->ecx
= context
->Ecx
;
970 frame
->edx
= context
->Edx
;
971 frame
->esi
= context
->Esi
;
972 frame
->edi
= context
->Edi
;
974 if (flags
& CONTEXT_CONTROL
)
976 frame
->esp
= context
->Esp
;
977 frame
->ebp
= context
->Ebp
;
978 frame
->eip
= context
->Eip
;
979 frame
->eflags
= context
->EFlags
;
980 frame
->cs
= context
->SegCs
;
981 frame
->ss
= context
->SegSs
;
983 if (flags
& CONTEXT_SEGMENTS
)
985 frame
->ds
= context
->SegDs
;
986 frame
->es
= context
->SegEs
;
987 frame
->fs
= context
->SegFs
;
988 frame
->gs
= context
->SegGs
;
990 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
992 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
993 memcpy( &xsave
->u
.xsave
, context
->ExtendedRegisters
, sizeof(xsave
->u
.xsave
) );
994 /* reset the current interrupt status */
995 xsave
->u
.xsave
.StatusWord
&= xsave
->u
.xsave
.ControlWord
| 0xff80;
996 xsave
->xstate
.mask
|= XSTATE_MASK_LEGACY
;
998 else if (flags
& CONTEXT_FLOATING_POINT
)
1000 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1001 if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_FXSR
)
1003 fpu_to_fpux( &xsave
->u
.xsave
, &context
->FloatSave
);
1007 xsave
->u
.fsave
= context
->FloatSave
;
1009 xsave
->xstate
.mask
|= XSTATE_MASK_LEGACY_FLOATING_POINT
;
1011 if (flags
& CONTEXT_XSTATE
)
1013 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1014 CONTEXT_EX
*context_ex
= (CONTEXT_EX
*)(context
+ 1);
1015 XSTATE
*xs
= (XSTATE
*)((char *)context_ex
+ context_ex
->XState
.Offset
);
1017 if (xs
->Mask
& XSTATE_MASK_GSSE
)
1019 xsave
->xstate
.mask
|= XSTATE_MASK_GSSE
;
1020 memcpy( &xsave
->xstate
.ymm_high
, &xs
->YmmContext
, sizeof(xsave
->xstate
.ymm_high
) );
1022 else xsave
->xstate
.mask
&= ~XSTATE_MASK_GSSE
;
1025 frame
->restore_flags
|= flags
& ~CONTEXT_INTEGER
;
1026 return STATUS_SUCCESS
;
1030 /***********************************************************************
1031 * NtGetContextThread (NTDLL.@)
1032 * ZwGetContextThread (NTDLL.@)
1034 * Note: we use a small assembly wrapper to save the necessary registers
1035 * in case we are fetching the context of the current thread.
1037 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
1039 struct syscall_frame
*frame
= x86_thread_data()->syscall_frame
;
1040 DWORD needed_flags
= context
->ContextFlags
& ~CONTEXT_i386
;
1041 BOOL self
= (handle
== GetCurrentThread());
1044 /* debug registers require a server call */
1045 if (needed_flags
& CONTEXT_DEBUG_REGISTERS
) self
= FALSE
;
1049 if ((ret
= get_thread_context( handle
, context
, &self
, IMAGE_FILE_MACHINE_I386
))) return ret
;
1050 needed_flags
&= ~context
->ContextFlags
;
1055 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1057 if (needed_flags
& CONTEXT_INTEGER
)
1059 context
->Eax
= frame
->eax
;
1060 context
->Ebx
= frame
->ebx
;
1061 context
->Ecx
= frame
->ecx
;
1062 context
->Edx
= frame
->edx
;
1063 context
->Esi
= frame
->esi
;
1064 context
->Edi
= frame
->edi
;
1065 context
->ContextFlags
|= CONTEXT_INTEGER
;
1067 if (needed_flags
& CONTEXT_CONTROL
)
1069 context
->Esp
= frame
->esp
;
1070 context
->Ebp
= frame
->ebp
;
1071 context
->Eip
= frame
->eip
;
1072 context
->EFlags
= frame
->eflags
;
1073 context
->SegCs
= frame
->cs
;
1074 context
->SegSs
= frame
->ss
;
1075 context
->ContextFlags
|= CONTEXT_CONTROL
;
1077 if (needed_flags
& CONTEXT_SEGMENTS
)
1079 context
->SegDs
= frame
->ds
;
1080 context
->SegEs
= frame
->es
;
1081 context
->SegFs
= frame
->fs
;
1082 context
->SegGs
= frame
->gs
;
1083 context
->ContextFlags
|= CONTEXT_SEGMENTS
;
1085 if (needed_flags
& CONTEXT_FLOATING_POINT
)
1087 if (!(cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_FXSR
))
1089 context
->FloatSave
= xsave
->u
.fsave
;
1091 else if (!xstate_compaction_enabled
||
1092 (xsave
->xstate
.mask
& XSTATE_MASK_LEGACY_FLOATING_POINT
))
1094 fpux_to_fpu( &context
->FloatSave
, &xsave
->u
.xsave
);
1098 memset( &context
->FloatSave
, 0, sizeof(context
->FloatSave
) );
1099 context
->FloatSave
.ControlWord
= 0x37f;
1101 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1103 if (needed_flags
& CONTEXT_EXTENDED_REGISTERS
)
1105 XSAVE_FORMAT
*xs
= (XSAVE_FORMAT
*)context
->ExtendedRegisters
;
1107 if (!xstate_compaction_enabled
||
1108 (xsave
->xstate
.mask
& XSTATE_MASK_LEGACY_FLOATING_POINT
))
1110 memcpy( xs
, &xsave
->u
.xsave
, FIELD_OFFSET( XSAVE_FORMAT
, MxCsr
));
1111 memcpy( xs
->FloatRegisters
, xsave
->u
.xsave
.FloatRegisters
,
1112 sizeof( xs
->FloatRegisters
));
1116 memset( xs
, 0, FIELD_OFFSET( XSAVE_FORMAT
, MxCsr
));
1117 memset( xs
->FloatRegisters
, 0, sizeof( xs
->FloatRegisters
));
1118 xs
->ControlWord
= 0x37f;
1121 if (!xstate_compaction_enabled
|| (xsave
->xstate
.mask
& XSTATE_MASK_LEGACY_SSE
))
1123 memcpy( xs
->XmmRegisters
, xsave
->u
.xsave
.XmmRegisters
, sizeof( xs
->XmmRegisters
));
1124 xs
->MxCsr
= xsave
->u
.xsave
.MxCsr
;
1125 xs
->MxCsr_Mask
= xsave
->u
.xsave
.MxCsr_Mask
;
1129 memset( xs
->XmmRegisters
, 0, sizeof( xs
->XmmRegisters
));
1131 xs
->MxCsr_Mask
= 0x2ffff;
1134 context
->ContextFlags
|= CONTEXT_EXTENDED_REGISTERS
;
1136 /* update the cached version of the debug registers */
1137 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1139 x86_thread_data()->dr0
= context
->Dr0
;
1140 x86_thread_data()->dr1
= context
->Dr1
;
1141 x86_thread_data()->dr2
= context
->Dr2
;
1142 x86_thread_data()->dr3
= context
->Dr3
;
1143 x86_thread_data()->dr6
= context
->Dr6
;
1144 x86_thread_data()->dr7
= context
->Dr7
;
1146 if ((cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_AVX
) && (xstate
= xstate_from_context( context
)))
1148 struct syscall_xsave
*xsave
= get_syscall_xsave( frame
);
1149 CONTEXT_EX
*context_ex
= (CONTEXT_EX
*)(context
+ 1);
1152 if (context_ex
->XState
.Length
< offsetof(XSTATE
, YmmContext
)
1153 || context_ex
->XState
.Length
> sizeof(XSTATE
))
1154 return STATUS_INVALID_PARAMETER
;
1156 mask
= (xstate_compaction_enabled
? xstate
->CompactionMask
: xstate
->Mask
) & XSTATE_MASK_GSSE
;
1157 xstate
->Mask
= xsave
->xstate
.mask
& mask
;
1158 xstate
->CompactionMask
= xstate_compaction_enabled
? (0x8000000000000000 | mask
) : 0;
1159 memset( xstate
->Reserved
, 0, sizeof(xstate
->Reserved
) );
1162 if (context_ex
->XState
.Length
< sizeof(XSTATE
))
1163 return STATUS_BUFFER_OVERFLOW
;
1165 memcpy( &xstate
->YmmContext
, xsave
->xstate
.ymm_high
, sizeof(xsave
->xstate
.ymm_high
) );
1170 if (context
->ContextFlags
& (CONTEXT_INTEGER
& ~CONTEXT_i386
))
1171 TRACE( "%p: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n", handle
,
1172 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
, context
->Esi
, context
->Edi
);
1173 if (context
->ContextFlags
& (CONTEXT_CONTROL
& ~CONTEXT_i386
))
1174 TRACE( "%p: ebp=%08x esp=%08x eip=%08x cs=%04x ss=%04x flags=%08x\n", handle
,
1175 context
->Ebp
, context
->Esp
, context
->Eip
, context
->SegCs
, context
->SegSs
, context
->EFlags
);
1176 if (context
->ContextFlags
& (CONTEXT_SEGMENTS
& ~CONTEXT_i386
))
1177 TRACE( "%p: ds=%04x es=%04x fs=%04x gs=%04x\n", handle
,
1178 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
);
1179 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1180 TRACE( "%p: dr0=%08x dr1=%08x dr2=%08x dr3=%08x dr6=%08x dr7=%08x\n", handle
,
1181 context
->Dr0
, context
->Dr1
, context
->Dr2
, context
->Dr3
, context
->Dr6
, context
->Dr7
);
1183 return STATUS_SUCCESS
;
1187 /***********************************************************************
1188 * set_thread_wow64_context
1190 NTSTATUS
set_thread_wow64_context( HANDLE handle
, const void *ctx
, ULONG size
)
1192 return STATUS_INVALID_INFO_CLASS
;
1196 /***********************************************************************
1197 * get_thread_wow64_context
1199 NTSTATUS
get_thread_wow64_context( HANDLE handle
, void *ctx
, ULONG size
)
1201 return STATUS_INVALID_INFO_CLASS
;
1205 /***********************************************************************
1206 * is_privileged_instr
1208 * Check if the fault location is a privileged instruction.
1209 * Based on the instruction emulation code in dlls/kernel/instr.c.
1211 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
1214 unsigned int i
, len
, prefix_count
= 0;
1216 if (!ldt_is_system( context
->SegCs
)) return 0;
1217 len
= virtual_uninterrupted_read_memory( (BYTE
*)context
->Eip
, instr
, sizeof(instr
) );
1219 for (i
= 0; i
< len
; i
++) switch (instr
[i
])
1221 /* instruction prefixes */
1222 case 0x2e: /* %cs: */
1223 case 0x36: /* %ss: */
1224 case 0x3e: /* %ds: */
1225 case 0x26: /* %es: */
1226 case 0x64: /* %fs: */
1227 case 0x65: /* %gs: */
1228 case 0x66: /* opcode size */
1229 case 0x67: /* addr size */
1230 case 0xf0: /* lock */
1231 case 0xf2: /* repne */
1232 case 0xf3: /* repe */
1233 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
1236 case 0x0f: /* extended instruction */
1237 if (i
== len
- 1) return 0;
1238 switch(instr
[i
+ 1])
1240 case 0x20: /* mov crX, reg */
1241 case 0x21: /* mov drX, reg */
1242 case 0x22: /* mov reg, crX */
1243 case 0x23: /* mov reg drX */
1244 return EXCEPTION_PRIV_INSTRUCTION
;
1247 case 0x6c: /* insb (%dx) */
1248 case 0x6d: /* insl (%dx) */
1249 case 0x6e: /* outsb (%dx) */
1250 case 0x6f: /* outsl (%dx) */
1251 case 0xcd: /* int $xx */
1252 case 0xe4: /* inb al,XX */
1253 case 0xe5: /* in (e)ax,XX */
1254 case 0xe6: /* outb XX,al */
1255 case 0xe7: /* out XX,(e)ax */
1256 case 0xec: /* inb (%dx),%al */
1257 case 0xed: /* inl (%dx),%eax */
1258 case 0xee: /* outb %al,(%dx) */
1259 case 0xef: /* outl %eax,(%dx) */
1260 case 0xf4: /* hlt */
1261 case 0xfa: /* cli */
1262 case 0xfb: /* sti */
1263 return EXCEPTION_PRIV_INSTRUCTION
;
1271 /***********************************************************************
1274 * Check for fault caused by invalid %gs value (some copy protection schemes mess with it).
1276 static inline BOOL
check_invalid_gs( ucontext_t
*sigcontext
, CONTEXT
*context
)
1278 unsigned int prefix_count
= 0;
1279 const BYTE
*instr
= (BYTE
*)context
->Eip
;
1280 WORD system_gs
= x86_thread_data()->gs
;
1282 if (context
->SegGs
== system_gs
) return FALSE
;
1283 if (!ldt_is_system( context
->SegCs
)) return FALSE
;
1284 /* only handle faults in system libraries */
1285 if (virtual_is_valid_code_address( instr
, 1 )) return FALSE
;
1287 for (;;) switch(*instr
)
1289 /* instruction prefixes */
1290 case 0x2e: /* %cs: */
1291 case 0x36: /* %ss: */
1292 case 0x3e: /* %ds: */
1293 case 0x26: /* %es: */
1294 case 0x64: /* %fs: */
1295 case 0x66: /* opcode size */
1296 case 0x67: /* addr size */
1297 case 0xf0: /* lock */
1298 case 0xf2: /* repne */
1299 case 0xf3: /* repe */
1300 if (++prefix_count
>= 15) return FALSE
;
1303 case 0x65: /* %gs: */
1304 TRACE( "%04x/%04x at %p, fixing up\n", context
->SegGs
, system_gs
, instr
);
1305 GS_sig(sigcontext
) = system_gs
;
1313 #include "pshpack1.h"
1318 DWORD movl
; /* movl this,4(%esp) */
1320 BYTE jmp
; /* jmp func */
1325 BYTE movl
; /* movl this,ecx */
1327 BYTE jmp
; /* jmp func */
1332 BYTE movl1
; /* movl this,edx */
1334 BYTE movl2
; /* movl func,ecx */
1336 WORD jmp
; /* jmp ecx */
1340 BYTE movl1
; /* movl this,ecx */
1342 BYTE movl2
; /* movl func,eax */
1344 WORD jmp
; /* jmp eax */
1348 DWORD inst1
; /* pop ecx
1355 #include "poppack.h"
1357 /**********************************************************************
1360 * Check if code destination is an ATL thunk, and emulate it if so.
1362 static BOOL
check_atl_thunk( ucontext_t
*sigcontext
, EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1364 const union atl_thunk
*thunk
= (const union atl_thunk
*)rec
->ExceptionInformation
[1];
1365 union atl_thunk thunk_copy
;
1368 thunk_len
= virtual_uninterrupted_read_memory( thunk
, &thunk_copy
, sizeof(*thunk
) );
1369 if (!thunk_len
) return FALSE
;
1371 if (thunk_len
>= sizeof(thunk_copy
.t1
) && thunk_copy
.t1
.movl
== 0x042444c7 &&
1372 thunk_copy
.t1
.jmp
== 0xe9)
1374 if (!virtual_uninterrupted_write_memory( (DWORD
*)context
->Esp
+ 1,
1375 &thunk_copy
.t1
.this, sizeof(DWORD
) ))
1377 EIP_sig(sigcontext
) = (DWORD_PTR
)(&thunk
->t1
.func
+ 1) + thunk_copy
.t1
.func
;
1378 TRACE( "emulating ATL thunk type 1 at %p, func=%08x arg=%08x\n",
1379 thunk
, EIP_sig(sigcontext
), thunk_copy
.t1
.this );
1383 else if (thunk_len
>= sizeof(thunk_copy
.t2
) && thunk_copy
.t2
.movl
== 0xb9 &&
1384 thunk_copy
.t2
.jmp
== 0xe9)
1386 ECX_sig(sigcontext
) = thunk_copy
.t2
.this;
1387 EIP_sig(sigcontext
) = (DWORD_PTR
)(&thunk
->t2
.func
+ 1) + thunk_copy
.t2
.func
;
1388 TRACE( "emulating ATL thunk type 2 at %p, func=%08x ecx=%08x\n",
1389 thunk
, EIP_sig(sigcontext
), ECX_sig(sigcontext
) );
1392 else if (thunk_len
>= sizeof(thunk_copy
.t3
) && thunk_copy
.t3
.movl1
== 0xba &&
1393 thunk_copy
.t3
.movl2
== 0xb9 &&
1394 thunk_copy
.t3
.jmp
== 0xe1ff)
1396 EDX_sig(sigcontext
) = thunk_copy
.t3
.this;
1397 ECX_sig(sigcontext
) = thunk_copy
.t3
.func
;
1398 EIP_sig(sigcontext
) = thunk_copy
.t3
.func
;
1399 TRACE( "emulating ATL thunk type 3 at %p, func=%08x ecx=%08x edx=%08x\n",
1400 thunk
, EIP_sig(sigcontext
), ECX_sig(sigcontext
), EDX_sig(sigcontext
) );
1403 else if (thunk_len
>= sizeof(thunk_copy
.t4
) && thunk_copy
.t4
.movl1
== 0xb9 &&
1404 thunk_copy
.t4
.movl2
== 0xb8 &&
1405 thunk_copy
.t4
.jmp
== 0xe0ff)
1407 ECX_sig(sigcontext
) = thunk_copy
.t4
.this;
1408 EAX_sig(sigcontext
) = thunk_copy
.t4
.func
;
1409 EIP_sig(sigcontext
) = thunk_copy
.t4
.func
;
1410 TRACE( "emulating ATL thunk type 4 at %p, func=%08x eax=%08x ecx=%08x\n",
1411 thunk
, EIP_sig(sigcontext
), EAX_sig(sigcontext
), ECX_sig(sigcontext
) );
1414 else if (thunk_len
>= sizeof(thunk_copy
.t5
) && thunk_copy
.t5
.inst1
== 0xff515859 &&
1415 thunk_copy
.t5
.inst2
== 0x0460)
1418 if (virtual_uninterrupted_read_memory( (DWORD
*)context
->Esp
, sp
, sizeof(sp
) ) == sizeof(sp
) &&
1419 virtual_uninterrupted_read_memory( (DWORD
*)sp
[1] + 1, &func
, sizeof(DWORD
) ) == sizeof(DWORD
) &&
1420 !virtual_uninterrupted_write_memory( (DWORD
*)context
->Esp
+ 1, &sp
[0], sizeof(sp
[0]) ))
1422 ECX_sig(sigcontext
) = sp
[0];
1423 EAX_sig(sigcontext
) = sp
[1];
1424 ESP_sig(sigcontext
) += sizeof(DWORD
);
1425 EIP_sig(sigcontext
) = func
;
1426 TRACE( "emulating ATL thunk type 5 at %p, func=%08x eax=%08x ecx=%08x esp=%08x\n",
1427 thunk
, EIP_sig(sigcontext
), EAX_sig(sigcontext
),
1428 ECX_sig(sigcontext
), ESP_sig(sigcontext
) );
1437 /***********************************************************************
1438 * setup_exception_record
1440 * Setup the exception record and context on the thread stack.
1442 static void *setup_exception_record( ucontext_t
*sigcontext
, EXCEPTION_RECORD
*rec
, struct xcontext
*xcontext
)
1444 void *stack
= init_handler( sigcontext
);
1446 rec
->ExceptionAddress
= (void *)EIP_sig( sigcontext
);
1447 save_context( xcontext
, sigcontext
);
1451 /***********************************************************************
1452 * setup_raise_exception
1454 * Change context to setup a call to a raise exception function.
1456 static void setup_raise_exception( ucontext_t
*sigcontext
, void *stack_ptr
,
1457 EXCEPTION_RECORD
*rec
, struct xcontext
*xcontext
)
1459 CONTEXT
*context
= &xcontext
->c
;
1465 EXCEPTION_RECORD
*rec_ptr
; /* first arg for KiUserExceptionDispatcher */
1466 CONTEXT
*context_ptr
; /* second arg for KiUserExceptionDispatcher */
1468 CONTEXT_EX context_ex
;
1469 EXCEPTION_RECORD rec
;
1475 C_ASSERT( (offsetof(struct stack_layout
, xstate
) == sizeof(struct stack_layout
)) );
1477 NTSTATUS status
= send_debug_event( rec
, context
, TRUE
);
1479 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
1481 restore_context( xcontext
, sigcontext
);
1485 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
1486 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Eip
--;
1488 stack_size
= sizeof(*stack
);
1489 if ((src_xs
= xstate_from_context( context
)))
1491 stack_size
+= (ULONG_PTR
)stack_ptr
- (((ULONG_PTR
)stack_ptr
1492 - sizeof(XSTATE
)) & ~(ULONG_PTR
)63);
1495 stack
= virtual_setup_exception( stack_ptr
, stack_size
, rec
);
1497 stack
->context
= *context
;
1501 XSTATE
*dst_xs
= (XSTATE
*)stack
->xstate
;
1503 assert(!((ULONG_PTR
)dst_xs
& 63));
1504 context_init_xstate( &stack
->context
, stack
->xstate
);
1505 memset( dst_xs
, 0, offsetof(XSTATE
, YmmContext
) );
1506 dst_xs
->CompactionMask
= xstate_compaction_enabled
? 0x8000000000000004 : 0;
1507 if (src_xs
->Mask
& 4)
1510 memcpy( &dst_xs
->YmmContext
, &src_xs
->YmmContext
, sizeof(dst_xs
->YmmContext
) );
1514 stack
->rec_ptr
= &stack
->rec
;
1515 stack
->context_ptr
= &stack
->context
;
1516 ESP_sig(sigcontext
) = (DWORD
)stack
;
1517 EIP_sig(sigcontext
) = (DWORD
)pKiUserExceptionDispatcher
;
1518 /* clear single-step, direction, and align check flag */
1519 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1520 CS_sig(sigcontext
) = get_cs();
1521 DS_sig(sigcontext
) = get_ds();
1522 ES_sig(sigcontext
) = get_ds();
1523 FS_sig(sigcontext
) = get_fs();
1524 GS_sig(sigcontext
) = get_gs();
1525 SS_sig(sigcontext
) = get_ds();
1529 /***********************************************************************
1532 * Do the full setup to raise an exception from an exception record.
1534 static void setup_exception( ucontext_t
*sigcontext
, EXCEPTION_RECORD
*rec
)
1536 struct xcontext xcontext
;
1537 void *stack
= setup_exception_record( sigcontext
, rec
, &xcontext
);
1539 setup_raise_exception( sigcontext
, stack
, rec
, &xcontext
);
1542 /* stack layout when calling an user apc function.
1543 * FIXME: match Windows ABI. */
1544 struct apc_stack_layout
1554 struct apc_stack_layout
* WINAPI
setup_user_apc_dispatcher_stack( CONTEXT
*context
, struct apc_stack_layout
*stack
,
1555 void *arg1
, void *arg2
, void *arg3
,
1556 void *func
, NTSTATUS status
) DECLSPEC_HIDDEN
;
1557 struct apc_stack_layout
* WINAPI
setup_user_apc_dispatcher_stack( CONTEXT
*context
, struct apc_stack_layout
*stack
,
1558 void *arg1
, void *arg2
, void *arg3
,
1559 void *func
, NTSTATUS status
)
1565 c
.ContextFlags
= CONTEXT_FULL
;
1566 NtGetContextThread( GetCurrentThread(), &c
);
1570 memmove( &stack
->context
, context
, sizeof(stack
->context
) );
1571 stack
->context_ptr
= &stack
->context
;
1579 C_ASSERT( sizeof(struct apc_stack_layout
) == 0x2e0 );
1580 C_ASSERT( offsetof(struct apc_stack_layout
, context
) == 20 );
1582 /***********************************************************************
1583 * call_user_apc_dispatcher
1585 __ASM_GLOBAL_FUNC( call_user_apc_dispatcher
,
1586 "movl 4(%esp),%esi\n\t" /* context_ptr */
1587 "movl 24(%esp),%edi\n\t" /* dispatcher */
1588 "leal 4(%esp),%ebp\n\t"
1589 "test %esi,%esi\n\t"
1591 "movl 0xc4(%esi),%eax\n\t" /* context_ptr->Esp */
1593 "1:\tmovl %fs:0x1f8,%eax\n\t" /* x86_thread_data()->syscall_frame */
1594 "leal 0x3c(%eax),%eax\n\t" /* &x86_thread_data()->syscall_frame->ret_addr */
1595 "2:\tsubl $0x2e0,%eax\n\t" /* sizeof(struct apc_stack_layout) */
1596 "movl %ebp,%esp\n\t" /* pop return address */
1597 "cmpl %esp,%eax\n\t"
1598 "cmovbl %eax,%esp\n\t"
1599 "pushl 24(%ebp)\n\t" /* status */
1600 "pushl 16(%ebp)\n\t" /* func */
1601 "pushl 12(%ebp)\n\t" /* arg3 */
1602 "pushl 8(%ebp)\n\t" /* arg2 */
1603 "pushl 4(%ebp)\n\t" /* arg1 */
1606 "call " __ASM_STDCALL("setup_user_apc_dispatcher_stack",24) "\n\t"
1607 "movl $0,%fs:0x1f8\n\t" /* x86_thread_data()->syscall_frame = NULL */
1608 "movl %eax,%esp\n\t"
1609 "leal 20(%eax),%esi\n\t"
1610 "movl 0xb4(%esi),%ebp\n\t" /* context.Ebp */
1611 "pushl 0xb8(%esi)\n\t" /* context.Eip */
1615 /***********************************************************************
1616 * call_raise_user_exception_dispatcher
1618 void WINAPI
call_raise_user_exception_dispatcher( NTSTATUS (WINAPI
*dispatcher
)(void) )
1620 x86_thread_data()->syscall_frame
->eip
= (DWORD
)dispatcher
;
1624 /***********************************************************************
1625 * call_user_exception_dispatcher
1627 __ASM_GLOBAL_FUNC( call_user_exception_dispatcher
,
1628 "movl 4(%esp),%edx\n\t" /* rec */
1629 "movl 8(%esp),%ecx\n\t" /* context */
1630 "cmpl $0x80000003,(%edx)\n\t" /* rec->ExceptionCode */
1632 "decl 0xb8(%ecx)\n" /* context->Eip */
1633 "1:\tmovl %fs:0x1f8,%eax\n\t" /* x86_thread_data()->syscall_frame */
1634 "movl 0x20(%eax),%ebx\n\t" /* frame->ebx */
1635 "movl 0x2c(%eax),%edi\n\t" /* frame->edi */
1636 "movl 0x30(%eax),%esi\n\t" /* frame->esi */
1637 "movl 0x34(%eax),%ebp\n\t" /* frame->ebp */
1638 "movl %edx,0x34(%eax)\n\t"
1639 "movl %ecx,0x38(%eax)\n\t"
1640 "movl 12(%esp),%edx\n\t" /* dispatcher */
1641 "movl $0,%fs:0x1f8\n\t"
1642 "leal 0x34(%eax),%esp\n\t"
1645 /**********************************************************************
1648 * Get the FPU exception code from the FPU status.
1650 static inline DWORD
get_fpu_code( const CONTEXT
*context
)
1652 DWORD status
= context
->FloatSave
.StatusWord
& ~(context
->FloatSave
.ControlWord
& 0x3f);
1654 if (status
& 0x01) /* IE */
1656 if (status
& 0x40) /* SF */
1657 return EXCEPTION_FLT_STACK_CHECK
;
1659 return EXCEPTION_FLT_INVALID_OPERATION
;
1661 if (status
& 0x02) return EXCEPTION_FLT_DENORMAL_OPERAND
; /* DE flag */
1662 if (status
& 0x04) return EXCEPTION_FLT_DIVIDE_BY_ZERO
; /* ZE flag */
1663 if (status
& 0x08) return EXCEPTION_FLT_OVERFLOW
; /* OE flag */
1664 if (status
& 0x10) return EXCEPTION_FLT_UNDERFLOW
; /* UE flag */
1665 if (status
& 0x20) return EXCEPTION_FLT_INEXACT_RESULT
; /* PE flag */
1666 return EXCEPTION_FLT_INVALID_OPERATION
; /* generic error */
1670 /***********************************************************************
1673 * Handle an interrupt.
1675 static BOOL
handle_interrupt( unsigned int interrupt
, ucontext_t
*sigcontext
, void *stack
,
1676 EXCEPTION_RECORD
*rec
, struct xcontext
*xcontext
)
1678 CONTEXT
*context
= &xcontext
->c
;
1685 /* On Wow64, the upper DWORD of Rax contains garbage, and the debug
1686 * service is usually not recognized when called from usermode. */
1687 switch (context
->Eax
)
1689 case 1: /* BREAKPOINT_PRINT */
1690 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
1691 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
1692 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
1693 EIP_sig(sigcontext
) += 3;
1698 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1699 rec
->ExceptionAddress
= (void *)context
->Eip
;
1700 rec
->NumberParameters
= is_wow64
? 1 : 3;
1701 rec
->ExceptionInformation
[0] = context
->Eax
;
1702 rec
->ExceptionInformation
[1] = context
->Ecx
;
1703 rec
->ExceptionInformation
[2] = context
->Edx
;
1704 setup_raise_exception( sigcontext
, stack
, rec
, xcontext
);
1712 /***********************************************************************
1713 * handle_syscall_fault
1715 * Handle a page fault happening during a system call.
1717 static BOOL
handle_syscall_fault( ucontext_t
*sigcontext
, void *stack_ptr
,
1718 EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1720 struct syscall_frame
*frame
= x86_thread_data()->syscall_frame
;
1723 if (!frame
) return FALSE
;
1725 TRACE( "code=%x flags=%x addr=%p ip=%08x tid=%04x\n",
1726 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
1727 context
->Eip
, GetCurrentThreadId() );
1728 for (i
= 0; i
< rec
->NumberParameters
; i
++)
1729 TRACE( " info[%d]=%08lx\n", i
, rec
->ExceptionInformation
[i
] );
1730 TRACE(" eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
1731 context
->Eax
, context
->Ebx
, context
->Ecx
,
1732 context
->Edx
, context
->Esi
, context
->Edi
);
1733 TRACE(" ebp=%08x esp=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
1734 context
->Ebp
, context
->Esp
, context
->SegCs
, context
->SegDs
,
1735 context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
1737 if (ntdll_get_thread_data()->jmp_buf)
1739 /* stack frame for calling __wine_longjmp */
1743 __wine_jmp_buf
*jmp
;
1747 TRACE( "returning to handler\n" );
1748 stack
= virtual_setup_exception( stack_ptr
, sizeof(*stack
), rec
);
1750 stack
->jmp
= ntdll_get_thread_data()->jmp_buf;
1751 stack
->retaddr
= (void *)0xdeadbabe;
1752 ESP_sig(sigcontext
) = (DWORD
)stack
;
1753 EIP_sig(sigcontext
) = (DWORD
)__wine_longjmp
;
1754 ntdll_get_thread_data()->jmp_buf = NULL
;
1758 TRACE( "returning to user mode ip=%08x ret=%08x\n", frame
->eip
, rec
->ExceptionCode
);
1759 EAX_sig(sigcontext
) = rec
->ExceptionCode
;
1760 EBX_sig(sigcontext
) = frame
->ebx
;
1761 ESI_sig(sigcontext
) = frame
->esi
;
1762 EDI_sig(sigcontext
) = frame
->edi
;
1763 EBP_sig(sigcontext
) = frame
->ebp
;
1764 ESP_sig(sigcontext
) = frame
->esp
;
1765 EIP_sig(sigcontext
) = frame
->eip
;
1766 x86_thread_data()->syscall_frame
= NULL
;
1772 /**********************************************************************
1775 * Handler for SIGSEGV and related errors.
1777 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1779 EXCEPTION_RECORD rec
= { 0 };
1780 struct xcontext xcontext
;
1781 ucontext_t
*ucontext
= sigcontext
;
1782 void *stack
= setup_exception_record( sigcontext
, &rec
, &xcontext
);
1784 switch (TRAP_sig(ucontext
))
1786 case TRAP_x86_OFLOW
: /* Overflow exception */
1787 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1789 case TRAP_x86_BOUND
: /* Bound range exception */
1790 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1792 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1793 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1795 case TRAP_x86_STKFLT
: /* Stack fault */
1796 rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1798 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1799 case TRAP_x86_PROTFLT
: /* General protection fault */
1801 WORD err
= ERROR_sig(ucontext
);
1802 if (!err
&& (rec
.ExceptionCode
= is_privileged_instr( &xcontext
.c
))) break;
1803 if ((err
& 7) == 2 && handle_interrupt( err
>> 3, ucontext
, stack
, &rec
, &xcontext
)) return;
1804 rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1805 rec
.NumberParameters
= 2;
1806 rec
.ExceptionInformation
[0] = 0;
1807 /* if error contains a LDT selector, use that as fault address */
1808 if ((err
& 7) == 4 && !ldt_is_system( err
| 7 )) rec
.ExceptionInformation
[1] = err
& ~7;
1811 rec
.ExceptionInformation
[1] = 0xffffffff;
1812 if (check_invalid_gs( ucontext
, &xcontext
.c
)) return;
1816 case TRAP_x86_PAGEFLT
: /* Page fault */
1817 rec
.NumberParameters
= 2;
1818 rec
.ExceptionInformation
[0] = (ERROR_sig(ucontext
) >> 1) & 0x09;
1819 rec
.ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
1820 rec
.ExceptionCode
= virtual_handle_fault( siginfo
->si_addr
, rec
.ExceptionInformation
[0], stack
);
1821 if (!rec
.ExceptionCode
) return;
1822 if (rec
.ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
&&
1823 rec
.ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
)
1826 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
,
1827 &flags
, sizeof(flags
), NULL
);
1828 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
) &&
1829 check_atl_thunk( ucontext
, &rec
, &xcontext
.c
))
1832 /* send EXCEPTION_EXECUTE_FAULT only if data execution prevention is enabled */
1833 if (!(flags
& MEM_EXECUTE_OPTION_DISABLE
)) rec
.ExceptionInformation
[0] = EXCEPTION_READ_FAULT
;
1836 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
1837 /* FIXME: pass through exception handler first? */
1838 if (xcontext
.c
.EFlags
& 0x00040000)
1840 EFL_sig(ucontext
) &= ~0x00040000; /* disable AC flag */
1843 rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1846 WINE_ERR( "Got unexpected trap %d\n", TRAP_sig(ucontext
) );
1848 case TRAP_x86_NMI
: /* NMI interrupt */
1849 case TRAP_x86_DNA
: /* Device not available exception */
1850 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
1851 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
1852 case TRAP_x86_MCHK
: /* Machine check exception */
1853 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
1854 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1857 if (handle_syscall_fault( ucontext
, stack
, &rec
, &xcontext
.c
)) return;
1858 setup_raise_exception( ucontext
, stack
, &rec
, &xcontext
);
1862 /**********************************************************************
1865 * Handler for SIGTRAP.
1867 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1869 EXCEPTION_RECORD rec
= { 0 };
1870 struct xcontext xcontext
;
1871 ucontext_t
*ucontext
= sigcontext
;
1872 void *stack
= setup_exception_record( sigcontext
, &rec
, &xcontext
);
1874 switch (TRAP_sig(ucontext
))
1876 case TRAP_x86_TRCTRAP
: /* Single-step exception */
1877 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1878 /* when single stepping can't tell whether this is a hw bp or a
1879 * single step interrupt. try to avoid as much overhead as possible
1880 * and only do a server call if there is any hw bp enabled. */
1881 if (!(xcontext
.c
.EFlags
& 0x100) || (xcontext
.c
.Dr7
& 0xff))
1883 /* (possible) hardware breakpoint, fetch the debug registers */
1884 DWORD saved_flags
= xcontext
.c
.ContextFlags
;
1885 xcontext
.c
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1886 NtGetContextThread( GetCurrentThread(), &xcontext
.c
);
1887 xcontext
.c
.ContextFlags
|= saved_flags
; /* restore flags */
1889 xcontext
.c
.EFlags
&= ~0x100; /* clear single-step flag */
1891 case TRAP_x86_BPTFLT
: /* Breakpoint exception */
1892 rec
.ExceptionAddress
= (char *)rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
1895 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
1896 rec
.NumberParameters
= is_wow64
? 1 : 3;
1897 rec
.ExceptionInformation
[0] = 0;
1898 rec
.ExceptionInformation
[1] = 0; /* FIXME */
1899 rec
.ExceptionInformation
[2] = 0; /* FIXME */
1902 setup_raise_exception( sigcontext
, stack
, &rec
, &xcontext
);
1906 /**********************************************************************
1909 * Handler for SIGFPE.
1911 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1913 EXCEPTION_RECORD rec
= { 0 };
1914 struct xcontext xcontext
;
1915 ucontext_t
*ucontext
= sigcontext
;
1916 void *stack
= setup_exception_record( sigcontext
, &rec
, &xcontext
);
1918 switch (TRAP_sig(ucontext
))
1920 case TRAP_x86_DIVIDE
: /* Division by zero exception */
1921 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1923 case TRAP_x86_FPOPFLT
: /* Coprocessor segment overrun */
1924 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1926 case TRAP_x86_ARITHTRAP
: /* Floating point exception */
1927 rec
.ExceptionCode
= get_fpu_code( &xcontext
.c
);
1928 rec
.ExceptionAddress
= (void *)xcontext
.c
.FloatSave
.ErrorOffset
;
1930 case TRAP_x86_CACHEFLT
: /* SIMD exception */
1932 * Behaviour only tested for divide-by-zero exceptions
1933 * Check for other SIMD exceptions as well */
1934 if(siginfo
->si_code
!= FPE_FLTDIV
&& siginfo
->si_code
!= FPE_FLTINV
)
1935 FIXME("untested SIMD exception: %#x. Might not work correctly\n",
1938 rec
.ExceptionCode
= STATUS_FLOAT_MULTIPLE_TRAPS
;
1939 rec
.NumberParameters
= 1;
1940 /* no idea what meaning is actually behind this but that's what native does */
1941 rec
.ExceptionInformation
[0] = 0;
1944 WINE_ERR( "Got unexpected trap %d\n", TRAP_sig(ucontext
) );
1945 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1948 setup_raise_exception( sigcontext
, stack
, &rec
, &xcontext
);
1952 /**********************************************************************
1955 * Handler for SIGINT.
1957 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1961 init_handler( sigcontext
);
1963 if (!p__wine_ctrl_routine
) return;
1964 if (!NtCreateThreadEx( &handle
, THREAD_ALL_ACCESS
, NULL
, NtCurrentProcess(),
1965 p__wine_ctrl_routine
, 0 /* CTRL_C_EVENT */, 0, 0, 0, 0, NULL
))
1969 /**********************************************************************
1972 * Handler for SIGABRT.
1974 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1976 EXCEPTION_RECORD rec
= { EXCEPTION_WINE_ASSERTION
, EH_NONCONTINUABLE
};
1978 setup_exception( sigcontext
, &rec
);
1982 /**********************************************************************
1985 * Handler for SIGQUIT.
1987 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1989 init_handler( sigcontext
);
1994 /**********************************************************************
1997 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1999 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2001 struct xcontext xcontext
;
2003 init_handler( sigcontext
);
2004 if (x86_thread_data()->syscall_frame
)
2006 DECLSPEC_ALIGN(64) XSTATE xs
;
2007 xcontext
.c
.ContextFlags
= CONTEXT_FULL
;
2008 context_init_xstate( &xcontext
.c
, &xs
);
2010 NtGetContextThread( GetCurrentThread(), &xcontext
.c
);
2011 wait_suspend( &xcontext
.c
);
2012 NtSetContextThread( GetCurrentThread(), &xcontext
.c
);
2016 save_context( &xcontext
, sigcontext
);
2017 wait_suspend( &xcontext
.c
);
2018 restore_context( &xcontext
, sigcontext
);
2023 /***********************************************************************
2027 #define LDT_SIZE 8192
2029 #define LDT_FLAGS_DATA 0x13 /* Data segment */
2030 #define LDT_FLAGS_CODE 0x1b /* Code segment */
2031 #define LDT_FLAGS_32BIT 0x40 /* Segment is 32-bit (code or stack) */
2032 #define LDT_FLAGS_ALLOCATED 0x80 /* Segment is allocated */
2036 void *base
[LDT_SIZE
];
2037 unsigned int limit
[LDT_SIZE
];
2038 unsigned char flags
[LDT_SIZE
];
2041 static WORD gdt_fs_sel
;
2042 static pthread_mutex_t ldt_mutex
= PTHREAD_MUTEX_INITIALIZER
;
2043 static const LDT_ENTRY null_entry
;
2045 static inline void *ldt_get_base( LDT_ENTRY ent
)
2047 return (void *)(ent
.BaseLow
|
2048 (ULONG_PTR
)ent
.HighWord
.Bits
.BaseMid
<< 16 |
2049 (ULONG_PTR
)ent
.HighWord
.Bits
.BaseHi
<< 24);
2052 static inline unsigned int ldt_get_limit( LDT_ENTRY ent
)
2054 unsigned int limit
= ent
.LimitLow
| (ent
.HighWord
.Bits
.LimitHi
<< 16);
2055 if (ent
.HighWord
.Bits
.Granularity
) limit
= (limit
<< 12) | 0xfff;
2059 static LDT_ENTRY
ldt_make_entry( void *base
, unsigned int limit
, unsigned char flags
)
2063 entry
.BaseLow
= (WORD
)(ULONG_PTR
)base
;
2064 entry
.HighWord
.Bits
.BaseMid
= (BYTE
)((ULONG_PTR
)base
>> 16);
2065 entry
.HighWord
.Bits
.BaseHi
= (BYTE
)((ULONG_PTR
)base
>> 24);
2066 if ((entry
.HighWord
.Bits
.Granularity
= (limit
>= 0x100000))) limit
>>= 12;
2067 entry
.LimitLow
= (WORD
)limit
;
2068 entry
.HighWord
.Bits
.LimitHi
= limit
>> 16;
2069 entry
.HighWord
.Bits
.Dpl
= 3;
2070 entry
.HighWord
.Bits
.Pres
= 1;
2071 entry
.HighWord
.Bits
.Type
= flags
;
2072 entry
.HighWord
.Bits
.Sys
= 0;
2073 entry
.HighWord
.Bits
.Reserved_0
= 0;
2074 entry
.HighWord
.Bits
.Default_Big
= (flags
& LDT_FLAGS_32BIT
) != 0;
2078 static void ldt_set_entry( WORD sel
, LDT_ENTRY entry
)
2080 int index
= sel
>> 3;
2083 struct modify_ldt_s ldt_info
= { index
};
2085 ldt_info
.base_addr
= ldt_get_base( entry
);
2086 ldt_info
.limit
= entry
.LimitLow
| (entry
.HighWord
.Bits
.LimitHi
<< 16);
2087 ldt_info
.seg_32bit
= entry
.HighWord
.Bits
.Default_Big
;
2088 ldt_info
.contents
= (entry
.HighWord
.Bits
.Type
>> 2) & 3;
2089 ldt_info
.read_exec_only
= !(entry
.HighWord
.Bits
.Type
& 2);
2090 ldt_info
.limit_in_pages
= entry
.HighWord
.Bits
.Granularity
;
2091 ldt_info
.seg_not_present
= !entry
.HighWord
.Bits
.Pres
;
2092 ldt_info
.usable
= entry
.HighWord
.Bits
.Sys
;
2093 if (modify_ldt( 0x11, &ldt_info
, sizeof(ldt_info
) ) < 0) perror( "modify_ldt" );
2094 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
2095 /* The kernel will only let us set LDTs with user priority level */
2096 if (entry
.HighWord
.Bits
.Pres
&& entry
.HighWord
.Bits
.Dpl
!= 3) entry
.HighWord
.Bits
.Dpl
= 3;
2097 if (i386_set_ldt(index
, (union descriptor
*)&entry
, 1) < 0)
2099 perror("i386_set_ldt");
2100 fprintf( stderr
, "Did you reconfigure the kernel with \"options USER_LDT\"?\n" );
2103 #elif defined(__svr4__) || defined(_SCO_DS)
2107 ldt_mod
.bo
= (unsigned long)ldt_get_base( entry
);
2108 ldt_mod
.ls
= entry
.LimitLow
| (entry
.HighWord
.Bits
.LimitHi
<< 16);
2109 ldt_mod
.acc1
= entry
.HighWord
.Bytes
.Flags1
;
2110 ldt_mod
.acc2
= entry
.HighWord
.Bytes
.Flags2
>> 4;
2111 if (sysi86(SI86DSCR
, &ldt_mod
) == -1) perror("sysi86");
2112 #elif defined(__APPLE__)
2113 if (i386_set_ldt(index
, (union ldt_entry
*)&entry
, 1) < 0) perror("i386_set_ldt");
2114 #elif defined(__GNU__)
2115 if (i386_set_ldt(mach_thread_self(), sel
, (descriptor_list_t
)&entry
, 1) != KERN_SUCCESS
)
2116 perror("i386_set_ldt");
2118 fprintf( stderr
, "No LDT support on this platform\n" );
2122 __wine_ldt_copy
.base
[index
] = ldt_get_base( entry
);
2123 __wine_ldt_copy
.limit
[index
] = ldt_get_limit( entry
);
2124 __wine_ldt_copy
.flags
[index
] = (entry
.HighWord
.Bits
.Type
|
2125 (entry
.HighWord
.Bits
.Default_Big
? LDT_FLAGS_32BIT
: 0) |
2126 LDT_FLAGS_ALLOCATED
);
2129 static void ldt_set_fs( WORD sel
, TEB
*teb
)
2131 if (sel
== gdt_fs_sel
)
2134 struct modify_ldt_s ldt_info
= { sel
>> 3 };
2136 ldt_info
.base_addr
= teb
;
2137 ldt_info
.limit
= page_size
- 1;
2138 ldt_info
.seg_32bit
= 1;
2139 if (set_thread_area( &ldt_info
) < 0) perror( "set_thread_area" );
2140 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
2141 i386_set_fsbase( teb
);
2148 /**********************************************************************
2149 * get_thread_ldt_entry
2151 NTSTATUS
get_thread_ldt_entry( HANDLE handle
, void *data
, ULONG len
, ULONG
*ret_len
)
2153 THREAD_DESCRIPTOR_INFORMATION
*info
= data
;
2154 NTSTATUS status
= STATUS_SUCCESS
;
2156 if (len
< sizeof(*info
)) return STATUS_INFO_LENGTH_MISMATCH
;
2157 if (info
->Selector
>> 16) return STATUS_UNSUCCESSFUL
;
2159 if (is_gdt_sel( info
->Selector
))
2161 if (!(info
->Selector
& ~3))
2162 info
->Entry
= null_entry
;
2163 else if ((info
->Selector
| 3) == get_cs())
2164 info
->Entry
= ldt_make_entry( 0, ~0u, LDT_FLAGS_CODE
| LDT_FLAGS_32BIT
);
2165 else if ((info
->Selector
| 3) == get_ds())
2166 info
->Entry
= ldt_make_entry( 0, ~0u, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2167 else if ((info
->Selector
| 3) == get_fs())
2168 info
->Entry
= ldt_make_entry( NtCurrentTeb(), 0xfff, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2170 return STATUS_UNSUCCESSFUL
;
2174 SERVER_START_REQ( get_selector_entry
)
2176 req
->handle
= wine_server_obj_handle( handle
);
2177 req
->entry
= info
->Selector
>> 3;
2178 status
= wine_server_call( req
);
2182 info
->Entry
= ldt_make_entry( (void *)reply
->base
, reply
->limit
, reply
->flags
);
2184 status
= STATUS_UNSUCCESSFUL
;
2189 if (status
== STATUS_SUCCESS
&& ret_len
)
2190 /* yes, that's a bit strange, but it's the way it is */
2191 *ret_len
= sizeof(info
->Entry
);
2197 /******************************************************************************
2198 * NtSetLdtEntries (NTDLL.@)
2199 * ZwSetLdtEntries (NTDLL.@)
2201 NTSTATUS WINAPI
NtSetLdtEntries( ULONG sel1
, LDT_ENTRY entry1
, ULONG sel2
, LDT_ENTRY entry2
)
2205 if (sel1
>> 16 || sel2
>> 16) return STATUS_INVALID_LDT_DESCRIPTOR
;
2206 if (sel1
&& (sel1
>> 3) < first_ldt_entry
) return STATUS_INVALID_LDT_DESCRIPTOR
;
2207 if (sel2
&& (sel2
>> 3) < first_ldt_entry
) return STATUS_INVALID_LDT_DESCRIPTOR
;
2209 server_enter_uninterrupted_section( &ldt_mutex
, &sigset
);
2210 if (sel1
) ldt_set_entry( sel1
, entry1
);
2211 if (sel2
) ldt_set_entry( sel2
, entry2
);
2212 server_leave_uninterrupted_section( &ldt_mutex
, &sigset
);
2213 return STATUS_SUCCESS
;
2217 /**********************************************************************
2218 * signal_init_threading
2220 void signal_init_threading(void)
2223 /* the preloader may have allocated it already */
2224 gdt_fs_sel
= get_fs();
2225 if (!gdt_fs_sel
|| !is_gdt_sel( gdt_fs_sel
))
2227 struct modify_ldt_s ldt_info
= { -1 };
2229 ldt_info
.seg_32bit
= 1;
2230 ldt_info
.usable
= 1;
2231 if (set_thread_area( &ldt_info
) >= 0) gdt_fs_sel
= (ldt_info
.entry_number
<< 3) | 3;
2232 else gdt_fs_sel
= 0;
2234 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
2235 gdt_fs_sel
= GSEL( GUFS_SEL
, SEL_UPL
);
2240 /**********************************************************************
2241 * signal_alloc_thread
2243 NTSTATUS
signal_alloc_thread( TEB
*teb
)
2245 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
2249 static int first_thread
= 1;
2252 LDT_ENTRY entry
= ldt_make_entry( teb
, page_size
- 1, LDT_FLAGS_DATA
| LDT_FLAGS_32BIT
);
2254 if (first_thread
) /* no locking for first thread */
2256 /* leave some space if libc is using the LDT for %gs */
2257 if (!is_gdt_sel( get_gs() )) first_ldt_entry
= 512;
2258 idx
= first_ldt_entry
;
2259 ldt_set_entry( (idx
<< 3) | 7, entry
);
2264 server_enter_uninterrupted_section( &ldt_mutex
, &sigset
);
2265 for (idx
= first_ldt_entry
; idx
< LDT_SIZE
; idx
++)
2267 if (__wine_ldt_copy
.flags
[idx
]) continue;
2268 ldt_set_entry( (idx
<< 3) | 7, entry
);
2271 server_leave_uninterrupted_section( &ldt_mutex
, &sigset
);
2272 if (idx
== LDT_SIZE
) return STATUS_TOO_MANY_THREADS
;
2274 thread_data
->fs
= (idx
<< 3) | 7;
2276 else thread_data
->fs
= gdt_fs_sel
;
2278 teb
->WOW32Reserved
= __wine_syscall_dispatcher
;
2279 return STATUS_SUCCESS
;
2283 /**********************************************************************
2284 * signal_free_thread
2286 void signal_free_thread( TEB
*teb
)
2288 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
2291 if (gdt_fs_sel
) return;
2293 server_enter_uninterrupted_section( &ldt_mutex
, &sigset
);
2294 __wine_ldt_copy
.flags
[thread_data
->fs
>> 3] = 0;
2295 server_leave_uninterrupted_section( &ldt_mutex
, &sigset
);
2299 /**********************************************************************
2300 * signal_init_thread
2302 void signal_init_thread( TEB
*teb
)
2304 const WORD fpu_cw
= 0x27f;
2305 struct x86_thread_data
*thread_data
= (struct x86_thread_data
*)&teb
->GdiTebBatch
;
2307 ldt_set_fs( thread_data
->fs
, teb
);
2308 thread_data
->gs
= get_gs();
2310 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2314 /**********************************************************************
2315 * signal_init_process
2317 void signal_init_process(void)
2319 struct sigaction sig_act
;
2321 if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_FXSR
) __wine_syscall_flags
|= SYSCALL_HAVE_FXSAVE
;
2322 if (cpu_info
.ProcessorFeatureBits
& CPU_FEATURE_XSAVE
) __wine_syscall_flags
|= SYSCALL_HAVE_XSAVE
;
2323 if (xstate_compaction_enabled
) __wine_syscall_flags
|= SYSCALL_HAVE_XSAVEC
;
2325 sig_act
.sa_mask
= server_block_set
;
2326 sig_act
.sa_flags
= SA_SIGINFO
| SA_RESTART
| SA_ONSTACK
;
2328 sig_act
.sa_flags
|= SA_RESTORER
;
2329 sig_act
.sa_restorer
= rt_sigreturn
;
2331 sig_act
.sa_sigaction
= int_handler
;
2332 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2333 sig_act
.sa_sigaction
= fpe_handler
;
2334 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2335 sig_act
.sa_sigaction
= abrt_handler
;
2336 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2337 sig_act
.sa_sigaction
= quit_handler
;
2338 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2339 sig_act
.sa_sigaction
= usr1_handler
;
2340 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2341 sig_act
.sa_sigaction
= trap_handler
;
2342 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2343 sig_act
.sa_sigaction
= segv_handler
;
2344 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2345 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2346 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2350 perror("sigaction");
2355 /***********************************************************************
2356 * init_thread_context
2358 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
)
2360 context
->SegCs
= get_cs();
2361 context
->SegDs
= get_ds();
2362 context
->SegEs
= get_ds();
2363 context
->SegFs
= get_fs();
2364 context
->SegGs
= get_gs();
2365 context
->SegSs
= get_ds();
2366 context
->EFlags
= 0x202;
2367 context
->Eax
= (DWORD
)entry
;
2368 context
->Ebx
= (DWORD
)arg
;
2369 context
->Esp
= (DWORD
)NtCurrentTeb()->Tib
.StackBase
- 16;
2370 context
->Eip
= (DWORD
)pRtlUserThreadStart
;
2371 context
->FloatSave
.ControlWord
= 0x27f;
2372 ((XSAVE_FORMAT
*)context
->ExtendedRegisters
)->ControlWord
= 0x27f;
2373 ((XSAVE_FORMAT
*)context
->ExtendedRegisters
)->MxCsr
= 0x1f80;
2374 if (NtCurrentTeb64())
2376 WOW64_CPURESERVED
*cpu
= ULongToPtr( NtCurrentTeb64()->TlsSlots
[WOW64_TLS_CPURESERVED
] );
2377 memcpy( cpu
+ 1, context
, sizeof(*context
) );
2382 /***********************************************************************
2383 * get_initial_context
2385 PCONTEXT DECLSPEC_HIDDEN
get_initial_context( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
2391 CONTEXT context
= { CONTEXT_ALL
};
2393 init_thread_context( &context
, entry
, arg
);
2394 wait_suspend( &context
);
2395 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Esp
& ~15) - 1;
2400 ctx
= (CONTEXT
*)((char *)NtCurrentTeb()->Tib
.StackBase
- 16) - 1;
2401 init_thread_context( ctx
, entry
, arg
);
2403 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
2404 ctx
->ContextFlags
= CONTEXT_FULL
| CONTEXT_FLOATING_POINT
| CONTEXT_EXTENDED_REGISTERS
;
2409 /***********************************************************************
2410 * signal_start_thread
2412 __ASM_GLOBAL_FUNC( signal_start_thread
,
2414 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2415 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2416 "movl %esp,%ebp\n\t"
2417 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2419 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2421 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2423 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2424 /* store exit frame */
2425 "movl %ebp,%fs:0x1f4\n\t" /* x86_thread_data()->exit_frame */
2426 /* switch to thread stack */
2427 "movl %fs:4,%eax\n\t" /* NtCurrentTeb()->StackBase */
2428 "leal -0x1004(%eax),%esp\n\t"
2430 "pushl 16(%ebp)\n\t" /* suspend */
2431 "pushl 12(%ebp)\n\t" /* arg */
2432 "pushl 8(%ebp)\n\t" /* entry */
2433 "call " __ASM_NAME("get_initial_context") "\n\t"
2434 "movl %eax,(%esp)\n\t" /* context */
2435 "movl 20(%ebp),%edx\n\t" /* thunk */
2436 "xorl %ebp,%ebp\n\t"
2441 /***********************************************************************
2442 * signal_exit_thread
2444 __ASM_GLOBAL_FUNC( signal_exit_thread
,
2445 "movl 8(%esp),%ecx\n\t"
2446 /* fetch exit frame */
2447 "movl %fs:0x1f4,%edx\n\t" /* x86_thread_data()->exit_frame */
2448 "testl %edx,%edx\n\t"
2451 /* switch to exit frame stack */
2452 "1:\tmovl 4(%esp),%eax\n\t"
2453 "movl $0,%fs:0x1f4\n\t"
2454 "movl %edx,%ebp\n\t"
2455 __ASM_CFI(".cfi_def_cfa %ebp,4\n\t")
2456 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2457 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
2458 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2459 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
2460 "leal -20(%ebp),%esp\n\t"
2464 /**********************************************************************
2465 * NtCurrentTeb (NTDLL.@)
2467 __ASM_STDCALL_FUNC( NtCurrentTeb
, 0, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
2469 #endif /* __i386__ */