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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
29 #define WIN32_NO_STATUS
32 #include "wine/exception.h"
33 #include "wine/list.h"
34 #include "ntdll_misc.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(unwind
);
38 WINE_DECLARE_DEBUG_CHANNEL(seh
);
40 typedef struct _SCOPE_TABLE
50 } SCOPE_TABLE
, *PSCOPE_TABLE
;
53 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
54 struct MSVCRT_JUMP_BUFFER
80 /***********************************************************************
81 * Definitions for Win32 unwind tables
86 RUNTIME_FUNCTION chain
;
104 BYTE frame_offset
: 4;
105 struct opcode opcodes
[1]; /* info->count entries */
106 /* followed by handler_data */
109 #define UWOP_PUSH_NONVOL 0
110 #define UWOP_ALLOC_LARGE 1
111 #define UWOP_ALLOC_SMALL 2
112 #define UWOP_SET_FPREG 3
113 #define UWOP_SAVE_NONVOL 4
114 #define UWOP_SAVE_NONVOL_FAR 5
115 #define UWOP_EPILOG 6
116 #define UWOP_SAVE_XMM128 8
117 #define UWOP_SAVE_XMM128_FAR 9
118 #define UWOP_PUSH_MACHFRAME 10
120 static void dump_unwind_info( ULONG64 base
, RUNTIME_FUNCTION
*function
)
122 static const char * const reg_names
[16] =
123 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
124 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
126 union handler_data
*handler_data
;
127 struct UNWIND_INFO
*info
;
128 unsigned int i
, count
;
130 TRACE( "**** func %x-%x\n", function
->BeginAddress
, function
->EndAddress
);
133 if (function
->UnwindData
& 1)
135 RUNTIME_FUNCTION
*next
= (RUNTIME_FUNCTION
*)((char *)base
+ (function
->UnwindData
& ~1));
136 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
137 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
,
138 (char *)base
+ next
->BeginAddress
, (char *)base
+ next
->EndAddress
);
142 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
144 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
145 info
, info
->flags
, info
->prolog
,
146 (char *)base
+ function
->BeginAddress
, (char *)base
+ function
->EndAddress
);
149 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
150 reg_names
[info
->frame_reg
], info
->frame_offset
* 16 );
152 for (i
= 0; i
< info
->count
; i
++)
154 TRACE( " 0x%x: ", info
->opcodes
[i
].offset
);
155 switch (info
->opcodes
[i
].code
)
157 case UWOP_PUSH_NONVOL
:
158 TRACE( "pushq %%%s\n", reg_names
[info
->opcodes
[i
].info
] );
160 case UWOP_ALLOC_LARGE
:
161 if (info
->opcodes
[i
].info
)
163 count
= *(DWORD
*)&info
->opcodes
[i
+1];
168 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
171 TRACE( "subq $0x%x,%%rsp\n", count
);
173 case UWOP_ALLOC_SMALL
:
174 count
= (info
->opcodes
[i
].info
+ 1) * 8;
175 TRACE( "subq $0x%x,%%rsp\n", count
);
178 TRACE( "leaq 0x%x(%%rsp),%s\n",
179 info
->frame_offset
* 16, reg_names
[info
->frame_reg
] );
181 case UWOP_SAVE_NONVOL
:
182 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
183 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
186 case UWOP_SAVE_NONVOL_FAR
:
187 count
= *(DWORD
*)&info
->opcodes
[i
+1];
188 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->opcodes
[i
].info
], count
);
191 case UWOP_SAVE_XMM128
:
192 count
= *(USHORT
*)&info
->opcodes
[i
+1] * 16;
193 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
196 case UWOP_SAVE_XMM128_FAR
:
197 count
= *(DWORD
*)&info
->opcodes
[i
+1];
198 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info
->opcodes
[i
].info
, count
);
201 case UWOP_PUSH_MACHFRAME
:
202 TRACE( "PUSH_MACHFRAME %u\n", info
->opcodes
[i
].info
);
205 if (info
->version
== 2)
208 if (info
->opcodes
[i
].info
)
209 offset
= info
->opcodes
[i
].offset
;
211 offset
= (info
->opcodes
[i
+1].info
<< 8) + info
->opcodes
[i
+1].offset
;
212 TRACE("epilog %p-%p\n", (char *)base
+ function
->EndAddress
- offset
,
213 (char *)base
+ function
->EndAddress
- offset
+ info
->opcodes
[i
].offset
);
218 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
223 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
224 if (info
->flags
& UNW_FLAG_CHAININFO
)
226 TRACE( " chained to function %p-%p\n",
227 (char *)base
+ handler_data
->chain
.BeginAddress
,
228 (char *)base
+ handler_data
->chain
.EndAddress
);
229 function
= &handler_data
->chain
;
232 if (info
->flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
233 TRACE( " handler %p data at %p\n",
234 (char *)base
+ handler_data
->handler
, &handler_data
->handler
+ 1 );
239 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
243 TRACE( "scope table at %p\n", table
);
244 for (i
= 0; i
< table
->Count
; i
++)
245 TRACE( " %u: %p-%p handler %p target %p\n", i
,
246 (char *)base
+ table
->ScopeRecord
[i
].BeginAddress
,
247 (char *)base
+ table
->ScopeRecord
[i
].EndAddress
,
248 (char *)base
+ table
->ScopeRecord
[i
].HandlerAddress
,
249 (char *)base
+ table
->ScopeRecord
[i
].JumpTarget
);
253 /***********************************************************************
256 static NTSTATUS
virtual_unwind( ULONG type
, DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*context
)
258 LDR_DATA_TABLE_ENTRY
*module
;
261 dispatch
->ImageBase
= 0;
262 dispatch
->ScopeIndex
= 0;
263 dispatch
->ControlPc
= context
->Rip
;
265 /* first look for PE exception information */
267 if ((dispatch
->FunctionEntry
= lookup_function_info( context
->Rip
, &dispatch
->ImageBase
, &module
)))
269 dispatch
->LanguageHandler
= RtlVirtualUnwind( type
, dispatch
->ImageBase
, context
->Rip
,
270 dispatch
->FunctionEntry
, context
,
271 &dispatch
->HandlerData
, &dispatch
->EstablisherFrame
,
273 return STATUS_SUCCESS
;
276 /* then look for host system exception information */
278 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
280 status
= unix_funcs
->unwind_builtin_dll( type
, dispatch
, context
);
282 if (!status
&& dispatch
->LanguageHandler
&& !module
)
284 FIXME( "calling personality routine in system library not supported yet\n" );
285 dispatch
->LanguageHandler
= NULL
;
287 if (status
!= STATUS_UNSUCCESSFUL
) return status
;
289 else WARN( "exception data not found in %s\n", debugstr_w(module
->BaseDllName
.Buffer
) );
291 /* no exception information, treat as a leaf function */
293 dispatch
->EstablisherFrame
= context
->Rsp
;
294 dispatch
->LanguageHandler
= NULL
;
295 context
->Rip
= *(ULONG64
*)context
->Rsp
;
296 context
->Rsp
= context
->Rsp
+ sizeof(ULONG64
);
297 return STATUS_SUCCESS
;
301 /**************************************************************************
304 * Supposed to touch all the stack pages, but we shouldn't need that.
306 __ASM_GLOBAL_FUNC( __chkstk
, "ret" );
309 /***********************************************************************
310 * RtlCaptureContext (NTDLL.@)
312 __ASM_GLOBAL_FUNC( RtlCaptureContext
,
314 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
315 "movl $0x10000f,0x30(%rcx)\n\t" /* context->ContextFlags */
316 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
317 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
318 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
319 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
320 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
321 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
322 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
323 "popq 0x44(%rcx)\n\t" /* context->Eflags */
324 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
325 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
326 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
327 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
328 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
329 "leaq 8(%rsp),%rax\n\t"
330 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
331 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
332 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
333 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
334 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
335 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
336 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
337 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
338 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
339 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
340 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
341 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
342 "movq (%rsp),%rax\n\t"
343 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
344 "fxsave 0x100(%rcx)\n\t" /* context->FltSave */
347 static DWORD __cdecl
nested_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
348 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
350 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
351 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
353 return ExceptionContinueSearch
;
356 /**********************************************************************
359 * Call a single exception handler.
360 * FIXME: Handle nested exceptions.
362 static DWORD
call_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
)
364 EXCEPTION_REGISTRATION_RECORD frame
;
367 frame
.Handler
= nested_exception_handler
;
368 __wine_push_frame( &frame
);
370 TRACE_(seh
)( "calling handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
371 dispatch
->LanguageHandler
, rec
, (void *)dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
372 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, context
, dispatch
);
373 TRACE_(seh
)( "handler at %p returned %u\n", dispatch
->LanguageHandler
, res
);
375 rec
->ExceptionFlags
&= EH_NONCONTINUABLE
;
376 __wine_pop_frame( &frame
);
381 /**********************************************************************
384 * Call a single exception handler from the TEB chain.
385 * FIXME: Handle nested exceptions.
387 static DWORD
call_teb_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
,
388 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
392 TRACE_(seh
)( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
393 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
394 res
= teb_frame
->Handler( rec
, teb_frame
, context
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
395 TRACE_(seh
)( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
400 /**********************************************************************
401 * call_stack_handlers
403 * Call the stack handlers chain.
405 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
407 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
408 UNWIND_HISTORY_TABLE table
;
409 DISPATCHER_CONTEXT dispatch
;
413 context
= *orig_context
;
414 context
.ContextFlags
&= ~0x40; /* Clear xstate flag. */
416 dispatch
.TargetIp
= 0;
417 dispatch
.ContextRecord
= &context
;
418 dispatch
.HistoryTable
= &table
;
421 status
= virtual_unwind( UNW_FLAG_EHANDLER
, &dispatch
, &context
);
422 if (status
!= STATUS_SUCCESS
) return status
;
425 if (!dispatch
.EstablisherFrame
) break;
427 if ((dispatch
.EstablisherFrame
& 7) ||
428 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
429 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
431 ERR_(seh
)( "invalid frame %p (%p-%p)\n", (void *)dispatch
.EstablisherFrame
,
432 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
433 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
437 if (dispatch
.LanguageHandler
)
439 switch (call_handler( rec
, orig_context
, &dispatch
))
441 case ExceptionContinueExecution
:
442 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
443 return STATUS_SUCCESS
;
444 case ExceptionContinueSearch
:
446 case ExceptionNestedException
:
447 FIXME_(seh
)( "nested exception\n" );
449 case ExceptionCollidedUnwind
: {
452 context
= *dispatch
.ContextRecord
;
453 dispatch
.ContextRecord
= &context
;
454 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
455 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
456 &context
, NULL
, &frame
, NULL
);
460 return STATUS_INVALID_DISPOSITION
;
463 /* hack: call wine handlers registered in the tib list */
464 else while ((ULONG64
)teb_frame
< context
.Rsp
)
466 TRACE_(seh
)( "found wine frame %p rsp %p handler %p\n",
467 teb_frame
, (void *)context
.Rsp
, teb_frame
->Handler
);
468 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
469 switch (call_teb_handler( rec
, orig_context
, &dispatch
, teb_frame
))
471 case ExceptionContinueExecution
:
472 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
473 return STATUS_SUCCESS
;
474 case ExceptionContinueSearch
:
476 case ExceptionNestedException
:
477 FIXME_(seh
)( "nested exception\n" );
479 case ExceptionCollidedUnwind
: {
482 context
= *dispatch
.ContextRecord
;
483 dispatch
.ContextRecord
= &context
;
484 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
485 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
486 &context
, NULL
, &frame
, NULL
);
487 teb_frame
= teb_frame
->Prev
;
491 return STATUS_INVALID_DISPOSITION
;
493 teb_frame
= teb_frame
->Prev
;
496 if (context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
498 return STATUS_UNHANDLED_EXCEPTION
;
502 NTSTATUS WINAPI
dispatch_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
507 TRACE_(seh
)( "code=%x flags=%x addr=%p ip=%p tid=%04x\n",
508 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
509 (void *)context
->Rip
, GetCurrentThreadId() );
510 for (c
= 0; c
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); c
++)
511 TRACE( " info[%d]=%016I64x\n", c
, rec
->ExceptionInformation
[c
] );
513 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
515 if (rec
->ExceptionInformation
[1] >> 16)
516 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
517 rec
->ExceptionAddress
,
518 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
520 MESSAGE( "wine: Call from %p to unimplemented function %s.%I64d, aborting\n",
521 rec
->ExceptionAddress
,
522 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
524 else if (rec
->ExceptionCode
== EXCEPTION_WINE_NAME_THREAD
&& rec
->ExceptionInformation
[0] == 0x1000)
526 WARN_(seh
)( "Thread %04x renamed to %s\n", (DWORD
)rec
->ExceptionInformation
[2],
527 debugstr_a((char *)rec
->ExceptionInformation
[1]) );
529 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_C
)
531 WARN_(seh
)( "%s\n", debugstr_an((char *)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
533 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_WIDE_C
)
535 WARN_(seh
)( "%s\n", debugstr_wn((WCHAR
*)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
539 if (rec
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
540 ERR_(seh
)( "%s exception (code=%x) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
542 WARN_(seh
)( "%s exception (code=%x) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
544 TRACE_(seh
)( " rax=%016I64x rbx=%016I64x rcx=%016I64x rdx=%016I64x\n",
545 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
546 TRACE_(seh
)( " rsi=%016I64x rdi=%016I64x rbp=%016I64x rsp=%016I64x\n",
547 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
548 TRACE_(seh
)( " r8=%016I64x r9=%016I64x r10=%016I64x r11=%016I64x\n",
549 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
550 TRACE_(seh
)( " r12=%016I64x r13=%016I64x r14=%016I64x r15=%016I64x\n",
551 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
554 /* Legends of Runeterra depends on having SegDs == SegSs in an exception
556 context
->SegDs
= context
->SegSs
;
558 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
559 NtContinue( context
, FALSE
);
561 if ((status
= call_stack_handlers( rec
, context
)) == STATUS_SUCCESS
)
562 NtContinue( context
, FALSE
);
564 if (status
!= STATUS_UNHANDLED_EXCEPTION
) RtlRaiseStatus( status
);
565 return NtRaiseException( rec
, context
, FALSE
);
569 /*******************************************************************
570 * KiUserExceptionDispatcher (NTDLL.@)
572 __ASM_GLOBAL_FUNC( KiUserExceptionDispatcher
,
573 "mov 0x98(%rsp),%rcx\n\t" /* context->Rsp */
574 "mov 0xf8(%rsp),%rdx\n\t" /* context->Rip */
575 "mov %rdx,-0x8(%rcx)\n\t"
576 "mov %rbp,-0x10(%rcx)\n\t"
577 "mov %rdi,-0x18(%rcx)\n\t"
578 "mov %rsi,-0x20(%rcx)\n\t"
579 "lea -0x20(%rcx),%rbp\n\t"
580 "mov %rsp,%rdx\n\t" /* context */
581 "lea 0x4f0(%rsp),%rcx\n\t" /* rec */
582 __ASM_SEH(".seh_pushreg %rbp\n\t")
583 __ASM_SEH(".seh_pushreg %rdi\n\t")
584 __ASM_SEH(".seh_pushreg %rsi\n\t")
585 __ASM_SEH(".seh_setframe %rbp,0\n\t")
586 __ASM_SEH(".seh_endprologue\n\t")
588 __ASM_CFI(".cfi_signal_frame\n\t")
589 __ASM_CFI(".cfi_adjust_cfa_offset 0x20\n\t")
590 __ASM_CFI(".cfi_def_cfa %rbp,0x20\n\t")
591 __ASM_CFI(".cfi_rel_offset %rip,0x18\n\t")
592 __ASM_CFI(".cfi_rel_offset %rbp,0x10\n\t")
593 __ASM_CFI(".cfi_rel_offset %rdi,0x8\n\t")
594 __ASM_CFI(".cfi_rel_offset %rsi,0\n\t")
595 "call " __ASM_NAME("dispatch_exception") "\n\t"
599 /*******************************************************************
600 * KiUserApcDispatcher (NTDLL.@)
602 void WINAPI
dispatch_apc( CONTEXT
*context
, ULONG_PTR arg1
, ULONG_PTR arg2
, ULONG_PTR arg3
,
603 void (CALLBACK
*func
)(ULONG_PTR
,ULONG_PTR
,ULONG_PTR
,CONTEXT
*) )
605 func( arg1
, arg2
, arg3
, context
);
606 NtContinue( context
, TRUE
);
609 __ASM_GLOBAL_FUNC( KiUserApcDispatcher
,
611 "mov 0x98(%rcx),%r10\n\t" /* context->Rsp */
612 "mov 0xf8(%rcx),%r11\n\t" /* context->Rip */
613 "mov %r11,-0x8(%r10)\n\t"
614 "mov %rbp,-0x10(%r10)\n\t"
615 "lea -0x10(%r10),%rbp\n\t"
616 __ASM_SEH(".seh_pushreg %rbp\n\t")
617 __ASM_SEH(".seh_setframe %rbp,0\n\t")
618 __ASM_SEH(".seh_endprologue\n\t")
619 __ASM_CFI(".cfi_signal_frame\n\t")
620 __ASM_CFI(".cfi_adjust_cfa_offset 0x10\n\t")
621 __ASM_CFI(".cfi_def_cfa %rbp,0x10\n\t")
622 __ASM_CFI(".cfi_rel_offset %rip,0x8\n\t")
623 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
624 "call " __ASM_NAME("dispatch_apc") "\n\t"
628 static ULONG64
get_int_reg( CONTEXT
*context
, int reg
)
630 return *(&context
->Rax
+ reg
);
633 static void set_int_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, ULONG64
*val
)
635 *(&context
->Rax
+ reg
) = *val
;
636 if (ctx_ptr
) ctx_ptr
->u2
.IntegerContext
[reg
] = val
;
639 static void set_float_reg( CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
, int reg
, M128A
*val
)
641 /* Use a memcpy() to avoid issues if val is misaligned. */
642 memcpy(&context
->u
.s
.Xmm0
+ reg
, val
, sizeof(*val
));
643 if (ctx_ptr
) ctx_ptr
->u
.FloatingContext
[reg
] = val
;
646 static int get_opcode_size( struct opcode op
)
650 case UWOP_ALLOC_LARGE
:
651 return 2 + (op
.info
!= 0);
652 case UWOP_SAVE_NONVOL
:
653 case UWOP_SAVE_XMM128
:
656 case UWOP_SAVE_NONVOL_FAR
:
657 case UWOP_SAVE_XMM128_FAR
:
664 static BOOL
is_inside_epilog( BYTE
*pc
, ULONG64 base
, const RUNTIME_FUNCTION
*function
)
666 /* add or lea must be the first instruction, and it must have a rex.W prefix */
667 if ((pc
[0] & 0xf8) == 0x48)
671 case 0x81: /* add $nnnn,%rsp */
672 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
678 case 0x83: /* add $n,%rsp */
679 if (pc
[0] == 0x48 && pc
[2] == 0xc4)
685 case 0x8d: /* lea n(reg),%rsp */
686 if (pc
[0] & 0x06) return FALSE
; /* rex.RX must be cleared */
687 if (((pc
[2] >> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
688 if ((pc
[2] & 7) == 4) return FALSE
; /* no SIB byte allowed */
689 if ((pc
[2] >> 6) == 1) /* 8-bit offset */
694 if ((pc
[2] >> 6) == 2) /* 32-bit offset */
703 /* now check for various pop instructions */
707 if ((*pc
& 0xf0) == 0x40) pc
++; /* rex prefix */
711 case 0x58: /* pop %rax/%r8 */
712 case 0x59: /* pop %rcx/%r9 */
713 case 0x5a: /* pop %rdx/%r10 */
714 case 0x5b: /* pop %rbx/%r11 */
715 case 0x5c: /* pop %rsp/%r12 */
716 case 0x5d: /* pop %rbp/%r13 */
717 case 0x5e: /* pop %rsi/%r14 */
718 case 0x5f: /* pop %rdi/%r15 */
721 case 0xc2: /* ret $nn */
724 case 0xe9: /* jmp nnnn */
725 pc
+= 5 + *(LONG
*)(pc
+ 1);
726 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
729 case 0xeb: /* jmp n */
730 pc
+= 2 + (signed char)pc
[1];
731 if (pc
- (BYTE
*)base
>= function
->BeginAddress
&& pc
- (BYTE
*)base
< function
->EndAddress
)
734 case 0xf3: /* rep; ret (for amd64 prediction bug) */
735 return pc
[1] == 0xc3;
741 /* execute a function epilog, which must have been validated with is_inside_epilog() */
742 static void interpret_epilog( BYTE
*pc
, CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
748 if ((*pc
& 0xf0) == 0x40) rex
= *pc
++ & 0x0f; /* rex prefix */
752 case 0x58: /* pop %rax/r8 */
753 case 0x59: /* pop %rcx/r9 */
754 case 0x5a: /* pop %rdx/r10 */
755 case 0x5b: /* pop %rbx/r11 */
756 case 0x5c: /* pop %rsp/r12 */
757 case 0x5d: /* pop %rbp/r13 */
758 case 0x5e: /* pop %rsi/r14 */
759 case 0x5f: /* pop %rdi/r15 */
760 set_int_reg( context
, ctx_ptr
, *pc
- 0x58 + (rex
& 1) * 8, (ULONG64
*)context
->Rsp
);
761 context
->Rsp
+= sizeof(ULONG64
);
764 case 0x81: /* add $nnnn,%rsp */
765 context
->Rsp
+= *(LONG
*)(pc
+ 2);
766 pc
+= 2 + sizeof(LONG
);
768 case 0x83: /* add $n,%rsp */
769 context
->Rsp
+= (signed char)pc
[2];
773 if ((pc
[1] >> 6) == 1) /* lea n(reg),%rsp */
775 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + (signed char)pc
[2];
778 else /* lea nnnn(reg),%rsp */
780 context
->Rsp
= get_int_reg( context
, (pc
[1] & 7) + (rex
& 1) * 8 ) + *(LONG
*)(pc
+ 2);
781 pc
+= 2 + sizeof(LONG
);
784 case 0xc2: /* ret $nn */
785 context
->Rip
= *(ULONG64
*)context
->Rsp
;
786 context
->Rsp
+= sizeof(ULONG64
) + *(WORD
*)(pc
+ 1);
789 case 0xf3: /* rep; ret */
790 context
->Rip
= *(ULONG64
*)context
->Rsp
;
791 context
->Rsp
+= sizeof(ULONG64
);
793 case 0xe9: /* jmp nnnn */
794 pc
+= 5 + *(LONG
*)(pc
+ 1);
796 case 0xeb: /* jmp n */
797 pc
+= 2 + (signed char)pc
[1];
804 /**********************************************************************
805 * RtlVirtualUnwind (NTDLL.@)
807 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG64 base
, ULONG64 pc
,
808 RUNTIME_FUNCTION
*function
, CONTEXT
*context
,
809 PVOID
*data
, ULONG64
*frame_ret
,
810 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
812 union handler_data
*handler_data
;
814 struct UNWIND_INFO
*info
;
815 unsigned int i
, prolog_offset
;
816 BOOL mach_frame
= FALSE
;
818 TRACE( "type %x rip %p rsp %p\n", type
, (void *)pc
, (void *)context
->Rsp
);
819 if (TRACE_ON(seh
)) dump_unwind_info( base
, function
);
821 frame
= *frame_ret
= context
->Rsp
;
824 info
= (struct UNWIND_INFO
*)((char *)base
+ function
->UnwindData
);
825 handler_data
= (union handler_data
*)&info
->opcodes
[(info
->count
+ 1) & ~1];
827 if (info
->version
!= 1 && info
->version
!= 2)
829 FIXME( "unknown unwind info version %u at %p\n", info
->version
, info
);
834 frame
= get_int_reg( context
, info
->frame_reg
) - info
->frame_offset
* 16;
836 /* check if in prolog */
837 if (pc
>= base
+ function
->BeginAddress
&& pc
< base
+ function
->BeginAddress
+ info
->prolog
)
839 TRACE("inside prolog.\n");
840 prolog_offset
= pc
- base
- function
->BeginAddress
;
845 /* Since Win10 1809 epilogue does not have a special treatment in case of zero opcode count. */
846 if (info
->count
&& is_inside_epilog( (BYTE
*)pc
, base
, function
))
848 TRACE("inside epilog.\n");
849 interpret_epilog( (BYTE
*)pc
, context
, ctx_ptr
);
855 for (i
= 0; i
< info
->count
; i
+= get_opcode_size(info
->opcodes
[i
]))
857 if (prolog_offset
< info
->opcodes
[i
].offset
) continue; /* skip it */
859 switch (info
->opcodes
[i
].code
)
861 case UWOP_PUSH_NONVOL
: /* pushq %reg */
862 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)context
->Rsp
);
863 context
->Rsp
+= sizeof(ULONG64
);
865 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
866 if (info
->opcodes
[i
].info
) context
->Rsp
+= *(DWORD
*)&info
->opcodes
[i
+1];
867 else context
->Rsp
+= *(USHORT
*)&info
->opcodes
[i
+1] * 8;
869 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
870 context
->Rsp
+= (info
->opcodes
[i
].info
+ 1) * 8;
872 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
873 context
->Rsp
= *frame_ret
= frame
;
875 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
876 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 8;
877 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
879 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
880 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
881 set_int_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (ULONG64
*)off
);
883 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
884 off
= frame
+ *(USHORT
*)&info
->opcodes
[i
+1] * 16;
885 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
887 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
888 off
= frame
+ *(DWORD
*)&info
->opcodes
[i
+1];
889 set_float_reg( context
, ctx_ptr
, info
->opcodes
[i
].info
, (M128A
*)off
);
891 case UWOP_PUSH_MACHFRAME
:
892 if (info
->flags
& UNW_FLAG_CHAININFO
)
894 FIXME("PUSH_MACHFRAME with chained unwind info.\n");
897 if (i
+ get_opcode_size(info
->opcodes
[i
]) < info
->count
)
899 FIXME("PUSH_MACHFRAME is not the last opcode.\n");
903 if (info
->opcodes
[i
].info
)
906 context
->Rip
= *(ULONG64
*)context
->Rsp
;
907 context
->Rsp
= *(ULONG64
*)(context
->Rsp
+ 24);
911 if (info
->version
== 2)
912 break; /* nothing to do */
914 FIXME( "unknown code %u\n", info
->opcodes
[i
].code
);
919 if (!(info
->flags
& UNW_FLAG_CHAININFO
)) break;
920 function
= &handler_data
->chain
; /* restart with the chained info */
925 /* now pop return address */
926 context
->Rip
= *(ULONG64
*)context
->Rsp
;
927 context
->Rsp
+= sizeof(ULONG64
);
930 if (!(info
->flags
& type
)) return NULL
; /* no matching handler */
931 if (prolog_offset
!= ~0) return NULL
; /* inside prolog */
933 *data
= &handler_data
->handler
+ 1;
934 return (char *)base
+ handler_data
->handler
;
937 struct unwind_exception_frame
939 EXCEPTION_REGISTRATION_RECORD frame
;
940 DISPATCHER_CONTEXT
*dispatch
;
943 /**********************************************************************
944 * unwind_exception_handler
946 * Handler for exceptions happening while calling an unwind handler.
948 static DWORD __cdecl
unwind_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
949 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
951 struct unwind_exception_frame
*unwind_frame
= (struct unwind_exception_frame
*)frame
;
952 DISPATCHER_CONTEXT
*dispatch
= (DISPATCHER_CONTEXT
*)dispatcher
;
954 /* copy the original dispatcher into the current one, except for the TargetIp */
955 dispatch
->ControlPc
= unwind_frame
->dispatch
->ControlPc
;
956 dispatch
->ImageBase
= unwind_frame
->dispatch
->ImageBase
;
957 dispatch
->FunctionEntry
= unwind_frame
->dispatch
->FunctionEntry
;
958 dispatch
->EstablisherFrame
= unwind_frame
->dispatch
->EstablisherFrame
;
959 dispatch
->ContextRecord
= unwind_frame
->dispatch
->ContextRecord
;
960 dispatch
->LanguageHandler
= unwind_frame
->dispatch
->LanguageHandler
;
961 dispatch
->HandlerData
= unwind_frame
->dispatch
->HandlerData
;
962 dispatch
->HistoryTable
= unwind_frame
->dispatch
->HistoryTable
;
963 dispatch
->ScopeIndex
= unwind_frame
->dispatch
->ScopeIndex
;
964 TRACE( "detected collided unwind\n" );
965 return ExceptionCollidedUnwind
;
968 /**********************************************************************
969 * call_unwind_handler
971 * Call a single unwind handler.
973 static DWORD
call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
975 struct unwind_exception_frame frame
;
978 frame
.frame
.Handler
= unwind_exception_handler
;
979 frame
.dispatch
= dispatch
;
980 __wine_push_frame( &frame
.frame
);
982 TRACE( "calling handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
983 dispatch
->LanguageHandler
, rec
, (void *)dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
984 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
985 TRACE( "handler %p returned %x\n", dispatch
->LanguageHandler
, res
);
987 __wine_pop_frame( &frame
.frame
);
991 case ExceptionContinueSearch
:
992 case ExceptionCollidedUnwind
:
995 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
1003 /**********************************************************************
1004 * call_teb_unwind_handler
1006 * Call a single unwind handler from the TEB chain.
1008 static DWORD
call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
1009 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
1013 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
1014 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
1015 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
1016 TRACE( "handler at %p returned %u\n", teb_frame
->Handler
, res
);
1020 case ExceptionContinueSearch
:
1021 case ExceptionCollidedUnwind
:
1024 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
1032 /**********************************************************************
1033 * call_consolidate_callback
1035 * Wrapper function to call a consolidate callback from a fake frame.
1036 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1037 * we have to skip all frames which were already processed. To do that we
1038 * trick the unwinding functions into thinking the call came from the specified
1039 * context. All CFI instructions are either DW_CFA_def_cfa_expression or
1040 * DW_CFA_expression, and the expressions have the following format:
1042 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
1043 * DW_OP_deref | Get *(%rbp + 0x10) == context
1044 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
1045 * [DW_OP_deref] | Dereference, only for CFA
1047 extern void * WINAPI
call_consolidate_callback( CONTEXT
*context
,
1048 void *(CALLBACK
*callback
)(EXCEPTION_RECORD
*),
1049 EXCEPTION_RECORD
*rec
);
1050 __ASM_GLOBAL_FUNC( call_consolidate_callback
,
1052 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1053 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1054 "movq %rsp,%rbp\n\t"
1055 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1057 /* Setup SEH machine frame. */
1058 "subq $0x28,%rsp\n\t"
1059 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
1060 "movq 0xf8(%rcx),%rax\n\t" /* Context->Rip */
1061 "movq %rax,(%rsp)\n\t"
1062 "movq 0x98(%rcx),%rax\n\t" /* context->Rsp */
1063 "movq %rax,0x18(%rsp)\n\t"
1064 __ASM_SEH(".seh_pushframe\n\t")
1065 __ASM_SEH(".seh_endprologue\n\t")
1067 "subq $0x108,%rsp\n\t" /* 10*16 (float regs) + 8*8 (int regs) + 32 (shadow store) + 8 (align). */
1068 __ASM_SEH(".seh_stackalloc 0x108\n\t")
1069 __ASM_CFI(".cfi_adjust_cfa_offset 0x108\n\t")
1071 /* Setup CFI unwind to context. */
1072 "movq %rcx,0x10(%rbp)\n\t"
1073 __ASM_CFI(".cfi_remember_state\n\t")
1074 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
1075 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
1076 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
1077 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
1078 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
1079 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
1080 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
1081 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
1082 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
1083 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
1084 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
1085 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
1086 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
1087 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
1088 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
1089 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
1090 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
1091 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
1092 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
1093 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
1095 /* Setup SEH unwind registers restore. */
1096 "movq 0xa0(%rcx),%rax\n\t" /* context->Rbp */
1097 "movq %rax,0x100(%rsp)\n\t"
1098 __ASM_SEH(".seh_savereg %rbp, 0x100\n\t")
1099 "movq 0x90(%rcx),%rax\n\t" /* context->Rbx */
1100 "movq %rax,0x20(%rsp)\n\t"
1101 __ASM_SEH(".seh_savereg %rbx, 0x20\n\t")
1102 "movq 0xa8(%rcx),%rax\n\t" /* context->Rsi */
1103 "movq %rax,0x28(%rsp)\n\t"
1104 __ASM_SEH(".seh_savereg %rsi, 0x28\n\t")
1105 "movq 0xb0(%rcx),%rax\n\t" /* context->Rdi */
1106 "movq %rax,0x30(%rsp)\n\t"
1107 __ASM_SEH(".seh_savereg %rdi, 0x30\n\t")
1109 "movq 0xd8(%rcx),%rax\n\t" /* context->R12 */
1110 "movq %rax,0x38(%rsp)\n\t"
1111 __ASM_SEH(".seh_savereg %r12, 0x38\n\t")
1112 "movq 0xe0(%rcx),%rax\n\t" /* context->R13 */
1113 "movq %rax,0x40(%rsp)\n\t"
1114 __ASM_SEH(".seh_savereg %r13, 0x40\n\t")
1115 "movq 0xe8(%rcx),%rax\n\t" /* context->R14 */
1116 "movq %rax,0x48(%rsp)\n\t"
1117 __ASM_SEH(".seh_savereg %r14, 0x48\n\t")
1118 "movq 0xf0(%rcx),%rax\n\t" /* context->R15 */
1119 "movq %rax,0x50(%rsp)\n\t"
1120 __ASM_SEH(".seh_savereg %r15, 0x50\n\t")
1123 "leaq 0x200(%rcx),%rsi\n\t"
1124 "leaq 0x70(%rsp),%rdi\n\t"
1125 "movq $0x14,%rcx\n\t"
1130 __ASM_SEH(".seh_savexmm %xmm6, 0x60\n\t")
1131 __ASM_SEH(".seh_savexmm %xmm7, 0x70\n\t")
1132 __ASM_SEH(".seh_savexmm %xmm8, 0x80\n\t")
1133 __ASM_SEH(".seh_savexmm %xmm9, 0x90\n\t")
1134 __ASM_SEH(".seh_savexmm %xmm10, 0xa0\n\t")
1135 __ASM_SEH(".seh_savexmm %xmm11, 0xb0\n\t")
1136 __ASM_SEH(".seh_savexmm %xmm12, 0xc0\n\t")
1137 __ASM_SEH(".seh_savexmm %xmm13, 0xd0\n\t")
1138 __ASM_SEH(".seh_savexmm %xmm14, 0xe0\n\t")
1139 __ASM_SEH(".seh_savexmm %xmm15, 0xf0\n\t")
1141 /* call the callback. */
1144 __ASM_CFI(".cfi_restore_state\n\t")
1145 "nop\n\t" /* Otherwise RtlVirtualUnwind() will think we are inside epilogue and
1146 * interpret / execute the rest of opcodes here instead of unwind through
1148 "leaq 0(%rbp),%rsp\n\t"
1149 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1151 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1152 __ASM_CFI(".cfi_same_value %rbp\n\t")
1155 /*******************************************************************
1156 * RtlRestoreContext (NTDLL.@)
1158 void CDECL
RtlRestoreContext( CONTEXT
*context
, EXCEPTION_RECORD
*rec
)
1160 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1162 if (rec
&& rec
->ExceptionCode
== STATUS_LONGJUMP
&& rec
->NumberParameters
>= 1)
1164 struct MSVCRT_JUMP_BUFFER
*jmp
= (struct MSVCRT_JUMP_BUFFER
*)rec
->ExceptionInformation
[0];
1165 context
->Rbx
= jmp
->Rbx
;
1166 context
->Rsp
= jmp
->Rsp
;
1167 context
->Rbp
= jmp
->Rbp
;
1168 context
->Rsi
= jmp
->Rsi
;
1169 context
->Rdi
= jmp
->Rdi
;
1170 context
->R12
= jmp
->R12
;
1171 context
->R13
= jmp
->R13
;
1172 context
->R14
= jmp
->R14
;
1173 context
->R15
= jmp
->R15
;
1174 context
->Rip
= jmp
->Rip
;
1175 context
->u
.s
.Xmm6
= jmp
->Xmm6
;
1176 context
->u
.s
.Xmm7
= jmp
->Xmm7
;
1177 context
->u
.s
.Xmm8
= jmp
->Xmm8
;
1178 context
->u
.s
.Xmm9
= jmp
->Xmm9
;
1179 context
->u
.s
.Xmm10
= jmp
->Xmm10
;
1180 context
->u
.s
.Xmm11
= jmp
->Xmm11
;
1181 context
->u
.s
.Xmm12
= jmp
->Xmm12
;
1182 context
->u
.s
.Xmm13
= jmp
->Xmm13
;
1183 context
->u
.s
.Xmm14
= jmp
->Xmm14
;
1184 context
->u
.s
.Xmm15
= jmp
->Xmm15
;
1186 else if (rec
&& rec
->ExceptionCode
== STATUS_UNWIND_CONSOLIDATE
&& rec
->NumberParameters
>= 1)
1188 PVOID (CALLBACK
*consolidate
)(EXCEPTION_RECORD
*) = (void *)rec
->ExceptionInformation
[0];
1189 TRACE_(seh
)( "calling consolidate callback %p (rec=%p)\n", consolidate
, rec
);
1190 context
->Rip
= (ULONG64
)call_consolidate_callback( context
, consolidate
, rec
);
1193 /* hack: remove no longer accessible TEB frames */
1194 while ((ULONG64
)teb_frame
< context
->Rsp
)
1196 TRACE_(seh
)( "removing TEB frame: %p\n", teb_frame
);
1197 teb_frame
= __wine_pop_frame( teb_frame
);
1200 TRACE_(seh
)( "returning to %p stack %p\n", (void *)context
->Rip
, (void *)context
->Rsp
);
1201 NtContinue( context
, FALSE
);
1205 /*******************************************************************
1206 * RtlUnwindEx (NTDLL.@)
1208 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
1209 PVOID retval
, CONTEXT
*context
, UNWIND_HISTORY_TABLE
*table
)
1211 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1212 EXCEPTION_RECORD record
;
1213 DISPATCHER_CONTEXT dispatch
;
1214 CONTEXT new_context
;
1218 RtlCaptureContext( context
);
1219 new_context
= *context
;
1221 /* build an exception record, if we do not have one */
1224 record
.ExceptionCode
= STATUS_UNWIND
;
1225 record
.ExceptionFlags
= 0;
1226 record
.ExceptionRecord
= NULL
;
1227 record
.ExceptionAddress
= (void *)context
->Rip
;
1228 record
.NumberParameters
= 0;
1232 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
1234 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016I64x\n",
1235 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, context
->Rip
);
1236 for (i
= 0; i
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); i
++)
1237 TRACE( " info[%d]=%016I64x\n", i
, rec
->ExceptionInformation
[i
] );
1238 TRACE(" rax=%016I64x rbx=%016I64x rcx=%016I64x rdx=%016I64x\n",
1239 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
1240 TRACE(" rsi=%016I64x rdi=%016I64x rbp=%016I64x rsp=%016I64x\n",
1241 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
1242 TRACE(" r8=%016I64x r9=%016I64x r10=%016I64x r11=%016I64x\n",
1243 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1244 TRACE(" r12=%016I64x r13=%016I64x r14=%016I64x r15=%016I64x\n",
1245 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
1247 dispatch
.EstablisherFrame
= context
->Rsp
;
1248 dispatch
.TargetIp
= (ULONG64
)target_ip
;
1249 dispatch
.ContextRecord
= context
;
1250 dispatch
.HistoryTable
= table
;
1254 status
= virtual_unwind( UNW_FLAG_UHANDLER
, &dispatch
, &new_context
);
1255 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
1258 if (!dispatch
.EstablisherFrame
) break;
1260 if ((dispatch
.EstablisherFrame
& 7) ||
1261 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
1262 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
1264 ERR( "invalid frame %p (%p-%p)\n", (void *)dispatch
.EstablisherFrame
,
1265 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1266 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
1270 if (dispatch
.LanguageHandler
)
1272 if (end_frame
&& (dispatch
.EstablisherFrame
> (ULONG64
)end_frame
))
1274 ERR( "invalid end frame %p/%p\n", (void *)dispatch
.EstablisherFrame
, end_frame
);
1275 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
1277 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) rec
->ExceptionFlags
|= EH_TARGET_UNWIND
;
1278 if (call_unwind_handler( rec
, &dispatch
) == ExceptionCollidedUnwind
)
1282 new_context
= *dispatch
.ContextRecord
;
1283 new_context
.ContextFlags
&= ~0x40;
1284 *context
= new_context
;
1285 dispatch
.ContextRecord
= context
;
1286 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
1287 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
1288 &new_context
, NULL
, &frame
, NULL
);
1289 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
1292 rec
->ExceptionFlags
&= ~EH_COLLIDED_UNWIND
;
1294 else /* hack: call builtin handlers registered in the tib list */
1296 DWORD64 backup_frame
= dispatch
.EstablisherFrame
;
1297 while ((ULONG64
)teb_frame
< new_context
.Rsp
&& (ULONG64
)teb_frame
< (ULONG64
)end_frame
)
1299 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
1300 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
1301 if (call_teb_unwind_handler( rec
, &dispatch
, teb_frame
) == ExceptionCollidedUnwind
)
1305 teb_frame
= __wine_pop_frame( teb_frame
);
1307 new_context
= *dispatch
.ContextRecord
;
1308 new_context
.ContextFlags
&= ~0x40;
1309 *context
= new_context
;
1310 dispatch
.ContextRecord
= context
;
1311 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
1312 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
1313 &new_context
, NULL
, &frame
, NULL
);
1314 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
1317 teb_frame
= __wine_pop_frame( teb_frame
);
1319 if ((ULONG64
)teb_frame
== (ULONG64
)end_frame
&& (ULONG64
)end_frame
< new_context
.Rsp
) break;
1320 dispatch
.EstablisherFrame
= backup_frame
;
1323 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) break;
1324 *context
= new_context
;
1327 context
->Rax
= (ULONG64
)retval
;
1328 context
->Rip
= (ULONG64
)target_ip
;
1329 RtlRestoreContext(context
, rec
);
1333 /*******************************************************************
1334 * RtlUnwind (NTDLL.@)
1336 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
1339 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
1343 /*******************************************************************
1344 * _local_unwind (NTDLL.@)
1346 void WINAPI
_local_unwind( void *frame
, void *target_ip
)
1349 RtlUnwindEx( frame
, target_ip
, NULL
, NULL
, &context
, NULL
);
1352 /*******************************************************************
1353 * __C_specific_handler (NTDLL.@)
1355 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
1358 struct _DISPATCHER_CONTEXT
*dispatch
)
1360 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
1363 TRACE_(seh
)( "%p %p %p %p\n", rec
, frame
, context
, dispatch
);
1364 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
1366 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
1368 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
1370 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1371 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1373 PTERMINATION_HANDLER handler
;
1375 if (table
->ScopeRecord
[i
].JumpTarget
) continue;
1377 if (rec
->ExceptionFlags
& EH_TARGET_UNWIND
&&
1378 dispatch
->TargetIp
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1379 dispatch
->TargetIp
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1384 handler
= (PTERMINATION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
1385 dispatch
->ScopeIndex
= i
+1;
1387 TRACE_(seh
)( "calling __finally %p frame %p\n", handler
, frame
);
1388 handler( TRUE
, frame
);
1391 return ExceptionContinueSearch
;
1394 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
1396 if (dispatch
->ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1397 dispatch
->ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1399 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
1400 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
1402 EXCEPTION_POINTERS ptrs
;
1403 PEXCEPTION_FILTER filter
;
1405 filter
= (PEXCEPTION_FILTER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
1406 ptrs
.ExceptionRecord
= rec
;
1407 ptrs
.ContextRecord
= context
;
1408 TRACE_(seh
)( "calling filter %p ptrs %p frame %p\n", filter
, &ptrs
, frame
);
1409 switch (filter( &ptrs
, frame
))
1411 case EXCEPTION_EXECUTE_HANDLER
:
1413 case EXCEPTION_CONTINUE_SEARCH
:
1415 case EXCEPTION_CONTINUE_EXECUTION
:
1416 return ExceptionContinueExecution
;
1419 TRACE( "unwinding to target %p\n", (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
1420 RtlUnwindEx( frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
1421 rec
, 0, dispatch
->ContextRecord
, dispatch
->HistoryTable
);
1424 return ExceptionContinueSearch
;
1428 /***********************************************************************
1429 * RtlRaiseException (NTDLL.@)
1431 __ASM_GLOBAL_FUNC( RtlRaiseException
,
1432 "sub $0x4f8,%rsp\n\t"
1433 __ASM_SEH(".seh_stackalloc 0x4f8\n\t")
1434 __ASM_SEH(".seh_endprologue\n\t")
1435 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
1436 "movq %rcx,0x500(%rsp)\n\t"
1437 "leaq 0x20(%rsp),%rcx\n\t"
1438 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
1439 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
1440 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
1441 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
1442 "movq (%rax),%rcx\n\t" /* original first parameter */
1443 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
1444 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
1445 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
1446 "movq %rax,0x10(%rcx)\n\t" /* rec->ExceptionAddress */
1448 "movq %gs:(0x30),%rax\n\t" /* Teb */
1449 "movq 0x60(%rax),%rax\n\t" /* Peb */
1450 "cmpb $0,0x02(%rax)\n\t" /* BeingDebugged */
1452 "call " __ASM_NAME("dispatch_exception") "\n"
1453 "1:\tcall " __ASM_NAME("NtRaiseException") "\n\t"
1454 "movq %rax,%rcx\n\t"
1455 "call " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1458 static inline ULONG
hash_pointers( void **ptrs
, ULONG count
)
1460 /* Based on MurmurHash2, which is in the public domain */
1461 static const ULONG m
= 0x5bd1e995;
1462 static const ULONG r
= 24;
1463 ULONG hash
= count
* sizeof(void*);
1464 for (; count
> 0; ptrs
++, count
--)
1466 ULONG_PTR data
= (ULONG_PTR
)*ptrs
;
1467 ULONG k1
= (ULONG
)(data
& 0xffffffff), k2
= (ULONG
)(data
>> 32);
1469 k1
= (k1
^ (k1
>> r
)) * m
;
1471 k2
= (k2
^ (k2
>> r
)) * m
;
1472 hash
= (((hash
* m
) ^ k1
) * m
) ^ k2
;
1474 hash
= (hash
^ (hash
>> 13)) * m
;
1475 return hash
^ (hash
>> 15);
1479 /*************************************************************************
1480 * RtlCaptureStackBackTrace (NTDLL.@)
1482 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1484 UNWIND_HISTORY_TABLE table
;
1485 DISPATCHER_CONTEXT dispatch
;
1489 USHORT num_entries
= 0;
1491 TRACE( "(%u, %u, %p, %p)\n", skip
, count
, buffer
, hash
);
1493 RtlCaptureContext( &context
);
1494 dispatch
.TargetIp
= 0;
1495 dispatch
.ContextRecord
= &context
;
1496 dispatch
.HistoryTable
= &table
;
1497 if (hash
) *hash
= 0;
1498 for (i
= 0; i
< skip
+ count
; i
++)
1500 status
= virtual_unwind( UNW_FLAG_NHANDLER
, &dispatch
, &context
);
1501 if (status
!= STATUS_SUCCESS
) return i
;
1503 if (!dispatch
.EstablisherFrame
) break;
1505 if ((dispatch
.EstablisherFrame
& 7) ||
1506 dispatch
.EstablisherFrame
< (ULONG64
)NtCurrentTeb()->Tib
.StackLimit
||
1507 dispatch
.EstablisherFrame
> (ULONG64
)NtCurrentTeb()->Tib
.StackBase
)
1509 ERR( "invalid frame %p (%p-%p)\n", (void *)dispatch
.EstablisherFrame
,
1510 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1514 if (context
.Rsp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
1516 if (i
>= skip
) buffer
[num_entries
++] = (void *)context
.Rip
;
1518 if (hash
&& num_entries
> 0) *hash
= hash_pointers( buffer
, num_entries
);
1519 TRACE( "captured %hu frames\n", num_entries
);
1524 /***********************************************************************
1525 * signal_start_thread
1527 __ASM_GLOBAL_FUNC( signal_start_thread
,
1528 "movq %rcx,%rbx\n\t" /* context */
1529 /* clear the thread stack */
1530 "andq $~0xfff,%rcx\n\t" /* round down to page size */
1531 "leaq -0xf0000(%rcx),%rdi\n\t"
1532 "movq %rdi,%rsp\n\t"
1533 "subq %rdi,%rcx\n\t"
1534 "xorl %eax,%eax\n\t"
1537 /* switch to the initial context */
1538 "leaq -32(%rbx),%rsp\n\t"
1539 "movq %rbx,%rcx\n\t"
1541 "call " __ASM_NAME("NtContinue") )
1544 /**********************************************************************
1545 * DbgBreakPoint (NTDLL.@)
1547 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "int $3; ret"
1548 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1549 "\n\tnop; nop; nop; nop; nop; nop" );
1551 /**********************************************************************
1552 * DbgUserBreakPoint (NTDLL.@)
1554 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "int $3; ret"
1555 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1556 "\n\tnop; nop; nop; nop; nop; nop" );
1558 #endif /* __x86_64__ */