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"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
38 #ifdef HAVE_SYS_SIGNAL_H
39 # include <sys/signal.h>
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
45 #define WIN32_NO_STATUS
48 #include "wine/library.h"
49 #include "wine/exception.h"
50 #include "ntdll_misc.h"
51 #include "wine/debug.h"
53 #ifdef HAVE_VALGRIND_MEMCHECK_H
54 #include <valgrind/memcheck.h>
57 WINE_DEFAULT_DEBUG_CHANNEL(seh);
59 struct _DISPATCHER_CONTEXT;
61 typedef LONG (WINAPI *PC_LANGUAGE_EXCEPTION_HANDLER)( EXCEPTION_POINTERS *ptrs, ULONG64 frame );
62 typedef EXCEPTION_DISPOSITION (WINAPI *PEXCEPTION_ROUTINE)( EXCEPTION_RECORD *rec,
65 struct _DISPATCHER_CONTEXT *dispatch );
67 typedef struct _DISPATCHER_CONTEXT
71 PRUNTIME_FUNCTION FunctionEntry;
72 ULONG64 EstablisherFrame;
74 PCONTEXT ContextRecord;
75 PEXCEPTION_ROUTINE LanguageHandler;
77 PUNWIND_HISTORY_TABLE HistoryTable;
79 } DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;
81 typedef struct _SCOPE_TABLE
91 } SCOPE_TABLE, *PSCOPE_TABLE;
94 /***********************************************************************
95 * signal context platform-specific definitions
99 #include <asm/prctl.h>
100 extern int arch_prctl(int func, void *ptr);
102 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
103 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
104 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
105 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
106 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
107 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
108 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
109 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
110 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
111 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
112 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
113 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
114 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
115 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
116 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
118 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
119 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
120 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
122 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
123 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
124 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
125 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
126 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
128 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
132 #if defined(__NetBSD__)
133 # include <sys/ucontext.h>
134 # include <sys/types.h>
137 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
138 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
139 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
140 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
141 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
142 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
143 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
144 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
145 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
146 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
147 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
148 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
149 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
150 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
151 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
153 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
154 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
155 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
156 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
157 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
158 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
160 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
162 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
163 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
165 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
166 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
168 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
169 #endif /* __NetBSD__ */
173 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
174 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
175 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
176 TRAP_x86_NMI = 2, /* NMI interrupt */
177 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
178 TRAP_x86_OFLOW = 4, /* Overflow exception */
179 TRAP_x86_BOUND = 5, /* Bound range exception */
180 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
181 TRAP_x86_DNA = 7, /* Device not available exception */
182 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
183 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
184 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
185 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
186 TRAP_x86_STKFLT = 12, /* Stack fault */
187 TRAP_x86_PROTFLT = 13, /* General protection fault */
188 TRAP_x86_PAGEFLT = 14, /* Page fault */
189 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
190 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
191 TRAP_x86_MCHK = 18, /* Machine check exception */
192 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
195 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
196 static size_t signal_stack_size;
198 typedef void (*raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
199 typedef int (*wine_signal_handler)(unsigned int sig);
201 static wine_signal_handler handlers[256];
204 /***********************************************************************
205 * Definitions for Win32 unwind tables
210 RUNTIME_FUNCTION chain;
228 BYTE frame_offset : 4;
229 struct opcode opcodes[1]; /* info->count entries */
230 /* followed by handler_data */
233 #define UWOP_PUSH_NONVOL 0
234 #define UWOP_ALLOC_LARGE 1
235 #define UWOP_ALLOC_SMALL 2
236 #define UWOP_SET_FPREG 3
237 #define UWOP_SAVE_NONVOL 4
238 #define UWOP_SAVE_NONVOL_FAR 5
239 #define UWOP_SAVE_XMM128 8
240 #define UWOP_SAVE_XMM128_FAR 9
241 #define UWOP_PUSH_MACHFRAME 10
243 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
245 static const char * const reg_names[16] =
246 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
247 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
249 union handler_data *handler_data;
250 struct UNWIND_INFO *info;
251 unsigned int i, count;
253 TRACE( "**** func %x-%x\n", function->BeginAddress, function->EndAddress );
256 if (function->UnwindData & 1)
258 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
259 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
260 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
261 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
265 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
267 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
268 info, info->flags, info->prolog,
269 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
272 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
273 reg_names[info->frame_reg], info->frame_offset * 16 );
275 for (i = 0; i < info->count; i++)
277 TRACE( " 0x%x: ", info->opcodes[i].offset );
278 switch (info->opcodes[i].code)
280 case UWOP_PUSH_NONVOL:
281 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
283 case UWOP_ALLOC_LARGE:
284 if (info->opcodes[i].info)
286 count = *(DWORD *)&info->opcodes[i+1];
291 count = *(USHORT *)&info->opcodes[i+1] * 8;
294 TRACE( "subq $0x%x,%%rsp\n", count );
296 case UWOP_ALLOC_SMALL:
297 count = (info->opcodes[i].info + 1) * 8;
298 TRACE( "subq $0x%x,%%rsp\n", count );
301 TRACE( "leaq 0x%x(%%rsp),%s\n",
302 info->frame_offset * 16, reg_names[info->frame_reg] );
304 case UWOP_SAVE_NONVOL:
305 count = *(USHORT *)&info->opcodes[i+1] * 8;
306 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
309 case UWOP_SAVE_NONVOL_FAR:
310 count = *(DWORD *)&info->opcodes[i+1];
311 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
314 case UWOP_SAVE_XMM128:
315 count = *(USHORT *)&info->opcodes[i+1] * 16;
316 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
319 case UWOP_SAVE_XMM128_FAR:
320 count = *(DWORD *)&info->opcodes[i+1];
321 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
324 case UWOP_PUSH_MACHFRAME:
325 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
328 FIXME( "unknown code %u\n", info->opcodes[i].code );
333 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
334 if (info->flags & UNW_FLAG_CHAININFO)
336 TRACE( " chained to function %p-%p\n",
337 (char *)base + handler_data->chain.BeginAddress,
338 (char *)base + handler_data->chain.EndAddress );
339 function = &handler_data->chain;
342 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
343 TRACE( " handler %p data at %p\n",
344 (char *)base + handler_data->handler, &handler_data->handler + 1 );
349 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
353 TRACE( "scope table at %p\n", table );
354 for (i = 0; i < table->Count; i++)
355 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
356 base + table->ScopeRecord[i].BeginAddress,
357 base + table->ScopeRecord[i].EndAddress,
358 base + table->ScopeRecord[i].HandlerAddress,
359 base + table->ScopeRecord[i].JumpTarget );
363 /***********************************************************************
364 * Definitions for Dwarf unwind tables
367 enum dwarf_call_frame_info
369 DW_CFA_advance_loc = 0x40,
370 DW_CFA_offset = 0x80,
371 DW_CFA_restore = 0xc0,
373 DW_CFA_set_loc = 0x01,
374 DW_CFA_advance_loc1 = 0x02,
375 DW_CFA_advance_loc2 = 0x03,
376 DW_CFA_advance_loc4 = 0x04,
377 DW_CFA_offset_extended = 0x05,
378 DW_CFA_restore_extended = 0x06,
379 DW_CFA_undefined = 0x07,
380 DW_CFA_same_value = 0x08,
381 DW_CFA_register = 0x09,
382 DW_CFA_remember_state = 0x0a,
383 DW_CFA_restore_state = 0x0b,
384 DW_CFA_def_cfa = 0x0c,
385 DW_CFA_def_cfa_register = 0x0d,
386 DW_CFA_def_cfa_offset = 0x0e,
387 DW_CFA_def_cfa_expression = 0x0f,
388 DW_CFA_expression = 0x10,
389 DW_CFA_offset_extended_sf = 0x11,
390 DW_CFA_def_cfa_sf = 0x12,
391 DW_CFA_def_cfa_offset_sf = 0x13,
392 DW_CFA_val_offset = 0x14,
393 DW_CFA_val_offset_sf = 0x15,
394 DW_CFA_val_expression = 0x16,
401 DW_OP_const1u = 0x08,
402 DW_OP_const1s = 0x09,
403 DW_OP_const2u = 0x0a,
404 DW_OP_const2s = 0x0b,
405 DW_OP_const4u = 0x0c,
406 DW_OP_const4s = 0x0d,
407 DW_OP_const8u = 0x0e,
408 DW_OP_const8s = 0x0f,
428 DW_OP_plus_uconst = 0x23,
541 DW_OP_deref_size = 0x94,
542 DW_OP_xderef_size = 0x95,
544 DW_OP_push_object_address = 0x97,
547 DW_OP_call_ref = 0x9a,
548 DW_OP_form_tls_address = 0x9b,
549 DW_OP_call_frame_cfa = 0x9c,
550 DW_OP_bit_piece = 0x9d,
551 DW_OP_lo_user = 0xe0,
552 DW_OP_hi_user = 0xff,
553 DW_OP_GNU_push_tls_address = 0xe0,
554 DW_OP_GNU_uninit = 0xf0,
555 DW_OP_GNU_encoded_addr = 0xf1,
558 #define DW_EH_PE_native 0x00
559 #define DW_EH_PE_leb128 0x01
560 #define DW_EH_PE_data2 0x02
561 #define DW_EH_PE_data4 0x03
562 #define DW_EH_PE_data8 0x04
563 #define DW_EH_PE_signed 0x08
564 #define DW_EH_PE_abs 0x00
565 #define DW_EH_PE_pcrel 0x10
566 #define DW_EH_PE_textrel 0x20
567 #define DW_EH_PE_datarel 0x30
568 #define DW_EH_PE_funcrel 0x40
569 #define DW_EH_PE_aligned 0x50
570 #define DW_EH_PE_indirect 0x80
571 #define DW_EH_PE_omit 0xff
573 struct dwarf_eh_bases
584 unsigned char version;
585 unsigned char augmentation[1];
591 unsigned int cie_offset;
594 extern const struct dwarf_fde *_Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
596 static unsigned char dwarf_get_u1( const unsigned char **p )
601 static unsigned short dwarf_get_u2( const unsigned char **p )
603 unsigned int ret = (*p)[0] | ((*p)[1] << 8);
608 static unsigned int dwarf_get_u4( const unsigned char **p )
610 unsigned int ret = (*p)[0] | ((*p)[1] << 8) | ((*p)[2] << 16) | ((*p)[3] << 24);
615 static ULONG64 dwarf_get_u8( const unsigned char **p )
617 ULONG64 low = dwarf_get_u4( p );
618 ULONG64 high = dwarf_get_u4( p );
619 return low | (high << 32);
622 static ULONG_PTR dwarf_get_uleb128( const unsigned char **p )
625 unsigned int shift = 0;
631 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
634 } while (byte & 0x80);
638 static LONG_PTR dwarf_get_sleb128( const unsigned char **p )
641 unsigned int shift = 0;
647 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
650 } while (byte & 0x80);
652 if ((shift < 8 * sizeof(ret)) && (byte & 0x40)) ret |= -((ULONG_PTR)1 << shift);
656 static ULONG_PTR dwarf_get_ptr( const unsigned char **p, unsigned char encoding )
660 if (encoding == DW_EH_PE_omit) return 0;
662 switch (encoding & 0xf0)
668 base = (ULONG_PTR)*p;
671 FIXME( "unsupported encoding %02x\n", encoding );
675 switch (encoding & 0x0f)
677 case DW_EH_PE_native:
678 return base + dwarf_get_u8( p );
679 case DW_EH_PE_leb128:
680 return base + dwarf_get_uleb128( p );
682 return base + dwarf_get_u2( p );
684 return base + dwarf_get_u4( p );
686 return base + dwarf_get_u8( p );
687 case DW_EH_PE_signed|DW_EH_PE_leb128:
688 return base + dwarf_get_sleb128( p );
689 case DW_EH_PE_signed|DW_EH_PE_data2:
690 return base + (signed short)dwarf_get_u2( p );
691 case DW_EH_PE_signed|DW_EH_PE_data4:
692 return base + (signed int)dwarf_get_u4( p );
693 case DW_EH_PE_signed|DW_EH_PE_data8:
694 return base + (LONG64)dwarf_get_u8( p );
696 FIXME( "unsupported encoding %02x\n", encoding );
703 RULE_UNSET, /* not set at all */
704 RULE_UNDEFINED, /* undefined value */
705 RULE_SAME, /* same value as previous frame */
706 RULE_CFA_OFFSET, /* stored at cfa offset */
707 RULE_OTHER_REG, /* stored in other register */
708 RULE_EXPRESSION, /* address specified by expression */
709 RULE_VAL_EXPRESSION /* value specified by expression */
712 #define NB_FRAME_REGS 41
713 #define MAX_SAVED_STATES 16
717 ULONG_PTR cfa_offset;
718 unsigned char cfa_reg;
719 enum reg_rule cfa_rule;
720 enum reg_rule rules[NB_FRAME_REGS];
721 ULONG64 regs[NB_FRAME_REGS];
727 ULONG_PTR code_align;
729 unsigned char retaddr_reg;
730 unsigned char fde_encoding;
731 unsigned char signal_frame;
732 unsigned char state_sp;
733 struct frame_state state;
734 struct frame_state *state_stack;
737 static const char *dwarf_reg_names[NB_FRAME_REGS] =
739 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
740 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
741 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
742 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
743 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
746 static int valid_reg( ULONG_PTR reg )
748 if (reg >= NB_FRAME_REGS) FIXME( "unsupported reg %lx\n", reg );
749 return (reg < NB_FRAME_REGS);
752 static void execute_cfa_instructions( const unsigned char *ptr, const unsigned char *end,
753 ULONG_PTR last_ip, struct frame_info *info )
755 while (ptr < end && info->ip < last_ip + info->signal_frame)
757 enum dwarf_call_frame_info op = *ptr++;
763 case DW_CFA_advance_loc:
765 ULONG_PTR offset = (op & 0x3f) * info->code_align;
766 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info->ip, offset );
772 ULONG_PTR reg = op & 0x3f;
773 LONG_PTR offset = dwarf_get_uleb128( &ptr ) * info->data_align;
774 if (!valid_reg( reg )) break;
775 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
776 info->state.regs[reg] = offset;
777 info->state.rules[reg] = RULE_CFA_OFFSET;
782 ULONG_PTR reg = op & 0x3f;
783 if (!valid_reg( reg )) break;
784 TRACE( "%lx: DW_CFA_restore %s\n", info->ip, dwarf_reg_names[reg] );
785 info->state.rules[reg] = RULE_UNSET;
796 ULONG_PTR loc = dwarf_get_ptr( &ptr, info->fde_encoding );
797 TRACE( "%lx: DW_CFA_set_loc %lx\n", info->ip, loc );
801 case DW_CFA_advance_loc1:
803 ULONG_PTR offset = *ptr++ * info->code_align;
804 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset );
808 case DW_CFA_advance_loc2:
810 ULONG_PTR offset = dwarf_get_u2( &ptr ) * info->code_align;
811 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset );
815 case DW_CFA_advance_loc4:
817 ULONG_PTR offset = dwarf_get_u4( &ptr ) * info->code_align;
818 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset );
822 case DW_CFA_offset_extended:
823 case DW_CFA_offset_extended_sf:
825 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
826 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf_get_uleb128( &ptr ) * info->data_align
827 : dwarf_get_sleb128( &ptr ) * info->data_align;
828 if (!valid_reg( reg )) break;
829 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
830 info->state.regs[reg] = offset;
831 info->state.rules[reg] = RULE_CFA_OFFSET;
834 case DW_CFA_restore_extended:
836 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
837 if (!valid_reg( reg )) break;
838 TRACE( "%lx: DW_CFA_restore_extended %s\n", info->ip, dwarf_reg_names[reg] );
839 info->state.rules[reg] = RULE_UNSET;
842 case DW_CFA_undefined:
844 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
845 if (!valid_reg( reg )) break;
846 TRACE( "%lx: DW_CFA_undefined %s\n", info->ip, dwarf_reg_names[reg] );
847 info->state.rules[reg] = RULE_UNDEFINED;
850 case DW_CFA_same_value:
852 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
853 if (!valid_reg( reg )) break;
854 TRACE( "%lx: DW_CFA_same_value %s\n", info->ip, dwarf_reg_names[reg] );
855 info->state.regs[reg] = reg;
856 info->state.rules[reg] = RULE_SAME;
859 case DW_CFA_register:
861 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
862 ULONG_PTR reg2 = dwarf_get_uleb128( &ptr );
863 if (!valid_reg( reg ) || !valid_reg( reg2 )) break;
864 TRACE( "%lx: DW_CFA_register %s == %s\n", info->ip, dwarf_reg_names[reg], dwarf_reg_names[reg2] );
865 info->state.regs[reg] = reg2;
866 info->state.rules[reg] = RULE_OTHER_REG;
869 case DW_CFA_remember_state:
870 TRACE( "%lx: DW_CFA_remember_state\n", info->ip );
871 if (info->state_sp >= MAX_SAVED_STATES)
872 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info->ip );
874 info->state_stack[info->state_sp++] = info->state;
876 case DW_CFA_restore_state:
877 TRACE( "%lx: DW_CFA_restore_state\n", info->ip );
879 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info->ip );
881 info->state = info->state_stack[--info->state_sp];
884 case DW_CFA_def_cfa_sf:
886 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
887 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf_get_uleb128( &ptr )
888 : dwarf_get_sleb128( &ptr ) * info->data_align;
889 if (!valid_reg( reg )) break;
890 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info->ip, dwarf_reg_names[reg], offset );
891 info->state.cfa_reg = reg;
892 info->state.cfa_offset = offset;
893 info->state.cfa_rule = RULE_CFA_OFFSET;
896 case DW_CFA_def_cfa_register:
898 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
899 if (!valid_reg( reg )) break;
900 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info->ip, dwarf_reg_names[reg] );
901 info->state.cfa_reg = reg;
902 info->state.cfa_rule = RULE_CFA_OFFSET;
905 case DW_CFA_def_cfa_offset:
906 case DW_CFA_def_cfa_offset_sf:
908 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf_get_uleb128( &ptr )
909 : dwarf_get_sleb128( &ptr ) * info->data_align;
910 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset );
911 info->state.cfa_offset = offset;
912 info->state.cfa_rule = RULE_CFA_OFFSET;
915 case DW_CFA_def_cfa_expression:
917 ULONG_PTR expr = (ULONG_PTR)ptr;
918 ULONG_PTR len = dwarf_get_uleb128( &ptr );
919 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len );
920 info->state.cfa_offset = expr;
921 info->state.cfa_rule = RULE_VAL_EXPRESSION;
925 case DW_CFA_expression:
926 case DW_CFA_val_expression:
928 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
929 ULONG_PTR expr = (ULONG_PTR)ptr;
930 ULONG_PTR len = dwarf_get_uleb128( &ptr );
931 if (!valid_reg( reg )) break;
932 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
933 info->ip, (op == DW_CFA_expression) ? "" : "val_", dwarf_reg_names[reg], expr, expr+len );
934 info->state.regs[reg] = expr;
935 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
940 FIXME( "%lx: unknown CFA opcode %02x\n", info->ip, op );
946 /* retrieve a context register from its dwarf number */
947 static void *get_context_reg( CONTEXT *context, ULONG_PTR dw_reg )
951 case 0: return &context->Rax;
952 case 1: return &context->Rdx;
953 case 2: return &context->Rcx;
954 case 3: return &context->Rbx;
955 case 4: return &context->Rsi;
956 case 5: return &context->Rdi;
957 case 6: return &context->Rbp;
958 case 7: return &context->Rsp;
959 case 8: return &context->R8;
960 case 9: return &context->R9;
961 case 10: return &context->R10;
962 case 11: return &context->R11;
963 case 12: return &context->R12;
964 case 13: return &context->R13;
965 case 14: return &context->R14;
966 case 15: return &context->R15;
967 case 16: return &context->Rip;
968 case 17: return &context->u.s.Xmm0;
969 case 18: return &context->u.s.Xmm1;
970 case 19: return &context->u.s.Xmm2;
971 case 20: return &context->u.s.Xmm3;
972 case 21: return &context->u.s.Xmm4;
973 case 22: return &context->u.s.Xmm5;
974 case 23: return &context->u.s.Xmm6;
975 case 24: return &context->u.s.Xmm7;
976 case 25: return &context->u.s.Xmm8;
977 case 26: return &context->u.s.Xmm9;
978 case 27: return &context->u.s.Xmm10;
979 case 28: return &context->u.s.Xmm11;
980 case 29: return &context->u.s.Xmm12;
981 case 30: return &context->u.s.Xmm13;
982 case 31: return &context->u.s.Xmm14;
983 case 32: return &context->u.s.Xmm15;
984 case 33: return &context->u.s.Legacy[0];
985 case 34: return &context->u.s.Legacy[1];
986 case 35: return &context->u.s.Legacy[2];
987 case 36: return &context->u.s.Legacy[3];
988 case 37: return &context->u.s.Legacy[4];
989 case 38: return &context->u.s.Legacy[5];
990 case 39: return &context->u.s.Legacy[6];
991 case 40: return &context->u.s.Legacy[7];
992 default: return NULL;
996 /* set a context register from its dwarf number */
997 static void set_context_reg( CONTEXT *context, ULONG_PTR dw_reg, void *val )
1001 case 0: context->Rax = *(ULONG64 *)val; break;
1002 case 1: context->Rdx = *(ULONG64 *)val; break;
1003 case 2: context->Rcx = *(ULONG64 *)val; break;
1004 case 3: context->Rbx = *(ULONG64 *)val; break;
1005 case 4: context->Rsi = *(ULONG64 *)val; break;
1006 case 5: context->Rdi = *(ULONG64 *)val; break;
1007 case 6: context->Rbp = *(ULONG64 *)val; break;
1008 case 7: context->Rsp = *(ULONG64 *)val; break;
1009 case 8: context->R8 = *(ULONG64 *)val; break;
1010 case 9: context->R9 = *(ULONG64 *)val; break;
1011 case 10: context->R10 = *(ULONG64 *)val; break;
1012 case 11: context->R11 = *(ULONG64 *)val; break;
1013 case 12: context->R12 = *(ULONG64 *)val; break;
1014 case 13: context->R13 = *(ULONG64 *)val; break;
1015 case 14: context->R14 = *(ULONG64 *)val; break;
1016 case 15: context->R15 = *(ULONG64 *)val; break;
1017 case 16: context->Rip = *(ULONG64 *)val; break;
1018 case 17: context->u.s.Xmm0 = *(M128A *)val; break;
1019 case 18: context->u.s.Xmm1 = *(M128A *)val; break;
1020 case 19: context->u.s.Xmm2 = *(M128A *)val; break;
1021 case 20: context->u.s.Xmm3 = *(M128A *)val; break;
1022 case 21: context->u.s.Xmm4 = *(M128A *)val; break;
1023 case 22: context->u.s.Xmm5 = *(M128A *)val; break;
1024 case 23: context->u.s.Xmm6 = *(M128A *)val; break;
1025 case 24: context->u.s.Xmm7 = *(M128A *)val; break;
1026 case 25: context->u.s.Xmm8 = *(M128A *)val; break;
1027 case 26: context->u.s.Xmm9 = *(M128A *)val; break;
1028 case 27: context->u.s.Xmm10 = *(M128A *)val; break;
1029 case 28: context->u.s.Xmm11 = *(M128A *)val; break;
1030 case 29: context->u.s.Xmm12 = *(M128A *)val; break;
1031 case 30: context->u.s.Xmm13 = *(M128A *)val; break;
1032 case 31: context->u.s.Xmm14 = *(M128A *)val; break;
1033 case 32: context->u.s.Xmm15 = *(M128A *)val; break;
1034 case 33: context->u.s.Legacy[0] = *(M128A *)val; break;
1035 case 34: context->u.s.Legacy[1] = *(M128A *)val; break;
1036 case 35: context->u.s.Legacy[2] = *(M128A *)val; break;
1037 case 36: context->u.s.Legacy[3] = *(M128A *)val; break;
1038 case 37: context->u.s.Legacy[4] = *(M128A *)val; break;
1039 case 38: context->u.s.Legacy[5] = *(M128A *)val; break;
1040 case 39: context->u.s.Legacy[6] = *(M128A *)val; break;
1041 case 40: context->u.s.Legacy[7] = *(M128A *)val; break;
1045 static ULONG_PTR eval_expression( const unsigned char *p, CONTEXT *context )
1047 ULONG_PTR reg, tmp, stack[64];
1049 ULONG_PTR len = dwarf_get_uleb128(&p);
1050 const unsigned char *end = p + len;
1054 unsigned char opcode = dwarf_get_u1(&p);
1056 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
1057 stack[++sp] = opcode - DW_OP_lit0;
1058 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
1059 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_reg0 );
1060 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
1061 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_breg0 ) + dwarf_get_sleb128(&p);
1062 else switch (opcode)
1064 case DW_OP_nop: break;
1065 case DW_OP_addr: stack[++sp] = dwarf_get_u8(&p); break;
1066 case DW_OP_const1u: stack[++sp] = dwarf_get_u1(&p); break;
1067 case DW_OP_const1s: stack[++sp] = (signed char)dwarf_get_u1(&p); break;
1068 case DW_OP_const2u: stack[++sp] = dwarf_get_u2(&p); break;
1069 case DW_OP_const2s: stack[++sp] = (short)dwarf_get_u2(&p); break;
1070 case DW_OP_const4u: stack[++sp] = dwarf_get_u4(&p); break;
1071 case DW_OP_const4s: stack[++sp] = (signed int)dwarf_get_u4(&p); break;
1072 case DW_OP_const8u: stack[++sp] = dwarf_get_u8(&p); break;
1073 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf_get_u8(&p); break;
1074 case DW_OP_constu: stack[++sp] = dwarf_get_uleb128(&p); break;
1075 case DW_OP_consts: stack[++sp] = dwarf_get_sleb128(&p); break;
1076 case DW_OP_deref: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1077 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
1078 case DW_OP_drop: sp--; break;
1079 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
1080 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf_get_u1(&p)]; sp++; break;
1081 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
1082 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
1083 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
1084 case DW_OP_neg: stack[sp] = -stack[sp]; break;
1085 case DW_OP_not: stack[sp] = ~stack[sp]; break;
1086 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
1087 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
1088 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
1089 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
1090 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
1091 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
1092 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
1093 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
1094 case DW_OP_plus_uconst: stack[sp] += dwarf_get_uleb128(&p); break;
1095 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
1096 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
1097 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
1098 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
1099 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
1100 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
1101 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
1102 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
1103 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
1104 case DW_OP_skip: tmp = (short)dwarf_get_u2(&p); p += tmp; break;
1105 case DW_OP_bra: tmp = (short)dwarf_get_u2(&p); if (!stack[sp--]) p += tmp; break;
1106 case DW_OP_GNU_encoded_addr: tmp = *p++; stack[++sp] = dwarf_get_ptr( &p, tmp ); break;
1107 case DW_OP_regx: stack[++sp] = *(ULONG_PTR *)get_context_reg( context, dwarf_get_uleb128(&p) ); break;
1109 reg = dwarf_get_uleb128(&p);
1110 tmp = dwarf_get_sleb128(&p);
1111 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, reg ) + tmp;
1113 case DW_OP_deref_size:
1116 case 1: stack[sp] = *(unsigned char *)stack[sp]; break;
1117 case 2: stack[sp] = *(unsigned short *)stack[sp]; break;
1118 case 4: stack[sp] = *(unsigned int *)stack[sp]; break;
1119 case 8: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1123 FIXME( "unhandled opcode %02x\n", opcode );
1129 /* apply the computed frame info to the actual context */
1130 static void apply_frame_state( CONTEXT *context, struct frame_state *state )
1133 ULONG_PTR cfa, value;
1134 CONTEXT new_context = *context;
1136 switch (state->cfa_rule)
1138 case RULE_EXPRESSION:
1139 cfa = *(ULONG_PTR *)eval_expression( (const unsigned char *)state->cfa_offset, context );
1141 case RULE_VAL_EXPRESSION:
1142 cfa = eval_expression( (const unsigned char *)state->cfa_offset, context );
1145 cfa = *(ULONG_PTR *)get_context_reg( context, state->cfa_reg ) + state->cfa_offset;
1150 for (i = 0; i < NB_FRAME_REGS; i++)
1152 switch (state->rules[i])
1155 case RULE_UNDEFINED:
1158 case RULE_CFA_OFFSET:
1159 set_context_reg( &new_context, i, (char *)cfa + state->regs[i] );
1161 case RULE_OTHER_REG:
1162 set_context_reg( &new_context, i, get_context_reg( context, state->regs[i] ));
1164 case RULE_EXPRESSION:
1165 value = eval_expression( (const unsigned char *)state->regs[i], context );
1166 set_context_reg( &new_context, i, (void *)value );
1168 case RULE_VAL_EXPRESSION:
1169 value = eval_expression( (const unsigned char *)state->regs[i], context );
1170 set_context_reg( &new_context, i, &value );
1174 new_context.Rsp = cfa;
1175 *context = new_context;
1179 /***********************************************************************
1180 * dwarf_virtual_unwind
1182 * Equivalent of RtlVirtualUnwind for builtin modules.
1184 static NTSTATUS dwarf_virtual_unwind( ULONG64 ip, ULONG64 *frame,CONTEXT *context,
1185 const struct dwarf_fde *fde, const struct dwarf_eh_bases *bases,
1186 PEXCEPTION_ROUTINE *handler, void **handler_data )
1188 const struct dwarf_cie *cie;
1189 const unsigned char *ptr, *augmentation, *end;
1190 ULONG_PTR len, code_end;
1191 struct frame_info info;
1192 struct frame_state state_stack[MAX_SAVED_STATES];
1193 int aug_z_format = 0;
1194 unsigned char lsda_encoding = DW_EH_PE_omit;
1196 memset( &info, 0, sizeof(info) );
1197 info.state_stack = state_stack;
1198 info.ip = (ULONG_PTR)bases->func;
1201 cie = (const struct dwarf_cie *)((const char *)&fde->cie_offset - fde->cie_offset);
1203 /* parse the CIE first */
1205 if (cie->version != 1)
1207 FIXME( "unknown CIE version %u at %p\n", cie->version, cie );
1208 return STATUS_INVALID_DISPOSITION;
1210 ptr = cie->augmentation + strlen((const char *)cie->augmentation) + 1;
1212 info.code_align = dwarf_get_uleb128( &ptr );
1213 info.data_align = dwarf_get_sleb128( &ptr );
1214 info.retaddr_reg = *ptr++;
1215 info.state.cfa_rule = RULE_CFA_OFFSET;
1217 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1218 ip, bases->func, cie, cie->length, cie->id, cie->version, cie->augmentation,
1219 info.code_align, info.data_align, dwarf_reg_names[info.retaddr_reg] );
1222 for (augmentation = cie->augmentation; *augmentation; augmentation++)
1224 switch (*augmentation)
1227 len = dwarf_get_uleb128( &ptr );
1232 lsda_encoding = *ptr++;
1236 unsigned char encoding = *ptr++;
1237 *handler = (void *)dwarf_get_ptr( &ptr, encoding );
1241 info.fde_encoding = *ptr++;
1244 info.signal_frame = 1;
1247 FIXME( "unknown augmentation '%c'\n", *augmentation );
1248 if (!end) return STATUS_INVALID_DISPOSITION; /* cannot continue */
1253 end = (const unsigned char *)(&cie->length + 1) + cie->length;
1254 execute_cfa_instructions( ptr, end, ip, &info );
1256 ptr = (const unsigned char *)(fde + 1);
1257 info.ip = dwarf_get_ptr( &ptr, info.fde_encoding ); /* fde code start */
1258 code_end = info.ip + dwarf_get_ptr( &ptr, info.fde_encoding & 0x0f ); /* fde code length */
1260 if (aug_z_format) /* get length of augmentation data */
1262 len = dwarf_get_uleb128( &ptr );
1267 *handler_data = (void *)dwarf_get_ptr( &ptr, lsda_encoding );
1270 end = (const unsigned char *)(&fde->length + 1) + fde->length;
1271 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1272 fde, fde->length, *handler, *handler_data, info.ip, code_end );
1273 execute_cfa_instructions( ptr, end, ip, &info );
1274 apply_frame_state( context, &info.state );
1275 *frame = context->Rsp;
1277 TRACE( "next function rip=%016lx\n", context->Rip );
1278 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1279 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1280 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1281 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1282 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1283 context->R8, context->R9, context->R10, context->R11 );
1284 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1285 context->R12, context->R13, context->R14, context->R15 );
1287 return STATUS_SUCCESS;
1291 /***********************************************************************
1294 static inline int dispatch_signal(unsigned int sig)
1296 if (handlers[sig] == NULL) return 0;
1297 return handlers[sig](sig);
1300 /***********************************************************************
1303 * Get the base of the signal stack for the current thread.
1305 static inline void *get_signal_stack(void)
1307 return (char *)NtCurrentTeb() + teb_size;
1310 /***********************************************************************
1311 * is_inside_signal_stack
1313 * Check if pointer is inside the signal stack.
1315 static inline int is_inside_signal_stack( void *ptr )
1317 return ((char *)ptr >= (char *)get_signal_stack() &&
1318 (char *)ptr < (char *)get_signal_stack() + signal_stack_size);
1321 /***********************************************************************
1324 * Set the register values from a sigcontext.
1326 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
1328 context->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
1329 context->Rax = RAX_sig(sigcontext);
1330 context->Rcx = RCX_sig(sigcontext);
1331 context->Rdx = RDX_sig(sigcontext);
1332 context->Rbx = RBX_sig(sigcontext);
1333 context->Rsp = RSP_sig(sigcontext);
1334 context->Rbp = RBP_sig(sigcontext);
1335 context->Rsi = RSI_sig(sigcontext);
1336 context->Rdi = RDI_sig(sigcontext);
1337 context->R8 = R8_sig(sigcontext);
1338 context->R9 = R9_sig(sigcontext);
1339 context->R10 = R10_sig(sigcontext);
1340 context->R11 = R11_sig(sigcontext);
1341 context->R12 = R12_sig(sigcontext);
1342 context->R13 = R13_sig(sigcontext);
1343 context->R14 = R14_sig(sigcontext);
1344 context->R15 = R15_sig(sigcontext);
1345 context->Rip = RIP_sig(sigcontext);
1346 context->SegCs = CS_sig(sigcontext);
1347 context->SegFs = FS_sig(sigcontext);
1348 context->SegGs = GS_sig(sigcontext);
1349 context->EFlags = EFL_sig(sigcontext);
1351 context->SegDs = DS_sig(sigcontext);
1353 __asm__("movw %%ds,%0" : "=m" (context->SegDs));
1356 context->SegEs = ES_sig(sigcontext);
1358 __asm__("movw %%es,%0" : "=m" (context->SegEs));
1361 context->SegSs = SS_sig(sigcontext);
1363 __asm__("movw %%ss,%0" : "=m" (context->SegSs));
1365 if (FPU_sig(sigcontext))
1367 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1368 context->u.FltSave = *FPU_sig(sigcontext);
1369 context->MxCsr = context->u.FltSave.MxCsr;
1374 /***********************************************************************
1377 * Build a sigcontext from the register values.
1379 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1381 RAX_sig(sigcontext) = context->Rax;
1382 RCX_sig(sigcontext) = context->Rcx;
1383 RDX_sig(sigcontext) = context->Rdx;
1384 RBX_sig(sigcontext) = context->Rbx;
1385 RSP_sig(sigcontext) = context->Rsp;
1386 RBP_sig(sigcontext) = context->Rbp;
1387 RSI_sig(sigcontext) = context->Rsi;
1388 RDI_sig(sigcontext) = context->Rdi;
1389 R8_sig(sigcontext) = context->R8;
1390 R9_sig(sigcontext) = context->R9;
1391 R10_sig(sigcontext) = context->R10;
1392 R11_sig(sigcontext) = context->R11;
1393 R12_sig(sigcontext) = context->R12;
1394 R13_sig(sigcontext) = context->R13;
1395 R14_sig(sigcontext) = context->R14;
1396 R15_sig(sigcontext) = context->R15;
1397 RIP_sig(sigcontext) = context->Rip;
1398 CS_sig(sigcontext) = context->SegCs;
1399 FS_sig(sigcontext) = context->SegFs;
1400 GS_sig(sigcontext) = context->SegGs;
1401 EFL_sig(sigcontext) = context->EFlags;
1403 DS_sig(sigcontext) = context->SegDs;
1406 ES_sig(sigcontext) = context->SegEs;
1409 SS_sig(sigcontext) = context->SegSs;
1411 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
1415 /***********************************************************************
1416 * RtlCaptureContext (NTDLL.@)
1418 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
1422 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
1425 /***********************************************************************
1428 * Set the new CPU context.
1430 void set_cpu_context( const CONTEXT *context )
1432 extern void CDECL __wine_restore_regs( const CONTEXT * ) DECLSPEC_NORETURN;
1433 __wine_restore_regs( context );
1437 /***********************************************************************
1440 * Copy a register context according to the flags.
1442 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1444 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
1445 if (flags & CONTEXT_CONTROL)
1447 to->Rbp = from->Rbp;
1448 to->Rip = from->Rip;
1449 to->Rsp = from->Rsp;
1450 to->SegCs = from->SegCs;
1451 to->SegSs = from->SegSs;
1452 to->EFlags = from->EFlags;
1454 if (flags & CONTEXT_INTEGER)
1456 to->Rax = from->Rax;
1457 to->Rcx = from->Rcx;
1458 to->Rdx = from->Rdx;
1459 to->Rbx = from->Rbx;
1460 to->Rsi = from->Rsi;
1461 to->Rdi = from->Rdi;
1464 to->R10 = from->R10;
1465 to->R11 = from->R11;
1466 to->R12 = from->R12;
1467 to->R13 = from->R13;
1468 to->R14 = from->R14;
1469 to->R15 = from->R15;
1471 if (flags & CONTEXT_SEGMENTS)
1473 to->SegDs = from->SegDs;
1474 to->SegEs = from->SegEs;
1475 to->SegFs = from->SegFs;
1476 to->SegGs = from->SegGs;
1478 if (flags & CONTEXT_FLOATING_POINT)
1480 to->MxCsr = from->MxCsr;
1481 to->u.FltSave = from->u.FltSave;
1483 if (flags & CONTEXT_DEBUG_REGISTERS)
1485 to->Dr0 = from->Dr0;
1486 to->Dr1 = from->Dr1;
1487 to->Dr2 = from->Dr2;
1488 to->Dr3 = from->Dr3;
1489 to->Dr6 = from->Dr6;
1490 to->Dr7 = from->Dr7;
1495 /***********************************************************************
1498 * Convert a register context to the server format.
1500 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1502 DWORD flags = from->ContextFlags & ~CONTEXT_AMD64; /* get rid of CPU id */
1504 memset( to, 0, sizeof(*to) );
1505 to->cpu = CPU_x86_64;
1507 if (flags & CONTEXT_CONTROL)
1509 to->flags |= SERVER_CTX_CONTROL;
1510 to->ctl.x86_64_regs.rbp = from->Rbp;
1511 to->ctl.x86_64_regs.rip = from->Rip;
1512 to->ctl.x86_64_regs.rsp = from->Rsp;
1513 to->ctl.x86_64_regs.cs = from->SegCs;
1514 to->ctl.x86_64_regs.ss = from->SegSs;
1515 to->ctl.x86_64_regs.flags = from->EFlags;
1517 if (flags & CONTEXT_INTEGER)
1519 to->flags |= SERVER_CTX_INTEGER;
1520 to->integer.x86_64_regs.rax = from->Rax;
1521 to->integer.x86_64_regs.rcx = from->Rcx;
1522 to->integer.x86_64_regs.rdx = from->Rdx;
1523 to->integer.x86_64_regs.rbx = from->Rbx;
1524 to->integer.x86_64_regs.rsi = from->Rsi;
1525 to->integer.x86_64_regs.rdi = from->Rdi;
1526 to->integer.x86_64_regs.r8 = from->R8;
1527 to->integer.x86_64_regs.r9 = from->R9;
1528 to->integer.x86_64_regs.r10 = from->R10;
1529 to->integer.x86_64_regs.r11 = from->R11;
1530 to->integer.x86_64_regs.r12 = from->R12;
1531 to->integer.x86_64_regs.r13 = from->R13;
1532 to->integer.x86_64_regs.r14 = from->R14;
1533 to->integer.x86_64_regs.r15 = from->R15;
1535 if (flags & CONTEXT_SEGMENTS)
1537 to->flags |= SERVER_CTX_SEGMENTS;
1538 to->seg.x86_64_regs.ds = from->SegDs;
1539 to->seg.x86_64_regs.es = from->SegEs;
1540 to->seg.x86_64_regs.fs = from->SegFs;
1541 to->seg.x86_64_regs.gs = from->SegGs;
1543 if (flags & CONTEXT_FLOATING_POINT)
1545 to->flags |= SERVER_CTX_FLOATING_POINT;
1546 memcpy( to->fp.x86_64_regs.fpregs, &from->u.FltSave, sizeof(to->fp.x86_64_regs.fpregs) );
1548 if (flags & CONTEXT_DEBUG_REGISTERS)
1550 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1551 to->debug.x86_64_regs.dr0 = from->Dr0;
1552 to->debug.x86_64_regs.dr1 = from->Dr1;
1553 to->debug.x86_64_regs.dr2 = from->Dr2;
1554 to->debug.x86_64_regs.dr3 = from->Dr3;
1555 to->debug.x86_64_regs.dr6 = from->Dr6;
1556 to->debug.x86_64_regs.dr7 = from->Dr7;
1558 return STATUS_SUCCESS;
1562 /***********************************************************************
1563 * context_from_server
1565 * Convert a register context from the server format.
1567 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1569 if (from->cpu != CPU_x86_64) return STATUS_INVALID_PARAMETER;
1571 to->ContextFlags = CONTEXT_AMD64;
1572 if (from->flags & SERVER_CTX_CONTROL)
1574 to->ContextFlags |= CONTEXT_CONTROL;
1575 to->Rbp = from->ctl.x86_64_regs.rbp;
1576 to->Rip = from->ctl.x86_64_regs.rip;
1577 to->Rsp = from->ctl.x86_64_regs.rsp;
1578 to->SegCs = from->ctl.x86_64_regs.cs;
1579 to->SegSs = from->ctl.x86_64_regs.ss;
1580 to->EFlags = from->ctl.x86_64_regs.flags;
1583 if (from->flags & SERVER_CTX_INTEGER)
1585 to->ContextFlags |= CONTEXT_INTEGER;
1586 to->Rax = from->integer.x86_64_regs.rax;
1587 to->Rcx = from->integer.x86_64_regs.rcx;
1588 to->Rdx = from->integer.x86_64_regs.rdx;
1589 to->Rbx = from->integer.x86_64_regs.rbx;
1590 to->Rsi = from->integer.x86_64_regs.rsi;
1591 to->Rdi = from->integer.x86_64_regs.rdi;
1592 to->R8 = from->integer.x86_64_regs.r8;
1593 to->R9 = from->integer.x86_64_regs.r9;
1594 to->R10 = from->integer.x86_64_regs.r10;
1595 to->R11 = from->integer.x86_64_regs.r11;
1596 to->R12 = from->integer.x86_64_regs.r12;
1597 to->R13 = from->integer.x86_64_regs.r13;
1598 to->R14 = from->integer.x86_64_regs.r14;
1599 to->R15 = from->integer.x86_64_regs.r15;
1601 if (from->flags & SERVER_CTX_SEGMENTS)
1603 to->ContextFlags |= CONTEXT_SEGMENTS;
1604 to->SegDs = from->seg.x86_64_regs.ds;
1605 to->SegEs = from->seg.x86_64_regs.es;
1606 to->SegFs = from->seg.x86_64_regs.fs;
1607 to->SegGs = from->seg.x86_64_regs.gs;
1609 if (from->flags & SERVER_CTX_FLOATING_POINT)
1611 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1612 memcpy( &to->u.FltSave, from->fp.x86_64_regs.fpregs, sizeof(from->fp.x86_64_regs.fpregs) );
1613 to->MxCsr = to->u.FltSave.MxCsr;
1615 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1617 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1618 to->Dr0 = from->debug.x86_64_regs.dr0;
1619 to->Dr1 = from->debug.x86_64_regs.dr1;
1620 to->Dr2 = from->debug.x86_64_regs.dr2;
1621 to->Dr3 = from->debug.x86_64_regs.dr3;
1622 to->Dr6 = from->debug.x86_64_regs.dr6;
1623 to->Dr7 = from->debug.x86_64_regs.dr7;
1625 return STATUS_SUCCESS;
1629 extern void raise_func_trampoline( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
1630 __ASM_GLOBAL_FUNC( raise_func_trampoline,
1631 __ASM_CFI(".cfi_signal_frame\n\t")
1632 __ASM_CFI(".cfi_def_cfa %rbp,144\n\t") /* red zone + rip + rbp */
1633 __ASM_CFI(".cfi_rel_offset %rip,8\n\t")
1634 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1638 /***********************************************************************
1641 * Setup a proper stack frame for the raise function, and modify the
1642 * sigcontext so that the return from the signal handler will call
1643 * the raise function.
1645 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1650 EXCEPTION_RECORD rec;
1653 ULONG64 red_zone[16];
1656 DWORD exception_code = 0;
1658 stack = (struct stack_layout *)(RSP_sig(sigcontext) & ~15);
1660 /* stack sanity checks */
1662 if (is_inside_signal_stack( stack ))
1664 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1665 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1666 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1670 if (stack - 1 > stack || /* check for overflow in subtraction */
1671 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1672 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1674 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1675 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1676 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1678 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1680 /* stack overflow on last page, unrecoverable */
1681 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1682 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1683 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1684 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1685 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1688 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1690 /* stack access below stack limit, may be recoverable */
1691 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1694 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1695 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1696 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1697 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1698 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1703 stack--; /* push the stack_layout structure */
1704 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1705 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1706 #elif defined(VALGRIND_MAKE_WRITABLE)
1707 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1709 stack->rec.ExceptionRecord = NULL;
1710 stack->rec.ExceptionCode = exception_code;
1711 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1712 stack->rec.ExceptionAddress = (void *)RIP_sig(sigcontext);
1713 stack->rec.NumberParameters = 0;
1714 save_context( &stack->context, sigcontext );
1716 /* store return address and %rbp without aligning, so that the offset is fixed */
1717 rsp_ptr = (ULONG64 *)RSP_sig(sigcontext) - 16;
1718 *(--rsp_ptr) = RIP_sig(sigcontext);
1719 *(--rsp_ptr) = RBP_sig(sigcontext);
1721 /* now modify the sigcontext to return to the raise function */
1722 RIP_sig(sigcontext) = (ULONG_PTR)raise_func_trampoline;
1723 RDI_sig(sigcontext) = (ULONG_PTR)&stack->rec;
1724 RSI_sig(sigcontext) = (ULONG_PTR)&stack->context;
1725 RDX_sig(sigcontext) = (ULONG_PTR)func;
1726 RBP_sig(sigcontext) = (ULONG_PTR)rsp_ptr;
1727 RSP_sig(sigcontext) = (ULONG_PTR)stack;
1728 /* clear single-step, direction, and align check flag */
1729 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1735 /**********************************************************************
1736 * find_function_info
1738 static RUNTIME_FUNCTION *find_function_info( ULONG64 pc, HMODULE module,
1739 RUNTIME_FUNCTION *func, ULONG size )
1742 int max = size/sizeof(*func) - 1;
1746 int pos = (min + max) / 2;
1747 if ((char *)pc < (char *)module + func[pos].BeginAddress) max = pos - 1;
1748 else if ((char *)pc >= (char *)module + func[pos].EndAddress) min = pos + 1;
1752 while (func->UnwindData & 1) /* follow chained entry */
1753 func = (RUNTIME_FUNCTION *)((char *)module + (func->UnwindData & ~1));
1761 /**********************************************************************
1764 * Call a single exception handler.
1765 * FIXME: Handle nested exceptions.
1767 static NTSTATUS call_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch, CONTEXT *orig_context )
1771 dispatch->ControlPc = dispatch->ContextRecord->Rip;
1773 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
1774 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1775 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1776 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
1780 case ExceptionContinueExecution:
1781 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1782 *orig_context = *dispatch->ContextRecord;
1783 return STATUS_SUCCESS;
1784 case ExceptionContinueSearch:
1786 case ExceptionNestedException:
1789 return STATUS_INVALID_DISPOSITION;
1791 return STATUS_UNHANDLED_EXCEPTION;
1795 /**********************************************************************
1798 * Call a single exception handler from the TEB chain.
1799 * FIXME: Handle nested exceptions.
1801 static NTSTATUS call_teb_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
1802 EXCEPTION_REGISTRATION_RECORD *teb_frame, CONTEXT *orig_context )
1804 EXCEPTION_REGISTRATION_RECORD *dispatcher;
1807 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
1808 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1809 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1810 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
1814 case ExceptionContinueExecution:
1815 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1816 *orig_context = *dispatch->ContextRecord;
1817 return STATUS_SUCCESS;
1818 case ExceptionContinueSearch:
1820 case ExceptionNestedException:
1823 return STATUS_INVALID_DISPOSITION;
1825 return STATUS_UNHANDLED_EXCEPTION;
1829 /**********************************************************************
1830 * call_stack_handlers
1832 * Call the stack handlers chain.
1834 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
1836 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1837 UNWIND_HISTORY_TABLE table;
1838 DISPATCHER_CONTEXT dispatch;
1839 CONTEXT context, new_context;
1844 context = *orig_context;
1845 dispatch.TargetIp = 0;
1846 dispatch.ContextRecord = &context;
1847 dispatch.HistoryTable = &table;
1848 dispatch.ScopeIndex = 0; /* FIXME */
1851 new_context = context;
1853 /* FIXME: should use the history table to make things faster */
1856 dispatch.ImageBase = 0;
1858 /* first look for PE exception information */
1860 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
1862 RUNTIME_FUNCTION *dir;
1864 dispatch.ImageBase = (ULONG64)module->BaseAddress;
1865 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
1866 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
1868 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
1871 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, dispatch.ImageBase,
1872 context.Rip, dispatch.FunctionEntry,
1873 &new_context, &dispatch.HandlerData,
1874 &dispatch.EstablisherFrame, NULL );
1878 else if (!(module->Flags & LDR_WINE_INTERNAL))
1879 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
1882 /* then look for host system exception information */
1884 if (!module || (module->Flags & LDR_WINE_INTERNAL))
1886 struct dwarf_eh_bases bases;
1887 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
1891 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context,
1892 fde, &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
1893 if (status != STATUS_SUCCESS) return status;
1894 dispatch.FunctionEntry = NULL;
1895 if (dispatch.LanguageHandler && !module)
1897 FIXME( "calling personality routine in system library not supported yet\n" );
1898 dispatch.LanguageHandler = NULL;
1904 /* no exception information, treat as a leaf function */
1906 new_context.Rip = *(ULONG64 *)context.Rsp;
1907 new_context.Rsp = context.Rsp + sizeof(ULONG64);
1908 dispatch.EstablisherFrame = new_context.Rsp;
1909 dispatch.LanguageHandler = NULL;
1912 if (!dispatch.EstablisherFrame) break;
1914 if ((dispatch.EstablisherFrame & 7) ||
1915 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
1916 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
1918 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1919 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1920 rec->ExceptionFlags |= EH_STACK_INVALID;
1924 if (dispatch.LanguageHandler)
1926 status = call_handler( rec, &dispatch, orig_context );
1927 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
1929 /* hack: call wine handlers registered in the tib list */
1930 else while ((ULONG64)teb_frame < new_context.Rsp)
1932 TRACE( "found wine frame %p rsp %lx handler %p\n",
1933 teb_frame, new_context.Rsp, teb_frame->Handler );
1934 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1935 context = *orig_context;
1936 status = call_teb_handler( rec, &dispatch, teb_frame, orig_context );
1937 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
1938 teb_frame = teb_frame->Prev;
1941 if (new_context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
1942 context = new_context;
1944 return STATUS_UNHANDLED_EXCEPTION;
1948 /*******************************************************************
1951 * Implementation of NtRaiseException.
1953 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1961 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
1962 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
1963 context->Rip, GetCurrentThreadId() );
1964 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
1965 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
1966 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
1968 if (rec->ExceptionInformation[1] >> 16)
1969 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
1970 rec->ExceptionAddress,
1971 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
1973 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
1974 rec->ExceptionAddress,
1975 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
1979 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1980 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1981 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1982 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1983 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1984 context->R8, context->R9, context->R10, context->R11 );
1985 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1986 context->R12, context->R13, context->R14, context->R15 );
1988 status = send_debug_event( rec, TRUE, context );
1989 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
1990 return STATUS_SUCCESS;
1992 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
1993 return STATUS_SUCCESS;
1995 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
1999 /* last chance exception */
2001 status = send_debug_event( rec, FALSE, context );
2002 if (status != DBG_CONTINUE)
2004 if (rec->ExceptionFlags & EH_STACK_INVALID)
2005 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2006 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
2007 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2009 ERR("Unhandled exception code %x flags %x addr %p\n",
2010 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
2011 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
2013 return STATUS_SUCCESS;
2017 /**********************************************************************
2018 * raise_segv_exception
2020 static void raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2024 switch(rec->ExceptionCode)
2026 case EXCEPTION_ACCESS_VIOLATION:
2027 if (rec->NumberParameters == 2)
2029 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
2030 rec->ExceptionInformation[0] )))
2031 set_cpu_context( context );
2035 status = raise_exception( rec, context, TRUE );
2036 if (status) raise_status( status, rec );
2037 set_cpu_context( context );
2041 /**********************************************************************
2042 * raise_generic_exception
2044 * Generic raise function for exceptions that don't need special treatment.
2046 static void raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2048 NTSTATUS status = raise_exception( rec, context, TRUE );
2049 if (status) raise_status( status, rec );
2050 set_cpu_context( context );
2054 /**********************************************************************
2057 * Handler for SIGSEGV and related errors.
2059 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2061 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_segv_exception );
2062 ucontext_t *ucontext = sigcontext;
2064 switch(TRAP_sig(ucontext))
2066 case TRAP_x86_OFLOW: /* Overflow exception */
2067 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2069 case TRAP_x86_BOUND: /* Bound range exception */
2070 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2072 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2073 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2075 case TRAP_x86_STKFLT: /* Stack fault */
2076 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2078 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2079 case TRAP_x86_PROTFLT: /* General protection fault */
2080 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2081 rec->ExceptionCode = ERROR_sig(ucontext) ? EXCEPTION_ACCESS_VIOLATION : EXCEPTION_PRIV_INSTRUCTION;
2082 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2084 case TRAP_x86_PAGEFLT: /* Page fault */
2085 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2086 rec->NumberParameters = 2;
2087 rec->ExceptionInformation[0] = (ERROR_sig(ucontext) & 2) != 0;
2088 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2090 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2091 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2094 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext) );
2096 case TRAP_x86_NMI: /* NMI interrupt */
2097 case TRAP_x86_DNA: /* Device not available exception */
2098 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2099 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2100 case TRAP_x86_MCHK: /* Machine check exception */
2101 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2102 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2107 /**********************************************************************
2110 * Handler for SIGTRAP.
2112 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2114 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2116 switch (siginfo->si_code)
2118 case TRAP_TRACE: /* Single-step exception */
2119 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2120 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2122 case TRAP_BRKPT: /* Breakpoint exception */
2123 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2126 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2131 /**********************************************************************
2134 * Handler for SIGFPE.
2136 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2138 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2140 switch (siginfo->si_code)
2143 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2146 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2149 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2152 rec->ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
2155 rec->ExceptionCode = EXCEPTION_FLT_OVERFLOW;
2158 rec->ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
2161 rec->ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
2165 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2170 /**********************************************************************
2173 * Handler for SIGINT.
2175 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2177 if (!dispatch_signal(SIGINT))
2179 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2180 rec->ExceptionCode = CONTROL_C_EXIT;
2185 /**********************************************************************
2188 * Handler for SIGABRT.
2190 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2192 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2193 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2194 rec->ExceptionFlags = EH_NONCONTINUABLE;
2198 /**********************************************************************
2201 * Handler for SIGQUIT.
2203 static void quit_handler( int signal, siginfo_t *siginfo, void *ucontext )
2209 /**********************************************************************
2212 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2214 static void usr1_handler( int signal, siginfo_t *siginfo, void *ucontext )
2218 save_context( &context, ucontext );
2219 wait_suspend( &context );
2220 restore_context( &context, ucontext );
2224 /***********************************************************************
2225 * __wine_set_signal_handler (NTDLL.@)
2227 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2229 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
2230 if (handlers[sig] != NULL) return -2;
2231 handlers[sig] = wsh;
2236 /**********************************************************************
2237 * signal_alloc_thread
2239 NTSTATUS signal_alloc_thread( TEB **teb )
2241 static size_t sigstack_zero_bits;
2245 if (!sigstack_zero_bits)
2247 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2248 /* find the first power of two not smaller than min_size */
2249 sigstack_zero_bits = 12;
2250 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2251 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2252 assert( sizeof(TEB) <= teb_size );
2255 size = 1 << sigstack_zero_bits;
2257 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
2258 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2260 (*teb)->Tib.Self = &(*teb)->Tib;
2261 (*teb)->Tib.ExceptionList = (void *)~0UL;
2267 /**********************************************************************
2268 * signal_free_thread
2270 void signal_free_thread( TEB *teb )
2274 if (teb->DeallocationStack)
2277 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2280 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2284 /**********************************************************************
2285 * signal_init_thread
2287 void signal_init_thread( TEB *teb )
2292 arch_prctl( ARCH_SET_GS, teb );
2294 # error Please define setting %gs for your architecture
2297 ss.ss_sp = (char *)teb + teb_size;
2298 ss.ss_size = signal_stack_size;
2300 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2303 /**********************************************************************
2304 * signal_init_process
2306 void signal_init_process(void)
2308 struct sigaction sig_act;
2310 sig_act.sa_mask = server_block_set;
2311 sig_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
2313 sig_act.sa_sigaction = int_handler;
2314 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2315 sig_act.sa_sigaction = fpe_handler;
2316 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2317 sig_act.sa_sigaction = abrt_handler;
2318 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2319 sig_act.sa_sigaction = quit_handler;
2320 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2321 sig_act.sa_sigaction = usr1_handler;
2322 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2324 sig_act.sa_sigaction = segv_handler;
2325 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2326 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2328 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2332 sig_act.sa_sigaction = trap_handler;
2333 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2338 perror("sigaction");
2343 /**********************************************************************
2344 * RtlAddFunctionTable (NTDLL.@)
2346 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD64 addr )
2348 FIXME( "%p %u %lx: stub\n", table, count, addr );
2353 /**********************************************************************
2354 * RtlDeleteFunctionTable (NTDLL.@)
2356 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
2358 FIXME( "%p: stub\n", table );
2363 /**********************************************************************
2364 * RtlLookupFunctionEntry (NTDLL.@)
2366 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base, UNWIND_HISTORY_TABLE *table )
2369 RUNTIME_FUNCTION *func;
2372 /* FIXME: should use the history table to make things faster */
2374 if (LdrFindEntryForAddress( (void *)pc, &module ))
2376 WARN( "module not found for %lx\n", pc );
2379 if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2380 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2382 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
2385 func = find_function_info( pc, module->BaseAddress, func, size );
2386 if (func) *base = (ULONG64)module->BaseAddress;
2390 static ULONG64 get_int_reg( CONTEXT *context, int reg )
2392 return *(&context->Rax + reg);
2395 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 val )
2397 *(&context->Rax + reg) = val;
2398 if (ctx_ptr) ctx_ptr->u2.IntegerContext[reg] = &context->Rax + reg;
2401 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A val )
2403 *(&context->u.s.Xmm0 + reg) = val;
2404 if (ctx_ptr) ctx_ptr->u1.FloatingContext[reg] = &context->u.s.Xmm0 + reg;
2407 static int get_opcode_size( struct opcode op )
2411 case UWOP_ALLOC_LARGE:
2412 return 2 + (op.info != 0);
2413 case UWOP_SAVE_NONVOL:
2414 case UWOP_SAVE_XMM128:
2416 case UWOP_SAVE_NONVOL_FAR:
2417 case UWOP_SAVE_XMM128_FAR:
2424 static BOOL is_inside_epilog( BYTE *pc )
2426 /* add or lea must be the first instruction, and it must have a rex.W prefix */
2427 if ((pc[0] & 0xf8) == 0x48)
2431 case 0x81: /* add $nnnn,%rsp */
2432 if (pc[0] == 0x48 && pc[2] == 0xc4)
2438 case 0x83: /* add $n,%rsp */
2439 if (pc[0] == 0x48 && pc[2] == 0xc4)
2445 case 0x8d: /* lea n(reg),%rsp */
2446 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
2447 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
2448 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
2449 if ((pc[2] >> 6) == 1) /* 8-bit offset */
2454 if ((pc[2] >> 6) == 2) /* 32-bit offset */
2463 /* now check for various pop instructions */
2469 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2473 case 0x58: /* pop %rax/%r8 */
2474 case 0x59: /* pop %rcx/%r9 */
2475 case 0x5a: /* pop %rdx/%r10 */
2476 case 0x5b: /* pop %rbx/%r11 */
2477 case 0x5c: /* pop %rsp/%r12 */
2478 case 0x5d: /* pop %rbp/%r13 */
2479 case 0x5e: /* pop %rsi/%r14 */
2480 case 0x5f: /* pop %rdi/%r15 */
2483 case 0xc2: /* ret $nn */
2484 case 0xc3: /* ret */
2486 /* FIXME: add various jump instructions */
2492 /* execute a function epilog, which must have been validated with is_inside_epilog() */
2493 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2499 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2503 case 0x58: /* pop %rax/r8 */
2504 case 0x59: /* pop %rcx/r9 */
2505 case 0x5a: /* pop %rdx/r10 */
2506 case 0x5b: /* pop %rbx/r11 */
2507 case 0x5c: /* pop %rsp/r12 */
2508 case 0x5d: /* pop %rbp/r13 */
2509 case 0x5e: /* pop %rsi/r14 */
2510 case 0x5f: /* pop %rdi/r15 */
2511 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, *(ULONG64 *)context->Rsp );
2512 context->Rsp += sizeof(ULONG64);
2515 case 0x81: /* add $nnnn,%rsp */
2516 context->Rsp += *(LONG *)(pc + 2);
2517 pc += 2 + sizeof(LONG);
2519 case 0x83: /* add $n,%rsp */
2520 context->Rsp += (signed char)pc[2];
2524 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
2526 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
2529 else /* lea nnnn(reg),%rsp */
2531 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
2532 pc += 2 + sizeof(LONG);
2535 case 0xc2: /* ret $nn */
2536 context->Rip = *(ULONG64 *)context->Rsp;
2537 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
2539 case 0xc3: /* ret */
2540 context->Rip = *(ULONG64 *)context->Rsp;
2541 context->Rsp += sizeof(ULONG64);
2543 /* FIXME: add various jump instructions */
2549 /**********************************************************************
2550 * RtlVirtualUnwind (NTDLL.@)
2552 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
2553 RUNTIME_FUNCTION *function, CONTEXT *context,
2554 PVOID *data, ULONG64 *frame_ret,
2555 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2557 union handler_data *handler_data;
2559 struct UNWIND_INFO *info;
2560 unsigned int i, prolog_offset;
2562 TRACE( "type %x rip %lx rsp %lx\n", type, pc, context->Rsp );
2563 if (TRACE_ON(seh)) dump_unwind_info( base, function );
2565 frame = *frame_ret = context->Rsp;
2568 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
2569 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
2571 if (info->version != 1)
2573 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
2577 if (info->frame_reg)
2578 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
2580 /* check if in prolog */
2581 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
2583 prolog_offset = pc - base - function->BeginAddress;
2588 if (is_inside_epilog( (BYTE *)pc ))
2590 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
2596 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
2598 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
2600 switch (info->opcodes[i].code)
2602 case UWOP_PUSH_NONVOL: /* pushq %reg */
2603 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)context->Rsp );
2604 context->Rsp += sizeof(ULONG64);
2606 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
2607 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
2608 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
2610 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
2611 context->Rsp += (info->opcodes[i].info + 1) * 8;
2613 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
2614 context->Rsp = *frame_ret = frame;
2616 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
2617 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
2618 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2620 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
2621 off = frame + *(DWORD *)&info->opcodes[i+1];
2622 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2624 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
2625 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
2626 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2628 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
2629 off = frame + *(DWORD *)&info->opcodes[i+1];
2630 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2632 case UWOP_PUSH_MACHFRAME:
2633 FIXME( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
2636 FIXME( "unknown code %u\n", info->opcodes[i].code );
2641 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
2642 function = &handler_data->chain; /* restart with the chained info */
2645 /* now pop return address */
2646 context->Rip = *(ULONG64 *)context->Rsp;
2647 context->Rsp += sizeof(ULONG64);
2649 if (!(info->flags & type)) return NULL; /* no matching handler */
2650 if (prolog_offset != ~0) return NULL; /* inside prolog */
2652 *data = &handler_data->handler + 1;
2653 return (char *)base + handler_data->handler;
2657 /**********************************************************************
2658 * call_unwind_handler
2660 * Call a single unwind handler.
2661 * FIXME: Handle nested exceptions.
2663 static void call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
2667 dispatch->ControlPc = dispatch->ContextRecord->Rip;
2669 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2670 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2671 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2672 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
2676 case ExceptionContinueSearch:
2678 case ExceptionCollidedUnwind:
2679 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2682 raise_status( STATUS_INVALID_DISPOSITION, rec );
2688 /**********************************************************************
2689 * call_teb_unwind_handler
2691 * Call a single unwind handler from the TEB chain.
2692 * FIXME: Handle nested exceptions.
2694 static void call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
2695 EXCEPTION_REGISTRATION_RECORD *teb_frame )
2697 EXCEPTION_REGISTRATION_RECORD *dispatcher;
2700 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2701 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2702 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2703 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
2707 case ExceptionContinueSearch:
2709 case ExceptionCollidedUnwind:
2710 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2713 raise_status( STATUS_INVALID_DISPOSITION, rec );
2719 /*******************************************************************
2720 * RtlUnwindEx (NTDLL.@)
2722 void WINAPI RtlUnwindEx( ULONG64 end_frame, ULONG64 target_ip, EXCEPTION_RECORD *rec,
2723 ULONG64 retval, CONTEXT *orig_context, UNWIND_HISTORY_TABLE *table )
2725 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
2726 EXCEPTION_RECORD record;
2727 DISPATCHER_CONTEXT dispatch;
2728 CONTEXT context, new_context;
2733 /* build an exception record, if we do not have one */
2736 record.ExceptionCode = STATUS_UNWIND;
2737 record.ExceptionFlags = 0;
2738 record.ExceptionRecord = NULL;
2739 record.ExceptionAddress = (void *)orig_context->Rip;
2740 record.NumberParameters = 0;
2744 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
2746 TRACE( "code=%x flags=%x end_frame=%lx target_ip=%lx rip=%016lx\n",
2747 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, orig_context->Rip );
2748 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2749 orig_context->Rax, orig_context->Rbx, orig_context->Rcx, orig_context->Rdx );
2750 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2751 orig_context->Rsi, orig_context->Rdi, orig_context->Rbp, orig_context->Rsp );
2752 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2753 orig_context->R8, orig_context->R9, orig_context->R10, orig_context->R11 );
2754 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2755 orig_context->R12, orig_context->R13, orig_context->R14, orig_context->R15 );
2757 context = *orig_context;
2758 dispatch.EstablisherFrame = context.Rsp;
2759 dispatch.TargetIp = target_ip;
2760 dispatch.ContextRecord = &context;
2761 dispatch.HistoryTable = table;
2763 while (dispatch.EstablisherFrame != end_frame)
2765 new_context = context;
2767 /* FIXME: should use the history table to make things faster */
2770 dispatch.ImageBase = 0;
2771 dispatch.ScopeIndex = 0; /* FIXME */
2773 /* first look for PE exception information */
2775 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
2777 RUNTIME_FUNCTION *dir;
2779 dispatch.ImageBase = (ULONG64)module->BaseAddress;
2780 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2781 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2783 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
2786 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_UHANDLER, dispatch.ImageBase,
2787 context.Rip, dispatch.FunctionEntry,
2788 &new_context, &dispatch.HandlerData,
2789 &dispatch.EstablisherFrame, NULL );
2793 else if (!(module->Flags & LDR_WINE_INTERNAL))
2794 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
2797 /* then look for host system exception information */
2799 if (!module || (module->Flags & LDR_WINE_INTERNAL))
2801 struct dwarf_eh_bases bases;
2802 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
2806 dispatch.FunctionEntry = NULL;
2807 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context, fde,
2808 &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
2809 if (status != STATUS_SUCCESS) raise_status( status, rec );
2810 if (dispatch.LanguageHandler && !module)
2812 FIXME( "calling personality routine in system library not supported yet\n" );
2813 dispatch.LanguageHandler = NULL;
2819 /* no exception information, treat as a leaf function */
2821 new_context.Rip = *(ULONG64 *)context.Rsp;
2822 new_context.Rsp = context.Rsp + sizeof(ULONG64);
2823 dispatch.EstablisherFrame = new_context.Rsp;
2824 dispatch.LanguageHandler = NULL;
2827 if (!dispatch.EstablisherFrame) break;
2829 if (is_inside_signal_stack( (void *)dispatch.EstablisherFrame ))
2831 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch.EstablisherFrame,
2832 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size );
2833 context = new_context;
2837 if ((dispatch.EstablisherFrame & 7) ||
2838 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
2839 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
2841 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
2842 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2843 rec->ExceptionFlags |= EH_STACK_INVALID;
2847 if (dispatch.LanguageHandler)
2849 if (end_frame && (dispatch.EstablisherFrame > end_frame))
2851 ERR( "invalid end frame %lx/%lx\n", dispatch.EstablisherFrame, end_frame );
2852 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
2854 call_unwind_handler( rec, &dispatch );
2856 else /* hack: call builtin handlers registered in the tib list */
2858 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < end_frame)
2860 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
2861 dispatch.EstablisherFrame = (ULONG64)teb_frame;
2862 call_teb_unwind_handler( rec, &dispatch, teb_frame );
2863 teb_frame = __wine_pop_frame( teb_frame );
2865 if ((ULONG64)teb_frame == end_frame && end_frame < new_context.Rsp) break;
2866 dispatch.EstablisherFrame = new_context.Rsp;
2869 context = new_context;
2871 context.Rax = retval;
2872 context.Rip = target_ip;
2873 TRACE( "returning to %lx stack %lx\n", context.Rip, context.Rsp );
2874 set_cpu_context( &context );
2878 /*******************************************************************
2879 * RtlUnwind (NTDLL.@)
2881 void WINAPI __regs_RtlUnwind( ULONG64 frame, ULONG64 target_ip, EXCEPTION_RECORD *rec,
2882 ULONG64 retval, CONTEXT *context )
2884 RtlUnwindEx( frame, target_ip, rec, retval, context, NULL );
2886 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2889 /*******************************************************************
2890 * __C_specific_handler (NTDLL.@)
2892 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
2895 struct _DISPATCHER_CONTEXT *dispatch )
2897 SCOPE_TABLE *table = dispatch->HandlerData;
2900 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
2901 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
2903 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)) /* FIXME */
2904 return ExceptionContinueSearch;
2906 for (i = 0; i < table->Count; i++)
2908 if (context->Rip >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
2909 context->Rip < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
2911 if (!table->ScopeRecord[i].JumpTarget) continue;
2912 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
2914 EXCEPTION_POINTERS ptrs;
2915 PC_LANGUAGE_EXCEPTION_HANDLER filter;
2917 filter = (PC_LANGUAGE_EXCEPTION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
2918 ptrs.ExceptionRecord = rec;
2919 ptrs.ContextRecord = context;
2920 TRACE( "calling filter %p ptrs %p frame %lx\n", filter, &ptrs, frame );
2921 switch (filter( &ptrs, frame ))
2923 case EXCEPTION_EXECUTE_HANDLER:
2925 case EXCEPTION_CONTINUE_SEARCH:
2927 case EXCEPTION_CONTINUE_EXECUTION:
2928 return ExceptionContinueExecution;
2931 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
2932 RtlUnwindEx( frame, dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
2933 rec, 0, context, dispatch->HistoryTable );
2936 return ExceptionContinueSearch;
2940 /*******************************************************************
2941 * NtRaiseException (NTDLL.@)
2943 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2945 NTSTATUS status = raise_exception( rec, context, first_chance );
2946 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2951 /***********************************************************************
2952 * RtlRaiseException (NTDLL.@)
2954 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2958 rec->ExceptionAddress = (void *)context->Rip;
2959 status = raise_exception( rec, context, TRUE );
2960 if (status != STATUS_SUCCESS) raise_status( status, rec );
2962 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2965 /*************************************************************************
2966 * RtlCaptureStackBackTrace (NTDLL.@)
2968 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2970 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
2975 /***********************************************************************
2978 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
2980 ntdll_get_thread_data()->exit_frame = frame;
2983 RtlExitUserThread( entry( arg ));
2985 __EXCEPT(unhandled_exception_filter)
2987 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2990 abort(); /* should not be reached */
2993 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
2994 __ASM_GLOBAL_FUNC( call_thread_entry_point,
2996 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
2997 "movq %rbp,48(%rsp)\n\t"
2998 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
2999 "movq %rbx,40(%rsp)\n\t"
3000 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3001 "movq %r12,32(%rsp)\n\t"
3002 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3003 "movq %r13,24(%rsp)\n\t"
3004 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3005 "movq %r14,16(%rsp)\n\t"
3006 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3007 "movq %r15,8(%rsp)\n\t"
3008 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3009 "movq %rsp,%rdx\n\t"
3010 "call " __ASM_NAME("call_thread_func") );
3012 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
3013 __ASM_GLOBAL_FUNC( call_thread_exit_func,
3014 "movq %rdx,%rsp\n\t"
3015 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3016 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3017 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3018 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3019 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3020 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3021 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3024 /***********************************************************************
3025 * RtlExitUserThread (NTDLL.@)
3027 void WINAPI RtlExitUserThread( ULONG status )
3029 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
3030 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
3033 /***********************************************************************
3036 void abort_thread( int status )
3038 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
3039 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
3042 /**********************************************************************
3043 * __wine_enter_vm86 (NTDLL.@)
3045 void __wine_enter_vm86( CONTEXT *context )
3047 MESSAGE("vm86 mode not supported on this platform\n");
3050 /**********************************************************************
3051 * DbgBreakPoint (NTDLL.@)
3053 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
3055 /**********************************************************************
3056 * DbgUserBreakPoint (NTDLL.@)
3058 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
3060 #endif /* __x86_64__ */