ntdll: Fix the initial FPU control word on 64-bit.
[wine/multimedia.git] / dlls / ntdll / signal_x86_64.c
blob48f8b6d4a0ac3de99f6b6503cc710ea5ddf11cf8
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 /***********************************************************************
104 * signal context platform-specific definitions
106 #ifdef linux
108 #include <asm/prctl.h>
109 extern int arch_prctl(int func, void *ptr);
111 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
112 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
113 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
114 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
115 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
116 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
117 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
118 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
119 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
120 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
121 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
122 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
123 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
124 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
125 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
127 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
128 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
129 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
131 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
132 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
133 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
134 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
135 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
137 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
139 #elif defined(__FreeBSD__)
140 #include <sys/ucontext.h>
142 #define RAX_sig(context) ((context)->uc_mcontext.mc_rax)
143 #define RBX_sig(context) ((context)->uc_mcontext.mc_rbx)
144 #define RCX_sig(context) ((context)->uc_mcontext.mc_rcx)
145 #define RDX_sig(context) ((context)->uc_mcontext.mc_rdx)
146 #define RSI_sig(context) ((context)->uc_mcontext.mc_rsi)
147 #define RDI_sig(context) ((context)->uc_mcontext.mc_rdi)
148 #define RBP_sig(context) ((context)->uc_mcontext.mc_rbp)
149 #define R8_sig(context) ((context)->uc_mcontext.mc_r8)
150 #define R9_sig(context) ((context)->uc_mcontext.mc_r9)
151 #define R10_sig(context) ((context)->uc_mcontext.mc_r10)
152 #define R11_sig(context) ((context)->uc_mcontext.mc_r11)
153 #define R12_sig(context) ((context)->uc_mcontext.mc_r12)
154 #define R13_sig(context) ((context)->uc_mcontext.mc_r13)
155 #define R14_sig(context) ((context)->uc_mcontext.mc_r14)
156 #define R15_sig(context) ((context)->uc_mcontext.mc_r15)
158 #define CS_sig(context) ((context)->uc_mcontext.mc_cs)
159 #define DS_sig(context) ((context)->uc_mcontext.mc_ds)
160 #define ES_sig(context) ((context)->uc_mcontext.mc_es)
161 #define FS_sig(context) ((context)->uc_mcontext.mc_fs)
162 #define GS_sig(context) ((context)->uc_mcontext.mc_gs)
163 #define SS_sig(context) ((context)->uc_mcontext.mc_ss)
165 #define EFL_sig(context) ((context)->uc_mcontext.mc_rflags)
167 #define RIP_sig(context) ((context)->uc_mcontext.mc_rip)
168 #define RSP_sig(context) ((context)->uc_mcontext.mc_rsp)
169 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
170 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
172 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.mc_fpstate))
174 #elif defined(__NetBSD__)
175 #include <sys/ucontext.h>
176 #include <sys/types.h>
177 #include <signal.h>
179 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
180 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
181 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
182 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
183 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
184 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
185 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
186 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
187 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
188 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
189 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
190 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
191 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
192 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
193 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
195 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
196 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
197 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
198 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
199 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
200 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
202 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
204 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
205 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
207 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
208 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
210 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
211 #else
212 #error You must define the signal context functions for your platform
213 #endif
215 enum i386_trap_code
217 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
218 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
219 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
220 TRAP_x86_NMI = 2, /* NMI interrupt */
221 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
222 TRAP_x86_OFLOW = 4, /* Overflow exception */
223 TRAP_x86_BOUND = 5, /* Bound range exception */
224 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
225 TRAP_x86_DNA = 7, /* Device not available exception */
226 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
227 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
228 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
229 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
230 TRAP_x86_STKFLT = 12, /* Stack fault */
231 TRAP_x86_PROTFLT = 13, /* General protection fault */
232 TRAP_x86_PAGEFLT = 14, /* Page fault */
233 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
234 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
235 TRAP_x86_MCHK = 18, /* Machine check exception */
236 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
239 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
240 static size_t signal_stack_size;
242 typedef void (*raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
243 typedef int (*wine_signal_handler)(unsigned int sig);
245 static wine_signal_handler handlers[256];
248 /***********************************************************************
249 * Definitions for Win32 unwind tables
252 union handler_data
254 RUNTIME_FUNCTION chain;
255 ULONG handler;
258 struct opcode
260 BYTE offset;
261 BYTE code : 4;
262 BYTE info : 4;
265 struct UNWIND_INFO
267 BYTE version : 3;
268 BYTE flags : 5;
269 BYTE prolog;
270 BYTE count;
271 BYTE frame_reg : 4;
272 BYTE frame_offset : 4;
273 struct opcode opcodes[1]; /* info->count entries */
274 /* followed by handler_data */
277 #define UWOP_PUSH_NONVOL 0
278 #define UWOP_ALLOC_LARGE 1
279 #define UWOP_ALLOC_SMALL 2
280 #define UWOP_SET_FPREG 3
281 #define UWOP_SAVE_NONVOL 4
282 #define UWOP_SAVE_NONVOL_FAR 5
283 #define UWOP_SAVE_XMM128 8
284 #define UWOP_SAVE_XMM128_FAR 9
285 #define UWOP_PUSH_MACHFRAME 10
287 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
289 static const char * const reg_names[16] =
290 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
291 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
293 union handler_data *handler_data;
294 struct UNWIND_INFO *info;
295 unsigned int i, count;
297 TRACE( "**** func %x-%x\n", function->BeginAddress, function->EndAddress );
298 for (;;)
300 if (function->UnwindData & 1)
302 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
303 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
304 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
305 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
306 function = next;
307 continue;
309 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
311 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
312 info, info->flags, info->prolog,
313 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
315 if (info->frame_reg)
316 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
317 reg_names[info->frame_reg], info->frame_offset * 16 );
319 for (i = 0; i < info->count; i++)
321 TRACE( " 0x%x: ", info->opcodes[i].offset );
322 switch (info->opcodes[i].code)
324 case UWOP_PUSH_NONVOL:
325 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
326 break;
327 case UWOP_ALLOC_LARGE:
328 if (info->opcodes[i].info)
330 count = *(DWORD *)&info->opcodes[i+1];
331 i += 2;
333 else
335 count = *(USHORT *)&info->opcodes[i+1] * 8;
336 i++;
338 TRACE( "subq $0x%x,%%rsp\n", count );
339 break;
340 case UWOP_ALLOC_SMALL:
341 count = (info->opcodes[i].info + 1) * 8;
342 TRACE( "subq $0x%x,%%rsp\n", count );
343 break;
344 case UWOP_SET_FPREG:
345 TRACE( "leaq 0x%x(%%rsp),%s\n",
346 info->frame_offset * 16, reg_names[info->frame_reg] );
347 break;
348 case UWOP_SAVE_NONVOL:
349 count = *(USHORT *)&info->opcodes[i+1] * 8;
350 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
351 i++;
352 break;
353 case UWOP_SAVE_NONVOL_FAR:
354 count = *(DWORD *)&info->opcodes[i+1];
355 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
356 i += 2;
357 break;
358 case UWOP_SAVE_XMM128:
359 count = *(USHORT *)&info->opcodes[i+1] * 16;
360 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
361 i++;
362 break;
363 case UWOP_SAVE_XMM128_FAR:
364 count = *(DWORD *)&info->opcodes[i+1];
365 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
366 i += 2;
367 break;
368 case UWOP_PUSH_MACHFRAME:
369 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
370 break;
371 default:
372 FIXME( "unknown code %u\n", info->opcodes[i].code );
373 break;
377 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
378 if (info->flags & UNW_FLAG_CHAININFO)
380 TRACE( " chained to function %p-%p\n",
381 (char *)base + handler_data->chain.BeginAddress,
382 (char *)base + handler_data->chain.EndAddress );
383 function = &handler_data->chain;
384 continue;
386 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
387 TRACE( " handler %p data at %p\n",
388 (char *)base + handler_data->handler, &handler_data->handler + 1 );
389 break;
393 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
395 unsigned int i;
397 TRACE( "scope table at %p\n", table );
398 for (i = 0; i < table->Count; i++)
399 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
400 base + table->ScopeRecord[i].BeginAddress,
401 base + table->ScopeRecord[i].EndAddress,
402 base + table->ScopeRecord[i].HandlerAddress,
403 base + table->ScopeRecord[i].JumpTarget );
407 /***********************************************************************
408 * Definitions for Dwarf unwind tables
411 enum dwarf_call_frame_info
413 DW_CFA_advance_loc = 0x40,
414 DW_CFA_offset = 0x80,
415 DW_CFA_restore = 0xc0,
416 DW_CFA_nop = 0x00,
417 DW_CFA_set_loc = 0x01,
418 DW_CFA_advance_loc1 = 0x02,
419 DW_CFA_advance_loc2 = 0x03,
420 DW_CFA_advance_loc4 = 0x04,
421 DW_CFA_offset_extended = 0x05,
422 DW_CFA_restore_extended = 0x06,
423 DW_CFA_undefined = 0x07,
424 DW_CFA_same_value = 0x08,
425 DW_CFA_register = 0x09,
426 DW_CFA_remember_state = 0x0a,
427 DW_CFA_restore_state = 0x0b,
428 DW_CFA_def_cfa = 0x0c,
429 DW_CFA_def_cfa_register = 0x0d,
430 DW_CFA_def_cfa_offset = 0x0e,
431 DW_CFA_def_cfa_expression = 0x0f,
432 DW_CFA_expression = 0x10,
433 DW_CFA_offset_extended_sf = 0x11,
434 DW_CFA_def_cfa_sf = 0x12,
435 DW_CFA_def_cfa_offset_sf = 0x13,
436 DW_CFA_val_offset = 0x14,
437 DW_CFA_val_offset_sf = 0x15,
438 DW_CFA_val_expression = 0x16,
441 enum dwarf_operation
443 DW_OP_addr = 0x03,
444 DW_OP_deref = 0x06,
445 DW_OP_const1u = 0x08,
446 DW_OP_const1s = 0x09,
447 DW_OP_const2u = 0x0a,
448 DW_OP_const2s = 0x0b,
449 DW_OP_const4u = 0x0c,
450 DW_OP_const4s = 0x0d,
451 DW_OP_const8u = 0x0e,
452 DW_OP_const8s = 0x0f,
453 DW_OP_constu = 0x10,
454 DW_OP_consts = 0x11,
455 DW_OP_dup = 0x12,
456 DW_OP_drop = 0x13,
457 DW_OP_over = 0x14,
458 DW_OP_pick = 0x15,
459 DW_OP_swap = 0x16,
460 DW_OP_rot = 0x17,
461 DW_OP_xderef = 0x18,
462 DW_OP_abs = 0x19,
463 DW_OP_and = 0x1a,
464 DW_OP_div = 0x1b,
465 DW_OP_minus = 0x1c,
466 DW_OP_mod = 0x1d,
467 DW_OP_mul = 0x1e,
468 DW_OP_neg = 0x1f,
469 DW_OP_not = 0x20,
470 DW_OP_or = 0x21,
471 DW_OP_plus = 0x22,
472 DW_OP_plus_uconst = 0x23,
473 DW_OP_shl = 0x24,
474 DW_OP_shr = 0x25,
475 DW_OP_shra = 0x26,
476 DW_OP_xor = 0x27,
477 DW_OP_bra = 0x28,
478 DW_OP_eq = 0x29,
479 DW_OP_ge = 0x2a,
480 DW_OP_gt = 0x2b,
481 DW_OP_le = 0x2c,
482 DW_OP_lt = 0x2d,
483 DW_OP_ne = 0x2e,
484 DW_OP_skip = 0x2f,
485 DW_OP_lit0 = 0x30,
486 DW_OP_lit1 = 0x31,
487 DW_OP_lit2 = 0x32,
488 DW_OP_lit3 = 0x33,
489 DW_OP_lit4 = 0x34,
490 DW_OP_lit5 = 0x35,
491 DW_OP_lit6 = 0x36,
492 DW_OP_lit7 = 0x37,
493 DW_OP_lit8 = 0x38,
494 DW_OP_lit9 = 0x39,
495 DW_OP_lit10 = 0x3a,
496 DW_OP_lit11 = 0x3b,
497 DW_OP_lit12 = 0x3c,
498 DW_OP_lit13 = 0x3d,
499 DW_OP_lit14 = 0x3e,
500 DW_OP_lit15 = 0x3f,
501 DW_OP_lit16 = 0x40,
502 DW_OP_lit17 = 0x41,
503 DW_OP_lit18 = 0x42,
504 DW_OP_lit19 = 0x43,
505 DW_OP_lit20 = 0x44,
506 DW_OP_lit21 = 0x45,
507 DW_OP_lit22 = 0x46,
508 DW_OP_lit23 = 0x47,
509 DW_OP_lit24 = 0x48,
510 DW_OP_lit25 = 0x49,
511 DW_OP_lit26 = 0x4a,
512 DW_OP_lit27 = 0x4b,
513 DW_OP_lit28 = 0x4c,
514 DW_OP_lit29 = 0x4d,
515 DW_OP_lit30 = 0x4e,
516 DW_OP_lit31 = 0x4f,
517 DW_OP_reg0 = 0x50,
518 DW_OP_reg1 = 0x51,
519 DW_OP_reg2 = 0x52,
520 DW_OP_reg3 = 0x53,
521 DW_OP_reg4 = 0x54,
522 DW_OP_reg5 = 0x55,
523 DW_OP_reg6 = 0x56,
524 DW_OP_reg7 = 0x57,
525 DW_OP_reg8 = 0x58,
526 DW_OP_reg9 = 0x59,
527 DW_OP_reg10 = 0x5a,
528 DW_OP_reg11 = 0x5b,
529 DW_OP_reg12 = 0x5c,
530 DW_OP_reg13 = 0x5d,
531 DW_OP_reg14 = 0x5e,
532 DW_OP_reg15 = 0x5f,
533 DW_OP_reg16 = 0x60,
534 DW_OP_reg17 = 0x61,
535 DW_OP_reg18 = 0x62,
536 DW_OP_reg19 = 0x63,
537 DW_OP_reg20 = 0x64,
538 DW_OP_reg21 = 0x65,
539 DW_OP_reg22 = 0x66,
540 DW_OP_reg23 = 0x67,
541 DW_OP_reg24 = 0x68,
542 DW_OP_reg25 = 0x69,
543 DW_OP_reg26 = 0x6a,
544 DW_OP_reg27 = 0x6b,
545 DW_OP_reg28 = 0x6c,
546 DW_OP_reg29 = 0x6d,
547 DW_OP_reg30 = 0x6e,
548 DW_OP_reg31 = 0x6f,
549 DW_OP_breg0 = 0x70,
550 DW_OP_breg1 = 0x71,
551 DW_OP_breg2 = 0x72,
552 DW_OP_breg3 = 0x73,
553 DW_OP_breg4 = 0x74,
554 DW_OP_breg5 = 0x75,
555 DW_OP_breg6 = 0x76,
556 DW_OP_breg7 = 0x77,
557 DW_OP_breg8 = 0x78,
558 DW_OP_breg9 = 0x79,
559 DW_OP_breg10 = 0x7a,
560 DW_OP_breg11 = 0x7b,
561 DW_OP_breg12 = 0x7c,
562 DW_OP_breg13 = 0x7d,
563 DW_OP_breg14 = 0x7e,
564 DW_OP_breg15 = 0x7f,
565 DW_OP_breg16 = 0x80,
566 DW_OP_breg17 = 0x81,
567 DW_OP_breg18 = 0x82,
568 DW_OP_breg19 = 0x83,
569 DW_OP_breg20 = 0x84,
570 DW_OP_breg21 = 0x85,
571 DW_OP_breg22 = 0x86,
572 DW_OP_breg23 = 0x87,
573 DW_OP_breg24 = 0x88,
574 DW_OP_breg25 = 0x89,
575 DW_OP_breg26 = 0x8a,
576 DW_OP_breg27 = 0x8b,
577 DW_OP_breg28 = 0x8c,
578 DW_OP_breg29 = 0x8d,
579 DW_OP_breg30 = 0x8e,
580 DW_OP_breg31 = 0x8f,
581 DW_OP_regx = 0x90,
582 DW_OP_fbreg = 0x91,
583 DW_OP_bregx = 0x92,
584 DW_OP_piece = 0x93,
585 DW_OP_deref_size = 0x94,
586 DW_OP_xderef_size = 0x95,
587 DW_OP_nop = 0x96,
588 DW_OP_push_object_address = 0x97,
589 DW_OP_call2 = 0x98,
590 DW_OP_call4 = 0x99,
591 DW_OP_call_ref = 0x9a,
592 DW_OP_form_tls_address = 0x9b,
593 DW_OP_call_frame_cfa = 0x9c,
594 DW_OP_bit_piece = 0x9d,
595 DW_OP_lo_user = 0xe0,
596 DW_OP_hi_user = 0xff,
597 DW_OP_GNU_push_tls_address = 0xe0,
598 DW_OP_GNU_uninit = 0xf0,
599 DW_OP_GNU_encoded_addr = 0xf1,
602 #define DW_EH_PE_native 0x00
603 #define DW_EH_PE_leb128 0x01
604 #define DW_EH_PE_data2 0x02
605 #define DW_EH_PE_data4 0x03
606 #define DW_EH_PE_data8 0x04
607 #define DW_EH_PE_signed 0x08
608 #define DW_EH_PE_abs 0x00
609 #define DW_EH_PE_pcrel 0x10
610 #define DW_EH_PE_textrel 0x20
611 #define DW_EH_PE_datarel 0x30
612 #define DW_EH_PE_funcrel 0x40
613 #define DW_EH_PE_aligned 0x50
614 #define DW_EH_PE_indirect 0x80
615 #define DW_EH_PE_omit 0xff
617 struct dwarf_eh_bases
619 void *tbase;
620 void *dbase;
621 void *func;
624 struct dwarf_cie
626 unsigned int length;
627 int id;
628 unsigned char version;
629 unsigned char augmentation[1];
632 struct dwarf_fde
634 unsigned int length;
635 unsigned int cie_offset;
638 extern const struct dwarf_fde *_Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
640 static unsigned char dwarf_get_u1( const unsigned char **p )
642 return *(*p)++;
645 static unsigned short dwarf_get_u2( const unsigned char **p )
647 unsigned int ret = (*p)[0] | ((*p)[1] << 8);
648 (*p) += 2;
649 return ret;
652 static unsigned int dwarf_get_u4( const unsigned char **p )
654 unsigned int ret = (*p)[0] | ((*p)[1] << 8) | ((*p)[2] << 16) | ((*p)[3] << 24);
655 (*p) += 4;
656 return ret;
659 static ULONG64 dwarf_get_u8( const unsigned char **p )
661 ULONG64 low = dwarf_get_u4( p );
662 ULONG64 high = dwarf_get_u4( p );
663 return low | (high << 32);
666 static ULONG_PTR dwarf_get_uleb128( const unsigned char **p )
668 ULONG_PTR ret = 0;
669 unsigned int shift = 0;
670 unsigned char byte;
674 byte = **p;
675 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
676 shift += 7;
677 (*p)++;
678 } while (byte & 0x80);
679 return ret;
682 static LONG_PTR dwarf_get_sleb128( const unsigned char **p )
684 ULONG_PTR ret = 0;
685 unsigned int shift = 0;
686 unsigned char byte;
690 byte = **p;
691 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
692 shift += 7;
693 (*p)++;
694 } while (byte & 0x80);
696 if ((shift < 8 * sizeof(ret)) && (byte & 0x40)) ret |= -((ULONG_PTR)1 << shift);
697 return ret;
700 static ULONG_PTR dwarf_get_ptr( const unsigned char **p, unsigned char encoding )
702 ULONG_PTR base;
704 if (encoding == DW_EH_PE_omit) return 0;
706 switch (encoding & 0xf0)
708 case DW_EH_PE_abs:
709 base = 0;
710 break;
711 case DW_EH_PE_pcrel:
712 base = (ULONG_PTR)*p;
713 break;
714 default:
715 FIXME( "unsupported encoding %02x\n", encoding );
716 return 0;
719 switch (encoding & 0x0f)
721 case DW_EH_PE_native:
722 return base + dwarf_get_u8( p );
723 case DW_EH_PE_leb128:
724 return base + dwarf_get_uleb128( p );
725 case DW_EH_PE_data2:
726 return base + dwarf_get_u2( p );
727 case DW_EH_PE_data4:
728 return base + dwarf_get_u4( p );
729 case DW_EH_PE_data8:
730 return base + dwarf_get_u8( p );
731 case DW_EH_PE_signed|DW_EH_PE_leb128:
732 return base + dwarf_get_sleb128( p );
733 case DW_EH_PE_signed|DW_EH_PE_data2:
734 return base + (signed short)dwarf_get_u2( p );
735 case DW_EH_PE_signed|DW_EH_PE_data4:
736 return base + (signed int)dwarf_get_u4( p );
737 case DW_EH_PE_signed|DW_EH_PE_data8:
738 return base + (LONG64)dwarf_get_u8( p );
739 default:
740 FIXME( "unsupported encoding %02x\n", encoding );
741 return 0;
745 enum reg_rule
747 RULE_UNSET, /* not set at all */
748 RULE_UNDEFINED, /* undefined value */
749 RULE_SAME, /* same value as previous frame */
750 RULE_CFA_OFFSET, /* stored at cfa offset */
751 RULE_OTHER_REG, /* stored in other register */
752 RULE_EXPRESSION, /* address specified by expression */
753 RULE_VAL_EXPRESSION /* value specified by expression */
756 #define NB_FRAME_REGS 41
757 #define MAX_SAVED_STATES 16
759 struct frame_state
761 ULONG_PTR cfa_offset;
762 unsigned char cfa_reg;
763 enum reg_rule cfa_rule;
764 enum reg_rule rules[NB_FRAME_REGS];
765 ULONG64 regs[NB_FRAME_REGS];
768 struct frame_info
770 ULONG_PTR ip;
771 ULONG_PTR code_align;
772 LONG_PTR data_align;
773 unsigned char retaddr_reg;
774 unsigned char fde_encoding;
775 unsigned char signal_frame;
776 unsigned char state_sp;
777 struct frame_state state;
778 struct frame_state *state_stack;
781 static const char *dwarf_reg_names[NB_FRAME_REGS] =
783 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
784 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
785 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
786 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
787 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
790 static int valid_reg( ULONG_PTR reg )
792 if (reg >= NB_FRAME_REGS) FIXME( "unsupported reg %lx\n", reg );
793 return (reg < NB_FRAME_REGS);
796 static void execute_cfa_instructions( const unsigned char *ptr, const unsigned char *end,
797 ULONG_PTR last_ip, struct frame_info *info )
799 while (ptr < end && info->ip < last_ip + info->signal_frame)
801 enum dwarf_call_frame_info op = *ptr++;
803 if (op & 0xc0)
805 switch (op & 0xc0)
807 case DW_CFA_advance_loc:
809 ULONG_PTR offset = (op & 0x3f) * info->code_align;
810 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info->ip, offset );
811 info->ip += offset;
812 break;
814 case DW_CFA_offset:
816 ULONG_PTR reg = op & 0x3f;
817 LONG_PTR offset = dwarf_get_uleb128( &ptr ) * info->data_align;
818 if (!valid_reg( reg )) break;
819 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
820 info->state.regs[reg] = offset;
821 info->state.rules[reg] = RULE_CFA_OFFSET;
822 break;
824 case DW_CFA_restore:
826 ULONG_PTR reg = op & 0x3f;
827 if (!valid_reg( reg )) break;
828 TRACE( "%lx: DW_CFA_restore %s\n", info->ip, dwarf_reg_names[reg] );
829 info->state.rules[reg] = RULE_UNSET;
830 break;
834 else switch (op)
836 case DW_CFA_nop:
837 break;
838 case DW_CFA_set_loc:
840 ULONG_PTR loc = dwarf_get_ptr( &ptr, info->fde_encoding );
841 TRACE( "%lx: DW_CFA_set_loc %lx\n", info->ip, loc );
842 info->ip = loc;
843 break;
845 case DW_CFA_advance_loc1:
847 ULONG_PTR offset = *ptr++ * info->code_align;
848 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset );
849 info->ip += offset;
850 break;
852 case DW_CFA_advance_loc2:
854 ULONG_PTR offset = dwarf_get_u2( &ptr ) * info->code_align;
855 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset );
856 info->ip += offset;
857 break;
859 case DW_CFA_advance_loc4:
861 ULONG_PTR offset = dwarf_get_u4( &ptr ) * info->code_align;
862 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset );
863 info->ip += offset;
864 break;
866 case DW_CFA_offset_extended:
867 case DW_CFA_offset_extended_sf:
869 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
870 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf_get_uleb128( &ptr ) * info->data_align
871 : dwarf_get_sleb128( &ptr ) * info->data_align;
872 if (!valid_reg( reg )) break;
873 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
874 info->state.regs[reg] = offset;
875 info->state.rules[reg] = RULE_CFA_OFFSET;
876 break;
878 case DW_CFA_restore_extended:
880 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
881 if (!valid_reg( reg )) break;
882 TRACE( "%lx: DW_CFA_restore_extended %s\n", info->ip, dwarf_reg_names[reg] );
883 info->state.rules[reg] = RULE_UNSET;
884 break;
886 case DW_CFA_undefined:
888 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
889 if (!valid_reg( reg )) break;
890 TRACE( "%lx: DW_CFA_undefined %s\n", info->ip, dwarf_reg_names[reg] );
891 info->state.rules[reg] = RULE_UNDEFINED;
892 break;
894 case DW_CFA_same_value:
896 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
897 if (!valid_reg( reg )) break;
898 TRACE( "%lx: DW_CFA_same_value %s\n", info->ip, dwarf_reg_names[reg] );
899 info->state.regs[reg] = reg;
900 info->state.rules[reg] = RULE_SAME;
901 break;
903 case DW_CFA_register:
905 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
906 ULONG_PTR reg2 = dwarf_get_uleb128( &ptr );
907 if (!valid_reg( reg ) || !valid_reg( reg2 )) break;
908 TRACE( "%lx: DW_CFA_register %s == %s\n", info->ip, dwarf_reg_names[reg], dwarf_reg_names[reg2] );
909 info->state.regs[reg] = reg2;
910 info->state.rules[reg] = RULE_OTHER_REG;
911 break;
913 case DW_CFA_remember_state:
914 TRACE( "%lx: DW_CFA_remember_state\n", info->ip );
915 if (info->state_sp >= MAX_SAVED_STATES)
916 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info->ip );
917 else
918 info->state_stack[info->state_sp++] = info->state;
919 break;
920 case DW_CFA_restore_state:
921 TRACE( "%lx: DW_CFA_restore_state\n", info->ip );
922 if (!info->state_sp)
923 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info->ip );
924 else
925 info->state = info->state_stack[--info->state_sp];
926 break;
927 case DW_CFA_def_cfa:
928 case DW_CFA_def_cfa_sf:
930 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
931 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf_get_uleb128( &ptr )
932 : dwarf_get_sleb128( &ptr ) * info->data_align;
933 if (!valid_reg( reg )) break;
934 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info->ip, dwarf_reg_names[reg], offset );
935 info->state.cfa_reg = reg;
936 info->state.cfa_offset = offset;
937 info->state.cfa_rule = RULE_CFA_OFFSET;
938 break;
940 case DW_CFA_def_cfa_register:
942 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
943 if (!valid_reg( reg )) break;
944 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info->ip, dwarf_reg_names[reg] );
945 info->state.cfa_reg = reg;
946 info->state.cfa_rule = RULE_CFA_OFFSET;
947 break;
949 case DW_CFA_def_cfa_offset:
950 case DW_CFA_def_cfa_offset_sf:
952 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf_get_uleb128( &ptr )
953 : dwarf_get_sleb128( &ptr ) * info->data_align;
954 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset );
955 info->state.cfa_offset = offset;
956 info->state.cfa_rule = RULE_CFA_OFFSET;
957 break;
959 case DW_CFA_def_cfa_expression:
961 ULONG_PTR expr = (ULONG_PTR)ptr;
962 ULONG_PTR len = dwarf_get_uleb128( &ptr );
963 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len );
964 info->state.cfa_offset = expr;
965 info->state.cfa_rule = RULE_VAL_EXPRESSION;
966 ptr += len;
967 break;
969 case DW_CFA_expression:
970 case DW_CFA_val_expression:
972 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
973 ULONG_PTR expr = (ULONG_PTR)ptr;
974 ULONG_PTR len = dwarf_get_uleb128( &ptr );
975 if (!valid_reg( reg )) break;
976 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
977 info->ip, (op == DW_CFA_expression) ? "" : "val_", dwarf_reg_names[reg], expr, expr+len );
978 info->state.regs[reg] = expr;
979 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
980 ptr += len;
981 break;
983 default:
984 FIXME( "%lx: unknown CFA opcode %02x\n", info->ip, op );
985 break;
990 /* retrieve a context register from its dwarf number */
991 static void *get_context_reg( CONTEXT *context, ULONG_PTR dw_reg )
993 switch (dw_reg)
995 case 0: return &context->Rax;
996 case 1: return &context->Rdx;
997 case 2: return &context->Rcx;
998 case 3: return &context->Rbx;
999 case 4: return &context->Rsi;
1000 case 5: return &context->Rdi;
1001 case 6: return &context->Rbp;
1002 case 7: return &context->Rsp;
1003 case 8: return &context->R8;
1004 case 9: return &context->R9;
1005 case 10: return &context->R10;
1006 case 11: return &context->R11;
1007 case 12: return &context->R12;
1008 case 13: return &context->R13;
1009 case 14: return &context->R14;
1010 case 15: return &context->R15;
1011 case 16: return &context->Rip;
1012 case 17: return &context->u.s.Xmm0;
1013 case 18: return &context->u.s.Xmm1;
1014 case 19: return &context->u.s.Xmm2;
1015 case 20: return &context->u.s.Xmm3;
1016 case 21: return &context->u.s.Xmm4;
1017 case 22: return &context->u.s.Xmm5;
1018 case 23: return &context->u.s.Xmm6;
1019 case 24: return &context->u.s.Xmm7;
1020 case 25: return &context->u.s.Xmm8;
1021 case 26: return &context->u.s.Xmm9;
1022 case 27: return &context->u.s.Xmm10;
1023 case 28: return &context->u.s.Xmm11;
1024 case 29: return &context->u.s.Xmm12;
1025 case 30: return &context->u.s.Xmm13;
1026 case 31: return &context->u.s.Xmm14;
1027 case 32: return &context->u.s.Xmm15;
1028 case 33: return &context->u.s.Legacy[0];
1029 case 34: return &context->u.s.Legacy[1];
1030 case 35: return &context->u.s.Legacy[2];
1031 case 36: return &context->u.s.Legacy[3];
1032 case 37: return &context->u.s.Legacy[4];
1033 case 38: return &context->u.s.Legacy[5];
1034 case 39: return &context->u.s.Legacy[6];
1035 case 40: return &context->u.s.Legacy[7];
1036 default: return NULL;
1040 /* set a context register from its dwarf number */
1041 static void set_context_reg( CONTEXT *context, ULONG_PTR dw_reg, void *val )
1043 switch (dw_reg)
1045 case 0: context->Rax = *(ULONG64 *)val; break;
1046 case 1: context->Rdx = *(ULONG64 *)val; break;
1047 case 2: context->Rcx = *(ULONG64 *)val; break;
1048 case 3: context->Rbx = *(ULONG64 *)val; break;
1049 case 4: context->Rsi = *(ULONG64 *)val; break;
1050 case 5: context->Rdi = *(ULONG64 *)val; break;
1051 case 6: context->Rbp = *(ULONG64 *)val; break;
1052 case 7: context->Rsp = *(ULONG64 *)val; break;
1053 case 8: context->R8 = *(ULONG64 *)val; break;
1054 case 9: context->R9 = *(ULONG64 *)val; break;
1055 case 10: context->R10 = *(ULONG64 *)val; break;
1056 case 11: context->R11 = *(ULONG64 *)val; break;
1057 case 12: context->R12 = *(ULONG64 *)val; break;
1058 case 13: context->R13 = *(ULONG64 *)val; break;
1059 case 14: context->R14 = *(ULONG64 *)val; break;
1060 case 15: context->R15 = *(ULONG64 *)val; break;
1061 case 16: context->Rip = *(ULONG64 *)val; break;
1062 case 17: context->u.s.Xmm0 = *(M128A *)val; break;
1063 case 18: context->u.s.Xmm1 = *(M128A *)val; break;
1064 case 19: context->u.s.Xmm2 = *(M128A *)val; break;
1065 case 20: context->u.s.Xmm3 = *(M128A *)val; break;
1066 case 21: context->u.s.Xmm4 = *(M128A *)val; break;
1067 case 22: context->u.s.Xmm5 = *(M128A *)val; break;
1068 case 23: context->u.s.Xmm6 = *(M128A *)val; break;
1069 case 24: context->u.s.Xmm7 = *(M128A *)val; break;
1070 case 25: context->u.s.Xmm8 = *(M128A *)val; break;
1071 case 26: context->u.s.Xmm9 = *(M128A *)val; break;
1072 case 27: context->u.s.Xmm10 = *(M128A *)val; break;
1073 case 28: context->u.s.Xmm11 = *(M128A *)val; break;
1074 case 29: context->u.s.Xmm12 = *(M128A *)val; break;
1075 case 30: context->u.s.Xmm13 = *(M128A *)val; break;
1076 case 31: context->u.s.Xmm14 = *(M128A *)val; break;
1077 case 32: context->u.s.Xmm15 = *(M128A *)val; break;
1078 case 33: context->u.s.Legacy[0] = *(M128A *)val; break;
1079 case 34: context->u.s.Legacy[1] = *(M128A *)val; break;
1080 case 35: context->u.s.Legacy[2] = *(M128A *)val; break;
1081 case 36: context->u.s.Legacy[3] = *(M128A *)val; break;
1082 case 37: context->u.s.Legacy[4] = *(M128A *)val; break;
1083 case 38: context->u.s.Legacy[5] = *(M128A *)val; break;
1084 case 39: context->u.s.Legacy[6] = *(M128A *)val; break;
1085 case 40: context->u.s.Legacy[7] = *(M128A *)val; break;
1089 static ULONG_PTR eval_expression( const unsigned char *p, CONTEXT *context )
1091 ULONG_PTR reg, tmp, stack[64];
1092 int sp = -1;
1093 ULONG_PTR len = dwarf_get_uleb128(&p);
1094 const unsigned char *end = p + len;
1096 while (p < end)
1098 unsigned char opcode = dwarf_get_u1(&p);
1100 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
1101 stack[++sp] = opcode - DW_OP_lit0;
1102 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
1103 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_reg0 );
1104 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
1105 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_breg0 ) + dwarf_get_sleb128(&p);
1106 else switch (opcode)
1108 case DW_OP_nop: break;
1109 case DW_OP_addr: stack[++sp] = dwarf_get_u8(&p); break;
1110 case DW_OP_const1u: stack[++sp] = dwarf_get_u1(&p); break;
1111 case DW_OP_const1s: stack[++sp] = (signed char)dwarf_get_u1(&p); break;
1112 case DW_OP_const2u: stack[++sp] = dwarf_get_u2(&p); break;
1113 case DW_OP_const2s: stack[++sp] = (short)dwarf_get_u2(&p); break;
1114 case DW_OP_const4u: stack[++sp] = dwarf_get_u4(&p); break;
1115 case DW_OP_const4s: stack[++sp] = (signed int)dwarf_get_u4(&p); break;
1116 case DW_OP_const8u: stack[++sp] = dwarf_get_u8(&p); break;
1117 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf_get_u8(&p); break;
1118 case DW_OP_constu: stack[++sp] = dwarf_get_uleb128(&p); break;
1119 case DW_OP_consts: stack[++sp] = dwarf_get_sleb128(&p); break;
1120 case DW_OP_deref: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1121 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
1122 case DW_OP_drop: sp--; break;
1123 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
1124 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf_get_u1(&p)]; sp++; break;
1125 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
1126 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
1127 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
1128 case DW_OP_neg: stack[sp] = -stack[sp]; break;
1129 case DW_OP_not: stack[sp] = ~stack[sp]; break;
1130 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
1131 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
1132 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
1133 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
1134 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
1135 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
1136 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
1137 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
1138 case DW_OP_plus_uconst: stack[sp] += dwarf_get_uleb128(&p); break;
1139 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
1140 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
1141 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
1142 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
1143 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
1144 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
1145 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
1146 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
1147 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
1148 case DW_OP_skip: tmp = (short)dwarf_get_u2(&p); p += tmp; break;
1149 case DW_OP_bra: tmp = (short)dwarf_get_u2(&p); if (!stack[sp--]) p += tmp; break;
1150 case DW_OP_GNU_encoded_addr: tmp = *p++; stack[++sp] = dwarf_get_ptr( &p, tmp ); break;
1151 case DW_OP_regx: stack[++sp] = *(ULONG_PTR *)get_context_reg( context, dwarf_get_uleb128(&p) ); break;
1152 case DW_OP_bregx:
1153 reg = dwarf_get_uleb128(&p);
1154 tmp = dwarf_get_sleb128(&p);
1155 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, reg ) + tmp;
1156 break;
1157 case DW_OP_deref_size:
1158 switch (*p++)
1160 case 1: stack[sp] = *(unsigned char *)stack[sp]; break;
1161 case 2: stack[sp] = *(unsigned short *)stack[sp]; break;
1162 case 4: stack[sp] = *(unsigned int *)stack[sp]; break;
1163 case 8: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1165 break;
1166 default:
1167 FIXME( "unhandled opcode %02x\n", opcode );
1170 return stack[sp];
1173 /* apply the computed frame info to the actual context */
1174 static void apply_frame_state( CONTEXT *context, struct frame_state *state )
1176 unsigned int i;
1177 ULONG_PTR cfa, value;
1178 CONTEXT new_context = *context;
1180 switch (state->cfa_rule)
1182 case RULE_EXPRESSION:
1183 cfa = *(ULONG_PTR *)eval_expression( (const unsigned char *)state->cfa_offset, context );
1184 break;
1185 case RULE_VAL_EXPRESSION:
1186 cfa = eval_expression( (const unsigned char *)state->cfa_offset, context );
1187 break;
1188 default:
1189 cfa = *(ULONG_PTR *)get_context_reg( context, state->cfa_reg ) + state->cfa_offset;
1190 break;
1192 if (!cfa) return;
1194 for (i = 0; i < NB_FRAME_REGS; i++)
1196 switch (state->rules[i])
1198 case RULE_UNSET:
1199 case RULE_UNDEFINED:
1200 case RULE_SAME:
1201 break;
1202 case RULE_CFA_OFFSET:
1203 set_context_reg( &new_context, i, (char *)cfa + state->regs[i] );
1204 break;
1205 case RULE_OTHER_REG:
1206 set_context_reg( &new_context, i, get_context_reg( context, state->regs[i] ));
1207 break;
1208 case RULE_EXPRESSION:
1209 value = eval_expression( (const unsigned char *)state->regs[i], context );
1210 set_context_reg( &new_context, i, (void *)value );
1211 break;
1212 case RULE_VAL_EXPRESSION:
1213 value = eval_expression( (const unsigned char *)state->regs[i], context );
1214 set_context_reg( &new_context, i, &value );
1215 break;
1218 new_context.Rsp = cfa;
1219 *context = new_context;
1223 /***********************************************************************
1224 * dwarf_virtual_unwind
1226 * Equivalent of RtlVirtualUnwind for builtin modules.
1228 static NTSTATUS dwarf_virtual_unwind( ULONG64 ip, ULONG64 *frame,CONTEXT *context,
1229 const struct dwarf_fde *fde, const struct dwarf_eh_bases *bases,
1230 PEXCEPTION_ROUTINE *handler, void **handler_data )
1232 const struct dwarf_cie *cie;
1233 const unsigned char *ptr, *augmentation, *end;
1234 ULONG_PTR len, code_end;
1235 struct frame_info info;
1236 struct frame_state state_stack[MAX_SAVED_STATES];
1237 int aug_z_format = 0;
1238 unsigned char lsda_encoding = DW_EH_PE_omit;
1240 memset( &info, 0, sizeof(info) );
1241 info.state_stack = state_stack;
1242 info.ip = (ULONG_PTR)bases->func;
1243 *handler = NULL;
1245 cie = (const struct dwarf_cie *)((const char *)&fde->cie_offset - fde->cie_offset);
1247 /* parse the CIE first */
1249 if (cie->version != 1)
1251 FIXME( "unknown CIE version %u at %p\n", cie->version, cie );
1252 return STATUS_INVALID_DISPOSITION;
1254 ptr = cie->augmentation + strlen((const char *)cie->augmentation) + 1;
1256 info.code_align = dwarf_get_uleb128( &ptr );
1257 info.data_align = dwarf_get_sleb128( &ptr );
1258 info.retaddr_reg = *ptr++;
1259 info.state.cfa_rule = RULE_CFA_OFFSET;
1261 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1262 ip, bases->func, cie, cie->length, cie->id, cie->version, cie->augmentation,
1263 info.code_align, info.data_align, dwarf_reg_names[info.retaddr_reg] );
1265 end = NULL;
1266 for (augmentation = cie->augmentation; *augmentation; augmentation++)
1268 switch (*augmentation)
1270 case 'z':
1271 len = dwarf_get_uleb128( &ptr );
1272 end = ptr + len;
1273 aug_z_format = 1;
1274 continue;
1275 case 'L':
1276 lsda_encoding = *ptr++;
1277 continue;
1278 case 'P':
1280 unsigned char encoding = *ptr++;
1281 *handler = (void *)dwarf_get_ptr( &ptr, encoding );
1282 continue;
1284 case 'R':
1285 info.fde_encoding = *ptr++;
1286 continue;
1287 case 'S':
1288 info.signal_frame = 1;
1289 continue;
1291 FIXME( "unknown augmentation '%c'\n", *augmentation );
1292 if (!end) return STATUS_INVALID_DISPOSITION; /* cannot continue */
1293 break;
1295 if (end) ptr = end;
1297 end = (const unsigned char *)(&cie->length + 1) + cie->length;
1298 execute_cfa_instructions( ptr, end, ip, &info );
1300 ptr = (const unsigned char *)(fde + 1);
1301 info.ip = dwarf_get_ptr( &ptr, info.fde_encoding ); /* fde code start */
1302 code_end = info.ip + dwarf_get_ptr( &ptr, info.fde_encoding & 0x0f ); /* fde code length */
1304 if (aug_z_format) /* get length of augmentation data */
1306 len = dwarf_get_uleb128( &ptr );
1307 end = ptr + len;
1309 else end = NULL;
1311 *handler_data = (void *)dwarf_get_ptr( &ptr, lsda_encoding );
1312 if (end) ptr = end;
1314 end = (const unsigned char *)(&fde->length + 1) + fde->length;
1315 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1316 fde, fde->length, *handler, *handler_data, info.ip, code_end );
1317 execute_cfa_instructions( ptr, end, ip, &info );
1318 apply_frame_state( context, &info.state );
1319 *frame = context->Rsp;
1321 TRACE( "next function rip=%016lx\n", context->Rip );
1322 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1323 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1324 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1325 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1326 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1327 context->R8, context->R9, context->R10, context->R11 );
1328 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1329 context->R12, context->R13, context->R14, context->R15 );
1331 return STATUS_SUCCESS;
1335 /***********************************************************************
1336 * dispatch_signal
1338 static inline int dispatch_signal(unsigned int sig)
1340 if (handlers[sig] == NULL) return 0;
1341 return handlers[sig](sig);
1344 /***********************************************************************
1345 * get_signal_stack
1347 * Get the base of the signal stack for the current thread.
1349 static inline void *get_signal_stack(void)
1351 return (char *)NtCurrentTeb() + teb_size;
1354 /***********************************************************************
1355 * is_inside_signal_stack
1357 * Check if pointer is inside the signal stack.
1359 static inline int is_inside_signal_stack( void *ptr )
1361 return ((char *)ptr >= (char *)get_signal_stack() &&
1362 (char *)ptr < (char *)get_signal_stack() + signal_stack_size);
1365 /***********************************************************************
1366 * save_context
1368 * Set the register values from a sigcontext.
1370 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
1372 context->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
1373 context->Rax = RAX_sig(sigcontext);
1374 context->Rcx = RCX_sig(sigcontext);
1375 context->Rdx = RDX_sig(sigcontext);
1376 context->Rbx = RBX_sig(sigcontext);
1377 context->Rsp = RSP_sig(sigcontext);
1378 context->Rbp = RBP_sig(sigcontext);
1379 context->Rsi = RSI_sig(sigcontext);
1380 context->Rdi = RDI_sig(sigcontext);
1381 context->R8 = R8_sig(sigcontext);
1382 context->R9 = R9_sig(sigcontext);
1383 context->R10 = R10_sig(sigcontext);
1384 context->R11 = R11_sig(sigcontext);
1385 context->R12 = R12_sig(sigcontext);
1386 context->R13 = R13_sig(sigcontext);
1387 context->R14 = R14_sig(sigcontext);
1388 context->R15 = R15_sig(sigcontext);
1389 context->Rip = RIP_sig(sigcontext);
1390 context->SegCs = CS_sig(sigcontext);
1391 context->SegFs = FS_sig(sigcontext);
1392 context->SegGs = GS_sig(sigcontext);
1393 context->EFlags = EFL_sig(sigcontext);
1394 #ifdef DS_sig
1395 context->SegDs = DS_sig(sigcontext);
1396 #else
1397 __asm__("movw %%ds,%0" : "=m" (context->SegDs));
1398 #endif
1399 #ifdef ES_sig
1400 context->SegEs = ES_sig(sigcontext);
1401 #else
1402 __asm__("movw %%es,%0" : "=m" (context->SegEs));
1403 #endif
1404 #ifdef SS_sig
1405 context->SegSs = SS_sig(sigcontext);
1406 #else
1407 __asm__("movw %%ss,%0" : "=m" (context->SegSs));
1408 #endif
1409 if (FPU_sig(sigcontext))
1411 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1412 context->u.FltSave = *FPU_sig(sigcontext);
1413 context->MxCsr = context->u.FltSave.MxCsr;
1418 /***********************************************************************
1419 * restore_context
1421 * Build a sigcontext from the register values.
1423 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1425 RAX_sig(sigcontext) = context->Rax;
1426 RCX_sig(sigcontext) = context->Rcx;
1427 RDX_sig(sigcontext) = context->Rdx;
1428 RBX_sig(sigcontext) = context->Rbx;
1429 RSP_sig(sigcontext) = context->Rsp;
1430 RBP_sig(sigcontext) = context->Rbp;
1431 RSI_sig(sigcontext) = context->Rsi;
1432 RDI_sig(sigcontext) = context->Rdi;
1433 R8_sig(sigcontext) = context->R8;
1434 R9_sig(sigcontext) = context->R9;
1435 R10_sig(sigcontext) = context->R10;
1436 R11_sig(sigcontext) = context->R11;
1437 R12_sig(sigcontext) = context->R12;
1438 R13_sig(sigcontext) = context->R13;
1439 R14_sig(sigcontext) = context->R14;
1440 R15_sig(sigcontext) = context->R15;
1441 RIP_sig(sigcontext) = context->Rip;
1442 CS_sig(sigcontext) = context->SegCs;
1443 FS_sig(sigcontext) = context->SegFs;
1444 GS_sig(sigcontext) = context->SegGs;
1445 EFL_sig(sigcontext) = context->EFlags;
1446 #ifdef DS_sig
1447 DS_sig(sigcontext) = context->SegDs;
1448 #endif
1449 #ifdef ES_sig
1450 ES_sig(sigcontext) = context->SegEs;
1451 #endif
1452 #ifdef SS_sig
1453 SS_sig(sigcontext) = context->SegSs;
1454 #endif
1455 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
1459 /***********************************************************************
1460 * RtlCaptureContext (NTDLL.@)
1462 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
1464 *context = *regs;
1466 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
1469 /***********************************************************************
1470 * set_cpu_context
1472 * Set the new CPU context.
1474 void set_cpu_context( const CONTEXT *context )
1476 extern void CDECL __wine_restore_regs( const CONTEXT * ) DECLSPEC_NORETURN;
1477 __wine_restore_regs( context );
1481 /***********************************************************************
1482 * copy_context
1484 * Copy a register context according to the flags.
1486 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1488 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
1489 if (flags & CONTEXT_CONTROL)
1491 to->Rbp = from->Rbp;
1492 to->Rip = from->Rip;
1493 to->Rsp = from->Rsp;
1494 to->SegCs = from->SegCs;
1495 to->SegSs = from->SegSs;
1496 to->EFlags = from->EFlags;
1498 if (flags & CONTEXT_INTEGER)
1500 to->Rax = from->Rax;
1501 to->Rcx = from->Rcx;
1502 to->Rdx = from->Rdx;
1503 to->Rbx = from->Rbx;
1504 to->Rsi = from->Rsi;
1505 to->Rdi = from->Rdi;
1506 to->R8 = from->R8;
1507 to->R9 = from->R9;
1508 to->R10 = from->R10;
1509 to->R11 = from->R11;
1510 to->R12 = from->R12;
1511 to->R13 = from->R13;
1512 to->R14 = from->R14;
1513 to->R15 = from->R15;
1515 if (flags & CONTEXT_SEGMENTS)
1517 to->SegDs = from->SegDs;
1518 to->SegEs = from->SegEs;
1519 to->SegFs = from->SegFs;
1520 to->SegGs = from->SegGs;
1522 if (flags & CONTEXT_FLOATING_POINT)
1524 to->MxCsr = from->MxCsr;
1525 to->u.FltSave = from->u.FltSave;
1527 if (flags & CONTEXT_DEBUG_REGISTERS)
1529 to->Dr0 = from->Dr0;
1530 to->Dr1 = from->Dr1;
1531 to->Dr2 = from->Dr2;
1532 to->Dr3 = from->Dr3;
1533 to->Dr6 = from->Dr6;
1534 to->Dr7 = from->Dr7;
1539 /***********************************************************************
1540 * context_to_server
1542 * Convert a register context to the server format.
1544 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1546 DWORD flags = from->ContextFlags & ~CONTEXT_AMD64; /* get rid of CPU id */
1548 memset( to, 0, sizeof(*to) );
1549 to->cpu = CPU_x86_64;
1551 if (flags & CONTEXT_CONTROL)
1553 to->flags |= SERVER_CTX_CONTROL;
1554 to->ctl.x86_64_regs.rbp = from->Rbp;
1555 to->ctl.x86_64_regs.rip = from->Rip;
1556 to->ctl.x86_64_regs.rsp = from->Rsp;
1557 to->ctl.x86_64_regs.cs = from->SegCs;
1558 to->ctl.x86_64_regs.ss = from->SegSs;
1559 to->ctl.x86_64_regs.flags = from->EFlags;
1561 if (flags & CONTEXT_INTEGER)
1563 to->flags |= SERVER_CTX_INTEGER;
1564 to->integer.x86_64_regs.rax = from->Rax;
1565 to->integer.x86_64_regs.rcx = from->Rcx;
1566 to->integer.x86_64_regs.rdx = from->Rdx;
1567 to->integer.x86_64_regs.rbx = from->Rbx;
1568 to->integer.x86_64_regs.rsi = from->Rsi;
1569 to->integer.x86_64_regs.rdi = from->Rdi;
1570 to->integer.x86_64_regs.r8 = from->R8;
1571 to->integer.x86_64_regs.r9 = from->R9;
1572 to->integer.x86_64_regs.r10 = from->R10;
1573 to->integer.x86_64_regs.r11 = from->R11;
1574 to->integer.x86_64_regs.r12 = from->R12;
1575 to->integer.x86_64_regs.r13 = from->R13;
1576 to->integer.x86_64_regs.r14 = from->R14;
1577 to->integer.x86_64_regs.r15 = from->R15;
1579 if (flags & CONTEXT_SEGMENTS)
1581 to->flags |= SERVER_CTX_SEGMENTS;
1582 to->seg.x86_64_regs.ds = from->SegDs;
1583 to->seg.x86_64_regs.es = from->SegEs;
1584 to->seg.x86_64_regs.fs = from->SegFs;
1585 to->seg.x86_64_regs.gs = from->SegGs;
1587 if (flags & CONTEXT_FLOATING_POINT)
1589 to->flags |= SERVER_CTX_FLOATING_POINT;
1590 memcpy( to->fp.x86_64_regs.fpregs, &from->u.FltSave, sizeof(to->fp.x86_64_regs.fpregs) );
1592 if (flags & CONTEXT_DEBUG_REGISTERS)
1594 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1595 to->debug.x86_64_regs.dr0 = from->Dr0;
1596 to->debug.x86_64_regs.dr1 = from->Dr1;
1597 to->debug.x86_64_regs.dr2 = from->Dr2;
1598 to->debug.x86_64_regs.dr3 = from->Dr3;
1599 to->debug.x86_64_regs.dr6 = from->Dr6;
1600 to->debug.x86_64_regs.dr7 = from->Dr7;
1602 return STATUS_SUCCESS;
1606 /***********************************************************************
1607 * context_from_server
1609 * Convert a register context from the server format.
1611 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1613 if (from->cpu != CPU_x86_64) return STATUS_INVALID_PARAMETER;
1615 to->ContextFlags = CONTEXT_AMD64;
1616 if (from->flags & SERVER_CTX_CONTROL)
1618 to->ContextFlags |= CONTEXT_CONTROL;
1619 to->Rbp = from->ctl.x86_64_regs.rbp;
1620 to->Rip = from->ctl.x86_64_regs.rip;
1621 to->Rsp = from->ctl.x86_64_regs.rsp;
1622 to->SegCs = from->ctl.x86_64_regs.cs;
1623 to->SegSs = from->ctl.x86_64_regs.ss;
1624 to->EFlags = from->ctl.x86_64_regs.flags;
1627 if (from->flags & SERVER_CTX_INTEGER)
1629 to->ContextFlags |= CONTEXT_INTEGER;
1630 to->Rax = from->integer.x86_64_regs.rax;
1631 to->Rcx = from->integer.x86_64_regs.rcx;
1632 to->Rdx = from->integer.x86_64_regs.rdx;
1633 to->Rbx = from->integer.x86_64_regs.rbx;
1634 to->Rsi = from->integer.x86_64_regs.rsi;
1635 to->Rdi = from->integer.x86_64_regs.rdi;
1636 to->R8 = from->integer.x86_64_regs.r8;
1637 to->R9 = from->integer.x86_64_regs.r9;
1638 to->R10 = from->integer.x86_64_regs.r10;
1639 to->R11 = from->integer.x86_64_regs.r11;
1640 to->R12 = from->integer.x86_64_regs.r12;
1641 to->R13 = from->integer.x86_64_regs.r13;
1642 to->R14 = from->integer.x86_64_regs.r14;
1643 to->R15 = from->integer.x86_64_regs.r15;
1645 if (from->flags & SERVER_CTX_SEGMENTS)
1647 to->ContextFlags |= CONTEXT_SEGMENTS;
1648 to->SegDs = from->seg.x86_64_regs.ds;
1649 to->SegEs = from->seg.x86_64_regs.es;
1650 to->SegFs = from->seg.x86_64_regs.fs;
1651 to->SegGs = from->seg.x86_64_regs.gs;
1653 if (from->flags & SERVER_CTX_FLOATING_POINT)
1655 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1656 memcpy( &to->u.FltSave, from->fp.x86_64_regs.fpregs, sizeof(from->fp.x86_64_regs.fpregs) );
1657 to->MxCsr = to->u.FltSave.MxCsr;
1659 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1661 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1662 to->Dr0 = from->debug.x86_64_regs.dr0;
1663 to->Dr1 = from->debug.x86_64_regs.dr1;
1664 to->Dr2 = from->debug.x86_64_regs.dr2;
1665 to->Dr3 = from->debug.x86_64_regs.dr3;
1666 to->Dr6 = from->debug.x86_64_regs.dr6;
1667 to->Dr7 = from->debug.x86_64_regs.dr7;
1669 return STATUS_SUCCESS;
1673 extern void raise_func_trampoline( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
1674 __ASM_GLOBAL_FUNC( raise_func_trampoline,
1675 __ASM_CFI(".cfi_signal_frame\n\t")
1676 __ASM_CFI(".cfi_def_cfa %rbp,144\n\t") /* red zone + rip + rbp */
1677 __ASM_CFI(".cfi_rel_offset %rip,8\n\t")
1678 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1679 "call *%rdx\n\t"
1680 "int $3")
1682 /***********************************************************************
1683 * setup_exception
1685 * Setup a proper stack frame for the raise function, and modify the
1686 * sigcontext so that the return from the signal handler will call
1687 * the raise function.
1689 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1691 struct stack_layout
1693 CONTEXT context;
1694 EXCEPTION_RECORD rec;
1695 ULONG64 rbp;
1696 ULONG64 rip;
1697 ULONG64 red_zone[16];
1698 } *stack;
1699 ULONG64 *rsp_ptr;
1700 DWORD exception_code = 0;
1702 stack = (struct stack_layout *)(RSP_sig(sigcontext) & ~15);
1704 /* stack sanity checks */
1706 if (is_inside_signal_stack( stack ))
1708 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1709 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1710 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1711 abort_thread(1);
1714 if (stack - 1 > stack || /* check for overflow in subtraction */
1715 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1716 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1718 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1719 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1720 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1722 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1724 /* stack overflow on last page, unrecoverable */
1725 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1726 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1727 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1728 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1729 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1730 abort_thread(1);
1732 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1734 /* stack access below stack limit, may be recoverable */
1735 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1736 else
1738 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1739 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1740 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1741 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1742 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1743 abort_thread(1);
1747 stack--; /* push the stack_layout structure */
1748 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1749 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1750 #elif defined(VALGRIND_MAKE_WRITABLE)
1751 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1752 #endif
1753 stack->rec.ExceptionRecord = NULL;
1754 stack->rec.ExceptionCode = exception_code;
1755 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1756 stack->rec.ExceptionAddress = (void *)RIP_sig(sigcontext);
1757 stack->rec.NumberParameters = 0;
1758 save_context( &stack->context, sigcontext );
1760 /* store return address and %rbp without aligning, so that the offset is fixed */
1761 rsp_ptr = (ULONG64 *)RSP_sig(sigcontext) - 16;
1762 *(--rsp_ptr) = RIP_sig(sigcontext);
1763 *(--rsp_ptr) = RBP_sig(sigcontext);
1765 /* now modify the sigcontext to return to the raise function */
1766 RIP_sig(sigcontext) = (ULONG_PTR)raise_func_trampoline;
1767 RDI_sig(sigcontext) = (ULONG_PTR)&stack->rec;
1768 RSI_sig(sigcontext) = (ULONG_PTR)&stack->context;
1769 RDX_sig(sigcontext) = (ULONG_PTR)func;
1770 RBP_sig(sigcontext) = (ULONG_PTR)rsp_ptr;
1771 RSP_sig(sigcontext) = (ULONG_PTR)stack;
1772 /* clear single-step, direction, and align check flag */
1773 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1775 return &stack->rec;
1779 /**********************************************************************
1780 * find_function_info
1782 static RUNTIME_FUNCTION *find_function_info( ULONG64 pc, HMODULE module,
1783 RUNTIME_FUNCTION *func, ULONG size )
1785 int min = 0;
1786 int max = size/sizeof(*func) - 1;
1788 while (min <= max)
1790 int pos = (min + max) / 2;
1791 if ((char *)pc < (char *)module + func[pos].BeginAddress) max = pos - 1;
1792 else if ((char *)pc >= (char *)module + func[pos].EndAddress) min = pos + 1;
1793 else
1795 func += pos;
1796 while (func->UnwindData & 1) /* follow chained entry */
1797 func = (RUNTIME_FUNCTION *)((char *)module + (func->UnwindData & ~1));
1798 return func;
1801 return NULL;
1805 /**********************************************************************
1806 * call_handler
1808 * Call a single exception handler.
1809 * FIXME: Handle nested exceptions.
1811 static NTSTATUS call_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch, CONTEXT *orig_context )
1813 DWORD res;
1815 dispatch->ControlPc = dispatch->ContextRecord->Rip;
1817 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
1818 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1819 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1820 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
1822 switch (res)
1824 case ExceptionContinueExecution:
1825 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1826 *orig_context = *dispatch->ContextRecord;
1827 return STATUS_SUCCESS;
1828 case ExceptionContinueSearch:
1829 break;
1830 case ExceptionNestedException:
1831 break;
1832 default:
1833 return STATUS_INVALID_DISPOSITION;
1835 return STATUS_UNHANDLED_EXCEPTION;
1839 /**********************************************************************
1840 * call_teb_handler
1842 * Call a single exception handler from the TEB chain.
1843 * FIXME: Handle nested exceptions.
1845 static NTSTATUS call_teb_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
1846 EXCEPTION_REGISTRATION_RECORD *teb_frame, CONTEXT *orig_context )
1848 EXCEPTION_REGISTRATION_RECORD *dispatcher;
1849 DWORD res;
1851 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
1852 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1853 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1854 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
1856 switch (res)
1858 case ExceptionContinueExecution:
1859 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1860 *orig_context = *dispatch->ContextRecord;
1861 return STATUS_SUCCESS;
1862 case ExceptionContinueSearch:
1863 break;
1864 case ExceptionNestedException:
1865 break;
1866 default:
1867 return STATUS_INVALID_DISPOSITION;
1869 return STATUS_UNHANDLED_EXCEPTION;
1873 /**********************************************************************
1874 * call_stack_handlers
1876 * Call the stack handlers chain.
1878 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
1880 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1881 UNWIND_HISTORY_TABLE table;
1882 DISPATCHER_CONTEXT dispatch;
1883 CONTEXT context, new_context;
1884 LDR_MODULE *module;
1885 DWORD size;
1886 NTSTATUS status;
1888 context = *orig_context;
1889 dispatch.TargetIp = 0;
1890 dispatch.ContextRecord = &context;
1891 dispatch.HistoryTable = &table;
1892 dispatch.ScopeIndex = 0; /* FIXME */
1893 for (;;)
1895 new_context = context;
1897 /* FIXME: should use the history table to make things faster */
1899 module = NULL;
1900 dispatch.ImageBase = 0;
1902 /* first look for PE exception information */
1904 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
1906 RUNTIME_FUNCTION *dir;
1908 dispatch.ImageBase = (ULONG64)module->BaseAddress;
1909 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
1910 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
1912 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
1913 dir, size )))
1915 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, dispatch.ImageBase,
1916 context.Rip, dispatch.FunctionEntry,
1917 &new_context, &dispatch.HandlerData,
1918 &dispatch.EstablisherFrame, NULL );
1919 goto unwind_done;
1922 else if (!(module->Flags & LDR_WINE_INTERNAL))
1923 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
1926 /* then look for host system exception information */
1928 if (!module || (module->Flags & LDR_WINE_INTERNAL))
1930 struct dwarf_eh_bases bases;
1931 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
1933 if (fde)
1935 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context,
1936 fde, &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
1937 if (status != STATUS_SUCCESS) return status;
1938 dispatch.FunctionEntry = NULL;
1939 if (dispatch.LanguageHandler && !module)
1941 FIXME( "calling personality routine in system library not supported yet\n" );
1942 dispatch.LanguageHandler = NULL;
1944 goto unwind_done;
1948 /* no exception information, treat as a leaf function */
1950 new_context.Rip = *(ULONG64 *)context.Rsp;
1951 new_context.Rsp = context.Rsp + sizeof(ULONG64);
1952 dispatch.EstablisherFrame = new_context.Rsp;
1953 dispatch.LanguageHandler = NULL;
1955 unwind_done:
1956 if (!dispatch.EstablisherFrame) break;
1958 if ((dispatch.EstablisherFrame & 7) ||
1959 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
1960 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
1962 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1963 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1964 rec->ExceptionFlags |= EH_STACK_INVALID;
1965 break;
1968 if (dispatch.LanguageHandler)
1970 status = call_handler( rec, &dispatch, orig_context );
1971 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
1973 /* hack: call wine handlers registered in the tib list */
1974 else while ((ULONG64)teb_frame < new_context.Rsp)
1976 TRACE( "found wine frame %p rsp %lx handler %p\n",
1977 teb_frame, new_context.Rsp, teb_frame->Handler );
1978 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1979 context = *orig_context;
1980 status = call_teb_handler( rec, &dispatch, teb_frame, orig_context );
1981 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
1982 teb_frame = teb_frame->Prev;
1985 if (new_context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
1986 context = new_context;
1988 return STATUS_UNHANDLED_EXCEPTION;
1992 /*******************************************************************
1993 * raise_exception
1995 * Implementation of NtRaiseException.
1997 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1999 NTSTATUS status;
2001 if (first_chance)
2003 DWORD c;
2005 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
2006 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
2007 context->Rip, GetCurrentThreadId() );
2008 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
2009 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
2010 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
2012 if (rec->ExceptionInformation[1] >> 16)
2013 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
2014 rec->ExceptionAddress,
2015 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
2016 else
2017 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
2018 rec->ExceptionAddress,
2019 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
2021 else
2023 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2024 context->Rax, context->Rbx, context->Rcx, context->Rdx );
2025 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2026 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
2027 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2028 context->R8, context->R9, context->R10, context->R11 );
2029 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2030 context->R12, context->R13, context->R14, context->R15 );
2032 status = send_debug_event( rec, TRUE, context );
2033 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
2034 return STATUS_SUCCESS;
2036 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
2037 return STATUS_SUCCESS;
2039 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
2040 return status;
2043 /* last chance exception */
2045 status = send_debug_event( rec, FALSE, context );
2046 if (status != DBG_CONTINUE)
2048 if (rec->ExceptionFlags & EH_STACK_INVALID)
2049 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2050 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
2051 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2052 else
2053 ERR("Unhandled exception code %x flags %x addr %p\n",
2054 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
2055 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
2057 return STATUS_SUCCESS;
2061 /**********************************************************************
2062 * raise_segv_exception
2064 static void raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2066 NTSTATUS status;
2068 switch(rec->ExceptionCode)
2070 case EXCEPTION_ACCESS_VIOLATION:
2071 if (rec->NumberParameters == 2)
2073 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
2074 rec->ExceptionInformation[0] )))
2075 set_cpu_context( context );
2077 break;
2079 status = raise_exception( rec, context, TRUE );
2080 if (status) raise_status( status, rec );
2081 set_cpu_context( context );
2085 /**********************************************************************
2086 * raise_generic_exception
2088 * Generic raise function for exceptions that don't need special treatment.
2090 static void raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2092 NTSTATUS status = raise_exception( rec, context, TRUE );
2093 if (status) raise_status( status, rec );
2094 set_cpu_context( context );
2098 /**********************************************************************
2099 * segv_handler
2101 * Handler for SIGSEGV and related errors.
2103 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2105 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_segv_exception );
2106 ucontext_t *ucontext = sigcontext;
2108 switch(TRAP_sig(ucontext))
2110 case TRAP_x86_OFLOW: /* Overflow exception */
2111 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2112 break;
2113 case TRAP_x86_BOUND: /* Bound range exception */
2114 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2115 break;
2116 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2117 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2118 break;
2119 case TRAP_x86_STKFLT: /* Stack fault */
2120 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2121 break;
2122 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2123 case TRAP_x86_PROTFLT: /* General protection fault */
2124 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2125 rec->ExceptionCode = ERROR_sig(ucontext) ? EXCEPTION_ACCESS_VIOLATION : EXCEPTION_PRIV_INSTRUCTION;
2126 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2127 break;
2128 case TRAP_x86_PAGEFLT: /* Page fault */
2129 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2130 rec->NumberParameters = 2;
2131 rec->ExceptionInformation[0] = (ERROR_sig(ucontext) & 2) != 0;
2132 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2133 break;
2134 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2135 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2136 break;
2137 default:
2138 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext) );
2139 /* fall through */
2140 case TRAP_x86_NMI: /* NMI interrupt */
2141 case TRAP_x86_DNA: /* Device not available exception */
2142 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2143 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2144 case TRAP_x86_MCHK: /* Machine check exception */
2145 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2146 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2147 break;
2151 /**********************************************************************
2152 * trap_handler
2154 * Handler for SIGTRAP.
2156 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2158 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2160 switch (siginfo->si_code)
2162 case TRAP_TRACE: /* Single-step exception */
2163 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2164 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2165 break;
2166 case TRAP_BRKPT: /* Breakpoint exception */
2167 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2168 /* fall through */
2169 default:
2170 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2171 break;
2175 /**********************************************************************
2176 * fpe_handler
2178 * Handler for SIGFPE.
2180 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2182 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2184 switch (siginfo->si_code)
2186 case FPE_FLTSUB:
2187 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2188 break;
2189 case FPE_INTDIV:
2190 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2191 break;
2192 case FPE_INTOVF:
2193 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2194 break;
2195 case FPE_FLTDIV:
2196 rec->ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
2197 break;
2198 case FPE_FLTOVF:
2199 rec->ExceptionCode = EXCEPTION_FLT_OVERFLOW;
2200 break;
2201 case FPE_FLTUND:
2202 rec->ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
2203 break;
2204 case FPE_FLTRES:
2205 rec->ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
2206 break;
2207 case FPE_FLTINV:
2208 default:
2209 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2210 break;
2214 /**********************************************************************
2215 * int_handler
2217 * Handler for SIGINT.
2219 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2221 if (!dispatch_signal(SIGINT))
2223 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2224 rec->ExceptionCode = CONTROL_C_EXIT;
2229 /**********************************************************************
2230 * abrt_handler
2232 * Handler for SIGABRT.
2234 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2236 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2237 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2238 rec->ExceptionFlags = EH_NONCONTINUABLE;
2242 /**********************************************************************
2243 * quit_handler
2245 * Handler for SIGQUIT.
2247 static void quit_handler( int signal, siginfo_t *siginfo, void *ucontext )
2249 abort_thread(0);
2253 /**********************************************************************
2254 * usr1_handler
2256 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2258 static void usr1_handler( int signal, siginfo_t *siginfo, void *ucontext )
2260 CONTEXT context;
2262 save_context( &context, ucontext );
2263 wait_suspend( &context );
2264 restore_context( &context, ucontext );
2268 /***********************************************************************
2269 * __wine_set_signal_handler (NTDLL.@)
2271 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2273 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
2274 if (handlers[sig] != NULL) return -2;
2275 handlers[sig] = wsh;
2276 return 0;
2280 /**********************************************************************
2281 * signal_alloc_thread
2283 NTSTATUS signal_alloc_thread( TEB **teb )
2285 static size_t sigstack_zero_bits;
2286 SIZE_T size;
2287 NTSTATUS status;
2289 if (!sigstack_zero_bits)
2291 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2292 /* find the first power of two not smaller than min_size */
2293 sigstack_zero_bits = 12;
2294 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2295 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2296 assert( sizeof(TEB) <= teb_size );
2299 size = 1 << sigstack_zero_bits;
2300 *teb = NULL;
2301 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
2302 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2304 (*teb)->Tib.Self = &(*teb)->Tib;
2305 (*teb)->Tib.ExceptionList = (void *)~0UL;
2307 return status;
2311 /**********************************************************************
2312 * signal_free_thread
2314 void signal_free_thread( TEB *teb )
2316 SIZE_T size;
2318 if (teb->DeallocationStack)
2320 size = 0;
2321 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2323 size = 0;
2324 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2328 /**********************************************************************
2329 * signal_init_thread
2331 void signal_init_thread( TEB *teb )
2333 const WORD fpu_cw = 0x27f;
2334 stack_t ss;
2336 #if defined __linux__
2337 arch_prctl( ARCH_SET_GS, teb );
2338 #elif defined __FreeBSD__
2339 amd64_set_gsbase( teb );
2340 #else
2341 # error Please define setting %gs for your architecture
2342 #endif
2344 ss.ss_sp = (char *)teb + teb_size;
2345 ss.ss_size = signal_stack_size;
2346 ss.ss_flags = 0;
2347 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2349 #ifdef __GNUC__
2350 __asm__ volatile ("fninit; fldcw %0" : : "m" (fpu_cw));
2351 #else
2352 FIXME("FPU setup not implemented for this platform.\n");
2353 #endif
2356 /**********************************************************************
2357 * signal_init_process
2359 void signal_init_process(void)
2361 struct sigaction sig_act;
2363 sig_act.sa_mask = server_block_set;
2364 sig_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
2366 sig_act.sa_sigaction = int_handler;
2367 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2368 sig_act.sa_sigaction = fpe_handler;
2369 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2370 sig_act.sa_sigaction = abrt_handler;
2371 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2372 sig_act.sa_sigaction = quit_handler;
2373 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2374 sig_act.sa_sigaction = usr1_handler;
2375 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2377 sig_act.sa_sigaction = segv_handler;
2378 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2379 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2380 #ifdef SIGBUS
2381 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2382 #endif
2384 #ifdef SIGTRAP
2385 sig_act.sa_sigaction = trap_handler;
2386 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2387 #endif
2388 return;
2390 error:
2391 perror("sigaction");
2392 exit(1);
2396 /**********************************************************************
2397 * RtlAddFunctionTable (NTDLL.@)
2399 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD64 addr )
2401 FIXME( "%p %u %lx: stub\n", table, count, addr );
2402 return FALSE;
2406 /**********************************************************************
2407 * RtlDeleteFunctionTable (NTDLL.@)
2409 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
2411 FIXME( "%p: stub\n", table );
2412 return FALSE;
2416 /**********************************************************************
2417 * RtlLookupFunctionEntry (NTDLL.@)
2419 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base, UNWIND_HISTORY_TABLE *table )
2421 LDR_MODULE *module;
2422 RUNTIME_FUNCTION *func;
2423 ULONG size;
2425 /* FIXME: should use the history table to make things faster */
2427 if (LdrFindEntryForAddress( (void *)pc, &module ))
2429 WARN( "module not found for %lx\n", pc );
2430 return NULL;
2432 if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2433 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2435 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
2436 return NULL;
2438 func = find_function_info( pc, module->BaseAddress, func, size );
2439 if (func) *base = (ULONG64)module->BaseAddress;
2440 return func;
2443 static ULONG64 get_int_reg( CONTEXT *context, int reg )
2445 return *(&context->Rax + reg);
2448 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 val )
2450 *(&context->Rax + reg) = val;
2451 if (ctx_ptr) ctx_ptr->u2.IntegerContext[reg] = &context->Rax + reg;
2454 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A val )
2456 *(&context->u.s.Xmm0 + reg) = val;
2457 if (ctx_ptr) ctx_ptr->u1.FloatingContext[reg] = &context->u.s.Xmm0 + reg;
2460 static int get_opcode_size( struct opcode op )
2462 switch (op.code)
2464 case UWOP_ALLOC_LARGE:
2465 return 2 + (op.info != 0);
2466 case UWOP_SAVE_NONVOL:
2467 case UWOP_SAVE_XMM128:
2468 return 2;
2469 case UWOP_SAVE_NONVOL_FAR:
2470 case UWOP_SAVE_XMM128_FAR:
2471 return 3;
2472 default:
2473 return 1;
2477 static BOOL is_inside_epilog( BYTE *pc )
2479 /* add or lea must be the first instruction, and it must have a rex.W prefix */
2480 if ((pc[0] & 0xf8) == 0x48)
2482 switch (pc[1])
2484 case 0x81: /* add $nnnn,%rsp */
2485 if (pc[0] == 0x48 && pc[2] == 0xc4)
2487 pc += 7;
2488 break;
2490 return FALSE;
2491 case 0x83: /* add $n,%rsp */
2492 if (pc[0] == 0x48 && pc[2] == 0xc4)
2494 pc += 4;
2495 break;
2497 return FALSE;
2498 case 0x8d: /* lea n(reg),%rsp */
2499 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
2500 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
2501 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
2502 if ((pc[2] >> 6) == 1) /* 8-bit offset */
2504 pc += 4;
2505 break;
2507 if ((pc[2] >> 6) == 2) /* 32-bit offset */
2509 pc += 7;
2510 break;
2512 return FALSE;
2516 /* now check for various pop instructions */
2518 for (;;)
2520 BYTE rex = 0;
2522 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2524 switch (*pc)
2526 case 0x58: /* pop %rax/%r8 */
2527 case 0x59: /* pop %rcx/%r9 */
2528 case 0x5a: /* pop %rdx/%r10 */
2529 case 0x5b: /* pop %rbx/%r11 */
2530 case 0x5c: /* pop %rsp/%r12 */
2531 case 0x5d: /* pop %rbp/%r13 */
2532 case 0x5e: /* pop %rsi/%r14 */
2533 case 0x5f: /* pop %rdi/%r15 */
2534 pc++;
2535 continue;
2536 case 0xc2: /* ret $nn */
2537 case 0xc3: /* ret */
2538 return TRUE;
2539 /* FIXME: add various jump instructions */
2541 return FALSE;
2545 /* execute a function epilog, which must have been validated with is_inside_epilog() */
2546 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2548 for (;;)
2550 BYTE rex = 0;
2552 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2554 switch (*pc)
2556 case 0x58: /* pop %rax/r8 */
2557 case 0x59: /* pop %rcx/r9 */
2558 case 0x5a: /* pop %rdx/r10 */
2559 case 0x5b: /* pop %rbx/r11 */
2560 case 0x5c: /* pop %rsp/r12 */
2561 case 0x5d: /* pop %rbp/r13 */
2562 case 0x5e: /* pop %rsi/r14 */
2563 case 0x5f: /* pop %rdi/r15 */
2564 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, *(ULONG64 *)context->Rsp );
2565 context->Rsp += sizeof(ULONG64);
2566 pc++;
2567 continue;
2568 case 0x81: /* add $nnnn,%rsp */
2569 context->Rsp += *(LONG *)(pc + 2);
2570 pc += 2 + sizeof(LONG);
2571 continue;
2572 case 0x83: /* add $n,%rsp */
2573 context->Rsp += (signed char)pc[2];
2574 pc += 3;
2575 continue;
2576 case 0x8d:
2577 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
2579 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
2580 pc += 3;
2582 else /* lea nnnn(reg),%rsp */
2584 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
2585 pc += 2 + sizeof(LONG);
2587 continue;
2588 case 0xc2: /* ret $nn */
2589 context->Rip = *(ULONG64 *)context->Rsp;
2590 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
2591 return;
2592 case 0xc3: /* ret */
2593 context->Rip = *(ULONG64 *)context->Rsp;
2594 context->Rsp += sizeof(ULONG64);
2595 return;
2596 /* FIXME: add various jump instructions */
2598 return;
2602 /**********************************************************************
2603 * RtlVirtualUnwind (NTDLL.@)
2605 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
2606 RUNTIME_FUNCTION *function, CONTEXT *context,
2607 PVOID *data, ULONG64 *frame_ret,
2608 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2610 union handler_data *handler_data;
2611 ULONG64 frame, off;
2612 struct UNWIND_INFO *info;
2613 unsigned int i, prolog_offset;
2615 TRACE( "type %x rip %lx rsp %lx\n", type, pc, context->Rsp );
2616 if (TRACE_ON(seh)) dump_unwind_info( base, function );
2618 frame = *frame_ret = context->Rsp;
2619 for (;;)
2621 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
2622 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
2624 if (info->version != 1)
2626 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
2627 return NULL;
2630 if (info->frame_reg)
2631 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
2633 /* check if in prolog */
2634 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
2636 prolog_offset = pc - base - function->BeginAddress;
2638 else
2640 prolog_offset = ~0;
2641 if (is_inside_epilog( (BYTE *)pc ))
2643 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
2644 *frame_ret = frame;
2645 return NULL;
2649 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
2651 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
2653 switch (info->opcodes[i].code)
2655 case UWOP_PUSH_NONVOL: /* pushq %reg */
2656 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)context->Rsp );
2657 context->Rsp += sizeof(ULONG64);
2658 break;
2659 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
2660 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
2661 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
2662 break;
2663 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
2664 context->Rsp += (info->opcodes[i].info + 1) * 8;
2665 break;
2666 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
2667 context->Rsp = *frame_ret = frame;
2668 break;
2669 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
2670 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
2671 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2672 break;
2673 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
2674 off = frame + *(DWORD *)&info->opcodes[i+1];
2675 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2676 break;
2677 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
2678 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
2679 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2680 break;
2681 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
2682 off = frame + *(DWORD *)&info->opcodes[i+1];
2683 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2684 break;
2685 case UWOP_PUSH_MACHFRAME:
2686 FIXME( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
2687 break;
2688 default:
2689 FIXME( "unknown code %u\n", info->opcodes[i].code );
2690 break;
2694 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
2695 function = &handler_data->chain; /* restart with the chained info */
2698 /* now pop return address */
2699 context->Rip = *(ULONG64 *)context->Rsp;
2700 context->Rsp += sizeof(ULONG64);
2702 if (!(info->flags & type)) return NULL; /* no matching handler */
2703 if (prolog_offset != ~0) return NULL; /* inside prolog */
2705 *data = &handler_data->handler + 1;
2706 return (char *)base + handler_data->handler;
2710 /**********************************************************************
2711 * call_unwind_handler
2713 * Call a single unwind handler.
2714 * FIXME: Handle nested exceptions.
2716 static void call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
2718 DWORD res;
2720 dispatch->ControlPc = dispatch->ContextRecord->Rip;
2722 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2723 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2724 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2725 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
2727 switch (res)
2729 case ExceptionContinueSearch:
2730 break;
2731 case ExceptionCollidedUnwind:
2732 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2733 break;
2734 default:
2735 raise_status( STATUS_INVALID_DISPOSITION, rec );
2736 break;
2741 /**********************************************************************
2742 * call_teb_unwind_handler
2744 * Call a single unwind handler from the TEB chain.
2745 * FIXME: Handle nested exceptions.
2747 static void call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
2748 EXCEPTION_REGISTRATION_RECORD *teb_frame )
2750 EXCEPTION_REGISTRATION_RECORD *dispatcher;
2751 DWORD res;
2753 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2754 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2755 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2756 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
2758 switch (res)
2760 case ExceptionContinueSearch:
2761 break;
2762 case ExceptionCollidedUnwind:
2763 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2764 break;
2765 default:
2766 raise_status( STATUS_INVALID_DISPOSITION, rec );
2767 break;
2772 /*******************************************************************
2773 * RtlUnwindEx (NTDLL.@)
2775 void WINAPI RtlUnwindEx( ULONG64 end_frame, ULONG64 target_ip, EXCEPTION_RECORD *rec,
2776 ULONG64 retval, CONTEXT *orig_context, UNWIND_HISTORY_TABLE *table )
2778 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
2779 EXCEPTION_RECORD record;
2780 DISPATCHER_CONTEXT dispatch;
2781 CONTEXT context, new_context;
2782 LDR_MODULE *module;
2783 NTSTATUS status;
2784 DWORD size;
2786 /* build an exception record, if we do not have one */
2787 if (!rec)
2789 record.ExceptionCode = STATUS_UNWIND;
2790 record.ExceptionFlags = 0;
2791 record.ExceptionRecord = NULL;
2792 record.ExceptionAddress = (void *)orig_context->Rip;
2793 record.NumberParameters = 0;
2794 rec = &record;
2797 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
2799 TRACE( "code=%x flags=%x end_frame=%lx target_ip=%lx rip=%016lx\n",
2800 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, orig_context->Rip );
2801 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2802 orig_context->Rax, orig_context->Rbx, orig_context->Rcx, orig_context->Rdx );
2803 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2804 orig_context->Rsi, orig_context->Rdi, orig_context->Rbp, orig_context->Rsp );
2805 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2806 orig_context->R8, orig_context->R9, orig_context->R10, orig_context->R11 );
2807 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2808 orig_context->R12, orig_context->R13, orig_context->R14, orig_context->R15 );
2810 context = *orig_context;
2811 dispatch.EstablisherFrame = context.Rsp;
2812 dispatch.TargetIp = target_ip;
2813 dispatch.ContextRecord = &context;
2814 dispatch.HistoryTable = table;
2816 while (dispatch.EstablisherFrame != end_frame)
2818 new_context = context;
2820 /* FIXME: should use the history table to make things faster */
2822 module = NULL;
2823 dispatch.ImageBase = 0;
2824 dispatch.ScopeIndex = 0; /* FIXME */
2826 /* first look for PE exception information */
2828 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
2830 RUNTIME_FUNCTION *dir;
2832 dispatch.ImageBase = (ULONG64)module->BaseAddress;
2833 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2834 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2836 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
2837 dir, size )))
2839 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_UHANDLER, dispatch.ImageBase,
2840 context.Rip, dispatch.FunctionEntry,
2841 &new_context, &dispatch.HandlerData,
2842 &dispatch.EstablisherFrame, NULL );
2843 goto unwind_done;
2846 else if (!(module->Flags & LDR_WINE_INTERNAL))
2847 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
2850 /* then look for host system exception information */
2852 if (!module || (module->Flags & LDR_WINE_INTERNAL))
2854 struct dwarf_eh_bases bases;
2855 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
2857 if (fde)
2859 dispatch.FunctionEntry = NULL;
2860 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context, fde,
2861 &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
2862 if (status != STATUS_SUCCESS) raise_status( status, rec );
2863 if (dispatch.LanguageHandler && !module)
2865 FIXME( "calling personality routine in system library not supported yet\n" );
2866 dispatch.LanguageHandler = NULL;
2868 goto unwind_done;
2872 /* no exception information, treat as a leaf function */
2874 new_context.Rip = *(ULONG64 *)context.Rsp;
2875 new_context.Rsp = context.Rsp + sizeof(ULONG64);
2876 dispatch.EstablisherFrame = new_context.Rsp;
2877 dispatch.LanguageHandler = NULL;
2879 unwind_done:
2880 if (!dispatch.EstablisherFrame) break;
2882 if (is_inside_signal_stack( (void *)dispatch.EstablisherFrame ))
2884 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch.EstablisherFrame,
2885 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size );
2886 context = new_context;
2887 continue;
2890 if ((dispatch.EstablisherFrame & 7) ||
2891 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
2892 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
2894 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
2895 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2896 rec->ExceptionFlags |= EH_STACK_INVALID;
2897 break;
2900 if (dispatch.LanguageHandler)
2902 if (end_frame && (dispatch.EstablisherFrame > end_frame))
2904 ERR( "invalid end frame %lx/%lx\n", dispatch.EstablisherFrame, end_frame );
2905 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
2907 call_unwind_handler( rec, &dispatch );
2909 else /* hack: call builtin handlers registered in the tib list */
2911 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < end_frame)
2913 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
2914 dispatch.EstablisherFrame = (ULONG64)teb_frame;
2915 call_teb_unwind_handler( rec, &dispatch, teb_frame );
2916 teb_frame = __wine_pop_frame( teb_frame );
2918 if ((ULONG64)teb_frame == end_frame && end_frame < new_context.Rsp) break;
2919 dispatch.EstablisherFrame = new_context.Rsp;
2922 context = new_context;
2924 context.Rax = retval;
2925 context.Rip = target_ip;
2926 TRACE( "returning to %lx stack %lx\n", context.Rip, context.Rsp );
2927 set_cpu_context( &context );
2931 /*******************************************************************
2932 * RtlUnwind (NTDLL.@)
2934 void WINAPI __regs_RtlUnwind( ULONG64 frame, ULONG64 target_ip, EXCEPTION_RECORD *rec,
2935 ULONG64 retval, CONTEXT *context )
2937 RtlUnwindEx( frame, target_ip, rec, retval, context, NULL );
2939 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2942 /*******************************************************************
2943 * __C_specific_handler (NTDLL.@)
2945 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
2946 ULONG64 frame,
2947 CONTEXT *context,
2948 struct _DISPATCHER_CONTEXT *dispatch )
2950 SCOPE_TABLE *table = dispatch->HandlerData;
2951 ULONG i;
2953 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
2954 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
2956 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)) /* FIXME */
2957 return ExceptionContinueSearch;
2959 for (i = 0; i < table->Count; i++)
2961 if (context->Rip >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
2962 context->Rip < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
2964 if (!table->ScopeRecord[i].JumpTarget) continue;
2965 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
2967 EXCEPTION_POINTERS ptrs;
2968 PC_LANGUAGE_EXCEPTION_HANDLER filter;
2970 filter = (PC_LANGUAGE_EXCEPTION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
2971 ptrs.ExceptionRecord = rec;
2972 ptrs.ContextRecord = context;
2973 TRACE( "calling filter %p ptrs %p frame %lx\n", filter, &ptrs, frame );
2974 switch (filter( &ptrs, frame ))
2976 case EXCEPTION_EXECUTE_HANDLER:
2977 break;
2978 case EXCEPTION_CONTINUE_SEARCH:
2979 continue;
2980 case EXCEPTION_CONTINUE_EXECUTION:
2981 return ExceptionContinueExecution;
2984 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
2985 RtlUnwindEx( frame, dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
2986 rec, 0, context, dispatch->HistoryTable );
2989 return ExceptionContinueSearch;
2993 /*******************************************************************
2994 * NtRaiseException (NTDLL.@)
2996 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2998 NTSTATUS status = raise_exception( rec, context, first_chance );
2999 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
3000 return status;
3004 /***********************************************************************
3005 * RtlRaiseException (NTDLL.@)
3007 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
3009 NTSTATUS status;
3011 rec->ExceptionAddress = (void *)context->Rip;
3012 status = raise_exception( rec, context, TRUE );
3013 if (status != STATUS_SUCCESS) raise_status( status, rec );
3015 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
3018 /*************************************************************************
3019 * RtlCaptureStackBackTrace (NTDLL.@)
3021 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
3023 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
3024 return 0;
3028 /***********************************************************************
3029 * call_thread_func
3031 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
3033 ntdll_get_thread_data()->exit_frame = frame;
3034 __TRY
3036 RtlExitUserThread( entry( arg ));
3038 __EXCEPT(unhandled_exception_filter)
3040 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
3042 __ENDTRY
3043 abort(); /* should not be reached */
3046 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
3047 __ASM_GLOBAL_FUNC( call_thread_entry_point,
3048 "subq $56,%rsp\n\t"
3049 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3050 "movq %rbp,48(%rsp)\n\t"
3051 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3052 "movq %rbx,40(%rsp)\n\t"
3053 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3054 "movq %r12,32(%rsp)\n\t"
3055 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3056 "movq %r13,24(%rsp)\n\t"
3057 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3058 "movq %r14,16(%rsp)\n\t"
3059 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3060 "movq %r15,8(%rsp)\n\t"
3061 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3062 "movq %rsp,%rdx\n\t"
3063 "call " __ASM_NAME("call_thread_func") );
3065 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
3066 __ASM_GLOBAL_FUNC( call_thread_exit_func,
3067 "movq %rdx,%rsp\n\t"
3068 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3069 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3070 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3071 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3072 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3073 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3074 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3075 "call *%rsi" );
3077 /***********************************************************************
3078 * RtlExitUserThread (NTDLL.@)
3080 void WINAPI RtlExitUserThread( ULONG status )
3082 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
3083 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
3086 /***********************************************************************
3087 * abort_thread
3089 void abort_thread( int status )
3091 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
3092 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
3095 /**********************************************************************
3096 * __wine_enter_vm86 (NTDLL.@)
3098 void __wine_enter_vm86( CONTEXT *context )
3100 MESSAGE("vm86 mode not supported on this platform\n");
3103 /**********************************************************************
3104 * DbgBreakPoint (NTDLL.@)
3106 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
3108 /**********************************************************************
3109 * DbgUserBreakPoint (NTDLL.@)
3111 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
3113 #endif /* __x86_64__ */