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"
32 #ifdef HAVE_UCONTEXT_H
33 # include <ucontext.h>
40 #ifdef HAVE_MACHINE_SYSARCH_H
41 # include <machine/sysarch.h>
44 #ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
47 #ifdef HAVE_SYS_SIGNAL_H
48 # include <sys/signal.h>
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
54 #define WIN32_NO_STATUS
57 #include "wine/library.h"
58 #include "wine/exception.h"
59 #include "ntdll_misc.h"
60 #include "wine/debug.h"
62 #ifdef HAVE_VALGRIND_MEMCHECK_H
63 #include <valgrind/memcheck.h>
66 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
68 struct _DISPATCHER_CONTEXT
;
70 typedef LONG (WINAPI
*PC_LANGUAGE_EXCEPTION_HANDLER
)( EXCEPTION_POINTERS
*ptrs
, ULONG64 frame
);
71 typedef EXCEPTION_DISPOSITION (WINAPI
*PEXCEPTION_ROUTINE
)( EXCEPTION_RECORD
*rec
,
74 struct _DISPATCHER_CONTEXT
*dispatch
);
76 typedef struct _DISPATCHER_CONTEXT
80 PRUNTIME_FUNCTION FunctionEntry
;
81 ULONG64 EstablisherFrame
;
83 PCONTEXT ContextRecord
;
84 PEXCEPTION_ROUTINE LanguageHandler
;
86 PUNWIND_HISTORY_TABLE HistoryTable
;
88 } DISPATCHER_CONTEXT
, *PDISPATCHER_CONTEXT
;
90 typedef struct _SCOPE_TABLE
100 } SCOPE_TABLE
, *PSCOPE_TABLE
;
103 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
104 struct MSVCRT_JUMP_BUFFER
130 /***********************************************************************
131 * signal context platform-specific definitions
135 #include <asm/prctl.h>
136 extern int arch_prctl(int func
, void *ptr
);
138 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
139 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
140 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
141 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
142 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
143 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
144 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
145 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
146 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
147 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
148 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
149 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
150 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
151 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
152 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
154 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
155 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
156 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
158 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
159 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
160 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
161 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
162 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
164 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
166 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
167 #include <sys/ucontext.h>
169 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
170 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
171 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
172 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
173 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
174 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
175 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
176 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
177 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
178 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
179 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
180 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
181 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
182 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
183 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
185 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
186 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
187 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
188 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
189 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
190 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
192 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
194 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
195 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
196 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
197 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
199 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
201 #elif defined(__NetBSD__)
202 #include <sys/ucontext.h>
203 #include <sys/types.h>
206 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
207 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
208 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
209 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
210 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
211 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
212 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
213 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
214 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
215 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
216 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
217 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
218 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
219 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
220 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
222 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
223 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
224 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
225 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
226 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
227 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
229 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
231 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
232 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
234 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
235 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
237 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
239 #error You must define the signal context functions for your platform
244 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
245 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
246 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
247 TRAP_x86_NMI
= 2, /* NMI interrupt */
248 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
249 TRAP_x86_OFLOW
= 4, /* Overflow exception */
250 TRAP_x86_BOUND
= 5, /* Bound range exception */
251 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
252 TRAP_x86_DNA
= 7, /* Device not available exception */
253 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
254 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
255 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
256 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
257 TRAP_x86_STKFLT
= 12, /* Stack fault */
258 TRAP_x86_PROTFLT
= 13, /* General protection fault */
259 TRAP_x86_PAGEFLT
= 14, /* Page fault */
260 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
261 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
262 TRAP_x86_MCHK
= 18, /* Machine check exception */
263 TRAP_x86_CACHEFLT
= 19 /* Cache flush exception */
266 static const size_t teb_size
= 0x2000; /* we reserve two pages for the TEB */
267 static size_t signal_stack_size
;
269 typedef void (*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
270 typedef int (*wine_signal_handler
)(unsigned int sig
);
272 static wine_signal_handler handlers
[256];
275 /***********************************************************************
276 * Definitions for Win32 unwind tables
281 RUNTIME_FUNCTION chain
;
299 BYTE frame_offset
: 4;
300 struct opcode opcodes
[1]; /* info->count entries */
301 /* followed by handler_data */
304 #define UWOP_PUSH_NONVOL 0
305 #define UWOP_ALLOC_LARGE 1
306 #define UWOP_ALLOC_SMALL 2
307 #define UWOP_SET_FPREG 3
308 #define UWOP_SAVE_NONVOL 4
309 #define UWOP_SAVE_NONVOL_FAR 5
310 #define UWOP_SAVE_XMM128 8
311 #define UWOP_SAVE_XMM128_FAR 9
312 #define UWOP_PUSH_MACHFRAME 10
314 static void dump_unwind_info( ULONG64 base
, RUNTIME_FUNCTION
*function
)
316 static const char * const reg_names
[16] =
317 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
318 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
320 union handler_data
*handler_data
;
321 struct UNWIND_INFO
*info
;
322 unsigned int i
, count
;
324 TRACE( "**** func %x-%x\n", function
->BeginAddress
, function
->EndAddress
);
327 if (function
->UnwindData
& 1)
329 RUNTIME_FUNCTION
*next
= (RUNTIME_FUNCTION
*)((char *)base
+ (function
->UnwindData
& ~1));
330 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
331 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
,
332 (char *)base
+ next
->BeginAddress
, (char *)base
+ next
->EndAddress
);
336 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
338 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
339 info
, info
->flags
, info
->prolog
,
340 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
);
343 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
344 reg_names
[info
->frame_reg
], info
->frame_offset
* 16 );
346 for (i
= 0; i
< info
->count
; i
++)
348 TRACE( " 0x%x: ", info
->opcodes
[i
].offset
);
349 switch (info
->opcodes
[i
].code
)
351 case UWOP_PUSH_NONVOL
:
352 TRACE( "pushq %%%s\n", reg_names
[info
->opcodes
[i
].info
] );
354 case UWOP_ALLOC_LARGE
:
355 if (info
->opcodes
[i
].info
)
357 count
= *(DWORD
*)&info
->opcodes
[i
+1];
362 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
365 TRACE( "subq $0x%x,%%rsp\n", count
);
367 case UWOP_ALLOC_SMALL
:
368 count
= (info
->opcodes
[i
].info
+ 1) * 8;
369 TRACE( "subq $0x%x,%%rsp\n", count
);
372 TRACE( "leaq 0x%x(%%rsp),%s\n",
373 info
->frame_offset
* 16, reg_names
[info
->frame_reg
] );
375 case UWOP_SAVE_NONVOL
:
376 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
377 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
380 case UWOP_SAVE_NONVOL_FAR
:
381 count
= *(DWORD
*)&info
->opcodes
[i
+1];
382 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
385 case UWOP_SAVE_XMM128
:
386 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 16;
387 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
390 case UWOP_SAVE_XMM128_FAR
:
391 count
= *(DWORD
*)&info
->opcodes
[i
+1];
392 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
395 case UWOP_PUSH_MACHFRAME
:
396 TRACE( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
399 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
404 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
405 if (info
->flags
& UNW_FLAG_CHAININFO
)
407 TRACE( " chained to function %p-%p\n",
408 (char *)base
+ handler_data
->chain
.BeginAddress
,
409 (char *)base
+ handler_data
->chain
.EndAddress
);
410 function
= &handler_data
->chain
;
413 if (info
->flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
414 TRACE( " handler %p data at %p\n",
415 (char *)base
+ handler_data
->handler
, &handler_data
->handler
+ 1 );
420 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
424 TRACE( "scope table at %p\n", table
);
425 for (i
= 0; i
< table
->Count
; i
++)
426 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i
,
427 base
+ table
->ScopeRecord
[i
].BeginAddress
,
428 base
+ table
->ScopeRecord
[i
].EndAddress
,
429 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
430 base
+ table
->ScopeRecord
[i
].JumpTarget
);
434 /***********************************************************************
435 * Definitions for Dwarf unwind tables
438 enum dwarf_call_frame_info
440 DW_CFA_advance_loc
= 0x40,
441 DW_CFA_offset
= 0x80,
442 DW_CFA_restore
= 0xc0,
444 DW_CFA_set_loc
= 0x01,
445 DW_CFA_advance_loc1
= 0x02,
446 DW_CFA_advance_loc2
= 0x03,
447 DW_CFA_advance_loc4
= 0x04,
448 DW_CFA_offset_extended
= 0x05,
449 DW_CFA_restore_extended
= 0x06,
450 DW_CFA_undefined
= 0x07,
451 DW_CFA_same_value
= 0x08,
452 DW_CFA_register
= 0x09,
453 DW_CFA_remember_state
= 0x0a,
454 DW_CFA_restore_state
= 0x0b,
455 DW_CFA_def_cfa
= 0x0c,
456 DW_CFA_def_cfa_register
= 0x0d,
457 DW_CFA_def_cfa_offset
= 0x0e,
458 DW_CFA_def_cfa_expression
= 0x0f,
459 DW_CFA_expression
= 0x10,
460 DW_CFA_offset_extended_sf
= 0x11,
461 DW_CFA_def_cfa_sf
= 0x12,
462 DW_CFA_def_cfa_offset_sf
= 0x13,
463 DW_CFA_val_offset
= 0x14,
464 DW_CFA_val_offset_sf
= 0x15,
465 DW_CFA_val_expression
= 0x16,
472 DW_OP_const1u
= 0x08,
473 DW_OP_const1s
= 0x09,
474 DW_OP_const2u
= 0x0a,
475 DW_OP_const2s
= 0x0b,
476 DW_OP_const4u
= 0x0c,
477 DW_OP_const4s
= 0x0d,
478 DW_OP_const8u
= 0x0e,
479 DW_OP_const8s
= 0x0f,
499 DW_OP_plus_uconst
= 0x23,
612 DW_OP_deref_size
= 0x94,
613 DW_OP_xderef_size
= 0x95,
615 DW_OP_push_object_address
= 0x97,
618 DW_OP_call_ref
= 0x9a,
619 DW_OP_form_tls_address
= 0x9b,
620 DW_OP_call_frame_cfa
= 0x9c,
621 DW_OP_bit_piece
= 0x9d,
622 DW_OP_lo_user
= 0xe0,
623 DW_OP_hi_user
= 0xff,
624 DW_OP_GNU_push_tls_address
= 0xe0,
625 DW_OP_GNU_uninit
= 0xf0,
626 DW_OP_GNU_encoded_addr
= 0xf1,
629 #define DW_EH_PE_native 0x00
630 #define DW_EH_PE_leb128 0x01
631 #define DW_EH_PE_data2 0x02
632 #define DW_EH_PE_data4 0x03
633 #define DW_EH_PE_data8 0x04
634 #define DW_EH_PE_signed 0x08
635 #define DW_EH_PE_abs 0x00
636 #define DW_EH_PE_pcrel 0x10
637 #define DW_EH_PE_textrel 0x20
638 #define DW_EH_PE_datarel 0x30
639 #define DW_EH_PE_funcrel 0x40
640 #define DW_EH_PE_aligned 0x50
641 #define DW_EH_PE_indirect 0x80
642 #define DW_EH_PE_omit 0xff
644 struct dwarf_eh_bases
655 unsigned char version
;
656 unsigned char augmentation
[1];
662 unsigned int cie_offset
;
665 extern const struct dwarf_fde
*_Unwind_Find_FDE (void *, struct dwarf_eh_bases
*);
667 static unsigned char dwarf_get_u1( const unsigned char **p
)
672 static unsigned short dwarf_get_u2( const unsigned char **p
)
674 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8);
679 static unsigned int dwarf_get_u4( const unsigned char **p
)
681 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8) | ((*p
)[2] << 16) | ((*p
)[3] << 24);
686 static ULONG64
dwarf_get_u8( const unsigned char **p
)
688 ULONG64 low
= dwarf_get_u4( p
);
689 ULONG64 high
= dwarf_get_u4( p
);
690 return low
| (high
<< 32);
693 static ULONG_PTR
dwarf_get_uleb128( const unsigned char **p
)
696 unsigned int shift
= 0;
702 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
705 } while (byte
& 0x80);
709 static LONG_PTR
dwarf_get_sleb128( const unsigned char **p
)
712 unsigned int shift
= 0;
718 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
721 } while (byte
& 0x80);
723 if ((shift
< 8 * sizeof(ret
)) && (byte
& 0x40)) ret
|= -((ULONG_PTR
)1 << shift
);
727 static ULONG_PTR
dwarf_get_ptr( const unsigned char **p
, unsigned char encoding
)
731 if (encoding
== DW_EH_PE_omit
) return 0;
733 switch (encoding
& 0xf0)
739 base
= (ULONG_PTR
)*p
;
742 FIXME( "unsupported encoding %02x\n", encoding
);
746 switch (encoding
& 0x0f)
748 case DW_EH_PE_native
:
749 return base
+ dwarf_get_u8( p
);
750 case DW_EH_PE_leb128
:
751 return base
+ dwarf_get_uleb128( p
);
753 return base
+ dwarf_get_u2( p
);
755 return base
+ dwarf_get_u4( p
);
757 return base
+ dwarf_get_u8( p
);
758 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
759 return base
+ dwarf_get_sleb128( p
);
760 case DW_EH_PE_signed
|DW_EH_PE_data2
:
761 return base
+ (signed short)dwarf_get_u2( p
);
762 case DW_EH_PE_signed
|DW_EH_PE_data4
:
763 return base
+ (signed int)dwarf_get_u4( p
);
764 case DW_EH_PE_signed
|DW_EH_PE_data8
:
765 return base
+ (LONG64
)dwarf_get_u8( p
);
767 FIXME( "unsupported encoding %02x\n", encoding
);
774 RULE_UNSET
, /* not set at all */
775 RULE_UNDEFINED
, /* undefined value */
776 RULE_SAME
, /* same value as previous frame */
777 RULE_CFA_OFFSET
, /* stored at cfa offset */
778 RULE_OTHER_REG
, /* stored in other register */
779 RULE_EXPRESSION
, /* address specified by expression */
780 RULE_VAL_EXPRESSION
/* value specified by expression */
783 #define NB_FRAME_REGS 41
784 #define MAX_SAVED_STATES 16
788 ULONG_PTR cfa_offset
;
789 unsigned char cfa_reg
;
790 enum reg_rule cfa_rule
;
791 enum reg_rule rules
[NB_FRAME_REGS
];
792 ULONG64 regs
[NB_FRAME_REGS
];
798 ULONG_PTR code_align
;
800 unsigned char retaddr_reg
;
801 unsigned char fde_encoding
;
802 unsigned char signal_frame
;
803 unsigned char state_sp
;
804 struct frame_state state
;
805 struct frame_state
*state_stack
;
808 static const char *dwarf_reg_names
[NB_FRAME_REGS
] =
810 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
811 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
812 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
813 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
814 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
817 static int valid_reg( ULONG_PTR reg
)
819 if (reg
>= NB_FRAME_REGS
) FIXME( "unsupported reg %lx\n", reg
);
820 return (reg
< NB_FRAME_REGS
);
823 static void execute_cfa_instructions( const unsigned char *ptr
, const unsigned char *end
,
824 ULONG_PTR last_ip
, struct frame_info
*info
)
826 while (ptr
< end
&& info
->ip
< last_ip
+ info
->signal_frame
)
828 enum dwarf_call_frame_info op
= *ptr
++;
834 case DW_CFA_advance_loc
:
836 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
837 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
843 ULONG_PTR reg
= op
& 0x3f;
844 LONG_PTR offset
= dwarf_get_uleb128( &ptr
) * info
->data_align
;
845 if (!valid_reg( reg
)) break;
846 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
847 info
->state
.regs
[reg
] = offset
;
848 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
853 ULONG_PTR reg
= op
& 0x3f;
854 if (!valid_reg( reg
)) break;
855 TRACE( "%lx: DW_CFA_restore %s\n", info
->ip
, dwarf_reg_names
[reg
] );
856 info
->state
.rules
[reg
] = RULE_UNSET
;
867 ULONG_PTR loc
= dwarf_get_ptr( &ptr
, info
->fde_encoding
);
868 TRACE( "%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
872 case DW_CFA_advance_loc1
:
874 ULONG_PTR offset
= *ptr
++ * info
->code_align
;
875 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
879 case DW_CFA_advance_loc2
:
881 ULONG_PTR offset
= dwarf_get_u2( &ptr
) * info
->code_align
;
882 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
886 case DW_CFA_advance_loc4
:
888 ULONG_PTR offset
= dwarf_get_u4( &ptr
) * info
->code_align
;
889 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
893 case DW_CFA_offset_extended
:
894 case DW_CFA_offset_extended_sf
:
896 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
897 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf_get_uleb128( &ptr
) * info
->data_align
898 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
899 if (!valid_reg( reg
)) break;
900 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
901 info
->state
.regs
[reg
] = offset
;
902 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
905 case DW_CFA_restore_extended
:
907 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
908 if (!valid_reg( reg
)) break;
909 TRACE( "%lx: DW_CFA_restore_extended %s\n", info
->ip
, dwarf_reg_names
[reg
] );
910 info
->state
.rules
[reg
] = RULE_UNSET
;
913 case DW_CFA_undefined
:
915 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
916 if (!valid_reg( reg
)) break;
917 TRACE( "%lx: DW_CFA_undefined %s\n", info
->ip
, dwarf_reg_names
[reg
] );
918 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
921 case DW_CFA_same_value
:
923 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
924 if (!valid_reg( reg
)) break;
925 TRACE( "%lx: DW_CFA_same_value %s\n", info
->ip
, dwarf_reg_names
[reg
] );
926 info
->state
.regs
[reg
] = reg
;
927 info
->state
.rules
[reg
] = RULE_SAME
;
930 case DW_CFA_register
:
932 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
933 ULONG_PTR reg2
= dwarf_get_uleb128( &ptr
);
934 if (!valid_reg( reg
) || !valid_reg( reg2
)) break;
935 TRACE( "%lx: DW_CFA_register %s == %s\n", info
->ip
, dwarf_reg_names
[reg
], dwarf_reg_names
[reg2
] );
936 info
->state
.regs
[reg
] = reg2
;
937 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
940 case DW_CFA_remember_state
:
941 TRACE( "%lx: DW_CFA_remember_state\n", info
->ip
);
942 if (info
->state_sp
>= MAX_SAVED_STATES
)
943 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
945 info
->state_stack
[info
->state_sp
++] = info
->state
;
947 case DW_CFA_restore_state
:
948 TRACE( "%lx: DW_CFA_restore_state\n", info
->ip
);
950 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
952 info
->state
= info
->state_stack
[--info
->state_sp
];
955 case DW_CFA_def_cfa_sf
:
957 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
958 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf_get_uleb128( &ptr
)
959 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
960 if (!valid_reg( reg
)) break;
961 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
962 info
->state
.cfa_reg
= reg
;
963 info
->state
.cfa_offset
= offset
;
964 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
967 case DW_CFA_def_cfa_register
:
969 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
970 if (!valid_reg( reg
)) break;
971 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info
->ip
, dwarf_reg_names
[reg
] );
972 info
->state
.cfa_reg
= reg
;
973 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
976 case DW_CFA_def_cfa_offset
:
977 case DW_CFA_def_cfa_offset_sf
:
979 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf_get_uleb128( &ptr
)
980 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
981 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info
->ip
, offset
);
982 info
->state
.cfa_offset
= offset
;
983 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
986 case DW_CFA_def_cfa_expression
:
988 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
989 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
990 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
991 info
->state
.cfa_offset
= expr
;
992 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
996 case DW_CFA_expression
:
997 case DW_CFA_val_expression
:
999 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
1000 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
1001 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
1002 if (!valid_reg( reg
)) break;
1003 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
1004 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_", dwarf_reg_names
[reg
], expr
, expr
+len
);
1005 info
->state
.regs
[reg
] = expr
;
1006 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
1011 FIXME( "%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
1017 /* retrieve a context register from its dwarf number */
1018 static void *get_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
)
1022 case 0: return &context
->Rax
;
1023 case 1: return &context
->Rdx
;
1024 case 2: return &context
->Rcx
;
1025 case 3: return &context
->Rbx
;
1026 case 4: return &context
->Rsi
;
1027 case 5: return &context
->Rdi
;
1028 case 6: return &context
->Rbp
;
1029 case 7: return &context
->Rsp
;
1030 case 8: return &context
->R8
;
1031 case 9: return &context
->R9
;
1032 case 10: return &context
->R10
;
1033 case 11: return &context
->R11
;
1034 case 12: return &context
->R12
;
1035 case 13: return &context
->R13
;
1036 case 14: return &context
->R14
;
1037 case 15: return &context
->R15
;
1038 case 16: return &context
->Rip
;
1039 case 17: return &context
->u
.s
.Xmm0
;
1040 case 18: return &context
->u
.s
.Xmm1
;
1041 case 19: return &context
->u
.s
.Xmm2
;
1042 case 20: return &context
->u
.s
.Xmm3
;
1043 case 21: return &context
->u
.s
.Xmm4
;
1044 case 22: return &context
->u
.s
.Xmm5
;
1045 case 23: return &context
->u
.s
.Xmm6
;
1046 case 24: return &context
->u
.s
.Xmm7
;
1047 case 25: return &context
->u
.s
.Xmm8
;
1048 case 26: return &context
->u
.s
.Xmm9
;
1049 case 27: return &context
->u
.s
.Xmm10
;
1050 case 28: return &context
->u
.s
.Xmm11
;
1051 case 29: return &context
->u
.s
.Xmm12
;
1052 case 30: return &context
->u
.s
.Xmm13
;
1053 case 31: return &context
->u
.s
.Xmm14
;
1054 case 32: return &context
->u
.s
.Xmm15
;
1055 case 33: return &context
->u
.s
.Legacy
[0];
1056 case 34: return &context
->u
.s
.Legacy
[1];
1057 case 35: return &context
->u
.s
.Legacy
[2];
1058 case 36: return &context
->u
.s
.Legacy
[3];
1059 case 37: return &context
->u
.s
.Legacy
[4];
1060 case 38: return &context
->u
.s
.Legacy
[5];
1061 case 39: return &context
->u
.s
.Legacy
[6];
1062 case 40: return &context
->u
.s
.Legacy
[7];
1063 default: return NULL
;
1067 /* set a context register from its dwarf number */
1068 static void set_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
, void *val
)
1072 case 0: context
->Rax
= *(ULONG64
*)val
; break;
1073 case 1: context
->Rdx
= *(ULONG64
*)val
; break;
1074 case 2: context
->Rcx
= *(ULONG64
*)val
; break;
1075 case 3: context
->Rbx
= *(ULONG64
*)val
; break;
1076 case 4: context
->Rsi
= *(ULONG64
*)val
; break;
1077 case 5: context
->Rdi
= *(ULONG64
*)val
; break;
1078 case 6: context
->Rbp
= *(ULONG64
*)val
; break;
1079 case 7: context
->Rsp
= *(ULONG64
*)val
; break;
1080 case 8: context
->R8
= *(ULONG64
*)val
; break;
1081 case 9: context
->R9
= *(ULONG64
*)val
; break;
1082 case 10: context
->R10
= *(ULONG64
*)val
; break;
1083 case 11: context
->R11
= *(ULONG64
*)val
; break;
1084 case 12: context
->R12
= *(ULONG64
*)val
; break;
1085 case 13: context
->R13
= *(ULONG64
*)val
; break;
1086 case 14: context
->R14
= *(ULONG64
*)val
; break;
1087 case 15: context
->R15
= *(ULONG64
*)val
; break;
1088 case 16: context
->Rip
= *(ULONG64
*)val
; break;
1089 case 17: context
->u
.s
.Xmm0
= *(M128A
*)val
; break;
1090 case 18: context
->u
.s
.Xmm1
= *(M128A
*)val
; break;
1091 case 19: context
->u
.s
.Xmm2
= *(M128A
*)val
; break;
1092 case 20: context
->u
.s
.Xmm3
= *(M128A
*)val
; break;
1093 case 21: context
->u
.s
.Xmm4
= *(M128A
*)val
; break;
1094 case 22: context
->u
.s
.Xmm5
= *(M128A
*)val
; break;
1095 case 23: context
->u
.s
.Xmm6
= *(M128A
*)val
; break;
1096 case 24: context
->u
.s
.Xmm7
= *(M128A
*)val
; break;
1097 case 25: context
->u
.s
.Xmm8
= *(M128A
*)val
; break;
1098 case 26: context
->u
.s
.Xmm9
= *(M128A
*)val
; break;
1099 case 27: context
->u
.s
.Xmm10
= *(M128A
*)val
; break;
1100 case 28: context
->u
.s
.Xmm11
= *(M128A
*)val
; break;
1101 case 29: context
->u
.s
.Xmm12
= *(M128A
*)val
; break;
1102 case 30: context
->u
.s
.Xmm13
= *(M128A
*)val
; break;
1103 case 31: context
->u
.s
.Xmm14
= *(M128A
*)val
; break;
1104 case 32: context
->u
.s
.Xmm15
= *(M128A
*)val
; break;
1105 case 33: context
->u
.s
.Legacy
[0] = *(M128A
*)val
; break;
1106 case 34: context
->u
.s
.Legacy
[1] = *(M128A
*)val
; break;
1107 case 35: context
->u
.s
.Legacy
[2] = *(M128A
*)val
; break;
1108 case 36: context
->u
.s
.Legacy
[3] = *(M128A
*)val
; break;
1109 case 37: context
->u
.s
.Legacy
[4] = *(M128A
*)val
; break;
1110 case 38: context
->u
.s
.Legacy
[5] = *(M128A
*)val
; break;
1111 case 39: context
->u
.s
.Legacy
[6] = *(M128A
*)val
; break;
1112 case 40: context
->u
.s
.Legacy
[7] = *(M128A
*)val
; break;
1116 static ULONG_PTR
eval_expression( const unsigned char *p
, CONTEXT
*context
)
1118 ULONG_PTR reg
, tmp
, stack
[64];
1120 ULONG_PTR len
= dwarf_get_uleb128(&p
);
1121 const unsigned char *end
= p
+ len
;
1125 unsigned char opcode
= dwarf_get_u1(&p
);
1127 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
1128 stack
[++sp
] = opcode
- DW_OP_lit0
;
1129 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
1130 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_reg0
);
1131 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
1132 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_breg0
) + dwarf_get_sleb128(&p
);
1133 else switch (opcode
)
1135 case DW_OP_nop
: break;
1136 case DW_OP_addr
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1137 case DW_OP_const1u
: stack
[++sp
] = dwarf_get_u1(&p
); break;
1138 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf_get_u1(&p
); break;
1139 case DW_OP_const2u
: stack
[++sp
] = dwarf_get_u2(&p
); break;
1140 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf_get_u2(&p
); break;
1141 case DW_OP_const4u
: stack
[++sp
] = dwarf_get_u4(&p
); break;
1142 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf_get_u4(&p
); break;
1143 case DW_OP_const8u
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1144 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf_get_u8(&p
); break;
1145 case DW_OP_constu
: stack
[++sp
] = dwarf_get_uleb128(&p
); break;
1146 case DW_OP_consts
: stack
[++sp
] = dwarf_get_sleb128(&p
); break;
1147 case DW_OP_deref
: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1148 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
1149 case DW_OP_drop
: sp
--; break;
1150 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
1151 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf_get_u1(&p
)]; sp
++; break;
1152 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
1153 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
1154 case DW_OP_abs
: stack
[sp
] = labs(stack
[sp
]); break;
1155 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
1156 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
1157 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
1158 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
1159 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
1160 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
1161 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
1162 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
1163 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
1164 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
1165 case DW_OP_plus_uconst
: stack
[sp
] += dwarf_get_uleb128(&p
); break;
1166 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
1167 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
1168 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
1169 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
1170 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
1171 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
1172 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
1173 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
1174 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
1175 case DW_OP_skip
: tmp
= (short)dwarf_get_u2(&p
); p
+= tmp
; break;
1176 case DW_OP_bra
: tmp
= (short)dwarf_get_u2(&p
); if (!stack
[sp
--]) p
+= tmp
; break;
1177 case DW_OP_GNU_encoded_addr
: tmp
= *p
++; stack
[++sp
] = dwarf_get_ptr( &p
, tmp
); break;
1178 case DW_OP_regx
: stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, dwarf_get_uleb128(&p
) ); break;
1180 reg
= dwarf_get_uleb128(&p
);
1181 tmp
= dwarf_get_sleb128(&p
);
1182 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, reg
) + tmp
;
1184 case DW_OP_deref_size
:
1187 case 1: stack
[sp
] = *(unsigned char *)stack
[sp
]; break;
1188 case 2: stack
[sp
] = *(unsigned short *)stack
[sp
]; break;
1189 case 4: stack
[sp
] = *(unsigned int *)stack
[sp
]; break;
1190 case 8: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1194 FIXME( "unhandled opcode %02x\n", opcode
);
1200 /* apply the computed frame info to the actual context */
1201 static void apply_frame_state( CONTEXT
*context
, struct frame_state
*state
)
1204 ULONG_PTR cfa
, value
;
1205 CONTEXT new_context
= *context
;
1207 switch (state
->cfa_rule
)
1209 case RULE_EXPRESSION
:
1210 cfa
= *(ULONG_PTR
*)eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1212 case RULE_VAL_EXPRESSION
:
1213 cfa
= eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1216 cfa
= *(ULONG_PTR
*)get_context_reg( context
, state
->cfa_reg
) + state
->cfa_offset
;
1221 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
1223 switch (state
->rules
[i
])
1226 case RULE_UNDEFINED
:
1229 case RULE_CFA_OFFSET
:
1230 set_context_reg( &new_context
, i
, (char *)cfa
+ state
->regs
[i
] );
1232 case RULE_OTHER_REG
:
1233 set_context_reg( &new_context
, i
, get_context_reg( context
, state
->regs
[i
] ));
1235 case RULE_EXPRESSION
:
1236 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1237 set_context_reg( &new_context
, i
, (void *)value
);
1239 case RULE_VAL_EXPRESSION
:
1240 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1241 set_context_reg( &new_context
, i
, &value
);
1245 new_context
.Rsp
= cfa
;
1246 *context
= new_context
;
1250 /***********************************************************************
1251 * dwarf_virtual_unwind
1253 * Equivalent of RtlVirtualUnwind for builtin modules.
1255 static NTSTATUS
dwarf_virtual_unwind( ULONG64 ip
, ULONG64
*frame
,CONTEXT
*context
,
1256 const struct dwarf_fde
*fde
, const struct dwarf_eh_bases
*bases
,
1257 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1259 const struct dwarf_cie
*cie
;
1260 const unsigned char *ptr
, *augmentation
, *end
;
1261 ULONG_PTR len
, code_end
;
1262 struct frame_info info
;
1263 struct frame_state state_stack
[MAX_SAVED_STATES
];
1264 int aug_z_format
= 0;
1265 unsigned char lsda_encoding
= DW_EH_PE_omit
;
1267 memset( &info
, 0, sizeof(info
) );
1268 info
.state_stack
= state_stack
;
1269 info
.ip
= (ULONG_PTR
)bases
->func
;
1272 cie
= (const struct dwarf_cie
*)((const char *)&fde
->cie_offset
- fde
->cie_offset
);
1274 /* parse the CIE first */
1276 if (cie
->version
!= 1)
1278 FIXME( "unknown CIE version %u at %p\n", cie
->version
, cie
);
1279 return STATUS_INVALID_DISPOSITION
;
1281 ptr
= cie
->augmentation
+ strlen((const char *)cie
->augmentation
) + 1;
1283 info
.code_align
= dwarf_get_uleb128( &ptr
);
1284 info
.data_align
= dwarf_get_sleb128( &ptr
);
1285 info
.retaddr_reg
= *ptr
++;
1286 info
.state
.cfa_rule
= RULE_CFA_OFFSET
;
1288 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1289 ip
, bases
->func
, cie
, cie
->length
, cie
->id
, cie
->version
, cie
->augmentation
,
1290 info
.code_align
, info
.data_align
, dwarf_reg_names
[info
.retaddr_reg
] );
1293 for (augmentation
= cie
->augmentation
; *augmentation
; augmentation
++)
1295 switch (*augmentation
)
1298 len
= dwarf_get_uleb128( &ptr
);
1303 lsda_encoding
= *ptr
++;
1307 unsigned char encoding
= *ptr
++;
1308 *handler
= (void *)dwarf_get_ptr( &ptr
, encoding
);
1312 info
.fde_encoding
= *ptr
++;
1315 info
.signal_frame
= 1;
1318 FIXME( "unknown augmentation '%c'\n", *augmentation
);
1319 if (!end
) return STATUS_INVALID_DISPOSITION
; /* cannot continue */
1324 end
= (const unsigned char *)(&cie
->length
+ 1) + cie
->length
;
1325 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1327 ptr
= (const unsigned char *)(fde
+ 1);
1328 info
.ip
= dwarf_get_ptr( &ptr
, info
.fde_encoding
); /* fde code start */
1329 code_end
= info
.ip
+ dwarf_get_ptr( &ptr
, info
.fde_encoding
& 0x0f ); /* fde code length */
1331 if (aug_z_format
) /* get length of augmentation data */
1333 len
= dwarf_get_uleb128( &ptr
);
1338 *handler_data
= (void *)dwarf_get_ptr( &ptr
, lsda_encoding
);
1341 end
= (const unsigned char *)(&fde
->length
+ 1) + fde
->length
;
1342 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1343 fde
, fde
->length
, *handler
, *handler_data
, info
.ip
, code_end
);
1344 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1345 apply_frame_state( context
, &info
.state
);
1346 *frame
= context
->Rsp
;
1348 TRACE( "next function rip=%016lx\n", context
->Rip
);
1349 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1350 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1351 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1352 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1353 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1354 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1355 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1356 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1358 return STATUS_SUCCESS
;
1362 /***********************************************************************
1365 static inline int dispatch_signal(unsigned int sig
)
1367 if (handlers
[sig
] == NULL
) return 0;
1368 return handlers
[sig
](sig
);
1371 /***********************************************************************
1374 * Get the base of the signal stack for the current thread.
1376 static inline void *get_signal_stack(void)
1378 return (char *)NtCurrentTeb() + teb_size
;
1381 /***********************************************************************
1382 * is_inside_signal_stack
1384 * Check if pointer is inside the signal stack.
1386 static inline int is_inside_signal_stack( void *ptr
)
1388 return ((char *)ptr
>= (char *)get_signal_stack() &&
1389 (char *)ptr
< (char *)get_signal_stack() + signal_stack_size
);
1392 /***********************************************************************
1395 * Set the register values from a sigcontext.
1397 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
1399 context
->ContextFlags
= CONTEXT_CONTROL
| CONTEXT_INTEGER
| CONTEXT_SEGMENTS
;
1400 context
->Rax
= RAX_sig(sigcontext
);
1401 context
->Rcx
= RCX_sig(sigcontext
);
1402 context
->Rdx
= RDX_sig(sigcontext
);
1403 context
->Rbx
= RBX_sig(sigcontext
);
1404 context
->Rsp
= RSP_sig(sigcontext
);
1405 context
->Rbp
= RBP_sig(sigcontext
);
1406 context
->Rsi
= RSI_sig(sigcontext
);
1407 context
->Rdi
= RDI_sig(sigcontext
);
1408 context
->R8
= R8_sig(sigcontext
);
1409 context
->R9
= R9_sig(sigcontext
);
1410 context
->R10
= R10_sig(sigcontext
);
1411 context
->R11
= R11_sig(sigcontext
);
1412 context
->R12
= R12_sig(sigcontext
);
1413 context
->R13
= R13_sig(sigcontext
);
1414 context
->R14
= R14_sig(sigcontext
);
1415 context
->R15
= R15_sig(sigcontext
);
1416 context
->Rip
= RIP_sig(sigcontext
);
1417 context
->SegCs
= CS_sig(sigcontext
);
1418 context
->SegFs
= FS_sig(sigcontext
);
1419 context
->SegGs
= GS_sig(sigcontext
);
1420 context
->EFlags
= EFL_sig(sigcontext
);
1422 context
->SegDs
= DS_sig(sigcontext
);
1424 __asm__("movw %%ds,%0" : "=m" (context
->SegDs
));
1427 context
->SegEs
= ES_sig(sigcontext
);
1429 __asm__("movw %%es,%0" : "=m" (context
->SegEs
));
1432 context
->SegSs
= SS_sig(sigcontext
);
1434 __asm__("movw %%ss,%0" : "=m" (context
->SegSs
));
1436 if (FPU_sig(sigcontext
))
1438 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1439 context
->u
.FltSave
= *FPU_sig(sigcontext
);
1440 context
->MxCsr
= context
->u
.FltSave
.MxCsr
;
1445 /***********************************************************************
1448 * Build a sigcontext from the register values.
1450 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1452 RAX_sig(sigcontext
) = context
->Rax
;
1453 RCX_sig(sigcontext
) = context
->Rcx
;
1454 RDX_sig(sigcontext
) = context
->Rdx
;
1455 RBX_sig(sigcontext
) = context
->Rbx
;
1456 RSP_sig(sigcontext
) = context
->Rsp
;
1457 RBP_sig(sigcontext
) = context
->Rbp
;
1458 RSI_sig(sigcontext
) = context
->Rsi
;
1459 RDI_sig(sigcontext
) = context
->Rdi
;
1460 R8_sig(sigcontext
) = context
->R8
;
1461 R9_sig(sigcontext
) = context
->R9
;
1462 R10_sig(sigcontext
) = context
->R10
;
1463 R11_sig(sigcontext
) = context
->R11
;
1464 R12_sig(sigcontext
) = context
->R12
;
1465 R13_sig(sigcontext
) = context
->R13
;
1466 R14_sig(sigcontext
) = context
->R14
;
1467 R15_sig(sigcontext
) = context
->R15
;
1468 RIP_sig(sigcontext
) = context
->Rip
;
1469 CS_sig(sigcontext
) = context
->SegCs
;
1470 FS_sig(sigcontext
) = context
->SegFs
;
1471 GS_sig(sigcontext
) = context
->SegGs
;
1472 EFL_sig(sigcontext
) = context
->EFlags
;
1474 DS_sig(sigcontext
) = context
->SegDs
;
1477 ES_sig(sigcontext
) = context
->SegEs
;
1480 SS_sig(sigcontext
) = context
->SegSs
;
1482 if (FPU_sig(sigcontext
)) *FPU_sig(sigcontext
) = context
->u
.FltSave
;
1486 /**************************************************************************
1487 * __chkstk (NTDLL.@)
1489 * Supposed to touch all the stack pages, but we shouldn't need that.
1491 __ASM_GLOBAL_FUNC( __chkstk
, "ret" );
1494 /***********************************************************************
1495 * RtlCaptureContext (NTDLL.@)
1497 __ASM_GLOBAL_FUNC( RtlCaptureContext
,
1499 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1500 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1501 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1502 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1503 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1504 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1505 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1506 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1507 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1508 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1509 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1510 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1511 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1512 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1513 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1514 "leaq 8(%rsp),%rax\n\t"
1515 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1516 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1517 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1518 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1519 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1520 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1521 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1522 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1523 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1524 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1525 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1526 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1527 "movq (%rsp),%rax\n\t"
1528 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1529 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1530 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1531 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1532 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1533 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1534 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1535 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1536 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1537 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1538 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1539 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1540 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1541 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1542 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1543 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1544 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1545 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1548 /***********************************************************************
1551 * Set the new CPU context.
1553 __ASM_GLOBAL_FUNC( set_cpu_context
,
1555 __ASM_CFI(".cfi_adjust_cfa_offset 40\n\t")
1556 "ldmxcsr 0x34(%rdi)\n\t" /* context->MxCsr */
1557 "movw 0x38(%rdi),%ax\n\t" /* context->SegCs */
1558 "movq %rax,8(%rsp)\n\t"
1559 "movw 0x42(%rdi),%ax\n\t" /* context->SegSs */
1560 "movq %rax,32(%rsp)\n\t"
1561 "movq 0x44(%rdi),%rax\n\t" /* context->Eflags */
1562 "movq %rax,16(%rsp)\n\t"
1563 "movq 0x80(%rdi),%rcx\n\t" /* context->Rcx */
1564 "movq 0x88(%rdi),%rdx\n\t" /* context->Rdx */
1565 "movq 0x90(%rdi),%rbx\n\t" /* context->Rbx */
1566 "movq 0x98(%rdi),%rax\n\t" /* context->Rsp */
1567 "movq %rax,24(%rsp)\n\t"
1568 "movq 0xa0(%rdi),%rbp\n\t" /* context->Rbp */
1569 "movq 0xa8(%rdi),%rsi\n\t" /* context->Rsi */
1570 "movq 0xb8(%rdi),%r8\n\t" /* context->R8 */
1571 "movq 0xc0(%rdi),%r9\n\t" /* context->R9 */
1572 "movq 0xc8(%rdi),%r10\n\t" /* context->R10 */
1573 "movq 0xd0(%rdi),%r11\n\t" /* context->R11 */
1574 "movq 0xd8(%rdi),%r12\n\t" /* context->R12 */
1575 "movq 0xe0(%rdi),%r13\n\t" /* context->R13 */
1576 "movq 0xe8(%rdi),%r14\n\t" /* context->R14 */
1577 "movq 0xf0(%rdi),%r15\n\t" /* context->R15 */
1578 "movq 0xf8(%rdi),%rax\n\t" /* context->Rip */
1579 "movq %rax,(%rsp)\n\t"
1580 "fxrstor 0x100(%rdi)\n\t" /* context->FtlSave */
1581 "movdqa 0x1a0(%rdi),%xmm0\n\t" /* context->Xmm0 */
1582 "movdqa 0x1b0(%rdi),%xmm1\n\t" /* context->Xmm1 */
1583 "movdqa 0x1c0(%rdi),%xmm2\n\t" /* context->Xmm2 */
1584 "movdqa 0x1d0(%rdi),%xmm3\n\t" /* context->Xmm3 */
1585 "movdqa 0x1e0(%rdi),%xmm4\n\t" /* context->Xmm4 */
1586 "movdqa 0x1f0(%rdi),%xmm5\n\t" /* context->Xmm5 */
1587 "movdqa 0x200(%rdi),%xmm6\n\t" /* context->Xmm6 */
1588 "movdqa 0x210(%rdi),%xmm7\n\t" /* context->Xmm7 */
1589 "movdqa 0x220(%rdi),%xmm8\n\t" /* context->Xmm8 */
1590 "movdqa 0x230(%rdi),%xmm9\n\t" /* context->Xmm9 */
1591 "movdqa 0x240(%rdi),%xmm10\n\t" /* context->Xmm10 */
1592 "movdqa 0x250(%rdi),%xmm11\n\t" /* context->Xmm11 */
1593 "movdqa 0x260(%rdi),%xmm12\n\t" /* context->Xmm12 */
1594 "movdqa 0x270(%rdi),%xmm13\n\t" /* context->Xmm13 */
1595 "movdqa 0x280(%rdi),%xmm14\n\t" /* context->Xmm14 */
1596 "movdqa 0x290(%rdi),%xmm15\n\t" /* context->Xmm15 */
1597 "movq 0x78(%rdi),%rax\n\t" /* context->Rax */
1598 "movq 0xb0(%rdi),%rdi\n\t" /* context->Rdi */
1601 /***********************************************************************
1604 * Copy a register context according to the flags.
1606 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1608 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1609 if (flags
& CONTEXT_CONTROL
)
1611 to
->Rbp
= from
->Rbp
;
1612 to
->Rip
= from
->Rip
;
1613 to
->Rsp
= from
->Rsp
;
1614 to
->SegCs
= from
->SegCs
;
1615 to
->SegSs
= from
->SegSs
;
1616 to
->EFlags
= from
->EFlags
;
1618 if (flags
& CONTEXT_INTEGER
)
1620 to
->Rax
= from
->Rax
;
1621 to
->Rcx
= from
->Rcx
;
1622 to
->Rdx
= from
->Rdx
;
1623 to
->Rbx
= from
->Rbx
;
1624 to
->Rsi
= from
->Rsi
;
1625 to
->Rdi
= from
->Rdi
;
1628 to
->R10
= from
->R10
;
1629 to
->R11
= from
->R11
;
1630 to
->R12
= from
->R12
;
1631 to
->R13
= from
->R13
;
1632 to
->R14
= from
->R14
;
1633 to
->R15
= from
->R15
;
1635 if (flags
& CONTEXT_SEGMENTS
)
1637 to
->SegDs
= from
->SegDs
;
1638 to
->SegEs
= from
->SegEs
;
1639 to
->SegFs
= from
->SegFs
;
1640 to
->SegGs
= from
->SegGs
;
1642 if (flags
& CONTEXT_FLOATING_POINT
)
1644 to
->MxCsr
= from
->MxCsr
;
1645 to
->u
.FltSave
= from
->u
.FltSave
;
1647 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1649 to
->Dr0
= from
->Dr0
;
1650 to
->Dr1
= from
->Dr1
;
1651 to
->Dr2
= from
->Dr2
;
1652 to
->Dr3
= from
->Dr3
;
1653 to
->Dr6
= from
->Dr6
;
1654 to
->Dr7
= from
->Dr7
;
1659 /***********************************************************************
1662 * Convert a register context to the server format.
1664 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1666 DWORD flags
= from
->ContextFlags
& ~CONTEXT_AMD64
; /* get rid of CPU id */
1668 memset( to
, 0, sizeof(*to
) );
1669 to
->cpu
= CPU_x86_64
;
1671 if (flags
& CONTEXT_CONTROL
)
1673 to
->flags
|= SERVER_CTX_CONTROL
;
1674 to
->ctl
.x86_64_regs
.rbp
= from
->Rbp
;
1675 to
->ctl
.x86_64_regs
.rip
= from
->Rip
;
1676 to
->ctl
.x86_64_regs
.rsp
= from
->Rsp
;
1677 to
->ctl
.x86_64_regs
.cs
= from
->SegCs
;
1678 to
->ctl
.x86_64_regs
.ss
= from
->SegSs
;
1679 to
->ctl
.x86_64_regs
.flags
= from
->EFlags
;
1681 if (flags
& CONTEXT_INTEGER
)
1683 to
->flags
|= SERVER_CTX_INTEGER
;
1684 to
->integer
.x86_64_regs
.rax
= from
->Rax
;
1685 to
->integer
.x86_64_regs
.rcx
= from
->Rcx
;
1686 to
->integer
.x86_64_regs
.rdx
= from
->Rdx
;
1687 to
->integer
.x86_64_regs
.rbx
= from
->Rbx
;
1688 to
->integer
.x86_64_regs
.rsi
= from
->Rsi
;
1689 to
->integer
.x86_64_regs
.rdi
= from
->Rdi
;
1690 to
->integer
.x86_64_regs
.r8
= from
->R8
;
1691 to
->integer
.x86_64_regs
.r9
= from
->R9
;
1692 to
->integer
.x86_64_regs
.r10
= from
->R10
;
1693 to
->integer
.x86_64_regs
.r11
= from
->R11
;
1694 to
->integer
.x86_64_regs
.r12
= from
->R12
;
1695 to
->integer
.x86_64_regs
.r13
= from
->R13
;
1696 to
->integer
.x86_64_regs
.r14
= from
->R14
;
1697 to
->integer
.x86_64_regs
.r15
= from
->R15
;
1699 if (flags
& CONTEXT_SEGMENTS
)
1701 to
->flags
|= SERVER_CTX_SEGMENTS
;
1702 to
->seg
.x86_64_regs
.ds
= from
->SegDs
;
1703 to
->seg
.x86_64_regs
.es
= from
->SegEs
;
1704 to
->seg
.x86_64_regs
.fs
= from
->SegFs
;
1705 to
->seg
.x86_64_regs
.gs
= from
->SegGs
;
1707 if (flags
& CONTEXT_FLOATING_POINT
)
1709 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1710 memcpy( to
->fp
.x86_64_regs
.fpregs
, &from
->u
.FltSave
, sizeof(to
->fp
.x86_64_regs
.fpregs
) );
1712 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1714 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1715 to
->debug
.x86_64_regs
.dr0
= from
->Dr0
;
1716 to
->debug
.x86_64_regs
.dr1
= from
->Dr1
;
1717 to
->debug
.x86_64_regs
.dr2
= from
->Dr2
;
1718 to
->debug
.x86_64_regs
.dr3
= from
->Dr3
;
1719 to
->debug
.x86_64_regs
.dr6
= from
->Dr6
;
1720 to
->debug
.x86_64_regs
.dr7
= from
->Dr7
;
1722 return STATUS_SUCCESS
;
1726 /***********************************************************************
1727 * context_from_server
1729 * Convert a register context from the server format.
1731 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1733 if (from
->cpu
!= CPU_x86_64
) return STATUS_INVALID_PARAMETER
;
1735 to
->ContextFlags
= CONTEXT_AMD64
;
1736 if (from
->flags
& SERVER_CTX_CONTROL
)
1738 to
->ContextFlags
|= CONTEXT_CONTROL
;
1739 to
->Rbp
= from
->ctl
.x86_64_regs
.rbp
;
1740 to
->Rip
= from
->ctl
.x86_64_regs
.rip
;
1741 to
->Rsp
= from
->ctl
.x86_64_regs
.rsp
;
1742 to
->SegCs
= from
->ctl
.x86_64_regs
.cs
;
1743 to
->SegSs
= from
->ctl
.x86_64_regs
.ss
;
1744 to
->EFlags
= from
->ctl
.x86_64_regs
.flags
;
1747 if (from
->flags
& SERVER_CTX_INTEGER
)
1749 to
->ContextFlags
|= CONTEXT_INTEGER
;
1750 to
->Rax
= from
->integer
.x86_64_regs
.rax
;
1751 to
->Rcx
= from
->integer
.x86_64_regs
.rcx
;
1752 to
->Rdx
= from
->integer
.x86_64_regs
.rdx
;
1753 to
->Rbx
= from
->integer
.x86_64_regs
.rbx
;
1754 to
->Rsi
= from
->integer
.x86_64_regs
.rsi
;
1755 to
->Rdi
= from
->integer
.x86_64_regs
.rdi
;
1756 to
->R8
= from
->integer
.x86_64_regs
.r8
;
1757 to
->R9
= from
->integer
.x86_64_regs
.r9
;
1758 to
->R10
= from
->integer
.x86_64_regs
.r10
;
1759 to
->R11
= from
->integer
.x86_64_regs
.r11
;
1760 to
->R12
= from
->integer
.x86_64_regs
.r12
;
1761 to
->R13
= from
->integer
.x86_64_regs
.r13
;
1762 to
->R14
= from
->integer
.x86_64_regs
.r14
;
1763 to
->R15
= from
->integer
.x86_64_regs
.r15
;
1765 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1767 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1768 to
->SegDs
= from
->seg
.x86_64_regs
.ds
;
1769 to
->SegEs
= from
->seg
.x86_64_regs
.es
;
1770 to
->SegFs
= from
->seg
.x86_64_regs
.fs
;
1771 to
->SegGs
= from
->seg
.x86_64_regs
.gs
;
1773 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1775 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1776 memcpy( &to
->u
.FltSave
, from
->fp
.x86_64_regs
.fpregs
, sizeof(from
->fp
.x86_64_regs
.fpregs
) );
1777 to
->MxCsr
= to
->u
.FltSave
.MxCsr
;
1779 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1781 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1782 to
->Dr0
= from
->debug
.x86_64_regs
.dr0
;
1783 to
->Dr1
= from
->debug
.x86_64_regs
.dr1
;
1784 to
->Dr2
= from
->debug
.x86_64_regs
.dr2
;
1785 to
->Dr3
= from
->debug
.x86_64_regs
.dr3
;
1786 to
->Dr6
= from
->debug
.x86_64_regs
.dr6
;
1787 to
->Dr7
= from
->debug
.x86_64_regs
.dr7
;
1789 return STATUS_SUCCESS
;
1793 extern void raise_func_trampoline( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
1794 __ASM_GLOBAL_FUNC( raise_func_trampoline
,
1795 __ASM_CFI(".cfi_signal_frame\n\t")
1796 __ASM_CFI(".cfi_def_cfa %rbp,144\n\t") /* red zone + rip + rbp */
1797 __ASM_CFI(".cfi_rel_offset %rip,8\n\t")
1798 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1802 /***********************************************************************
1805 * Setup a proper stack frame for the raise function, and modify the
1806 * sigcontext so that the return from the signal handler will call
1807 * the raise function.
1809 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
1814 EXCEPTION_RECORD rec
;
1817 ULONG64 red_zone
[16];
1820 DWORD exception_code
= 0;
1822 stack
= (struct stack_layout
*)(RSP_sig(sigcontext
) & ~15);
1824 /* stack sanity checks */
1826 if (is_inside_signal_stack( stack
))
1828 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1829 GetCurrentThreadId(), RIP_sig(sigcontext
), RSP_sig(sigcontext
),
1830 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1834 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1835 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1836 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1838 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1839 GetCurrentThreadId(), RIP_sig(sigcontext
), RSP_sig(sigcontext
),
1840 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1842 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1844 /* stack overflow on last page, unrecoverable */
1845 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1846 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1847 diff
, GetCurrentThreadId(), RIP_sig(sigcontext
),
1848 RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1849 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1852 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1854 /* stack access below stack limit, may be recoverable */
1855 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1858 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1859 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1860 diff
, GetCurrentThreadId(), RIP_sig(sigcontext
),
1861 RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1862 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1867 stack
--; /* push the stack_layout structure */
1868 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1869 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1870 #elif defined(VALGRIND_MAKE_WRITABLE)
1871 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1873 stack
->rec
.ExceptionRecord
= NULL
;
1874 stack
->rec
.ExceptionCode
= exception_code
;
1875 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1876 stack
->rec
.ExceptionAddress
= (void *)RIP_sig(sigcontext
);
1877 stack
->rec
.NumberParameters
= 0;
1878 save_context( &stack
->context
, sigcontext
);
1880 /* store return address and %rbp without aligning, so that the offset is fixed */
1881 rsp_ptr
= (ULONG64
*)RSP_sig(sigcontext
) - 16;
1882 *(--rsp_ptr
) = RIP_sig(sigcontext
);
1883 *(--rsp_ptr
) = RBP_sig(sigcontext
);
1885 /* now modify the sigcontext to return to the raise function */
1886 RIP_sig(sigcontext
) = (ULONG_PTR
)raise_func_trampoline
;
1887 RDI_sig(sigcontext
) = (ULONG_PTR
)&stack
->rec
;
1888 RSI_sig(sigcontext
) = (ULONG_PTR
)&stack
->context
;
1889 RDX_sig(sigcontext
) = (ULONG_PTR
)func
;
1890 RBP_sig(sigcontext
) = (ULONG_PTR
)rsp_ptr
;
1891 RSP_sig(sigcontext
) = (ULONG_PTR
)stack
;
1892 /* clear single-step, direction, and align check flag */
1893 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1899 /**********************************************************************
1900 * find_function_info
1902 static RUNTIME_FUNCTION
*find_function_info( ULONG64 pc
, HMODULE module
,
1903 RUNTIME_FUNCTION
*func
, ULONG size
)
1906 int max
= size
/sizeof(*func
) - 1;
1910 int pos
= (min
+ max
) / 2;
1911 if ((char *)pc
< (char *)module
+ func
[pos
].BeginAddress
) max
= pos
- 1;
1912 else if ((char *)pc
>= (char *)module
+ func
[pos
].EndAddress
) min
= pos
+ 1;
1916 while (func
->UnwindData
& 1) /* follow chained entry */
1917 func
= (RUNTIME_FUNCTION
*)((char *)module
+ (func
->UnwindData
& ~1));
1925 /**********************************************************************
1928 * Call a single exception handler.
1929 * FIXME: Handle nested exceptions.
1931 static NTSTATUS
call_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*orig_context
)
1935 dispatch
->ControlPc
= dispatch
->ContextRecord
->Rip
;
1937 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
1938 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
1939 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
1940 TRACE( "handler at %p returned %u\n", dispatch
->LanguageHandler
, res
);
1944 case ExceptionContinueExecution
:
1945 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
1946 *orig_context
= *dispatch
->ContextRecord
;
1947 return STATUS_SUCCESS
;
1948 case ExceptionContinueSearch
:
1950 case ExceptionNestedException
:
1953 return STATUS_INVALID_DISPOSITION
;
1955 return STATUS_UNHANDLED_EXCEPTION
;
1959 /**********************************************************************
1962 * Call a single exception handler from the TEB chain.
1963 * FIXME: Handle nested exceptions.
1965 static NTSTATUS
call_teb_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
1966 EXCEPTION_REGISTRATION_RECORD
*teb_frame
, CONTEXT
*orig_context
)
1968 EXCEPTION_REGISTRATION_RECORD
*dispatcher
;
1971 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
1972 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
1973 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
1974 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
1978 case ExceptionContinueExecution
:
1979 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
1980 *orig_context
= *dispatch
->ContextRecord
;
1981 return STATUS_SUCCESS
;
1982 case ExceptionContinueSearch
:
1984 case ExceptionNestedException
:
1987 return STATUS_INVALID_DISPOSITION
;
1989 return STATUS_UNHANDLED_EXCEPTION
;
1993 /**********************************************************************
1994 * call_stack_handlers
1996 * Call the stack handlers chain.
1998 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
2000 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2001 UNWIND_HISTORY_TABLE table
;
2002 DISPATCHER_CONTEXT dispatch
;
2003 CONTEXT context
, new_context
;
2008 context
= *orig_context
;
2009 dispatch
.TargetIp
= 0;
2010 dispatch
.ContextRecord
= &context
;
2011 dispatch
.HistoryTable
= &table
;
2012 dispatch
.ScopeIndex
= 0; /* FIXME */
2015 new_context
= context
;
2017 /* FIXME: should use the history table to make things faster */
2020 dispatch
.ImageBase
= 0;
2022 /* first look for PE exception information */
2024 if (!LdrFindEntryForAddress( (void *)context
.Rip
, &module
))
2026 RUNTIME_FUNCTION
*dir
;
2028 dispatch
.ImageBase
= (ULONG64
)module
->BaseAddress
;
2029 if ((dir
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
2030 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
2032 if ((dispatch
.FunctionEntry
= find_function_info( context
.Rip
, module
->BaseAddress
,
2035 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_EHANDLER
, dispatch
.ImageBase
,
2036 context
.Rip
, dispatch
.FunctionEntry
,
2037 &new_context
, &dispatch
.HandlerData
,
2038 &dispatch
.EstablisherFrame
, NULL
);
2042 else if (!(module
->Flags
& LDR_WINE_INTERNAL
))
2043 WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
2046 /* then look for host system exception information */
2048 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
2050 struct dwarf_eh_bases bases
;
2051 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
.Rip
- 1), &bases
);
2055 status
= dwarf_virtual_unwind( context
.Rip
, &dispatch
.EstablisherFrame
, &new_context
,
2056 fde
, &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2057 if (status
!= STATUS_SUCCESS
) return status
;
2058 dispatch
.FunctionEntry
= NULL
;
2059 if (dispatch
.LanguageHandler
&& !module
)
2061 FIXME( "calling personality routine in system library not supported yet\n" );
2062 dispatch
.LanguageHandler
= NULL
;
2068 /* no exception information, treat as a leaf function */
2070 new_context
.Rip
= *(ULONG64
*)context
.Rsp
;
2071 new_context
.Rsp
= context
.Rsp
+ sizeof(ULONG64
);
2072 dispatch
.EstablisherFrame
= new_context
.Rsp
;
2073 dispatch
.LanguageHandler
= NULL
;
2076 if (!dispatch
.EstablisherFrame
) break;
2078 if ((dispatch
.EstablisherFrame
& 7) ||
2079 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
2080 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
2082 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
2083 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2084 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
2088 if (dispatch
.LanguageHandler
)
2090 status
= call_handler( rec
, &dispatch
, orig_context
);
2091 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
2093 /* hack: call wine handlers registered in the tib list */
2094 else while ((ULONG64
)teb_frame
< new_context
.Rsp
)
2096 TRACE( "found wine frame %p rsp %lx handler %p\n",
2097 teb_frame
, new_context
.Rsp
, teb_frame
->Handler
);
2098 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
2099 context
= *orig_context
;
2100 status
= call_teb_handler( rec
, &dispatch
, teb_frame
, orig_context
);
2101 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
2102 teb_frame
= teb_frame
->Prev
;
2105 if (new_context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
2106 context
= new_context
;
2108 return STATUS_UNHANDLED_EXCEPTION
;
2112 /*******************************************************************
2115 * Implementation of NtRaiseException.
2117 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2125 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2126 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
2127 context
->Rip
, GetCurrentThreadId() );
2128 for (c
= 0; c
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); c
++)
2129 TRACE( " info[%d]=%016lx\n", c
, rec
->ExceptionInformation
[c
] );
2130 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
2132 if (rec
->ExceptionInformation
[1] >> 16)
2133 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2134 rec
->ExceptionAddress
,
2135 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
2137 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2138 rec
->ExceptionAddress
,
2139 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
2143 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2144 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
2145 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2146 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
2147 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2148 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
2149 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2150 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
2152 status
= send_debug_event( rec
, TRUE
, context
);
2153 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
2154 return STATUS_SUCCESS
;
2156 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
2157 return STATUS_SUCCESS
;
2159 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
2163 /* last chance exception */
2165 status
= send_debug_event( rec
, FALSE
, context
);
2166 if (status
!= DBG_CONTINUE
)
2168 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
2169 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2170 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
2171 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2173 ERR("Unhandled exception code %x flags %x addr %p\n",
2174 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
2175 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
2177 return STATUS_SUCCESS
;
2181 /**********************************************************************
2182 * raise_segv_exception
2184 static void raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2188 switch(rec
->ExceptionCode
)
2190 case EXCEPTION_ACCESS_VIOLATION
:
2191 if (rec
->NumberParameters
== 2)
2193 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
2194 rec
->ExceptionInformation
[0] )))
2195 set_cpu_context( context
);
2199 status
= raise_exception( rec
, context
, TRUE
);
2200 if (status
) raise_status( status
, rec
);
2201 set_cpu_context( context
);
2205 /**********************************************************************
2206 * raise_generic_exception
2208 * Generic raise function for exceptions that don't need special treatment.
2210 static void raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2212 NTSTATUS status
= raise_exception( rec
, context
, TRUE
);
2213 if (status
) raise_status( status
, rec
);
2214 set_cpu_context( context
);
2218 /**********************************************************************
2221 * Handler for SIGSEGV and related errors.
2223 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2225 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_segv_exception
);
2226 ucontext_t
*ucontext
= sigcontext
;
2228 switch(TRAP_sig(ucontext
))
2230 case TRAP_x86_OFLOW
: /* Overflow exception */
2231 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
2233 case TRAP_x86_BOUND
: /* Bound range exception */
2234 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
2236 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
2237 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2239 case TRAP_x86_STKFLT
: /* Stack fault */
2240 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2242 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
2243 case TRAP_x86_PROTFLT
: /* General protection fault */
2244 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2245 rec
->ExceptionCode
= ERROR_sig(ucontext
) ? EXCEPTION_ACCESS_VIOLATION
: EXCEPTION_PRIV_INSTRUCTION
;
2246 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2248 case TRAP_x86_PAGEFLT
: /* Page fault */
2249 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2250 rec
->NumberParameters
= 2;
2251 rec
->ExceptionInformation
[0] = (ERROR_sig(ucontext
) & 2) != 0;
2252 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
2254 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
2255 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
2258 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext
) );
2260 case TRAP_x86_NMI
: /* NMI interrupt */
2261 case TRAP_x86_DNA
: /* Device not available exception */
2262 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
2263 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
2264 case TRAP_x86_MCHK
: /* Machine check exception */
2265 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
2266 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2271 /**********************************************************************
2274 * Handler for SIGTRAP.
2276 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2278 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2280 switch (siginfo
->si_code
)
2282 case TRAP_TRACE
: /* Single-step exception */
2283 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2284 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2286 case TRAP_BRKPT
: /* Breakpoint exception */
2287 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
2290 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
2295 /**********************************************************************
2298 * Handler for SIGFPE.
2300 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2302 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2304 switch (siginfo
->si_code
)
2307 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
2310 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
2313 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
2316 rec
->ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
2319 rec
->ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
2322 rec
->ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
2325 rec
->ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
2329 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2334 /**********************************************************************
2337 * Handler for SIGINT.
2339 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2341 if (!dispatch_signal(SIGINT
))
2343 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2344 rec
->ExceptionCode
= CONTROL_C_EXIT
;
2349 /**********************************************************************
2352 * Handler for SIGABRT.
2354 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2356 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2357 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2358 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
2362 /**********************************************************************
2365 * Handler for SIGQUIT.
2367 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
2373 /**********************************************************************
2376 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2378 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
2382 save_context( &context
, ucontext
);
2383 wait_suspend( &context
);
2384 restore_context( &context
, ucontext
);
2388 /***********************************************************************
2389 * __wine_set_signal_handler (NTDLL.@)
2391 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2393 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
2394 if (handlers
[sig
] != NULL
) return -2;
2395 handlers
[sig
] = wsh
;
2400 /**********************************************************************
2401 * signal_alloc_thread
2403 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2405 static size_t sigstack_zero_bits
;
2409 if (!sigstack_zero_bits
)
2411 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2412 /* find the first power of two not smaller than min_size */
2413 sigstack_zero_bits
= 12;
2414 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
2415 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
2416 assert( sizeof(TEB
) <= teb_size
);
2419 size
= 1 << sigstack_zero_bits
;
2421 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
2422 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
2424 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2425 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2431 /**********************************************************************
2432 * signal_free_thread
2434 void signal_free_thread( TEB
*teb
)
2438 if (teb
->DeallocationStack
)
2441 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
2444 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2448 /**********************************************************************
2449 * signal_init_thread
2451 void signal_init_thread( TEB
*teb
)
2453 const WORD fpu_cw
= 0x27f;
2456 #if defined __linux__
2457 arch_prctl( ARCH_SET_GS
, teb
);
2458 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
2459 amd64_set_gsbase( teb
);
2461 # error Please define setting %gs for your architecture
2464 ss
.ss_sp
= (char *)teb
+ teb_size
;
2465 ss
.ss_size
= signal_stack_size
;
2467 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2470 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2472 FIXME("FPU setup not implemented for this platform.\n");
2476 /**********************************************************************
2477 * signal_init_process
2479 void signal_init_process(void)
2481 struct sigaction sig_act
;
2483 sig_act
.sa_mask
= server_block_set
;
2484 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
| SA_ONSTACK
;
2486 sig_act
.sa_sigaction
= int_handler
;
2487 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2488 sig_act
.sa_sigaction
= fpe_handler
;
2489 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2490 sig_act
.sa_sigaction
= abrt_handler
;
2491 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2492 sig_act
.sa_sigaction
= quit_handler
;
2493 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2494 sig_act
.sa_sigaction
= usr1_handler
;
2495 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2497 sig_act
.sa_sigaction
= segv_handler
;
2498 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2499 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2501 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2505 sig_act
.sa_sigaction
= trap_handler
;
2506 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2511 perror("sigaction");
2516 /**********************************************************************
2517 * RtlAddFunctionTable (NTDLL.@)
2519 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, DWORD64 addr
)
2521 FIXME( "%p %u %lx: stub\n", table
, count
, addr
);
2526 /**********************************************************************
2527 * RtlDeleteFunctionTable (NTDLL.@)
2529 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
2531 FIXME( "%p: stub\n", table
);
2536 /**********************************************************************
2537 * RtlLookupFunctionEntry (NTDLL.@)
2539 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG64 pc
, ULONG64
*base
, UNWIND_HISTORY_TABLE
*table
)
2542 RUNTIME_FUNCTION
*func
;
2545 /* FIXME: should use the history table to make things faster */
2547 if (LdrFindEntryForAddress( (void *)pc
, &module
))
2549 WARN( "module not found for %lx\n", pc
);
2552 if (!(func
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
2553 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
2555 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
2558 func
= find_function_info( pc
, module
->BaseAddress
, func
, size
);
2559 if (func
) *base
= (ULONG64
)module
->BaseAddress
;
2563 static ULONG64
get_int_reg( CONTEXT
*context
, int reg
)
2565 return *(&context
->Rax
+ reg
);
2568 static void set_int_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, ULONG64 val
)
2570 *(&context
->Rax
+ reg
) = val
;
2571 if (ctx_ptr
) ctx_ptr
->u2
.IntegerContext
[reg
] = &context
->Rax
+ reg
;
2574 static void set_float_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, M128A val
)
2576 *(&context
->u
.s
.Xmm0
+ reg
) = val
;
2577 if (ctx_ptr
) ctx_ptr
->u1
.FloatingContext
[reg
] = &context
->u
.s
.Xmm0
+ reg
;
2580 static int get_opcode_size( struct opcode op
)
2584 case UWOP_ALLOC_LARGE
:
2585 return 2 + (op
.info
!= 0);
2586 case UWOP_SAVE_NONVOL
:
2587 case UWOP_SAVE_XMM128
:
2589 case UWOP_SAVE_NONVOL_FAR
:
2590 case UWOP_SAVE_XMM128_FAR
:
2597 static BOOL
is_inside_epilog( BYTE
*pc
, ULONG64 base
, const RUNTIME_FUNCTION
*function
)
2599 /* add or lea must be the first instruction, and it must have a rex.W prefix */
2600 if ((pc
[0] & 0xf8) == 0x48)
2604 case 0x81: /* add $nnnn,%rsp */
2605 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
2611 case 0x83: /* add $n,%rsp */
2612 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
2618 case 0x8d: /* lea n(reg),%rsp */
2619 if (pc
[0] & 0x06) return FALSE
; /* rex.RX must be cleared */
2620 if (((pc
[2] >> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
2621 if ((pc
[2] & 7) == 4) return FALSE
; /* no SIB byte allowed */
2622 if ((pc
[2] >> 6) == 1) /* 8-bit offset */
2627 if ((pc
[2] >> 6) == 2) /* 32-bit offset */
2636 /* now check for various pop instructions */
2640 if ((*pc
& 0xf0) == 0x40) pc
++; /* rex prefix */
2644 case 0x58: /* pop %rax/%r8 */
2645 case 0x59: /* pop %rcx/%r9 */
2646 case 0x5a: /* pop %rdx/%r10 */
2647 case 0x5b: /* pop %rbx/%r11 */
2648 case 0x5c: /* pop %rsp/%r12 */
2649 case 0x5d: /* pop %rbp/%r13 */
2650 case 0x5e: /* pop %rsi/%r14 */
2651 case 0x5f: /* pop %rdi/%r15 */
2654 case 0xc2: /* ret $nn */
2655 case 0xc3: /* ret */
2657 case 0xe9: /* jmp nnnn */
2658 pc
+= 5 + *(LONG
*)(pc
+ 1);
2659 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
2662 case 0xeb: /* jmp n */
2663 pc
+= 2 + (signed char)pc
[1];
2664 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
2667 case 0xf3: /* rep; ret (for amd64 prediction bug) */
2668 return pc
[1] == 0xc3;
2674 /* execute a function epilog, which must have been validated with is_inside_epilog() */
2675 static void interpret_epilog( BYTE
*pc
, CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
2681 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
2685 case 0x58: /* pop %rax/r8 */
2686 case 0x59: /* pop %rcx/r9 */
2687 case 0x5a: /* pop %rdx/r10 */
2688 case 0x5b: /* pop %rbx/r11 */
2689 case 0x5c: /* pop %rsp/r12 */
2690 case 0x5d: /* pop %rbp/r13 */
2691 case 0x5e: /* pop %rsi/r14 */
2692 case 0x5f: /* pop %rdi/r15 */
2693 set_int_reg( context
, ctx_ptr
, *pc
- 0x58 + (rex
& 1) * 8, *(ULONG64
*)context
->Rsp
);
2694 context
->Rsp
+= sizeof(ULONG64
);
2697 case 0x81: /* add $nnnn,%rsp */
2698 context
->Rsp
+= *(LONG
*)(pc
+ 2);
2699 pc
+= 2 + sizeof(LONG
);
2701 case 0x83: /* add $n,%rsp */
2702 context
->Rsp
+= (signed char)pc
[2];
2706 if ((pc
[1] >> 6) == 1) /* lea n(reg),%rsp */
2708 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + (signed char)pc
[2];
2711 else /* lea nnnn(reg),%rsp */
2713 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + *(LONG
*)(pc
+ 2);
2714 pc
+= 2 + sizeof(LONG
);
2717 case 0xc2: /* ret $nn */
2718 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2719 context
->Rsp
+= sizeof(ULONG64
) + *(WORD
*)(pc
+ 1);
2721 case 0xc3: /* ret */
2722 case 0xf3: /* rep; ret */
2723 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2724 context
->Rsp
+= sizeof(ULONG64
);
2726 case 0xe9: /* jmp nnnn */
2727 pc
+= 5 + *(LONG
*)(pc
+ 1);
2729 case 0xeb: /* jmp n */
2730 pc
+= 2 + (signed char)pc
[1];
2737 /**********************************************************************
2738 * RtlVirtualUnwind (NTDLL.@)
2740 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG64 base
, ULONG64 pc
,
2741 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
2742 PVOID
*data
, ULONG64
*frame_ret
,
2743 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
2745 union handler_data
*handler_data
;
2747 struct UNWIND_INFO
*info
;
2748 unsigned int i
, prolog_offset
;
2750 TRACE( "type %x rip %lx rsp %lx\n", type
, pc
, context
->Rsp
);
2751 if (TRACE_ON(seh
)) dump_unwind_info( base
, function
);
2753 frame
= *frame_ret
= context
->Rsp
;
2756 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
2757 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
2759 if (info
->version
!= 1)
2761 FIXME( "unknown unwind info version %u at %p\n", info
->version
, info
);
2765 if (info
->frame_reg
)
2766 frame
= get_int_reg( context
, info
->frame_reg
) - info
->frame_offset
* 16;
2768 /* check if in prolog */
2769 if (pc
>= base
+ function
->BeginAddress
&& pc
< base
+ function
->BeginAddress
+ info
->prolog
)
2771 prolog_offset
= pc
- base
- function
->BeginAddress
;
2776 if (is_inside_epilog( (BYTE
*)pc
, base
, function
))
2778 interpret_epilog( (BYTE
*)pc
, context
, ctx_ptr
);
2784 for (i
= 0; i
< info
->count
; i
+= get_opcode_size(info
->opcodes
[i
]))
2786 if (prolog_offset
< info
->opcodes
[i
].offset
) continue; /* skip it */
2788 switch (info
->opcodes
[i
].code
)
2790 case UWOP_PUSH_NONVOL
: /* pushq %reg */
2791 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)context
->Rsp
);
2792 context
->Rsp
+= sizeof(ULONG64
);
2794 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
2795 if (info
->opcodes
[i
].info
) context
->Rsp
+= *(DWORD
*)&info
->opcodes
[i
+1];
2796 else context
->Rsp
+= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
2798 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
2799 context
->Rsp
+= (info
->opcodes
[i
].info
+ 1) * 8;
2801 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
2802 context
->Rsp
= *frame_ret
= frame
;
2804 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
2805 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 8;
2806 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)off
);
2808 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
2809 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
2810 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)off
);
2812 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
2813 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 16;
2814 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(M128A
*)off
);
2816 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
2817 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
2818 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(M128A
*)off
);
2820 case UWOP_PUSH_MACHFRAME
:
2821 FIXME( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
2824 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
2829 if (!(info
->flags
& UNW_FLAG_CHAININFO
)) break;
2830 function
= &handler_data
->chain
; /* restart with the chained info */
2833 /* now pop return address */
2834 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2835 context
->Rsp
+= sizeof(ULONG64
);
2837 if (!(info
->flags
& type
)) return NULL
; /* no matching handler */
2838 if (prolog_offset
!= ~0) return NULL
; /* inside prolog */
2840 *data
= &handler_data
->handler
+ 1;
2841 return (char *)base
+ handler_data
->handler
;
2845 /**********************************************************************
2846 * call_unwind_handler
2848 * Call a single unwind handler.
2849 * FIXME: Handle nested exceptions.
2851 static void call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
2855 dispatch
->ControlPc
= dispatch
->ContextRecord
->Rip
;
2857 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2858 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2859 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2860 TRACE( "handler %p returned %x\n", dispatch
->LanguageHandler
, res
);
2864 case ExceptionContinueSearch
:
2866 case ExceptionCollidedUnwind
:
2867 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2870 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
2876 /**********************************************************************
2877 * call_teb_unwind_handler
2879 * Call a single unwind handler from the TEB chain.
2880 * FIXME: Handle nested exceptions.
2882 static void call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
2883 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
2885 EXCEPTION_REGISTRATION_RECORD
*dispatcher
;
2888 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2889 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
2890 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
2891 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
2895 case ExceptionContinueSearch
:
2897 case ExceptionCollidedUnwind
:
2898 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2901 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
2907 /*******************************************************************
2908 * RtlUnwindEx (NTDLL.@)
2910 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
2911 PVOID retval
, CONTEXT
*context
, UNWIND_HISTORY_TABLE
*table
)
2913 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2914 EXCEPTION_RECORD record
;
2915 DISPATCHER_CONTEXT dispatch
;
2916 CONTEXT new_context
;
2921 RtlCaptureContext( context
);
2922 new_context
= *context
;
2924 /* build an exception record, if we do not have one */
2927 record
.ExceptionCode
= STATUS_UNWIND
;
2928 record
.ExceptionFlags
= 0;
2929 record
.ExceptionRecord
= NULL
;
2930 record
.ExceptionAddress
= (void *)context
->Rip
;
2931 record
.NumberParameters
= 0;
2935 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
2937 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
2938 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, context
->Rip
);
2939 for (i
= 0; i
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); i
++)
2940 TRACE( " info[%d]=%016lx\n", i
, rec
->ExceptionInformation
[i
] );
2941 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2942 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
2943 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2944 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
2945 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2946 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
2947 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2948 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
2950 dispatch
.EstablisherFrame
= context
->Rsp
;
2951 dispatch
.TargetIp
= (ULONG64
)target_ip
;
2952 dispatch
.ContextRecord
= context
;
2953 dispatch
.HistoryTable
= table
;
2957 /* FIXME: should use the history table to make things faster */
2960 dispatch
.ImageBase
= 0;
2961 dispatch
.ScopeIndex
= 0; /* FIXME */
2963 /* first look for PE exception information */
2965 if (!LdrFindEntryForAddress( (void *)context
->Rip
, &module
))
2967 RUNTIME_FUNCTION
*dir
;
2969 dispatch
.ImageBase
= (ULONG64
)module
->BaseAddress
;
2970 if ((dir
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
2971 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
2973 if ((dispatch
.FunctionEntry
= find_function_info( context
->Rip
, module
->BaseAddress
,
2976 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_UHANDLER
, dispatch
.ImageBase
,
2977 context
->Rip
, dispatch
.FunctionEntry
,
2978 &new_context
, &dispatch
.HandlerData
,
2979 &dispatch
.EstablisherFrame
, NULL
);
2983 else if (!(module
->Flags
& LDR_WINE_INTERNAL
))
2984 WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
2987 /* then look for host system exception information */
2989 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
2991 struct dwarf_eh_bases bases
;
2992 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
->Rip
- 1), &bases
);
2996 dispatch
.FunctionEntry
= NULL
;
2997 status
= dwarf_virtual_unwind( context
->Rip
, &dispatch
.EstablisherFrame
, &new_context
, fde
,
2998 &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2999 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
3000 if (dispatch
.LanguageHandler
&& !module
)
3002 FIXME( "calling personality routine in system library not supported yet\n" );
3003 dispatch
.LanguageHandler
= NULL
;
3009 /* no exception information, treat as a leaf function */
3011 new_context
.Rip
= *(ULONG64
*)context
->Rsp
;
3012 new_context
.Rsp
= context
->Rsp
+ sizeof(ULONG64
);
3013 dispatch
.EstablisherFrame
= new_context
.Rsp
;
3014 dispatch
.LanguageHandler
= NULL
;
3017 if (!dispatch
.EstablisherFrame
) break;
3019 if (is_inside_signal_stack( (void *)dispatch
.EstablisherFrame
))
3021 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch
.EstablisherFrame
,
3022 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size
);
3023 *context
= new_context
;
3027 if ((dispatch
.EstablisherFrame
& 7) ||
3028 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
3029 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
3031 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
3032 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
3033 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
3037 if (dispatch
.LanguageHandler
)
3039 if (end_frame
&& (dispatch
.EstablisherFrame
> (ULONG64
)end_frame
))
3041 ERR( "invalid end frame %lx/%p\n", dispatch
.EstablisherFrame
, end_frame
);
3042 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
3044 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) rec
->ExceptionFlags
|= EH_TARGET_UNWIND
;
3045 call_unwind_handler( rec
, &dispatch
);
3047 else /* hack: call builtin handlers registered in the tib list */
3049 while ((ULONG64
)teb_frame
< new_context
.Rsp
&& (ULONG64
)teb_frame
< (ULONG64
)end_frame
)
3051 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
3052 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
3053 call_teb_unwind_handler( rec
, &dispatch
, teb_frame
);
3054 teb_frame
= __wine_pop_frame( teb_frame
);
3056 if ((ULONG64
)teb_frame
== (ULONG64
)end_frame
&& (ULONG64
)end_frame
< new_context
.Rsp
) break;
3057 dispatch
.EstablisherFrame
= new_context
.Rsp
;
3060 if (context
->Rsp
== (ULONG64
)end_frame
) break;
3061 *context
= new_context
;
3064 if (rec
->ExceptionCode
== STATUS_LONGJUMP
&& rec
->NumberParameters
>= 1)
3066 struct MSVCRT_JUMP_BUFFER
*jmp
= (struct MSVCRT_JUMP_BUFFER
*)rec
->ExceptionInformation
[0];
3067 context
->Rbx
= jmp
->Rbx
;
3068 context
->Rsp
= jmp
->Rsp
;
3069 context
->Rbp
= jmp
->Rbp
;
3070 context
->Rsi
= jmp
->Rsi
;
3071 context
->Rdi
= jmp
->Rdi
;
3072 context
->R12
= jmp
->R12
;
3073 context
->R13
= jmp
->R13
;
3074 context
->R14
= jmp
->R14
;
3075 context
->R15
= jmp
->R15
;
3076 context
->Rip
= jmp
->Rip
;
3077 context
->u
.s
.Xmm6
= jmp
->Xmm6
;
3078 context
->u
.s
.Xmm7
= jmp
->Xmm7
;
3079 context
->u
.s
.Xmm8
= jmp
->Xmm8
;
3080 context
->u
.s
.Xmm9
= jmp
->Xmm9
;
3081 context
->u
.s
.Xmm10
= jmp
->Xmm10
;
3082 context
->u
.s
.Xmm11
= jmp
->Xmm11
;
3083 context
->u
.s
.Xmm12
= jmp
->Xmm12
;
3084 context
->u
.s
.Xmm13
= jmp
->Xmm13
;
3085 context
->u
.s
.Xmm14
= jmp
->Xmm14
;
3086 context
->u
.s
.Xmm15
= jmp
->Xmm15
;
3088 else if (rec
->ExceptionCode
== STATUS_UNWIND_CONSOLIDATE
&& rec
->NumberParameters
>= 1)
3090 PVOID (CALLBACK
*consolidate
)(EXCEPTION_RECORD
*) = (void *)rec
->ExceptionInformation
[0];
3091 TRACE( "calling consolidate callback %p\n", consolidate
);
3092 target_ip
= consolidate( rec
);
3094 context
->Rax
= (ULONG64
)retval
;
3095 context
->Rip
= (ULONG64
)target_ip
;
3096 TRACE( "returning to %lx stack %lx\n", context
->Rip
, context
->Rsp
);
3097 set_cpu_context( context
);
3101 /*******************************************************************
3102 * RtlUnwind (NTDLL.@)
3104 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
3107 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
3111 /*******************************************************************
3112 * _local_unwind (NTDLL.@)
3114 void WINAPI
_local_unwind( void *frame
, void *target_ip
)
3117 RtlUnwindEx( frame
, target_ip
, NULL
, NULL
, &context
, NULL
);
3121 /*******************************************************************
3122 * __C_specific_handler (NTDLL.@)
3124 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
3127 struct _DISPATCHER_CONTEXT
*dispatch
)
3129 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
3132 TRACE( "%p %lx %p %p\n", rec
, frame
, context
, dispatch
);
3133 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
3135 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)) /* FIXME */
3136 return ExceptionContinueSearch
;
3138 for (i
= 0; i
< table
->Count
; i
++)
3140 if (context
->Rip
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
3141 context
->Rip
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
3143 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
3144 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
3146 EXCEPTION_POINTERS ptrs
;
3147 PC_LANGUAGE_EXCEPTION_HANDLER filter
;
3149 filter
= (PC_LANGUAGE_EXCEPTION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
3150 ptrs
.ExceptionRecord
= rec
;
3151 ptrs
.ContextRecord
= context
;
3152 TRACE( "calling filter %p ptrs %p frame %lx\n", filter
, &ptrs
, frame
);
3153 switch (filter( &ptrs
, frame
))
3155 case EXCEPTION_EXECUTE_HANDLER
:
3157 case EXCEPTION_CONTINUE_SEARCH
:
3159 case EXCEPTION_CONTINUE_EXECUTION
:
3160 return ExceptionContinueExecution
;
3163 TRACE( "unwinding to target %lx\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
3164 RtlUnwindEx( (void *)frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
3165 rec
, 0, context
, dispatch
->HistoryTable
);
3168 return ExceptionContinueSearch
;
3172 /*******************************************************************
3173 * NtRaiseException (NTDLL.@)
3175 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
3177 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
3178 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
3183 /***********************************************************************
3184 * RtlRaiseException (NTDLL.@)
3186 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
3190 rec
->ExceptionAddress
= (void *)context
->Rip
;
3191 status
= raise_exception( rec
, context
, TRUE
);
3192 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
3194 __ASM_GLOBAL_FUNC( RtlRaiseException
,
3195 "movq %rcx,8(%rsp)\n\t"
3196 "sub $0x4f8,%rsp\n\t"
3197 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
3198 "leaq 0x20(%rsp),%rcx\n\t"
3199 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
3200 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
3201 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
3202 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
3203 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
3204 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
3205 "movq (%rax),%rcx\n\t" /* original first parameter */
3206 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
3207 "call " __ASM_NAME("__regs_RtlRaiseException") "\n\t"
3208 "leaq 0x20(%rsp),%rdi\n\t" /* context pointer */
3209 "call " __ASM_NAME("set_cpu_context") /* does not return */ );
3212 /*************************************************************************
3213 * RtlCaptureStackBackTrace (NTDLL.@)
3215 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
3217 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
3222 /***********************************************************************
3225 void call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
, void *frame
)
3227 ntdll_get_thread_data()->exit_frame
= frame
;
3230 RtlExitUserThread( entry( arg
));
3232 __EXCEPT(unhandled_exception_filter
)
3234 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
3237 abort(); /* should not be reached */
3240 extern void DECLSPEC_NORETURN
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
);
3241 __ASM_GLOBAL_FUNC( call_thread_entry_point
,
3243 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3244 "movq %rbp,48(%rsp)\n\t"
3245 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3246 "movq %rbx,40(%rsp)\n\t"
3247 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3248 "movq %r12,32(%rsp)\n\t"
3249 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3250 "movq %r13,24(%rsp)\n\t"
3251 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3252 "movq %r14,16(%rsp)\n\t"
3253 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3254 "movq %r15,8(%rsp)\n\t"
3255 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3256 "movq %rsp,%rdx\n\t"
3257 "call " __ASM_NAME("call_thread_func") );
3259 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int), void *frame
);
3260 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
3261 "movq %rdx,%rsp\n\t"
3262 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3263 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3264 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3265 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3266 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3267 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3268 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3271 /***********************************************************************
3272 * RtlExitUserThread (NTDLL.@)
3274 void WINAPI
RtlExitUserThread( ULONG status
)
3276 if (!ntdll_get_thread_data()->exit_frame
) exit_thread( status
);
3277 call_thread_exit_func( status
, exit_thread
, ntdll_get_thread_data()->exit_frame
);
3280 /***********************************************************************
3283 void abort_thread( int status
)
3285 if (!ntdll_get_thread_data()->exit_frame
) terminate_thread( status
);
3286 call_thread_exit_func( status
, terminate_thread
, ntdll_get_thread_data()->exit_frame
);
3289 /**********************************************************************
3290 * __wine_enter_vm86 (NTDLL.@)
3292 void __wine_enter_vm86( CONTEXT
*context
)
3294 MESSAGE("vm86 mode not supported on this platform\n");
3297 /**********************************************************************
3298 * DbgBreakPoint (NTDLL.@)
3300 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
3302 /**********************************************************************
3303 * DbgUserBreakPoint (NTDLL.@)
3305 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
3307 #endif /* __x86_64__ */