2 * x86-64 signal handling routines
4 * Copyright 1999, 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
31 #include <sys/types.h>
35 #ifdef HAVE_MACHINE_SYSARCH_H
36 # include <machine/sysarch.h>
38 #ifdef HAVE_SYS_PARAM_H
39 # include <sys/param.h>
44 # ifdef HAVE_SYS_SYSCALL_H
45 # include <sys/syscall.h>
48 #ifdef HAVE_SYS_SIGNAL_H
49 # include <sys/signal.h>
51 #ifdef HAVE_SYS_UCONTEXT_H
52 # include <sys/ucontext.h>
54 #ifdef HAVE_LIBUNWIND_H
55 # define UNW_LOCAL_ONLY
56 # include <libunwind.h>
59 #define NONAMELESSUNION
60 #define NONAMELESSSTRUCT
62 #define WIN32_NO_STATUS
65 #include "wine/library.h"
66 #include "wine/exception.h"
67 #include "wine/list.h"
68 #include "ntdll_misc.h"
69 #include "wine/debug.h"
71 #ifdef HAVE_VALGRIND_MEMCHECK_H
72 #include <valgrind/memcheck.h>
75 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
76 WINE_DECLARE_DEBUG_CHANNEL(relay
);
78 struct _DISPATCHER_CONTEXT
;
80 typedef LONG (WINAPI
*PC_LANGUAGE_EXCEPTION_HANDLER
)( EXCEPTION_POINTERS
*ptrs
, ULONG64 frame
);
81 typedef EXCEPTION_DISPOSITION (WINAPI
*PEXCEPTION_ROUTINE
)( EXCEPTION_RECORD
*rec
,
84 struct _DISPATCHER_CONTEXT
*dispatch
);
85 typedef void (WINAPI
*TERMINATION_HANDLER
)( ULONG flags
, ULONG64 frame
);
87 typedef struct _DISPATCHER_CONTEXT
91 PRUNTIME_FUNCTION FunctionEntry
;
92 ULONG64 EstablisherFrame
;
94 PCONTEXT ContextRecord
;
95 PEXCEPTION_ROUTINE LanguageHandler
;
97 PUNWIND_HISTORY_TABLE HistoryTable
;
99 } DISPATCHER_CONTEXT
, *PDISPATCHER_CONTEXT
;
101 typedef struct _SCOPE_TABLE
108 ULONG HandlerAddress
;
111 } SCOPE_TABLE
, *PSCOPE_TABLE
;
114 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
115 struct MSVCRT_JUMP_BUFFER
141 /***********************************************************************
142 * signal context platform-specific definitions
146 #include <asm/prctl.h>
147 static inline int arch_prctl( int func
, void *ptr
) { return syscall( __NR_arch_prctl
, func
, ptr
); }
149 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
150 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
151 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
152 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
153 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
154 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
155 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
156 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
157 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
158 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
159 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
160 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
161 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
162 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
163 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
165 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
166 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
167 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
169 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
170 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
171 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
172 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
173 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
175 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
177 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
179 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
180 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
181 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
182 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
183 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
184 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
185 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
186 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
187 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
188 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
189 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
190 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
191 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
192 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
193 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
195 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
196 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
197 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
198 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
199 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
200 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
202 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
204 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
205 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
206 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
207 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
209 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
211 #elif defined(__NetBSD__)
213 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
214 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
215 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
216 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
217 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
218 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
219 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
220 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
221 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
222 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
223 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
224 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
225 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
226 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
227 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
229 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
230 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
231 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
232 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
233 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
234 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
236 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
238 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
239 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
241 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
242 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
244 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
245 #elif defined (__APPLE__)
246 #define RAX_sig(context) ((context)->uc_mcontext->__ss.__rax)
247 #define RBX_sig(context) ((context)->uc_mcontext->__ss.__rbx)
248 #define RCX_sig(context) ((context)->uc_mcontext->__ss.__rcx)
249 #define RDX_sig(context) ((context)->uc_mcontext->__ss.__rdx)
250 #define RSI_sig(context) ((context)->uc_mcontext->__ss.__rsi)
251 #define RDI_sig(context) ((context)->uc_mcontext->__ss.__rdi)
252 #define RBP_sig(context) ((context)->uc_mcontext->__ss.__rbp)
253 #define R8_sig(context) ((context)->uc_mcontext->__ss.__r8)
254 #define R9_sig(context) ((context)->uc_mcontext->__ss.__r9)
255 #define R10_sig(context) ((context)->uc_mcontext->__ss.__r10)
256 #define R11_sig(context) ((context)->uc_mcontext->__ss.__r11)
257 #define R12_sig(context) ((context)->uc_mcontext->__ss.__r12)
258 #define R13_sig(context) ((context)->uc_mcontext->__ss.__r13)
259 #define R14_sig(context) ((context)->uc_mcontext->__ss.__r14)
260 #define R15_sig(context) ((context)->uc_mcontext->__ss.__r15)
262 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
263 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
264 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
266 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__rflags)
268 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rip))
269 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rsp))
271 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
272 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
274 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
277 #error You must define the signal context functions for your platform
282 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
283 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
284 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
285 TRAP_x86_NMI
= 2, /* NMI interrupt */
286 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
287 TRAP_x86_OFLOW
= 4, /* Overflow exception */
288 TRAP_x86_BOUND
= 5, /* Bound range exception */
289 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
290 TRAP_x86_DNA
= 7, /* Device not available exception */
291 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
292 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
293 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
294 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
295 TRAP_x86_STKFLT
= 12, /* Stack fault */
296 TRAP_x86_PROTFLT
= 13, /* General protection fault */
297 TRAP_x86_PAGEFLT
= 14, /* Page fault */
298 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
299 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
300 TRAP_x86_MCHK
= 18, /* Machine check exception */
301 TRAP_x86_CACHEFLT
= 19 /* Cache flush exception */
304 static const size_t teb_size
= 0x2000; /* we reserve two pages for the TEB */
305 static size_t signal_stack_size
;
307 typedef void (*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
308 typedef int (*wine_signal_handler
)(unsigned int sig
);
310 static wine_signal_handler handlers
[256];
312 struct amd64_thread_data
314 DWORD_PTR dr0
; /* debug registers */
320 void *exit_frame
; /* exit frame pointer */
323 C_ASSERT( sizeof(struct amd64_thread_data
) <= sizeof(((TEB
*)0)->SystemReserved2
) );
324 C_ASSERT( offsetof( TEB
, SystemReserved2
) + offsetof( struct amd64_thread_data
, exit_frame
) == 0x330 );
326 static inline struct amd64_thread_data
*amd64_thread_data(void)
328 return (struct amd64_thread_data
*)NtCurrentTeb()->SystemReserved2
;
331 /***********************************************************************
332 * Dynamic unwind table
335 struct dynamic_unwind_entry
339 /* memory region which matches this entry */
344 RUNTIME_FUNCTION
*table
;
348 /* user defined callback */
349 PGET_RUNTIME_FUNCTION_CALLBACK callback
;
353 static struct list dynamic_unwind_list
= LIST_INIT(dynamic_unwind_list
);
355 static RTL_CRITICAL_SECTION dynamic_unwind_section
;
356 static RTL_CRITICAL_SECTION_DEBUG dynamic_unwind_debug
=
358 0, 0, &dynamic_unwind_section
,
359 { &dynamic_unwind_debug
.ProcessLocksList
, &dynamic_unwind_debug
.ProcessLocksList
},
360 0, 0, { (DWORD_PTR
)(__FILE__
": dynamic_unwind_section") }
362 static RTL_CRITICAL_SECTION dynamic_unwind_section
= { &dynamic_unwind_debug
, -1, 0, 0, 0, 0 };
364 /***********************************************************************
365 * Definitions for Win32 unwind tables
370 RUNTIME_FUNCTION chain
;
388 BYTE frame_offset
: 4;
389 struct opcode opcodes
[1]; /* info->count entries */
390 /* followed by handler_data */
393 #define UWOP_PUSH_NONVOL 0
394 #define UWOP_ALLOC_LARGE 1
395 #define UWOP_ALLOC_SMALL 2
396 #define UWOP_SET_FPREG 3
397 #define UWOP_SAVE_NONVOL 4
398 #define UWOP_SAVE_NONVOL_FAR 5
399 #define UWOP_SAVE_XMM128 8
400 #define UWOP_SAVE_XMM128_FAR 9
401 #define UWOP_PUSH_MACHFRAME 10
403 static void dump_unwind_info( ULONG64 base
, RUNTIME_FUNCTION
*function
)
405 static const char * const reg_names
[16] =
406 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
407 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
409 union handler_data
*handler_data
;
410 struct UNWIND_INFO
*info
;
411 unsigned int i
, count
;
413 TRACE( "**** func %x-%x\n", function
->BeginAddress
, function
->EndAddress
);
416 if (function
->UnwindData
& 1)
418 RUNTIME_FUNCTION
*next
= (RUNTIME_FUNCTION
*)((char *)base
+ (function
->UnwindData
& ~1));
419 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
420 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
,
421 (char *)base
+ next
->BeginAddress
, (char *)base
+ next
->EndAddress
);
425 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
427 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
428 info
, info
->flags
, info
->prolog
,
429 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
);
432 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
433 reg_names
[info
->frame_reg
], info
->frame_offset
* 16 );
435 for (i
= 0; i
< info
->count
; i
++)
437 TRACE( " 0x%x: ", info
->opcodes
[i
].offset
);
438 switch (info
->opcodes
[i
].code
)
440 case UWOP_PUSH_NONVOL
:
441 TRACE( "pushq %%%s\n", reg_names
[info
->opcodes
[i
].info
] );
443 case UWOP_ALLOC_LARGE
:
444 if (info
->opcodes
[i
].info
)
446 count
= *(DWORD
*)&info
->opcodes
[i
+1];
451 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
454 TRACE( "subq $0x%x,%%rsp\n", count
);
456 case UWOP_ALLOC_SMALL
:
457 count
= (info
->opcodes
[i
].info
+ 1) * 8;
458 TRACE( "subq $0x%x,%%rsp\n", count
);
461 TRACE( "leaq 0x%x(%%rsp),%s\n",
462 info
->frame_offset
* 16, reg_names
[info
->frame_reg
] );
464 case UWOP_SAVE_NONVOL
:
465 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
466 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
469 case UWOP_SAVE_NONVOL_FAR
:
470 count
= *(DWORD
*)&info
->opcodes
[i
+1];
471 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
474 case UWOP_SAVE_XMM128
:
475 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 16;
476 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
479 case UWOP_SAVE_XMM128_FAR
:
480 count
= *(DWORD
*)&info
->opcodes
[i
+1];
481 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
484 case UWOP_PUSH_MACHFRAME
:
485 TRACE( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
488 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
493 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
494 if (info
->flags
& UNW_FLAG_CHAININFO
)
496 TRACE( " chained to function %p-%p\n",
497 (char *)base
+ handler_data
->chain
.BeginAddress
,
498 (char *)base
+ handler_data
->chain
.EndAddress
);
499 function
= &handler_data
->chain
;
502 if (info
->flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
503 TRACE( " handler %p data at %p\n",
504 (char *)base
+ handler_data
->handler
, &handler_data
->handler
+ 1 );
509 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
513 TRACE( "scope table at %p\n", table
);
514 for (i
= 0; i
< table
->Count
; i
++)
515 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i
,
516 base
+ table
->ScopeRecord
[i
].BeginAddress
,
517 base
+ table
->ScopeRecord
[i
].EndAddress
,
518 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
519 base
+ table
->ScopeRecord
[i
].JumpTarget
);
523 /***********************************************************************
524 * Definitions for Dwarf unwind tables
527 enum dwarf_call_frame_info
529 DW_CFA_advance_loc
= 0x40,
530 DW_CFA_offset
= 0x80,
531 DW_CFA_restore
= 0xc0,
533 DW_CFA_set_loc
= 0x01,
534 DW_CFA_advance_loc1
= 0x02,
535 DW_CFA_advance_loc2
= 0x03,
536 DW_CFA_advance_loc4
= 0x04,
537 DW_CFA_offset_extended
= 0x05,
538 DW_CFA_restore_extended
= 0x06,
539 DW_CFA_undefined
= 0x07,
540 DW_CFA_same_value
= 0x08,
541 DW_CFA_register
= 0x09,
542 DW_CFA_remember_state
= 0x0a,
543 DW_CFA_restore_state
= 0x0b,
544 DW_CFA_def_cfa
= 0x0c,
545 DW_CFA_def_cfa_register
= 0x0d,
546 DW_CFA_def_cfa_offset
= 0x0e,
547 DW_CFA_def_cfa_expression
= 0x0f,
548 DW_CFA_expression
= 0x10,
549 DW_CFA_offset_extended_sf
= 0x11,
550 DW_CFA_def_cfa_sf
= 0x12,
551 DW_CFA_def_cfa_offset_sf
= 0x13,
552 DW_CFA_val_offset
= 0x14,
553 DW_CFA_val_offset_sf
= 0x15,
554 DW_CFA_val_expression
= 0x16,
561 DW_OP_const1u
= 0x08,
562 DW_OP_const1s
= 0x09,
563 DW_OP_const2u
= 0x0a,
564 DW_OP_const2s
= 0x0b,
565 DW_OP_const4u
= 0x0c,
566 DW_OP_const4s
= 0x0d,
567 DW_OP_const8u
= 0x0e,
568 DW_OP_const8s
= 0x0f,
588 DW_OP_plus_uconst
= 0x23,
701 DW_OP_deref_size
= 0x94,
702 DW_OP_xderef_size
= 0x95,
704 DW_OP_push_object_address
= 0x97,
707 DW_OP_call_ref
= 0x9a,
708 DW_OP_form_tls_address
= 0x9b,
709 DW_OP_call_frame_cfa
= 0x9c,
710 DW_OP_bit_piece
= 0x9d,
711 DW_OP_lo_user
= 0xe0,
712 DW_OP_hi_user
= 0xff,
713 DW_OP_GNU_push_tls_address
= 0xe0,
714 DW_OP_GNU_uninit
= 0xf0,
715 DW_OP_GNU_encoded_addr
= 0xf1,
718 #define DW_EH_PE_native 0x00
719 #define DW_EH_PE_leb128 0x01
720 #define DW_EH_PE_data2 0x02
721 #define DW_EH_PE_data4 0x03
722 #define DW_EH_PE_data8 0x04
723 #define DW_EH_PE_signed 0x08
724 #define DW_EH_PE_abs 0x00
725 #define DW_EH_PE_pcrel 0x10
726 #define DW_EH_PE_textrel 0x20
727 #define DW_EH_PE_datarel 0x30
728 #define DW_EH_PE_funcrel 0x40
729 #define DW_EH_PE_aligned 0x50
730 #define DW_EH_PE_indirect 0x80
731 #define DW_EH_PE_omit 0xff
733 struct dwarf_eh_bases
744 unsigned char version
;
745 unsigned char augmentation
[1];
751 unsigned int cie_offset
;
754 extern const struct dwarf_fde
*_Unwind_Find_FDE (void *, struct dwarf_eh_bases
*);
756 static unsigned char dwarf_get_u1( const unsigned char **p
)
761 static unsigned short dwarf_get_u2( const unsigned char **p
)
763 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8);
768 static unsigned int dwarf_get_u4( const unsigned char **p
)
770 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8) | ((*p
)[2] << 16) | ((*p
)[3] << 24);
775 static ULONG64
dwarf_get_u8( const unsigned char **p
)
777 ULONG64 low
= dwarf_get_u4( p
);
778 ULONG64 high
= dwarf_get_u4( p
);
779 return low
| (high
<< 32);
782 static ULONG_PTR
dwarf_get_uleb128( const unsigned char **p
)
785 unsigned int shift
= 0;
791 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
794 } while (byte
& 0x80);
798 static LONG_PTR
dwarf_get_sleb128( const unsigned char **p
)
801 unsigned int shift
= 0;
807 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
810 } while (byte
& 0x80);
812 if ((shift
< 8 * sizeof(ret
)) && (byte
& 0x40)) ret
|= -((ULONG_PTR
)1 << shift
);
816 static ULONG_PTR
dwarf_get_ptr( const unsigned char **p
, unsigned char encoding
)
820 if (encoding
== DW_EH_PE_omit
) return 0;
822 switch (encoding
& 0xf0)
828 base
= (ULONG_PTR
)*p
;
831 FIXME( "unsupported encoding %02x\n", encoding
);
835 switch (encoding
& 0x0f)
837 case DW_EH_PE_native
:
838 return base
+ dwarf_get_u8( p
);
839 case DW_EH_PE_leb128
:
840 return base
+ dwarf_get_uleb128( p
);
842 return base
+ dwarf_get_u2( p
);
844 return base
+ dwarf_get_u4( p
);
846 return base
+ dwarf_get_u8( p
);
847 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
848 return base
+ dwarf_get_sleb128( p
);
849 case DW_EH_PE_signed
|DW_EH_PE_data2
:
850 return base
+ (signed short)dwarf_get_u2( p
);
851 case DW_EH_PE_signed
|DW_EH_PE_data4
:
852 return base
+ (signed int)dwarf_get_u4( p
);
853 case DW_EH_PE_signed
|DW_EH_PE_data8
:
854 return base
+ (LONG64
)dwarf_get_u8( p
);
856 FIXME( "unsupported encoding %02x\n", encoding
);
863 RULE_UNSET
, /* not set at all */
864 RULE_UNDEFINED
, /* undefined value */
865 RULE_SAME
, /* same value as previous frame */
866 RULE_CFA_OFFSET
, /* stored at cfa offset */
867 RULE_OTHER_REG
, /* stored in other register */
868 RULE_EXPRESSION
, /* address specified by expression */
869 RULE_VAL_EXPRESSION
/* value specified by expression */
872 #define NB_FRAME_REGS 41
873 #define MAX_SAVED_STATES 16
877 ULONG_PTR cfa_offset
;
878 unsigned char cfa_reg
;
879 enum reg_rule cfa_rule
;
880 enum reg_rule rules
[NB_FRAME_REGS
];
881 ULONG64 regs
[NB_FRAME_REGS
];
887 ULONG_PTR code_align
;
889 unsigned char retaddr_reg
;
890 unsigned char fde_encoding
;
891 unsigned char signal_frame
;
892 unsigned char state_sp
;
893 struct frame_state state
;
894 struct frame_state
*state_stack
;
897 static const char *dwarf_reg_names
[NB_FRAME_REGS
] =
899 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
900 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
901 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
902 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
903 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
906 static BOOL
valid_reg( ULONG_PTR reg
)
908 if (reg
>= NB_FRAME_REGS
) FIXME( "unsupported reg %lx\n", reg
);
909 return (reg
< NB_FRAME_REGS
);
912 static void execute_cfa_instructions( const unsigned char *ptr
, const unsigned char *end
,
913 ULONG_PTR last_ip
, struct frame_info
*info
)
915 while (ptr
< end
&& info
->ip
< last_ip
+ info
->signal_frame
)
917 enum dwarf_call_frame_info op
= *ptr
++;
923 case DW_CFA_advance_loc
:
925 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
926 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
932 ULONG_PTR reg
= op
& 0x3f;
933 LONG_PTR offset
= dwarf_get_uleb128( &ptr
) * info
->data_align
;
934 if (!valid_reg( reg
)) break;
935 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
936 info
->state
.regs
[reg
] = offset
;
937 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
942 ULONG_PTR reg
= op
& 0x3f;
943 if (!valid_reg( reg
)) break;
944 TRACE( "%lx: DW_CFA_restore %s\n", info
->ip
, dwarf_reg_names
[reg
] );
945 info
->state
.rules
[reg
] = RULE_UNSET
;
956 ULONG_PTR loc
= dwarf_get_ptr( &ptr
, info
->fde_encoding
);
957 TRACE( "%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
961 case DW_CFA_advance_loc1
:
963 ULONG_PTR offset
= *ptr
++ * info
->code_align
;
964 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
968 case DW_CFA_advance_loc2
:
970 ULONG_PTR offset
= dwarf_get_u2( &ptr
) * info
->code_align
;
971 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
975 case DW_CFA_advance_loc4
:
977 ULONG_PTR offset
= dwarf_get_u4( &ptr
) * info
->code_align
;
978 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
982 case DW_CFA_offset_extended
:
983 case DW_CFA_offset_extended_sf
:
985 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
986 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf_get_uleb128( &ptr
) * info
->data_align
987 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
988 if (!valid_reg( reg
)) break;
989 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
990 info
->state
.regs
[reg
] = offset
;
991 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
994 case DW_CFA_restore_extended
:
996 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
997 if (!valid_reg( reg
)) break;
998 TRACE( "%lx: DW_CFA_restore_extended %s\n", info
->ip
, dwarf_reg_names
[reg
] );
999 info
->state
.rules
[reg
] = RULE_UNSET
;
1002 case DW_CFA_undefined
:
1004 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1005 if (!valid_reg( reg
)) break;
1006 TRACE( "%lx: DW_CFA_undefined %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1007 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
1010 case DW_CFA_same_value
:
1012 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1013 if (!valid_reg( reg
)) break;
1014 TRACE( "%lx: DW_CFA_same_value %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1015 info
->state
.regs
[reg
] = reg
;
1016 info
->state
.rules
[reg
] = RULE_SAME
;
1019 case DW_CFA_register
:
1021 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1022 ULONG_PTR reg2
= dwarf_get_uleb128( &ptr
);
1023 if (!valid_reg( reg
) || !valid_reg( reg2
)) break;
1024 TRACE( "%lx: DW_CFA_register %s == %s\n", info
->ip
, dwarf_reg_names
[reg
], dwarf_reg_names
[reg2
] );
1025 info
->state
.regs
[reg
] = reg2
;
1026 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
1029 case DW_CFA_remember_state
:
1030 TRACE( "%lx: DW_CFA_remember_state\n", info
->ip
);
1031 if (info
->state_sp
>= MAX_SAVED_STATES
)
1032 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
1034 info
->state_stack
[info
->state_sp
++] = info
->state
;
1036 case DW_CFA_restore_state
:
1037 TRACE( "%lx: DW_CFA_restore_state\n", info
->ip
);
1038 if (!info
->state_sp
)
1039 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
1041 info
->state
= info
->state_stack
[--info
->state_sp
];
1043 case DW_CFA_def_cfa
:
1044 case DW_CFA_def_cfa_sf
:
1046 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1047 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf_get_uleb128( &ptr
)
1048 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
1049 if (!valid_reg( reg
)) break;
1050 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
1051 info
->state
.cfa_reg
= reg
;
1052 info
->state
.cfa_offset
= offset
;
1053 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1056 case DW_CFA_def_cfa_register
:
1058 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1059 if (!valid_reg( reg
)) break;
1060 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1061 info
->state
.cfa_reg
= reg
;
1062 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1065 case DW_CFA_def_cfa_offset
:
1066 case DW_CFA_def_cfa_offset_sf
:
1068 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf_get_uleb128( &ptr
)
1069 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
1070 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info
->ip
, offset
);
1071 info
->state
.cfa_offset
= offset
;
1072 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1075 case DW_CFA_def_cfa_expression
:
1077 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
1078 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
1079 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
1080 info
->state
.cfa_offset
= expr
;
1081 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
1085 case DW_CFA_expression
:
1086 case DW_CFA_val_expression
:
1088 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1089 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
1090 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
1091 if (!valid_reg( reg
)) break;
1092 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
1093 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_", dwarf_reg_names
[reg
], expr
, expr
+len
);
1094 info
->state
.regs
[reg
] = expr
;
1095 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
1100 FIXME( "%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
1106 /* retrieve a context register from its dwarf number */
1107 static void *get_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
)
1111 case 0: return &context
->Rax
;
1112 case 1: return &context
->Rdx
;
1113 case 2: return &context
->Rcx
;
1114 case 3: return &context
->Rbx
;
1115 case 4: return &context
->Rsi
;
1116 case 5: return &context
->Rdi
;
1117 case 6: return &context
->Rbp
;
1118 case 7: return &context
->Rsp
;
1119 case 8: return &context
->R8
;
1120 case 9: return &context
->R9
;
1121 case 10: return &context
->R10
;
1122 case 11: return &context
->R11
;
1123 case 12: return &context
->R12
;
1124 case 13: return &context
->R13
;
1125 case 14: return &context
->R14
;
1126 case 15: return &context
->R15
;
1127 case 16: return &context
->Rip
;
1128 case 17: return &context
->u
.s
.Xmm0
;
1129 case 18: return &context
->u
.s
.Xmm1
;
1130 case 19: return &context
->u
.s
.Xmm2
;
1131 case 20: return &context
->u
.s
.Xmm3
;
1132 case 21: return &context
->u
.s
.Xmm4
;
1133 case 22: return &context
->u
.s
.Xmm5
;
1134 case 23: return &context
->u
.s
.Xmm6
;
1135 case 24: return &context
->u
.s
.Xmm7
;
1136 case 25: return &context
->u
.s
.Xmm8
;
1137 case 26: return &context
->u
.s
.Xmm9
;
1138 case 27: return &context
->u
.s
.Xmm10
;
1139 case 28: return &context
->u
.s
.Xmm11
;
1140 case 29: return &context
->u
.s
.Xmm12
;
1141 case 30: return &context
->u
.s
.Xmm13
;
1142 case 31: return &context
->u
.s
.Xmm14
;
1143 case 32: return &context
->u
.s
.Xmm15
;
1144 case 33: return &context
->u
.s
.Legacy
[0];
1145 case 34: return &context
->u
.s
.Legacy
[1];
1146 case 35: return &context
->u
.s
.Legacy
[2];
1147 case 36: return &context
->u
.s
.Legacy
[3];
1148 case 37: return &context
->u
.s
.Legacy
[4];
1149 case 38: return &context
->u
.s
.Legacy
[5];
1150 case 39: return &context
->u
.s
.Legacy
[6];
1151 case 40: return &context
->u
.s
.Legacy
[7];
1152 default: return NULL
;
1156 /* set a context register from its dwarf number */
1157 static void set_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
, void *val
)
1161 case 0: context
->Rax
= *(ULONG64
*)val
; break;
1162 case 1: context
->Rdx
= *(ULONG64
*)val
; break;
1163 case 2: context
->Rcx
= *(ULONG64
*)val
; break;
1164 case 3: context
->Rbx
= *(ULONG64
*)val
; break;
1165 case 4: context
->Rsi
= *(ULONG64
*)val
; break;
1166 case 5: context
->Rdi
= *(ULONG64
*)val
; break;
1167 case 6: context
->Rbp
= *(ULONG64
*)val
; break;
1168 case 7: context
->Rsp
= *(ULONG64
*)val
; break;
1169 case 8: context
->R8
= *(ULONG64
*)val
; break;
1170 case 9: context
->R9
= *(ULONG64
*)val
; break;
1171 case 10: context
->R10
= *(ULONG64
*)val
; break;
1172 case 11: context
->R11
= *(ULONG64
*)val
; break;
1173 case 12: context
->R12
= *(ULONG64
*)val
; break;
1174 case 13: context
->R13
= *(ULONG64
*)val
; break;
1175 case 14: context
->R14
= *(ULONG64
*)val
; break;
1176 case 15: context
->R15
= *(ULONG64
*)val
; break;
1177 case 16: context
->Rip
= *(ULONG64
*)val
; break;
1178 case 17: memcpy( &context
->u
.s
.Xmm0
, val
, sizeof(M128A
) ); break;
1179 case 18: memcpy( &context
->u
.s
.Xmm1
, val
, sizeof(M128A
) ); break;
1180 case 19: memcpy( &context
->u
.s
.Xmm2
, val
, sizeof(M128A
) ); break;
1181 case 20: memcpy( &context
->u
.s
.Xmm3
, val
, sizeof(M128A
) ); break;
1182 case 21: memcpy( &context
->u
.s
.Xmm4
, val
, sizeof(M128A
) ); break;
1183 case 22: memcpy( &context
->u
.s
.Xmm5
, val
, sizeof(M128A
) ); break;
1184 case 23: memcpy( &context
->u
.s
.Xmm6
, val
, sizeof(M128A
) ); break;
1185 case 24: memcpy( &context
->u
.s
.Xmm7
, val
, sizeof(M128A
) ); break;
1186 case 25: memcpy( &context
->u
.s
.Xmm8
, val
, sizeof(M128A
) ); break;
1187 case 26: memcpy( &context
->u
.s
.Xmm9
, val
, sizeof(M128A
) ); break;
1188 case 27: memcpy( &context
->u
.s
.Xmm10
, val
, sizeof(M128A
) ); break;
1189 case 28: memcpy( &context
->u
.s
.Xmm11
, val
, sizeof(M128A
) ); break;
1190 case 29: memcpy( &context
->u
.s
.Xmm12
, val
, sizeof(M128A
) ); break;
1191 case 30: memcpy( &context
->u
.s
.Xmm13
, val
, sizeof(M128A
) ); break;
1192 case 31: memcpy( &context
->u
.s
.Xmm14
, val
, sizeof(M128A
) ); break;
1193 case 32: memcpy( &context
->u
.s
.Xmm15
, val
, sizeof(M128A
) ); break;
1194 case 33: memcpy( &context
->u
.s
.Legacy
[0], val
, sizeof(M128A
) ); break;
1195 case 34: memcpy( &context
->u
.s
.Legacy
[1], val
, sizeof(M128A
) ); break;
1196 case 35: memcpy( &context
->u
.s
.Legacy
[2], val
, sizeof(M128A
) ); break;
1197 case 36: memcpy( &context
->u
.s
.Legacy
[3], val
, sizeof(M128A
) ); break;
1198 case 37: memcpy( &context
->u
.s
.Legacy
[4], val
, sizeof(M128A
) ); break;
1199 case 38: memcpy( &context
->u
.s
.Legacy
[5], val
, sizeof(M128A
) ); break;
1200 case 39: memcpy( &context
->u
.s
.Legacy
[6], val
, sizeof(M128A
) ); break;
1201 case 40: memcpy( &context
->u
.s
.Legacy
[7], val
, sizeof(M128A
) ); break;
1205 static ULONG_PTR
eval_expression( const unsigned char *p
, CONTEXT
*context
)
1207 ULONG_PTR reg
, tmp
, stack
[64];
1209 ULONG_PTR len
= dwarf_get_uleb128(&p
);
1210 const unsigned char *end
= p
+ len
;
1214 unsigned char opcode
= dwarf_get_u1(&p
);
1216 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
1217 stack
[++sp
] = opcode
- DW_OP_lit0
;
1218 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
1219 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_reg0
);
1220 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
1221 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_breg0
) + dwarf_get_sleb128(&p
);
1222 else switch (opcode
)
1224 case DW_OP_nop
: break;
1225 case DW_OP_addr
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1226 case DW_OP_const1u
: stack
[++sp
] = dwarf_get_u1(&p
); break;
1227 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf_get_u1(&p
); break;
1228 case DW_OP_const2u
: stack
[++sp
] = dwarf_get_u2(&p
); break;
1229 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf_get_u2(&p
); break;
1230 case DW_OP_const4u
: stack
[++sp
] = dwarf_get_u4(&p
); break;
1231 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf_get_u4(&p
); break;
1232 case DW_OP_const8u
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1233 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf_get_u8(&p
); break;
1234 case DW_OP_constu
: stack
[++sp
] = dwarf_get_uleb128(&p
); break;
1235 case DW_OP_consts
: stack
[++sp
] = dwarf_get_sleb128(&p
); break;
1236 case DW_OP_deref
: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1237 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
1238 case DW_OP_drop
: sp
--; break;
1239 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
1240 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf_get_u1(&p
)]; sp
++; break;
1241 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
1242 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
1243 case DW_OP_abs
: stack
[sp
] = labs(stack
[sp
]); break;
1244 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
1245 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
1246 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
1247 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
1248 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
1249 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
1250 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
1251 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
1252 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
1253 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
1254 case DW_OP_plus_uconst
: stack
[sp
] += dwarf_get_uleb128(&p
); break;
1255 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
1256 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
1257 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
1258 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
1259 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
1260 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
1261 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
1262 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
1263 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
1264 case DW_OP_skip
: tmp
= (short)dwarf_get_u2(&p
); p
+= tmp
; break;
1265 case DW_OP_bra
: tmp
= (short)dwarf_get_u2(&p
); if (!stack
[sp
--]) p
+= tmp
; break;
1266 case DW_OP_GNU_encoded_addr
: tmp
= *p
++; stack
[++sp
] = dwarf_get_ptr( &p
, tmp
); break;
1267 case DW_OP_regx
: stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, dwarf_get_uleb128(&p
) ); break;
1269 reg
= dwarf_get_uleb128(&p
);
1270 tmp
= dwarf_get_sleb128(&p
);
1271 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, reg
) + tmp
;
1273 case DW_OP_deref_size
:
1276 case 1: stack
[sp
] = *(unsigned char *)stack
[sp
]; break;
1277 case 2: stack
[sp
] = *(unsigned short *)stack
[sp
]; break;
1278 case 4: stack
[sp
] = *(unsigned int *)stack
[sp
]; break;
1279 case 8: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1283 FIXME( "unhandled opcode %02x\n", opcode
);
1289 /* apply the computed frame info to the actual context */
1290 static void apply_frame_state( CONTEXT
*context
, struct frame_state
*state
)
1293 ULONG_PTR cfa
, value
;
1294 CONTEXT new_context
= *context
;
1296 switch (state
->cfa_rule
)
1298 case RULE_EXPRESSION
:
1299 cfa
= *(ULONG_PTR
*)eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1301 case RULE_VAL_EXPRESSION
:
1302 cfa
= eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1305 cfa
= *(ULONG_PTR
*)get_context_reg( context
, state
->cfa_reg
) + state
->cfa_offset
;
1310 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
1312 switch (state
->rules
[i
])
1315 case RULE_UNDEFINED
:
1318 case RULE_CFA_OFFSET
:
1319 set_context_reg( &new_context
, i
, (char *)cfa
+ state
->regs
[i
] );
1321 case RULE_OTHER_REG
:
1322 set_context_reg( &new_context
, i
, get_context_reg( context
, state
->regs
[i
] ));
1324 case RULE_EXPRESSION
:
1325 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1326 set_context_reg( &new_context
, i
, (void *)value
);
1328 case RULE_VAL_EXPRESSION
:
1329 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1330 set_context_reg( &new_context
, i
, &value
);
1334 new_context
.Rsp
= cfa
;
1335 *context
= new_context
;
1339 /***********************************************************************
1340 * dwarf_virtual_unwind
1342 * Equivalent of RtlVirtualUnwind for builtin modules.
1344 static NTSTATUS
dwarf_virtual_unwind( ULONG64 ip
, ULONG64
*frame
,CONTEXT
*context
,
1345 const struct dwarf_fde
*fde
, const struct dwarf_eh_bases
*bases
,
1346 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1348 const struct dwarf_cie
*cie
;
1349 const unsigned char *ptr
, *augmentation
, *end
;
1350 ULONG_PTR len
, code_end
;
1351 struct frame_info info
;
1352 struct frame_state state_stack
[MAX_SAVED_STATES
];
1353 int aug_z_format
= 0;
1354 unsigned char lsda_encoding
= DW_EH_PE_omit
;
1356 memset( &info
, 0, sizeof(info
) );
1357 info
.state_stack
= state_stack
;
1358 info
.ip
= (ULONG_PTR
)bases
->func
;
1361 cie
= (const struct dwarf_cie
*)((const char *)&fde
->cie_offset
- fde
->cie_offset
);
1363 /* parse the CIE first */
1365 if (cie
->version
!= 1 && cie
->version
!= 3)
1367 FIXME( "unknown CIE version %u at %p\n", cie
->version
, cie
);
1368 return STATUS_INVALID_DISPOSITION
;
1370 ptr
= cie
->augmentation
+ strlen((const char *)cie
->augmentation
) + 1;
1372 info
.code_align
= dwarf_get_uleb128( &ptr
);
1373 info
.data_align
= dwarf_get_sleb128( &ptr
);
1374 if (cie
->version
== 1)
1375 info
.retaddr_reg
= *ptr
++;
1377 info
.retaddr_reg
= dwarf_get_uleb128( &ptr
);
1378 info
.state
.cfa_rule
= RULE_CFA_OFFSET
;
1380 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1381 ip
, bases
->func
, cie
, cie
->length
, cie
->id
, cie
->version
, cie
->augmentation
,
1382 info
.code_align
, info
.data_align
, dwarf_reg_names
[info
.retaddr_reg
] );
1385 for (augmentation
= cie
->augmentation
; *augmentation
; augmentation
++)
1387 switch (*augmentation
)
1390 len
= dwarf_get_uleb128( &ptr
);
1395 lsda_encoding
= *ptr
++;
1399 unsigned char encoding
= *ptr
++;
1400 *handler
= (void *)dwarf_get_ptr( &ptr
, encoding
);
1404 info
.fde_encoding
= *ptr
++;
1407 info
.signal_frame
= 1;
1410 FIXME( "unknown augmentation '%c'\n", *augmentation
);
1411 if (!end
) return STATUS_INVALID_DISPOSITION
; /* cannot continue */
1416 end
= (const unsigned char *)(&cie
->length
+ 1) + cie
->length
;
1417 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1419 ptr
= (const unsigned char *)(fde
+ 1);
1420 info
.ip
= dwarf_get_ptr( &ptr
, info
.fde_encoding
); /* fde code start */
1421 code_end
= info
.ip
+ dwarf_get_ptr( &ptr
, info
.fde_encoding
& 0x0f ); /* fde code length */
1423 if (aug_z_format
) /* get length of augmentation data */
1425 len
= dwarf_get_uleb128( &ptr
);
1430 *handler_data
= (void *)dwarf_get_ptr( &ptr
, lsda_encoding
);
1433 end
= (const unsigned char *)(&fde
->length
+ 1) + fde
->length
;
1434 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1435 fde
, fde
->length
, *handler
, *handler_data
, info
.ip
, code_end
);
1436 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1437 *frame
= context
->Rsp
;
1438 apply_frame_state( context
, &info
.state
);
1440 TRACE( "next function rip=%016lx\n", context
->Rip
);
1441 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1442 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1443 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1444 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1445 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1446 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1447 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1448 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1450 return STATUS_SUCCESS
;
1454 #ifdef HAVE_LIBUNWIND_H
1455 /***********************************************************************
1456 * libunwind_set_cursor_from_context
1458 static int libunwind_set_cursor_from_context( unw_cursor_t
*cursor
, CONTEXT
*context
, ULONG64 ip
)
1462 rc
= unw_set_reg(cursor
, UNW_REG_IP
, ip
);
1463 if (rc
== UNW_ESUCCESS
)
1464 rc
= unw_set_reg(cursor
, UNW_REG_SP
, context
->Rsp
);
1465 if (rc
== UNW_ESUCCESS
)
1466 rc
= unw_set_reg(cursor
, UNW_X86_64_RAX
, context
->Rax
);
1467 if (rc
== UNW_ESUCCESS
)
1468 rc
= unw_set_reg(cursor
, UNW_X86_64_RDX
, context
->Rdx
);
1469 if (rc
== UNW_ESUCCESS
)
1470 rc
= unw_set_reg(cursor
, UNW_X86_64_RCX
, context
->Rcx
);
1471 if (rc
== UNW_ESUCCESS
)
1472 rc
= unw_set_reg(cursor
, UNW_X86_64_RBX
, context
->Rbx
);
1473 if (rc
== UNW_ESUCCESS
)
1474 rc
= unw_set_reg(cursor
, UNW_X86_64_RSI
, context
->Rsi
);
1475 if (rc
== UNW_ESUCCESS
)
1476 rc
= unw_set_reg(cursor
, UNW_X86_64_RDI
, context
->Rdi
);
1477 if (rc
== UNW_ESUCCESS
)
1478 rc
= unw_set_reg(cursor
, UNW_X86_64_RBP
, context
->Rbp
);
1479 if (rc
== UNW_ESUCCESS
)
1480 rc
= unw_set_reg(cursor
, UNW_X86_64_R8
, context
->R8
);
1481 if (rc
== UNW_ESUCCESS
)
1482 rc
= unw_set_reg(cursor
, UNW_X86_64_R9
, context
->R9
);
1483 if (rc
== UNW_ESUCCESS
)
1484 rc
= unw_set_reg(cursor
, UNW_X86_64_R10
, context
->R10
);
1485 if (rc
== UNW_ESUCCESS
)
1486 rc
= unw_set_reg(cursor
, UNW_X86_64_R11
, context
->R11
);
1487 if (rc
== UNW_ESUCCESS
)
1488 rc
= unw_set_reg(cursor
, UNW_X86_64_R12
, context
->R12
);
1489 if (rc
== UNW_ESUCCESS
)
1490 rc
= unw_set_reg(cursor
, UNW_X86_64_R13
, context
->R13
);
1491 if (rc
== UNW_ESUCCESS
)
1492 rc
= unw_set_reg(cursor
, UNW_X86_64_R14
, context
->R14
);
1493 if (rc
== UNW_ESUCCESS
)
1494 rc
= unw_set_reg(cursor
, UNW_X86_64_R15
, context
->R15
);
1500 /***********************************************************************
1503 static int libunwind_get_reg( unw_cursor_t
*cursor
, unw_regnum_t reg
, ULONG64
*val
)
1508 rc
= unw_get_reg(cursor
, reg
, &word
);
1509 if (rc
== UNW_ESUCCESS
)
1516 /***********************************************************************
1517 * libunwind_set_context_from_cursor
1519 static BOOL
libunwind_set_context_from_cursor( CONTEXT
*context
, unw_cursor_t
*cursor
)
1523 rc
= libunwind_get_reg(cursor
, UNW_REG_IP
, &context
->Rip
);
1524 if (rc
== UNW_ESUCCESS
)
1525 rc
= libunwind_get_reg(cursor
, UNW_REG_SP
, &context
->Rsp
);
1526 if (rc
== UNW_ESUCCESS
)
1527 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RAX
, &context
->Rax
);
1528 if (rc
== UNW_ESUCCESS
)
1529 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RDX
, &context
->Rdx
);
1530 if (rc
== UNW_ESUCCESS
)
1531 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RCX
, &context
->Rcx
);
1532 if (rc
== UNW_ESUCCESS
)
1533 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RBX
, &context
->Rbx
);
1534 if (rc
== UNW_ESUCCESS
)
1535 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RSI
, &context
->Rsi
);
1536 if (rc
== UNW_ESUCCESS
)
1537 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RDI
, &context
->Rdi
);
1538 if (rc
== UNW_ESUCCESS
)
1539 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RBP
, &context
->Rbp
);
1540 if (rc
== UNW_ESUCCESS
)
1541 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R8
, &context
->R8
);
1542 if (rc
== UNW_ESUCCESS
)
1543 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R9
, &context
->R9
);
1544 if (rc
== UNW_ESUCCESS
)
1545 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R10
, &context
->R10
);
1546 if (rc
== UNW_ESUCCESS
)
1547 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R11
, &context
->R11
);
1548 if (rc
== UNW_ESUCCESS
)
1549 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R12
, &context
->R12
);
1550 if (rc
== UNW_ESUCCESS
)
1551 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R13
, &context
->R13
);
1552 if (rc
== UNW_ESUCCESS
)
1553 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R14
, &context
->R14
);
1554 if (rc
== UNW_ESUCCESS
)
1555 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R15
, &context
->R15
);
1561 /***********************************************************************
1562 * libunwind_virtual_unwind
1564 * Equivalent of RtlVirtualUnwind for builtin modules.
1566 static NTSTATUS
libunwind_virtual_unwind( ULONG64 ip
, BOOL
* got_info
, ULONG64
*frame
, CONTEXT
*context
,
1567 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1569 unw_context_t unw_context
;
1570 unw_cursor_t cursor
;
1571 unw_proc_info_t info
;
1574 rc
= unw_getcontext( &unw_context
);
1575 if (rc
== UNW_ESUCCESS
)
1576 rc
= unw_init_local( &cursor
, &unw_context
);
1577 if (rc
== UNW_ESUCCESS
)
1578 rc
= libunwind_set_cursor_from_context( &cursor
, context
, ip
- 1 );
1579 if (rc
!= UNW_ESUCCESS
)
1581 WARN( "setup failed: %d\n", rc
);
1582 return STATUS_INVALID_DISPOSITION
;
1585 rc
= unw_get_proc_info(&cursor
, &info
);
1586 if (rc
!= UNW_ESUCCESS
&& rc
!= UNW_ENOINFO
)
1588 WARN( "failed to get info: %d\n", rc
);
1589 return STATUS_INVALID_DISPOSITION
;
1591 if (rc
== UNW_ENOINFO
|| ip
< info
.start_ip
|| info
.end_ip
<= ip
|| !info
.format
)
1594 return STATUS_SUCCESS
;
1597 TRACE( "ip %#lx function %#lx-%#lx personality %#lx lsda %#lx fde %#lx\n",
1598 ip
, (unsigned long)info
.start_ip
, (unsigned long)info
.end_ip
, (unsigned long)info
.handler
,
1599 (unsigned long)info
.lsda
, (unsigned long)info
.unwind_info
);
1601 rc
= unw_step(&cursor
);
1604 WARN( "failed to unwind: %d\n", rc
);
1605 return STATUS_INVALID_DISPOSITION
;
1608 *frame
= context
->Rsp
;
1610 rc
= libunwind_set_context_from_cursor( context
, &cursor
);
1611 if (rc
!= UNW_ESUCCESS
)
1613 WARN( "failed to update context after unwind: %d\n", rc
);
1614 return STATUS_INVALID_DISPOSITION
;
1617 *handler
= (void*)info
.handler
;
1618 *handler_data
= (void*)info
.lsda
;
1621 TRACE( "next function rip=%016lx\n", context
->Rip
);
1622 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1623 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1624 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1625 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1626 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1627 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1628 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1629 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1631 return STATUS_SUCCESS
;
1636 /***********************************************************************
1639 static inline int dispatch_signal(unsigned int sig
)
1641 if (handlers
[sig
] == NULL
) return 0;
1642 return handlers
[sig
](sig
);
1645 /***********************************************************************
1648 * Get the base of the signal stack for the current thread.
1650 static inline void *get_signal_stack(void)
1652 return (char *)NtCurrentTeb() + teb_size
;
1655 /***********************************************************************
1656 * is_inside_signal_stack
1658 * Check if pointer is inside the signal stack.
1660 static inline BOOL
is_inside_signal_stack( void *ptr
)
1662 return ((char *)ptr
>= (char *)get_signal_stack() &&
1663 (char *)ptr
< (char *)get_signal_stack() + signal_stack_size
);
1666 /***********************************************************************
1669 * Set the register values from a sigcontext.
1671 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
1673 context
->ContextFlags
= CONTEXT_CONTROL
| CONTEXT_INTEGER
| CONTEXT_SEGMENTS
| CONTEXT_DEBUG_REGISTERS
;
1674 context
->Rax
= RAX_sig(sigcontext
);
1675 context
->Rcx
= RCX_sig(sigcontext
);
1676 context
->Rdx
= RDX_sig(sigcontext
);
1677 context
->Rbx
= RBX_sig(sigcontext
);
1678 context
->Rsp
= RSP_sig(sigcontext
);
1679 context
->Rbp
= RBP_sig(sigcontext
);
1680 context
->Rsi
= RSI_sig(sigcontext
);
1681 context
->Rdi
= RDI_sig(sigcontext
);
1682 context
->R8
= R8_sig(sigcontext
);
1683 context
->R9
= R9_sig(sigcontext
);
1684 context
->R10
= R10_sig(sigcontext
);
1685 context
->R11
= R11_sig(sigcontext
);
1686 context
->R12
= R12_sig(sigcontext
);
1687 context
->R13
= R13_sig(sigcontext
);
1688 context
->R14
= R14_sig(sigcontext
);
1689 context
->R15
= R15_sig(sigcontext
);
1690 context
->Rip
= RIP_sig(sigcontext
);
1691 context
->SegCs
= CS_sig(sigcontext
);
1692 context
->SegFs
= FS_sig(sigcontext
);
1693 context
->SegGs
= GS_sig(sigcontext
);
1694 context
->EFlags
= EFL_sig(sigcontext
);
1696 context
->SegDs
= DS_sig(sigcontext
);
1698 __asm__("movw %%ds,%0" : "=m" (context
->SegDs
));
1701 context
->SegEs
= ES_sig(sigcontext
);
1703 __asm__("movw %%es,%0" : "=m" (context
->SegEs
));
1706 context
->SegSs
= SS_sig(sigcontext
);
1708 __asm__("movw %%ss,%0" : "=m" (context
->SegSs
));
1710 context
->Dr0
= amd64_thread_data()->dr0
;
1711 context
->Dr1
= amd64_thread_data()->dr1
;
1712 context
->Dr2
= amd64_thread_data()->dr2
;
1713 context
->Dr3
= amd64_thread_data()->dr3
;
1714 context
->Dr6
= amd64_thread_data()->dr6
;
1715 context
->Dr7
= amd64_thread_data()->dr7
;
1716 if (FPU_sig(sigcontext
))
1718 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1719 context
->u
.FltSave
= *FPU_sig(sigcontext
);
1720 context
->MxCsr
= context
->u
.FltSave
.MxCsr
;
1725 /***********************************************************************
1728 * Build a sigcontext from the register values.
1730 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1732 amd64_thread_data()->dr0
= context
->Dr0
;
1733 amd64_thread_data()->dr1
= context
->Dr1
;
1734 amd64_thread_data()->dr2
= context
->Dr2
;
1735 amd64_thread_data()->dr3
= context
->Dr3
;
1736 amd64_thread_data()->dr6
= context
->Dr6
;
1737 amd64_thread_data()->dr7
= context
->Dr7
;
1738 RAX_sig(sigcontext
) = context
->Rax
;
1739 RCX_sig(sigcontext
) = context
->Rcx
;
1740 RDX_sig(sigcontext
) = context
->Rdx
;
1741 RBX_sig(sigcontext
) = context
->Rbx
;
1742 RSP_sig(sigcontext
) = context
->Rsp
;
1743 RBP_sig(sigcontext
) = context
->Rbp
;
1744 RSI_sig(sigcontext
) = context
->Rsi
;
1745 RDI_sig(sigcontext
) = context
->Rdi
;
1746 R8_sig(sigcontext
) = context
->R8
;
1747 R9_sig(sigcontext
) = context
->R9
;
1748 R10_sig(sigcontext
) = context
->R10
;
1749 R11_sig(sigcontext
) = context
->R11
;
1750 R12_sig(sigcontext
) = context
->R12
;
1751 R13_sig(sigcontext
) = context
->R13
;
1752 R14_sig(sigcontext
) = context
->R14
;
1753 R15_sig(sigcontext
) = context
->R15
;
1754 RIP_sig(sigcontext
) = context
->Rip
;
1755 CS_sig(sigcontext
) = context
->SegCs
;
1756 FS_sig(sigcontext
) = context
->SegFs
;
1757 GS_sig(sigcontext
) = context
->SegGs
;
1758 EFL_sig(sigcontext
) = context
->EFlags
;
1760 DS_sig(sigcontext
) = context
->SegDs
;
1763 ES_sig(sigcontext
) = context
->SegEs
;
1766 SS_sig(sigcontext
) = context
->SegSs
;
1768 if (FPU_sig(sigcontext
)) *FPU_sig(sigcontext
) = context
->u
.FltSave
;
1772 /**************************************************************************
1773 * __chkstk (NTDLL.@)
1775 * Supposed to touch all the stack pages, but we shouldn't need that.
1777 __ASM_GLOBAL_FUNC( __chkstk
, "ret" );
1780 /***********************************************************************
1781 * RtlCaptureContext (NTDLL.@)
1783 __ASM_GLOBAL_FUNC( RtlCaptureContext
,
1785 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1786 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1787 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1788 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1789 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1790 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1791 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1792 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1793 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1794 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1795 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1796 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1797 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1798 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1799 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1800 "leaq 8(%rsp),%rax\n\t"
1801 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1802 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1803 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1804 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1805 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1806 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1807 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1808 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1809 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1810 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1811 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1812 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1813 "movq (%rsp),%rax\n\t"
1814 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1815 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1816 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1817 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1818 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1819 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1820 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1821 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1822 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1823 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1824 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1825 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1826 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1827 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1828 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1829 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1830 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1831 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1834 /***********************************************************************
1835 * set_full_cpu_context
1837 * Set the new CPU context.
1839 extern void set_full_cpu_context( const CONTEXT
*context
);
1840 __ASM_GLOBAL_FUNC( set_full_cpu_context
,
1842 __ASM_CFI(".cfi_adjust_cfa_offset 40\n\t")
1843 "ldmxcsr 0x34(%rdi)\n\t" /* context->MxCsr */
1844 "movw 0x38(%rdi),%ax\n\t" /* context->SegCs */
1845 "movq %rax,8(%rsp)\n\t"
1846 "movw 0x42(%rdi),%ax\n\t" /* context->SegSs */
1847 "movq %rax,32(%rsp)\n\t"
1848 "movq 0x44(%rdi),%rax\n\t" /* context->Eflags */
1849 "movq %rax,16(%rsp)\n\t"
1850 "movq 0x80(%rdi),%rcx\n\t" /* context->Rcx */
1851 "movq 0x88(%rdi),%rdx\n\t" /* context->Rdx */
1852 "movq 0x90(%rdi),%rbx\n\t" /* context->Rbx */
1853 "movq 0x98(%rdi),%rax\n\t" /* context->Rsp */
1854 "movq %rax,24(%rsp)\n\t"
1855 "movq 0xa0(%rdi),%rbp\n\t" /* context->Rbp */
1856 "movq 0xa8(%rdi),%rsi\n\t" /* context->Rsi */
1857 "movq 0xb8(%rdi),%r8\n\t" /* context->R8 */
1858 "movq 0xc0(%rdi),%r9\n\t" /* context->R9 */
1859 "movq 0xc8(%rdi),%r10\n\t" /* context->R10 */
1860 "movq 0xd0(%rdi),%r11\n\t" /* context->R11 */
1861 "movq 0xd8(%rdi),%r12\n\t" /* context->R12 */
1862 "movq 0xe0(%rdi),%r13\n\t" /* context->R13 */
1863 "movq 0xe8(%rdi),%r14\n\t" /* context->R14 */
1864 "movq 0xf0(%rdi),%r15\n\t" /* context->R15 */
1865 "movq 0xf8(%rdi),%rax\n\t" /* context->Rip */
1866 "movq %rax,(%rsp)\n\t"
1867 "fxrstor 0x100(%rdi)\n\t" /* context->FtlSave */
1868 "movdqa 0x1a0(%rdi),%xmm0\n\t" /* context->Xmm0 */
1869 "movdqa 0x1b0(%rdi),%xmm1\n\t" /* context->Xmm1 */
1870 "movdqa 0x1c0(%rdi),%xmm2\n\t" /* context->Xmm2 */
1871 "movdqa 0x1d0(%rdi),%xmm3\n\t" /* context->Xmm3 */
1872 "movdqa 0x1e0(%rdi),%xmm4\n\t" /* context->Xmm4 */
1873 "movdqa 0x1f0(%rdi),%xmm5\n\t" /* context->Xmm5 */
1874 "movdqa 0x200(%rdi),%xmm6\n\t" /* context->Xmm6 */
1875 "movdqa 0x210(%rdi),%xmm7\n\t" /* context->Xmm7 */
1876 "movdqa 0x220(%rdi),%xmm8\n\t" /* context->Xmm8 */
1877 "movdqa 0x230(%rdi),%xmm9\n\t" /* context->Xmm9 */
1878 "movdqa 0x240(%rdi),%xmm10\n\t" /* context->Xmm10 */
1879 "movdqa 0x250(%rdi),%xmm11\n\t" /* context->Xmm11 */
1880 "movdqa 0x260(%rdi),%xmm12\n\t" /* context->Xmm12 */
1881 "movdqa 0x270(%rdi),%xmm13\n\t" /* context->Xmm13 */
1882 "movdqa 0x280(%rdi),%xmm14\n\t" /* context->Xmm14 */
1883 "movdqa 0x290(%rdi),%xmm15\n\t" /* context->Xmm15 */
1884 "movq 0x78(%rdi),%rax\n\t" /* context->Rax */
1885 "movq 0xb0(%rdi),%rdi\n\t" /* context->Rdi */
1889 /***********************************************************************
1892 * Set the new CPU context. Used by NtSetContextThread.
1894 void DECLSPEC_HIDDEN
set_cpu_context( const CONTEXT
*context
)
1896 DWORD flags
= context
->ContextFlags
& ~CONTEXT_AMD64
;
1898 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1900 amd64_thread_data()->dr0
= context
->Dr0
;
1901 amd64_thread_data()->dr1
= context
->Dr1
;
1902 amd64_thread_data()->dr2
= context
->Dr2
;
1903 amd64_thread_data()->dr3
= context
->Dr3
;
1904 amd64_thread_data()->dr6
= context
->Dr6
;
1905 amd64_thread_data()->dr7
= context
->Dr7
;
1907 if (flags
& CONTEXT_FULL
)
1909 if (!(flags
& CONTEXT_CONTROL
))
1910 FIXME( "setting partial context (%x) not supported\n", flags
);
1912 set_full_cpu_context( context
);
1917 /***********************************************************************
1918 * get_server_context_flags
1920 * Convert CPU-specific flags to generic server flags
1922 static unsigned int get_server_context_flags( DWORD flags
)
1924 unsigned int ret
= 0;
1926 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1927 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
1928 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
1929 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
1930 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
1931 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
1936 /***********************************************************************
1939 * Copy a register context according to the flags.
1941 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1943 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1944 if (flags
& CONTEXT_CONTROL
)
1946 to
->Rbp
= from
->Rbp
;
1947 to
->Rip
= from
->Rip
;
1948 to
->Rsp
= from
->Rsp
;
1949 to
->SegCs
= from
->SegCs
;
1950 to
->SegSs
= from
->SegSs
;
1951 to
->EFlags
= from
->EFlags
;
1953 if (flags
& CONTEXT_INTEGER
)
1955 to
->Rax
= from
->Rax
;
1956 to
->Rcx
= from
->Rcx
;
1957 to
->Rdx
= from
->Rdx
;
1958 to
->Rbx
= from
->Rbx
;
1959 to
->Rsi
= from
->Rsi
;
1960 to
->Rdi
= from
->Rdi
;
1963 to
->R10
= from
->R10
;
1964 to
->R11
= from
->R11
;
1965 to
->R12
= from
->R12
;
1966 to
->R13
= from
->R13
;
1967 to
->R14
= from
->R14
;
1968 to
->R15
= from
->R15
;
1970 if (flags
& CONTEXT_SEGMENTS
)
1972 to
->SegDs
= from
->SegDs
;
1973 to
->SegEs
= from
->SegEs
;
1974 to
->SegFs
= from
->SegFs
;
1975 to
->SegGs
= from
->SegGs
;
1977 if (flags
& CONTEXT_FLOATING_POINT
)
1979 to
->MxCsr
= from
->MxCsr
;
1980 to
->u
.FltSave
= from
->u
.FltSave
;
1982 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1984 to
->Dr0
= from
->Dr0
;
1985 to
->Dr1
= from
->Dr1
;
1986 to
->Dr2
= from
->Dr2
;
1987 to
->Dr3
= from
->Dr3
;
1988 to
->Dr6
= from
->Dr6
;
1989 to
->Dr7
= from
->Dr7
;
1994 /***********************************************************************
1997 * Convert a register context to the server format.
1999 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
2001 DWORD flags
= from
->ContextFlags
& ~CONTEXT_AMD64
; /* get rid of CPU id */
2003 memset( to
, 0, sizeof(*to
) );
2004 to
->cpu
= CPU_x86_64
;
2006 if (flags
& CONTEXT_CONTROL
)
2008 to
->flags
|= SERVER_CTX_CONTROL
;
2009 to
->ctl
.x86_64_regs
.rbp
= from
->Rbp
;
2010 to
->ctl
.x86_64_regs
.rip
= from
->Rip
;
2011 to
->ctl
.x86_64_regs
.rsp
= from
->Rsp
;
2012 to
->ctl
.x86_64_regs
.cs
= from
->SegCs
;
2013 to
->ctl
.x86_64_regs
.ss
= from
->SegSs
;
2014 to
->ctl
.x86_64_regs
.flags
= from
->EFlags
;
2016 if (flags
& CONTEXT_INTEGER
)
2018 to
->flags
|= SERVER_CTX_INTEGER
;
2019 to
->integer
.x86_64_regs
.rax
= from
->Rax
;
2020 to
->integer
.x86_64_regs
.rcx
= from
->Rcx
;
2021 to
->integer
.x86_64_regs
.rdx
= from
->Rdx
;
2022 to
->integer
.x86_64_regs
.rbx
= from
->Rbx
;
2023 to
->integer
.x86_64_regs
.rsi
= from
->Rsi
;
2024 to
->integer
.x86_64_regs
.rdi
= from
->Rdi
;
2025 to
->integer
.x86_64_regs
.r8
= from
->R8
;
2026 to
->integer
.x86_64_regs
.r9
= from
->R9
;
2027 to
->integer
.x86_64_regs
.r10
= from
->R10
;
2028 to
->integer
.x86_64_regs
.r11
= from
->R11
;
2029 to
->integer
.x86_64_regs
.r12
= from
->R12
;
2030 to
->integer
.x86_64_regs
.r13
= from
->R13
;
2031 to
->integer
.x86_64_regs
.r14
= from
->R14
;
2032 to
->integer
.x86_64_regs
.r15
= from
->R15
;
2034 if (flags
& CONTEXT_SEGMENTS
)
2036 to
->flags
|= SERVER_CTX_SEGMENTS
;
2037 to
->seg
.x86_64_regs
.ds
= from
->SegDs
;
2038 to
->seg
.x86_64_regs
.es
= from
->SegEs
;
2039 to
->seg
.x86_64_regs
.fs
= from
->SegFs
;
2040 to
->seg
.x86_64_regs
.gs
= from
->SegGs
;
2042 if (flags
& CONTEXT_FLOATING_POINT
)
2044 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
2045 memcpy( to
->fp
.x86_64_regs
.fpregs
, &from
->u
.FltSave
, sizeof(to
->fp
.x86_64_regs
.fpregs
) );
2047 if (flags
& CONTEXT_DEBUG_REGISTERS
)
2049 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
2050 to
->debug
.x86_64_regs
.dr0
= from
->Dr0
;
2051 to
->debug
.x86_64_regs
.dr1
= from
->Dr1
;
2052 to
->debug
.x86_64_regs
.dr2
= from
->Dr2
;
2053 to
->debug
.x86_64_regs
.dr3
= from
->Dr3
;
2054 to
->debug
.x86_64_regs
.dr6
= from
->Dr6
;
2055 to
->debug
.x86_64_regs
.dr7
= from
->Dr7
;
2057 return STATUS_SUCCESS
;
2061 /***********************************************************************
2062 * context_from_server
2064 * Convert a register context from the server format.
2066 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
2068 if (from
->cpu
!= CPU_x86_64
) return STATUS_INVALID_PARAMETER
;
2070 to
->ContextFlags
= CONTEXT_AMD64
;
2071 if (from
->flags
& SERVER_CTX_CONTROL
)
2073 to
->ContextFlags
|= CONTEXT_CONTROL
;
2074 to
->Rbp
= from
->ctl
.x86_64_regs
.rbp
;
2075 to
->Rip
= from
->ctl
.x86_64_regs
.rip
;
2076 to
->Rsp
= from
->ctl
.x86_64_regs
.rsp
;
2077 to
->SegCs
= from
->ctl
.x86_64_regs
.cs
;
2078 to
->SegSs
= from
->ctl
.x86_64_regs
.ss
;
2079 to
->EFlags
= from
->ctl
.x86_64_regs
.flags
;
2082 if (from
->flags
& SERVER_CTX_INTEGER
)
2084 to
->ContextFlags
|= CONTEXT_INTEGER
;
2085 to
->Rax
= from
->integer
.x86_64_regs
.rax
;
2086 to
->Rcx
= from
->integer
.x86_64_regs
.rcx
;
2087 to
->Rdx
= from
->integer
.x86_64_regs
.rdx
;
2088 to
->Rbx
= from
->integer
.x86_64_regs
.rbx
;
2089 to
->Rsi
= from
->integer
.x86_64_regs
.rsi
;
2090 to
->Rdi
= from
->integer
.x86_64_regs
.rdi
;
2091 to
->R8
= from
->integer
.x86_64_regs
.r8
;
2092 to
->R9
= from
->integer
.x86_64_regs
.r9
;
2093 to
->R10
= from
->integer
.x86_64_regs
.r10
;
2094 to
->R11
= from
->integer
.x86_64_regs
.r11
;
2095 to
->R12
= from
->integer
.x86_64_regs
.r12
;
2096 to
->R13
= from
->integer
.x86_64_regs
.r13
;
2097 to
->R14
= from
->integer
.x86_64_regs
.r14
;
2098 to
->R15
= from
->integer
.x86_64_regs
.r15
;
2100 if (from
->flags
& SERVER_CTX_SEGMENTS
)
2102 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
2103 to
->SegDs
= from
->seg
.x86_64_regs
.ds
;
2104 to
->SegEs
= from
->seg
.x86_64_regs
.es
;
2105 to
->SegFs
= from
->seg
.x86_64_regs
.fs
;
2106 to
->SegGs
= from
->seg
.x86_64_regs
.gs
;
2108 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
2110 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
2111 memcpy( &to
->u
.FltSave
, from
->fp
.x86_64_regs
.fpregs
, sizeof(from
->fp
.x86_64_regs
.fpregs
) );
2112 to
->MxCsr
= to
->u
.FltSave
.MxCsr
;
2114 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
2116 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
2117 to
->Dr0
= from
->debug
.x86_64_regs
.dr0
;
2118 to
->Dr1
= from
->debug
.x86_64_regs
.dr1
;
2119 to
->Dr2
= from
->debug
.x86_64_regs
.dr2
;
2120 to
->Dr3
= from
->debug
.x86_64_regs
.dr3
;
2121 to
->Dr6
= from
->debug
.x86_64_regs
.dr6
;
2122 to
->Dr7
= from
->debug
.x86_64_regs
.dr7
;
2124 return STATUS_SUCCESS
;
2128 /***********************************************************************
2129 * NtSetContextThread (NTDLL.@)
2130 * ZwSetContextThread (NTDLL.@)
2132 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
2134 NTSTATUS ret
= STATUS_SUCCESS
;
2135 BOOL self
= (handle
== GetCurrentThread());
2137 /* debug registers require a server call */
2138 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
)))
2139 self
= (amd64_thread_data()->dr0
== context
->Dr0
&&
2140 amd64_thread_data()->dr1
== context
->Dr1
&&
2141 amd64_thread_data()->dr2
== context
->Dr2
&&
2142 amd64_thread_data()->dr3
== context
->Dr3
&&
2143 amd64_thread_data()->dr6
== context
->Dr6
&&
2144 amd64_thread_data()->dr7
== context
->Dr7
);
2148 context_t server_context
;
2149 context_to_server( &server_context
, context
);
2150 ret
= set_thread_context( handle
, &server_context
, &self
);
2152 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
2157 /***********************************************************************
2158 * NtGetContextThread (NTDLL.@)
2159 * ZwGetContextThread (NTDLL.@)
2161 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
2165 BOOL self
= (handle
== GetCurrentThread());
2167 if (!context
) return STATUS_INVALID_PARAMETER
;
2169 needed_flags
= context
->ContextFlags
;
2171 /* debug registers require a server call */
2172 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
)) self
= FALSE
;
2176 context_t server_context
;
2177 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
2179 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
2180 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
2181 needed_flags
&= ~context
->ContextFlags
;
2189 RtlCaptureContext( &ctx
);
2190 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
2191 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
2193 /* update the cached version of the debug registers */
2194 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
))
2196 amd64_thread_data()->dr0
= context
->Dr0
;
2197 amd64_thread_data()->dr1
= context
->Dr1
;
2198 amd64_thread_data()->dr2
= context
->Dr2
;
2199 amd64_thread_data()->dr3
= context
->Dr3
;
2200 amd64_thread_data()->dr6
= context
->Dr6
;
2201 amd64_thread_data()->dr7
= context
->Dr7
;
2204 return STATUS_SUCCESS
;
2208 /***********************************************************************
2209 * wow64_get_server_context_flags
2211 static unsigned int wow64_get_server_context_flags( DWORD flags
)
2213 unsigned int ret
= 0;
2215 flags
&= ~WOW64_CONTEXT_i386
; /* get rid of CPU id */
2216 if (flags
& WOW64_CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
2217 if (flags
& WOW64_CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
2218 if (flags
& WOW64_CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
2219 if (flags
& WOW64_CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
2220 if (flags
& WOW64_CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
2221 if (flags
& WOW64_CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
2225 /***********************************************************************
2226 * wow64_context_from_server
2228 static NTSTATUS
wow64_context_from_server( WOW64_CONTEXT
*to
, const context_t
*from
)
2230 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
2232 to
->ContextFlags
= WOW64_CONTEXT_i386
;
2233 if (from
->flags
& SERVER_CTX_CONTROL
)
2235 to
->ContextFlags
|= WOW64_CONTEXT_CONTROL
;
2236 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
2237 to
->Esp
= from
->ctl
.i386_regs
.esp
;
2238 to
->Eip
= from
->ctl
.i386_regs
.eip
;
2239 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
2240 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
2241 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
2243 if (from
->flags
& SERVER_CTX_INTEGER
)
2245 to
->ContextFlags
|= WOW64_CONTEXT_INTEGER
;
2246 to
->Eax
= from
->integer
.i386_regs
.eax
;
2247 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
2248 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
2249 to
->Edx
= from
->integer
.i386_regs
.edx
;
2250 to
->Esi
= from
->integer
.i386_regs
.esi
;
2251 to
->Edi
= from
->integer
.i386_regs
.edi
;
2253 if (from
->flags
& SERVER_CTX_SEGMENTS
)
2255 to
->ContextFlags
|= WOW64_CONTEXT_SEGMENTS
;
2256 to
->SegDs
= from
->seg
.i386_regs
.ds
;
2257 to
->SegEs
= from
->seg
.i386_regs
.es
;
2258 to
->SegFs
= from
->seg
.i386_regs
.fs
;
2259 to
->SegGs
= from
->seg
.i386_regs
.gs
;
2261 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
2263 to
->ContextFlags
|= WOW64_CONTEXT_FLOATING_POINT
;
2264 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
2265 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
2266 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
2267 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
2268 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
2269 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
2270 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
2271 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
2272 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
2274 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
2276 to
->ContextFlags
|= WOW64_CONTEXT_DEBUG_REGISTERS
;
2277 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
2278 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
2279 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
2280 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
2281 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
2282 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
2284 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
2286 to
->ContextFlags
|= WOW64_CONTEXT_EXTENDED_REGISTERS
;
2287 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
2289 return STATUS_SUCCESS
;
2292 /***********************************************************************
2293 * wow64_context_to_server
2295 static void wow64_context_to_server( context_t
*to
, const WOW64_CONTEXT
*from
)
2297 DWORD flags
= from
->ContextFlags
& ~WOW64_CONTEXT_i386
; /* get rid of CPU id */
2299 memset( to
, 0, sizeof(*to
) );
2302 if (flags
& WOW64_CONTEXT_CONTROL
)
2304 to
->flags
|= SERVER_CTX_CONTROL
;
2305 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
2306 to
->ctl
.i386_regs
.esp
= from
->Esp
;
2307 to
->ctl
.i386_regs
.eip
= from
->Eip
;
2308 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
2309 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
2310 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
2312 if (flags
& WOW64_CONTEXT_INTEGER
)
2314 to
->flags
|= SERVER_CTX_INTEGER
;
2315 to
->integer
.i386_regs
.eax
= from
->Eax
;
2316 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
2317 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
2318 to
->integer
.i386_regs
.edx
= from
->Edx
;
2319 to
->integer
.i386_regs
.esi
= from
->Esi
;
2320 to
->integer
.i386_regs
.edi
= from
->Edi
;
2322 if (flags
& WOW64_CONTEXT_SEGMENTS
)
2324 to
->flags
|= SERVER_CTX_SEGMENTS
;
2325 to
->seg
.i386_regs
.ds
= from
->SegDs
;
2326 to
->seg
.i386_regs
.es
= from
->SegEs
;
2327 to
->seg
.i386_regs
.fs
= from
->SegFs
;
2328 to
->seg
.i386_regs
.gs
= from
->SegGs
;
2330 if (flags
& WOW64_CONTEXT_FLOATING_POINT
)
2332 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
2333 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
2334 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
2335 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
2336 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
2337 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
2338 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
2339 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
2340 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
2341 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
2343 if (flags
& WOW64_CONTEXT_DEBUG_REGISTERS
)
2345 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
2346 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
2347 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
2348 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
2349 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
2350 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
2351 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
2353 if (flags
& WOW64_CONTEXT_EXTENDED_REGISTERS
)
2355 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
2356 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
2361 /******************************************************************************
2362 * RtlWow64GetThreadContext (NTDLL.@)
2364 NTSTATUS WINAPI
RtlWow64GetThreadContext( HANDLE handle
, WOW64_CONTEXT
*context
)
2368 context_t server_context
;
2369 unsigned int server_flags
= wow64_get_server_context_flags( context
->ContextFlags
);
2371 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
2372 if (self
) return STATUS_INVALID_PARAMETER
;
2373 return wow64_context_from_server( context
, &server_context
);
2377 /******************************************************************************
2378 * RtlWow64SetThreadContext (NTDLL.@)
2380 NTSTATUS WINAPI
RtlWow64SetThreadContext( HANDLE handle
, const WOW64_CONTEXT
*context
)
2383 context_t server_context
;
2385 wow64_context_to_server( &server_context
, context
);
2386 return set_thread_context( handle
, &server_context
, &self
);
2390 extern void raise_func_trampoline( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
2391 __ASM_GLOBAL_FUNC( raise_func_trampoline
,
2392 __ASM_CFI(".cfi_signal_frame\n\t")
2393 __ASM_CFI(".cfi_def_cfa %rbp,160\n\t") /* red zone + rip + rbp + rdi + rsi */
2394 __ASM_CFI(".cfi_rel_offset %rip,24\n\t")
2395 __ASM_CFI(".cfi_rel_offset %rbp,16\n\t")
2396 __ASM_CFI(".cfi_rel_offset %rdi,8\n\t")
2397 __ASM_CFI(".cfi_rel_offset %rsi,0\n\t")
2401 /***********************************************************************
2404 * Setup a proper stack frame for the raise function, and modify the
2405 * sigcontext so that the return from the signal handler will call
2406 * the raise function.
2408 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
2413 EXCEPTION_RECORD rec
;
2418 ULONG64 red_zone
[16];
2421 DWORD exception_code
= 0;
2423 stack
= (struct stack_layout
*)(RSP_sig(sigcontext
) & ~15);
2425 /* stack sanity checks */
2427 if (is_inside_signal_stack( stack
))
2429 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2430 GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
), (ULONG_PTR
)RSP_sig(sigcontext
),
2431 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2435 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
2436 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
2437 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
2439 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2440 GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
), (ULONG_PTR
)RSP_sig(sigcontext
),
2441 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2443 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
2445 /* stack overflow on last page, unrecoverable */
2446 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
2447 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2448 diff
, GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
),
2449 (ULONG_PTR
)RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
2450 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2453 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
2455 /* stack access below stack limit, may be recoverable */
2456 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
2459 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
2460 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2461 diff
, GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
),
2462 (ULONG_PTR
)RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
2463 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2468 stack
--; /* push the stack_layout structure */
2469 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
2470 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
2471 #elif defined(VALGRIND_MAKE_WRITABLE)
2472 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
2474 stack
->rec
.ExceptionRecord
= NULL
;
2475 stack
->rec
.ExceptionCode
= exception_code
;
2476 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2477 stack
->rec
.ExceptionAddress
= (void *)RIP_sig(sigcontext
);
2478 stack
->rec
.NumberParameters
= 0;
2479 save_context( &stack
->context
, sigcontext
);
2481 /* store return address and %rbp without aligning, so that the offset is fixed */
2482 rsp_ptr
= (ULONG64
*)RSP_sig(sigcontext
) - 16;
2483 *(--rsp_ptr
) = RIP_sig(sigcontext
);
2484 *(--rsp_ptr
) = RBP_sig(sigcontext
);
2485 *(--rsp_ptr
) = RDI_sig(sigcontext
);
2486 *(--rsp_ptr
) = RSI_sig(sigcontext
);
2488 /* now modify the sigcontext to return to the raise function */
2489 RIP_sig(sigcontext
) = (ULONG_PTR
)raise_func_trampoline
;
2490 RDI_sig(sigcontext
) = (ULONG_PTR
)&stack
->rec
;
2491 RSI_sig(sigcontext
) = (ULONG_PTR
)&stack
->context
;
2492 RDX_sig(sigcontext
) = (ULONG_PTR
)func
;
2493 RBP_sig(sigcontext
) = (ULONG_PTR
)rsp_ptr
;
2494 RSP_sig(sigcontext
) = (ULONG_PTR
)stack
;
2495 /* clear single-step, direction, and align check flag */
2496 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
2502 /***********************************************************************
2503 * get_exception_context
2505 * Get a pointer to the context built by setup_exception.
2507 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
2509 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
2513 /**********************************************************************
2514 * find_function_info
2516 static RUNTIME_FUNCTION
*find_function_info( ULONG64 pc
, HMODULE module
,
2517 RUNTIME_FUNCTION
*func
, ULONG size
)
2524 int pos
= (min
+ max
) / 2;
2525 if ((char *)pc
< (char *)module
+ func
[pos
].BeginAddress
) max
= pos
- 1;
2526 else if ((char *)pc
>= (char *)module
+ func
[pos
].EndAddress
) min
= pos
+ 1;
2530 while (func
->UnwindData
& 1) /* follow chained entry */
2531 func
= (RUNTIME_FUNCTION
*)((char *)module
+ (func
->UnwindData
& ~1));
2538 /**********************************************************************
2539 * lookup_function_info
2541 static RUNTIME_FUNCTION
*lookup_function_info( ULONG64 pc
, ULONG64
*base
, LDR_MODULE
**module
)
2543 RUNTIME_FUNCTION
*func
= NULL
;
2544 struct dynamic_unwind_entry
*entry
;
2547 /* PE module or wine module */
2548 if (!LdrFindEntryForAddress( (void *)pc
, module
))
2550 *base
= (ULONG64
)(*module
)->BaseAddress
;
2551 if ((func
= RtlImageDirectoryEntryToData( (*module
)->BaseAddress
, TRUE
,
2552 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
2554 /* lookup in function table */
2555 func
= find_function_info( pc
, (*module
)->BaseAddress
, func
, size
/sizeof(*func
) );
2562 RtlEnterCriticalSection( &dynamic_unwind_section
);
2563 LIST_FOR_EACH_ENTRY( entry
, &dynamic_unwind_list
, struct dynamic_unwind_entry
, entry
)
2565 if (pc
>= entry
->base
&& pc
< entry
->end
)
2567 *base
= entry
->base
;
2569 /* use callback or lookup in function table */
2570 if (entry
->callback
)
2571 func
= entry
->callback( pc
, entry
->context
);
2573 func
= find_function_info( pc
, (HMODULE
)entry
->base
, entry
->table
, entry
->count
);
2577 RtlLeaveCriticalSection( &dynamic_unwind_section
);
2583 static DWORD __cdecl
nested_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
2584 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
2586 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)) return ExceptionContinueSearch
;
2589 return ExceptionNestedException
;
2592 /**********************************************************************
2595 * Call a single exception handler.
2596 * FIXME: Handle nested exceptions.
2598 static DWORD
call_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
)
2600 EXCEPTION_REGISTRATION_RECORD frame
;
2603 frame
.Handler
= nested_exception_handler
;
2604 __wine_push_frame( &frame
);
2606 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2607 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2608 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, context
, dispatch
);
2609 TRACE( "handler at %p returned %u\n", dispatch
->LanguageHandler
, res
);
2611 __wine_pop_frame( &frame
);
2616 /**********************************************************************
2619 * Call a single exception handler from the TEB chain.
2620 * FIXME: Handle nested exceptions.
2622 static DWORD
call_teb_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
,
2623 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
2627 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
2628 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
2629 res
= teb_frame
->Handler( rec
, teb_frame
, context
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
2630 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
2635 /**********************************************************************
2636 * call_stack_handlers
2638 * Call the stack handlers chain.
2640 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
2642 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2643 UNWIND_HISTORY_TABLE table
;
2644 DISPATCHER_CONTEXT dispatch
;
2649 context
= *orig_context
;
2650 dispatch
.TargetIp
= 0;
2651 dispatch
.ContextRecord
= &context
;
2652 dispatch
.HistoryTable
= &table
;
2655 /* FIXME: should use the history table to make things faster */
2657 dispatch
.ImageBase
= 0;
2658 dispatch
.ControlPc
= context
.Rip
;
2659 dispatch
.ScopeIndex
= 0;
2661 /* first look for PE exception information */
2663 if ((dispatch
.FunctionEntry
= lookup_function_info( dispatch
.ControlPc
, &dispatch
.ImageBase
, &module
)))
2665 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_EHANDLER
, dispatch
.ImageBase
,
2666 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2667 &context
, &dispatch
.HandlerData
,
2668 &dispatch
.EstablisherFrame
, NULL
);
2672 /* then look for host system exception information */
2674 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
2676 BOOL got_info
= FALSE
;
2677 struct dwarf_eh_bases bases
;
2678 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(dispatch
.ControlPc
- 1), &bases
);
2682 status
= dwarf_virtual_unwind( dispatch
.ControlPc
, &dispatch
.EstablisherFrame
, &context
,
2683 fde
, &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2684 if (status
!= STATUS_SUCCESS
) return status
;
2687 #ifdef HAVE_LIBUNWIND_H
2690 status
= libunwind_virtual_unwind( dispatch
.ControlPc
, &got_info
, &dispatch
.EstablisherFrame
, &context
,
2691 &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2692 if (status
!= STATUS_SUCCESS
) return status
;
2698 dispatch
.FunctionEntry
= NULL
;
2699 if (dispatch
.LanguageHandler
&& !module
)
2701 FIXME( "calling personality routine in system library not supported yet\n" );
2702 dispatch
.LanguageHandler
= NULL
;
2707 else WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
2709 context
.Rip
= *(ULONG64
*)context
.Rsp
;
2710 context
.Rsp
= context
.Rsp
+ sizeof(ULONG64
);
2711 dispatch
.EstablisherFrame
= context
.Rsp
;
2712 dispatch
.LanguageHandler
= NULL
;
2715 if (!dispatch
.EstablisherFrame
) break;
2717 if ((dispatch
.EstablisherFrame
& 7) ||
2718 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
2719 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
2721 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
2722 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2723 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
2727 if (dispatch
.LanguageHandler
)
2729 switch (call_handler( rec
, orig_context
, &dispatch
))
2731 case ExceptionContinueExecution
:
2732 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
2733 return STATUS_SUCCESS
;
2734 case ExceptionContinueSearch
:
2736 case ExceptionNestedException
:
2737 FIXME( "nested exception\n" );
2739 case ExceptionCollidedUnwind
: {
2742 context
= *dispatch
.ContextRecord
;
2743 dispatch
.ContextRecord
= &context
;
2744 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
2745 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2746 &context
, NULL
, &frame
, NULL
);
2750 return STATUS_INVALID_DISPOSITION
;
2753 /* hack: call wine handlers registered in the tib list */
2754 else while ((ULONG64
)teb_frame
< context
.Rsp
)
2756 TRACE( "found wine frame %p rsp %lx handler %p\n",
2757 teb_frame
, context
.Rsp
, teb_frame
->Handler
);
2758 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
2759 switch (call_teb_handler( rec
, orig_context
, &dispatch
, teb_frame
))
2761 case ExceptionContinueExecution
:
2762 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
2763 return STATUS_SUCCESS
;
2764 case ExceptionContinueSearch
:
2766 case ExceptionNestedException
:
2767 FIXME( "nested exception\n" );
2769 case ExceptionCollidedUnwind
: {
2772 context
= *dispatch
.ContextRecord
;
2773 dispatch
.ContextRecord
= &context
;
2774 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
2775 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2776 &context
, NULL
, &frame
, NULL
);
2777 teb_frame
= teb_frame
->Prev
;
2781 return STATUS_INVALID_DISPOSITION
;
2783 teb_frame
= teb_frame
->Prev
;
2786 if (context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
2788 return STATUS_UNHANDLED_EXCEPTION
;
2792 /*******************************************************************
2793 * NtRaiseException (NTDLL.@)
2795 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2803 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2804 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
2805 context
->Rip
, GetCurrentThreadId() );
2806 for (c
= 0; c
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); c
++)
2807 TRACE( " info[%d]=%016lx\n", c
, rec
->ExceptionInformation
[c
] );
2808 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
2810 if (rec
->ExceptionInformation
[1] >> 16)
2811 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2812 rec
->ExceptionAddress
,
2813 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
2815 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2816 rec
->ExceptionAddress
,
2817 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
2821 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2822 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
2823 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2824 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
2825 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2826 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
2827 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2828 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
2830 status
= send_debug_event( rec
, TRUE
, context
);
2831 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
) goto done
;
2833 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
2834 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Rip
--;
2836 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
) goto done
;
2838 if ((status
= call_stack_handlers( rec
, context
)) == STATUS_SUCCESS
) goto done
;
2839 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
2842 /* last chance exception */
2844 status
= send_debug_event( rec
, FALSE
, context
);
2845 if (status
!= DBG_CONTINUE
)
2847 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
2848 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2849 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
2850 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2852 ERR("Unhandled exception code %x flags %x addr %p\n",
2853 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
2854 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
2858 return NtSetContextThread( GetCurrentThread(), context
);
2862 /**********************************************************************
2863 * raise_segv_exception
2865 static void raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2869 switch(rec
->ExceptionCode
)
2871 case EXCEPTION_ACCESS_VIOLATION
:
2872 if (rec
->NumberParameters
== 2)
2874 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
2875 rec
->ExceptionInformation
[0], FALSE
)))
2876 set_cpu_context( context
);
2879 case EXCEPTION_BREAKPOINT
:
2880 switch (rec
->ExceptionInformation
[0])
2882 case 1: /* BREAKPOINT_PRINT */
2883 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2884 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2885 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2886 set_cpu_context( context
);
2890 status
= NtRaiseException( rec
, context
, TRUE
);
2891 raise_status( status
, rec
);
2895 /**********************************************************************
2896 * raise_trap_exception
2898 static void raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2902 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
2904 /* when single stepping can't tell whether this is a hw bp or a
2905 * single step interrupt. try to avoid as much overhead as possible
2906 * and only do a server call if there is any hw bp enabled. */
2908 if( !(context
->EFlags
& 0x100) || (amd64_thread_data()->dr7
& 0xff) )
2910 /* (possible) hardware breakpoint, fetch the debug registers */
2911 DWORD saved_flags
= context
->ContextFlags
;
2912 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
2913 NtGetContextThread(GetCurrentThread(), context
);
2914 context
->ContextFlags
|= saved_flags
; /* restore flags */
2917 context
->EFlags
&= ~0x100; /* clear single-step flag */
2920 status
= NtRaiseException( rec
, context
, TRUE
);
2921 raise_status( status
, rec
);
2925 /**********************************************************************
2926 * raise_generic_exception
2928 * Generic raise function for exceptions that don't need special treatment.
2930 static void raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2932 NTSTATUS status
= NtRaiseException( rec
, context
, TRUE
);
2933 raise_status( status
, rec
);
2937 /***********************************************************************
2938 * is_privileged_instr
2940 * Check if the fault location is a privileged instruction.
2942 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
2944 const BYTE
*instr
= (BYTE
*)context
->Rip
;
2945 unsigned int prefix_count
= 0;
2947 for (;;) switch(*instr
)
2949 /* instruction prefixes */
2950 case 0x2e: /* %cs: */
2951 case 0x36: /* %ss: */
2952 case 0x3e: /* %ds: */
2953 case 0x26: /* %es: */
2954 case 0x40: /* rex */
2955 case 0x41: /* rex */
2956 case 0x42: /* rex */
2957 case 0x43: /* rex */
2958 case 0x44: /* rex */
2959 case 0x45: /* rex */
2960 case 0x46: /* rex */
2961 case 0x47: /* rex */
2962 case 0x48: /* rex */
2963 case 0x49: /* rex */
2964 case 0x4a: /* rex */
2965 case 0x4b: /* rex */
2966 case 0x4c: /* rex */
2967 case 0x4d: /* rex */
2968 case 0x4e: /* rex */
2969 case 0x4f: /* rex */
2970 case 0x64: /* %fs: */
2971 case 0x65: /* %gs: */
2972 case 0x66: /* opcode size */
2973 case 0x67: /* addr size */
2974 case 0xf0: /* lock */
2975 case 0xf2: /* repne */
2976 case 0xf3: /* repe */
2977 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
2981 case 0x0f: /* extended instruction */
2984 case 0x06: /* clts */
2985 case 0x08: /* invd */
2986 case 0x09: /* wbinvd */
2987 case 0x20: /* mov crX, reg */
2988 case 0x21: /* mov drX, reg */
2989 case 0x22: /* mov reg, crX */
2990 case 0x23: /* mov reg drX */
2991 return EXCEPTION_PRIV_INSTRUCTION
;
2994 case 0x6c: /* insb (%dx) */
2995 case 0x6d: /* insl (%dx) */
2996 case 0x6e: /* outsb (%dx) */
2997 case 0x6f: /* outsl (%dx) */
2998 case 0xcd: /* int $xx */
2999 case 0xe4: /* inb al,XX */
3000 case 0xe5: /* in (e)ax,XX */
3001 case 0xe6: /* outb XX,al */
3002 case 0xe7: /* out XX,(e)ax */
3003 case 0xec: /* inb (%dx),%al */
3004 case 0xed: /* inl (%dx),%eax */
3005 case 0xee: /* outb %al,(%dx) */
3006 case 0xef: /* outl %eax,(%dx) */
3007 case 0xf4: /* hlt */
3008 case 0xfa: /* cli */
3009 case 0xfb: /* sti */
3010 return EXCEPTION_PRIV_INSTRUCTION
;
3017 /***********************************************************************
3020 * Handle an interrupt.
3022 static inline BOOL
handle_interrupt( unsigned int interrupt
, EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
3027 rec
->ExceptionCode
= STATUS_ASSERTION_FAILURE
;
3031 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
3032 rec
->ExceptionAddress
= (void *)context
->Rip
;
3033 rec
->NumberParameters
= 1;
3034 rec
->ExceptionInformation
[0] = context
->Rax
;
3042 /**********************************************************************
3045 * Handler for SIGSEGV and related errors.
3047 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3049 EXCEPTION_RECORD
*rec
;
3050 ucontext_t
*ucontext
= sigcontext
;
3052 /* check for page fault inside the thread stack */
3053 if (TRAP_sig(ucontext
) == TRAP_x86_PAGEFLT
&&
3054 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
3055 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
3056 virtual_handle_stack_fault( siginfo
->si_addr
))
3058 /* check if this was the last guard page */
3059 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
3061 rec
= setup_exception( sigcontext
, raise_segv_exception
);
3062 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
3067 rec
= setup_exception( sigcontext
, raise_segv_exception
);
3068 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
3070 switch(TRAP_sig(ucontext
))
3072 case TRAP_x86_OFLOW
: /* Overflow exception */
3073 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
3075 case TRAP_x86_BOUND
: /* Bound range exception */
3076 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
3078 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
3079 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
3081 case TRAP_x86_STKFLT
: /* Stack fault */
3082 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
3084 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
3085 case TRAP_x86_PROTFLT
: /* General protection fault */
3086 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
3088 CONTEXT
*win_context
= get_exception_context( rec
);
3089 WORD err
= ERROR_sig(ucontext
);
3090 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( win_context
))) break;
3091 if ((err
& 7) == 2 && handle_interrupt( err
>> 3, rec
, win_context
)) break;
3092 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
3093 rec
->NumberParameters
= 2;
3094 rec
->ExceptionInformation
[0] = 0;
3095 rec
->ExceptionInformation
[1] = 0xffffffffffffffff;
3098 case TRAP_x86_PAGEFLT
: /* Page fault */
3099 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
3100 rec
->NumberParameters
= 2;
3101 rec
->ExceptionInformation
[0] = (ERROR_sig(ucontext
) >> 1) & 0x09;
3102 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
3104 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
3105 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
3108 ERR( "Got unexpected trap %ld\n", (ULONG_PTR
)TRAP_sig(ucontext
) );
3110 case TRAP_x86_NMI
: /* NMI interrupt */
3111 case TRAP_x86_DNA
: /* Device not available exception */
3112 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
3113 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
3114 case TRAP_x86_MCHK
: /* Machine check exception */
3115 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
3116 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
3121 /**********************************************************************
3124 * Handler for SIGTRAP.
3126 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3128 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_trap_exception
);
3130 switch (siginfo
->si_code
)
3132 case TRAP_TRACE
: /* Single-step exception */
3133 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
3134 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
3136 case TRAP_BRKPT
: /* Breakpoint exception */
3140 /* Check if this is actually icebp instruction */
3141 if (((unsigned char *)rec
->ExceptionAddress
)[-1] == 0xF1)
3143 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
3146 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
3149 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
3150 rec
->NumberParameters
= 1;
3151 rec
->ExceptionInformation
[0] = 0;
3156 /**********************************************************************
3159 * Handler for SIGFPE.
3161 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3163 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
3165 switch (siginfo
->si_code
)
3168 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
3171 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
3174 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
3177 rec
->ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3180 rec
->ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
3183 rec
->ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
3186 rec
->ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
3190 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
3195 /**********************************************************************
3198 * Handler for SIGINT.
3200 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3202 if (!dispatch_signal(SIGINT
))
3204 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
3205 rec
->ExceptionCode
= CONTROL_C_EXIT
;
3210 /**********************************************************************
3213 * Handler for SIGABRT.
3215 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3217 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
3218 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
3219 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
3223 /**********************************************************************
3226 * Handler for SIGQUIT.
3228 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
3234 /**********************************************************************
3237 * Handler for SIGUSR1, used to signal a thread that it got suspended.
3239 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
3243 save_context( &context
, ucontext
);
3244 wait_suspend( &context
);
3245 restore_context( &context
, ucontext
);
3249 /***********************************************************************
3250 * __wine_set_signal_handler (NTDLL.@)
3252 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
3254 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
3255 if (handlers
[sig
] != NULL
) return -2;
3256 handlers
[sig
] = wsh
;
3261 /**********************************************************************
3262 * signal_alloc_thread
3264 NTSTATUS
signal_alloc_thread( TEB
**teb
)
3266 static size_t sigstack_alignment
;
3270 if (!sigstack_alignment
)
3272 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
3273 /* find the first power of two not smaller than min_size */
3274 sigstack_alignment
= 12;
3275 while ((1u << sigstack_alignment
) < min_size
) sigstack_alignment
++;
3276 signal_stack_size
= (1 << sigstack_alignment
) - teb_size
;
3277 assert( sizeof(TEB
) <= teb_size
);
3280 size
= 1 << sigstack_alignment
;
3282 if (!(status
= virtual_alloc_aligned( (void **)teb
, 0, &size
, MEM_COMMIT
| MEM_TOP_DOWN
,
3283 PAGE_READWRITE
, sigstack_alignment
)))
3285 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
3286 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
3292 /**********************************************************************
3293 * signal_free_thread
3295 void signal_free_thread( TEB
*teb
)
3299 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
3303 /**********************************************************************
3306 static void *mac_thread_gsbase(void)
3308 static int gsbase_offset
= -1;
3311 if (gsbase_offset
< 0)
3313 /* Search for the array of TLS slots within the pthread data structure.
3314 That's what the macOS pthread implementation uses for gsbase. */
3315 const void* const sentinel1
= (const void*)0x2bffb6b4f11228ae;
3316 const void* const sentinel2
= (const void*)0x0845a7ff6ab76707;
3319 const void** p
= (const void**)pthread_self();
3323 if ((rc
= pthread_key_create(&key
, NULL
)))
3325 ERR("failed to create sentinel key for gsbase search: %d\n", rc
);
3329 pthread_setspecific(key
, sentinel1
);
3331 for (i
= key
+ 1; i
< 2000; i
++) /* arbitrary limit */
3333 if (p
[i
] == sentinel1
)
3335 pthread_setspecific(key
, sentinel2
);
3337 if (p
[i
] == sentinel2
)
3339 gsbase_offset
= (i
- key
) * sizeof(*p
);
3343 pthread_setspecific(key
, sentinel1
);
3347 pthread_key_delete(key
);
3352 ret
= (char*)pthread_self() + gsbase_offset
;
3353 TRACE("pthread_self() %p + offset 0x%08x -> gsbase %p\n", pthread_self(), gsbase_offset
, ret
);
3358 ERR("failed to locate gsbase; won't be able to poke ThreadLocalStoragePointer into pthread TLS; expect crashes\n");
3366 /**********************************************************************
3367 * signal_init_thread
3369 void signal_init_thread( TEB
*teb
)
3371 const WORD fpu_cw
= 0x27f;
3374 #if defined __linux__
3375 arch_prctl( ARCH_SET_GS
, teb
);
3376 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
3377 amd64_set_gsbase( teb
);
3378 #elif defined(__NetBSD__)
3379 sysarch( X86_64_SET_GSBASE
, &teb
);
3380 #elif defined (__APPLE__)
3381 __asm__
volatile (".byte 0x65\n\tmovq %0,%c1"
3383 : "r" (teb
->Tib
.Self
), "n" (FIELD_OFFSET(TEB
, Tib
.Self
)));
3384 __asm__
volatile (".byte 0x65\n\tmovq %0,%c1"
3386 : "r" (teb
->ThreadLocalStoragePointer
), "n" (FIELD_OFFSET(TEB
, ThreadLocalStoragePointer
)));
3388 /* alloc_tls_slot() needs to poke a value to an address relative to each
3389 thread's gsbase. Have each thread record its gsbase pointer into its
3390 TEB so alloc_tls_slot() can find it. */
3391 teb
->Reserved5
[0] = mac_thread_gsbase();
3393 # error Please define setting %gs for your architecture
3396 ss
.ss_sp
= (char *)teb
+ teb_size
;
3397 ss
.ss_size
= signal_stack_size
;
3399 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
3402 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
3404 FIXME("FPU setup not implemented for this platform.\n");
3408 /**********************************************************************
3409 * signal_init_process
3411 void signal_init_process(void)
3413 struct sigaction sig_act
;
3415 sig_act
.sa_mask
= server_block_set
;
3416 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
| SA_ONSTACK
;
3418 sig_act
.sa_sigaction
= int_handler
;
3419 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
3420 sig_act
.sa_sigaction
= fpe_handler
;
3421 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
3422 sig_act
.sa_sigaction
= abrt_handler
;
3423 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
3424 sig_act
.sa_sigaction
= quit_handler
;
3425 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
3426 sig_act
.sa_sigaction
= usr1_handler
;
3427 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
3429 sig_act
.sa_sigaction
= segv_handler
;
3430 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
3431 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
3433 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
3437 sig_act
.sa_sigaction
= trap_handler
;
3438 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
3443 perror("sigaction");
3448 /**********************************************************************
3449 * RtlAddFunctionTable (NTDLL.@)
3451 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, DWORD64 addr
)
3453 struct dynamic_unwind_entry
*entry
;
3455 TRACE( "%p %u %lx\n", table
, count
, addr
);
3457 /* NOTE: Windows doesn't check if table is aligned or a NULL pointer */
3459 entry
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry
) );
3464 entry
->end
= addr
+ (count
? table
[count
- 1].EndAddress
: 0);
3465 entry
->table
= table
;
3466 entry
->count
= count
;
3467 entry
->max_count
= 0;
3468 entry
->callback
= NULL
;
3469 entry
->context
= NULL
;
3471 RtlEnterCriticalSection( &dynamic_unwind_section
);
3472 list_add_tail( &dynamic_unwind_list
, &entry
->entry
);
3473 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3479 /**********************************************************************
3480 * RtlInstallFunctionTableCallback (NTDLL.@)
3482 BOOLEAN CDECL
RtlInstallFunctionTableCallback( DWORD64 table
, DWORD64 base
, DWORD length
,
3483 PGET_RUNTIME_FUNCTION_CALLBACK callback
, PVOID context
, PCWSTR dll
)
3485 struct dynamic_unwind_entry
*entry
;
3487 TRACE( "%lx %lx %d %p %p %s\n", table
, base
, length
, callback
, context
, wine_dbgstr_w(dll
) );
3489 /* NOTE: Windows doesn't check if the provided callback is a NULL pointer */
3491 /* both low-order bits must be set */
3492 if ((table
& 0x3) != 0x3)
3495 entry
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry
) );
3500 entry
->end
= base
+ length
;
3501 entry
->table
= (RUNTIME_FUNCTION
*)table
;
3503 entry
->max_count
= 0;
3504 entry
->callback
= callback
;
3505 entry
->context
= context
;
3507 RtlEnterCriticalSection( &dynamic_unwind_section
);
3508 list_add_tail( &dynamic_unwind_list
, &entry
->entry
);
3509 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3515 /*************************************************************************
3516 * RtlAddGrowableFunctionTable (NTDLL.@)
3518 DWORD WINAPI
RtlAddGrowableFunctionTable( void **table
, RUNTIME_FUNCTION
*functions
, DWORD count
, DWORD max_count
,
3519 ULONG_PTR base
, ULONG_PTR end
)
3521 struct dynamic_unwind_entry
*entry
;
3523 TRACE( "%p, %p, %u, %u, %lx, %lx\n", table
, functions
, count
, max_count
, base
, end
);
3525 entry
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry
) );
3527 return STATUS_NO_MEMORY
;
3531 entry
->table
= functions
;
3532 entry
->count
= count
;
3533 entry
->max_count
= max_count
;
3534 entry
->callback
= NULL
;
3535 entry
->context
= NULL
;
3537 RtlEnterCriticalSection( &dynamic_unwind_section
);
3538 list_add_tail( &dynamic_unwind_list
, &entry
->entry
);
3539 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3543 return STATUS_SUCCESS
;
3547 /*************************************************************************
3548 * RtlGrowFunctionTable (NTDLL.@)
3550 void WINAPI
RtlGrowFunctionTable( void *table
, DWORD count
)
3552 struct dynamic_unwind_entry
*entry
;
3554 TRACE( "%p, %u\n", table
, count
);
3556 RtlEnterCriticalSection( &dynamic_unwind_section
);
3557 LIST_FOR_EACH_ENTRY( entry
, &dynamic_unwind_list
, struct dynamic_unwind_entry
, entry
)
3561 if (count
> entry
->count
&& count
<= entry
->max_count
)
3562 entry
->count
= count
;
3566 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3570 /*************************************************************************
3571 * RtlDeleteGrowableFunctionTable (NTDLL.@)
3573 void WINAPI
RtlDeleteGrowableFunctionTable( void *table
)
3575 struct dynamic_unwind_entry
*entry
, *to_free
= NULL
;
3577 TRACE( "%p\n", table
);
3579 RtlEnterCriticalSection( &dynamic_unwind_section
);
3580 LIST_FOR_EACH_ENTRY( entry
, &dynamic_unwind_list
, struct dynamic_unwind_entry
, entry
)
3585 list_remove( &entry
->entry
);
3589 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3591 RtlFreeHeap( GetProcessHeap(), 0, to_free
);
3595 /**********************************************************************
3596 * RtlDeleteFunctionTable (NTDLL.@)
3598 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
3600 struct dynamic_unwind_entry
*entry
, *to_free
= NULL
;
3602 TRACE( "%p\n", table
);
3604 RtlEnterCriticalSection( &dynamic_unwind_section
);
3605 LIST_FOR_EACH_ENTRY( entry
, &dynamic_unwind_list
, struct dynamic_unwind_entry
, entry
)
3607 if (entry
->table
== table
)
3610 list_remove( &entry
->entry
);
3614 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3619 RtlFreeHeap( GetProcessHeap(), 0, to_free
);
3624 /**********************************************************************
3625 * RtlLookupFunctionEntry (NTDLL.@)
3627 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG64 pc
, ULONG64
*base
, UNWIND_HISTORY_TABLE
*table
)
3630 RUNTIME_FUNCTION
*func
;
3632 /* FIXME: should use the history table to make things faster */
3634 func
= lookup_function_info( pc
, base
, &module
);
3639 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
3641 WARN( "module not found for %lx\n", pc
);
3647 static ULONG64
get_int_reg( CONTEXT
*context
, int reg
)
3649 return *(&context
->Rax
+ reg
);
3652 static void set_int_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, ULONG64
*val
)
3654 *(&context
->Rax
+ reg
) = *val
;
3655 if (ctx_ptr
) ctx_ptr
->u2
.IntegerContext
[reg
] = val
;
3658 static void set_float_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, M128A
*val
)
3660 *(&context
->u
.s
.Xmm0
+ reg
) = *val
;
3661 if (ctx_ptr
) ctx_ptr
->u
.FloatingContext
[reg
] = val
;
3664 static int get_opcode_size( struct opcode op
)
3668 case UWOP_ALLOC_LARGE
:
3669 return 2 + (op
.info
!= 0);
3670 case UWOP_SAVE_NONVOL
:
3671 case UWOP_SAVE_XMM128
:
3673 case UWOP_SAVE_NONVOL_FAR
:
3674 case UWOP_SAVE_XMM128_FAR
:
3681 static BOOL
is_inside_epilog( BYTE
*pc
, ULONG64 base
, const RUNTIME_FUNCTION
*function
)
3683 /* add or lea must be the first instruction, and it must have a rex.W prefix */
3684 if ((pc
[0] & 0xf8) == 0x48)
3688 case 0x81: /* add $nnnn,%rsp */
3689 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
3695 case 0x83: /* add $n,%rsp */
3696 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
3702 case 0x8d: /* lea n(reg),%rsp */
3703 if (pc
[0] & 0x06) return FALSE
; /* rex.RX must be cleared */
3704 if (((pc
[2] >> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
3705 if ((pc
[2] & 7) == 4) return FALSE
; /* no SIB byte allowed */
3706 if ((pc
[2] >> 6) == 1) /* 8-bit offset */
3711 if ((pc
[2] >> 6) == 2) /* 32-bit offset */
3720 /* now check for various pop instructions */
3724 if ((*pc
& 0xf0) == 0x40) pc
++; /* rex prefix */
3728 case 0x58: /* pop %rax/%r8 */
3729 case 0x59: /* pop %rcx/%r9 */
3730 case 0x5a: /* pop %rdx/%r10 */
3731 case 0x5b: /* pop %rbx/%r11 */
3732 case 0x5c: /* pop %rsp/%r12 */
3733 case 0x5d: /* pop %rbp/%r13 */
3734 case 0x5e: /* pop %rsi/%r14 */
3735 case 0x5f: /* pop %rdi/%r15 */
3738 case 0xc2: /* ret $nn */
3739 case 0xc3: /* ret */
3741 case 0xe9: /* jmp nnnn */
3742 pc
+= 5 + *(LONG
*)(pc
+ 1);
3743 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
3746 case 0xeb: /* jmp n */
3747 pc
+= 2 + (signed char)pc
[1];
3748 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
3751 case 0xf3: /* rep; ret (for amd64 prediction bug) */
3752 return pc
[1] == 0xc3;
3758 /* execute a function epilog, which must have been validated with is_inside_epilog() */
3759 static void interpret_epilog( BYTE
*pc
, CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
3765 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
3769 case 0x58: /* pop %rax/r8 */
3770 case 0x59: /* pop %rcx/r9 */
3771 case 0x5a: /* pop %rdx/r10 */
3772 case 0x5b: /* pop %rbx/r11 */
3773 case 0x5c: /* pop %rsp/r12 */
3774 case 0x5d: /* pop %rbp/r13 */
3775 case 0x5e: /* pop %rsi/r14 */
3776 case 0x5f: /* pop %rdi/r15 */
3777 set_int_reg( context
, ctx_ptr
, *pc
- 0x58 + (rex
& 1) * 8, (ULONG64
*)context
->Rsp
);
3778 context
->Rsp
+= sizeof(ULONG64
);
3781 case 0x81: /* add $nnnn,%rsp */
3782 context
->Rsp
+= *(LONG
*)(pc
+ 2);
3783 pc
+= 2 + sizeof(LONG
);
3785 case 0x83: /* add $n,%rsp */
3786 context
->Rsp
+= (signed char)pc
[2];
3790 if ((pc
[1] >> 6) == 1) /* lea n(reg),%rsp */
3792 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + (signed char)pc
[2];
3795 else /* lea nnnn(reg),%rsp */
3797 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + *(LONG
*)(pc
+ 2);
3798 pc
+= 2 + sizeof(LONG
);
3801 case 0xc2: /* ret $nn */
3802 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3803 context
->Rsp
+= sizeof(ULONG64
) + *(WORD
*)(pc
+ 1);
3805 case 0xc3: /* ret */
3806 case 0xf3: /* rep; ret */
3807 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3808 context
->Rsp
+= sizeof(ULONG64
);
3810 case 0xe9: /* jmp nnnn */
3811 pc
+= 5 + *(LONG
*)(pc
+ 1);
3813 case 0xeb: /* jmp n */
3814 pc
+= 2 + (signed char)pc
[1];
3821 /**********************************************************************
3822 * RtlVirtualUnwind (NTDLL.@)
3824 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG64 base
, ULONG64 pc
,
3825 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
3826 PVOID
*data
, ULONG64
*frame_ret
,
3827 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
3829 union handler_data
*handler_data
;
3831 struct UNWIND_INFO
*info
;
3832 unsigned int i
, prolog_offset
;
3834 TRACE( "type %x rip %lx rsp %lx\n", type
, pc
, context
->Rsp
);
3835 if (TRACE_ON(seh
)) dump_unwind_info( base
, function
);
3837 frame
= *frame_ret
= context
->Rsp
;
3840 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
3841 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
3843 if (info
->version
!= 1)
3845 FIXME( "unknown unwind info version %u at %p\n", info
->version
, info
);
3849 if (info
->frame_reg
)
3850 frame
= get_int_reg( context
, info
->frame_reg
) - info
->frame_offset
* 16;
3852 /* check if in prolog */
3853 if (pc
>= base
+ function
->BeginAddress
&& pc
< base
+ function
->BeginAddress
+ info
->prolog
)
3855 prolog_offset
= pc
- base
- function
->BeginAddress
;
3860 if (is_inside_epilog( (BYTE
*)pc
, base
, function
))
3862 interpret_epilog( (BYTE
*)pc
, context
, ctx_ptr
);
3868 for (i
= 0; i
< info
->count
; i
+= get_opcode_size(info
->opcodes
[i
]))
3870 if (prolog_offset
< info
->opcodes
[i
].offset
) continue; /* skip it */
3872 switch (info
->opcodes
[i
].code
)
3874 case UWOP_PUSH_NONVOL
: /* pushq %reg */
3875 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)context
->Rsp
);
3876 context
->Rsp
+= sizeof(ULONG64
);
3878 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
3879 if (info
->opcodes
[i
].info
) context
->Rsp
+= *(DWORD
*)&info
->opcodes
[i
+1];
3880 else context
->Rsp
+= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
3882 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
3883 context
->Rsp
+= (info
->opcodes
[i
].info
+ 1) * 8;
3885 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
3886 context
->Rsp
= *frame_ret
= frame
;
3888 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
3889 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 8;
3890 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
3892 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
3893 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
3894 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
3896 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
3897 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 16;
3898 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
3900 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
3901 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
3902 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
3904 case UWOP_PUSH_MACHFRAME
:
3905 FIXME( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
3908 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
3913 if (!(info
->flags
& UNW_FLAG_CHAININFO
)) break;
3914 function
= &handler_data
->chain
; /* restart with the chained info */
3917 /* now pop return address */
3918 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3919 context
->Rsp
+= sizeof(ULONG64
);
3921 if (!(info
->flags
& type
)) return NULL
; /* no matching handler */
3922 if (prolog_offset
!= ~0) return NULL
; /* inside prolog */
3924 *data
= &handler_data
->handler
+ 1;
3925 return (char *)base
+ handler_data
->handler
;
3928 struct unwind_exception_frame
3930 EXCEPTION_REGISTRATION_RECORD frame
;
3931 DISPATCHER_CONTEXT
*dispatch
;
3934 /**********************************************************************
3935 * unwind_exception_handler
3937 * Handler for exceptions happening while calling an unwind handler.
3939 static DWORD __cdecl
unwind_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
3940 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
3942 struct unwind_exception_frame
*unwind_frame
= (struct unwind_exception_frame
*)frame
;
3943 DISPATCHER_CONTEXT
*dispatch
= (DISPATCHER_CONTEXT
*)dispatcher
;
3945 /* copy the original dispatcher into the current one, except for the TargetIp */
3946 dispatch
->ControlPc
= unwind_frame
->dispatch
->ControlPc
;
3947 dispatch
->ImageBase
= unwind_frame
->dispatch
->ImageBase
;
3948 dispatch
->FunctionEntry
= unwind_frame
->dispatch
->FunctionEntry
;
3949 dispatch
->EstablisherFrame
= unwind_frame
->dispatch
->EstablisherFrame
;
3950 dispatch
->ContextRecord
= unwind_frame
->dispatch
->ContextRecord
;
3951 dispatch
->LanguageHandler
= unwind_frame
->dispatch
->LanguageHandler
;
3952 dispatch
->HandlerData
= unwind_frame
->dispatch
->HandlerData
;
3953 dispatch
->HistoryTable
= unwind_frame
->dispatch
->HistoryTable
;
3954 dispatch
->ScopeIndex
= unwind_frame
->dispatch
->ScopeIndex
;
3955 TRACE( "detected collided unwind\n" );
3956 return ExceptionCollidedUnwind
;
3959 /**********************************************************************
3960 * call_unwind_handler
3962 * Call a single unwind handler.
3964 static DWORD
call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
3966 struct unwind_exception_frame frame
;
3969 frame
.frame
.Handler
= unwind_exception_handler
;
3970 frame
.dispatch
= dispatch
;
3971 __wine_push_frame( &frame
.frame
);
3973 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
3974 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
3975 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
3976 TRACE( "handler %p returned %x\n", dispatch
->LanguageHandler
, res
);
3978 __wine_pop_frame( &frame
.frame
);
3982 case ExceptionContinueSearch
:
3983 case ExceptionCollidedUnwind
:
3986 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
3994 /**********************************************************************
3995 * call_teb_unwind_handler
3997 * Call a single unwind handler from the TEB chain.
3999 static DWORD
call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
4000 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
4004 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
4005 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
4006 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
4007 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
4011 case ExceptionContinueSearch
:
4012 case ExceptionCollidedUnwind
:
4015 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
4023 /**********************************************************************
4024 * call_consolidate_callback
4026 * Wrapper function to call a consolidate callback from a fake frame.
4027 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
4028 * we have to skip all frames which were already processed. To do that we
4029 * trick the unwinding functions into thinking the call came from somewhere
4030 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
4031 * DW_CFA_expression, and the expressions have the following format:
4033 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
4034 * DW_OP_deref | Get *(%rbp + 0x10) == context
4035 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
4036 * [DW_OP_deref] | Dereference, only for CFA
4038 extern void * WINAPI
call_consolidate_callback( CONTEXT
*context
,
4039 void *(CALLBACK
*callback
)(EXCEPTION_RECORD
*),
4040 EXCEPTION_RECORD
*rec
);
4041 __ASM_GLOBAL_FUNC( call_consolidate_callback
,
4043 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
4044 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
4045 "movq %rsp,%rbp\n\t"
4046 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
4047 "subq $0x20,%rsp\n\t"
4048 "movq %rcx,0x10(%rbp)\n\t"
4049 __ASM_CFI(".cfi_remember_state\n\t")
4050 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
4051 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
4052 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
4053 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
4054 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
4055 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
4056 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
4057 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
4058 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
4059 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
4060 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
4061 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
4062 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
4063 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
4064 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
4065 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
4066 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
4067 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
4068 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
4069 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
4072 __ASM_CFI(".cfi_restore_state\n\t")
4073 "movq %rbp,%rsp\n\t"
4074 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
4076 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
4077 __ASM_CFI(".cfi_same_value %rbp\n\t")
4080 /*******************************************************************
4081 * RtlRestoreContext (NTDLL.@)
4083 void WINAPI
RtlRestoreContext( CONTEXT
*context
, EXCEPTION_RECORD
*rec
)
4085 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
4087 if (rec
&& rec
->ExceptionCode
== STATUS_LONGJUMP
&& rec
->NumberParameters
>= 1)
4089 struct MSVCRT_JUMP_BUFFER
*jmp
= (struct MSVCRT_JUMP_BUFFER
*)rec
->ExceptionInformation
[0];
4090 context
->Rbx
= jmp
->Rbx
;
4091 context
->Rsp
= jmp
->Rsp
;
4092 context
->Rbp
= jmp
->Rbp
;
4093 context
->Rsi
= jmp
->Rsi
;
4094 context
->Rdi
= jmp
->Rdi
;
4095 context
->R12
= jmp
->R12
;
4096 context
->R13
= jmp
->R13
;
4097 context
->R14
= jmp
->R14
;
4098 context
->R15
= jmp
->R15
;
4099 context
->Rip
= jmp
->Rip
;
4100 context
->u
.s
.Xmm6
= jmp
->Xmm6
;
4101 context
->u
.s
.Xmm7
= jmp
->Xmm7
;
4102 context
->u
.s
.Xmm8
= jmp
->Xmm8
;
4103 context
->u
.s
.Xmm9
= jmp
->Xmm9
;
4104 context
->u
.s
.Xmm10
= jmp
->Xmm10
;
4105 context
->u
.s
.Xmm11
= jmp
->Xmm11
;
4106 context
->u
.s
.Xmm12
= jmp
->Xmm12
;
4107 context
->u
.s
.Xmm13
= jmp
->Xmm13
;
4108 context
->u
.s
.Xmm14
= jmp
->Xmm14
;
4109 context
->u
.s
.Xmm15
= jmp
->Xmm15
;
4111 else if (rec
&& rec
->ExceptionCode
== STATUS_UNWIND_CONSOLIDATE
&& rec
->NumberParameters
>= 1)
4113 PVOID (CALLBACK
*consolidate
)(EXCEPTION_RECORD
*) = (void *)rec
->ExceptionInformation
[0];
4114 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate
, rec
);
4115 context
->Rip
= (ULONG64
)call_consolidate_callback( context
, consolidate
, rec
);
4118 /* hack: remove no longer accessible TEB frames */
4119 while ((ULONG64
)teb_frame
< context
->Rsp
)
4121 TRACE( "removing TEB frame: %p\n", teb_frame
);
4122 teb_frame
= __wine_pop_frame( teb_frame
);
4125 TRACE( "returning to %lx stack %lx\n", context
->Rip
, context
->Rsp
);
4126 set_cpu_context( context
);
4130 /*******************************************************************
4131 * RtlUnwindEx (NTDLL.@)
4133 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
4134 PVOID retval
, CONTEXT
*context
, UNWIND_HISTORY_TABLE
*table
)
4136 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
4137 EXCEPTION_RECORD record
;
4138 DISPATCHER_CONTEXT dispatch
;
4139 CONTEXT new_context
;
4144 RtlCaptureContext( context
);
4145 new_context
= *context
;
4147 /* build an exception record, if we do not have one */
4150 record
.ExceptionCode
= STATUS_UNWIND
;
4151 record
.ExceptionFlags
= 0;
4152 record
.ExceptionRecord
= NULL
;
4153 record
.ExceptionAddress
= (void *)context
->Rip
;
4154 record
.NumberParameters
= 0;
4158 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
4160 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
4161 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, context
->Rip
);
4162 for (i
= 0; i
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); i
++)
4163 TRACE( " info[%d]=%016lx\n", i
, rec
->ExceptionInformation
[i
] );
4164 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
4165 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
4166 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
4167 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
4168 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
4169 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
4170 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
4171 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
4173 dispatch
.EstablisherFrame
= context
->Rsp
;
4174 dispatch
.TargetIp
= (ULONG64
)target_ip
;
4175 dispatch
.ContextRecord
= context
;
4176 dispatch
.HistoryTable
= table
;
4180 /* FIXME: should use the history table to make things faster */
4182 dispatch
.ImageBase
= 0;
4183 dispatch
.ScopeIndex
= 0;
4184 dispatch
.ControlPc
= context
->Rip
;
4186 /* first look for PE exception information */
4188 if ((dispatch
.FunctionEntry
= lookup_function_info( context
->Rip
, &dispatch
.ImageBase
, &module
)))
4190 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_UHANDLER
, dispatch
.ImageBase
,
4191 context
->Rip
, dispatch
.FunctionEntry
,
4192 &new_context
, &dispatch
.HandlerData
,
4193 &dispatch
.EstablisherFrame
, NULL
);
4197 /* then look for host system exception information */
4199 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
4201 BOOL got_info
= FALSE
;
4202 struct dwarf_eh_bases bases
;
4203 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
->Rip
- 1), &bases
);
4207 status
= dwarf_virtual_unwind( context
->Rip
, &dispatch
.EstablisherFrame
, &new_context
, fde
,
4208 &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
4209 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
4212 #ifdef HAVE_LIBUNWIND_H
4215 status
= libunwind_virtual_unwind( context
->Rip
, &got_info
, &dispatch
.EstablisherFrame
, &new_context
,
4216 &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
4217 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
4223 dispatch
.FunctionEntry
= NULL
;
4224 if (dispatch
.LanguageHandler
&& !module
)
4226 FIXME( "calling personality routine in system library not supported yet\n" );
4227 dispatch
.LanguageHandler
= NULL
;
4232 else WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
4234 /* no exception information, treat as a leaf function */
4236 new_context
.Rip
= *(ULONG64
*)context
->Rsp
;
4237 new_context
.Rsp
= context
->Rsp
+ sizeof(ULONG64
);
4238 dispatch
.EstablisherFrame
= context
->Rsp
;
4239 dispatch
.LanguageHandler
= NULL
;
4242 if (!dispatch
.EstablisherFrame
) break;
4244 if (is_inside_signal_stack( (void *)dispatch
.EstablisherFrame
))
4246 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch
.EstablisherFrame
,
4247 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size
);
4248 *context
= new_context
;
4252 if ((dispatch
.EstablisherFrame
& 7) ||
4253 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
4254 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
4256 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
4257 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
4258 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
4262 if (dispatch
.LanguageHandler
)
4264 if (end_frame
&& (dispatch
.EstablisherFrame
> (ULONG64
)end_frame
))
4266 ERR( "invalid end frame %lx/%p\n", dispatch
.EstablisherFrame
, end_frame
);
4267 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
4269 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) rec
->ExceptionFlags
|= EH_TARGET_UNWIND
;
4270 if (call_unwind_handler( rec
, &dispatch
) == ExceptionCollidedUnwind
)
4274 *context
= new_context
= *dispatch
.ContextRecord
;
4275 dispatch
.ContextRecord
= context
;
4276 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
4277 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
4278 &new_context
, NULL
, &frame
, NULL
);
4279 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
4282 rec
->ExceptionFlags
&= ~EH_COLLIDED_UNWIND
;
4284 else /* hack: call builtin handlers registered in the tib list */
4286 DWORD64 backup_frame
= dispatch
.EstablisherFrame
;
4287 while ((ULONG64
)teb_frame
< new_context
.Rsp
&& (ULONG64
)teb_frame
< (ULONG64
)end_frame
)
4289 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
4290 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
4291 if (call_teb_unwind_handler( rec
, &dispatch
, teb_frame
) == ExceptionCollidedUnwind
)
4295 teb_frame
= __wine_pop_frame( teb_frame
);
4297 *context
= new_context
= *dispatch
.ContextRecord
;
4298 dispatch
.ContextRecord
= context
;
4299 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
4300 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
4301 &new_context
, NULL
, &frame
, NULL
);
4302 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
4305 teb_frame
= __wine_pop_frame( teb_frame
);
4307 if ((ULONG64
)teb_frame
== (ULONG64
)end_frame
&& (ULONG64
)end_frame
< new_context
.Rsp
) break;
4308 dispatch
.EstablisherFrame
= backup_frame
;
4311 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) break;
4312 *context
= new_context
;
4315 context
->Rax
= (ULONG64
)retval
;
4316 context
->Rip
= (ULONG64
)target_ip
;
4317 RtlRestoreContext(context
, rec
);
4321 /*******************************************************************
4322 * RtlUnwind (NTDLL.@)
4324 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
4327 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
4331 /*******************************************************************
4332 * _local_unwind (NTDLL.@)
4334 void WINAPI
_local_unwind( void *frame
, void *target_ip
)
4337 RtlUnwindEx( frame
, target_ip
, NULL
, NULL
, &context
, NULL
);
4340 /*******************************************************************
4341 * __C_specific_handler (NTDLL.@)
4343 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
4346 struct _DISPATCHER_CONTEXT
*dispatch
)
4348 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
4351 TRACE( "%p %lx %p %p\n", rec
, frame
, context
, dispatch
);
4352 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
4354 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
4356 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
4358 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
4359 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
4361 TERMINATION_HANDLER handler
;
4363 if (table
->ScopeRecord
[i
].JumpTarget
) continue;
4365 if (rec
->ExceptionFlags
& EH_TARGET_UNWIND
&&
4366 dispatch
->TargetIp
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
4367 dispatch
->TargetIp
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
4372 handler
= (TERMINATION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
4373 dispatch
->ScopeIndex
= i
+1;
4375 TRACE( "calling __finally %p frame %lx\n", handler
, frame
);
4376 handler( 1, frame
);
4379 return ExceptionContinueSearch
;
4382 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
4384 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
4385 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
4387 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
4388 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
4390 EXCEPTION_POINTERS ptrs
;
4391 PC_LANGUAGE_EXCEPTION_HANDLER filter
;
4393 filter
= (PC_LANGUAGE_EXCEPTION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
4394 ptrs
.ExceptionRecord
= rec
;
4395 ptrs
.ContextRecord
= context
;
4396 TRACE( "calling filter %p ptrs %p frame %lx\n", filter
, &ptrs
, frame
);
4397 switch (filter( &ptrs
, frame
))
4399 case EXCEPTION_EXECUTE_HANDLER
:
4401 case EXCEPTION_CONTINUE_SEARCH
:
4403 case EXCEPTION_CONTINUE_EXECUTION
:
4404 return ExceptionContinueExecution
;
4407 TRACE( "unwinding to target %lx\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
4408 RtlUnwindEx( (void *)frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
4409 rec
, 0, dispatch
->ContextRecord
, dispatch
->HistoryTable
);
4412 return ExceptionContinueSearch
;
4416 /***********************************************************************
4417 * RtlRaiseException (NTDLL.@)
4419 __ASM_GLOBAL_FUNC( RtlRaiseException
,
4420 "movq %rcx,8(%rsp)\n\t"
4421 "sub $0x4f8,%rsp\n\t"
4422 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
4423 "leaq 0x20(%rsp),%rcx\n\t"
4424 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
4425 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
4426 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
4427 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
4428 "movq (%rax),%rcx\n\t" /* original first parameter */
4429 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
4430 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
4431 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
4432 "movq %rax,0x10(%rcx)\n\t" /* rec->ExceptionAddress */
4434 "call " __ASM_NAME("NtRaiseException") "\n\t"
4435 "movq %rax,%rcx\n\t"
4436 "call " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
4439 static inline ULONG
hash_pointers( void **ptrs
, ULONG count
)
4441 /* Based on MurmurHash2, which is in the public domain */
4442 static const ULONG m
= 0x5bd1e995;
4443 static const ULONG r
= 24;
4444 ULONG hash
= count
* sizeof(void*);
4445 for (; count
> 0; ptrs
++, count
--)
4447 ULONG_PTR data
= (ULONG_PTR
)*ptrs
;
4448 ULONG k1
= (ULONG
)(data
& 0xffffffff), k2
= (ULONG
)(data
>> 32);
4450 k1
= (k1
^ (k1
>> r
)) * m
;
4452 k2
= (k2
^ (k2
>> r
)) * m
;
4453 hash
= (((hash
* m
) ^ k1
) * m
) ^ k2
;
4455 hash
= (hash
^ (hash
>> 13)) * m
;
4456 return hash
^ (hash
>> 15);
4460 /*************************************************************************
4461 * RtlCaptureStackBackTrace (NTDLL.@)
4463 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
4465 UNWIND_HISTORY_TABLE table
;
4466 DISPATCHER_CONTEXT dispatch
;
4471 USHORT num_entries
= 0;
4473 TRACE( "(%u, %u, %p, %p)", skip
, count
, buffer
, hash
);
4475 RtlCaptureContext( &context
);
4476 dispatch
.TargetIp
= 0;
4477 dispatch
.ContextRecord
= &context
;
4478 dispatch
.HistoryTable
= &table
;
4479 if (hash
) *hash
= 0;
4480 for (i
= 0; i
< skip
+ count
; i
++)
4482 /* FIXME: should use the history table to make things faster */
4484 dispatch
.ImageBase
= 0;
4485 dispatch
.ControlPc
= context
.Rip
;
4486 dispatch
.ScopeIndex
= 0;
4488 /* first look for PE exception information */
4490 if ((dispatch
.FunctionEntry
= lookup_function_info( dispatch
.ControlPc
, &dispatch
.ImageBase
, &module
)))
4492 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
, dispatch
.ControlPc
,
4493 dispatch
.FunctionEntry
, &context
, &dispatch
.HandlerData
,
4494 &dispatch
.EstablisherFrame
, NULL
);
4498 /* then look for host system exception information */
4500 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
4502 BOOL got_info
= FALSE
;
4503 struct dwarf_eh_bases bases
;
4504 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(dispatch
.ControlPc
- 1), &bases
);
4508 status
= dwarf_virtual_unwind( dispatch
.ControlPc
, &dispatch
.EstablisherFrame
, &context
,
4509 fde
, &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
4510 if (status
!= STATUS_SUCCESS
) return status
;
4513 #ifdef HAVE_LIBUNWIND_H
4516 status
= libunwind_virtual_unwind( dispatch
.ControlPc
, &got_info
, &dispatch
.EstablisherFrame
, &context
,
4517 &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
4518 if (status
!= STATUS_SUCCESS
) return i
;
4524 dispatch
.FunctionEntry
= NULL
;
4528 else WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
4530 context
.Rip
= *(ULONG64
*)context
.Rsp
;
4531 context
.Rsp
= context
.Rsp
+ sizeof(ULONG64
);
4532 dispatch
.EstablisherFrame
= context
.Rsp
;
4535 if (!dispatch
.EstablisherFrame
) break;
4537 if ((dispatch
.EstablisherFrame
& 7) ||
4538 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
4539 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
4541 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
4542 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
4546 if (context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
4548 if (i
>= skip
) buffer
[num_entries
++] = (void *)context
.Rip
;
4550 if (hash
&& num_entries
> 0) *hash
= hash_pointers( buffer
, num_entries
);
4551 TRACE( "captured %hu frames\n", num_entries
);
4556 /***********************************************************************
4559 static void WINAPI
call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
)
4563 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
4564 RtlExitUserThread( entry( arg
));
4566 __EXCEPT(call_unhandled_exception_filter
)
4568 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
4571 abort(); /* should not be reached */
4575 extern void DECLSPEC_NORETURN
start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
,
4577 __ASM_GLOBAL_FUNC( start_thread
,
4579 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
4580 "movq %rbp,48(%rsp)\n\t"
4581 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
4582 "movq %rbx,40(%rsp)\n\t"
4583 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
4584 "movq %r12,32(%rsp)\n\t"
4585 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
4586 "movq %r13,24(%rsp)\n\t"
4587 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
4588 "movq %r14,16(%rsp)\n\t"
4589 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
4590 "movq %r15,8(%rsp)\n\t"
4591 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
4592 /* store exit frame */
4593 "movq %gs:0x30,%rax\n\t"
4594 "movq %rsp,0x330(%rax)\n\t" /* amd64_thread_data()->exit_frame */
4595 /* switch to thread stack */
4596 "movq 8(%rax),%rax\n\t" /* NtCurrentTeb()->Tib.StackBase */
4597 "leaq -0x1000(%rax),%rsp\n\t"
4599 "call " __ASM_NAME("attach_thread") "\n\t"
4600 "movq %rax,%rsp\n\t"
4601 /* clear the stack */
4602 "andq $~0xfff,%rax\n\t" /* round down to page size */
4603 "movq %rax,%rdi\n\t"
4604 "call " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
4605 /* switch to the initial context */
4606 "movq %rsp,%rdi\n\t"
4607 "call " __ASM_NAME("set_cpu_context") )
4609 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int) );
4610 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
4611 /* fetch exit frame */
4612 "movq %gs:0x30,%rax\n\t"
4613 "movq 0x330(%rax),%rdx\n\t" /* amd64_thread_data()->exit_frame */
4614 "testq %rdx,%rdx\n\t"
4617 /* switch to exit frame stack */
4618 "1:\tmovq $0,0x330(%rax)\n\t"
4619 "movq %rdx,%rsp\n\t"
4620 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
4621 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
4622 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
4623 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
4624 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
4625 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
4626 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
4630 /***********************************************************************
4631 * init_thread_context
4633 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
, void *relay
)
4635 __asm__( "movw %%cs,%0" : "=m" (context
->SegCs
) );
4636 __asm__( "movw %%ss,%0" : "=m" (context
->SegSs
) );
4637 __asm__( "fxsave %0" : "=m" (context
->u
.FltSave
) );
4638 context
->Rcx
= (ULONG_PTR
)entry
;
4639 context
->Rdx
= (ULONG_PTR
)arg
;
4640 context
->Rsp
= (ULONG_PTR
)NtCurrentTeb()->Tib
.StackBase
- 0x28;
4641 context
->Rip
= (ULONG_PTR
)relay
;
4642 context
->EFlags
= 0x200;
4646 /***********************************************************************
4649 PCONTEXT DECLSPEC_HIDDEN
attach_thread( LPTHREAD_START_ROUTINE entry
, void *arg
,
4650 BOOL suspend
, void *relay
)
4656 CONTEXT context
= { 0 };
4658 context
.ContextFlags
= CONTEXT_ALL
;
4659 init_thread_context( &context
, entry
, arg
, relay
);
4660 wait_suspend( &context
);
4661 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Rsp
& ~15) - 1;
4666 ctx
= (CONTEXT
*)((char *)NtCurrentTeb()->Tib
.StackBase
- 0x30) - 1;
4667 init_thread_context( ctx
, entry
, arg
, relay
);
4669 ctx
->ContextFlags
= CONTEXT_FULL
;
4670 LdrInitializeThunk( ctx
, (void **)&ctx
->Rcx
, 0, 0 );
4675 /***********************************************************************
4676 * signal_start_thread
4678 * Thread startup sequence:
4679 * signal_start_thread()
4681 * -> call_thread_func()
4683 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
4685 start_thread( entry
, arg
, suspend
, call_thread_func
);
4689 /**********************************************************************
4690 * signal_start_process
4692 * Process startup sequence:
4693 * signal_start_process()
4695 * -> kernel32_start_process()
4697 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
4699 start_thread( entry
, NtCurrentTeb()->Peb
, suspend
, kernel32_start_process
);
4703 /***********************************************************************
4704 * signal_exit_thread
4706 void signal_exit_thread( int status
)
4708 call_thread_exit_func( status
, exit_thread
);
4711 /***********************************************************************
4712 * signal_exit_process
4714 void signal_exit_process( int status
)
4716 call_thread_exit_func( status
, exit
);
4719 /**********************************************************************
4720 * DbgBreakPoint (NTDLL.@)
4722 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
4724 /**********************************************************************
4725 * DbgUserBreakPoint (NTDLL.@)
4727 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
4729 #endif /* __x86_64__ */