winex11: Fix printing NULL strings.
[wine/multimedia.git] / dlls / ntdll / signal_x86_64.c
blob0fc53c790af897152547a27379a5ec12a5c8b13b
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>
32 #ifdef HAVE_UCONTEXT_H
33 # include <ucontext.h>
34 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
40 #ifdef HAVE_MACHINE_SYSARCH_H
41 # include <machine/sysarch.h>
42 #endif
44 #ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
46 #endif
47 #ifdef HAVE_SYS_SIGNAL_H
48 # include <sys/signal.h>
49 #endif
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
53 #include "ntstatus.h"
54 #define WIN32_NO_STATUS
55 #include "windef.h"
56 #include "winternl.h"
57 #include "wine/library.h"
58 #include "wine/exception.h"
59 #include "ntdll_misc.h"
60 #include "wine/debug.h"
62 #ifdef HAVE_VALGRIND_MEMCHECK_H
63 #include <valgrind/memcheck.h>
64 #endif
66 WINE_DEFAULT_DEBUG_CHANNEL(seh);
68 struct _DISPATCHER_CONTEXT;
70 typedef LONG (WINAPI *PC_LANGUAGE_EXCEPTION_HANDLER)( EXCEPTION_POINTERS *ptrs, ULONG64 frame );
71 typedef EXCEPTION_DISPOSITION (WINAPI *PEXCEPTION_ROUTINE)( EXCEPTION_RECORD *rec,
72 ULONG64 frame,
73 CONTEXT *context,
74 struct _DISPATCHER_CONTEXT *dispatch );
76 typedef struct _DISPATCHER_CONTEXT
78 ULONG64 ControlPc;
79 ULONG64 ImageBase;
80 PRUNTIME_FUNCTION FunctionEntry;
81 ULONG64 EstablisherFrame;
82 ULONG64 TargetIp;
83 PCONTEXT ContextRecord;
84 PEXCEPTION_ROUTINE LanguageHandler;
85 PVOID HandlerData;
86 PUNWIND_HISTORY_TABLE HistoryTable;
87 ULONG ScopeIndex;
88 } DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;
90 typedef struct _SCOPE_TABLE
92 ULONG Count;
93 struct
95 ULONG BeginAddress;
96 ULONG EndAddress;
97 ULONG HandlerAddress;
98 ULONG JumpTarget;
99 } ScopeRecord[1];
100 } SCOPE_TABLE, *PSCOPE_TABLE;
103 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
104 struct MSVCRT_JUMP_BUFFER
106 ULONG64 Frame;
107 ULONG64 Rbx;
108 ULONG64 Rsp;
109 ULONG64 Rbp;
110 ULONG64 Rsi;
111 ULONG64 Rdi;
112 ULONG64 R12;
113 ULONG64 R13;
114 ULONG64 R14;
115 ULONG64 R15;
116 ULONG64 Rip;
117 ULONG64 Spare;
118 M128A Xmm6;
119 M128A Xmm7;
120 M128A Xmm8;
121 M128A Xmm9;
122 M128A Xmm10;
123 M128A Xmm11;
124 M128A Xmm12;
125 M128A Xmm13;
126 M128A Xmm14;
127 M128A Xmm15;
130 /***********************************************************************
131 * signal context platform-specific definitions
133 #ifdef linux
135 #include <asm/prctl.h>
136 extern int arch_prctl(int func, void *ptr);
138 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
139 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
140 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
141 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
142 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
143 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
144 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
145 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
146 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
147 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
148 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
149 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
150 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
151 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
152 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
154 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
155 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
156 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
158 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
159 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
160 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
161 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
162 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
164 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
166 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__)
167 #include <sys/ucontext.h>
169 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
170 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
171 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
172 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
173 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
174 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
175 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
176 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
177 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
178 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
179 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
180 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
181 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
182 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
183 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
185 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
186 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
187 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
188 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
189 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
190 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
192 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
194 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
195 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
196 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
197 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
199 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
201 #elif defined(__NetBSD__)
202 #include <sys/ucontext.h>
203 #include <sys/types.h>
204 #include <signal.h>
206 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
207 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
208 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
209 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
210 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
211 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
212 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
213 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
214 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
215 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
216 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
217 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
218 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
219 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
220 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
222 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
223 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
224 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
225 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
226 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
227 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
229 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
231 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
232 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
234 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
235 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
237 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
238 #else
239 #error You must define the signal context functions for your platform
240 #endif
242 enum i386_trap_code
244 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
245 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
246 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
247 TRAP_x86_NMI = 2, /* NMI interrupt */
248 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
249 TRAP_x86_OFLOW = 4, /* Overflow exception */
250 TRAP_x86_BOUND = 5, /* Bound range exception */
251 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
252 TRAP_x86_DNA = 7, /* Device not available exception */
253 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
254 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
255 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
256 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
257 TRAP_x86_STKFLT = 12, /* Stack fault */
258 TRAP_x86_PROTFLT = 13, /* General protection fault */
259 TRAP_x86_PAGEFLT = 14, /* Page fault */
260 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
261 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
262 TRAP_x86_MCHK = 18, /* Machine check exception */
263 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
266 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
267 static size_t signal_stack_size;
269 typedef void (*raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
270 typedef int (*wine_signal_handler)(unsigned int sig);
272 static wine_signal_handler handlers[256];
275 /***********************************************************************
276 * Definitions for Win32 unwind tables
279 union handler_data
281 RUNTIME_FUNCTION chain;
282 ULONG handler;
285 struct opcode
287 BYTE offset;
288 BYTE code : 4;
289 BYTE info : 4;
292 struct UNWIND_INFO
294 BYTE version : 3;
295 BYTE flags : 5;
296 BYTE prolog;
297 BYTE count;
298 BYTE frame_reg : 4;
299 BYTE frame_offset : 4;
300 struct opcode opcodes[1]; /* info->count entries */
301 /* followed by handler_data */
304 #define UWOP_PUSH_NONVOL 0
305 #define UWOP_ALLOC_LARGE 1
306 #define UWOP_ALLOC_SMALL 2
307 #define UWOP_SET_FPREG 3
308 #define UWOP_SAVE_NONVOL 4
309 #define UWOP_SAVE_NONVOL_FAR 5
310 #define UWOP_SAVE_XMM128 8
311 #define UWOP_SAVE_XMM128_FAR 9
312 #define UWOP_PUSH_MACHFRAME 10
314 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
316 static const char * const reg_names[16] =
317 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
318 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
320 union handler_data *handler_data;
321 struct UNWIND_INFO *info;
322 unsigned int i, count;
324 TRACE( "**** func %x-%x\n", function->BeginAddress, function->EndAddress );
325 for (;;)
327 if (function->UnwindData & 1)
329 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
330 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
331 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
332 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
333 function = next;
334 continue;
336 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
338 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
339 info, info->flags, info->prolog,
340 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
342 if (info->frame_reg)
343 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
344 reg_names[info->frame_reg], info->frame_offset * 16 );
346 for (i = 0; i < info->count; i++)
348 TRACE( " 0x%x: ", info->opcodes[i].offset );
349 switch (info->opcodes[i].code)
351 case UWOP_PUSH_NONVOL:
352 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
353 break;
354 case UWOP_ALLOC_LARGE:
355 if (info->opcodes[i].info)
357 count = *(DWORD *)&info->opcodes[i+1];
358 i += 2;
360 else
362 count = *(USHORT *)&info->opcodes[i+1] * 8;
363 i++;
365 TRACE( "subq $0x%x,%%rsp\n", count );
366 break;
367 case UWOP_ALLOC_SMALL:
368 count = (info->opcodes[i].info + 1) * 8;
369 TRACE( "subq $0x%x,%%rsp\n", count );
370 break;
371 case UWOP_SET_FPREG:
372 TRACE( "leaq 0x%x(%%rsp),%s\n",
373 info->frame_offset * 16, reg_names[info->frame_reg] );
374 break;
375 case UWOP_SAVE_NONVOL:
376 count = *(USHORT *)&info->opcodes[i+1] * 8;
377 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
378 i++;
379 break;
380 case UWOP_SAVE_NONVOL_FAR:
381 count = *(DWORD *)&info->opcodes[i+1];
382 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
383 i += 2;
384 break;
385 case UWOP_SAVE_XMM128:
386 count = *(USHORT *)&info->opcodes[i+1] * 16;
387 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
388 i++;
389 break;
390 case UWOP_SAVE_XMM128_FAR:
391 count = *(DWORD *)&info->opcodes[i+1];
392 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
393 i += 2;
394 break;
395 case UWOP_PUSH_MACHFRAME:
396 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
397 break;
398 default:
399 FIXME( "unknown code %u\n", info->opcodes[i].code );
400 break;
404 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
405 if (info->flags & UNW_FLAG_CHAININFO)
407 TRACE( " chained to function %p-%p\n",
408 (char *)base + handler_data->chain.BeginAddress,
409 (char *)base + handler_data->chain.EndAddress );
410 function = &handler_data->chain;
411 continue;
413 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
414 TRACE( " handler %p data at %p\n",
415 (char *)base + handler_data->handler, &handler_data->handler + 1 );
416 break;
420 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
422 unsigned int i;
424 TRACE( "scope table at %p\n", table );
425 for (i = 0; i < table->Count; i++)
426 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
427 base + table->ScopeRecord[i].BeginAddress,
428 base + table->ScopeRecord[i].EndAddress,
429 base + table->ScopeRecord[i].HandlerAddress,
430 base + table->ScopeRecord[i].JumpTarget );
434 /***********************************************************************
435 * Definitions for Dwarf unwind tables
438 enum dwarf_call_frame_info
440 DW_CFA_advance_loc = 0x40,
441 DW_CFA_offset = 0x80,
442 DW_CFA_restore = 0xc0,
443 DW_CFA_nop = 0x00,
444 DW_CFA_set_loc = 0x01,
445 DW_CFA_advance_loc1 = 0x02,
446 DW_CFA_advance_loc2 = 0x03,
447 DW_CFA_advance_loc4 = 0x04,
448 DW_CFA_offset_extended = 0x05,
449 DW_CFA_restore_extended = 0x06,
450 DW_CFA_undefined = 0x07,
451 DW_CFA_same_value = 0x08,
452 DW_CFA_register = 0x09,
453 DW_CFA_remember_state = 0x0a,
454 DW_CFA_restore_state = 0x0b,
455 DW_CFA_def_cfa = 0x0c,
456 DW_CFA_def_cfa_register = 0x0d,
457 DW_CFA_def_cfa_offset = 0x0e,
458 DW_CFA_def_cfa_expression = 0x0f,
459 DW_CFA_expression = 0x10,
460 DW_CFA_offset_extended_sf = 0x11,
461 DW_CFA_def_cfa_sf = 0x12,
462 DW_CFA_def_cfa_offset_sf = 0x13,
463 DW_CFA_val_offset = 0x14,
464 DW_CFA_val_offset_sf = 0x15,
465 DW_CFA_val_expression = 0x16,
468 enum dwarf_operation
470 DW_OP_addr = 0x03,
471 DW_OP_deref = 0x06,
472 DW_OP_const1u = 0x08,
473 DW_OP_const1s = 0x09,
474 DW_OP_const2u = 0x0a,
475 DW_OP_const2s = 0x0b,
476 DW_OP_const4u = 0x0c,
477 DW_OP_const4s = 0x0d,
478 DW_OP_const8u = 0x0e,
479 DW_OP_const8s = 0x0f,
480 DW_OP_constu = 0x10,
481 DW_OP_consts = 0x11,
482 DW_OP_dup = 0x12,
483 DW_OP_drop = 0x13,
484 DW_OP_over = 0x14,
485 DW_OP_pick = 0x15,
486 DW_OP_swap = 0x16,
487 DW_OP_rot = 0x17,
488 DW_OP_xderef = 0x18,
489 DW_OP_abs = 0x19,
490 DW_OP_and = 0x1a,
491 DW_OP_div = 0x1b,
492 DW_OP_minus = 0x1c,
493 DW_OP_mod = 0x1d,
494 DW_OP_mul = 0x1e,
495 DW_OP_neg = 0x1f,
496 DW_OP_not = 0x20,
497 DW_OP_or = 0x21,
498 DW_OP_plus = 0x22,
499 DW_OP_plus_uconst = 0x23,
500 DW_OP_shl = 0x24,
501 DW_OP_shr = 0x25,
502 DW_OP_shra = 0x26,
503 DW_OP_xor = 0x27,
504 DW_OP_bra = 0x28,
505 DW_OP_eq = 0x29,
506 DW_OP_ge = 0x2a,
507 DW_OP_gt = 0x2b,
508 DW_OP_le = 0x2c,
509 DW_OP_lt = 0x2d,
510 DW_OP_ne = 0x2e,
511 DW_OP_skip = 0x2f,
512 DW_OP_lit0 = 0x30,
513 DW_OP_lit1 = 0x31,
514 DW_OP_lit2 = 0x32,
515 DW_OP_lit3 = 0x33,
516 DW_OP_lit4 = 0x34,
517 DW_OP_lit5 = 0x35,
518 DW_OP_lit6 = 0x36,
519 DW_OP_lit7 = 0x37,
520 DW_OP_lit8 = 0x38,
521 DW_OP_lit9 = 0x39,
522 DW_OP_lit10 = 0x3a,
523 DW_OP_lit11 = 0x3b,
524 DW_OP_lit12 = 0x3c,
525 DW_OP_lit13 = 0x3d,
526 DW_OP_lit14 = 0x3e,
527 DW_OP_lit15 = 0x3f,
528 DW_OP_lit16 = 0x40,
529 DW_OP_lit17 = 0x41,
530 DW_OP_lit18 = 0x42,
531 DW_OP_lit19 = 0x43,
532 DW_OP_lit20 = 0x44,
533 DW_OP_lit21 = 0x45,
534 DW_OP_lit22 = 0x46,
535 DW_OP_lit23 = 0x47,
536 DW_OP_lit24 = 0x48,
537 DW_OP_lit25 = 0x49,
538 DW_OP_lit26 = 0x4a,
539 DW_OP_lit27 = 0x4b,
540 DW_OP_lit28 = 0x4c,
541 DW_OP_lit29 = 0x4d,
542 DW_OP_lit30 = 0x4e,
543 DW_OP_lit31 = 0x4f,
544 DW_OP_reg0 = 0x50,
545 DW_OP_reg1 = 0x51,
546 DW_OP_reg2 = 0x52,
547 DW_OP_reg3 = 0x53,
548 DW_OP_reg4 = 0x54,
549 DW_OP_reg5 = 0x55,
550 DW_OP_reg6 = 0x56,
551 DW_OP_reg7 = 0x57,
552 DW_OP_reg8 = 0x58,
553 DW_OP_reg9 = 0x59,
554 DW_OP_reg10 = 0x5a,
555 DW_OP_reg11 = 0x5b,
556 DW_OP_reg12 = 0x5c,
557 DW_OP_reg13 = 0x5d,
558 DW_OP_reg14 = 0x5e,
559 DW_OP_reg15 = 0x5f,
560 DW_OP_reg16 = 0x60,
561 DW_OP_reg17 = 0x61,
562 DW_OP_reg18 = 0x62,
563 DW_OP_reg19 = 0x63,
564 DW_OP_reg20 = 0x64,
565 DW_OP_reg21 = 0x65,
566 DW_OP_reg22 = 0x66,
567 DW_OP_reg23 = 0x67,
568 DW_OP_reg24 = 0x68,
569 DW_OP_reg25 = 0x69,
570 DW_OP_reg26 = 0x6a,
571 DW_OP_reg27 = 0x6b,
572 DW_OP_reg28 = 0x6c,
573 DW_OP_reg29 = 0x6d,
574 DW_OP_reg30 = 0x6e,
575 DW_OP_reg31 = 0x6f,
576 DW_OP_breg0 = 0x70,
577 DW_OP_breg1 = 0x71,
578 DW_OP_breg2 = 0x72,
579 DW_OP_breg3 = 0x73,
580 DW_OP_breg4 = 0x74,
581 DW_OP_breg5 = 0x75,
582 DW_OP_breg6 = 0x76,
583 DW_OP_breg7 = 0x77,
584 DW_OP_breg8 = 0x78,
585 DW_OP_breg9 = 0x79,
586 DW_OP_breg10 = 0x7a,
587 DW_OP_breg11 = 0x7b,
588 DW_OP_breg12 = 0x7c,
589 DW_OP_breg13 = 0x7d,
590 DW_OP_breg14 = 0x7e,
591 DW_OP_breg15 = 0x7f,
592 DW_OP_breg16 = 0x80,
593 DW_OP_breg17 = 0x81,
594 DW_OP_breg18 = 0x82,
595 DW_OP_breg19 = 0x83,
596 DW_OP_breg20 = 0x84,
597 DW_OP_breg21 = 0x85,
598 DW_OP_breg22 = 0x86,
599 DW_OP_breg23 = 0x87,
600 DW_OP_breg24 = 0x88,
601 DW_OP_breg25 = 0x89,
602 DW_OP_breg26 = 0x8a,
603 DW_OP_breg27 = 0x8b,
604 DW_OP_breg28 = 0x8c,
605 DW_OP_breg29 = 0x8d,
606 DW_OP_breg30 = 0x8e,
607 DW_OP_breg31 = 0x8f,
608 DW_OP_regx = 0x90,
609 DW_OP_fbreg = 0x91,
610 DW_OP_bregx = 0x92,
611 DW_OP_piece = 0x93,
612 DW_OP_deref_size = 0x94,
613 DW_OP_xderef_size = 0x95,
614 DW_OP_nop = 0x96,
615 DW_OP_push_object_address = 0x97,
616 DW_OP_call2 = 0x98,
617 DW_OP_call4 = 0x99,
618 DW_OP_call_ref = 0x9a,
619 DW_OP_form_tls_address = 0x9b,
620 DW_OP_call_frame_cfa = 0x9c,
621 DW_OP_bit_piece = 0x9d,
622 DW_OP_lo_user = 0xe0,
623 DW_OP_hi_user = 0xff,
624 DW_OP_GNU_push_tls_address = 0xe0,
625 DW_OP_GNU_uninit = 0xf0,
626 DW_OP_GNU_encoded_addr = 0xf1,
629 #define DW_EH_PE_native 0x00
630 #define DW_EH_PE_leb128 0x01
631 #define DW_EH_PE_data2 0x02
632 #define DW_EH_PE_data4 0x03
633 #define DW_EH_PE_data8 0x04
634 #define DW_EH_PE_signed 0x08
635 #define DW_EH_PE_abs 0x00
636 #define DW_EH_PE_pcrel 0x10
637 #define DW_EH_PE_textrel 0x20
638 #define DW_EH_PE_datarel 0x30
639 #define DW_EH_PE_funcrel 0x40
640 #define DW_EH_PE_aligned 0x50
641 #define DW_EH_PE_indirect 0x80
642 #define DW_EH_PE_omit 0xff
644 struct dwarf_eh_bases
646 void *tbase;
647 void *dbase;
648 void *func;
651 struct dwarf_cie
653 unsigned int length;
654 int id;
655 unsigned char version;
656 unsigned char augmentation[1];
659 struct dwarf_fde
661 unsigned int length;
662 unsigned int cie_offset;
665 extern const struct dwarf_fde *_Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
667 static unsigned char dwarf_get_u1( const unsigned char **p )
669 return *(*p)++;
672 static unsigned short dwarf_get_u2( const unsigned char **p )
674 unsigned int ret = (*p)[0] | ((*p)[1] << 8);
675 (*p) += 2;
676 return ret;
679 static unsigned int dwarf_get_u4( const unsigned char **p )
681 unsigned int ret = (*p)[0] | ((*p)[1] << 8) | ((*p)[2] << 16) | ((*p)[3] << 24);
682 (*p) += 4;
683 return ret;
686 static ULONG64 dwarf_get_u8( const unsigned char **p )
688 ULONG64 low = dwarf_get_u4( p );
689 ULONG64 high = dwarf_get_u4( p );
690 return low | (high << 32);
693 static ULONG_PTR dwarf_get_uleb128( const unsigned char **p )
695 ULONG_PTR ret = 0;
696 unsigned int shift = 0;
697 unsigned char byte;
701 byte = **p;
702 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
703 shift += 7;
704 (*p)++;
705 } while (byte & 0x80);
706 return ret;
709 static LONG_PTR dwarf_get_sleb128( const unsigned char **p )
711 ULONG_PTR ret = 0;
712 unsigned int shift = 0;
713 unsigned char byte;
717 byte = **p;
718 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
719 shift += 7;
720 (*p)++;
721 } while (byte & 0x80);
723 if ((shift < 8 * sizeof(ret)) && (byte & 0x40)) ret |= -((ULONG_PTR)1 << shift);
724 return ret;
727 static ULONG_PTR dwarf_get_ptr( const unsigned char **p, unsigned char encoding )
729 ULONG_PTR base;
731 if (encoding == DW_EH_PE_omit) return 0;
733 switch (encoding & 0xf0)
735 case DW_EH_PE_abs:
736 base = 0;
737 break;
738 case DW_EH_PE_pcrel:
739 base = (ULONG_PTR)*p;
740 break;
741 default:
742 FIXME( "unsupported encoding %02x\n", encoding );
743 return 0;
746 switch (encoding & 0x0f)
748 case DW_EH_PE_native:
749 return base + dwarf_get_u8( p );
750 case DW_EH_PE_leb128:
751 return base + dwarf_get_uleb128( p );
752 case DW_EH_PE_data2:
753 return base + dwarf_get_u2( p );
754 case DW_EH_PE_data4:
755 return base + dwarf_get_u4( p );
756 case DW_EH_PE_data8:
757 return base + dwarf_get_u8( p );
758 case DW_EH_PE_signed|DW_EH_PE_leb128:
759 return base + dwarf_get_sleb128( p );
760 case DW_EH_PE_signed|DW_EH_PE_data2:
761 return base + (signed short)dwarf_get_u2( p );
762 case DW_EH_PE_signed|DW_EH_PE_data4:
763 return base + (signed int)dwarf_get_u4( p );
764 case DW_EH_PE_signed|DW_EH_PE_data8:
765 return base + (LONG64)dwarf_get_u8( p );
766 default:
767 FIXME( "unsupported encoding %02x\n", encoding );
768 return 0;
772 enum reg_rule
774 RULE_UNSET, /* not set at all */
775 RULE_UNDEFINED, /* undefined value */
776 RULE_SAME, /* same value as previous frame */
777 RULE_CFA_OFFSET, /* stored at cfa offset */
778 RULE_OTHER_REG, /* stored in other register */
779 RULE_EXPRESSION, /* address specified by expression */
780 RULE_VAL_EXPRESSION /* value specified by expression */
783 #define NB_FRAME_REGS 41
784 #define MAX_SAVED_STATES 16
786 struct frame_state
788 ULONG_PTR cfa_offset;
789 unsigned char cfa_reg;
790 enum reg_rule cfa_rule;
791 enum reg_rule rules[NB_FRAME_REGS];
792 ULONG64 regs[NB_FRAME_REGS];
795 struct frame_info
797 ULONG_PTR ip;
798 ULONG_PTR code_align;
799 LONG_PTR data_align;
800 unsigned char retaddr_reg;
801 unsigned char fde_encoding;
802 unsigned char signal_frame;
803 unsigned char state_sp;
804 struct frame_state state;
805 struct frame_state *state_stack;
808 static const char *dwarf_reg_names[NB_FRAME_REGS] =
810 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
811 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
812 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
813 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
814 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
817 static int valid_reg( ULONG_PTR reg )
819 if (reg >= NB_FRAME_REGS) FIXME( "unsupported reg %lx\n", reg );
820 return (reg < NB_FRAME_REGS);
823 static void execute_cfa_instructions( const unsigned char *ptr, const unsigned char *end,
824 ULONG_PTR last_ip, struct frame_info *info )
826 while (ptr < end && info->ip < last_ip + info->signal_frame)
828 enum dwarf_call_frame_info op = *ptr++;
830 if (op & 0xc0)
832 switch (op & 0xc0)
834 case DW_CFA_advance_loc:
836 ULONG_PTR offset = (op & 0x3f) * info->code_align;
837 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info->ip, offset );
838 info->ip += offset;
839 break;
841 case DW_CFA_offset:
843 ULONG_PTR reg = op & 0x3f;
844 LONG_PTR offset = dwarf_get_uleb128( &ptr ) * info->data_align;
845 if (!valid_reg( reg )) break;
846 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
847 info->state.regs[reg] = offset;
848 info->state.rules[reg] = RULE_CFA_OFFSET;
849 break;
851 case DW_CFA_restore:
853 ULONG_PTR reg = op & 0x3f;
854 if (!valid_reg( reg )) break;
855 TRACE( "%lx: DW_CFA_restore %s\n", info->ip, dwarf_reg_names[reg] );
856 info->state.rules[reg] = RULE_UNSET;
857 break;
861 else switch (op)
863 case DW_CFA_nop:
864 break;
865 case DW_CFA_set_loc:
867 ULONG_PTR loc = dwarf_get_ptr( &ptr, info->fde_encoding );
868 TRACE( "%lx: DW_CFA_set_loc %lx\n", info->ip, loc );
869 info->ip = loc;
870 break;
872 case DW_CFA_advance_loc1:
874 ULONG_PTR offset = *ptr++ * info->code_align;
875 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset );
876 info->ip += offset;
877 break;
879 case DW_CFA_advance_loc2:
881 ULONG_PTR offset = dwarf_get_u2( &ptr ) * info->code_align;
882 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset );
883 info->ip += offset;
884 break;
886 case DW_CFA_advance_loc4:
888 ULONG_PTR offset = dwarf_get_u4( &ptr ) * info->code_align;
889 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset );
890 info->ip += offset;
891 break;
893 case DW_CFA_offset_extended:
894 case DW_CFA_offset_extended_sf:
896 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
897 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf_get_uleb128( &ptr ) * info->data_align
898 : dwarf_get_sleb128( &ptr ) * info->data_align;
899 if (!valid_reg( reg )) break;
900 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
901 info->state.regs[reg] = offset;
902 info->state.rules[reg] = RULE_CFA_OFFSET;
903 break;
905 case DW_CFA_restore_extended:
907 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
908 if (!valid_reg( reg )) break;
909 TRACE( "%lx: DW_CFA_restore_extended %s\n", info->ip, dwarf_reg_names[reg] );
910 info->state.rules[reg] = RULE_UNSET;
911 break;
913 case DW_CFA_undefined:
915 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
916 if (!valid_reg( reg )) break;
917 TRACE( "%lx: DW_CFA_undefined %s\n", info->ip, dwarf_reg_names[reg] );
918 info->state.rules[reg] = RULE_UNDEFINED;
919 break;
921 case DW_CFA_same_value:
923 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
924 if (!valid_reg( reg )) break;
925 TRACE( "%lx: DW_CFA_same_value %s\n", info->ip, dwarf_reg_names[reg] );
926 info->state.regs[reg] = reg;
927 info->state.rules[reg] = RULE_SAME;
928 break;
930 case DW_CFA_register:
932 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
933 ULONG_PTR reg2 = dwarf_get_uleb128( &ptr );
934 if (!valid_reg( reg ) || !valid_reg( reg2 )) break;
935 TRACE( "%lx: DW_CFA_register %s == %s\n", info->ip, dwarf_reg_names[reg], dwarf_reg_names[reg2] );
936 info->state.regs[reg] = reg2;
937 info->state.rules[reg] = RULE_OTHER_REG;
938 break;
940 case DW_CFA_remember_state:
941 TRACE( "%lx: DW_CFA_remember_state\n", info->ip );
942 if (info->state_sp >= MAX_SAVED_STATES)
943 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info->ip );
944 else
945 info->state_stack[info->state_sp++] = info->state;
946 break;
947 case DW_CFA_restore_state:
948 TRACE( "%lx: DW_CFA_restore_state\n", info->ip );
949 if (!info->state_sp)
950 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info->ip );
951 else
952 info->state = info->state_stack[--info->state_sp];
953 break;
954 case DW_CFA_def_cfa:
955 case DW_CFA_def_cfa_sf:
957 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
958 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf_get_uleb128( &ptr )
959 : dwarf_get_sleb128( &ptr ) * info->data_align;
960 if (!valid_reg( reg )) break;
961 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info->ip, dwarf_reg_names[reg], offset );
962 info->state.cfa_reg = reg;
963 info->state.cfa_offset = offset;
964 info->state.cfa_rule = RULE_CFA_OFFSET;
965 break;
967 case DW_CFA_def_cfa_register:
969 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
970 if (!valid_reg( reg )) break;
971 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info->ip, dwarf_reg_names[reg] );
972 info->state.cfa_reg = reg;
973 info->state.cfa_rule = RULE_CFA_OFFSET;
974 break;
976 case DW_CFA_def_cfa_offset:
977 case DW_CFA_def_cfa_offset_sf:
979 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf_get_uleb128( &ptr )
980 : dwarf_get_sleb128( &ptr ) * info->data_align;
981 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset );
982 info->state.cfa_offset = offset;
983 info->state.cfa_rule = RULE_CFA_OFFSET;
984 break;
986 case DW_CFA_def_cfa_expression:
988 ULONG_PTR expr = (ULONG_PTR)ptr;
989 ULONG_PTR len = dwarf_get_uleb128( &ptr );
990 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len );
991 info->state.cfa_offset = expr;
992 info->state.cfa_rule = RULE_VAL_EXPRESSION;
993 ptr += len;
994 break;
996 case DW_CFA_expression:
997 case DW_CFA_val_expression:
999 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
1000 ULONG_PTR expr = (ULONG_PTR)ptr;
1001 ULONG_PTR len = dwarf_get_uleb128( &ptr );
1002 if (!valid_reg( reg )) break;
1003 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
1004 info->ip, (op == DW_CFA_expression) ? "" : "val_", dwarf_reg_names[reg], expr, expr+len );
1005 info->state.regs[reg] = expr;
1006 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
1007 ptr += len;
1008 break;
1010 default:
1011 FIXME( "%lx: unknown CFA opcode %02x\n", info->ip, op );
1012 break;
1017 /* retrieve a context register from its dwarf number */
1018 static void *get_context_reg( CONTEXT *context, ULONG_PTR dw_reg )
1020 switch (dw_reg)
1022 case 0: return &context->Rax;
1023 case 1: return &context->Rdx;
1024 case 2: return &context->Rcx;
1025 case 3: return &context->Rbx;
1026 case 4: return &context->Rsi;
1027 case 5: return &context->Rdi;
1028 case 6: return &context->Rbp;
1029 case 7: return &context->Rsp;
1030 case 8: return &context->R8;
1031 case 9: return &context->R9;
1032 case 10: return &context->R10;
1033 case 11: return &context->R11;
1034 case 12: return &context->R12;
1035 case 13: return &context->R13;
1036 case 14: return &context->R14;
1037 case 15: return &context->R15;
1038 case 16: return &context->Rip;
1039 case 17: return &context->u.s.Xmm0;
1040 case 18: return &context->u.s.Xmm1;
1041 case 19: return &context->u.s.Xmm2;
1042 case 20: return &context->u.s.Xmm3;
1043 case 21: return &context->u.s.Xmm4;
1044 case 22: return &context->u.s.Xmm5;
1045 case 23: return &context->u.s.Xmm6;
1046 case 24: return &context->u.s.Xmm7;
1047 case 25: return &context->u.s.Xmm8;
1048 case 26: return &context->u.s.Xmm9;
1049 case 27: return &context->u.s.Xmm10;
1050 case 28: return &context->u.s.Xmm11;
1051 case 29: return &context->u.s.Xmm12;
1052 case 30: return &context->u.s.Xmm13;
1053 case 31: return &context->u.s.Xmm14;
1054 case 32: return &context->u.s.Xmm15;
1055 case 33: return &context->u.s.Legacy[0];
1056 case 34: return &context->u.s.Legacy[1];
1057 case 35: return &context->u.s.Legacy[2];
1058 case 36: return &context->u.s.Legacy[3];
1059 case 37: return &context->u.s.Legacy[4];
1060 case 38: return &context->u.s.Legacy[5];
1061 case 39: return &context->u.s.Legacy[6];
1062 case 40: return &context->u.s.Legacy[7];
1063 default: return NULL;
1067 /* set a context register from its dwarf number */
1068 static void set_context_reg( CONTEXT *context, ULONG_PTR dw_reg, void *val )
1070 switch (dw_reg)
1072 case 0: context->Rax = *(ULONG64 *)val; break;
1073 case 1: context->Rdx = *(ULONG64 *)val; break;
1074 case 2: context->Rcx = *(ULONG64 *)val; break;
1075 case 3: context->Rbx = *(ULONG64 *)val; break;
1076 case 4: context->Rsi = *(ULONG64 *)val; break;
1077 case 5: context->Rdi = *(ULONG64 *)val; break;
1078 case 6: context->Rbp = *(ULONG64 *)val; break;
1079 case 7: context->Rsp = *(ULONG64 *)val; break;
1080 case 8: context->R8 = *(ULONG64 *)val; break;
1081 case 9: context->R9 = *(ULONG64 *)val; break;
1082 case 10: context->R10 = *(ULONG64 *)val; break;
1083 case 11: context->R11 = *(ULONG64 *)val; break;
1084 case 12: context->R12 = *(ULONG64 *)val; break;
1085 case 13: context->R13 = *(ULONG64 *)val; break;
1086 case 14: context->R14 = *(ULONG64 *)val; break;
1087 case 15: context->R15 = *(ULONG64 *)val; break;
1088 case 16: context->Rip = *(ULONG64 *)val; break;
1089 case 17: context->u.s.Xmm0 = *(M128A *)val; break;
1090 case 18: context->u.s.Xmm1 = *(M128A *)val; break;
1091 case 19: context->u.s.Xmm2 = *(M128A *)val; break;
1092 case 20: context->u.s.Xmm3 = *(M128A *)val; break;
1093 case 21: context->u.s.Xmm4 = *(M128A *)val; break;
1094 case 22: context->u.s.Xmm5 = *(M128A *)val; break;
1095 case 23: context->u.s.Xmm6 = *(M128A *)val; break;
1096 case 24: context->u.s.Xmm7 = *(M128A *)val; break;
1097 case 25: context->u.s.Xmm8 = *(M128A *)val; break;
1098 case 26: context->u.s.Xmm9 = *(M128A *)val; break;
1099 case 27: context->u.s.Xmm10 = *(M128A *)val; break;
1100 case 28: context->u.s.Xmm11 = *(M128A *)val; break;
1101 case 29: context->u.s.Xmm12 = *(M128A *)val; break;
1102 case 30: context->u.s.Xmm13 = *(M128A *)val; break;
1103 case 31: context->u.s.Xmm14 = *(M128A *)val; break;
1104 case 32: context->u.s.Xmm15 = *(M128A *)val; break;
1105 case 33: context->u.s.Legacy[0] = *(M128A *)val; break;
1106 case 34: context->u.s.Legacy[1] = *(M128A *)val; break;
1107 case 35: context->u.s.Legacy[2] = *(M128A *)val; break;
1108 case 36: context->u.s.Legacy[3] = *(M128A *)val; break;
1109 case 37: context->u.s.Legacy[4] = *(M128A *)val; break;
1110 case 38: context->u.s.Legacy[5] = *(M128A *)val; break;
1111 case 39: context->u.s.Legacy[6] = *(M128A *)val; break;
1112 case 40: context->u.s.Legacy[7] = *(M128A *)val; break;
1116 static ULONG_PTR eval_expression( const unsigned char *p, CONTEXT *context )
1118 ULONG_PTR reg, tmp, stack[64];
1119 int sp = -1;
1120 ULONG_PTR len = dwarf_get_uleb128(&p);
1121 const unsigned char *end = p + len;
1123 while (p < end)
1125 unsigned char opcode = dwarf_get_u1(&p);
1127 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
1128 stack[++sp] = opcode - DW_OP_lit0;
1129 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
1130 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_reg0 );
1131 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
1132 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_breg0 ) + dwarf_get_sleb128(&p);
1133 else switch (opcode)
1135 case DW_OP_nop: break;
1136 case DW_OP_addr: stack[++sp] = dwarf_get_u8(&p); break;
1137 case DW_OP_const1u: stack[++sp] = dwarf_get_u1(&p); break;
1138 case DW_OP_const1s: stack[++sp] = (signed char)dwarf_get_u1(&p); break;
1139 case DW_OP_const2u: stack[++sp] = dwarf_get_u2(&p); break;
1140 case DW_OP_const2s: stack[++sp] = (short)dwarf_get_u2(&p); break;
1141 case DW_OP_const4u: stack[++sp] = dwarf_get_u4(&p); break;
1142 case DW_OP_const4s: stack[++sp] = (signed int)dwarf_get_u4(&p); break;
1143 case DW_OP_const8u: stack[++sp] = dwarf_get_u8(&p); break;
1144 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf_get_u8(&p); break;
1145 case DW_OP_constu: stack[++sp] = dwarf_get_uleb128(&p); break;
1146 case DW_OP_consts: stack[++sp] = dwarf_get_sleb128(&p); break;
1147 case DW_OP_deref: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1148 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
1149 case DW_OP_drop: sp--; break;
1150 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
1151 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf_get_u1(&p)]; sp++; break;
1152 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
1153 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
1154 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
1155 case DW_OP_neg: stack[sp] = -stack[sp]; break;
1156 case DW_OP_not: stack[sp] = ~stack[sp]; break;
1157 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
1158 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
1159 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
1160 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
1161 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
1162 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
1163 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
1164 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
1165 case DW_OP_plus_uconst: stack[sp] += dwarf_get_uleb128(&p); break;
1166 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
1167 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
1168 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
1169 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
1170 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
1171 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
1172 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
1173 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
1174 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
1175 case DW_OP_skip: tmp = (short)dwarf_get_u2(&p); p += tmp; break;
1176 case DW_OP_bra: tmp = (short)dwarf_get_u2(&p); if (!stack[sp--]) p += tmp; break;
1177 case DW_OP_GNU_encoded_addr: tmp = *p++; stack[++sp] = dwarf_get_ptr( &p, tmp ); break;
1178 case DW_OP_regx: stack[++sp] = *(ULONG_PTR *)get_context_reg( context, dwarf_get_uleb128(&p) ); break;
1179 case DW_OP_bregx:
1180 reg = dwarf_get_uleb128(&p);
1181 tmp = dwarf_get_sleb128(&p);
1182 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, reg ) + tmp;
1183 break;
1184 case DW_OP_deref_size:
1185 switch (*p++)
1187 case 1: stack[sp] = *(unsigned char *)stack[sp]; break;
1188 case 2: stack[sp] = *(unsigned short *)stack[sp]; break;
1189 case 4: stack[sp] = *(unsigned int *)stack[sp]; break;
1190 case 8: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1192 break;
1193 default:
1194 FIXME( "unhandled opcode %02x\n", opcode );
1197 return stack[sp];
1200 /* apply the computed frame info to the actual context */
1201 static void apply_frame_state( CONTEXT *context, struct frame_state *state )
1203 unsigned int i;
1204 ULONG_PTR cfa, value;
1205 CONTEXT new_context = *context;
1207 switch (state->cfa_rule)
1209 case RULE_EXPRESSION:
1210 cfa = *(ULONG_PTR *)eval_expression( (const unsigned char *)state->cfa_offset, context );
1211 break;
1212 case RULE_VAL_EXPRESSION:
1213 cfa = eval_expression( (const unsigned char *)state->cfa_offset, context );
1214 break;
1215 default:
1216 cfa = *(ULONG_PTR *)get_context_reg( context, state->cfa_reg ) + state->cfa_offset;
1217 break;
1219 if (!cfa) return;
1221 for (i = 0; i < NB_FRAME_REGS; i++)
1223 switch (state->rules[i])
1225 case RULE_UNSET:
1226 case RULE_UNDEFINED:
1227 case RULE_SAME:
1228 break;
1229 case RULE_CFA_OFFSET:
1230 set_context_reg( &new_context, i, (char *)cfa + state->regs[i] );
1231 break;
1232 case RULE_OTHER_REG:
1233 set_context_reg( &new_context, i, get_context_reg( context, state->regs[i] ));
1234 break;
1235 case RULE_EXPRESSION:
1236 value = eval_expression( (const unsigned char *)state->regs[i], context );
1237 set_context_reg( &new_context, i, (void *)value );
1238 break;
1239 case RULE_VAL_EXPRESSION:
1240 value = eval_expression( (const unsigned char *)state->regs[i], context );
1241 set_context_reg( &new_context, i, &value );
1242 break;
1245 new_context.Rsp = cfa;
1246 *context = new_context;
1250 /***********************************************************************
1251 * dwarf_virtual_unwind
1253 * Equivalent of RtlVirtualUnwind for builtin modules.
1255 static NTSTATUS dwarf_virtual_unwind( ULONG64 ip, ULONG64 *frame,CONTEXT *context,
1256 const struct dwarf_fde *fde, const struct dwarf_eh_bases *bases,
1257 PEXCEPTION_ROUTINE *handler, void **handler_data )
1259 const struct dwarf_cie *cie;
1260 const unsigned char *ptr, *augmentation, *end;
1261 ULONG_PTR len, code_end;
1262 struct frame_info info;
1263 struct frame_state state_stack[MAX_SAVED_STATES];
1264 int aug_z_format = 0;
1265 unsigned char lsda_encoding = DW_EH_PE_omit;
1267 memset( &info, 0, sizeof(info) );
1268 info.state_stack = state_stack;
1269 info.ip = (ULONG_PTR)bases->func;
1270 *handler = NULL;
1272 cie = (const struct dwarf_cie *)((const char *)&fde->cie_offset - fde->cie_offset);
1274 /* parse the CIE first */
1276 if (cie->version != 1)
1278 FIXME( "unknown CIE version %u at %p\n", cie->version, cie );
1279 return STATUS_INVALID_DISPOSITION;
1281 ptr = cie->augmentation + strlen((const char *)cie->augmentation) + 1;
1283 info.code_align = dwarf_get_uleb128( &ptr );
1284 info.data_align = dwarf_get_sleb128( &ptr );
1285 info.retaddr_reg = *ptr++;
1286 info.state.cfa_rule = RULE_CFA_OFFSET;
1288 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1289 ip, bases->func, cie, cie->length, cie->id, cie->version, cie->augmentation,
1290 info.code_align, info.data_align, dwarf_reg_names[info.retaddr_reg] );
1292 end = NULL;
1293 for (augmentation = cie->augmentation; *augmentation; augmentation++)
1295 switch (*augmentation)
1297 case 'z':
1298 len = dwarf_get_uleb128( &ptr );
1299 end = ptr + len;
1300 aug_z_format = 1;
1301 continue;
1302 case 'L':
1303 lsda_encoding = *ptr++;
1304 continue;
1305 case 'P':
1307 unsigned char encoding = *ptr++;
1308 *handler = (void *)dwarf_get_ptr( &ptr, encoding );
1309 continue;
1311 case 'R':
1312 info.fde_encoding = *ptr++;
1313 continue;
1314 case 'S':
1315 info.signal_frame = 1;
1316 continue;
1318 FIXME( "unknown augmentation '%c'\n", *augmentation );
1319 if (!end) return STATUS_INVALID_DISPOSITION; /* cannot continue */
1320 break;
1322 if (end) ptr = end;
1324 end = (const unsigned char *)(&cie->length + 1) + cie->length;
1325 execute_cfa_instructions( ptr, end, ip, &info );
1327 ptr = (const unsigned char *)(fde + 1);
1328 info.ip = dwarf_get_ptr( &ptr, info.fde_encoding ); /* fde code start */
1329 code_end = info.ip + dwarf_get_ptr( &ptr, info.fde_encoding & 0x0f ); /* fde code length */
1331 if (aug_z_format) /* get length of augmentation data */
1333 len = dwarf_get_uleb128( &ptr );
1334 end = ptr + len;
1336 else end = NULL;
1338 *handler_data = (void *)dwarf_get_ptr( &ptr, lsda_encoding );
1339 if (end) ptr = end;
1341 end = (const unsigned char *)(&fde->length + 1) + fde->length;
1342 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1343 fde, fde->length, *handler, *handler_data, info.ip, code_end );
1344 execute_cfa_instructions( ptr, end, ip, &info );
1345 apply_frame_state( context, &info.state );
1346 *frame = context->Rsp;
1348 TRACE( "next function rip=%016lx\n", context->Rip );
1349 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1350 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1351 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1352 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1353 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1354 context->R8, context->R9, context->R10, context->R11 );
1355 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1356 context->R12, context->R13, context->R14, context->R15 );
1358 return STATUS_SUCCESS;
1362 /***********************************************************************
1363 * dispatch_signal
1365 static inline int dispatch_signal(unsigned int sig)
1367 if (handlers[sig] == NULL) return 0;
1368 return handlers[sig](sig);
1371 /***********************************************************************
1372 * get_signal_stack
1374 * Get the base of the signal stack for the current thread.
1376 static inline void *get_signal_stack(void)
1378 return (char *)NtCurrentTeb() + teb_size;
1381 /***********************************************************************
1382 * is_inside_signal_stack
1384 * Check if pointer is inside the signal stack.
1386 static inline int is_inside_signal_stack( void *ptr )
1388 return ((char *)ptr >= (char *)get_signal_stack() &&
1389 (char *)ptr < (char *)get_signal_stack() + signal_stack_size);
1392 /***********************************************************************
1393 * save_context
1395 * Set the register values from a sigcontext.
1397 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
1399 context->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
1400 context->Rax = RAX_sig(sigcontext);
1401 context->Rcx = RCX_sig(sigcontext);
1402 context->Rdx = RDX_sig(sigcontext);
1403 context->Rbx = RBX_sig(sigcontext);
1404 context->Rsp = RSP_sig(sigcontext);
1405 context->Rbp = RBP_sig(sigcontext);
1406 context->Rsi = RSI_sig(sigcontext);
1407 context->Rdi = RDI_sig(sigcontext);
1408 context->R8 = R8_sig(sigcontext);
1409 context->R9 = R9_sig(sigcontext);
1410 context->R10 = R10_sig(sigcontext);
1411 context->R11 = R11_sig(sigcontext);
1412 context->R12 = R12_sig(sigcontext);
1413 context->R13 = R13_sig(sigcontext);
1414 context->R14 = R14_sig(sigcontext);
1415 context->R15 = R15_sig(sigcontext);
1416 context->Rip = RIP_sig(sigcontext);
1417 context->SegCs = CS_sig(sigcontext);
1418 context->SegFs = FS_sig(sigcontext);
1419 context->SegGs = GS_sig(sigcontext);
1420 context->EFlags = EFL_sig(sigcontext);
1421 #ifdef DS_sig
1422 context->SegDs = DS_sig(sigcontext);
1423 #else
1424 __asm__("movw %%ds,%0" : "=m" (context->SegDs));
1425 #endif
1426 #ifdef ES_sig
1427 context->SegEs = ES_sig(sigcontext);
1428 #else
1429 __asm__("movw %%es,%0" : "=m" (context->SegEs));
1430 #endif
1431 #ifdef SS_sig
1432 context->SegSs = SS_sig(sigcontext);
1433 #else
1434 __asm__("movw %%ss,%0" : "=m" (context->SegSs));
1435 #endif
1436 if (FPU_sig(sigcontext))
1438 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1439 context->u.FltSave = *FPU_sig(sigcontext);
1440 context->MxCsr = context->u.FltSave.MxCsr;
1445 /***********************************************************************
1446 * restore_context
1448 * Build a sigcontext from the register values.
1450 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1452 RAX_sig(sigcontext) = context->Rax;
1453 RCX_sig(sigcontext) = context->Rcx;
1454 RDX_sig(sigcontext) = context->Rdx;
1455 RBX_sig(sigcontext) = context->Rbx;
1456 RSP_sig(sigcontext) = context->Rsp;
1457 RBP_sig(sigcontext) = context->Rbp;
1458 RSI_sig(sigcontext) = context->Rsi;
1459 RDI_sig(sigcontext) = context->Rdi;
1460 R8_sig(sigcontext) = context->R8;
1461 R9_sig(sigcontext) = context->R9;
1462 R10_sig(sigcontext) = context->R10;
1463 R11_sig(sigcontext) = context->R11;
1464 R12_sig(sigcontext) = context->R12;
1465 R13_sig(sigcontext) = context->R13;
1466 R14_sig(sigcontext) = context->R14;
1467 R15_sig(sigcontext) = context->R15;
1468 RIP_sig(sigcontext) = context->Rip;
1469 CS_sig(sigcontext) = context->SegCs;
1470 FS_sig(sigcontext) = context->SegFs;
1471 GS_sig(sigcontext) = context->SegGs;
1472 EFL_sig(sigcontext) = context->EFlags;
1473 #ifdef DS_sig
1474 DS_sig(sigcontext) = context->SegDs;
1475 #endif
1476 #ifdef ES_sig
1477 ES_sig(sigcontext) = context->SegEs;
1478 #endif
1479 #ifdef SS_sig
1480 SS_sig(sigcontext) = context->SegSs;
1481 #endif
1482 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
1486 /***********************************************************************
1487 * RtlCaptureContext (NTDLL.@)
1489 __ASM_GLOBAL_FUNC( RtlCaptureContext,
1490 "pushfq\n\t"
1491 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1492 "movl $0x001000f,0x30(%rcx)\n\t" /* context->ContextFlags */
1493 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
1494 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
1495 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
1496 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
1497 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
1498 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
1499 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
1500 "popq 0x44(%rcx)\n\t" /* context->Eflags */
1501 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1502 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
1503 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
1504 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
1505 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
1506 "leaq 8(%rsp),%rax\n\t"
1507 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
1508 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
1509 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
1510 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
1511 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
1512 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
1513 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
1514 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
1515 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
1516 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
1517 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
1518 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
1519 "movq (%rsp),%rax\n\t"
1520 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
1521 "fxsave 0x100(%rcx)\n\t" /* context->FtlSave */
1522 "movdqa %xmm0,0x1a0(%rcx)\n\t" /* context->Xmm0 */
1523 "movdqa %xmm1,0x1b0(%rcx)\n\t" /* context->Xmm1 */
1524 "movdqa %xmm2,0x1c0(%rcx)\n\t" /* context->Xmm2 */
1525 "movdqa %xmm3,0x1d0(%rcx)\n\t" /* context->Xmm3 */
1526 "movdqa %xmm4,0x1e0(%rcx)\n\t" /* context->Xmm4 */
1527 "movdqa %xmm5,0x1f0(%rcx)\n\t" /* context->Xmm5 */
1528 "movdqa %xmm6,0x200(%rcx)\n\t" /* context->Xmm6 */
1529 "movdqa %xmm7,0x210(%rcx)\n\t" /* context->Xmm7 */
1530 "movdqa %xmm8,0x220(%rcx)\n\t" /* context->Xmm8 */
1531 "movdqa %xmm9,0x230(%rcx)\n\t" /* context->Xmm9 */
1532 "movdqa %xmm10,0x240(%rcx)\n\t" /* context->Xmm10 */
1533 "movdqa %xmm11,0x250(%rcx)\n\t" /* context->Xmm11 */
1534 "movdqa %xmm12,0x260(%rcx)\n\t" /* context->Xmm12 */
1535 "movdqa %xmm13,0x270(%rcx)\n\t" /* context->Xmm13 */
1536 "movdqa %xmm14,0x280(%rcx)\n\t" /* context->Xmm14 */
1537 "movdqa %xmm15,0x290(%rcx)\n\t" /* context->Xmm15 */
1538 "ret" );
1540 /***********************************************************************
1541 * set_cpu_context
1543 * Set the new CPU context.
1545 __ASM_GLOBAL_FUNC( set_cpu_context,
1546 "subq $40,%rsp\n\t"
1547 __ASM_CFI(".cfi_adjust_cfa_offset 40\n\t")
1548 "ldmxcsr 0x34(%rdi)\n\t" /* context->MxCsr */
1549 "movw 0x38(%rdi),%ax\n\t" /* context->SegCs */
1550 "movq %rax,8(%rsp)\n\t"
1551 "movw 0x42(%rdi),%ax\n\t" /* context->SegSs */
1552 "movq %rax,32(%rsp)\n\t"
1553 "movq 0x44(%rdi),%rax\n\t" /* context->Eflags */
1554 "movq %rax,16(%rsp)\n\t"
1555 "movq 0x80(%rdi),%rcx\n\t" /* context->Rcx */
1556 "movq 0x88(%rdi),%rdx\n\t" /* context->Rdx */
1557 "movq 0x90(%rdi),%rbx\n\t" /* context->Rbx */
1558 "movq 0x98(%rdi),%rax\n\t" /* context->Rsp */
1559 "movq %rax,24(%rsp)\n\t"
1560 "movq 0xa0(%rdi),%rbp\n\t" /* context->Rbp */
1561 "movq 0xa8(%rdi),%rsi\n\t" /* context->Rsi */
1562 "movq 0xb8(%rdi),%r8\n\t" /* context->R8 */
1563 "movq 0xc0(%rdi),%r9\n\t" /* context->R9 */
1564 "movq 0xc8(%rdi),%r10\n\t" /* context->R10 */
1565 "movq 0xd0(%rdi),%r11\n\t" /* context->R11 */
1566 "movq 0xd8(%rdi),%r12\n\t" /* context->R12 */
1567 "movq 0xe0(%rdi),%r13\n\t" /* context->R13 */
1568 "movq 0xe8(%rdi),%r14\n\t" /* context->R14 */
1569 "movq 0xf0(%rdi),%r15\n\t" /* context->R15 */
1570 "movq 0xf8(%rdi),%rax\n\t" /* context->Rip */
1571 "movq %rax,(%rsp)\n\t"
1572 "fxrstor 0x100(%rdi)\n\t" /* context->FtlSave */
1573 "movdqa 0x1a0(%rdi),%xmm0\n\t" /* context->Xmm0 */
1574 "movdqa 0x1b0(%rdi),%xmm1\n\t" /* context->Xmm1 */
1575 "movdqa 0x1c0(%rdi),%xmm2\n\t" /* context->Xmm2 */
1576 "movdqa 0x1d0(%rdi),%xmm3\n\t" /* context->Xmm3 */
1577 "movdqa 0x1e0(%rdi),%xmm4\n\t" /* context->Xmm4 */
1578 "movdqa 0x1f0(%rdi),%xmm5\n\t" /* context->Xmm5 */
1579 "movdqa 0x200(%rdi),%xmm6\n\t" /* context->Xmm6 */
1580 "movdqa 0x210(%rdi),%xmm7\n\t" /* context->Xmm7 */
1581 "movdqa 0x220(%rdi),%xmm8\n\t" /* context->Xmm8 */
1582 "movdqa 0x230(%rdi),%xmm9\n\t" /* context->Xmm9 */
1583 "movdqa 0x240(%rdi),%xmm10\n\t" /* context->Xmm10 */
1584 "movdqa 0x250(%rdi),%xmm11\n\t" /* context->Xmm11 */
1585 "movdqa 0x260(%rdi),%xmm12\n\t" /* context->Xmm12 */
1586 "movdqa 0x270(%rdi),%xmm13\n\t" /* context->Xmm13 */
1587 "movdqa 0x280(%rdi),%xmm14\n\t" /* context->Xmm14 */
1588 "movdqa 0x290(%rdi),%xmm15\n\t" /* context->Xmm15 */
1589 "movq 0x78(%rdi),%rax\n\t" /* context->Rax */
1590 "movq 0xb0(%rdi),%rdi\n\t" /* context->Rdi */
1591 "iretq" );
1593 /***********************************************************************
1594 * copy_context
1596 * Copy a register context according to the flags.
1598 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1600 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
1601 if (flags & CONTEXT_CONTROL)
1603 to->Rbp = from->Rbp;
1604 to->Rip = from->Rip;
1605 to->Rsp = from->Rsp;
1606 to->SegCs = from->SegCs;
1607 to->SegSs = from->SegSs;
1608 to->EFlags = from->EFlags;
1610 if (flags & CONTEXT_INTEGER)
1612 to->Rax = from->Rax;
1613 to->Rcx = from->Rcx;
1614 to->Rdx = from->Rdx;
1615 to->Rbx = from->Rbx;
1616 to->Rsi = from->Rsi;
1617 to->Rdi = from->Rdi;
1618 to->R8 = from->R8;
1619 to->R9 = from->R9;
1620 to->R10 = from->R10;
1621 to->R11 = from->R11;
1622 to->R12 = from->R12;
1623 to->R13 = from->R13;
1624 to->R14 = from->R14;
1625 to->R15 = from->R15;
1627 if (flags & CONTEXT_SEGMENTS)
1629 to->SegDs = from->SegDs;
1630 to->SegEs = from->SegEs;
1631 to->SegFs = from->SegFs;
1632 to->SegGs = from->SegGs;
1634 if (flags & CONTEXT_FLOATING_POINT)
1636 to->MxCsr = from->MxCsr;
1637 to->u.FltSave = from->u.FltSave;
1639 if (flags & CONTEXT_DEBUG_REGISTERS)
1641 to->Dr0 = from->Dr0;
1642 to->Dr1 = from->Dr1;
1643 to->Dr2 = from->Dr2;
1644 to->Dr3 = from->Dr3;
1645 to->Dr6 = from->Dr6;
1646 to->Dr7 = from->Dr7;
1651 /***********************************************************************
1652 * context_to_server
1654 * Convert a register context to the server format.
1656 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1658 DWORD flags = from->ContextFlags & ~CONTEXT_AMD64; /* get rid of CPU id */
1660 memset( to, 0, sizeof(*to) );
1661 to->cpu = CPU_x86_64;
1663 if (flags & CONTEXT_CONTROL)
1665 to->flags |= SERVER_CTX_CONTROL;
1666 to->ctl.x86_64_regs.rbp = from->Rbp;
1667 to->ctl.x86_64_regs.rip = from->Rip;
1668 to->ctl.x86_64_regs.rsp = from->Rsp;
1669 to->ctl.x86_64_regs.cs = from->SegCs;
1670 to->ctl.x86_64_regs.ss = from->SegSs;
1671 to->ctl.x86_64_regs.flags = from->EFlags;
1673 if (flags & CONTEXT_INTEGER)
1675 to->flags |= SERVER_CTX_INTEGER;
1676 to->integer.x86_64_regs.rax = from->Rax;
1677 to->integer.x86_64_regs.rcx = from->Rcx;
1678 to->integer.x86_64_regs.rdx = from->Rdx;
1679 to->integer.x86_64_regs.rbx = from->Rbx;
1680 to->integer.x86_64_regs.rsi = from->Rsi;
1681 to->integer.x86_64_regs.rdi = from->Rdi;
1682 to->integer.x86_64_regs.r8 = from->R8;
1683 to->integer.x86_64_regs.r9 = from->R9;
1684 to->integer.x86_64_regs.r10 = from->R10;
1685 to->integer.x86_64_regs.r11 = from->R11;
1686 to->integer.x86_64_regs.r12 = from->R12;
1687 to->integer.x86_64_regs.r13 = from->R13;
1688 to->integer.x86_64_regs.r14 = from->R14;
1689 to->integer.x86_64_regs.r15 = from->R15;
1691 if (flags & CONTEXT_SEGMENTS)
1693 to->flags |= SERVER_CTX_SEGMENTS;
1694 to->seg.x86_64_regs.ds = from->SegDs;
1695 to->seg.x86_64_regs.es = from->SegEs;
1696 to->seg.x86_64_regs.fs = from->SegFs;
1697 to->seg.x86_64_regs.gs = from->SegGs;
1699 if (flags & CONTEXT_FLOATING_POINT)
1701 to->flags |= SERVER_CTX_FLOATING_POINT;
1702 memcpy( to->fp.x86_64_regs.fpregs, &from->u.FltSave, sizeof(to->fp.x86_64_regs.fpregs) );
1704 if (flags & CONTEXT_DEBUG_REGISTERS)
1706 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1707 to->debug.x86_64_regs.dr0 = from->Dr0;
1708 to->debug.x86_64_regs.dr1 = from->Dr1;
1709 to->debug.x86_64_regs.dr2 = from->Dr2;
1710 to->debug.x86_64_regs.dr3 = from->Dr3;
1711 to->debug.x86_64_regs.dr6 = from->Dr6;
1712 to->debug.x86_64_regs.dr7 = from->Dr7;
1714 return STATUS_SUCCESS;
1718 /***********************************************************************
1719 * context_from_server
1721 * Convert a register context from the server format.
1723 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1725 if (from->cpu != CPU_x86_64) return STATUS_INVALID_PARAMETER;
1727 to->ContextFlags = CONTEXT_AMD64;
1728 if (from->flags & SERVER_CTX_CONTROL)
1730 to->ContextFlags |= CONTEXT_CONTROL;
1731 to->Rbp = from->ctl.x86_64_regs.rbp;
1732 to->Rip = from->ctl.x86_64_regs.rip;
1733 to->Rsp = from->ctl.x86_64_regs.rsp;
1734 to->SegCs = from->ctl.x86_64_regs.cs;
1735 to->SegSs = from->ctl.x86_64_regs.ss;
1736 to->EFlags = from->ctl.x86_64_regs.flags;
1739 if (from->flags & SERVER_CTX_INTEGER)
1741 to->ContextFlags |= CONTEXT_INTEGER;
1742 to->Rax = from->integer.x86_64_regs.rax;
1743 to->Rcx = from->integer.x86_64_regs.rcx;
1744 to->Rdx = from->integer.x86_64_regs.rdx;
1745 to->Rbx = from->integer.x86_64_regs.rbx;
1746 to->Rsi = from->integer.x86_64_regs.rsi;
1747 to->Rdi = from->integer.x86_64_regs.rdi;
1748 to->R8 = from->integer.x86_64_regs.r8;
1749 to->R9 = from->integer.x86_64_regs.r9;
1750 to->R10 = from->integer.x86_64_regs.r10;
1751 to->R11 = from->integer.x86_64_regs.r11;
1752 to->R12 = from->integer.x86_64_regs.r12;
1753 to->R13 = from->integer.x86_64_regs.r13;
1754 to->R14 = from->integer.x86_64_regs.r14;
1755 to->R15 = from->integer.x86_64_regs.r15;
1757 if (from->flags & SERVER_CTX_SEGMENTS)
1759 to->ContextFlags |= CONTEXT_SEGMENTS;
1760 to->SegDs = from->seg.x86_64_regs.ds;
1761 to->SegEs = from->seg.x86_64_regs.es;
1762 to->SegFs = from->seg.x86_64_regs.fs;
1763 to->SegGs = from->seg.x86_64_regs.gs;
1765 if (from->flags & SERVER_CTX_FLOATING_POINT)
1767 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1768 memcpy( &to->u.FltSave, from->fp.x86_64_regs.fpregs, sizeof(from->fp.x86_64_regs.fpregs) );
1769 to->MxCsr = to->u.FltSave.MxCsr;
1771 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1773 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1774 to->Dr0 = from->debug.x86_64_regs.dr0;
1775 to->Dr1 = from->debug.x86_64_regs.dr1;
1776 to->Dr2 = from->debug.x86_64_regs.dr2;
1777 to->Dr3 = from->debug.x86_64_regs.dr3;
1778 to->Dr6 = from->debug.x86_64_regs.dr6;
1779 to->Dr7 = from->debug.x86_64_regs.dr7;
1781 return STATUS_SUCCESS;
1785 extern void raise_func_trampoline( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
1786 __ASM_GLOBAL_FUNC( raise_func_trampoline,
1787 __ASM_CFI(".cfi_signal_frame\n\t")
1788 __ASM_CFI(".cfi_def_cfa %rbp,144\n\t") /* red zone + rip + rbp */
1789 __ASM_CFI(".cfi_rel_offset %rip,8\n\t")
1790 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1791 "call *%rdx\n\t"
1792 "int $3")
1794 /***********************************************************************
1795 * setup_exception
1797 * Setup a proper stack frame for the raise function, and modify the
1798 * sigcontext so that the return from the signal handler will call
1799 * the raise function.
1801 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1803 struct stack_layout
1805 CONTEXT context;
1806 EXCEPTION_RECORD rec;
1807 ULONG64 rbp;
1808 ULONG64 rip;
1809 ULONG64 red_zone[16];
1810 } *stack;
1811 ULONG64 *rsp_ptr;
1812 DWORD exception_code = 0;
1814 stack = (struct stack_layout *)(RSP_sig(sigcontext) & ~15);
1816 /* stack sanity checks */
1818 if (is_inside_signal_stack( stack ))
1820 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1821 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1822 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1823 abort_thread(1);
1826 if (stack - 1 > stack || /* check for overflow in subtraction */
1827 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1828 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1830 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1831 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1832 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1834 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1836 /* stack overflow on last page, unrecoverable */
1837 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1838 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1839 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1840 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1841 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1842 abort_thread(1);
1844 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1846 /* stack access below stack limit, may be recoverable */
1847 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1848 else
1850 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1851 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1852 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1853 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1854 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1855 abort_thread(1);
1859 stack--; /* push the stack_layout structure */
1860 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1861 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1862 #elif defined(VALGRIND_MAKE_WRITABLE)
1863 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1864 #endif
1865 stack->rec.ExceptionRecord = NULL;
1866 stack->rec.ExceptionCode = exception_code;
1867 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1868 stack->rec.ExceptionAddress = (void *)RIP_sig(sigcontext);
1869 stack->rec.NumberParameters = 0;
1870 save_context( &stack->context, sigcontext );
1872 /* store return address and %rbp without aligning, so that the offset is fixed */
1873 rsp_ptr = (ULONG64 *)RSP_sig(sigcontext) - 16;
1874 *(--rsp_ptr) = RIP_sig(sigcontext);
1875 *(--rsp_ptr) = RBP_sig(sigcontext);
1877 /* now modify the sigcontext to return to the raise function */
1878 RIP_sig(sigcontext) = (ULONG_PTR)raise_func_trampoline;
1879 RDI_sig(sigcontext) = (ULONG_PTR)&stack->rec;
1880 RSI_sig(sigcontext) = (ULONG_PTR)&stack->context;
1881 RDX_sig(sigcontext) = (ULONG_PTR)func;
1882 RBP_sig(sigcontext) = (ULONG_PTR)rsp_ptr;
1883 RSP_sig(sigcontext) = (ULONG_PTR)stack;
1884 /* clear single-step, direction, and align check flag */
1885 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1887 return &stack->rec;
1891 /**********************************************************************
1892 * find_function_info
1894 static RUNTIME_FUNCTION *find_function_info( ULONG64 pc, HMODULE module,
1895 RUNTIME_FUNCTION *func, ULONG size )
1897 int min = 0;
1898 int max = size/sizeof(*func) - 1;
1900 while (min <= max)
1902 int pos = (min + max) / 2;
1903 if ((char *)pc < (char *)module + func[pos].BeginAddress) max = pos - 1;
1904 else if ((char *)pc >= (char *)module + func[pos].EndAddress) min = pos + 1;
1905 else
1907 func += pos;
1908 while (func->UnwindData & 1) /* follow chained entry */
1909 func = (RUNTIME_FUNCTION *)((char *)module + (func->UnwindData & ~1));
1910 return func;
1913 return NULL;
1917 /**********************************************************************
1918 * call_handler
1920 * Call a single exception handler.
1921 * FIXME: Handle nested exceptions.
1923 static NTSTATUS call_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch, CONTEXT *orig_context )
1925 DWORD res;
1927 dispatch->ControlPc = dispatch->ContextRecord->Rip;
1929 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
1930 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1931 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1932 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
1934 switch (res)
1936 case ExceptionContinueExecution:
1937 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1938 *orig_context = *dispatch->ContextRecord;
1939 return STATUS_SUCCESS;
1940 case ExceptionContinueSearch:
1941 break;
1942 case ExceptionNestedException:
1943 break;
1944 default:
1945 return STATUS_INVALID_DISPOSITION;
1947 return STATUS_UNHANDLED_EXCEPTION;
1951 /**********************************************************************
1952 * call_teb_handler
1954 * Call a single exception handler from the TEB chain.
1955 * FIXME: Handle nested exceptions.
1957 static NTSTATUS call_teb_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
1958 EXCEPTION_REGISTRATION_RECORD *teb_frame, CONTEXT *orig_context )
1960 EXCEPTION_REGISTRATION_RECORD *dispatcher;
1961 DWORD res;
1963 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
1964 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1965 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1966 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
1968 switch (res)
1970 case ExceptionContinueExecution:
1971 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1972 *orig_context = *dispatch->ContextRecord;
1973 return STATUS_SUCCESS;
1974 case ExceptionContinueSearch:
1975 break;
1976 case ExceptionNestedException:
1977 break;
1978 default:
1979 return STATUS_INVALID_DISPOSITION;
1981 return STATUS_UNHANDLED_EXCEPTION;
1985 /**********************************************************************
1986 * call_stack_handlers
1988 * Call the stack handlers chain.
1990 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
1992 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1993 UNWIND_HISTORY_TABLE table;
1994 DISPATCHER_CONTEXT dispatch;
1995 CONTEXT context, new_context;
1996 LDR_MODULE *module;
1997 DWORD size;
1998 NTSTATUS status;
2000 context = *orig_context;
2001 dispatch.TargetIp = 0;
2002 dispatch.ContextRecord = &context;
2003 dispatch.HistoryTable = &table;
2004 dispatch.ScopeIndex = 0; /* FIXME */
2005 for (;;)
2007 new_context = context;
2009 /* FIXME: should use the history table to make things faster */
2011 module = NULL;
2012 dispatch.ImageBase = 0;
2014 /* first look for PE exception information */
2016 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
2018 RUNTIME_FUNCTION *dir;
2020 dispatch.ImageBase = (ULONG64)module->BaseAddress;
2021 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2022 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2024 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
2025 dir, size )))
2027 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, dispatch.ImageBase,
2028 context.Rip, dispatch.FunctionEntry,
2029 &new_context, &dispatch.HandlerData,
2030 &dispatch.EstablisherFrame, NULL );
2031 goto unwind_done;
2034 else if (!(module->Flags & LDR_WINE_INTERNAL))
2035 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
2038 /* then look for host system exception information */
2040 if (!module || (module->Flags & LDR_WINE_INTERNAL))
2042 struct dwarf_eh_bases bases;
2043 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
2045 if (fde)
2047 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context,
2048 fde, &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
2049 if (status != STATUS_SUCCESS) return status;
2050 dispatch.FunctionEntry = NULL;
2051 if (dispatch.LanguageHandler && !module)
2053 FIXME( "calling personality routine in system library not supported yet\n" );
2054 dispatch.LanguageHandler = NULL;
2056 goto unwind_done;
2060 /* no exception information, treat as a leaf function */
2062 new_context.Rip = *(ULONG64 *)context.Rsp;
2063 new_context.Rsp = context.Rsp + sizeof(ULONG64);
2064 dispatch.EstablisherFrame = new_context.Rsp;
2065 dispatch.LanguageHandler = NULL;
2067 unwind_done:
2068 if (!dispatch.EstablisherFrame) break;
2070 if ((dispatch.EstablisherFrame & 7) ||
2071 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
2072 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
2074 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
2075 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2076 rec->ExceptionFlags |= EH_STACK_INVALID;
2077 break;
2080 if (dispatch.LanguageHandler)
2082 status = call_handler( rec, &dispatch, orig_context );
2083 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
2085 /* hack: call wine handlers registered in the tib list */
2086 else while ((ULONG64)teb_frame < new_context.Rsp)
2088 TRACE( "found wine frame %p rsp %lx handler %p\n",
2089 teb_frame, new_context.Rsp, teb_frame->Handler );
2090 dispatch.EstablisherFrame = (ULONG64)teb_frame;
2091 context = *orig_context;
2092 status = call_teb_handler( rec, &dispatch, teb_frame, orig_context );
2093 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
2094 teb_frame = teb_frame->Prev;
2097 if (new_context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
2098 context = new_context;
2100 return STATUS_UNHANDLED_EXCEPTION;
2104 /*******************************************************************
2105 * raise_exception
2107 * Implementation of NtRaiseException.
2109 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2111 NTSTATUS status;
2113 if (first_chance)
2115 DWORD c;
2117 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2118 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
2119 context->Rip, GetCurrentThreadId() );
2120 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
2121 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
2122 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
2124 if (rec->ExceptionInformation[1] >> 16)
2125 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2126 rec->ExceptionAddress,
2127 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
2128 else
2129 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2130 rec->ExceptionAddress,
2131 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
2133 else
2135 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2136 context->Rax, context->Rbx, context->Rcx, context->Rdx );
2137 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2138 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
2139 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2140 context->R8, context->R9, context->R10, context->R11 );
2141 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2142 context->R12, context->R13, context->R14, context->R15 );
2144 status = send_debug_event( rec, TRUE, context );
2145 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
2146 return STATUS_SUCCESS;
2148 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
2149 return STATUS_SUCCESS;
2151 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
2152 return status;
2155 /* last chance exception */
2157 status = send_debug_event( rec, FALSE, context );
2158 if (status != DBG_CONTINUE)
2160 if (rec->ExceptionFlags & EH_STACK_INVALID)
2161 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2162 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
2163 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2164 else
2165 ERR("Unhandled exception code %x flags %x addr %p\n",
2166 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
2167 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
2169 return STATUS_SUCCESS;
2173 /**********************************************************************
2174 * raise_segv_exception
2176 static void raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2178 NTSTATUS status;
2180 switch(rec->ExceptionCode)
2182 case EXCEPTION_ACCESS_VIOLATION:
2183 if (rec->NumberParameters == 2)
2185 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
2186 rec->ExceptionInformation[0] )))
2187 set_cpu_context( context );
2189 break;
2191 status = raise_exception( rec, context, TRUE );
2192 if (status) raise_status( status, rec );
2193 set_cpu_context( context );
2197 /**********************************************************************
2198 * raise_generic_exception
2200 * Generic raise function for exceptions that don't need special treatment.
2202 static void raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2204 NTSTATUS status = raise_exception( rec, context, TRUE );
2205 if (status) raise_status( status, rec );
2206 set_cpu_context( context );
2210 /**********************************************************************
2211 * segv_handler
2213 * Handler for SIGSEGV and related errors.
2215 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2217 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_segv_exception );
2218 ucontext_t *ucontext = sigcontext;
2220 switch(TRAP_sig(ucontext))
2222 case TRAP_x86_OFLOW: /* Overflow exception */
2223 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2224 break;
2225 case TRAP_x86_BOUND: /* Bound range exception */
2226 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2227 break;
2228 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2229 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2230 break;
2231 case TRAP_x86_STKFLT: /* Stack fault */
2232 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2233 break;
2234 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2235 case TRAP_x86_PROTFLT: /* General protection fault */
2236 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2237 rec->ExceptionCode = ERROR_sig(ucontext) ? EXCEPTION_ACCESS_VIOLATION : EXCEPTION_PRIV_INSTRUCTION;
2238 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2239 break;
2240 case TRAP_x86_PAGEFLT: /* Page fault */
2241 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2242 rec->NumberParameters = 2;
2243 rec->ExceptionInformation[0] = (ERROR_sig(ucontext) & 2) != 0;
2244 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2245 break;
2246 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2247 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2248 break;
2249 default:
2250 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext) );
2251 /* fall through */
2252 case TRAP_x86_NMI: /* NMI interrupt */
2253 case TRAP_x86_DNA: /* Device not available exception */
2254 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2255 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2256 case TRAP_x86_MCHK: /* Machine check exception */
2257 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2258 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2259 break;
2263 /**********************************************************************
2264 * trap_handler
2266 * Handler for SIGTRAP.
2268 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2270 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2272 switch (siginfo->si_code)
2274 case TRAP_TRACE: /* Single-step exception */
2275 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2276 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2277 break;
2278 case TRAP_BRKPT: /* Breakpoint exception */
2279 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2280 /* fall through */
2281 default:
2282 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2283 break;
2287 /**********************************************************************
2288 * fpe_handler
2290 * Handler for SIGFPE.
2292 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2294 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2296 switch (siginfo->si_code)
2298 case FPE_FLTSUB:
2299 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2300 break;
2301 case FPE_INTDIV:
2302 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2303 break;
2304 case FPE_INTOVF:
2305 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2306 break;
2307 case FPE_FLTDIV:
2308 rec->ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
2309 break;
2310 case FPE_FLTOVF:
2311 rec->ExceptionCode = EXCEPTION_FLT_OVERFLOW;
2312 break;
2313 case FPE_FLTUND:
2314 rec->ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
2315 break;
2316 case FPE_FLTRES:
2317 rec->ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
2318 break;
2319 case FPE_FLTINV:
2320 default:
2321 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2322 break;
2326 /**********************************************************************
2327 * int_handler
2329 * Handler for SIGINT.
2331 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2333 if (!dispatch_signal(SIGINT))
2335 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2336 rec->ExceptionCode = CONTROL_C_EXIT;
2341 /**********************************************************************
2342 * abrt_handler
2344 * Handler for SIGABRT.
2346 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2348 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2349 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2350 rec->ExceptionFlags = EH_NONCONTINUABLE;
2354 /**********************************************************************
2355 * quit_handler
2357 * Handler for SIGQUIT.
2359 static void quit_handler( int signal, siginfo_t *siginfo, void *ucontext )
2361 abort_thread(0);
2365 /**********************************************************************
2366 * usr1_handler
2368 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2370 static void usr1_handler( int signal, siginfo_t *siginfo, void *ucontext )
2372 CONTEXT context;
2374 save_context( &context, ucontext );
2375 wait_suspend( &context );
2376 restore_context( &context, ucontext );
2380 /***********************************************************************
2381 * __wine_set_signal_handler (NTDLL.@)
2383 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2385 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
2386 if (handlers[sig] != NULL) return -2;
2387 handlers[sig] = wsh;
2388 return 0;
2392 /**********************************************************************
2393 * signal_alloc_thread
2395 NTSTATUS signal_alloc_thread( TEB **teb )
2397 static size_t sigstack_zero_bits;
2398 SIZE_T size;
2399 NTSTATUS status;
2401 if (!sigstack_zero_bits)
2403 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2404 /* find the first power of two not smaller than min_size */
2405 sigstack_zero_bits = 12;
2406 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2407 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2408 assert( sizeof(TEB) <= teb_size );
2411 size = 1 << sigstack_zero_bits;
2412 *teb = NULL;
2413 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
2414 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2416 (*teb)->Tib.Self = &(*teb)->Tib;
2417 (*teb)->Tib.ExceptionList = (void *)~0UL;
2419 return status;
2423 /**********************************************************************
2424 * signal_free_thread
2426 void signal_free_thread( TEB *teb )
2428 SIZE_T size;
2430 if (teb->DeallocationStack)
2432 size = 0;
2433 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2435 size = 0;
2436 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2440 /**********************************************************************
2441 * signal_init_thread
2443 void signal_init_thread( TEB *teb )
2445 const WORD fpu_cw = 0x27f;
2446 stack_t ss;
2448 #if defined __linux__
2449 arch_prctl( ARCH_SET_GS, teb );
2450 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
2451 amd64_set_gsbase( teb );
2452 #else
2453 # error Please define setting %gs for your architecture
2454 #endif
2456 ss.ss_sp = (char *)teb + teb_size;
2457 ss.ss_size = signal_stack_size;
2458 ss.ss_flags = 0;
2459 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2461 #ifdef __GNUC__
2462 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2463 #else
2464 FIXME("FPU setup not implemented for this platform.\n");
2465 #endif
2468 /**********************************************************************
2469 * signal_init_process
2471 void signal_init_process(void)
2473 struct sigaction sig_act;
2475 sig_act.sa_mask = server_block_set;
2476 sig_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
2478 sig_act.sa_sigaction = int_handler;
2479 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2480 sig_act.sa_sigaction = fpe_handler;
2481 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2482 sig_act.sa_sigaction = abrt_handler;
2483 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2484 sig_act.sa_sigaction = quit_handler;
2485 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2486 sig_act.sa_sigaction = usr1_handler;
2487 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2489 sig_act.sa_sigaction = segv_handler;
2490 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2491 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2492 #ifdef SIGBUS
2493 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2494 #endif
2496 #ifdef SIGTRAP
2497 sig_act.sa_sigaction = trap_handler;
2498 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2499 #endif
2500 return;
2502 error:
2503 perror("sigaction");
2504 exit(1);
2508 /**********************************************************************
2509 * RtlAddFunctionTable (NTDLL.@)
2511 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD64 addr )
2513 FIXME( "%p %u %lx: stub\n", table, count, addr );
2514 return FALSE;
2518 /**********************************************************************
2519 * RtlDeleteFunctionTable (NTDLL.@)
2521 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
2523 FIXME( "%p: stub\n", table );
2524 return FALSE;
2528 /**********************************************************************
2529 * RtlLookupFunctionEntry (NTDLL.@)
2531 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base, UNWIND_HISTORY_TABLE *table )
2533 LDR_MODULE *module;
2534 RUNTIME_FUNCTION *func;
2535 ULONG size;
2537 /* FIXME: should use the history table to make things faster */
2539 if (LdrFindEntryForAddress( (void *)pc, &module ))
2541 WARN( "module not found for %lx\n", pc );
2542 return NULL;
2544 if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2545 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2547 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
2548 return NULL;
2550 func = find_function_info( pc, module->BaseAddress, func, size );
2551 if (func) *base = (ULONG64)module->BaseAddress;
2552 return func;
2555 static ULONG64 get_int_reg( CONTEXT *context, int reg )
2557 return *(&context->Rax + reg);
2560 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 val )
2562 *(&context->Rax + reg) = val;
2563 if (ctx_ptr) ctx_ptr->u2.IntegerContext[reg] = &context->Rax + reg;
2566 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A val )
2568 *(&context->u.s.Xmm0 + reg) = val;
2569 if (ctx_ptr) ctx_ptr->u1.FloatingContext[reg] = &context->u.s.Xmm0 + reg;
2572 static int get_opcode_size( struct opcode op )
2574 switch (op.code)
2576 case UWOP_ALLOC_LARGE:
2577 return 2 + (op.info != 0);
2578 case UWOP_SAVE_NONVOL:
2579 case UWOP_SAVE_XMM128:
2580 return 2;
2581 case UWOP_SAVE_NONVOL_FAR:
2582 case UWOP_SAVE_XMM128_FAR:
2583 return 3;
2584 default:
2585 return 1;
2589 static BOOL is_inside_epilog( BYTE *pc )
2591 /* add or lea must be the first instruction, and it must have a rex.W prefix */
2592 if ((pc[0] & 0xf8) == 0x48)
2594 switch (pc[1])
2596 case 0x81: /* add $nnnn,%rsp */
2597 if (pc[0] == 0x48 && pc[2] == 0xc4)
2599 pc += 7;
2600 break;
2602 return FALSE;
2603 case 0x83: /* add $n,%rsp */
2604 if (pc[0] == 0x48 && pc[2] == 0xc4)
2606 pc += 4;
2607 break;
2609 return FALSE;
2610 case 0x8d: /* lea n(reg),%rsp */
2611 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
2612 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
2613 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
2614 if ((pc[2] >> 6) == 1) /* 8-bit offset */
2616 pc += 4;
2617 break;
2619 if ((pc[2] >> 6) == 2) /* 32-bit offset */
2621 pc += 7;
2622 break;
2624 return FALSE;
2628 /* now check for various pop instructions */
2630 for (;;)
2632 BYTE rex = 0;
2634 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2636 switch (*pc)
2638 case 0x58: /* pop %rax/%r8 */
2639 case 0x59: /* pop %rcx/%r9 */
2640 case 0x5a: /* pop %rdx/%r10 */
2641 case 0x5b: /* pop %rbx/%r11 */
2642 case 0x5c: /* pop %rsp/%r12 */
2643 case 0x5d: /* pop %rbp/%r13 */
2644 case 0x5e: /* pop %rsi/%r14 */
2645 case 0x5f: /* pop %rdi/%r15 */
2646 pc++;
2647 continue;
2648 case 0xc2: /* ret $nn */
2649 case 0xc3: /* ret */
2650 return TRUE;
2651 /* FIXME: add various jump instructions */
2653 return FALSE;
2657 /* execute a function epilog, which must have been validated with is_inside_epilog() */
2658 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2660 for (;;)
2662 BYTE rex = 0;
2664 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2666 switch (*pc)
2668 case 0x58: /* pop %rax/r8 */
2669 case 0x59: /* pop %rcx/r9 */
2670 case 0x5a: /* pop %rdx/r10 */
2671 case 0x5b: /* pop %rbx/r11 */
2672 case 0x5c: /* pop %rsp/r12 */
2673 case 0x5d: /* pop %rbp/r13 */
2674 case 0x5e: /* pop %rsi/r14 */
2675 case 0x5f: /* pop %rdi/r15 */
2676 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, *(ULONG64 *)context->Rsp );
2677 context->Rsp += sizeof(ULONG64);
2678 pc++;
2679 continue;
2680 case 0x81: /* add $nnnn,%rsp */
2681 context->Rsp += *(LONG *)(pc + 2);
2682 pc += 2 + sizeof(LONG);
2683 continue;
2684 case 0x83: /* add $n,%rsp */
2685 context->Rsp += (signed char)pc[2];
2686 pc += 3;
2687 continue;
2688 case 0x8d:
2689 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
2691 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
2692 pc += 3;
2694 else /* lea nnnn(reg),%rsp */
2696 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
2697 pc += 2 + sizeof(LONG);
2699 continue;
2700 case 0xc2: /* ret $nn */
2701 context->Rip = *(ULONG64 *)context->Rsp;
2702 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
2703 return;
2704 case 0xc3: /* ret */
2705 context->Rip = *(ULONG64 *)context->Rsp;
2706 context->Rsp += sizeof(ULONG64);
2707 return;
2708 /* FIXME: add various jump instructions */
2710 return;
2714 /**********************************************************************
2715 * RtlVirtualUnwind (NTDLL.@)
2717 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
2718 RUNTIME_FUNCTION *function, CONTEXT *context,
2719 PVOID *data, ULONG64 *frame_ret,
2720 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2722 union handler_data *handler_data;
2723 ULONG64 frame, off;
2724 struct UNWIND_INFO *info;
2725 unsigned int i, prolog_offset;
2727 TRACE( "type %x rip %lx rsp %lx\n", type, pc, context->Rsp );
2728 if (TRACE_ON(seh)) dump_unwind_info( base, function );
2730 frame = *frame_ret = context->Rsp;
2731 for (;;)
2733 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
2734 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
2736 if (info->version != 1)
2738 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
2739 return NULL;
2742 if (info->frame_reg)
2743 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
2745 /* check if in prolog */
2746 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
2748 prolog_offset = pc - base - function->BeginAddress;
2750 else
2752 prolog_offset = ~0;
2753 if (is_inside_epilog( (BYTE *)pc ))
2755 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
2756 *frame_ret = frame;
2757 return NULL;
2761 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
2763 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
2765 switch (info->opcodes[i].code)
2767 case UWOP_PUSH_NONVOL: /* pushq %reg */
2768 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)context->Rsp );
2769 context->Rsp += sizeof(ULONG64);
2770 break;
2771 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
2772 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
2773 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
2774 break;
2775 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
2776 context->Rsp += (info->opcodes[i].info + 1) * 8;
2777 break;
2778 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
2779 context->Rsp = *frame_ret = frame;
2780 break;
2781 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
2782 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
2783 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2784 break;
2785 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
2786 off = frame + *(DWORD *)&info->opcodes[i+1];
2787 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2788 break;
2789 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
2790 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
2791 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2792 break;
2793 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
2794 off = frame + *(DWORD *)&info->opcodes[i+1];
2795 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2796 break;
2797 case UWOP_PUSH_MACHFRAME:
2798 FIXME( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
2799 break;
2800 default:
2801 FIXME( "unknown code %u\n", info->opcodes[i].code );
2802 break;
2806 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
2807 function = &handler_data->chain; /* restart with the chained info */
2810 /* now pop return address */
2811 context->Rip = *(ULONG64 *)context->Rsp;
2812 context->Rsp += sizeof(ULONG64);
2814 if (!(info->flags & type)) return NULL; /* no matching handler */
2815 if (prolog_offset != ~0) return NULL; /* inside prolog */
2817 *data = &handler_data->handler + 1;
2818 return (char *)base + handler_data->handler;
2822 /**********************************************************************
2823 * call_unwind_handler
2825 * Call a single unwind handler.
2826 * FIXME: Handle nested exceptions.
2828 static void call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
2830 DWORD res;
2832 dispatch->ControlPc = dispatch->ContextRecord->Rip;
2834 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2835 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2836 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2837 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
2839 switch (res)
2841 case ExceptionContinueSearch:
2842 break;
2843 case ExceptionCollidedUnwind:
2844 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2845 break;
2846 default:
2847 raise_status( STATUS_INVALID_DISPOSITION, rec );
2848 break;
2853 /**********************************************************************
2854 * call_teb_unwind_handler
2856 * Call a single unwind handler from the TEB chain.
2857 * FIXME: Handle nested exceptions.
2859 static void call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
2860 EXCEPTION_REGISTRATION_RECORD *teb_frame )
2862 EXCEPTION_REGISTRATION_RECORD *dispatcher;
2863 DWORD res;
2865 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2866 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2867 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2868 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
2870 switch (res)
2872 case ExceptionContinueSearch:
2873 break;
2874 case ExceptionCollidedUnwind:
2875 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2876 break;
2877 default:
2878 raise_status( STATUS_INVALID_DISPOSITION, rec );
2879 break;
2884 /*******************************************************************
2885 * RtlUnwindEx (NTDLL.@)
2887 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
2888 PVOID retval, CONTEXT *orig_context, UNWIND_HISTORY_TABLE *table )
2890 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
2891 EXCEPTION_RECORD record;
2892 DISPATCHER_CONTEXT dispatch;
2893 CONTEXT context, new_context;
2894 LDR_MODULE *module;
2895 NTSTATUS status;
2896 DWORD size;
2898 RtlCaptureContext( orig_context );
2900 /* build an exception record, if we do not have one */
2901 if (!rec)
2903 record.ExceptionCode = STATUS_UNWIND;
2904 record.ExceptionFlags = 0;
2905 record.ExceptionRecord = NULL;
2906 record.ExceptionAddress = (void *)orig_context->Rip;
2907 record.NumberParameters = 0;
2908 rec = &record;
2911 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
2913 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016lx\n",
2914 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, orig_context->Rip );
2915 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2916 orig_context->Rax, orig_context->Rbx, orig_context->Rcx, orig_context->Rdx );
2917 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2918 orig_context->Rsi, orig_context->Rdi, orig_context->Rbp, orig_context->Rsp );
2919 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2920 orig_context->R8, orig_context->R9, orig_context->R10, orig_context->R11 );
2921 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2922 orig_context->R12, orig_context->R13, orig_context->R14, orig_context->R15 );
2924 context = *orig_context;
2925 dispatch.EstablisherFrame = context.Rsp;
2926 dispatch.TargetIp = (ULONG64)target_ip;
2927 dispatch.ContextRecord = &context;
2928 dispatch.HistoryTable = table;
2930 while (dispatch.EstablisherFrame != (ULONG64)end_frame)
2932 new_context = context;
2934 /* FIXME: should use the history table to make things faster */
2936 module = NULL;
2937 dispatch.ImageBase = 0;
2938 dispatch.ScopeIndex = 0; /* FIXME */
2940 /* first look for PE exception information */
2942 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
2944 RUNTIME_FUNCTION *dir;
2946 dispatch.ImageBase = (ULONG64)module->BaseAddress;
2947 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2948 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2950 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
2951 dir, size )))
2953 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_UHANDLER, dispatch.ImageBase,
2954 context.Rip, dispatch.FunctionEntry,
2955 &new_context, &dispatch.HandlerData,
2956 &dispatch.EstablisherFrame, NULL );
2957 goto unwind_done;
2960 else if (!(module->Flags & LDR_WINE_INTERNAL))
2961 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
2964 /* then look for host system exception information */
2966 if (!module || (module->Flags & LDR_WINE_INTERNAL))
2968 struct dwarf_eh_bases bases;
2969 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
2971 if (fde)
2973 dispatch.FunctionEntry = NULL;
2974 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context, fde,
2975 &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
2976 if (status != STATUS_SUCCESS) raise_status( status, rec );
2977 if (dispatch.LanguageHandler && !module)
2979 FIXME( "calling personality routine in system library not supported yet\n" );
2980 dispatch.LanguageHandler = NULL;
2982 goto unwind_done;
2986 /* no exception information, treat as a leaf function */
2988 new_context.Rip = *(ULONG64 *)context.Rsp;
2989 new_context.Rsp = context.Rsp + sizeof(ULONG64);
2990 dispatch.EstablisherFrame = new_context.Rsp;
2991 dispatch.LanguageHandler = NULL;
2993 unwind_done:
2994 if (!dispatch.EstablisherFrame) break;
2996 if (is_inside_signal_stack( (void *)dispatch.EstablisherFrame ))
2998 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch.EstablisherFrame,
2999 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size );
3000 context = new_context;
3001 continue;
3004 if ((dispatch.EstablisherFrame & 7) ||
3005 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
3006 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
3008 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
3009 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
3010 rec->ExceptionFlags |= EH_STACK_INVALID;
3011 break;
3014 if (dispatch.LanguageHandler)
3016 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
3018 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
3019 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
3021 call_unwind_handler( rec, &dispatch );
3023 else /* hack: call builtin handlers registered in the tib list */
3025 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < (ULONG64)end_frame)
3027 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
3028 dispatch.EstablisherFrame = (ULONG64)teb_frame;
3029 call_teb_unwind_handler( rec, &dispatch, teb_frame );
3030 teb_frame = __wine_pop_frame( teb_frame );
3032 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Rsp) break;
3033 dispatch.EstablisherFrame = new_context.Rsp;
3036 context = new_context;
3039 if (rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
3041 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
3042 context.Rbx = jmp->Rbx;
3043 context.Rsp = jmp->Rsp;
3044 context.Rbp = jmp->Rbp;
3045 context.Rsi = jmp->Rsi;
3046 context.Rdi = jmp->Rdi;
3047 context.R12 = jmp->R12;
3048 context.R13 = jmp->R13;
3049 context.R14 = jmp->R14;
3050 context.R15 = jmp->R15;
3051 context.Rip = jmp->Rip;
3052 context.u.s.Xmm6 = jmp->Xmm6;
3053 context.u.s.Xmm7 = jmp->Xmm7;
3054 context.u.s.Xmm8 = jmp->Xmm8;
3055 context.u.s.Xmm9 = jmp->Xmm9;
3056 context.u.s.Xmm10 = jmp->Xmm10;
3057 context.u.s.Xmm11 = jmp->Xmm11;
3058 context.u.s.Xmm12 = jmp->Xmm12;
3059 context.u.s.Xmm13 = jmp->Xmm13;
3060 context.u.s.Xmm14 = jmp->Xmm14;
3061 context.u.s.Xmm15 = jmp->Xmm15;
3063 context.Rax = (ULONG64)retval;
3064 context.Rip = (ULONG64)target_ip;
3065 TRACE( "returning to %lx stack %lx\n", context.Rip, context.Rsp );
3066 set_cpu_context( &context );
3070 /*******************************************************************
3071 * RtlUnwind (NTDLL.@)
3073 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
3075 CONTEXT context;
3076 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
3080 /*******************************************************************
3081 * __C_specific_handler (NTDLL.@)
3083 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
3084 ULONG64 frame,
3085 CONTEXT *context,
3086 struct _DISPATCHER_CONTEXT *dispatch )
3088 SCOPE_TABLE *table = dispatch->HandlerData;
3089 ULONG i;
3091 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
3092 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
3094 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)) /* FIXME */
3095 return ExceptionContinueSearch;
3097 for (i = 0; i < table->Count; i++)
3099 if (context->Rip >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
3100 context->Rip < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
3102 if (!table->ScopeRecord[i].JumpTarget) continue;
3103 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
3105 EXCEPTION_POINTERS ptrs;
3106 PC_LANGUAGE_EXCEPTION_HANDLER filter;
3108 filter = (PC_LANGUAGE_EXCEPTION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
3109 ptrs.ExceptionRecord = rec;
3110 ptrs.ContextRecord = context;
3111 TRACE( "calling filter %p ptrs %p frame %lx\n", filter, &ptrs, frame );
3112 switch (filter( &ptrs, frame ))
3114 case EXCEPTION_EXECUTE_HANDLER:
3115 break;
3116 case EXCEPTION_CONTINUE_SEARCH:
3117 continue;
3118 case EXCEPTION_CONTINUE_EXECUTION:
3119 return ExceptionContinueExecution;
3122 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
3123 RtlUnwindEx( (void *)frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
3124 rec, 0, context, dispatch->HistoryTable );
3127 return ExceptionContinueSearch;
3131 /*******************************************************************
3132 * NtRaiseException (NTDLL.@)
3134 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
3136 NTSTATUS status = raise_exception( rec, context, first_chance );
3137 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
3138 return status;
3142 /***********************************************************************
3143 * RtlRaiseException (NTDLL.@)
3145 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
3147 NTSTATUS status;
3149 rec->ExceptionAddress = (void *)context->Rip;
3150 status = raise_exception( rec, context, TRUE );
3151 if (status != STATUS_SUCCESS) raise_status( status, rec );
3153 __ASM_GLOBAL_FUNC( RtlRaiseException,
3154 "movq %rcx,8(%rsp)\n\t"
3155 "sub $0x4f8,%rsp\n\t"
3156 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
3157 "leaq 0x20(%rsp),%rcx\n\t"
3158 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
3159 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
3160 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
3161 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
3162 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
3163 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
3164 "movq (%rax),%rcx\n\t" /* original first parameter */
3165 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
3166 "call " __ASM_NAME("__regs_RtlRaiseException") "\n\t"
3167 "leaq 0x20(%rsp),%rdi\n\t" /* context pointer */
3168 "call " __ASM_NAME("set_cpu_context") /* does not return */ );
3171 /*************************************************************************
3172 * RtlCaptureStackBackTrace (NTDLL.@)
3174 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
3176 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
3177 return 0;
3181 /***********************************************************************
3182 * call_thread_func
3184 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
3186 ntdll_get_thread_data()->exit_frame = frame;
3187 __TRY
3189 RtlExitUserThread( entry( arg ));
3191 __EXCEPT(unhandled_exception_filter)
3193 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
3195 __ENDTRY
3196 abort(); /* should not be reached */
3199 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
3200 __ASM_GLOBAL_FUNC( call_thread_entry_point,
3201 "subq $56,%rsp\n\t"
3202 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3203 "movq %rbp,48(%rsp)\n\t"
3204 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3205 "movq %rbx,40(%rsp)\n\t"
3206 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3207 "movq %r12,32(%rsp)\n\t"
3208 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3209 "movq %r13,24(%rsp)\n\t"
3210 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3211 "movq %r14,16(%rsp)\n\t"
3212 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3213 "movq %r15,8(%rsp)\n\t"
3214 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3215 "movq %rsp,%rdx\n\t"
3216 "call " __ASM_NAME("call_thread_func") );
3218 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
3219 __ASM_GLOBAL_FUNC( call_thread_exit_func,
3220 "movq %rdx,%rsp\n\t"
3221 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3222 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3223 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3224 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3225 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3226 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3227 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3228 "call *%rsi" );
3230 /***********************************************************************
3231 * RtlExitUserThread (NTDLL.@)
3233 void WINAPI RtlExitUserThread( ULONG status )
3235 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
3236 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
3239 /***********************************************************************
3240 * abort_thread
3242 void abort_thread( int status )
3244 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
3245 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
3248 /**********************************************************************
3249 * __wine_enter_vm86 (NTDLL.@)
3251 void __wine_enter_vm86( CONTEXT *context )
3253 MESSAGE("vm86 mode not supported on this platform\n");
3256 /**********************************************************************
3257 * DbgBreakPoint (NTDLL.@)
3259 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
3261 /**********************************************************************
3262 * DbgUserBreakPoint (NTDLL.@)
3264 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
3266 #endif /* __x86_64__ */