comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / ntdll / signal_x86_64.c
blobca3e6cace45cdaffe159fc740392618da804cce2
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 );
85 typedef struct _DISPATCHER_CONTEXT
87 ULONG64 ControlPc;
88 ULONG64 ImageBase;
89 PRUNTIME_FUNCTION FunctionEntry;
90 ULONG64 EstablisherFrame;
91 ULONG64 TargetIp;
92 PCONTEXT ContextRecord;
93 PEXCEPTION_ROUTINE LanguageHandler;
94 PVOID HandlerData;
95 PUNWIND_HISTORY_TABLE HistoryTable;
96 ULONG ScopeIndex;
97 } DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;
99 typedef struct _SCOPE_TABLE
101 ULONG Count;
102 struct
104 ULONG BeginAddress;
105 ULONG EndAddress;
106 ULONG HandlerAddress;
107 ULONG JumpTarget;
108 } ScopeRecord[1];
109 } SCOPE_TABLE, *PSCOPE_TABLE;
112 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
113 struct MSVCRT_JUMP_BUFFER
115 ULONG64 Frame;
116 ULONG64 Rbx;
117 ULONG64 Rsp;
118 ULONG64 Rbp;
119 ULONG64 Rsi;
120 ULONG64 Rdi;
121 ULONG64 R12;
122 ULONG64 R13;
123 ULONG64 R14;
124 ULONG64 R15;
125 ULONG64 Rip;
126 ULONG64 Spare;
127 M128A Xmm6;
128 M128A Xmm7;
129 M128A Xmm8;
130 M128A Xmm9;
131 M128A Xmm10;
132 M128A Xmm11;
133 M128A Xmm12;
134 M128A Xmm13;
135 M128A Xmm14;
136 M128A Xmm15;
139 /***********************************************************************
140 * signal context platform-specific definitions
142 #ifdef linux
144 #include <asm/prctl.h>
145 static inline int arch_prctl( int func, void *ptr ) { return syscall( __NR_arch_prctl, func, ptr ); }
147 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
148 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
149 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
150 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
151 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
152 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
153 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
154 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
155 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
156 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
157 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
158 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
159 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
160 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
161 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
163 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
164 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
165 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
167 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
168 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
169 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
170 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
171 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
173 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
175 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
177 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
178 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
179 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
180 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
181 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
182 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
183 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
184 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
185 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
186 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
187 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
188 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
189 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
190 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
191 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
193 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
194 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
195 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
196 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
197 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
198 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
200 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
202 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
203 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
204 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
205 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
207 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
209 #elif defined(__NetBSD__)
211 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
212 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
213 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
214 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
215 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
216 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
217 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
218 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
219 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
220 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
221 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
222 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
223 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
224 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
225 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
227 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
228 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
229 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
230 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
231 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
232 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
234 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
236 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
237 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
239 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
240 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
242 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
243 #elif defined (__APPLE__)
244 static pthread_key_t teb_key;
246 #define RAX_sig(context) ((context)->uc_mcontext->__ss.__rax)
247 #define RBX_sig(context) ((context)->uc_mcontext->__ss.__rbx)
248 #define RCX_sig(context) ((context)->uc_mcontext->__ss.__rcx)
249 #define RDX_sig(context) ((context)->uc_mcontext->__ss.__rdx)
250 #define RSI_sig(context) ((context)->uc_mcontext->__ss.__rsi)
251 #define RDI_sig(context) ((context)->uc_mcontext->__ss.__rdi)
252 #define RBP_sig(context) ((context)->uc_mcontext->__ss.__rbp)
253 #define R8_sig(context) ((context)->uc_mcontext->__ss.__r8)
254 #define R9_sig(context) ((context)->uc_mcontext->__ss.__r9)
255 #define R10_sig(context) ((context)->uc_mcontext->__ss.__r10)
256 #define R11_sig(context) ((context)->uc_mcontext->__ss.__r11)
257 #define R12_sig(context) ((context)->uc_mcontext->__ss.__r12)
258 #define R13_sig(context) ((context)->uc_mcontext->__ss.__r13)
259 #define R14_sig(context) ((context)->uc_mcontext->__ss.__r14)
260 #define R15_sig(context) ((context)->uc_mcontext->__ss.__r15)
262 #define CS_sig(context) ((context)->uc_mcontext->__ss.__cs)
263 #define FS_sig(context) ((context)->uc_mcontext->__ss.__fs)
264 #define GS_sig(context) ((context)->uc_mcontext->__ss.__gs)
266 #define EFL_sig(context) ((context)->uc_mcontext->__ss.__rflags)
268 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rip))
269 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext->__ss.__rsp))
271 #define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
272 #define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
274 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)&(context)->uc_mcontext->__fs.__fpu_fcw)
276 #else
277 #error You must define the signal context functions for your platform
278 #endif
280 enum i386_trap_code
282 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
283 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
284 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
285 TRAP_x86_NMI = 2, /* NMI interrupt */
286 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
287 TRAP_x86_OFLOW = 4, /* Overflow exception */
288 TRAP_x86_BOUND = 5, /* Bound range exception */
289 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
290 TRAP_x86_DNA = 7, /* Device not available exception */
291 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
292 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
293 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
294 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
295 TRAP_x86_STKFLT = 12, /* Stack fault */
296 TRAP_x86_PROTFLT = 13, /* General protection fault */
297 TRAP_x86_PAGEFLT = 14, /* Page fault */
298 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
299 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
300 TRAP_x86_MCHK = 18, /* Machine check exception */
301 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
304 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
305 static size_t signal_stack_size;
307 typedef void (*raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
308 typedef int (*wine_signal_handler)(unsigned int sig);
310 static wine_signal_handler handlers[256];
312 /***********************************************************************
313 * Dynamic unwind table
316 struct dynamic_unwind_entry
318 struct list entry;
320 /* memory region which matches this entry */
321 DWORD64 base;
322 DWORD size;
324 /* lookup table */
325 RUNTIME_FUNCTION *table;
326 DWORD table_size;
328 /* user defined callback */
329 PGET_RUNTIME_FUNCTION_CALLBACK callback;
330 PVOID context;
333 static struct list dynamic_unwind_list = LIST_INIT(dynamic_unwind_list);
335 static RTL_CRITICAL_SECTION dynamic_unwind_section;
336 static RTL_CRITICAL_SECTION_DEBUG dynamic_unwind_debug =
338 0, 0, &dynamic_unwind_section,
339 { &dynamic_unwind_debug.ProcessLocksList, &dynamic_unwind_debug.ProcessLocksList },
340 0, 0, { (DWORD_PTR)(__FILE__ ": dynamic_unwind_section") }
342 static RTL_CRITICAL_SECTION dynamic_unwind_section = { &dynamic_unwind_debug, -1, 0, 0, 0, 0 };
344 /***********************************************************************
345 * Definitions for Win32 unwind tables
348 union handler_data
350 RUNTIME_FUNCTION chain;
351 ULONG handler;
354 struct opcode
356 BYTE offset;
357 BYTE code : 4;
358 BYTE info : 4;
361 struct UNWIND_INFO
363 BYTE version : 3;
364 BYTE flags : 5;
365 BYTE prolog;
366 BYTE count;
367 BYTE frame_reg : 4;
368 BYTE frame_offset : 4;
369 struct opcode opcodes[1]; /* info->count entries */
370 /* followed by handler_data */
373 #define UWOP_PUSH_NONVOL 0
374 #define UWOP_ALLOC_LARGE 1
375 #define UWOP_ALLOC_SMALL 2
376 #define UWOP_SET_FPREG 3
377 #define UWOP_SAVE_NONVOL 4
378 #define UWOP_SAVE_NONVOL_FAR 5
379 #define UWOP_SAVE_XMM128 8
380 #define UWOP_SAVE_XMM128_FAR 9
381 #define UWOP_PUSH_MACHFRAME 10
383 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
385 static const char * const reg_names[16] =
386 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
387 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
389 union handler_data *handler_data;
390 struct UNWIND_INFO *info;
391 unsigned int i, count;
393 TRACE( "**** func %x-%x\n", function->BeginAddress, function->EndAddress );
394 for (;;)
396 if (function->UnwindData & 1)
398 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
399 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
400 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
401 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
402 function = next;
403 continue;
405 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
407 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
408 info, info->flags, info->prolog,
409 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
411 if (info->frame_reg)
412 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
413 reg_names[info->frame_reg], info->frame_offset * 16 );
415 for (i = 0; i < info->count; i++)
417 TRACE( " 0x%x: ", info->opcodes[i].offset );
418 switch (info->opcodes[i].code)
420 case UWOP_PUSH_NONVOL:
421 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
422 break;
423 case UWOP_ALLOC_LARGE:
424 if (info->opcodes[i].info)
426 count = *(DWORD *)&info->opcodes[i+1];
427 i += 2;
429 else
431 count = *(USHORT *)&info->opcodes[i+1] * 8;
432 i++;
434 TRACE( "subq $0x%x,%%rsp\n", count );
435 break;
436 case UWOP_ALLOC_SMALL:
437 count = (info->opcodes[i].info + 1) * 8;
438 TRACE( "subq $0x%x,%%rsp\n", count );
439 break;
440 case UWOP_SET_FPREG:
441 TRACE( "leaq 0x%x(%%rsp),%s\n",
442 info->frame_offset * 16, reg_names[info->frame_reg] );
443 break;
444 case UWOP_SAVE_NONVOL:
445 count = *(USHORT *)&info->opcodes[i+1] * 8;
446 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
447 i++;
448 break;
449 case UWOP_SAVE_NONVOL_FAR:
450 count = *(DWORD *)&info->opcodes[i+1];
451 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
452 i += 2;
453 break;
454 case UWOP_SAVE_XMM128:
455 count = *(USHORT *)&info->opcodes[i+1] * 16;
456 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
457 i++;
458 break;
459 case UWOP_SAVE_XMM128_FAR:
460 count = *(DWORD *)&info->opcodes[i+1];
461 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
462 i += 2;
463 break;
464 case UWOP_PUSH_MACHFRAME:
465 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
466 break;
467 default:
468 FIXME( "unknown code %u\n", info->opcodes[i].code );
469 break;
473 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
474 if (info->flags & UNW_FLAG_CHAININFO)
476 TRACE( " chained to function %p-%p\n",
477 (char *)base + handler_data->chain.BeginAddress,
478 (char *)base + handler_data->chain.EndAddress );
479 function = &handler_data->chain;
480 continue;
482 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
483 TRACE( " handler %p data at %p\n",
484 (char *)base + handler_data->handler, &handler_data->handler + 1 );
485 break;
489 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
491 unsigned int i;
493 TRACE( "scope table at %p\n", table );
494 for (i = 0; i < table->Count; i++)
495 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
496 base + table->ScopeRecord[i].BeginAddress,
497 base + table->ScopeRecord[i].EndAddress,
498 base + table->ScopeRecord[i].HandlerAddress,
499 base + table->ScopeRecord[i].JumpTarget );
503 /***********************************************************************
504 * Definitions for Dwarf unwind tables
507 enum dwarf_call_frame_info
509 DW_CFA_advance_loc = 0x40,
510 DW_CFA_offset = 0x80,
511 DW_CFA_restore = 0xc0,
512 DW_CFA_nop = 0x00,
513 DW_CFA_set_loc = 0x01,
514 DW_CFA_advance_loc1 = 0x02,
515 DW_CFA_advance_loc2 = 0x03,
516 DW_CFA_advance_loc4 = 0x04,
517 DW_CFA_offset_extended = 0x05,
518 DW_CFA_restore_extended = 0x06,
519 DW_CFA_undefined = 0x07,
520 DW_CFA_same_value = 0x08,
521 DW_CFA_register = 0x09,
522 DW_CFA_remember_state = 0x0a,
523 DW_CFA_restore_state = 0x0b,
524 DW_CFA_def_cfa = 0x0c,
525 DW_CFA_def_cfa_register = 0x0d,
526 DW_CFA_def_cfa_offset = 0x0e,
527 DW_CFA_def_cfa_expression = 0x0f,
528 DW_CFA_expression = 0x10,
529 DW_CFA_offset_extended_sf = 0x11,
530 DW_CFA_def_cfa_sf = 0x12,
531 DW_CFA_def_cfa_offset_sf = 0x13,
532 DW_CFA_val_offset = 0x14,
533 DW_CFA_val_offset_sf = 0x15,
534 DW_CFA_val_expression = 0x16,
537 enum dwarf_operation
539 DW_OP_addr = 0x03,
540 DW_OP_deref = 0x06,
541 DW_OP_const1u = 0x08,
542 DW_OP_const1s = 0x09,
543 DW_OP_const2u = 0x0a,
544 DW_OP_const2s = 0x0b,
545 DW_OP_const4u = 0x0c,
546 DW_OP_const4s = 0x0d,
547 DW_OP_const8u = 0x0e,
548 DW_OP_const8s = 0x0f,
549 DW_OP_constu = 0x10,
550 DW_OP_consts = 0x11,
551 DW_OP_dup = 0x12,
552 DW_OP_drop = 0x13,
553 DW_OP_over = 0x14,
554 DW_OP_pick = 0x15,
555 DW_OP_swap = 0x16,
556 DW_OP_rot = 0x17,
557 DW_OP_xderef = 0x18,
558 DW_OP_abs = 0x19,
559 DW_OP_and = 0x1a,
560 DW_OP_div = 0x1b,
561 DW_OP_minus = 0x1c,
562 DW_OP_mod = 0x1d,
563 DW_OP_mul = 0x1e,
564 DW_OP_neg = 0x1f,
565 DW_OP_not = 0x20,
566 DW_OP_or = 0x21,
567 DW_OP_plus = 0x22,
568 DW_OP_plus_uconst = 0x23,
569 DW_OP_shl = 0x24,
570 DW_OP_shr = 0x25,
571 DW_OP_shra = 0x26,
572 DW_OP_xor = 0x27,
573 DW_OP_bra = 0x28,
574 DW_OP_eq = 0x29,
575 DW_OP_ge = 0x2a,
576 DW_OP_gt = 0x2b,
577 DW_OP_le = 0x2c,
578 DW_OP_lt = 0x2d,
579 DW_OP_ne = 0x2e,
580 DW_OP_skip = 0x2f,
581 DW_OP_lit0 = 0x30,
582 DW_OP_lit1 = 0x31,
583 DW_OP_lit2 = 0x32,
584 DW_OP_lit3 = 0x33,
585 DW_OP_lit4 = 0x34,
586 DW_OP_lit5 = 0x35,
587 DW_OP_lit6 = 0x36,
588 DW_OP_lit7 = 0x37,
589 DW_OP_lit8 = 0x38,
590 DW_OP_lit9 = 0x39,
591 DW_OP_lit10 = 0x3a,
592 DW_OP_lit11 = 0x3b,
593 DW_OP_lit12 = 0x3c,
594 DW_OP_lit13 = 0x3d,
595 DW_OP_lit14 = 0x3e,
596 DW_OP_lit15 = 0x3f,
597 DW_OP_lit16 = 0x40,
598 DW_OP_lit17 = 0x41,
599 DW_OP_lit18 = 0x42,
600 DW_OP_lit19 = 0x43,
601 DW_OP_lit20 = 0x44,
602 DW_OP_lit21 = 0x45,
603 DW_OP_lit22 = 0x46,
604 DW_OP_lit23 = 0x47,
605 DW_OP_lit24 = 0x48,
606 DW_OP_lit25 = 0x49,
607 DW_OP_lit26 = 0x4a,
608 DW_OP_lit27 = 0x4b,
609 DW_OP_lit28 = 0x4c,
610 DW_OP_lit29 = 0x4d,
611 DW_OP_lit30 = 0x4e,
612 DW_OP_lit31 = 0x4f,
613 DW_OP_reg0 = 0x50,
614 DW_OP_reg1 = 0x51,
615 DW_OP_reg2 = 0x52,
616 DW_OP_reg3 = 0x53,
617 DW_OP_reg4 = 0x54,
618 DW_OP_reg5 = 0x55,
619 DW_OP_reg6 = 0x56,
620 DW_OP_reg7 = 0x57,
621 DW_OP_reg8 = 0x58,
622 DW_OP_reg9 = 0x59,
623 DW_OP_reg10 = 0x5a,
624 DW_OP_reg11 = 0x5b,
625 DW_OP_reg12 = 0x5c,
626 DW_OP_reg13 = 0x5d,
627 DW_OP_reg14 = 0x5e,
628 DW_OP_reg15 = 0x5f,
629 DW_OP_reg16 = 0x60,
630 DW_OP_reg17 = 0x61,
631 DW_OP_reg18 = 0x62,
632 DW_OP_reg19 = 0x63,
633 DW_OP_reg20 = 0x64,
634 DW_OP_reg21 = 0x65,
635 DW_OP_reg22 = 0x66,
636 DW_OP_reg23 = 0x67,
637 DW_OP_reg24 = 0x68,
638 DW_OP_reg25 = 0x69,
639 DW_OP_reg26 = 0x6a,
640 DW_OP_reg27 = 0x6b,
641 DW_OP_reg28 = 0x6c,
642 DW_OP_reg29 = 0x6d,
643 DW_OP_reg30 = 0x6e,
644 DW_OP_reg31 = 0x6f,
645 DW_OP_breg0 = 0x70,
646 DW_OP_breg1 = 0x71,
647 DW_OP_breg2 = 0x72,
648 DW_OP_breg3 = 0x73,
649 DW_OP_breg4 = 0x74,
650 DW_OP_breg5 = 0x75,
651 DW_OP_breg6 = 0x76,
652 DW_OP_breg7 = 0x77,
653 DW_OP_breg8 = 0x78,
654 DW_OP_breg9 = 0x79,
655 DW_OP_breg10 = 0x7a,
656 DW_OP_breg11 = 0x7b,
657 DW_OP_breg12 = 0x7c,
658 DW_OP_breg13 = 0x7d,
659 DW_OP_breg14 = 0x7e,
660 DW_OP_breg15 = 0x7f,
661 DW_OP_breg16 = 0x80,
662 DW_OP_breg17 = 0x81,
663 DW_OP_breg18 = 0x82,
664 DW_OP_breg19 = 0x83,
665 DW_OP_breg20 = 0x84,
666 DW_OP_breg21 = 0x85,
667 DW_OP_breg22 = 0x86,
668 DW_OP_breg23 = 0x87,
669 DW_OP_breg24 = 0x88,
670 DW_OP_breg25 = 0x89,
671 DW_OP_breg26 = 0x8a,
672 DW_OP_breg27 = 0x8b,
673 DW_OP_breg28 = 0x8c,
674 DW_OP_breg29 = 0x8d,
675 DW_OP_breg30 = 0x8e,
676 DW_OP_breg31 = 0x8f,
677 DW_OP_regx = 0x90,
678 DW_OP_fbreg = 0x91,
679 DW_OP_bregx = 0x92,
680 DW_OP_piece = 0x93,
681 DW_OP_deref_size = 0x94,
682 DW_OP_xderef_size = 0x95,
683 DW_OP_nop = 0x96,
684 DW_OP_push_object_address = 0x97,
685 DW_OP_call2 = 0x98,
686 DW_OP_call4 = 0x99,
687 DW_OP_call_ref = 0x9a,
688 DW_OP_form_tls_address = 0x9b,
689 DW_OP_call_frame_cfa = 0x9c,
690 DW_OP_bit_piece = 0x9d,
691 DW_OP_lo_user = 0xe0,
692 DW_OP_hi_user = 0xff,
693 DW_OP_GNU_push_tls_address = 0xe0,
694 DW_OP_GNU_uninit = 0xf0,
695 DW_OP_GNU_encoded_addr = 0xf1,
698 #define DW_EH_PE_native 0x00
699 #define DW_EH_PE_leb128 0x01
700 #define DW_EH_PE_data2 0x02
701 #define DW_EH_PE_data4 0x03
702 #define DW_EH_PE_data8 0x04
703 #define DW_EH_PE_signed 0x08
704 #define DW_EH_PE_abs 0x00
705 #define DW_EH_PE_pcrel 0x10
706 #define DW_EH_PE_textrel 0x20
707 #define DW_EH_PE_datarel 0x30
708 #define DW_EH_PE_funcrel 0x40
709 #define DW_EH_PE_aligned 0x50
710 #define DW_EH_PE_indirect 0x80
711 #define DW_EH_PE_omit 0xff
713 struct dwarf_eh_bases
715 void *tbase;
716 void *dbase;
717 void *func;
720 struct dwarf_cie
722 unsigned int length;
723 int id;
724 unsigned char version;
725 unsigned char augmentation[1];
728 struct dwarf_fde
730 unsigned int length;
731 unsigned int cie_offset;
734 extern const struct dwarf_fde *_Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
736 static unsigned char dwarf_get_u1( const unsigned char **p )
738 return *(*p)++;
741 static unsigned short dwarf_get_u2( const unsigned char **p )
743 unsigned int ret = (*p)[0] | ((*p)[1] << 8);
744 (*p) += 2;
745 return ret;
748 static unsigned int dwarf_get_u4( const unsigned char **p )
750 unsigned int ret = (*p)[0] | ((*p)[1] << 8) | ((*p)[2] << 16) | ((*p)[3] << 24);
751 (*p) += 4;
752 return ret;
755 static ULONG64 dwarf_get_u8( const unsigned char **p )
757 ULONG64 low = dwarf_get_u4( p );
758 ULONG64 high = dwarf_get_u4( p );
759 return low | (high << 32);
762 static ULONG_PTR dwarf_get_uleb128( const unsigned char **p )
764 ULONG_PTR ret = 0;
765 unsigned int shift = 0;
766 unsigned char byte;
770 byte = **p;
771 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
772 shift += 7;
773 (*p)++;
774 } while (byte & 0x80);
775 return ret;
778 static LONG_PTR dwarf_get_sleb128( const unsigned char **p )
780 ULONG_PTR ret = 0;
781 unsigned int shift = 0;
782 unsigned char byte;
786 byte = **p;
787 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
788 shift += 7;
789 (*p)++;
790 } while (byte & 0x80);
792 if ((shift < 8 * sizeof(ret)) && (byte & 0x40)) ret |= -((ULONG_PTR)1 << shift);
793 return ret;
796 static ULONG_PTR dwarf_get_ptr( const unsigned char **p, unsigned char encoding )
798 ULONG_PTR base;
800 if (encoding == DW_EH_PE_omit) return 0;
802 switch (encoding & 0xf0)
804 case DW_EH_PE_abs:
805 base = 0;
806 break;
807 case DW_EH_PE_pcrel:
808 base = (ULONG_PTR)*p;
809 break;
810 default:
811 FIXME( "unsupported encoding %02x\n", encoding );
812 return 0;
815 switch (encoding & 0x0f)
817 case DW_EH_PE_native:
818 return base + dwarf_get_u8( p );
819 case DW_EH_PE_leb128:
820 return base + dwarf_get_uleb128( p );
821 case DW_EH_PE_data2:
822 return base + dwarf_get_u2( p );
823 case DW_EH_PE_data4:
824 return base + dwarf_get_u4( p );
825 case DW_EH_PE_data8:
826 return base + dwarf_get_u8( p );
827 case DW_EH_PE_signed|DW_EH_PE_leb128:
828 return base + dwarf_get_sleb128( p );
829 case DW_EH_PE_signed|DW_EH_PE_data2:
830 return base + (signed short)dwarf_get_u2( p );
831 case DW_EH_PE_signed|DW_EH_PE_data4:
832 return base + (signed int)dwarf_get_u4( p );
833 case DW_EH_PE_signed|DW_EH_PE_data8:
834 return base + (LONG64)dwarf_get_u8( p );
835 default:
836 FIXME( "unsupported encoding %02x\n", encoding );
837 return 0;
841 enum reg_rule
843 RULE_UNSET, /* not set at all */
844 RULE_UNDEFINED, /* undefined value */
845 RULE_SAME, /* same value as previous frame */
846 RULE_CFA_OFFSET, /* stored at cfa offset */
847 RULE_OTHER_REG, /* stored in other register */
848 RULE_EXPRESSION, /* address specified by expression */
849 RULE_VAL_EXPRESSION /* value specified by expression */
852 #define NB_FRAME_REGS 41
853 #define MAX_SAVED_STATES 16
855 struct frame_state
857 ULONG_PTR cfa_offset;
858 unsigned char cfa_reg;
859 enum reg_rule cfa_rule;
860 enum reg_rule rules[NB_FRAME_REGS];
861 ULONG64 regs[NB_FRAME_REGS];
864 struct frame_info
866 ULONG_PTR ip;
867 ULONG_PTR code_align;
868 LONG_PTR data_align;
869 unsigned char retaddr_reg;
870 unsigned char fde_encoding;
871 unsigned char signal_frame;
872 unsigned char state_sp;
873 struct frame_state state;
874 struct frame_state *state_stack;
877 static const char *dwarf_reg_names[NB_FRAME_REGS] =
879 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
880 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
881 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
882 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
883 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
886 static BOOL valid_reg( ULONG_PTR reg )
888 if (reg >= NB_FRAME_REGS) FIXME( "unsupported reg %lx\n", reg );
889 return (reg < NB_FRAME_REGS);
892 static void execute_cfa_instructions( const unsigned char *ptr, const unsigned char *end,
893 ULONG_PTR last_ip, struct frame_info *info )
895 while (ptr < end && info->ip < last_ip + info->signal_frame)
897 enum dwarf_call_frame_info op = *ptr++;
899 if (op & 0xc0)
901 switch (op & 0xc0)
903 case DW_CFA_advance_loc:
905 ULONG_PTR offset = (op & 0x3f) * info->code_align;
906 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info->ip, offset );
907 info->ip += offset;
908 break;
910 case DW_CFA_offset:
912 ULONG_PTR reg = op & 0x3f;
913 LONG_PTR offset = dwarf_get_uleb128( &ptr ) * info->data_align;
914 if (!valid_reg( reg )) break;
915 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
916 info->state.regs[reg] = offset;
917 info->state.rules[reg] = RULE_CFA_OFFSET;
918 break;
920 case DW_CFA_restore:
922 ULONG_PTR reg = op & 0x3f;
923 if (!valid_reg( reg )) break;
924 TRACE( "%lx: DW_CFA_restore %s\n", info->ip, dwarf_reg_names[reg] );
925 info->state.rules[reg] = RULE_UNSET;
926 break;
930 else switch (op)
932 case DW_CFA_nop:
933 break;
934 case DW_CFA_set_loc:
936 ULONG_PTR loc = dwarf_get_ptr( &ptr, info->fde_encoding );
937 TRACE( "%lx: DW_CFA_set_loc %lx\n", info->ip, loc );
938 info->ip = loc;
939 break;
941 case DW_CFA_advance_loc1:
943 ULONG_PTR offset = *ptr++ * info->code_align;
944 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset );
945 info->ip += offset;
946 break;
948 case DW_CFA_advance_loc2:
950 ULONG_PTR offset = dwarf_get_u2( &ptr ) * info->code_align;
951 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset );
952 info->ip += offset;
953 break;
955 case DW_CFA_advance_loc4:
957 ULONG_PTR offset = dwarf_get_u4( &ptr ) * info->code_align;
958 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset );
959 info->ip += offset;
960 break;
962 case DW_CFA_offset_extended:
963 case DW_CFA_offset_extended_sf:
965 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
966 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf_get_uleb128( &ptr ) * info->data_align
967 : dwarf_get_sleb128( &ptr ) * info->data_align;
968 if (!valid_reg( reg )) break;
969 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
970 info->state.regs[reg] = offset;
971 info->state.rules[reg] = RULE_CFA_OFFSET;
972 break;
974 case DW_CFA_restore_extended:
976 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
977 if (!valid_reg( reg )) break;
978 TRACE( "%lx: DW_CFA_restore_extended %s\n", info->ip, dwarf_reg_names[reg] );
979 info->state.rules[reg] = RULE_UNSET;
980 break;
982 case DW_CFA_undefined:
984 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
985 if (!valid_reg( reg )) break;
986 TRACE( "%lx: DW_CFA_undefined %s\n", info->ip, dwarf_reg_names[reg] );
987 info->state.rules[reg] = RULE_UNDEFINED;
988 break;
990 case DW_CFA_same_value:
992 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
993 if (!valid_reg( reg )) break;
994 TRACE( "%lx: DW_CFA_same_value %s\n", info->ip, dwarf_reg_names[reg] );
995 info->state.regs[reg] = reg;
996 info->state.rules[reg] = RULE_SAME;
997 break;
999 case DW_CFA_register:
1001 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1002 ULONG_PTR reg2 = dwarf_get_uleb128( &ptr );
1003 if (!valid_reg( reg ) || !valid_reg( reg2 )) break;
1004 TRACE( "%lx: DW_CFA_register %s == %s\n", info->ip, dwarf_reg_names[reg], dwarf_reg_names[reg2] );
1005 info->state.regs[reg] = reg2;
1006 info->state.rules[reg] = RULE_OTHER_REG;
1007 break;
1009 case DW_CFA_remember_state:
1010 TRACE( "%lx: DW_CFA_remember_state\n", info->ip );
1011 if (info->state_sp >= MAX_SAVED_STATES)
1012 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info->ip );
1013 else
1014 info->state_stack[info->state_sp++] = info->state;
1015 break;
1016 case DW_CFA_restore_state:
1017 TRACE( "%lx: DW_CFA_restore_state\n", info->ip );
1018 if (!info->state_sp)
1019 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info->ip );
1020 else
1021 info->state = info->state_stack[--info->state_sp];
1022 break;
1023 case DW_CFA_def_cfa:
1024 case DW_CFA_def_cfa_sf:
1026 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1027 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf_get_uleb128( &ptr )
1028 : dwarf_get_sleb128( &ptr ) * info->data_align;
1029 if (!valid_reg( reg )) break;
1030 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info->ip, dwarf_reg_names[reg], offset );
1031 info->state.cfa_reg = reg;
1032 info->state.cfa_offset = offset;
1033 info->state.cfa_rule = RULE_CFA_OFFSET;
1034 break;
1036 case DW_CFA_def_cfa_register:
1038 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1039 if (!valid_reg( reg )) break;
1040 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info->ip, dwarf_reg_names[reg] );
1041 info->state.cfa_reg = reg;
1042 info->state.cfa_rule = RULE_CFA_OFFSET;
1043 break;
1045 case DW_CFA_def_cfa_offset:
1046 case DW_CFA_def_cfa_offset_sf:
1048 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf_get_uleb128( &ptr )
1049 : dwarf_get_sleb128( &ptr ) * info->data_align;
1050 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset );
1051 info->state.cfa_offset = offset;
1052 info->state.cfa_rule = RULE_CFA_OFFSET;
1053 break;
1055 case DW_CFA_def_cfa_expression:
1057 ULONG_PTR expr = (ULONG_PTR)ptr;
1058 ULONG_PTR len = dwarf_get_uleb128( &ptr );
1059 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len );
1060 info->state.cfa_offset = expr;
1061 info->state.cfa_rule = RULE_VAL_EXPRESSION;
1062 ptr += len;
1063 break;
1065 case DW_CFA_expression:
1066 case DW_CFA_val_expression:
1068 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1069 ULONG_PTR expr = (ULONG_PTR)ptr;
1070 ULONG_PTR len = dwarf_get_uleb128( &ptr );
1071 if (!valid_reg( reg )) break;
1072 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
1073 info->ip, (op == DW_CFA_expression) ? "" : "val_", dwarf_reg_names[reg], expr, expr+len );
1074 info->state.regs[reg] = expr;
1075 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
1076 ptr += len;
1077 break;
1079 default:
1080 FIXME( "%lx: unknown CFA opcode %02x\n", info->ip, op );
1081 break;
1086 /* retrieve a context register from its dwarf number */
1087 static void *get_context_reg( CONTEXT *context, ULONG_PTR dw_reg )
1089 switch (dw_reg)
1091 case 0: return &context->Rax;
1092 case 1: return &context->Rdx;
1093 case 2: return &context->Rcx;
1094 case 3: return &context->Rbx;
1095 case 4: return &context->Rsi;
1096 case 5: return &context->Rdi;
1097 case 6: return &context->Rbp;
1098 case 7: return &context->Rsp;
1099 case 8: return &context->R8;
1100 case 9: return &context->R9;
1101 case 10: return &context->R10;
1102 case 11: return &context->R11;
1103 case 12: return &context->R12;
1104 case 13: return &context->R13;
1105 case 14: return &context->R14;
1106 case 15: return &context->R15;
1107 case 16: return &context->Rip;
1108 case 17: return &context->u.s.Xmm0;
1109 case 18: return &context->u.s.Xmm1;
1110 case 19: return &context->u.s.Xmm2;
1111 case 20: return &context->u.s.Xmm3;
1112 case 21: return &context->u.s.Xmm4;
1113 case 22: return &context->u.s.Xmm5;
1114 case 23: return &context->u.s.Xmm6;
1115 case 24: return &context->u.s.Xmm7;
1116 case 25: return &context->u.s.Xmm8;
1117 case 26: return &context->u.s.Xmm9;
1118 case 27: return &context->u.s.Xmm10;
1119 case 28: return &context->u.s.Xmm11;
1120 case 29: return &context->u.s.Xmm12;
1121 case 30: return &context->u.s.Xmm13;
1122 case 31: return &context->u.s.Xmm14;
1123 case 32: return &context->u.s.Xmm15;
1124 case 33: return &context->u.s.Legacy[0];
1125 case 34: return &context->u.s.Legacy[1];
1126 case 35: return &context->u.s.Legacy[2];
1127 case 36: return &context->u.s.Legacy[3];
1128 case 37: return &context->u.s.Legacy[4];
1129 case 38: return &context->u.s.Legacy[5];
1130 case 39: return &context->u.s.Legacy[6];
1131 case 40: return &context->u.s.Legacy[7];
1132 default: return NULL;
1136 /* set a context register from its dwarf number */
1137 static void set_context_reg( CONTEXT *context, ULONG_PTR dw_reg, void *val )
1139 switch (dw_reg)
1141 case 0: context->Rax = *(ULONG64 *)val; break;
1142 case 1: context->Rdx = *(ULONG64 *)val; break;
1143 case 2: context->Rcx = *(ULONG64 *)val; break;
1144 case 3: context->Rbx = *(ULONG64 *)val; break;
1145 case 4: context->Rsi = *(ULONG64 *)val; break;
1146 case 5: context->Rdi = *(ULONG64 *)val; break;
1147 case 6: context->Rbp = *(ULONG64 *)val; break;
1148 case 7: context->Rsp = *(ULONG64 *)val; break;
1149 case 8: context->R8 = *(ULONG64 *)val; break;
1150 case 9: context->R9 = *(ULONG64 *)val; break;
1151 case 10: context->R10 = *(ULONG64 *)val; break;
1152 case 11: context->R11 = *(ULONG64 *)val; break;
1153 case 12: context->R12 = *(ULONG64 *)val; break;
1154 case 13: context->R13 = *(ULONG64 *)val; break;
1155 case 14: context->R14 = *(ULONG64 *)val; break;
1156 case 15: context->R15 = *(ULONG64 *)val; break;
1157 case 16: context->Rip = *(ULONG64 *)val; break;
1158 case 17: context->u.s.Xmm0 = *(M128A *)val; break;
1159 case 18: context->u.s.Xmm1 = *(M128A *)val; break;
1160 case 19: context->u.s.Xmm2 = *(M128A *)val; break;
1161 case 20: context->u.s.Xmm3 = *(M128A *)val; break;
1162 case 21: context->u.s.Xmm4 = *(M128A *)val; break;
1163 case 22: context->u.s.Xmm5 = *(M128A *)val; break;
1164 case 23: context->u.s.Xmm6 = *(M128A *)val; break;
1165 case 24: context->u.s.Xmm7 = *(M128A *)val; break;
1166 case 25: context->u.s.Xmm8 = *(M128A *)val; break;
1167 case 26: context->u.s.Xmm9 = *(M128A *)val; break;
1168 case 27: context->u.s.Xmm10 = *(M128A *)val; break;
1169 case 28: context->u.s.Xmm11 = *(M128A *)val; break;
1170 case 29: context->u.s.Xmm12 = *(M128A *)val; break;
1171 case 30: context->u.s.Xmm13 = *(M128A *)val; break;
1172 case 31: context->u.s.Xmm14 = *(M128A *)val; break;
1173 case 32: context->u.s.Xmm15 = *(M128A *)val; break;
1174 case 33: context->u.s.Legacy[0] = *(M128A *)val; break;
1175 case 34: context->u.s.Legacy[1] = *(M128A *)val; break;
1176 case 35: context->u.s.Legacy[2] = *(M128A *)val; break;
1177 case 36: context->u.s.Legacy[3] = *(M128A *)val; break;
1178 case 37: context->u.s.Legacy[4] = *(M128A *)val; break;
1179 case 38: context->u.s.Legacy[5] = *(M128A *)val; break;
1180 case 39: context->u.s.Legacy[6] = *(M128A *)val; break;
1181 case 40: context->u.s.Legacy[7] = *(M128A *)val; break;
1185 static ULONG_PTR eval_expression( const unsigned char *p, CONTEXT *context )
1187 ULONG_PTR reg, tmp, stack[64];
1188 int sp = -1;
1189 ULONG_PTR len = dwarf_get_uleb128(&p);
1190 const unsigned char *end = p + len;
1192 while (p < end)
1194 unsigned char opcode = dwarf_get_u1(&p);
1196 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
1197 stack[++sp] = opcode - DW_OP_lit0;
1198 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
1199 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_reg0 );
1200 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
1201 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_breg0 ) + dwarf_get_sleb128(&p);
1202 else switch (opcode)
1204 case DW_OP_nop: break;
1205 case DW_OP_addr: stack[++sp] = dwarf_get_u8(&p); break;
1206 case DW_OP_const1u: stack[++sp] = dwarf_get_u1(&p); break;
1207 case DW_OP_const1s: stack[++sp] = (signed char)dwarf_get_u1(&p); break;
1208 case DW_OP_const2u: stack[++sp] = dwarf_get_u2(&p); break;
1209 case DW_OP_const2s: stack[++sp] = (short)dwarf_get_u2(&p); break;
1210 case DW_OP_const4u: stack[++sp] = dwarf_get_u4(&p); break;
1211 case DW_OP_const4s: stack[++sp] = (signed int)dwarf_get_u4(&p); break;
1212 case DW_OP_const8u: stack[++sp] = dwarf_get_u8(&p); break;
1213 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf_get_u8(&p); break;
1214 case DW_OP_constu: stack[++sp] = dwarf_get_uleb128(&p); break;
1215 case DW_OP_consts: stack[++sp] = dwarf_get_sleb128(&p); break;
1216 case DW_OP_deref: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1217 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
1218 case DW_OP_drop: sp--; break;
1219 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
1220 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf_get_u1(&p)]; sp++; break;
1221 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
1222 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
1223 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
1224 case DW_OP_neg: stack[sp] = -stack[sp]; break;
1225 case DW_OP_not: stack[sp] = ~stack[sp]; break;
1226 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
1227 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
1228 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
1229 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
1230 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
1231 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
1232 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
1233 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
1234 case DW_OP_plus_uconst: stack[sp] += dwarf_get_uleb128(&p); break;
1235 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
1236 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
1237 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
1238 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
1239 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
1240 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
1241 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
1242 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
1243 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
1244 case DW_OP_skip: tmp = (short)dwarf_get_u2(&p); p += tmp; break;
1245 case DW_OP_bra: tmp = (short)dwarf_get_u2(&p); if (!stack[sp--]) p += tmp; break;
1246 case DW_OP_GNU_encoded_addr: tmp = *p++; stack[++sp] = dwarf_get_ptr( &p, tmp ); break;
1247 case DW_OP_regx: stack[++sp] = *(ULONG_PTR *)get_context_reg( context, dwarf_get_uleb128(&p) ); break;
1248 case DW_OP_bregx:
1249 reg = dwarf_get_uleb128(&p);
1250 tmp = dwarf_get_sleb128(&p);
1251 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, reg ) + tmp;
1252 break;
1253 case DW_OP_deref_size:
1254 switch (*p++)
1256 case 1: stack[sp] = *(unsigned char *)stack[sp]; break;
1257 case 2: stack[sp] = *(unsigned short *)stack[sp]; break;
1258 case 4: stack[sp] = *(unsigned int *)stack[sp]; break;
1259 case 8: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1261 break;
1262 default:
1263 FIXME( "unhandled opcode %02x\n", opcode );
1266 return stack[sp];
1269 /* apply the computed frame info to the actual context */
1270 static void apply_frame_state( CONTEXT *context, struct frame_state *state )
1272 unsigned int i;
1273 ULONG_PTR cfa, value;
1274 CONTEXT new_context = *context;
1276 switch (state->cfa_rule)
1278 case RULE_EXPRESSION:
1279 cfa = *(ULONG_PTR *)eval_expression( (const unsigned char *)state->cfa_offset, context );
1280 break;
1281 case RULE_VAL_EXPRESSION:
1282 cfa = eval_expression( (const unsigned char *)state->cfa_offset, context );
1283 break;
1284 default:
1285 cfa = *(ULONG_PTR *)get_context_reg( context, state->cfa_reg ) + state->cfa_offset;
1286 break;
1288 if (!cfa) return;
1290 for (i = 0; i < NB_FRAME_REGS; i++)
1292 switch (state->rules[i])
1294 case RULE_UNSET:
1295 case RULE_UNDEFINED:
1296 case RULE_SAME:
1297 break;
1298 case RULE_CFA_OFFSET:
1299 set_context_reg( &new_context, i, (char *)cfa + state->regs[i] );
1300 break;
1301 case RULE_OTHER_REG:
1302 set_context_reg( &new_context, i, get_context_reg( context, state->regs[i] ));
1303 break;
1304 case RULE_EXPRESSION:
1305 value = eval_expression( (const unsigned char *)state->regs[i], context );
1306 set_context_reg( &new_context, i, (void *)value );
1307 break;
1308 case RULE_VAL_EXPRESSION:
1309 value = eval_expression( (const unsigned char *)state->regs[i], context );
1310 set_context_reg( &new_context, i, &value );
1311 break;
1314 new_context.Rsp = cfa;
1315 *context = new_context;
1319 /***********************************************************************
1320 * dwarf_virtual_unwind
1322 * Equivalent of RtlVirtualUnwind for builtin modules.
1324 static NTSTATUS dwarf_virtual_unwind( ULONG64 ip, ULONG64 *frame,CONTEXT *context,
1325 const struct dwarf_fde *fde, const struct dwarf_eh_bases *bases,
1326 PEXCEPTION_ROUTINE *handler, void **handler_data )
1328 const struct dwarf_cie *cie;
1329 const unsigned char *ptr, *augmentation, *end;
1330 ULONG_PTR len, code_end;
1331 struct frame_info info;
1332 struct frame_state state_stack[MAX_SAVED_STATES];
1333 int aug_z_format = 0;
1334 unsigned char lsda_encoding = DW_EH_PE_omit;
1336 memset( &info, 0, sizeof(info) );
1337 info.state_stack = state_stack;
1338 info.ip = (ULONG_PTR)bases->func;
1339 *handler = NULL;
1341 cie = (const struct dwarf_cie *)((const char *)&fde->cie_offset - fde->cie_offset);
1343 /* parse the CIE first */
1345 if (cie->version != 1 && cie->version != 3)
1347 FIXME( "unknown CIE version %u at %p\n", cie->version, cie );
1348 return STATUS_INVALID_DISPOSITION;
1350 ptr = cie->augmentation + strlen((const char *)cie->augmentation) + 1;
1352 info.code_align = dwarf_get_uleb128( &ptr );
1353 info.data_align = dwarf_get_sleb128( &ptr );
1354 if (cie->version == 1)
1355 info.retaddr_reg = *ptr++;
1356 else
1357 info.retaddr_reg = dwarf_get_uleb128( &ptr );
1358 info.state.cfa_rule = RULE_CFA_OFFSET;
1360 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1361 ip, bases->func, cie, cie->length, cie->id, cie->version, cie->augmentation,
1362 info.code_align, info.data_align, dwarf_reg_names[info.retaddr_reg] );
1364 end = NULL;
1365 for (augmentation = cie->augmentation; *augmentation; augmentation++)
1367 switch (*augmentation)
1369 case 'z':
1370 len = dwarf_get_uleb128( &ptr );
1371 end = ptr + len;
1372 aug_z_format = 1;
1373 continue;
1374 case 'L':
1375 lsda_encoding = *ptr++;
1376 continue;
1377 case 'P':
1379 unsigned char encoding = *ptr++;
1380 *handler = (void *)dwarf_get_ptr( &ptr, encoding );
1381 continue;
1383 case 'R':
1384 info.fde_encoding = *ptr++;
1385 continue;
1386 case 'S':
1387 info.signal_frame = 1;
1388 continue;
1390 FIXME( "unknown augmentation '%c'\n", *augmentation );
1391 if (!end) return STATUS_INVALID_DISPOSITION; /* cannot continue */
1392 break;
1394 if (end) ptr = end;
1396 end = (const unsigned char *)(&cie->length + 1) + cie->length;
1397 execute_cfa_instructions( ptr, end, ip, &info );
1399 ptr = (const unsigned char *)(fde + 1);
1400 info.ip = dwarf_get_ptr( &ptr, info.fde_encoding ); /* fde code start */
1401 code_end = info.ip + dwarf_get_ptr( &ptr, info.fde_encoding & 0x0f ); /* fde code length */
1403 if (aug_z_format) /* get length of augmentation data */
1405 len = dwarf_get_uleb128( &ptr );
1406 end = ptr + len;
1408 else end = NULL;
1410 *handler_data = (void *)dwarf_get_ptr( &ptr, lsda_encoding );
1411 if (end) ptr = end;
1413 end = (const unsigned char *)(&fde->length + 1) + fde->length;
1414 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1415 fde, fde->length, *handler, *handler_data, info.ip, code_end );
1416 execute_cfa_instructions( ptr, end, ip, &info );
1417 *frame = context->Rsp;
1418 apply_frame_state( context, &info.state );
1420 TRACE( "next function rip=%016lx\n", context->Rip );
1421 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1422 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1423 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1424 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1425 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1426 context->R8, context->R9, context->R10, context->R11 );
1427 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1428 context->R12, context->R13, context->R14, context->R15 );
1430 return STATUS_SUCCESS;
1434 #ifdef HAVE_LIBUNWIND_H
1435 /***********************************************************************
1436 * libunwind_set_cursor_from_context
1438 static int libunwind_set_cursor_from_context( unw_cursor_t *cursor, CONTEXT *context, ULONG64 ip )
1440 int rc;
1442 rc = unw_set_reg(cursor, UNW_REG_IP, ip);
1443 if (rc == UNW_ESUCCESS)
1444 rc = unw_set_reg(cursor, UNW_REG_SP, context->Rsp);
1445 if (rc == UNW_ESUCCESS)
1446 rc = unw_set_reg(cursor, UNW_X86_64_RAX, context->Rax);
1447 if (rc == UNW_ESUCCESS)
1448 rc = unw_set_reg(cursor, UNW_X86_64_RDX, context->Rdx);
1449 if (rc == UNW_ESUCCESS)
1450 rc = unw_set_reg(cursor, UNW_X86_64_RCX, context->Rcx);
1451 if (rc == UNW_ESUCCESS)
1452 rc = unw_set_reg(cursor, UNW_X86_64_RBX, context->Rbx);
1453 if (rc == UNW_ESUCCESS)
1454 rc = unw_set_reg(cursor, UNW_X86_64_RSI, context->Rsi);
1455 if (rc == UNW_ESUCCESS)
1456 rc = unw_set_reg(cursor, UNW_X86_64_RDI, context->Rdi);
1457 if (rc == UNW_ESUCCESS)
1458 rc = unw_set_reg(cursor, UNW_X86_64_RBP, context->Rbp);
1459 if (rc == UNW_ESUCCESS)
1460 rc = unw_set_reg(cursor, UNW_X86_64_R8, context->R8);
1461 if (rc == UNW_ESUCCESS)
1462 rc = unw_set_reg(cursor, UNW_X86_64_R9, context->R9);
1463 if (rc == UNW_ESUCCESS)
1464 rc = unw_set_reg(cursor, UNW_X86_64_R10, context->R10);
1465 if (rc == UNW_ESUCCESS)
1466 rc = unw_set_reg(cursor, UNW_X86_64_R11, context->R11);
1467 if (rc == UNW_ESUCCESS)
1468 rc = unw_set_reg(cursor, UNW_X86_64_R12, context->R12);
1469 if (rc == UNW_ESUCCESS)
1470 rc = unw_set_reg(cursor, UNW_X86_64_R13, context->R13);
1471 if (rc == UNW_ESUCCESS)
1472 rc = unw_set_reg(cursor, UNW_X86_64_R14, context->R14);
1473 if (rc == UNW_ESUCCESS)
1474 rc = unw_set_reg(cursor, UNW_X86_64_R15, context->R15);
1476 return rc;
1480 /***********************************************************************
1481 * libunwind_get_reg
1483 static int libunwind_get_reg( unw_cursor_t *cursor, unw_regnum_t reg, ULONG64 *val )
1485 int rc;
1486 unw_word_t word;
1488 rc = unw_get_reg(cursor, reg, &word);
1489 if (rc == UNW_ESUCCESS)
1490 *val = word;
1492 return rc;
1496 /***********************************************************************
1497 * libunwind_set_context_from_cursor
1499 static BOOL libunwind_set_context_from_cursor( CONTEXT *context, unw_cursor_t *cursor )
1501 int rc;
1503 rc = libunwind_get_reg(cursor, UNW_REG_IP, &context->Rip);
1504 if (rc == UNW_ESUCCESS)
1505 rc = libunwind_get_reg(cursor, UNW_REG_SP, &context->Rsp);
1506 if (rc == UNW_ESUCCESS)
1507 rc = libunwind_get_reg(cursor, UNW_X86_64_RAX, &context->Rax);
1508 if (rc == UNW_ESUCCESS)
1509 rc = libunwind_get_reg(cursor, UNW_X86_64_RDX, &context->Rdx);
1510 if (rc == UNW_ESUCCESS)
1511 rc = libunwind_get_reg(cursor, UNW_X86_64_RCX, &context->Rcx);
1512 if (rc == UNW_ESUCCESS)
1513 rc = libunwind_get_reg(cursor, UNW_X86_64_RBX, &context->Rbx);
1514 if (rc == UNW_ESUCCESS)
1515 rc = libunwind_get_reg(cursor, UNW_X86_64_RSI, &context->Rsi);
1516 if (rc == UNW_ESUCCESS)
1517 rc = libunwind_get_reg(cursor, UNW_X86_64_RDI, &context->Rdi);
1518 if (rc == UNW_ESUCCESS)
1519 rc = libunwind_get_reg(cursor, UNW_X86_64_RBP, &context->Rbp);
1520 if (rc == UNW_ESUCCESS)
1521 rc = libunwind_get_reg(cursor, UNW_X86_64_R8, &context->R8);
1522 if (rc == UNW_ESUCCESS)
1523 rc = libunwind_get_reg(cursor, UNW_X86_64_R9, &context->R9);
1524 if (rc == UNW_ESUCCESS)
1525 rc = libunwind_get_reg(cursor, UNW_X86_64_R10, &context->R10);
1526 if (rc == UNW_ESUCCESS)
1527 rc = libunwind_get_reg(cursor, UNW_X86_64_R11, &context->R11);
1528 if (rc == UNW_ESUCCESS)
1529 rc = libunwind_get_reg(cursor, UNW_X86_64_R12, &context->R12);
1530 if (rc == UNW_ESUCCESS)
1531 rc = libunwind_get_reg(cursor, UNW_X86_64_R13, &context->R13);
1532 if (rc == UNW_ESUCCESS)
1533 rc = libunwind_get_reg(cursor, UNW_X86_64_R14, &context->R14);
1534 if (rc == UNW_ESUCCESS)
1535 rc = libunwind_get_reg(cursor, UNW_X86_64_R15, &context->R15);
1537 return rc;
1541 /***********************************************************************
1542 * libunwind_virtual_unwind
1544 * Equivalent of RtlVirtualUnwind for builtin modules.
1546 static NTSTATUS libunwind_virtual_unwind( ULONG64 ip, BOOL* got_info, ULONG64 *frame, CONTEXT *context,
1547 PEXCEPTION_ROUTINE *handler, void **handler_data )
1549 unw_context_t unw_context;
1550 unw_cursor_t cursor;
1551 unw_proc_info_t info;
1552 int rc;
1554 rc = unw_getcontext( &unw_context );
1555 if (rc == UNW_ESUCCESS)
1556 rc = unw_init_local( &cursor, &unw_context );
1557 if (rc == UNW_ESUCCESS)
1558 rc = libunwind_set_cursor_from_context( &cursor, context, ip - 1 );
1559 if (rc != UNW_ESUCCESS)
1561 WARN( "setup failed: %d\n", rc );
1562 return STATUS_INVALID_DISPOSITION;
1565 rc = unw_get_proc_info(&cursor, &info);
1566 if (rc != UNW_ESUCCESS && rc != UNW_ENOINFO)
1568 WARN( "failed to get info: %d\n", rc );
1569 return STATUS_INVALID_DISPOSITION;
1571 if (rc == UNW_ENOINFO || ip < info.start_ip || info.end_ip <= ip || !info.format)
1573 *got_info = FALSE;
1574 return STATUS_SUCCESS;
1577 TRACE( "ip %#lx function %#lx-%#lx personality %#lx lsda %#lx fde %#lx\n",
1578 ip, (unsigned long)info.start_ip, (unsigned long)info.end_ip, (unsigned long)info.handler,
1579 (unsigned long)info.lsda, (unsigned long)info.unwind_info );
1581 rc = unw_step(&cursor);
1582 if (rc < 0)
1584 WARN( "failed to unwind: %d\n", rc );
1585 return STATUS_INVALID_DISPOSITION;
1588 *frame = context->Rsp;
1590 rc = libunwind_set_context_from_cursor( context, &cursor );
1591 if (rc != UNW_ESUCCESS)
1593 WARN( "failed to update context after unwind: %d\n", rc );
1594 return STATUS_INVALID_DISPOSITION;
1597 *handler = (void*)info.handler;
1598 *handler_data = (void*)info.lsda;
1599 *got_info = TRUE;
1601 TRACE( "next function rip=%016lx\n", context->Rip );
1602 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1603 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1604 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1605 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1606 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1607 context->R8, context->R9, context->R10, context->R11 );
1608 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1609 context->R12, context->R13, context->R14, context->R15 );
1611 return STATUS_SUCCESS;
1613 #endif
1616 /***********************************************************************
1617 * dispatch_signal
1619 static inline int dispatch_signal(unsigned int sig)
1621 if (handlers[sig] == NULL) return 0;
1622 return handlers[sig](sig);
1625 /***********************************************************************
1626 * get_signal_stack
1628 * Get the base of the signal stack for the current thread.
1630 static inline void *get_signal_stack(void)
1632 return (char *)NtCurrentTeb() + teb_size;
1635 /***********************************************************************
1636 * is_inside_signal_stack
1638 * Check if pointer is inside the signal stack.
1640 static inline BOOL is_inside_signal_stack( void *ptr )
1642 return ((char *)ptr >= (char *)get_signal_stack() &&
1643 (char *)ptr < (char *)get_signal_stack() + signal_stack_size);
1646 /***********************************************************************
1647 * save_context
1649 * Set the register values from a sigcontext.
1651 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
1653 context->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
1654 context->Rax = RAX_sig(sigcontext);
1655 context->Rcx = RCX_sig(sigcontext);
1656 context->Rdx = RDX_sig(sigcontext);
1657 context->Rbx = RBX_sig(sigcontext);
1658 context->Rsp = RSP_sig(sigcontext);
1659 context->Rbp = RBP_sig(sigcontext);
1660 context->Rsi = RSI_sig(sigcontext);
1661 context->Rdi = RDI_sig(sigcontext);
1662 context->R8 = R8_sig(sigcontext);
1663 context->R9 = R9_sig(sigcontext);
1664 context->R10 = R10_sig(sigcontext);
1665 context->R11 = R11_sig(sigcontext);
1666 context->R12 = R12_sig(sigcontext);
1667 context->R13 = R13_sig(sigcontext);
1668 context->R14 = R14_sig(sigcontext);
1669 context->R15 = R15_sig(sigcontext);
1670 context->Rip = RIP_sig(sigcontext);
1671 context->SegCs = CS_sig(sigcontext);
1672 context->SegFs = FS_sig(sigcontext);
1673 context->SegGs = GS_sig(sigcontext);
1674 context->EFlags = EFL_sig(sigcontext);
1675 #ifdef DS_sig
1676 context->SegDs = DS_sig(sigcontext);
1677 #else
1678 __asm__("movw %%ds,%0" : "=m" (context->SegDs));
1679 #endif
1680 #ifdef ES_sig
1681 context->SegEs = ES_sig(sigcontext);
1682 #else
1683 __asm__("movw %%es,%0" : "=m" (context->SegEs));
1684 #endif
1685 #ifdef SS_sig
1686 context->SegSs = SS_sig(sigcontext);
1687 #else
1688 __asm__("movw %%ss,%0" : "=m" (context->SegSs));
1689 #endif
1690 if (FPU_sig(sigcontext))
1692 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1693 context->u.FltSave = *FPU_sig(sigcontext);
1694 context->MxCsr = context->u.FltSave.MxCsr;
1699 /***********************************************************************
1700 * restore_context
1702 * Build a sigcontext from the register values.
1704 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1706 RAX_sig(sigcontext) = context->Rax;
1707 RCX_sig(sigcontext) = context->Rcx;
1708 RDX_sig(sigcontext) = context->Rdx;
1709 RBX_sig(sigcontext) = context->Rbx;
1710 RSP_sig(sigcontext) = context->Rsp;
1711 RBP_sig(sigcontext) = context->Rbp;
1712 RSI_sig(sigcontext) = context->Rsi;
1713 RDI_sig(sigcontext) = context->Rdi;
1714 R8_sig(sigcontext) = context->R8;
1715 R9_sig(sigcontext) = context->R9;
1716 R10_sig(sigcontext) = context->R10;
1717 R11_sig(sigcontext) = context->R11;
1718 R12_sig(sigcontext) = context->R12;
1719 R13_sig(sigcontext) = context->R13;
1720 R14_sig(sigcontext) = context->R14;
1721 R15_sig(sigcontext) = context->R15;
1722 RIP_sig(sigcontext) = context->Rip;
1723 CS_sig(sigcontext) = context->SegCs;
1724 FS_sig(sigcontext) = context->SegFs;
1725 GS_sig(sigcontext) = context->SegGs;
1726 EFL_sig(sigcontext) = context->EFlags;
1727 #ifdef DS_sig
1728 DS_sig(sigcontext) = context->SegDs;
1729 #endif
1730 #ifdef ES_sig
1731 ES_sig(sigcontext) = context->SegEs;
1732 #endif
1733 #ifdef SS_sig
1734 SS_sig(sigcontext) = context->SegSs;
1735 #endif
1736 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
1740 /**************************************************************************
1741 * __chkstk (NTDLL.@)
1743 * Supposed to touch all the stack pages, but we shouldn't need that.
1745 __ASM_GLOBAL_FUNC( __chkstk, "ret" );
1748 /***********************************************************************
1749 * RtlCaptureContext (NTDLL.@)
1751 __ASM_GLOBAL_FUNC( RtlCaptureContext,
1752 "pushfq\n\t"
1753 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1754 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1755 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1756 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1757 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1758 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1759 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1760 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1761 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1762 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1763 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1764 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1765 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1766 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1767 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1768 "leaq 8(%rsp),%rax\n\t"
1769 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1770 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1771 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1772 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1773 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1774 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1775 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1776 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1777 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1778 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1779 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1780 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1781 "movq (%rsp),%rax\n\t"
1782 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1783 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1784 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1785 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1786 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1787 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1788 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1789 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1790 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1791 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1792 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1793 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1794 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1795 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1796 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1797 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1798 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1799 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1800 "ret" );
1802 /***********************************************************************
1803 * set_full_cpu_context
1805 * Set the new CPU context.
1807 extern void set_full_cpu_context( const CONTEXT *context );
1808 __ASM_GLOBAL_FUNC( set_full_cpu_context,
1809 "subq $40,%rsp\n\t"
1810 __ASM_CFI(".cfi_adjust_cfa_offset 40\n\t")
1811 "ldmxcsr 0x34(%rdi)\n\t" /* context->MxCsr */
1812 "movw 0x38(%rdi),%ax\n\t" /* context->SegCs */
1813 "movq %rax,8(%rsp)\n\t"
1814 "movw 0x42(%rdi),%ax\n\t" /* context->SegSs */
1815 "movq %rax,32(%rsp)\n\t"
1816 "movq 0x44(%rdi),%rax\n\t" /* context->Eflags */
1817 "movq %rax,16(%rsp)\n\t"
1818 "movq 0x80(%rdi),%rcx\n\t" /* context->Rcx */
1819 "movq 0x88(%rdi),%rdx\n\t" /* context->Rdx */
1820 "movq 0x90(%rdi),%rbx\n\t" /* context->Rbx */
1821 "movq 0x98(%rdi),%rax\n\t" /* context->Rsp */
1822 "movq %rax,24(%rsp)\n\t"
1823 "movq 0xa0(%rdi),%rbp\n\t" /* context->Rbp */
1824 "movq 0xa8(%rdi),%rsi\n\t" /* context->Rsi */
1825 "movq 0xb8(%rdi),%r8\n\t" /* context->R8 */
1826 "movq 0xc0(%rdi),%r9\n\t" /* context->R9 */
1827 "movq 0xc8(%rdi),%r10\n\t" /* context->R10 */
1828 "movq 0xd0(%rdi),%r11\n\t" /* context->R11 */
1829 "movq 0xd8(%rdi),%r12\n\t" /* context->R12 */
1830 "movq 0xe0(%rdi),%r13\n\t" /* context->R13 */
1831 "movq 0xe8(%rdi),%r14\n\t" /* context->R14 */
1832 "movq 0xf0(%rdi),%r15\n\t" /* context->R15 */
1833 "movq 0xf8(%rdi),%rax\n\t" /* context->Rip */
1834 "movq %rax,(%rsp)\n\t"
1835 "fxrstor 0x100(%rdi)\n\t" /* context->FtlSave */
1836 "movdqa 0x1a0(%rdi),%xmm0\n\t" /* context->Xmm0 */
1837 "movdqa 0x1b0(%rdi),%xmm1\n\t" /* context->Xmm1 */
1838 "movdqa 0x1c0(%rdi),%xmm2\n\t" /* context->Xmm2 */
1839 "movdqa 0x1d0(%rdi),%xmm3\n\t" /* context->Xmm3 */
1840 "movdqa 0x1e0(%rdi),%xmm4\n\t" /* context->Xmm4 */
1841 "movdqa 0x1f0(%rdi),%xmm5\n\t" /* context->Xmm5 */
1842 "movdqa 0x200(%rdi),%xmm6\n\t" /* context->Xmm6 */
1843 "movdqa 0x210(%rdi),%xmm7\n\t" /* context->Xmm7 */
1844 "movdqa 0x220(%rdi),%xmm8\n\t" /* context->Xmm8 */
1845 "movdqa 0x230(%rdi),%xmm9\n\t" /* context->Xmm9 */
1846 "movdqa 0x240(%rdi),%xmm10\n\t" /* context->Xmm10 */
1847 "movdqa 0x250(%rdi),%xmm11\n\t" /* context->Xmm11 */
1848 "movdqa 0x260(%rdi),%xmm12\n\t" /* context->Xmm12 */
1849 "movdqa 0x270(%rdi),%xmm13\n\t" /* context->Xmm13 */
1850 "movdqa 0x280(%rdi),%xmm14\n\t" /* context->Xmm14 */
1851 "movdqa 0x290(%rdi),%xmm15\n\t" /* context->Xmm15 */
1852 "movq 0x78(%rdi),%rax\n\t" /* context->Rax */
1853 "movq 0xb0(%rdi),%rdi\n\t" /* context->Rdi */
1854 "iretq" );
1857 /***********************************************************************
1858 * set_cpu_context
1860 * Set the new CPU context. Used by NtSetContextThread.
1862 void set_cpu_context( const CONTEXT *context )
1864 DWORD flags = context->ContextFlags & ~CONTEXT_AMD64;
1865 if (flags & CONTEXT_FULL)
1867 if (!(flags & CONTEXT_CONTROL))
1868 FIXME( "setting partial context (%x) not supported\n", flags );
1869 else
1870 set_full_cpu_context( context );
1875 /***********************************************************************
1876 * copy_context
1878 * Copy a register context according to the flags.
1880 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1882 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
1883 if (flags & CONTEXT_CONTROL)
1885 to->Rbp = from->Rbp;
1886 to->Rip = from->Rip;
1887 to->Rsp = from->Rsp;
1888 to->SegCs = from->SegCs;
1889 to->SegSs = from->SegSs;
1890 to->EFlags = from->EFlags;
1892 if (flags & CONTEXT_INTEGER)
1894 to->Rax = from->Rax;
1895 to->Rcx = from->Rcx;
1896 to->Rdx = from->Rdx;
1897 to->Rbx = from->Rbx;
1898 to->Rsi = from->Rsi;
1899 to->Rdi = from->Rdi;
1900 to->R8 = from->R8;
1901 to->R9 = from->R9;
1902 to->R10 = from->R10;
1903 to->R11 = from->R11;
1904 to->R12 = from->R12;
1905 to->R13 = from->R13;
1906 to->R14 = from->R14;
1907 to->R15 = from->R15;
1909 if (flags & CONTEXT_SEGMENTS)
1911 to->SegDs = from->SegDs;
1912 to->SegEs = from->SegEs;
1913 to->SegFs = from->SegFs;
1914 to->SegGs = from->SegGs;
1916 if (flags & CONTEXT_FLOATING_POINT)
1918 to->MxCsr = from->MxCsr;
1919 to->u.FltSave = from->u.FltSave;
1921 if (flags & CONTEXT_DEBUG_REGISTERS)
1923 to->Dr0 = from->Dr0;
1924 to->Dr1 = from->Dr1;
1925 to->Dr2 = from->Dr2;
1926 to->Dr3 = from->Dr3;
1927 to->Dr6 = from->Dr6;
1928 to->Dr7 = from->Dr7;
1933 /***********************************************************************
1934 * context_to_server
1936 * Convert a register context to the server format.
1938 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1940 DWORD flags = from->ContextFlags & ~CONTEXT_AMD64; /* get rid of CPU id */
1942 memset( to, 0, sizeof(*to) );
1943 to->cpu = CPU_x86_64;
1945 if (flags & CONTEXT_CONTROL)
1947 to->flags |= SERVER_CTX_CONTROL;
1948 to->ctl.x86_64_regs.rbp = from->Rbp;
1949 to->ctl.x86_64_regs.rip = from->Rip;
1950 to->ctl.x86_64_regs.rsp = from->Rsp;
1951 to->ctl.x86_64_regs.cs = from->SegCs;
1952 to->ctl.x86_64_regs.ss = from->SegSs;
1953 to->ctl.x86_64_regs.flags = from->EFlags;
1955 if (flags & CONTEXT_INTEGER)
1957 to->flags |= SERVER_CTX_INTEGER;
1958 to->integer.x86_64_regs.rax = from->Rax;
1959 to->integer.x86_64_regs.rcx = from->Rcx;
1960 to->integer.x86_64_regs.rdx = from->Rdx;
1961 to->integer.x86_64_regs.rbx = from->Rbx;
1962 to->integer.x86_64_regs.rsi = from->Rsi;
1963 to->integer.x86_64_regs.rdi = from->Rdi;
1964 to->integer.x86_64_regs.r8 = from->R8;
1965 to->integer.x86_64_regs.r9 = from->R9;
1966 to->integer.x86_64_regs.r10 = from->R10;
1967 to->integer.x86_64_regs.r11 = from->R11;
1968 to->integer.x86_64_regs.r12 = from->R12;
1969 to->integer.x86_64_regs.r13 = from->R13;
1970 to->integer.x86_64_regs.r14 = from->R14;
1971 to->integer.x86_64_regs.r15 = from->R15;
1973 if (flags & CONTEXT_SEGMENTS)
1975 to->flags |= SERVER_CTX_SEGMENTS;
1976 to->seg.x86_64_regs.ds = from->SegDs;
1977 to->seg.x86_64_regs.es = from->SegEs;
1978 to->seg.x86_64_regs.fs = from->SegFs;
1979 to->seg.x86_64_regs.gs = from->SegGs;
1981 if (flags & CONTEXT_FLOATING_POINT)
1983 to->flags |= SERVER_CTX_FLOATING_POINT;
1984 memcpy( to->fp.x86_64_regs.fpregs, &from->u.FltSave, sizeof(to->fp.x86_64_regs.fpregs) );
1986 if (flags & CONTEXT_DEBUG_REGISTERS)
1988 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1989 to->debug.x86_64_regs.dr0 = from->Dr0;
1990 to->debug.x86_64_regs.dr1 = from->Dr1;
1991 to->debug.x86_64_regs.dr2 = from->Dr2;
1992 to->debug.x86_64_regs.dr3 = from->Dr3;
1993 to->debug.x86_64_regs.dr6 = from->Dr6;
1994 to->debug.x86_64_regs.dr7 = from->Dr7;
1996 return STATUS_SUCCESS;
2000 /***********************************************************************
2001 * context_from_server
2003 * Convert a register context from the server format.
2005 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
2007 if (from->cpu != CPU_x86_64) return STATUS_INVALID_PARAMETER;
2009 to->ContextFlags = CONTEXT_AMD64;
2010 if (from->flags & SERVER_CTX_CONTROL)
2012 to->ContextFlags |= CONTEXT_CONTROL;
2013 to->Rbp = from->ctl.x86_64_regs.rbp;
2014 to->Rip = from->ctl.x86_64_regs.rip;
2015 to->Rsp = from->ctl.x86_64_regs.rsp;
2016 to->SegCs = from->ctl.x86_64_regs.cs;
2017 to->SegSs = from->ctl.x86_64_regs.ss;
2018 to->EFlags = from->ctl.x86_64_regs.flags;
2021 if (from->flags & SERVER_CTX_INTEGER)
2023 to->ContextFlags |= CONTEXT_INTEGER;
2024 to->Rax = from->integer.x86_64_regs.rax;
2025 to->Rcx = from->integer.x86_64_regs.rcx;
2026 to->Rdx = from->integer.x86_64_regs.rdx;
2027 to->Rbx = from->integer.x86_64_regs.rbx;
2028 to->Rsi = from->integer.x86_64_regs.rsi;
2029 to->Rdi = from->integer.x86_64_regs.rdi;
2030 to->R8 = from->integer.x86_64_regs.r8;
2031 to->R9 = from->integer.x86_64_regs.r9;
2032 to->R10 = from->integer.x86_64_regs.r10;
2033 to->R11 = from->integer.x86_64_regs.r11;
2034 to->R12 = from->integer.x86_64_regs.r12;
2035 to->R13 = from->integer.x86_64_regs.r13;
2036 to->R14 = from->integer.x86_64_regs.r14;
2037 to->R15 = from->integer.x86_64_regs.r15;
2039 if (from->flags & SERVER_CTX_SEGMENTS)
2041 to->ContextFlags |= CONTEXT_SEGMENTS;
2042 to->SegDs = from->seg.x86_64_regs.ds;
2043 to->SegEs = from->seg.x86_64_regs.es;
2044 to->SegFs = from->seg.x86_64_regs.fs;
2045 to->SegGs = from->seg.x86_64_regs.gs;
2047 if (from->flags & SERVER_CTX_FLOATING_POINT)
2049 to->ContextFlags |= CONTEXT_FLOATING_POINT;
2050 memcpy( &to->u.FltSave, from->fp.x86_64_regs.fpregs, sizeof(from->fp.x86_64_regs.fpregs) );
2051 to->MxCsr = to->u.FltSave.MxCsr;
2053 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
2055 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
2056 to->Dr0 = from->debug.x86_64_regs.dr0;
2057 to->Dr1 = from->debug.x86_64_regs.dr1;
2058 to->Dr2 = from->debug.x86_64_regs.dr2;
2059 to->Dr3 = from->debug.x86_64_regs.dr3;
2060 to->Dr6 = from->debug.x86_64_regs.dr6;
2061 to->Dr7 = from->debug.x86_64_regs.dr7;
2063 return STATUS_SUCCESS;
2067 extern void raise_func_trampoline( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
2068 __ASM_GLOBAL_FUNC( raise_func_trampoline,
2069 __ASM_CFI(".cfi_signal_frame\n\t")
2070 __ASM_CFI(".cfi_def_cfa %rbp,144\n\t") /* red zone + rip + rbp */
2071 __ASM_CFI(".cfi_rel_offset %rip,8\n\t")
2072 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
2073 "call *%rdx\n\t"
2074 "int $3")
2076 /***********************************************************************
2077 * setup_exception
2079 * Setup a proper stack frame for the raise function, and modify the
2080 * sigcontext so that the return from the signal handler will call
2081 * the raise function.
2083 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
2085 struct stack_layout
2087 CONTEXT context;
2088 EXCEPTION_RECORD rec;
2089 ULONG64 rbp;
2090 ULONG64 rip;
2091 ULONG64 red_zone[16];
2092 } *stack;
2093 ULONG64 *rsp_ptr;
2094 DWORD exception_code = 0;
2096 stack = (struct stack_layout *)(RSP_sig(sigcontext) & ~15);
2098 /* stack sanity checks */
2100 if (is_inside_signal_stack( stack ))
2102 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2103 GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext), (ULONG_PTR)RSP_sig(sigcontext),
2104 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2105 abort_thread(1);
2108 if (stack - 1 > stack || /* check for overflow in subtraction */
2109 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
2110 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
2112 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
2113 GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext), (ULONG_PTR)RSP_sig(sigcontext),
2114 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2116 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
2118 /* stack overflow on last page, unrecoverable */
2119 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
2120 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2121 diff, GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext),
2122 (ULONG_PTR)RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
2123 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2124 abort_thread(1);
2126 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
2128 /* stack access below stack limit, may be recoverable */
2129 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
2130 else
2132 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
2133 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
2134 diff, GetCurrentThreadId(), (ULONG_PTR)RIP_sig(sigcontext),
2135 (ULONG_PTR)RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
2136 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2137 abort_thread(1);
2141 stack--; /* push the stack_layout structure */
2142 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
2143 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
2144 #elif defined(VALGRIND_MAKE_WRITABLE)
2145 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
2146 #endif
2147 stack->rec.ExceptionRecord = NULL;
2148 stack->rec.ExceptionCode = exception_code;
2149 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
2150 stack->rec.ExceptionAddress = (void *)RIP_sig(sigcontext);
2151 stack->rec.NumberParameters = 0;
2152 save_context( &stack->context, sigcontext );
2154 /* store return address and %rbp without aligning, so that the offset is fixed */
2155 rsp_ptr = (ULONG64 *)RSP_sig(sigcontext) - 16;
2156 *(--rsp_ptr) = RIP_sig(sigcontext);
2157 *(--rsp_ptr) = RBP_sig(sigcontext);
2159 /* now modify the sigcontext to return to the raise function */
2160 RIP_sig(sigcontext) = (ULONG_PTR)raise_func_trampoline;
2161 RDI_sig(sigcontext) = (ULONG_PTR)&stack->rec;
2162 RSI_sig(sigcontext) = (ULONG_PTR)&stack->context;
2163 RDX_sig(sigcontext) = (ULONG_PTR)func;
2164 RBP_sig(sigcontext) = (ULONG_PTR)rsp_ptr;
2165 RSP_sig(sigcontext) = (ULONG_PTR)stack;
2166 /* clear single-step, direction, and align check flag */
2167 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
2169 return &stack->rec;
2173 /***********************************************************************
2174 * get_exception_context
2176 * Get a pointer to the context built by setup_exception.
2178 static inline CONTEXT *get_exception_context( EXCEPTION_RECORD *rec )
2180 return (CONTEXT *)rec - 1; /* cf. stack_layout structure */
2184 /**********************************************************************
2185 * find_function_info
2187 static RUNTIME_FUNCTION *find_function_info( ULONG64 pc, HMODULE module,
2188 RUNTIME_FUNCTION *func, ULONG size )
2190 int min = 0;
2191 int max = size/sizeof(*func) - 1;
2193 while (min <= max)
2195 int pos = (min + max) / 2;
2196 if ((char *)pc < (char *)module + func[pos].BeginAddress) max = pos - 1;
2197 else if ((char *)pc >= (char *)module + func[pos].EndAddress) min = pos + 1;
2198 else
2200 func += pos;
2201 while (func->UnwindData & 1) /* follow chained entry */
2202 func = (RUNTIME_FUNCTION *)((char *)module + (func->UnwindData & ~1));
2203 return func;
2206 return NULL;
2209 /**********************************************************************
2210 * lookup_function_info
2212 static RUNTIME_FUNCTION *lookup_function_info( ULONG64 pc, ULONG64 *base, LDR_MODULE **module )
2214 RUNTIME_FUNCTION *func = NULL;
2215 struct dynamic_unwind_entry *entry;
2216 ULONG size;
2218 /* PE module or wine module */
2219 if (!LdrFindEntryForAddress( (void *)pc, module ))
2221 *base = (ULONG64)(*module)->BaseAddress;
2222 if ((func = RtlImageDirectoryEntryToData( (*module)->BaseAddress, TRUE,
2223 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2225 /* lookup in function table */
2226 func = find_function_info( pc, (*module)->BaseAddress, func, size );
2229 else
2231 *module = NULL;
2233 RtlEnterCriticalSection( &dynamic_unwind_section );
2234 LIST_FOR_EACH_ENTRY( entry, &dynamic_unwind_list, struct dynamic_unwind_entry, entry )
2236 if (pc >= entry->base && pc < entry->base + entry->size)
2238 *base = entry->base;
2240 /* use callback or lookup in function table */
2241 if (entry->callback)
2242 func = entry->callback( pc, entry->context );
2243 else
2244 func = find_function_info( pc, (HMODULE)entry->base, entry->table, entry->table_size );
2245 break;
2248 RtlLeaveCriticalSection( &dynamic_unwind_section );
2251 return func;
2254 /**********************************************************************
2255 * call_handler
2257 * Call a single exception handler.
2258 * FIXME: Handle nested exceptions.
2260 static NTSTATUS call_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch, CONTEXT *orig_context )
2262 DWORD res;
2264 dispatch->ControlPc = dispatch->ContextRecord->Rip;
2266 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2267 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2268 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2269 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
2271 switch (res)
2273 case ExceptionContinueExecution:
2274 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
2275 *orig_context = *dispatch->ContextRecord;
2276 return STATUS_SUCCESS;
2277 case ExceptionContinueSearch:
2278 break;
2279 case ExceptionNestedException:
2280 break;
2281 default:
2282 return STATUS_INVALID_DISPOSITION;
2284 return STATUS_UNHANDLED_EXCEPTION;
2288 /**********************************************************************
2289 * call_teb_handler
2291 * Call a single exception handler from the TEB chain.
2292 * FIXME: Handle nested exceptions.
2294 static NTSTATUS call_teb_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
2295 EXCEPTION_REGISTRATION_RECORD *teb_frame, CONTEXT *orig_context )
2297 EXCEPTION_REGISTRATION_RECORD *dispatcher;
2298 DWORD res;
2300 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2301 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2302 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2303 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
2305 switch (res)
2307 case ExceptionContinueExecution:
2308 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
2309 *orig_context = *dispatch->ContextRecord;
2310 return STATUS_SUCCESS;
2311 case ExceptionContinueSearch:
2312 break;
2313 case ExceptionNestedException:
2314 break;
2315 default:
2316 return STATUS_INVALID_DISPOSITION;
2318 return STATUS_UNHANDLED_EXCEPTION;
2322 /**********************************************************************
2323 * call_stack_handlers
2325 * Call the stack handlers chain.
2327 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
2329 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
2330 UNWIND_HISTORY_TABLE table;
2331 DISPATCHER_CONTEXT dispatch;
2332 CONTEXT context, new_context;
2333 LDR_MODULE *module;
2334 NTSTATUS status;
2336 context = *orig_context;
2337 dispatch.TargetIp = 0;
2338 dispatch.ContextRecord = &context;
2339 dispatch.HistoryTable = &table;
2340 dispatch.ScopeIndex = 0; /* FIXME */
2341 for (;;)
2343 new_context = context;
2345 /* FIXME: should use the history table to make things faster */
2347 dispatch.ImageBase = 0;
2349 /* first look for PE exception information */
2351 if ((dispatch.FunctionEntry = lookup_function_info( context.Rip, &dispatch.ImageBase, &module )))
2353 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, dispatch.ImageBase,
2354 context.Rip, dispatch.FunctionEntry,
2355 &new_context, &dispatch.HandlerData,
2356 &dispatch.EstablisherFrame, NULL );
2357 goto unwind_done;
2360 /* then look for host system exception information */
2362 if (!module || (module->Flags & LDR_WINE_INTERNAL))
2364 BOOL got_info = FALSE;
2365 struct dwarf_eh_bases bases;
2366 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
2368 if (fde)
2370 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context,
2371 fde, &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
2372 if (status != STATUS_SUCCESS) return status;
2373 got_info = TRUE;
2375 #ifdef HAVE_LIBUNWIND_H
2376 else
2378 status = libunwind_virtual_unwind( context.Rip, &got_info, &dispatch.EstablisherFrame, &new_context,
2379 &dispatch.LanguageHandler, &dispatch.HandlerData );
2380 if (status != STATUS_SUCCESS) return status;
2382 #endif
2384 if (got_info)
2386 dispatch.FunctionEntry = NULL;
2387 if (dispatch.LanguageHandler && !module)
2389 FIXME( "calling personality routine in system library not supported yet\n" );
2390 dispatch.LanguageHandler = NULL;
2392 goto unwind_done;
2395 else WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
2397 /* no exception information, treat as a leaf function */
2399 new_context.Rip = *(ULONG64 *)context.Rsp;
2400 new_context.Rsp = context.Rsp + sizeof(ULONG64);
2401 dispatch.EstablisherFrame = new_context.Rsp;
2402 dispatch.LanguageHandler = NULL;
2404 unwind_done:
2405 if (!dispatch.EstablisherFrame) break;
2407 if ((dispatch.EstablisherFrame & 7) ||
2408 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
2409 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
2411 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
2412 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2413 rec->ExceptionFlags |= EH_STACK_INVALID;
2414 break;
2417 if (dispatch.LanguageHandler)
2419 status = call_handler( rec, &dispatch, orig_context );
2420 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
2422 /* hack: call wine handlers registered in the tib list */
2423 else while ((ULONG64)teb_frame < new_context.Rsp)
2425 TRACE( "found wine frame %p rsp %lx handler %p\n",
2426 teb_frame, new_context.Rsp, teb_frame->Handler );
2427 dispatch.EstablisherFrame = (ULONG64)teb_frame;
2428 context = *orig_context;
2429 status = call_teb_handler( rec, &dispatch, teb_frame, orig_context );
2430 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
2431 teb_frame = teb_frame->Prev;
2434 if (new_context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
2435 context = new_context;
2437 return STATUS_UNHANDLED_EXCEPTION;
2441 /*******************************************************************
2442 * raise_exception
2444 * Implementation of NtRaiseException.
2446 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2448 NTSTATUS status;
2450 if (first_chance)
2452 DWORD c;
2454 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2455 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
2456 context->Rip, GetCurrentThreadId() );
2457 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
2458 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
2459 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
2461 if (rec->ExceptionInformation[1] >> 16)
2462 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2463 rec->ExceptionAddress,
2464 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
2465 else
2466 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2467 rec->ExceptionAddress,
2468 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
2470 else
2472 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2473 context->Rax, context->Rbx, context->Rcx, context->Rdx );
2474 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2475 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
2476 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2477 context->R8, context->R9, context->R10, context->R11 );
2478 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2479 context->R12, context->R13, context->R14, context->R15 );
2481 status = send_debug_event( rec, TRUE, context );
2482 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
2483 return STATUS_SUCCESS;
2485 /* fix up instruction pointer in context for EXCEPTION_BREAKPOINT */
2486 if (rec->ExceptionCode == EXCEPTION_BREAKPOINT) context->Rip--;
2488 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
2489 return STATUS_SUCCESS;
2491 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
2492 return status;
2495 /* last chance exception */
2497 status = send_debug_event( rec, FALSE, context );
2498 if (status != DBG_CONTINUE)
2500 if (rec->ExceptionFlags & EH_STACK_INVALID)
2501 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2502 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
2503 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2504 else
2505 ERR("Unhandled exception code %x flags %x addr %p\n",
2506 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
2507 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
2509 return STATUS_SUCCESS;
2513 /**********************************************************************
2514 * raise_segv_exception
2516 static void raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2518 NTSTATUS status;
2520 switch(rec->ExceptionCode)
2522 case EXCEPTION_ACCESS_VIOLATION:
2523 if (rec->NumberParameters == 2)
2525 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
2526 rec->ExceptionInformation[0], FALSE )))
2527 set_cpu_context( context );
2529 break;
2530 case EXCEPTION_BREAKPOINT:
2531 switch (rec->ExceptionInformation[0])
2533 case 1: /* BREAKPOINT_PRINT */
2534 case 3: /* BREAKPOINT_LOAD_SYMBOLS */
2535 case 4: /* BREAKPOINT_UNLOAD_SYMBOLS */
2536 case 5: /* BREAKPOINT_COMMAND_STRING (>= Win2003) */
2537 goto done;
2539 break;
2541 status = raise_exception( rec, context, TRUE );
2542 if (status) raise_status( status, rec );
2543 done:
2544 set_cpu_context( context );
2548 /**********************************************************************
2549 * raise_generic_exception
2551 * Generic raise function for exceptions that don't need special treatment.
2553 static void raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2555 NTSTATUS status = raise_exception( rec, context, TRUE );
2556 if (status) raise_status( status, rec );
2557 set_cpu_context( context );
2561 /***********************************************************************
2562 * handle_interrupt
2564 * Handle an interrupt.
2566 static inline BOOL handle_interrupt( unsigned int interrupt, EXCEPTION_RECORD *rec, CONTEXT *context )
2568 switch(interrupt)
2570 case 0x2d:
2571 context->Rip += 3;
2572 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2573 rec->ExceptionAddress = (void *)context->Rip;
2574 rec->NumberParameters = 1;
2575 rec->ExceptionInformation[0] = context->Rax;
2576 return TRUE;
2577 default:
2578 return FALSE;
2583 /**********************************************************************
2584 * segv_handler
2586 * Handler for SIGSEGV and related errors.
2588 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2590 EXCEPTION_RECORD *rec;
2591 ucontext_t *ucontext = sigcontext;
2593 /* check for page fault inside the thread stack */
2594 if (TRAP_sig(ucontext) == TRAP_x86_PAGEFLT &&
2595 (char *)siginfo->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
2596 (char *)siginfo->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
2597 virtual_handle_stack_fault( siginfo->si_addr ))
2599 /* check if this was the last guard page */
2600 if ((char *)siginfo->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
2602 rec = setup_exception( sigcontext, raise_segv_exception );
2603 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2605 return;
2608 rec = setup_exception( sigcontext, raise_segv_exception );
2609 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
2611 switch(TRAP_sig(ucontext))
2613 case TRAP_x86_OFLOW: /* Overflow exception */
2614 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2615 break;
2616 case TRAP_x86_BOUND: /* Bound range exception */
2617 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2618 break;
2619 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2620 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2621 break;
2622 case TRAP_x86_STKFLT: /* Stack fault */
2623 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2624 break;
2625 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2626 case TRAP_x86_PROTFLT: /* General protection fault */
2627 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2629 CONTEXT *win_context = get_exception_context( rec );
2630 WORD err = ERROR_sig(ucontext);
2631 if ((err & 7) == 2 && handle_interrupt( err >> 3, rec, win_context )) break;
2632 rec->ExceptionCode = err ? EXCEPTION_ACCESS_VIOLATION : EXCEPTION_PRIV_INSTRUCTION;
2633 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2635 break;
2636 case TRAP_x86_PAGEFLT: /* Page fault */
2637 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2638 rec->NumberParameters = 2;
2639 rec->ExceptionInformation[0] = (ERROR_sig(ucontext) & 2) != 0;
2640 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2641 break;
2642 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2643 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2644 break;
2645 default:
2646 ERR( "Got unexpected trap %ld\n", (ULONG_PTR)TRAP_sig(ucontext) );
2647 /* fall through */
2648 case TRAP_x86_NMI: /* NMI interrupt */
2649 case TRAP_x86_DNA: /* Device not available exception */
2650 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2651 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2652 case TRAP_x86_MCHK: /* Machine check exception */
2653 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2654 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2655 break;
2659 /**********************************************************************
2660 * trap_handler
2662 * Handler for SIGTRAP.
2664 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2666 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2668 switch (siginfo->si_code)
2670 case TRAP_TRACE: /* Single-step exception */
2671 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2672 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2673 break;
2674 case TRAP_BRKPT: /* Breakpoint exception */
2675 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2676 /* fall through */
2677 default:
2678 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2679 rec->NumberParameters = 1;
2680 rec->ExceptionInformation[0] = 0;
2681 break;
2685 /**********************************************************************
2686 * fpe_handler
2688 * Handler for SIGFPE.
2690 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2692 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2694 switch (siginfo->si_code)
2696 case FPE_FLTSUB:
2697 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2698 break;
2699 case FPE_INTDIV:
2700 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2701 break;
2702 case FPE_INTOVF:
2703 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2704 break;
2705 case FPE_FLTDIV:
2706 rec->ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
2707 break;
2708 case FPE_FLTOVF:
2709 rec->ExceptionCode = EXCEPTION_FLT_OVERFLOW;
2710 break;
2711 case FPE_FLTUND:
2712 rec->ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
2713 break;
2714 case FPE_FLTRES:
2715 rec->ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
2716 break;
2717 case FPE_FLTINV:
2718 default:
2719 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2720 break;
2724 /**********************************************************************
2725 * int_handler
2727 * Handler for SIGINT.
2729 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2731 if (!dispatch_signal(SIGINT))
2733 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2734 rec->ExceptionCode = CONTROL_C_EXIT;
2739 /**********************************************************************
2740 * abrt_handler
2742 * Handler for SIGABRT.
2744 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2746 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2747 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2748 rec->ExceptionFlags = EH_NONCONTINUABLE;
2752 /**********************************************************************
2753 * quit_handler
2755 * Handler for SIGQUIT.
2757 static void quit_handler( int signal, siginfo_t *siginfo, void *ucontext )
2759 abort_thread(0);
2763 /**********************************************************************
2764 * usr1_handler
2766 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2768 static void usr1_handler( int signal, siginfo_t *siginfo, void *ucontext )
2770 CONTEXT context;
2772 save_context( &context, ucontext );
2773 wait_suspend( &context );
2774 restore_context( &context, ucontext );
2778 /***********************************************************************
2779 * __wine_set_signal_handler (NTDLL.@)
2781 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2783 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
2784 if (handlers[sig] != NULL) return -2;
2785 handlers[sig] = wsh;
2786 return 0;
2790 /**********************************************************************
2791 * signal_alloc_thread
2793 NTSTATUS signal_alloc_thread( TEB **teb )
2795 static size_t sigstack_zero_bits;
2796 SIZE_T size;
2797 NTSTATUS status;
2799 if (!sigstack_zero_bits)
2801 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2802 /* find the first power of two not smaller than min_size */
2803 sigstack_zero_bits = 12;
2804 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2805 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2806 assert( sizeof(TEB) <= teb_size );
2809 size = 1 << sigstack_zero_bits;
2810 *teb = NULL;
2811 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
2812 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2814 (*teb)->Tib.Self = &(*teb)->Tib;
2815 (*teb)->Tib.ExceptionList = (void *)~0UL;
2817 return status;
2821 /**********************************************************************
2822 * signal_free_thread
2824 void signal_free_thread( TEB *teb )
2826 SIZE_T size;
2828 if (teb->DeallocationStack)
2830 size = 0;
2831 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2833 size = 0;
2834 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2837 #ifdef __APPLE__
2838 static void init_teb_key(void)
2840 pthread_key_create( &teb_key, NULL );
2842 #endif
2844 /**********************************************************************
2845 * signal_init_thread
2847 void signal_init_thread( TEB *teb )
2849 const WORD fpu_cw = 0x27f;
2850 stack_t ss;
2852 #ifdef __APPLE__
2853 static pthread_once_t init_once = PTHREAD_ONCE_INIT;
2854 #endif
2856 #if defined __linux__
2857 arch_prctl( ARCH_SET_GS, teb );
2858 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
2859 amd64_set_gsbase( teb );
2860 #elif defined(__NetBSD__)
2861 sysarch( X86_64_SET_GSBASE, &teb );
2862 #elif defined (__APPLE__)
2863 /* FIXME: Actually setting %gs needs support from the OS */
2864 pthread_once( &init_once, init_teb_key );
2865 pthread_setspecific( teb_key, teb );
2866 #else
2867 # error Please define setting %gs for your architecture
2868 #endif
2870 ss.ss_sp = (char *)teb + teb_size;
2871 ss.ss_size = signal_stack_size;
2872 ss.ss_flags = 0;
2873 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2875 #ifdef __GNUC__
2876 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2877 #else
2878 FIXME("FPU setup not implemented for this platform.\n");
2879 #endif
2882 /**********************************************************************
2883 * signal_init_process
2885 void signal_init_process(void)
2887 struct sigaction sig_act;
2889 sig_act.sa_mask = server_block_set;
2890 sig_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
2892 sig_act.sa_sigaction = int_handler;
2893 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2894 sig_act.sa_sigaction = fpe_handler;
2895 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2896 sig_act.sa_sigaction = abrt_handler;
2897 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2898 sig_act.sa_sigaction = quit_handler;
2899 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2900 sig_act.sa_sigaction = usr1_handler;
2901 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2903 sig_act.sa_sigaction = segv_handler;
2904 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2905 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2906 #ifdef SIGBUS
2907 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2908 #endif
2910 #ifdef SIGTRAP
2911 sig_act.sa_sigaction = trap_handler;
2912 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2913 #endif
2914 return;
2916 error:
2917 perror("sigaction");
2918 exit(1);
2922 /**********************************************************************
2923 * RtlAddFunctionTable (NTDLL.@)
2925 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD64 addr )
2927 struct dynamic_unwind_entry *entry;
2929 TRACE( "%p %u %lx\n", table, count, addr );
2931 /* NOTE: Windows doesn't check if table is aligned or a NULL pointer */
2933 entry = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry) );
2934 if (!entry)
2935 return FALSE;
2937 entry->base = addr;
2938 entry->size = table[count - 1].EndAddress;
2939 entry->table = table;
2940 entry->table_size = count * sizeof(RUNTIME_FUNCTION);
2941 entry->callback = NULL;
2942 entry->context = NULL;
2944 RtlEnterCriticalSection( &dynamic_unwind_section );
2945 list_add_tail( &dynamic_unwind_list, &entry->entry );
2946 RtlLeaveCriticalSection( &dynamic_unwind_section );
2948 return TRUE;
2952 /**********************************************************************
2953 * RtlInstallFunctionTableCallback (NTDLL.@)
2955 BOOLEAN CDECL RtlInstallFunctionTableCallback( DWORD64 table, DWORD64 base, DWORD length,
2956 PGET_RUNTIME_FUNCTION_CALLBACK callback, PVOID context, PCWSTR dll )
2958 struct dynamic_unwind_entry *entry;
2960 TRACE( "%lx %lx %d %p %p %s\n", table, base, length, callback, context, wine_dbgstr_w(dll) );
2962 /* NOTE: Windows doesn't check if the provided callback is a NULL pointer */
2964 /* both low-order bits must be set */
2965 if ((table & 0x3) != 0x3)
2966 return FALSE;
2968 entry = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*entry) );
2969 if (!entry)
2970 return FALSE;
2972 entry->base = base;
2973 entry->size = length;
2974 entry->table = (RUNTIME_FUNCTION *)table;
2975 entry->table_size = 0;
2976 entry->callback = callback;
2977 entry->context = context;
2979 RtlEnterCriticalSection( &dynamic_unwind_section );
2980 list_add_tail( &dynamic_unwind_list, &entry->entry );
2981 RtlLeaveCriticalSection( &dynamic_unwind_section );
2983 return TRUE;
2987 /**********************************************************************
2988 * RtlDeleteFunctionTable (NTDLL.@)
2990 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
2992 struct dynamic_unwind_entry *entry, *to_free = NULL;
2994 TRACE( "%p\n", table );
2996 RtlEnterCriticalSection( &dynamic_unwind_section );
2997 LIST_FOR_EACH_ENTRY( entry, &dynamic_unwind_list, struct dynamic_unwind_entry, entry )
2999 if (entry->table == table)
3001 to_free = entry;
3002 list_remove( &entry->entry );
3003 break;
3006 RtlLeaveCriticalSection( &dynamic_unwind_section );
3008 if (!to_free)
3009 return FALSE;
3011 RtlFreeHeap( GetProcessHeap(), 0, to_free );
3012 return TRUE;
3016 /**********************************************************************
3017 * RtlLookupFunctionEntry (NTDLL.@)
3019 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base, UNWIND_HISTORY_TABLE *table )
3021 LDR_MODULE *module;
3022 RUNTIME_FUNCTION *func;
3024 /* FIXME: should use the history table to make things faster */
3026 func = lookup_function_info( pc, base, &module );
3027 if (!func)
3029 *base = 0;
3030 if (module)
3031 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
3032 else
3033 WARN( "module not found for %lx\n", pc );
3036 return func;
3039 static ULONG64 get_int_reg( CONTEXT *context, int reg )
3041 return *(&context->Rax + reg);
3044 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 val )
3046 *(&context->Rax + reg) = val;
3047 if (ctx_ptr) ctx_ptr->u2.IntegerContext[reg] = &context->Rax + reg;
3050 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A val )
3052 *(&context->u.s.Xmm0 + reg) = val;
3053 if (ctx_ptr) ctx_ptr->u.FloatingContext[reg] = &context->u.s.Xmm0 + reg;
3056 static int get_opcode_size( struct opcode op )
3058 switch (op.code)
3060 case UWOP_ALLOC_LARGE:
3061 return 2 + (op.info != 0);
3062 case UWOP_SAVE_NONVOL:
3063 case UWOP_SAVE_XMM128:
3064 return 2;
3065 case UWOP_SAVE_NONVOL_FAR:
3066 case UWOP_SAVE_XMM128_FAR:
3067 return 3;
3068 default:
3069 return 1;
3073 static BOOL is_inside_epilog( BYTE *pc, ULONG64 base, const RUNTIME_FUNCTION *function )
3075 /* add or lea must be the first instruction, and it must have a rex.W prefix */
3076 if ((pc[0] & 0xf8) == 0x48)
3078 switch (pc[1])
3080 case 0x81: /* add $nnnn,%rsp */
3081 if (pc[0] == 0x48 && pc[2] == 0xc4)
3083 pc += 7;
3084 break;
3086 return FALSE;
3087 case 0x83: /* add $n,%rsp */
3088 if (pc[0] == 0x48 && pc[2] == 0xc4)
3090 pc += 4;
3091 break;
3093 return FALSE;
3094 case 0x8d: /* lea n(reg),%rsp */
3095 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
3096 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
3097 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
3098 if ((pc[2] >> 6) == 1) /* 8-bit offset */
3100 pc += 4;
3101 break;
3103 if ((pc[2] >> 6) == 2) /* 32-bit offset */
3105 pc += 7;
3106 break;
3108 return FALSE;
3112 /* now check for various pop instructions */
3114 for (;;)
3116 if ((*pc & 0xf0) == 0x40) pc++; /* rex prefix */
3118 switch (*pc)
3120 case 0x58: /* pop %rax/%r8 */
3121 case 0x59: /* pop %rcx/%r9 */
3122 case 0x5a: /* pop %rdx/%r10 */
3123 case 0x5b: /* pop %rbx/%r11 */
3124 case 0x5c: /* pop %rsp/%r12 */
3125 case 0x5d: /* pop %rbp/%r13 */
3126 case 0x5e: /* pop %rsi/%r14 */
3127 case 0x5f: /* pop %rdi/%r15 */
3128 pc++;
3129 continue;
3130 case 0xc2: /* ret $nn */
3131 case 0xc3: /* ret */
3132 return TRUE;
3133 case 0xe9: /* jmp nnnn */
3134 pc += 5 + *(LONG *)(pc + 1);
3135 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
3136 continue;
3137 break;
3138 case 0xeb: /* jmp n */
3139 pc += 2 + (signed char)pc[1];
3140 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
3141 continue;
3142 break;
3143 case 0xf3: /* rep; ret (for amd64 prediction bug) */
3144 return pc[1] == 0xc3;
3146 return FALSE;
3150 /* execute a function epilog, which must have been validated with is_inside_epilog() */
3151 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
3153 for (;;)
3155 BYTE rex = 0;
3157 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
3159 switch (*pc)
3161 case 0x58: /* pop %rax/r8 */
3162 case 0x59: /* pop %rcx/r9 */
3163 case 0x5a: /* pop %rdx/r10 */
3164 case 0x5b: /* pop %rbx/r11 */
3165 case 0x5c: /* pop %rsp/r12 */
3166 case 0x5d: /* pop %rbp/r13 */
3167 case 0x5e: /* pop %rsi/r14 */
3168 case 0x5f: /* pop %rdi/r15 */
3169 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, *(ULONG64 *)context->Rsp );
3170 context->Rsp += sizeof(ULONG64);
3171 pc++;
3172 continue;
3173 case 0x81: /* add $nnnn,%rsp */
3174 context->Rsp += *(LONG *)(pc + 2);
3175 pc += 2 + sizeof(LONG);
3176 continue;
3177 case 0x83: /* add $n,%rsp */
3178 context->Rsp += (signed char)pc[2];
3179 pc += 3;
3180 continue;
3181 case 0x8d:
3182 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
3184 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
3185 pc += 3;
3187 else /* lea nnnn(reg),%rsp */
3189 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
3190 pc += 2 + sizeof(LONG);
3192 continue;
3193 case 0xc2: /* ret $nn */
3194 context->Rip = *(ULONG64 *)context->Rsp;
3195 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
3196 return;
3197 case 0xc3: /* ret */
3198 case 0xf3: /* rep; ret */
3199 context->Rip = *(ULONG64 *)context->Rsp;
3200 context->Rsp += sizeof(ULONG64);
3201 return;
3202 case 0xe9: /* jmp nnnn */
3203 pc += 5 + *(LONG *)(pc + 1);
3204 continue;
3205 case 0xeb: /* jmp n */
3206 pc += 2 + (signed char)pc[1];
3207 continue;
3209 return;
3213 /**********************************************************************
3214 * RtlVirtualUnwind (NTDLL.@)
3216 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
3217 RUNTIME_FUNCTION *function, CONTEXT *context,
3218 PVOID *data, ULONG64 *frame_ret,
3219 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
3221 union handler_data *handler_data;
3222 ULONG64 frame, off;
3223 struct UNWIND_INFO *info;
3224 unsigned int i, prolog_offset;
3226 TRACE( "type %x rip %lx rsp %lx\n", type, pc, context->Rsp );
3227 if (TRACE_ON(seh)) dump_unwind_info( base, function );
3229 frame = *frame_ret = context->Rsp;
3230 for (;;)
3232 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
3233 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
3235 if (info->version != 1)
3237 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
3238 return NULL;
3241 if (info->frame_reg)
3242 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
3244 /* check if in prolog */
3245 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
3247 prolog_offset = pc - base - function->BeginAddress;
3249 else
3251 prolog_offset = ~0;
3252 if (is_inside_epilog( (BYTE *)pc, base, function ))
3254 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
3255 *frame_ret = frame;
3256 return NULL;
3260 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
3262 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
3264 switch (info->opcodes[i].code)
3266 case UWOP_PUSH_NONVOL: /* pushq %reg */
3267 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)context->Rsp );
3268 context->Rsp += sizeof(ULONG64);
3269 break;
3270 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
3271 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
3272 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
3273 break;
3274 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
3275 context->Rsp += (info->opcodes[i].info + 1) * 8;
3276 break;
3277 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
3278 context->Rsp = *frame_ret = frame;
3279 break;
3280 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
3281 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
3282 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
3283 break;
3284 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
3285 off = frame + *(DWORD *)&info->opcodes[i+1];
3286 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
3287 break;
3288 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
3289 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
3290 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
3291 break;
3292 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
3293 off = frame + *(DWORD *)&info->opcodes[i+1];
3294 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
3295 break;
3296 case UWOP_PUSH_MACHFRAME:
3297 FIXME( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
3298 break;
3299 default:
3300 FIXME( "unknown code %u\n", info->opcodes[i].code );
3301 break;
3305 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
3306 function = &handler_data->chain; /* restart with the chained info */
3309 /* now pop return address */
3310 context->Rip = *(ULONG64 *)context->Rsp;
3311 context->Rsp += sizeof(ULONG64);
3313 if (!(info->flags & type)) return NULL; /* no matching handler */
3314 if (prolog_offset != ~0) return NULL; /* inside prolog */
3316 *data = &handler_data->handler + 1;
3317 return (char *)base + handler_data->handler;
3321 /**********************************************************************
3322 * call_unwind_handler
3324 * Call a single unwind handler.
3325 * FIXME: Handle nested exceptions.
3327 static void call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
3329 DWORD res;
3331 dispatch->ControlPc = dispatch->ContextRecord->Rip;
3333 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
3334 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
3335 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
3336 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
3338 switch (res)
3340 case ExceptionContinueSearch:
3341 break;
3342 case ExceptionCollidedUnwind:
3343 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
3344 break;
3345 default:
3346 raise_status( STATUS_INVALID_DISPOSITION, rec );
3347 break;
3352 /**********************************************************************
3353 * call_teb_unwind_handler
3355 * Call a single unwind handler from the TEB chain.
3356 * FIXME: Handle nested exceptions.
3358 static void call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
3359 EXCEPTION_REGISTRATION_RECORD *teb_frame )
3361 EXCEPTION_REGISTRATION_RECORD *dispatcher;
3362 DWORD res;
3364 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
3365 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
3366 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
3367 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
3369 switch (res)
3371 case ExceptionContinueSearch:
3372 break;
3373 case ExceptionCollidedUnwind:
3374 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
3375 break;
3376 default:
3377 raise_status( STATUS_INVALID_DISPOSITION, rec );
3378 break;
3383 /**********************************************************************
3384 * call_consolidate_callback
3386 * Wrapper function to call a consolidate callback from a fake frame.
3387 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
3388 * we have to skip all frames which were already processed. To do that we
3389 * trick the unwinding functions into thinking the call came from somewhere
3390 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
3391 * DW_CFA_expression, and the expressions have the following format:
3393 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
3394 * DW_OP_deref | Get *(%rbp + 0x10) == context
3395 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
3396 * [DW_OP_deref] | Dereference, only for CFA
3398 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
3399 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
3400 EXCEPTION_RECORD *rec );
3401 __ASM_GLOBAL_FUNC( call_consolidate_callback,
3402 "pushq %rbp\n\t"
3403 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
3404 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
3405 "movq %rsp,%rbp\n\t"
3406 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
3407 "subq $0x20,%rsp\n\t"
3408 "movq %rcx,0x10(%rbp)\n\t"
3409 __ASM_CFI(".cfi_remember_state\n\t")
3410 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
3411 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
3412 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
3413 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
3414 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
3415 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
3416 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
3417 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
3418 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
3419 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
3420 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
3421 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
3422 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
3423 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
3424 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
3425 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
3426 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
3427 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
3428 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
3429 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
3430 "movq %r8,%rcx\n\t"
3431 "callq *%rdx\n\t"
3432 __ASM_CFI(".cfi_restore_state\n\t")
3433 "movq %rbp,%rsp\n\t"
3434 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
3435 "popq %rbp\n\t"
3436 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
3437 __ASM_CFI(".cfi_same_value %rbp\n\t")
3438 "ret")
3441 /*******************************************************************
3442 * RtlUnwindEx (NTDLL.@)
3444 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
3445 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
3447 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
3448 EXCEPTION_RECORD record;
3449 DISPATCHER_CONTEXT dispatch;
3450 CONTEXT new_context;
3451 LDR_MODULE *module;
3452 NTSTATUS status;
3453 DWORD i;
3455 RtlCaptureContext( context );
3456 new_context = *context;
3458 /* build an exception record, if we do not have one */
3459 if (!rec)
3461 record.ExceptionCode = STATUS_UNWIND;
3462 record.ExceptionFlags = 0;
3463 record.ExceptionRecord = NULL;
3464 record.ExceptionAddress = (void *)context->Rip;
3465 record.NumberParameters = 0;
3466 rec = &record;
3469 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
3471 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
3472 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Rip );
3473 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
3474 TRACE( " info[%d]=%016lx\n", i, rec->ExceptionInformation[i] );
3475 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
3476 context->Rax, context->Rbx, context->Rcx, context->Rdx );
3477 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
3478 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
3479 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
3480 context->R8, context->R9, context->R10, context->R11 );
3481 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
3482 context->R12, context->R13, context->R14, context->R15 );
3484 dispatch.EstablisherFrame = context->Rsp;
3485 dispatch.TargetIp = (ULONG64)target_ip;
3486 dispatch.ContextRecord = context;
3487 dispatch.HistoryTable = table;
3489 for (;;)
3491 /* FIXME: should use the history table to make things faster */
3493 dispatch.ImageBase = 0;
3494 dispatch.ScopeIndex = 0; /* FIXME */
3496 /* first look for PE exception information */
3498 if ((dispatch.FunctionEntry = lookup_function_info( context->Rip, &dispatch.ImageBase, &module )))
3500 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_UHANDLER, dispatch.ImageBase,
3501 context->Rip, dispatch.FunctionEntry,
3502 &new_context, &dispatch.HandlerData,
3503 &dispatch.EstablisherFrame, NULL );
3504 goto unwind_done;
3507 /* then look for host system exception information */
3509 if (!module || (module->Flags & LDR_WINE_INTERNAL))
3511 BOOL got_info = FALSE;
3512 struct dwarf_eh_bases bases;
3513 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context->Rip - 1), &bases );
3515 if (fde)
3517 status = dwarf_virtual_unwind( context->Rip, &dispatch.EstablisherFrame, &new_context, fde,
3518 &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
3519 if (status != STATUS_SUCCESS) raise_status( status, rec );
3520 got_info = TRUE;
3522 #ifdef HAVE_LIBUNWIND_H
3523 else
3525 status = libunwind_virtual_unwind( context->Rip, &got_info, &dispatch.EstablisherFrame, &new_context,
3526 &dispatch.LanguageHandler, &dispatch.HandlerData );
3527 if (status != STATUS_SUCCESS) raise_status( status, rec );
3529 #endif
3531 if (got_info)
3533 dispatch.FunctionEntry = NULL;
3534 if (dispatch.LanguageHandler && !module)
3536 FIXME( "calling personality routine in system library not supported yet\n" );
3537 dispatch.LanguageHandler = NULL;
3539 goto unwind_done;
3542 else WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
3544 /* no exception information, treat as a leaf function */
3546 new_context.Rip = *(ULONG64 *)context->Rsp;
3547 new_context.Rsp = context->Rsp + sizeof(ULONG64);
3548 dispatch.EstablisherFrame = new_context.Rsp;
3549 dispatch.LanguageHandler = NULL;
3551 unwind_done:
3552 if (!dispatch.EstablisherFrame) break;
3554 if (is_inside_signal_stack( (void *)dispatch.EstablisherFrame ))
3556 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch.EstablisherFrame,
3557 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size );
3558 *context = new_context;
3559 continue;
3562 if ((dispatch.EstablisherFrame & 7) ||
3563 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
3564 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
3566 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
3567 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
3568 rec->ExceptionFlags |= EH_STACK_INVALID;
3569 break;
3572 if (dispatch.LanguageHandler)
3574 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
3576 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
3577 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
3579 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
3580 call_unwind_handler( rec, &dispatch );
3582 else /* hack: call builtin handlers registered in the tib list */
3584 DWORD64 backup_frame = dispatch.EstablisherFrame;
3585 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < (ULONG64)end_frame)
3587 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
3588 dispatch.EstablisherFrame = (ULONG64)teb_frame;
3589 call_teb_unwind_handler( rec, &dispatch, teb_frame );
3590 teb_frame = __wine_pop_frame( teb_frame );
3592 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Rsp) break;
3593 dispatch.EstablisherFrame = backup_frame;
3596 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
3597 *context = new_context;
3600 if (rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
3602 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
3603 context->Rbx = jmp->Rbx;
3604 context->Rsp = jmp->Rsp;
3605 context->Rbp = jmp->Rbp;
3606 context->Rsi = jmp->Rsi;
3607 context->Rdi = jmp->Rdi;
3608 context->R12 = jmp->R12;
3609 context->R13 = jmp->R13;
3610 context->R14 = jmp->R14;
3611 context->R15 = jmp->R15;
3612 context->Rip = jmp->Rip;
3613 context->u.s.Xmm6 = jmp->Xmm6;
3614 context->u.s.Xmm7 = jmp->Xmm7;
3615 context->u.s.Xmm8 = jmp->Xmm8;
3616 context->u.s.Xmm9 = jmp->Xmm9;
3617 context->u.s.Xmm10 = jmp->Xmm10;
3618 context->u.s.Xmm11 = jmp->Xmm11;
3619 context->u.s.Xmm12 = jmp->Xmm12;
3620 context->u.s.Xmm13 = jmp->Xmm13;
3621 context->u.s.Xmm14 = jmp->Xmm14;
3622 context->u.s.Xmm15 = jmp->Xmm15;
3624 else if (rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
3626 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
3627 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
3628 target_ip = call_consolidate_callback( context, consolidate, rec );
3629 TRACE( "-> target=%p\n", target_ip );
3631 context->Rax = (ULONG64)retval;
3632 context->Rip = (ULONG64)target_ip;
3633 TRACE( "returning to %lx stack %lx\n", context->Rip, context->Rsp );
3634 set_cpu_context( context );
3638 /*******************************************************************
3639 * RtlUnwind (NTDLL.@)
3641 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
3643 CONTEXT context;
3644 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
3648 /*******************************************************************
3649 * _local_unwind (NTDLL.@)
3651 void WINAPI _local_unwind( void *frame, void *target_ip )
3653 CONTEXT context;
3654 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
3658 /*******************************************************************
3659 * __C_specific_handler (NTDLL.@)
3661 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
3662 ULONG64 frame,
3663 CONTEXT *context,
3664 struct _DISPATCHER_CONTEXT *dispatch )
3666 SCOPE_TABLE *table = dispatch->HandlerData;
3667 ULONG i;
3669 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
3670 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
3672 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)) /* FIXME */
3673 return ExceptionContinueSearch;
3675 for (i = 0; i < table->Count; i++)
3677 if (context->Rip >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
3678 context->Rip < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
3680 if (!table->ScopeRecord[i].JumpTarget) continue;
3681 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
3683 EXCEPTION_POINTERS ptrs;
3684 PC_LANGUAGE_EXCEPTION_HANDLER filter;
3686 filter = (PC_LANGUAGE_EXCEPTION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
3687 ptrs.ExceptionRecord = rec;
3688 ptrs.ContextRecord = context;
3689 TRACE( "calling filter %p ptrs %p frame %lx\n", filter, &ptrs, frame );
3690 switch (filter( &ptrs, frame ))
3692 case EXCEPTION_EXECUTE_HANDLER:
3693 break;
3694 case EXCEPTION_CONTINUE_SEARCH:
3695 continue;
3696 case EXCEPTION_CONTINUE_EXECUTION:
3697 return ExceptionContinueExecution;
3700 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
3701 RtlUnwindEx( (void *)frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
3702 rec, 0, context, dispatch->HistoryTable );
3705 return ExceptionContinueSearch;
3709 /*******************************************************************
3710 * NtRaiseException (NTDLL.@)
3712 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
3714 NTSTATUS status = raise_exception( rec, context, first_chance );
3715 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
3716 return status;
3720 /***********************************************************************
3721 * RtlRaiseException (NTDLL.@)
3723 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
3725 NTSTATUS status;
3727 rec->ExceptionAddress = (void *)context->Rip;
3728 status = raise_exception( rec, context, TRUE );
3729 if (status != STATUS_SUCCESS) raise_status( status, rec );
3731 __ASM_GLOBAL_FUNC( RtlRaiseException,
3732 "movq %rcx,8(%rsp)\n\t"
3733 "sub $0x4f8,%rsp\n\t"
3734 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
3735 "leaq 0x20(%rsp),%rcx\n\t"
3736 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
3737 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
3738 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
3739 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
3740 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
3741 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
3742 "movq (%rax),%rcx\n\t" /* original first parameter */
3743 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
3744 "call " __ASM_NAME("__regs_RtlRaiseException") "\n\t"
3745 "leaq 0x20(%rsp),%rdi\n\t" /* context pointer */
3746 "call " __ASM_NAME("set_cpu_context") /* does not return */ );
3749 /*************************************************************************
3750 * RtlCaptureStackBackTrace (NTDLL.@)
3752 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
3754 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
3755 return 0;
3759 /***********************************************************************
3760 * call_thread_func
3762 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
3764 ntdll_get_thread_data()->exit_frame = frame;
3765 __TRY
3767 RtlExitUserThread( entry( arg ));
3769 __EXCEPT(unhandled_exception_filter)
3771 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
3773 __ENDTRY
3774 abort(); /* should not be reached */
3777 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
3778 __ASM_GLOBAL_FUNC( call_thread_entry_point,
3779 "subq $56,%rsp\n\t"
3780 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3781 "movq %rbp,48(%rsp)\n\t"
3782 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3783 "movq %rbx,40(%rsp)\n\t"
3784 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3785 "movq %r12,32(%rsp)\n\t"
3786 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3787 "movq %r13,24(%rsp)\n\t"
3788 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3789 "movq %r14,16(%rsp)\n\t"
3790 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3791 "movq %r15,8(%rsp)\n\t"
3792 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3793 "movq %rsp,%rdx\n\t"
3794 "call " __ASM_NAME("call_thread_func") );
3796 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
3797 __ASM_GLOBAL_FUNC( call_thread_exit_func,
3798 "movq %rdx,%rsp\n\t"
3799 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3800 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3801 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3802 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3803 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3804 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3805 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3806 "call *%rsi" );
3808 /***********************************************************************
3809 * RtlExitUserThread (NTDLL.@)
3811 void WINAPI RtlExitUserThread( ULONG status )
3813 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
3814 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
3817 /***********************************************************************
3818 * abort_thread
3820 void abort_thread( int status )
3822 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
3823 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
3826 /**********************************************************************
3827 * __wine_enter_vm86 (NTDLL.@)
3829 void __wine_enter_vm86( CONTEXT *context )
3831 MESSAGE("vm86 mode not supported on this platform\n");
3834 /**********************************************************************
3835 * DbgBreakPoint (NTDLL.@)
3837 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
3839 /**********************************************************************
3840 * DbgUserBreakPoint (NTDLL.@)
3842 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
3844 /**********************************************************************
3845 * NtCurrentTeb (NTDLL.@)
3847 * FIXME: This isn't exported from NTDLL on real NT. This should be
3848 * removed if and when we can set the GSBASE MSR on Mac OS X.
3850 #ifdef __APPLE__
3851 TEB * WINAPI NtCurrentTeb(void)
3853 return pthread_getspecific( teb_key );
3855 #else
3856 __ASM_STDCALL_FUNC( NtCurrentTeb, 0, ".byte 0x65\n\tmovq 0x30,%rax\n\tret" )
3857 #endif
3859 #endif /* __x86_64__ */