oleaut32: Use VariantInit() rather than open coding it.
[wine.git] / dlls / ntdll / signal_x86_64.c
blob61bb2ddce6f6487cd0d0a1b1ded78609a6bd818b
1 /*
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
21 #ifdef __x86_64__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #ifdef HAVE_MACHINE_SYSARCH_H
36 # include <machine/sysarch.h>
37 #endif
38 #ifdef HAVE_SYS_PARAM_H
39 # include <sys/param.h>
40 #endif
41 #ifdef HAVE_SYSCALL_H
42 # include <syscall.h>
43 #else
44 # ifdef HAVE_SYS_SYSCALL_H
45 # include <sys/syscall.h>
46 # endif
47 #endif
48 #ifdef HAVE_SYS_SIGNAL_H
49 # include <sys/signal.h>
50 #endif
51 #ifdef HAVE_SYS_UCONTEXT_H
52 # include <sys/ucontext.h>
53 #endif
54 #ifdef HAVE_LIBUNWIND_H
55 # define UNW_LOCAL_ONLY
56 # include <libunwind.h>
57 #endif
59 #define NONAMELESSUNION
60 #define NONAMELESSSTRUCT
61 #include "ntstatus.h"
62 #define WIN32_NO_STATUS
63 #include "windef.h"
64 #include "winternl.h"
65 #include "wine/library.h"
66 #include "wine/exception.h"
67 #include "wine/list.h"
68 #include "ntdll_misc.h"
69 #include "wine/debug.h"
71 #ifdef HAVE_VALGRIND_MEMCHECK_H
72 #include <valgrind/memcheck.h>
73 #endif
75 WINE_DEFAULT_DEBUG_CHANNEL(seh);
77 struct _DISPATCHER_CONTEXT;
79 typedef LONG (WINAPI *PC_LANGUAGE_EXCEPTION_HANDLER)( EXCEPTION_POINTERS *ptrs, ULONG64 frame );
80 typedef EXCEPTION_DISPOSITION (WINAPI *PEXCEPTION_ROUTINE)( EXCEPTION_RECORD *rec,
81 ULONG64 frame,
82 CONTEXT *context,
83 struct _DISPATCHER_CONTEXT *dispatch );
84 typedef void (WINAPI *TERMINATION_HANDLER)( ULONG flags, ULONG64 frame );
86 typedef struct _DISPATCHER_CONTEXT
88 ULONG64 ControlPc;
89 ULONG64 ImageBase;
90 PRUNTIME_FUNCTION FunctionEntry;
91 ULONG64 EstablisherFrame;
92 ULONG64 TargetIp;
93 PCONTEXT ContextRecord;
94 PEXCEPTION_ROUTINE LanguageHandler;
95 PVOID HandlerData;
96 PUNWIND_HISTORY_TABLE HistoryTable;
97 ULONG ScopeIndex;
98 } DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;
100 typedef struct _SCOPE_TABLE
102 ULONG Count;
103 struct
105 ULONG BeginAddress;
106 ULONG EndAddress;
107 ULONG HandlerAddress;
108 ULONG JumpTarget;
109 } ScopeRecord[1];
110 } SCOPE_TABLE, *PSCOPE_TABLE;
113 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
114 struct MSVCRT_JUMP_BUFFER
116 ULONG64 Frame;
117 ULONG64 Rbx;
118 ULONG64 Rsp;
119 ULONG64 Rbp;
120 ULONG64 Rsi;
121 ULONG64 Rdi;
122 ULONG64 R12;
123 ULONG64 R13;
124 ULONG64 R14;
125 ULONG64 R15;
126 ULONG64 Rip;
127 ULONG64 Spare;
128 M128A Xmm6;
129 M128A Xmm7;
130 M128A Xmm8;
131 M128A Xmm9;
132 M128A Xmm10;
133 M128A Xmm11;
134 M128A Xmm12;
135 M128A Xmm13;
136 M128A Xmm14;
137 M128A Xmm15;
140 /***********************************************************************
141 * signal context platform-specific definitions
143 #ifdef linux
145 #include <asm/prctl.h>
146 static inline int arch_prctl( int func, void *ptr ) { return syscall( __NR_arch_prctl, func, ptr ); }
148 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
149 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
150 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
151 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
152 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
153 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
154 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
155 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
156 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
157 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
158 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
159 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
160 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
161 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
162 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
164 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
165 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
166 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
168 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
169 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
170 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
171 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
172 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
174 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
176 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
178 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
179 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
180 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
181 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
182 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
183 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
184 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
185 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
186 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
187 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
188 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
189 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
190 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
191 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
192 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
194 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
195 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
196 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
197 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
198 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
199 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
201 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
203 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
204 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
205 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
206 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
208 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
210 #elif defined(__NetBSD__)
212 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
213 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
214 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
215 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
216 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
217 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
218 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
219 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
220 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
221 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
222 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
223 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
224 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
225 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
226 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
228 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
229 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
230 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
231 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
232 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
233 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
235 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
237 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
238 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
240 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
241 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
243 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
244 #elif defined (__APPLE__)
245 #define RAX_sig(context) ((context)->uc_mcontext->__ss.__rax)
246 #define RBX_sig(context) ((context)->uc_mcontext->__ss.__rbx)
247 #define RCX_sig(context) ((context)->uc_mcontext->__ss.__rcx)
248 #define RDX_sig(context) ((context)->uc_mcontext->__ss.__rdx)
249 #define RSI_sig(context) ((context)->uc_mcontext->__ss.__rsi)
250 #define RDI_sig(context) ((context)->uc_mcontext->__ss.__rdi)
251 #define RBP_sig(context) ((context)->uc_mcontext->__ss.__rbp)
252 #define R8_sig(context) ((context)->uc_mcontext->__ss.__r8)
253 #define R9_sig(context) ((context)->uc_mcontext->__ss.__r9)
254 #define R10_sig(context) ((context)->uc_mcontext->__ss.__r10)
255 #define R11_sig(context) ((context)->uc_mcontext->__ss.__r11)
256 #define R12_sig(context) ((context)->uc_mcontext->__ss.__r12)
257 #define R13_sig(context) ((context)->uc_mcontext->__ss.__r13)
258 #define R14_sig(context) ((context)->uc_mcontext->__ss.__r14)
259 #define R15_sig(context) ((context)->uc_mcontext->__ss.__r15)
261 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
262 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
263 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
265 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__rflags)
267 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rip))
268 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rsp))
270 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
271 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
273 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
275 #else
276 #error You must define the signal context functions for your platform
277 #endif
279 enum i386_trap_code
281 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
282 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
283 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
284 TRAP_x86_NMI = 2, /* NMI interrupt */
285 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
286 TRAP_x86_OFLOW = 4, /* Overflow exception */
287 TRAP_x86_BOUND = 5, /* Bound range exception */
288 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
289 TRAP_x86_DNA = 7, /* Device not available exception */
290 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
291 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
292 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
293 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
294 TRAP_x86_STKFLT = 12, /* Stack fault */
295 TRAP_x86_PROTFLT = 13, /* General protection fault */
296 TRAP_x86_PAGEFLT = 14, /* Page fault */
297 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
298 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
299 TRAP_x86_MCHK = 18, /* Machine check exception */
300 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
303 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
304 static size_t signal_stack_size;
306 typedef void (*raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
307 typedef int (*wine_signal_handler)(unsigned int sig);
309 static wine_signal_handler handlers[256];
311 /***********************************************************************
312 * Dynamic unwind table
315 struct dynamic_unwind_entry
317 struct list entry;
319 /* memory region which matches this entry */
320 DWORD64 base;
321 DWORD size;
323 /* lookup table */
324 RUNTIME_FUNCTION *table;
325 DWORD table_size;
327 /* user defined callback */
328 PGET_RUNTIME_FUNCTION_CALLBACK callback;
329 PVOID context;
332 static struct list dynamic_unwind_list = LIST_INIT(dynamic_unwind_list);
334 static RTL_CRITICAL_SECTION dynamic_unwind_section;
335 static RTL_CRITICAL_SECTION_DEBUG dynamic_unwind_debug =
337 0, 0, &dynamic_unwind_section,
338 { &dynamic_unwind_debug.ProcessLocksList, &dynamic_unwind_debug.ProcessLocksList },
339 0, 0, { (DWORD_PTR)(__FILE__ ": dynamic_unwind_section") }
341 static RTL_CRITICAL_SECTION dynamic_unwind_section = { &dynamic_unwind_debug, -1, 0, 0, 0, 0 };
343 /***********************************************************************
344 * Definitions for Win32 unwind tables
347 union handler_data
349 RUNTIME_FUNCTION chain;
350 ULONG handler;
353 struct opcode
355 BYTE offset;
356 BYTE code : 4;
357 BYTE info : 4;
360 struct UNWIND_INFO
362 BYTE version : 3;
363 BYTE flags : 5;
364 BYTE prolog;
365 BYTE count;
366 BYTE frame_reg : 4;
367 BYTE frame_offset : 4;
368 struct opcode opcodes[1]; /* info->count entries */
369 /* followed by handler_data */
372 #define UWOP_PUSH_NONVOL 0
373 #define UWOP_ALLOC_LARGE 1
374 #define UWOP_ALLOC_SMALL 2
375 #define UWOP_SET_FPREG 3
376 #define UWOP_SAVE_NONVOL 4
377 #define UWOP_SAVE_NONVOL_FAR 5
378 #define UWOP_SAVE_XMM128 8
379 #define UWOP_SAVE_XMM128_FAR 9
380 #define UWOP_PUSH_MACHFRAME 10
382 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
384 static const char * const reg_names[16] =
385 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
386 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
388 union handler_data *handler_data;
389 struct UNWIND_INFO *info;
390 unsigned int i, count;
392 TRACE( "**** func %x-%x\n", function->BeginAddress, function->EndAddress );
393 for (;;)
395 if (function->UnwindData & 1)
397 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
398 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
399 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
400 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
401 function = next;
402 continue;
404 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
406 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
407 info, info->flags, info->prolog,
408 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
410 if (info->frame_reg)
411 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
412 reg_names[info->frame_reg], info->frame_offset * 16 );
414 for (i = 0; i < info->count; i++)
416 TRACE( " 0x%x: ", info->opcodes[i].offset );
417 switch (info->opcodes[i].code)
419 case UWOP_PUSH_NONVOL:
420 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
421 break;
422 case UWOP_ALLOC_LARGE:
423 if (info->opcodes[i].info)
425 count = *(DWORD *)&info->opcodes[i+1];
426 i += 2;
428 else
430 count = *(USHORT *)&info->opcodes[i+1] * 8;
431 i++;
433 TRACE( "subq $0x%x,%%rsp\n", count );
434 break;
435 case UWOP_ALLOC_SMALL:
436 count = (info->opcodes[i].info + 1) * 8;
437 TRACE( "subq $0x%x,%%rsp\n", count );
438 break;
439 case UWOP_SET_FPREG:
440 TRACE( "leaq 0x%x(%%rsp),%s\n",
441 info->frame_offset * 16, reg_names[info->frame_reg] );
442 break;
443 case UWOP_SAVE_NONVOL:
444 count = *(USHORT *)&info->opcodes[i+1] * 8;
445 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
446 i++;
447 break;
448 case UWOP_SAVE_NONVOL_FAR:
449 count = *(DWORD *)&info->opcodes[i+1];
450 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
451 i += 2;
452 break;
453 case UWOP_SAVE_XMM128:
454 count = *(USHORT *)&info->opcodes[i+1] * 16;
455 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
456 i++;
457 break;
458 case UWOP_SAVE_XMM128_FAR:
459 count = *(DWORD *)&info->opcodes[i+1];
460 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
461 i += 2;
462 break;
463 case UWOP_PUSH_MACHFRAME:
464 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
465 break;
466 default:
467 FIXME( "unknown code %u\n", info->opcodes[i].code );
468 break;
472 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
473 if (info->flags & UNW_FLAG_CHAININFO)
475 TRACE( " chained to function %p-%p\n",
476 (char *)base + handler_data->chain.BeginAddress,
477 (char *)base + handler_data->chain.EndAddress );
478 function = &handler_data->chain;
479 continue;
481 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
482 TRACE( " handler %p data at %p\n",
483 (char *)base + handler_data->handler, &handler_data->handler + 1 );
484 break;
488 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
490 unsigned int i;
492 TRACE( "scope table at %p\n", table );
493 for (i = 0; i < table->Count; i++)
494 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
495 base + table->ScopeRecord[i].BeginAddress,
496 base + table->ScopeRecord[i].EndAddress,
497 base + table->ScopeRecord[i].HandlerAddress,
498 base + table->ScopeRecord[i].JumpTarget );
502 /***********************************************************************
503 * Definitions for Dwarf unwind tables
506 enum dwarf_call_frame_info
508 DW_CFA_advance_loc = 0x40,
509 DW_CFA_offset = 0x80,
510 DW_CFA_restore = 0xc0,
511 DW_CFA_nop = 0x00,
512 DW_CFA_set_loc = 0x01,
513 DW_CFA_advance_loc1 = 0x02,
514 DW_CFA_advance_loc2 = 0x03,
515 DW_CFA_advance_loc4 = 0x04,
516 DW_CFA_offset_extended = 0x05,
517 DW_CFA_restore_extended = 0x06,
518 DW_CFA_undefined = 0x07,
519 DW_CFA_same_value = 0x08,
520 DW_CFA_register = 0x09,
521 DW_CFA_remember_state = 0x0a,
522 DW_CFA_restore_state = 0x0b,
523 DW_CFA_def_cfa = 0x0c,
524 DW_CFA_def_cfa_register = 0x0d,
525 DW_CFA_def_cfa_offset = 0x0e,
526 DW_CFA_def_cfa_expression = 0x0f,
527 DW_CFA_expression = 0x10,
528 DW_CFA_offset_extended_sf = 0x11,
529 DW_CFA_def_cfa_sf = 0x12,
530 DW_CFA_def_cfa_offset_sf = 0x13,
531 DW_CFA_val_offset = 0x14,
532 DW_CFA_val_offset_sf = 0x15,
533 DW_CFA_val_expression = 0x16,
536 enum dwarf_operation
538 DW_OP_addr = 0x03,
539 DW_OP_deref = 0x06,
540 DW_OP_const1u = 0x08,
541 DW_OP_const1s = 0x09,
542 DW_OP_const2u = 0x0a,
543 DW_OP_const2s = 0x0b,
544 DW_OP_const4u = 0x0c,
545 DW_OP_const4s = 0x0d,
546 DW_OP_const8u = 0x0e,
547 DW_OP_const8s = 0x0f,
548 DW_OP_constu = 0x10,
549 DW_OP_consts = 0x11,
550 DW_OP_dup = 0x12,
551 DW_OP_drop = 0x13,
552 DW_OP_over = 0x14,
553 DW_OP_pick = 0x15,
554 DW_OP_swap = 0x16,
555 DW_OP_rot = 0x17,
556 DW_OP_xderef = 0x18,
557 DW_OP_abs = 0x19,
558 DW_OP_and = 0x1a,
559 DW_OP_div = 0x1b,
560 DW_OP_minus = 0x1c,
561 DW_OP_mod = 0x1d,
562 DW_OP_mul = 0x1e,
563 DW_OP_neg = 0x1f,
564 DW_OP_not = 0x20,
565 DW_OP_or = 0x21,
566 DW_OP_plus = 0x22,
567 DW_OP_plus_uconst = 0x23,
568 DW_OP_shl = 0x24,
569 DW_OP_shr = 0x25,
570 DW_OP_shra = 0x26,
571 DW_OP_xor = 0x27,
572 DW_OP_bra = 0x28,
573 DW_OP_eq = 0x29,
574 DW_OP_ge = 0x2a,
575 DW_OP_gt = 0x2b,
576 DW_OP_le = 0x2c,
577 DW_OP_lt = 0x2d,
578 DW_OP_ne = 0x2e,
579 DW_OP_skip = 0x2f,
580 DW_OP_lit0 = 0x30,
581 DW_OP_lit1 = 0x31,
582 DW_OP_lit2 = 0x32,
583 DW_OP_lit3 = 0x33,
584 DW_OP_lit4 = 0x34,
585 DW_OP_lit5 = 0x35,
586 DW_OP_lit6 = 0x36,
587 DW_OP_lit7 = 0x37,
588 DW_OP_lit8 = 0x38,
589 DW_OP_lit9 = 0x39,
590 DW_OP_lit10 = 0x3a,
591 DW_OP_lit11 = 0x3b,
592 DW_OP_lit12 = 0x3c,
593 DW_OP_lit13 = 0x3d,
594 DW_OP_lit14 = 0x3e,
595 DW_OP_lit15 = 0x3f,
596 DW_OP_lit16 = 0x40,
597 DW_OP_lit17 = 0x41,
598 DW_OP_lit18 = 0x42,
599 DW_OP_lit19 = 0x43,
600 DW_OP_lit20 = 0x44,
601 DW_OP_lit21 = 0x45,
602 DW_OP_lit22 = 0x46,
603 DW_OP_lit23 = 0x47,
604 DW_OP_lit24 = 0x48,
605 DW_OP_lit25 = 0x49,
606 DW_OP_lit26 = 0x4a,
607 DW_OP_lit27 = 0x4b,
608 DW_OP_lit28 = 0x4c,
609 DW_OP_lit29 = 0x4d,
610 DW_OP_lit30 = 0x4e,
611 DW_OP_lit31 = 0x4f,
612 DW_OP_reg0 = 0x50,
613 DW_OP_reg1 = 0x51,
614 DW_OP_reg2 = 0x52,
615 DW_OP_reg3 = 0x53,
616 DW_OP_reg4 = 0x54,
617 DW_OP_reg5 = 0x55,
618 DW_OP_reg6 = 0x56,
619 DW_OP_reg7 = 0x57,
620 DW_OP_reg8 = 0x58,
621 DW_OP_reg9 = 0x59,
622 DW_OP_reg10 = 0x5a,
623 DW_OP_reg11 = 0x5b,
624 DW_OP_reg12 = 0x5c,
625 DW_OP_reg13 = 0x5d,
626 DW_OP_reg14 = 0x5e,
627 DW_OP_reg15 = 0x5f,
628 DW_OP_reg16 = 0x60,
629 DW_OP_reg17 = 0x61,
630 DW_OP_reg18 = 0x62,
631 DW_OP_reg19 = 0x63,
632 DW_OP_reg20 = 0x64,
633 DW_OP_reg21 = 0x65,
634 DW_OP_reg22 = 0x66,
635 DW_OP_reg23 = 0x67,
636 DW_OP_reg24 = 0x68,
637 DW_OP_reg25 = 0x69,
638 DW_OP_reg26 = 0x6a,
639 DW_OP_reg27 = 0x6b,
640 DW_OP_reg28 = 0x6c,
641 DW_OP_reg29 = 0x6d,
642 DW_OP_reg30 = 0x6e,
643 DW_OP_reg31 = 0x6f,
644 DW_OP_breg0 = 0x70,
645 DW_OP_breg1 = 0x71,
646 DW_OP_breg2 = 0x72,
647 DW_OP_breg3 = 0x73,
648 DW_OP_breg4 = 0x74,
649 DW_OP_breg5 = 0x75,
650 DW_OP_breg6 = 0x76,
651 DW_OP_breg7 = 0x77,
652 DW_OP_breg8 = 0x78,
653 DW_OP_breg9 = 0x79,
654 DW_OP_breg10 = 0x7a,
655 DW_OP_breg11 = 0x7b,
656 DW_OP_breg12 = 0x7c,
657 DW_OP_breg13 = 0x7d,
658 DW_OP_breg14 = 0x7e,
659 DW_OP_breg15 = 0x7f,
660 DW_OP_breg16 = 0x80,
661 DW_OP_breg17 = 0x81,
662 DW_OP_breg18 = 0x82,
663 DW_OP_breg19 = 0x83,
664 DW_OP_breg20 = 0x84,
665 DW_OP_breg21 = 0x85,
666 DW_OP_breg22 = 0x86,
667 DW_OP_breg23 = 0x87,
668 DW_OP_breg24 = 0x88,
669 DW_OP_breg25 = 0x89,
670 DW_OP_breg26 = 0x8a,
671 DW_OP_breg27 = 0x8b,
672 DW_OP_breg28 = 0x8c,
673 DW_OP_breg29 = 0x8d,
674 DW_OP_breg30 = 0x8e,
675 DW_OP_breg31 = 0x8f,
676 DW_OP_regx = 0x90,
677 DW_OP_fbreg = 0x91,
678 DW_OP_bregx = 0x92,
679 DW_OP_piece = 0x93,
680 DW_OP_deref_size = 0x94,
681 DW_OP_xderef_size = 0x95,
682 DW_OP_nop = 0x96,
683 DW_OP_push_object_address = 0x97,
684 DW_OP_call2 = 0x98,
685 DW_OP_call4 = 0x99,
686 DW_OP_call_ref = 0x9a,
687 DW_OP_form_tls_address = 0x9b,
688 DW_OP_call_frame_cfa = 0x9c,
689 DW_OP_bit_piece = 0x9d,
690 DW_OP_lo_user = 0xe0,
691 DW_OP_hi_user = 0xff,
692 DW_OP_GNU_push_tls_address = 0xe0,
693 DW_OP_GNU_uninit = 0xf0,
694 DW_OP_GNU_encoded_addr = 0xf1,
697 #define DW_EH_PE_native 0x00
698 #define DW_EH_PE_leb128 0x01
699 #define DW_EH_PE_data2 0x02
700 #define DW_EH_PE_data4 0x03
701 #define DW_EH_PE_data8 0x04
702 #define DW_EH_PE_signed 0x08
703 #define DW_EH_PE_abs 0x00
704 #define DW_EH_PE_pcrel 0x10
705 #define DW_EH_PE_textrel 0x20
706 #define DW_EH_PE_datarel 0x30
707 #define DW_EH_PE_funcrel 0x40
708 #define DW_EH_PE_aligned 0x50
709 #define DW_EH_PE_indirect 0x80
710 #define DW_EH_PE_omit 0xff
712 struct dwarf_eh_bases
714 void *tbase;
715 void *dbase;
716 void *func;
719 struct dwarf_cie
721 unsigned int length;
722 int id;
723 unsigned char version;
724 unsigned char augmentation[1];
727 struct dwarf_fde
729 unsigned int length;
730 unsigned int cie_offset;
733 extern const struct dwarf_fde *_Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
735 static unsigned char dwarf_get_u1( const unsigned char **p )
737 return *(*p)++;
740 static unsigned short dwarf_get_u2( const unsigned char **p )
742 unsigned int ret = (*p)[0] | ((*p)[1] << 8);
743 (*p) += 2;
744 return ret;
747 static unsigned int dwarf_get_u4( const unsigned char **p )
749 unsigned int ret = (*p)[0] | ((*p)[1] << 8) | ((*p)[2] << 16) | ((*p)[3] << 24);
750 (*p) += 4;
751 return ret;
754 static ULONG64 dwarf_get_u8( const unsigned char **p )
756 ULONG64 low = dwarf_get_u4( p );
757 ULONG64 high = dwarf_get_u4( p );
758 return low | (high << 32);
761 static ULONG_PTR dwarf_get_uleb128( const unsigned char **p )
763 ULONG_PTR ret = 0;
764 unsigned int shift = 0;
765 unsigned char byte;
769 byte = **p;
770 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
771 shift += 7;
772 (*p)++;
773 } while (byte & 0x80);
774 return ret;
777 static LONG_PTR dwarf_get_sleb128( const unsigned char **p )
779 ULONG_PTR ret = 0;
780 unsigned int shift = 0;
781 unsigned char byte;
785 byte = **p;
786 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
787 shift += 7;
788 (*p)++;
789 } while (byte & 0x80);
791 if ((shift < 8 * sizeof(ret)) && (byte & 0x40)) ret |= -((ULONG_PTR)1 << shift);
792 return ret;
795 static ULONG_PTR dwarf_get_ptr( const unsigned char **p, unsigned char encoding )
797 ULONG_PTR base;
799 if (encoding == DW_EH_PE_omit) return 0;
801 switch (encoding & 0xf0)
803 case DW_EH_PE_abs:
804 base = 0;
805 break;
806 case DW_EH_PE_pcrel:
807 base = (ULONG_PTR)*p;
808 break;
809 default:
810 FIXME( "unsupported encoding %02x\n", encoding );
811 return 0;
814 switch (encoding & 0x0f)
816 case DW_EH_PE_native:
817 return base + dwarf_get_u8( p );
818 case DW_EH_PE_leb128:
819 return base + dwarf_get_uleb128( p );
820 case DW_EH_PE_data2:
821 return base + dwarf_get_u2( p );
822 case DW_EH_PE_data4:
823 return base + dwarf_get_u4( p );
824 case DW_EH_PE_data8:
825 return base + dwarf_get_u8( p );
826 case DW_EH_PE_signed|DW_EH_PE_leb128:
827 return base + dwarf_get_sleb128( p );
828 case DW_EH_PE_signed|DW_EH_PE_data2:
829 return base + (signed short)dwarf_get_u2( p );
830 case DW_EH_PE_signed|DW_EH_PE_data4:
831 return base + (signed int)dwarf_get_u4( p );
832 case DW_EH_PE_signed|DW_EH_PE_data8:
833 return base + (LONG64)dwarf_get_u8( p );
834 default:
835 FIXME( "unsupported encoding %02x\n", encoding );
836 return 0;
840 enum reg_rule
842 RULE_UNSET, /* not set at all */
843 RULE_UNDEFINED, /* undefined value */
844 RULE_SAME, /* same value as previous frame */
845 RULE_CFA_OFFSET, /* stored at cfa offset */
846 RULE_OTHER_REG, /* stored in other register */
847 RULE_EXPRESSION, /* address specified by expression */
848 RULE_VAL_EXPRESSION /* value specified by expression */
851 #define NB_FRAME_REGS 41
852 #define MAX_SAVED_STATES 16
854 struct frame_state
856 ULONG_PTR cfa_offset;
857 unsigned char cfa_reg;
858 enum reg_rule cfa_rule;
859 enum reg_rule rules[NB_FRAME_REGS];
860 ULONG64 regs[NB_FRAME_REGS];
863 struct frame_info
865 ULONG_PTR ip;
866 ULONG_PTR code_align;
867 LONG_PTR data_align;
868 unsigned char retaddr_reg;
869 unsigned char fde_encoding;
870 unsigned char signal_frame;
871 unsigned char state_sp;
872 struct frame_state state;
873 struct frame_state *state_stack;
876 static const char *dwarf_reg_names[NB_FRAME_REGS] =
878 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
879 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
880 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
881 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
882 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
885 static BOOL valid_reg( ULONG_PTR reg )
887 if (reg >= NB_FRAME_REGS) FIXME( "unsupported reg %lx\n", reg );
888 return (reg < NB_FRAME_REGS);
891 static void execute_cfa_instructions( const unsigned char *ptr, const unsigned char *end,
892 ULONG_PTR last_ip, struct frame_info *info )
894 while (ptr < end && info->ip < last_ip + info->signal_frame)
896 enum dwarf_call_frame_info op = *ptr++;
898 if (op & 0xc0)
900 switch (op & 0xc0)
902 case DW_CFA_advance_loc:
904 ULONG_PTR offset = (op & 0x3f) * info->code_align;
905 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info->ip, offset );
906 info->ip += offset;
907 break;
909 case DW_CFA_offset:
911 ULONG_PTR reg = op & 0x3f;
912 LONG_PTR offset = dwarf_get_uleb128( &ptr ) * info->data_align;
913 if (!valid_reg( reg )) break;
914 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
915 info->state.regs[reg] = offset;
916 info->state.rules[reg] = RULE_CFA_OFFSET;
917 break;
919 case DW_CFA_restore:
921 ULONG_PTR reg = op & 0x3f;
922 if (!valid_reg( reg )) break;
923 TRACE( "%lx: DW_CFA_restore %s\n", info->ip, dwarf_reg_names[reg] );
924 info->state.rules[reg] = RULE_UNSET;
925 break;
929 else switch (op)
931 case DW_CFA_nop:
932 break;
933 case DW_CFA_set_loc:
935 ULONG_PTR loc = dwarf_get_ptr( &ptr, info->fde_encoding );
936 TRACE( "%lx: DW_CFA_set_loc %lx\n", info->ip, loc );
937 info->ip = loc;
938 break;
940 case DW_CFA_advance_loc1:
942 ULONG_PTR offset = *ptr++ * info->code_align;
943 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset );
944 info->ip += offset;
945 break;
947 case DW_CFA_advance_loc2:
949 ULONG_PTR offset = dwarf_get_u2( &ptr ) * info->code_align;
950 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset );
951 info->ip += offset;
952 break;
954 case DW_CFA_advance_loc4:
956 ULONG_PTR offset = dwarf_get_u4( &ptr ) * info->code_align;
957 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset );
958 info->ip += offset;
959 break;
961 case DW_CFA_offset_extended:
962 case DW_CFA_offset_extended_sf:
964 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
965 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf_get_uleb128( &ptr ) * info->data_align
966 : dwarf_get_sleb128( &ptr ) * info->data_align;
967 if (!valid_reg( reg )) break;
968 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
969 info->state.regs[reg] = offset;
970 info->state.rules[reg] = RULE_CFA_OFFSET;
971 break;
973 case DW_CFA_restore_extended:
975 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
976 if (!valid_reg( reg )) break;
977 TRACE( "%lx: DW_CFA_restore_extended %s\n", info->ip, dwarf_reg_names[reg] );
978 info->state.rules[reg] = RULE_UNSET;
979 break;
981 case DW_CFA_undefined:
983 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
984 if (!valid_reg( reg )) break;
985 TRACE( "%lx: DW_CFA_undefined %s\n", info->ip, dwarf_reg_names[reg] );
986 info->state.rules[reg] = RULE_UNDEFINED;
987 break;
989 case DW_CFA_same_value:
991 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
992 if (!valid_reg( reg )) break;
993 TRACE( "%lx: DW_CFA_same_value %s\n", info->ip, dwarf_reg_names[reg] );
994 info->state.regs[reg] = reg;
995 info->state.rules[reg] = RULE_SAME;
996 break;
998 case DW_CFA_register:
1000 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1001 ULONG_PTR reg2 = dwarf_get_uleb128( &ptr );
1002 if (!valid_reg( reg ) || !valid_reg( reg2 )) break;
1003 TRACE( "%lx: DW_CFA_register %s == %s\n", info->ip, dwarf_reg_names[reg], dwarf_reg_names[reg2] );
1004 info->state.regs[reg] = reg2;
1005 info->state.rules[reg] = RULE_OTHER_REG;
1006 break;
1008 case DW_CFA_remember_state:
1009 TRACE( "%lx: DW_CFA_remember_state\n", info->ip );
1010 if (info->state_sp >= MAX_SAVED_STATES)
1011 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info->ip );
1012 else
1013 info->state_stack[info->state_sp++] = info->state;
1014 break;
1015 case DW_CFA_restore_state:
1016 TRACE( "%lx: DW_CFA_restore_state\n", info->ip );
1017 if (!info->state_sp)
1018 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info->ip );
1019 else
1020 info->state = info->state_stack[--info->state_sp];
1021 break;
1022 case DW_CFA_def_cfa:
1023 case DW_CFA_def_cfa_sf:
1025 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1026 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf_get_uleb128( &ptr )
1027 : dwarf_get_sleb128( &ptr ) * info->data_align;
1028 if (!valid_reg( reg )) break;
1029 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info->ip, dwarf_reg_names[reg], offset );
1030 info->state.cfa_reg = reg;
1031 info->state.cfa_offset = offset;
1032 info->state.cfa_rule = RULE_CFA_OFFSET;
1033 break;
1035 case DW_CFA_def_cfa_register:
1037 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1038 if (!valid_reg( reg )) break;
1039 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info->ip, dwarf_reg_names[reg] );
1040 info->state.cfa_reg = reg;
1041 info->state.cfa_rule = RULE_CFA_OFFSET;
1042 break;
1044 case DW_CFA_def_cfa_offset:
1045 case DW_CFA_def_cfa_offset_sf:
1047 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf_get_uleb128( &ptr )
1048 : dwarf_get_sleb128( &ptr ) * info->data_align;
1049 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset );
1050 info->state.cfa_offset = offset;
1051 info->state.cfa_rule = RULE_CFA_OFFSET;
1052 break;
1054 case DW_CFA_def_cfa_expression:
1056 ULONG_PTR expr = (ULONG_PTR)ptr;
1057 ULONG_PTR len = dwarf_get_uleb128( &ptr );
1058 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len );
1059 info->state.cfa_offset = expr;
1060 info->state.cfa_rule = RULE_VAL_EXPRESSION;
1061 ptr += len;
1062 break;
1064 case DW_CFA_expression:
1065 case DW_CFA_val_expression:
1067 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1068 ULONG_PTR expr = (ULONG_PTR)ptr;
1069 ULONG_PTR len = dwarf_get_uleb128( &ptr );
1070 if (!valid_reg( reg )) break;
1071 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
1072 info->ip, (op == DW_CFA_expression) ? "" : "val_", dwarf_reg_names[reg], expr, expr+len );
1073 info->state.regs[reg] = expr;
1074 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
1075 ptr += len;
1076 break;
1078 default:
1079 FIXME( "%lx: unknown CFA opcode %02x\n", info->ip, op );
1080 break;
1085 /* retrieve a context register from its dwarf number */
1086 static void *get_context_reg( CONTEXT *context, ULONG_PTR dw_reg )
1088 switch (dw_reg)
1090 case 0: return &context->Rax;
1091 case 1: return &context->Rdx;
1092 case 2: return &context->Rcx;
1093 case 3: return &context->Rbx;
1094 case 4: return &context->Rsi;
1095 case 5: return &context->Rdi;
1096 case 6: return &context->Rbp;
1097 case 7: return &context->Rsp;
1098 case 8: return &context->R8;
1099 case 9: return &context->R9;
1100 case 10: return &context->R10;
1101 case 11: return &context->R11;
1102 case 12: return &context->R12;
1103 case 13: return &context->R13;
1104 case 14: return &context->R14;
1105 case 15: return &context->R15;
1106 case 16: return &context->Rip;
1107 case 17: return &context->u.s.Xmm0;
1108 case 18: return &context->u.s.Xmm1;
1109 case 19: return &context->u.s.Xmm2;
1110 case 20: return &context->u.s.Xmm3;
1111 case 21: return &context->u.s.Xmm4;
1112 case 22: return &context->u.s.Xmm5;
1113 case 23: return &context->u.s.Xmm6;
1114 case 24: return &context->u.s.Xmm7;
1115 case 25: return &context->u.s.Xmm8;
1116 case 26: return &context->u.s.Xmm9;
1117 case 27: return &context->u.s.Xmm10;
1118 case 28: return &context->u.s.Xmm11;
1119 case 29: return &context->u.s.Xmm12;
1120 case 30: return &context->u.s.Xmm13;
1121 case 31: return &context->u.s.Xmm14;
1122 case 32: return &context->u.s.Xmm15;
1123 case 33: return &context->u.s.Legacy[0];
1124 case 34: return &context->u.s.Legacy[1];
1125 case 35: return &context->u.s.Legacy[2];
1126 case 36: return &context->u.s.Legacy[3];
1127 case 37: return &context->u.s.Legacy[4];
1128 case 38: return &context->u.s.Legacy[5];
1129 case 39: return &context->u.s.Legacy[6];
1130 case 40: return &context->u.s.Legacy[7];
1131 default: return NULL;
1135 /* set a context register from its dwarf number */
1136 static void set_context_reg( CONTEXT *context, ULONG_PTR dw_reg, void *val )
1138 switch (dw_reg)
1140 case 0: context->Rax = *(ULONG64 *)val; break;
1141 case 1: context->Rdx = *(ULONG64 *)val; break;
1142 case 2: context->Rcx = *(ULONG64 *)val; break;
1143 case 3: context->Rbx = *(ULONG64 *)val; break;
1144 case 4: context->Rsi = *(ULONG64 *)val; break;
1145 case 5: context->Rdi = *(ULONG64 *)val; break;
1146 case 6: context->Rbp = *(ULONG64 *)val; break;
1147 case 7: context->Rsp = *(ULONG64 *)val; break;
1148 case 8: context->R8 = *(ULONG64 *)val; break;
1149 case 9: context->R9 = *(ULONG64 *)val; break;
1150 case 10: context->R10 = *(ULONG64 *)val; break;
1151 case 11: context->R11 = *(ULONG64 *)val; break;
1152 case 12: context->R12 = *(ULONG64 *)val; break;
1153 case 13: context->R13 = *(ULONG64 *)val; break;
1154 case 14: context->R14 = *(ULONG64 *)val; break;
1155 case 15: context->R15 = *(ULONG64 *)val; break;
1156 case 16: context->Rip = *(ULONG64 *)val; break;
1157 case 17: context->u.s.Xmm0 = *(M128A *)val; break;
1158 case 18: context->u.s.Xmm1 = *(M128A *)val; break;
1159 case 19: context->u.s.Xmm2 = *(M128A *)val; break;
1160 case 20: context->u.s.Xmm3 = *(M128A *)val; break;
1161 case 21: context->u.s.Xmm4 = *(M128A *)val; break;
1162 case 22: context->u.s.Xmm5 = *(M128A *)val; break;
1163 case 23: context->u.s.Xmm6 = *(M128A *)val; break;
1164 case 24: context->u.s.Xmm7 = *(M128A *)val; break;
1165 case 25: context->u.s.Xmm8 = *(M128A *)val; break;
1166 case 26: context->u.s.Xmm9 = *(M128A *)val; break;
1167 case 27: context->u.s.Xmm10 = *(M128A *)val; break;
1168 case 28: context->u.s.Xmm11 = *(M128A *)val; break;
1169 case 29: context->u.s.Xmm12 = *(M128A *)val; break;
1170 case 30: context->u.s.Xmm13 = *(M128A *)val; break;
1171 case 31: context->u.s.Xmm14 = *(M128A *)val; break;
1172 case 32: context->u.s.Xmm15 = *(M128A *)val; break;
1173 case 33: context->u.s.Legacy[0] = *(M128A *)val; break;
1174 case 34: context->u.s.Legacy[1] = *(M128A *)val; break;
1175 case 35: context->u.s.Legacy[2] = *(M128A *)val; break;
1176 case 36: context->u.s.Legacy[3] = *(M128A *)val; break;
1177 case 37: context->u.s.Legacy[4] = *(M128A *)val; break;
1178 case 38: context->u.s.Legacy[5] = *(M128A *)val; break;
1179 case 39: context->u.s.Legacy[6] = *(M128A *)val; break;
1180 case 40: context->u.s.Legacy[7] = *(M128A *)val; break;
1184 static ULONG_PTR eval_expression( const unsigned char *p, CONTEXT *context )
1186 ULONG_PTR reg, tmp, stack[64];
1187 int sp = -1;
1188 ULONG_PTR len = dwarf_get_uleb128(&p);
1189 const unsigned char *end = p + len;
1191 while (p < end)
1193 unsigned char opcode = dwarf_get_u1(&p);
1195 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
1196 stack[++sp] = opcode - DW_OP_lit0;
1197 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
1198 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_reg0 );
1199 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
1200 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_breg0 ) + dwarf_get_sleb128(&p);
1201 else switch (opcode)
1203 case DW_OP_nop: break;
1204 case DW_OP_addr: stack[++sp] = dwarf_get_u8(&p); break;
1205 case DW_OP_const1u: stack[++sp] = dwarf_get_u1(&p); break;
1206 case DW_OP_const1s: stack[++sp] = (signed char)dwarf_get_u1(&p); break;
1207 case DW_OP_const2u: stack[++sp] = dwarf_get_u2(&p); break;
1208 case DW_OP_const2s: stack[++sp] = (short)dwarf_get_u2(&p); break;
1209 case DW_OP_const4u: stack[++sp] = dwarf_get_u4(&p); break;
1210 case DW_OP_const4s: stack[++sp] = (signed int)dwarf_get_u4(&p); break;
1211 case DW_OP_const8u: stack[++sp] = dwarf_get_u8(&p); break;
1212 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf_get_u8(&p); break;
1213 case DW_OP_constu: stack[++sp] = dwarf_get_uleb128(&p); break;
1214 case DW_OP_consts: stack[++sp] = dwarf_get_sleb128(&p); break;
1215 case DW_OP_deref: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1216 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
1217 case DW_OP_drop: sp--; break;
1218 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
1219 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf_get_u1(&p)]; sp++; break;
1220 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
1221 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
1222 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
1223 case DW_OP_neg: stack[sp] = -stack[sp]; break;
1224 case DW_OP_not: stack[sp] = ~stack[sp]; break;
1225 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
1226 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
1227 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
1228 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
1229 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
1230 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
1231 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
1232 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
1233 case DW_OP_plus_uconst: stack[sp] += dwarf_get_uleb128(&p); break;
1234 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
1235 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
1236 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
1237 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
1238 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
1239 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
1240 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
1241 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
1242 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
1243 case DW_OP_skip: tmp = (short)dwarf_get_u2(&p); p += tmp; break;
1244 case DW_OP_bra: tmp = (short)dwarf_get_u2(&p); if (!stack[sp--]) p += tmp; break;
1245 case DW_OP_GNU_encoded_addr: tmp = *p++; stack[++sp] = dwarf_get_ptr( &p, tmp ); break;
1246 case DW_OP_regx: stack[++sp] = *(ULONG_PTR *)get_context_reg( context, dwarf_get_uleb128(&p) ); break;
1247 case DW_OP_bregx:
1248 reg = dwarf_get_uleb128(&p);
1249 tmp = dwarf_get_sleb128(&p);
1250 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, reg ) + tmp;
1251 break;
1252 case DW_OP_deref_size:
1253 switch (*p++)
1255 case 1: stack[sp] = *(unsigned char *)stack[sp]; break;
1256 case 2: stack[sp] = *(unsigned short *)stack[sp]; break;
1257 case 4: stack[sp] = *(unsigned int *)stack[sp]; break;
1258 case 8: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1260 break;
1261 default:
1262 FIXME( "unhandled opcode %02x\n", opcode );
1265 return stack[sp];
1268 /* apply the computed frame info to the actual context */
1269 static void apply_frame_state( CONTEXT *context, struct frame_state *state )
1271 unsigned int i;
1272 ULONG_PTR cfa, value;
1273 CONTEXT new_context = *context;
1275 switch (state->cfa_rule)
1277 case RULE_EXPRESSION:
1278 cfa = *(ULONG_PTR *)eval_expression( (const unsigned char *)state->cfa_offset, context );
1279 break;
1280 case RULE_VAL_EXPRESSION:
1281 cfa = eval_expression( (const unsigned char *)state->cfa_offset, context );
1282 break;
1283 default:
1284 cfa = *(ULONG_PTR *)get_context_reg( context, state->cfa_reg ) + state->cfa_offset;
1285 break;
1287 if (!cfa) return;
1289 for (i = 0; i < NB_FRAME_REGS; i++)
1291 switch (state->rules[i])
1293 case RULE_UNSET:
1294 case RULE_UNDEFINED:
1295 case RULE_SAME:
1296 break;
1297 case RULE_CFA_OFFSET:
1298 set_context_reg( &new_context, i, (char *)cfa + state->regs[i] );
1299 break;
1300 case RULE_OTHER_REG:
1301 set_context_reg( &new_context, i, get_context_reg( context, state->regs[i] ));
1302 break;
1303 case RULE_EXPRESSION:
1304 value = eval_expression( (const unsigned char *)state->regs[i], context );
1305 set_context_reg( &new_context, i, (void *)value );
1306 break;
1307 case RULE_VAL_EXPRESSION:
1308 value = eval_expression( (const unsigned char *)state->regs[i], context );
1309 set_context_reg( &new_context, i, &value );
1310 break;
1313 new_context.Rsp = cfa;
1314 *context = new_context;
1318 /***********************************************************************
1319 * dwarf_virtual_unwind
1321 * Equivalent of RtlVirtualUnwind for builtin modules.
1323 static NTSTATUS dwarf_virtual_unwind( ULONG64 ip, ULONG64 *frame,CONTEXT *context,
1324 const struct dwarf_fde *fde, const struct dwarf_eh_bases *bases,
1325 PEXCEPTION_ROUTINE *handler, void **handler_data )
1327 const struct dwarf_cie *cie;
1328 const unsigned char *ptr, *augmentation, *end;
1329 ULONG_PTR len, code_end;
1330 struct frame_info info;
1331 struct frame_state state_stack[MAX_SAVED_STATES];
1332 int aug_z_format = 0;
1333 unsigned char lsda_encoding = DW_EH_PE_omit;
1335 memset( &info, 0, sizeof(info) );
1336 info.state_stack = state_stack;
1337 info.ip = (ULONG_PTR)bases->func;
1338 *handler = NULL;
1340 cie = (const struct dwarf_cie *)((const char *)&fde->cie_offset - fde->cie_offset);
1342 /* parse the CIE first */
1344 if (cie->version != 1 && cie->version != 3)
1346 FIXME( "unknown CIE version %u at %p\n", cie->version, cie );
1347 return STATUS_INVALID_DISPOSITION;
1349 ptr = cie->augmentation + strlen((const char *)cie->augmentation) + 1;
1351 info.code_align = dwarf_get_uleb128( &ptr );
1352 info.data_align = dwarf_get_sleb128( &ptr );
1353 if (cie->version == 1)
1354 info.retaddr_reg = *ptr++;
1355 else
1356 info.retaddr_reg = dwarf_get_uleb128( &ptr );
1357 info.state.cfa_rule = RULE_CFA_OFFSET;
1359 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1360 ip, bases->func, cie, cie->length, cie->id, cie->version, cie->augmentation,
1361 info.code_align, info.data_align, dwarf_reg_names[info.retaddr_reg] );
1363 end = NULL;
1364 for (augmentation = cie->augmentation; *augmentation; augmentation++)
1366 switch (*augmentation)
1368 case 'z':
1369 len = dwarf_get_uleb128( &ptr );
1370 end = ptr + len;
1371 aug_z_format = 1;
1372 continue;
1373 case 'L':
1374 lsda_encoding = *ptr++;
1375 continue;
1376 case 'P':
1378 unsigned char encoding = *ptr++;
1379 *handler = (void *)dwarf_get_ptr( &ptr, encoding );
1380 continue;
1382 case 'R':
1383 info.fde_encoding = *ptr++;
1384 continue;
1385 case 'S':
1386 info.signal_frame = 1;
1387 continue;
1389 FIXME( "unknown augmentation '%c'\n", *augmentation );
1390 if (!end) return STATUS_INVALID_DISPOSITION; /* cannot continue */
1391 break;
1393 if (end) ptr = end;
1395 end = (const unsigned char *)(&cie->length + 1) + cie->length;
1396 execute_cfa_instructions( ptr, end, ip, &info );
1398 ptr = (const unsigned char *)(fde + 1);
1399 info.ip = dwarf_get_ptr( &ptr, info.fde_encoding ); /* fde code start */
1400 code_end = info.ip + dwarf_get_ptr( &ptr, info.fde_encoding & 0x0f ); /* fde code length */
1402 if (aug_z_format) /* get length of augmentation data */
1404 len = dwarf_get_uleb128( &ptr );
1405 end = ptr + len;
1407 else end = NULL;
1409 *handler_data = (void *)dwarf_get_ptr( &ptr, lsda_encoding );
1410 if (end) ptr = end;
1412 end = (const unsigned char *)(&fde->length + 1) + fde->length;
1413 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1414 fde, fde->length, *handler, *handler_data, info.ip, code_end );
1415 execute_cfa_instructions( ptr, end, ip, &info );
1416 *frame = context->Rsp;
1417 apply_frame_state( context, &info.state );
1419 TRACE( "next function rip=%016lx\n", context->Rip );
1420 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1421 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1422 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1423 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1424 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1425 context->R8, context->R9, context->R10, context->R11 );
1426 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1427 context->R12, context->R13, context->R14, context->R15 );
1429 return STATUS_SUCCESS;
1433 #ifdef HAVE_LIBUNWIND_H
1434 /***********************************************************************
1435 * libunwind_set_cursor_from_context
1437 static int libunwind_set_cursor_from_context( unw_cursor_t *cursor, CONTEXT *context, ULONG64 ip )
1439 int rc;
1441 rc = unw_set_reg(cursor, UNW_REG_IP, ip);
1442 if (rc == UNW_ESUCCESS)
1443 rc = unw_set_reg(cursor, UNW_REG_SP, context->Rsp);
1444 if (rc == UNW_ESUCCESS)
1445 rc = unw_set_reg(cursor, UNW_X86_64_RAX, context->Rax);
1446 if (rc == UNW_ESUCCESS)
1447 rc = unw_set_reg(cursor, UNW_X86_64_RDX, context->Rdx);
1448 if (rc == UNW_ESUCCESS)
1449 rc = unw_set_reg(cursor, UNW_X86_64_RCX, context->Rcx);
1450 if (rc == UNW_ESUCCESS)
1451 rc = unw_set_reg(cursor, UNW_X86_64_RBX, context->Rbx);
1452 if (rc == UNW_ESUCCESS)
1453 rc = unw_set_reg(cursor, UNW_X86_64_RSI, context->Rsi);
1454 if (rc == UNW_ESUCCESS)
1455 rc = unw_set_reg(cursor, UNW_X86_64_RDI, context->Rdi);
1456 if (rc == UNW_ESUCCESS)
1457 rc = unw_set_reg(cursor, UNW_X86_64_RBP, context->Rbp);
1458 if (rc == UNW_ESUCCESS)
1459 rc = unw_set_reg(cursor, UNW_X86_64_R8, context->R8);
1460 if (rc == UNW_ESUCCESS)
1461 rc = unw_set_reg(cursor, UNW_X86_64_R9, context->R9);
1462 if (rc == UNW_ESUCCESS)
1463 rc = unw_set_reg(cursor, UNW_X86_64_R10, context->R10);
1464 if (rc == UNW_ESUCCESS)
1465 rc = unw_set_reg(cursor, UNW_X86_64_R11, context->R11);
1466 if (rc == UNW_ESUCCESS)
1467 rc = unw_set_reg(cursor, UNW_X86_64_R12, context->R12);
1468 if (rc == UNW_ESUCCESS)
1469 rc = unw_set_reg(cursor, UNW_X86_64_R13, context->R13);
1470 if (rc == UNW_ESUCCESS)
1471 rc = unw_set_reg(cursor, UNW_X86_64_R14, context->R14);
1472 if (rc == UNW_ESUCCESS)
1473 rc = unw_set_reg(cursor, UNW_X86_64_R15, context->R15);
1475 return rc;
1479 /***********************************************************************
1480 * libunwind_get_reg
1482 static int libunwind_get_reg( unw_cursor_t *cursor, unw_regnum_t reg, ULONG64 *val )
1484 int rc;
1485 unw_word_t word;
1487 rc = unw_get_reg(cursor, reg, &word);
1488 if (rc == UNW_ESUCCESS)
1489 *val = word;
1491 return rc;
1495 /***********************************************************************
1496 * libunwind_set_context_from_cursor
1498 static BOOL libunwind_set_context_from_cursor( CONTEXT *context, unw_cursor_t *cursor )
1500 int rc;
1502 rc = libunwind_get_reg(cursor, UNW_REG_IP, &context->Rip);
1503 if (rc == UNW_ESUCCESS)
1504 rc = libunwind_get_reg(cursor, UNW_REG_SP, &context->Rsp);
1505 if (rc == UNW_ESUCCESS)
1506 rc = libunwind_get_reg(cursor, UNW_X86_64_RAX, &context->Rax);
1507 if (rc == UNW_ESUCCESS)
1508 rc = libunwind_get_reg(cursor, UNW_X86_64_RDX, &context->Rdx);
1509 if (rc == UNW_ESUCCESS)
1510 rc = libunwind_get_reg(cursor, UNW_X86_64_RCX, &context->Rcx);
1511 if (rc == UNW_ESUCCESS)
1512 rc = libunwind_get_reg(cursor, UNW_X86_64_RBX, &context->Rbx);
1513 if (rc == UNW_ESUCCESS)
1514 rc = libunwind_get_reg(cursor, UNW_X86_64_RSI, &context->Rsi);
1515 if (rc == UNW_ESUCCESS)
1516 rc = libunwind_get_reg(cursor, UNW_X86_64_RDI, &context->Rdi);
1517 if (rc == UNW_ESUCCESS)
1518 rc = libunwind_get_reg(cursor, UNW_X86_64_RBP, &context->Rbp);
1519 if (rc == UNW_ESUCCESS)
1520 rc = libunwind_get_reg(cursor, UNW_X86_64_R8, &context->R8);
1521 if (rc == UNW_ESUCCESS)
1522 rc = libunwind_get_reg(cursor, UNW_X86_64_R9, &context->R9);
1523 if (rc == UNW_ESUCCESS)
1524 rc = libunwind_get_reg(cursor, UNW_X86_64_R10, &context->R10);
1525 if (rc == UNW_ESUCCESS)
1526 rc = libunwind_get_reg(cursor, UNW_X86_64_R11, &context->R11);
1527 if (rc == UNW_ESUCCESS)
1528 rc = libunwind_get_reg(cursor, UNW_X86_64_R12, &context->R12);
1529 if (rc == UNW_ESUCCESS)
1530 rc = libunwind_get_reg(cursor, UNW_X86_64_R13, &context->R13);
1531 if (rc == UNW_ESUCCESS)
1532 rc = libunwind_get_reg(cursor, UNW_X86_64_R14, &context->R14);
1533 if (rc == UNW_ESUCCESS)
1534 rc = libunwind_get_reg(cursor, UNW_X86_64_R15, &context->R15);
1536 return rc;
1540 /***********************************************************************
1541 * libunwind_virtual_unwind
1543 * Equivalent of RtlVirtualUnwind for builtin modules.
1545 static NTSTATUS libunwind_virtual_unwind( ULONG64 ip, BOOL* got_info, ULONG64 *frame, CONTEXT *context,
1546 PEXCEPTION_ROUTINE *handler, void **handler_data )
1548 unw_context_t unw_context;
1549 unw_cursor_t cursor;
1550 unw_proc_info_t info;
1551 int rc;
1553 rc = unw_getcontext( &unw_context );
1554 if (rc == UNW_ESUCCESS)
1555 rc = unw_init_local( &cursor, &unw_context );
1556 if (rc == UNW_ESUCCESS)
1557 rc = libunwind_set_cursor_from_context( &cursor, context, ip - 1 );
1558 if (rc != UNW_ESUCCESS)
1560 WARN( "setup failed: %d\n", rc );
1561 return STATUS_INVALID_DISPOSITION;
1564 rc = unw_get_proc_info(&cursor, &info);
1565 if (rc != UNW_ESUCCESS && rc != UNW_ENOINFO)
1567 WARN( "failed to get info: %d\n", rc );
1568 return STATUS_INVALID_DISPOSITION;
1570 if (rc == UNW_ENOINFO || ip < info.start_ip || info.end_ip <= ip || !info.format)
1572 *got_info = FALSE;
1573 return STATUS_SUCCESS;
1576 TRACE( "ip %#lx function %#lx-%#lx personality %#lx lsda %#lx fde %#lx\n",
1577 ip, (unsigned long)info.start_ip, (unsigned long)info.end_ip, (unsigned long)info.handler,
1578 (unsigned long)info.lsda, (unsigned long)info.unwind_info );
1580 rc = unw_step(&cursor);
1581 if (rc < 0)
1583 WARN( "failed to unwind: %d\n", rc );
1584 return STATUS_INVALID_DISPOSITION;
1587 *frame = context->Rsp;
1589 rc = libunwind_set_context_from_cursor( context, &cursor );
1590 if (rc != UNW_ESUCCESS)
1592 WARN( "failed to update context after unwind: %d\n", rc );
1593 return STATUS_INVALID_DISPOSITION;
1596 *handler = (void*)info.handler;
1597 *handler_data = (void*)info.lsda;
1598 *got_info = TRUE;
1600 TRACE( "next function rip=%016lx\n", context->Rip );
1601 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1602 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1603 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1604 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1605 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1606 context->R8, context->R9, context->R10, context->R11 );
1607 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1608 context->R12, context->R13, context->R14, context->R15 );
1610 return STATUS_SUCCESS;
1612 #endif
1615 /***********************************************************************
1616 * dispatch_signal
1618 static inline int dispatch_signal(unsigned int sig)
1620 if (handlers[sig] == NULL) return 0;
1621 return handlers[sig](sig);
1624 /***********************************************************************
1625 * get_signal_stack
1627 * Get the base of the signal stack for the current thread.
1629 static inline void *get_signal_stack(void)
1631 return (char *)NtCurrentTeb() + teb_size;
1634 /***********************************************************************
1635 * is_inside_signal_stack
1637 * Check if pointer is inside the signal stack.
1639 static inline BOOL is_inside_signal_stack( void *ptr )
1641 return ((char *)ptr >= (char *)get_signal_stack() &&
1642 (char *)ptr < (char *)get_signal_stack() + signal_stack_size);
1645 /***********************************************************************
1646 * save_context
1648 * Set the register values from a sigcontext.
1650 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
1652 context->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
1653 context->Rax = RAX_sig(sigcontext);
1654 context->Rcx = RCX_sig(sigcontext);
1655 context->Rdx = RDX_sig(sigcontext);
1656 context->Rbx = RBX_sig(sigcontext);
1657 context->Rsp = RSP_sig(sigcontext);
1658 context->Rbp = RBP_sig(sigcontext);
1659 context->Rsi = RSI_sig(sigcontext);
1660 context->Rdi = RDI_sig(sigcontext);
1661 context->R8 = R8_sig(sigcontext);
1662 context->R9 = R9_sig(sigcontext);
1663 context->R10 = R10_sig(sigcontext);
1664 context->R11 = R11_sig(sigcontext);
1665 context->R12 = R12_sig(sigcontext);
1666 context->R13 = R13_sig(sigcontext);
1667 context->R14 = R14_sig(sigcontext);
1668 context->R15 = R15_sig(sigcontext);
1669 context->Rip = RIP_sig(sigcontext);
1670 context->SegCs = CS_sig(sigcontext);
1671 context->SegFs = FS_sig(sigcontext);
1672 context->SegGs = GS_sig(sigcontext);
1673 context->EFlags = EFL_sig(sigcontext);
1674 #ifdef DS_sig
1675 context->SegDs = DS_sig(sigcontext);
1676 #else
1677 __asm__("movw %%ds,%0" : "=m" (context->SegDs));
1678 #endif
1679 #ifdef ES_sig
1680 context->SegEs = ES_sig(sigcontext);
1681 #else
1682 __asm__("movw %%es,%0" : "=m" (context->SegEs));
1683 #endif
1684 #ifdef SS_sig
1685 context->SegSs = SS_sig(sigcontext);
1686 #else
1687 __asm__("movw %%ss,%0" : "=m" (context->SegSs));
1688 #endif
1689 if (FPU_sig(sigcontext))
1691 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1692 context->u.FltSave = *FPU_sig(sigcontext);
1693 context->MxCsr = context->u.FltSave.MxCsr;
1698 /***********************************************************************
1699 * restore_context
1701 * Build a sigcontext from the register values.
1703 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1705 RAX_sig(sigcontext) = context->Rax;
1706 RCX_sig(sigcontext) = context->Rcx;
1707 RDX_sig(sigcontext) = context->Rdx;
1708 RBX_sig(sigcontext) = context->Rbx;
1709 RSP_sig(sigcontext) = context->Rsp;
1710 RBP_sig(sigcontext) = context->Rbp;
1711 RSI_sig(sigcontext) = context->Rsi;
1712 RDI_sig(sigcontext) = context->Rdi;
1713 R8_sig(sigcontext) = context->R8;
1714 R9_sig(sigcontext) = context->R9;
1715 R10_sig(sigcontext) = context->R10;
1716 R11_sig(sigcontext) = context->R11;
1717 R12_sig(sigcontext) = context->R12;
1718 R13_sig(sigcontext) = context->R13;
1719 R14_sig(sigcontext) = context->R14;
1720 R15_sig(sigcontext) = context->R15;
1721 RIP_sig(sigcontext) = context->Rip;
1722 CS_sig(sigcontext) = context->SegCs;
1723 FS_sig(sigcontext) = context->SegFs;
1724 GS_sig(sigcontext) = context->SegGs;
1725 EFL_sig(sigcontext) = context->EFlags;
1726 #ifdef DS_sig
1727 DS_sig(sigcontext) = context->SegDs;
1728 #endif
1729 #ifdef ES_sig
1730 ES_sig(sigcontext) = context->SegEs;
1731 #endif
1732 #ifdef SS_sig
1733 SS_sig(sigcontext) = context->SegSs;
1734 #endif
1735 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
1739 /**************************************************************************
1740 * __chkstk (NTDLL.@)
1742 * Supposed to touch all the stack pages, but we shouldn't need that.
1744 __ASM_GLOBAL_FUNC( __chkstk, "ret" );
1747 /***********************************************************************
1748 * RtlCaptureContext (NTDLL.@)
1750 __ASM_GLOBAL_FUNC( RtlCaptureContext,
1751 "pushfq\n\t"
1752 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1753 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1754 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1755 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1756 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1757 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1758 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1759 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1760 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1761 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1762 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1763 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1764 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1765 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1766 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1767 "leaq 8(%rsp),%rax\n\t"
1768 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1769 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1770 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1771 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1772 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1773 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1774 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1775 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1776 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1777 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1778 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1779 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1780 "movq (%rsp),%rax\n\t"
1781 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1782 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1783 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1784 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1785 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1786 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1787 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1788 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1789 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1790 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1791 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1792 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1793 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1794 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1795 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1796 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1797 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1798 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1799 "ret" );
1801 /***********************************************************************
1802 * set_full_cpu_context
1804 * Set the new CPU context.
1806 extern void set_full_cpu_context( const CONTEXT *context );
1807 __ASM_GLOBAL_FUNC( set_full_cpu_context,
1808 "subq $40,%rsp\n\t"
1809 __ASM_CFI(".cfi_adjust_cfa_offset 40\n\t")
1810 "ldmxcsr 0x34(%rdi)\n\t" /* context->MxCsr */
1811 "movw 0x38(%rdi),%ax\n\t" /* context->SegCs */
1812 "movq %rax,8(%rsp)\n\t"
1813 "movw 0x42(%rdi),%ax\n\t" /* context->SegSs */
1814 "movq %rax,32(%rsp)\n\t"
1815 "movq 0x44(%rdi),%rax\n\t" /* context->Eflags */
1816 "movq %rax,16(%rsp)\n\t"
1817 "movq 0x80(%rdi),%rcx\n\t" /* context->Rcx */
1818 "movq 0x88(%rdi),%rdx\n\t" /* context->Rdx */
1819 "movq 0x90(%rdi),%rbx\n\t" /* context->Rbx */
1820 "movq 0x98(%rdi),%rax\n\t" /* context->Rsp */
1821 "movq %rax,24(%rsp)\n\t"
1822 "movq 0xa0(%rdi),%rbp\n\t" /* context->Rbp */
1823 "movq 0xa8(%rdi),%rsi\n\t" /* context->Rsi */
1824 "movq 0xb8(%rdi),%r8\n\t" /* context->R8 */
1825 "movq 0xc0(%rdi),%r9\n\t" /* context->R9 */
1826 "movq 0xc8(%rdi),%r10\n\t" /* context->R10 */
1827 "movq 0xd0(%rdi),%r11\n\t" /* context->R11 */
1828 "movq 0xd8(%rdi),%r12\n\t" /* context->R12 */
1829 "movq 0xe0(%rdi),%r13\n\t" /* context->R13 */
1830 "movq 0xe8(%rdi),%r14\n\t" /* context->R14 */
1831 "movq 0xf0(%rdi),%r15\n\t" /* context->R15 */
1832 "movq 0xf8(%rdi),%rax\n\t" /* context->Rip */
1833 "movq %rax,(%rsp)\n\t"
1834 "fxrstor 0x100(%rdi)\n\t" /* context->FtlSave */
1835 "movdqa 0x1a0(%rdi),%xmm0\n\t" /* context->Xmm0 */
1836 "movdqa 0x1b0(%rdi),%xmm1\n\t" /* context->Xmm1 */
1837 "movdqa 0x1c0(%rdi),%xmm2\n\t" /* context->Xmm2 */
1838 "movdqa 0x1d0(%rdi),%xmm3\n\t" /* context->Xmm3 */
1839 "movdqa 0x1e0(%rdi),%xmm4\n\t" /* context->Xmm4 */
1840 "movdqa 0x1f0(%rdi),%xmm5\n\t" /* context->Xmm5 */
1841 "movdqa 0x200(%rdi),%xmm6\n\t" /* context->Xmm6 */
1842 "movdqa 0x210(%rdi),%xmm7\n\t" /* context->Xmm7 */
1843 "movdqa 0x220(%rdi),%xmm8\n\t" /* context->Xmm8 */
1844 "movdqa 0x230(%rdi),%xmm9\n\t" /* context->Xmm9 */
1845 "movdqa 0x240(%rdi),%xmm10\n\t" /* context->Xmm10 */
1846 "movdqa 0x250(%rdi),%xmm11\n\t" /* context->Xmm11 */
1847 "movdqa 0x260(%rdi),%xmm12\n\t" /* context->Xmm12 */
1848 "movdqa 0x270(%rdi),%xmm13\n\t" /* context->Xmm13 */
1849 "movdqa 0x280(%rdi),%xmm14\n\t" /* context->Xmm14 */
1850 "movdqa 0x290(%rdi),%xmm15\n\t" /* context->Xmm15 */
1851 "movq 0x78(%rdi),%rax\n\t" /* context->Rax */
1852 "movq 0xb0(%rdi),%rdi\n\t" /* context->Rdi */
1853 "iretq" );
1856 /***********************************************************************
1857 * set_cpu_context
1859 * Set the new CPU context. Used by NtSetContextThread.
1861 void set_cpu_context( const CONTEXT *context )
1863 DWORD flags = context->ContextFlags & ~CONTEXT_AMD64;
1864 if (flags & CONTEXT_FULL)
1866 if (!(flags & CONTEXT_CONTROL))
1867 FIXME( "setting partial context (%x) not supported\n", flags );
1868 else
1869 set_full_cpu_context( context );
1874 /***********************************************************************
1875 * copy_context
1877 * Copy a register context according to the flags.
1879 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1881 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
1882 if (flags & CONTEXT_CONTROL)
1884 to->Rbp = from->Rbp;
1885 to->Rip = from->Rip;
1886 to->Rsp = from->Rsp;
1887 to->SegCs = from->SegCs;
1888 to->SegSs = from->SegSs;
1889 to->EFlags = from->EFlags;
1891 if (flags & CONTEXT_INTEGER)
1893 to->Rax = from->Rax;
1894 to->Rcx = from->Rcx;
1895 to->Rdx = from->Rdx;
1896 to->Rbx = from->Rbx;
1897 to->Rsi = from->Rsi;
1898 to->Rdi = from->Rdi;
1899 to->R8 = from->R8;
1900 to->R9 = from->R9;
1901 to->R10 = from->R10;
1902 to->R11 = from->R11;
1903 to->R12 = from->R12;
1904 to->R13 = from->R13;
1905 to->R14 = from->R14;
1906 to->R15 = from->R15;
1908 if (flags & CONTEXT_SEGMENTS)
1910 to->SegDs = from->SegDs;
1911 to->SegEs = from->SegEs;
1912 to->SegFs = from->SegFs;
1913 to->SegGs = from->SegGs;
1915 if (flags & CONTEXT_FLOATING_POINT)
1917 to->MxCsr = from->MxCsr;
1918 to->u.FltSave = from->u.FltSave;
1920 if (flags & CONTEXT_DEBUG_REGISTERS)
1922 to->Dr0 = from->Dr0;
1923 to->Dr1 = from->Dr1;
1924 to->Dr2 = from->Dr2;
1925 to->Dr3 = from->Dr3;
1926 to->Dr6 = from->Dr6;
1927 to->Dr7 = from->Dr7;
1932 /***********************************************************************
1933 * context_to_server
1935 * Convert a register context to the server format.
1937 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1939 DWORD flags = from->ContextFlags & ~CONTEXT_AMD64; /* get rid of CPU id */
1941 memset( to, 0, sizeof(*to) );
1942 to->cpu = CPU_x86_64;
1944 if (flags & CONTEXT_CONTROL)
1946 to->flags |= SERVER_CTX_CONTROL;
1947 to->ctl.x86_64_regs.rbp = from->Rbp;
1948 to->ctl.x86_64_regs.rip = from->Rip;
1949 to->ctl.x86_64_regs.rsp = from->Rsp;
1950 to->ctl.x86_64_regs.cs = from->SegCs;
1951 to->ctl.x86_64_regs.ss = from->SegSs;
1952 to->ctl.x86_64_regs.flags = from->EFlags;
1954 if (flags & CONTEXT_INTEGER)
1956 to->flags |= SERVER_CTX_INTEGER;
1957 to->integer.x86_64_regs.rax = from->Rax;
1958 to->integer.x86_64_regs.rcx = from->Rcx;
1959 to->integer.x86_64_regs.rdx = from->Rdx;
1960 to->integer.x86_64_regs.rbx = from->Rbx;
1961 to->integer.x86_64_regs.rsi = from->Rsi;
1962 to->integer.x86_64_regs.rdi = from->Rdi;
1963 to->integer.x86_64_regs.r8 = from->R8;
1964 to->integer.x86_64_regs.r9 = from->R9;
1965 to->integer.x86_64_regs.r10 = from->R10;
1966 to->integer.x86_64_regs.r11 = from->R11;
1967 to->integer.x86_64_regs.r12 = from->R12;
1968 to->integer.x86_64_regs.r13 = from->R13;
1969 to->integer.x86_64_regs.r14 = from->R14;
1970 to->integer.x86_64_regs.r15 = from->R15;
1972 if (flags & CONTEXT_SEGMENTS)
1974 to->flags |= SERVER_CTX_SEGMENTS;
1975 to->seg.x86_64_regs.ds = from->SegDs;
1976 to->seg.x86_64_regs.es = from->SegEs;
1977 to->seg.x86_64_regs.fs = from->SegFs;
1978 to->seg.x86_64_regs.gs = from->SegGs;
1980 if (flags & CONTEXT_FLOATING_POINT)
1982 to->flags |= SERVER_CTX_FLOATING_POINT;
1983 memcpy( to->fp.x86_64_regs.fpregs, &from->u.FltSave, sizeof(to->fp.x86_64_regs.fpregs) );
1985 if (flags & CONTEXT_DEBUG_REGISTERS)
1987 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1988 to->debug.x86_64_regs.dr0 = from->Dr0;
1989 to->debug.x86_64_regs.dr1 = from->Dr1;
1990 to->debug.x86_64_regs.dr2 = from->Dr2;
1991 to->debug.x86_64_regs.dr3 = from->Dr3;
1992 to->debug.x86_64_regs.dr6 = from->Dr6;
1993 to->debug.x86_64_regs.dr7 = from->Dr7;
1995 return STATUS_SUCCESS;
1999 /***********************************************************************
2000 * context_from_server
2002 * Convert a register context from the server format.
2004 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
2006 if (from->cpu != CPU_x86_64) return STATUS_INVALID_PARAMETER;
2008 to->ContextFlags = CONTEXT_AMD64;
2009 if (from->flags & SERVER_CTX_CONTROL)
2011 to->ContextFlags |= CONTEXT_CONTROL;
2012 to->Rbp = from->ctl.x86_64_regs.rbp;
2013 to->Rip = from->ctl.x86_64_regs.rip;
2014 to->Rsp = from->ctl.x86_64_regs.rsp;
2015 to->SegCs = from->ctl.x86_64_regs.cs;
2016 to->SegSs = from->ctl.x86_64_regs.ss;
2017 to->EFlags = from->ctl.x86_64_regs.flags;
2020 if (from->flags & SERVER_CTX_INTEGER)
2022 to->ContextFlags |= CONTEXT_INTEGER;
2023 to->Rax = from->integer.x86_64_regs.rax;
2024 to->Rcx = from->integer.x86_64_regs.rcx;
2025 to->Rdx = from->integer.x86_64_regs.rdx;
2026 to->Rbx = from->integer.x86_64_regs.rbx;
2027 to->Rsi = from->integer.x86_64_regs.rsi;
2028 to->Rdi = from->integer.x86_64_regs.rdi;
2029 to->R8 = from->integer.x86_64_regs.r8;
2030 to->R9 = from->integer.x86_64_regs.r9;
2031 to->R10 = from->integer.x86_64_regs.r10;
2032 to->R11 = from->integer.x86_64_regs.r11;
2033 to->R12 = from->integer.x86_64_regs.r12;
2034 to->R13 = from->integer.x86_64_regs.r13;
2035 to->R14 = from->integer.x86_64_regs.r14;
2036 to->R15 = from->integer.x86_64_regs.r15;
2038 if (from->flags & SERVER_CTX_SEGMENTS)
2040 to->ContextFlags |= CONTEXT_SEGMENTS;
2041 to->SegDs = from->seg.x86_64_regs.ds;
2042 to->SegEs = from->seg.x86_64_regs.es;
2043 to->SegFs = from->seg.x86_64_regs.fs;
2044 to->SegGs = from->seg.x86_64_regs.gs;
2046 if (from->flags & SERVER_CTX_FLOATING_POINT)
2048 to->ContextFlags |= CONTEXT_FLOATING_POINT;
2049 memcpy( &to->u.FltSave, from->fp.x86_64_regs.fpregs, sizeof(from->fp.x86_64_regs.fpregs) );
2050 to->MxCsr = to->u.FltSave.MxCsr;
2052 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
2054 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
2055 to->Dr0 = from->debug.x86_64_regs.dr0;
2056 to->Dr1 = from->debug.x86_64_regs.dr1;
2057 to->Dr2 = from->debug.x86_64_regs.dr2;
2058 to->Dr3 = from->debug.x86_64_regs.dr3;
2059 to->Dr6 = from->debug.x86_64_regs.dr6;
2060 to->Dr7 = from->debug.x86_64_regs.dr7;
2062 return STATUS_SUCCESS;
2066 extern void raise_func_trampoline( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
2067 __ASM_GLOBAL_FUNC( raise_func_trampoline,
2068 __ASM_CFI(".cfi_signal_frame\n\t")
2069 __ASM_CFI(".cfi_def_cfa %rbp,160\n\t") /* red zone + rip + rbp + rdi + rsi */
2070 __ASM_CFI(".cfi_rel_offset %rip,24\n\t")
2071 __ASM_CFI(".cfi_rel_offset %rbp,16\n\t")
2072 __ASM_CFI(".cfi_rel_offset %rdi,8\n\t")
2073 __ASM_CFI(".cfi_rel_offset %rsi,0\n\t")
2074 "call *%rdx\n\t"
2075 "int $3")
2077 /***********************************************************************
2078 * setup_exception
2080 * Setup a proper stack frame for the raise function, and modify the
2081 * sigcontext so that the return from the signal handler will call
2082 * the raise function.
2084 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
2086 struct stack_layout
2088 CONTEXT context;
2089 EXCEPTION_RECORD rec;
2090 ULONG64 rsi;
2091 ULONG64 rdi;
2092 ULONG64 rbp;
2093 ULONG64 rip;
2094 ULONG64 red_zone[16];
2095 } *stack;
2096 ULONG64 *rsp_ptr;
2097 DWORD exception_code = 0;
2099 stack = (struct stack_layout *)(RSP_sig(sigcontext) & ~15);
2101 /* stack sanity checks */
2103 if (is_inside_signal_stack( stack ))
2105 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2106 GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext), (ULONG_PTR)RSP_sig(sigcontext),
2107 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2108 abort_thread(1);
2111 if (stack - 1 > stack || /* check for overflow in subtraction */
2112 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
2113 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
2115 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2116 GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext), (ULONG_PTR)RSP_sig(sigcontext),
2117 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2119 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
2121 /* stack overflow on last page, unrecoverable */
2122 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
2123 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2124 diff, GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext),
2125 (ULONG_PTR)RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
2126 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2127 abort_thread(1);
2129 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
2131 /* stack access below stack limit, may be recoverable */
2132 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
2133 else
2135 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
2136 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2137 diff, GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext),
2138 (ULONG_PTR)RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
2139 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2140 abort_thread(1);
2144 stack--; /* push the stack_layout structure */
2145 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
2146 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
2147 #elif defined(VALGRIND_MAKE_WRITABLE)
2148 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
2149 #endif
2150 stack->rec.ExceptionRecord = NULL;
2151 stack->rec.ExceptionCode = exception_code;
2152 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2153 stack->rec.ExceptionAddress = (void *)RIP_sig(sigcontext);
2154 stack->rec.NumberParameters = 0;
2155 save_context( &stack->context, sigcontext );
2157 /* store return address and %rbp without aligning, so that the offset is fixed */
2158 rsp_ptr = (ULONG64 *)RSP_sig(sigcontext) - 16;
2159 *(--rsp_ptr) = RIP_sig(sigcontext);
2160 *(--rsp_ptr) = RBP_sig(sigcontext);
2161 *(--rsp_ptr) = RDI_sig(sigcontext);
2162 *(--rsp_ptr) = RSI_sig(sigcontext);
2164 /* now modify the sigcontext to return to the raise function */
2165 RIP_sig(sigcontext) = (ULONG_PTR)raise_func_trampoline;
2166 RDI_sig(sigcontext) = (ULONG_PTR)&stack->rec;
2167 RSI_sig(sigcontext) = (ULONG_PTR)&stack->context;
2168 RDX_sig(sigcontext) = (ULONG_PTR)func;
2169 RBP_sig(sigcontext) = (ULONG_PTR)rsp_ptr;
2170 RSP_sig(sigcontext) = (ULONG_PTR)stack;
2171 /* clear single-step, direction, and align check flag */
2172 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
2174 return &stack->rec;
2178 /***********************************************************************
2179 * get_exception_context
2181 * Get a pointer to the context built by setup_exception.
2183 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
2185 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
2189 /**********************************************************************
2190 * find_function_info
2192 static RUNTIME_FUNCTION *find_function_info( ULONG64 pc, HMODULE module,
2193 RUNTIME_FUNCTION *func, ULONG size )
2195 int min = 0;
2196 int max = size/sizeof(*func) - 1;
2198 while (min <= max)
2200 int pos = (min + max) / 2;
2201 if ((char *)pc < (char *)module + func[pos].BeginAddress) max = pos - 1;
2202 else if ((char *)pc >= (char *)module + func[pos].EndAddress) min = pos + 1;
2203 else
2205 func += pos;
2206 while (func->UnwindData & 1) /* follow chained entry */
2207 func = (RUNTIME_FUNCTION *)((char *)module + (func->UnwindData & ~1));
2208 return func;
2211 return NULL;
2214 /**********************************************************************
2215 * lookup_function_info
2217 static RUNTIME_FUNCTION *lookup_function_info( ULONG64 pc, ULONG64 *base, LDR_MODULE **module )
2219 RUNTIME_FUNCTION *func = NULL;
2220 struct dynamic_unwind_entry *entry;
2221 ULONG size;
2223 /* PE module or wine module */
2224 if (!LdrFindEntryForAddress( (void *)pc, module ))
2226 *base = (ULONG64)(*module)->BaseAddress;
2227 if ((func = RtlImageDirectoryEntryToData( (*module)->BaseAddress, TRUE,
2228 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2230 /* lookup in function table */
2231 func = find_function_info( pc, (*module)->BaseAddress, func, size );
2234 else
2236 *module = NULL;
2238 RtlEnterCriticalSection( &dynamic_unwind_section );
2239 LIST_FOR_EACH_ENTRY( entry, &dynamic_unwind_list, struct dynamic_unwind_entry, entry )
2241 if (pc >= entry->base && pc < entry->base + entry->size)
2243 *base = entry->base;
2245 /* use callback or lookup in function table */
2246 if (entry->callback)
2247 func = entry->callback( pc, entry->context );
2248 else
2249 func = find_function_info( pc, (HMODULE)entry->base, entry->table, entry->table_size );
2250 break;
2253 RtlLeaveCriticalSection( &dynamic_unwind_section );
2256 return func;
2259 static DWORD nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
2260 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
2262 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)) return ExceptionContinueSearch;
2264 /* FIXME */
2265 return ExceptionNestedException;
2268 /**********************************************************************
2269 * call_handler
2271 * Call a single exception handler.
2272 * FIXME: Handle nested exceptions.
2274 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
2276 EXCEPTION_REGISTRATION_RECORD frame;
2277 DWORD res;
2279 frame.Handler = nested_exception_handler;
2280 __wine_push_frame( &frame );
2282 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2283 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2284 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, context, dispatch );
2285 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
2287 __wine_pop_frame( &frame );
2288 return res;
2292 /**********************************************************************
2293 * call_teb_handler
2295 * Call a single exception handler from the TEB chain.
2296 * FIXME: Handle nested exceptions.
2298 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
2299 EXCEPTION_REGISTRATION_RECORD *teb_frame )
2301 DWORD res;
2303 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
2304 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
2305 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
2306 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
2307 return res;
2311 /**********************************************************************
2312 * call_stack_handlers
2314 * Call the stack handlers chain.
2316 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
2318 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
2319 UNWIND_HISTORY_TABLE table;
2320 DISPATCHER_CONTEXT dispatch;
2321 CONTEXT context;
2322 LDR_MODULE *module;
2323 NTSTATUS status;
2325 context = *orig_context;
2326 dispatch.TargetIp = 0;
2327 dispatch.ContextRecord = &context;
2328 dispatch.HistoryTable = &table;
2329 for (;;)
2331 /* FIXME: should use the history table to make things faster */
2333 dispatch.ImageBase = 0;
2334 dispatch.ControlPc = context.Rip;
2335 dispatch.ScopeIndex = 0;
2337 /* first look for PE exception information */
2339 if ((dispatch.FunctionEntry = lookup_function_info( dispatch.ControlPc, &dispatch.ImageBase, &module )))
2341 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, dispatch.ImageBase,
2342 dispatch.ControlPc, dispatch.FunctionEntry,
2343 &context, &dispatch.HandlerData,
2344 &dispatch.EstablisherFrame, NULL );
2345 goto unwind_done;
2348 /* then look for host system exception information */
2350 if (!module || (module->Flags & LDR_WINE_INTERNAL))
2352 BOOL got_info = FALSE;
2353 struct dwarf_eh_bases bases;
2354 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(dispatch.ControlPc - 1), &bases );
2356 if (fde)
2358 status = dwarf_virtual_unwind( dispatch.ControlPc, &dispatch.EstablisherFrame, &context,
2359 fde, &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
2360 if (status != STATUS_SUCCESS) return status;
2361 got_info = TRUE;
2363 #ifdef HAVE_LIBUNWIND_H
2364 else
2366 status = libunwind_virtual_unwind( dispatch.ControlPc, &got_info, &dispatch.EstablisherFrame, &context,
2367 &dispatch.LanguageHandler, &dispatch.HandlerData );
2368 if (status != STATUS_SUCCESS) return status;
2370 #endif
2372 if (got_info)
2374 dispatch.FunctionEntry = NULL;
2375 if (dispatch.LanguageHandler && !module)
2377 FIXME( "calling personality routine in system library not supported yet\n" );
2378 dispatch.LanguageHandler = NULL;
2380 goto unwind_done;
2383 else WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
2385 context.Rip = *(ULONG64 *)context.Rsp;
2386 context.Rsp = context.Rsp + sizeof(ULONG64);
2387 dispatch.EstablisherFrame = context.Rsp;
2388 dispatch.LanguageHandler = NULL;
2390 unwind_done:
2391 if (!dispatch.EstablisherFrame) break;
2393 if ((dispatch.EstablisherFrame & 7) ||
2394 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
2395 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
2397 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
2398 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2399 rec->ExceptionFlags |= EH_STACK_INVALID;
2400 break;
2403 if (dispatch.LanguageHandler)
2405 switch (call_handler( rec, orig_context, &dispatch ))
2407 case ExceptionContinueExecution:
2408 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
2409 return STATUS_SUCCESS;
2410 case ExceptionContinueSearch:
2411 break;
2412 case ExceptionNestedException:
2413 FIXME( "nested exception\n" );
2414 break;
2415 case ExceptionCollidedUnwind: {
2416 ULONG64 frame;
2418 context = *dispatch.ContextRecord;
2419 dispatch.ContextRecord = &context;
2420 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
2421 dispatch.ControlPc, dispatch.FunctionEntry,
2422 &context, NULL, &frame, NULL );
2423 goto unwind_done;
2425 default:
2426 return STATUS_INVALID_DISPOSITION;
2429 /* hack: call wine handlers registered in the tib list */
2430 else while ((ULONG64)teb_frame < context.Rsp)
2432 TRACE( "found wine frame %p rsp %lx handler %p\n",
2433 teb_frame, context.Rsp, teb_frame->Handler );
2434 dispatch.EstablisherFrame = (ULONG64)teb_frame;
2435 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
2437 case ExceptionContinueExecution:
2438 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
2439 return STATUS_SUCCESS;
2440 case ExceptionContinueSearch:
2441 break;
2442 case ExceptionNestedException:
2443 FIXME( "nested exception\n" );
2444 break;
2445 case ExceptionCollidedUnwind: {
2446 ULONG64 frame;
2448 context = *dispatch.ContextRecord;
2449 dispatch.ContextRecord = &context;
2450 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
2451 dispatch.ControlPc, dispatch.FunctionEntry,
2452 &context, NULL, &frame, NULL );
2453 teb_frame = teb_frame->Prev;
2454 goto unwind_done;
2456 default:
2457 return STATUS_INVALID_DISPOSITION;
2459 teb_frame = teb_frame->Prev;
2462 if (context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
2464 return STATUS_UNHANDLED_EXCEPTION;
2468 /*******************************************************************
2469 * raise_exception
2471 * Implementation of NtRaiseException.
2473 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2475 NTSTATUS status;
2477 if (first_chance)
2479 DWORD c;
2481 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2482 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
2483 context->Rip, GetCurrentThreadId() );
2484 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
2485 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
2486 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
2488 if (rec->ExceptionInformation[1] >> 16)
2489 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2490 rec->ExceptionAddress,
2491 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
2492 else
2493 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2494 rec->ExceptionAddress,
2495 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
2497 else
2499 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2500 context->Rax, context->Rbx, context->Rcx, context->Rdx );
2501 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2502 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
2503 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2504 context->R8, context->R9, context->R10, context->R11 );
2505 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2506 context->R12, context->R13, context->R14, context->R15 );
2508 status = send_debug_event( rec, TRUE, context );
2509 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
2510 return STATUS_SUCCESS;
2512 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
2513 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Rip--;
2515 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
2516 return STATUS_SUCCESS;
2518 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
2519 return status;
2522 /* last chance exception */
2524 status = send_debug_event( rec, FALSE, context );
2525 if (status != DBG_CONTINUE)
2527 if (rec->ExceptionFlags & EH_STACK_INVALID)
2528 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2529 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
2530 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2531 else
2532 ERR("Unhandled exception code %x flags %x addr %p\n",
2533 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
2534 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
2536 return STATUS_SUCCESS;
2540 /**********************************************************************
2541 * raise_segv_exception
2543 static void raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2545 NTSTATUS status;
2547 switch(rec->ExceptionCode)
2549 case EXCEPTION_ACCESS_VIOLATION:
2550 if (rec->NumberParameters == 2)
2552 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
2553 rec->ExceptionInformation[0], FALSE )))
2554 set_cpu_context( context );
2556 break;
2557 case EXCEPTION_BREAKPOINT:
2558 switch (rec->ExceptionInformation[0])
2560 case 1: /* BREAKPOINT_PRINT */
2561 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2562 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2563 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2564 goto done;
2566 break;
2568 status = raise_exception( rec, context, TRUE );
2569 if (status) raise_status( status, rec );
2570 done:
2571 set_cpu_context( context );
2575 /**********************************************************************
2576 * raise_generic_exception
2578 * Generic raise function for exceptions that don't need special treatment.
2580 static void raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2582 NTSTATUS status = raise_exception( rec, context, TRUE );
2583 if (status) raise_status( status, rec );
2584 set_cpu_context( context );
2588 /***********************************************************************
2589 * handle_interrupt
2591 * Handle an interrupt.
2593 static inline BOOL handle_interrupt( unsigned int interrupt, EXCEPTION_RECORD *rec, CONTEXT *context )
2595 switch(interrupt)
2597 case 0x2d:
2598 context->Rip += 3;
2599 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2600 rec->ExceptionAddress = (void *)context->Rip;
2601 rec->NumberParameters = 1;
2602 rec->ExceptionInformation[0] = context->Rax;
2603 return TRUE;
2604 default:
2605 return FALSE;
2610 /**********************************************************************
2611 * segv_handler
2613 * Handler for SIGSEGV and related errors.
2615 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2617 EXCEPTION_RECORD *rec;
2618 ucontext_t *ucontext = sigcontext;
2620 /* check for page fault inside the thread stack */
2621 if (TRAP_sig(ucontext) == TRAP_x86_PAGEFLT &&
2622 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
2623 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
2624 virtual_handle_stack_fault( siginfo->si_addr ))
2626 /* check if this was the last guard page */
2627 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
2629 rec = setup_exception( sigcontext, raise_segv_exception );
2630 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2632 return;
2635 rec = setup_exception( sigcontext, raise_segv_exception );
2636 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
2638 switch(TRAP_sig(ucontext))
2640 case TRAP_x86_OFLOW: /* Overflow exception */
2641 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2642 break;
2643 case TRAP_x86_BOUND: /* Bound range exception */
2644 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2645 break;
2646 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2647 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2648 break;
2649 case TRAP_x86_STKFLT: /* Stack fault */
2650 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2651 break;
2652 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2653 case TRAP_x86_PROTFLT: /* General protection fault */
2654 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2656 CONTEXT *win_context = get_exception_context( rec );
2657 WORD err = ERROR_sig(ucontext);
2658 if ((err & 7) == 2 && handle_interrupt( err >> 3, rec, win_context )) break;
2659 rec->ExceptionCode = err ? EXCEPTION_ACCESS_VIOLATION : EXCEPTION_PRIV_INSTRUCTION;
2660 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2662 break;
2663 case TRAP_x86_PAGEFLT: /* Page fault */
2664 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2665 rec->NumberParameters = 2;
2666 rec->ExceptionInformation[0] = (ERROR_sig(ucontext) & 2) != 0;
2667 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2668 break;
2669 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2670 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2671 break;
2672 default:
2673 ERR( "Got unexpected trap %ld\n", (ULONG_PTR)TRAP_sig(ucontext) );
2674 /* fall through */
2675 case TRAP_x86_NMI: /* NMI interrupt */
2676 case TRAP_x86_DNA: /* Device not available exception */
2677 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2678 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2679 case TRAP_x86_MCHK: /* Machine check exception */
2680 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2681 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2682 break;
2686 /**********************************************************************
2687 * trap_handler
2689 * Handler for SIGTRAP.
2691 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2693 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2695 switch (siginfo->si_code)
2697 case TRAP_TRACE: /* Single-step exception */
2698 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2699 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2700 break;
2701 case TRAP_BRKPT: /* Breakpoint exception */
2702 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2703 /* fall through */
2704 default:
2705 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2706 rec->NumberParameters = 1;
2707 rec->ExceptionInformation[0] = 0;
2708 break;
2712 /**********************************************************************
2713 * fpe_handler
2715 * Handler for SIGFPE.
2717 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2719 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2721 switch (siginfo->si_code)
2723 case FPE_FLTSUB:
2724 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2725 break;
2726 case FPE_INTDIV:
2727 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2728 break;
2729 case FPE_INTOVF:
2730 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2731 break;
2732 case FPE_FLTDIV:
2733 rec->ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
2734 break;
2735 case FPE_FLTOVF:
2736 rec->ExceptionCode = EXCEPTION_FLT_OVERFLOW;
2737 break;
2738 case FPE_FLTUND:
2739 rec->ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
2740 break;
2741 case FPE_FLTRES:
2742 rec->ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
2743 break;
2744 case FPE_FLTINV:
2745 default:
2746 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2747 break;
2751 /**********************************************************************
2752 * int_handler
2754 * Handler for SIGINT.
2756 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2758 if (!dispatch_signal(SIGINT))
2760 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2761 rec->ExceptionCode = CONTROL_C_EXIT;
2766 /**********************************************************************
2767 * abrt_handler
2769 * Handler for SIGABRT.
2771 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2773 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2774 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2775 rec->ExceptionFlags = EH_NONCONTINUABLE;
2779 /**********************************************************************
2780 * quit_handler
2782 * Handler for SIGQUIT.
2784 static void quit_handler( int signal, siginfo_t *siginfo, void *ucontext )
2786 abort_thread(0);
2790 /**********************************************************************
2791 * usr1_handler
2793 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2795 static void usr1_handler( int signal, siginfo_t *siginfo, void *ucontext )
2797 CONTEXT context;
2799 save_context( &context, ucontext );
2800 wait_suspend( &context );
2801 restore_context( &context, ucontext );
2805 /***********************************************************************
2806 * __wine_set_signal_handler (NTDLL.@)
2808 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2810 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2811 if (handlers[sig] != NULL) return -2;
2812 handlers[sig] = wsh;
2813 return 0;
2817 /**********************************************************************
2818 * signal_alloc_thread
2820 NTSTATUS signal_alloc_thread( TEB **teb )
2822 static size_t sigstack_zero_bits;
2823 SIZE_T size;
2824 NTSTATUS status;
2826 if (!sigstack_zero_bits)
2828 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2829 /* find the first power of two not smaller than min_size */
2830 sigstack_zero_bits = 12;
2831 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2832 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2833 assert( sizeof(TEB) <= teb_size );
2836 size = 1 << sigstack_zero_bits;
2837 *teb = NULL;
2838 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
2839 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2841 (*teb)->Tib.Self = &(*teb)->Tib;
2842 (*teb)->Tib.ExceptionList = (void *)~0UL;
2844 return status;
2848 /**********************************************************************
2849 * signal_free_thread
2851 void signal_free_thread( TEB *teb )
2853 SIZE_T size;
2855 if (teb->DeallocationStack)
2857 size = 0;
2858 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2860 size = 0;
2861 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2864 #ifdef __APPLE__
2865 /**********************************************************************
2866 * mac_thread_gsbase
2868 static void *mac_thread_gsbase(void)
2870 static int gsbase_offset = -1;
2871 void *ret;
2873 if (gsbase_offset < 0)
2875 /* Search for the array of TLS slots within the pthread data structure.
2876 That's what the macOS pthread implementation uses for gsbase. */
2877 const void* const sentinel1 = (const void*)0x2bffb6b4f11228ae;
2878 const void* const sentinel2 = (const void*)0x0845a7ff6ab76707;
2879 int rc;
2880 pthread_key_t key;
2881 const void** p = (const void**)pthread_self();
2882 int i;
2884 gsbase_offset = 0;
2885 if ((rc = pthread_key_create(&key, NULL)))
2887 ERR("failed to create sentinel key for gsbase search: %d\n", rc);
2888 return NULL;
2891 pthread_setspecific(key, sentinel1);
2893 for (i = key + 1; i < 2000; i++) /* arbitrary limit */
2895 if (p[i] == sentinel1)
2897 pthread_setspecific(key, sentinel2);
2899 if (p[i] == sentinel2)
2901 gsbase_offset = (i - key) * sizeof(*p);
2902 break;
2905 pthread_setspecific(key, sentinel1);
2909 pthread_key_delete(key);
2912 if (gsbase_offset)
2914 ret = (char*)pthread_self() + gsbase_offset;
2915 TRACE("pthread_self() %p + offset 0x%08x -> gsbase %p\n", pthread_self(), gsbase_offset, ret);
2917 else
2919 ret = NULL;
2920 ERR("failed to locate gsbase; won't be able to poke ThreadLocalStoragePointer into pthread TLS; expect crashes\n");
2923 return ret;
2925 #endif
2927 /**********************************************************************
2928 * signal_init_thread
2930 void signal_init_thread( TEB *teb )
2932 const WORD fpu_cw = 0x27f;
2933 stack_t ss;
2935 #if defined __linux__
2936 arch_prctl( ARCH_SET_GS, teb );
2937 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
2938 amd64_set_gsbase( teb );
2939 #elif defined(__NetBSD__)
2940 sysarch( X86_64_SET_GSBASE, &teb );
2941 #elif defined (__APPLE__)
2942 __asm__ volatile (".byte 0x65\n\tmovq %0,%c1"
2944 : "r" (teb->Tib.Self), "n" (FIELD_OFFSET(TEB, Tib.Self)));
2945 __asm__ volatile (".byte 0x65\n\tmovq %0,%c1"
2947 : "r" (teb->ThreadLocalStoragePointer), "n" (FIELD_OFFSET(TEB, ThreadLocalStoragePointer)));
2949 /* alloc_tls_slot() needs to poke a value to an address relative to each
2950 thread's gsbase. Have each thread record its gsbase pointer into its
2951 TEB so alloc_tls_slot() can find it. */
2952 teb->Reserved5[0] = mac_thread_gsbase();
2953 #else
2954 # error Please define setting %gs for your architecture
2955 #endif
2957 ss.ss_sp = (char *)teb + teb_size;
2958 ss.ss_size = signal_stack_size;
2959 ss.ss_flags = 0;
2960 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2962 #ifdef __GNUC__
2963 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2964 #else
2965 FIXME("FPU setup not implemented for this platform.\n");
2966 #endif
2969 /**********************************************************************
2970 * signal_init_process
2972 void signal_init_process(void)
2974 struct sigaction sig_act;
2976 sig_act.sa_mask = server_block_set;
2977 sig_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
2979 sig_act.sa_sigaction = int_handler;
2980 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2981 sig_act.sa_sigaction = fpe_handler;
2982 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2983 sig_act.sa_sigaction = abrt_handler;
2984 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2985 sig_act.sa_sigaction = quit_handler;
2986 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2987 sig_act.sa_sigaction = usr1_handler;
2988 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2990 sig_act.sa_sigaction = segv_handler;
2991 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2992 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2993 #ifdef SIGBUS
2994 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2995 #endif
2997 #ifdef SIGTRAP
2998 sig_act.sa_sigaction = trap_handler;
2999 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
3000 #endif
3001 return;
3003 error:
3004 perror("sigaction");
3005 exit(1);
3009 /**********************************************************************
3010 * RtlAddFunctionTable (NTDLL.@)
3012 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD64 addr )
3014 struct dynamic_unwind_entry *entry;
3016 TRACE( "%p %u %lx\n", table, count, addr );
3018 /* NOTE: Windows doesn't check if table is aligned or a NULL pointer */
3020 entry = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry) );
3021 if (!entry)
3022 return FALSE;
3024 entry->base = addr;
3025 entry->size = table[count - 1].EndAddress;
3026 entry->table = table;
3027 entry->table_size = count * sizeof(RUNTIME_FUNCTION);
3028 entry->callback = NULL;
3029 entry->context = NULL;
3031 RtlEnterCriticalSection( &dynamic_unwind_section );
3032 list_add_tail( &dynamic_unwind_list, &entry->entry );
3033 RtlLeaveCriticalSection( &dynamic_unwind_section );
3035 return TRUE;
3039 /**********************************************************************
3040 * RtlInstallFunctionTableCallback (NTDLL.@)
3042 BOOLEAN CDECL RtlInstallFunctionTableCallback( DWORD64 table, DWORD64 base, DWORD length,
3043 PGET_RUNTIME_FUNCTION_CALLBACK callback, PVOID context, PCWSTR dll )
3045 struct dynamic_unwind_entry *entry;
3047 TRACE( "%lx %lx %d %p %p %s\n", table, base, length, callback, context, wine_dbgstr_w(dll) );
3049 /* NOTE: Windows doesn't check if the provided callback is a NULL pointer */
3051 /* both low-order bits must be set */
3052 if ((table & 0x3) != 0x3)
3053 return FALSE;
3055 entry = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry) );
3056 if (!entry)
3057 return FALSE;
3059 entry->base = base;
3060 entry->size = length;
3061 entry->table = (RUNTIME_FUNCTION *)table;
3062 entry->table_size = 0;
3063 entry->callback = callback;
3064 entry->context = context;
3066 RtlEnterCriticalSection( &dynamic_unwind_section );
3067 list_add_tail( &dynamic_unwind_list, &entry->entry );
3068 RtlLeaveCriticalSection( &dynamic_unwind_section );
3070 return TRUE;
3074 /**********************************************************************
3075 * RtlDeleteFunctionTable (NTDLL.@)
3077 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
3079 struct dynamic_unwind_entry *entry, *to_free = NULL;
3081 TRACE( "%p\n", table );
3083 RtlEnterCriticalSection( &dynamic_unwind_section );
3084 LIST_FOR_EACH_ENTRY( entry, &dynamic_unwind_list, struct dynamic_unwind_entry, entry )
3086 if (entry->table == table)
3088 to_free = entry;
3089 list_remove( &entry->entry );
3090 break;
3093 RtlLeaveCriticalSection( &dynamic_unwind_section );
3095 if (!to_free)
3096 return FALSE;
3098 RtlFreeHeap( GetProcessHeap(), 0, to_free );
3099 return TRUE;
3103 /**********************************************************************
3104 * RtlLookupFunctionEntry (NTDLL.@)
3106 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base, UNWIND_HISTORY_TABLE *table )
3108 LDR_MODULE *module;
3109 RUNTIME_FUNCTION *func;
3111 /* FIXME: should use the history table to make things faster */
3113 func = lookup_function_info( pc, base, &module );
3114 if (!func)
3116 *base = 0;
3117 if (module)
3118 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
3119 else
3120 WARN( "module not found for %lx\n", pc );
3123 return func;
3126 static ULONG64 get_int_reg( CONTEXT *context, int reg )
3128 return *(&context->Rax + reg);
3131 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 *val )
3133 *(&context->Rax + reg) = *val;
3134 if (ctx_ptr) ctx_ptr->u2.IntegerContext[reg] = val;
3137 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A *val )
3139 *(&context->u.s.Xmm0 + reg) = *val;
3140 if (ctx_ptr) ctx_ptr->u.FloatingContext[reg] = val;
3143 static int get_opcode_size( struct opcode op )
3145 switch (op.code)
3147 case UWOP_ALLOC_LARGE:
3148 return 2 + (op.info != 0);
3149 case UWOP_SAVE_NONVOL:
3150 case UWOP_SAVE_XMM128:
3151 return 2;
3152 case UWOP_SAVE_NONVOL_FAR:
3153 case UWOP_SAVE_XMM128_FAR:
3154 return 3;
3155 default:
3156 return 1;
3160 static BOOL is_inside_epilog( BYTE *pc, ULONG64 base, const RUNTIME_FUNCTION *function )
3162 /* add or lea must be the first instruction, and it must have a rex.W prefix */
3163 if ((pc[0] & 0xf8) == 0x48)
3165 switch (pc[1])
3167 case 0x81: /* add $nnnn,%rsp */
3168 if (pc[0] == 0x48 && pc[2] == 0xc4)
3170 pc += 7;
3171 break;
3173 return FALSE;
3174 case 0x83: /* add $n,%rsp */
3175 if (pc[0] == 0x48 && pc[2] == 0xc4)
3177 pc += 4;
3178 break;
3180 return FALSE;
3181 case 0x8d: /* lea n(reg),%rsp */
3182 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
3183 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
3184 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
3185 if ((pc[2] >> 6) == 1) /* 8-bit offset */
3187 pc += 4;
3188 break;
3190 if ((pc[2] >> 6) == 2) /* 32-bit offset */
3192 pc += 7;
3193 break;
3195 return FALSE;
3199 /* now check for various pop instructions */
3201 for (;;)
3203 if ((*pc & 0xf0) == 0x40) pc++; /* rex prefix */
3205 switch (*pc)
3207 case 0x58: /* pop %rax/%r8 */
3208 case 0x59: /* pop %rcx/%r9 */
3209 case 0x5a: /* pop %rdx/%r10 */
3210 case 0x5b: /* pop %rbx/%r11 */
3211 case 0x5c: /* pop %rsp/%r12 */
3212 case 0x5d: /* pop %rbp/%r13 */
3213 case 0x5e: /* pop %rsi/%r14 */
3214 case 0x5f: /* pop %rdi/%r15 */
3215 pc++;
3216 continue;
3217 case 0xc2: /* ret $nn */
3218 case 0xc3: /* ret */
3219 return TRUE;
3220 case 0xe9: /* jmp nnnn */
3221 pc += 5 + *(LONG *)(pc + 1);
3222 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
3223 continue;
3224 break;
3225 case 0xeb: /* jmp n */
3226 pc += 2 + (signed char)pc[1];
3227 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
3228 continue;
3229 break;
3230 case 0xf3: /* rep; ret (for amd64 prediction bug) */
3231 return pc[1] == 0xc3;
3233 return FALSE;
3237 /* execute a function epilog, which must have been validated with is_inside_epilog() */
3238 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
3240 for (;;)
3242 BYTE rex = 0;
3244 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
3246 switch (*pc)
3248 case 0x58: /* pop %rax/r8 */
3249 case 0x59: /* pop %rcx/r9 */
3250 case 0x5a: /* pop %rdx/r10 */
3251 case 0x5b: /* pop %rbx/r11 */
3252 case 0x5c: /* pop %rsp/r12 */
3253 case 0x5d: /* pop %rbp/r13 */
3254 case 0x5e: /* pop %rsi/r14 */
3255 case 0x5f: /* pop %rdi/r15 */
3256 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, (ULONG64 *)context->Rsp );
3257 context->Rsp += sizeof(ULONG64);
3258 pc++;
3259 continue;
3260 case 0x81: /* add $nnnn,%rsp */
3261 context->Rsp += *(LONG *)(pc + 2);
3262 pc += 2 + sizeof(LONG);
3263 continue;
3264 case 0x83: /* add $n,%rsp */
3265 context->Rsp += (signed char)pc[2];
3266 pc += 3;
3267 continue;
3268 case 0x8d:
3269 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
3271 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
3272 pc += 3;
3274 else /* lea nnnn(reg),%rsp */
3276 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
3277 pc += 2 + sizeof(LONG);
3279 continue;
3280 case 0xc2: /* ret $nn */
3281 context->Rip = *(ULONG64 *)context->Rsp;
3282 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
3283 return;
3284 case 0xc3: /* ret */
3285 case 0xf3: /* rep; ret */
3286 context->Rip = *(ULONG64 *)context->Rsp;
3287 context->Rsp += sizeof(ULONG64);
3288 return;
3289 case 0xe9: /* jmp nnnn */
3290 pc += 5 + *(LONG *)(pc + 1);
3291 continue;
3292 case 0xeb: /* jmp n */
3293 pc += 2 + (signed char)pc[1];
3294 continue;
3296 return;
3300 /**********************************************************************
3301 * RtlVirtualUnwind (NTDLL.@)
3303 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
3304 RUNTIME_FUNCTION *function, CONTEXT *context,
3305 PVOID *data, ULONG64 *frame_ret,
3306 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
3308 union handler_data *handler_data;
3309 ULONG64 frame, off;
3310 struct UNWIND_INFO *info;
3311 unsigned int i, prolog_offset;
3313 TRACE( "type %x rip %lx rsp %lx\n", type, pc, context->Rsp );
3314 if (TRACE_ON(seh)) dump_unwind_info( base, function );
3316 frame = *frame_ret = context->Rsp;
3317 for (;;)
3319 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
3320 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
3322 if (info->version != 1)
3324 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
3325 return NULL;
3328 if (info->frame_reg)
3329 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
3331 /* check if in prolog */
3332 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
3334 prolog_offset = pc - base - function->BeginAddress;
3336 else
3338 prolog_offset = ~0;
3339 if (is_inside_epilog( (BYTE *)pc, base, function ))
3341 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
3342 *frame_ret = frame;
3343 return NULL;
3347 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
3349 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
3351 switch (info->opcodes[i].code)
3353 case UWOP_PUSH_NONVOL: /* pushq %reg */
3354 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)context->Rsp );
3355 context->Rsp += sizeof(ULONG64);
3356 break;
3357 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
3358 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
3359 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
3360 break;
3361 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
3362 context->Rsp += (info->opcodes[i].info + 1) * 8;
3363 break;
3364 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
3365 context->Rsp = *frame_ret = frame;
3366 break;
3367 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
3368 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
3369 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)off );
3370 break;
3371 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
3372 off = frame + *(DWORD *)&info->opcodes[i+1];
3373 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)off );
3374 break;
3375 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
3376 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
3377 set_float_reg( context, ctx_ptr, info->opcodes[i].info, (M128A *)off );
3378 break;
3379 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
3380 off = frame + *(DWORD *)&info->opcodes[i+1];
3381 set_float_reg( context, ctx_ptr, info->opcodes[i].info, (M128A *)off );
3382 break;
3383 case UWOP_PUSH_MACHFRAME:
3384 FIXME( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
3385 break;
3386 default:
3387 FIXME( "unknown code %u\n", info->opcodes[i].code );
3388 break;
3392 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
3393 function = &handler_data->chain; /* restart with the chained info */
3396 /* now pop return address */
3397 context->Rip = *(ULONG64 *)context->Rsp;
3398 context->Rsp += sizeof(ULONG64);
3400 if (!(info->flags & type)) return NULL; /* no matching handler */
3401 if (prolog_offset != ~0) return NULL; /* inside prolog */
3403 *data = &handler_data->handler + 1;
3404 return (char *)base + handler_data->handler;
3407 struct unwind_exception_frame
3409 EXCEPTION_REGISTRATION_RECORD frame;
3410 DISPATCHER_CONTEXT *dispatch;
3413 /**********************************************************************
3414 * unwind_exception_handler
3416 * Handler for exceptions happening while calling an unwind handler.
3418 static DWORD unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
3419 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
3421 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
3422 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
3424 /* copy the original dispatcher into the current one, except for the TargetIp */
3425 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
3426 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
3427 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
3428 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
3429 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
3430 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
3431 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
3432 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
3433 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
3434 TRACE( "detected collided unwind\n" );
3435 return ExceptionCollidedUnwind;
3438 /**********************************************************************
3439 * call_unwind_handler
3441 * Call a single unwind handler.
3443 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
3445 struct unwind_exception_frame frame;
3446 DWORD res;
3448 frame.frame.Handler = unwind_exception_handler;
3449 frame.dispatch = dispatch;
3450 __wine_push_frame( &frame.frame );
3452 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
3453 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
3454 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
3455 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
3457 __wine_pop_frame( &frame.frame );
3459 switch (res)
3461 case ExceptionContinueSearch:
3462 case ExceptionCollidedUnwind:
3463 break;
3464 default:
3465 raise_status( STATUS_INVALID_DISPOSITION, rec );
3466 break;
3469 return res;
3473 /**********************************************************************
3474 * call_teb_unwind_handler
3476 * Call a single unwind handler from the TEB chain.
3478 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
3479 EXCEPTION_REGISTRATION_RECORD *teb_frame )
3481 DWORD res;
3483 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
3484 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
3485 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
3486 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
3488 switch (res)
3490 case ExceptionContinueSearch:
3491 case ExceptionCollidedUnwind:
3492 break;
3493 default:
3494 raise_status( STATUS_INVALID_DISPOSITION, rec );
3495 break;
3498 return res;
3502 /**********************************************************************
3503 * call_consolidate_callback
3505 * Wrapper function to call a consolidate callback from a fake frame.
3506 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
3507 * we have to skip all frames which were already processed. To do that we
3508 * trick the unwinding functions into thinking the call came from somewhere
3509 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
3510 * DW_CFA_expression, and the expressions have the following format:
3512 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
3513 * DW_OP_deref | Get *(%rbp + 0x10) == context
3514 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
3515 * [DW_OP_deref] | Dereference, only for CFA
3517 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
3518 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
3519 EXCEPTION_RECORD *rec );
3520 __ASM_GLOBAL_FUNC( call_consolidate_callback,
3521 "pushq %rbp\n\t"
3522 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
3523 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
3524 "movq %rsp,%rbp\n\t"
3525 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
3526 "subq $0x20,%rsp\n\t"
3527 "movq %rcx,0x10(%rbp)\n\t"
3528 __ASM_CFI(".cfi_remember_state\n\t")
3529 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
3530 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
3531 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
3532 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
3533 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
3534 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
3535 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
3536 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
3537 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
3538 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
3539 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
3540 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
3541 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
3542 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
3543 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
3544 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
3545 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
3546 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
3547 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
3548 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
3549 "movq %r8,%rcx\n\t"
3550 "callq *%rdx\n\t"
3551 __ASM_CFI(".cfi_restore_state\n\t")
3552 "movq %rbp,%rsp\n\t"
3553 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
3554 "popq %rbp\n\t"
3555 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
3556 __ASM_CFI(".cfi_same_value %rbp\n\t")
3557 "ret")
3559 /*******************************************************************
3560 * RtlRestoreContext (NTDLL.@)
3562 void WINAPI RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
3564 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
3566 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
3568 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
3569 context->Rbx = jmp->Rbx;
3570 context->Rsp = jmp->Rsp;
3571 context->Rbp = jmp->Rbp;
3572 context->Rsi = jmp->Rsi;
3573 context->Rdi = jmp->Rdi;
3574 context->R12 = jmp->R12;
3575 context->R13 = jmp->R13;
3576 context->R14 = jmp->R14;
3577 context->R15 = jmp->R15;
3578 context->Rip = jmp->Rip;
3579 context->u.s.Xmm6 = jmp->Xmm6;
3580 context->u.s.Xmm7 = jmp->Xmm7;
3581 context->u.s.Xmm8 = jmp->Xmm8;
3582 context->u.s.Xmm9 = jmp->Xmm9;
3583 context->u.s.Xmm10 = jmp->Xmm10;
3584 context->u.s.Xmm11 = jmp->Xmm11;
3585 context->u.s.Xmm12 = jmp->Xmm12;
3586 context->u.s.Xmm13 = jmp->Xmm13;
3587 context->u.s.Xmm14 = jmp->Xmm14;
3588 context->u.s.Xmm15 = jmp->Xmm15;
3590 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
3592 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
3593 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
3594 context->Rip = (ULONG64)call_consolidate_callback( context, consolidate, rec );
3597 /* hack: remove no longer accessible TEB frames */
3598 while ((ULONG64)teb_frame < context->Rsp)
3600 TRACE( "removing TEB frame: %p\n", teb_frame );
3601 teb_frame = __wine_pop_frame( teb_frame );
3604 TRACE( "returning to %lx stack %lx\n", context->Rip, context->Rsp );
3605 set_cpu_context( context );
3609 /*******************************************************************
3610 * RtlUnwindEx (NTDLL.@)
3612 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
3613 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
3615 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
3616 EXCEPTION_RECORD record;
3617 DISPATCHER_CONTEXT dispatch;
3618 CONTEXT new_context;
3619 LDR_MODULE *module;
3620 NTSTATUS status;
3621 DWORD i;
3623 RtlCaptureContext( context );
3624 new_context = *context;
3626 /* build an exception record, if we do not have one */
3627 if (!rec)
3629 record.ExceptionCode = STATUS_UNWIND;
3630 record.ExceptionFlags = 0;
3631 record.ExceptionRecord = NULL;
3632 record.ExceptionAddress = (void *)context->Rip;
3633 record.NumberParameters = 0;
3634 rec = &record;
3637 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
3639 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
3640 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Rip );
3641 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
3642 TRACE( " info[%d]=%016lx\n", i, rec->ExceptionInformation[i] );
3643 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
3644 context->Rax, context->Rbx, context->Rcx, context->Rdx );
3645 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
3646 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
3647 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
3648 context->R8, context->R9, context->R10, context->R11 );
3649 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
3650 context->R12, context->R13, context->R14, context->R15 );
3652 dispatch.EstablisherFrame = context->Rsp;
3653 dispatch.TargetIp = (ULONG64)target_ip;
3654 dispatch.ContextRecord = context;
3655 dispatch.HistoryTable = table;
3657 for (;;)
3659 /* FIXME: should use the history table to make things faster */
3661 dispatch.ImageBase = 0;
3662 dispatch.ScopeIndex = 0;
3663 dispatch.ControlPc = context->Rip;
3665 /* first look for PE exception information */
3667 if ((dispatch.FunctionEntry = lookup_function_info( context->Rip, &dispatch.ImageBase, &module )))
3669 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_UHANDLER, dispatch.ImageBase,
3670 context->Rip, dispatch.FunctionEntry,
3671 &new_context, &dispatch.HandlerData,
3672 &dispatch.EstablisherFrame, NULL );
3673 goto unwind_done;
3676 /* then look for host system exception information */
3678 if (!module || (module->Flags & LDR_WINE_INTERNAL))
3680 BOOL got_info = FALSE;
3681 struct dwarf_eh_bases bases;
3682 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context->Rip - 1), &bases );
3684 if (fde)
3686 status = dwarf_virtual_unwind( context->Rip, &dispatch.EstablisherFrame, &new_context, fde,
3687 &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
3688 if (status != STATUS_SUCCESS) raise_status( status, rec );
3689 got_info = TRUE;
3691 #ifdef HAVE_LIBUNWIND_H
3692 else
3694 status = libunwind_virtual_unwind( context->Rip, &got_info, &dispatch.EstablisherFrame, &new_context,
3695 &dispatch.LanguageHandler, &dispatch.HandlerData );
3696 if (status != STATUS_SUCCESS) raise_status( status, rec );
3698 #endif
3700 if (got_info)
3702 dispatch.FunctionEntry = NULL;
3703 if (dispatch.LanguageHandler && !module)
3705 FIXME( "calling personality routine in system library not supported yet\n" );
3706 dispatch.LanguageHandler = NULL;
3708 goto unwind_done;
3711 else WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
3713 /* no exception information, treat as a leaf function */
3715 new_context.Rip = *(ULONG64 *)context->Rsp;
3716 new_context.Rsp = context->Rsp + sizeof(ULONG64);
3717 dispatch.EstablisherFrame = new_context.Rsp;
3718 dispatch.LanguageHandler = NULL;
3720 unwind_done:
3721 if (!dispatch.EstablisherFrame) break;
3723 if (is_inside_signal_stack( (void *)dispatch.EstablisherFrame ))
3725 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch.EstablisherFrame,
3726 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size );
3727 *context = new_context;
3728 continue;
3731 if ((dispatch.EstablisherFrame & 7) ||
3732 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
3733 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
3735 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
3736 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
3737 rec->ExceptionFlags |= EH_STACK_INVALID;
3738 break;
3741 if (dispatch.LanguageHandler)
3743 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
3745 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
3746 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
3748 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
3749 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
3751 ULONG64 frame;
3753 *context = new_context = *dispatch.ContextRecord;
3754 dispatch.ContextRecord = context;
3755 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
3756 dispatch.ControlPc, dispatch.FunctionEntry,
3757 &new_context, NULL, &frame, NULL );
3758 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
3759 goto unwind_done;
3761 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
3763 else /* hack: call builtin handlers registered in the tib list */
3765 DWORD64 backup_frame = dispatch.EstablisherFrame;
3766 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < (ULONG64)end_frame)
3768 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
3769 dispatch.EstablisherFrame = (ULONG64)teb_frame;
3770 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
3772 ULONG64 frame;
3774 teb_frame = __wine_pop_frame( teb_frame );
3776 *context = new_context = *dispatch.ContextRecord;
3777 dispatch.ContextRecord = context;
3778 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
3779 dispatch.ControlPc, dispatch.FunctionEntry,
3780 &new_context, NULL, &frame, NULL );
3781 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
3782 goto unwind_done;
3784 teb_frame = __wine_pop_frame( teb_frame );
3786 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Rsp) break;
3787 dispatch.EstablisherFrame = backup_frame;
3790 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
3791 *context = new_context;
3794 context->Rax = (ULONG64)retval;
3795 context->Rip = (ULONG64)target_ip;
3796 RtlRestoreContext(context, rec);
3800 /*******************************************************************
3801 * RtlUnwind (NTDLL.@)
3803 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
3805 CONTEXT context;
3806 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
3810 /*******************************************************************
3811 * _local_unwind (NTDLL.@)
3813 void WINAPI _local_unwind( void *frame, void *target_ip )
3815 CONTEXT context;
3816 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
3819 /*******************************************************************
3820 * __C_specific_handler (NTDLL.@)
3822 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
3823 ULONG64 frame,
3824 CONTEXT *context,
3825 struct _DISPATCHER_CONTEXT *dispatch )
3827 SCOPE_TABLE *table = dispatch->HandlerData;
3828 ULONG i;
3830 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
3831 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
3833 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
3835 for (i = dispatch->ScopeIndex; i < table->Count; i++)
3837 if (dispatch->ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
3838 dispatch->ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
3840 TERMINATION_HANDLER handler;
3842 if (table->ScopeRecord[i].JumpTarget) continue;
3844 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
3845 dispatch->TargetIp >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
3846 dispatch->TargetIp < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
3848 break;
3851 handler = (TERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
3852 dispatch->ScopeIndex = i+1;
3854 TRACE( "calling __finally %p frame %lx\n", handler, frame );
3855 handler( 1, frame );
3858 return ExceptionContinueSearch;
3861 for (i = dispatch->ScopeIndex; i < table->Count; i++)
3863 if (dispatch->ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
3864 dispatch->ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
3866 if (!table->ScopeRecord[i].JumpTarget) continue;
3867 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
3869 EXCEPTION_POINTERS ptrs;
3870 PC_LANGUAGE_EXCEPTION_HANDLER filter;
3872 filter = (PC_LANGUAGE_EXCEPTION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
3873 ptrs.ExceptionRecord = rec;
3874 ptrs.ContextRecord = context;
3875 TRACE( "calling filter %p ptrs %p frame %lx\n", filter, &ptrs, frame );
3876 switch (filter( &ptrs, frame ))
3878 case EXCEPTION_EXECUTE_HANDLER:
3879 break;
3880 case EXCEPTION_CONTINUE_SEARCH:
3881 continue;
3882 case EXCEPTION_CONTINUE_EXECUTION:
3883 return ExceptionContinueExecution;
3886 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
3887 RtlUnwindEx( (void *)frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
3888 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
3891 return ExceptionContinueSearch;
3895 /*******************************************************************
3896 * NtRaiseException (NTDLL.@)
3898 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
3900 NTSTATUS status = raise_exception( rec, context, first_chance );
3901 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
3902 return status;
3906 /***********************************************************************
3907 * RtlRaiseException (NTDLL.@)
3909 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
3911 NTSTATUS status;
3913 rec->ExceptionAddress = (void *)context->Rip;
3914 status = raise_exception( rec, context, TRUE );
3915 if (status != STATUS_SUCCESS) raise_status( status, rec );
3917 __ASM_GLOBAL_FUNC( RtlRaiseException,
3918 "movq %rcx,8(%rsp)\n\t"
3919 "sub $0x4f8,%rsp\n\t"
3920 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
3921 "leaq 0x20(%rsp),%rcx\n\t"
3922 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
3923 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
3924 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
3925 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
3926 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
3927 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
3928 "movq (%rax),%rcx\n\t" /* original first parameter */
3929 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
3930 "call " __ASM_NAME("__regs_RtlRaiseException") "\n\t"
3931 "leaq 0x20(%rsp),%rdi\n\t" /* context pointer */
3932 "call " __ASM_NAME("set_cpu_context") /* does not return */ );
3935 /*************************************************************************
3936 * RtlCaptureStackBackTrace (NTDLL.@)
3938 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
3940 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
3941 return 0;
3945 /***********************************************************************
3946 * call_thread_func
3948 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
3950 ntdll_get_thread_data()->exit_frame = frame;
3951 __TRY
3953 RtlExitUserThread( entry( arg ));
3955 __EXCEPT(unhandled_exception_filter)
3957 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
3959 __ENDTRY
3960 abort(); /* should not be reached */
3963 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
3964 __ASM_GLOBAL_FUNC( call_thread_entry_point,
3965 "subq $56,%rsp\n\t"
3966 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3967 "movq %rbp,48(%rsp)\n\t"
3968 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3969 "movq %rbx,40(%rsp)\n\t"
3970 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3971 "movq %r12,32(%rsp)\n\t"
3972 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3973 "movq %r13,24(%rsp)\n\t"
3974 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3975 "movq %r14,16(%rsp)\n\t"
3976 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3977 "movq %r15,8(%rsp)\n\t"
3978 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3979 "movq %rsp,%rdx\n\t"
3980 "call " __ASM_NAME("call_thread_func") );
3982 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
3983 __ASM_GLOBAL_FUNC( call_thread_exit_func,
3984 "movq %rdx,%rsp\n\t"
3985 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3986 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3987 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3988 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3989 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3990 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3991 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3992 "call *%rsi" );
3994 /***********************************************************************
3995 * RtlExitUserThread (NTDLL.@)
3997 void WINAPI RtlExitUserThread( ULONG status )
3999 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
4000 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
4003 /***********************************************************************
4004 * abort_thread
4006 void abort_thread( int status )
4008 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
4009 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
4012 /**********************************************************************
4013 * __wine_enter_vm86 (NTDLL.@)
4015 void __wine_enter_vm86( CONTEXT *context )
4017 MESSAGE("vm86 mode not supported on this platform\n");
4020 /**********************************************************************
4021 * DbgBreakPoint (NTDLL.@)
4023 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
4025 /**********************************************************************
4026 * DbgUserBreakPoint (NTDLL.@)
4028 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
4030 #endif /* __x86_64__ */