2 * ARM64 signal handling routines
4 * Copyright 2010-2013 André Hentschel
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
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
32 #define WIN32_NO_STATUS
35 #include "wine/exception.h"
36 #include "ntdll_misc.h"
37 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
41 WINE_DECLARE_DEBUG_CHANNEL(threadname
);
43 TEB
* (* WINAPI __wine_current_teb
)(void) = NULL
;
45 typedef struct _SCOPE_TABLE
55 } SCOPE_TABLE
, *PSCOPE_TABLE
;
58 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
59 struct MSVCRT_JUMP_BUFFER
61 unsigned __int64 Frame
;
62 unsigned __int64 Reserved
;
82 static void dump_scope_table( ULONG64 base
, const SCOPE_TABLE
*table
)
86 TRACE( "scope table at %p\n", table
);
87 for (i
= 0; i
< table
->Count
; i
++)
88 TRACE( " %u: %I64x-%I64x handler %I64x target %I64x\n", i
,
89 base
+ table
->ScopeRecord
[i
].BeginAddress
,
90 base
+ table
->ScopeRecord
[i
].EndAddress
,
91 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
92 base
+ table
->ScopeRecord
[i
].JumpTarget
);
95 /*******************************************************************
98 static inline BOOL
is_valid_frame( ULONG_PTR frame
)
100 if (frame
& 7) return FALSE
;
101 return ((void *)frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
102 (void *)frame
<= NtCurrentTeb()->Tib
.StackBase
);
106 /**************************************************************************
109 * Supposed to touch all the stack pages, but we shouldn't need that.
111 __ASM_GLOBAL_FUNC( __chkstk
, "ret")
114 /***********************************************************************
115 * RtlCaptureContext (NTDLL.@)
117 __ASM_STDCALL_FUNC( RtlCaptureContext
, 8,
118 "str xzr, [x0, #0x8]\n\t" /* context->X0 */
119 "stp x1, x2, [x0, #0x10]\n\t" /* context->X1,X2 */
120 "stp x3, x4, [x0, #0x20]\n\t" /* context->X3,X4 */
121 "stp x5, x6, [x0, #0x30]\n\t" /* context->X5,X6 */
122 "stp x7, x8, [x0, #0x40]\n\t" /* context->X7,X8 */
123 "stp x9, x10, [x0, #0x50]\n\t" /* context->X9,X10 */
124 "stp x11, x12, [x0, #0x60]\n\t" /* context->X11,X12 */
125 "stp x13, x14, [x0, #0x70]\n\t" /* context->X13,X14 */
126 "stp x15, x16, [x0, #0x80]\n\t" /* context->X15,X16 */
127 "stp x17, x18, [x0, #0x90]\n\t" /* context->X17,X18 */
128 "stp x19, x20, [x0, #0xa0]\n\t" /* context->X19,X20 */
129 "stp x21, x22, [x0, #0xb0]\n\t" /* context->X21,X22 */
130 "stp x23, x24, [x0, #0xc0]\n\t" /* context->X23,X24 */
131 "stp x25, x26, [x0, #0xd0]\n\t" /* context->X25,X26 */
132 "stp x27, x28, [x0, #0xe0]\n\t" /* context->X27,X28 */
133 "stp x29, xzr, [x0, #0xf0]\n\t" /* context->Fp,Lr */
135 "stp x1, x30, [x0, #0x100]\n\t" /* context->Sp,Pc */
136 "stp q0, q1, [x0, #0x110]\n\t" /* context->V[0-1] */
137 "stp q2, q3, [x0, #0x130]\n\t" /* context->V[2-3] */
138 "stp q4, q5, [x0, #0x150]\n\t" /* context->V[4-5] */
139 "stp q6, q7, [x0, #0x170]\n\t" /* context->V[6-7] */
140 "stp q8, q9, [x0, #0x190]\n\t" /* context->V[8-9] */
141 "stp q10, q11, [x0, #0x1b0]\n\t" /* context->V[10-11] */
142 "stp q12, q13, [x0, #0x1d0]\n\t" /* context->V[12-13] */
143 "stp q14, q15, [x0, #0x1f0]\n\t" /* context->V[14-15] */
144 "stp q16, q17, [x0, #0x210]\n\t" /* context->V[16-17] */
145 "stp q18, q19, [x0, #0x230]\n\t" /* context->V[18-19] */
146 "stp q20, q21, [x0, #0x250]\n\t" /* context->V[20-21] */
147 "stp q22, q23, [x0, #0x270]\n\t" /* context->V[22-23] */
148 "stp q24, q25, [x0, #0x290]\n\t" /* context->V[24-25] */
149 "stp q26, q27, [x0, #0x2b0]\n\t" /* context->V[26-27] */
150 "stp q28, q29, [x0, #0x2d0]\n\t" /* context->V[28-29] */
151 "stp q30, q31, [x0, #0x2f0]\n\t" /* context->V[30-31] */
152 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
153 "movk w1, #0x7\n\t" /* CONTEXT_FULL */
154 "str w1, [x0]\n\t" /* context->ContextFlags */
156 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
158 "str w1, [x0, #0x310]\n\t" /* context->Fpcr */
160 "str w1, [x0, #0x314]\n\t" /* context->Fpsr */
164 /**********************************************************************
167 static NTSTATUS
virtual_unwind( ULONG type
, DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*context
)
169 LDR_DATA_TABLE_ENTRY
*module
;
173 dispatch
->ImageBase
= 0;
174 dispatch
->ScopeIndex
= 0;
175 dispatch
->EstablisherFrame
= 0;
176 dispatch
->ControlPc
= context
->Pc
;
178 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
181 dispatch
->ControlPcIsUnwound
= (context
->ContextFlags
& CONTEXT_UNWOUND_TO_CALL
) != 0;
182 pc
= context
->Pc
- (dispatch
->ControlPcIsUnwound
? 4 : 0);
184 /* first look for PE exception information */
186 if ((dispatch
->FunctionEntry
= lookup_function_info(pc
,
187 &dispatch
->ImageBase
, &module
)))
189 dispatch
->LanguageHandler
= RtlVirtualUnwind( type
, dispatch
->ImageBase
, pc
,
190 dispatch
->FunctionEntry
, context
,
191 &dispatch
->HandlerData
, &dispatch
->EstablisherFrame
,
193 return STATUS_SUCCESS
;
196 /* then look for host system exception information */
198 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
200 struct unwind_builtin_dll_params params
= { type
, dispatch
, context
};
202 status
= WINE_UNIX_CALL( unix_unwind_builtin_dll
, ¶ms
);
203 if (status
!= STATUS_SUCCESS
) return status
;
205 if (dispatch
->EstablisherFrame
)
207 dispatch
->FunctionEntry
= NULL
;
208 if (dispatch
->LanguageHandler
&& !module
)
210 FIXME( "calling personality routine in system library not supported yet\n" );
211 dispatch
->LanguageHandler
= NULL
;
213 return STATUS_SUCCESS
;
218 status
= context
->Pc
!= context
->u
.s
.Lr
?
219 STATUS_SUCCESS
: STATUS_INVALID_DISPOSITION
;
220 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
221 debugstr_w(module
->BaseDllName
.Buffer
), (void*) context
->Pc
,
222 (void*) context
->u
.s
.Lr
, status
);
223 dispatch
->EstablisherFrame
= context
->Sp
;
224 dispatch
->LanguageHandler
= NULL
;
225 context
->Pc
= context
->u
.s
.Lr
;
226 context
->ContextFlags
|= CONTEXT_UNWOUND_TO_CALL
;
230 dispatch
->EstablisherFrame
= context
->u
.s
.Fp
;
231 dispatch
->LanguageHandler
= NULL
;
232 context
->Pc
= context
->u
.s
.Lr
;
233 context
->ContextFlags
|= CONTEXT_UNWOUND_TO_CALL
;
234 return STATUS_SUCCESS
;
238 struct unwind_exception_frame
240 EXCEPTION_REGISTRATION_RECORD frame
;
241 DISPATCHER_CONTEXT
*dispatch
;
244 /**********************************************************************
245 * unwind_exception_handler
247 * Handler for exceptions happening while calling an unwind handler.
249 static DWORD __cdecl
unwind_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
250 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
252 struct unwind_exception_frame
*unwind_frame
= (struct unwind_exception_frame
*)frame
;
253 DISPATCHER_CONTEXT
*dispatch
= (DISPATCHER_CONTEXT
*)dispatcher
;
255 /* copy the original dispatcher into the current one, except for the TargetIp */
256 dispatch
->ControlPc
= unwind_frame
->dispatch
->ControlPc
;
257 dispatch
->ImageBase
= unwind_frame
->dispatch
->ImageBase
;
258 dispatch
->FunctionEntry
= unwind_frame
->dispatch
->FunctionEntry
;
259 dispatch
->EstablisherFrame
= unwind_frame
->dispatch
->EstablisherFrame
;
260 dispatch
->ContextRecord
= unwind_frame
->dispatch
->ContextRecord
;
261 dispatch
->LanguageHandler
= unwind_frame
->dispatch
->LanguageHandler
;
262 dispatch
->HandlerData
= unwind_frame
->dispatch
->HandlerData
;
263 dispatch
->HistoryTable
= unwind_frame
->dispatch
->HistoryTable
;
264 dispatch
->ScopeIndex
= unwind_frame
->dispatch
->ScopeIndex
;
265 TRACE( "detected collided unwind\n" );
266 return ExceptionCollidedUnwind
;
269 /**********************************************************************
270 * call_unwind_handler
272 * Call a single unwind handler.
274 static DWORD
call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
276 struct unwind_exception_frame frame
;
279 frame
.frame
.Handler
= unwind_exception_handler
;
280 frame
.dispatch
= dispatch
;
281 __wine_push_frame( &frame
.frame
);
283 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
284 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
285 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
286 TRACE( "handler %p returned %lx\n", dispatch
->LanguageHandler
, res
);
288 __wine_pop_frame( &frame
.frame
);
292 case ExceptionContinueSearch
:
293 case ExceptionCollidedUnwind
:
296 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
304 /**********************************************************************
305 * call_teb_unwind_handler
307 * Call a single unwind handler from the TEB chain.
309 static DWORD
call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
310 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
314 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
315 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
316 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
317 TRACE( "handler at %p returned %lu\n", teb_frame
->Handler
, res
);
321 case ExceptionContinueSearch
:
322 case ExceptionCollidedUnwind
:
325 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
333 static DWORD __cdecl
nested_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
334 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
336 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
337 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
339 return ExceptionContinueSearch
;
343 /**********************************************************************
346 * Call a single exception handler.
347 * FIXME: Handle nested exceptions.
349 static DWORD
call_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
)
351 EXCEPTION_REGISTRATION_RECORD frame
;
354 frame
.Handler
= nested_exception_handler
;
355 __wine_push_frame( &frame
);
357 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
358 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
359 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, context
, dispatch
);
360 TRACE( "handler at %p returned %lu\n", dispatch
->LanguageHandler
, res
);
362 rec
->ExceptionFlags
&= EH_NONCONTINUABLE
;
363 __wine_pop_frame( &frame
);
368 /**********************************************************************
371 * Call a single exception handler from the TEB chain.
372 * FIXME: Handle nested exceptions.
374 static DWORD
call_teb_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
,
375 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
379 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
380 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
381 res
= teb_frame
->Handler( rec
, teb_frame
, context
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
382 TRACE( "handler at %p returned %lu\n", teb_frame
->Handler
, res
);
387 /**********************************************************************
388 * call_function_handlers
390 * Call the per-function handlers.
392 static NTSTATUS
call_function_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
394 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
395 UNWIND_HISTORY_TABLE table
;
396 DISPATCHER_CONTEXT dispatch
;
397 CONTEXT context
, prev_context
;
400 context
= *orig_context
;
401 dispatch
.TargetPc
= 0;
402 dispatch
.ContextRecord
= &context
;
403 dispatch
.HistoryTable
= &table
;
404 prev_context
= context
;
405 dispatch
.NonVolatileRegisters
= (BYTE
*)&prev_context
.u
.s
.X19
;
409 status
= virtual_unwind( UNW_FLAG_EHANDLER
, &dispatch
, &context
);
410 if (status
!= STATUS_SUCCESS
) return status
;
413 if (!dispatch
.EstablisherFrame
) break;
415 if (!is_valid_frame( dispatch
.EstablisherFrame
))
417 ERR( "invalid frame %I64x (%p-%p)\n", dispatch
.EstablisherFrame
,
418 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
419 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
423 if (dispatch
.LanguageHandler
)
425 switch (call_handler( rec
, orig_context
, &dispatch
))
427 case ExceptionContinueExecution
:
428 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
429 return STATUS_SUCCESS
;
430 case ExceptionContinueSearch
:
432 case ExceptionNestedException
:
433 FIXME( "nested exception\n" );
435 case ExceptionCollidedUnwind
: {
438 context
= *dispatch
.ContextRecord
;
439 dispatch
.ContextRecord
= &context
;
440 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
441 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
442 &context
, &dispatch
.HandlerData
, &frame
, NULL
);
446 return STATUS_INVALID_DISPOSITION
;
449 /* hack: call wine handlers registered in the tib list */
450 else while ((ULONG64
)teb_frame
< context
.Sp
)
452 TRACE( "found wine frame %p rsp %I64x handler %p\n",
453 teb_frame
, context
.Sp
, teb_frame
->Handler
);
454 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
455 switch (call_teb_handler( rec
, orig_context
, &dispatch
, teb_frame
))
457 case ExceptionContinueExecution
:
458 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
459 return STATUS_SUCCESS
;
460 case ExceptionContinueSearch
:
462 case ExceptionNestedException
:
463 FIXME( "nested exception\n" );
465 case ExceptionCollidedUnwind
: {
468 context
= *dispatch
.ContextRecord
;
469 dispatch
.ContextRecord
= &context
;
470 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
471 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
472 &context
, &dispatch
.HandlerData
, &frame
, NULL
);
473 teb_frame
= teb_frame
->Prev
;
477 return STATUS_INVALID_DISPOSITION
;
479 teb_frame
= teb_frame
->Prev
;
482 if (context
.Sp
== (ULONG64
)NtCurrentTeb()->Tib
.StackBase
) break;
483 prev_context
= context
;
485 return STATUS_UNHANDLED_EXCEPTION
;
489 /*******************************************************************
490 * KiUserExceptionDispatcher (NTDLL.@)
492 NTSTATUS WINAPI
KiUserExceptionDispatcher( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
497 if (pWow64PrepareForException
) pWow64PrepareForException( rec
, context
);
499 TRACE( "code=%lx flags=%lx addr=%p pc=%016I64x\n",
500 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
, context
->Pc
);
501 for (c
= 0; c
< rec
->NumberParameters
; c
++)
502 TRACE( " info[%ld]=%016I64x\n", c
, rec
->ExceptionInformation
[c
] );
504 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
506 if (rec
->ExceptionInformation
[1] >> 16)
507 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
508 rec
->ExceptionAddress
,
509 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
511 MESSAGE( "wine: Call from %p to unimplemented function %s.%Id, aborting\n",
512 rec
->ExceptionAddress
,
513 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
515 else if (rec
->ExceptionCode
== EXCEPTION_WINE_NAME_THREAD
&& rec
->ExceptionInformation
[0] == 0x1000)
517 if ((DWORD
)rec
->ExceptionInformation
[2] == -1 || (DWORD
)rec
->ExceptionInformation
[2] == GetCurrentThreadId())
518 WARN_(threadname
)( "Thread renamed to %s\n", debugstr_a((char *)rec
->ExceptionInformation
[1]) );
520 WARN_(threadname
)( "Thread ID %04lx renamed to %s\n", (DWORD
)rec
->ExceptionInformation
[2],
521 debugstr_a((char *)rec
->ExceptionInformation
[1]) );
523 set_native_thread_name((DWORD
)rec
->ExceptionInformation
[2], (char *)rec
->ExceptionInformation
[1]);
525 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_C
)
527 WARN( "%s\n", debugstr_an((char *)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
529 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_WIDE_C
)
531 WARN( "%s\n", debugstr_wn((WCHAR
*)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
535 if (rec
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
536 ERR( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
538 WARN( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
540 TRACE(" x0=%016I64x x1=%016I64x x2=%016I64x x3=%016I64x\n",
541 context
->u
.s
.X0
, context
->u
.s
.X1
, context
->u
.s
.X2
, context
->u
.s
.X3
);
542 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
543 context
->u
.s
.X4
, context
->u
.s
.X5
, context
->u
.s
.X6
, context
->u
.s
.X7
);
544 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
545 context
->u
.s
.X8
, context
->u
.s
.X9
, context
->u
.s
.X10
, context
->u
.s
.X11
);
546 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
547 context
->u
.s
.X12
, context
->u
.s
.X13
, context
->u
.s
.X14
, context
->u
.s
.X15
);
548 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
549 context
->u
.s
.X16
, context
->u
.s
.X17
, context
->u
.s
.X18
, context
->u
.s
.X19
);
550 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
551 context
->u
.s
.X20
, context
->u
.s
.X21
, context
->u
.s
.X22
, context
->u
.s
.X23
);
552 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
553 context
->u
.s
.X24
, context
->u
.s
.X25
, context
->u
.s
.X26
, context
->u
.s
.X27
);
554 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
555 context
->u
.s
.X28
, context
->u
.s
.Fp
, context
->u
.s
.Lr
, context
->Sp
);
558 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
559 NtContinue( context
, FALSE
);
561 if ((status
= call_function_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 * KiUserApcDispatcher (NTDLL.@)
572 void WINAPI
KiUserApcDispatcher( CONTEXT
*context
, ULONG_PTR arg1
, ULONG_PTR arg2
, ULONG_PTR arg3
,
575 void (CALLBACK
*func
)(ULONG_PTR
,ULONG_PTR
,ULONG_PTR
,CONTEXT
*) = (void *)apc
;
576 func( arg1
, arg2
, arg3
, context
);
577 NtContinue( context
, TRUE
);
581 /*******************************************************************
582 * KiUserCallbackDispatcher (NTDLL.@)
584 void WINAPI
KiUserCallbackDispatcher( ULONG id
, void *args
, ULONG len
)
590 NTSTATUS (WINAPI
*func
)(void *, ULONG
) = ((void **)NtCurrentTeb()->Peb
->KernelCallbackTable
)[id
];
591 status
= NtCallbackReturn( NULL
, 0, func( args
, len
));
595 ERR_(seh
)( "ignoring exception\n" );
596 status
= NtCallbackReturn( 0, 0, 0 );
600 RtlRaiseStatus( status
);
604 /***********************************************************************
605 * Definitions for Win32 unwind tables
610 DWORD function_length
: 18;
618 struct unwind_info_ext
625 struct unwind_info_epilog
632 static const BYTE unwind_code_len
[256] =
634 /* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
635 /* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
636 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
637 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
638 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
639 /* a0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
640 /* c0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
641 /* e0 */ 4,1,2,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
644 /***********************************************************************
647 static unsigned int get_sequence_len( BYTE
*ptr
, BYTE
*end
)
649 unsigned int ret
= 0;
653 if (*ptr
== 0xe4 || *ptr
== 0xe5) break;
654 ptr
+= unwind_code_len
[*ptr
];
661 /***********************************************************************
664 static void restore_regs( int reg
, int count
, int pos
, CONTEXT
*context
,
665 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
667 int i
, offset
= max( 0, pos
);
668 for (i
= 0; i
< count
; i
++)
670 if (ptrs
&& reg
+ i
>= 19) (&ptrs
->X19
)[reg
+ i
- 19] = (DWORD64
*)context
->Sp
+ i
+ offset
;
671 context
->u
.X
[reg
+ i
] = ((DWORD64
*)context
->Sp
)[i
+ offset
];
673 if (pos
< 0) context
->Sp
+= -8 * pos
;
677 /***********************************************************************
680 static void restore_fpregs( int reg
, int count
, int pos
, CONTEXT
*context
,
681 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
683 int i
, offset
= max( 0, pos
);
684 for (i
= 0; i
< count
; i
++)
686 if (ptrs
&& reg
+ i
>= 8) (&ptrs
->D8
)[reg
+ i
- 8] = (DWORD64
*)context
->Sp
+ i
+ offset
;
687 context
->V
[reg
+ i
].D
[0] = ((double *)context
->Sp
)[i
+ offset
];
689 if (pos
< 0) context
->Sp
+= -8 * pos
;
693 static void do_pac_auth( CONTEXT
*context
)
695 register DWORD64 x17
__asm__( "x17" ) = context
->u
.s
.Lr
;
696 register DWORD64 x16
__asm__( "x16" ) = context
->Sp
;
698 /* This is the autib1716 instruction. The hint instruction is used here
699 * as gcc does not assemble autib1716 for pre armv8.3a targets. For
700 * pre-armv8.3a targets, this is just treated as a hint instruction, which
702 __asm__( "hint 0xe" : "+r"(x17
) : "r"(x16
) );
704 context
->u
.s
.Lr
= x17
;
707 /***********************************************************************
708 * process_unwind_codes
710 static void process_unwind_codes( BYTE
*ptr
, BYTE
*end
, CONTEXT
*context
,
711 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
, int skip
)
713 unsigned int val
, len
, save_next
= 2;
716 while (ptr
< end
&& skip
)
718 if (*ptr
== 0xe4 || *ptr
== 0xe5) break;
719 ptr
+= unwind_code_len
[*ptr
];
725 if ((len
= unwind_code_len
[*ptr
]) > 1)
727 if (ptr
+ len
> end
) break;
728 val
= ptr
[0] * 0x100 + ptr
[1];
732 if (*ptr
< 0x20) /* alloc_s */
733 context
->Sp
+= 16 * (val
& 0x1f);
734 else if (*ptr
< 0x40) /* save_r19r20_x */
735 restore_regs( 19, save_next
, -(val
& 0x1f), context
, ptrs
);
736 else if (*ptr
< 0x80) /* save_fplr */
737 restore_regs( 29, 2, val
& 0x3f, context
, ptrs
);
738 else if (*ptr
< 0xc0) /* save_fplr_x */
739 restore_regs( 29, 2, -(val
& 0x3f) - 1, context
, ptrs
);
740 else if (*ptr
< 0xc8) /* alloc_m */
741 context
->Sp
+= 16 * (val
& 0x7ff);
742 else if (*ptr
< 0xcc) /* save_regp */
743 restore_regs( 19 + ((val
>> 6) & 0xf), save_next
, val
& 0x3f, context
, ptrs
);
744 else if (*ptr
< 0xd0) /* save_regp_x */
745 restore_regs( 19 + ((val
>> 6) & 0xf), save_next
, -(val
& 0x3f) - 1, context
, ptrs
);
746 else if (*ptr
< 0xd4) /* save_reg */
747 restore_regs( 19 + ((val
>> 6) & 0xf), 1, val
& 0x3f, context
, ptrs
);
748 else if (*ptr
< 0xd6) /* save_reg_x */
749 restore_regs( 19 + ((val
>> 5) & 0xf), 1, -(val
& 0x1f) - 1, context
, ptrs
);
750 else if (*ptr
< 0xd8) /* save_lrpair */
752 restore_regs( 19 + 2 * ((val
>> 6) & 0x7), 1, val
& 0x3f, context
, ptrs
);
753 restore_regs( 30, 1, (val
& 0x3f) + 1, context
, ptrs
);
755 else if (*ptr
< 0xda) /* save_fregp */
756 restore_fpregs( 8 + ((val
>> 6) & 0x7), save_next
, val
& 0x3f, context
, ptrs
);
757 else if (*ptr
< 0xdc) /* save_fregp_x */
758 restore_fpregs( 8 + ((val
>> 6) & 0x7), save_next
, -(val
& 0x3f) - 1, context
, ptrs
);
759 else if (*ptr
< 0xde) /* save_freg */
760 restore_fpregs( 8 + ((val
>> 6) & 0x7), 1, val
& 0x3f, context
, ptrs
);
761 else if (*ptr
== 0xde) /* save_freg_x */
762 restore_fpregs( 8 + ((val
>> 5) & 0x7), 1, -(val
& 0x3f) - 1, context
, ptrs
);
763 else if (*ptr
== 0xe0) /* alloc_l */
764 context
->Sp
+= 16 * ((ptr
[1] << 16) + (ptr
[2] << 8) + ptr
[3]);
765 else if (*ptr
== 0xe1) /* set_fp */
766 context
->Sp
= context
->u
.s
.Fp
;
767 else if (*ptr
== 0xe2) /* add_fp */
768 context
->Sp
= context
->u
.s
.Fp
- 8 * (val
& 0xff);
769 else if (*ptr
== 0xe3) /* nop */
771 else if (*ptr
== 0xe4) /* end */
773 else if (*ptr
== 0xe5) /* end_c */
775 else if (*ptr
== 0xe6) /* save_next */
781 else if (*ptr
== 0xe9) /* MSFT_OP_MACHINE_FRAME */
783 context
->Pc
= ((DWORD64
*)context
->Sp
)[1];
784 context
->Sp
= ((DWORD64
*)context
->Sp
)[0];
786 else if (*ptr
== 0xea) /* MSFT_OP_CONTEXT */
788 memcpy( context
, (DWORD64
*)context
->Sp
, sizeof(CONTEXT
) );
790 else if (*ptr
== 0xfc) /* pac_sign_lr */
792 do_pac_auth( context
);
796 WARN( "unsupported code %02x\n", *ptr
);
805 /***********************************************************************
808 static void *unwind_packed_data( ULONG_PTR base
, ULONG_PTR pc
, RUNTIME_FUNCTION
*func
,
809 CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
812 unsigned int len
, offset
, skip
= 0;
813 unsigned int int_size
= func
->u
.s
.RegI
* 8, fp_size
= func
->u
.s
.RegF
* 8, regsave
, local_size
;
814 unsigned int int_regs
, fp_regs
, saved_regs
, local_size_regs
;
816 TRACE( "function %I64x-%I64x: len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
817 base
+ func
->BeginAddress
, base
+ func
->BeginAddress
+ func
->u
.s
.FunctionLength
* 4,
818 func
->u
.s
.FunctionLength
, func
->u
.s
.Flag
, func
->u
.s
.RegF
, func
->u
.s
.RegI
,
819 func
->u
.s
.H
, func
->u
.s
.CR
, func
->u
.s
.FrameSize
);
821 if (func
->u
.s
.CR
== 1) int_size
+= 8;
822 if (func
->u
.s
.RegF
) fp_size
+= 8;
824 regsave
= ((int_size
+ fp_size
+ 8 * 8 * func
->u
.s
.H
) + 0xf) & ~0xf;
825 local_size
= func
->u
.s
.FrameSize
* 16 - regsave
;
827 int_regs
= int_size
/ 8;
828 fp_regs
= fp_size
/ 8;
829 saved_regs
= regsave
/ 8;
830 local_size_regs
= local_size
/ 8;
832 /* check for prolog/epilog */
833 if (func
->u
.s
.Flag
== 1)
835 offset
= ((pc
- base
) - func
->BeginAddress
) / 4;
836 if (offset
< 17 || offset
>= func
->u
.s
.FunctionLength
- 15)
838 len
= (int_size
+ 8) / 16 + (fp_size
+ 8) / 16;
839 switch (func
->u
.s
.CR
)
845 len
++; /* mov x29,sp */
846 len
++; /* stp x29,lr,[sp,0] */
847 if (local_size
<= 512) break;
851 if (local_size
) len
++; /* sub sp,sp,#local_size */
852 if (local_size
> 4088) len
++; /* sub sp,sp,#4088 */
855 len
+= 4 * func
->u
.s
.H
;
856 if (offset
< len
) /* prolog */
860 else if (offset
>= func
->u
.s
.FunctionLength
- (len
+ 1)) /* epilog */
862 skip
= offset
- (func
->u
.s
.FunctionLength
- (len
+ 1));
869 if (func
->u
.s
.CR
== 3 || func
->u
.s
.CR
== 2)
871 DWORD64
*fp
= (DWORD64
*) context
->u
.s
.Fp
; /* u.X[29] */
872 context
->Sp
= context
->u
.s
.Fp
;
873 context
->u
.X
[29] = fp
[0];
874 context
->u
.X
[30] = fp
[1];
876 context
->Sp
+= local_size
;
877 if (fp_size
) restore_fpregs( 8, fp_regs
, int_regs
, context
, ptrs
);
878 if (func
->u
.s
.CR
== 1) restore_regs( 30, 1, int_regs
- 1, context
, ptrs
);
879 restore_regs( 19, func
->u
.s
.RegI
, -saved_regs
, context
, ptrs
);
883 unsigned int pos
= 0;
885 switch (func
->u
.s
.CR
)
890 if (pos
++ >= skip
) context
->Sp
= context
->u
.s
.Fp
;
891 if (local_size
<= 512)
893 /* stp x29,lr,[sp,-#local_size]! */
894 if (pos
++ >= skip
) restore_regs( 29, 2, -local_size_regs
, context
, ptrs
);
897 /* stp x29,lr,[sp,0] */
898 if (pos
++ >= skip
) restore_regs( 29, 2, 0, context
, ptrs
);
902 if (!local_size
) break;
903 /* sub sp,sp,#local_size */
904 if (pos
++ >= skip
) context
->Sp
+= (local_size
- 1) % 4088 + 1;
905 if (local_size
> 4088 && pos
++ >= skip
) context
->Sp
+= 4088;
909 if (func
->u
.s
.H
) pos
+= 4;
913 if (func
->u
.s
.RegF
% 2 == 0 && pos
++ >= skip
)
914 /* str d%u,[sp,#fp_size] */
915 restore_fpregs( 8 + func
->u
.s
.RegF
, 1, int_regs
+ fp_regs
- 1, context
, ptrs
);
916 for (i
= (func
->u
.s
.RegF
+ 1) / 2 - 1; i
>= 0; i
--)
918 if (pos
++ < skip
) continue;
920 /* stp d8,d9,[sp,-#regsave]! */
921 restore_fpregs( 8, 2, -saved_regs
, context
, ptrs
);
923 /* stp dn,dn+1,[sp,#offset] */
924 restore_fpregs( 8 + 2 * i
, 2, int_regs
+ 2 * i
, context
, ptrs
);
928 if (func
->u
.s
.RegI
% 2)
932 /* stp xn,lr,[sp,#offset] */
933 if (func
->u
.s
.CR
== 1) restore_regs( 30, 1, int_regs
- 1, context
, ptrs
);
934 /* str xn,[sp,#offset] */
935 restore_regs( 18 + func
->u
.s
.RegI
, 1,
936 (func
->u
.s
.RegI
> 1) ? func
->u
.s
.RegI
- 1 : -saved_regs
,
940 else if (func
->u
.s
.CR
== 1)
942 /* str lr,[sp,#offset] */
943 if (pos
++ >= skip
) restore_regs( 30, 1, func
->u
.s
.RegI
? int_regs
- 1 : -saved_regs
, context
, ptrs
);
946 for (i
= func
->u
.s
.RegI
/ 2 - 1; i
>= 0; i
--)
948 if (pos
++ < skip
) continue;
950 /* stp xn,xn+1,[sp,#offset] */
951 restore_regs( 19 + 2 * i
, 2, 2 * i
, context
, ptrs
);
953 /* stp x19,x20,[sp,-#regsave]! */
954 restore_regs( 19, 2, -saved_regs
, context
, ptrs
);
957 if (func
->u
.s
.CR
== 2) do_pac_auth( context
);
962 /***********************************************************************
965 static void *unwind_full_data( ULONG_PTR base
, ULONG_PTR pc
, RUNTIME_FUNCTION
*func
,
966 CONTEXT
*context
, PVOID
*handler_data
, KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
968 struct unwind_info
*info
;
969 struct unwind_info_epilog
*info_epilog
;
970 unsigned int i
, codes
, epilogs
, len
, offset
;
974 info
= (struct unwind_info
*)((char *)base
+ func
->u
.UnwindData
);
976 epilogs
= info
->epilog
;
978 if (!codes
&& !epilogs
)
980 struct unwind_info_ext
*infoex
= data
;
981 codes
= infoex
->codes
;
982 epilogs
= infoex
->epilog
;
986 if (!info
->e
) data
= info_epilog
+ epilogs
;
988 offset
= ((pc
- base
) - func
->BeginAddress
) / 4;
989 end
= (BYTE
*)data
+ codes
* 4;
991 TRACE( "function %I64x-%I64x: len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
992 base
+ func
->BeginAddress
, base
+ func
->BeginAddress
+ info
->function_length
* 4,
993 info
->function_length
, info
->version
, info
->x
, info
->e
, epilogs
, codes
* 4 );
995 /* check for prolog */
996 if (offset
< codes
* 4)
998 len
= get_sequence_len( data
, end
);
1001 process_unwind_codes( data
, end
, context
, ptrs
, len
- offset
);
1006 /* check for epilog */
1009 for (i
= 0; i
< epilogs
; i
++)
1011 if (offset
< info_epilog
[i
].offset
) break;
1012 if (offset
- info_epilog
[i
].offset
< codes
* 4 - info_epilog
[i
].index
)
1014 BYTE
*ptr
= (BYTE
*)data
+ info_epilog
[i
].index
;
1015 len
= get_sequence_len( ptr
, end
);
1016 if (offset
<= info_epilog
[i
].offset
+ len
)
1018 process_unwind_codes( ptr
, end
, context
, ptrs
, offset
- info_epilog
[i
].offset
);
1024 else if (info
->function_length
- offset
<= codes
* 4 - epilogs
)
1026 BYTE
*ptr
= (BYTE
*)data
+ epilogs
;
1027 len
= get_sequence_len( ptr
, end
) + 1;
1028 if (offset
>= info
->function_length
- len
)
1030 process_unwind_codes( ptr
, end
, context
, ptrs
, offset
- (info
->function_length
- len
) );
1035 process_unwind_codes( data
, end
, context
, ptrs
, 0 );
1037 /* get handler since we are inside the main code */
1040 DWORD
*handler_rva
= (DWORD
*)data
+ codes
;
1041 *handler_data
= handler_rva
+ 1;
1042 return (char *)base
+ *handler_rva
;
1048 /**********************************************************************
1049 * RtlVirtualUnwind (NTDLL.@)
1051 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG_PTR base
, ULONG_PTR pc
,
1052 RUNTIME_FUNCTION
*func
, CONTEXT
*context
,
1053 PVOID
*handler_data
, ULONG_PTR
*frame_ret
,
1054 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
1058 TRACE( "type %lx pc %I64x sp %I64x func %I64x\n", type
, pc
, context
->Sp
, base
+ func
->BeginAddress
);
1060 *handler_data
= NULL
;
1064 handler
= unwind_packed_data( base
, pc
, func
, context
, ctx_ptr
);
1066 handler
= unwind_full_data( base
, pc
, func
, context
, handler_data
, ctx_ptr
);
1068 TRACE( "ret: lr=%I64x sp=%I64x handler=%p\n", context
->u
.s
.Lr
, context
->Sp
, handler
);
1070 context
->Pc
= context
->u
.s
.Lr
;
1071 context
->ContextFlags
|= CONTEXT_UNWOUND_TO_CALL
;
1072 *frame_ret
= context
->Sp
;
1076 /**********************************************************************
1077 * call_consolidate_callback
1079 * Wrapper function to call a consolidate callback from a fake frame.
1080 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1081 * we have to skip all frames which were already processed. To do that we
1082 * trick the unwinding functions into thinking the call came from somewhere
1083 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1084 * DW_CFA_expression, and the expressions have the following format:
1086 * DW_OP_breg31; sleb128 <OFFSET> | Load x31 + struct member offset
1087 * [DW_OP_deref] | Dereference, only for CFA
1089 extern void * WINAPI
call_consolidate_callback( CONTEXT
*context
,
1090 void *(CALLBACK
*callback
)(EXCEPTION_RECORD
*),
1091 EXCEPTION_RECORD
*rec
,
1093 __ASM_GLOBAL_FUNC( call_consolidate_callback
,
1094 "stp x29, x30, [sp, #-0x30]!\n\t"
1095 __ASM_CFI(".cfi_def_cfa_offset 48\n\t")
1096 __ASM_CFI(".cfi_offset 29, -48\n\t")
1097 __ASM_CFI(".cfi_offset 30, -40\n\t")
1098 __ASM_SEH(".seh_nop\n\t")
1099 "stp x1, x2, [sp, #0x10]\n\t"
1100 __ASM_SEH(".seh_nop\n\t")
1101 "str x3, [sp, #0x20]\n\t"
1102 __ASM_SEH(".seh_nop\n\t")
1104 __ASM_CFI(".cfi_def_cfa_register 29\n\t")
1105 __ASM_SEH(".seh_nop\n\t")
1106 __ASM_CFI(".cfi_remember_state\n\t")
1107 /* Memcpy the context onto the stack */
1108 "sub sp, sp, #0x390\n\t"
1109 __ASM_SEH(".seh_nop\n\t")
1111 __ASM_SEH(".seh_nop\n\t")
1113 __ASM_SEH(".seh_nop\n\t")
1114 "mov x2, #0x390\n\t"
1115 __ASM_SEH(".seh_nop\n\t")
1116 "bl " __ASM_NAME("memcpy") "\n\t"
1117 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1118 __ASM_CFI(".cfi_escape 0x0f,0x04,0x8f,0x80,0x02,0x06\n\t") /* CFA, DW_OP_breg31 + 0x100, DW_OP_deref */
1119 __ASM_CFI(".cfi_escape 0x10,0x13,0x03,0x8f,0xa0,0x01\n\t") /* x19, DW_OP_breg31 + 0xA0 */
1120 __ASM_CFI(".cfi_escape 0x10,0x14,0x03,0x8f,0xa8,0x01\n\t") /* x20 */
1121 __ASM_CFI(".cfi_escape 0x10,0x15,0x03,0x8f,0xb0,0x01\n\t") /* x21 */
1122 __ASM_CFI(".cfi_escape 0x10,0x16,0x03,0x8f,0xb8,0x01\n\t") /* x22 */
1123 __ASM_CFI(".cfi_escape 0x10,0x17,0x03,0x8f,0xc0,0x01\n\t") /* x23 */
1124 __ASM_CFI(".cfi_escape 0x10,0x18,0x03,0x8f,0xc8,0x01\n\t") /* x24 */
1125 __ASM_CFI(".cfi_escape 0x10,0x19,0x03,0x8f,0xd0,0x01\n\t") /* x25 */
1126 __ASM_CFI(".cfi_escape 0x10,0x1a,0x03,0x8f,0xd8,0x01\n\t") /* x26 */
1127 __ASM_CFI(".cfi_escape 0x10,0x1b,0x03,0x8f,0xe0,0x01\n\t") /* x27 */
1128 __ASM_CFI(".cfi_escape 0x10,0x1c,0x03,0x8f,0xe8,0x01\n\t") /* x28 */
1129 __ASM_CFI(".cfi_escape 0x10,0x1d,0x03,0x8f,0xf0,0x01\n\t") /* x29 */
1130 __ASM_CFI(".cfi_escape 0x10,0x1e,0x03,0x8f,0xf8,0x01\n\t") /* x30 */
1131 __ASM_CFI(".cfi_escape 0x10,0x48,0x03,0x8f,0x90,0x03\n\t") /* d8 */
1132 __ASM_CFI(".cfi_escape 0x10,0x49,0x03,0x8f,0x98,0x03\n\t") /* d9 */
1133 __ASM_CFI(".cfi_escape 0x10,0x4a,0x03,0x8f,0xa0,0x03\n\t") /* d10 */
1134 __ASM_CFI(".cfi_escape 0x10,0x4b,0x03,0x8f,0xa8,0x03\n\t") /* d11 */
1135 __ASM_CFI(".cfi_escape 0x10,0x4c,0x03,0x8f,0xb0,0x03\n\t") /* d12 */
1136 __ASM_CFI(".cfi_escape 0x10,0x4d,0x03,0x8f,0xb8,0x03\n\t") /* d13 */
1137 __ASM_CFI(".cfi_escape 0x10,0x4e,0x03,0x8f,0xc0,0x03\n\t") /* d14 */
1138 __ASM_CFI(".cfi_escape 0x10,0x4f,0x03,0x8f,0xc8,0x03\n\t") /* d15 */
1139 __ASM_SEH(".seh_context\n\t")
1140 __ASM_SEH(".seh_endprologue\n\t")
1141 "ldp x1, x2, [x29, #0x10]\n\t"
1142 "ldr x18, [x29, #0x20]\n\t"
1146 __ASM_CFI(".cfi_restore_state\n\t")
1147 "ldp x29, x30, [sp], #48\n\t"
1148 __ASM_CFI(".cfi_restore 30\n\t")
1149 __ASM_CFI(".cfi_restore 29\n\t")
1150 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1153 /*******************************************************************
1154 * RtlRestoreContext (NTDLL.@)
1156 void CDECL
RtlRestoreContext( CONTEXT
*context
, EXCEPTION_RECORD
*rec
)
1158 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1160 if (rec
&& rec
->ExceptionCode
== STATUS_LONGJUMP
&& rec
->NumberParameters
>= 1)
1162 struct MSVCRT_JUMP_BUFFER
*jmp
= (struct MSVCRT_JUMP_BUFFER
*)rec
->ExceptionInformation
[0];
1165 context
->u
.s
.X19
= jmp
->X19
;
1166 context
->u
.s
.X20
= jmp
->X20
;
1167 context
->u
.s
.X21
= jmp
->X21
;
1168 context
->u
.s
.X22
= jmp
->X22
;
1169 context
->u
.s
.X23
= jmp
->X23
;
1170 context
->u
.s
.X24
= jmp
->X24
;
1171 context
->u
.s
.X25
= jmp
->X25
;
1172 context
->u
.s
.X26
= jmp
->X26
;
1173 context
->u
.s
.X27
= jmp
->X27
;
1174 context
->u
.s
.X28
= jmp
->X28
;
1175 context
->u
.s
.Fp
= jmp
->Fp
;
1176 context
->u
.s
.Lr
= jmp
->Lr
;
1177 context
->Sp
= jmp
->Sp
;
1178 context
->Fpcr
= jmp
->Fpcr
;
1179 context
->Fpsr
= jmp
->Fpsr
;
1181 for (i
= 0; i
< 8; i
++)
1182 context
->V
[8+i
].D
[0] = jmp
->D
[i
];
1184 else if (rec
&& rec
->ExceptionCode
== STATUS_UNWIND_CONSOLIDATE
&& rec
->NumberParameters
>= 1)
1186 PVOID (CALLBACK
*consolidate
)(EXCEPTION_RECORD
*) = (void *)rec
->ExceptionInformation
[0];
1187 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate
, rec
);
1188 rec
->ExceptionInformation
[10] = (ULONG_PTR
)&context
->u
.s
.X19
;
1190 context
->Pc
= (ULONG64
)call_consolidate_callback( context
, consolidate
, rec
, NtCurrentTeb() );
1193 /* hack: remove no longer accessible TEB frames */
1194 while ((ULONG64
)teb_frame
< context
->Sp
)
1196 TRACE( "removing TEB frame: %p\n", teb_frame
);
1197 teb_frame
= __wine_pop_frame( teb_frame
);
1200 TRACE( "returning to %I64x stack %I64x\n", context
->Pc
, context
->Sp
);
1201 NtContinue( context
, FALSE
);
1204 /*******************************************************************
1205 * RtlUnwindEx (NTDLL.@)
1207 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
1208 PVOID retval
, CONTEXT
*context
, UNWIND_HISTORY_TABLE
*table
)
1210 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1211 EXCEPTION_RECORD record
;
1212 DISPATCHER_CONTEXT dispatch
;
1213 CONTEXT new_context
;
1217 RtlCaptureContext( context
);
1218 new_context
= *context
;
1220 /* build an exception record, if we do not have one */
1223 record
.ExceptionCode
= STATUS_UNWIND
;
1224 record
.ExceptionFlags
= 0;
1225 record
.ExceptionRecord
= NULL
;
1226 record
.ExceptionAddress
= (void *)context
->Pc
;
1227 record
.NumberParameters
= 0;
1231 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
1233 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%016I64x\n",
1234 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, context
->Pc
);
1235 for (i
= 0; i
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); i
++)
1236 TRACE( " info[%ld]=%016I64x\n", i
, rec
->ExceptionInformation
[i
] );
1237 TRACE(" x0=%016I64x x1=%016I64x x2=%016I64x x3=%016I64x\n",
1238 context
->u
.s
.X0
, context
->u
.s
.X1
, context
->u
.s
.X2
, context
->u
.s
.X3
);
1239 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
1240 context
->u
.s
.X4
, context
->u
.s
.X5
, context
->u
.s
.X6
, context
->u
.s
.X7
);
1241 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
1242 context
->u
.s
.X8
, context
->u
.s
.X9
, context
->u
.s
.X10
, context
->u
.s
.X11
);
1243 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
1244 context
->u
.s
.X12
, context
->u
.s
.X13
, context
->u
.s
.X14
, context
->u
.s
.X15
);
1245 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
1246 context
->u
.s
.X16
, context
->u
.s
.X17
, context
->u
.s
.X18
, context
->u
.s
.X19
);
1247 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
1248 context
->u
.s
.X20
, context
->u
.s
.X21
, context
->u
.s
.X22
, context
->u
.s
.X23
);
1249 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
1250 context
->u
.s
.X24
, context
->u
.s
.X25
, context
->u
.s
.X26
, context
->u
.s
.X27
);
1251 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
1252 context
->u
.s
.X28
, context
->u
.s
.Fp
, context
->u
.s
.Lr
, context
->Sp
);
1254 dispatch
.TargetPc
= (ULONG64
)target_ip
;
1255 dispatch
.ContextRecord
= context
;
1256 dispatch
.HistoryTable
= table
;
1257 dispatch
.NonVolatileRegisters
= (BYTE
*)&context
->u
.s
.X19
;
1261 status
= virtual_unwind( UNW_FLAG_UHANDLER
, &dispatch
, &new_context
);
1262 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
1265 if (!dispatch
.EstablisherFrame
) break;
1267 if (!is_valid_frame( dispatch
.EstablisherFrame
))
1269 ERR( "invalid frame %I64x (%p-%p)\n", dispatch
.EstablisherFrame
,
1270 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1271 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
1275 if (dispatch
.LanguageHandler
)
1277 if (end_frame
&& (dispatch
.EstablisherFrame
> (ULONG64
)end_frame
))
1279 ERR( "invalid end frame %I64x/%p\n", dispatch
.EstablisherFrame
, end_frame
);
1280 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
1282 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) rec
->ExceptionFlags
|= EH_TARGET_UNWIND
;
1283 if (call_unwind_handler( rec
, &dispatch
) == ExceptionCollidedUnwind
)
1287 *context
= new_context
= *dispatch
.ContextRecord
;
1288 dispatch
.ContextRecord
= context
;
1289 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
1290 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
1291 &new_context
, &dispatch
.HandlerData
, &frame
,
1293 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
1296 rec
->ExceptionFlags
&= ~EH_COLLIDED_UNWIND
;
1298 else /* hack: call builtin handlers registered in the tib list */
1300 DWORD64 backup_frame
= dispatch
.EstablisherFrame
;
1301 while ((ULONG64
)teb_frame
< new_context
.Sp
&& (ULONG64
)teb_frame
< (ULONG64
)end_frame
)
1303 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
1304 dispatch
.EstablisherFrame
= (ULONG64
)teb_frame
;
1305 if (call_teb_unwind_handler( rec
, &dispatch
, teb_frame
) == ExceptionCollidedUnwind
)
1309 teb_frame
= __wine_pop_frame( teb_frame
);
1311 *context
= new_context
= *dispatch
.ContextRecord
;
1312 dispatch
.ContextRecord
= context
;
1313 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
1314 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
1315 &new_context
, &dispatch
.HandlerData
,
1317 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
1320 teb_frame
= __wine_pop_frame( teb_frame
);
1322 if ((ULONG64
)teb_frame
== (ULONG64
)end_frame
&& (ULONG64
)end_frame
< new_context
.Sp
) break;
1323 dispatch
.EstablisherFrame
= backup_frame
;
1326 if (dispatch
.EstablisherFrame
== (ULONG64
)end_frame
) break;
1327 *context
= new_context
;
1330 context
->u
.s
.X0
= (ULONG64
)retval
;
1331 context
->Pc
= (ULONG64
)target_ip
;
1332 RtlRestoreContext(context
, rec
);
1336 /***********************************************************************
1337 * RtlUnwind (NTDLL.@)
1339 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
1342 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
1345 /*******************************************************************
1346 * _local_unwind (NTDLL.@)
1348 void WINAPI
_local_unwind( void *frame
, void *target_ip
)
1351 RtlUnwindEx( frame
, target_ip
, NULL
, NULL
, &context
, NULL
);
1354 extern LONG
__C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs
, PVOID frame
,
1355 PEXCEPTION_FILTER filter
,
1356 PUCHAR nonvolatile
);
1357 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter
,
1358 "stp x29, x30, [sp, #-96]!\n\t"
1359 __ASM_SEH(".seh_save_fplr_x 96\n\t")
1360 "stp x19, x20, [sp, #16]\n\t"
1361 __ASM_SEH(".seh_save_regp x19, 16\n\t")
1362 "stp x21, x22, [sp, #32]\n\t"
1363 __ASM_SEH(".seh_save_regp x21, 32\n\t")
1364 "stp x23, x24, [sp, #48]\n\t"
1365 __ASM_SEH(".seh_save_regp x23, 48\n\t")
1366 "stp x25, x26, [sp, #64]\n\t"
1367 __ASM_SEH(".seh_save_regp x25, 64\n\t")
1368 "stp x27, x28, [sp, #80]\n\t"
1369 __ASM_SEH(".seh_save_regp x27, 80\n\t")
1371 __ASM_SEH(".seh_set_fp\n\t")
1372 __ASM_SEH(".seh_endprologue\n\t")
1374 __ASM_CFI(".cfi_def_cfa x29, 96\n\t")
1375 __ASM_CFI(".cfi_offset x29, -96\n\t")
1376 __ASM_CFI(".cfi_offset x30, -88\n\t")
1377 __ASM_CFI(".cfi_offset x19, -80\n\t")
1378 __ASM_CFI(".cfi_offset x20, -72\n\t")
1379 __ASM_CFI(".cfi_offset x21, -64\n\t")
1380 __ASM_CFI(".cfi_offset x22, -56\n\t")
1381 __ASM_CFI(".cfi_offset x23, -48\n\t")
1382 __ASM_CFI(".cfi_offset x24, -40\n\t")
1383 __ASM_CFI(".cfi_offset x25, -32\n\t")
1384 __ASM_CFI(".cfi_offset x26, -24\n\t")
1385 __ASM_CFI(".cfi_offset x27, -16\n\t")
1386 __ASM_CFI(".cfi_offset x28, -8\n\t")
1388 "ldp x19, x20, [x3, #0]\n\t"
1389 "ldp x21, x22, [x3, #16]\n\t"
1390 "ldp x23, x24, [x3, #32]\n\t"
1391 "ldp x25, x26, [x3, #48]\n\t"
1392 "ldp x27, x28, [x3, #64]\n\t"
1393 /* Overwrite the frame parameter with Fp from the
1394 * nonvolatile regs */
1395 "ldr x1, [x3, #80]\n\t"
1397 "ldp x19, x20, [sp, #16]\n\t"
1398 "ldp x21, x22, [sp, #32]\n\t"
1399 "ldp x23, x24, [sp, #48]\n\t"
1400 "ldp x25, x26, [sp, #64]\n\t"
1401 "ldp x27, x28, [sp, #80]\n\t"
1402 "ldp x29, x30, [sp], #96\n\t"
1405 extern void __C_ExecuteTerminationHandler(BOOL abnormal
, PVOID frame
,
1406 PTERMINATION_HANDLER handler
,
1407 PUCHAR nonvolatile
);
1408 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1409 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler
,
1410 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1412 /*******************************************************************
1413 * __C_specific_handler (NTDLL.@)
1415 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
1418 struct _DISPATCHER_CONTEXT
*dispatch
)
1420 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
1422 DWORD64 ControlPc
= dispatch
->ControlPc
;
1424 TRACE( "%p %p %p %p\n", rec
, frame
, context
, dispatch
);
1425 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
1427 if (dispatch
->ControlPcIsUnwound
)
1430 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
1432 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
1434 if (ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1435 ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1437 PTERMINATION_HANDLER handler
;
1439 if (table
->ScopeRecord
[i
].JumpTarget
) continue;
1441 if (rec
->ExceptionFlags
& EH_TARGET_UNWIND
&&
1442 dispatch
->TargetPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1443 dispatch
->TargetPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1448 handler
= (PTERMINATION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
1449 dispatch
->ScopeIndex
= i
+1;
1451 TRACE( "calling __finally %p frame %p\n", handler
, frame
);
1452 __C_ExecuteTerminationHandler( TRUE
, frame
, handler
,
1453 dispatch
->NonVolatileRegisters
);
1456 return ExceptionContinueSearch
;
1459 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
1461 if (ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1462 ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1464 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
1465 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
1467 EXCEPTION_POINTERS ptrs
;
1468 PEXCEPTION_FILTER filter
;
1470 filter
= (PEXCEPTION_FILTER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
1471 ptrs
.ExceptionRecord
= rec
;
1472 ptrs
.ContextRecord
= context
;
1473 TRACE( "calling filter %p ptrs %p frame %p\n", filter
, &ptrs
, frame
);
1474 switch (__C_ExecuteExceptionFilter( &ptrs
, frame
, filter
,
1475 dispatch
->NonVolatileRegisters
))
1477 case EXCEPTION_EXECUTE_HANDLER
:
1479 case EXCEPTION_CONTINUE_SEARCH
:
1481 case EXCEPTION_CONTINUE_EXECUTION
:
1482 return ExceptionContinueExecution
;
1485 TRACE( "unwinding to target %I64x\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
1486 RtlUnwindEx( frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
1487 rec
, 0, dispatch
->ContextRecord
, dispatch
->HistoryTable
);
1490 return ExceptionContinueSearch
;
1494 /***********************************************************************
1495 * RtlRaiseException (NTDLL.@)
1497 __ASM_STDCALL_FUNC( RtlRaiseException
, 4,
1498 "sub sp, sp, #0x3b0\n\t" /* 0x390 (context) + 0x20 */
1499 "stp x29, x30, [sp]\n\t"
1500 __ASM_SEH(".seh_stackalloc 0x3b0\n\t")
1501 __ASM_SEH(".seh_save_fplr 0\n\t")
1502 __ASM_SEH(".seh_endprologue\n\t")
1503 __ASM_CFI(".cfi_def_cfa x29, 944\n\t")
1504 __ASM_CFI(".cfi_offset x30, -936\n\t")
1505 __ASM_CFI(".cfi_offset x29, -944\n\t")
1507 "str x0, [sp, #0x10]\n\t"
1508 "add x0, sp, #0x20\n\t"
1509 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1510 "add x1, sp, #0x20\n\t" /* context pointer */
1511 "add x2, sp, #0x3b0\n\t" /* orig stack pointer */
1512 "str x2, [x1, #0x100]\n\t" /* context->Sp */
1513 "ldr x0, [sp, #0x10]\n\t" /* original first parameter */
1514 "str x0, [x1, #0x08]\n\t" /* context->X0 */
1515 "ldp x4, x5, [sp]\n\t" /* frame pointer, return address */
1516 "stp x4, x5, [x1, #0xf0]\n\t" /* context->Fp, Lr */
1517 "str x5, [x1, #0x108]\n\t" /* context->Pc */
1518 "str x5, [x0, #0x10]\n\t" /* rec->ExceptionAddress */
1520 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1521 "bl " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1523 /*************************************************************************
1524 * RtlCaptureStackBackTrace (NTDLL.@)
1526 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1528 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1532 /***********************************************************************
1533 * signal_start_thread
1535 __ASM_GLOBAL_FUNC( signal_start_thread
,
1536 "mov sp, x0\n\t" /* context */
1538 "b " __ASM_NAME("NtContinue") )
1540 /**********************************************************************
1541 * DbgBreakPoint (NTDLL.@)
1543 __ASM_STDCALL_FUNC( DbgBreakPoint
, 0, "brk #0xf000; ret"
1544 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1545 "\n\tnop; nop; nop; nop; nop; nop" );
1547 /**********************************************************************
1548 * DbgUserBreakPoint (NTDLL.@)
1550 __ASM_STDCALL_FUNC( DbgUserBreakPoint
, 0, "brk #0xf000; ret"
1551 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1552 "\n\tnop; nop; nop; nop; nop; nop" );
1554 /**********************************************************************
1555 * NtCurrentTeb (NTDLL.@)
1557 TEB
* WINAPI
NtCurrentTeb(void)
1559 return __wine_current_teb();
1562 #endif /* __aarch64__ */