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
;
347 /* user defined callback */
348 PGET_RUNTIME_FUNCTION_CALLBACK callback
;
352 static struct list dynamic_unwind_list
= LIST_INIT(dynamic_unwind_list
);
354 static RTL_CRITICAL_SECTION dynamic_unwind_section
;
355 static RTL_CRITICAL_SECTION_DEBUG dynamic_unwind_debug
=
357 0, 0, &dynamic_unwind_section
,
358 { &dynamic_unwind_debug
.ProcessLocksList
, &dynamic_unwind_debug
.ProcessLocksList
},
359 0, 0, { (DWORD_PTR
)(__FILE__
": dynamic_unwind_section") }
361 static RTL_CRITICAL_SECTION dynamic_unwind_section
= { &dynamic_unwind_debug
, -1, 0, 0, 0, 0 };
363 /***********************************************************************
364 * Definitions for Win32 unwind tables
369 RUNTIME_FUNCTION chain
;
387 BYTE frame_offset
: 4;
388 struct opcode opcodes
[1]; /* info->count entries */
389 /* followed by handler_data */
392 #define UWOP_PUSH_NONVOL 0
393 #define UWOP_ALLOC_LARGE 1
394 #define UWOP_ALLOC_SMALL 2
395 #define UWOP_SET_FPREG 3
396 #define UWOP_SAVE_NONVOL 4
397 #define UWOP_SAVE_NONVOL_FAR 5
398 #define UWOP_SAVE_XMM128 8
399 #define UWOP_SAVE_XMM128_FAR 9
400 #define UWOP_PUSH_MACHFRAME 10
402 static void dump_unwind_info( ULONG64 base
, RUNTIME_FUNCTION
*function
)
404 static const char * const reg_names
[16] =
405 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
406 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
408 union handler_data
*handler_data
;
409 struct UNWIND_INFO
*info
;
410 unsigned int i
, count
;
412 TRACE( "**** func %x-%x\n", function
->BeginAddress
, function
->EndAddress
);
415 if (function
->UnwindData
& 1)
417 RUNTIME_FUNCTION
*next
= (RUNTIME_FUNCTION
*)((char *)base
+ (function
->UnwindData
& ~1));
418 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
419 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
,
420 (char *)base
+ next
->BeginAddress
, (char *)base
+ next
->EndAddress
);
424 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
426 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
427 info
, info
->flags
, info
->prolog
,
428 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
);
431 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
432 reg_names
[info
->frame_reg
], info
->frame_offset
* 16 );
434 for (i
= 0; i
< info
->count
; i
++)
436 TRACE( " 0x%x: ", info
->opcodes
[i
].offset
);
437 switch (info
->opcodes
[i
].code
)
439 case UWOP_PUSH_NONVOL
:
440 TRACE( "pushq %%%s\n", reg_names
[info
->opcodes
[i
].info
] );
442 case UWOP_ALLOC_LARGE
:
443 if (info
->opcodes
[i
].info
)
445 count
= *(DWORD
*)&info
->opcodes
[i
+1];
450 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
453 TRACE( "subq $0x%x,%%rsp\n", count
);
455 case UWOP_ALLOC_SMALL
:
456 count
= (info
->opcodes
[i
].info
+ 1) * 8;
457 TRACE( "subq $0x%x,%%rsp\n", count
);
460 TRACE( "leaq 0x%x(%%rsp),%s\n",
461 info
->frame_offset
* 16, reg_names
[info
->frame_reg
] );
463 case UWOP_SAVE_NONVOL
:
464 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
465 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
468 case UWOP_SAVE_NONVOL_FAR
:
469 count
= *(DWORD
*)&info
->opcodes
[i
+1];
470 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
473 case UWOP_SAVE_XMM128
:
474 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 16;
475 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
478 case UWOP_SAVE_XMM128_FAR
:
479 count
= *(DWORD
*)&info
->opcodes
[i
+1];
480 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
483 case UWOP_PUSH_MACHFRAME
:
484 TRACE( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
487 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
492 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
493 if (info
->flags
& UNW_FLAG_CHAININFO
)
495 TRACE( " chained to function %p-%p\n",
496 (char *)base
+ handler_data
->chain
.BeginAddress
,
497 (char *)base
+ handler_data
->chain
.EndAddress
);
498 function
= &handler_data
->chain
;
501 if (info
->flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
502 TRACE( " handler %p data at %p\n",
503 (char *)base
+ handler_data
->handler
, &handler_data
->handler
+ 1 );
508 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
512 TRACE( "scope table at %p\n", table
);
513 for (i
= 0; i
< table
->Count
; i
++)
514 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i
,
515 base
+ table
->ScopeRecord
[i
].BeginAddress
,
516 base
+ table
->ScopeRecord
[i
].EndAddress
,
517 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
518 base
+ table
->ScopeRecord
[i
].JumpTarget
);
522 /***********************************************************************
523 * Definitions for Dwarf unwind tables
526 enum dwarf_call_frame_info
528 DW_CFA_advance_loc
= 0x40,
529 DW_CFA_offset
= 0x80,
530 DW_CFA_restore
= 0xc0,
532 DW_CFA_set_loc
= 0x01,
533 DW_CFA_advance_loc1
= 0x02,
534 DW_CFA_advance_loc2
= 0x03,
535 DW_CFA_advance_loc4
= 0x04,
536 DW_CFA_offset_extended
= 0x05,
537 DW_CFA_restore_extended
= 0x06,
538 DW_CFA_undefined
= 0x07,
539 DW_CFA_same_value
= 0x08,
540 DW_CFA_register
= 0x09,
541 DW_CFA_remember_state
= 0x0a,
542 DW_CFA_restore_state
= 0x0b,
543 DW_CFA_def_cfa
= 0x0c,
544 DW_CFA_def_cfa_register
= 0x0d,
545 DW_CFA_def_cfa_offset
= 0x0e,
546 DW_CFA_def_cfa_expression
= 0x0f,
547 DW_CFA_expression
= 0x10,
548 DW_CFA_offset_extended_sf
= 0x11,
549 DW_CFA_def_cfa_sf
= 0x12,
550 DW_CFA_def_cfa_offset_sf
= 0x13,
551 DW_CFA_val_offset
= 0x14,
552 DW_CFA_val_offset_sf
= 0x15,
553 DW_CFA_val_expression
= 0x16,
560 DW_OP_const1u
= 0x08,
561 DW_OP_const1s
= 0x09,
562 DW_OP_const2u
= 0x0a,
563 DW_OP_const2s
= 0x0b,
564 DW_OP_const4u
= 0x0c,
565 DW_OP_const4s
= 0x0d,
566 DW_OP_const8u
= 0x0e,
567 DW_OP_const8s
= 0x0f,
587 DW_OP_plus_uconst
= 0x23,
700 DW_OP_deref_size
= 0x94,
701 DW_OP_xderef_size
= 0x95,
703 DW_OP_push_object_address
= 0x97,
706 DW_OP_call_ref
= 0x9a,
707 DW_OP_form_tls_address
= 0x9b,
708 DW_OP_call_frame_cfa
= 0x9c,
709 DW_OP_bit_piece
= 0x9d,
710 DW_OP_lo_user
= 0xe0,
711 DW_OP_hi_user
= 0xff,
712 DW_OP_GNU_push_tls_address
= 0xe0,
713 DW_OP_GNU_uninit
= 0xf0,
714 DW_OP_GNU_encoded_addr
= 0xf1,
717 #define DW_EH_PE_native 0x00
718 #define DW_EH_PE_leb128 0x01
719 #define DW_EH_PE_data2 0x02
720 #define DW_EH_PE_data4 0x03
721 #define DW_EH_PE_data8 0x04
722 #define DW_EH_PE_signed 0x08
723 #define DW_EH_PE_abs 0x00
724 #define DW_EH_PE_pcrel 0x10
725 #define DW_EH_PE_textrel 0x20
726 #define DW_EH_PE_datarel 0x30
727 #define DW_EH_PE_funcrel 0x40
728 #define DW_EH_PE_aligned 0x50
729 #define DW_EH_PE_indirect 0x80
730 #define DW_EH_PE_omit 0xff
732 struct dwarf_eh_bases
743 unsigned char version
;
744 unsigned char augmentation
[1];
750 unsigned int cie_offset
;
753 extern const struct dwarf_fde
*_Unwind_Find_FDE (void *, struct dwarf_eh_bases
*);
755 static unsigned char dwarf_get_u1( const unsigned char **p
)
760 static unsigned short dwarf_get_u2( const unsigned char **p
)
762 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8);
767 static unsigned int dwarf_get_u4( const unsigned char **p
)
769 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8) | ((*p
)[2] << 16) | ((*p
)[3] << 24);
774 static ULONG64
dwarf_get_u8( const unsigned char **p
)
776 ULONG64 low
= dwarf_get_u4( p
);
777 ULONG64 high
= dwarf_get_u4( p
);
778 return low
| (high
<< 32);
781 static ULONG_PTR
dwarf_get_uleb128( const unsigned char **p
)
784 unsigned int shift
= 0;
790 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
793 } while (byte
& 0x80);
797 static LONG_PTR
dwarf_get_sleb128( const unsigned char **p
)
800 unsigned int shift
= 0;
806 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
809 } while (byte
& 0x80);
811 if ((shift
< 8 * sizeof(ret
)) && (byte
& 0x40)) ret
|= -((ULONG_PTR
)1 << shift
);
815 static ULONG_PTR
dwarf_get_ptr( const unsigned char **p
, unsigned char encoding
)
819 if (encoding
== DW_EH_PE_omit
) return 0;
821 switch (encoding
& 0xf0)
827 base
= (ULONG_PTR
)*p
;
830 FIXME( "unsupported encoding %02x\n", encoding
);
834 switch (encoding
& 0x0f)
836 case DW_EH_PE_native
:
837 return base
+ dwarf_get_u8( p
);
838 case DW_EH_PE_leb128
:
839 return base
+ dwarf_get_uleb128( p
);
841 return base
+ dwarf_get_u2( p
);
843 return base
+ dwarf_get_u4( p
);
845 return base
+ dwarf_get_u8( p
);
846 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
847 return base
+ dwarf_get_sleb128( p
);
848 case DW_EH_PE_signed
|DW_EH_PE_data2
:
849 return base
+ (signed short)dwarf_get_u2( p
);
850 case DW_EH_PE_signed
|DW_EH_PE_data4
:
851 return base
+ (signed int)dwarf_get_u4( p
);
852 case DW_EH_PE_signed
|DW_EH_PE_data8
:
853 return base
+ (LONG64
)dwarf_get_u8( p
);
855 FIXME( "unsupported encoding %02x\n", encoding
);
862 RULE_UNSET
, /* not set at all */
863 RULE_UNDEFINED
, /* undefined value */
864 RULE_SAME
, /* same value as previous frame */
865 RULE_CFA_OFFSET
, /* stored at cfa offset */
866 RULE_OTHER_REG
, /* stored in other register */
867 RULE_EXPRESSION
, /* address specified by expression */
868 RULE_VAL_EXPRESSION
/* value specified by expression */
871 #define NB_FRAME_REGS 41
872 #define MAX_SAVED_STATES 16
876 ULONG_PTR cfa_offset
;
877 unsigned char cfa_reg
;
878 enum reg_rule cfa_rule
;
879 enum reg_rule rules
[NB_FRAME_REGS
];
880 ULONG64 regs
[NB_FRAME_REGS
];
886 ULONG_PTR code_align
;
888 unsigned char retaddr_reg
;
889 unsigned char fde_encoding
;
890 unsigned char signal_frame
;
891 unsigned char state_sp
;
892 struct frame_state state
;
893 struct frame_state
*state_stack
;
896 static const char *dwarf_reg_names
[NB_FRAME_REGS
] =
898 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
899 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
900 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
901 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
902 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
905 static BOOL
valid_reg( ULONG_PTR reg
)
907 if (reg
>= NB_FRAME_REGS
) FIXME( "unsupported reg %lx\n", reg
);
908 return (reg
< NB_FRAME_REGS
);
911 static void execute_cfa_instructions( const unsigned char *ptr
, const unsigned char *end
,
912 ULONG_PTR last_ip
, struct frame_info
*info
)
914 while (ptr
< end
&& info
->ip
< last_ip
+ info
->signal_frame
)
916 enum dwarf_call_frame_info op
= *ptr
++;
922 case DW_CFA_advance_loc
:
924 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
925 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
931 ULONG_PTR reg
= op
& 0x3f;
932 LONG_PTR offset
= dwarf_get_uleb128( &ptr
) * info
->data_align
;
933 if (!valid_reg( reg
)) break;
934 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
935 info
->state
.regs
[reg
] = offset
;
936 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
941 ULONG_PTR reg
= op
& 0x3f;
942 if (!valid_reg( reg
)) break;
943 TRACE( "%lx: DW_CFA_restore %s\n", info
->ip
, dwarf_reg_names
[reg
] );
944 info
->state
.rules
[reg
] = RULE_UNSET
;
955 ULONG_PTR loc
= dwarf_get_ptr( &ptr
, info
->fde_encoding
);
956 TRACE( "%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
960 case DW_CFA_advance_loc1
:
962 ULONG_PTR offset
= *ptr
++ * info
->code_align
;
963 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
967 case DW_CFA_advance_loc2
:
969 ULONG_PTR offset
= dwarf_get_u2( &ptr
) * info
->code_align
;
970 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
974 case DW_CFA_advance_loc4
:
976 ULONG_PTR offset
= dwarf_get_u4( &ptr
) * info
->code_align
;
977 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
981 case DW_CFA_offset_extended
:
982 case DW_CFA_offset_extended_sf
:
984 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
985 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf_get_uleb128( &ptr
) * info
->data_align
986 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
987 if (!valid_reg( reg
)) break;
988 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
989 info
->state
.regs
[reg
] = offset
;
990 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
993 case DW_CFA_restore_extended
:
995 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
996 if (!valid_reg( reg
)) break;
997 TRACE( "%lx: DW_CFA_restore_extended %s\n", info
->ip
, dwarf_reg_names
[reg
] );
998 info
->state
.rules
[reg
] = RULE_UNSET
;
1001 case DW_CFA_undefined
:
1003 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1004 if (!valid_reg( reg
)) break;
1005 TRACE( "%lx: DW_CFA_undefined %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1006 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
1009 case DW_CFA_same_value
:
1011 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1012 if (!valid_reg( reg
)) break;
1013 TRACE( "%lx: DW_CFA_same_value %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1014 info
->state
.regs
[reg
] = reg
;
1015 info
->state
.rules
[reg
] = RULE_SAME
;
1018 case DW_CFA_register
:
1020 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1021 ULONG_PTR reg2
= dwarf_get_uleb128( &ptr
);
1022 if (!valid_reg( reg
) || !valid_reg( reg2
)) break;
1023 TRACE( "%lx: DW_CFA_register %s == %s\n", info
->ip
, dwarf_reg_names
[reg
], dwarf_reg_names
[reg2
] );
1024 info
->state
.regs
[reg
] = reg2
;
1025 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
1028 case DW_CFA_remember_state
:
1029 TRACE( "%lx: DW_CFA_remember_state\n", info
->ip
);
1030 if (info
->state_sp
>= MAX_SAVED_STATES
)
1031 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
1033 info
->state_stack
[info
->state_sp
++] = info
->state
;
1035 case DW_CFA_restore_state
:
1036 TRACE( "%lx: DW_CFA_restore_state\n", info
->ip
);
1037 if (!info
->state_sp
)
1038 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
1040 info
->state
= info
->state_stack
[--info
->state_sp
];
1042 case DW_CFA_def_cfa
:
1043 case DW_CFA_def_cfa_sf
:
1045 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1046 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf_get_uleb128( &ptr
)
1047 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
1048 if (!valid_reg( reg
)) break;
1049 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
1050 info
->state
.cfa_reg
= reg
;
1051 info
->state
.cfa_offset
= offset
;
1052 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1055 case DW_CFA_def_cfa_register
:
1057 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1058 if (!valid_reg( reg
)) break;
1059 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1060 info
->state
.cfa_reg
= reg
;
1061 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1064 case DW_CFA_def_cfa_offset
:
1065 case DW_CFA_def_cfa_offset_sf
:
1067 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf_get_uleb128( &ptr
)
1068 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
1069 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info
->ip
, offset
);
1070 info
->state
.cfa_offset
= offset
;
1071 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1074 case DW_CFA_def_cfa_expression
:
1076 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
1077 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
1078 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
1079 info
->state
.cfa_offset
= expr
;
1080 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
1084 case DW_CFA_expression
:
1085 case DW_CFA_val_expression
:
1087 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1088 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
1089 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
1090 if (!valid_reg( reg
)) break;
1091 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
1092 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_", dwarf_reg_names
[reg
], expr
, expr
+len
);
1093 info
->state
.regs
[reg
] = expr
;
1094 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
1099 FIXME( "%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
1105 /* retrieve a context register from its dwarf number */
1106 static void *get_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
)
1110 case 0: return &context
->Rax
;
1111 case 1: return &context
->Rdx
;
1112 case 2: return &context
->Rcx
;
1113 case 3: return &context
->Rbx
;
1114 case 4: return &context
->Rsi
;
1115 case 5: return &context
->Rdi
;
1116 case 6: return &context
->Rbp
;
1117 case 7: return &context
->Rsp
;
1118 case 8: return &context
->R8
;
1119 case 9: return &context
->R9
;
1120 case 10: return &context
->R10
;
1121 case 11: return &context
->R11
;
1122 case 12: return &context
->R12
;
1123 case 13: return &context
->R13
;
1124 case 14: return &context
->R14
;
1125 case 15: return &context
->R15
;
1126 case 16: return &context
->Rip
;
1127 case 17: return &context
->u
.s
.Xmm0
;
1128 case 18: return &context
->u
.s
.Xmm1
;
1129 case 19: return &context
->u
.s
.Xmm2
;
1130 case 20: return &context
->u
.s
.Xmm3
;
1131 case 21: return &context
->u
.s
.Xmm4
;
1132 case 22: return &context
->u
.s
.Xmm5
;
1133 case 23: return &context
->u
.s
.Xmm6
;
1134 case 24: return &context
->u
.s
.Xmm7
;
1135 case 25: return &context
->u
.s
.Xmm8
;
1136 case 26: return &context
->u
.s
.Xmm9
;
1137 case 27: return &context
->u
.s
.Xmm10
;
1138 case 28: return &context
->u
.s
.Xmm11
;
1139 case 29: return &context
->u
.s
.Xmm12
;
1140 case 30: return &context
->u
.s
.Xmm13
;
1141 case 31: return &context
->u
.s
.Xmm14
;
1142 case 32: return &context
->u
.s
.Xmm15
;
1143 case 33: return &context
->u
.s
.Legacy
[0];
1144 case 34: return &context
->u
.s
.Legacy
[1];
1145 case 35: return &context
->u
.s
.Legacy
[2];
1146 case 36: return &context
->u
.s
.Legacy
[3];
1147 case 37: return &context
->u
.s
.Legacy
[4];
1148 case 38: return &context
->u
.s
.Legacy
[5];
1149 case 39: return &context
->u
.s
.Legacy
[6];
1150 case 40: return &context
->u
.s
.Legacy
[7];
1151 default: return NULL
;
1155 /* set a context register from its dwarf number */
1156 static void set_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
, void *val
)
1160 case 0: context
->Rax
= *(ULONG64
*)val
; break;
1161 case 1: context
->Rdx
= *(ULONG64
*)val
; break;
1162 case 2: context
->Rcx
= *(ULONG64
*)val
; break;
1163 case 3: context
->Rbx
= *(ULONG64
*)val
; break;
1164 case 4: context
->Rsi
= *(ULONG64
*)val
; break;
1165 case 5: context
->Rdi
= *(ULONG64
*)val
; break;
1166 case 6: context
->Rbp
= *(ULONG64
*)val
; break;
1167 case 7: context
->Rsp
= *(ULONG64
*)val
; break;
1168 case 8: context
->R8
= *(ULONG64
*)val
; break;
1169 case 9: context
->R9
= *(ULONG64
*)val
; break;
1170 case 10: context
->R10
= *(ULONG64
*)val
; break;
1171 case 11: context
->R11
= *(ULONG64
*)val
; break;
1172 case 12: context
->R12
= *(ULONG64
*)val
; break;
1173 case 13: context
->R13
= *(ULONG64
*)val
; break;
1174 case 14: context
->R14
= *(ULONG64
*)val
; break;
1175 case 15: context
->R15
= *(ULONG64
*)val
; break;
1176 case 16: context
->Rip
= *(ULONG64
*)val
; break;
1177 case 17: memcpy( &context
->u
.s
.Xmm0
, val
, sizeof(M128A
) ); break;
1178 case 18: memcpy( &context
->u
.s
.Xmm1
, val
, sizeof(M128A
) ); break;
1179 case 19: memcpy( &context
->u
.s
.Xmm2
, val
, sizeof(M128A
) ); break;
1180 case 20: memcpy( &context
->u
.s
.Xmm3
, val
, sizeof(M128A
) ); break;
1181 case 21: memcpy( &context
->u
.s
.Xmm4
, val
, sizeof(M128A
) ); break;
1182 case 22: memcpy( &context
->u
.s
.Xmm5
, val
, sizeof(M128A
) ); break;
1183 case 23: memcpy( &context
->u
.s
.Xmm6
, val
, sizeof(M128A
) ); break;
1184 case 24: memcpy( &context
->u
.s
.Xmm7
, val
, sizeof(M128A
) ); break;
1185 case 25: memcpy( &context
->u
.s
.Xmm8
, val
, sizeof(M128A
) ); break;
1186 case 26: memcpy( &context
->u
.s
.Xmm9
, val
, sizeof(M128A
) ); break;
1187 case 27: memcpy( &context
->u
.s
.Xmm10
, val
, sizeof(M128A
) ); break;
1188 case 28: memcpy( &context
->u
.s
.Xmm11
, val
, sizeof(M128A
) ); break;
1189 case 29: memcpy( &context
->u
.s
.Xmm12
, val
, sizeof(M128A
) ); break;
1190 case 30: memcpy( &context
->u
.s
.Xmm13
, val
, sizeof(M128A
) ); break;
1191 case 31: memcpy( &context
->u
.s
.Xmm14
, val
, sizeof(M128A
) ); break;
1192 case 32: memcpy( &context
->u
.s
.Xmm15
, val
, sizeof(M128A
) ); break;
1193 case 33: memcpy( &context
->u
.s
.Legacy
[0], val
, sizeof(M128A
) ); break;
1194 case 34: memcpy( &context
->u
.s
.Legacy
[1], val
, sizeof(M128A
) ); break;
1195 case 35: memcpy( &context
->u
.s
.Legacy
[2], val
, sizeof(M128A
) ); break;
1196 case 36: memcpy( &context
->u
.s
.Legacy
[3], val
, sizeof(M128A
) ); break;
1197 case 37: memcpy( &context
->u
.s
.Legacy
[4], val
, sizeof(M128A
) ); break;
1198 case 38: memcpy( &context
->u
.s
.Legacy
[5], val
, sizeof(M128A
) ); break;
1199 case 39: memcpy( &context
->u
.s
.Legacy
[6], val
, sizeof(M128A
) ); break;
1200 case 40: memcpy( &context
->u
.s
.Legacy
[7], val
, sizeof(M128A
) ); break;
1204 static ULONG_PTR
eval_expression( const unsigned char *p
, CONTEXT
*context
)
1206 ULONG_PTR reg
, tmp
, stack
[64];
1208 ULONG_PTR len
= dwarf_get_uleb128(&p
);
1209 const unsigned char *end
= p
+ len
;
1213 unsigned char opcode
= dwarf_get_u1(&p
);
1215 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
1216 stack
[++sp
] = opcode
- DW_OP_lit0
;
1217 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
1218 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_reg0
);
1219 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
1220 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_breg0
) + dwarf_get_sleb128(&p
);
1221 else switch (opcode
)
1223 case DW_OP_nop
: break;
1224 case DW_OP_addr
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1225 case DW_OP_const1u
: stack
[++sp
] = dwarf_get_u1(&p
); break;
1226 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf_get_u1(&p
); break;
1227 case DW_OP_const2u
: stack
[++sp
] = dwarf_get_u2(&p
); break;
1228 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf_get_u2(&p
); break;
1229 case DW_OP_const4u
: stack
[++sp
] = dwarf_get_u4(&p
); break;
1230 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf_get_u4(&p
); break;
1231 case DW_OP_const8u
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1232 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf_get_u8(&p
); break;
1233 case DW_OP_constu
: stack
[++sp
] = dwarf_get_uleb128(&p
); break;
1234 case DW_OP_consts
: stack
[++sp
] = dwarf_get_sleb128(&p
); break;
1235 case DW_OP_deref
: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1236 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
1237 case DW_OP_drop
: sp
--; break;
1238 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
1239 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf_get_u1(&p
)]; sp
++; break;
1240 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
1241 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
1242 case DW_OP_abs
: stack
[sp
] = labs(stack
[sp
]); break;
1243 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
1244 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
1245 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
1246 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
1247 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
1248 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
1249 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
1250 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
1251 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
1252 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
1253 case DW_OP_plus_uconst
: stack
[sp
] += dwarf_get_uleb128(&p
); break;
1254 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
1255 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
1256 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
1257 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
1258 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
1259 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
1260 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
1261 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
1262 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
1263 case DW_OP_skip
: tmp
= (short)dwarf_get_u2(&p
); p
+= tmp
; break;
1264 case DW_OP_bra
: tmp
= (short)dwarf_get_u2(&p
); if (!stack
[sp
--]) p
+= tmp
; break;
1265 case DW_OP_GNU_encoded_addr
: tmp
= *p
++; stack
[++sp
] = dwarf_get_ptr( &p
, tmp
); break;
1266 case DW_OP_regx
: stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, dwarf_get_uleb128(&p
) ); break;
1268 reg
= dwarf_get_uleb128(&p
);
1269 tmp
= dwarf_get_sleb128(&p
);
1270 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, reg
) + tmp
;
1272 case DW_OP_deref_size
:
1275 case 1: stack
[sp
] = *(unsigned char *)stack
[sp
]; break;
1276 case 2: stack
[sp
] = *(unsigned short *)stack
[sp
]; break;
1277 case 4: stack
[sp
] = *(unsigned int *)stack
[sp
]; break;
1278 case 8: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1282 FIXME( "unhandled opcode %02x\n", opcode
);
1288 /* apply the computed frame info to the actual context */
1289 static void apply_frame_state( CONTEXT
*context
, struct frame_state
*state
)
1292 ULONG_PTR cfa
, value
;
1293 CONTEXT new_context
= *context
;
1295 switch (state
->cfa_rule
)
1297 case RULE_EXPRESSION
:
1298 cfa
= *(ULONG_PTR
*)eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1300 case RULE_VAL_EXPRESSION
:
1301 cfa
= eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1304 cfa
= *(ULONG_PTR
*)get_context_reg( context
, state
->cfa_reg
) + state
->cfa_offset
;
1309 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
1311 switch (state
->rules
[i
])
1314 case RULE_UNDEFINED
:
1317 case RULE_CFA_OFFSET
:
1318 set_context_reg( &new_context
, i
, (char *)cfa
+ state
->regs
[i
] );
1320 case RULE_OTHER_REG
:
1321 set_context_reg( &new_context
, i
, get_context_reg( context
, state
->regs
[i
] ));
1323 case RULE_EXPRESSION
:
1324 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1325 set_context_reg( &new_context
, i
, (void *)value
);
1327 case RULE_VAL_EXPRESSION
:
1328 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1329 set_context_reg( &new_context
, i
, &value
);
1333 new_context
.Rsp
= cfa
;
1334 *context
= new_context
;
1338 /***********************************************************************
1339 * dwarf_virtual_unwind
1341 * Equivalent of RtlVirtualUnwind for builtin modules.
1343 static NTSTATUS
dwarf_virtual_unwind( ULONG64 ip
, ULONG64
*frame
,CONTEXT
*context
,
1344 const struct dwarf_fde
*fde
, const struct dwarf_eh_bases
*bases
,
1345 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1347 const struct dwarf_cie
*cie
;
1348 const unsigned char *ptr
, *augmentation
, *end
;
1349 ULONG_PTR len
, code_end
;
1350 struct frame_info info
;
1351 struct frame_state state_stack
[MAX_SAVED_STATES
];
1352 int aug_z_format
= 0;
1353 unsigned char lsda_encoding
= DW_EH_PE_omit
;
1355 memset( &info
, 0, sizeof(info
) );
1356 info
.state_stack
= state_stack
;
1357 info
.ip
= (ULONG_PTR
)bases
->func
;
1360 cie
= (const struct dwarf_cie
*)((const char *)&fde
->cie_offset
- fde
->cie_offset
);
1362 /* parse the CIE first */
1364 if (cie
->version
!= 1 && cie
->version
!= 3)
1366 FIXME( "unknown CIE version %u at %p\n", cie
->version
, cie
);
1367 return STATUS_INVALID_DISPOSITION
;
1369 ptr
= cie
->augmentation
+ strlen((const char *)cie
->augmentation
) + 1;
1371 info
.code_align
= dwarf_get_uleb128( &ptr
);
1372 info
.data_align
= dwarf_get_sleb128( &ptr
);
1373 if (cie
->version
== 1)
1374 info
.retaddr_reg
= *ptr
++;
1376 info
.retaddr_reg
= dwarf_get_uleb128( &ptr
);
1377 info
.state
.cfa_rule
= RULE_CFA_OFFSET
;
1379 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1380 ip
, bases
->func
, cie
, cie
->length
, cie
->id
, cie
->version
, cie
->augmentation
,
1381 info
.code_align
, info
.data_align
, dwarf_reg_names
[info
.retaddr_reg
] );
1384 for (augmentation
= cie
->augmentation
; *augmentation
; augmentation
++)
1386 switch (*augmentation
)
1389 len
= dwarf_get_uleb128( &ptr
);
1394 lsda_encoding
= *ptr
++;
1398 unsigned char encoding
= *ptr
++;
1399 *handler
= (void *)dwarf_get_ptr( &ptr
, encoding
);
1403 info
.fde_encoding
= *ptr
++;
1406 info
.signal_frame
= 1;
1409 FIXME( "unknown augmentation '%c'\n", *augmentation
);
1410 if (!end
) return STATUS_INVALID_DISPOSITION
; /* cannot continue */
1415 end
= (const unsigned char *)(&cie
->length
+ 1) + cie
->length
;
1416 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1418 ptr
= (const unsigned char *)(fde
+ 1);
1419 info
.ip
= dwarf_get_ptr( &ptr
, info
.fde_encoding
); /* fde code start */
1420 code_end
= info
.ip
+ dwarf_get_ptr( &ptr
, info
.fde_encoding
& 0x0f ); /* fde code length */
1422 if (aug_z_format
) /* get length of augmentation data */
1424 len
= dwarf_get_uleb128( &ptr
);
1429 *handler_data
= (void *)dwarf_get_ptr( &ptr
, lsda_encoding
);
1432 end
= (const unsigned char *)(&fde
->length
+ 1) + fde
->length
;
1433 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1434 fde
, fde
->length
, *handler
, *handler_data
, info
.ip
, code_end
);
1435 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1436 *frame
= context
->Rsp
;
1437 apply_frame_state( context
, &info
.state
);
1439 TRACE( "next function rip=%016lx\n", context
->Rip
);
1440 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1441 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1442 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1443 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1444 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1445 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1446 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1447 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1449 return STATUS_SUCCESS
;
1453 #ifdef HAVE_LIBUNWIND_H
1454 /***********************************************************************
1455 * libunwind_set_cursor_from_context
1457 static int libunwind_set_cursor_from_context( unw_cursor_t
*cursor
, CONTEXT
*context
, ULONG64 ip
)
1461 rc
= unw_set_reg(cursor
, UNW_REG_IP
, ip
);
1462 if (rc
== UNW_ESUCCESS
)
1463 rc
= unw_set_reg(cursor
, UNW_REG_SP
, context
->Rsp
);
1464 if (rc
== UNW_ESUCCESS
)
1465 rc
= unw_set_reg(cursor
, UNW_X86_64_RAX
, context
->Rax
);
1466 if (rc
== UNW_ESUCCESS
)
1467 rc
= unw_set_reg(cursor
, UNW_X86_64_RDX
, context
->Rdx
);
1468 if (rc
== UNW_ESUCCESS
)
1469 rc
= unw_set_reg(cursor
, UNW_X86_64_RCX
, context
->Rcx
);
1470 if (rc
== UNW_ESUCCESS
)
1471 rc
= unw_set_reg(cursor
, UNW_X86_64_RBX
, context
->Rbx
);
1472 if (rc
== UNW_ESUCCESS
)
1473 rc
= unw_set_reg(cursor
, UNW_X86_64_RSI
, context
->Rsi
);
1474 if (rc
== UNW_ESUCCESS
)
1475 rc
= unw_set_reg(cursor
, UNW_X86_64_RDI
, context
->Rdi
);
1476 if (rc
== UNW_ESUCCESS
)
1477 rc
= unw_set_reg(cursor
, UNW_X86_64_RBP
, context
->Rbp
);
1478 if (rc
== UNW_ESUCCESS
)
1479 rc
= unw_set_reg(cursor
, UNW_X86_64_R8
, context
->R8
);
1480 if (rc
== UNW_ESUCCESS
)
1481 rc
= unw_set_reg(cursor
, UNW_X86_64_R9
, context
->R9
);
1482 if (rc
== UNW_ESUCCESS
)
1483 rc
= unw_set_reg(cursor
, UNW_X86_64_R10
, context
->R10
);
1484 if (rc
== UNW_ESUCCESS
)
1485 rc
= unw_set_reg(cursor
, UNW_X86_64_R11
, context
->R11
);
1486 if (rc
== UNW_ESUCCESS
)
1487 rc
= unw_set_reg(cursor
, UNW_X86_64_R12
, context
->R12
);
1488 if (rc
== UNW_ESUCCESS
)
1489 rc
= unw_set_reg(cursor
, UNW_X86_64_R13
, context
->R13
);
1490 if (rc
== UNW_ESUCCESS
)
1491 rc
= unw_set_reg(cursor
, UNW_X86_64_R14
, context
->R14
);
1492 if (rc
== UNW_ESUCCESS
)
1493 rc
= unw_set_reg(cursor
, UNW_X86_64_R15
, context
->R15
);
1499 /***********************************************************************
1502 static int libunwind_get_reg( unw_cursor_t
*cursor
, unw_regnum_t reg
, ULONG64
*val
)
1507 rc
= unw_get_reg(cursor
, reg
, &word
);
1508 if (rc
== UNW_ESUCCESS
)
1515 /***********************************************************************
1516 * libunwind_set_context_from_cursor
1518 static BOOL
libunwind_set_context_from_cursor( CONTEXT
*context
, unw_cursor_t
*cursor
)
1522 rc
= libunwind_get_reg(cursor
, UNW_REG_IP
, &context
->Rip
);
1523 if (rc
== UNW_ESUCCESS
)
1524 rc
= libunwind_get_reg(cursor
, UNW_REG_SP
, &context
->Rsp
);
1525 if (rc
== UNW_ESUCCESS
)
1526 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RAX
, &context
->Rax
);
1527 if (rc
== UNW_ESUCCESS
)
1528 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RDX
, &context
->Rdx
);
1529 if (rc
== UNW_ESUCCESS
)
1530 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RCX
, &context
->Rcx
);
1531 if (rc
== UNW_ESUCCESS
)
1532 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RBX
, &context
->Rbx
);
1533 if (rc
== UNW_ESUCCESS
)
1534 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RSI
, &context
->Rsi
);
1535 if (rc
== UNW_ESUCCESS
)
1536 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RDI
, &context
->Rdi
);
1537 if (rc
== UNW_ESUCCESS
)
1538 rc
= libunwind_get_reg(cursor
, UNW_X86_64_RBP
, &context
->Rbp
);
1539 if (rc
== UNW_ESUCCESS
)
1540 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R8
, &context
->R8
);
1541 if (rc
== UNW_ESUCCESS
)
1542 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R9
, &context
->R9
);
1543 if (rc
== UNW_ESUCCESS
)
1544 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R10
, &context
->R10
);
1545 if (rc
== UNW_ESUCCESS
)
1546 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R11
, &context
->R11
);
1547 if (rc
== UNW_ESUCCESS
)
1548 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R12
, &context
->R12
);
1549 if (rc
== UNW_ESUCCESS
)
1550 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R13
, &context
->R13
);
1551 if (rc
== UNW_ESUCCESS
)
1552 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R14
, &context
->R14
);
1553 if (rc
== UNW_ESUCCESS
)
1554 rc
= libunwind_get_reg(cursor
, UNW_X86_64_R15
, &context
->R15
);
1560 /***********************************************************************
1561 * libunwind_virtual_unwind
1563 * Equivalent of RtlVirtualUnwind for builtin modules.
1565 static NTSTATUS
libunwind_virtual_unwind( ULONG64 ip
, BOOL
* got_info
, ULONG64
*frame
, CONTEXT
*context
,
1566 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1568 unw_context_t unw_context
;
1569 unw_cursor_t cursor
;
1570 unw_proc_info_t info
;
1573 rc
= unw_getcontext( &unw_context
);
1574 if (rc
== UNW_ESUCCESS
)
1575 rc
= unw_init_local( &cursor
, &unw_context
);
1576 if (rc
== UNW_ESUCCESS
)
1577 rc
= libunwind_set_cursor_from_context( &cursor
, context
, ip
- 1 );
1578 if (rc
!= UNW_ESUCCESS
)
1580 WARN( "setup failed: %d\n", rc
);
1581 return STATUS_INVALID_DISPOSITION
;
1584 rc
= unw_get_proc_info(&cursor
, &info
);
1585 if (rc
!= UNW_ESUCCESS
&& rc
!= UNW_ENOINFO
)
1587 WARN( "failed to get info: %d\n", rc
);
1588 return STATUS_INVALID_DISPOSITION
;
1590 if (rc
== UNW_ENOINFO
|| ip
< info
.start_ip
|| info
.end_ip
<= ip
|| !info
.format
)
1593 return STATUS_SUCCESS
;
1596 TRACE( "ip %#lx function %#lx-%#lx personality %#lx lsda %#lx fde %#lx\n",
1597 ip
, (unsigned long)info
.start_ip
, (unsigned long)info
.end_ip
, (unsigned long)info
.handler
,
1598 (unsigned long)info
.lsda
, (unsigned long)info
.unwind_info
);
1600 rc
= unw_step(&cursor
);
1603 WARN( "failed to unwind: %d\n", rc
);
1604 return STATUS_INVALID_DISPOSITION
;
1607 *frame
= context
->Rsp
;
1609 rc
= libunwind_set_context_from_cursor( context
, &cursor
);
1610 if (rc
!= UNW_ESUCCESS
)
1612 WARN( "failed to update context after unwind: %d\n", rc
);
1613 return STATUS_INVALID_DISPOSITION
;
1616 *handler
= (void*)info
.handler
;
1617 *handler_data
= (void*)info
.lsda
;
1620 TRACE( "next function rip=%016lx\n", context
->Rip
);
1621 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1622 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1623 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1624 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1625 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1626 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1627 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1628 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1630 return STATUS_SUCCESS
;
1635 /***********************************************************************
1638 static inline int dispatch_signal(unsigned int sig
)
1640 if (handlers
[sig
] == NULL
) return 0;
1641 return handlers
[sig
](sig
);
1644 /***********************************************************************
1647 * Get the base of the signal stack for the current thread.
1649 static inline void *get_signal_stack(void)
1651 return (char *)NtCurrentTeb() + teb_size
;
1654 /***********************************************************************
1655 * is_inside_signal_stack
1657 * Check if pointer is inside the signal stack.
1659 static inline BOOL
is_inside_signal_stack( void *ptr
)
1661 return ((char *)ptr
>= (char *)get_signal_stack() &&
1662 (char *)ptr
< (char *)get_signal_stack() + signal_stack_size
);
1665 /***********************************************************************
1668 * Set the register values from a sigcontext.
1670 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
1672 context
->ContextFlags
= CONTEXT_CONTROL
| CONTEXT_INTEGER
| CONTEXT_SEGMENTS
| CONTEXT_DEBUG_REGISTERS
;
1673 context
->Rax
= RAX_sig(sigcontext
);
1674 context
->Rcx
= RCX_sig(sigcontext
);
1675 context
->Rdx
= RDX_sig(sigcontext
);
1676 context
->Rbx
= RBX_sig(sigcontext
);
1677 context
->Rsp
= RSP_sig(sigcontext
);
1678 context
->Rbp
= RBP_sig(sigcontext
);
1679 context
->Rsi
= RSI_sig(sigcontext
);
1680 context
->Rdi
= RDI_sig(sigcontext
);
1681 context
->R8
= R8_sig(sigcontext
);
1682 context
->R9
= R9_sig(sigcontext
);
1683 context
->R10
= R10_sig(sigcontext
);
1684 context
->R11
= R11_sig(sigcontext
);
1685 context
->R12
= R12_sig(sigcontext
);
1686 context
->R13
= R13_sig(sigcontext
);
1687 context
->R14
= R14_sig(sigcontext
);
1688 context
->R15
= R15_sig(sigcontext
);
1689 context
->Rip
= RIP_sig(sigcontext
);
1690 context
->SegCs
= CS_sig(sigcontext
);
1691 context
->SegFs
= FS_sig(sigcontext
);
1692 context
->SegGs
= GS_sig(sigcontext
);
1693 context
->EFlags
= EFL_sig(sigcontext
);
1695 context
->SegDs
= DS_sig(sigcontext
);
1697 __asm__("movw %%ds,%0" : "=m" (context
->SegDs
));
1700 context
->SegEs
= ES_sig(sigcontext
);
1702 __asm__("movw %%es,%0" : "=m" (context
->SegEs
));
1705 context
->SegSs
= SS_sig(sigcontext
);
1707 __asm__("movw %%ss,%0" : "=m" (context
->SegSs
));
1709 context
->Dr0
= amd64_thread_data()->dr0
;
1710 context
->Dr1
= amd64_thread_data()->dr1
;
1711 context
->Dr2
= amd64_thread_data()->dr2
;
1712 context
->Dr3
= amd64_thread_data()->dr3
;
1713 context
->Dr6
= amd64_thread_data()->dr6
;
1714 context
->Dr7
= amd64_thread_data()->dr7
;
1715 if (FPU_sig(sigcontext
))
1717 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1718 context
->u
.FltSave
= *FPU_sig(sigcontext
);
1719 context
->MxCsr
= context
->u
.FltSave
.MxCsr
;
1724 /***********************************************************************
1727 * Build a sigcontext from the register values.
1729 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1731 amd64_thread_data()->dr0
= context
->Dr0
;
1732 amd64_thread_data()->dr1
= context
->Dr1
;
1733 amd64_thread_data()->dr2
= context
->Dr2
;
1734 amd64_thread_data()->dr3
= context
->Dr3
;
1735 amd64_thread_data()->dr6
= context
->Dr6
;
1736 amd64_thread_data()->dr7
= context
->Dr7
;
1737 RAX_sig(sigcontext
) = context
->Rax
;
1738 RCX_sig(sigcontext
) = context
->Rcx
;
1739 RDX_sig(sigcontext
) = context
->Rdx
;
1740 RBX_sig(sigcontext
) = context
->Rbx
;
1741 RSP_sig(sigcontext
) = context
->Rsp
;
1742 RBP_sig(sigcontext
) = context
->Rbp
;
1743 RSI_sig(sigcontext
) = context
->Rsi
;
1744 RDI_sig(sigcontext
) = context
->Rdi
;
1745 R8_sig(sigcontext
) = context
->R8
;
1746 R9_sig(sigcontext
) = context
->R9
;
1747 R10_sig(sigcontext
) = context
->R10
;
1748 R11_sig(sigcontext
) = context
->R11
;
1749 R12_sig(sigcontext
) = context
->R12
;
1750 R13_sig(sigcontext
) = context
->R13
;
1751 R14_sig(sigcontext
) = context
->R14
;
1752 R15_sig(sigcontext
) = context
->R15
;
1753 RIP_sig(sigcontext
) = context
->Rip
;
1754 CS_sig(sigcontext
) = context
->SegCs
;
1755 FS_sig(sigcontext
) = context
->SegFs
;
1756 GS_sig(sigcontext
) = context
->SegGs
;
1757 EFL_sig(sigcontext
) = context
->EFlags
;
1759 DS_sig(sigcontext
) = context
->SegDs
;
1762 ES_sig(sigcontext
) = context
->SegEs
;
1765 SS_sig(sigcontext
) = context
->SegSs
;
1767 if (FPU_sig(sigcontext
)) *FPU_sig(sigcontext
) = context
->u
.FltSave
;
1771 /**************************************************************************
1772 * __chkstk (NTDLL.@)
1774 * Supposed to touch all the stack pages, but we shouldn't need that.
1776 __ASM_GLOBAL_FUNC( __chkstk
, "ret" );
1779 /***********************************************************************
1780 * RtlCaptureContext (NTDLL.@)
1782 __ASM_GLOBAL_FUNC( RtlCaptureContext
,
1784 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1785 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1786 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1787 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1788 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1789 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1790 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1791 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1792 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1793 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1794 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1795 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1796 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1797 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1798 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1799 "leaq 8(%rsp),%rax\n\t"
1800 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1801 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1802 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1803 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1804 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1805 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1806 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1807 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1808 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1809 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1810 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1811 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1812 "movq (%rsp),%rax\n\t"
1813 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1814 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1815 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1816 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1817 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1818 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1819 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1820 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1821 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1822 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1823 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1824 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1825 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1826 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1827 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1828 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1829 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1830 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1833 /***********************************************************************
1834 * set_full_cpu_context
1836 * Set the new CPU context.
1838 extern void set_full_cpu_context( const CONTEXT
*context
);
1839 __ASM_GLOBAL_FUNC( set_full_cpu_context
,
1841 __ASM_CFI(".cfi_adjust_cfa_offset 40\n\t")
1842 "ldmxcsr 0x34(%rdi)\n\t" /* context->MxCsr */
1843 "movw 0x38(%rdi),%ax\n\t" /* context->SegCs */
1844 "movq %rax,8(%rsp)\n\t"
1845 "movw 0x42(%rdi),%ax\n\t" /* context->SegSs */
1846 "movq %rax,32(%rsp)\n\t"
1847 "movq 0x44(%rdi),%rax\n\t" /* context->Eflags */
1848 "movq %rax,16(%rsp)\n\t"
1849 "movq 0x80(%rdi),%rcx\n\t" /* context->Rcx */
1850 "movq 0x88(%rdi),%rdx\n\t" /* context->Rdx */
1851 "movq 0x90(%rdi),%rbx\n\t" /* context->Rbx */
1852 "movq 0x98(%rdi),%rax\n\t" /* context->Rsp */
1853 "movq %rax,24(%rsp)\n\t"
1854 "movq 0xa0(%rdi),%rbp\n\t" /* context->Rbp */
1855 "movq 0xa8(%rdi),%rsi\n\t" /* context->Rsi */
1856 "movq 0xb8(%rdi),%r8\n\t" /* context->R8 */
1857 "movq 0xc0(%rdi),%r9\n\t" /* context->R9 */
1858 "movq 0xc8(%rdi),%r10\n\t" /* context->R10 */
1859 "movq 0xd0(%rdi),%r11\n\t" /* context->R11 */
1860 "movq 0xd8(%rdi),%r12\n\t" /* context->R12 */
1861 "movq 0xe0(%rdi),%r13\n\t" /* context->R13 */
1862 "movq 0xe8(%rdi),%r14\n\t" /* context->R14 */
1863 "movq 0xf0(%rdi),%r15\n\t" /* context->R15 */
1864 "movq 0xf8(%rdi),%rax\n\t" /* context->Rip */
1865 "movq %rax,(%rsp)\n\t"
1866 "fxrstor 0x100(%rdi)\n\t" /* context->FtlSave */
1867 "movdqa 0x1a0(%rdi),%xmm0\n\t" /* context->Xmm0 */
1868 "movdqa 0x1b0(%rdi),%xmm1\n\t" /* context->Xmm1 */
1869 "movdqa 0x1c0(%rdi),%xmm2\n\t" /* context->Xmm2 */
1870 "movdqa 0x1d0(%rdi),%xmm3\n\t" /* context->Xmm3 */
1871 "movdqa 0x1e0(%rdi),%xmm4\n\t" /* context->Xmm4 */
1872 "movdqa 0x1f0(%rdi),%xmm5\n\t" /* context->Xmm5 */
1873 "movdqa 0x200(%rdi),%xmm6\n\t" /* context->Xmm6 */
1874 "movdqa 0x210(%rdi),%xmm7\n\t" /* context->Xmm7 */
1875 "movdqa 0x220(%rdi),%xmm8\n\t" /* context->Xmm8 */
1876 "movdqa 0x230(%rdi),%xmm9\n\t" /* context->Xmm9 */
1877 "movdqa 0x240(%rdi),%xmm10\n\t" /* context->Xmm10 */
1878 "movdqa 0x250(%rdi),%xmm11\n\t" /* context->Xmm11 */
1879 "movdqa 0x260(%rdi),%xmm12\n\t" /* context->Xmm12 */
1880 "movdqa 0x270(%rdi),%xmm13\n\t" /* context->Xmm13 */
1881 "movdqa 0x280(%rdi),%xmm14\n\t" /* context->Xmm14 */
1882 "movdqa 0x290(%rdi),%xmm15\n\t" /* context->Xmm15 */
1883 "movq 0x78(%rdi),%rax\n\t" /* context->Rax */
1884 "movq 0xb0(%rdi),%rdi\n\t" /* context->Rdi */
1888 /***********************************************************************
1891 * Set the new CPU context. Used by NtSetContextThread.
1893 void DECLSPEC_HIDDEN
set_cpu_context( const CONTEXT
*context
)
1895 DWORD flags
= context
->ContextFlags
& ~CONTEXT_AMD64
;
1897 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1899 amd64_thread_data()->dr0
= context
->Dr0
;
1900 amd64_thread_data()->dr1
= context
->Dr1
;
1901 amd64_thread_data()->dr2
= context
->Dr2
;
1902 amd64_thread_data()->dr3
= context
->Dr3
;
1903 amd64_thread_data()->dr6
= context
->Dr6
;
1904 amd64_thread_data()->dr7
= context
->Dr7
;
1906 if (flags
& CONTEXT_FULL
)
1908 if (!(flags
& CONTEXT_CONTROL
))
1909 FIXME( "setting partial context (%x) not supported\n", flags
);
1911 set_full_cpu_context( context
);
1916 /***********************************************************************
1917 * get_server_context_flags
1919 * Convert CPU-specific flags to generic server flags
1921 static unsigned int get_server_context_flags( DWORD flags
)
1923 unsigned int ret
= 0;
1925 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1926 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
1927 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
1928 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
1929 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
1930 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
1935 /***********************************************************************
1938 * Copy a register context according to the flags.
1940 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1942 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1943 if (flags
& CONTEXT_CONTROL
)
1945 to
->Rbp
= from
->Rbp
;
1946 to
->Rip
= from
->Rip
;
1947 to
->Rsp
= from
->Rsp
;
1948 to
->SegCs
= from
->SegCs
;
1949 to
->SegSs
= from
->SegSs
;
1950 to
->EFlags
= from
->EFlags
;
1952 if (flags
& CONTEXT_INTEGER
)
1954 to
->Rax
= from
->Rax
;
1955 to
->Rcx
= from
->Rcx
;
1956 to
->Rdx
= from
->Rdx
;
1957 to
->Rbx
= from
->Rbx
;
1958 to
->Rsi
= from
->Rsi
;
1959 to
->Rdi
= from
->Rdi
;
1962 to
->R10
= from
->R10
;
1963 to
->R11
= from
->R11
;
1964 to
->R12
= from
->R12
;
1965 to
->R13
= from
->R13
;
1966 to
->R14
= from
->R14
;
1967 to
->R15
= from
->R15
;
1969 if (flags
& CONTEXT_SEGMENTS
)
1971 to
->SegDs
= from
->SegDs
;
1972 to
->SegEs
= from
->SegEs
;
1973 to
->SegFs
= from
->SegFs
;
1974 to
->SegGs
= from
->SegGs
;
1976 if (flags
& CONTEXT_FLOATING_POINT
)
1978 to
->MxCsr
= from
->MxCsr
;
1979 to
->u
.FltSave
= from
->u
.FltSave
;
1981 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1983 to
->Dr0
= from
->Dr0
;
1984 to
->Dr1
= from
->Dr1
;
1985 to
->Dr2
= from
->Dr2
;
1986 to
->Dr3
= from
->Dr3
;
1987 to
->Dr6
= from
->Dr6
;
1988 to
->Dr7
= from
->Dr7
;
1993 /***********************************************************************
1996 * Convert a register context to the server format.
1998 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
2000 DWORD flags
= from
->ContextFlags
& ~CONTEXT_AMD64
; /* get rid of CPU id */
2002 memset( to
, 0, sizeof(*to
) );
2003 to
->cpu
= CPU_x86_64
;
2005 if (flags
& CONTEXT_CONTROL
)
2007 to
->flags
|= SERVER_CTX_CONTROL
;
2008 to
->ctl
.x86_64_regs
.rbp
= from
->Rbp
;
2009 to
->ctl
.x86_64_regs
.rip
= from
->Rip
;
2010 to
->ctl
.x86_64_regs
.rsp
= from
->Rsp
;
2011 to
->ctl
.x86_64_regs
.cs
= from
->SegCs
;
2012 to
->ctl
.x86_64_regs
.ss
= from
->SegSs
;
2013 to
->ctl
.x86_64_regs
.flags
= from
->EFlags
;
2015 if (flags
& CONTEXT_INTEGER
)
2017 to
->flags
|= SERVER_CTX_INTEGER
;
2018 to
->integer
.x86_64_regs
.rax
= from
->Rax
;
2019 to
->integer
.x86_64_regs
.rcx
= from
->Rcx
;
2020 to
->integer
.x86_64_regs
.rdx
= from
->Rdx
;
2021 to
->integer
.x86_64_regs
.rbx
= from
->Rbx
;
2022 to
->integer
.x86_64_regs
.rsi
= from
->Rsi
;
2023 to
->integer
.x86_64_regs
.rdi
= from
->Rdi
;
2024 to
->integer
.x86_64_regs
.r8
= from
->R8
;
2025 to
->integer
.x86_64_regs
.r9
= from
->R9
;
2026 to
->integer
.x86_64_regs
.r10
= from
->R10
;
2027 to
->integer
.x86_64_regs
.r11
= from
->R11
;
2028 to
->integer
.x86_64_regs
.r12
= from
->R12
;
2029 to
->integer
.x86_64_regs
.r13
= from
->R13
;
2030 to
->integer
.x86_64_regs
.r14
= from
->R14
;
2031 to
->integer
.x86_64_regs
.r15
= from
->R15
;
2033 if (flags
& CONTEXT_SEGMENTS
)
2035 to
->flags
|= SERVER_CTX_SEGMENTS
;
2036 to
->seg
.x86_64_regs
.ds
= from
->SegDs
;
2037 to
->seg
.x86_64_regs
.es
= from
->SegEs
;
2038 to
->seg
.x86_64_regs
.fs
= from
->SegFs
;
2039 to
->seg
.x86_64_regs
.gs
= from
->SegGs
;
2041 if (flags
& CONTEXT_FLOATING_POINT
)
2043 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
2044 memcpy( to
->fp
.x86_64_regs
.fpregs
, &from
->u
.FltSave
, sizeof(to
->fp
.x86_64_regs
.fpregs
) );
2046 if (flags
& CONTEXT_DEBUG_REGISTERS
)
2048 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
2049 to
->debug
.x86_64_regs
.dr0
= from
->Dr0
;
2050 to
->debug
.x86_64_regs
.dr1
= from
->Dr1
;
2051 to
->debug
.x86_64_regs
.dr2
= from
->Dr2
;
2052 to
->debug
.x86_64_regs
.dr3
= from
->Dr3
;
2053 to
->debug
.x86_64_regs
.dr6
= from
->Dr6
;
2054 to
->debug
.x86_64_regs
.dr7
= from
->Dr7
;
2056 return STATUS_SUCCESS
;
2060 /***********************************************************************
2061 * context_from_server
2063 * Convert a register context from the server format.
2065 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
2067 if (from
->cpu
!= CPU_x86_64
) return STATUS_INVALID_PARAMETER
;
2069 to
->ContextFlags
= CONTEXT_AMD64
;
2070 if (from
->flags
& SERVER_CTX_CONTROL
)
2072 to
->ContextFlags
|= CONTEXT_CONTROL
;
2073 to
->Rbp
= from
->ctl
.x86_64_regs
.rbp
;
2074 to
->Rip
= from
->ctl
.x86_64_regs
.rip
;
2075 to
->Rsp
= from
->ctl
.x86_64_regs
.rsp
;
2076 to
->SegCs
= from
->ctl
.x86_64_regs
.cs
;
2077 to
->SegSs
= from
->ctl
.x86_64_regs
.ss
;
2078 to
->EFlags
= from
->ctl
.x86_64_regs
.flags
;
2081 if (from
->flags
& SERVER_CTX_INTEGER
)
2083 to
->ContextFlags
|= CONTEXT_INTEGER
;
2084 to
->Rax
= from
->integer
.x86_64_regs
.rax
;
2085 to
->Rcx
= from
->integer
.x86_64_regs
.rcx
;
2086 to
->Rdx
= from
->integer
.x86_64_regs
.rdx
;
2087 to
->Rbx
= from
->integer
.x86_64_regs
.rbx
;
2088 to
->Rsi
= from
->integer
.x86_64_regs
.rsi
;
2089 to
->Rdi
= from
->integer
.x86_64_regs
.rdi
;
2090 to
->R8
= from
->integer
.x86_64_regs
.r8
;
2091 to
->R9
= from
->integer
.x86_64_regs
.r9
;
2092 to
->R10
= from
->integer
.x86_64_regs
.r10
;
2093 to
->R11
= from
->integer
.x86_64_regs
.r11
;
2094 to
->R12
= from
->integer
.x86_64_regs
.r12
;
2095 to
->R13
= from
->integer
.x86_64_regs
.r13
;
2096 to
->R14
= from
->integer
.x86_64_regs
.r14
;
2097 to
->R15
= from
->integer
.x86_64_regs
.r15
;
2099 if (from
->flags
& SERVER_CTX_SEGMENTS
)
2101 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
2102 to
->SegDs
= from
->seg
.x86_64_regs
.ds
;
2103 to
->SegEs
= from
->seg
.x86_64_regs
.es
;
2104 to
->SegFs
= from
->seg
.x86_64_regs
.fs
;
2105 to
->SegGs
= from
->seg
.x86_64_regs
.gs
;
2107 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
2109 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
2110 memcpy( &to
->u
.FltSave
, from
->fp
.x86_64_regs
.fpregs
, sizeof(from
->fp
.x86_64_regs
.fpregs
) );
2111 to
->MxCsr
= to
->u
.FltSave
.MxCsr
;
2113 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
2115 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
2116 to
->Dr0
= from
->debug
.x86_64_regs
.dr0
;
2117 to
->Dr1
= from
->debug
.x86_64_regs
.dr1
;
2118 to
->Dr2
= from
->debug
.x86_64_regs
.dr2
;
2119 to
->Dr3
= from
->debug
.x86_64_regs
.dr3
;
2120 to
->Dr6
= from
->debug
.x86_64_regs
.dr6
;
2121 to
->Dr7
= from
->debug
.x86_64_regs
.dr7
;
2123 return STATUS_SUCCESS
;
2127 /***********************************************************************
2128 * NtSetContextThread (NTDLL.@)
2129 * ZwSetContextThread (NTDLL.@)
2131 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
2133 NTSTATUS ret
= STATUS_SUCCESS
;
2134 BOOL self
= (handle
== GetCurrentThread());
2136 /* debug registers require a server call */
2137 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
)))
2138 self
= (amd64_thread_data()->dr0
== context
->Dr0
&&
2139 amd64_thread_data()->dr1
== context
->Dr1
&&
2140 amd64_thread_data()->dr2
== context
->Dr2
&&
2141 amd64_thread_data()->dr3
== context
->Dr3
&&
2142 amd64_thread_data()->dr6
== context
->Dr6
&&
2143 amd64_thread_data()->dr7
== context
->Dr7
);
2147 context_t server_context
;
2148 context_to_server( &server_context
, context
);
2149 ret
= set_thread_context( handle
, &server_context
, &self
);
2151 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
2156 /***********************************************************************
2157 * NtGetContextThread (NTDLL.@)
2158 * ZwGetContextThread (NTDLL.@)
2160 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
2164 BOOL self
= (handle
== GetCurrentThread());
2166 if (!context
) return STATUS_INVALID_PARAMETER
;
2168 needed_flags
= context
->ContextFlags
;
2170 /* debug registers require a server call */
2171 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
)) self
= FALSE
;
2175 context_t server_context
;
2176 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
2178 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
2179 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
2180 needed_flags
&= ~context
->ContextFlags
;
2188 RtlCaptureContext( &ctx
);
2189 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
2190 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
2192 /* update the cached version of the debug registers */
2193 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
))
2195 amd64_thread_data()->dr0
= context
->Dr0
;
2196 amd64_thread_data()->dr1
= context
->Dr1
;
2197 amd64_thread_data()->dr2
= context
->Dr2
;
2198 amd64_thread_data()->dr3
= context
->Dr3
;
2199 amd64_thread_data()->dr6
= context
->Dr6
;
2200 amd64_thread_data()->dr7
= context
->Dr7
;
2203 return STATUS_SUCCESS
;
2207 /***********************************************************************
2208 * wow64_get_server_context_flags
2210 static unsigned int wow64_get_server_context_flags( DWORD flags
)
2212 unsigned int ret
= 0;
2214 flags
&= ~WOW64_CONTEXT_i386
; /* get rid of CPU id */
2215 if (flags
& WOW64_CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
2216 if (flags
& WOW64_CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
2217 if (flags
& WOW64_CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
2218 if (flags
& WOW64_CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
2219 if (flags
& WOW64_CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
2220 if (flags
& WOW64_CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
2224 /***********************************************************************
2225 * wow64_context_from_server
2227 static NTSTATUS
wow64_context_from_server( WOW64_CONTEXT
*to
, const context_t
*from
)
2229 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
2231 to
->ContextFlags
= WOW64_CONTEXT_i386
;
2232 if (from
->flags
& SERVER_CTX_CONTROL
)
2234 to
->ContextFlags
|= WOW64_CONTEXT_CONTROL
;
2235 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
2236 to
->Esp
= from
->ctl
.i386_regs
.esp
;
2237 to
->Eip
= from
->ctl
.i386_regs
.eip
;
2238 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
2239 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
2240 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
2242 if (from
->flags
& SERVER_CTX_INTEGER
)
2244 to
->ContextFlags
|= WOW64_CONTEXT_INTEGER
;
2245 to
->Eax
= from
->integer
.i386_regs
.eax
;
2246 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
2247 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
2248 to
->Edx
= from
->integer
.i386_regs
.edx
;
2249 to
->Esi
= from
->integer
.i386_regs
.esi
;
2250 to
->Edi
= from
->integer
.i386_regs
.edi
;
2252 if (from
->flags
& SERVER_CTX_SEGMENTS
)
2254 to
->ContextFlags
|= WOW64_CONTEXT_SEGMENTS
;
2255 to
->SegDs
= from
->seg
.i386_regs
.ds
;
2256 to
->SegEs
= from
->seg
.i386_regs
.es
;
2257 to
->SegFs
= from
->seg
.i386_regs
.fs
;
2258 to
->SegGs
= from
->seg
.i386_regs
.gs
;
2260 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
2262 to
->ContextFlags
|= WOW64_CONTEXT_FLOATING_POINT
;
2263 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
2264 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
2265 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
2266 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
2267 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
2268 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
2269 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
2270 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
2271 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
2273 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
2275 to
->ContextFlags
|= WOW64_CONTEXT_DEBUG_REGISTERS
;
2276 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
2277 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
2278 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
2279 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
2280 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
2281 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
2283 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
2285 to
->ContextFlags
|= WOW64_CONTEXT_EXTENDED_REGISTERS
;
2286 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
2288 return STATUS_SUCCESS
;
2291 /***********************************************************************
2292 * wow64_context_to_server
2294 static void wow64_context_to_server( context_t
*to
, const WOW64_CONTEXT
*from
)
2296 DWORD flags
= from
->ContextFlags
& ~WOW64_CONTEXT_i386
; /* get rid of CPU id */
2298 memset( to
, 0, sizeof(*to
) );
2301 if (flags
& WOW64_CONTEXT_CONTROL
)
2303 to
->flags
|= SERVER_CTX_CONTROL
;
2304 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
2305 to
->ctl
.i386_regs
.esp
= from
->Esp
;
2306 to
->ctl
.i386_regs
.eip
= from
->Eip
;
2307 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
2308 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
2309 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
2311 if (flags
& WOW64_CONTEXT_INTEGER
)
2313 to
->flags
|= SERVER_CTX_INTEGER
;
2314 to
->integer
.i386_regs
.eax
= from
->Eax
;
2315 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
2316 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
2317 to
->integer
.i386_regs
.edx
= from
->Edx
;
2318 to
->integer
.i386_regs
.esi
= from
->Esi
;
2319 to
->integer
.i386_regs
.edi
= from
->Edi
;
2321 if (flags
& WOW64_CONTEXT_SEGMENTS
)
2323 to
->flags
|= SERVER_CTX_SEGMENTS
;
2324 to
->seg
.i386_regs
.ds
= from
->SegDs
;
2325 to
->seg
.i386_regs
.es
= from
->SegEs
;
2326 to
->seg
.i386_regs
.fs
= from
->SegFs
;
2327 to
->seg
.i386_regs
.gs
= from
->SegGs
;
2329 if (flags
& WOW64_CONTEXT_FLOATING_POINT
)
2331 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
2332 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
2333 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
2334 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
2335 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
2336 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
2337 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
2338 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
2339 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
2340 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
2342 if (flags
& WOW64_CONTEXT_DEBUG_REGISTERS
)
2344 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
2345 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
2346 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
2347 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
2348 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
2349 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
2350 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
2352 if (flags
& WOW64_CONTEXT_EXTENDED_REGISTERS
)
2354 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
2355 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
2360 /******************************************************************************
2361 * RtlWow64GetThreadContext (NTDLL.@)
2363 NTSTATUS WINAPI
RtlWow64GetThreadContext( HANDLE handle
, WOW64_CONTEXT
*context
)
2367 context_t server_context
;
2368 unsigned int server_flags
= wow64_get_server_context_flags( context
->ContextFlags
);
2370 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
2371 if (self
) return STATUS_INVALID_PARAMETER
;
2372 return wow64_context_from_server( context
, &server_context
);
2376 /******************************************************************************
2377 * RtlWow64SetThreadContext (NTDLL.@)
2379 NTSTATUS WINAPI
RtlWow64SetThreadContext( HANDLE handle
, const WOW64_CONTEXT
*context
)
2382 context_t server_context
;
2384 wow64_context_to_server( &server_context
, context
);
2385 return set_thread_context( handle
, &server_context
, &self
);
2389 extern void raise_func_trampoline( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
2390 __ASM_GLOBAL_FUNC( raise_func_trampoline
,
2391 __ASM_CFI(".cfi_signal_frame\n\t")
2392 __ASM_CFI(".cfi_def_cfa %rbp,160\n\t") /* red zone + rip + rbp + rdi + rsi */
2393 __ASM_CFI(".cfi_rel_offset %rip,24\n\t")
2394 __ASM_CFI(".cfi_rel_offset %rbp,16\n\t")
2395 __ASM_CFI(".cfi_rel_offset %rdi,8\n\t")
2396 __ASM_CFI(".cfi_rel_offset %rsi,0\n\t")
2400 /***********************************************************************
2403 * Setup a proper stack frame for the raise function, and modify the
2404 * sigcontext so that the return from the signal handler will call
2405 * the raise function.
2407 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
2412 EXCEPTION_RECORD rec
;
2417 ULONG64 red_zone
[16];
2420 DWORD exception_code
= 0;
2422 stack
= (struct stack_layout
*)(RSP_sig(sigcontext
) & ~15);
2424 /* stack sanity checks */
2426 if (is_inside_signal_stack( stack
))
2428 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2429 GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
), (ULONG_PTR
)RSP_sig(sigcontext
),
2430 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2434 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
2435 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
2436 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
2438 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2439 GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
), (ULONG_PTR
)RSP_sig(sigcontext
),
2440 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2442 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
2444 /* stack overflow on last page, unrecoverable */
2445 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
2446 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2447 diff
, GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
),
2448 (ULONG_PTR
)RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
2449 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2452 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
2454 /* stack access below stack limit, may be recoverable */
2455 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
2458 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
2459 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2460 diff
, GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
),
2461 (ULONG_PTR
)RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
2462 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2467 stack
--; /* push the stack_layout structure */
2468 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
2469 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
2470 #elif defined(VALGRIND_MAKE_WRITABLE)
2471 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
2473 stack
->rec
.ExceptionRecord
= NULL
;
2474 stack
->rec
.ExceptionCode
= exception_code
;
2475 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2476 stack
->rec
.ExceptionAddress
= (void *)RIP_sig(sigcontext
);
2477 stack
->rec
.NumberParameters
= 0;
2478 save_context( &stack
->context
, sigcontext
);
2480 /* store return address and %rbp without aligning, so that the offset is fixed */
2481 rsp_ptr
= (ULONG64
*)RSP_sig(sigcontext
) - 16;
2482 *(--rsp_ptr
) = RIP_sig(sigcontext
);
2483 *(--rsp_ptr
) = RBP_sig(sigcontext
);
2484 *(--rsp_ptr
) = RDI_sig(sigcontext
);
2485 *(--rsp_ptr
) = RSI_sig(sigcontext
);
2487 /* now modify the sigcontext to return to the raise function */
2488 RIP_sig(sigcontext
) = (ULONG_PTR
)raise_func_trampoline
;
2489 RDI_sig(sigcontext
) = (ULONG_PTR
)&stack
->rec
;
2490 RSI_sig(sigcontext
) = (ULONG_PTR
)&stack
->context
;
2491 RDX_sig(sigcontext
) = (ULONG_PTR
)func
;
2492 RBP_sig(sigcontext
) = (ULONG_PTR
)rsp_ptr
;
2493 RSP_sig(sigcontext
) = (ULONG_PTR
)stack
;
2494 /* clear single-step, direction, and align check flag */
2495 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
2501 /***********************************************************************
2502 * get_exception_context
2504 * Get a pointer to the context built by setup_exception.
2506 static inline CONTEXT
*get_exception_context( EXCEPTION_RECORD
*rec
)
2508 return (CONTEXT
*)rec
- 1; /* cf. stack_layout structure */
2512 /**********************************************************************
2513 * find_function_info
2515 static RUNTIME_FUNCTION
*find_function_info( ULONG64 pc
, HMODULE module
,
2516 RUNTIME_FUNCTION
*func
, ULONG size
)
2519 int max
= size
/sizeof(*func
) - 1;
2523 int pos
= (min
+ max
) / 2;
2524 if ((char *)pc
< (char *)module
+ func
[pos
].BeginAddress
) max
= pos
- 1;
2525 else if ((char *)pc
>= (char *)module
+ func
[pos
].EndAddress
) min
= pos
+ 1;
2529 while (func
->UnwindData
& 1) /* follow chained entry */
2530 func
= (RUNTIME_FUNCTION
*)((char *)module
+ (func
->UnwindData
& ~1));
2537 /**********************************************************************
2538 * lookup_function_info
2540 static RUNTIME_FUNCTION
*lookup_function_info( ULONG64 pc
, ULONG64
*base
, LDR_MODULE
**module
)
2542 RUNTIME_FUNCTION
*func
= NULL
;
2543 struct dynamic_unwind_entry
*entry
;
2546 /* PE module or wine module */
2547 if (!LdrFindEntryForAddress( (void *)pc
, module
))
2549 *base
= (ULONG64
)(*module
)->BaseAddress
;
2550 if ((func
= RtlImageDirectoryEntryToData( (*module
)->BaseAddress
, TRUE
,
2551 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
2553 /* lookup in function table */
2554 func
= find_function_info( pc
, (*module
)->BaseAddress
, func
, size
);
2561 RtlEnterCriticalSection( &dynamic_unwind_section
);
2562 LIST_FOR_EACH_ENTRY( entry
, &dynamic_unwind_list
, struct dynamic_unwind_entry
, entry
)
2564 if (pc
>= entry
->base
&& pc
< entry
->base
+ entry
->size
)
2566 *base
= entry
->base
;
2568 /* use callback or lookup in function table */
2569 if (entry
->callback
)
2570 func
= entry
->callback( pc
, entry
->context
);
2572 func
= find_function_info( pc
, (HMODULE
)entry
->base
, entry
->table
, entry
->table_size
);
2576 RtlLeaveCriticalSection( &dynamic_unwind_section
);
2582 static DWORD
nested_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
2583 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
2585 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)) return ExceptionContinueSearch
;
2588 return ExceptionNestedException
;
2591 /**********************************************************************
2594 * Call a single exception handler.
2595 * FIXME: Handle nested exceptions.
2597 static DWORD
call_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
)
2599 EXCEPTION_REGISTRATION_RECORD frame
;
2602 frame
.Handler
= nested_exception_handler
;
2603 __wine_push_frame( &frame
);
2605 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2606 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2607 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, context
, dispatch
);
2608 TRACE( "handler at %p returned %u\n", dispatch
->LanguageHandler
, res
);
2610 __wine_pop_frame( &frame
);
2615 /**********************************************************************
2618 * Call a single exception handler from the TEB chain.
2619 * FIXME: Handle nested exceptions.
2621 static DWORD
call_teb_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
,
2622 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
2626 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
2627 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
2628 res
= teb_frame
->Handler( rec
, teb_frame
, context
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
2629 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
2634 /**********************************************************************
2635 * call_stack_handlers
2637 * Call the stack handlers chain.
2639 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
2641 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2642 UNWIND_HISTORY_TABLE table
;
2643 DISPATCHER_CONTEXT dispatch
;
2648 context
= *orig_context
;
2649 dispatch
.TargetIp
= 0;
2650 dispatch
.ContextRecord
= &context
;
2651 dispatch
.HistoryTable
= &table
;
2654 /* FIXME: should use the history table to make things faster */
2656 dispatch
.ImageBase
= 0;
2657 dispatch
.ControlPc
= context
.Rip
;
2658 dispatch
.ScopeIndex
= 0;
2660 /* first look for PE exception information */
2662 if ((dispatch
.FunctionEntry
= lookup_function_info( dispatch
.ControlPc
, &dispatch
.ImageBase
, &module
)))
2664 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_EHANDLER
, dispatch
.ImageBase
,
2665 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2666 &context
, &dispatch
.HandlerData
,
2667 &dispatch
.EstablisherFrame
, NULL
);
2671 /* then look for host system exception information */
2673 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
2675 BOOL got_info
= FALSE
;
2676 struct dwarf_eh_bases bases
;
2677 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(dispatch
.ControlPc
- 1), &bases
);
2681 status
= dwarf_virtual_unwind( dispatch
.ControlPc
, &dispatch
.EstablisherFrame
, &context
,
2682 fde
, &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2683 if (status
!= STATUS_SUCCESS
) return status
;
2686 #ifdef HAVE_LIBUNWIND_H
2689 status
= libunwind_virtual_unwind( dispatch
.ControlPc
, &got_info
, &dispatch
.EstablisherFrame
, &context
,
2690 &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2691 if (status
!= STATUS_SUCCESS
) return status
;
2697 dispatch
.FunctionEntry
= NULL
;
2698 if (dispatch
.LanguageHandler
&& !module
)
2700 FIXME( "calling personality routine in system library not supported yet\n" );
2701 dispatch
.LanguageHandler
= NULL
;
2706 else WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
2708 context
.Rip
= *(ULONG64
*)context
.Rsp
;
2709 context
.Rsp
= context
.Rsp
+ sizeof(ULONG64
);
2710 dispatch
.EstablisherFrame
= context
.Rsp
;
2711 dispatch
.LanguageHandler
= NULL
;
2714 if (!dispatch
.EstablisherFrame
) break;
2716 if ((dispatch
.EstablisherFrame
& 7) ||
2717 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
2718 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
2720 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
2721 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2722 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
2726 if (dispatch
.LanguageHandler
)
2728 switch (call_handler( rec
, orig_context
, &dispatch
))
2730 case ExceptionContinueExecution
:
2731 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
2732 return STATUS_SUCCESS
;
2733 case ExceptionContinueSearch
:
2735 case ExceptionNestedException
:
2736 FIXME( "nested exception\n" );
2738 case ExceptionCollidedUnwind
: {
2741 context
= *dispatch
.ContextRecord
;
2742 dispatch
.ContextRecord
= &context
;
2743 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
2744 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2745 &context
, NULL
, &frame
, NULL
);
2749 return STATUS_INVALID_DISPOSITION
;
2752 /* hack: call wine handlers registered in the tib list */
2753 else while ((ULONG64
)teb_frame
< context
.Rsp
)
2755 TRACE( "found wine frame %p rsp %lx handler %p\n",
2756 teb_frame
, context
.Rsp
, teb_frame
->Handler
);
2757 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
2758 switch (call_teb_handler( rec
, orig_context
, &dispatch
, teb_frame
))
2760 case ExceptionContinueExecution
:
2761 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
2762 return STATUS_SUCCESS
;
2763 case ExceptionContinueSearch
:
2765 case ExceptionNestedException
:
2766 FIXME( "nested exception\n" );
2768 case ExceptionCollidedUnwind
: {
2771 context
= *dispatch
.ContextRecord
;
2772 dispatch
.ContextRecord
= &context
;
2773 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
2774 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2775 &context
, NULL
, &frame
, NULL
);
2776 teb_frame
= teb_frame
->Prev
;
2780 return STATUS_INVALID_DISPOSITION
;
2782 teb_frame
= teb_frame
->Prev
;
2785 if (context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
2787 return STATUS_UNHANDLED_EXCEPTION
;
2791 /*******************************************************************
2792 * NtRaiseException (NTDLL.@)
2794 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2802 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2803 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
2804 context
->Rip
, GetCurrentThreadId() );
2805 for (c
= 0; c
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); c
++)
2806 TRACE( " info[%d]=%016lx\n", c
, rec
->ExceptionInformation
[c
] );
2807 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
2809 if (rec
->ExceptionInformation
[1] >> 16)
2810 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2811 rec
->ExceptionAddress
,
2812 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
2814 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2815 rec
->ExceptionAddress
,
2816 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
2820 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2821 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
2822 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2823 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
2824 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2825 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
2826 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2827 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
2829 status
= send_debug_event( rec
, TRUE
, context
);
2830 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
) goto done
;
2832 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
2833 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Rip
--;
2835 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
) goto done
;
2837 if ((status
= call_stack_handlers( rec
, context
)) == STATUS_SUCCESS
) goto done
;
2838 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
2841 /* last chance exception */
2843 status
= send_debug_event( rec
, FALSE
, context
);
2844 if (status
!= DBG_CONTINUE
)
2846 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
2847 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2848 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
2849 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2851 ERR("Unhandled exception code %x flags %x addr %p\n",
2852 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
2853 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
2857 return NtSetContextThread( GetCurrentThread(), context
);
2861 /**********************************************************************
2862 * raise_segv_exception
2864 static void raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2868 switch(rec
->ExceptionCode
)
2870 case EXCEPTION_ACCESS_VIOLATION
:
2871 if (rec
->NumberParameters
== 2)
2873 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
2874 rec
->ExceptionInformation
[0], FALSE
)))
2875 set_cpu_context( context
);
2878 case EXCEPTION_BREAKPOINT
:
2879 switch (rec
->ExceptionInformation
[0])
2881 case 1: /* BREAKPOINT_PRINT */
2882 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2883 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2884 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2885 set_cpu_context( context
);
2889 status
= NtRaiseException( rec
, context
, TRUE
);
2890 raise_status( status
, rec
);
2894 /**********************************************************************
2895 * raise_trap_exception
2897 static void raise_trap_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2901 if (rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
)
2903 /* when single stepping can't tell whether this is a hw bp or a
2904 * single step interrupt. try to avoid as much overhead as possible
2905 * and only do a server call if there is any hw bp enabled. */
2907 if( !(context
->EFlags
& 0x100) || (amd64_thread_data()->dr7
& 0xff) )
2909 /* (possible) hardware breakpoint, fetch the debug registers */
2910 DWORD saved_flags
= context
->ContextFlags
;
2911 context
->ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
2912 NtGetContextThread(GetCurrentThread(), context
);
2913 context
->ContextFlags
|= saved_flags
; /* restore flags */
2916 context
->EFlags
&= ~0x100; /* clear single-step flag */
2919 status
= NtRaiseException( rec
, context
, TRUE
);
2920 raise_status( status
, rec
);
2924 /**********************************************************************
2925 * raise_generic_exception
2927 * Generic raise function for exceptions that don't need special treatment.
2929 static void raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2931 NTSTATUS status
= NtRaiseException( rec
, context
, TRUE
);
2932 raise_status( status
, rec
);
2936 /***********************************************************************
2937 * is_privileged_instr
2939 * Check if the fault location is a privileged instruction.
2941 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
2943 const BYTE
*instr
= (BYTE
*)context
->Rip
;
2944 unsigned int prefix_count
= 0;
2946 for (;;) switch(*instr
)
2948 /* instruction prefixes */
2949 case 0x2e: /* %cs: */
2950 case 0x36: /* %ss: */
2951 case 0x3e: /* %ds: */
2952 case 0x26: /* %es: */
2953 case 0x40: /* rex */
2954 case 0x41: /* rex */
2955 case 0x42: /* rex */
2956 case 0x43: /* rex */
2957 case 0x44: /* rex */
2958 case 0x45: /* rex */
2959 case 0x46: /* rex */
2960 case 0x47: /* rex */
2961 case 0x48: /* rex */
2962 case 0x49: /* rex */
2963 case 0x4a: /* rex */
2964 case 0x4b: /* rex */
2965 case 0x4c: /* rex */
2966 case 0x4d: /* rex */
2967 case 0x4e: /* rex */
2968 case 0x4f: /* rex */
2969 case 0x64: /* %fs: */
2970 case 0x65: /* %gs: */
2971 case 0x66: /* opcode size */
2972 case 0x67: /* addr size */
2973 case 0xf0: /* lock */
2974 case 0xf2: /* repne */
2975 case 0xf3: /* repe */
2976 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
2980 case 0x0f: /* extended instruction */
2983 case 0x06: /* clts */
2984 case 0x08: /* invd */
2985 case 0x09: /* wbinvd */
2986 case 0x20: /* mov crX, reg */
2987 case 0x21: /* mov drX, reg */
2988 case 0x22: /* mov reg, crX */
2989 case 0x23: /* mov reg drX */
2990 return EXCEPTION_PRIV_INSTRUCTION
;
2993 case 0x6c: /* insb (%dx) */
2994 case 0x6d: /* insl (%dx) */
2995 case 0x6e: /* outsb (%dx) */
2996 case 0x6f: /* outsl (%dx) */
2997 case 0xcd: /* int $xx */
2998 case 0xe4: /* inb al,XX */
2999 case 0xe5: /* in (e)ax,XX */
3000 case 0xe6: /* outb XX,al */
3001 case 0xe7: /* out XX,(e)ax */
3002 case 0xec: /* inb (%dx),%al */
3003 case 0xed: /* inl (%dx),%eax */
3004 case 0xee: /* outb %al,(%dx) */
3005 case 0xef: /* outl %eax,(%dx) */
3006 case 0xf4: /* hlt */
3007 case 0xfa: /* cli */
3008 case 0xfb: /* sti */
3009 return EXCEPTION_PRIV_INSTRUCTION
;
3016 /***********************************************************************
3019 * Handle an interrupt.
3021 static inline BOOL
handle_interrupt( unsigned int interrupt
, EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
3026 rec
->ExceptionCode
= STATUS_ASSERTION_FAILURE
;
3030 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
3031 rec
->ExceptionAddress
= (void *)context
->Rip
;
3032 rec
->NumberParameters
= 1;
3033 rec
->ExceptionInformation
[0] = context
->Rax
;
3041 /**********************************************************************
3044 * Handler for SIGSEGV and related errors.
3046 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3048 EXCEPTION_RECORD
*rec
;
3049 ucontext_t
*ucontext
= sigcontext
;
3051 /* check for page fault inside the thread stack */
3052 if (TRAP_sig(ucontext
) == TRAP_x86_PAGEFLT
&&
3053 (char *)siginfo
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
3054 (char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
3055 virtual_handle_stack_fault( siginfo
->si_addr
))
3057 /* check if this was the last guard page */
3058 if ((char *)siginfo
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
3060 rec
= setup_exception( sigcontext
, raise_segv_exception
);
3061 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
3066 rec
= setup_exception( sigcontext
, raise_segv_exception
);
3067 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
3069 switch(TRAP_sig(ucontext
))
3071 case TRAP_x86_OFLOW
: /* Overflow exception */
3072 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
3074 case TRAP_x86_BOUND
: /* Bound range exception */
3075 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
3077 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
3078 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
3080 case TRAP_x86_STKFLT
: /* Stack fault */
3081 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
3083 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
3084 case TRAP_x86_PROTFLT
: /* General protection fault */
3085 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
3087 CONTEXT
*win_context
= get_exception_context( rec
);
3088 WORD err
= ERROR_sig(ucontext
);
3089 if (!err
&& (rec
->ExceptionCode
= is_privileged_instr( win_context
))) break;
3090 if ((err
& 7) == 2 && handle_interrupt( err
>> 3, rec
, win_context
)) break;
3091 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
3092 rec
->NumberParameters
= 2;
3093 rec
->ExceptionInformation
[0] = 0;
3094 rec
->ExceptionInformation
[1] = 0xffffffffffffffff;
3097 case TRAP_x86_PAGEFLT
: /* Page fault */
3098 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
3099 rec
->NumberParameters
= 2;
3100 rec
->ExceptionInformation
[0] = (ERROR_sig(ucontext
) >> 1) & 0x09;
3101 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
3103 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
3104 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
3107 ERR( "Got unexpected trap %ld\n", (ULONG_PTR
)TRAP_sig(ucontext
) );
3109 case TRAP_x86_NMI
: /* NMI interrupt */
3110 case TRAP_x86_DNA
: /* Device not available exception */
3111 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
3112 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
3113 case TRAP_x86_MCHK
: /* Machine check exception */
3114 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
3115 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
3120 /**********************************************************************
3123 * Handler for SIGTRAP.
3125 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3127 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_trap_exception
);
3129 switch (siginfo
->si_code
)
3131 case TRAP_TRACE
: /* Single-step exception */
3132 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
3133 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
3135 case TRAP_BRKPT
: /* Breakpoint exception */
3136 /* Check if this is actuallly icebp instruction */
3137 if (((unsigned char *)rec
->ExceptionAddress
)[-1] == 0xF1)
3139 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
3142 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
3145 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
3146 rec
->NumberParameters
= 1;
3147 rec
->ExceptionInformation
[0] = 0;
3152 /**********************************************************************
3155 * Handler for SIGFPE.
3157 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3159 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
3161 switch (siginfo
->si_code
)
3164 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
3167 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
3170 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
3173 rec
->ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3176 rec
->ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
3179 rec
->ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
3182 rec
->ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
3186 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
3191 /**********************************************************************
3194 * Handler for SIGINT.
3196 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3198 if (!dispatch_signal(SIGINT
))
3200 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
3201 rec
->ExceptionCode
= CONTROL_C_EXIT
;
3206 /**********************************************************************
3209 * Handler for SIGABRT.
3211 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3213 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
3214 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
3215 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
3219 /**********************************************************************
3222 * Handler for SIGQUIT.
3224 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
3230 /**********************************************************************
3233 * Handler for SIGUSR1, used to signal a thread that it got suspended.
3235 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
3239 save_context( &context
, ucontext
);
3240 wait_suspend( &context
);
3241 restore_context( &context
, ucontext
);
3245 /***********************************************************************
3246 * __wine_set_signal_handler (NTDLL.@)
3248 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
3250 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
3251 if (handlers
[sig
] != NULL
) return -2;
3252 handlers
[sig
] = wsh
;
3257 /**********************************************************************
3258 * signal_alloc_thread
3260 NTSTATUS
signal_alloc_thread( TEB
**teb
)
3262 static size_t sigstack_zero_bits
;
3266 if (!sigstack_zero_bits
)
3268 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
3269 /* find the first power of two not smaller than min_size */
3270 sigstack_zero_bits
= 12;
3271 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
3272 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
3273 assert( sizeof(TEB
) <= teb_size
);
3276 size
= 1 << sigstack_zero_bits
;
3278 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
3279 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
3281 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
3282 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
3288 /**********************************************************************
3289 * signal_free_thread
3291 void signal_free_thread( TEB
*teb
)
3295 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
3299 /**********************************************************************
3302 static void *mac_thread_gsbase(void)
3304 static int gsbase_offset
= -1;
3307 if (gsbase_offset
< 0)
3309 /* Search for the array of TLS slots within the pthread data structure.
3310 That's what the macOS pthread implementation uses for gsbase. */
3311 const void* const sentinel1
= (const void*)0x2bffb6b4f11228ae;
3312 const void* const sentinel2
= (const void*)0x0845a7ff6ab76707;
3315 const void** p
= (const void**)pthread_self();
3319 if ((rc
= pthread_key_create(&key
, NULL
)))
3321 ERR("failed to create sentinel key for gsbase search: %d\n", rc
);
3325 pthread_setspecific(key
, sentinel1
);
3327 for (i
= key
+ 1; i
< 2000; i
++) /* arbitrary limit */
3329 if (p
[i
] == sentinel1
)
3331 pthread_setspecific(key
, sentinel2
);
3333 if (p
[i
] == sentinel2
)
3335 gsbase_offset
= (i
- key
) * sizeof(*p
);
3339 pthread_setspecific(key
, sentinel1
);
3343 pthread_key_delete(key
);
3348 ret
= (char*)pthread_self() + gsbase_offset
;
3349 TRACE("pthread_self() %p + offset 0x%08x -> gsbase %p\n", pthread_self(), gsbase_offset
, ret
);
3354 ERR("failed to locate gsbase; won't be able to poke ThreadLocalStoragePointer into pthread TLS; expect crashes\n");
3362 /**********************************************************************
3363 * signal_init_thread
3365 void signal_init_thread( TEB
*teb
)
3367 const WORD fpu_cw
= 0x27f;
3370 #if defined __linux__
3371 arch_prctl( ARCH_SET_GS
, teb
);
3372 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
3373 amd64_set_gsbase( teb
);
3374 #elif defined(__NetBSD__)
3375 sysarch( X86_64_SET_GSBASE
, &teb
);
3376 #elif defined (__APPLE__)
3377 __asm__
volatile (".byte 0x65\n\tmovq %0,%c1"
3379 : "r" (teb
->Tib
.Self
), "n" (FIELD_OFFSET(TEB
, Tib
.Self
)));
3380 __asm__
volatile (".byte 0x65\n\tmovq %0,%c1"
3382 : "r" (teb
->ThreadLocalStoragePointer
), "n" (FIELD_OFFSET(TEB
, ThreadLocalStoragePointer
)));
3384 /* alloc_tls_slot() needs to poke a value to an address relative to each
3385 thread's gsbase. Have each thread record its gsbase pointer into its
3386 TEB so alloc_tls_slot() can find it. */
3387 teb
->Reserved5
[0] = mac_thread_gsbase();
3389 # error Please define setting %gs for your architecture
3392 ss
.ss_sp
= (char *)teb
+ teb_size
;
3393 ss
.ss_size
= signal_stack_size
;
3395 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
3398 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
3400 FIXME("FPU setup not implemented for this platform.\n");
3404 /**********************************************************************
3405 * signal_init_process
3407 void signal_init_process(void)
3409 struct sigaction sig_act
;
3411 sig_act
.sa_mask
= server_block_set
;
3412 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
| SA_ONSTACK
;
3414 sig_act
.sa_sigaction
= int_handler
;
3415 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
3416 sig_act
.sa_sigaction
= fpe_handler
;
3417 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
3418 sig_act
.sa_sigaction
= abrt_handler
;
3419 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
3420 sig_act
.sa_sigaction
= quit_handler
;
3421 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
3422 sig_act
.sa_sigaction
= usr1_handler
;
3423 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
3425 sig_act
.sa_sigaction
= segv_handler
;
3426 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
3427 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
3429 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
3433 sig_act
.sa_sigaction
= trap_handler
;
3434 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
3439 perror("sigaction");
3444 /**********************************************************************
3445 * RtlAddFunctionTable (NTDLL.@)
3447 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, DWORD64 addr
)
3449 struct dynamic_unwind_entry
*entry
;
3451 TRACE( "%p %u %lx\n", table
, count
, addr
);
3453 /* NOTE: Windows doesn't check if table is aligned or a NULL pointer */
3455 entry
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry
) );
3460 entry
->size
= table
[count
- 1].EndAddress
;
3461 entry
->table
= table
;
3462 entry
->table_size
= count
* sizeof(RUNTIME_FUNCTION
);
3463 entry
->callback
= NULL
;
3464 entry
->context
= NULL
;
3466 RtlEnterCriticalSection( &dynamic_unwind_section
);
3467 list_add_tail( &dynamic_unwind_list
, &entry
->entry
);
3468 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3474 /**********************************************************************
3475 * RtlInstallFunctionTableCallback (NTDLL.@)
3477 BOOLEAN CDECL
RtlInstallFunctionTableCallback( DWORD64 table
, DWORD64 base
, DWORD length
,
3478 PGET_RUNTIME_FUNCTION_CALLBACK callback
, PVOID context
, PCWSTR dll
)
3480 struct dynamic_unwind_entry
*entry
;
3482 TRACE( "%lx %lx %d %p %p %s\n", table
, base
, length
, callback
, context
, wine_dbgstr_w(dll
) );
3484 /* NOTE: Windows doesn't check if the provided callback is a NULL pointer */
3486 /* both low-order bits must be set */
3487 if ((table
& 0x3) != 0x3)
3490 entry
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry
) );
3495 entry
->size
= length
;
3496 entry
->table
= (RUNTIME_FUNCTION
*)table
;
3497 entry
->table_size
= 0;
3498 entry
->callback
= callback
;
3499 entry
->context
= context
;
3501 RtlEnterCriticalSection( &dynamic_unwind_section
);
3502 list_add_tail( &dynamic_unwind_list
, &entry
->entry
);
3503 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3509 /**********************************************************************
3510 * RtlDeleteFunctionTable (NTDLL.@)
3512 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
3514 struct dynamic_unwind_entry
*entry
, *to_free
= NULL
;
3516 TRACE( "%p\n", table
);
3518 RtlEnterCriticalSection( &dynamic_unwind_section
);
3519 LIST_FOR_EACH_ENTRY( entry
, &dynamic_unwind_list
, struct dynamic_unwind_entry
, entry
)
3521 if (entry
->table
== table
)
3524 list_remove( &entry
->entry
);
3528 RtlLeaveCriticalSection( &dynamic_unwind_section
);
3533 RtlFreeHeap( GetProcessHeap(), 0, to_free
);
3538 /**********************************************************************
3539 * RtlLookupFunctionEntry (NTDLL.@)
3541 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG64 pc
, ULONG64
*base
, UNWIND_HISTORY_TABLE
*table
)
3544 RUNTIME_FUNCTION
*func
;
3546 /* FIXME: should use the history table to make things faster */
3548 func
= lookup_function_info( pc
, base
, &module
);
3553 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
3555 WARN( "module not found for %lx\n", pc
);
3561 static ULONG64
get_int_reg( CONTEXT
*context
, int reg
)
3563 return *(&context
->Rax
+ reg
);
3566 static void set_int_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, ULONG64
*val
)
3568 *(&context
->Rax
+ reg
) = *val
;
3569 if (ctx_ptr
) ctx_ptr
->u2
.IntegerContext
[reg
] = val
;
3572 static void set_float_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, M128A
*val
)
3574 *(&context
->u
.s
.Xmm0
+ reg
) = *val
;
3575 if (ctx_ptr
) ctx_ptr
->u
.FloatingContext
[reg
] = val
;
3578 static int get_opcode_size( struct opcode op
)
3582 case UWOP_ALLOC_LARGE
:
3583 return 2 + (op
.info
!= 0);
3584 case UWOP_SAVE_NONVOL
:
3585 case UWOP_SAVE_XMM128
:
3587 case UWOP_SAVE_NONVOL_FAR
:
3588 case UWOP_SAVE_XMM128_FAR
:
3595 static BOOL
is_inside_epilog( BYTE
*pc
, ULONG64 base
, const RUNTIME_FUNCTION
*function
)
3597 /* add or lea must be the first instruction, and it must have a rex.W prefix */
3598 if ((pc
[0] & 0xf8) == 0x48)
3602 case 0x81: /* add $nnnn,%rsp */
3603 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
3609 case 0x83: /* add $n,%rsp */
3610 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
3616 case 0x8d: /* lea n(reg),%rsp */
3617 if (pc
[0] & 0x06) return FALSE
; /* rex.RX must be cleared */
3618 if (((pc
[2] >> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
3619 if ((pc
[2] & 7) == 4) return FALSE
; /* no SIB byte allowed */
3620 if ((pc
[2] >> 6) == 1) /* 8-bit offset */
3625 if ((pc
[2] >> 6) == 2) /* 32-bit offset */
3634 /* now check for various pop instructions */
3638 if ((*pc
& 0xf0) == 0x40) pc
++; /* rex prefix */
3642 case 0x58: /* pop %rax/%r8 */
3643 case 0x59: /* pop %rcx/%r9 */
3644 case 0x5a: /* pop %rdx/%r10 */
3645 case 0x5b: /* pop %rbx/%r11 */
3646 case 0x5c: /* pop %rsp/%r12 */
3647 case 0x5d: /* pop %rbp/%r13 */
3648 case 0x5e: /* pop %rsi/%r14 */
3649 case 0x5f: /* pop %rdi/%r15 */
3652 case 0xc2: /* ret $nn */
3653 case 0xc3: /* ret */
3655 case 0xe9: /* jmp nnnn */
3656 pc
+= 5 + *(LONG
*)(pc
+ 1);
3657 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
3660 case 0xeb: /* jmp n */
3661 pc
+= 2 + (signed char)pc
[1];
3662 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
3665 case 0xf3: /* rep; ret (for amd64 prediction bug) */
3666 return pc
[1] == 0xc3;
3672 /* execute a function epilog, which must have been validated with is_inside_epilog() */
3673 static void interpret_epilog( BYTE
*pc
, CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
3679 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
3683 case 0x58: /* pop %rax/r8 */
3684 case 0x59: /* pop %rcx/r9 */
3685 case 0x5a: /* pop %rdx/r10 */
3686 case 0x5b: /* pop %rbx/r11 */
3687 case 0x5c: /* pop %rsp/r12 */
3688 case 0x5d: /* pop %rbp/r13 */
3689 case 0x5e: /* pop %rsi/r14 */
3690 case 0x5f: /* pop %rdi/r15 */
3691 set_int_reg( context
, ctx_ptr
, *pc
- 0x58 + (rex
& 1) * 8, (ULONG64
*)context
->Rsp
);
3692 context
->Rsp
+= sizeof(ULONG64
);
3695 case 0x81: /* add $nnnn,%rsp */
3696 context
->Rsp
+= *(LONG
*)(pc
+ 2);
3697 pc
+= 2 + sizeof(LONG
);
3699 case 0x83: /* add $n,%rsp */
3700 context
->Rsp
+= (signed char)pc
[2];
3704 if ((pc
[1] >> 6) == 1) /* lea n(reg),%rsp */
3706 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + (signed char)pc
[2];
3709 else /* lea nnnn(reg),%rsp */
3711 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + *(LONG
*)(pc
+ 2);
3712 pc
+= 2 + sizeof(LONG
);
3715 case 0xc2: /* ret $nn */
3716 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3717 context
->Rsp
+= sizeof(ULONG64
) + *(WORD
*)(pc
+ 1);
3719 case 0xc3: /* ret */
3720 case 0xf3: /* rep; ret */
3721 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3722 context
->Rsp
+= sizeof(ULONG64
);
3724 case 0xe9: /* jmp nnnn */
3725 pc
+= 5 + *(LONG
*)(pc
+ 1);
3727 case 0xeb: /* jmp n */
3728 pc
+= 2 + (signed char)pc
[1];
3735 /**********************************************************************
3736 * RtlVirtualUnwind (NTDLL.@)
3738 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG64 base
, ULONG64 pc
,
3739 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
3740 PVOID
*data
, ULONG64
*frame_ret
,
3741 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
3743 union handler_data
*handler_data
;
3745 struct UNWIND_INFO
*info
;
3746 unsigned int i
, prolog_offset
;
3748 TRACE( "type %x rip %lx rsp %lx\n", type
, pc
, context
->Rsp
);
3749 if (TRACE_ON(seh
)) dump_unwind_info( base
, function
);
3751 frame
= *frame_ret
= context
->Rsp
;
3754 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
3755 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
3757 if (info
->version
!= 1)
3759 FIXME( "unknown unwind info version %u at %p\n", info
->version
, info
);
3763 if (info
->frame_reg
)
3764 frame
= get_int_reg( context
, info
->frame_reg
) - info
->frame_offset
* 16;
3766 /* check if in prolog */
3767 if (pc
>= base
+ function
->BeginAddress
&& pc
< base
+ function
->BeginAddress
+ info
->prolog
)
3769 prolog_offset
= pc
- base
- function
->BeginAddress
;
3774 if (is_inside_epilog( (BYTE
*)pc
, base
, function
))
3776 interpret_epilog( (BYTE
*)pc
, context
, ctx_ptr
);
3782 for (i
= 0; i
< info
->count
; i
+= get_opcode_size(info
->opcodes
[i
]))
3784 if (prolog_offset
< info
->opcodes
[i
].offset
) continue; /* skip it */
3786 switch (info
->opcodes
[i
].code
)
3788 case UWOP_PUSH_NONVOL
: /* pushq %reg */
3789 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)context
->Rsp
);
3790 context
->Rsp
+= sizeof(ULONG64
);
3792 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
3793 if (info
->opcodes
[i
].info
) context
->Rsp
+= *(DWORD
*)&info
->opcodes
[i
+1];
3794 else context
->Rsp
+= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
3796 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
3797 context
->Rsp
+= (info
->opcodes
[i
].info
+ 1) * 8;
3799 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
3800 context
->Rsp
= *frame_ret
= frame
;
3802 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
3803 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 8;
3804 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
3806 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
3807 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
3808 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
3810 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
3811 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 16;
3812 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
3814 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
3815 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
3816 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
3818 case UWOP_PUSH_MACHFRAME
:
3819 FIXME( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
3822 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
3827 if (!(info
->flags
& UNW_FLAG_CHAININFO
)) break;
3828 function
= &handler_data
->chain
; /* restart with the chained info */
3831 /* now pop return address */
3832 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3833 context
->Rsp
+= sizeof(ULONG64
);
3835 if (!(info
->flags
& type
)) return NULL
; /* no matching handler */
3836 if (prolog_offset
!= ~0) return NULL
; /* inside prolog */
3838 *data
= &handler_data
->handler
+ 1;
3839 return (char *)base
+ handler_data
->handler
;
3842 struct unwind_exception_frame
3844 EXCEPTION_REGISTRATION_RECORD frame
;
3845 DISPATCHER_CONTEXT
*dispatch
;
3848 /**********************************************************************
3849 * unwind_exception_handler
3851 * Handler for exceptions happening while calling an unwind handler.
3853 static DWORD
unwind_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
3854 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
3856 struct unwind_exception_frame
*unwind_frame
= (struct unwind_exception_frame
*)frame
;
3857 DISPATCHER_CONTEXT
*dispatch
= (DISPATCHER_CONTEXT
*)dispatcher
;
3859 /* copy the original dispatcher into the current one, except for the TargetIp */
3860 dispatch
->ControlPc
= unwind_frame
->dispatch
->ControlPc
;
3861 dispatch
->ImageBase
= unwind_frame
->dispatch
->ImageBase
;
3862 dispatch
->FunctionEntry
= unwind_frame
->dispatch
->FunctionEntry
;
3863 dispatch
->EstablisherFrame
= unwind_frame
->dispatch
->EstablisherFrame
;
3864 dispatch
->ContextRecord
= unwind_frame
->dispatch
->ContextRecord
;
3865 dispatch
->LanguageHandler
= unwind_frame
->dispatch
->LanguageHandler
;
3866 dispatch
->HandlerData
= unwind_frame
->dispatch
->HandlerData
;
3867 dispatch
->HistoryTable
= unwind_frame
->dispatch
->HistoryTable
;
3868 dispatch
->ScopeIndex
= unwind_frame
->dispatch
->ScopeIndex
;
3869 TRACE( "detected collided unwind\n" );
3870 return ExceptionCollidedUnwind
;
3873 /**********************************************************************
3874 * call_unwind_handler
3876 * Call a single unwind handler.
3878 static DWORD
call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
3880 struct unwind_exception_frame frame
;
3883 frame
.frame
.Handler
= unwind_exception_handler
;
3884 frame
.dispatch
= dispatch
;
3885 __wine_push_frame( &frame
.frame
);
3887 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
3888 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
3889 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
3890 TRACE( "handler %p returned %x\n", dispatch
->LanguageHandler
, res
);
3892 __wine_pop_frame( &frame
.frame
);
3896 case ExceptionContinueSearch
:
3897 case ExceptionCollidedUnwind
:
3900 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
3908 /**********************************************************************
3909 * call_teb_unwind_handler
3911 * Call a single unwind handler from the TEB chain.
3913 static DWORD
call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
3914 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
3918 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
3919 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
3920 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
3921 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
3925 case ExceptionContinueSearch
:
3926 case ExceptionCollidedUnwind
:
3929 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
3937 /**********************************************************************
3938 * call_consolidate_callback
3940 * Wrapper function to call a consolidate callback from a fake frame.
3941 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
3942 * we have to skip all frames which were already processed. To do that we
3943 * trick the unwinding functions into thinking the call came from somewhere
3944 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
3945 * DW_CFA_expression, and the expressions have the following format:
3947 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
3948 * DW_OP_deref | Get *(%rbp + 0x10) == context
3949 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
3950 * [DW_OP_deref] | Dereference, only for CFA
3952 extern void * WINAPI
call_consolidate_callback( CONTEXT
*context
,
3953 void *(CALLBACK
*callback
)(EXCEPTION_RECORD
*),
3954 EXCEPTION_RECORD
*rec
);
3955 __ASM_GLOBAL_FUNC( call_consolidate_callback
,
3957 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
3958 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
3959 "movq %rsp,%rbp\n\t"
3960 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
3961 "subq $0x20,%rsp\n\t"
3962 "movq %rcx,0x10(%rbp)\n\t"
3963 __ASM_CFI(".cfi_remember_state\n\t")
3964 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
3965 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
3966 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
3967 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
3968 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
3969 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
3970 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
3971 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
3972 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
3973 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
3974 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
3975 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
3976 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
3977 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
3978 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
3979 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
3980 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
3981 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
3982 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
3983 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
3986 __ASM_CFI(".cfi_restore_state\n\t")
3987 "movq %rbp,%rsp\n\t"
3988 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
3990 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
3991 __ASM_CFI(".cfi_same_value %rbp\n\t")
3994 /*******************************************************************
3995 * RtlRestoreContext (NTDLL.@)
3997 void WINAPI
RtlRestoreContext( CONTEXT
*context
, EXCEPTION_RECORD
*rec
)
3999 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
4001 if (rec
&& rec
->ExceptionCode
== STATUS_LONGJUMP
&& rec
->NumberParameters
>= 1)
4003 struct MSVCRT_JUMP_BUFFER
*jmp
= (struct MSVCRT_JUMP_BUFFER
*)rec
->ExceptionInformation
[0];
4004 context
->Rbx
= jmp
->Rbx
;
4005 context
->Rsp
= jmp
->Rsp
;
4006 context
->Rbp
= jmp
->Rbp
;
4007 context
->Rsi
= jmp
->Rsi
;
4008 context
->Rdi
= jmp
->Rdi
;
4009 context
->R12
= jmp
->R12
;
4010 context
->R13
= jmp
->R13
;
4011 context
->R14
= jmp
->R14
;
4012 context
->R15
= jmp
->R15
;
4013 context
->Rip
= jmp
->Rip
;
4014 context
->u
.s
.Xmm6
= jmp
->Xmm6
;
4015 context
->u
.s
.Xmm7
= jmp
->Xmm7
;
4016 context
->u
.s
.Xmm8
= jmp
->Xmm8
;
4017 context
->u
.s
.Xmm9
= jmp
->Xmm9
;
4018 context
->u
.s
.Xmm10
= jmp
->Xmm10
;
4019 context
->u
.s
.Xmm11
= jmp
->Xmm11
;
4020 context
->u
.s
.Xmm12
= jmp
->Xmm12
;
4021 context
->u
.s
.Xmm13
= jmp
->Xmm13
;
4022 context
->u
.s
.Xmm14
= jmp
->Xmm14
;
4023 context
->u
.s
.Xmm15
= jmp
->Xmm15
;
4025 else if (rec
&& rec
->ExceptionCode
== STATUS_UNWIND_CONSOLIDATE
&& rec
->NumberParameters
>= 1)
4027 PVOID (CALLBACK
*consolidate
)(EXCEPTION_RECORD
*) = (void *)rec
->ExceptionInformation
[0];
4028 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate
, rec
);
4029 context
->Rip
= (ULONG64
)call_consolidate_callback( context
, consolidate
, rec
);
4032 /* hack: remove no longer accessible TEB frames */
4033 while ((ULONG64
)teb_frame
< context
->Rsp
)
4035 TRACE( "removing TEB frame: %p\n", teb_frame
);
4036 teb_frame
= __wine_pop_frame( teb_frame
);
4039 TRACE( "returning to %lx stack %lx\n", context
->Rip
, context
->Rsp
);
4040 set_cpu_context( context
);
4044 /*******************************************************************
4045 * RtlUnwindEx (NTDLL.@)
4047 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
4048 PVOID retval
, CONTEXT
*context
, UNWIND_HISTORY_TABLE
*table
)
4050 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
4051 EXCEPTION_RECORD record
;
4052 DISPATCHER_CONTEXT dispatch
;
4053 CONTEXT new_context
;
4058 RtlCaptureContext( context
);
4059 new_context
= *context
;
4061 /* build an exception record, if we do not have one */
4064 record
.ExceptionCode
= STATUS_UNWIND
;
4065 record
.ExceptionFlags
= 0;
4066 record
.ExceptionRecord
= NULL
;
4067 record
.ExceptionAddress
= (void *)context
->Rip
;
4068 record
.NumberParameters
= 0;
4072 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
4074 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
4075 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, context
->Rip
);
4076 for (i
= 0; i
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); i
++)
4077 TRACE( " info[%d]=%016lx\n", i
, rec
->ExceptionInformation
[i
] );
4078 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
4079 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
4080 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
4081 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
4082 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
4083 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
4084 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
4085 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
4087 dispatch
.EstablisherFrame
= context
->Rsp
;
4088 dispatch
.TargetIp
= (ULONG64
)target_ip
;
4089 dispatch
.ContextRecord
= context
;
4090 dispatch
.HistoryTable
= table
;
4094 /* FIXME: should use the history table to make things faster */
4096 dispatch
.ImageBase
= 0;
4097 dispatch
.ScopeIndex
= 0;
4098 dispatch
.ControlPc
= context
->Rip
;
4100 /* first look for PE exception information */
4102 if ((dispatch
.FunctionEntry
= lookup_function_info( context
->Rip
, &dispatch
.ImageBase
, &module
)))
4104 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_UHANDLER
, dispatch
.ImageBase
,
4105 context
->Rip
, dispatch
.FunctionEntry
,
4106 &new_context
, &dispatch
.HandlerData
,
4107 &dispatch
.EstablisherFrame
, NULL
);
4111 /* then look for host system exception information */
4113 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
4115 BOOL got_info
= FALSE
;
4116 struct dwarf_eh_bases bases
;
4117 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
->Rip
- 1), &bases
);
4121 status
= dwarf_virtual_unwind( context
->Rip
, &dispatch
.EstablisherFrame
, &new_context
, fde
,
4122 &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
4123 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
4126 #ifdef HAVE_LIBUNWIND_H
4129 status
= libunwind_virtual_unwind( context
->Rip
, &got_info
, &dispatch
.EstablisherFrame
, &new_context
,
4130 &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
4131 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
4137 dispatch
.FunctionEntry
= NULL
;
4138 if (dispatch
.LanguageHandler
&& !module
)
4140 FIXME( "calling personality routine in system library not supported yet\n" );
4141 dispatch
.LanguageHandler
= NULL
;
4146 else WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
4148 /* no exception information, treat as a leaf function */
4150 new_context
.Rip
= *(ULONG64
*)context
->Rsp
;
4151 new_context
.Rsp
= context
->Rsp
+ sizeof(ULONG64
);
4152 dispatch
.EstablisherFrame
= new_context
.Rsp
;
4153 dispatch
.LanguageHandler
= NULL
;
4156 if (!dispatch
.EstablisherFrame
) break;
4158 if (is_inside_signal_stack( (void *)dispatch
.EstablisherFrame
))
4160 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch
.EstablisherFrame
,
4161 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size
);
4162 *context
= new_context
;
4166 if ((dispatch
.EstablisherFrame
& 7) ||
4167 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
4168 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
4170 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
4171 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
4172 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
4176 if (dispatch
.LanguageHandler
)
4178 if (end_frame
&& (dispatch
.EstablisherFrame
> (ULONG64
)end_frame
))
4180 ERR( "invalid end frame %lx/%p\n", dispatch
.EstablisherFrame
, end_frame
);
4181 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
4183 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) rec
->ExceptionFlags
|= EH_TARGET_UNWIND
;
4184 if (call_unwind_handler( rec
, &dispatch
) == ExceptionCollidedUnwind
)
4188 *context
= new_context
= *dispatch
.ContextRecord
;
4189 dispatch
.ContextRecord
= context
;
4190 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
4191 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
4192 &new_context
, NULL
, &frame
, NULL
);
4193 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
4196 rec
->ExceptionFlags
&= ~EH_COLLIDED_UNWIND
;
4198 else /* hack: call builtin handlers registered in the tib list */
4200 DWORD64 backup_frame
= dispatch
.EstablisherFrame
;
4201 while ((ULONG64
)teb_frame
< new_context
.Rsp
&& (ULONG64
)teb_frame
< (ULONG64
)end_frame
)
4203 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
4204 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
4205 if (call_teb_unwind_handler( rec
, &dispatch
, teb_frame
) == ExceptionCollidedUnwind
)
4209 teb_frame
= __wine_pop_frame( teb_frame
);
4211 *context
= new_context
= *dispatch
.ContextRecord
;
4212 dispatch
.ContextRecord
= context
;
4213 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
4214 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
4215 &new_context
, NULL
, &frame
, NULL
);
4216 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
4219 teb_frame
= __wine_pop_frame( teb_frame
);
4221 if ((ULONG64
)teb_frame
== (ULONG64
)end_frame
&& (ULONG64
)end_frame
< new_context
.Rsp
) break;
4222 dispatch
.EstablisherFrame
= backup_frame
;
4225 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) break;
4226 *context
= new_context
;
4229 context
->Rax
= (ULONG64
)retval
;
4230 context
->Rip
= (ULONG64
)target_ip
;
4231 RtlRestoreContext(context
, rec
);
4235 /*******************************************************************
4236 * RtlUnwind (NTDLL.@)
4238 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
4241 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
4245 /*******************************************************************
4246 * _local_unwind (NTDLL.@)
4248 void WINAPI
_local_unwind( void *frame
, void *target_ip
)
4251 RtlUnwindEx( frame
, target_ip
, NULL
, NULL
, &context
, NULL
);
4254 /*******************************************************************
4255 * __C_specific_handler (NTDLL.@)
4257 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
4260 struct _DISPATCHER_CONTEXT
*dispatch
)
4262 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
4265 TRACE( "%p %lx %p %p\n", rec
, frame
, context
, dispatch
);
4266 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
4268 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
4270 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
4272 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
4273 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
4275 TERMINATION_HANDLER handler
;
4277 if (table
->ScopeRecord
[i
].JumpTarget
) continue;
4279 if (rec
->ExceptionFlags
& EH_TARGET_UNWIND
&&
4280 dispatch
->TargetIp
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
4281 dispatch
->TargetIp
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
4286 handler
= (TERMINATION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
4287 dispatch
->ScopeIndex
= i
+1;
4289 TRACE( "calling __finally %p frame %lx\n", handler
, frame
);
4290 handler( 1, frame
);
4293 return ExceptionContinueSearch
;
4296 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
4298 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
4299 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
4301 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
4302 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
4304 EXCEPTION_POINTERS ptrs
;
4305 PC_LANGUAGE_EXCEPTION_HANDLER filter
;
4307 filter
= (PC_LANGUAGE_EXCEPTION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
4308 ptrs
.ExceptionRecord
= rec
;
4309 ptrs
.ContextRecord
= context
;
4310 TRACE( "calling filter %p ptrs %p frame %lx\n", filter
, &ptrs
, frame
);
4311 switch (filter( &ptrs
, frame
))
4313 case EXCEPTION_EXECUTE_HANDLER
:
4315 case EXCEPTION_CONTINUE_SEARCH
:
4317 case EXCEPTION_CONTINUE_EXECUTION
:
4318 return ExceptionContinueExecution
;
4321 TRACE( "unwinding to target %lx\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
4322 RtlUnwindEx( (void *)frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
4323 rec
, 0, dispatch
->ContextRecord
, dispatch
->HistoryTable
);
4326 return ExceptionContinueSearch
;
4330 /***********************************************************************
4331 * RtlRaiseException (NTDLL.@)
4333 __ASM_GLOBAL_FUNC( RtlRaiseException
,
4334 "movq %rcx,8(%rsp)\n\t"
4335 "sub $0x4f8,%rsp\n\t"
4336 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
4337 "leaq 0x20(%rsp),%rcx\n\t"
4338 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
4339 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
4340 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
4341 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
4342 "movq (%rax),%rcx\n\t" /* original first parameter */
4343 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
4344 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
4345 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
4346 "movq %rax,0x10(%rcx)\n\t" /* rec->ExceptionAddress */
4348 "call " __ASM_NAME("NtRaiseException") "\n\t"
4349 "movq %rax,%rcx\n\t"
4350 "call " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
4353 /*************************************************************************
4354 * RtlCaptureStackBackTrace (NTDLL.@)
4356 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
4358 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
4363 /***********************************************************************
4366 static void WINAPI
call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
)
4370 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
4371 RtlExitUserThread( entry( arg
));
4373 __EXCEPT(call_unhandled_exception_filter
)
4375 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
4378 abort(); /* should not be reached */
4382 extern void DECLSPEC_NORETURN
start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
,
4384 __ASM_GLOBAL_FUNC( start_thread
,
4386 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
4387 "movq %rbp,48(%rsp)\n\t"
4388 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
4389 "movq %rbx,40(%rsp)\n\t"
4390 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
4391 "movq %r12,32(%rsp)\n\t"
4392 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
4393 "movq %r13,24(%rsp)\n\t"
4394 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
4395 "movq %r14,16(%rsp)\n\t"
4396 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
4397 "movq %r15,8(%rsp)\n\t"
4398 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
4399 /* store exit frame */
4400 "movq %gs:0x30,%rax\n\t"
4401 "movq %rsp,0x330(%rax)\n\t" /* amd64_thread_data()->exit_frame */
4402 /* switch to thread stack */
4403 "movq 8(%rax),%rax\n\t" /* NtCurrentTeb()->Tib.StackBase */
4404 "leaq -0x1000(%rax),%rsp\n\t"
4406 "call " __ASM_NAME("attach_thread") "\n\t"
4407 "movq %rax,%rsp\n\t"
4408 /* clear the stack */
4409 "andq $~0xfff,%rax\n\t" /* round down to page size */
4410 "movq %rax,%rdi\n\t"
4411 "call " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
4412 /* switch to the initial context */
4413 "movq %rsp,%rdi\n\t"
4414 "call " __ASM_NAME("set_cpu_context") )
4416 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int) );
4417 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
4418 /* fetch exit frame */
4419 "movq %gs:0x30,%rax\n\t"
4420 "movq 0x330(%rax),%rdx\n\t" /* amd64_thread_data()->exit_frame */
4421 "testq %rdx,%rdx\n\t"
4424 /* switch to exit frame stack */
4425 "1:\tmovq $0,0x330(%rax)\n\t"
4426 "movq %rdx,%rsp\n\t"
4427 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
4428 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
4429 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
4430 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
4431 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
4432 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
4433 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
4437 /***********************************************************************
4438 * init_thread_context
4440 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
, void *relay
)
4442 __asm__( "movw %%cs,%0" : "=m" (context
->SegCs
) );
4443 __asm__( "movw %%ss,%0" : "=m" (context
->SegSs
) );
4444 __asm__( "fxsave %0" : "=m" (context
->u
.FltSave
) );
4445 context
->Rcx
= (ULONG_PTR
)entry
;
4446 context
->Rdx
= (ULONG_PTR
)arg
;
4447 context
->Rsp
= (ULONG_PTR
)NtCurrentTeb()->Tib
.StackBase
- 0x28;
4448 context
->Rip
= (ULONG_PTR
)relay
;
4449 context
->EFlags
= 0x200;
4453 /***********************************************************************
4456 PCONTEXT DECLSPEC_HIDDEN
attach_thread( LPTHREAD_START_ROUTINE entry
, void *arg
,
4457 BOOL suspend
, void *relay
)
4463 CONTEXT context
= { 0 };
4465 context
.ContextFlags
= CONTEXT_ALL
;
4466 init_thread_context( &context
, entry
, arg
, relay
);
4467 wait_suspend( &context
);
4468 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Rsp
& ~15) - 1;
4473 ctx
= (CONTEXT
*)((char *)NtCurrentTeb()->Tib
.StackBase
- 0x30) - 1;
4474 init_thread_context( ctx
, entry
, arg
, relay
);
4476 ctx
->ContextFlags
= CONTEXT_FULL
;
4477 attach_dlls( ctx
, (void **)&ctx
->Rcx
);
4482 /***********************************************************************
4483 * signal_start_thread
4485 * Thread startup sequence:
4486 * signal_start_thread()
4488 * -> call_thread_func()
4490 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
4492 start_thread( entry
, arg
, suspend
, call_thread_func
);
4496 /**********************************************************************
4497 * signal_start_process
4499 * Process startup sequence:
4500 * signal_start_process()
4502 * -> kernel32_start_process()
4504 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
4506 start_thread( entry
, NtCurrentTeb()->Peb
, suspend
, kernel32_start_process
);
4510 /***********************************************************************
4511 * signal_exit_thread
4513 void signal_exit_thread( int status
)
4515 call_thread_exit_func( status
, exit_thread
);
4518 /***********************************************************************
4519 * signal_exit_process
4521 void signal_exit_process( int status
)
4523 call_thread_exit_func( status
, exit
);
4526 /**********************************************************************
4527 * DbgBreakPoint (NTDLL.@)
4529 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
4531 /**********************************************************************
4532 * DbgUserBreakPoint (NTDLL.@)
4534 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
4536 #endif /* __x86_64__ */