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>
55 # define UNW_LOCAL_ONLY
56 # include <libunwind.h>
59 # include <mach/mach.h>
62 #define NONAMELESSUNION
63 #define NONAMELESSSTRUCT
65 #define WIN32_NO_STATUS
68 #include "wine/library.h"
69 #include "wine/exception.h"
70 #include "wine/list.h"
71 #include "ntdll_misc.h"
72 #include "wine/debug.h"
74 #ifdef HAVE_VALGRIND_MEMCHECK_H
75 #include <valgrind/memcheck.h>
78 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
79 WINE_DECLARE_DEBUG_CHANNEL(relay
);
81 typedef struct _SCOPE_TABLE
91 } SCOPE_TABLE
, *PSCOPE_TABLE
;
94 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
95 struct MSVCRT_JUMP_BUFFER
121 /***********************************************************************
122 * signal context platform-specific definitions
126 #include <asm/prctl.h>
127 static inline int arch_prctl( int func
, void *ptr
) { return syscall( __NR_arch_prctl
, func
, ptr
); }
129 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
130 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
131 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
132 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
133 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
134 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
135 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
136 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
137 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
138 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
139 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
140 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
141 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
142 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
143 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
145 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
146 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
147 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
149 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
150 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
151 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
152 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
153 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
155 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
157 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
159 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
160 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
161 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
162 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
163 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
164 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
165 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
166 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
167 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
168 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
169 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
170 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
171 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
172 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
173 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
175 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
176 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
177 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
178 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
179 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
180 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
182 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
184 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
185 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
186 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
187 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
189 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
191 #elif defined(__NetBSD__)
193 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
194 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
195 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
196 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
197 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
198 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
199 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
200 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
201 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
202 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
203 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
204 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
205 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
206 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
207 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
209 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
210 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
211 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
212 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
213 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
214 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
216 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
218 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
219 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
221 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
222 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
224 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
225 #elif defined (__APPLE__)
226 #define RAX_sig(context) ((context)->uc_mcontext->__ss.__rax)
227 #define RBX_sig(context) ((context)->uc_mcontext->__ss.__rbx)
228 #define RCX_sig(context) ((context)->uc_mcontext->__ss.__rcx)
229 #define RDX_sig(context) ((context)->uc_mcontext->__ss.__rdx)
230 #define RSI_sig(context) ((context)->uc_mcontext->__ss.__rsi)
231 #define RDI_sig(context) ((context)->uc_mcontext->__ss.__rdi)
232 #define RBP_sig(context) ((context)->uc_mcontext->__ss.__rbp)
233 #define R8_sig(context) ((context)->uc_mcontext->__ss.__r8)
234 #define R9_sig(context) ((context)->uc_mcontext->__ss.__r9)
235 #define R10_sig(context) ((context)->uc_mcontext->__ss.__r10)
236 #define R11_sig(context) ((context)->uc_mcontext->__ss.__r11)
237 #define R12_sig(context) ((context)->uc_mcontext->__ss.__r12)
238 #define R13_sig(context) ((context)->uc_mcontext->__ss.__r13)
239 #define R14_sig(context) ((context)->uc_mcontext->__ss.__r14)
240 #define R15_sig(context) ((context)->uc_mcontext->__ss.__r15)
242 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
243 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
244 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
246 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__rflags)
248 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rip))
249 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rsp))
251 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
252 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
254 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
257 #error You must define the signal context functions for your platform
262 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
263 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
264 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
265 TRAP_x86_NMI
= 2, /* NMI interrupt */
266 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
267 TRAP_x86_OFLOW
= 4, /* Overflow exception */
268 TRAP_x86_BOUND
= 5, /* Bound range exception */
269 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
270 TRAP_x86_DNA
= 7, /* Device not available exception */
271 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
272 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
273 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
274 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
275 TRAP_x86_STKFLT
= 12, /* Stack fault */
276 TRAP_x86_PROTFLT
= 13, /* General protection fault */
277 TRAP_x86_PAGEFLT
= 14, /* Page fault */
278 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
279 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
280 TRAP_x86_MCHK
= 18, /* Machine check exception */
281 TRAP_x86_CACHEFLT
= 19 /* Cache flush exception */
284 static const size_t teb_size
= 0x2000; /* we reserve two pages for the TEB */
285 static size_t signal_stack_size
;
287 typedef void (*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
289 /* stack layout when calling an exception raise function */
293 EXCEPTION_RECORD rec
;
298 ULONG64 red_zone
[16];
301 typedef int (*wine_signal_handler
)(unsigned int sig
);
303 static wine_signal_handler handlers
[256];
305 struct amd64_thread_data
307 DWORD_PTR dr0
; /* debug registers */
313 void *exit_frame
; /* exit frame pointer */
316 C_ASSERT( sizeof(struct amd64_thread_data
) <= sizeof(((TEB
*)0)->SystemReserved2
) );
317 C_ASSERT( offsetof( TEB
, SystemReserved2
) + offsetof( struct amd64_thread_data
, exit_frame
) == 0x330 );
319 static inline struct amd64_thread_data
*amd64_thread_data(void)
321 return (struct amd64_thread_data
*)NtCurrentTeb()->SystemReserved2
;
324 static inline void set_sigcontext( const CONTEXT
*context
, ucontext_t
*sigcontext
)
326 RAX_sig(sigcontext
) = context
->Rax
;
327 RCX_sig(sigcontext
) = context
->Rcx
;
328 RDX_sig(sigcontext
) = context
->Rdx
;
329 RBX_sig(sigcontext
) = context
->Rbx
;
330 RSP_sig(sigcontext
) = context
->Rsp
;
331 RBP_sig(sigcontext
) = context
->Rbp
;
332 RSI_sig(sigcontext
) = context
->Rsi
;
333 RDI_sig(sigcontext
) = context
->Rdi
;
334 R8_sig(sigcontext
) = context
->R8
;
335 R9_sig(sigcontext
) = context
->R9
;
336 R10_sig(sigcontext
) = context
->R10
;
337 R11_sig(sigcontext
) = context
->R11
;
338 R12_sig(sigcontext
) = context
->R12
;
339 R13_sig(sigcontext
) = context
->R13
;
340 R14_sig(sigcontext
) = context
->R14
;
341 R15_sig(sigcontext
) = context
->R15
;
342 RIP_sig(sigcontext
) = context
->Rip
;
343 CS_sig(sigcontext
) = context
->SegCs
;
344 FS_sig(sigcontext
) = context
->SegFs
;
345 GS_sig(sigcontext
) = context
->SegGs
;
346 EFL_sig(sigcontext
) = context
->EFlags
;
348 DS_sig(sigcontext
) = context
->SegDs
;
351 ES_sig(sigcontext
) = context
->SegEs
;
354 SS_sig(sigcontext
) = context
->SegSs
;
359 /***********************************************************************
360 * Definitions for Win32 unwind tables
365 RUNTIME_FUNCTION chain
;
383 BYTE frame_offset
: 4;
384 struct opcode opcodes
[1]; /* info->count entries */
385 /* followed by handler_data */
388 #define UWOP_PUSH_NONVOL 0
389 #define UWOP_ALLOC_LARGE 1
390 #define UWOP_ALLOC_SMALL 2
391 #define UWOP_SET_FPREG 3
392 #define UWOP_SAVE_NONVOL 4
393 #define UWOP_SAVE_NONVOL_FAR 5
394 #define UWOP_EPILOG 6
395 #define UWOP_SAVE_XMM128 8
396 #define UWOP_SAVE_XMM128_FAR 9
397 #define UWOP_PUSH_MACHFRAME 10
399 static void dump_unwind_info( ULONG64 base
, RUNTIME_FUNCTION
*function
)
401 static const char * const reg_names
[16] =
402 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
403 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
405 union handler_data
*handler_data
;
406 struct UNWIND_INFO
*info
;
407 unsigned int i
, count
;
409 TRACE( "**** func %x-%x\n", function
->BeginAddress
, function
->EndAddress
);
412 if (function
->UnwindData
& 1)
414 RUNTIME_FUNCTION
*next
= (RUNTIME_FUNCTION
*)((char *)base
+ (function
->UnwindData
& ~1));
415 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
416 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
,
417 (char *)base
+ next
->BeginAddress
, (char *)base
+ next
->EndAddress
);
421 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
423 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
424 info
, info
->flags
, info
->prolog
,
425 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
);
428 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
429 reg_names
[info
->frame_reg
], info
->frame_offset
* 16 );
431 for (i
= 0; i
< info
->count
; i
++)
433 TRACE( " 0x%x: ", info
->opcodes
[i
].offset
);
434 switch (info
->opcodes
[i
].code
)
436 case UWOP_PUSH_NONVOL
:
437 TRACE( "pushq %%%s\n", reg_names
[info
->opcodes
[i
].info
] );
439 case UWOP_ALLOC_LARGE
:
440 if (info
->opcodes
[i
].info
)
442 count
= *(DWORD
*)&info
->opcodes
[i
+1];
447 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
450 TRACE( "subq $0x%x,%%rsp\n", count
);
452 case UWOP_ALLOC_SMALL
:
453 count
= (info
->opcodes
[i
].info
+ 1) * 8;
454 TRACE( "subq $0x%x,%%rsp\n", count
);
457 TRACE( "leaq 0x%x(%%rsp),%s\n",
458 info
->frame_offset
* 16, reg_names
[info
->frame_reg
] );
460 case UWOP_SAVE_NONVOL
:
461 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
462 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
465 case UWOP_SAVE_NONVOL_FAR
:
466 count
= *(DWORD
*)&info
->opcodes
[i
+1];
467 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
470 case UWOP_SAVE_XMM128
:
471 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 16;
472 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
475 case UWOP_SAVE_XMM128_FAR
:
476 count
= *(DWORD
*)&info
->opcodes
[i
+1];
477 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
480 case UWOP_PUSH_MACHFRAME
:
481 TRACE( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
484 if (info
->version
== 2)
487 if (info
->opcodes
[i
].info
)
488 offset
= info
->opcodes
[i
].offset
;
490 offset
= (info
->opcodes
[i
+1].info
<< 8) + info
->opcodes
[i
+1].offset
;
491 TRACE("epilog %p-%p\n", (char *)base
+ function
->EndAddress
- offset
,
492 (char *)base
+ function
->EndAddress
- offset
+ info
->opcodes
[i
].offset
);
497 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
502 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
503 if (info
->flags
& UNW_FLAG_CHAININFO
)
505 TRACE( " chained to function %p-%p\n",
506 (char *)base
+ handler_data
->chain
.BeginAddress
,
507 (char *)base
+ handler_data
->chain
.EndAddress
);
508 function
= &handler_data
->chain
;
511 if (info
->flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
512 TRACE( " handler %p data at %p\n",
513 (char *)base
+ handler_data
->handler
, &handler_data
->handler
+ 1 );
518 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
522 TRACE( "scope table at %p\n", table
);
523 for (i
= 0; i
< table
->Count
; i
++)
524 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i
,
525 base
+ table
->ScopeRecord
[i
].BeginAddress
,
526 base
+ table
->ScopeRecord
[i
].EndAddress
,
527 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
528 base
+ table
->ScopeRecord
[i
].JumpTarget
);
532 /***********************************************************************
533 * Definitions for Dwarf unwind tables
536 enum dwarf_call_frame_info
538 DW_CFA_advance_loc
= 0x40,
539 DW_CFA_offset
= 0x80,
540 DW_CFA_restore
= 0xc0,
542 DW_CFA_set_loc
= 0x01,
543 DW_CFA_advance_loc1
= 0x02,
544 DW_CFA_advance_loc2
= 0x03,
545 DW_CFA_advance_loc4
= 0x04,
546 DW_CFA_offset_extended
= 0x05,
547 DW_CFA_restore_extended
= 0x06,
548 DW_CFA_undefined
= 0x07,
549 DW_CFA_same_value
= 0x08,
550 DW_CFA_register
= 0x09,
551 DW_CFA_remember_state
= 0x0a,
552 DW_CFA_restore_state
= 0x0b,
553 DW_CFA_def_cfa
= 0x0c,
554 DW_CFA_def_cfa_register
= 0x0d,
555 DW_CFA_def_cfa_offset
= 0x0e,
556 DW_CFA_def_cfa_expression
= 0x0f,
557 DW_CFA_expression
= 0x10,
558 DW_CFA_offset_extended_sf
= 0x11,
559 DW_CFA_def_cfa_sf
= 0x12,
560 DW_CFA_def_cfa_offset_sf
= 0x13,
561 DW_CFA_val_offset
= 0x14,
562 DW_CFA_val_offset_sf
= 0x15,
563 DW_CFA_val_expression
= 0x16,
570 DW_OP_const1u
= 0x08,
571 DW_OP_const1s
= 0x09,
572 DW_OP_const2u
= 0x0a,
573 DW_OP_const2s
= 0x0b,
574 DW_OP_const4u
= 0x0c,
575 DW_OP_const4s
= 0x0d,
576 DW_OP_const8u
= 0x0e,
577 DW_OP_const8s
= 0x0f,
597 DW_OP_plus_uconst
= 0x23,
710 DW_OP_deref_size
= 0x94,
711 DW_OP_xderef_size
= 0x95,
713 DW_OP_push_object_address
= 0x97,
716 DW_OP_call_ref
= 0x9a,
717 DW_OP_form_tls_address
= 0x9b,
718 DW_OP_call_frame_cfa
= 0x9c,
719 DW_OP_bit_piece
= 0x9d,
720 DW_OP_lo_user
= 0xe0,
721 DW_OP_hi_user
= 0xff,
722 DW_OP_GNU_push_tls_address
= 0xe0,
723 DW_OP_GNU_uninit
= 0xf0,
724 DW_OP_GNU_encoded_addr
= 0xf1,
727 #define DW_EH_PE_native 0x00
728 #define DW_EH_PE_leb128 0x01
729 #define DW_EH_PE_data2 0x02
730 #define DW_EH_PE_data4 0x03
731 #define DW_EH_PE_data8 0x04
732 #define DW_EH_PE_signed 0x08
733 #define DW_EH_PE_abs 0x00
734 #define DW_EH_PE_pcrel 0x10
735 #define DW_EH_PE_textrel 0x20
736 #define DW_EH_PE_datarel 0x30
737 #define DW_EH_PE_funcrel 0x40
738 #define DW_EH_PE_aligned 0x50
739 #define DW_EH_PE_indirect 0x80
740 #define DW_EH_PE_omit 0xff
742 struct dwarf_eh_bases
753 unsigned char version
;
754 unsigned char augmentation
[1];
760 unsigned int cie_offset
;
763 extern const struct dwarf_fde
*_Unwind_Find_FDE (void *, struct dwarf_eh_bases
*);
765 static unsigned char dwarf_get_u1( const unsigned char **p
)
770 static unsigned short dwarf_get_u2( const unsigned char **p
)
772 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8);
777 static unsigned int dwarf_get_u4( const unsigned char **p
)
779 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8) | ((*p
)[2] << 16) | ((*p
)[3] << 24);
784 static ULONG64
dwarf_get_u8( const unsigned char **p
)
786 ULONG64 low
= dwarf_get_u4( p
);
787 ULONG64 high
= dwarf_get_u4( p
);
788 return low
| (high
<< 32);
791 static ULONG_PTR
dwarf_get_uleb128( const unsigned char **p
)
794 unsigned int shift
= 0;
800 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
803 } while (byte
& 0x80);
807 static LONG_PTR
dwarf_get_sleb128( const unsigned char **p
)
810 unsigned int shift
= 0;
816 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
819 } while (byte
& 0x80);
821 if ((shift
< 8 * sizeof(ret
)) && (byte
& 0x40)) ret
|= -((ULONG_PTR
)1 << shift
);
825 static ULONG_PTR
dwarf_get_ptr( const unsigned char **p
, unsigned char encoding
)
829 if (encoding
== DW_EH_PE_omit
) return 0;
831 switch (encoding
& 0xf0)
837 base
= (ULONG_PTR
)*p
;
840 FIXME( "unsupported encoding %02x\n", encoding
);
844 switch (encoding
& 0x0f)
846 case DW_EH_PE_native
:
847 return base
+ dwarf_get_u8( p
);
848 case DW_EH_PE_leb128
:
849 return base
+ dwarf_get_uleb128( p
);
851 return base
+ dwarf_get_u2( p
);
853 return base
+ dwarf_get_u4( p
);
855 return base
+ dwarf_get_u8( p
);
856 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
857 return base
+ dwarf_get_sleb128( p
);
858 case DW_EH_PE_signed
|DW_EH_PE_data2
:
859 return base
+ (signed short)dwarf_get_u2( p
);
860 case DW_EH_PE_signed
|DW_EH_PE_data4
:
861 return base
+ (signed int)dwarf_get_u4( p
);
862 case DW_EH_PE_signed
|DW_EH_PE_data8
:
863 return base
+ (LONG64
)dwarf_get_u8( p
);
865 FIXME( "unsupported encoding %02x\n", encoding
);
872 RULE_UNSET
, /* not set at all */
873 RULE_UNDEFINED
, /* undefined value */
874 RULE_SAME
, /* same value as previous frame */
875 RULE_CFA_OFFSET
, /* stored at cfa offset */
876 RULE_OTHER_REG
, /* stored in other register */
877 RULE_EXPRESSION
, /* address specified by expression */
878 RULE_VAL_EXPRESSION
/* value specified by expression */
881 #define NB_FRAME_REGS 41
882 #define MAX_SAVED_STATES 16
886 ULONG_PTR cfa_offset
;
887 unsigned char cfa_reg
;
888 enum reg_rule cfa_rule
;
889 enum reg_rule rules
[NB_FRAME_REGS
];
890 ULONG64 regs
[NB_FRAME_REGS
];
896 ULONG_PTR code_align
;
898 unsigned char retaddr_reg
;
899 unsigned char fde_encoding
;
900 unsigned char signal_frame
;
901 unsigned char state_sp
;
902 struct frame_state state
;
903 struct frame_state
*state_stack
;
906 static const char *dwarf_reg_names
[NB_FRAME_REGS
] =
908 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
909 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
910 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
911 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
912 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
915 static BOOL
valid_reg( ULONG_PTR reg
)
917 if (reg
>= NB_FRAME_REGS
) FIXME( "unsupported reg %lx\n", reg
);
918 return (reg
< NB_FRAME_REGS
);
921 static void execute_cfa_instructions( const unsigned char *ptr
, const unsigned char *end
,
922 ULONG_PTR last_ip
, struct frame_info
*info
)
924 while (ptr
< end
&& info
->ip
< last_ip
+ info
->signal_frame
)
926 enum dwarf_call_frame_info op
= *ptr
++;
932 case DW_CFA_advance_loc
:
934 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
935 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
941 ULONG_PTR reg
= op
& 0x3f;
942 LONG_PTR offset
= dwarf_get_uleb128( &ptr
) * info
->data_align
;
943 if (!valid_reg( reg
)) break;
944 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
945 info
->state
.regs
[reg
] = offset
;
946 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
951 ULONG_PTR reg
= op
& 0x3f;
952 if (!valid_reg( reg
)) break;
953 TRACE( "%lx: DW_CFA_restore %s\n", info
->ip
, dwarf_reg_names
[reg
] );
954 info
->state
.rules
[reg
] = RULE_UNSET
;
965 ULONG_PTR loc
= dwarf_get_ptr( &ptr
, info
->fde_encoding
);
966 TRACE( "%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
970 case DW_CFA_advance_loc1
:
972 ULONG_PTR offset
= *ptr
++ * info
->code_align
;
973 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
977 case DW_CFA_advance_loc2
:
979 ULONG_PTR offset
= dwarf_get_u2( &ptr
) * info
->code_align
;
980 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
984 case DW_CFA_advance_loc4
:
986 ULONG_PTR offset
= dwarf_get_u4( &ptr
) * info
->code_align
;
987 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
991 case DW_CFA_offset_extended
:
992 case DW_CFA_offset_extended_sf
:
994 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
995 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf_get_uleb128( &ptr
) * info
->data_align
996 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
997 if (!valid_reg( reg
)) break;
998 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
999 info
->state
.regs
[reg
] = offset
;
1000 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
1003 case DW_CFA_restore_extended
:
1005 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1006 if (!valid_reg( reg
)) break;
1007 TRACE( "%lx: DW_CFA_restore_extended %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1008 info
->state
.rules
[reg
] = RULE_UNSET
;
1011 case DW_CFA_undefined
:
1013 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1014 if (!valid_reg( reg
)) break;
1015 TRACE( "%lx: DW_CFA_undefined %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1016 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
1019 case DW_CFA_same_value
:
1021 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1022 if (!valid_reg( reg
)) break;
1023 TRACE( "%lx: DW_CFA_same_value %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1024 info
->state
.regs
[reg
] = reg
;
1025 info
->state
.rules
[reg
] = RULE_SAME
;
1028 case DW_CFA_register
:
1030 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1031 ULONG_PTR reg2
= dwarf_get_uleb128( &ptr
);
1032 if (!valid_reg( reg
) || !valid_reg( reg2
)) break;
1033 TRACE( "%lx: DW_CFA_register %s == %s\n", info
->ip
, dwarf_reg_names
[reg
], dwarf_reg_names
[reg2
] );
1034 info
->state
.regs
[reg
] = reg2
;
1035 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
1038 case DW_CFA_remember_state
:
1039 TRACE( "%lx: DW_CFA_remember_state\n", info
->ip
);
1040 if (info
->state_sp
>= MAX_SAVED_STATES
)
1041 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
1043 info
->state_stack
[info
->state_sp
++] = info
->state
;
1045 case DW_CFA_restore_state
:
1046 TRACE( "%lx: DW_CFA_restore_state\n", info
->ip
);
1047 if (!info
->state_sp
)
1048 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
1050 info
->state
= info
->state_stack
[--info
->state_sp
];
1052 case DW_CFA_def_cfa
:
1053 case DW_CFA_def_cfa_sf
:
1055 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1056 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf_get_uleb128( &ptr
)
1057 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
1058 if (!valid_reg( reg
)) break;
1059 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
1060 info
->state
.cfa_reg
= reg
;
1061 info
->state
.cfa_offset
= offset
;
1062 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1065 case DW_CFA_def_cfa_register
:
1067 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1068 if (!valid_reg( reg
)) break;
1069 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info
->ip
, dwarf_reg_names
[reg
] );
1070 info
->state
.cfa_reg
= reg
;
1071 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1074 case DW_CFA_def_cfa_offset
:
1075 case DW_CFA_def_cfa_offset_sf
:
1077 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf_get_uleb128( &ptr
)
1078 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
1079 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info
->ip
, offset
);
1080 info
->state
.cfa_offset
= offset
;
1081 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
1084 case DW_CFA_def_cfa_expression
:
1086 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
1087 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
1088 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
1089 info
->state
.cfa_offset
= expr
;
1090 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
1094 case DW_CFA_expression
:
1095 case DW_CFA_val_expression
:
1097 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1098 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
1099 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
1100 if (!valid_reg( reg
)) break;
1101 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
1102 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_", dwarf_reg_names
[reg
], expr
, expr
+len
);
1103 info
->state
.regs
[reg
] = expr
;
1104 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
1109 FIXME( "%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
1115 /* retrieve a context register from its dwarf number */
1116 static void *get_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
)
1120 case 0: return &context
->Rax
;
1121 case 1: return &context
->Rdx
;
1122 case 2: return &context
->Rcx
;
1123 case 3: return &context
->Rbx
;
1124 case 4: return &context
->Rsi
;
1125 case 5: return &context
->Rdi
;
1126 case 6: return &context
->Rbp
;
1127 case 7: return &context
->Rsp
;
1128 case 8: return &context
->R8
;
1129 case 9: return &context
->R9
;
1130 case 10: return &context
->R10
;
1131 case 11: return &context
->R11
;
1132 case 12: return &context
->R12
;
1133 case 13: return &context
->R13
;
1134 case 14: return &context
->R14
;
1135 case 15: return &context
->R15
;
1136 case 16: return &context
->Rip
;
1137 case 17: return &context
->u
.s
.Xmm0
;
1138 case 18: return &context
->u
.s
.Xmm1
;
1139 case 19: return &context
->u
.s
.Xmm2
;
1140 case 20: return &context
->u
.s
.Xmm3
;
1141 case 21: return &context
->u
.s
.Xmm4
;
1142 case 22: return &context
->u
.s
.Xmm5
;
1143 case 23: return &context
->u
.s
.Xmm6
;
1144 case 24: return &context
->u
.s
.Xmm7
;
1145 case 25: return &context
->u
.s
.Xmm8
;
1146 case 26: return &context
->u
.s
.Xmm9
;
1147 case 27: return &context
->u
.s
.Xmm10
;
1148 case 28: return &context
->u
.s
.Xmm11
;
1149 case 29: return &context
->u
.s
.Xmm12
;
1150 case 30: return &context
->u
.s
.Xmm13
;
1151 case 31: return &context
->u
.s
.Xmm14
;
1152 case 32: return &context
->u
.s
.Xmm15
;
1153 case 33: return &context
->u
.s
.Legacy
[0];
1154 case 34: return &context
->u
.s
.Legacy
[1];
1155 case 35: return &context
->u
.s
.Legacy
[2];
1156 case 36: return &context
->u
.s
.Legacy
[3];
1157 case 37: return &context
->u
.s
.Legacy
[4];
1158 case 38: return &context
->u
.s
.Legacy
[5];
1159 case 39: return &context
->u
.s
.Legacy
[6];
1160 case 40: return &context
->u
.s
.Legacy
[7];
1161 default: return NULL
;
1165 /* set a context register from its dwarf number */
1166 static void set_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
, void *val
)
1170 case 0: context
->Rax
= *(ULONG64
*)val
; break;
1171 case 1: context
->Rdx
= *(ULONG64
*)val
; break;
1172 case 2: context
->Rcx
= *(ULONG64
*)val
; break;
1173 case 3: context
->Rbx
= *(ULONG64
*)val
; break;
1174 case 4: context
->Rsi
= *(ULONG64
*)val
; break;
1175 case 5: context
->Rdi
= *(ULONG64
*)val
; break;
1176 case 6: context
->Rbp
= *(ULONG64
*)val
; break;
1177 case 7: context
->Rsp
= *(ULONG64
*)val
; break;
1178 case 8: context
->R8
= *(ULONG64
*)val
; break;
1179 case 9: context
->R9
= *(ULONG64
*)val
; break;
1180 case 10: context
->R10
= *(ULONG64
*)val
; break;
1181 case 11: context
->R11
= *(ULONG64
*)val
; break;
1182 case 12: context
->R12
= *(ULONG64
*)val
; break;
1183 case 13: context
->R13
= *(ULONG64
*)val
; break;
1184 case 14: context
->R14
= *(ULONG64
*)val
; break;
1185 case 15: context
->R15
= *(ULONG64
*)val
; break;
1186 case 16: context
->Rip
= *(ULONG64
*)val
; break;
1187 case 17: memcpy( &context
->u
.s
.Xmm0
, val
, sizeof(M128A
) ); break;
1188 case 18: memcpy( &context
->u
.s
.Xmm1
, val
, sizeof(M128A
) ); break;
1189 case 19: memcpy( &context
->u
.s
.Xmm2
, val
, sizeof(M128A
) ); break;
1190 case 20: memcpy( &context
->u
.s
.Xmm3
, val
, sizeof(M128A
) ); break;
1191 case 21: memcpy( &context
->u
.s
.Xmm4
, val
, sizeof(M128A
) ); break;
1192 case 22: memcpy( &context
->u
.s
.Xmm5
, val
, sizeof(M128A
) ); break;
1193 case 23: memcpy( &context
->u
.s
.Xmm6
, val
, sizeof(M128A
) ); break;
1194 case 24: memcpy( &context
->u
.s
.Xmm7
, val
, sizeof(M128A
) ); break;
1195 case 25: memcpy( &context
->u
.s
.Xmm8
, val
, sizeof(M128A
) ); break;
1196 case 26: memcpy( &context
->u
.s
.Xmm9
, val
, sizeof(M128A
) ); break;
1197 case 27: memcpy( &context
->u
.s
.Xmm10
, val
, sizeof(M128A
) ); break;
1198 case 28: memcpy( &context
->u
.s
.Xmm11
, val
, sizeof(M128A
) ); break;
1199 case 29: memcpy( &context
->u
.s
.Xmm12
, val
, sizeof(M128A
) ); break;
1200 case 30: memcpy( &context
->u
.s
.Xmm13
, val
, sizeof(M128A
) ); break;
1201 case 31: memcpy( &context
->u
.s
.Xmm14
, val
, sizeof(M128A
) ); break;
1202 case 32: memcpy( &context
->u
.s
.Xmm15
, val
, sizeof(M128A
) ); break;
1203 case 33: memcpy( &context
->u
.s
.Legacy
[0], val
, sizeof(M128A
) ); break;
1204 case 34: memcpy( &context
->u
.s
.Legacy
[1], val
, sizeof(M128A
) ); break;
1205 case 35: memcpy( &context
->u
.s
.Legacy
[2], val
, sizeof(M128A
) ); break;
1206 case 36: memcpy( &context
->u
.s
.Legacy
[3], val
, sizeof(M128A
) ); break;
1207 case 37: memcpy( &context
->u
.s
.Legacy
[4], val
, sizeof(M128A
) ); break;
1208 case 38: memcpy( &context
->u
.s
.Legacy
[5], val
, sizeof(M128A
) ); break;
1209 case 39: memcpy( &context
->u
.s
.Legacy
[6], val
, sizeof(M128A
) ); break;
1210 case 40: memcpy( &context
->u
.s
.Legacy
[7], val
, sizeof(M128A
) ); break;
1214 static ULONG_PTR
eval_expression( const unsigned char *p
, CONTEXT
*context
)
1216 ULONG_PTR reg
, tmp
, stack
[64];
1218 ULONG_PTR len
= dwarf_get_uleb128(&p
);
1219 const unsigned char *end
= p
+ len
;
1223 unsigned char opcode
= dwarf_get_u1(&p
);
1225 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
1226 stack
[++sp
] = opcode
- DW_OP_lit0
;
1227 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
1228 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_reg0
);
1229 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
1230 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_breg0
) + dwarf_get_sleb128(&p
);
1231 else switch (opcode
)
1233 case DW_OP_nop
: break;
1234 case DW_OP_addr
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1235 case DW_OP_const1u
: stack
[++sp
] = dwarf_get_u1(&p
); break;
1236 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf_get_u1(&p
); break;
1237 case DW_OP_const2u
: stack
[++sp
] = dwarf_get_u2(&p
); break;
1238 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf_get_u2(&p
); break;
1239 case DW_OP_const4u
: stack
[++sp
] = dwarf_get_u4(&p
); break;
1240 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf_get_u4(&p
); break;
1241 case DW_OP_const8u
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1242 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf_get_u8(&p
); break;
1243 case DW_OP_constu
: stack
[++sp
] = dwarf_get_uleb128(&p
); break;
1244 case DW_OP_consts
: stack
[++sp
] = dwarf_get_sleb128(&p
); break;
1245 case DW_OP_deref
: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1246 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
1247 case DW_OP_drop
: sp
--; break;
1248 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
1249 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf_get_u1(&p
)]; sp
++; break;
1250 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
1251 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
1252 case DW_OP_abs
: stack
[sp
] = labs(stack
[sp
]); break;
1253 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
1254 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
1255 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
1256 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
1257 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
1258 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
1259 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
1260 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
1261 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
1262 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
1263 case DW_OP_plus_uconst
: stack
[sp
] += dwarf_get_uleb128(&p
); break;
1264 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
1265 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
1266 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
1267 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
1268 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
1269 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
1270 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
1271 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
1272 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
1273 case DW_OP_skip
: tmp
= (short)dwarf_get_u2(&p
); p
+= tmp
; break;
1274 case DW_OP_bra
: tmp
= (short)dwarf_get_u2(&p
); if (!stack
[sp
--]) p
+= tmp
; break;
1275 case DW_OP_GNU_encoded_addr
: tmp
= *p
++; stack
[++sp
] = dwarf_get_ptr( &p
, tmp
); break;
1276 case DW_OP_regx
: stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, dwarf_get_uleb128(&p
) ); break;
1278 reg
= dwarf_get_uleb128(&p
);
1279 tmp
= dwarf_get_sleb128(&p
);
1280 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, reg
) + tmp
;
1282 case DW_OP_deref_size
:
1285 case 1: stack
[sp
] = *(unsigned char *)stack
[sp
]; break;
1286 case 2: stack
[sp
] = *(unsigned short *)stack
[sp
]; break;
1287 case 4: stack
[sp
] = *(unsigned int *)stack
[sp
]; break;
1288 case 8: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1292 FIXME( "unhandled opcode %02x\n", opcode
);
1298 /* apply the computed frame info to the actual context */
1299 static void apply_frame_state( CONTEXT
*context
, struct frame_state
*state
)
1302 ULONG_PTR cfa
, value
;
1303 CONTEXT new_context
= *context
;
1305 switch (state
->cfa_rule
)
1307 case RULE_EXPRESSION
:
1308 cfa
= *(ULONG_PTR
*)eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1310 case RULE_VAL_EXPRESSION
:
1311 cfa
= eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1314 cfa
= *(ULONG_PTR
*)get_context_reg( context
, state
->cfa_reg
) + state
->cfa_offset
;
1319 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
1321 switch (state
->rules
[i
])
1324 case RULE_UNDEFINED
:
1327 case RULE_CFA_OFFSET
:
1328 set_context_reg( &new_context
, i
, (char *)cfa
+ state
->regs
[i
] );
1330 case RULE_OTHER_REG
:
1331 set_context_reg( &new_context
, i
, get_context_reg( context
, state
->regs
[i
] ));
1333 case RULE_EXPRESSION
:
1334 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1335 set_context_reg( &new_context
, i
, (void *)value
);
1337 case RULE_VAL_EXPRESSION
:
1338 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1339 set_context_reg( &new_context
, i
, &value
);
1343 new_context
.Rsp
= cfa
;
1344 *context
= new_context
;
1348 /***********************************************************************
1349 * dwarf_virtual_unwind
1351 * Equivalent of RtlVirtualUnwind for builtin modules.
1353 static NTSTATUS
dwarf_virtual_unwind( ULONG64 ip
, ULONG64
*frame
,CONTEXT
*context
,
1354 const struct dwarf_fde
*fde
, const struct dwarf_eh_bases
*bases
,
1355 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1357 const struct dwarf_cie
*cie
;
1358 const unsigned char *ptr
, *augmentation
, *end
;
1359 ULONG_PTR len
, code_end
;
1360 struct frame_info info
;
1361 struct frame_state state_stack
[MAX_SAVED_STATES
];
1362 int aug_z_format
= 0;
1363 unsigned char lsda_encoding
= DW_EH_PE_omit
;
1365 memset( &info
, 0, sizeof(info
) );
1366 info
.state_stack
= state_stack
;
1367 info
.ip
= (ULONG_PTR
)bases
->func
;
1370 cie
= (const struct dwarf_cie
*)((const char *)&fde
->cie_offset
- fde
->cie_offset
);
1372 /* parse the CIE first */
1374 if (cie
->version
!= 1 && cie
->version
!= 3)
1376 FIXME( "unknown CIE version %u at %p\n", cie
->version
, cie
);
1377 return STATUS_INVALID_DISPOSITION
;
1379 ptr
= cie
->augmentation
+ strlen((const char *)cie
->augmentation
) + 1;
1381 info
.code_align
= dwarf_get_uleb128( &ptr
);
1382 info
.data_align
= dwarf_get_sleb128( &ptr
);
1383 if (cie
->version
== 1)
1384 info
.retaddr_reg
= *ptr
++;
1386 info
.retaddr_reg
= dwarf_get_uleb128( &ptr
);
1387 info
.state
.cfa_rule
= RULE_CFA_OFFSET
;
1389 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1390 ip
, bases
->func
, cie
, cie
->length
, cie
->id
, cie
->version
, cie
->augmentation
,
1391 info
.code_align
, info
.data_align
, dwarf_reg_names
[info
.retaddr_reg
] );
1394 for (augmentation
= cie
->augmentation
; *augmentation
; augmentation
++)
1396 switch (*augmentation
)
1399 len
= dwarf_get_uleb128( &ptr
);
1404 lsda_encoding
= *ptr
++;
1408 unsigned char encoding
= *ptr
++;
1409 *handler
= (void *)dwarf_get_ptr( &ptr
, encoding
);
1413 info
.fde_encoding
= *ptr
++;
1416 info
.signal_frame
= 1;
1419 FIXME( "unknown augmentation '%c'\n", *augmentation
);
1420 if (!end
) return STATUS_INVALID_DISPOSITION
; /* cannot continue */
1425 end
= (const unsigned char *)(&cie
->length
+ 1) + cie
->length
;
1426 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1428 ptr
= (const unsigned char *)(fde
+ 1);
1429 info
.ip
= dwarf_get_ptr( &ptr
, info
.fde_encoding
); /* fde code start */
1430 code_end
= info
.ip
+ dwarf_get_ptr( &ptr
, info
.fde_encoding
& 0x0f ); /* fde code length */
1432 if (aug_z_format
) /* get length of augmentation data */
1434 len
= dwarf_get_uleb128( &ptr
);
1439 *handler_data
= (void *)dwarf_get_ptr( &ptr
, lsda_encoding
);
1442 end
= (const unsigned char *)(&fde
->length
+ 1) + fde
->length
;
1443 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1444 fde
, fde
->length
, *handler
, *handler_data
, info
.ip
, code_end
);
1445 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1446 *frame
= context
->Rsp
;
1447 apply_frame_state( context
, &info
.state
);
1449 TRACE( "next function rip=%016lx\n", context
->Rip
);
1450 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1451 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1452 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1453 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1454 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1455 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1456 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1457 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1459 return STATUS_SUCCESS
;
1463 #ifdef HAVE_LIBUNWIND
1464 /***********************************************************************
1465 * libunwind_virtual_unwind
1467 * Equivalent of RtlVirtualUnwind for builtin modules.
1469 static NTSTATUS
libunwind_virtual_unwind( ULONG64 ip
, BOOL
* got_info
, ULONG64
*frame
, CONTEXT
*context
,
1470 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1472 unw_context_t unw_context
;
1473 unw_cursor_t cursor
;
1474 unw_proc_info_t info
;
1478 rc
= unw_getcontext( &unw_context
);
1479 if (rc
== UNW_ESUCCESS
)
1480 rc
= unw_init_local( &cursor
, &unw_context
);
1481 if (rc
== UNW_ESUCCESS
)
1483 unw_set_reg( &cursor
, UNW_REG_IP
, context
->Rip
);
1484 unw_set_reg( &cursor
, UNW_REG_SP
, context
->Rsp
);
1485 unw_set_reg( &cursor
, UNW_X86_64_RAX
, context
->Rax
);
1486 unw_set_reg( &cursor
, UNW_X86_64_RDX
, context
->Rdx
);
1487 unw_set_reg( &cursor
, UNW_X86_64_RCX
, context
->Rcx
);
1488 unw_set_reg( &cursor
, UNW_X86_64_RBX
, context
->Rbx
);
1489 unw_set_reg( &cursor
, UNW_X86_64_RSI
, context
->Rsi
);
1490 unw_set_reg( &cursor
, UNW_X86_64_RDI
, context
->Rdi
);
1491 unw_set_reg( &cursor
, UNW_X86_64_RBP
, context
->Rbp
);
1492 unw_set_reg( &cursor
, UNW_X86_64_R8
, context
->R8
);
1493 unw_set_reg( &cursor
, UNW_X86_64_R9
, context
->R9
);
1494 unw_set_reg( &cursor
, UNW_X86_64_R10
, context
->R10
);
1495 unw_set_reg( &cursor
, UNW_X86_64_R11
, context
->R11
);
1496 unw_set_reg( &cursor
, UNW_X86_64_R12
, context
->R12
);
1497 unw_set_reg( &cursor
, UNW_X86_64_R13
, context
->R13
);
1498 unw_set_reg( &cursor
, UNW_X86_64_R14
, context
->R14
);
1499 unw_set_reg( &cursor
, UNW_X86_64_R15
, context
->R15
);
1502 set_sigcontext( context
, &unw_context
);
1503 rc
= unw_init_local( &cursor
, &unw_context
);
1505 if (rc
!= UNW_ESUCCESS
)
1507 WARN( "setup failed: %d\n", rc
);
1508 return STATUS_INVALID_DISPOSITION
;
1511 rc
= unw_get_proc_info(&cursor
, &info
);
1512 if (rc
!= UNW_ESUCCESS
&& rc
!= UNW_ENOINFO
)
1514 WARN( "failed to get info: %d\n", rc
);
1515 return STATUS_INVALID_DISPOSITION
;
1517 if (rc
== UNW_ENOINFO
|| ip
< info
.start_ip
|| ip
> info
.end_ip
|| info
.end_ip
== info
.start_ip
+ 1)
1520 return STATUS_SUCCESS
;
1523 TRACE( "ip %#lx function %#lx-%#lx personality %#lx lsda %#lx fde %#lx\n",
1524 ip
, (unsigned long)info
.start_ip
, (unsigned long)info
.end_ip
, (unsigned long)info
.handler
,
1525 (unsigned long)info
.lsda
, (unsigned long)info
.unwind_info
);
1527 if (!(rc
= unw_step( &cursor
)))
1529 WARN( "last frame\n" );
1531 return STATUS_SUCCESS
;
1535 WARN( "failed to unwind: %d\n", rc
);
1536 return STATUS_INVALID_DISPOSITION
;
1539 *frame
= context
->Rsp
;
1540 unw_get_reg( &cursor
, UNW_REG_IP
, (unw_word_t
*)&context
->Rip
);
1541 unw_get_reg( &cursor
, UNW_REG_SP
, (unw_word_t
*)&context
->Rsp
);
1542 unw_get_reg( &cursor
, UNW_X86_64_RAX
, (unw_word_t
*)&context
->Rax
);
1543 unw_get_reg( &cursor
, UNW_X86_64_RDX
, (unw_word_t
*)&context
->Rdx
);
1544 unw_get_reg( &cursor
, UNW_X86_64_RCX
, (unw_word_t
*)&context
->Rcx
);
1545 unw_get_reg( &cursor
, UNW_X86_64_RBX
, (unw_word_t
*)&context
->Rbx
);
1546 unw_get_reg( &cursor
, UNW_X86_64_RSI
, (unw_word_t
*)&context
->Rsi
);
1547 unw_get_reg( &cursor
, UNW_X86_64_RDI
, (unw_word_t
*)&context
->Rdi
);
1548 unw_get_reg( &cursor
, UNW_X86_64_RBP
, (unw_word_t
*)&context
->Rbp
);
1549 unw_get_reg( &cursor
, UNW_X86_64_R8
, (unw_word_t
*)&context
->R8
);
1550 unw_get_reg( &cursor
, UNW_X86_64_R9
, (unw_word_t
*)&context
->R9
);
1551 unw_get_reg( &cursor
, UNW_X86_64_R10
, (unw_word_t
*)&context
->R10
);
1552 unw_get_reg( &cursor
, UNW_X86_64_R11
, (unw_word_t
*)&context
->R11
);
1553 unw_get_reg( &cursor
, UNW_X86_64_R12
, (unw_word_t
*)&context
->R12
);
1554 unw_get_reg( &cursor
, UNW_X86_64_R13
, (unw_word_t
*)&context
->R13
);
1555 unw_get_reg( &cursor
, UNW_X86_64_R14
, (unw_word_t
*)&context
->R14
);
1556 unw_get_reg( &cursor
, UNW_X86_64_R15
, (unw_word_t
*)&context
->R15
);
1557 *handler
= (void*)info
.handler
;
1558 *handler_data
= (void*)info
.lsda
;
1561 TRACE( "next function rip=%016lx\n", context
->Rip
);
1562 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1563 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1564 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1565 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1566 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1567 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1568 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1569 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1571 return STATUS_SUCCESS
;
1576 /***********************************************************************
1579 static NTSTATUS
virtual_unwind( ULONG type
, DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*context
)
1584 dispatch
->ImageBase
= 0;
1585 dispatch
->ScopeIndex
= 0;
1586 dispatch
->ControlPc
= context
->Rip
;
1588 /* first look for PE exception information */
1590 if ((dispatch
->FunctionEntry
= lookup_function_info( context
->Rip
, &dispatch
->ImageBase
, &module
)))
1592 dispatch
->LanguageHandler
= RtlVirtualUnwind( type
, dispatch
->ImageBase
, context
->Rip
,
1593 dispatch
->FunctionEntry
, context
,
1594 &dispatch
->HandlerData
, &dispatch
->EstablisherFrame
,
1596 return STATUS_SUCCESS
;
1599 /* then look for host system exception information */
1601 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
1603 BOOL got_info
= FALSE
;
1604 struct dwarf_eh_bases bases
;
1605 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
->Rip
- 1), &bases
);
1609 status
= dwarf_virtual_unwind( context
->Rip
, &dispatch
->EstablisherFrame
, context
, fde
,
1610 &bases
, &dispatch
->LanguageHandler
, &dispatch
->HandlerData
);
1611 if (status
!= STATUS_SUCCESS
) return status
;
1614 #ifdef HAVE_LIBUNWIND
1617 status
= libunwind_virtual_unwind( context
->Rip
, &got_info
, &dispatch
->EstablisherFrame
,
1618 context
, &dispatch
->LanguageHandler
, &dispatch
->HandlerData
);
1619 if (status
!= STATUS_SUCCESS
) return status
;
1624 if (dispatch
->LanguageHandler
&& !module
)
1626 FIXME( "calling personality routine in system library not supported yet\n" );
1627 dispatch
->LanguageHandler
= NULL
;
1629 return STATUS_SUCCESS
;
1632 else WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
1634 /* no exception information, treat as a leaf function */
1636 dispatch
->EstablisherFrame
= context
->Rsp
;
1637 dispatch
->LanguageHandler
= NULL
;
1638 context
->Rip
= *(ULONG64
*)context
->Rsp
;
1639 context
->Rsp
= context
->Rsp
+ sizeof(ULONG64
);
1640 return STATUS_SUCCESS
;
1643 /***********************************************************************
1646 static inline int dispatch_signal(unsigned int sig
)
1648 if (handlers
[sig
] == NULL
) return 0;
1649 return handlers
[sig
](sig
);
1652 /***********************************************************************
1655 * Get the base of the signal stack for the current thread.
1657 static inline void *get_signal_stack(void)
1659 return (char *)NtCurrentTeb() + teb_size
;
1662 /***********************************************************************
1663 * is_inside_signal_stack
1665 * Check if pointer is inside the signal stack.
1667 static inline BOOL
is_inside_signal_stack( void *ptr
)
1669 return ((char *)ptr
>= (char *)get_signal_stack() &&
1670 (char *)ptr
< (char *)get_signal_stack() + signal_stack_size
);
1673 /***********************************************************************
1676 * Set the register values from a sigcontext.
1678 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
1680 context
->ContextFlags
= CONTEXT_CONTROL
| CONTEXT_INTEGER
| CONTEXT_SEGMENTS
| CONTEXT_DEBUG_REGISTERS
;
1681 context
->Rax
= RAX_sig(sigcontext
);
1682 context
->Rcx
= RCX_sig(sigcontext
);
1683 context
->Rdx
= RDX_sig(sigcontext
);
1684 context
->Rbx
= RBX_sig(sigcontext
);
1685 context
->Rsp
= RSP_sig(sigcontext
);
1686 context
->Rbp
= RBP_sig(sigcontext
);
1687 context
->Rsi
= RSI_sig(sigcontext
);
1688 context
->Rdi
= RDI_sig(sigcontext
);
1689 context
->R8
= R8_sig(sigcontext
);
1690 context
->R9
= R9_sig(sigcontext
);
1691 context
->R10
= R10_sig(sigcontext
);
1692 context
->R11
= R11_sig(sigcontext
);
1693 context
->R12
= R12_sig(sigcontext
);
1694 context
->R13
= R13_sig(sigcontext
);
1695 context
->R14
= R14_sig(sigcontext
);
1696 context
->R15
= R15_sig(sigcontext
);
1697 context
->Rip
= RIP_sig(sigcontext
);
1698 context
->SegCs
= CS_sig(sigcontext
);
1699 context
->SegFs
= FS_sig(sigcontext
);
1700 context
->SegGs
= GS_sig(sigcontext
);
1701 context
->EFlags
= EFL_sig(sigcontext
);
1703 context
->SegDs
= DS_sig(sigcontext
);
1705 __asm__("movw %%ds,%0" : "=m" (context
->SegDs
));
1708 context
->SegEs
= ES_sig(sigcontext
);
1710 __asm__("movw %%es,%0" : "=m" (context
->SegEs
));
1713 context
->SegSs
= SS_sig(sigcontext
);
1715 __asm__("movw %%ss,%0" : "=m" (context
->SegSs
));
1717 context
->Dr0
= amd64_thread_data()->dr0
;
1718 context
->Dr1
= amd64_thread_data()->dr1
;
1719 context
->Dr2
= amd64_thread_data()->dr2
;
1720 context
->Dr3
= amd64_thread_data()->dr3
;
1721 context
->Dr6
= amd64_thread_data()->dr6
;
1722 context
->Dr7
= amd64_thread_data()->dr7
;
1723 if (FPU_sig(sigcontext
))
1725 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1726 context
->u
.FltSave
= *FPU_sig(sigcontext
);
1727 context
->MxCsr
= context
->u
.FltSave
.MxCsr
;
1732 /***********************************************************************
1735 * Build a sigcontext from the register values.
1737 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1739 amd64_thread_data()->dr0
= context
->Dr0
;
1740 amd64_thread_data()->dr1
= context
->Dr1
;
1741 amd64_thread_data()->dr2
= context
->Dr2
;
1742 amd64_thread_data()->dr3
= context
->Dr3
;
1743 amd64_thread_data()->dr6
= context
->Dr6
;
1744 amd64_thread_data()->dr7
= context
->Dr7
;
1745 set_sigcontext( context
, sigcontext
);
1746 if (FPU_sig(sigcontext
)) *FPU_sig(sigcontext
) = context
->u
.FltSave
;
1750 /**************************************************************************
1751 * __chkstk (NTDLL.@)
1753 * Supposed to touch all the stack pages, but we shouldn't need that.
1755 __ASM_GLOBAL_FUNC( __chkstk
, "ret" );
1758 /***********************************************************************
1759 * RtlCaptureContext (NTDLL.@)
1761 __ASM_GLOBAL_FUNC( RtlCaptureContext
,
1763 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1764 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1765 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1766 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1767 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1768 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1769 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1770 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1771 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1772 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1773 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1774 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1775 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1776 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1777 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1778 "leaq 8(%rsp),%rax\n\t"
1779 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1780 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1781 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1782 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1783 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1784 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1785 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1786 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1787 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1788 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1789 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1790 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1791 "movq (%rsp),%rax\n\t"
1792 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1793 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1794 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1795 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1796 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1797 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1798 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1799 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1800 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1801 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1802 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1803 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1804 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1805 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1806 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1807 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1808 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1809 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1812 /***********************************************************************
1813 * set_full_cpu_context
1815 * Set the new CPU context.
1817 extern void set_full_cpu_context( const CONTEXT
*context
);
1818 __ASM_GLOBAL_FUNC( set_full_cpu_context
,
1820 __ASM_SEH(".seh_stackalloc 0x40\n\t")
1821 __ASM_SEH(".seh_endprologue\n\t")
1822 __ASM_CFI(".cfi_adjust_cfa_offset 40\n\t")
1823 "ldmxcsr 0x34(%rdi)\n\t" /* context->MxCsr */
1824 "movw 0x38(%rdi),%ax\n\t" /* context->SegCs */
1825 "movq %rax,8(%rsp)\n\t"
1826 "movw 0x42(%rdi),%ax\n\t" /* context->SegSs */
1827 "movq %rax,32(%rsp)\n\t"
1828 "movq 0x44(%rdi),%rax\n\t" /* context->Eflags */
1829 "movq %rax,16(%rsp)\n\t"
1830 "movq 0x80(%rdi),%rcx\n\t" /* context->Rcx */
1831 "movq 0x88(%rdi),%rdx\n\t" /* context->Rdx */
1832 "movq 0x90(%rdi),%rbx\n\t" /* context->Rbx */
1833 "movq 0x98(%rdi),%rax\n\t" /* context->Rsp */
1834 "movq %rax,24(%rsp)\n\t"
1835 "movq 0xa0(%rdi),%rbp\n\t" /* context->Rbp */
1836 "movq 0xa8(%rdi),%rsi\n\t" /* context->Rsi */
1837 "movq 0xb8(%rdi),%r8\n\t" /* context->R8 */
1838 "movq 0xc0(%rdi),%r9\n\t" /* context->R9 */
1839 "movq 0xc8(%rdi),%r10\n\t" /* context->R10 */
1840 "movq 0xd0(%rdi),%r11\n\t" /* context->R11 */
1841 "movq 0xd8(%rdi),%r12\n\t" /* context->R12 */
1842 "movq 0xe0(%rdi),%r13\n\t" /* context->R13 */
1843 "movq 0xe8(%rdi),%r14\n\t" /* context->R14 */
1844 "movq 0xf0(%rdi),%r15\n\t" /* context->R15 */
1845 "movq 0xf8(%rdi),%rax\n\t" /* context->Rip */
1846 "movq %rax,(%rsp)\n\t"
1847 "fxrstor 0x100(%rdi)\n\t" /* context->FtlSave */
1848 "movdqa 0x1a0(%rdi),%xmm0\n\t" /* context->Xmm0 */
1849 "movdqa 0x1b0(%rdi),%xmm1\n\t" /* context->Xmm1 */
1850 "movdqa 0x1c0(%rdi),%xmm2\n\t" /* context->Xmm2 */
1851 "movdqa 0x1d0(%rdi),%xmm3\n\t" /* context->Xmm3 */
1852 "movdqa 0x1e0(%rdi),%xmm4\n\t" /* context->Xmm4 */
1853 "movdqa 0x1f0(%rdi),%xmm5\n\t" /* context->Xmm5 */
1854 "movdqa 0x200(%rdi),%xmm6\n\t" /* context->Xmm6 */
1855 "movdqa 0x210(%rdi),%xmm7\n\t" /* context->Xmm7 */
1856 "movdqa 0x220(%rdi),%xmm8\n\t" /* context->Xmm8 */
1857 "movdqa 0x230(%rdi),%xmm9\n\t" /* context->Xmm9 */
1858 "movdqa 0x240(%rdi),%xmm10\n\t" /* context->Xmm10 */
1859 "movdqa 0x250(%rdi),%xmm11\n\t" /* context->Xmm11 */
1860 "movdqa 0x260(%rdi),%xmm12\n\t" /* context->Xmm12 */
1861 "movdqa 0x270(%rdi),%xmm13\n\t" /* context->Xmm13 */
1862 "movdqa 0x280(%rdi),%xmm14\n\t" /* context->Xmm14 */
1863 "movdqa 0x290(%rdi),%xmm15\n\t" /* context->Xmm15 */
1864 "movq 0x78(%rdi),%rax\n\t" /* context->Rax */
1865 "movq 0xb0(%rdi),%rdi\n\t" /* context->Rdi */
1869 /***********************************************************************
1872 * Set the new CPU context. Used by NtSetContextThread.
1874 void DECLSPEC_HIDDEN
set_cpu_context( const CONTEXT
*context
)
1876 DWORD flags
= context
->ContextFlags
& ~CONTEXT_AMD64
;
1878 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1880 amd64_thread_data()->dr0
= context
->Dr0
;
1881 amd64_thread_data()->dr1
= context
->Dr1
;
1882 amd64_thread_data()->dr2
= context
->Dr2
;
1883 amd64_thread_data()->dr3
= context
->Dr3
;
1884 amd64_thread_data()->dr6
= context
->Dr6
;
1885 amd64_thread_data()->dr7
= context
->Dr7
;
1887 if (flags
& CONTEXT_FULL
)
1889 if (!(flags
& CONTEXT_CONTROL
))
1890 FIXME( "setting partial context (%x) not supported\n", flags
);
1892 set_full_cpu_context( context
);
1897 /***********************************************************************
1898 * get_server_context_flags
1900 * Convert CPU-specific flags to generic server flags
1902 static unsigned int get_server_context_flags( DWORD flags
)
1904 unsigned int ret
= 0;
1906 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1907 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
1908 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
1909 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
1910 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
1911 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
1916 /***********************************************************************
1919 * Copy a register context according to the flags.
1921 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1923 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1924 if (flags
& CONTEXT_CONTROL
)
1926 to
->Rbp
= from
->Rbp
;
1927 to
->Rip
= from
->Rip
;
1928 to
->Rsp
= from
->Rsp
;
1929 to
->SegCs
= from
->SegCs
;
1930 to
->SegSs
= from
->SegSs
;
1931 to
->EFlags
= from
->EFlags
;
1933 if (flags
& CONTEXT_INTEGER
)
1935 to
->Rax
= from
->Rax
;
1936 to
->Rcx
= from
->Rcx
;
1937 to
->Rdx
= from
->Rdx
;
1938 to
->Rbx
= from
->Rbx
;
1939 to
->Rsi
= from
->Rsi
;
1940 to
->Rdi
= from
->Rdi
;
1943 to
->R10
= from
->R10
;
1944 to
->R11
= from
->R11
;
1945 to
->R12
= from
->R12
;
1946 to
->R13
= from
->R13
;
1947 to
->R14
= from
->R14
;
1948 to
->R15
= from
->R15
;
1950 if (flags
& CONTEXT_SEGMENTS
)
1952 to
->SegDs
= from
->SegDs
;
1953 to
->SegEs
= from
->SegEs
;
1954 to
->SegFs
= from
->SegFs
;
1955 to
->SegGs
= from
->SegGs
;
1957 if (flags
& CONTEXT_FLOATING_POINT
)
1959 to
->MxCsr
= from
->MxCsr
;
1960 to
->u
.FltSave
= from
->u
.FltSave
;
1962 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1964 to
->Dr0
= from
->Dr0
;
1965 to
->Dr1
= from
->Dr1
;
1966 to
->Dr2
= from
->Dr2
;
1967 to
->Dr3
= from
->Dr3
;
1968 to
->Dr6
= from
->Dr6
;
1969 to
->Dr7
= from
->Dr7
;
1974 /***********************************************************************
1977 * Convert a register context to the server format.
1979 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1981 DWORD flags
= from
->ContextFlags
& ~CONTEXT_AMD64
; /* get rid of CPU id */
1983 memset( to
, 0, sizeof(*to
) );
1984 to
->cpu
= CPU_x86_64
;
1986 if (flags
& CONTEXT_CONTROL
)
1988 to
->flags
|= SERVER_CTX_CONTROL
;
1989 to
->ctl
.x86_64_regs
.rbp
= from
->Rbp
;
1990 to
->ctl
.x86_64_regs
.rip
= from
->Rip
;
1991 to
->ctl
.x86_64_regs
.rsp
= from
->Rsp
;
1992 to
->ctl
.x86_64_regs
.cs
= from
->SegCs
;
1993 to
->ctl
.x86_64_regs
.ss
= from
->SegSs
;
1994 to
->ctl
.x86_64_regs
.flags
= from
->EFlags
;
1996 if (flags
& CONTEXT_INTEGER
)
1998 to
->flags
|= SERVER_CTX_INTEGER
;
1999 to
->integer
.x86_64_regs
.rax
= from
->Rax
;
2000 to
->integer
.x86_64_regs
.rcx
= from
->Rcx
;
2001 to
->integer
.x86_64_regs
.rdx
= from
->Rdx
;
2002 to
->integer
.x86_64_regs
.rbx
= from
->Rbx
;
2003 to
->integer
.x86_64_regs
.rsi
= from
->Rsi
;
2004 to
->integer
.x86_64_regs
.rdi
= from
->Rdi
;
2005 to
->integer
.x86_64_regs
.r8
= from
->R8
;
2006 to
->integer
.x86_64_regs
.r9
= from
->R9
;
2007 to
->integer
.x86_64_regs
.r10
= from
->R10
;
2008 to
->integer
.x86_64_regs
.r11
= from
->R11
;
2009 to
->integer
.x86_64_regs
.r12
= from
->R12
;
2010 to
->integer
.x86_64_regs
.r13
= from
->R13
;
2011 to
->integer
.x86_64_regs
.r14
= from
->R14
;
2012 to
->integer
.x86_64_regs
.r15
= from
->R15
;
2014 if (flags
& CONTEXT_SEGMENTS
)
2016 to
->flags
|= SERVER_CTX_SEGMENTS
;
2017 to
->seg
.x86_64_regs
.ds
= from
->SegDs
;
2018 to
->seg
.x86_64_regs
.es
= from
->SegEs
;
2019 to
->seg
.x86_64_regs
.fs
= from
->SegFs
;
2020 to
->seg
.x86_64_regs
.gs
= from
->SegGs
;
2022 if (flags
& CONTEXT_FLOATING_POINT
)
2024 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
2025 memcpy( to
->fp
.x86_64_regs
.fpregs
, &from
->u
.FltSave
, sizeof(to
->fp
.x86_64_regs
.fpregs
) );
2027 if (flags
& CONTEXT_DEBUG_REGISTERS
)
2029 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
2030 to
->debug
.x86_64_regs
.dr0
= from
->Dr0
;
2031 to
->debug
.x86_64_regs
.dr1
= from
->Dr1
;
2032 to
->debug
.x86_64_regs
.dr2
= from
->Dr2
;
2033 to
->debug
.x86_64_regs
.dr3
= from
->Dr3
;
2034 to
->debug
.x86_64_regs
.dr6
= from
->Dr6
;
2035 to
->debug
.x86_64_regs
.dr7
= from
->Dr7
;
2037 return STATUS_SUCCESS
;
2041 /***********************************************************************
2042 * context_from_server
2044 * Convert a register context from the server format.
2046 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
2048 if (from
->cpu
!= CPU_x86_64
) return STATUS_INVALID_PARAMETER
;
2050 to
->ContextFlags
= CONTEXT_AMD64
;
2051 if (from
->flags
& SERVER_CTX_CONTROL
)
2053 to
->ContextFlags
|= CONTEXT_CONTROL
;
2054 to
->Rbp
= from
->ctl
.x86_64_regs
.rbp
;
2055 to
->Rip
= from
->ctl
.x86_64_regs
.rip
;
2056 to
->Rsp
= from
->ctl
.x86_64_regs
.rsp
;
2057 to
->SegCs
= from
->ctl
.x86_64_regs
.cs
;
2058 to
->SegSs
= from
->ctl
.x86_64_regs
.ss
;
2059 to
->EFlags
= from
->ctl
.x86_64_regs
.flags
;
2062 if (from
->flags
& SERVER_CTX_INTEGER
)
2064 to
->ContextFlags
|= CONTEXT_INTEGER
;
2065 to
->Rax
= from
->integer
.x86_64_regs
.rax
;
2066 to
->Rcx
= from
->integer
.x86_64_regs
.rcx
;
2067 to
->Rdx
= from
->integer
.x86_64_regs
.rdx
;
2068 to
->Rbx
= from
->integer
.x86_64_regs
.rbx
;
2069 to
->Rsi
= from
->integer
.x86_64_regs
.rsi
;
2070 to
->Rdi
= from
->integer
.x86_64_regs
.rdi
;
2071 to
->R8
= from
->integer
.x86_64_regs
.r8
;
2072 to
->R9
= from
->integer
.x86_64_regs
.r9
;
2073 to
->R10
= from
->integer
.x86_64_regs
.r10
;
2074 to
->R11
= from
->integer
.x86_64_regs
.r11
;
2075 to
->R12
= from
->integer
.x86_64_regs
.r12
;
2076 to
->R13
= from
->integer
.x86_64_regs
.r13
;
2077 to
->R14
= from
->integer
.x86_64_regs
.r14
;
2078 to
->R15
= from
->integer
.x86_64_regs
.r15
;
2080 if (from
->flags
& SERVER_CTX_SEGMENTS
)
2082 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
2083 to
->SegDs
= from
->seg
.x86_64_regs
.ds
;
2084 to
->SegEs
= from
->seg
.x86_64_regs
.es
;
2085 to
->SegFs
= from
->seg
.x86_64_regs
.fs
;
2086 to
->SegGs
= from
->seg
.x86_64_regs
.gs
;
2088 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
2090 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
2091 memcpy( &to
->u
.FltSave
, from
->fp
.x86_64_regs
.fpregs
, sizeof(from
->fp
.x86_64_regs
.fpregs
) );
2092 to
->MxCsr
= to
->u
.FltSave
.MxCsr
;
2094 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
2096 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
2097 to
->Dr0
= from
->debug
.x86_64_regs
.dr0
;
2098 to
->Dr1
= from
->debug
.x86_64_regs
.dr1
;
2099 to
->Dr2
= from
->debug
.x86_64_regs
.dr2
;
2100 to
->Dr3
= from
->debug
.x86_64_regs
.dr3
;
2101 to
->Dr6
= from
->debug
.x86_64_regs
.dr6
;
2102 to
->Dr7
= from
->debug
.x86_64_regs
.dr7
;
2104 return STATUS_SUCCESS
;
2108 /***********************************************************************
2109 * NtSetContextThread (NTDLL.@)
2110 * ZwSetContextThread (NTDLL.@)
2112 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
2114 NTSTATUS ret
= STATUS_SUCCESS
;
2115 BOOL self
= (handle
== GetCurrentThread());
2117 /* debug registers require a server call */
2118 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
)))
2119 self
= (amd64_thread_data()->dr0
== context
->Dr0
&&
2120 amd64_thread_data()->dr1
== context
->Dr1
&&
2121 amd64_thread_data()->dr2
== context
->Dr2
&&
2122 amd64_thread_data()->dr3
== context
->Dr3
&&
2123 amd64_thread_data()->dr6
== context
->Dr6
&&
2124 amd64_thread_data()->dr7
== context
->Dr7
);
2128 context_t server_context
;
2129 context_to_server( &server_context
, context
);
2130 ret
= set_thread_context( handle
, &server_context
, &self
);
2132 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
2137 /***********************************************************************
2138 * NtGetContextThread (NTDLL.@)
2139 * ZwGetContextThread (NTDLL.@)
2141 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
2145 BOOL self
= (handle
== GetCurrentThread());
2147 if (!context
) return STATUS_INVALID_PARAMETER
;
2149 needed_flags
= context
->ContextFlags
;
2151 /* debug registers require a server call */
2152 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
)) self
= FALSE
;
2156 context_t server_context
;
2157 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
2159 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
2160 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
2161 needed_flags
&= ~context
->ContextFlags
;
2169 RtlCaptureContext( &ctx
);
2170 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
2171 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
2173 /* update the cached version of the debug registers */
2174 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
))
2176 amd64_thread_data()->dr0
= context
->Dr0
;
2177 amd64_thread_data()->dr1
= context
->Dr1
;
2178 amd64_thread_data()->dr2
= context
->Dr2
;
2179 amd64_thread_data()->dr3
= context
->Dr3
;
2180 amd64_thread_data()->dr6
= context
->Dr6
;
2181 amd64_thread_data()->dr7
= context
->Dr7
;
2184 return STATUS_SUCCESS
;
2188 /***********************************************************************
2189 * wow64_get_server_context_flags
2191 static unsigned int wow64_get_server_context_flags( DWORD flags
)
2193 unsigned int ret
= 0;
2195 flags
&= ~WOW64_CONTEXT_i386
; /* get rid of CPU id */
2196 if (flags
& WOW64_CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
2197 if (flags
& WOW64_CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
2198 if (flags
& WOW64_CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
2199 if (flags
& WOW64_CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
2200 if (flags
& WOW64_CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
2201 if (flags
& WOW64_CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
2205 /***********************************************************************
2206 * wow64_context_from_server
2208 static NTSTATUS
wow64_context_from_server( WOW64_CONTEXT
*to
, const context_t
*from
)
2210 if (from
->cpu
!= CPU_x86
) return STATUS_INVALID_PARAMETER
;
2212 to
->ContextFlags
= WOW64_CONTEXT_i386
;
2213 if (from
->flags
& SERVER_CTX_CONTROL
)
2215 to
->ContextFlags
|= WOW64_CONTEXT_CONTROL
;
2216 to
->Ebp
= from
->ctl
.i386_regs
.ebp
;
2217 to
->Esp
= from
->ctl
.i386_regs
.esp
;
2218 to
->Eip
= from
->ctl
.i386_regs
.eip
;
2219 to
->SegCs
= from
->ctl
.i386_regs
.cs
;
2220 to
->SegSs
= from
->ctl
.i386_regs
.ss
;
2221 to
->EFlags
= from
->ctl
.i386_regs
.eflags
;
2223 if (from
->flags
& SERVER_CTX_INTEGER
)
2225 to
->ContextFlags
|= WOW64_CONTEXT_INTEGER
;
2226 to
->Eax
= from
->integer
.i386_regs
.eax
;
2227 to
->Ebx
= from
->integer
.i386_regs
.ebx
;
2228 to
->Ecx
= from
->integer
.i386_regs
.ecx
;
2229 to
->Edx
= from
->integer
.i386_regs
.edx
;
2230 to
->Esi
= from
->integer
.i386_regs
.esi
;
2231 to
->Edi
= from
->integer
.i386_regs
.edi
;
2233 if (from
->flags
& SERVER_CTX_SEGMENTS
)
2235 to
->ContextFlags
|= WOW64_CONTEXT_SEGMENTS
;
2236 to
->SegDs
= from
->seg
.i386_regs
.ds
;
2237 to
->SegEs
= from
->seg
.i386_regs
.es
;
2238 to
->SegFs
= from
->seg
.i386_regs
.fs
;
2239 to
->SegGs
= from
->seg
.i386_regs
.gs
;
2241 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
2243 to
->ContextFlags
|= WOW64_CONTEXT_FLOATING_POINT
;
2244 to
->FloatSave
.ControlWord
= from
->fp
.i386_regs
.ctrl
;
2245 to
->FloatSave
.StatusWord
= from
->fp
.i386_regs
.status
;
2246 to
->FloatSave
.TagWord
= from
->fp
.i386_regs
.tag
;
2247 to
->FloatSave
.ErrorOffset
= from
->fp
.i386_regs
.err_off
;
2248 to
->FloatSave
.ErrorSelector
= from
->fp
.i386_regs
.err_sel
;
2249 to
->FloatSave
.DataOffset
= from
->fp
.i386_regs
.data_off
;
2250 to
->FloatSave
.DataSelector
= from
->fp
.i386_regs
.data_sel
;
2251 to
->FloatSave
.Cr0NpxState
= from
->fp
.i386_regs
.cr0npx
;
2252 memcpy( to
->FloatSave
.RegisterArea
, from
->fp
.i386_regs
.regs
, sizeof(to
->FloatSave
.RegisterArea
) );
2254 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
2256 to
->ContextFlags
|= WOW64_CONTEXT_DEBUG_REGISTERS
;
2257 to
->Dr0
= from
->debug
.i386_regs
.dr0
;
2258 to
->Dr1
= from
->debug
.i386_regs
.dr1
;
2259 to
->Dr2
= from
->debug
.i386_regs
.dr2
;
2260 to
->Dr3
= from
->debug
.i386_regs
.dr3
;
2261 to
->Dr6
= from
->debug
.i386_regs
.dr6
;
2262 to
->Dr7
= from
->debug
.i386_regs
.dr7
;
2264 if (from
->flags
& SERVER_CTX_EXTENDED_REGISTERS
)
2266 to
->ContextFlags
|= WOW64_CONTEXT_EXTENDED_REGISTERS
;
2267 memcpy( to
->ExtendedRegisters
, from
->ext
.i386_regs
, sizeof(to
->ExtendedRegisters
) );
2269 return STATUS_SUCCESS
;
2272 /***********************************************************************
2273 * wow64_context_to_server
2275 static void wow64_context_to_server( context_t
*to
, const WOW64_CONTEXT
*from
)
2277 DWORD flags
= from
->ContextFlags
& ~WOW64_CONTEXT_i386
; /* get rid of CPU id */
2279 memset( to
, 0, sizeof(*to
) );
2282 if (flags
& WOW64_CONTEXT_CONTROL
)
2284 to
->flags
|= SERVER_CTX_CONTROL
;
2285 to
->ctl
.i386_regs
.ebp
= from
->Ebp
;
2286 to
->ctl
.i386_regs
.esp
= from
->Esp
;
2287 to
->ctl
.i386_regs
.eip
= from
->Eip
;
2288 to
->ctl
.i386_regs
.cs
= from
->SegCs
;
2289 to
->ctl
.i386_regs
.ss
= from
->SegSs
;
2290 to
->ctl
.i386_regs
.eflags
= from
->EFlags
;
2292 if (flags
& WOW64_CONTEXT_INTEGER
)
2294 to
->flags
|= SERVER_CTX_INTEGER
;
2295 to
->integer
.i386_regs
.eax
= from
->Eax
;
2296 to
->integer
.i386_regs
.ebx
= from
->Ebx
;
2297 to
->integer
.i386_regs
.ecx
= from
->Ecx
;
2298 to
->integer
.i386_regs
.edx
= from
->Edx
;
2299 to
->integer
.i386_regs
.esi
= from
->Esi
;
2300 to
->integer
.i386_regs
.edi
= from
->Edi
;
2302 if (flags
& WOW64_CONTEXT_SEGMENTS
)
2304 to
->flags
|= SERVER_CTX_SEGMENTS
;
2305 to
->seg
.i386_regs
.ds
= from
->SegDs
;
2306 to
->seg
.i386_regs
.es
= from
->SegEs
;
2307 to
->seg
.i386_regs
.fs
= from
->SegFs
;
2308 to
->seg
.i386_regs
.gs
= from
->SegGs
;
2310 if (flags
& WOW64_CONTEXT_FLOATING_POINT
)
2312 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
2313 to
->fp
.i386_regs
.ctrl
= from
->FloatSave
.ControlWord
;
2314 to
->fp
.i386_regs
.status
= from
->FloatSave
.StatusWord
;
2315 to
->fp
.i386_regs
.tag
= from
->FloatSave
.TagWord
;
2316 to
->fp
.i386_regs
.err_off
= from
->FloatSave
.ErrorOffset
;
2317 to
->fp
.i386_regs
.err_sel
= from
->FloatSave
.ErrorSelector
;
2318 to
->fp
.i386_regs
.data_off
= from
->FloatSave
.DataOffset
;
2319 to
->fp
.i386_regs
.data_sel
= from
->FloatSave
.DataSelector
;
2320 to
->fp
.i386_regs
.cr0npx
= from
->FloatSave
.Cr0NpxState
;
2321 memcpy( to
->fp
.i386_regs
.regs
, from
->FloatSave
.RegisterArea
, sizeof(to
->fp
.i386_regs
.regs
) );
2323 if (flags
& WOW64_CONTEXT_DEBUG_REGISTERS
)
2325 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
2326 to
->debug
.i386_regs
.dr0
= from
->Dr0
;
2327 to
->debug
.i386_regs
.dr1
= from
->Dr1
;
2328 to
->debug
.i386_regs
.dr2
= from
->Dr2
;
2329 to
->debug
.i386_regs
.dr3
= from
->Dr3
;
2330 to
->debug
.i386_regs
.dr6
= from
->Dr6
;
2331 to
->debug
.i386_regs
.dr7
= from
->Dr7
;
2333 if (flags
& WOW64_CONTEXT_EXTENDED_REGISTERS
)
2335 to
->flags
|= SERVER_CTX_EXTENDED_REGISTERS
;
2336 memcpy( to
->ext
.i386_regs
, from
->ExtendedRegisters
, sizeof(to
->ext
.i386_regs
) );
2341 /******************************************************************************
2342 * RtlWow64GetThreadContext (NTDLL.@)
2344 NTSTATUS WINAPI
RtlWow64GetThreadContext( HANDLE handle
, WOW64_CONTEXT
*context
)
2348 context_t server_context
;
2349 unsigned int server_flags
= wow64_get_server_context_flags( context
->ContextFlags
);
2351 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
2352 if (self
) return STATUS_INVALID_PARAMETER
;
2353 return wow64_context_from_server( context
, &server_context
);
2357 /******************************************************************************
2358 * RtlWow64SetThreadContext (NTDLL.@)
2360 NTSTATUS WINAPI
RtlWow64SetThreadContext( HANDLE handle
, const WOW64_CONTEXT
*context
)
2363 context_t server_context
;
2365 wow64_context_to_server( &server_context
, context
);
2366 return set_thread_context( handle
, &server_context
, &self
);
2370 static DWORD __cdecl
nested_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
2371 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
2373 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)) return ExceptionContinueSearch
;
2376 return ExceptionNestedException
;
2379 /**********************************************************************
2382 * Call a single exception handler.
2383 * FIXME: Handle nested exceptions.
2385 static DWORD
call_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
)
2387 EXCEPTION_REGISTRATION_RECORD frame
;
2390 frame
.Handler
= nested_exception_handler
;
2391 __wine_push_frame( &frame
);
2393 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2394 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2395 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, context
, dispatch
);
2396 TRACE( "handler at %p returned %u\n", dispatch
->LanguageHandler
, res
);
2398 rec
->ExceptionFlags
&= EH_NONCONTINUABLE
;
2399 __wine_pop_frame( &frame
);
2404 /**********************************************************************
2407 * Call a single exception handler from the TEB chain.
2408 * FIXME: Handle nested exceptions.
2410 static DWORD
call_teb_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
,
2411 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
2415 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
2416 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
2417 res
= teb_frame
->Handler( rec
, teb_frame
, context
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
2418 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
2423 /**********************************************************************
2424 * call_stack_handlers
2426 * Call the stack handlers chain.
2428 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
2430 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2431 UNWIND_HISTORY_TABLE table
;
2432 DISPATCHER_CONTEXT dispatch
;
2436 context
= *orig_context
;
2437 dispatch
.TargetIp
= 0;
2438 dispatch
.ContextRecord
= &context
;
2439 dispatch
.HistoryTable
= &table
;
2442 status
= virtual_unwind( UNW_FLAG_EHANDLER
, &dispatch
, &context
);
2443 if (status
!= STATUS_SUCCESS
) return status
;
2446 if (!dispatch
.EstablisherFrame
) break;
2448 if ((dispatch
.EstablisherFrame
& 7) ||
2449 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
2450 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
2452 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
2453 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2454 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
2458 if (dispatch
.LanguageHandler
)
2460 switch (call_handler( rec
, orig_context
, &dispatch
))
2462 case ExceptionContinueExecution
:
2463 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
2464 return STATUS_SUCCESS
;
2465 case ExceptionContinueSearch
:
2467 case ExceptionNestedException
:
2468 FIXME( "nested exception\n" );
2470 case ExceptionCollidedUnwind
: {
2473 context
= *dispatch
.ContextRecord
;
2474 dispatch
.ContextRecord
= &context
;
2475 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
2476 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2477 &context
, NULL
, &frame
, NULL
);
2481 return STATUS_INVALID_DISPOSITION
;
2484 /* hack: call wine handlers registered in the tib list */
2485 else while ((ULONG64
)teb_frame
< context
.Rsp
)
2487 TRACE( "found wine frame %p rsp %lx handler %p\n",
2488 teb_frame
, context
.Rsp
, teb_frame
->Handler
);
2489 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
2490 switch (call_teb_handler( rec
, orig_context
, &dispatch
, teb_frame
))
2492 case ExceptionContinueExecution
:
2493 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
2494 return STATUS_SUCCESS
;
2495 case ExceptionContinueSearch
:
2497 case ExceptionNestedException
:
2498 FIXME( "nested exception\n" );
2500 case ExceptionCollidedUnwind
: {
2503 context
= *dispatch
.ContextRecord
;
2504 dispatch
.ContextRecord
= &context
;
2505 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
2506 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
2507 &context
, NULL
, &frame
, NULL
);
2508 teb_frame
= teb_frame
->Prev
;
2512 return STATUS_INVALID_DISPOSITION
;
2514 teb_frame
= teb_frame
->Prev
;
2517 if (context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
2519 return STATUS_UNHANDLED_EXCEPTION
;
2523 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2531 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2532 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
2533 context
->Rip
, GetCurrentThreadId() );
2534 for (c
= 0; c
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); c
++)
2535 TRACE( " info[%d]=%016lx\n", c
, rec
->ExceptionInformation
[c
] );
2536 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
2538 if (rec
->ExceptionInformation
[1] >> 16)
2539 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2540 rec
->ExceptionAddress
,
2541 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
2543 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2544 rec
->ExceptionAddress
,
2545 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
2549 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2550 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
2551 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2552 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
2553 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2554 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
2555 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2556 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
2559 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
2560 if (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) context
->Rip
--;
2562 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
) goto done
;
2564 if ((status
= call_stack_handlers( rec
, context
)) == STATUS_SUCCESS
) goto done
;
2565 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
2568 /* last chance exception */
2570 status
= send_debug_event( rec
, FALSE
, context
);
2571 if (status
!= DBG_CONTINUE
)
2573 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
2574 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2575 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
2576 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2578 ERR("Unhandled exception code %x flags %x addr %p\n",
2579 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
2580 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
2584 return NtSetContextThread( GetCurrentThread(), context
);
2588 /*******************************************************************
2589 * NtRaiseException (NTDLL.@)
2591 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2597 status
= send_debug_event( rec
, TRUE
, context
);
2598 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
2599 return NtSetContextThread( GetCurrentThread(), context
);
2602 return raise_exception( rec
, context
, first_chance
);
2606 /**********************************************************************
2607 * raise_generic_exception
2609 * Generic raise function for exceptions that don't need special treatment.
2611 static void raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2613 NTSTATUS status
= raise_exception( rec
, context
, TRUE
);
2614 raise_status( status
, rec
);
2618 extern void raise_func_trampoline( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
2619 __ASM_GLOBAL_FUNC( raise_func_trampoline
,
2620 __ASM_CFI(".cfi_signal_frame\n\t")
2621 __ASM_CFI(".cfi_def_cfa %rbp,160\n\t") /* red zone + rip + rbp + rdi + rsi */
2622 __ASM_CFI(".cfi_rel_offset %rip,24\n\t")
2623 __ASM_CFI(".cfi_rel_offset %rbp,16\n\t")
2624 __ASM_CFI(".cfi_rel_offset %rdi,8\n\t")
2625 __ASM_CFI(".cfi_rel_offset %rsi,0\n\t")
2629 /***********************************************************************
2632 * Setup a proper stack frame for the raise function, and modify the
2633 * sigcontext so that the return from the signal handler will call
2634 * the raise function.
2636 static struct stack_layout
*setup_exception( ucontext_t
*sigcontext
)
2638 struct stack_layout
*stack
;
2639 DWORD exception_code
= 0;
2641 stack
= (struct stack_layout
*)(RSP_sig(sigcontext
) & ~15);
2643 /* stack sanity checks */
2645 if (is_inside_signal_stack( stack
))
2647 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2648 GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
), (ULONG_PTR
)RSP_sig(sigcontext
),
2649 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2653 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
2654 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
2655 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
2657 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2658 GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
), (ULONG_PTR
)RSP_sig(sigcontext
),
2659 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2661 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
2663 /* stack overflow on last page, unrecoverable */
2664 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
2665 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2666 diff
, GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
),
2667 (ULONG_PTR
)RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
2668 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2671 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
2673 /* stack access below stack limit, may be recoverable */
2674 switch (virtual_handle_stack_fault( stack
- 1 ))
2676 case 0: /* not handled */
2678 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
2679 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2680 diff
, GetCurrentThreadId(), (ULONG_PTR
)RIP_sig(sigcontext
),
2681 (ULONG_PTR
)RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
2682 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2685 case -1: /* overflow */
2686 exception_code
= EXCEPTION_STACK_OVERFLOW
;
2691 stack
--; /* push the stack_layout structure */
2692 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
2693 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
2694 #elif defined(VALGRIND_MAKE_WRITABLE)
2695 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
2697 stack
->rec
.ExceptionRecord
= NULL
;
2698 stack
->rec
.ExceptionCode
= exception_code
;
2699 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
2700 stack
->rec
.ExceptionAddress
= (void *)RIP_sig(sigcontext
);
2701 stack
->rec
.NumberParameters
= 0;
2702 save_context( &stack
->context
, sigcontext
);
2707 static void setup_raise_exception( ucontext_t
*sigcontext
, struct stack_layout
*stack
)
2712 if (stack
->rec
.ExceptionCode
== EXCEPTION_SINGLE_STEP
)
2714 /* when single stepping can't tell whether this is a hw bp or a
2715 * single step interrupt. try to avoid as much overhead as possible
2716 * and only do a server call if there is any hw bp enabled. */
2718 if (!(stack
->context
.EFlags
& 0x100) || (stack
->context
.Dr7
& 0xff))
2720 /* (possible) hardware breakpoint, fetch the debug registers */
2721 DWORD saved_flags
= stack
->context
.ContextFlags
;
2722 stack
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
2723 NtGetContextThread(GetCurrentThread(), &stack
->context
);
2724 stack
->context
.ContextFlags
|= saved_flags
; /* restore flags */
2727 stack
->context
.EFlags
&= ~0x100; /* clear single-step flag */
2730 status
= send_debug_event( &stack
->rec
, TRUE
, &stack
->context
);
2731 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
2733 restore_context( &stack
->context
, sigcontext
);
2737 /* store return address and %rbp without aligning, so that the offset is fixed */
2738 rsp_ptr
= (ULONG64
*)RSP_sig(sigcontext
) - 16;
2739 *(--rsp_ptr
) = stack
->context
.Rip
;
2740 *(--rsp_ptr
) = stack
->context
.Rbp
;
2741 *(--rsp_ptr
) = stack
->context
.Rdi
;
2742 *(--rsp_ptr
) = stack
->context
.Rsi
;
2744 /* now modify the sigcontext to return to the raise function */
2745 RIP_sig(sigcontext
) = (ULONG_PTR
)raise_func_trampoline
;
2746 RDI_sig(sigcontext
) = (ULONG_PTR
)&stack
->rec
;
2747 RSI_sig(sigcontext
) = (ULONG_PTR
)&stack
->context
;
2748 RDX_sig(sigcontext
) = (ULONG_PTR
)raise_generic_exception
;
2749 RBP_sig(sigcontext
) = (ULONG_PTR
)rsp_ptr
;
2750 RSP_sig(sigcontext
) = (ULONG_PTR
)stack
;
2751 /* clear single-step, direction, and align check flag */
2752 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
2756 /***********************************************************************
2757 * is_privileged_instr
2759 * Check if the fault location is a privileged instruction.
2761 static inline DWORD
is_privileged_instr( CONTEXT
*context
)
2764 unsigned int i
, prefix_count
= 0;
2765 unsigned int len
= virtual_uninterrupted_read_memory( (BYTE
*)context
->Rip
, instr
, sizeof(instr
) );
2767 for (i
= 0; i
< len
; i
++) switch (instr
[i
])
2769 /* instruction prefixes */
2770 case 0x2e: /* %cs: */
2771 case 0x36: /* %ss: */
2772 case 0x3e: /* %ds: */
2773 case 0x26: /* %es: */
2774 case 0x40: /* rex */
2775 case 0x41: /* rex */
2776 case 0x42: /* rex */
2777 case 0x43: /* rex */
2778 case 0x44: /* rex */
2779 case 0x45: /* rex */
2780 case 0x46: /* rex */
2781 case 0x47: /* rex */
2782 case 0x48: /* rex */
2783 case 0x49: /* rex */
2784 case 0x4a: /* rex */
2785 case 0x4b: /* rex */
2786 case 0x4c: /* rex */
2787 case 0x4d: /* rex */
2788 case 0x4e: /* rex */
2789 case 0x4f: /* rex */
2790 case 0x64: /* %fs: */
2791 case 0x65: /* %gs: */
2792 case 0x66: /* opcode size */
2793 case 0x67: /* addr size */
2794 case 0xf0: /* lock */
2795 case 0xf2: /* repne */
2796 case 0xf3: /* repe */
2797 if (++prefix_count
>= 15) return EXCEPTION_ILLEGAL_INSTRUCTION
;
2800 case 0x0f: /* extended instruction */
2801 if (i
== len
- 1) return 0;
2802 switch (instr
[i
+ 1])
2804 case 0x06: /* clts */
2805 case 0x08: /* invd */
2806 case 0x09: /* wbinvd */
2807 case 0x20: /* mov crX, reg */
2808 case 0x21: /* mov drX, reg */
2809 case 0x22: /* mov reg, crX */
2810 case 0x23: /* mov reg drX */
2811 return EXCEPTION_PRIV_INSTRUCTION
;
2814 case 0x6c: /* insb (%dx) */
2815 case 0x6d: /* insl (%dx) */
2816 case 0x6e: /* outsb (%dx) */
2817 case 0x6f: /* outsl (%dx) */
2818 case 0xcd: /* int $xx */
2819 case 0xe4: /* inb al,XX */
2820 case 0xe5: /* in (e)ax,XX */
2821 case 0xe6: /* outb XX,al */
2822 case 0xe7: /* out XX,(e)ax */
2823 case 0xec: /* inb (%dx),%al */
2824 case 0xed: /* inl (%dx),%eax */
2825 case 0xee: /* outb %al,(%dx) */
2826 case 0xef: /* outl %eax,(%dx) */
2827 case 0xf4: /* hlt */
2828 case 0xfa: /* cli */
2829 case 0xfb: /* sti */
2830 return EXCEPTION_PRIV_INSTRUCTION
;
2838 /***********************************************************************
2841 * Handle an interrupt.
2843 static inline BOOL
handle_interrupt( ucontext_t
*sigcontext
, struct stack_layout
*stack
)
2845 switch (ERROR_sig(sigcontext
) >> 3)
2848 stack
->rec
.ExceptionCode
= STATUS_ASSERTION_FAILURE
;
2851 switch (stack
->context
.Rax
)
2853 case 1: /* BREAKPOINT_PRINT */
2854 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2855 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2856 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2857 RIP_sig(sigcontext
) += 3;
2860 stack
->context
.Rip
+= 3;
2861 stack
->rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2862 stack
->rec
.ExceptionAddress
= (void *)stack
->context
.Rip
;
2863 stack
->rec
.NumberParameters
= 1;
2864 stack
->rec
.ExceptionInformation
[0] = stack
->context
.Rax
;
2869 setup_raise_exception( sigcontext
, stack
);
2874 /**********************************************************************
2877 * Handler for SIGSEGV and related errors.
2879 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2881 struct stack_layout
*stack
;
2882 ucontext_t
*ucontext
= sigcontext
;
2884 stack
= (struct stack_layout
*)(RSP_sig(ucontext
) & ~15);
2886 /* check for exceptions on the signal stack caused by write watches */
2887 if (TRAP_sig(ucontext
) == TRAP_x86_PAGEFLT
&& is_inside_signal_stack( stack
) &&
2888 !virtual_handle_fault( siginfo
->si_addr
, (ERROR_sig(ucontext
) >> 1) & 0x09, TRUE
))
2893 /* check for page fault inside the thread stack */
2894 if (TRAP_sig(ucontext
) == TRAP_x86_PAGEFLT
)
2896 switch (virtual_handle_stack_fault( siginfo
->si_addr
))
2898 case 1: /* handled */
2900 case -1: /* overflow */
2901 stack
= setup_exception( sigcontext
);
2902 stack
->rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2907 stack
= setup_exception( sigcontext
);
2908 if (stack
->rec
.ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) goto done
;
2910 switch(TRAP_sig(ucontext
))
2912 case TRAP_x86_OFLOW
: /* Overflow exception */
2913 stack
->rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
2915 case TRAP_x86_BOUND
: /* Bound range exception */
2916 stack
->rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
2918 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
2919 stack
->rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2921 case TRAP_x86_STKFLT
: /* Stack fault */
2922 stack
->rec
.ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2924 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
2925 case TRAP_x86_PROTFLT
: /* General protection fault */
2926 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2928 WORD err
= ERROR_sig(ucontext
);
2929 if (!err
&& (stack
->rec
.ExceptionCode
= is_privileged_instr( &stack
->context
))) break;
2930 if ((err
& 7) == 2 && handle_interrupt( ucontext
, stack
)) return;
2931 stack
->rec
.ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2932 stack
->rec
.NumberParameters
= 2;
2933 stack
->rec
.ExceptionInformation
[0] = 0;
2934 stack
->rec
.ExceptionInformation
[1] = 0xffffffffffffffff;
2937 case TRAP_x86_PAGEFLT
: /* Page fault */
2938 stack
->rec
.NumberParameters
= 2;
2939 stack
->rec
.ExceptionInformation
[0] = (ERROR_sig(ucontext
) >> 1) & 0x09;
2940 stack
->rec
.ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
2941 if (!(stack
->rec
.ExceptionCode
= virtual_handle_fault((void *)stack
->rec
.ExceptionInformation
[1],
2942 stack
->rec
.ExceptionInformation
[0], FALSE
)))
2945 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
2946 stack
->rec
.ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
2949 ERR( "Got unexpected trap %ld\n", (ULONG_PTR
)TRAP_sig(ucontext
) );
2951 case TRAP_x86_NMI
: /* NMI interrupt */
2952 case TRAP_x86_DNA
: /* Device not available exception */
2953 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
2954 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
2955 case TRAP_x86_MCHK
: /* Machine check exception */
2956 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
2957 stack
->rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2961 setup_raise_exception( sigcontext
, stack
);
2964 /**********************************************************************
2967 * Handler for SIGTRAP.
2969 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2971 struct stack_layout
*stack
= setup_exception( sigcontext
);
2973 switch (siginfo
->si_code
)
2975 case TRAP_TRACE
: /* Single-step exception */
2976 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2977 stack
->rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2979 case TRAP_BRKPT
: /* Breakpoint exception */
2983 /* Check if this is actually icebp instruction */
2984 if (((unsigned char *)stack
->rec
.ExceptionAddress
)[-1] == 0xF1)
2986 stack
->rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2989 stack
->rec
.ExceptionAddress
= (char *)stack
->rec
.ExceptionAddress
- 1; /* back up over the int3 instruction */
2992 stack
->rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
2993 stack
->rec
.NumberParameters
= 1;
2994 stack
->rec
.ExceptionInformation
[0] = 0;
2998 setup_raise_exception( sigcontext
, stack
);
3001 /**********************************************************************
3004 * Handler for SIGFPE.
3006 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3008 struct stack_layout
*stack
= setup_exception( sigcontext
);
3010 switch (siginfo
->si_code
)
3013 stack
->rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
3016 stack
->rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
3019 stack
->rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
3022 stack
->rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3025 stack
->rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
3028 stack
->rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
3031 stack
->rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
3035 stack
->rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
3039 setup_raise_exception( sigcontext
, stack
);
3042 /**********************************************************************
3045 * Handler for SIGINT.
3047 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3049 if (!dispatch_signal(SIGINT
))
3051 struct stack_layout
*stack
= setup_exception( sigcontext
);
3052 stack
->rec
.ExceptionCode
= CONTROL_C_EXIT
;
3053 setup_raise_exception( sigcontext
, stack
);
3058 /**********************************************************************
3061 * Handler for SIGABRT.
3063 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
3065 struct stack_layout
*stack
= setup_exception( sigcontext
);
3066 stack
->rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
3067 stack
->rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
3068 setup_raise_exception( sigcontext
, stack
);
3072 /**********************************************************************
3075 * Handler for SIGQUIT.
3077 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
3083 /**********************************************************************
3086 * Handler for SIGUSR1, used to signal a thread that it got suspended.
3088 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
3092 save_context( &context
, ucontext
);
3093 wait_suspend( &context
);
3094 restore_context( &context
, ucontext
);
3098 /***********************************************************************
3099 * __wine_set_signal_handler (NTDLL.@)
3101 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
3103 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
3104 if (handlers
[sig
] != NULL
) return -2;
3105 handlers
[sig
] = wsh
;
3110 /**********************************************************************
3111 * signal_alloc_thread
3113 NTSTATUS
signal_alloc_thread( TEB
**teb
)
3115 static size_t sigstack_alignment
;
3119 if (!sigstack_alignment
)
3121 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
3122 /* find the first power of two not smaller than min_size */
3123 sigstack_alignment
= 12;
3124 while ((1u << sigstack_alignment
) < min_size
) sigstack_alignment
++;
3125 signal_stack_size
= (1 << sigstack_alignment
) - teb_size
;
3126 assert( sizeof(TEB
) <= teb_size
);
3129 size
= 1 << sigstack_alignment
;
3131 if (!(status
= virtual_alloc_aligned( (void **)teb
, 0, &size
, MEM_COMMIT
| MEM_TOP_DOWN
,
3132 PAGE_READWRITE
, sigstack_alignment
)))
3134 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
3135 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
3141 /**********************************************************************
3142 * signal_free_thread
3144 void signal_free_thread( TEB
*teb
)
3148 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
3152 /**********************************************************************
3155 static void *mac_thread_gsbase(void)
3157 struct thread_identifier_info tiinfo
;
3158 unsigned int info_count
= THREAD_IDENTIFIER_INFO_COUNT
;
3159 static int gsbase_offset
= -1;
3162 kern_return_t kr
= thread_info(mach_thread_self(), THREAD_IDENTIFIER_INFO
, (thread_info_t
) &tiinfo
, &info_count
);
3163 if (kr
== KERN_SUCCESS
)
3165 TRACE("pthread_self() %p thread ID %llx gsbase %llx\n", pthread_self(), tiinfo
.thread_id
, tiinfo
.thread_handle
);
3166 return (void*)tiinfo
.thread_handle
;
3169 if (gsbase_offset
< 0)
3171 /* Search for the array of TLS slots within the pthread data structure.
3172 That's what the macOS pthread implementation uses for gsbase. */
3173 const void* const sentinel1
= (const void*)0x2bffb6b4f11228ae;
3174 const void* const sentinel2
= (const void*)0x0845a7ff6ab76707;
3177 const void** p
= (const void**)pthread_self();
3181 if ((rc
= pthread_key_create(&key
, NULL
)))
3183 ERR("failed to create sentinel key for gsbase search: %d\n", rc
);
3187 pthread_setspecific(key
, sentinel1
);
3189 for (i
= key
+ 1; i
< 2000; i
++) /* arbitrary limit */
3191 if (p
[i
] == sentinel1
)
3193 pthread_setspecific(key
, sentinel2
);
3195 if (p
[i
] == sentinel2
)
3197 gsbase_offset
= (i
- key
) * sizeof(*p
);
3201 pthread_setspecific(key
, sentinel1
);
3205 pthread_key_delete(key
);
3210 ret
= (char*)pthread_self() + gsbase_offset
;
3211 TRACE("pthread_self() %p + offset 0x%08x -> gsbase %p\n", pthread_self(), gsbase_offset
, ret
);
3216 ERR("failed to locate gsbase; won't be able to poke ThreadLocalStoragePointer into pthread TLS; expect crashes\n");
3224 /**********************************************************************
3225 * signal_init_thread
3227 void signal_init_thread( TEB
*teb
)
3229 const WORD fpu_cw
= 0x27f;
3232 #if defined __linux__
3233 arch_prctl( ARCH_SET_GS
, teb
);
3234 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
3235 amd64_set_gsbase( teb
);
3236 #elif defined(__NetBSD__)
3237 sysarch( X86_64_SET_GSBASE
, &teb
);
3238 #elif defined (__APPLE__)
3239 __asm__
volatile (".byte 0x65\n\tmovq %0,%c1"
3241 : "r" (teb
->Tib
.Self
), "n" (FIELD_OFFSET(TEB
, Tib
.Self
)));
3242 __asm__
volatile (".byte 0x65\n\tmovq %0,%c1"
3244 : "r" (teb
->ThreadLocalStoragePointer
), "n" (FIELD_OFFSET(TEB
, ThreadLocalStoragePointer
)));
3246 /* alloc_tls_slot() needs to poke a value to an address relative to each
3247 thread's gsbase. Have each thread record its gsbase pointer into its
3248 TEB so alloc_tls_slot() can find it. */
3249 teb
->Reserved5
[0] = mac_thread_gsbase();
3251 # error Please define setting %gs for your architecture
3254 ss
.ss_sp
= (char *)teb
+ teb_size
;
3255 ss
.ss_size
= signal_stack_size
;
3257 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
3260 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
3262 FIXME("FPU setup not implemented for this platform.\n");
3266 /**********************************************************************
3267 * signal_init_process
3269 void signal_init_process(void)
3271 struct sigaction sig_act
;
3273 sig_act
.sa_mask
= server_block_set
;
3274 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
| SA_ONSTACK
;
3276 sig_act
.sa_sigaction
= int_handler
;
3277 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
3278 sig_act
.sa_sigaction
= fpe_handler
;
3279 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
3280 sig_act
.sa_sigaction
= abrt_handler
;
3281 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
3282 sig_act
.sa_sigaction
= quit_handler
;
3283 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
3284 sig_act
.sa_sigaction
= usr1_handler
;
3285 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
3287 sig_act
.sa_sigaction
= segv_handler
;
3288 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
3289 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
3291 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
3295 sig_act
.sa_sigaction
= trap_handler
;
3296 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
3301 perror("sigaction");
3306 static ULONG64
get_int_reg( CONTEXT
*context
, int reg
)
3308 return *(&context
->Rax
+ reg
);
3311 static void set_int_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, ULONG64
*val
)
3313 *(&context
->Rax
+ reg
) = *val
;
3314 if (ctx_ptr
) ctx_ptr
->u2
.IntegerContext
[reg
] = val
;
3317 static void set_float_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, M128A
*val
)
3319 *(&context
->u
.s
.Xmm0
+ reg
) = *val
;
3320 if (ctx_ptr
) ctx_ptr
->u
.FloatingContext
[reg
] = val
;
3323 static int get_opcode_size( struct opcode op
)
3327 case UWOP_ALLOC_LARGE
:
3328 return 2 + (op
.info
!= 0);
3329 case UWOP_SAVE_NONVOL
:
3330 case UWOP_SAVE_XMM128
:
3333 case UWOP_SAVE_NONVOL_FAR
:
3334 case UWOP_SAVE_XMM128_FAR
:
3341 static BOOL
is_inside_epilog( BYTE
*pc
, ULONG64 base
, const RUNTIME_FUNCTION
*function
)
3343 /* add or lea must be the first instruction, and it must have a rex.W prefix */
3344 if ((pc
[0] & 0xf8) == 0x48)
3348 case 0x81: /* add $nnnn,%rsp */
3349 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
3355 case 0x83: /* add $n,%rsp */
3356 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
3362 case 0x8d: /* lea n(reg),%rsp */
3363 if (pc
[0] & 0x06) return FALSE
; /* rex.RX must be cleared */
3364 if (((pc
[2] >> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
3365 if ((pc
[2] & 7) == 4) return FALSE
; /* no SIB byte allowed */
3366 if ((pc
[2] >> 6) == 1) /* 8-bit offset */
3371 if ((pc
[2] >> 6) == 2) /* 32-bit offset */
3380 /* now check for various pop instructions */
3384 if ((*pc
& 0xf0) == 0x40) pc
++; /* rex prefix */
3388 case 0x58: /* pop %rax/%r8 */
3389 case 0x59: /* pop %rcx/%r9 */
3390 case 0x5a: /* pop %rdx/%r10 */
3391 case 0x5b: /* pop %rbx/%r11 */
3392 case 0x5c: /* pop %rsp/%r12 */
3393 case 0x5d: /* pop %rbp/%r13 */
3394 case 0x5e: /* pop %rsi/%r14 */
3395 case 0x5f: /* pop %rdi/%r15 */
3398 case 0xc2: /* ret $nn */
3399 case 0xc3: /* ret */
3401 case 0xe9: /* jmp nnnn */
3402 pc
+= 5 + *(LONG
*)(pc
+ 1);
3403 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
3406 case 0xeb: /* jmp n */
3407 pc
+= 2 + (signed char)pc
[1];
3408 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
3411 case 0xf3: /* rep; ret (for amd64 prediction bug) */
3412 return pc
[1] == 0xc3;
3418 /* execute a function epilog, which must have been validated with is_inside_epilog() */
3419 static void interpret_epilog( BYTE
*pc
, CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
3425 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
3429 case 0x58: /* pop %rax/r8 */
3430 case 0x59: /* pop %rcx/r9 */
3431 case 0x5a: /* pop %rdx/r10 */
3432 case 0x5b: /* pop %rbx/r11 */
3433 case 0x5c: /* pop %rsp/r12 */
3434 case 0x5d: /* pop %rbp/r13 */
3435 case 0x5e: /* pop %rsi/r14 */
3436 case 0x5f: /* pop %rdi/r15 */
3437 set_int_reg( context
, ctx_ptr
, *pc
- 0x58 + (rex
& 1) * 8, (ULONG64
*)context
->Rsp
);
3438 context
->Rsp
+= sizeof(ULONG64
);
3441 case 0x81: /* add $nnnn,%rsp */
3442 context
->Rsp
+= *(LONG
*)(pc
+ 2);
3443 pc
+= 2 + sizeof(LONG
);
3445 case 0x83: /* add $n,%rsp */
3446 context
->Rsp
+= (signed char)pc
[2];
3450 if ((pc
[1] >> 6) == 1) /* lea n(reg),%rsp */
3452 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + (signed char)pc
[2];
3455 else /* lea nnnn(reg),%rsp */
3457 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + *(LONG
*)(pc
+ 2);
3458 pc
+= 2 + sizeof(LONG
);
3461 case 0xc2: /* ret $nn */
3462 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3463 context
->Rsp
+= sizeof(ULONG64
) + *(WORD
*)(pc
+ 1);
3465 case 0xc3: /* ret */
3466 case 0xf3: /* rep; ret */
3467 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3468 context
->Rsp
+= sizeof(ULONG64
);
3470 case 0xe9: /* jmp nnnn */
3471 pc
+= 5 + *(LONG
*)(pc
+ 1);
3473 case 0xeb: /* jmp n */
3474 pc
+= 2 + (signed char)pc
[1];
3481 /**********************************************************************
3482 * RtlVirtualUnwind (NTDLL.@)
3484 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG64 base
, ULONG64 pc
,
3485 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
3486 PVOID
*data
, ULONG64
*frame_ret
,
3487 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
3489 union handler_data
*handler_data
;
3491 struct UNWIND_INFO
*info
;
3492 unsigned int i
, prolog_offset
;
3494 TRACE( "type %x rip %lx rsp %lx\n", type
, pc
, context
->Rsp
);
3495 if (TRACE_ON(seh
)) dump_unwind_info( base
, function
);
3497 frame
= *frame_ret
= context
->Rsp
;
3500 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
3501 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
3503 if (info
->version
!= 1 && info
->version
!= 2)
3505 FIXME( "unknown unwind info version %u at %p\n", info
->version
, info
);
3509 if (info
->frame_reg
)
3510 frame
= get_int_reg( context
, info
->frame_reg
) - info
->frame_offset
* 16;
3512 /* check if in prolog */
3513 if (pc
>= base
+ function
->BeginAddress
&& pc
< base
+ function
->BeginAddress
+ info
->prolog
)
3515 prolog_offset
= pc
- base
- function
->BeginAddress
;
3520 if (is_inside_epilog( (BYTE
*)pc
, base
, function
))
3522 interpret_epilog( (BYTE
*)pc
, context
, ctx_ptr
);
3528 for (i
= 0; i
< info
->count
; i
+= get_opcode_size(info
->opcodes
[i
]))
3530 if (prolog_offset
< info
->opcodes
[i
].offset
) continue; /* skip it */
3532 switch (info
->opcodes
[i
].code
)
3534 case UWOP_PUSH_NONVOL
: /* pushq %reg */
3535 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)context
->Rsp
);
3536 context
->Rsp
+= sizeof(ULONG64
);
3538 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
3539 if (info
->opcodes
[i
].info
) context
->Rsp
+= *(DWORD
*)&info
->opcodes
[i
+1];
3540 else context
->Rsp
+= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
3542 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
3543 context
->Rsp
+= (info
->opcodes
[i
].info
+ 1) * 8;
3545 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
3546 context
->Rsp
= *frame_ret
= frame
;
3548 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
3549 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 8;
3550 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
3552 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
3553 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
3554 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
3556 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
3557 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 16;
3558 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
3560 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
3561 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
3562 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
3564 case UWOP_PUSH_MACHFRAME
:
3565 FIXME( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
3568 if (info
->version
== 2)
3569 break; /* nothing to do */
3571 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
3576 if (!(info
->flags
& UNW_FLAG_CHAININFO
)) break;
3577 function
= &handler_data
->chain
; /* restart with the chained info */
3580 /* now pop return address */
3581 context
->Rip
= *(ULONG64
*)context
->Rsp
;
3582 context
->Rsp
+= sizeof(ULONG64
);
3584 if (!(info
->flags
& type
)) return NULL
; /* no matching handler */
3585 if (prolog_offset
!= ~0) return NULL
; /* inside prolog */
3587 *data
= &handler_data
->handler
+ 1;
3588 return (char *)base
+ handler_data
->handler
;
3591 struct unwind_exception_frame
3593 EXCEPTION_REGISTRATION_RECORD frame
;
3594 DISPATCHER_CONTEXT
*dispatch
;
3597 /**********************************************************************
3598 * unwind_exception_handler
3600 * Handler for exceptions happening while calling an unwind handler.
3602 static DWORD __cdecl
unwind_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
3603 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
3605 struct unwind_exception_frame
*unwind_frame
= (struct unwind_exception_frame
*)frame
;
3606 DISPATCHER_CONTEXT
*dispatch
= (DISPATCHER_CONTEXT
*)dispatcher
;
3608 /* copy the original dispatcher into the current one, except for the TargetIp */
3609 dispatch
->ControlPc
= unwind_frame
->dispatch
->ControlPc
;
3610 dispatch
->ImageBase
= unwind_frame
->dispatch
->ImageBase
;
3611 dispatch
->FunctionEntry
= unwind_frame
->dispatch
->FunctionEntry
;
3612 dispatch
->EstablisherFrame
= unwind_frame
->dispatch
->EstablisherFrame
;
3613 dispatch
->ContextRecord
= unwind_frame
->dispatch
->ContextRecord
;
3614 dispatch
->LanguageHandler
= unwind_frame
->dispatch
->LanguageHandler
;
3615 dispatch
->HandlerData
= unwind_frame
->dispatch
->HandlerData
;
3616 dispatch
->HistoryTable
= unwind_frame
->dispatch
->HistoryTable
;
3617 dispatch
->ScopeIndex
= unwind_frame
->dispatch
->ScopeIndex
;
3618 TRACE( "detected collided unwind\n" );
3619 return ExceptionCollidedUnwind
;
3622 /**********************************************************************
3623 * call_unwind_handler
3625 * Call a single unwind handler.
3627 static DWORD
call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
3629 struct unwind_exception_frame frame
;
3632 frame
.frame
.Handler
= unwind_exception_handler
;
3633 frame
.dispatch
= dispatch
;
3634 __wine_push_frame( &frame
.frame
);
3636 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
3637 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
3638 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
3639 TRACE( "handler %p returned %x\n", dispatch
->LanguageHandler
, res
);
3641 __wine_pop_frame( &frame
.frame
);
3645 case ExceptionContinueSearch
:
3646 case ExceptionCollidedUnwind
:
3649 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
3657 /**********************************************************************
3658 * call_teb_unwind_handler
3660 * Call a single unwind handler from the TEB chain.
3662 static DWORD
call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
3663 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
3667 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
3668 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
3669 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
3670 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
3674 case ExceptionContinueSearch
:
3675 case ExceptionCollidedUnwind
:
3678 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
3686 /**********************************************************************
3687 * call_consolidate_callback
3689 * Wrapper function to call a consolidate callback from a fake frame.
3690 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
3691 * we have to skip all frames which were already processed. To do that we
3692 * trick the unwinding functions into thinking the call came from somewhere
3693 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
3694 * DW_CFA_expression, and the expressions have the following format:
3696 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
3697 * DW_OP_deref | Get *(%rbp + 0x10) == context
3698 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
3699 * [DW_OP_deref] | Dereference, only for CFA
3701 extern void * WINAPI
call_consolidate_callback( CONTEXT
*context
,
3702 void *(CALLBACK
*callback
)(EXCEPTION_RECORD
*),
3703 EXCEPTION_RECORD
*rec
);
3704 __ASM_GLOBAL_FUNC( call_consolidate_callback
,
3706 __ASM_SEH(".seh_pushreg %rbp\n\t")
3707 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
3708 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
3709 "movq %rsp,%rbp\n\t"
3710 __ASM_SEH(".seh_setframe %rbp,0\n\t")
3711 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
3712 "subq $0x20,%rsp\n\t"
3713 __ASM_SEH(".seh_stackalloc 0x20\n\t")
3714 __ASM_SEH(".seh_endprologue\n\t")
3715 "movq %rcx,0x10(%rbp)\n\t"
3716 __ASM_CFI(".cfi_remember_state\n\t")
3717 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
3718 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
3719 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
3720 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
3721 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
3722 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
3723 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
3724 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
3725 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
3726 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
3727 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
3728 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
3729 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
3730 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
3731 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
3732 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
3733 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
3734 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
3735 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
3736 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
3739 __ASM_CFI(".cfi_restore_state\n\t")
3740 "leaq 0(%rbp),%rsp\n\t"
3741 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
3743 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
3744 __ASM_CFI(".cfi_same_value %rbp\n\t")
3747 /*******************************************************************
3748 * RtlRestoreContext (NTDLL.@)
3750 void CDECL
RtlRestoreContext( CONTEXT
*context
, EXCEPTION_RECORD
*rec
)
3752 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
3754 if (rec
&& rec
->ExceptionCode
== STATUS_LONGJUMP
&& rec
->NumberParameters
>= 1)
3756 struct MSVCRT_JUMP_BUFFER
*jmp
= (struct MSVCRT_JUMP_BUFFER
*)rec
->ExceptionInformation
[0];
3757 context
->Rbx
= jmp
->Rbx
;
3758 context
->Rsp
= jmp
->Rsp
;
3759 context
->Rbp
= jmp
->Rbp
;
3760 context
->Rsi
= jmp
->Rsi
;
3761 context
->Rdi
= jmp
->Rdi
;
3762 context
->R12
= jmp
->R12
;
3763 context
->R13
= jmp
->R13
;
3764 context
->R14
= jmp
->R14
;
3765 context
->R15
= jmp
->R15
;
3766 context
->Rip
= jmp
->Rip
;
3767 context
->u
.s
.Xmm6
= jmp
->Xmm6
;
3768 context
->u
.s
.Xmm7
= jmp
->Xmm7
;
3769 context
->u
.s
.Xmm8
= jmp
->Xmm8
;
3770 context
->u
.s
.Xmm9
= jmp
->Xmm9
;
3771 context
->u
.s
.Xmm10
= jmp
->Xmm10
;
3772 context
->u
.s
.Xmm11
= jmp
->Xmm11
;
3773 context
->u
.s
.Xmm12
= jmp
->Xmm12
;
3774 context
->u
.s
.Xmm13
= jmp
->Xmm13
;
3775 context
->u
.s
.Xmm14
= jmp
->Xmm14
;
3776 context
->u
.s
.Xmm15
= jmp
->Xmm15
;
3778 else if (rec
&& rec
->ExceptionCode
== STATUS_UNWIND_CONSOLIDATE
&& rec
->NumberParameters
>= 1)
3780 PVOID (CALLBACK
*consolidate
)(EXCEPTION_RECORD
*) = (void *)rec
->ExceptionInformation
[0];
3781 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate
, rec
);
3782 context
->Rip
= (ULONG64
)call_consolidate_callback( context
, consolidate
, rec
);
3785 /* hack: remove no longer accessible TEB frames */
3786 while ((ULONG64
)teb_frame
< context
->Rsp
)
3788 TRACE( "removing TEB frame: %p\n", teb_frame
);
3789 teb_frame
= __wine_pop_frame( teb_frame
);
3792 TRACE( "returning to %lx stack %lx\n", context
->Rip
, context
->Rsp
);
3793 set_cpu_context( context
);
3797 /*******************************************************************
3798 * RtlUnwindEx (NTDLL.@)
3800 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
3801 PVOID retval
, CONTEXT
*context
, UNWIND_HISTORY_TABLE
*table
)
3803 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
3804 EXCEPTION_RECORD record
;
3805 DISPATCHER_CONTEXT dispatch
;
3806 CONTEXT new_context
;
3810 RtlCaptureContext( context
);
3811 new_context
= *context
;
3813 /* build an exception record, if we do not have one */
3816 record
.ExceptionCode
= STATUS_UNWIND
;
3817 record
.ExceptionFlags
= 0;
3818 record
.ExceptionRecord
= NULL
;
3819 record
.ExceptionAddress
= (void *)context
->Rip
;
3820 record
.NumberParameters
= 0;
3824 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
3826 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
3827 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, context
->Rip
);
3828 for (i
= 0; i
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); i
++)
3829 TRACE( " info[%d]=%016lx\n", i
, rec
->ExceptionInformation
[i
] );
3830 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
3831 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
3832 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
3833 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
3834 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
3835 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
3836 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
3837 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
3839 dispatch
.EstablisherFrame
= context
->Rsp
;
3840 dispatch
.TargetIp
= (ULONG64
)target_ip
;
3841 dispatch
.ContextRecord
= context
;
3842 dispatch
.HistoryTable
= table
;
3846 status
= virtual_unwind( UNW_FLAG_UHANDLER
, &dispatch
, &new_context
);
3847 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
3850 if (!dispatch
.EstablisherFrame
) break;
3852 if (is_inside_signal_stack( (void *)dispatch
.EstablisherFrame
))
3854 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch
.EstablisherFrame
,
3855 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size
);
3856 *context
= new_context
;
3860 if ((dispatch
.EstablisherFrame
& 7) ||
3861 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
3862 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
3864 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
3865 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
3866 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
3870 if (dispatch
.LanguageHandler
)
3872 if (end_frame
&& (dispatch
.EstablisherFrame
> (ULONG64
)end_frame
))
3874 ERR( "invalid end frame %lx/%p\n", dispatch
.EstablisherFrame
, end_frame
);
3875 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
3877 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) rec
->ExceptionFlags
|= EH_TARGET_UNWIND
;
3878 if (call_unwind_handler( rec
, &dispatch
) == ExceptionCollidedUnwind
)
3882 *context
= new_context
= *dispatch
.ContextRecord
;
3883 dispatch
.ContextRecord
= context
;
3884 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
3885 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
3886 &new_context
, NULL
, &frame
, NULL
);
3887 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
3890 rec
->ExceptionFlags
&= ~EH_COLLIDED_UNWIND
;
3892 else /* hack: call builtin handlers registered in the tib list */
3894 DWORD64 backup_frame
= dispatch
.EstablisherFrame
;
3895 while ((ULONG64
)teb_frame
< new_context
.Rsp
&& (ULONG64
)teb_frame
< (ULONG64
)end_frame
)
3897 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
3898 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
3899 if (call_teb_unwind_handler( rec
, &dispatch
, teb_frame
) == ExceptionCollidedUnwind
)
3903 teb_frame
= __wine_pop_frame( teb_frame
);
3905 *context
= new_context
= *dispatch
.ContextRecord
;
3906 dispatch
.ContextRecord
= context
;
3907 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
3908 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
3909 &new_context
, NULL
, &frame
, NULL
);
3910 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
3913 teb_frame
= __wine_pop_frame( teb_frame
);
3915 if ((ULONG64
)teb_frame
== (ULONG64
)end_frame
&& (ULONG64
)end_frame
< new_context
.Rsp
) break;
3916 dispatch
.EstablisherFrame
= backup_frame
;
3919 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) break;
3920 *context
= new_context
;
3923 context
->Rax
= (ULONG64
)retval
;
3924 context
->Rip
= (ULONG64
)target_ip
;
3925 RtlRestoreContext(context
, rec
);
3929 /*******************************************************************
3930 * RtlUnwind (NTDLL.@)
3932 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
3935 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
3939 /*******************************************************************
3940 * _local_unwind (NTDLL.@)
3942 void WINAPI
_local_unwind( void *frame
, void *target_ip
)
3945 RtlUnwindEx( frame
, target_ip
, NULL
, NULL
, &context
, NULL
);
3948 /*******************************************************************
3949 * __C_specific_handler (NTDLL.@)
3951 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
3954 struct _DISPATCHER_CONTEXT
*dispatch
)
3956 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
3959 TRACE( "%p %p %p %p\n", rec
, frame
, context
, dispatch
);
3960 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
3962 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
3964 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
3966 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
3967 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
3969 PTERMINATION_HANDLER handler
;
3971 if (table
->ScopeRecord
[i
].JumpTarget
) continue;
3973 if (rec
->ExceptionFlags
& EH_TARGET_UNWIND
&&
3974 dispatch
->TargetIp
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
3975 dispatch
->TargetIp
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
3980 handler
= (PTERMINATION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
3981 dispatch
->ScopeIndex
= i
+1;
3983 TRACE( "calling __finally %p frame %p\n", handler
, frame
);
3984 handler( TRUE
, frame
);
3987 return ExceptionContinueSearch
;
3990 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
3992 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
3993 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
3995 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
3996 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
3998 EXCEPTION_POINTERS ptrs
;
3999 PEXCEPTION_FILTER filter
;
4001 filter
= (PEXCEPTION_FILTER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
4002 ptrs
.ExceptionRecord
= rec
;
4003 ptrs
.ContextRecord
= context
;
4004 TRACE( "calling filter %p ptrs %p frame %p\n", filter
, &ptrs
, frame
);
4005 switch (filter( &ptrs
, frame
))
4007 case EXCEPTION_EXECUTE_HANDLER
:
4009 case EXCEPTION_CONTINUE_SEARCH
:
4011 case EXCEPTION_CONTINUE_EXECUTION
:
4012 return ExceptionContinueExecution
;
4015 TRACE( "unwinding to target %lx\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
4016 RtlUnwindEx( frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
4017 rec
, 0, dispatch
->ContextRecord
, dispatch
->HistoryTable
);
4020 return ExceptionContinueSearch
;
4024 /***********************************************************************
4025 * RtlRaiseException (NTDLL.@)
4027 __ASM_GLOBAL_FUNC( RtlRaiseException
,
4028 "sub $0x4f8,%rsp\n\t"
4029 __ASM_SEH(".seh_stackalloc 0x4f8\n\t")
4030 __ASM_SEH(".seh_endprologue\n\t")
4031 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
4032 "movq %rcx,0x500(%rsp)\n\t"
4033 "leaq 0x20(%rsp),%rcx\n\t"
4034 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
4035 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
4036 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
4037 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
4038 "movq (%rax),%rcx\n\t" /* original first parameter */
4039 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
4040 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
4041 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
4042 "movq %rax,0x10(%rcx)\n\t" /* rec->ExceptionAddress */
4044 "call " __ASM_NAME("NtRaiseException") "\n\t"
4045 "movq %rax,%rcx\n\t"
4046 "call " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
4049 static inline ULONG
hash_pointers( void **ptrs
, ULONG count
)
4051 /* Based on MurmurHash2, which is in the public domain */
4052 static const ULONG m
= 0x5bd1e995;
4053 static const ULONG r
= 24;
4054 ULONG hash
= count
* sizeof(void*);
4055 for (; count
> 0; ptrs
++, count
--)
4057 ULONG_PTR data
= (ULONG_PTR
)*ptrs
;
4058 ULONG k1
= (ULONG
)(data
& 0xffffffff), k2
= (ULONG
)(data
>> 32);
4060 k1
= (k1
^ (k1
>> r
)) * m
;
4062 k2
= (k2
^ (k2
>> r
)) * m
;
4063 hash
= (((hash
* m
) ^ k1
) * m
) ^ k2
;
4065 hash
= (hash
^ (hash
>> 13)) * m
;
4066 return hash
^ (hash
>> 15);
4070 /*************************************************************************
4071 * RtlCaptureStackBackTrace (NTDLL.@)
4073 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
4075 UNWIND_HISTORY_TABLE table
;
4076 DISPATCHER_CONTEXT dispatch
;
4080 USHORT num_entries
= 0;
4082 TRACE( "(%u, %u, %p, %p)\n", skip
, count
, buffer
, hash
);
4084 RtlCaptureContext( &context
);
4085 dispatch
.TargetIp
= 0;
4086 dispatch
.ContextRecord
= &context
;
4087 dispatch
.HistoryTable
= &table
;
4088 if (hash
) *hash
= 0;
4089 for (i
= 0; i
< skip
+ count
; i
++)
4091 status
= virtual_unwind( UNW_FLAG_NHANDLER
, &dispatch
, &context
);
4092 if (status
!= STATUS_SUCCESS
) return i
;
4094 if (!dispatch
.EstablisherFrame
) break;
4096 if ((dispatch
.EstablisherFrame
& 7) ||
4097 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
4098 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
4100 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
4101 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
4105 if (context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
4107 if (i
>= skip
) buffer
[num_entries
++] = (void *)context
.Rip
;
4109 if (hash
&& num_entries
> 0) *hash
= hash_pointers( buffer
, num_entries
);
4110 TRACE( "captured %hu frames\n", num_entries
);
4115 /***********************************************************************
4118 static void WINAPI
call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
)
4122 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
4123 RtlExitUserThread( entry( arg
));
4125 __EXCEPT(call_unhandled_exception_filter
)
4127 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
4130 abort(); /* should not be reached */
4134 extern void DECLSPEC_NORETURN
start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
,
4136 __ASM_GLOBAL_FUNC( start_thread
,
4138 __ASM_SEH(".seh_stackalloc 56\n\t")
4139 __ASM_SEH(".seh_endprologue\n\t")
4140 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
4141 "movq %rbp,48(%rsp)\n\t"
4142 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
4143 "movq %rbx,40(%rsp)\n\t"
4144 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
4145 "movq %r12,32(%rsp)\n\t"
4146 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
4147 "movq %r13,24(%rsp)\n\t"
4148 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
4149 "movq %r14,16(%rsp)\n\t"
4150 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
4151 "movq %r15,8(%rsp)\n\t"
4152 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
4153 /* store exit frame */
4154 "movq %gs:0x30,%rax\n\t"
4155 "movq %rsp,0x330(%rax)\n\t" /* amd64_thread_data()->exit_frame */
4156 /* switch to thread stack */
4157 "movq 8(%rax),%rax\n\t" /* NtCurrentTeb()->Tib.StackBase */
4158 "leaq -0x1000(%rax),%rsp\n\t"
4160 "call " __ASM_NAME("attach_thread") "\n\t"
4161 "movq %rax,%rsp\n\t"
4162 /* clear the stack */
4163 "andq $~0xfff,%rax\n\t" /* round down to page size */
4164 "movq %rax,%rdi\n\t"
4165 "call " __ASM_NAME("virtual_clear_thread_stack") "\n\t"
4166 /* switch to the initial context */
4167 "movq %rsp,%rdi\n\t"
4168 "call " __ASM_NAME("set_cpu_context") )
4170 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int) );
4171 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
4172 /* fetch exit frame */
4173 "movq %gs:0x30,%rax\n\t"
4174 "movq 0x330(%rax),%rdx\n\t" /* amd64_thread_data()->exit_frame */
4175 "testq %rdx,%rdx\n\t"
4178 /* switch to exit frame stack */
4179 "1:\tmovq $0,0x330(%rax)\n\t"
4180 "movq %rdx,%rsp\n\t"
4181 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
4182 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
4183 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
4184 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
4185 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
4186 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
4187 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
4191 /***********************************************************************
4192 * init_thread_context
4194 static void init_thread_context( CONTEXT
*context
, LPTHREAD_START_ROUTINE entry
, void *arg
, void *relay
)
4196 __asm__( "movw %%cs,%0" : "=m" (context
->SegCs
) );
4197 __asm__( "movw %%ss,%0" : "=m" (context
->SegSs
) );
4198 context
->Rcx
= (ULONG_PTR
)entry
;
4199 context
->Rdx
= (ULONG_PTR
)arg
;
4200 context
->Rsp
= (ULONG_PTR
)NtCurrentTeb()->Tib
.StackBase
- 0x28;
4201 context
->Rip
= (ULONG_PTR
)relay
;
4202 context
->EFlags
= 0x200;
4203 context
->u
.FltSave
.ControlWord
= 0x27f;
4204 context
->u
.FltSave
.MxCsr
= context
->MxCsr
= 0x1f80;
4208 /***********************************************************************
4211 PCONTEXT DECLSPEC_HIDDEN
attach_thread( LPTHREAD_START_ROUTINE entry
, void *arg
,
4212 BOOL suspend
, void *relay
)
4218 CONTEXT context
= { 0 };
4220 context
.ContextFlags
= CONTEXT_ALL
;
4221 init_thread_context( &context
, entry
, arg
, relay
);
4222 wait_suspend( &context
);
4223 ctx
= (CONTEXT
*)((ULONG_PTR
)context
.Rsp
& ~15) - 1;
4228 ctx
= (CONTEXT
*)((char *)NtCurrentTeb()->Tib
.StackBase
- 0x30) - 1;
4229 init_thread_context( ctx
, entry
, arg
, relay
);
4231 ctx
->ContextFlags
= CONTEXT_FULL
;
4232 LdrInitializeThunk( ctx
, (void **)&ctx
->Rcx
, 0, 0 );
4237 /***********************************************************************
4238 * signal_start_thread
4240 * Thread startup sequence:
4241 * signal_start_thread()
4243 * -> call_thread_func()
4245 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
4247 start_thread( entry
, arg
, suspend
, call_thread_func
);
4251 /**********************************************************************
4252 * signal_start_process
4254 * Process startup sequence:
4255 * signal_start_process()
4257 * -> kernel32_start_process()
4259 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
4261 start_thread( entry
, NtCurrentTeb()->Peb
, suspend
, kernel32_start_process
);
4265 /***********************************************************************
4266 * signal_exit_thread
4268 void signal_exit_thread( int status
)
4270 call_thread_exit_func( status
, exit_thread
);
4273 /***********************************************************************
4274 * signal_exit_process
4276 void signal_exit_process( int status
)
4278 call_thread_exit_func( status
, exit
);
4281 /**********************************************************************
4282 * DbgBreakPoint (NTDLL.@)
4284 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
4286 /**********************************************************************
4287 * DbgUserBreakPoint (NTDLL.@)
4289 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
4291 #endif /* __x86_64__ */