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 /***********************************************************************
104 * signal context platform-specific definitions
108 #include <asm/prctl.h>
109 extern int arch_prctl(int func
, void *ptr
);
111 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
112 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
113 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
114 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
115 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
116 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
117 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
118 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
119 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
120 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
121 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
122 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
123 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
124 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
125 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
127 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
128 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
129 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
131 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
132 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
133 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
134 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
135 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
137 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
139 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
140 #include <sys/ucontext.h>
142 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
143 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
144 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
145 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
146 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
147 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
148 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
149 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
150 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
151 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
152 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
153 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
154 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
155 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
156 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
158 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
159 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
160 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
161 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
162 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
163 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
165 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
167 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
168 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
169 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
170 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
172 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
174 #elif defined(__NetBSD__)
175 #include <sys/ucontext.h>
176 #include <sys/types.h>
179 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
180 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
181 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
182 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
183 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
184 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
185 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
186 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
187 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
188 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
189 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
190 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
191 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
192 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
193 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
195 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
196 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
197 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
198 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
199 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
200 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
202 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
204 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
205 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
207 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
208 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
210 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
212 #error You must define the signal context functions for your platform
217 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
218 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
219 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
220 TRAP_x86_NMI
= 2, /* NMI interrupt */
221 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
222 TRAP_x86_OFLOW
= 4, /* Overflow exception */
223 TRAP_x86_BOUND
= 5, /* Bound range exception */
224 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
225 TRAP_x86_DNA
= 7, /* Device not available exception */
226 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
227 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
228 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
229 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
230 TRAP_x86_STKFLT
= 12, /* Stack fault */
231 TRAP_x86_PROTFLT
= 13, /* General protection fault */
232 TRAP_x86_PAGEFLT
= 14, /* Page fault */
233 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
234 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
235 TRAP_x86_MCHK
= 18, /* Machine check exception */
236 TRAP_x86_CACHEFLT
= 19 /* Cache flush exception */
239 static const size_t teb_size
= 0x2000; /* we reserve two pages for the TEB */
240 static size_t signal_stack_size
;
242 typedef void (*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
243 typedef int (*wine_signal_handler
)(unsigned int sig
);
245 static wine_signal_handler handlers
[256];
248 /***********************************************************************
249 * Definitions for Win32 unwind tables
254 RUNTIME_FUNCTION chain
;
272 BYTE frame_offset
: 4;
273 struct opcode opcodes
[1]; /* info->count entries */
274 /* followed by handler_data */
277 #define UWOP_PUSH_NONVOL 0
278 #define UWOP_ALLOC_LARGE 1
279 #define UWOP_ALLOC_SMALL 2
280 #define UWOP_SET_FPREG 3
281 #define UWOP_SAVE_NONVOL 4
282 #define UWOP_SAVE_NONVOL_FAR 5
283 #define UWOP_SAVE_XMM128 8
284 #define UWOP_SAVE_XMM128_FAR 9
285 #define UWOP_PUSH_MACHFRAME 10
287 static void dump_unwind_info( ULONG64 base
, RUNTIME_FUNCTION
*function
)
289 static const char * const reg_names
[16] =
290 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
291 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
293 union handler_data
*handler_data
;
294 struct UNWIND_INFO
*info
;
295 unsigned int i
, count
;
297 TRACE( "**** func %x-%x\n", function
->BeginAddress
, function
->EndAddress
);
300 if (function
->UnwindData
& 1)
302 RUNTIME_FUNCTION
*next
= (RUNTIME_FUNCTION
*)((char *)base
+ (function
->UnwindData
& ~1));
303 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
304 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
,
305 (char *)base
+ next
->BeginAddress
, (char *)base
+ next
->EndAddress
);
309 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
311 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
312 info
, info
->flags
, info
->prolog
,
313 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
);
316 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
317 reg_names
[info
->frame_reg
], info
->frame_offset
* 16 );
319 for (i
= 0; i
< info
->count
; i
++)
321 TRACE( " 0x%x: ", info
->opcodes
[i
].offset
);
322 switch (info
->opcodes
[i
].code
)
324 case UWOP_PUSH_NONVOL
:
325 TRACE( "pushq %%%s\n", reg_names
[info
->opcodes
[i
].info
] );
327 case UWOP_ALLOC_LARGE
:
328 if (info
->opcodes
[i
].info
)
330 count
= *(DWORD
*)&info
->opcodes
[i
+1];
335 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
338 TRACE( "subq $0x%x,%%rsp\n", count
);
340 case UWOP_ALLOC_SMALL
:
341 count
= (info
->opcodes
[i
].info
+ 1) * 8;
342 TRACE( "subq $0x%x,%%rsp\n", count
);
345 TRACE( "leaq 0x%x(%%rsp),%s\n",
346 info
->frame_offset
* 16, reg_names
[info
->frame_reg
] );
348 case UWOP_SAVE_NONVOL
:
349 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
350 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
353 case UWOP_SAVE_NONVOL_FAR
:
354 count
= *(DWORD
*)&info
->opcodes
[i
+1];
355 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
358 case UWOP_SAVE_XMM128
:
359 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 16;
360 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
363 case UWOP_SAVE_XMM128_FAR
:
364 count
= *(DWORD
*)&info
->opcodes
[i
+1];
365 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
368 case UWOP_PUSH_MACHFRAME
:
369 TRACE( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
372 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
377 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
378 if (info
->flags
& UNW_FLAG_CHAININFO
)
380 TRACE( " chained to function %p-%p\n",
381 (char *)base
+ handler_data
->chain
.BeginAddress
,
382 (char *)base
+ handler_data
->chain
.EndAddress
);
383 function
= &handler_data
->chain
;
386 if (info
->flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
387 TRACE( " handler %p data at %p\n",
388 (char *)base
+ handler_data
->handler
, &handler_data
->handler
+ 1 );
393 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
397 TRACE( "scope table at %p\n", table
);
398 for (i
= 0; i
< table
->Count
; i
++)
399 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i
,
400 base
+ table
->ScopeRecord
[i
].BeginAddress
,
401 base
+ table
->ScopeRecord
[i
].EndAddress
,
402 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
403 base
+ table
->ScopeRecord
[i
].JumpTarget
);
407 /***********************************************************************
408 * Definitions for Dwarf unwind tables
411 enum dwarf_call_frame_info
413 DW_CFA_advance_loc
= 0x40,
414 DW_CFA_offset
= 0x80,
415 DW_CFA_restore
= 0xc0,
417 DW_CFA_set_loc
= 0x01,
418 DW_CFA_advance_loc1
= 0x02,
419 DW_CFA_advance_loc2
= 0x03,
420 DW_CFA_advance_loc4
= 0x04,
421 DW_CFA_offset_extended
= 0x05,
422 DW_CFA_restore_extended
= 0x06,
423 DW_CFA_undefined
= 0x07,
424 DW_CFA_same_value
= 0x08,
425 DW_CFA_register
= 0x09,
426 DW_CFA_remember_state
= 0x0a,
427 DW_CFA_restore_state
= 0x0b,
428 DW_CFA_def_cfa
= 0x0c,
429 DW_CFA_def_cfa_register
= 0x0d,
430 DW_CFA_def_cfa_offset
= 0x0e,
431 DW_CFA_def_cfa_expression
= 0x0f,
432 DW_CFA_expression
= 0x10,
433 DW_CFA_offset_extended_sf
= 0x11,
434 DW_CFA_def_cfa_sf
= 0x12,
435 DW_CFA_def_cfa_offset_sf
= 0x13,
436 DW_CFA_val_offset
= 0x14,
437 DW_CFA_val_offset_sf
= 0x15,
438 DW_CFA_val_expression
= 0x16,
445 DW_OP_const1u
= 0x08,
446 DW_OP_const1s
= 0x09,
447 DW_OP_const2u
= 0x0a,
448 DW_OP_const2s
= 0x0b,
449 DW_OP_const4u
= 0x0c,
450 DW_OP_const4s
= 0x0d,
451 DW_OP_const8u
= 0x0e,
452 DW_OP_const8s
= 0x0f,
472 DW_OP_plus_uconst
= 0x23,
585 DW_OP_deref_size
= 0x94,
586 DW_OP_xderef_size
= 0x95,
588 DW_OP_push_object_address
= 0x97,
591 DW_OP_call_ref
= 0x9a,
592 DW_OP_form_tls_address
= 0x9b,
593 DW_OP_call_frame_cfa
= 0x9c,
594 DW_OP_bit_piece
= 0x9d,
595 DW_OP_lo_user
= 0xe0,
596 DW_OP_hi_user
= 0xff,
597 DW_OP_GNU_push_tls_address
= 0xe0,
598 DW_OP_GNU_uninit
= 0xf0,
599 DW_OP_GNU_encoded_addr
= 0xf1,
602 #define DW_EH_PE_native 0x00
603 #define DW_EH_PE_leb128 0x01
604 #define DW_EH_PE_data2 0x02
605 #define DW_EH_PE_data4 0x03
606 #define DW_EH_PE_data8 0x04
607 #define DW_EH_PE_signed 0x08
608 #define DW_EH_PE_abs 0x00
609 #define DW_EH_PE_pcrel 0x10
610 #define DW_EH_PE_textrel 0x20
611 #define DW_EH_PE_datarel 0x30
612 #define DW_EH_PE_funcrel 0x40
613 #define DW_EH_PE_aligned 0x50
614 #define DW_EH_PE_indirect 0x80
615 #define DW_EH_PE_omit 0xff
617 struct dwarf_eh_bases
628 unsigned char version
;
629 unsigned char augmentation
[1];
635 unsigned int cie_offset
;
638 extern const struct dwarf_fde
*_Unwind_Find_FDE (void *, struct dwarf_eh_bases
*);
640 static unsigned char dwarf_get_u1( const unsigned char **p
)
645 static unsigned short dwarf_get_u2( const unsigned char **p
)
647 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8);
652 static unsigned int dwarf_get_u4( const unsigned char **p
)
654 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8) | ((*p
)[2] << 16) | ((*p
)[3] << 24);
659 static ULONG64
dwarf_get_u8( const unsigned char **p
)
661 ULONG64 low
= dwarf_get_u4( p
);
662 ULONG64 high
= dwarf_get_u4( p
);
663 return low
| (high
<< 32);
666 static ULONG_PTR
dwarf_get_uleb128( const unsigned char **p
)
669 unsigned int shift
= 0;
675 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
678 } while (byte
& 0x80);
682 static LONG_PTR
dwarf_get_sleb128( const unsigned char **p
)
685 unsigned int shift
= 0;
691 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
694 } while (byte
& 0x80);
696 if ((shift
< 8 * sizeof(ret
)) && (byte
& 0x40)) ret
|= -((ULONG_PTR
)1 << shift
);
700 static ULONG_PTR
dwarf_get_ptr( const unsigned char **p
, unsigned char encoding
)
704 if (encoding
== DW_EH_PE_omit
) return 0;
706 switch (encoding
& 0xf0)
712 base
= (ULONG_PTR
)*p
;
715 FIXME( "unsupported encoding %02x\n", encoding
);
719 switch (encoding
& 0x0f)
721 case DW_EH_PE_native
:
722 return base
+ dwarf_get_u8( p
);
723 case DW_EH_PE_leb128
:
724 return base
+ dwarf_get_uleb128( p
);
726 return base
+ dwarf_get_u2( p
);
728 return base
+ dwarf_get_u4( p
);
730 return base
+ dwarf_get_u8( p
);
731 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
732 return base
+ dwarf_get_sleb128( p
);
733 case DW_EH_PE_signed
|DW_EH_PE_data2
:
734 return base
+ (signed short)dwarf_get_u2( p
);
735 case DW_EH_PE_signed
|DW_EH_PE_data4
:
736 return base
+ (signed int)dwarf_get_u4( p
);
737 case DW_EH_PE_signed
|DW_EH_PE_data8
:
738 return base
+ (LONG64
)dwarf_get_u8( p
);
740 FIXME( "unsupported encoding %02x\n", encoding
);
747 RULE_UNSET
, /* not set at all */
748 RULE_UNDEFINED
, /* undefined value */
749 RULE_SAME
, /* same value as previous frame */
750 RULE_CFA_OFFSET
, /* stored at cfa offset */
751 RULE_OTHER_REG
, /* stored in other register */
752 RULE_EXPRESSION
, /* address specified by expression */
753 RULE_VAL_EXPRESSION
/* value specified by expression */
756 #define NB_FRAME_REGS 41
757 #define MAX_SAVED_STATES 16
761 ULONG_PTR cfa_offset
;
762 unsigned char cfa_reg
;
763 enum reg_rule cfa_rule
;
764 enum reg_rule rules
[NB_FRAME_REGS
];
765 ULONG64 regs
[NB_FRAME_REGS
];
771 ULONG_PTR code_align
;
773 unsigned char retaddr_reg
;
774 unsigned char fde_encoding
;
775 unsigned char signal_frame
;
776 unsigned char state_sp
;
777 struct frame_state state
;
778 struct frame_state
*state_stack
;
781 static const char *dwarf_reg_names
[NB_FRAME_REGS
] =
783 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
784 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
785 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
786 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
787 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
790 static int valid_reg( ULONG_PTR reg
)
792 if (reg
>= NB_FRAME_REGS
) FIXME( "unsupported reg %lx\n", reg
);
793 return (reg
< NB_FRAME_REGS
);
796 static void execute_cfa_instructions( const unsigned char *ptr
, const unsigned char *end
,
797 ULONG_PTR last_ip
, struct frame_info
*info
)
799 while (ptr
< end
&& info
->ip
< last_ip
+ info
->signal_frame
)
801 enum dwarf_call_frame_info op
= *ptr
++;
807 case DW_CFA_advance_loc
:
809 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
810 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
816 ULONG_PTR reg
= op
& 0x3f;
817 LONG_PTR offset
= dwarf_get_uleb128( &ptr
) * info
->data_align
;
818 if (!valid_reg( reg
)) break;
819 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
820 info
->state
.regs
[reg
] = offset
;
821 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
826 ULONG_PTR reg
= op
& 0x3f;
827 if (!valid_reg( reg
)) break;
828 TRACE( "%lx: DW_CFA_restore %s\n", info
->ip
, dwarf_reg_names
[reg
] );
829 info
->state
.rules
[reg
] = RULE_UNSET
;
840 ULONG_PTR loc
= dwarf_get_ptr( &ptr
, info
->fde_encoding
);
841 TRACE( "%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
845 case DW_CFA_advance_loc1
:
847 ULONG_PTR offset
= *ptr
++ * info
->code_align
;
848 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
852 case DW_CFA_advance_loc2
:
854 ULONG_PTR offset
= dwarf_get_u2( &ptr
) * info
->code_align
;
855 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
859 case DW_CFA_advance_loc4
:
861 ULONG_PTR offset
= dwarf_get_u4( &ptr
) * info
->code_align
;
862 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
866 case DW_CFA_offset_extended
:
867 case DW_CFA_offset_extended_sf
:
869 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
870 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf_get_uleb128( &ptr
) * info
->data_align
871 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
872 if (!valid_reg( reg
)) break;
873 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
874 info
->state
.regs
[reg
] = offset
;
875 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
878 case DW_CFA_restore_extended
:
880 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
881 if (!valid_reg( reg
)) break;
882 TRACE( "%lx: DW_CFA_restore_extended %s\n", info
->ip
, dwarf_reg_names
[reg
] );
883 info
->state
.rules
[reg
] = RULE_UNSET
;
886 case DW_CFA_undefined
:
888 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
889 if (!valid_reg( reg
)) break;
890 TRACE( "%lx: DW_CFA_undefined %s\n", info
->ip
, dwarf_reg_names
[reg
] );
891 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
894 case DW_CFA_same_value
:
896 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
897 if (!valid_reg( reg
)) break;
898 TRACE( "%lx: DW_CFA_same_value %s\n", info
->ip
, dwarf_reg_names
[reg
] );
899 info
->state
.regs
[reg
] = reg
;
900 info
->state
.rules
[reg
] = RULE_SAME
;
903 case DW_CFA_register
:
905 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
906 ULONG_PTR reg2
= dwarf_get_uleb128( &ptr
);
907 if (!valid_reg( reg
) || !valid_reg( reg2
)) break;
908 TRACE( "%lx: DW_CFA_register %s == %s\n", info
->ip
, dwarf_reg_names
[reg
], dwarf_reg_names
[reg2
] );
909 info
->state
.regs
[reg
] = reg2
;
910 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
913 case DW_CFA_remember_state
:
914 TRACE( "%lx: DW_CFA_remember_state\n", info
->ip
);
915 if (info
->state_sp
>= MAX_SAVED_STATES
)
916 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
918 info
->state_stack
[info
->state_sp
++] = info
->state
;
920 case DW_CFA_restore_state
:
921 TRACE( "%lx: DW_CFA_restore_state\n", info
->ip
);
923 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
925 info
->state
= info
->state_stack
[--info
->state_sp
];
928 case DW_CFA_def_cfa_sf
:
930 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
931 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf_get_uleb128( &ptr
)
932 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
933 if (!valid_reg( reg
)) break;
934 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
935 info
->state
.cfa_reg
= reg
;
936 info
->state
.cfa_offset
= offset
;
937 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
940 case DW_CFA_def_cfa_register
:
942 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
943 if (!valid_reg( reg
)) break;
944 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info
->ip
, dwarf_reg_names
[reg
] );
945 info
->state
.cfa_reg
= reg
;
946 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
949 case DW_CFA_def_cfa_offset
:
950 case DW_CFA_def_cfa_offset_sf
:
952 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf_get_uleb128( &ptr
)
953 : dwarf_get_sleb128( &ptr
) * info
->data_align
;
954 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info
->ip
, offset
);
955 info
->state
.cfa_offset
= offset
;
956 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
959 case DW_CFA_def_cfa_expression
:
961 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
962 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
963 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
964 info
->state
.cfa_offset
= expr
;
965 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
969 case DW_CFA_expression
:
970 case DW_CFA_val_expression
:
972 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
973 ULONG_PTR expr
= (ULONG_PTR
)ptr
;
974 ULONG_PTR len
= dwarf_get_uleb128( &ptr
);
975 if (!valid_reg( reg
)) break;
976 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
977 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_", dwarf_reg_names
[reg
], expr
, expr
+len
);
978 info
->state
.regs
[reg
] = expr
;
979 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
984 FIXME( "%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
990 /* retrieve a context register from its dwarf number */
991 static void *get_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
)
995 case 0: return &context
->Rax
;
996 case 1: return &context
->Rdx
;
997 case 2: return &context
->Rcx
;
998 case 3: return &context
->Rbx
;
999 case 4: return &context
->Rsi
;
1000 case 5: return &context
->Rdi
;
1001 case 6: return &context
->Rbp
;
1002 case 7: return &context
->Rsp
;
1003 case 8: return &context
->R8
;
1004 case 9: return &context
->R9
;
1005 case 10: return &context
->R10
;
1006 case 11: return &context
->R11
;
1007 case 12: return &context
->R12
;
1008 case 13: return &context
->R13
;
1009 case 14: return &context
->R14
;
1010 case 15: return &context
->R15
;
1011 case 16: return &context
->Rip
;
1012 case 17: return &context
->u
.s
.Xmm0
;
1013 case 18: return &context
->u
.s
.Xmm1
;
1014 case 19: return &context
->u
.s
.Xmm2
;
1015 case 20: return &context
->u
.s
.Xmm3
;
1016 case 21: return &context
->u
.s
.Xmm4
;
1017 case 22: return &context
->u
.s
.Xmm5
;
1018 case 23: return &context
->u
.s
.Xmm6
;
1019 case 24: return &context
->u
.s
.Xmm7
;
1020 case 25: return &context
->u
.s
.Xmm8
;
1021 case 26: return &context
->u
.s
.Xmm9
;
1022 case 27: return &context
->u
.s
.Xmm10
;
1023 case 28: return &context
->u
.s
.Xmm11
;
1024 case 29: return &context
->u
.s
.Xmm12
;
1025 case 30: return &context
->u
.s
.Xmm13
;
1026 case 31: return &context
->u
.s
.Xmm14
;
1027 case 32: return &context
->u
.s
.Xmm15
;
1028 case 33: return &context
->u
.s
.Legacy
[0];
1029 case 34: return &context
->u
.s
.Legacy
[1];
1030 case 35: return &context
->u
.s
.Legacy
[2];
1031 case 36: return &context
->u
.s
.Legacy
[3];
1032 case 37: return &context
->u
.s
.Legacy
[4];
1033 case 38: return &context
->u
.s
.Legacy
[5];
1034 case 39: return &context
->u
.s
.Legacy
[6];
1035 case 40: return &context
->u
.s
.Legacy
[7];
1036 default: return NULL
;
1040 /* set a context register from its dwarf number */
1041 static void set_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
, void *val
)
1045 case 0: context
->Rax
= *(ULONG64
*)val
; break;
1046 case 1: context
->Rdx
= *(ULONG64
*)val
; break;
1047 case 2: context
->Rcx
= *(ULONG64
*)val
; break;
1048 case 3: context
->Rbx
= *(ULONG64
*)val
; break;
1049 case 4: context
->Rsi
= *(ULONG64
*)val
; break;
1050 case 5: context
->Rdi
= *(ULONG64
*)val
; break;
1051 case 6: context
->Rbp
= *(ULONG64
*)val
; break;
1052 case 7: context
->Rsp
= *(ULONG64
*)val
; break;
1053 case 8: context
->R8
= *(ULONG64
*)val
; break;
1054 case 9: context
->R9
= *(ULONG64
*)val
; break;
1055 case 10: context
->R10
= *(ULONG64
*)val
; break;
1056 case 11: context
->R11
= *(ULONG64
*)val
; break;
1057 case 12: context
->R12
= *(ULONG64
*)val
; break;
1058 case 13: context
->R13
= *(ULONG64
*)val
; break;
1059 case 14: context
->R14
= *(ULONG64
*)val
; break;
1060 case 15: context
->R15
= *(ULONG64
*)val
; break;
1061 case 16: context
->Rip
= *(ULONG64
*)val
; break;
1062 case 17: context
->u
.s
.Xmm0
= *(M128A
*)val
; break;
1063 case 18: context
->u
.s
.Xmm1
= *(M128A
*)val
; break;
1064 case 19: context
->u
.s
.Xmm2
= *(M128A
*)val
; break;
1065 case 20: context
->u
.s
.Xmm3
= *(M128A
*)val
; break;
1066 case 21: context
->u
.s
.Xmm4
= *(M128A
*)val
; break;
1067 case 22: context
->u
.s
.Xmm5
= *(M128A
*)val
; break;
1068 case 23: context
->u
.s
.Xmm6
= *(M128A
*)val
; break;
1069 case 24: context
->u
.s
.Xmm7
= *(M128A
*)val
; break;
1070 case 25: context
->u
.s
.Xmm8
= *(M128A
*)val
; break;
1071 case 26: context
->u
.s
.Xmm9
= *(M128A
*)val
; break;
1072 case 27: context
->u
.s
.Xmm10
= *(M128A
*)val
; break;
1073 case 28: context
->u
.s
.Xmm11
= *(M128A
*)val
; break;
1074 case 29: context
->u
.s
.Xmm12
= *(M128A
*)val
; break;
1075 case 30: context
->u
.s
.Xmm13
= *(M128A
*)val
; break;
1076 case 31: context
->u
.s
.Xmm14
= *(M128A
*)val
; break;
1077 case 32: context
->u
.s
.Xmm15
= *(M128A
*)val
; break;
1078 case 33: context
->u
.s
.Legacy
[0] = *(M128A
*)val
; break;
1079 case 34: context
->u
.s
.Legacy
[1] = *(M128A
*)val
; break;
1080 case 35: context
->u
.s
.Legacy
[2] = *(M128A
*)val
; break;
1081 case 36: context
->u
.s
.Legacy
[3] = *(M128A
*)val
; break;
1082 case 37: context
->u
.s
.Legacy
[4] = *(M128A
*)val
; break;
1083 case 38: context
->u
.s
.Legacy
[5] = *(M128A
*)val
; break;
1084 case 39: context
->u
.s
.Legacy
[6] = *(M128A
*)val
; break;
1085 case 40: context
->u
.s
.Legacy
[7] = *(M128A
*)val
; break;
1089 static ULONG_PTR
eval_expression( const unsigned char *p
, CONTEXT
*context
)
1091 ULONG_PTR reg
, tmp
, stack
[64];
1093 ULONG_PTR len
= dwarf_get_uleb128(&p
);
1094 const unsigned char *end
= p
+ len
;
1098 unsigned char opcode
= dwarf_get_u1(&p
);
1100 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
1101 stack
[++sp
] = opcode
- DW_OP_lit0
;
1102 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
1103 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_reg0
);
1104 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
1105 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, opcode
- DW_OP_breg0
) + dwarf_get_sleb128(&p
);
1106 else switch (opcode
)
1108 case DW_OP_nop
: break;
1109 case DW_OP_addr
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1110 case DW_OP_const1u
: stack
[++sp
] = dwarf_get_u1(&p
); break;
1111 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf_get_u1(&p
); break;
1112 case DW_OP_const2u
: stack
[++sp
] = dwarf_get_u2(&p
); break;
1113 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf_get_u2(&p
); break;
1114 case DW_OP_const4u
: stack
[++sp
] = dwarf_get_u4(&p
); break;
1115 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf_get_u4(&p
); break;
1116 case DW_OP_const8u
: stack
[++sp
] = dwarf_get_u8(&p
); break;
1117 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf_get_u8(&p
); break;
1118 case DW_OP_constu
: stack
[++sp
] = dwarf_get_uleb128(&p
); break;
1119 case DW_OP_consts
: stack
[++sp
] = dwarf_get_sleb128(&p
); break;
1120 case DW_OP_deref
: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1121 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
1122 case DW_OP_drop
: sp
--; break;
1123 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
1124 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf_get_u1(&p
)]; sp
++; break;
1125 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
1126 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
1127 case DW_OP_abs
: stack
[sp
] = labs(stack
[sp
]); break;
1128 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
1129 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
1130 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
1131 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
1132 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
1133 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
1134 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
1135 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
1136 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
1137 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
1138 case DW_OP_plus_uconst
: stack
[sp
] += dwarf_get_uleb128(&p
); break;
1139 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
1140 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
1141 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
1142 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
1143 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
1144 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
1145 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
1146 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
1147 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
1148 case DW_OP_skip
: tmp
= (short)dwarf_get_u2(&p
); p
+= tmp
; break;
1149 case DW_OP_bra
: tmp
= (short)dwarf_get_u2(&p
); if (!stack
[sp
--]) p
+= tmp
; break;
1150 case DW_OP_GNU_encoded_addr
: tmp
= *p
++; stack
[++sp
] = dwarf_get_ptr( &p
, tmp
); break;
1151 case DW_OP_regx
: stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, dwarf_get_uleb128(&p
) ); break;
1153 reg
= dwarf_get_uleb128(&p
);
1154 tmp
= dwarf_get_sleb128(&p
);
1155 stack
[++sp
] = *(ULONG_PTR
*)get_context_reg( context
, reg
) + tmp
;
1157 case DW_OP_deref_size
:
1160 case 1: stack
[sp
] = *(unsigned char *)stack
[sp
]; break;
1161 case 2: stack
[sp
] = *(unsigned short *)stack
[sp
]; break;
1162 case 4: stack
[sp
] = *(unsigned int *)stack
[sp
]; break;
1163 case 8: stack
[sp
] = *(ULONG_PTR
*)stack
[sp
]; break;
1167 FIXME( "unhandled opcode %02x\n", opcode
);
1173 /* apply the computed frame info to the actual context */
1174 static void apply_frame_state( CONTEXT
*context
, struct frame_state
*state
)
1177 ULONG_PTR cfa
, value
;
1178 CONTEXT new_context
= *context
;
1180 switch (state
->cfa_rule
)
1182 case RULE_EXPRESSION
:
1183 cfa
= *(ULONG_PTR
*)eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1185 case RULE_VAL_EXPRESSION
:
1186 cfa
= eval_expression( (const unsigned char *)state
->cfa_offset
, context
);
1189 cfa
= *(ULONG_PTR
*)get_context_reg( context
, state
->cfa_reg
) + state
->cfa_offset
;
1194 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
1196 switch (state
->rules
[i
])
1199 case RULE_UNDEFINED
:
1202 case RULE_CFA_OFFSET
:
1203 set_context_reg( &new_context
, i
, (char *)cfa
+ state
->regs
[i
] );
1205 case RULE_OTHER_REG
:
1206 set_context_reg( &new_context
, i
, get_context_reg( context
, state
->regs
[i
] ));
1208 case RULE_EXPRESSION
:
1209 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1210 set_context_reg( &new_context
, i
, (void *)value
);
1212 case RULE_VAL_EXPRESSION
:
1213 value
= eval_expression( (const unsigned char *)state
->regs
[i
], context
);
1214 set_context_reg( &new_context
, i
, &value
);
1218 new_context
.Rsp
= cfa
;
1219 *context
= new_context
;
1223 /***********************************************************************
1224 * dwarf_virtual_unwind
1226 * Equivalent of RtlVirtualUnwind for builtin modules.
1228 static NTSTATUS
dwarf_virtual_unwind( ULONG64 ip
, ULONG64
*frame
,CONTEXT
*context
,
1229 const struct dwarf_fde
*fde
, const struct dwarf_eh_bases
*bases
,
1230 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
1232 const struct dwarf_cie
*cie
;
1233 const unsigned char *ptr
, *augmentation
, *end
;
1234 ULONG_PTR len
, code_end
;
1235 struct frame_info info
;
1236 struct frame_state state_stack
[MAX_SAVED_STATES
];
1237 int aug_z_format
= 0;
1238 unsigned char lsda_encoding
= DW_EH_PE_omit
;
1240 memset( &info
, 0, sizeof(info
) );
1241 info
.state_stack
= state_stack
;
1242 info
.ip
= (ULONG_PTR
)bases
->func
;
1245 cie
= (const struct dwarf_cie
*)((const char *)&fde
->cie_offset
- fde
->cie_offset
);
1247 /* parse the CIE first */
1249 if (cie
->version
!= 1)
1251 FIXME( "unknown CIE version %u at %p\n", cie
->version
, cie
);
1252 return STATUS_INVALID_DISPOSITION
;
1254 ptr
= cie
->augmentation
+ strlen((const char *)cie
->augmentation
) + 1;
1256 info
.code_align
= dwarf_get_uleb128( &ptr
);
1257 info
.data_align
= dwarf_get_sleb128( &ptr
);
1258 info
.retaddr_reg
= *ptr
++;
1259 info
.state
.cfa_rule
= RULE_CFA_OFFSET
;
1261 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1262 ip
, bases
->func
, cie
, cie
->length
, cie
->id
, cie
->version
, cie
->augmentation
,
1263 info
.code_align
, info
.data_align
, dwarf_reg_names
[info
.retaddr_reg
] );
1266 for (augmentation
= cie
->augmentation
; *augmentation
; augmentation
++)
1268 switch (*augmentation
)
1271 len
= dwarf_get_uleb128( &ptr
);
1276 lsda_encoding
= *ptr
++;
1280 unsigned char encoding
= *ptr
++;
1281 *handler
= (void *)dwarf_get_ptr( &ptr
, encoding
);
1285 info
.fde_encoding
= *ptr
++;
1288 info
.signal_frame
= 1;
1291 FIXME( "unknown augmentation '%c'\n", *augmentation
);
1292 if (!end
) return STATUS_INVALID_DISPOSITION
; /* cannot continue */
1297 end
= (const unsigned char *)(&cie
->length
+ 1) + cie
->length
;
1298 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1300 ptr
= (const unsigned char *)(fde
+ 1);
1301 info
.ip
= dwarf_get_ptr( &ptr
, info
.fde_encoding
); /* fde code start */
1302 code_end
= info
.ip
+ dwarf_get_ptr( &ptr
, info
.fde_encoding
& 0x0f ); /* fde code length */
1304 if (aug_z_format
) /* get length of augmentation data */
1306 len
= dwarf_get_uleb128( &ptr
);
1311 *handler_data
= (void *)dwarf_get_ptr( &ptr
, lsda_encoding
);
1314 end
= (const unsigned char *)(&fde
->length
+ 1) + fde
->length
;
1315 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1316 fde
, fde
->length
, *handler
, *handler_data
, info
.ip
, code_end
);
1317 execute_cfa_instructions( ptr
, end
, ip
, &info
);
1318 apply_frame_state( context
, &info
.state
);
1319 *frame
= context
->Rsp
;
1321 TRACE( "next function rip=%016lx\n", context
->Rip
);
1322 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1323 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1324 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1325 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1326 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1327 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1328 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1329 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1331 return STATUS_SUCCESS
;
1335 /***********************************************************************
1338 static inline int dispatch_signal(unsigned int sig
)
1340 if (handlers
[sig
] == NULL
) return 0;
1341 return handlers
[sig
](sig
);
1344 /***********************************************************************
1347 * Get the base of the signal stack for the current thread.
1349 static inline void *get_signal_stack(void)
1351 return (char *)NtCurrentTeb() + teb_size
;
1354 /***********************************************************************
1355 * is_inside_signal_stack
1357 * Check if pointer is inside the signal stack.
1359 static inline int is_inside_signal_stack( void *ptr
)
1361 return ((char *)ptr
>= (char *)get_signal_stack() &&
1362 (char *)ptr
< (char *)get_signal_stack() + signal_stack_size
);
1365 /***********************************************************************
1368 * Set the register values from a sigcontext.
1370 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
1372 context
->ContextFlags
= CONTEXT_CONTROL
| CONTEXT_INTEGER
| CONTEXT_SEGMENTS
;
1373 context
->Rax
= RAX_sig(sigcontext
);
1374 context
->Rcx
= RCX_sig(sigcontext
);
1375 context
->Rdx
= RDX_sig(sigcontext
);
1376 context
->Rbx
= RBX_sig(sigcontext
);
1377 context
->Rsp
= RSP_sig(sigcontext
);
1378 context
->Rbp
= RBP_sig(sigcontext
);
1379 context
->Rsi
= RSI_sig(sigcontext
);
1380 context
->Rdi
= RDI_sig(sigcontext
);
1381 context
->R8
= R8_sig(sigcontext
);
1382 context
->R9
= R9_sig(sigcontext
);
1383 context
->R10
= R10_sig(sigcontext
);
1384 context
->R11
= R11_sig(sigcontext
);
1385 context
->R12
= R12_sig(sigcontext
);
1386 context
->R13
= R13_sig(sigcontext
);
1387 context
->R14
= R14_sig(sigcontext
);
1388 context
->R15
= R15_sig(sigcontext
);
1389 context
->Rip
= RIP_sig(sigcontext
);
1390 context
->SegCs
= CS_sig(sigcontext
);
1391 context
->SegFs
= FS_sig(sigcontext
);
1392 context
->SegGs
= GS_sig(sigcontext
);
1393 context
->EFlags
= EFL_sig(sigcontext
);
1395 context
->SegDs
= DS_sig(sigcontext
);
1397 __asm__("movw %%ds,%0" : "=m" (context
->SegDs
));
1400 context
->SegEs
= ES_sig(sigcontext
);
1402 __asm__("movw %%es,%0" : "=m" (context
->SegEs
));
1405 context
->SegSs
= SS_sig(sigcontext
);
1407 __asm__("movw %%ss,%0" : "=m" (context
->SegSs
));
1409 if (FPU_sig(sigcontext
))
1411 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1412 context
->u
.FltSave
= *FPU_sig(sigcontext
);
1413 context
->MxCsr
= context
->u
.FltSave
.MxCsr
;
1418 /***********************************************************************
1421 * Build a sigcontext from the register values.
1423 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1425 RAX_sig(sigcontext
) = context
->Rax
;
1426 RCX_sig(sigcontext
) = context
->Rcx
;
1427 RDX_sig(sigcontext
) = context
->Rdx
;
1428 RBX_sig(sigcontext
) = context
->Rbx
;
1429 RSP_sig(sigcontext
) = context
->Rsp
;
1430 RBP_sig(sigcontext
) = context
->Rbp
;
1431 RSI_sig(sigcontext
) = context
->Rsi
;
1432 RDI_sig(sigcontext
) = context
->Rdi
;
1433 R8_sig(sigcontext
) = context
->R8
;
1434 R9_sig(sigcontext
) = context
->R9
;
1435 R10_sig(sigcontext
) = context
->R10
;
1436 R11_sig(sigcontext
) = context
->R11
;
1437 R12_sig(sigcontext
) = context
->R12
;
1438 R13_sig(sigcontext
) = context
->R13
;
1439 R14_sig(sigcontext
) = context
->R14
;
1440 R15_sig(sigcontext
) = context
->R15
;
1441 RIP_sig(sigcontext
) = context
->Rip
;
1442 CS_sig(sigcontext
) = context
->SegCs
;
1443 FS_sig(sigcontext
) = context
->SegFs
;
1444 GS_sig(sigcontext
) = context
->SegGs
;
1445 EFL_sig(sigcontext
) = context
->EFlags
;
1447 DS_sig(sigcontext
) = context
->SegDs
;
1450 ES_sig(sigcontext
) = context
->SegEs
;
1453 SS_sig(sigcontext
) = context
->SegSs
;
1455 if (FPU_sig(sigcontext
)) *FPU_sig(sigcontext
) = context
->u
.FltSave
;
1459 /***********************************************************************
1460 * RtlCaptureContext (NTDLL.@)
1462 __ASM_GLOBAL_FUNC( RtlCaptureContext
,
1464 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1465 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1466 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1467 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1468 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1469 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1470 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1471 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1472 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1473 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1474 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1475 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1476 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1477 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1478 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1479 "leaq 8(%rsp),%rax\n\t"
1480 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1481 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1482 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1483 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1484 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1485 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1486 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1487 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1488 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1489 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1490 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1491 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1492 "movq (%rsp),%rax\n\t"
1493 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1494 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1495 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1496 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1497 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1498 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1499 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1500 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1501 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1502 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1503 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1504 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1505 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1506 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1507 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1508 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1509 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1510 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1513 /***********************************************************************
1516 * Set the new CPU context.
1518 void set_cpu_context( const CONTEXT
*context
)
1520 extern void CDECL
__wine_restore_regs( const CONTEXT
* ) DECLSPEC_NORETURN
;
1521 __wine_restore_regs( context
);
1525 /***********************************************************************
1528 * Copy a register context according to the flags.
1530 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1532 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1533 if (flags
& CONTEXT_CONTROL
)
1535 to
->Rbp
= from
->Rbp
;
1536 to
->Rip
= from
->Rip
;
1537 to
->Rsp
= from
->Rsp
;
1538 to
->SegCs
= from
->SegCs
;
1539 to
->SegSs
= from
->SegSs
;
1540 to
->EFlags
= from
->EFlags
;
1542 if (flags
& CONTEXT_INTEGER
)
1544 to
->Rax
= from
->Rax
;
1545 to
->Rcx
= from
->Rcx
;
1546 to
->Rdx
= from
->Rdx
;
1547 to
->Rbx
= from
->Rbx
;
1548 to
->Rsi
= from
->Rsi
;
1549 to
->Rdi
= from
->Rdi
;
1552 to
->R10
= from
->R10
;
1553 to
->R11
= from
->R11
;
1554 to
->R12
= from
->R12
;
1555 to
->R13
= from
->R13
;
1556 to
->R14
= from
->R14
;
1557 to
->R15
= from
->R15
;
1559 if (flags
& CONTEXT_SEGMENTS
)
1561 to
->SegDs
= from
->SegDs
;
1562 to
->SegEs
= from
->SegEs
;
1563 to
->SegFs
= from
->SegFs
;
1564 to
->SegGs
= from
->SegGs
;
1566 if (flags
& CONTEXT_FLOATING_POINT
)
1568 to
->MxCsr
= from
->MxCsr
;
1569 to
->u
.FltSave
= from
->u
.FltSave
;
1571 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1573 to
->Dr0
= from
->Dr0
;
1574 to
->Dr1
= from
->Dr1
;
1575 to
->Dr2
= from
->Dr2
;
1576 to
->Dr3
= from
->Dr3
;
1577 to
->Dr6
= from
->Dr6
;
1578 to
->Dr7
= from
->Dr7
;
1583 /***********************************************************************
1586 * Convert a register context to the server format.
1588 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1590 DWORD flags
= from
->ContextFlags
& ~CONTEXT_AMD64
; /* get rid of CPU id */
1592 memset( to
, 0, sizeof(*to
) );
1593 to
->cpu
= CPU_x86_64
;
1595 if (flags
& CONTEXT_CONTROL
)
1597 to
->flags
|= SERVER_CTX_CONTROL
;
1598 to
->ctl
.x86_64_regs
.rbp
= from
->Rbp
;
1599 to
->ctl
.x86_64_regs
.rip
= from
->Rip
;
1600 to
->ctl
.x86_64_regs
.rsp
= from
->Rsp
;
1601 to
->ctl
.x86_64_regs
.cs
= from
->SegCs
;
1602 to
->ctl
.x86_64_regs
.ss
= from
->SegSs
;
1603 to
->ctl
.x86_64_regs
.flags
= from
->EFlags
;
1605 if (flags
& CONTEXT_INTEGER
)
1607 to
->flags
|= SERVER_CTX_INTEGER
;
1608 to
->integer
.x86_64_regs
.rax
= from
->Rax
;
1609 to
->integer
.x86_64_regs
.rcx
= from
->Rcx
;
1610 to
->integer
.x86_64_regs
.rdx
= from
->Rdx
;
1611 to
->integer
.x86_64_regs
.rbx
= from
->Rbx
;
1612 to
->integer
.x86_64_regs
.rsi
= from
->Rsi
;
1613 to
->integer
.x86_64_regs
.rdi
= from
->Rdi
;
1614 to
->integer
.x86_64_regs
.r8
= from
->R8
;
1615 to
->integer
.x86_64_regs
.r9
= from
->R9
;
1616 to
->integer
.x86_64_regs
.r10
= from
->R10
;
1617 to
->integer
.x86_64_regs
.r11
= from
->R11
;
1618 to
->integer
.x86_64_regs
.r12
= from
->R12
;
1619 to
->integer
.x86_64_regs
.r13
= from
->R13
;
1620 to
->integer
.x86_64_regs
.r14
= from
->R14
;
1621 to
->integer
.x86_64_regs
.r15
= from
->R15
;
1623 if (flags
& CONTEXT_SEGMENTS
)
1625 to
->flags
|= SERVER_CTX_SEGMENTS
;
1626 to
->seg
.x86_64_regs
.ds
= from
->SegDs
;
1627 to
->seg
.x86_64_regs
.es
= from
->SegEs
;
1628 to
->seg
.x86_64_regs
.fs
= from
->SegFs
;
1629 to
->seg
.x86_64_regs
.gs
= from
->SegGs
;
1631 if (flags
& CONTEXT_FLOATING_POINT
)
1633 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1634 memcpy( to
->fp
.x86_64_regs
.fpregs
, &from
->u
.FltSave
, sizeof(to
->fp
.x86_64_regs
.fpregs
) );
1636 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1638 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1639 to
->debug
.x86_64_regs
.dr0
= from
->Dr0
;
1640 to
->debug
.x86_64_regs
.dr1
= from
->Dr1
;
1641 to
->debug
.x86_64_regs
.dr2
= from
->Dr2
;
1642 to
->debug
.x86_64_regs
.dr3
= from
->Dr3
;
1643 to
->debug
.x86_64_regs
.dr6
= from
->Dr6
;
1644 to
->debug
.x86_64_regs
.dr7
= from
->Dr7
;
1646 return STATUS_SUCCESS
;
1650 /***********************************************************************
1651 * context_from_server
1653 * Convert a register context from the server format.
1655 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1657 if (from
->cpu
!= CPU_x86_64
) return STATUS_INVALID_PARAMETER
;
1659 to
->ContextFlags
= CONTEXT_AMD64
;
1660 if (from
->flags
& SERVER_CTX_CONTROL
)
1662 to
->ContextFlags
|= CONTEXT_CONTROL
;
1663 to
->Rbp
= from
->ctl
.x86_64_regs
.rbp
;
1664 to
->Rip
= from
->ctl
.x86_64_regs
.rip
;
1665 to
->Rsp
= from
->ctl
.x86_64_regs
.rsp
;
1666 to
->SegCs
= from
->ctl
.x86_64_regs
.cs
;
1667 to
->SegSs
= from
->ctl
.x86_64_regs
.ss
;
1668 to
->EFlags
= from
->ctl
.x86_64_regs
.flags
;
1671 if (from
->flags
& SERVER_CTX_INTEGER
)
1673 to
->ContextFlags
|= CONTEXT_INTEGER
;
1674 to
->Rax
= from
->integer
.x86_64_regs
.rax
;
1675 to
->Rcx
= from
->integer
.x86_64_regs
.rcx
;
1676 to
->Rdx
= from
->integer
.x86_64_regs
.rdx
;
1677 to
->Rbx
= from
->integer
.x86_64_regs
.rbx
;
1678 to
->Rsi
= from
->integer
.x86_64_regs
.rsi
;
1679 to
->Rdi
= from
->integer
.x86_64_regs
.rdi
;
1680 to
->R8
= from
->integer
.x86_64_regs
.r8
;
1681 to
->R9
= from
->integer
.x86_64_regs
.r9
;
1682 to
->R10
= from
->integer
.x86_64_regs
.r10
;
1683 to
->R11
= from
->integer
.x86_64_regs
.r11
;
1684 to
->R12
= from
->integer
.x86_64_regs
.r12
;
1685 to
->R13
= from
->integer
.x86_64_regs
.r13
;
1686 to
->R14
= from
->integer
.x86_64_regs
.r14
;
1687 to
->R15
= from
->integer
.x86_64_regs
.r15
;
1689 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1691 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1692 to
->SegDs
= from
->seg
.x86_64_regs
.ds
;
1693 to
->SegEs
= from
->seg
.x86_64_regs
.es
;
1694 to
->SegFs
= from
->seg
.x86_64_regs
.fs
;
1695 to
->SegGs
= from
->seg
.x86_64_regs
.gs
;
1697 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1699 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1700 memcpy( &to
->u
.FltSave
, from
->fp
.x86_64_regs
.fpregs
, sizeof(from
->fp
.x86_64_regs
.fpregs
) );
1701 to
->MxCsr
= to
->u
.FltSave
.MxCsr
;
1703 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1705 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1706 to
->Dr0
= from
->debug
.x86_64_regs
.dr0
;
1707 to
->Dr1
= from
->debug
.x86_64_regs
.dr1
;
1708 to
->Dr2
= from
->debug
.x86_64_regs
.dr2
;
1709 to
->Dr3
= from
->debug
.x86_64_regs
.dr3
;
1710 to
->Dr6
= from
->debug
.x86_64_regs
.dr6
;
1711 to
->Dr7
= from
->debug
.x86_64_regs
.dr7
;
1713 return STATUS_SUCCESS
;
1717 extern void raise_func_trampoline( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
1718 __ASM_GLOBAL_FUNC( raise_func_trampoline
,
1719 __ASM_CFI(".cfi_signal_frame\n\t")
1720 __ASM_CFI(".cfi_def_cfa %rbp,144\n\t") /* red zone + rip + rbp */
1721 __ASM_CFI(".cfi_rel_offset %rip,8\n\t")
1722 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1726 /***********************************************************************
1729 * Setup a proper stack frame for the raise function, and modify the
1730 * sigcontext so that the return from the signal handler will call
1731 * the raise function.
1733 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
1738 EXCEPTION_RECORD rec
;
1741 ULONG64 red_zone
[16];
1744 DWORD exception_code
= 0;
1746 stack
= (struct stack_layout
*)(RSP_sig(sigcontext
) & ~15);
1748 /* stack sanity checks */
1750 if (is_inside_signal_stack( stack
))
1752 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1753 GetCurrentThreadId(), RIP_sig(sigcontext
), RSP_sig(sigcontext
),
1754 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1758 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1759 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1760 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1762 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1763 GetCurrentThreadId(), RIP_sig(sigcontext
), RSP_sig(sigcontext
),
1764 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1766 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1768 /* stack overflow on last page, unrecoverable */
1769 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1770 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1771 diff
, GetCurrentThreadId(), RIP_sig(sigcontext
),
1772 RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1773 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1776 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1778 /* stack access below stack limit, may be recoverable */
1779 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1782 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1783 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1784 diff
, GetCurrentThreadId(), RIP_sig(sigcontext
),
1785 RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1786 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1791 stack
--; /* push the stack_layout structure */
1792 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1793 VALGRIND_MAKE_MEM_UNDEFINED(stack
, sizeof(*stack
));
1794 #elif defined(VALGRIND_MAKE_WRITABLE)
1795 VALGRIND_MAKE_WRITABLE(stack
, sizeof(*stack
));
1797 stack
->rec
.ExceptionRecord
= NULL
;
1798 stack
->rec
.ExceptionCode
= exception_code
;
1799 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1800 stack
->rec
.ExceptionAddress
= (void *)RIP_sig(sigcontext
);
1801 stack
->rec
.NumberParameters
= 0;
1802 save_context( &stack
->context
, sigcontext
);
1804 /* store return address and %rbp without aligning, so that the offset is fixed */
1805 rsp_ptr
= (ULONG64
*)RSP_sig(sigcontext
) - 16;
1806 *(--rsp_ptr
) = RIP_sig(sigcontext
);
1807 *(--rsp_ptr
) = RBP_sig(sigcontext
);
1809 /* now modify the sigcontext to return to the raise function */
1810 RIP_sig(sigcontext
) = (ULONG_PTR
)raise_func_trampoline
;
1811 RDI_sig(sigcontext
) = (ULONG_PTR
)&stack
->rec
;
1812 RSI_sig(sigcontext
) = (ULONG_PTR
)&stack
->context
;
1813 RDX_sig(sigcontext
) = (ULONG_PTR
)func
;
1814 RBP_sig(sigcontext
) = (ULONG_PTR
)rsp_ptr
;
1815 RSP_sig(sigcontext
) = (ULONG_PTR
)stack
;
1816 /* clear single-step, direction, and align check flag */
1817 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1823 /**********************************************************************
1824 * find_function_info
1826 static RUNTIME_FUNCTION
*find_function_info( ULONG64 pc
, HMODULE module
,
1827 RUNTIME_FUNCTION
*func
, ULONG size
)
1830 int max
= size
/sizeof(*func
) - 1;
1834 int pos
= (min
+ max
) / 2;
1835 if ((char *)pc
< (char *)module
+ func
[pos
].BeginAddress
) max
= pos
- 1;
1836 else if ((char *)pc
>= (char *)module
+ func
[pos
].EndAddress
) min
= pos
+ 1;
1840 while (func
->UnwindData
& 1) /* follow chained entry */
1841 func
= (RUNTIME_FUNCTION
*)((char *)module
+ (func
->UnwindData
& ~1));
1849 /**********************************************************************
1852 * Call a single exception handler.
1853 * FIXME: Handle nested exceptions.
1855 static NTSTATUS
call_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*orig_context
)
1859 dispatch
->ControlPc
= dispatch
->ContextRecord
->Rip
;
1861 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
1862 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
1863 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
1864 TRACE( "handler at %p returned %u\n", dispatch
->LanguageHandler
, res
);
1868 case ExceptionContinueExecution
:
1869 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
1870 *orig_context
= *dispatch
->ContextRecord
;
1871 return STATUS_SUCCESS
;
1872 case ExceptionContinueSearch
:
1874 case ExceptionNestedException
:
1877 return STATUS_INVALID_DISPOSITION
;
1879 return STATUS_UNHANDLED_EXCEPTION
;
1883 /**********************************************************************
1886 * Call a single exception handler from the TEB chain.
1887 * FIXME: Handle nested exceptions.
1889 static NTSTATUS
call_teb_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
1890 EXCEPTION_REGISTRATION_RECORD
*teb_frame
, CONTEXT
*orig_context
)
1892 EXCEPTION_REGISTRATION_RECORD
*dispatcher
;
1895 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
1896 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
1897 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
1898 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
1902 case ExceptionContinueExecution
:
1903 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
1904 *orig_context
= *dispatch
->ContextRecord
;
1905 return STATUS_SUCCESS
;
1906 case ExceptionContinueSearch
:
1908 case ExceptionNestedException
:
1911 return STATUS_INVALID_DISPOSITION
;
1913 return STATUS_UNHANDLED_EXCEPTION
;
1917 /**********************************************************************
1918 * call_stack_handlers
1920 * Call the stack handlers chain.
1922 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
1924 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1925 UNWIND_HISTORY_TABLE table
;
1926 DISPATCHER_CONTEXT dispatch
;
1927 CONTEXT context
, new_context
;
1932 context
= *orig_context
;
1933 dispatch
.TargetIp
= 0;
1934 dispatch
.ContextRecord
= &context
;
1935 dispatch
.HistoryTable
= &table
;
1936 dispatch
.ScopeIndex
= 0; /* FIXME */
1939 new_context
= context
;
1941 /* FIXME: should use the history table to make things faster */
1944 dispatch
.ImageBase
= 0;
1946 /* first look for PE exception information */
1948 if (!LdrFindEntryForAddress( (void *)context
.Rip
, &module
))
1950 RUNTIME_FUNCTION
*dir
;
1952 dispatch
.ImageBase
= (ULONG64
)module
->BaseAddress
;
1953 if ((dir
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
1954 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
1956 if ((dispatch
.FunctionEntry
= find_function_info( context
.Rip
, module
->BaseAddress
,
1959 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_EHANDLER
, dispatch
.ImageBase
,
1960 context
.Rip
, dispatch
.FunctionEntry
,
1961 &new_context
, &dispatch
.HandlerData
,
1962 &dispatch
.EstablisherFrame
, NULL
);
1966 else if (!(module
->Flags
& LDR_WINE_INTERNAL
))
1967 WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
1970 /* then look for host system exception information */
1972 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
1974 struct dwarf_eh_bases bases
;
1975 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
.Rip
- 1), &bases
);
1979 status
= dwarf_virtual_unwind( context
.Rip
, &dispatch
.EstablisherFrame
, &new_context
,
1980 fde
, &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
1981 if (status
!= STATUS_SUCCESS
) return status
;
1982 dispatch
.FunctionEntry
= NULL
;
1983 if (dispatch
.LanguageHandler
&& !module
)
1985 FIXME( "calling personality routine in system library not supported yet\n" );
1986 dispatch
.LanguageHandler
= NULL
;
1992 /* no exception information, treat as a leaf function */
1994 new_context
.Rip
= *(ULONG64
*)context
.Rsp
;
1995 new_context
.Rsp
= context
.Rsp
+ sizeof(ULONG64
);
1996 dispatch
.EstablisherFrame
= new_context
.Rsp
;
1997 dispatch
.LanguageHandler
= NULL
;
2000 if (!dispatch
.EstablisherFrame
) break;
2002 if ((dispatch
.EstablisherFrame
& 7) ||
2003 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
2004 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
2006 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
2007 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2008 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
2012 if (dispatch
.LanguageHandler
)
2014 status
= call_handler( rec
, &dispatch
, orig_context
);
2015 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
2017 /* hack: call wine handlers registered in the tib list */
2018 else while ((ULONG64
)teb_frame
< new_context
.Rsp
)
2020 TRACE( "found wine frame %p rsp %lx handler %p\n",
2021 teb_frame
, new_context
.Rsp
, teb_frame
->Handler
);
2022 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
2023 context
= *orig_context
;
2024 status
= call_teb_handler( rec
, &dispatch
, teb_frame
, orig_context
);
2025 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
2026 teb_frame
= teb_frame
->Prev
;
2029 if (new_context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
2030 context
= new_context
;
2032 return STATUS_UNHANDLED_EXCEPTION
;
2036 /*******************************************************************
2039 * Implementation of NtRaiseException.
2041 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2049 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2050 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
2051 context
->Rip
, GetCurrentThreadId() );
2052 for (c
= 0; c
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); c
++)
2053 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
2054 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
2056 if (rec
->ExceptionInformation
[1] >> 16)
2057 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2058 rec
->ExceptionAddress
,
2059 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
2061 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2062 rec
->ExceptionAddress
,
2063 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
2067 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2068 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
2069 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2070 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
2071 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2072 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
2073 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2074 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
2076 status
= send_debug_event( rec
, TRUE
, context
);
2077 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
2078 return STATUS_SUCCESS
;
2080 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
2081 return STATUS_SUCCESS
;
2083 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
2087 /* last chance exception */
2089 status
= send_debug_event( rec
, FALSE
, context
);
2090 if (status
!= DBG_CONTINUE
)
2092 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
2093 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2094 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
2095 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2097 ERR("Unhandled exception code %x flags %x addr %p\n",
2098 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
2099 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
2101 return STATUS_SUCCESS
;
2105 /**********************************************************************
2106 * raise_segv_exception
2108 static void raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2112 switch(rec
->ExceptionCode
)
2114 case EXCEPTION_ACCESS_VIOLATION
:
2115 if (rec
->NumberParameters
== 2)
2117 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
2118 rec
->ExceptionInformation
[0] )))
2119 set_cpu_context( context
);
2123 status
= raise_exception( rec
, context
, TRUE
);
2124 if (status
) raise_status( status
, rec
);
2125 set_cpu_context( context
);
2129 /**********************************************************************
2130 * raise_generic_exception
2132 * Generic raise function for exceptions that don't need special treatment.
2134 static void raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2136 NTSTATUS status
= raise_exception( rec
, context
, TRUE
);
2137 if (status
) raise_status( status
, rec
);
2138 set_cpu_context( context
);
2142 /**********************************************************************
2145 * Handler for SIGSEGV and related errors.
2147 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2149 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_segv_exception
);
2150 ucontext_t
*ucontext
= sigcontext
;
2152 switch(TRAP_sig(ucontext
))
2154 case TRAP_x86_OFLOW
: /* Overflow exception */
2155 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
2157 case TRAP_x86_BOUND
: /* Bound range exception */
2158 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
2160 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
2161 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2163 case TRAP_x86_STKFLT
: /* Stack fault */
2164 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
2166 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
2167 case TRAP_x86_PROTFLT
: /* General protection fault */
2168 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
2169 rec
->ExceptionCode
= ERROR_sig(ucontext
) ? EXCEPTION_ACCESS_VIOLATION
: EXCEPTION_PRIV_INSTRUCTION
;
2170 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2172 case TRAP_x86_PAGEFLT
: /* Page fault */
2173 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
2174 rec
->NumberParameters
= 2;
2175 rec
->ExceptionInformation
[0] = (ERROR_sig(ucontext
) & 2) != 0;
2176 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
2178 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
2179 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
2182 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext
) );
2184 case TRAP_x86_NMI
: /* NMI interrupt */
2185 case TRAP_x86_DNA
: /* Device not available exception */
2186 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
2187 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
2188 case TRAP_x86_MCHK
: /* Machine check exception */
2189 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
2190 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
2195 /**********************************************************************
2198 * Handler for SIGTRAP.
2200 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2202 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2204 switch (siginfo
->si_code
)
2206 case TRAP_TRACE
: /* Single-step exception */
2207 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2208 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
2210 case TRAP_BRKPT
: /* Breakpoint exception */
2211 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
2214 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
2219 /**********************************************************************
2222 * Handler for SIGFPE.
2224 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2226 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2228 switch (siginfo
->si_code
)
2231 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
2234 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
2237 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
2240 rec
->ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
2243 rec
->ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
2246 rec
->ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
2249 rec
->ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
2253 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
2258 /**********************************************************************
2261 * Handler for SIGINT.
2263 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2265 if (!dispatch_signal(SIGINT
))
2267 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2268 rec
->ExceptionCode
= CONTROL_C_EXIT
;
2273 /**********************************************************************
2276 * Handler for SIGABRT.
2278 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
2280 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
2281 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
2282 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
2286 /**********************************************************************
2289 * Handler for SIGQUIT.
2291 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
2297 /**********************************************************************
2300 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2302 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
2306 save_context( &context
, ucontext
);
2307 wait_suspend( &context
);
2308 restore_context( &context
, ucontext
);
2312 /***********************************************************************
2313 * __wine_set_signal_handler (NTDLL.@)
2315 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
2317 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
2318 if (handlers
[sig
] != NULL
) return -2;
2319 handlers
[sig
] = wsh
;
2324 /**********************************************************************
2325 * signal_alloc_thread
2327 NTSTATUS
signal_alloc_thread( TEB
**teb
)
2329 static size_t sigstack_zero_bits
;
2333 if (!sigstack_zero_bits
)
2335 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
2336 /* find the first power of two not smaller than min_size */
2337 sigstack_zero_bits
= 12;
2338 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
2339 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
2340 assert( sizeof(TEB
) <= teb_size
);
2343 size
= 1 << sigstack_zero_bits
;
2345 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
2346 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
2348 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
2349 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
2355 /**********************************************************************
2356 * signal_free_thread
2358 void signal_free_thread( TEB
*teb
)
2362 if (teb
->DeallocationStack
)
2365 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
2368 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
2372 /**********************************************************************
2373 * signal_init_thread
2375 void signal_init_thread( TEB
*teb
)
2377 const WORD fpu_cw
= 0x27f;
2380 #if defined __linux__
2381 arch_prctl( ARCH_SET_GS
, teb
);
2382 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
2383 amd64_set_gsbase( teb
);
2385 # error Please define setting %gs for your architecture
2388 ss
.ss_sp
= (char *)teb
+ teb_size
;
2389 ss
.ss_size
= signal_stack_size
;
2391 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
2394 __asm__
volatile ("fninit; fldcw %0" : : "m" (fpu_cw
));
2396 FIXME("FPU setup not implemented for this platform.\n");
2400 /**********************************************************************
2401 * signal_init_process
2403 void signal_init_process(void)
2405 struct sigaction sig_act
;
2407 sig_act
.sa_mask
= server_block_set
;
2408 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
| SA_ONSTACK
;
2410 sig_act
.sa_sigaction
= int_handler
;
2411 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
2412 sig_act
.sa_sigaction
= fpe_handler
;
2413 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
2414 sig_act
.sa_sigaction
= abrt_handler
;
2415 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
2416 sig_act
.sa_sigaction
= quit_handler
;
2417 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
2418 sig_act
.sa_sigaction
= usr1_handler
;
2419 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
2421 sig_act
.sa_sigaction
= segv_handler
;
2422 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
2423 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
2425 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
2429 sig_act
.sa_sigaction
= trap_handler
;
2430 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
2435 perror("sigaction");
2440 /**********************************************************************
2441 * RtlAddFunctionTable (NTDLL.@)
2443 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, DWORD64 addr
)
2445 FIXME( "%p %u %lx: stub\n", table
, count
, addr
);
2450 /**********************************************************************
2451 * RtlDeleteFunctionTable (NTDLL.@)
2453 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
2455 FIXME( "%p: stub\n", table
);
2460 /**********************************************************************
2461 * RtlLookupFunctionEntry (NTDLL.@)
2463 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG64 pc
, ULONG64
*base
, UNWIND_HISTORY_TABLE
*table
)
2466 RUNTIME_FUNCTION
*func
;
2469 /* FIXME: should use the history table to make things faster */
2471 if (LdrFindEntryForAddress( (void *)pc
, &module
))
2473 WARN( "module not found for %lx\n", pc
);
2476 if (!(func
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
2477 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
2479 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
2482 func
= find_function_info( pc
, module
->BaseAddress
, func
, size
);
2483 if (func
) *base
= (ULONG64
)module
->BaseAddress
;
2487 static ULONG64
get_int_reg( CONTEXT
*context
, int reg
)
2489 return *(&context
->Rax
+ reg
);
2492 static void set_int_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, ULONG64 val
)
2494 *(&context
->Rax
+ reg
) = val
;
2495 if (ctx_ptr
) ctx_ptr
->u2
.IntegerContext
[reg
] = &context
->Rax
+ reg
;
2498 static void set_float_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, M128A val
)
2500 *(&context
->u
.s
.Xmm0
+ reg
) = val
;
2501 if (ctx_ptr
) ctx_ptr
->u1
.FloatingContext
[reg
] = &context
->u
.s
.Xmm0
+ reg
;
2504 static int get_opcode_size( struct opcode op
)
2508 case UWOP_ALLOC_LARGE
:
2509 return 2 + (op
.info
!= 0);
2510 case UWOP_SAVE_NONVOL
:
2511 case UWOP_SAVE_XMM128
:
2513 case UWOP_SAVE_NONVOL_FAR
:
2514 case UWOP_SAVE_XMM128_FAR
:
2521 static BOOL
is_inside_epilog( BYTE
*pc
)
2523 /* add or lea must be the first instruction, and it must have a rex.W prefix */
2524 if ((pc
[0] & 0xf8) == 0x48)
2528 case 0x81: /* add $nnnn,%rsp */
2529 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
2535 case 0x83: /* add $n,%rsp */
2536 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
2542 case 0x8d: /* lea n(reg),%rsp */
2543 if (pc
[0] & 0x06) return FALSE
; /* rex.RX must be cleared */
2544 if (((pc
[2] >> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
2545 if ((pc
[2] & 7) == 4) return FALSE
; /* no SIB byte allowed */
2546 if ((pc
[2] >> 6) == 1) /* 8-bit offset */
2551 if ((pc
[2] >> 6) == 2) /* 32-bit offset */
2560 /* now check for various pop instructions */
2566 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
2570 case 0x58: /* pop %rax/%r8 */
2571 case 0x59: /* pop %rcx/%r9 */
2572 case 0x5a: /* pop %rdx/%r10 */
2573 case 0x5b: /* pop %rbx/%r11 */
2574 case 0x5c: /* pop %rsp/%r12 */
2575 case 0x5d: /* pop %rbp/%r13 */
2576 case 0x5e: /* pop %rsi/%r14 */
2577 case 0x5f: /* pop %rdi/%r15 */
2580 case 0xc2: /* ret $nn */
2581 case 0xc3: /* ret */
2583 /* FIXME: add various jump instructions */
2589 /* execute a function epilog, which must have been validated with is_inside_epilog() */
2590 static void interpret_epilog( BYTE
*pc
, CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
2596 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
2600 case 0x58: /* pop %rax/r8 */
2601 case 0x59: /* pop %rcx/r9 */
2602 case 0x5a: /* pop %rdx/r10 */
2603 case 0x5b: /* pop %rbx/r11 */
2604 case 0x5c: /* pop %rsp/r12 */
2605 case 0x5d: /* pop %rbp/r13 */
2606 case 0x5e: /* pop %rsi/r14 */
2607 case 0x5f: /* pop %rdi/r15 */
2608 set_int_reg( context
, ctx_ptr
, *pc
- 0x58 + (rex
& 1) * 8, *(ULONG64
*)context
->Rsp
);
2609 context
->Rsp
+= sizeof(ULONG64
);
2612 case 0x81: /* add $nnnn,%rsp */
2613 context
->Rsp
+= *(LONG
*)(pc
+ 2);
2614 pc
+= 2 + sizeof(LONG
);
2616 case 0x83: /* add $n,%rsp */
2617 context
->Rsp
+= (signed char)pc
[2];
2621 if ((pc
[1] >> 6) == 1) /* lea n(reg),%rsp */
2623 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + (signed char)pc
[2];
2626 else /* lea nnnn(reg),%rsp */
2628 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + *(LONG
*)(pc
+ 2);
2629 pc
+= 2 + sizeof(LONG
);
2632 case 0xc2: /* ret $nn */
2633 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2634 context
->Rsp
+= sizeof(ULONG64
) + *(WORD
*)(pc
+ 1);
2636 case 0xc3: /* ret */
2637 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2638 context
->Rsp
+= sizeof(ULONG64
);
2640 /* FIXME: add various jump instructions */
2646 /**********************************************************************
2647 * RtlVirtualUnwind (NTDLL.@)
2649 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG64 base
, ULONG64 pc
,
2650 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
2651 PVOID
*data
, ULONG64
*frame_ret
,
2652 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
2654 union handler_data
*handler_data
;
2656 struct UNWIND_INFO
*info
;
2657 unsigned int i
, prolog_offset
;
2659 TRACE( "type %x rip %lx rsp %lx\n", type
, pc
, context
->Rsp
);
2660 if (TRACE_ON(seh
)) dump_unwind_info( base
, function
);
2662 frame
= *frame_ret
= context
->Rsp
;
2665 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
2666 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
2668 if (info
->version
!= 1)
2670 FIXME( "unknown unwind info version %u at %p\n", info
->version
, info
);
2674 if (info
->frame_reg
)
2675 frame
= get_int_reg( context
, info
->frame_reg
) - info
->frame_offset
* 16;
2677 /* check if in prolog */
2678 if (pc
>= base
+ function
->BeginAddress
&& pc
< base
+ function
->BeginAddress
+ info
->prolog
)
2680 prolog_offset
= pc
- base
- function
->BeginAddress
;
2685 if (is_inside_epilog( (BYTE
*)pc
))
2687 interpret_epilog( (BYTE
*)pc
, context
, ctx_ptr
);
2693 for (i
= 0; i
< info
->count
; i
+= get_opcode_size(info
->opcodes
[i
]))
2695 if (prolog_offset
< info
->opcodes
[i
].offset
) continue; /* skip it */
2697 switch (info
->opcodes
[i
].code
)
2699 case UWOP_PUSH_NONVOL
: /* pushq %reg */
2700 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)context
->Rsp
);
2701 context
->Rsp
+= sizeof(ULONG64
);
2703 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
2704 if (info
->opcodes
[i
].info
) context
->Rsp
+= *(DWORD
*)&info
->opcodes
[i
+1];
2705 else context
->Rsp
+= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
2707 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
2708 context
->Rsp
+= (info
->opcodes
[i
].info
+ 1) * 8;
2710 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
2711 context
->Rsp
= *frame_ret
= frame
;
2713 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
2714 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 8;
2715 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)off
);
2717 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
2718 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
2719 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)off
);
2721 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
2722 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 16;
2723 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(M128A
*)off
);
2725 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
2726 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
2727 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(M128A
*)off
);
2729 case UWOP_PUSH_MACHFRAME
:
2730 FIXME( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
2733 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
2738 if (!(info
->flags
& UNW_FLAG_CHAININFO
)) break;
2739 function
= &handler_data
->chain
; /* restart with the chained info */
2742 /* now pop return address */
2743 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2744 context
->Rsp
+= sizeof(ULONG64
);
2746 if (!(info
->flags
& type
)) return NULL
; /* no matching handler */
2747 if (prolog_offset
!= ~0) return NULL
; /* inside prolog */
2749 *data
= &handler_data
->handler
+ 1;
2750 return (char *)base
+ handler_data
->handler
;
2754 /**********************************************************************
2755 * call_unwind_handler
2757 * Call a single unwind handler.
2758 * FIXME: Handle nested exceptions.
2760 static void call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
2764 dispatch
->ControlPc
= dispatch
->ContextRecord
->Rip
;
2766 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2767 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2768 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2769 TRACE( "handler %p returned %x\n", dispatch
->LanguageHandler
, res
);
2773 case ExceptionContinueSearch
:
2775 case ExceptionCollidedUnwind
:
2776 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2779 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
2785 /**********************************************************************
2786 * call_teb_unwind_handler
2788 * Call a single unwind handler from the TEB chain.
2789 * FIXME: Handle nested exceptions.
2791 static void call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
2792 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
2794 EXCEPTION_REGISTRATION_RECORD
*dispatcher
;
2797 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2798 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
2799 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
2800 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
2804 case ExceptionContinueSearch
:
2806 case ExceptionCollidedUnwind
:
2807 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2810 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
2816 /*******************************************************************
2817 * RtlUnwindEx (NTDLL.@)
2819 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
2820 PVOID retval
, CONTEXT
*orig_context
, UNWIND_HISTORY_TABLE
*table
)
2822 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2823 EXCEPTION_RECORD record
;
2824 DISPATCHER_CONTEXT dispatch
;
2825 CONTEXT context
, new_context
;
2830 RtlCaptureContext( orig_context
);
2832 /* build an exception record, if we do not have one */
2835 record
.ExceptionCode
= STATUS_UNWIND
;
2836 record
.ExceptionFlags
= 0;
2837 record
.ExceptionRecord
= NULL
;
2838 record
.ExceptionAddress
= (void *)orig_context
->Rip
;
2839 record
.NumberParameters
= 0;
2843 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
2845 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
2846 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, orig_context
->Rip
);
2847 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2848 orig_context
->Rax
, orig_context
->Rbx
, orig_context
->Rcx
, orig_context
->Rdx
);
2849 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2850 orig_context
->Rsi
, orig_context
->Rdi
, orig_context
->Rbp
, orig_context
->Rsp
);
2851 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2852 orig_context
->R8
, orig_context
->R9
, orig_context
->R10
, orig_context
->R11
);
2853 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2854 orig_context
->R12
, orig_context
->R13
, orig_context
->R14
, orig_context
->R15
);
2856 context
= *orig_context
;
2857 dispatch
.EstablisherFrame
= context
.Rsp
;
2858 dispatch
.TargetIp
= (ULONG64
)target_ip
;
2859 dispatch
.ContextRecord
= &context
;
2860 dispatch
.HistoryTable
= table
;
2862 while (dispatch
.EstablisherFrame
!= (ULONG64
)end_frame
)
2864 new_context
= context
;
2866 /* FIXME: should use the history table to make things faster */
2869 dispatch
.ImageBase
= 0;
2870 dispatch
.ScopeIndex
= 0; /* FIXME */
2872 /* first look for PE exception information */
2874 if (!LdrFindEntryForAddress( (void *)context
.Rip
, &module
))
2876 RUNTIME_FUNCTION
*dir
;
2878 dispatch
.ImageBase
= (ULONG64
)module
->BaseAddress
;
2879 if ((dir
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
2880 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
2882 if ((dispatch
.FunctionEntry
= find_function_info( context
.Rip
, module
->BaseAddress
,
2885 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_UHANDLER
, dispatch
.ImageBase
,
2886 context
.Rip
, dispatch
.FunctionEntry
,
2887 &new_context
, &dispatch
.HandlerData
,
2888 &dispatch
.EstablisherFrame
, NULL
);
2892 else if (!(module
->Flags
& LDR_WINE_INTERNAL
))
2893 WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
2896 /* then look for host system exception information */
2898 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
2900 struct dwarf_eh_bases bases
;
2901 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
.Rip
- 1), &bases
);
2905 dispatch
.FunctionEntry
= NULL
;
2906 status
= dwarf_virtual_unwind( context
.Rip
, &dispatch
.EstablisherFrame
, &new_context
, fde
,
2907 &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2908 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
2909 if (dispatch
.LanguageHandler
&& !module
)
2911 FIXME( "calling personality routine in system library not supported yet\n" );
2912 dispatch
.LanguageHandler
= NULL
;
2918 /* no exception information, treat as a leaf function */
2920 new_context
.Rip
= *(ULONG64
*)context
.Rsp
;
2921 new_context
.Rsp
= context
.Rsp
+ sizeof(ULONG64
);
2922 dispatch
.EstablisherFrame
= new_context
.Rsp
;
2923 dispatch
.LanguageHandler
= NULL
;
2926 if (!dispatch
.EstablisherFrame
) break;
2928 if (is_inside_signal_stack( (void *)dispatch
.EstablisherFrame
))
2930 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch
.EstablisherFrame
,
2931 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size
);
2932 context
= new_context
;
2936 if ((dispatch
.EstablisherFrame
& 7) ||
2937 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
2938 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
2940 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
2941 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2942 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
2946 if (dispatch
.LanguageHandler
)
2948 if (end_frame
&& (dispatch
.EstablisherFrame
> (ULONG64
)end_frame
))
2950 ERR( "invalid end frame %lx/%p\n", dispatch
.EstablisherFrame
, end_frame
);
2951 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
2953 call_unwind_handler( rec
, &dispatch
);
2955 else /* hack: call builtin handlers registered in the tib list */
2957 while ((ULONG64
)teb_frame
< new_context
.Rsp
&& (ULONG64
)teb_frame
< (ULONG64
)end_frame
)
2959 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
2960 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
2961 call_teb_unwind_handler( rec
, &dispatch
, teb_frame
);
2962 teb_frame
= __wine_pop_frame( teb_frame
);
2964 if ((ULONG64
)teb_frame
== (ULONG64
)end_frame
&& (ULONG64
)end_frame
< new_context
.Rsp
) break;
2965 dispatch
.EstablisherFrame
= new_context
.Rsp
;
2968 context
= new_context
;
2970 context
.Rax
= (ULONG64
)retval
;
2971 context
.Rip
= (ULONG64
)target_ip
;
2972 TRACE( "returning to %lx stack %lx\n", context
.Rip
, context
.Rsp
);
2973 set_cpu_context( &context
);
2977 /*******************************************************************
2978 * RtlUnwind (NTDLL.@)
2980 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
2983 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
2987 /*******************************************************************
2988 * __C_specific_handler (NTDLL.@)
2990 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
2993 struct _DISPATCHER_CONTEXT
*dispatch
)
2995 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
2998 TRACE( "%p %lx %p %p\n", rec
, frame
, context
, dispatch
);
2999 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
3001 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)) /* FIXME */
3002 return ExceptionContinueSearch
;
3004 for (i
= 0; i
< table
->Count
; i
++)
3006 if (context
->Rip
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
3007 context
->Rip
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
3009 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
3010 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
3012 EXCEPTION_POINTERS ptrs
;
3013 PC_LANGUAGE_EXCEPTION_HANDLER filter
;
3015 filter
= (PC_LANGUAGE_EXCEPTION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
3016 ptrs
.ExceptionRecord
= rec
;
3017 ptrs
.ContextRecord
= context
;
3018 TRACE( "calling filter %p ptrs %p frame %lx\n", filter
, &ptrs
, frame
);
3019 switch (filter( &ptrs
, frame
))
3021 case EXCEPTION_EXECUTE_HANDLER
:
3023 case EXCEPTION_CONTINUE_SEARCH
:
3025 case EXCEPTION_CONTINUE_EXECUTION
:
3026 return ExceptionContinueExecution
;
3029 TRACE( "unwinding to target %lx\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
3030 RtlUnwindEx( (void *)frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
3031 rec
, 0, context
, dispatch
->HistoryTable
);
3034 return ExceptionContinueSearch
;
3038 /*******************************************************************
3039 * NtRaiseException (NTDLL.@)
3041 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
3043 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
3044 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
3049 /***********************************************************************
3050 * RtlRaiseException (NTDLL.@)
3052 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
3056 rec
->ExceptionAddress
= (void *)context
->Rip
;
3057 status
= raise_exception( rec
, context
, TRUE
);
3058 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
3060 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 1 )
3063 /*************************************************************************
3064 * RtlCaptureStackBackTrace (NTDLL.@)
3066 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
3068 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
3073 /***********************************************************************
3076 void call_thread_func( LPTHREAD_START_ROUTINE entry
, void *arg
, void *frame
)
3078 ntdll_get_thread_data()->exit_frame
= frame
;
3081 RtlExitUserThread( entry( arg
));
3083 __EXCEPT(unhandled_exception_filter
)
3085 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
3088 abort(); /* should not be reached */
3091 extern void DECLSPEC_NORETURN
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
);
3092 __ASM_GLOBAL_FUNC( call_thread_entry_point
,
3094 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3095 "movq %rbp,48(%rsp)\n\t"
3096 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3097 "movq %rbx,40(%rsp)\n\t"
3098 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3099 "movq %r12,32(%rsp)\n\t"
3100 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3101 "movq %r13,24(%rsp)\n\t"
3102 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3103 "movq %r14,16(%rsp)\n\t"
3104 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3105 "movq %r15,8(%rsp)\n\t"
3106 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3107 "movq %rsp,%rdx\n\t"
3108 "call " __ASM_NAME("call_thread_func") );
3110 extern void DECLSPEC_NORETURN
call_thread_exit_func( int status
, void (*func
)(int), void *frame
);
3111 __ASM_GLOBAL_FUNC( call_thread_exit_func
,
3112 "movq %rdx,%rsp\n\t"
3113 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3114 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3115 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3116 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3117 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3118 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3119 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3122 /***********************************************************************
3123 * RtlExitUserThread (NTDLL.@)
3125 void WINAPI
RtlExitUserThread( ULONG status
)
3127 if (!ntdll_get_thread_data()->exit_frame
) exit_thread( status
);
3128 call_thread_exit_func( status
, exit_thread
, ntdll_get_thread_data()->exit_frame
);
3131 /***********************************************************************
3134 void abort_thread( int status
)
3136 if (!ntdll_get_thread_data()->exit_frame
) terminate_thread( status
);
3137 call_thread_exit_func( status
, terminate_thread
, ntdll_get_thread_data()->exit_frame
);
3140 /**********************************************************************
3141 * __wine_enter_vm86 (NTDLL.@)
3143 void __wine_enter_vm86( CONTEXT
*context
)
3145 MESSAGE("vm86 mode not supported on this platform\n");
3148 /**********************************************************************
3149 * DbgBreakPoint (NTDLL.@)
3151 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
3153 /**********************************************************************
3154 * DbgUserBreakPoint (NTDLL.@)
3156 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
3158 #endif /* __x86_64__ */