jscript: Add Italian translation.
[wine.git] / dlls / ntdll / signal_x86_64.c
blobccea6df01e116539014c939869eaca0b7f7d0e7d
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_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #ifdef HAVE_MACHINE_SYSARCH_H
37 # include <machine/sysarch.h>
38 #endif
40 #ifdef HAVE_SYS_PARAM_H
41 # include <sys/param.h>
42 #endif
43 #ifdef HAVE_SYS_SIGNAL_H
44 # include <sys/signal.h>
45 #endif
47 #define NONAMELESSUNION
48 #define NONAMELESSSTRUCT
49 #include "ntstatus.h"
50 #define WIN32_NO_STATUS
51 #include "windef.h"
52 #include "winternl.h"
53 #include "wine/library.h"
54 #include "wine/exception.h"
55 #include "ntdll_misc.h"
56 #include "wine/debug.h"
58 #ifdef HAVE_VALGRIND_MEMCHECK_H
59 #include <valgrind/memcheck.h>
60 #endif
62 WINE_DEFAULT_DEBUG_CHANNEL(seh);
64 struct _DISPATCHER_CONTEXT;
66 typedef LONG (WINAPI *PC_LANGUAGE_EXCEPTION_HANDLER)( EXCEPTION_POINTERS *ptrs, ULONG64 frame );
67 typedef EXCEPTION_DISPOSITION (WINAPI *PEXCEPTION_ROUTINE)( EXCEPTION_RECORD *rec,
68 ULONG64 frame,
69 CONTEXT *context,
70 struct _DISPATCHER_CONTEXT *dispatch );
72 typedef struct _DISPATCHER_CONTEXT
74 ULONG64 ControlPc;
75 ULONG64 ImageBase;
76 PRUNTIME_FUNCTION FunctionEntry;
77 ULONG64 EstablisherFrame;
78 ULONG64 TargetIp;
79 PCONTEXT ContextRecord;
80 PEXCEPTION_ROUTINE LanguageHandler;
81 PVOID HandlerData;
82 PUNWIND_HISTORY_TABLE HistoryTable;
83 ULONG ScopeIndex;
84 } DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;
86 typedef struct _SCOPE_TABLE
88 ULONG Count;
89 struct
91 ULONG BeginAddress;
92 ULONG EndAddress;
93 ULONG HandlerAddress;
94 ULONG JumpTarget;
95 } ScopeRecord[1];
96 } SCOPE_TABLE, *PSCOPE_TABLE;
99 /***********************************************************************
100 * signal context platform-specific definitions
102 #ifdef linux
104 #include <asm/prctl.h>
105 extern int arch_prctl(int func, void *ptr);
107 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
108 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
109 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
110 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
111 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
112 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
113 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
114 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
115 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
116 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
117 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
118 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
119 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
120 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
121 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
123 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
124 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
125 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
127 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
128 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
129 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
130 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
131 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
133 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
135 #endif /* linux */
137 #if defined(__NetBSD__)
138 # include <sys/ucontext.h>
139 # include <sys/types.h>
140 # include <signal.h>
142 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
143 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
144 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
145 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
146 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
147 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
148 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
149 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
150 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
151 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
152 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
153 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
154 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
155 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
156 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
158 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
159 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
160 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
161 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
162 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
163 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
165 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
167 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
168 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
170 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
171 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
173 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
174 #endif /* __NetBSD__ */
176 enum i386_trap_code
178 TRAP_x86_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
179 TRAP_x86_DIVIDE = 0, /* Division by zero exception */
180 TRAP_x86_TRCTRAP = 1, /* Single-step exception */
181 TRAP_x86_NMI = 2, /* NMI interrupt */
182 TRAP_x86_BPTFLT = 3, /* Breakpoint exception */
183 TRAP_x86_OFLOW = 4, /* Overflow exception */
184 TRAP_x86_BOUND = 5, /* Bound range exception */
185 TRAP_x86_PRIVINFLT = 6, /* Invalid opcode exception */
186 TRAP_x86_DNA = 7, /* Device not available exception */
187 TRAP_x86_DOUBLEFLT = 8, /* Double fault exception */
188 TRAP_x86_FPOPFLT = 9, /* Coprocessor segment overrun */
189 TRAP_x86_TSSFLT = 10, /* Invalid TSS exception */
190 TRAP_x86_SEGNPFLT = 11, /* Segment not present exception */
191 TRAP_x86_STKFLT = 12, /* Stack fault */
192 TRAP_x86_PROTFLT = 13, /* General protection fault */
193 TRAP_x86_PAGEFLT = 14, /* Page fault */
194 TRAP_x86_ARITHTRAP = 16, /* Floating point exception */
195 TRAP_x86_ALIGNFLT = 17, /* Alignment check exception */
196 TRAP_x86_MCHK = 18, /* Machine check exception */
197 TRAP_x86_CACHEFLT = 19 /* Cache flush exception */
200 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
201 static size_t signal_stack_size;
203 typedef void (*raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
204 typedef int (*wine_signal_handler)(unsigned int sig);
206 static wine_signal_handler handlers[256];
209 /***********************************************************************
210 * Definitions for Win32 unwind tables
213 union handler_data
215 RUNTIME_FUNCTION chain;
216 ULONG handler;
219 struct opcode
221 BYTE offset;
222 BYTE code : 4;
223 BYTE info : 4;
226 struct UNWIND_INFO
228 BYTE version : 3;
229 BYTE flags : 5;
230 BYTE prolog;
231 BYTE count;
232 BYTE frame_reg : 4;
233 BYTE frame_offset : 4;
234 struct opcode opcodes[1]; /* info->count entries */
235 /* followed by handler_data */
238 #define UWOP_PUSH_NONVOL 0
239 #define UWOP_ALLOC_LARGE 1
240 #define UWOP_ALLOC_SMALL 2
241 #define UWOP_SET_FPREG 3
242 #define UWOP_SAVE_NONVOL 4
243 #define UWOP_SAVE_NONVOL_FAR 5
244 #define UWOP_SAVE_XMM128 8
245 #define UWOP_SAVE_XMM128_FAR 9
246 #define UWOP_PUSH_MACHFRAME 10
248 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
250 static const char * const reg_names[16] =
251 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
252 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
254 union handler_data *handler_data;
255 struct UNWIND_INFO *info;
256 unsigned int i, count;
258 TRACE( "**** func %x-%x\n", function->BeginAddress, function->EndAddress );
259 for (;;)
261 if (function->UnwindData & 1)
263 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
264 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
265 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
266 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
267 function = next;
268 continue;
270 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
272 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
273 info, info->flags, info->prolog,
274 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
276 if (info->frame_reg)
277 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
278 reg_names[info->frame_reg], info->frame_offset * 16 );
280 for (i = 0; i < info->count; i++)
282 TRACE( " 0x%x: ", info->opcodes[i].offset );
283 switch (info->opcodes[i].code)
285 case UWOP_PUSH_NONVOL:
286 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
287 break;
288 case UWOP_ALLOC_LARGE:
289 if (info->opcodes[i].info)
291 count = *(DWORD *)&info->opcodes[i+1];
292 i += 2;
294 else
296 count = *(USHORT *)&info->opcodes[i+1] * 8;
297 i++;
299 TRACE( "subq $0x%x,%%rsp\n", count );
300 break;
301 case UWOP_ALLOC_SMALL:
302 count = (info->opcodes[i].info + 1) * 8;
303 TRACE( "subq $0x%x,%%rsp\n", count );
304 break;
305 case UWOP_SET_FPREG:
306 TRACE( "leaq 0x%x(%%rsp),%s\n",
307 info->frame_offset * 16, reg_names[info->frame_reg] );
308 break;
309 case UWOP_SAVE_NONVOL:
310 count = *(USHORT *)&info->opcodes[i+1] * 8;
311 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
312 i++;
313 break;
314 case UWOP_SAVE_NONVOL_FAR:
315 count = *(DWORD *)&info->opcodes[i+1];
316 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
317 i += 2;
318 break;
319 case UWOP_SAVE_XMM128:
320 count = *(USHORT *)&info->opcodes[i+1] * 16;
321 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
322 i++;
323 break;
324 case UWOP_SAVE_XMM128_FAR:
325 count = *(DWORD *)&info->opcodes[i+1];
326 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
327 i += 2;
328 break;
329 case UWOP_PUSH_MACHFRAME:
330 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
331 break;
332 default:
333 FIXME( "unknown code %u\n", info->opcodes[i].code );
334 break;
338 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
339 if (info->flags & UNW_FLAG_CHAININFO)
341 TRACE( " chained to function %p-%p\n",
342 (char *)base + handler_data->chain.BeginAddress,
343 (char *)base + handler_data->chain.EndAddress );
344 function = &handler_data->chain;
345 continue;
347 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
348 TRACE( " handler %p data at %p\n",
349 (char *)base + handler_data->handler, &handler_data->handler + 1 );
350 break;
354 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
356 unsigned int i;
358 TRACE( "scope table at %p\n", table );
359 for (i = 0; i < table->Count; i++)
360 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
361 base + table->ScopeRecord[i].BeginAddress,
362 base + table->ScopeRecord[i].EndAddress,
363 base + table->ScopeRecord[i].HandlerAddress,
364 base + table->ScopeRecord[i].JumpTarget );
368 /***********************************************************************
369 * Definitions for Dwarf unwind tables
372 enum dwarf_call_frame_info
374 DW_CFA_advance_loc = 0x40,
375 DW_CFA_offset = 0x80,
376 DW_CFA_restore = 0xc0,
377 DW_CFA_nop = 0x00,
378 DW_CFA_set_loc = 0x01,
379 DW_CFA_advance_loc1 = 0x02,
380 DW_CFA_advance_loc2 = 0x03,
381 DW_CFA_advance_loc4 = 0x04,
382 DW_CFA_offset_extended = 0x05,
383 DW_CFA_restore_extended = 0x06,
384 DW_CFA_undefined = 0x07,
385 DW_CFA_same_value = 0x08,
386 DW_CFA_register = 0x09,
387 DW_CFA_remember_state = 0x0a,
388 DW_CFA_restore_state = 0x0b,
389 DW_CFA_def_cfa = 0x0c,
390 DW_CFA_def_cfa_register = 0x0d,
391 DW_CFA_def_cfa_offset = 0x0e,
392 DW_CFA_def_cfa_expression = 0x0f,
393 DW_CFA_expression = 0x10,
394 DW_CFA_offset_extended_sf = 0x11,
395 DW_CFA_def_cfa_sf = 0x12,
396 DW_CFA_def_cfa_offset_sf = 0x13,
397 DW_CFA_val_offset = 0x14,
398 DW_CFA_val_offset_sf = 0x15,
399 DW_CFA_val_expression = 0x16,
402 enum dwarf_operation
404 DW_OP_addr = 0x03,
405 DW_OP_deref = 0x06,
406 DW_OP_const1u = 0x08,
407 DW_OP_const1s = 0x09,
408 DW_OP_const2u = 0x0a,
409 DW_OP_const2s = 0x0b,
410 DW_OP_const4u = 0x0c,
411 DW_OP_const4s = 0x0d,
412 DW_OP_const8u = 0x0e,
413 DW_OP_const8s = 0x0f,
414 DW_OP_constu = 0x10,
415 DW_OP_consts = 0x11,
416 DW_OP_dup = 0x12,
417 DW_OP_drop = 0x13,
418 DW_OP_over = 0x14,
419 DW_OP_pick = 0x15,
420 DW_OP_swap = 0x16,
421 DW_OP_rot = 0x17,
422 DW_OP_xderef = 0x18,
423 DW_OP_abs = 0x19,
424 DW_OP_and = 0x1a,
425 DW_OP_div = 0x1b,
426 DW_OP_minus = 0x1c,
427 DW_OP_mod = 0x1d,
428 DW_OP_mul = 0x1e,
429 DW_OP_neg = 0x1f,
430 DW_OP_not = 0x20,
431 DW_OP_or = 0x21,
432 DW_OP_plus = 0x22,
433 DW_OP_plus_uconst = 0x23,
434 DW_OP_shl = 0x24,
435 DW_OP_shr = 0x25,
436 DW_OP_shra = 0x26,
437 DW_OP_xor = 0x27,
438 DW_OP_bra = 0x28,
439 DW_OP_eq = 0x29,
440 DW_OP_ge = 0x2a,
441 DW_OP_gt = 0x2b,
442 DW_OP_le = 0x2c,
443 DW_OP_lt = 0x2d,
444 DW_OP_ne = 0x2e,
445 DW_OP_skip = 0x2f,
446 DW_OP_lit0 = 0x30,
447 DW_OP_lit1 = 0x31,
448 DW_OP_lit2 = 0x32,
449 DW_OP_lit3 = 0x33,
450 DW_OP_lit4 = 0x34,
451 DW_OP_lit5 = 0x35,
452 DW_OP_lit6 = 0x36,
453 DW_OP_lit7 = 0x37,
454 DW_OP_lit8 = 0x38,
455 DW_OP_lit9 = 0x39,
456 DW_OP_lit10 = 0x3a,
457 DW_OP_lit11 = 0x3b,
458 DW_OP_lit12 = 0x3c,
459 DW_OP_lit13 = 0x3d,
460 DW_OP_lit14 = 0x3e,
461 DW_OP_lit15 = 0x3f,
462 DW_OP_lit16 = 0x40,
463 DW_OP_lit17 = 0x41,
464 DW_OP_lit18 = 0x42,
465 DW_OP_lit19 = 0x43,
466 DW_OP_lit20 = 0x44,
467 DW_OP_lit21 = 0x45,
468 DW_OP_lit22 = 0x46,
469 DW_OP_lit23 = 0x47,
470 DW_OP_lit24 = 0x48,
471 DW_OP_lit25 = 0x49,
472 DW_OP_lit26 = 0x4a,
473 DW_OP_lit27 = 0x4b,
474 DW_OP_lit28 = 0x4c,
475 DW_OP_lit29 = 0x4d,
476 DW_OP_lit30 = 0x4e,
477 DW_OP_lit31 = 0x4f,
478 DW_OP_reg0 = 0x50,
479 DW_OP_reg1 = 0x51,
480 DW_OP_reg2 = 0x52,
481 DW_OP_reg3 = 0x53,
482 DW_OP_reg4 = 0x54,
483 DW_OP_reg5 = 0x55,
484 DW_OP_reg6 = 0x56,
485 DW_OP_reg7 = 0x57,
486 DW_OP_reg8 = 0x58,
487 DW_OP_reg9 = 0x59,
488 DW_OP_reg10 = 0x5a,
489 DW_OP_reg11 = 0x5b,
490 DW_OP_reg12 = 0x5c,
491 DW_OP_reg13 = 0x5d,
492 DW_OP_reg14 = 0x5e,
493 DW_OP_reg15 = 0x5f,
494 DW_OP_reg16 = 0x60,
495 DW_OP_reg17 = 0x61,
496 DW_OP_reg18 = 0x62,
497 DW_OP_reg19 = 0x63,
498 DW_OP_reg20 = 0x64,
499 DW_OP_reg21 = 0x65,
500 DW_OP_reg22 = 0x66,
501 DW_OP_reg23 = 0x67,
502 DW_OP_reg24 = 0x68,
503 DW_OP_reg25 = 0x69,
504 DW_OP_reg26 = 0x6a,
505 DW_OP_reg27 = 0x6b,
506 DW_OP_reg28 = 0x6c,
507 DW_OP_reg29 = 0x6d,
508 DW_OP_reg30 = 0x6e,
509 DW_OP_reg31 = 0x6f,
510 DW_OP_breg0 = 0x70,
511 DW_OP_breg1 = 0x71,
512 DW_OP_breg2 = 0x72,
513 DW_OP_breg3 = 0x73,
514 DW_OP_breg4 = 0x74,
515 DW_OP_breg5 = 0x75,
516 DW_OP_breg6 = 0x76,
517 DW_OP_breg7 = 0x77,
518 DW_OP_breg8 = 0x78,
519 DW_OP_breg9 = 0x79,
520 DW_OP_breg10 = 0x7a,
521 DW_OP_breg11 = 0x7b,
522 DW_OP_breg12 = 0x7c,
523 DW_OP_breg13 = 0x7d,
524 DW_OP_breg14 = 0x7e,
525 DW_OP_breg15 = 0x7f,
526 DW_OP_breg16 = 0x80,
527 DW_OP_breg17 = 0x81,
528 DW_OP_breg18 = 0x82,
529 DW_OP_breg19 = 0x83,
530 DW_OP_breg20 = 0x84,
531 DW_OP_breg21 = 0x85,
532 DW_OP_breg22 = 0x86,
533 DW_OP_breg23 = 0x87,
534 DW_OP_breg24 = 0x88,
535 DW_OP_breg25 = 0x89,
536 DW_OP_breg26 = 0x8a,
537 DW_OP_breg27 = 0x8b,
538 DW_OP_breg28 = 0x8c,
539 DW_OP_breg29 = 0x8d,
540 DW_OP_breg30 = 0x8e,
541 DW_OP_breg31 = 0x8f,
542 DW_OP_regx = 0x90,
543 DW_OP_fbreg = 0x91,
544 DW_OP_bregx = 0x92,
545 DW_OP_piece = 0x93,
546 DW_OP_deref_size = 0x94,
547 DW_OP_xderef_size = 0x95,
548 DW_OP_nop = 0x96,
549 DW_OP_push_object_address = 0x97,
550 DW_OP_call2 = 0x98,
551 DW_OP_call4 = 0x99,
552 DW_OP_call_ref = 0x9a,
553 DW_OP_form_tls_address = 0x9b,
554 DW_OP_call_frame_cfa = 0x9c,
555 DW_OP_bit_piece = 0x9d,
556 DW_OP_lo_user = 0xe0,
557 DW_OP_hi_user = 0xff,
558 DW_OP_GNU_push_tls_address = 0xe0,
559 DW_OP_GNU_uninit = 0xf0,
560 DW_OP_GNU_encoded_addr = 0xf1,
563 #define DW_EH_PE_native 0x00
564 #define DW_EH_PE_leb128 0x01
565 #define DW_EH_PE_data2 0x02
566 #define DW_EH_PE_data4 0x03
567 #define DW_EH_PE_data8 0x04
568 #define DW_EH_PE_signed 0x08
569 #define DW_EH_PE_abs 0x00
570 #define DW_EH_PE_pcrel 0x10
571 #define DW_EH_PE_textrel 0x20
572 #define DW_EH_PE_datarel 0x30
573 #define DW_EH_PE_funcrel 0x40
574 #define DW_EH_PE_aligned 0x50
575 #define DW_EH_PE_indirect 0x80
576 #define DW_EH_PE_omit 0xff
578 struct dwarf_eh_bases
580 void *tbase;
581 void *dbase;
582 void *func;
585 struct dwarf_cie
587 unsigned int length;
588 int id;
589 unsigned char version;
590 unsigned char augmentation[1];
593 struct dwarf_fde
595 unsigned int length;
596 unsigned int cie_offset;
599 extern const struct dwarf_fde *_Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
601 static unsigned char dwarf_get_u1( const unsigned char **p )
603 return *(*p)++;
606 static unsigned short dwarf_get_u2( const unsigned char **p )
608 unsigned int ret = (*p)[0] | ((*p)[1] << 8);
609 (*p) += 2;
610 return ret;
613 static unsigned int dwarf_get_u4( const unsigned char **p )
615 unsigned int ret = (*p)[0] | ((*p)[1] << 8) | ((*p)[2] << 16) | ((*p)[3] << 24);
616 (*p) += 4;
617 return ret;
620 static ULONG64 dwarf_get_u8( const unsigned char **p )
622 ULONG64 low = dwarf_get_u4( p );
623 ULONG64 high = dwarf_get_u4( p );
624 return low | (high << 32);
627 static ULONG_PTR dwarf_get_uleb128( const unsigned char **p )
629 ULONG_PTR ret = 0;
630 unsigned int shift = 0;
631 unsigned char byte;
635 byte = **p;
636 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
637 shift += 7;
638 (*p)++;
639 } while (byte & 0x80);
640 return ret;
643 static LONG_PTR dwarf_get_sleb128( const unsigned char **p )
645 ULONG_PTR ret = 0;
646 unsigned int shift = 0;
647 unsigned char byte;
651 byte = **p;
652 ret |= (ULONG_PTR)(byte & 0x7f) << shift;
653 shift += 7;
654 (*p)++;
655 } while (byte & 0x80);
657 if ((shift < 8 * sizeof(ret)) && (byte & 0x40)) ret |= -((ULONG_PTR)1 << shift);
658 return ret;
661 static ULONG_PTR dwarf_get_ptr( const unsigned char **p, unsigned char encoding )
663 ULONG_PTR base;
665 if (encoding == DW_EH_PE_omit) return 0;
667 switch (encoding & 0xf0)
669 case DW_EH_PE_abs:
670 base = 0;
671 break;
672 case DW_EH_PE_pcrel:
673 base = (ULONG_PTR)*p;
674 break;
675 default:
676 FIXME( "unsupported encoding %02x\n", encoding );
677 return 0;
680 switch (encoding & 0x0f)
682 case DW_EH_PE_native:
683 return base + dwarf_get_u8( p );
684 case DW_EH_PE_leb128:
685 return base + dwarf_get_uleb128( p );
686 case DW_EH_PE_data2:
687 return base + dwarf_get_u2( p );
688 case DW_EH_PE_data4:
689 return base + dwarf_get_u4( p );
690 case DW_EH_PE_data8:
691 return base + dwarf_get_u8( p );
692 case DW_EH_PE_signed|DW_EH_PE_leb128:
693 return base + dwarf_get_sleb128( p );
694 case DW_EH_PE_signed|DW_EH_PE_data2:
695 return base + (signed short)dwarf_get_u2( p );
696 case DW_EH_PE_signed|DW_EH_PE_data4:
697 return base + (signed int)dwarf_get_u4( p );
698 case DW_EH_PE_signed|DW_EH_PE_data8:
699 return base + (LONG64)dwarf_get_u8( p );
700 default:
701 FIXME( "unsupported encoding %02x\n", encoding );
702 return 0;
706 enum reg_rule
708 RULE_UNSET, /* not set at all */
709 RULE_UNDEFINED, /* undefined value */
710 RULE_SAME, /* same value as previous frame */
711 RULE_CFA_OFFSET, /* stored at cfa offset */
712 RULE_OTHER_REG, /* stored in other register */
713 RULE_EXPRESSION, /* address specified by expression */
714 RULE_VAL_EXPRESSION /* value specified by expression */
717 #define NB_FRAME_REGS 41
718 #define MAX_SAVED_STATES 16
720 struct frame_state
722 ULONG_PTR cfa_offset;
723 unsigned char cfa_reg;
724 enum reg_rule cfa_rule;
725 enum reg_rule rules[NB_FRAME_REGS];
726 ULONG64 regs[NB_FRAME_REGS];
729 struct frame_info
731 ULONG_PTR ip;
732 ULONG_PTR code_align;
733 LONG_PTR data_align;
734 unsigned char retaddr_reg;
735 unsigned char fde_encoding;
736 unsigned char signal_frame;
737 unsigned char state_sp;
738 struct frame_state state;
739 struct frame_state *state_stack;
742 static const char *dwarf_reg_names[NB_FRAME_REGS] =
744 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
745 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
746 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
747 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
748 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
751 static int valid_reg( ULONG_PTR reg )
753 if (reg >= NB_FRAME_REGS) FIXME( "unsupported reg %lx\n", reg );
754 return (reg < NB_FRAME_REGS);
757 static void execute_cfa_instructions( const unsigned char *ptr, const unsigned char *end,
758 ULONG_PTR last_ip, struct frame_info *info )
760 while (ptr < end && info->ip < last_ip + info->signal_frame)
762 enum dwarf_call_frame_info op = *ptr++;
764 if (op & 0xc0)
766 switch (op & 0xc0)
768 case DW_CFA_advance_loc:
770 ULONG_PTR offset = (op & 0x3f) * info->code_align;
771 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info->ip, offset );
772 info->ip += offset;
773 break;
775 case DW_CFA_offset:
777 ULONG_PTR reg = op & 0x3f;
778 LONG_PTR offset = dwarf_get_uleb128( &ptr ) * info->data_align;
779 if (!valid_reg( reg )) break;
780 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
781 info->state.regs[reg] = offset;
782 info->state.rules[reg] = RULE_CFA_OFFSET;
783 break;
785 case DW_CFA_restore:
787 ULONG_PTR reg = op & 0x3f;
788 if (!valid_reg( reg )) break;
789 TRACE( "%lx: DW_CFA_restore %s\n", info->ip, dwarf_reg_names[reg] );
790 info->state.rules[reg] = RULE_UNSET;
791 break;
795 else switch (op)
797 case DW_CFA_nop:
798 break;
799 case DW_CFA_set_loc:
801 ULONG_PTR loc = dwarf_get_ptr( &ptr, info->fde_encoding );
802 TRACE( "%lx: DW_CFA_set_loc %lx\n", info->ip, loc );
803 info->ip = loc;
804 break;
806 case DW_CFA_advance_loc1:
808 ULONG_PTR offset = *ptr++ * info->code_align;
809 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset );
810 info->ip += offset;
811 break;
813 case DW_CFA_advance_loc2:
815 ULONG_PTR offset = dwarf_get_u2( &ptr ) * info->code_align;
816 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset );
817 info->ip += offset;
818 break;
820 case DW_CFA_advance_loc4:
822 ULONG_PTR offset = dwarf_get_u4( &ptr ) * info->code_align;
823 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset );
824 info->ip += offset;
825 break;
827 case DW_CFA_offset_extended:
828 case DW_CFA_offset_extended_sf:
830 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
831 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf_get_uleb128( &ptr ) * info->data_align
832 : dwarf_get_sleb128( &ptr ) * info->data_align;
833 if (!valid_reg( reg )) break;
834 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info->ip, dwarf_reg_names[reg], offset );
835 info->state.regs[reg] = offset;
836 info->state.rules[reg] = RULE_CFA_OFFSET;
837 break;
839 case DW_CFA_restore_extended:
841 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
842 if (!valid_reg( reg )) break;
843 TRACE( "%lx: DW_CFA_restore_extended %s\n", info->ip, dwarf_reg_names[reg] );
844 info->state.rules[reg] = RULE_UNSET;
845 break;
847 case DW_CFA_undefined:
849 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
850 if (!valid_reg( reg )) break;
851 TRACE( "%lx: DW_CFA_undefined %s\n", info->ip, dwarf_reg_names[reg] );
852 info->state.rules[reg] = RULE_UNDEFINED;
853 break;
855 case DW_CFA_same_value:
857 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
858 if (!valid_reg( reg )) break;
859 TRACE( "%lx: DW_CFA_same_value %s\n", info->ip, dwarf_reg_names[reg] );
860 info->state.regs[reg] = reg;
861 info->state.rules[reg] = RULE_SAME;
862 break;
864 case DW_CFA_register:
866 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
867 ULONG_PTR reg2 = dwarf_get_uleb128( &ptr );
868 if (!valid_reg( reg ) || !valid_reg( reg2 )) break;
869 TRACE( "%lx: DW_CFA_register %s == %s\n", info->ip, dwarf_reg_names[reg], dwarf_reg_names[reg2] );
870 info->state.regs[reg] = reg2;
871 info->state.rules[reg] = RULE_OTHER_REG;
872 break;
874 case DW_CFA_remember_state:
875 TRACE( "%lx: DW_CFA_remember_state\n", info->ip );
876 if (info->state_sp >= MAX_SAVED_STATES)
877 FIXME( "%lx: DW_CFA_remember_state too many nested saves\n", info->ip );
878 else
879 info->state_stack[info->state_sp++] = info->state;
880 break;
881 case DW_CFA_restore_state:
882 TRACE( "%lx: DW_CFA_restore_state\n", info->ip );
883 if (!info->state_sp)
884 FIXME( "%lx: DW_CFA_restore_state without corresponding save\n", info->ip );
885 else
886 info->state = info->state_stack[--info->state_sp];
887 break;
888 case DW_CFA_def_cfa:
889 case DW_CFA_def_cfa_sf:
891 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
892 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf_get_uleb128( &ptr )
893 : dwarf_get_sleb128( &ptr ) * info->data_align;
894 if (!valid_reg( reg )) break;
895 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info->ip, dwarf_reg_names[reg], offset );
896 info->state.cfa_reg = reg;
897 info->state.cfa_offset = offset;
898 info->state.cfa_rule = RULE_CFA_OFFSET;
899 break;
901 case DW_CFA_def_cfa_register:
903 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
904 if (!valid_reg( reg )) break;
905 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info->ip, dwarf_reg_names[reg] );
906 info->state.cfa_reg = reg;
907 info->state.cfa_rule = RULE_CFA_OFFSET;
908 break;
910 case DW_CFA_def_cfa_offset:
911 case DW_CFA_def_cfa_offset_sf:
913 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf_get_uleb128( &ptr )
914 : dwarf_get_sleb128( &ptr ) * info->data_align;
915 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset );
916 info->state.cfa_offset = offset;
917 info->state.cfa_rule = RULE_CFA_OFFSET;
918 break;
920 case DW_CFA_def_cfa_expression:
922 ULONG_PTR expr = (ULONG_PTR)ptr;
923 ULONG_PTR len = dwarf_get_uleb128( &ptr );
924 TRACE( "%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len );
925 info->state.cfa_offset = expr;
926 info->state.cfa_rule = RULE_VAL_EXPRESSION;
927 ptr += len;
928 break;
930 case DW_CFA_expression:
931 case DW_CFA_val_expression:
933 ULONG_PTR reg = dwarf_get_uleb128( &ptr );
934 ULONG_PTR expr = (ULONG_PTR)ptr;
935 ULONG_PTR len = dwarf_get_uleb128( &ptr );
936 if (!valid_reg( reg )) break;
937 TRACE( "%lx: DW_CFA_%sexpression %s %lx-%lx\n",
938 info->ip, (op == DW_CFA_expression) ? "" : "val_", dwarf_reg_names[reg], expr, expr+len );
939 info->state.regs[reg] = expr;
940 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
941 ptr += len;
942 break;
944 default:
945 FIXME( "%lx: unknown CFA opcode %02x\n", info->ip, op );
946 break;
951 /* retrieve a context register from its dwarf number */
952 static void *get_context_reg( CONTEXT *context, ULONG_PTR dw_reg )
954 switch (dw_reg)
956 case 0: return &context->Rax;
957 case 1: return &context->Rdx;
958 case 2: return &context->Rcx;
959 case 3: return &context->Rbx;
960 case 4: return &context->Rsi;
961 case 5: return &context->Rdi;
962 case 6: return &context->Rbp;
963 case 7: return &context->Rsp;
964 case 8: return &context->R8;
965 case 9: return &context->R9;
966 case 10: return &context->R10;
967 case 11: return &context->R11;
968 case 12: return &context->R12;
969 case 13: return &context->R13;
970 case 14: return &context->R14;
971 case 15: return &context->R15;
972 case 16: return &context->Rip;
973 case 17: return &context->u.s.Xmm0;
974 case 18: return &context->u.s.Xmm1;
975 case 19: return &context->u.s.Xmm2;
976 case 20: return &context->u.s.Xmm3;
977 case 21: return &context->u.s.Xmm4;
978 case 22: return &context->u.s.Xmm5;
979 case 23: return &context->u.s.Xmm6;
980 case 24: return &context->u.s.Xmm7;
981 case 25: return &context->u.s.Xmm8;
982 case 26: return &context->u.s.Xmm9;
983 case 27: return &context->u.s.Xmm10;
984 case 28: return &context->u.s.Xmm11;
985 case 29: return &context->u.s.Xmm12;
986 case 30: return &context->u.s.Xmm13;
987 case 31: return &context->u.s.Xmm14;
988 case 32: return &context->u.s.Xmm15;
989 case 33: return &context->u.s.Legacy[0];
990 case 34: return &context->u.s.Legacy[1];
991 case 35: return &context->u.s.Legacy[2];
992 case 36: return &context->u.s.Legacy[3];
993 case 37: return &context->u.s.Legacy[4];
994 case 38: return &context->u.s.Legacy[5];
995 case 39: return &context->u.s.Legacy[6];
996 case 40: return &context->u.s.Legacy[7];
997 default: return NULL;
1001 /* set a context register from its dwarf number */
1002 static void set_context_reg( CONTEXT *context, ULONG_PTR dw_reg, void *val )
1004 switch (dw_reg)
1006 case 0: context->Rax = *(ULONG64 *)val; break;
1007 case 1: context->Rdx = *(ULONG64 *)val; break;
1008 case 2: context->Rcx = *(ULONG64 *)val; break;
1009 case 3: context->Rbx = *(ULONG64 *)val; break;
1010 case 4: context->Rsi = *(ULONG64 *)val; break;
1011 case 5: context->Rdi = *(ULONG64 *)val; break;
1012 case 6: context->Rbp = *(ULONG64 *)val; break;
1013 case 7: context->Rsp = *(ULONG64 *)val; break;
1014 case 8: context->R8 = *(ULONG64 *)val; break;
1015 case 9: context->R9 = *(ULONG64 *)val; break;
1016 case 10: context->R10 = *(ULONG64 *)val; break;
1017 case 11: context->R11 = *(ULONG64 *)val; break;
1018 case 12: context->R12 = *(ULONG64 *)val; break;
1019 case 13: context->R13 = *(ULONG64 *)val; break;
1020 case 14: context->R14 = *(ULONG64 *)val; break;
1021 case 15: context->R15 = *(ULONG64 *)val; break;
1022 case 16: context->Rip = *(ULONG64 *)val; break;
1023 case 17: context->u.s.Xmm0 = *(M128A *)val; break;
1024 case 18: context->u.s.Xmm1 = *(M128A *)val; break;
1025 case 19: context->u.s.Xmm2 = *(M128A *)val; break;
1026 case 20: context->u.s.Xmm3 = *(M128A *)val; break;
1027 case 21: context->u.s.Xmm4 = *(M128A *)val; break;
1028 case 22: context->u.s.Xmm5 = *(M128A *)val; break;
1029 case 23: context->u.s.Xmm6 = *(M128A *)val; break;
1030 case 24: context->u.s.Xmm7 = *(M128A *)val; break;
1031 case 25: context->u.s.Xmm8 = *(M128A *)val; break;
1032 case 26: context->u.s.Xmm9 = *(M128A *)val; break;
1033 case 27: context->u.s.Xmm10 = *(M128A *)val; break;
1034 case 28: context->u.s.Xmm11 = *(M128A *)val; break;
1035 case 29: context->u.s.Xmm12 = *(M128A *)val; break;
1036 case 30: context->u.s.Xmm13 = *(M128A *)val; break;
1037 case 31: context->u.s.Xmm14 = *(M128A *)val; break;
1038 case 32: context->u.s.Xmm15 = *(M128A *)val; break;
1039 case 33: context->u.s.Legacy[0] = *(M128A *)val; break;
1040 case 34: context->u.s.Legacy[1] = *(M128A *)val; break;
1041 case 35: context->u.s.Legacy[2] = *(M128A *)val; break;
1042 case 36: context->u.s.Legacy[3] = *(M128A *)val; break;
1043 case 37: context->u.s.Legacy[4] = *(M128A *)val; break;
1044 case 38: context->u.s.Legacy[5] = *(M128A *)val; break;
1045 case 39: context->u.s.Legacy[6] = *(M128A *)val; break;
1046 case 40: context->u.s.Legacy[7] = *(M128A *)val; break;
1050 static ULONG_PTR eval_expression( const unsigned char *p, CONTEXT *context )
1052 ULONG_PTR reg, tmp, stack[64];
1053 int sp = -1;
1054 ULONG_PTR len = dwarf_get_uleb128(&p);
1055 const unsigned char *end = p + len;
1057 while (p < end)
1059 unsigned char opcode = dwarf_get_u1(&p);
1061 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
1062 stack[++sp] = opcode - DW_OP_lit0;
1063 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
1064 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_reg0 );
1065 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
1066 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, opcode - DW_OP_breg0 ) + dwarf_get_sleb128(&p);
1067 else switch (opcode)
1069 case DW_OP_nop: break;
1070 case DW_OP_addr: stack[++sp] = dwarf_get_u8(&p); break;
1071 case DW_OP_const1u: stack[++sp] = dwarf_get_u1(&p); break;
1072 case DW_OP_const1s: stack[++sp] = (signed char)dwarf_get_u1(&p); break;
1073 case DW_OP_const2u: stack[++sp] = dwarf_get_u2(&p); break;
1074 case DW_OP_const2s: stack[++sp] = (short)dwarf_get_u2(&p); break;
1075 case DW_OP_const4u: stack[++sp] = dwarf_get_u4(&p); break;
1076 case DW_OP_const4s: stack[++sp] = (signed int)dwarf_get_u4(&p); break;
1077 case DW_OP_const8u: stack[++sp] = dwarf_get_u8(&p); break;
1078 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf_get_u8(&p); break;
1079 case DW_OP_constu: stack[++sp] = dwarf_get_uleb128(&p); break;
1080 case DW_OP_consts: stack[++sp] = dwarf_get_sleb128(&p); break;
1081 case DW_OP_deref: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1082 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
1083 case DW_OP_drop: sp--; break;
1084 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
1085 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf_get_u1(&p)]; sp++; break;
1086 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
1087 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
1088 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
1089 case DW_OP_neg: stack[sp] = -stack[sp]; break;
1090 case DW_OP_not: stack[sp] = ~stack[sp]; break;
1091 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
1092 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
1093 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
1094 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
1095 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
1096 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
1097 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
1098 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
1099 case DW_OP_plus_uconst: stack[sp] += dwarf_get_uleb128(&p); break;
1100 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
1101 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
1102 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
1103 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
1104 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
1105 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
1106 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
1107 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
1108 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
1109 case DW_OP_skip: tmp = (short)dwarf_get_u2(&p); p += tmp; break;
1110 case DW_OP_bra: tmp = (short)dwarf_get_u2(&p); if (!stack[sp--]) p += tmp; break;
1111 case DW_OP_GNU_encoded_addr: tmp = *p++; stack[++sp] = dwarf_get_ptr( &p, tmp ); break;
1112 case DW_OP_regx: stack[++sp] = *(ULONG_PTR *)get_context_reg( context, dwarf_get_uleb128(&p) ); break;
1113 case DW_OP_bregx:
1114 reg = dwarf_get_uleb128(&p);
1115 tmp = dwarf_get_sleb128(&p);
1116 stack[++sp] = *(ULONG_PTR *)get_context_reg( context, reg ) + tmp;
1117 break;
1118 case DW_OP_deref_size:
1119 switch (*p++)
1121 case 1: stack[sp] = *(unsigned char *)stack[sp]; break;
1122 case 2: stack[sp] = *(unsigned short *)stack[sp]; break;
1123 case 4: stack[sp] = *(unsigned int *)stack[sp]; break;
1124 case 8: stack[sp] = *(ULONG_PTR *)stack[sp]; break;
1126 break;
1127 default:
1128 FIXME( "unhandled opcode %02x\n", opcode );
1131 return stack[sp];
1134 /* apply the computed frame info to the actual context */
1135 static void apply_frame_state( CONTEXT *context, struct frame_state *state )
1137 unsigned int i;
1138 ULONG_PTR cfa, value;
1139 CONTEXT new_context = *context;
1141 switch (state->cfa_rule)
1143 case RULE_EXPRESSION:
1144 cfa = *(ULONG_PTR *)eval_expression( (const unsigned char *)state->cfa_offset, context );
1145 break;
1146 case RULE_VAL_EXPRESSION:
1147 cfa = eval_expression( (const unsigned char *)state->cfa_offset, context );
1148 break;
1149 default:
1150 cfa = *(ULONG_PTR *)get_context_reg( context, state->cfa_reg ) + state->cfa_offset;
1151 break;
1153 if (!cfa) return;
1155 for (i = 0; i < NB_FRAME_REGS; i++)
1157 switch (state->rules[i])
1159 case RULE_UNSET:
1160 case RULE_UNDEFINED:
1161 case RULE_SAME:
1162 break;
1163 case RULE_CFA_OFFSET:
1164 set_context_reg( &new_context, i, (char *)cfa + state->regs[i] );
1165 break;
1166 case RULE_OTHER_REG:
1167 set_context_reg( &new_context, i, get_context_reg( context, state->regs[i] ));
1168 break;
1169 case RULE_EXPRESSION:
1170 value = eval_expression( (const unsigned char *)state->regs[i], context );
1171 set_context_reg( &new_context, i, (void *)value );
1172 break;
1173 case RULE_VAL_EXPRESSION:
1174 value = eval_expression( (const unsigned char *)state->regs[i], context );
1175 set_context_reg( &new_context, i, &value );
1176 break;
1179 new_context.Rsp = cfa;
1180 *context = new_context;
1184 /***********************************************************************
1185 * dwarf_virtual_unwind
1187 * Equivalent of RtlVirtualUnwind for builtin modules.
1189 static NTSTATUS dwarf_virtual_unwind( ULONG64 ip, ULONG64 *frame,CONTEXT *context,
1190 const struct dwarf_fde *fde, const struct dwarf_eh_bases *bases,
1191 PEXCEPTION_ROUTINE *handler, void **handler_data )
1193 const struct dwarf_cie *cie;
1194 const unsigned char *ptr, *augmentation, *end;
1195 ULONG_PTR len, code_end;
1196 struct frame_info info;
1197 struct frame_state state_stack[MAX_SAVED_STATES];
1198 int aug_z_format = 0;
1199 unsigned char lsda_encoding = DW_EH_PE_omit;
1201 memset( &info, 0, sizeof(info) );
1202 info.state_stack = state_stack;
1203 info.ip = (ULONG_PTR)bases->func;
1204 *handler = NULL;
1206 cie = (const struct dwarf_cie *)((const char *)&fde->cie_offset - fde->cie_offset);
1208 /* parse the CIE first */
1210 if (cie->version != 1)
1212 FIXME( "unknown CIE version %u at %p\n", cie->version, cie );
1213 return STATUS_INVALID_DISPOSITION;
1215 ptr = cie->augmentation + strlen((const char *)cie->augmentation) + 1;
1217 info.code_align = dwarf_get_uleb128( &ptr );
1218 info.data_align = dwarf_get_sleb128( &ptr );
1219 info.retaddr_reg = *ptr++;
1220 info.state.cfa_rule = RULE_CFA_OFFSET;
1222 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
1223 ip, bases->func, cie, cie->length, cie->id, cie->version, cie->augmentation,
1224 info.code_align, info.data_align, dwarf_reg_names[info.retaddr_reg] );
1226 end = NULL;
1227 for (augmentation = cie->augmentation; *augmentation; augmentation++)
1229 switch (*augmentation)
1231 case 'z':
1232 len = dwarf_get_uleb128( &ptr );
1233 end = ptr + len;
1234 aug_z_format = 1;
1235 continue;
1236 case 'L':
1237 lsda_encoding = *ptr++;
1238 continue;
1239 case 'P':
1241 unsigned char encoding = *ptr++;
1242 *handler = (void *)dwarf_get_ptr( &ptr, encoding );
1243 continue;
1245 case 'R':
1246 info.fde_encoding = *ptr++;
1247 continue;
1248 case 'S':
1249 info.signal_frame = 1;
1250 continue;
1252 FIXME( "unknown augmentation '%c'\n", *augmentation );
1253 if (!end) return STATUS_INVALID_DISPOSITION; /* cannot continue */
1254 break;
1256 if (end) ptr = end;
1258 end = (const unsigned char *)(&cie->length + 1) + cie->length;
1259 execute_cfa_instructions( ptr, end, ip, &info );
1261 ptr = (const unsigned char *)(fde + 1);
1262 info.ip = dwarf_get_ptr( &ptr, info.fde_encoding ); /* fde code start */
1263 code_end = info.ip + dwarf_get_ptr( &ptr, info.fde_encoding & 0x0f ); /* fde code length */
1265 if (aug_z_format) /* get length of augmentation data */
1267 len = dwarf_get_uleb128( &ptr );
1268 end = ptr + len;
1270 else end = NULL;
1272 *handler_data = (void *)dwarf_get_ptr( &ptr, lsda_encoding );
1273 if (end) ptr = end;
1275 end = (const unsigned char *)(&fde->length + 1) + fde->length;
1276 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
1277 fde, fde->length, *handler, *handler_data, info.ip, code_end );
1278 execute_cfa_instructions( ptr, end, ip, &info );
1279 apply_frame_state( context, &info.state );
1280 *frame = context->Rsp;
1282 TRACE( "next function rip=%016lx\n", context->Rip );
1283 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1284 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1285 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1286 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1287 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1288 context->R8, context->R9, context->R10, context->R11 );
1289 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1290 context->R12, context->R13, context->R14, context->R15 );
1292 return STATUS_SUCCESS;
1296 /***********************************************************************
1297 * dispatch_signal
1299 static inline int dispatch_signal(unsigned int sig)
1301 if (handlers[sig] == NULL) return 0;
1302 return handlers[sig](sig);
1305 /***********************************************************************
1306 * get_signal_stack
1308 * Get the base of the signal stack for the current thread.
1310 static inline void *get_signal_stack(void)
1312 return (char *)NtCurrentTeb() + teb_size;
1315 /***********************************************************************
1316 * is_inside_signal_stack
1318 * Check if pointer is inside the signal stack.
1320 static inline int is_inside_signal_stack( void *ptr )
1322 return ((char *)ptr >= (char *)get_signal_stack() &&
1323 (char *)ptr < (char *)get_signal_stack() + signal_stack_size);
1326 /***********************************************************************
1327 * save_context
1329 * Set the register values from a sigcontext.
1331 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
1333 context->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
1334 context->Rax = RAX_sig(sigcontext);
1335 context->Rcx = RCX_sig(sigcontext);
1336 context->Rdx = RDX_sig(sigcontext);
1337 context->Rbx = RBX_sig(sigcontext);
1338 context->Rsp = RSP_sig(sigcontext);
1339 context->Rbp = RBP_sig(sigcontext);
1340 context->Rsi = RSI_sig(sigcontext);
1341 context->Rdi = RDI_sig(sigcontext);
1342 context->R8 = R8_sig(sigcontext);
1343 context->R9 = R9_sig(sigcontext);
1344 context->R10 = R10_sig(sigcontext);
1345 context->R11 = R11_sig(sigcontext);
1346 context->R12 = R12_sig(sigcontext);
1347 context->R13 = R13_sig(sigcontext);
1348 context->R14 = R14_sig(sigcontext);
1349 context->R15 = R15_sig(sigcontext);
1350 context->Rip = RIP_sig(sigcontext);
1351 context->SegCs = CS_sig(sigcontext);
1352 context->SegFs = FS_sig(sigcontext);
1353 context->SegGs = GS_sig(sigcontext);
1354 context->EFlags = EFL_sig(sigcontext);
1355 #ifdef DS_sig
1356 context->SegDs = DS_sig(sigcontext);
1357 #else
1358 __asm__("movw %%ds,%0" : "=m" (context->SegDs));
1359 #endif
1360 #ifdef ES_sig
1361 context->SegEs = ES_sig(sigcontext);
1362 #else
1363 __asm__("movw %%es,%0" : "=m" (context->SegEs));
1364 #endif
1365 #ifdef SS_sig
1366 context->SegSs = SS_sig(sigcontext);
1367 #else
1368 __asm__("movw %%ss,%0" : "=m" (context->SegSs));
1369 #endif
1370 if (FPU_sig(sigcontext))
1372 context->ContextFlags |= CONTEXT_FLOATING_POINT;
1373 context->u.FltSave = *FPU_sig(sigcontext);
1374 context->MxCsr = context->u.FltSave.MxCsr;
1379 /***********************************************************************
1380 * restore_context
1382 * Build a sigcontext from the register values.
1384 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
1386 RAX_sig(sigcontext) = context->Rax;
1387 RCX_sig(sigcontext) = context->Rcx;
1388 RDX_sig(sigcontext) = context->Rdx;
1389 RBX_sig(sigcontext) = context->Rbx;
1390 RSP_sig(sigcontext) = context->Rsp;
1391 RBP_sig(sigcontext) = context->Rbp;
1392 RSI_sig(sigcontext) = context->Rsi;
1393 RDI_sig(sigcontext) = context->Rdi;
1394 R8_sig(sigcontext) = context->R8;
1395 R9_sig(sigcontext) = context->R9;
1396 R10_sig(sigcontext) = context->R10;
1397 R11_sig(sigcontext) = context->R11;
1398 R12_sig(sigcontext) = context->R12;
1399 R13_sig(sigcontext) = context->R13;
1400 R14_sig(sigcontext) = context->R14;
1401 R15_sig(sigcontext) = context->R15;
1402 RIP_sig(sigcontext) = context->Rip;
1403 CS_sig(sigcontext) = context->SegCs;
1404 FS_sig(sigcontext) = context->SegFs;
1405 GS_sig(sigcontext) = context->SegGs;
1406 EFL_sig(sigcontext) = context->EFlags;
1407 #ifdef DS_sig
1408 DS_sig(sigcontext) = context->SegDs;
1409 #endif
1410 #ifdef ES_sig
1411 ES_sig(sigcontext) = context->SegEs;
1412 #endif
1413 #ifdef SS_sig
1414 SS_sig(sigcontext) = context->SegSs;
1415 #endif
1416 if (FPU_sig(sigcontext)) *FPU_sig(sigcontext) = context->u.FltSave;
1420 /***********************************************************************
1421 * RtlCaptureContext (NTDLL.@)
1423 void WINAPI __regs_RtlCaptureContext( CONTEXT *context, CONTEXT *regs )
1425 *context = *regs;
1427 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext, 1 )
1430 /***********************************************************************
1431 * set_cpu_context
1433 * Set the new CPU context.
1435 void set_cpu_context( const CONTEXT *context )
1437 extern void CDECL __wine_restore_regs( const CONTEXT * ) DECLSPEC_NORETURN;
1438 __wine_restore_regs( context );
1442 /***********************************************************************
1443 * copy_context
1445 * Copy a register context according to the flags.
1447 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
1449 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
1450 if (flags & CONTEXT_CONTROL)
1452 to->Rbp = from->Rbp;
1453 to->Rip = from->Rip;
1454 to->Rsp = from->Rsp;
1455 to->SegCs = from->SegCs;
1456 to->SegSs = from->SegSs;
1457 to->EFlags = from->EFlags;
1459 if (flags & CONTEXT_INTEGER)
1461 to->Rax = from->Rax;
1462 to->Rcx = from->Rcx;
1463 to->Rdx = from->Rdx;
1464 to->Rbx = from->Rbx;
1465 to->Rsi = from->Rsi;
1466 to->Rdi = from->Rdi;
1467 to->R8 = from->R8;
1468 to->R9 = from->R9;
1469 to->R10 = from->R10;
1470 to->R11 = from->R11;
1471 to->R12 = from->R12;
1472 to->R13 = from->R13;
1473 to->R14 = from->R14;
1474 to->R15 = from->R15;
1476 if (flags & CONTEXT_SEGMENTS)
1478 to->SegDs = from->SegDs;
1479 to->SegEs = from->SegEs;
1480 to->SegFs = from->SegFs;
1481 to->SegGs = from->SegGs;
1483 if (flags & CONTEXT_FLOATING_POINT)
1485 to->MxCsr = from->MxCsr;
1486 to->u.FltSave = from->u.FltSave;
1488 if (flags & CONTEXT_DEBUG_REGISTERS)
1490 to->Dr0 = from->Dr0;
1491 to->Dr1 = from->Dr1;
1492 to->Dr2 = from->Dr2;
1493 to->Dr3 = from->Dr3;
1494 to->Dr6 = from->Dr6;
1495 to->Dr7 = from->Dr7;
1500 /***********************************************************************
1501 * context_to_server
1503 * Convert a register context to the server format.
1505 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
1507 DWORD flags = from->ContextFlags & ~CONTEXT_AMD64; /* get rid of CPU id */
1509 memset( to, 0, sizeof(*to) );
1510 to->cpu = CPU_x86_64;
1512 if (flags & CONTEXT_CONTROL)
1514 to->flags |= SERVER_CTX_CONTROL;
1515 to->ctl.x86_64_regs.rbp = from->Rbp;
1516 to->ctl.x86_64_regs.rip = from->Rip;
1517 to->ctl.x86_64_regs.rsp = from->Rsp;
1518 to->ctl.x86_64_regs.cs = from->SegCs;
1519 to->ctl.x86_64_regs.ss = from->SegSs;
1520 to->ctl.x86_64_regs.flags = from->EFlags;
1522 if (flags & CONTEXT_INTEGER)
1524 to->flags |= SERVER_CTX_INTEGER;
1525 to->integer.x86_64_regs.rax = from->Rax;
1526 to->integer.x86_64_regs.rcx = from->Rcx;
1527 to->integer.x86_64_regs.rdx = from->Rdx;
1528 to->integer.x86_64_regs.rbx = from->Rbx;
1529 to->integer.x86_64_regs.rsi = from->Rsi;
1530 to->integer.x86_64_regs.rdi = from->Rdi;
1531 to->integer.x86_64_regs.r8 = from->R8;
1532 to->integer.x86_64_regs.r9 = from->R9;
1533 to->integer.x86_64_regs.r10 = from->R10;
1534 to->integer.x86_64_regs.r11 = from->R11;
1535 to->integer.x86_64_regs.r12 = from->R12;
1536 to->integer.x86_64_regs.r13 = from->R13;
1537 to->integer.x86_64_regs.r14 = from->R14;
1538 to->integer.x86_64_regs.r15 = from->R15;
1540 if (flags & CONTEXT_SEGMENTS)
1542 to->flags |= SERVER_CTX_SEGMENTS;
1543 to->seg.x86_64_regs.ds = from->SegDs;
1544 to->seg.x86_64_regs.es = from->SegEs;
1545 to->seg.x86_64_regs.fs = from->SegFs;
1546 to->seg.x86_64_regs.gs = from->SegGs;
1548 if (flags & CONTEXT_FLOATING_POINT)
1550 to->flags |= SERVER_CTX_FLOATING_POINT;
1551 memcpy( to->fp.x86_64_regs.fpregs, &from->u.FltSave, sizeof(to->fp.x86_64_regs.fpregs) );
1553 if (flags & CONTEXT_DEBUG_REGISTERS)
1555 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
1556 to->debug.x86_64_regs.dr0 = from->Dr0;
1557 to->debug.x86_64_regs.dr1 = from->Dr1;
1558 to->debug.x86_64_regs.dr2 = from->Dr2;
1559 to->debug.x86_64_regs.dr3 = from->Dr3;
1560 to->debug.x86_64_regs.dr6 = from->Dr6;
1561 to->debug.x86_64_regs.dr7 = from->Dr7;
1563 return STATUS_SUCCESS;
1567 /***********************************************************************
1568 * context_from_server
1570 * Convert a register context from the server format.
1572 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
1574 if (from->cpu != CPU_x86_64) return STATUS_INVALID_PARAMETER;
1576 to->ContextFlags = CONTEXT_AMD64;
1577 if (from->flags & SERVER_CTX_CONTROL)
1579 to->ContextFlags |= CONTEXT_CONTROL;
1580 to->Rbp = from->ctl.x86_64_regs.rbp;
1581 to->Rip = from->ctl.x86_64_regs.rip;
1582 to->Rsp = from->ctl.x86_64_regs.rsp;
1583 to->SegCs = from->ctl.x86_64_regs.cs;
1584 to->SegSs = from->ctl.x86_64_regs.ss;
1585 to->EFlags = from->ctl.x86_64_regs.flags;
1588 if (from->flags & SERVER_CTX_INTEGER)
1590 to->ContextFlags |= CONTEXT_INTEGER;
1591 to->Rax = from->integer.x86_64_regs.rax;
1592 to->Rcx = from->integer.x86_64_regs.rcx;
1593 to->Rdx = from->integer.x86_64_regs.rdx;
1594 to->Rbx = from->integer.x86_64_regs.rbx;
1595 to->Rsi = from->integer.x86_64_regs.rsi;
1596 to->Rdi = from->integer.x86_64_regs.rdi;
1597 to->R8 = from->integer.x86_64_regs.r8;
1598 to->R9 = from->integer.x86_64_regs.r9;
1599 to->R10 = from->integer.x86_64_regs.r10;
1600 to->R11 = from->integer.x86_64_regs.r11;
1601 to->R12 = from->integer.x86_64_regs.r12;
1602 to->R13 = from->integer.x86_64_regs.r13;
1603 to->R14 = from->integer.x86_64_regs.r14;
1604 to->R15 = from->integer.x86_64_regs.r15;
1606 if (from->flags & SERVER_CTX_SEGMENTS)
1608 to->ContextFlags |= CONTEXT_SEGMENTS;
1609 to->SegDs = from->seg.x86_64_regs.ds;
1610 to->SegEs = from->seg.x86_64_regs.es;
1611 to->SegFs = from->seg.x86_64_regs.fs;
1612 to->SegGs = from->seg.x86_64_regs.gs;
1614 if (from->flags & SERVER_CTX_FLOATING_POINT)
1616 to->ContextFlags |= CONTEXT_FLOATING_POINT;
1617 memcpy( &to->u.FltSave, from->fp.x86_64_regs.fpregs, sizeof(from->fp.x86_64_regs.fpregs) );
1618 to->MxCsr = to->u.FltSave.MxCsr;
1620 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
1622 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
1623 to->Dr0 = from->debug.x86_64_regs.dr0;
1624 to->Dr1 = from->debug.x86_64_regs.dr1;
1625 to->Dr2 = from->debug.x86_64_regs.dr2;
1626 to->Dr3 = from->debug.x86_64_regs.dr3;
1627 to->Dr6 = from->debug.x86_64_regs.dr6;
1628 to->Dr7 = from->debug.x86_64_regs.dr7;
1630 return STATUS_SUCCESS;
1634 extern void raise_func_trampoline( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
1635 __ASM_GLOBAL_FUNC( raise_func_trampoline,
1636 __ASM_CFI(".cfi_signal_frame\n\t")
1637 __ASM_CFI(".cfi_def_cfa %rbp,144\n\t") /* red zone + rip + rbp */
1638 __ASM_CFI(".cfi_rel_offset %rip,8\n\t")
1639 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1640 "call *%rdx\n\t"
1641 "int $3")
1643 /***********************************************************************
1644 * setup_exception
1646 * Setup a proper stack frame for the raise function, and modify the
1647 * sigcontext so that the return from the signal handler will call
1648 * the raise function.
1650 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
1652 struct stack_layout
1654 CONTEXT context;
1655 EXCEPTION_RECORD rec;
1656 ULONG64 rbp;
1657 ULONG64 rip;
1658 ULONG64 red_zone[16];
1659 } *stack;
1660 ULONG64 *rsp_ptr;
1661 DWORD exception_code = 0;
1663 stack = (struct stack_layout *)(RSP_sig(sigcontext) & ~15);
1665 /* stack sanity checks */
1667 if (is_inside_signal_stack( stack ))
1669 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1670 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1671 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1672 abort_thread(1);
1675 if (stack - 1 > stack || /* check for overflow in subtraction */
1676 (char *)stack <= (char *)NtCurrentTeb()->DeallocationStack ||
1677 (char *)stack > (char *)NtCurrentTeb()->Tib.StackBase)
1679 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1680 GetCurrentThreadId(), RIP_sig(sigcontext), RSP_sig(sigcontext),
1681 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1683 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->DeallocationStack + 4096)
1685 /* stack overflow on last page, unrecoverable */
1686 UINT diff = (char *)NtCurrentTeb()->DeallocationStack + 4096 - (char *)(stack - 1);
1687 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1688 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1689 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1690 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1691 abort_thread(1);
1693 else if ((char *)(stack - 1) < (char *)NtCurrentTeb()->Tib.StackLimit)
1695 /* stack access below stack limit, may be recoverable */
1696 if (virtual_handle_stack_fault( stack - 1 )) exception_code = EXCEPTION_STACK_OVERFLOW;
1697 else
1699 UINT diff = (char *)NtCurrentTeb()->Tib.StackLimit - (char *)(stack - 1);
1700 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1701 diff, GetCurrentThreadId(), RIP_sig(sigcontext),
1702 RSP_sig(sigcontext), NtCurrentTeb()->DeallocationStack,
1703 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1704 abort_thread(1);
1708 stack--; /* push the stack_layout structure */
1709 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
1710 VALGRIND_MAKE_MEM_UNDEFINED(stack, sizeof(*stack));
1711 #elif defined(VALGRIND_MAKE_WRITABLE)
1712 VALGRIND_MAKE_WRITABLE(stack, sizeof(*stack));
1713 #endif
1714 stack->rec.ExceptionRecord = NULL;
1715 stack->rec.ExceptionCode = exception_code;
1716 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
1717 stack->rec.ExceptionAddress = (void *)RIP_sig(sigcontext);
1718 stack->rec.NumberParameters = 0;
1719 save_context( &stack->context, sigcontext );
1721 /* store return address and %rbp without aligning, so that the offset is fixed */
1722 rsp_ptr = (ULONG64 *)RSP_sig(sigcontext) - 16;
1723 *(--rsp_ptr) = RIP_sig(sigcontext);
1724 *(--rsp_ptr) = RBP_sig(sigcontext);
1726 /* now modify the sigcontext to return to the raise function */
1727 RIP_sig(sigcontext) = (ULONG_PTR)raise_func_trampoline;
1728 RDI_sig(sigcontext) = (ULONG_PTR)&stack->rec;
1729 RSI_sig(sigcontext) = (ULONG_PTR)&stack->context;
1730 RDX_sig(sigcontext) = (ULONG_PTR)func;
1731 RBP_sig(sigcontext) = (ULONG_PTR)rsp_ptr;
1732 RSP_sig(sigcontext) = (ULONG_PTR)stack;
1733 /* clear single-step, direction, and align check flag */
1734 EFL_sig(sigcontext) &= ~(0x100|0x400|0x40000);
1736 return &stack->rec;
1740 /**********************************************************************
1741 * find_function_info
1743 static RUNTIME_FUNCTION *find_function_info( ULONG64 pc, HMODULE module,
1744 RUNTIME_FUNCTION *func, ULONG size )
1746 int min = 0;
1747 int max = size/sizeof(*func) - 1;
1749 while (min <= max)
1751 int pos = (min + max) / 2;
1752 if ((char *)pc < (char *)module + func[pos].BeginAddress) max = pos - 1;
1753 else if ((char *)pc >= (char *)module + func[pos].EndAddress) min = pos + 1;
1754 else
1756 func += pos;
1757 while (func->UnwindData & 1) /* follow chained entry */
1758 func = (RUNTIME_FUNCTION *)((char *)module + (func->UnwindData & ~1));
1759 return func;
1762 return NULL;
1766 /**********************************************************************
1767 * call_handler
1769 * Call a single exception handler.
1770 * FIXME: Handle nested exceptions.
1772 static NTSTATUS call_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch, CONTEXT *orig_context )
1774 DWORD res;
1776 dispatch->ControlPc = dispatch->ContextRecord->Rip;
1778 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
1779 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1780 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1781 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
1783 switch (res)
1785 case ExceptionContinueExecution:
1786 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1787 *orig_context = *dispatch->ContextRecord;
1788 return STATUS_SUCCESS;
1789 case ExceptionContinueSearch:
1790 break;
1791 case ExceptionNestedException:
1792 break;
1793 default:
1794 return STATUS_INVALID_DISPOSITION;
1796 return STATUS_UNHANDLED_EXCEPTION;
1800 /**********************************************************************
1801 * call_teb_handler
1803 * Call a single exception handler from the TEB chain.
1804 * FIXME: Handle nested exceptions.
1806 static NTSTATUS call_teb_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
1807 EXCEPTION_REGISTRATION_RECORD *teb_frame, CONTEXT *orig_context )
1809 EXCEPTION_REGISTRATION_RECORD *dispatcher;
1810 DWORD res;
1812 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
1813 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1814 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
1815 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
1817 switch (res)
1819 case ExceptionContinueExecution:
1820 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
1821 *orig_context = *dispatch->ContextRecord;
1822 return STATUS_SUCCESS;
1823 case ExceptionContinueSearch:
1824 break;
1825 case ExceptionNestedException:
1826 break;
1827 default:
1828 return STATUS_INVALID_DISPOSITION;
1830 return STATUS_UNHANDLED_EXCEPTION;
1834 /**********************************************************************
1835 * call_stack_handlers
1837 * Call the stack handlers chain.
1839 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
1841 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1842 UNWIND_HISTORY_TABLE table;
1843 DISPATCHER_CONTEXT dispatch;
1844 CONTEXT context, new_context;
1845 LDR_MODULE *module;
1846 DWORD size;
1847 NTSTATUS status;
1849 context = *orig_context;
1850 dispatch.TargetIp = 0;
1851 dispatch.ContextRecord = &context;
1852 dispatch.HistoryTable = &table;
1853 dispatch.ScopeIndex = 0; /* FIXME */
1854 for (;;)
1856 new_context = context;
1858 /* FIXME: should use the history table to make things faster */
1860 module = NULL;
1861 dispatch.ImageBase = 0;
1863 /* first look for PE exception information */
1865 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
1867 RUNTIME_FUNCTION *dir;
1869 dispatch.ImageBase = (ULONG64)module->BaseAddress;
1870 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
1871 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
1873 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
1874 dir, size )))
1876 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, dispatch.ImageBase,
1877 context.Rip, dispatch.FunctionEntry,
1878 &new_context, &dispatch.HandlerData,
1879 &dispatch.EstablisherFrame, NULL );
1880 goto unwind_done;
1883 else if (!(module->Flags & LDR_WINE_INTERNAL))
1884 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
1887 /* then look for host system exception information */
1889 if (!module || (module->Flags & LDR_WINE_INTERNAL))
1891 struct dwarf_eh_bases bases;
1892 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
1894 if (fde)
1896 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context,
1897 fde, &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
1898 if (status != STATUS_SUCCESS) return status;
1899 dispatch.FunctionEntry = NULL;
1900 if (dispatch.LanguageHandler && !module)
1902 FIXME( "calling personality routine in system library not supported yet\n" );
1903 dispatch.LanguageHandler = NULL;
1905 goto unwind_done;
1909 /* no exception information, treat as a leaf function */
1911 new_context.Rip = *(ULONG64 *)context.Rsp;
1912 new_context.Rsp = context.Rsp + sizeof(ULONG64);
1913 dispatch.EstablisherFrame = new_context.Rsp;
1914 dispatch.LanguageHandler = NULL;
1916 unwind_done:
1917 if (!dispatch.EstablisherFrame) break;
1919 if ((dispatch.EstablisherFrame & 7) ||
1920 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
1921 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
1923 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1924 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1925 rec->ExceptionFlags |= EH_STACK_INVALID;
1926 break;
1929 if (dispatch.LanguageHandler)
1931 status = call_handler( rec, &dispatch, orig_context );
1932 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
1934 /* hack: call wine handlers registered in the tib list */
1935 else while ((ULONG64)teb_frame < new_context.Rsp)
1937 TRACE( "found wine frame %p rsp %lx handler %p\n",
1938 teb_frame, new_context.Rsp, teb_frame->Handler );
1939 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1940 context = *orig_context;
1941 status = call_teb_handler( rec, &dispatch, teb_frame, orig_context );
1942 if (status != STATUS_UNHANDLED_EXCEPTION) return status;
1943 teb_frame = teb_frame->Prev;
1946 if (new_context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
1947 context = new_context;
1949 return STATUS_UNHANDLED_EXCEPTION;
1953 /*******************************************************************
1954 * raise_exception
1956 * Implementation of NtRaiseException.
1958 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1960 NTSTATUS status;
1962 if (first_chance)
1964 DWORD c;
1966 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
1967 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
1968 context->Rip, GetCurrentThreadId() );
1969 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
1970 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
1971 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
1973 if (rec->ExceptionInformation[1] >> 16)
1974 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
1975 rec->ExceptionAddress,
1976 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
1977 else
1978 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
1979 rec->ExceptionAddress,
1980 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
1982 else
1984 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1985 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1986 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1987 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1988 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1989 context->R8, context->R9, context->R10, context->R11 );
1990 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1991 context->R12, context->R13, context->R14, context->R15 );
1993 status = send_debug_event( rec, TRUE, context );
1994 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
1995 return STATUS_SUCCESS;
1997 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
1998 return STATUS_SUCCESS;
2000 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
2001 return status;
2004 /* last chance exception */
2006 status = send_debug_event( rec, FALSE, context );
2007 if (status != DBG_CONTINUE)
2009 if (rec->ExceptionFlags & EH_STACK_INVALID)
2010 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
2011 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
2012 ERR("Process attempted to continue execution after noncontinuable exception.\n");
2013 else
2014 ERR("Unhandled exception code %x flags %x addr %p\n",
2015 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
2016 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
2018 return STATUS_SUCCESS;
2022 /**********************************************************************
2023 * raise_segv_exception
2025 static void raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2027 NTSTATUS status;
2029 switch(rec->ExceptionCode)
2031 case EXCEPTION_ACCESS_VIOLATION:
2032 if (rec->NumberParameters == 2)
2034 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
2035 rec->ExceptionInformation[0] )))
2036 set_cpu_context( context );
2038 break;
2040 status = raise_exception( rec, context, TRUE );
2041 if (status) raise_status( status, rec );
2042 set_cpu_context( context );
2046 /**********************************************************************
2047 * raise_generic_exception
2049 * Generic raise function for exceptions that don't need special treatment.
2051 static void raise_generic_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
2053 NTSTATUS status = raise_exception( rec, context, TRUE );
2054 if (status) raise_status( status, rec );
2055 set_cpu_context( context );
2059 /**********************************************************************
2060 * segv_handler
2062 * Handler for SIGSEGV and related errors.
2064 static void segv_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2066 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_segv_exception );
2067 ucontext_t *ucontext = sigcontext;
2069 switch(TRAP_sig(ucontext))
2071 case TRAP_x86_OFLOW: /* Overflow exception */
2072 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2073 break;
2074 case TRAP_x86_BOUND: /* Bound range exception */
2075 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2076 break;
2077 case TRAP_x86_PRIVINFLT: /* Invalid opcode exception */
2078 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2079 break;
2080 case TRAP_x86_STKFLT: /* Stack fault */
2081 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
2082 break;
2083 case TRAP_x86_SEGNPFLT: /* Segment not present exception */
2084 case TRAP_x86_PROTFLT: /* General protection fault */
2085 case TRAP_x86_UNKNOWN: /* Unknown fault code */
2086 rec->ExceptionCode = ERROR_sig(ucontext) ? EXCEPTION_ACCESS_VIOLATION : EXCEPTION_PRIV_INSTRUCTION;
2087 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2088 break;
2089 case TRAP_x86_PAGEFLT: /* Page fault */
2090 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
2091 rec->NumberParameters = 2;
2092 rec->ExceptionInformation[0] = (ERROR_sig(ucontext) & 2) != 0;
2093 rec->ExceptionInformation[1] = (ULONG_PTR)siginfo->si_addr;
2094 break;
2095 case TRAP_x86_ALIGNFLT: /* Alignment check exception */
2096 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
2097 break;
2098 default:
2099 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext) );
2100 /* fall through */
2101 case TRAP_x86_NMI: /* NMI interrupt */
2102 case TRAP_x86_DNA: /* Device not available exception */
2103 case TRAP_x86_DOUBLEFLT: /* Double fault exception */
2104 case TRAP_x86_TSSFLT: /* Invalid TSS exception */
2105 case TRAP_x86_MCHK: /* Machine check exception */
2106 case TRAP_x86_CACHEFLT: /* Cache flush exception */
2107 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
2108 break;
2112 /**********************************************************************
2113 * trap_handler
2115 * Handler for SIGTRAP.
2117 static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2119 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2121 switch (siginfo->si_code)
2123 case TRAP_TRACE: /* Single-step exception */
2124 case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
2125 rec->ExceptionCode = EXCEPTION_SINGLE_STEP;
2126 break;
2127 case TRAP_BRKPT: /* Breakpoint exception */
2128 rec->ExceptionAddress = (char *)rec->ExceptionAddress - 1; /* back up over the int3 instruction */
2129 /* fall through */
2130 default:
2131 rec->ExceptionCode = EXCEPTION_BREAKPOINT;
2132 break;
2136 /**********************************************************************
2137 * fpe_handler
2139 * Handler for SIGFPE.
2141 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2143 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2145 switch (siginfo->si_code)
2147 case FPE_FLTSUB:
2148 rec->ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
2149 break;
2150 case FPE_INTDIV:
2151 rec->ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
2152 break;
2153 case FPE_INTOVF:
2154 rec->ExceptionCode = EXCEPTION_INT_OVERFLOW;
2155 break;
2156 case FPE_FLTDIV:
2157 rec->ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
2158 break;
2159 case FPE_FLTOVF:
2160 rec->ExceptionCode = EXCEPTION_FLT_OVERFLOW;
2161 break;
2162 case FPE_FLTUND:
2163 rec->ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
2164 break;
2165 case FPE_FLTRES:
2166 rec->ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
2167 break;
2168 case FPE_FLTINV:
2169 default:
2170 rec->ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
2171 break;
2175 /**********************************************************************
2176 * int_handler
2178 * Handler for SIGINT.
2180 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2182 if (!dispatch_signal(SIGINT))
2184 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2185 rec->ExceptionCode = CONTROL_C_EXIT;
2190 /**********************************************************************
2191 * abrt_handler
2193 * Handler for SIGABRT.
2195 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
2197 EXCEPTION_RECORD *rec = setup_exception( sigcontext, raise_generic_exception );
2198 rec->ExceptionCode = EXCEPTION_WINE_ASSERTION;
2199 rec->ExceptionFlags = EH_NONCONTINUABLE;
2203 /**********************************************************************
2204 * quit_handler
2206 * Handler for SIGQUIT.
2208 static void quit_handler( int signal, siginfo_t *siginfo, void *ucontext )
2210 abort_thread(0);
2214 /**********************************************************************
2215 * usr1_handler
2217 * Handler for SIGUSR1, used to signal a thread that it got suspended.
2219 static void usr1_handler( int signal, siginfo_t *siginfo, void *ucontext )
2221 CONTEXT context;
2223 save_context( &context, ucontext );
2224 wait_suspend( &context );
2225 restore_context( &context, ucontext );
2229 /***********************************************************************
2230 * __wine_set_signal_handler (NTDLL.@)
2232 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
2234 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
2235 if (handlers[sig] != NULL) return -2;
2236 handlers[sig] = wsh;
2237 return 0;
2241 /**********************************************************************
2242 * signal_alloc_thread
2244 NTSTATUS signal_alloc_thread( TEB **teb )
2246 static size_t sigstack_zero_bits;
2247 SIZE_T size;
2248 NTSTATUS status;
2250 if (!sigstack_zero_bits)
2252 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
2253 /* find the first power of two not smaller than min_size */
2254 sigstack_zero_bits = 12;
2255 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
2256 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
2257 assert( sizeof(TEB) <= teb_size );
2260 size = 1 << sigstack_zero_bits;
2261 *teb = NULL;
2262 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
2263 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
2265 (*teb)->Tib.Self = &(*teb)->Tib;
2266 (*teb)->Tib.ExceptionList = (void *)~0UL;
2268 return status;
2272 /**********************************************************************
2273 * signal_free_thread
2275 void signal_free_thread( TEB *teb )
2277 SIZE_T size;
2279 if (teb->DeallocationStack)
2281 size = 0;
2282 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
2284 size = 0;
2285 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
2289 /**********************************************************************
2290 * signal_init_thread
2292 void signal_init_thread( TEB *teb )
2294 stack_t ss;
2296 #if defined __linux__
2297 arch_prctl( ARCH_SET_GS, teb );
2298 #elif defined __FreeBSD__
2299 amd64_set_gsbase( teb );
2300 #else
2301 # error Please define setting %gs for your architecture
2302 #endif
2304 ss.ss_sp = (char *)teb + teb_size;
2305 ss.ss_size = signal_stack_size;
2306 ss.ss_flags = 0;
2307 if (sigaltstack(&ss, NULL) == -1) perror( "sigaltstack" );
2310 /**********************************************************************
2311 * signal_init_process
2313 void signal_init_process(void)
2315 struct sigaction sig_act;
2317 sig_act.sa_mask = server_block_set;
2318 sig_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
2320 sig_act.sa_sigaction = int_handler;
2321 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
2322 sig_act.sa_sigaction = fpe_handler;
2323 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
2324 sig_act.sa_sigaction = abrt_handler;
2325 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
2326 sig_act.sa_sigaction = quit_handler;
2327 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
2328 sig_act.sa_sigaction = usr1_handler;
2329 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
2331 sig_act.sa_sigaction = segv_handler;
2332 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
2333 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
2334 #ifdef SIGBUS
2335 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
2336 #endif
2338 #ifdef SIGTRAP
2339 sig_act.sa_sigaction = trap_handler;
2340 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
2341 #endif
2342 return;
2344 error:
2345 perror("sigaction");
2346 exit(1);
2350 /**********************************************************************
2351 * RtlAddFunctionTable (NTDLL.@)
2353 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD64 addr )
2355 FIXME( "%p %u %lx: stub\n", table, count, addr );
2356 return FALSE;
2360 /**********************************************************************
2361 * RtlDeleteFunctionTable (NTDLL.@)
2363 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
2365 FIXME( "%p: stub\n", table );
2366 return FALSE;
2370 /**********************************************************************
2371 * RtlLookupFunctionEntry (NTDLL.@)
2373 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG64 pc, ULONG64 *base, UNWIND_HISTORY_TABLE *table )
2375 LDR_MODULE *module;
2376 RUNTIME_FUNCTION *func;
2377 ULONG size;
2379 /* FIXME: should use the history table to make things faster */
2381 if (LdrFindEntryForAddress( (void *)pc, &module ))
2383 WARN( "module not found for %lx\n", pc );
2384 return NULL;
2386 if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2387 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2389 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
2390 return NULL;
2392 func = find_function_info( pc, module->BaseAddress, func, size );
2393 if (func) *base = (ULONG64)module->BaseAddress;
2394 return func;
2397 static ULONG64 get_int_reg( CONTEXT *context, int reg )
2399 return *(&context->Rax + reg);
2402 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 val )
2404 *(&context->Rax + reg) = val;
2405 if (ctx_ptr) ctx_ptr->u2.IntegerContext[reg] = &context->Rax + reg;
2408 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A val )
2410 *(&context->u.s.Xmm0 + reg) = val;
2411 if (ctx_ptr) ctx_ptr->u1.FloatingContext[reg] = &context->u.s.Xmm0 + reg;
2414 static int get_opcode_size( struct opcode op )
2416 switch (op.code)
2418 case UWOP_ALLOC_LARGE:
2419 return 2 + (op.info != 0);
2420 case UWOP_SAVE_NONVOL:
2421 case UWOP_SAVE_XMM128:
2422 return 2;
2423 case UWOP_SAVE_NONVOL_FAR:
2424 case UWOP_SAVE_XMM128_FAR:
2425 return 3;
2426 default:
2427 return 1;
2431 static BOOL is_inside_epilog( BYTE *pc )
2433 /* add or lea must be the first instruction, and it must have a rex.W prefix */
2434 if ((pc[0] & 0xf8) == 0x48)
2436 switch (pc[1])
2438 case 0x81: /* add $nnnn,%rsp */
2439 if (pc[0] == 0x48 && pc[2] == 0xc4)
2441 pc += 7;
2442 break;
2444 return FALSE;
2445 case 0x83: /* add $n,%rsp */
2446 if (pc[0] == 0x48 && pc[2] == 0xc4)
2448 pc += 4;
2449 break;
2451 return FALSE;
2452 case 0x8d: /* lea n(reg),%rsp */
2453 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
2454 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
2455 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
2456 if ((pc[2] >> 6) == 1) /* 8-bit offset */
2458 pc += 4;
2459 break;
2461 if ((pc[2] >> 6) == 2) /* 32-bit offset */
2463 pc += 7;
2464 break;
2466 return FALSE;
2470 /* now check for various pop instructions */
2472 for (;;)
2474 BYTE rex = 0;
2476 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2478 switch (*pc)
2480 case 0x58: /* pop %rax/%r8 */
2481 case 0x59: /* pop %rcx/%r9 */
2482 case 0x5a: /* pop %rdx/%r10 */
2483 case 0x5b: /* pop %rbx/%r11 */
2484 case 0x5c: /* pop %rsp/%r12 */
2485 case 0x5d: /* pop %rbp/%r13 */
2486 case 0x5e: /* pop %rsi/%r14 */
2487 case 0x5f: /* pop %rdi/%r15 */
2488 pc++;
2489 continue;
2490 case 0xc2: /* ret $nn */
2491 case 0xc3: /* ret */
2492 return TRUE;
2493 /* FIXME: add various jump instructions */
2495 return FALSE;
2499 /* execute a function epilog, which must have been validated with is_inside_epilog() */
2500 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2502 for (;;)
2504 BYTE rex = 0;
2506 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
2508 switch (*pc)
2510 case 0x58: /* pop %rax/r8 */
2511 case 0x59: /* pop %rcx/r9 */
2512 case 0x5a: /* pop %rdx/r10 */
2513 case 0x5b: /* pop %rbx/r11 */
2514 case 0x5c: /* pop %rsp/r12 */
2515 case 0x5d: /* pop %rbp/r13 */
2516 case 0x5e: /* pop %rsi/r14 */
2517 case 0x5f: /* pop %rdi/r15 */
2518 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, *(ULONG64 *)context->Rsp );
2519 context->Rsp += sizeof(ULONG64);
2520 pc++;
2521 continue;
2522 case 0x81: /* add $nnnn,%rsp */
2523 context->Rsp += *(LONG *)(pc + 2);
2524 pc += 2 + sizeof(LONG);
2525 continue;
2526 case 0x83: /* add $n,%rsp */
2527 context->Rsp += (signed char)pc[2];
2528 pc += 3;
2529 continue;
2530 case 0x8d:
2531 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
2533 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
2534 pc += 3;
2536 else /* lea nnnn(reg),%rsp */
2538 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
2539 pc += 2 + sizeof(LONG);
2541 continue;
2542 case 0xc2: /* ret $nn */
2543 context->Rip = *(ULONG64 *)context->Rsp;
2544 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
2545 return;
2546 case 0xc3: /* ret */
2547 context->Rip = *(ULONG64 *)context->Rsp;
2548 context->Rsp += sizeof(ULONG64);
2549 return;
2550 /* FIXME: add various jump instructions */
2552 return;
2556 /**********************************************************************
2557 * RtlVirtualUnwind (NTDLL.@)
2559 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
2560 RUNTIME_FUNCTION *function, CONTEXT *context,
2561 PVOID *data, ULONG64 *frame_ret,
2562 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
2564 union handler_data *handler_data;
2565 ULONG64 frame, off;
2566 struct UNWIND_INFO *info;
2567 unsigned int i, prolog_offset;
2569 TRACE( "type %x rip %lx rsp %lx\n", type, pc, context->Rsp );
2570 if (TRACE_ON(seh)) dump_unwind_info( base, function );
2572 frame = *frame_ret = context->Rsp;
2573 for (;;)
2575 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
2576 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
2578 if (info->version != 1)
2580 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
2581 return NULL;
2584 if (info->frame_reg)
2585 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
2587 /* check if in prolog */
2588 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
2590 prolog_offset = pc - base - function->BeginAddress;
2592 else
2594 prolog_offset = ~0;
2595 if (is_inside_epilog( (BYTE *)pc ))
2597 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
2598 *frame_ret = frame;
2599 return NULL;
2603 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
2605 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
2607 switch (info->opcodes[i].code)
2609 case UWOP_PUSH_NONVOL: /* pushq %reg */
2610 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)context->Rsp );
2611 context->Rsp += sizeof(ULONG64);
2612 break;
2613 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
2614 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
2615 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
2616 break;
2617 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
2618 context->Rsp += (info->opcodes[i].info + 1) * 8;
2619 break;
2620 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
2621 context->Rsp = *frame_ret = frame;
2622 break;
2623 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
2624 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
2625 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2626 break;
2627 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
2628 off = frame + *(DWORD *)&info->opcodes[i+1];
2629 set_int_reg( context, ctx_ptr, info->opcodes[i].info, *(ULONG64 *)off );
2630 break;
2631 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
2632 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
2633 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2634 break;
2635 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
2636 off = frame + *(DWORD *)&info->opcodes[i+1];
2637 set_float_reg( context, ctx_ptr, info->opcodes[i].info, *(M128A *)off );
2638 break;
2639 case UWOP_PUSH_MACHFRAME:
2640 FIXME( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
2641 break;
2642 default:
2643 FIXME( "unknown code %u\n", info->opcodes[i].code );
2644 break;
2648 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
2649 function = &handler_data->chain; /* restart with the chained info */
2652 /* now pop return address */
2653 context->Rip = *(ULONG64 *)context->Rsp;
2654 context->Rsp += sizeof(ULONG64);
2656 if (!(info->flags & type)) return NULL; /* no matching handler */
2657 if (prolog_offset != ~0) return NULL; /* inside prolog */
2659 *data = &handler_data->handler + 1;
2660 return (char *)base + handler_data->handler;
2664 /**********************************************************************
2665 * call_unwind_handler
2667 * Call a single unwind handler.
2668 * FIXME: Handle nested exceptions.
2670 static void call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
2672 DWORD res;
2674 dispatch->ControlPc = dispatch->ContextRecord->Rip;
2676 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2677 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2678 res = dispatch->LanguageHandler( rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
2679 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
2681 switch (res)
2683 case ExceptionContinueSearch:
2684 break;
2685 case ExceptionCollidedUnwind:
2686 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2687 break;
2688 default:
2689 raise_status( STATUS_INVALID_DISPOSITION, rec );
2690 break;
2695 /**********************************************************************
2696 * call_teb_unwind_handler
2698 * Call a single unwind handler from the TEB chain.
2699 * FIXME: Handle nested exceptions.
2701 static void call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
2702 EXCEPTION_REGISTRATION_RECORD *teb_frame )
2704 EXCEPTION_REGISTRATION_RECORD *dispatcher;
2705 DWORD res;
2707 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2708 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2709 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, &dispatcher );
2710 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
2712 switch (res)
2714 case ExceptionContinueSearch:
2715 break;
2716 case ExceptionCollidedUnwind:
2717 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2718 break;
2719 default:
2720 raise_status( STATUS_INVALID_DISPOSITION, rec );
2721 break;
2726 /*******************************************************************
2727 * RtlUnwindEx (NTDLL.@)
2729 void WINAPI RtlUnwindEx( ULONG64 end_frame, ULONG64 target_ip, EXCEPTION_RECORD *rec,
2730 ULONG64 retval, CONTEXT *orig_context, UNWIND_HISTORY_TABLE *table )
2732 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
2733 EXCEPTION_RECORD record;
2734 DISPATCHER_CONTEXT dispatch;
2735 CONTEXT context, new_context;
2736 LDR_MODULE *module;
2737 NTSTATUS status;
2738 DWORD size;
2740 /* build an exception record, if we do not have one */
2741 if (!rec)
2743 record.ExceptionCode = STATUS_UNWIND;
2744 record.ExceptionFlags = 0;
2745 record.ExceptionRecord = NULL;
2746 record.ExceptionAddress = (void *)orig_context->Rip;
2747 record.NumberParameters = 0;
2748 rec = &record;
2751 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
2753 TRACE( "code=%x flags=%x end_frame=%lx target_ip=%lx rip=%016lx\n",
2754 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, orig_context->Rip );
2755 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2756 orig_context->Rax, orig_context->Rbx, orig_context->Rcx, orig_context->Rdx );
2757 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2758 orig_context->Rsi, orig_context->Rdi, orig_context->Rbp, orig_context->Rsp );
2759 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2760 orig_context->R8, orig_context->R9, orig_context->R10, orig_context->R11 );
2761 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2762 orig_context->R12, orig_context->R13, orig_context->R14, orig_context->R15 );
2764 context = *orig_context;
2765 dispatch.EstablisherFrame = context.Rsp;
2766 dispatch.TargetIp = target_ip;
2767 dispatch.ContextRecord = &context;
2768 dispatch.HistoryTable = table;
2770 while (dispatch.EstablisherFrame != end_frame)
2772 new_context = context;
2774 /* FIXME: should use the history table to make things faster */
2776 module = NULL;
2777 dispatch.ImageBase = 0;
2778 dispatch.ScopeIndex = 0; /* FIXME */
2780 /* first look for PE exception information */
2782 if (!LdrFindEntryForAddress( (void *)context.Rip, &module ))
2784 RUNTIME_FUNCTION *dir;
2786 dispatch.ImageBase = (ULONG64)module->BaseAddress;
2787 if ((dir = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
2788 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
2790 if ((dispatch.FunctionEntry = find_function_info( context.Rip, module->BaseAddress,
2791 dir, size )))
2793 dispatch.LanguageHandler = RtlVirtualUnwind( UNW_FLAG_UHANDLER, dispatch.ImageBase,
2794 context.Rip, dispatch.FunctionEntry,
2795 &new_context, &dispatch.HandlerData,
2796 &dispatch.EstablisherFrame, NULL );
2797 goto unwind_done;
2800 else if (!(module->Flags & LDR_WINE_INTERNAL))
2801 WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
2804 /* then look for host system exception information */
2806 if (!module || (module->Flags & LDR_WINE_INTERNAL))
2808 struct dwarf_eh_bases bases;
2809 const struct dwarf_fde *fde = _Unwind_Find_FDE( (void *)(context.Rip - 1), &bases );
2811 if (fde)
2813 dispatch.FunctionEntry = NULL;
2814 status = dwarf_virtual_unwind( context.Rip, &dispatch.EstablisherFrame, &new_context, fde,
2815 &bases, &dispatch.LanguageHandler, &dispatch.HandlerData );
2816 if (status != STATUS_SUCCESS) raise_status( status, rec );
2817 if (dispatch.LanguageHandler && !module)
2819 FIXME( "calling personality routine in system library not supported yet\n" );
2820 dispatch.LanguageHandler = NULL;
2822 goto unwind_done;
2826 /* no exception information, treat as a leaf function */
2828 new_context.Rip = *(ULONG64 *)context.Rsp;
2829 new_context.Rsp = context.Rsp + sizeof(ULONG64);
2830 dispatch.EstablisherFrame = new_context.Rsp;
2831 dispatch.LanguageHandler = NULL;
2833 unwind_done:
2834 if (!dispatch.EstablisherFrame) break;
2836 if (is_inside_signal_stack( (void *)dispatch.EstablisherFrame ))
2838 TRACE( "frame %lx is inside signal stack (%p-%p)\n", dispatch.EstablisherFrame,
2839 get_signal_stack(), (char *)get_signal_stack() + signal_stack_size );
2840 context = new_context;
2841 continue;
2844 if ((dispatch.EstablisherFrame & 7) ||
2845 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
2846 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
2848 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
2849 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
2850 rec->ExceptionFlags |= EH_STACK_INVALID;
2851 break;
2854 if (dispatch.LanguageHandler)
2856 if (end_frame && (dispatch.EstablisherFrame > end_frame))
2858 ERR( "invalid end frame %lx/%lx\n", dispatch.EstablisherFrame, end_frame );
2859 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
2861 call_unwind_handler( rec, &dispatch );
2863 else /* hack: call builtin handlers registered in the tib list */
2865 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < end_frame)
2867 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
2868 dispatch.EstablisherFrame = (ULONG64)teb_frame;
2869 call_teb_unwind_handler( rec, &dispatch, teb_frame );
2870 teb_frame = __wine_pop_frame( teb_frame );
2872 if ((ULONG64)teb_frame == end_frame && end_frame < new_context.Rsp) break;
2873 dispatch.EstablisherFrame = new_context.Rsp;
2876 context = new_context;
2878 context.Rax = retval;
2879 context.Rip = target_ip;
2880 TRACE( "returning to %lx stack %lx\n", context.Rip, context.Rsp );
2881 set_cpu_context( &context );
2885 /*******************************************************************
2886 * RtlUnwind (NTDLL.@)
2888 void WINAPI __regs_RtlUnwind( ULONG64 frame, ULONG64 target_ip, EXCEPTION_RECORD *rec,
2889 ULONG64 retval, CONTEXT *context )
2891 RtlUnwindEx( frame, target_ip, rec, retval, context, NULL );
2893 DEFINE_REGS_ENTRYPOINT( RtlUnwind, 4 )
2896 /*******************************************************************
2897 * __C_specific_handler (NTDLL.@)
2899 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
2900 ULONG64 frame,
2901 CONTEXT *context,
2902 struct _DISPATCHER_CONTEXT *dispatch )
2904 SCOPE_TABLE *table = dispatch->HandlerData;
2905 ULONG i;
2907 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
2908 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
2910 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)) /* FIXME */
2911 return ExceptionContinueSearch;
2913 for (i = 0; i < table->Count; i++)
2915 if (context->Rip >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
2916 context->Rip < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
2918 if (!table->ScopeRecord[i].JumpTarget) continue;
2919 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
2921 EXCEPTION_POINTERS ptrs;
2922 PC_LANGUAGE_EXCEPTION_HANDLER filter;
2924 filter = (PC_LANGUAGE_EXCEPTION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
2925 ptrs.ExceptionRecord = rec;
2926 ptrs.ContextRecord = context;
2927 TRACE( "calling filter %p ptrs %p frame %lx\n", filter, &ptrs, frame );
2928 switch (filter( &ptrs, frame ))
2930 case EXCEPTION_EXECUTE_HANDLER:
2931 break;
2932 case EXCEPTION_CONTINUE_SEARCH:
2933 continue;
2934 case EXCEPTION_CONTINUE_EXECUTION:
2935 return ExceptionContinueExecution;
2938 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
2939 RtlUnwindEx( frame, dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
2940 rec, 0, context, dispatch->HistoryTable );
2943 return ExceptionContinueSearch;
2947 /*******************************************************************
2948 * NtRaiseException (NTDLL.@)
2950 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
2952 NTSTATUS status = raise_exception( rec, context, first_chance );
2953 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
2954 return status;
2958 /***********************************************************************
2959 * RtlRaiseException (NTDLL.@)
2961 void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
2963 NTSTATUS status;
2965 rec->ExceptionAddress = (void *)context->Rip;
2966 status = raise_exception( rec, context, TRUE );
2967 if (status != STATUS_SUCCESS) raise_status( status, rec );
2969 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 1 )
2972 /*************************************************************************
2973 * RtlCaptureStackBackTrace (NTDLL.@)
2975 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
2977 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
2978 return 0;
2982 /***********************************************************************
2983 * call_thread_func
2985 void call_thread_func( LPTHREAD_START_ROUTINE entry, void *arg, void *frame )
2987 ntdll_get_thread_data()->exit_frame = frame;
2988 __TRY
2990 RtlExitUserThread( entry( arg ));
2992 __EXCEPT(unhandled_exception_filter)
2994 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2996 __ENDTRY
2997 abort(); /* should not be reached */
3000 extern void DECLSPEC_NORETURN call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg );
3001 __ASM_GLOBAL_FUNC( call_thread_entry_point,
3002 "subq $56,%rsp\n\t"
3003 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3004 "movq %rbp,48(%rsp)\n\t"
3005 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3006 "movq %rbx,40(%rsp)\n\t"
3007 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3008 "movq %r12,32(%rsp)\n\t"
3009 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3010 "movq %r13,24(%rsp)\n\t"
3011 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3012 "movq %r14,16(%rsp)\n\t"
3013 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3014 "movq %r15,8(%rsp)\n\t"
3015 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3016 "movq %rsp,%rdx\n\t"
3017 "call " __ASM_NAME("call_thread_func") );
3019 extern void DECLSPEC_NORETURN call_thread_exit_func( int status, void (*func)(int), void *frame );
3020 __ASM_GLOBAL_FUNC( call_thread_exit_func,
3021 "movq %rdx,%rsp\n\t"
3022 __ASM_CFI(".cfi_adjust_cfa_offset 56\n\t")
3023 __ASM_CFI(".cfi_rel_offset %rbp,48\n\t")
3024 __ASM_CFI(".cfi_rel_offset %rbx,40\n\t")
3025 __ASM_CFI(".cfi_rel_offset %r12,32\n\t")
3026 __ASM_CFI(".cfi_rel_offset %r13,24\n\t")
3027 __ASM_CFI(".cfi_rel_offset %r14,16\n\t")
3028 __ASM_CFI(".cfi_rel_offset %r15,8\n\t")
3029 "call *%rsi" );
3031 /***********************************************************************
3032 * RtlExitUserThread (NTDLL.@)
3034 void WINAPI RtlExitUserThread( ULONG status )
3036 if (!ntdll_get_thread_data()->exit_frame) exit_thread( status );
3037 call_thread_exit_func( status, exit_thread, ntdll_get_thread_data()->exit_frame );
3040 /***********************************************************************
3041 * abort_thread
3043 void abort_thread( int status )
3045 if (!ntdll_get_thread_data()->exit_frame) terminate_thread( status );
3046 call_thread_exit_func( status, terminate_thread, ntdll_get_thread_data()->exit_frame );
3049 /**********************************************************************
3050 * __wine_enter_vm86 (NTDLL.@)
3052 void __wine_enter_vm86( CONTEXT *context )
3054 MESSAGE("vm86 mode not supported on this platform\n");
3057 /**********************************************************************
3058 * DbgBreakPoint (NTDLL.@)
3060 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret")
3062 /**********************************************************************
3063 * DbgUserBreakPoint (NTDLL.@)
3065 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret")
3067 #endif /* __x86_64__ */