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
24 #include "wine/port.h"
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
38 #ifdef HAVE_SYS_SIGNAL_H
39 # include <sys/signal.h>
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
45 #define WIN32_NO_STATUS
48 #include "wine/library.h"
49 #include "wine/exception.h"
50 #include "ntdll_misc.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
55 struct _DISPATCHER_CONTEXT
;
57 typedef LONG (WINAPI
*PC_LANGUAGE_EXCEPTION_HANDLER
)( EXCEPTION_POINTERS
*ptrs
, ULONG64 frame
);
58 typedef EXCEPTION_DISPOSITION (WINAPI
*PEXCEPTION_ROUTINE
)( EXCEPTION_RECORD
*rec
,
61 struct _DISPATCHER_CONTEXT
*dispatch
);
63 typedef struct _DISPATCHER_CONTEXT
67 PRUNTIME_FUNCTION FunctionEntry
;
68 ULONG64 EstablisherFrame
;
70 PCONTEXT ContextRecord
;
71 PEXCEPTION_ROUTINE LanguageHandler
;
73 PUNWIND_HISTORY_TABLE HistoryTable
;
75 } DISPATCHER_CONTEXT
, *PDISPATCHER_CONTEXT
;
77 typedef struct _SCOPE_TABLE
87 } SCOPE_TABLE
, *PSCOPE_TABLE
;
90 /***********************************************************************
91 * signal context platform-specific definitions
95 #include <asm/prctl.h>
96 extern int arch_prctl(int func
, void *ptr
);
98 #define RAX_sig(context) ((context)->uc_mcontext.gregs[REG_RAX])
99 #define RBX_sig(context) ((context)->uc_mcontext.gregs[REG_RBX])
100 #define RCX_sig(context) ((context)->uc_mcontext.gregs[REG_RCX])
101 #define RDX_sig(context) ((context)->uc_mcontext.gregs[REG_RDX])
102 #define RSI_sig(context) ((context)->uc_mcontext.gregs[REG_RSI])
103 #define RDI_sig(context) ((context)->uc_mcontext.gregs[REG_RDI])
104 #define RBP_sig(context) ((context)->uc_mcontext.gregs[REG_RBP])
105 #define R8_sig(context) ((context)->uc_mcontext.gregs[REG_R8])
106 #define R9_sig(context) ((context)->uc_mcontext.gregs[REG_R9])
107 #define R10_sig(context) ((context)->uc_mcontext.gregs[REG_R10])
108 #define R11_sig(context) ((context)->uc_mcontext.gregs[REG_R11])
109 #define R12_sig(context) ((context)->uc_mcontext.gregs[REG_R12])
110 #define R13_sig(context) ((context)->uc_mcontext.gregs[REG_R13])
111 #define R14_sig(context) ((context)->uc_mcontext.gregs[REG_R14])
112 #define R15_sig(context) ((context)->uc_mcontext.gregs[REG_R15])
114 #define CS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 0))
115 #define GS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 1))
116 #define FS_sig(context) (*((WORD *)&(context)->uc_mcontext.gregs[REG_CSGSFS] + 2))
118 #define RSP_sig(context) ((context)->uc_mcontext.gregs[REG_RSP])
119 #define RIP_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
120 #define EFL_sig(context) ((context)->uc_mcontext.gregs[REG_EFL])
121 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
122 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
124 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.fpregs))
128 #if defined(__NetBSD__)
129 # include <sys/ucontext.h>
130 # include <sys/types.h>
133 #define RAX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RAX])
134 #define RBX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBX])
135 #define RCX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RCX])
136 #define RDX_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDX])
137 #define RSI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RSI])
138 #define RDI_sig(context) ((context)->uc_mcontext.__gregs[_REG_RDI])
139 #define RBP_sig(context) ((context)->uc_mcontext.__gregs[_REG_RBP])
140 #define R8_sig(context) ((context)->uc_mcontext.__gregs[_REG_R8])
141 #define R9_sig(context) ((context)->uc_mcontext.__gregs[_REG_R9])
142 #define R10_sig(context) ((context)->uc_mcontext.__gregs[_REG_R10])
143 #define R11_sig(context) ((context)->uc_mcontext.__gregs[_REG_R11])
144 #define R12_sig(context) ((context)->uc_mcontext.__gregs[_REG_R12])
145 #define R13_sig(context) ((context)->uc_mcontext.__gregs[_REG_R13])
146 #define R14_sig(context) ((context)->uc_mcontext.__gregs[_REG_R14])
147 #define R15_sig(context) ((context)->uc_mcontext.__gregs[_REG_R15])
149 #define CS_sig(context) ((context)->uc_mcontext.__gregs[_REG_CS])
150 #define DS_sig(context) ((context)->uc_mcontext.__gregs[_REG_DS])
151 #define ES_sig(context) ((context)->uc_mcontext.__gregs[_REG_ES])
152 #define FS_sig(context) ((context)->uc_mcontext.__gregs[_REG_FS])
153 #define GS_sig(context) ((context)->uc_mcontext.__gregs[_REG_GS])
154 #define SS_sig(context) ((context)->uc_mcontext.__gregs[_REG_SS])
156 #define EFL_sig(context) ((context)->uc_mcontext.__gregs[_REG_RFL])
158 #define RIP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_RIP]))
159 #define RSP_sig(context) (*((unsigned long*)&(context)->uc_mcontext.__gregs[_REG_URSP]))
161 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
162 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
164 #define FPU_sig(context) ((XMM_SAVE_AREA32 *)((context)->uc_mcontext.__fpregs))
165 #endif /* __NetBSD__ */
169 TRAP_x86_UNKNOWN
= -1, /* Unknown fault (TRAP_sig not defined) */
170 TRAP_x86_DIVIDE
= 0, /* Division by zero exception */
171 TRAP_x86_TRCTRAP
= 1, /* Single-step exception */
172 TRAP_x86_NMI
= 2, /* NMI interrupt */
173 TRAP_x86_BPTFLT
= 3, /* Breakpoint exception */
174 TRAP_x86_OFLOW
= 4, /* Overflow exception */
175 TRAP_x86_BOUND
= 5, /* Bound range exception */
176 TRAP_x86_PRIVINFLT
= 6, /* Invalid opcode exception */
177 TRAP_x86_DNA
= 7, /* Device not available exception */
178 TRAP_x86_DOUBLEFLT
= 8, /* Double fault exception */
179 TRAP_x86_FPOPFLT
= 9, /* Coprocessor segment overrun */
180 TRAP_x86_TSSFLT
= 10, /* Invalid TSS exception */
181 TRAP_x86_SEGNPFLT
= 11, /* Segment not present exception */
182 TRAP_x86_STKFLT
= 12, /* Stack fault */
183 TRAP_x86_PROTFLT
= 13, /* General protection fault */
184 TRAP_x86_PAGEFLT
= 14, /* Page fault */
185 TRAP_x86_ARITHTRAP
= 16, /* Floating point exception */
186 TRAP_x86_ALIGNFLT
= 17, /* Alignment check exception */
187 TRAP_x86_MCHK
= 18, /* Machine check exception */
188 TRAP_x86_CACHEFLT
= 19 /* Cache flush exception */
191 static const size_t teb_size
= 0x2000; /* we reserve two pages for the TEB */
192 static size_t signal_stack_size
;
194 typedef void (*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
195 typedef int (*wine_signal_handler
)(unsigned int sig
);
197 static wine_signal_handler handlers
[256];
200 /***********************************************************************
201 * Definitions for Win32 unwind tables
206 RUNTIME_FUNCTION chain
;
224 BYTE frame_offset
: 4;
225 struct opcode opcodes
[1]; /* info->count entries */
226 /* followed by handler_data */
229 #define UWOP_PUSH_NONVOL 0
230 #define UWOP_ALLOC_LARGE 1
231 #define UWOP_ALLOC_SMALL 2
232 #define UWOP_SET_FPREG 3
233 #define UWOP_SAVE_NONVOL 4
234 #define UWOP_SAVE_NONVOL_FAR 5
235 #define UWOP_SAVE_XMM128 8
236 #define UWOP_SAVE_XMM128_FAR 9
237 #define UWOP_PUSH_MACHFRAME 10
239 static void dump_unwind_info( ULONG64 base
, RUNTIME_FUNCTION
*function
)
241 static const char * const reg_names
[16] =
242 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
243 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
245 union handler_data
*handler_data
;
246 struct UNWIND_INFO
*info
;
247 unsigned int i
, count
;
249 TRACE( "**** func %x-%x\n", function
->BeginAddress
, function
->EndAddress
);
252 if (function
->UnwindData
& 1)
254 RUNTIME_FUNCTION
*next
= (RUNTIME_FUNCTION
*)((char *)base
+ (function
->UnwindData
& ~1));
255 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
256 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
,
257 (char *)base
+ next
->BeginAddress
, (char *)base
+ next
->EndAddress
);
261 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
263 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
264 info
, info
->flags
, info
->prolog
,
265 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
);
268 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
269 reg_names
[info
->frame_reg
], info
->frame_offset
* 16 );
271 for (i
= 0; i
< info
->count
; i
++)
273 TRACE( " 0x%x: ", info
->opcodes
[i
].offset
);
274 switch (info
->opcodes
[i
].code
)
276 case UWOP_PUSH_NONVOL
:
277 TRACE( "pushq %%%s\n", reg_names
[info
->opcodes
[i
].info
] );
279 case UWOP_ALLOC_LARGE
:
280 if (info
->opcodes
[i
].info
)
282 count
= *(DWORD
*)&info
->opcodes
[i
+1];
287 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
290 TRACE( "subq $0x%x,%%rsp\n", count
);
292 case UWOP_ALLOC_SMALL
:
293 count
= (info
->opcodes
[i
].info
+ 1) * 8;
294 TRACE( "subq $0x%x,%%rsp\n", count
);
297 TRACE( "leaq 0x%x(%%rsp),%s\n",
298 info
->frame_offset
* 16, reg_names
[info
->frame_reg
] );
300 case UWOP_SAVE_NONVOL
:
301 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
302 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
305 case UWOP_SAVE_NONVOL_FAR
:
306 count
= *(DWORD
*)&info
->opcodes
[i
+1];
307 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
310 case UWOP_SAVE_XMM128
:
311 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 16;
312 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
315 case UWOP_SAVE_XMM128_FAR
:
316 count
= *(DWORD
*)&info
->opcodes
[i
+1];
317 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
320 case UWOP_PUSH_MACHFRAME
:
321 TRACE( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
324 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
329 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
330 if (info
->flags
& UNW_FLAG_CHAININFO
)
332 TRACE( " chained to function %p-%p\n",
333 (char *)base
+ handler_data
->chain
.BeginAddress
,
334 (char *)base
+ handler_data
->chain
.EndAddress
);
335 function
= &handler_data
->chain
;
338 if (info
->flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
339 TRACE( " handler %p data at %p\n",
340 (char *)base
+ handler_data
->handler
, &handler_data
->handler
+ 1 );
345 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
349 TRACE( "scope table at %p\n", table
);
350 for (i
= 0; i
< table
->Count
; i
++)
351 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i
,
352 base
+ table
->ScopeRecord
[i
].BeginAddress
,
353 base
+ table
->ScopeRecord
[i
].EndAddress
,
354 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
355 base
+ table
->ScopeRecord
[i
].JumpTarget
);
359 /***********************************************************************
360 * Definitions for Dwarf unwind tables
363 enum dwarf_call_frame_info
365 DW_CFA_advance_loc
= 0x40,
366 DW_CFA_offset
= 0x80,
367 DW_CFA_restore
= 0xc0,
369 DW_CFA_set_loc
= 0x01,
370 DW_CFA_advance_loc1
= 0x02,
371 DW_CFA_advance_loc2
= 0x03,
372 DW_CFA_advance_loc4
= 0x04,
373 DW_CFA_offset_extended
= 0x05,
374 DW_CFA_restore_extended
= 0x06,
375 DW_CFA_undefined
= 0x07,
376 DW_CFA_same_value
= 0x08,
377 DW_CFA_register
= 0x09,
378 DW_CFA_remember_state
= 0x0a,
379 DW_CFA_restore_state
= 0x0b,
380 DW_CFA_def_cfa
= 0x0c,
381 DW_CFA_def_cfa_register
= 0x0d,
382 DW_CFA_def_cfa_offset
= 0x0e,
383 DW_CFA_def_cfa_expression
= 0x0f,
384 DW_CFA_expression
= 0x10,
385 DW_CFA_offset_extended_sf
= 0x11,
386 DW_CFA_def_cfa_sf
= 0x12,
387 DW_CFA_def_cfa_offset_sf
= 0x13,
388 DW_CFA_val_offset
= 0x14,
389 DW_CFA_val_offset_sf
= 0x15,
390 DW_CFA_val_expression
= 0x16,
393 #define DW_EH_PE_native 0x00
394 #define DW_EH_PE_leb128 0x01
395 #define DW_EH_PE_data2 0x02
396 #define DW_EH_PE_data4 0x03
397 #define DW_EH_PE_data8 0x04
398 #define DW_EH_PE_signed 0x08
399 #define DW_EH_PE_abs 0x00
400 #define DW_EH_PE_pcrel 0x10
401 #define DW_EH_PE_textrel 0x20
402 #define DW_EH_PE_datarel 0x30
403 #define DW_EH_PE_funcrel 0x40
404 #define DW_EH_PE_aligned 0x50
405 #define DW_EH_PE_indirect 0x80
406 #define DW_EH_PE_omit 0xff
408 struct dwarf_eh_bases
419 unsigned char version
;
420 unsigned char augmentation
[1];
426 unsigned int cie_offset
;
429 extern const struct dwarf_fde
*_Unwind_Find_FDE (void *, struct dwarf_eh_bases
*);
431 static unsigned short dwarf_get_u2( const unsigned char **p
)
433 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8);
438 static unsigned int dwarf_get_u4( const unsigned char **p
)
440 unsigned int ret
= (*p
)[0] | ((*p
)[1] << 8) | ((*p
)[2] << 16) | ((*p
)[3] << 24);
445 static ULONG64
dwarf_get_u8( const unsigned char **p
)
447 ULONG64 low
= dwarf_get_u4( p
);
448 ULONG64 high
= dwarf_get_u4( p
);
449 return low
| (high
<< 32);
452 static ULONG_PTR
dwarf_get_uleb128( const unsigned char **p
)
455 unsigned int shift
= 0;
461 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
464 } while (byte
& 0x80);
468 static LONG_PTR
dwarf_get_sleb128( const unsigned char **p
)
471 unsigned int shift
= 0;
477 ret
|= (ULONG_PTR
)(byte
& 0x7f) << shift
;
480 } while (byte
& 0x80);
482 if ((shift
< 8 * sizeof(ret
)) && (byte
& 0x40)) ret
|= -((ULONG_PTR
)1 << shift
);
486 static ULONG_PTR
dwarf_get_ptr( const unsigned char **p
, unsigned char encoding
)
490 if (encoding
== DW_EH_PE_omit
) return 0;
492 switch (encoding
& 0xf0)
498 base
= (ULONG_PTR
)*p
;
501 FIXME( "unsupported encoding %02x\n", encoding
);
505 switch (encoding
& 0x0f)
507 case DW_EH_PE_native
:
508 return base
+ dwarf_get_u8( p
);
509 case DW_EH_PE_leb128
:
510 return base
+ dwarf_get_uleb128( p
);
512 return base
+ dwarf_get_u2( p
);
514 return base
+ dwarf_get_u4( p
);
516 return base
+ dwarf_get_u8( p
);
517 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
518 return base
+ dwarf_get_sleb128( p
);
519 case DW_EH_PE_signed
|DW_EH_PE_data2
:
520 return base
+ (signed short)dwarf_get_u2( p
);
521 case DW_EH_PE_signed
|DW_EH_PE_data4
:
522 return base
+ (signed int)dwarf_get_u4( p
);
523 case DW_EH_PE_signed
|DW_EH_PE_data8
:
524 return base
+ (LONG64
)dwarf_get_u8( p
);
526 FIXME( "unsupported encoding %02x\n", encoding
);
533 RULE_UNSET
, /* not set at all */
534 RULE_UNDEFINED
, /* undefined value */
535 RULE_SAME
, /* same value as previous frame */
536 RULE_CFA_OFFSET
, /* stored at cfa offset */
537 RULE_OTHER_REG
/* stored in other register */
540 #define NB_FRAME_REGS 41
545 ULONG_PTR code_align
;
547 ULONG_PTR cfa_offset
;
548 unsigned char cfa_reg
;
549 unsigned char retaddr_reg
;
550 unsigned char fde_encoding
;
551 unsigned char signal_frame
;
552 enum reg_rule rules
[NB_FRAME_REGS
];
553 ULONG64 regs
[NB_FRAME_REGS
];
556 static const char *dwarf_reg_names
[NB_FRAME_REGS
] =
558 /* 0-7 */ "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
559 /* 8-16 */ "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "%rip",
560 /* 17-24 */ "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
561 /* 25-32 */ "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15",
562 /* 33-40 */ "%st0", "%st1", "%st2", "%st3", "%st4", "%st5", "%st6", "%st7"
565 static int valid_reg( ULONG_PTR reg
)
567 if (reg
>= NB_FRAME_REGS
) FIXME( "unsupported reg %lx\n", reg
);
568 return (reg
< NB_FRAME_REGS
);
571 static void execute_cfa_instructions( const unsigned char *ptr
, const unsigned char *end
,
572 ULONG_PTR last_ip
, struct frame_info
*info
)
574 while (ptr
< end
&& info
->ip
< last_ip
+ info
->signal_frame
)
576 enum dwarf_call_frame_info op
= *ptr
++;
582 case DW_CFA_advance_loc
:
584 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
585 TRACE( "%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
591 ULONG_PTR reg
= op
& 0x3f;
592 LONG_PTR offset
= dwarf_get_uleb128( &ptr
) * info
->data_align
;
593 if (!valid_reg( reg
)) break;
594 TRACE( "%lx: DW_CFA_offset %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
595 info
->regs
[reg
] = offset
;
596 info
->rules
[reg
] = RULE_CFA_OFFSET
;
601 ULONG_PTR reg
= op
& 0x3f;
602 if (!valid_reg( reg
)) break;
603 TRACE( "%lx: DW_CFA_restore %s\n", info
->ip
, dwarf_reg_names
[reg
] );
604 info
->rules
[reg
] = RULE_UNSET
;
615 ULONG_PTR loc
= dwarf_get_ptr( &ptr
, info
->fde_encoding
);
616 TRACE( "%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
620 case DW_CFA_advance_loc1
:
622 ULONG_PTR offset
= *ptr
++ * info
->code_align
;
623 TRACE( "%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
627 case DW_CFA_advance_loc2
:
629 ULONG_PTR offset
= dwarf_get_u2( &ptr
) * info
->code_align
;
630 TRACE( "%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
634 case DW_CFA_advance_loc4
:
636 ULONG_PTR offset
= dwarf_get_u4( &ptr
) * info
->code_align
;
637 TRACE( "%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
641 case DW_CFA_offset_extended
:
643 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
644 LONG_PTR offset
= dwarf_get_uleb128( &ptr
) * info
->data_align
;
645 if (!valid_reg( reg
)) break;
646 TRACE( "%lx: DW_CFA_offset_extended %s, %ld\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
647 info
->regs
[reg
] = offset
;
648 info
->rules
[reg
] = RULE_CFA_OFFSET
;
651 case DW_CFA_restore_extended
:
653 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
654 if (!valid_reg( reg
)) break;
655 TRACE( "%lx: DW_CFA_restore_extended %s\n", info
->ip
, dwarf_reg_names
[reg
] );
656 info
->rules
[reg
] = RULE_UNSET
;
659 case DW_CFA_undefined
:
661 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
662 if (!valid_reg( reg
)) break;
663 TRACE( "%lx: DW_CFA_undefined %s\n", info
->ip
, dwarf_reg_names
[reg
] );
664 info
->rules
[reg
] = RULE_UNDEFINED
;
667 case DW_CFA_same_value
:
669 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
670 if (!valid_reg( reg
)) break;
671 TRACE( "%lx: DW_CFA_same_value %s\n", info
->ip
, dwarf_reg_names
[reg
] );
672 info
->regs
[reg
] = reg
;
673 info
->rules
[reg
] = RULE_SAME
;
676 case DW_CFA_register
:
678 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
679 ULONG_PTR reg2
= dwarf_get_uleb128( &ptr
);
680 if (!valid_reg( reg
) || !valid_reg( reg2
)) break;
681 TRACE( "%lx: DW_CFA_register %s == %s\n", info
->ip
, dwarf_reg_names
[reg
], dwarf_reg_names
[reg2
] );
682 info
->regs
[reg
] = reg2
;
683 info
->rules
[reg
] = RULE_OTHER_REG
;
686 case DW_CFA_remember_state
:
687 FIXME( "%lx: DW_CFA_remember_state not implemented\n", info
->ip
);
689 case DW_CFA_restore_state
:
690 FIXME( "%lx: DW_CFA_restore_state not implemented\n", info
->ip
);
694 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
695 ULONG_PTR offset
= dwarf_get_uleb128( &ptr
);
696 if (!valid_reg( reg
)) break;
697 TRACE( "%lx: DW_CFA_def_cfa %s, %lu\n", info
->ip
, dwarf_reg_names
[reg
], offset
);
699 info
->cfa_offset
= offset
;
702 case DW_CFA_def_cfa_register
:
704 ULONG_PTR reg
= dwarf_get_uleb128( &ptr
);
705 if (!valid_reg( reg
)) break;
706 TRACE( "%lx: DW_CFA_def_cfa_register %s\n", info
->ip
, dwarf_reg_names
[reg
] );
710 case DW_CFA_def_cfa_offset
:
712 ULONG_PTR offset
= dwarf_get_uleb128( &ptr
);
713 TRACE( "%lx: DW_CFA_def_cfa_offset %lu\n", info
->ip
, offset
);
714 info
->cfa_offset
= offset
;
718 FIXME( "%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
724 /* retrieve a context register from its dwarf number */
725 static void *get_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
)
729 case 0: return &context
->Rax
;
730 case 1: return &context
->Rdx
;
731 case 2: return &context
->Rcx
;
732 case 3: return &context
->Rbx
;
733 case 4: return &context
->Rsi
;
734 case 5: return &context
->Rdi
;
735 case 6: return &context
->Rbp
;
736 case 7: return &context
->Rsp
;
737 case 8: return &context
->R8
;
738 case 9: return &context
->R9
;
739 case 10: return &context
->R10
;
740 case 11: return &context
->R11
;
741 case 12: return &context
->R12
;
742 case 13: return &context
->R13
;
743 case 14: return &context
->R14
;
744 case 15: return &context
->R15
;
745 case 16: return &context
->Rip
;
746 case 17: return &context
->u
.s
.Xmm0
;
747 case 18: return &context
->u
.s
.Xmm1
;
748 case 19: return &context
->u
.s
.Xmm2
;
749 case 20: return &context
->u
.s
.Xmm3
;
750 case 21: return &context
->u
.s
.Xmm4
;
751 case 22: return &context
->u
.s
.Xmm5
;
752 case 23: return &context
->u
.s
.Xmm6
;
753 case 24: return &context
->u
.s
.Xmm7
;
754 case 25: return &context
->u
.s
.Xmm8
;
755 case 26: return &context
->u
.s
.Xmm9
;
756 case 27: return &context
->u
.s
.Xmm10
;
757 case 28: return &context
->u
.s
.Xmm11
;
758 case 29: return &context
->u
.s
.Xmm12
;
759 case 30: return &context
->u
.s
.Xmm13
;
760 case 31: return &context
->u
.s
.Xmm14
;
761 case 32: return &context
->u
.s
.Xmm15
;
762 case 33: return &context
->u
.s
.Legacy
[0];
763 case 34: return &context
->u
.s
.Legacy
[1];
764 case 35: return &context
->u
.s
.Legacy
[2];
765 case 36: return &context
->u
.s
.Legacy
[3];
766 case 37: return &context
->u
.s
.Legacy
[4];
767 case 38: return &context
->u
.s
.Legacy
[5];
768 case 39: return &context
->u
.s
.Legacy
[6];
769 case 40: return &context
->u
.s
.Legacy
[7];
770 default: return NULL
;
774 /* set a context register from its dwarf number */
775 static void set_context_reg( CONTEXT
*context
, ULONG_PTR dw_reg
, void *val
)
779 case 0: context
->Rax
= *(ULONG64
*)val
; break;
780 case 1: context
->Rdx
= *(ULONG64
*)val
; break;
781 case 2: context
->Rcx
= *(ULONG64
*)val
; break;
782 case 3: context
->Rbx
= *(ULONG64
*)val
; break;
783 case 4: context
->Rsi
= *(ULONG64
*)val
; break;
784 case 5: context
->Rdi
= *(ULONG64
*)val
; break;
785 case 6: context
->Rbp
= *(ULONG64
*)val
; break;
786 case 7: context
->Rsp
= *(ULONG64
*)val
; break;
787 case 8: context
->R8
= *(ULONG64
*)val
; break;
788 case 9: context
->R9
= *(ULONG64
*)val
; break;
789 case 10: context
->R10
= *(ULONG64
*)val
; break;
790 case 11: context
->R11
= *(ULONG64
*)val
; break;
791 case 12: context
->R12
= *(ULONG64
*)val
; break;
792 case 13: context
->R13
= *(ULONG64
*)val
; break;
793 case 14: context
->R14
= *(ULONG64
*)val
; break;
794 case 15: context
->R15
= *(ULONG64
*)val
; break;
795 case 16: context
->Rip
= *(ULONG64
*)val
; break;
796 case 17: context
->u
.s
.Xmm0
= *(M128A
*)val
; break;
797 case 18: context
->u
.s
.Xmm1
= *(M128A
*)val
; break;
798 case 19: context
->u
.s
.Xmm2
= *(M128A
*)val
; break;
799 case 20: context
->u
.s
.Xmm3
= *(M128A
*)val
; break;
800 case 21: context
->u
.s
.Xmm4
= *(M128A
*)val
; break;
801 case 22: context
->u
.s
.Xmm5
= *(M128A
*)val
; break;
802 case 23: context
->u
.s
.Xmm6
= *(M128A
*)val
; break;
803 case 24: context
->u
.s
.Xmm7
= *(M128A
*)val
; break;
804 case 25: context
->u
.s
.Xmm8
= *(M128A
*)val
; break;
805 case 26: context
->u
.s
.Xmm9
= *(M128A
*)val
; break;
806 case 27: context
->u
.s
.Xmm10
= *(M128A
*)val
; break;
807 case 28: context
->u
.s
.Xmm11
= *(M128A
*)val
; break;
808 case 29: context
->u
.s
.Xmm12
= *(M128A
*)val
; break;
809 case 30: context
->u
.s
.Xmm13
= *(M128A
*)val
; break;
810 case 31: context
->u
.s
.Xmm14
= *(M128A
*)val
; break;
811 case 32: context
->u
.s
.Xmm15
= *(M128A
*)val
; break;
812 case 33: context
->u
.s
.Legacy
[0] = *(M128A
*)val
; break;
813 case 34: context
->u
.s
.Legacy
[1] = *(M128A
*)val
; break;
814 case 35: context
->u
.s
.Legacy
[2] = *(M128A
*)val
; break;
815 case 36: context
->u
.s
.Legacy
[3] = *(M128A
*)val
; break;
816 case 37: context
->u
.s
.Legacy
[4] = *(M128A
*)val
; break;
817 case 38: context
->u
.s
.Legacy
[5] = *(M128A
*)val
; break;
818 case 39: context
->u
.s
.Legacy
[6] = *(M128A
*)val
; break;
819 case 40: context
->u
.s
.Legacy
[7] = *(M128A
*)val
; break;
823 /* apply the computed frame info to the actual context */
824 static void apply_frame_info( CONTEXT
*context
, struct frame_info
*info
, ULONG_PTR cfa
)
828 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
830 switch (info
->rules
[i
])
836 case RULE_CFA_OFFSET
:
837 set_context_reg( context
, i
, (char *)cfa
+ info
->regs
[i
] );
840 FIXME( "other reg rule (%s == %s) not supported yet\n",
841 dwarf_reg_names
[i
], dwarf_reg_names
[info
->regs
[i
]] );
849 /***********************************************************************
850 * dwarf_virtual_unwind
852 * Equivalent of RtlVirtualUnwind for builtin modules.
854 static NTSTATUS
dwarf_virtual_unwind( ULONG64 ip
, ULONG64
*frame
,CONTEXT
*context
,
855 const struct dwarf_fde
*fde
, const struct dwarf_eh_bases
*bases
,
856 PEXCEPTION_ROUTINE
*handler
, void **handler_data
)
858 const struct dwarf_cie
*cie
;
859 const unsigned char *ptr
, *augmentation
, *end
;
860 ULONG_PTR len
, cfa
, code_end
;
861 struct frame_info info
;
862 int aug_z_format
= 0;
863 unsigned char lsda_encoding
= DW_EH_PE_omit
;
865 memset( &info
, 0, sizeof(info
) );
866 info
.ip
= (ULONG_PTR
)bases
->func
;
869 cie
= (const struct dwarf_cie
*)((const char *)&fde
->cie_offset
- fde
->cie_offset
);
871 /* parse the CIE first */
873 if (cie
->version
!= 1)
875 FIXME( "unknown CIE version %u at %p\n", cie
->version
, cie
);
876 return STATUS_INVALID_DISPOSITION
;
878 ptr
= cie
->augmentation
+ strlen((const char *)cie
->augmentation
) + 1;
880 info
.code_align
= dwarf_get_uleb128( &ptr
);
881 info
.data_align
= dwarf_get_sleb128( &ptr
);
882 info
.retaddr_reg
= *ptr
++;
884 TRACE( "function %lx base %p cie %p len %x id %x version %x aug '%s' code_align %lu data_align %ld retaddr %s\n",
885 ip
, bases
->func
, cie
, cie
->length
, cie
->id
, cie
->version
, cie
->augmentation
,
886 info
.code_align
, info
.data_align
, dwarf_reg_names
[info
.retaddr_reg
] );
889 for (augmentation
= cie
->augmentation
; *augmentation
; augmentation
++)
891 switch (*augmentation
)
894 len
= dwarf_get_uleb128( &ptr
);
899 lsda_encoding
= *ptr
++;
903 unsigned char encoding
= *ptr
++;
904 *handler
= (void *)dwarf_get_ptr( &ptr
, encoding
);
908 info
.fde_encoding
= *ptr
++;
911 info
.signal_frame
= 1;
914 FIXME( "unknown augmentation '%c'\n", *augmentation
);
915 if (!end
) return STATUS_INVALID_DISPOSITION
; /* cannot continue */
920 end
= (const unsigned char *)(&cie
->length
+ 1) + cie
->length
;
921 execute_cfa_instructions( ptr
, end
, ip
, &info
);
923 ptr
= (const unsigned char *)(fde
+ 1);
924 info
.ip
= dwarf_get_ptr( &ptr
, info
.fde_encoding
); /* fde code start */
925 code_end
= info
.ip
+ dwarf_get_ptr( &ptr
, info
.fde_encoding
& 0x0f ); /* fde code length */
927 if (aug_z_format
) /* get length of augmentation data */
929 len
= dwarf_get_uleb128( &ptr
);
934 *handler_data
= (void *)dwarf_get_ptr( &ptr
, lsda_encoding
);
937 end
= (const unsigned char *)(&fde
->length
+ 1) + fde
->length
;
938 TRACE( "fde %p len %x personality %p lsda %p code %lx-%lx\n",
939 fde
, fde
->length
, *handler
, *handler_data
, info
.ip
, code_end
);
940 execute_cfa_instructions( ptr
, end
, ip
, &info
);
942 *frame
= cfa
= *(ULONG_PTR
*)get_context_reg( context
, info
.cfa_reg
) + info
.cfa_offset
;
943 if (cfa
) apply_frame_info( context
, &info
, cfa
);
945 TRACE( "next function rip=%016lx\n", context
->Rip
);
946 TRACE( " rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
947 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
948 TRACE( " rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
949 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
950 TRACE( " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
951 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
952 TRACE( " r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
953 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
955 return STATUS_SUCCESS
;
959 /***********************************************************************
962 static inline int dispatch_signal(unsigned int sig
)
964 if (handlers
[sig
] == NULL
) return 0;
965 return handlers
[sig
](sig
);
968 /***********************************************************************
971 * Get the base of the signal stack for the current thread.
973 static inline void *get_signal_stack(void)
975 return (char *)NtCurrentTeb() + teb_size
;
978 /***********************************************************************
981 * Set the register values from a sigcontext.
983 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
985 context
->ContextFlags
= CONTEXT_CONTROL
| CONTEXT_INTEGER
| CONTEXT_SEGMENTS
;
986 context
->Rax
= RAX_sig(sigcontext
);
987 context
->Rcx
= RCX_sig(sigcontext
);
988 context
->Rdx
= RDX_sig(sigcontext
);
989 context
->Rbx
= RBX_sig(sigcontext
);
990 context
->Rsp
= RSP_sig(sigcontext
);
991 context
->Rbp
= RBP_sig(sigcontext
);
992 context
->Rsi
= RSI_sig(sigcontext
);
993 context
->Rdi
= RDI_sig(sigcontext
);
994 context
->R8
= R8_sig(sigcontext
);
995 context
->R9
= R9_sig(sigcontext
);
996 context
->R10
= R10_sig(sigcontext
);
997 context
->R11
= R11_sig(sigcontext
);
998 context
->R12
= R12_sig(sigcontext
);
999 context
->R13
= R13_sig(sigcontext
);
1000 context
->R14
= R14_sig(sigcontext
);
1001 context
->R15
= R15_sig(sigcontext
);
1002 context
->Rip
= RIP_sig(sigcontext
);
1003 context
->SegCs
= CS_sig(sigcontext
);
1004 context
->SegFs
= FS_sig(sigcontext
);
1005 context
->SegGs
= GS_sig(sigcontext
);
1006 context
->EFlags
= EFL_sig(sigcontext
);
1008 context
->SegDs
= DS_sig(sigcontext
);
1010 __asm__("movw %%ds,%0" : "=m" (context
->SegDs
));
1013 context
->SegEs
= ES_sig(sigcontext
);
1015 __asm__("movw %%es,%0" : "=m" (context
->SegEs
));
1018 context
->SegSs
= SS_sig(sigcontext
);
1020 __asm__("movw %%ss,%0" : "=m" (context
->SegSs
));
1022 context
->MxCsr
= 0; /* FIXME */
1023 if (FPU_sig(sigcontext
))
1025 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1026 context
->u
.FltSave
= *FPU_sig(sigcontext
);
1031 /***********************************************************************
1034 * Build a sigcontext from the register values.
1036 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
1038 RAX_sig(sigcontext
) = context
->Rax
;
1039 RCX_sig(sigcontext
) = context
->Rcx
;
1040 RDX_sig(sigcontext
) = context
->Rdx
;
1041 RBX_sig(sigcontext
) = context
->Rbx
;
1042 RSP_sig(sigcontext
) = context
->Rsp
;
1043 RBP_sig(sigcontext
) = context
->Rbp
;
1044 RSI_sig(sigcontext
) = context
->Rsi
;
1045 RDI_sig(sigcontext
) = context
->Rdi
;
1046 R8_sig(sigcontext
) = context
->R8
;
1047 R9_sig(sigcontext
) = context
->R9
;
1048 R10_sig(sigcontext
) = context
->R10
;
1049 R11_sig(sigcontext
) = context
->R11
;
1050 R12_sig(sigcontext
) = context
->R12
;
1051 R13_sig(sigcontext
) = context
->R13
;
1052 R14_sig(sigcontext
) = context
->R14
;
1053 R15_sig(sigcontext
) = context
->R15
;
1054 RIP_sig(sigcontext
) = context
->Rip
;
1055 CS_sig(sigcontext
) = context
->SegCs
;
1056 FS_sig(sigcontext
) = context
->SegFs
;
1057 GS_sig(sigcontext
) = context
->SegGs
;
1058 EFL_sig(sigcontext
) = context
->EFlags
;
1060 DS_sig(sigcontext
) = context
->SegDs
;
1063 ES_sig(sigcontext
) = context
->SegEs
;
1066 SS_sig(sigcontext
) = context
->SegSs
;
1068 if (FPU_sig(sigcontext
)) *FPU_sig(sigcontext
) = context
->u
.FltSave
;
1072 /***********************************************************************
1073 * RtlCaptureContext (NTDLL.@)
1075 void WINAPI
__regs_RtlCaptureContext( CONTEXT
*context
, CONTEXT
*regs
)
1079 DEFINE_REGS_ENTRYPOINT( RtlCaptureContext
, 1 )
1082 /***********************************************************************
1085 * Set the new CPU context.
1087 void set_cpu_context( const CONTEXT
*context
)
1089 extern void CDECL
__wine_restore_regs( const CONTEXT
* ) DECLSPEC_NORETURN
;
1090 __wine_restore_regs( context
);
1094 /***********************************************************************
1097 * Copy a register context according to the flags.
1099 void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
1101 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
1102 if (flags
& CONTEXT_CONTROL
)
1104 to
->Rbp
= from
->Rbp
;
1105 to
->Rip
= from
->Rip
;
1106 to
->Rsp
= from
->Rsp
;
1107 to
->SegCs
= from
->SegCs
;
1108 to
->SegSs
= from
->SegSs
;
1109 to
->EFlags
= from
->EFlags
;
1110 to
->MxCsr
= from
->MxCsr
;
1112 if (flags
& CONTEXT_INTEGER
)
1114 to
->Rax
= from
->Rax
;
1115 to
->Rcx
= from
->Rcx
;
1116 to
->Rdx
= from
->Rdx
;
1117 to
->Rbx
= from
->Rbx
;
1118 to
->Rsi
= from
->Rsi
;
1119 to
->Rdi
= from
->Rdi
;
1122 to
->R10
= from
->R10
;
1123 to
->R11
= from
->R11
;
1124 to
->R12
= from
->R12
;
1125 to
->R13
= from
->R13
;
1126 to
->R14
= from
->R14
;
1127 to
->R15
= from
->R15
;
1129 if (flags
& CONTEXT_SEGMENTS
)
1131 to
->SegDs
= from
->SegDs
;
1132 to
->SegEs
= from
->SegEs
;
1133 to
->SegFs
= from
->SegFs
;
1134 to
->SegGs
= from
->SegGs
;
1136 if (flags
& CONTEXT_FLOATING_POINT
)
1138 to
->u
.FltSave
= from
->u
.FltSave
;
1140 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1142 to
->Dr0
= from
->Dr0
;
1143 to
->Dr1
= from
->Dr1
;
1144 to
->Dr2
= from
->Dr2
;
1145 to
->Dr3
= from
->Dr3
;
1146 to
->Dr6
= from
->Dr6
;
1147 to
->Dr7
= from
->Dr7
;
1152 /***********************************************************************
1155 * Convert a register context to the server format.
1157 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
1159 DWORD flags
= from
->ContextFlags
& ~CONTEXT_AMD64
; /* get rid of CPU id */
1161 memset( to
, 0, sizeof(*to
) );
1162 to
->cpu
= CPU_x86_64
;
1164 if (flags
& CONTEXT_CONTROL
)
1166 to
->flags
|= SERVER_CTX_CONTROL
;
1167 to
->ctl
.x86_64_regs
.rbp
= from
->Rbp
;
1168 to
->ctl
.x86_64_regs
.rip
= from
->Rip
;
1169 to
->ctl
.x86_64_regs
.rsp
= from
->Rsp
;
1170 to
->ctl
.x86_64_regs
.cs
= from
->SegCs
;
1171 to
->ctl
.x86_64_regs
.ss
= from
->SegSs
;
1172 to
->ctl
.x86_64_regs
.flags
= from
->EFlags
;
1173 to
->ctl
.x86_64_regs
.mxcsr
= from
->MxCsr
;
1175 if (flags
& CONTEXT_INTEGER
)
1177 to
->flags
|= SERVER_CTX_INTEGER
;
1178 to
->integer
.x86_64_regs
.rax
= from
->Rax
;
1179 to
->integer
.x86_64_regs
.rcx
= from
->Rcx
;
1180 to
->integer
.x86_64_regs
.rdx
= from
->Rdx
;
1181 to
->integer
.x86_64_regs
.rbx
= from
->Rbx
;
1182 to
->integer
.x86_64_regs
.rsi
= from
->Rsi
;
1183 to
->integer
.x86_64_regs
.rdi
= from
->Rdi
;
1184 to
->integer
.x86_64_regs
.r8
= from
->R8
;
1185 to
->integer
.x86_64_regs
.r9
= from
->R9
;
1186 to
->integer
.x86_64_regs
.r10
= from
->R10
;
1187 to
->integer
.x86_64_regs
.r11
= from
->R11
;
1188 to
->integer
.x86_64_regs
.r12
= from
->R12
;
1189 to
->integer
.x86_64_regs
.r13
= from
->R13
;
1190 to
->integer
.x86_64_regs
.r14
= from
->R14
;
1191 to
->integer
.x86_64_regs
.r15
= from
->R15
;
1193 if (flags
& CONTEXT_SEGMENTS
)
1195 to
->flags
|= SERVER_CTX_SEGMENTS
;
1196 to
->seg
.x86_64_regs
.ds
= from
->SegDs
;
1197 to
->seg
.x86_64_regs
.es
= from
->SegEs
;
1198 to
->seg
.x86_64_regs
.fs
= from
->SegFs
;
1199 to
->seg
.x86_64_regs
.gs
= from
->SegGs
;
1201 if (flags
& CONTEXT_FLOATING_POINT
)
1203 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
1204 memcpy( to
->fp
.x86_64_regs
.fpregs
, &from
->u
.FltSave
, sizeof(to
->fp
.x86_64_regs
.fpregs
) );
1206 if (flags
& CONTEXT_DEBUG_REGISTERS
)
1208 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
1209 to
->debug
.x86_64_regs
.dr0
= from
->Dr0
;
1210 to
->debug
.x86_64_regs
.dr1
= from
->Dr1
;
1211 to
->debug
.x86_64_regs
.dr2
= from
->Dr2
;
1212 to
->debug
.x86_64_regs
.dr3
= from
->Dr3
;
1213 to
->debug
.x86_64_regs
.dr6
= from
->Dr6
;
1214 to
->debug
.x86_64_regs
.dr7
= from
->Dr7
;
1216 return STATUS_SUCCESS
;
1220 /***********************************************************************
1221 * context_from_server
1223 * Convert a register context from the server format.
1225 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
1227 if (from
->cpu
!= CPU_x86_64
) return STATUS_INVALID_PARAMETER
;
1229 to
->ContextFlags
= CONTEXT_AMD64
;
1230 if (from
->flags
& SERVER_CTX_CONTROL
)
1232 to
->ContextFlags
|= CONTEXT_CONTROL
;
1233 to
->Rbp
= from
->ctl
.x86_64_regs
.rbp
;
1234 to
->Rip
= from
->ctl
.x86_64_regs
.rip
;
1235 to
->Rsp
= from
->ctl
.x86_64_regs
.rsp
;
1236 to
->SegCs
= from
->ctl
.x86_64_regs
.cs
;
1237 to
->SegSs
= from
->ctl
.x86_64_regs
.ss
;
1238 to
->EFlags
= from
->ctl
.x86_64_regs
.flags
;
1239 to
->MxCsr
= from
->ctl
.x86_64_regs
.mxcsr
;
1242 if (from
->flags
& SERVER_CTX_INTEGER
)
1244 to
->ContextFlags
|= CONTEXT_INTEGER
;
1245 to
->Rax
= from
->integer
.x86_64_regs
.rax
;
1246 to
->Rcx
= from
->integer
.x86_64_regs
.rcx
;
1247 to
->Rdx
= from
->integer
.x86_64_regs
.rdx
;
1248 to
->Rbx
= from
->integer
.x86_64_regs
.rbx
;
1249 to
->Rsi
= from
->integer
.x86_64_regs
.rsi
;
1250 to
->Rdi
= from
->integer
.x86_64_regs
.rdi
;
1251 to
->R8
= from
->integer
.x86_64_regs
.r8
;
1252 to
->R9
= from
->integer
.x86_64_regs
.r9
;
1253 to
->R10
= from
->integer
.x86_64_regs
.r10
;
1254 to
->R11
= from
->integer
.x86_64_regs
.r11
;
1255 to
->R12
= from
->integer
.x86_64_regs
.r12
;
1256 to
->R13
= from
->integer
.x86_64_regs
.r13
;
1257 to
->R14
= from
->integer
.x86_64_regs
.r14
;
1258 to
->R15
= from
->integer
.x86_64_regs
.r15
;
1260 if (from
->flags
& SERVER_CTX_SEGMENTS
)
1262 to
->ContextFlags
|= CONTEXT_SEGMENTS
;
1263 to
->SegDs
= from
->seg
.x86_64_regs
.ds
;
1264 to
->SegEs
= from
->seg
.x86_64_regs
.es
;
1265 to
->SegFs
= from
->seg
.x86_64_regs
.fs
;
1266 to
->SegGs
= from
->seg
.x86_64_regs
.gs
;
1268 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
1270 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
1271 memcpy( &to
->u
.FltSave
, from
->fp
.x86_64_regs
.fpregs
, sizeof(from
->fp
.x86_64_regs
.fpregs
) );
1273 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
1275 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
1276 to
->Dr0
= from
->debug
.x86_64_regs
.dr0
;
1277 to
->Dr1
= from
->debug
.x86_64_regs
.dr1
;
1278 to
->Dr2
= from
->debug
.x86_64_regs
.dr2
;
1279 to
->Dr3
= from
->debug
.x86_64_regs
.dr3
;
1280 to
->Dr6
= from
->debug
.x86_64_regs
.dr6
;
1281 to
->Dr7
= from
->debug
.x86_64_regs
.dr7
;
1283 return STATUS_SUCCESS
;
1287 extern void raise_func_trampoline( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, raise_func func
);
1288 __ASM_GLOBAL_FUNC( raise_func_trampoline
,
1289 ".cfi_signal_frame\n\t"
1290 ".cfi_def_cfa %rbp,144\n\t" /* red zone + rip + rbp */
1291 ".cfi_rel_offset %rip,8\n\t"
1292 ".cfi_rel_offset %rbp,0\n\t"
1296 /***********************************************************************
1299 * Setup a proper stack frame for the raise function, and modify the
1300 * sigcontext so that the return from the signal handler will call
1301 * the raise function.
1303 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
1308 EXCEPTION_RECORD rec
;
1311 ULONG64 red_zone
[16];
1314 DWORD exception_code
= 0;
1316 stack
= (struct stack_layout
*)(RSP_sig(sigcontext
) & ~15);
1318 /* stack sanity checks */
1320 if ((char *)stack
>= (char *)get_signal_stack() &&
1321 (char *)stack
< (char *)get_signal_stack() + signal_stack_size
)
1323 ERR( "nested exception on signal stack in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1324 GetCurrentThreadId(), RIP_sig(sigcontext
), RSP_sig(sigcontext
),
1325 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1329 if (stack
- 1 > stack
|| /* check for overflow in subtraction */
1330 (char *)stack
<= (char *)NtCurrentTeb()->DeallocationStack
||
1331 (char *)stack
> (char *)NtCurrentTeb()->Tib
.StackBase
)
1333 WARN( "exception outside of stack limits in thread %04x eip %016lx esp %016lx stack %p-%p\n",
1334 GetCurrentThreadId(), RIP_sig(sigcontext
), RSP_sig(sigcontext
),
1335 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1337 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->DeallocationStack
+ 4096)
1339 /* stack overflow on last page, unrecoverable */
1340 UINT diff
= (char *)NtCurrentTeb()->DeallocationStack
+ 4096 - (char *)(stack
- 1);
1341 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1342 diff
, GetCurrentThreadId(), RIP_sig(sigcontext
),
1343 RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1344 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1347 else if ((char *)(stack
- 1) < (char *)NtCurrentTeb()->Tib
.StackLimit
)
1349 /* stack access below stack limit, may be recoverable */
1350 if (virtual_handle_stack_fault( stack
- 1 )) exception_code
= EXCEPTION_STACK_OVERFLOW
;
1353 UINT diff
= (char *)NtCurrentTeb()->Tib
.StackLimit
- (char *)(stack
- 1);
1354 ERR( "stack overflow %u bytes in thread %04x eip %016lx esp %016lx stack %p-%p-%p\n",
1355 diff
, GetCurrentThreadId(), RIP_sig(sigcontext
),
1356 RSP_sig(sigcontext
), NtCurrentTeb()->DeallocationStack
,
1357 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1362 stack
--; /* push the stack_layout structure */
1363 stack
->rec
.ExceptionRecord
= NULL
;
1364 stack
->rec
.ExceptionCode
= exception_code
;
1365 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
1366 stack
->rec
.ExceptionAddress
= (void *)RIP_sig(sigcontext
);
1367 stack
->rec
.NumberParameters
= 0;
1368 save_context( &stack
->context
, sigcontext
);
1370 /* store return address and %rbp without aligning, so that the offset is fixed */
1371 rsp_ptr
= (ULONG64
*)RSP_sig(sigcontext
) - 16;
1372 *(--rsp_ptr
) = RIP_sig(sigcontext
);
1373 *(--rsp_ptr
) = RBP_sig(sigcontext
);
1375 /* now modify the sigcontext to return to the raise function */
1376 RIP_sig(sigcontext
) = (ULONG_PTR
)raise_func_trampoline
;
1377 RDI_sig(sigcontext
) = (ULONG_PTR
)&stack
->rec
;
1378 RSI_sig(sigcontext
) = (ULONG_PTR
)&stack
->context
;
1379 RDX_sig(sigcontext
) = (ULONG_PTR
)func
;
1380 RBP_sig(sigcontext
) = (ULONG_PTR
)rsp_ptr
;
1381 RSP_sig(sigcontext
) = (ULONG_PTR
)stack
;
1382 /* clear single-step, direction, and align check flag */
1383 EFL_sig(sigcontext
) &= ~(0x100|0x400|0x40000);
1389 /**********************************************************************
1390 * find_function_info
1392 static RUNTIME_FUNCTION
*find_function_info( ULONG64 pc
, HMODULE module
,
1393 RUNTIME_FUNCTION
*func
, ULONG size
)
1396 int max
= size
/sizeof(*func
) - 1;
1400 int pos
= (min
+ max
) / 2;
1401 if ((char *)pc
< (char *)module
+ func
[pos
].BeginAddress
) max
= pos
- 1;
1402 else if ((char *)pc
>= (char *)module
+ func
[pos
].EndAddress
) min
= pos
+ 1;
1406 while (func
->UnwindData
& 1) /* follow chained entry */
1407 func
= (RUNTIME_FUNCTION
*)((char *)module
+ (func
->UnwindData
& ~1));
1415 /**********************************************************************
1418 * Call a single exception handler.
1419 * FIXME: Handle nested exceptions.
1421 static NTSTATUS
call_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*orig_context
)
1425 dispatch
->ControlPc
= dispatch
->ContextRecord
->Rip
;
1427 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
1428 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
1429 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
1430 TRACE( "handler at %p returned %u\n", dispatch
->LanguageHandler
, res
);
1434 case ExceptionContinueExecution
:
1435 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
1436 *orig_context
= *dispatch
->ContextRecord
;
1437 return STATUS_SUCCESS
;
1438 case ExceptionContinueSearch
:
1440 case ExceptionNestedException
:
1443 return STATUS_INVALID_DISPOSITION
;
1445 return STATUS_UNHANDLED_EXCEPTION
;
1449 /**********************************************************************
1452 * Call a single exception handler from the TEB chain.
1453 * FIXME: Handle nested exceptions.
1455 static NTSTATUS
call_teb_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
1456 EXCEPTION_REGISTRATION_RECORD
*teb_frame
, CONTEXT
*orig_context
)
1458 EXCEPTION_REGISTRATION_RECORD
*dispatcher
;
1461 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
1462 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
1463 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
1464 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
1468 case ExceptionContinueExecution
:
1469 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
1470 *orig_context
= *dispatch
->ContextRecord
;
1471 return STATUS_SUCCESS
;
1472 case ExceptionContinueSearch
:
1474 case ExceptionNestedException
:
1477 return STATUS_INVALID_DISPOSITION
;
1479 return STATUS_UNHANDLED_EXCEPTION
;
1483 /**********************************************************************
1484 * call_stack_handlers
1486 * Call the stack handlers chain.
1488 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
1490 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1491 UNWIND_HISTORY_TABLE table
;
1492 RUNTIME_FUNCTION
*dir
;
1493 DISPATCHER_CONTEXT dispatch
;
1494 CONTEXT context
, new_context
;
1499 context
= *orig_context
;
1500 dispatch
.TargetIp
= 0;
1501 dispatch
.ContextRecord
= &context
;
1502 dispatch
.HistoryTable
= &table
;
1503 dispatch
.ScopeIndex
= 0; /* FIXME */
1506 new_context
= context
;
1508 /* FIXME: should use the history table to make things faster */
1512 dispatch
.ImageBase
= 0;
1514 if (!LdrFindEntryForAddress( (void *)context
.Rip
, &module
))
1516 if (!(dir
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
1517 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)) &&
1518 !(module
->Flags
& LDR_WINE_INTERNAL
))
1520 ERR( "module %s doesn't contain exception data, can't dispatch exception\n",
1521 debugstr_w(module
->BaseDllName
.Buffer
) );
1524 dispatch
.ImageBase
= (ULONG64
)module
->BaseAddress
;
1529 struct dwarf_eh_bases bases
;
1530 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
.Rip
- 1), &bases
);
1533 ERR( "no exception data found in %s for function %lx\n",
1534 module
? debugstr_w(module
->BaseDllName
.Buffer
) : "system library", context
.Rip
);
1537 status
= dwarf_virtual_unwind( context
.Rip
, &dispatch
.EstablisherFrame
, &new_context
,
1538 fde
, &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
1539 if (status
!= STATUS_SUCCESS
) return status
;
1540 dispatch
.FunctionEntry
= NULL
;
1541 if (dispatch
.LanguageHandler
&& !module
)
1543 FIXME( "calling personality routine in system library not supported yet\n" );
1544 dispatch
.LanguageHandler
= NULL
;
1549 if (!(dispatch
.FunctionEntry
= find_function_info( context
.Rip
, module
->BaseAddress
,
1553 context
.Rip
= *(ULONG64
*)context
.Rsp
;
1554 context
.Rsp
+= sizeof(ULONG64
);
1557 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_EHANDLER
, dispatch
.ImageBase
,
1558 context
.Rip
, dispatch
.FunctionEntry
,
1559 &new_context
, &dispatch
.HandlerData
,
1560 &dispatch
.EstablisherFrame
, NULL
);
1563 if (!dispatch
.EstablisherFrame
) break;
1565 if ((dispatch
.EstablisherFrame
& 7) ||
1566 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
1567 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
1569 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
1570 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1571 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
1575 if (dispatch
.LanguageHandler
)
1577 status
= call_handler( rec
, &dispatch
, orig_context
);
1578 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
1580 /* hack: call wine handlers registered in the tib list */
1581 else while ((ULONG64
)teb_frame
< new_context
.Rsp
)
1583 TRACE( "found wine frame %p rsp %lx handler %p\n",
1584 teb_frame
, new_context
.Rsp
, teb_frame
->Handler
);
1585 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
1586 context
= *orig_context
;
1587 status
= call_teb_handler( rec
, &dispatch
, teb_frame
, orig_context
);
1588 if (status
!= STATUS_UNHANDLED_EXCEPTION
) return status
;
1589 teb_frame
= teb_frame
->Prev
;
1592 if (new_context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
1593 context
= new_context
;
1595 return STATUS_UNHANDLED_EXCEPTION
;
1599 /*******************************************************************
1602 * Implementation of NtRaiseException.
1604 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
1612 TRACE( "code=%x flags=%x addr=%p ip=%lx tid=%04x\n",
1613 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
1614 context
->Rip
, GetCurrentThreadId() );
1615 for (c
= 0; c
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); c
++)
1616 TRACE( " info[%d]=%08lx\n", c
, rec
->ExceptionInformation
[c
] );
1617 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
1619 if (rec
->ExceptionInformation
[1] >> 16)
1620 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
1621 rec
->ExceptionAddress
,
1622 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
1624 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
1625 rec
->ExceptionAddress
,
1626 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
1630 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
1631 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1632 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
1633 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1634 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
1635 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1636 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
1637 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1639 status
= send_debug_event( rec
, TRUE
, context
);
1640 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
1641 return STATUS_SUCCESS
;
1643 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
1644 return STATUS_SUCCESS
;
1646 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
1650 /* last chance exception */
1652 status
= send_debug_event( rec
, FALSE
, context
);
1653 if (status
!= DBG_CONTINUE
)
1655 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
1656 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
1657 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
1658 ERR("Process attempted to continue execution after noncontinuable exception.\n");
1660 ERR("Unhandled exception code %x flags %x addr %p\n",
1661 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
1662 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
1664 return STATUS_SUCCESS
;
1668 /**********************************************************************
1669 * raise_segv_exception
1671 static void raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1675 switch(rec
->ExceptionCode
)
1677 case EXCEPTION_ACCESS_VIOLATION
:
1678 if (rec
->NumberParameters
== 2)
1680 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
1681 rec
->ExceptionInformation
[0] )))
1682 set_cpu_context( context
);
1686 status
= raise_exception( rec
, context
, TRUE
);
1687 if (status
) raise_status( status
, rec
);
1691 /**********************************************************************
1692 * raise_generic_exception
1694 * Generic raise function for exceptions that don't need special treatment.
1696 static void raise_generic_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
1698 NTSTATUS status
= raise_exception( rec
, context
, TRUE
);
1699 if (status
) raise_status( status
, rec
);
1703 /**********************************************************************
1706 * Handler for SIGSEGV and related errors.
1708 static void segv_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1710 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_segv_exception
);
1711 ucontext_t
*ucontext
= sigcontext
;
1713 switch(TRAP_sig(ucontext
))
1715 case TRAP_x86_OFLOW
: /* Overflow exception */
1716 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1718 case TRAP_x86_BOUND
: /* Bound range exception */
1719 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1721 case TRAP_x86_PRIVINFLT
: /* Invalid opcode exception */
1722 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1724 case TRAP_x86_STKFLT
: /* Stack fault */
1725 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
1727 case TRAP_x86_SEGNPFLT
: /* Segment not present exception */
1728 case TRAP_x86_PROTFLT
: /* General protection fault */
1729 case TRAP_x86_UNKNOWN
: /* Unknown fault code */
1730 rec
->ExceptionCode
= ERROR_sig(ucontext
) ? EXCEPTION_ACCESS_VIOLATION
: EXCEPTION_PRIV_INSTRUCTION
;
1731 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1733 case TRAP_x86_PAGEFLT
: /* Page fault */
1734 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
1735 rec
->NumberParameters
= 2;
1736 rec
->ExceptionInformation
[0] = (ERROR_sig(ucontext
) & 2) != 0;
1737 rec
->ExceptionInformation
[1] = (ULONG_PTR
)siginfo
->si_addr
;
1739 case TRAP_x86_ALIGNFLT
: /* Alignment check exception */
1740 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
1743 ERR( "Got unexpected trap %ld\n", TRAP_sig(ucontext
) );
1745 case TRAP_x86_NMI
: /* NMI interrupt */
1746 case TRAP_x86_DNA
: /* Device not available exception */
1747 case TRAP_x86_DOUBLEFLT
: /* Double fault exception */
1748 case TRAP_x86_TSSFLT
: /* Invalid TSS exception */
1749 case TRAP_x86_MCHK
: /* Machine check exception */
1750 case TRAP_x86_CACHEFLT
: /* Cache flush exception */
1751 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
1756 /**********************************************************************
1759 * Handler for SIGTRAP.
1761 static void trap_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1763 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
1765 switch (siginfo
->si_code
)
1767 case TRAP_TRACE
: /* Single-step exception */
1768 rec
->ExceptionCode
= EXCEPTION_SINGLE_STEP
;
1770 case TRAP_BRKPT
: /* Breakpoint exception */
1771 rec
->ExceptionAddress
= (char *)rec
->ExceptionAddress
- 1; /* back up over the int3 instruction */
1774 rec
->ExceptionCode
= EXCEPTION_BREAKPOINT
;
1779 /**********************************************************************
1782 * Handler for SIGFPE.
1784 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1786 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
1788 switch (siginfo
->si_code
)
1791 rec
->ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
1794 rec
->ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
1797 rec
->ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
1800 rec
->ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
1803 rec
->ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
1806 rec
->ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
1809 rec
->ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
1813 rec
->ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
1818 /**********************************************************************
1821 * Handler for SIGINT.
1823 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1825 if (!dispatch_signal(SIGINT
))
1827 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
1828 rec
->ExceptionCode
= CONTROL_C_EXIT
;
1833 /**********************************************************************
1836 * Handler for SIGABRT.
1838 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
1840 EXCEPTION_RECORD
*rec
= setup_exception( sigcontext
, raise_generic_exception
);
1841 rec
->ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
1842 rec
->ExceptionFlags
= EH_NONCONTINUABLE
;
1846 /**********************************************************************
1849 * Handler for SIGQUIT.
1851 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
1857 /**********************************************************************
1860 * Handler for SIGUSR1, used to signal a thread that it got suspended.
1862 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *ucontext
)
1866 save_context( &context
, ucontext
);
1867 wait_suspend( &context
);
1868 restore_context( &context
, ucontext
);
1872 /**********************************************************************
1873 * get_signal_stack_total_size
1875 * Retrieve the size to allocate for the signal stack, including the TEB at the bottom.
1876 * Must be a power of two.
1878 size_t get_signal_stack_total_size(void)
1880 assert( sizeof(TEB
) <= teb_size
);
1881 if (!signal_stack_size
)
1883 size_t size
= 8192, min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
1884 /* find the first power of two not smaller than min_size */
1885 while (size
< min_size
) size
*= 2;
1886 signal_stack_size
= size
- teb_size
;
1888 return signal_stack_size
+ teb_size
;
1892 /***********************************************************************
1893 * __wine_set_signal_handler (NTDLL.@)
1895 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
1897 if (sig
> sizeof(handlers
) / sizeof(handlers
[0])) return -1;
1898 if (handlers
[sig
] != NULL
) return -2;
1899 handlers
[sig
] = wsh
;
1904 /**********************************************************************
1905 * signal_init_thread
1907 void signal_init_thread( TEB
*teb
)
1912 arch_prctl( ARCH_SET_GS
, teb
);
1914 # error Please define setting %gs for your architecture
1917 ss
.ss_sp
= (char *)teb
+ teb_size
;
1918 ss
.ss_size
= signal_stack_size
;
1920 if (sigaltstack(&ss
, NULL
) == -1) perror( "sigaltstack" );
1923 /**********************************************************************
1924 * signal_init_process
1926 void signal_init_process(void)
1928 struct sigaction sig_act
;
1930 sig_act
.sa_mask
= server_block_set
;
1931 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
| SA_ONSTACK
;
1933 sig_act
.sa_sigaction
= int_handler
;
1934 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
1935 sig_act
.sa_sigaction
= fpe_handler
;
1936 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
1937 sig_act
.sa_sigaction
= abrt_handler
;
1938 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
1939 sig_act
.sa_sigaction
= quit_handler
;
1940 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
1941 sig_act
.sa_sigaction
= usr1_handler
;
1942 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
1944 sig_act
.sa_sigaction
= segv_handler
;
1945 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
1946 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
1948 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
1952 sig_act
.sa_sigaction
= trap_handler
;
1953 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
1958 perror("sigaction");
1963 /**********************************************************************
1964 * RtlLookupFunctionEntry (NTDLL.@)
1966 PRUNTIME_FUNCTION WINAPI
RtlLookupFunctionEntry( ULONG64 pc
, ULONG64
*base
, UNWIND_HISTORY_TABLE
*table
)
1969 RUNTIME_FUNCTION
*func
;
1972 /* FIXME: should use the history table to make things faster */
1974 if (LdrFindEntryForAddress( (void *)pc
, &module
))
1976 WARN( "module not found for %lx\n", pc
);
1979 if (!(func
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
1980 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)))
1982 WARN( "no exception table found in module %p pc %lx\n", module
->BaseAddress
, pc
);
1985 func
= find_function_info( pc
, module
->BaseAddress
, func
, size
);
1986 if (func
) *base
= (ULONG64
)module
->BaseAddress
;
1990 static ULONG64
get_int_reg( CONTEXT
*context
, int reg
)
1992 return *(&context
->Rax
+ reg
);
1995 static void set_int_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, ULONG64 val
)
1997 *(&context
->Rax
+ reg
) = val
;
1998 if (ctx_ptr
) ctx_ptr
->u2
.IntegerContext
[reg
] = &context
->Rax
+ reg
;
2001 static void set_float_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, M128A val
)
2003 *(&context
->u
.s
.Xmm0
+ reg
) = val
;
2004 if (ctx_ptr
) ctx_ptr
->u1
.FloatingContext
[reg
] = &context
->u
.s
.Xmm0
+ reg
;
2007 static int get_opcode_size( struct opcode op
)
2011 case UWOP_ALLOC_LARGE
:
2012 return 2 + (op
.info
!= 0);
2013 case UWOP_SAVE_NONVOL
:
2014 case UWOP_SAVE_XMM128
:
2016 case UWOP_SAVE_NONVOL_FAR
:
2017 case UWOP_SAVE_XMM128_FAR
:
2024 static BOOL
is_inside_epilog( BYTE
*pc
)
2026 /* add or lea must be the first instruction, and it must have a rex.W prefix */
2027 if ((pc
[0] & 0xf8) == 0x48)
2031 case 0x81: /* add $nnnn,%rsp */
2032 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
2038 case 0x83: /* add $n,%rsp */
2039 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
2045 case 0x8d: /* lea n(reg),%rsp */
2046 if (pc
[0] & 0x06) return FALSE
; /* rex.RX must be cleared */
2047 if (((pc
[2] >> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
2048 if ((pc
[2] & 7) == 4) return FALSE
; /* no SIB byte allowed */
2049 if ((pc
[2] >> 6) == 1) /* 8-bit offset */
2054 if ((pc
[2] >> 6) == 2) /* 32-bit offset */
2063 /* now check for various pop instructions */
2069 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
2073 case 0x58: /* pop %rax/%r8 */
2074 case 0x59: /* pop %rcx/%r9 */
2075 case 0x5a: /* pop %rdx/%r10 */
2076 case 0x5b: /* pop %rbx/%r11 */
2077 case 0x5c: /* pop %rsp/%r12 */
2078 case 0x5d: /* pop %rbp/%r13 */
2079 case 0x5e: /* pop %rsi/%r14 */
2080 case 0x5f: /* pop %rdi/%r15 */
2083 case 0xc2: /* ret $nn */
2084 case 0xc3: /* ret */
2086 /* FIXME: add various jump instructions */
2092 /* execute a function epilog, which must have been validated with is_inside_epilog() */
2093 static void interpret_epilog( BYTE
*pc
, CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
2099 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
2103 case 0x58: /* pop %rax/r8 */
2104 case 0x59: /* pop %rcx/r9 */
2105 case 0x5a: /* pop %rdx/r10 */
2106 case 0x5b: /* pop %rbx/r11 */
2107 case 0x5c: /* pop %rsp/r12 */
2108 case 0x5d: /* pop %rbp/r13 */
2109 case 0x5e: /* pop %rsi/r14 */
2110 case 0x5f: /* pop %rdi/r15 */
2111 set_int_reg( context
, ctx_ptr
, *pc
- 0x58 + (rex
& 1) * 8, *(ULONG64
*)context
->Rsp
);
2112 context
->Rsp
+= sizeof(ULONG64
);
2115 case 0x81: /* add $nnnn,%rsp */
2116 context
->Rsp
+= *(LONG
*)(pc
+ 2);
2117 pc
+= 2 + sizeof(LONG
);
2119 case 0x83: /* add $n,%rsp */
2120 context
->Rsp
+= (signed char)pc
[2];
2124 if ((pc
[1] >> 6) == 1) /* lea n(reg),%rsp */
2126 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + (signed char)pc
[2];
2129 else /* lea nnnn(reg),%rsp */
2131 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + *(LONG
*)(pc
+ 2);
2132 pc
+= 2 + sizeof(LONG
);
2135 case 0xc2: /* ret $nn */
2136 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2137 context
->Rsp
+= sizeof(ULONG64
) + *(WORD
*)(pc
+ 1);
2139 case 0xc3: /* ret */
2140 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2141 context
->Rsp
+= sizeof(ULONG64
);
2143 /* FIXME: add various jump instructions */
2149 /**********************************************************************
2150 * RtlVirtualUnwind (NTDLL.@)
2152 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG64 base
, ULONG64 pc
,
2153 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
2154 PVOID
*data
, ULONG64
*frame_ret
,
2155 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
2157 union handler_data
*handler_data
;
2159 struct UNWIND_INFO
*info
;
2160 unsigned int i
, prolog_offset
;
2162 TRACE( "type %x rip %lx rsp %lx\n", type
, pc
, context
->Rsp
);
2163 if (TRACE_ON(seh
)) dump_unwind_info( base
, function
);
2165 frame
= *frame_ret
= context
->Rsp
;
2168 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
2169 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
2171 if (info
->version
!= 1)
2173 FIXME( "unknown unwind info version %u at %p\n", info
->version
, info
);
2177 if (info
->frame_reg
)
2178 frame
= get_int_reg( context
, info
->frame_reg
) - info
->frame_offset
* 16;
2180 /* check if in prolog */
2181 if (pc
>= base
+ function
->BeginAddress
&& pc
< base
+ function
->BeginAddress
+ info
->prolog
)
2183 prolog_offset
= pc
- base
- function
->BeginAddress
;
2188 if (is_inside_epilog( (BYTE
*)pc
))
2190 interpret_epilog( (BYTE
*)pc
, context
, ctx_ptr
);
2196 for (i
= 0; i
< info
->count
; i
+= get_opcode_size(info
->opcodes
[i
]))
2198 if (prolog_offset
< info
->opcodes
[i
].offset
) continue; /* skip it */
2200 switch (info
->opcodes
[i
].code
)
2202 case UWOP_PUSH_NONVOL
: /* pushq %reg */
2203 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)context
->Rsp
);
2204 context
->Rsp
+= sizeof(ULONG64
);
2206 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
2207 if (info
->opcodes
[i
].info
) context
->Rsp
+= *(DWORD
*)&info
->opcodes
[i
+1];
2208 else context
->Rsp
+= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
2210 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
2211 context
->Rsp
+= (info
->opcodes
[i
].info
+ 1) * 8;
2213 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
2214 context
->Rsp
= *frame_ret
= frame
;
2216 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
2217 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 8;
2218 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)off
);
2220 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
2221 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
2222 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(ULONG64
*)off
);
2224 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
2225 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 16;
2226 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(M128A
*)off
);
2228 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
2229 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
2230 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, *(M128A
*)off
);
2232 case UWOP_PUSH_MACHFRAME
:
2233 FIXME( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
2236 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
2241 if (!(info
->flags
& UNW_FLAG_CHAININFO
)) break;
2242 function
= &handler_data
->chain
; /* restart with the chained info */
2245 /* now pop return address */
2246 context
->Rip
= *(ULONG64
*)context
->Rsp
;
2247 context
->Rsp
+= sizeof(ULONG64
);
2249 if (!(info
->flags
& type
)) return NULL
; /* no matching handler */
2250 if (prolog_offset
!= ~0) return NULL
; /* inside prolog */
2252 *data
= &handler_data
->handler
+ 1;
2253 return (char *)base
+ handler_data
->handler
;
2257 /**********************************************************************
2258 * call_unwind_handler
2260 * Call a single unwind handler.
2261 * FIXME: Handle nested exceptions.
2263 static void call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
2267 dispatch
->ControlPc
= dispatch
->ContextRecord
->Rip
;
2269 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
2270 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2271 res
= dispatch
->LanguageHandler( rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
2272 TRACE( "handler %p returned %x\n", dispatch
->LanguageHandler
, res
);
2276 case ExceptionContinueSearch
:
2278 case ExceptionCollidedUnwind
:
2279 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2282 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
2288 /**********************************************************************
2289 * call_teb_unwind_handler
2291 * Call a single unwind handler from the TEB chain.
2292 * FIXME: Handle nested exceptions.
2294 static void call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
2295 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
2297 EXCEPTION_REGISTRATION_RECORD
*dispatcher
;
2300 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatcher=%p)\n",
2301 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
2302 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, &dispatcher
);
2303 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
2307 case ExceptionContinueSearch
:
2309 case ExceptionCollidedUnwind
:
2310 FIXME( "ExceptionCollidedUnwind not supported yet\n" );
2313 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
2319 /*******************************************************************
2320 * RtlUnwindEx (NTDLL.@)
2322 void WINAPI
RtlUnwindEx( ULONG64 end_frame
, ULONG64 target_ip
, EXCEPTION_RECORD
*rec
,
2323 ULONG64 retval
, CONTEXT
*orig_context
, UNWIND_HISTORY_TABLE
*table
)
2325 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
2326 EXCEPTION_RECORD record
;
2327 RUNTIME_FUNCTION
*dir
;
2328 DISPATCHER_CONTEXT dispatch
;
2329 CONTEXT context
, new_context
;
2334 /* build an exception record, if we do not have one */
2337 record
.ExceptionCode
= STATUS_UNWIND
;
2338 record
.ExceptionFlags
= 0;
2339 record
.ExceptionRecord
= NULL
;
2340 record
.ExceptionAddress
= (void *)orig_context
->Rip
;
2341 record
.NumberParameters
= 0;
2345 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
2347 TRACE( "code=%x flags=%x end_frame=%lx target_ip=%lx rip=%016lx\n",
2348 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, orig_context
->Rip
);
2349 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
2350 orig_context
->Rax
, orig_context
->Rbx
, orig_context
->Rcx
, orig_context
->Rdx
);
2351 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
2352 orig_context
->Rsi
, orig_context
->Rdi
, orig_context
->Rbp
, orig_context
->Rsp
);
2353 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
2354 orig_context
->R8
, orig_context
->R9
, orig_context
->R10
, orig_context
->R11
);
2355 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
2356 orig_context
->R12
, orig_context
->R13
, orig_context
->R14
, orig_context
->R15
);
2358 context
= *orig_context
;
2359 dispatch
.EstablisherFrame
= context
.Rsp
;
2360 dispatch
.TargetIp
= target_ip
;
2361 dispatch
.ContextRecord
= &context
;
2362 dispatch
.HistoryTable
= table
;
2364 while (dispatch
.EstablisherFrame
!= end_frame
)
2366 new_context
= context
;
2368 /* FIXME: should use the history table to make things faster */
2372 dispatch
.ImageBase
= 0;
2373 dispatch
.ScopeIndex
= 0; /* FIXME */
2375 if (!LdrFindEntryForAddress( (void *)context
.Rip
, &module
))
2377 if (!(dir
= RtlImageDirectoryEntryToData( module
->BaseAddress
, TRUE
,
2378 IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
)) &&
2379 !(module
->Flags
& LDR_WINE_INTERNAL
))
2381 ERR( "module %s doesn't contain exception data, can't unwind exception\n",
2382 debugstr_w(module
->BaseDllName
.Buffer
) );
2383 raise_status( STATUS_BAD_FUNCTION_TABLE
, rec
);
2385 dispatch
.ImageBase
= (ULONG64
)module
->BaseAddress
;
2390 struct dwarf_eh_bases bases
;
2391 const struct dwarf_fde
*fde
= _Unwind_Find_FDE( (void *)(context
.Rip
- 1), &bases
);
2394 ERR( "no exception data found in %s for function %lx\n",
2395 module
? debugstr_w(module
->BaseDllName
.Buffer
) : "system library", context
.Rip
);
2396 raise_status( STATUS_BAD_FUNCTION_TABLE
, rec
);
2398 dispatch
.FunctionEntry
= NULL
;
2399 status
= dwarf_virtual_unwind( context
.Rip
, &dispatch
.EstablisherFrame
, &new_context
, fde
,
2400 &bases
, &dispatch
.LanguageHandler
, &dispatch
.HandlerData
);
2401 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
2402 if (dispatch
.LanguageHandler
&& !module
)
2404 FIXME( "calling personality routine in system library not supported yet\n" );
2405 dispatch
.LanguageHandler
= NULL
;
2410 if (!(dispatch
.FunctionEntry
= find_function_info( context
.Rip
, module
->BaseAddress
,
2414 context
.Rip
= *(ULONG64
*)context
.Rsp
;
2415 context
.Rsp
+= sizeof(ULONG64
);
2418 dispatch
.LanguageHandler
= RtlVirtualUnwind( UNW_FLAG_UHANDLER
, dispatch
.ImageBase
,
2419 context
.Rip
, dispatch
.FunctionEntry
,
2420 &new_context
, &dispatch
.HandlerData
,
2421 &dispatch
.EstablisherFrame
, NULL
);
2424 if (!dispatch
.EstablisherFrame
) break;
2426 if ((dispatch
.EstablisherFrame
& 7) ||
2427 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
2428 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
2430 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
2431 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
2432 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
2436 if (dispatch
.LanguageHandler
)
2438 if (end_frame
&& (dispatch
.EstablisherFrame
> end_frame
))
2440 ERR( "invalid end frame %lx/%lx\n", dispatch
.EstablisherFrame
, end_frame
);
2441 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
2443 call_unwind_handler( rec
, &dispatch
);
2445 else /* hack: call builtin handlers registered in the tib list */
2447 while ((ULONG64
)teb_frame
< new_context
.Rsp
&& (ULONG64
)teb_frame
< end_frame
)
2449 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
2450 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
2451 call_teb_unwind_handler( rec
, &dispatch
, teb_frame
);
2452 teb_frame
= __wine_pop_frame( teb_frame
);
2454 if ((ULONG64
)teb_frame
== end_frame
&& end_frame
< new_context
.Rsp
) break;
2455 dispatch
.EstablisherFrame
= new_context
.Rsp
;
2458 context
= new_context
;
2460 context
.Rax
= retval
;
2461 context
.Rip
= target_ip
;
2462 TRACE( "returning to %lx stack %lx\n", context
.Rip
, context
.Rsp
);
2463 set_cpu_context( &context
);
2467 /*******************************************************************
2468 * RtlUnwind (NTDLL.@)
2470 void WINAPI
__regs_RtlUnwind( ULONG64 frame
, ULONG64 target_ip
, EXCEPTION_RECORD
*rec
,
2471 ULONG64 retval
, CONTEXT
*context
)
2473 RtlUnwindEx( frame
, target_ip
, rec
, retval
, context
, NULL
);
2475 DEFINE_REGS_ENTRYPOINT( RtlUnwind
, 4 )
2478 /*******************************************************************
2479 * __C_specific_handler (NTDLL.@)
2481 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
2484 struct _DISPATCHER_CONTEXT
*dispatch
)
2486 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
2489 TRACE( "%p %lx %p %p\n", rec
, frame
, context
, dispatch
);
2490 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
2492 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)) /* FIXME */
2493 return ExceptionContinueSearch
;
2495 for (i
= 0; i
< table
->Count
; i
++)
2497 if (context
->Rip
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
2498 context
->Rip
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
2500 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
2501 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
2503 EXCEPTION_POINTERS ptrs
;
2504 PC_LANGUAGE_EXCEPTION_HANDLER filter
;
2506 filter
= (PC_LANGUAGE_EXCEPTION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
2507 ptrs
.ExceptionRecord
= rec
;
2508 ptrs
.ContextRecord
= context
;
2509 TRACE( "calling filter %p ptrs %p frame %lx\n", filter
, &ptrs
, frame
);
2510 switch (filter( &ptrs
, frame
))
2512 case EXCEPTION_EXECUTE_HANDLER
:
2514 case EXCEPTION_CONTINUE_SEARCH
:
2516 case EXCEPTION_CONTINUE_EXECUTION
:
2517 return ExceptionContinueExecution
;
2520 TRACE( "unwinding to target %lx\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
2521 RtlUnwindEx( frame
, dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
2522 rec
, 0, context
, dispatch
->HistoryTable
);
2525 return ExceptionContinueSearch
;
2529 /*******************************************************************
2530 * NtRaiseException (NTDLL.@)
2532 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
2534 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
2535 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
2540 /***********************************************************************
2541 * RtlRaiseException (NTDLL.@)
2543 void WINAPI
__regs_RtlRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
2547 rec
->ExceptionAddress
= (void *)context
->Rip
;
2548 status
= raise_exception( rec
, context
, TRUE
);
2549 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
2551 DEFINE_REGS_ENTRYPOINT( RtlRaiseException
, 1 )
2554 /***********************************************************************
2555 * call_thread_entry_point
2557 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
2561 RtlExitUserThread( entry( arg
));
2563 __EXCEPT(unhandled_exception_filter
)
2565 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
2568 abort(); /* should not be reached */
2572 /**********************************************************************
2573 * __wine_enter_vm86 (NTDLL.@)
2575 void __wine_enter_vm86( CONTEXT
*context
)
2577 MESSAGE("vm86 mode not supported on this platform\n");
2580 /**********************************************************************
2581 * DbgBreakPoint (NTDLL.@)
2583 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret")
2585 /**********************************************************************
2586 * DbgUserBreakPoint (NTDLL.@)
2588 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret")
2590 #endif /* __x86_64__ */