ntdll: Use x18 for the TEB on ARM64.
[wine.git] / dlls / ntdll / signal_arm64.c
bloba19aa2f54b0efb538e78ac27f3833d2194e7e13b
1 /*
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
21 #ifdef __aarch64__
23 #include <assert.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33 #include "wine/exception.h"
34 #include "ntdll_misc.h"
35 #include "wine/debug.h"
36 #include "winnt.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
39 WINE_DECLARE_DEBUG_CHANNEL(threadname);
41 typedef struct _SCOPE_TABLE
43 ULONG Count;
44 struct
46 ULONG BeginAddress;
47 ULONG EndAddress;
48 ULONG HandlerAddress;
49 ULONG JumpTarget;
50 } ScopeRecord[1];
51 } SCOPE_TABLE, *PSCOPE_TABLE;
54 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
55 struct MSVCRT_JUMP_BUFFER
57 unsigned __int64 Frame;
58 unsigned __int64 Reserved;
59 unsigned __int64 X19;
60 unsigned __int64 X20;
61 unsigned __int64 X21;
62 unsigned __int64 X22;
63 unsigned __int64 X23;
64 unsigned __int64 X24;
65 unsigned __int64 X25;
66 unsigned __int64 X26;
67 unsigned __int64 X27;
68 unsigned __int64 X28;
69 unsigned __int64 Fp;
70 unsigned __int64 Lr;
71 unsigned __int64 Sp;
72 ULONG Fpcr;
73 ULONG Fpsr;
74 double D[8];
78 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
80 unsigned int i;
82 TRACE( "scope table at %p\n", table );
83 for (i = 0; i < table->Count; i++)
84 TRACE( " %u: %I64x-%I64x handler %I64x target %I64x\n", i,
85 base + table->ScopeRecord[i].BeginAddress,
86 base + table->ScopeRecord[i].EndAddress,
87 base + table->ScopeRecord[i].HandlerAddress,
88 base + table->ScopeRecord[i].JumpTarget );
91 /*******************************************************************
92 * is_valid_frame
94 static inline BOOL is_valid_frame( ULONG_PTR frame )
96 if (frame & 7) return FALSE;
97 return ((void *)frame >= NtCurrentTeb()->Tib.StackLimit &&
98 (void *)frame <= NtCurrentTeb()->Tib.StackBase);
102 /**************************************************************************
103 * __chkstk (NTDLL.@)
105 * Supposed to touch all the stack pages, but we shouldn't need that.
107 __ASM_GLOBAL_FUNC( __chkstk, "ret")
110 /***********************************************************************
111 * RtlCaptureContext (NTDLL.@)
113 __ASM_STDCALL_FUNC( RtlCaptureContext, 8,
114 "str xzr, [x0, #0x8]\n\t" /* context->X0 */
115 "stp x1, x2, [x0, #0x10]\n\t" /* context->X1,X2 */
116 "stp x3, x4, [x0, #0x20]\n\t" /* context->X3,X4 */
117 "stp x5, x6, [x0, #0x30]\n\t" /* context->X5,X6 */
118 "stp x7, x8, [x0, #0x40]\n\t" /* context->X7,X8 */
119 "stp x9, x10, [x0, #0x50]\n\t" /* context->X9,X10 */
120 "stp x11, x12, [x0, #0x60]\n\t" /* context->X11,X12 */
121 "stp x13, x14, [x0, #0x70]\n\t" /* context->X13,X14 */
122 "stp x15, x16, [x0, #0x80]\n\t" /* context->X15,X16 */
123 "stp x17, x18, [x0, #0x90]\n\t" /* context->X17,X18 */
124 "stp x19, x20, [x0, #0xa0]\n\t" /* context->X19,X20 */
125 "stp x21, x22, [x0, #0xb0]\n\t" /* context->X21,X22 */
126 "stp x23, x24, [x0, #0xc0]\n\t" /* context->X23,X24 */
127 "stp x25, x26, [x0, #0xd0]\n\t" /* context->X25,X26 */
128 "stp x27, x28, [x0, #0xe0]\n\t" /* context->X27,X28 */
129 "stp x29, xzr, [x0, #0xf0]\n\t" /* context->Fp,Lr */
130 "mov x1, sp\n\t"
131 "stp x1, x30, [x0, #0x100]\n\t" /* context->Sp,Pc */
132 "stp q0, q1, [x0, #0x110]\n\t" /* context->V[0-1] */
133 "stp q2, q3, [x0, #0x130]\n\t" /* context->V[2-3] */
134 "stp q4, q5, [x0, #0x150]\n\t" /* context->V[4-5] */
135 "stp q6, q7, [x0, #0x170]\n\t" /* context->V[6-7] */
136 "stp q8, q9, [x0, #0x190]\n\t" /* context->V[8-9] */
137 "stp q10, q11, [x0, #0x1b0]\n\t" /* context->V[10-11] */
138 "stp q12, q13, [x0, #0x1d0]\n\t" /* context->V[12-13] */
139 "stp q14, q15, [x0, #0x1f0]\n\t" /* context->V[14-15] */
140 "stp q16, q17, [x0, #0x210]\n\t" /* context->V[16-17] */
141 "stp q18, q19, [x0, #0x230]\n\t" /* context->V[18-19] */
142 "stp q20, q21, [x0, #0x250]\n\t" /* context->V[20-21] */
143 "stp q22, q23, [x0, #0x270]\n\t" /* context->V[22-23] */
144 "stp q24, q25, [x0, #0x290]\n\t" /* context->V[24-25] */
145 "stp q26, q27, [x0, #0x2b0]\n\t" /* context->V[26-27] */
146 "stp q28, q29, [x0, #0x2d0]\n\t" /* context->V[28-29] */
147 "stp q30, q31, [x0, #0x2f0]\n\t" /* context->V[30-31] */
148 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
149 "movk w1, #0x7\n\t" /* CONTEXT_FULL */
150 "str w1, [x0]\n\t" /* context->ContextFlags */
151 "mrs x1, NZCV\n\t"
152 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
153 "mrs x1, FPCR\n\t"
154 "str w1, [x0, #0x310]\n\t" /* context->Fpcr */
155 "mrs x1, FPSR\n\t"
156 "str w1, [x0, #0x314]\n\t" /* context->Fpsr */
157 "ret" )
160 /**********************************************************************
161 * virtual_unwind
163 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
165 LDR_DATA_TABLE_ENTRY *module;
166 NTSTATUS status;
167 DWORD64 pc;
169 dispatch->ImageBase = 0;
170 dispatch->ScopeIndex = 0;
171 dispatch->EstablisherFrame = 0;
172 dispatch->ControlPc = context->Pc;
174 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
175 * signal frame.
177 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
178 pc = context->Pc - (dispatch->ControlPcIsUnwound ? 4 : 0);
180 /* first look for PE exception information */
182 if ((dispatch->FunctionEntry = lookup_function_info(pc,
183 &dispatch->ImageBase, &module )))
185 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, pc,
186 dispatch->FunctionEntry, context,
187 &dispatch->HandlerData, &dispatch->EstablisherFrame,
188 NULL );
189 return STATUS_SUCCESS;
192 /* then look for host system exception information */
194 if (!module || (module->Flags & LDR_WINE_INTERNAL))
196 struct unwind_builtin_dll_params params = { type, dispatch, context };
198 status = WINE_UNIX_CALL( unix_unwind_builtin_dll, &params );
199 if (status != STATUS_SUCCESS) return status;
201 if (dispatch->EstablisherFrame)
203 dispatch->FunctionEntry = NULL;
204 if (dispatch->LanguageHandler && !module)
206 FIXME( "calling personality routine in system library not supported yet\n" );
207 dispatch->LanguageHandler = NULL;
209 return STATUS_SUCCESS;
212 else
214 status = context->Pc != context->Lr ?
215 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
216 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
217 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
218 (void*) context->Lr, status );
219 dispatch->EstablisherFrame = context->Sp;
220 dispatch->LanguageHandler = NULL;
221 context->Pc = context->Lr;
222 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
223 return status;
226 dispatch->EstablisherFrame = context->Fp;
227 dispatch->LanguageHandler = NULL;
228 context->Pc = context->Lr;
229 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
230 return STATUS_SUCCESS;
234 struct unwind_exception_frame
236 EXCEPTION_REGISTRATION_RECORD frame;
237 DISPATCHER_CONTEXT *dispatch;
240 /**********************************************************************
241 * unwind_exception_handler
243 * Handler for exceptions happening while calling an unwind handler.
245 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
246 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
248 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
249 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
251 /* copy the original dispatcher into the current one, except for the TargetIp */
252 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
253 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
254 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
255 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
256 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
257 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
258 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
259 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
260 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
261 TRACE( "detected collided unwind\n" );
262 return ExceptionCollidedUnwind;
265 /**********************************************************************
266 * call_unwind_handler
268 * Call a single unwind handler.
270 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
272 struct unwind_exception_frame frame;
273 DWORD res;
275 frame.frame.Handler = unwind_exception_handler;
276 frame.dispatch = dispatch;
277 __wine_push_frame( &frame.frame );
279 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
280 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
281 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
282 TRACE( "handler %p returned %lx\n", dispatch->LanguageHandler, res );
284 __wine_pop_frame( &frame.frame );
286 switch (res)
288 case ExceptionContinueSearch:
289 case ExceptionCollidedUnwind:
290 break;
291 default:
292 raise_status( STATUS_INVALID_DISPOSITION, rec );
293 break;
296 return res;
300 /**********************************************************************
301 * call_teb_unwind_handler
303 * Call a single unwind handler from the TEB chain.
305 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
306 EXCEPTION_REGISTRATION_RECORD *teb_frame )
308 DWORD res;
310 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
311 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
312 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
313 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
315 switch (res)
317 case ExceptionContinueSearch:
318 case ExceptionCollidedUnwind:
319 break;
320 default:
321 raise_status( STATUS_INVALID_DISPOSITION, rec );
322 break;
325 return res;
329 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
330 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
332 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
333 rec->ExceptionFlags |= EH_NESTED_CALL;
335 return ExceptionContinueSearch;
339 /**********************************************************************
340 * call_handler
342 * Call a single exception handler.
343 * FIXME: Handle nested exceptions.
345 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
347 EXCEPTION_REGISTRATION_RECORD frame;
348 DWORD res;
350 frame.Handler = nested_exception_handler;
351 __wine_push_frame( &frame );
353 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
354 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
355 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
356 TRACE( "handler at %p returned %lu\n", dispatch->LanguageHandler, res );
358 rec->ExceptionFlags &= EH_NONCONTINUABLE;
359 __wine_pop_frame( &frame );
360 return res;
364 /**********************************************************************
365 * call_teb_handler
367 * Call a single exception handler from the TEB chain.
368 * FIXME: Handle nested exceptions.
370 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
371 EXCEPTION_REGISTRATION_RECORD *teb_frame )
373 DWORD res;
375 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
376 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
377 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
378 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
379 return res;
383 /**********************************************************************
384 * call_function_handlers
386 * Call the per-function handlers.
388 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
390 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
391 UNWIND_HISTORY_TABLE table;
392 DISPATCHER_CONTEXT dispatch;
393 CONTEXT context, prev_context;
394 NTSTATUS status;
396 context = *orig_context;
397 dispatch.TargetPc = 0;
398 dispatch.ContextRecord = &context;
399 dispatch.HistoryTable = &table;
400 prev_context = context;
401 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.X19;
403 for (;;)
405 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
406 if (status != STATUS_SUCCESS) return status;
408 unwind_done:
409 if (!dispatch.EstablisherFrame) break;
411 if (!is_valid_frame( dispatch.EstablisherFrame ))
413 ERR( "invalid frame %I64x (%p-%p)\n", dispatch.EstablisherFrame,
414 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
415 rec->ExceptionFlags |= EH_STACK_INVALID;
416 break;
419 if (dispatch.LanguageHandler)
421 switch (call_handler( rec, orig_context, &dispatch ))
423 case ExceptionContinueExecution:
424 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
425 return STATUS_SUCCESS;
426 case ExceptionContinueSearch:
427 break;
428 case ExceptionNestedException:
429 FIXME( "nested exception\n" );
430 break;
431 case ExceptionCollidedUnwind: {
432 ULONG64 frame;
434 context = *dispatch.ContextRecord;
435 dispatch.ContextRecord = &context;
436 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
437 dispatch.ControlPc, dispatch.FunctionEntry,
438 &context, &dispatch.HandlerData, &frame, NULL );
439 goto unwind_done;
441 default:
442 return STATUS_INVALID_DISPOSITION;
445 /* hack: call wine handlers registered in the tib list */
446 else while ((ULONG64)teb_frame < context.Sp)
448 TRACE( "found wine frame %p rsp %I64x handler %p\n",
449 teb_frame, context.Sp, teb_frame->Handler );
450 dispatch.EstablisherFrame = (ULONG64)teb_frame;
451 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
453 case ExceptionContinueExecution:
454 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
455 return STATUS_SUCCESS;
456 case ExceptionContinueSearch:
457 break;
458 case ExceptionNestedException:
459 FIXME( "nested exception\n" );
460 break;
461 case ExceptionCollidedUnwind: {
462 ULONG64 frame;
464 context = *dispatch.ContextRecord;
465 dispatch.ContextRecord = &context;
466 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
467 dispatch.ControlPc, dispatch.FunctionEntry,
468 &context, &dispatch.HandlerData, &frame, NULL );
469 teb_frame = teb_frame->Prev;
470 goto unwind_done;
472 default:
473 return STATUS_INVALID_DISPOSITION;
475 teb_frame = teb_frame->Prev;
478 if (context.Sp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
479 prev_context = context;
481 return STATUS_UNHANDLED_EXCEPTION;
485 /*******************************************************************
486 * KiUserExceptionDispatcher (NTDLL.@)
488 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
490 NTSTATUS status;
491 DWORD c;
493 if (pWow64PrepareForException) pWow64PrepareForException( rec, context );
495 TRACE( "code=%lx flags=%lx addr=%p pc=%016I64x\n",
496 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Pc );
497 for (c = 0; c < rec->NumberParameters; c++)
498 TRACE( " info[%ld]=%016I64x\n", c, rec->ExceptionInformation[c] );
500 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
502 if (rec->ExceptionInformation[1] >> 16)
503 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
504 rec->ExceptionAddress,
505 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
506 else
507 MESSAGE( "wine: Call from %p to unimplemented function %s.%Id, aborting\n",
508 rec->ExceptionAddress,
509 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
511 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
513 if ((DWORD)rec->ExceptionInformation[2] == -1 || (DWORD)rec->ExceptionInformation[2] == GetCurrentThreadId())
514 WARN_(threadname)( "Thread renamed to %s\n", debugstr_a((char *)rec->ExceptionInformation[1]) );
515 else
516 WARN_(threadname)( "Thread ID %04lx renamed to %s\n", (DWORD)rec->ExceptionInformation[2],
517 debugstr_a((char *)rec->ExceptionInformation[1]) );
519 set_native_thread_name((DWORD)rec->ExceptionInformation[2], (char *)rec->ExceptionInformation[1]);
521 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
523 WARN( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
525 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
527 WARN( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
529 else
531 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
532 ERR( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
533 else
534 WARN( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
536 TRACE(" x0=%016I64x x1=%016I64x x2=%016I64x x3=%016I64x\n",
537 context->X0, context->X1, context->X2, context->X3 );
538 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
539 context->X4, context->X5, context->X6, context->X7 );
540 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
541 context->X8, context->X9, context->X10, context->X11 );
542 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
543 context->X12, context->X13, context->X14, context->X15 );
544 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
545 context->X16, context->X17, context->X18, context->X19 );
546 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
547 context->X20, context->X21, context->X22, context->X23 );
548 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
549 context->X24, context->X25, context->X26, context->X27 );
550 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
551 context->X28, context->Fp, context->Lr, context->Sp );
554 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
555 NtContinue( context, FALSE );
557 if ((status = call_function_handlers( rec, context )) == STATUS_SUCCESS)
558 NtContinue( context, FALSE );
560 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
561 return NtRaiseException( rec, context, FALSE );
565 /*******************************************************************
566 * KiUserApcDispatcher (NTDLL.@)
568 void WINAPI KiUserApcDispatcher( CONTEXT *context, ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3,
569 PNTAPCFUNC apc )
571 void (CALLBACK *func)(ULONG_PTR,ULONG_PTR,ULONG_PTR,CONTEXT*) = (void *)apc;
572 func( arg1, arg2, arg3, context );
573 NtContinue( context, TRUE );
577 /*******************************************************************
578 * KiUserCallbackDispatcher (NTDLL.@)
580 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
582 NTSTATUS status;
584 __TRY
586 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
587 status = NtCallbackReturn( NULL, 0, func( args, len ));
589 __EXCEPT_ALL
591 ERR_(seh)( "ignoring exception\n" );
592 status = NtCallbackReturn( 0, 0, 0 );
594 __ENDTRY
596 RtlRaiseStatus( status );
600 /***********************************************************************
601 * Definitions for Win32 unwind tables
604 struct unwind_info
606 DWORD function_length : 18;
607 DWORD version : 2;
608 DWORD x : 1;
609 DWORD e : 1;
610 DWORD epilog : 5;
611 DWORD codes : 5;
614 struct unwind_info_ext
616 WORD epilog;
617 BYTE codes;
618 BYTE reserved;
621 struct unwind_info_epilog
623 DWORD offset : 18;
624 DWORD res : 4;
625 DWORD index : 10;
628 static const BYTE unwind_code_len[256] =
630 /* 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,
631 /* 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,
632 /* 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,
633 /* 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,
634 /* 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,
635 /* 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,
636 /* 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,
637 /* 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
640 /***********************************************************************
641 * get_sequence_len
643 static unsigned int get_sequence_len( BYTE *ptr, BYTE *end )
645 unsigned int ret = 0;
647 while (ptr < end)
649 if (*ptr == 0xe4 || *ptr == 0xe5) break;
650 ptr += unwind_code_len[*ptr];
651 ret++;
653 return ret;
657 /***********************************************************************
658 * restore_regs
660 static void restore_regs( int reg, int count, int pos, CONTEXT *context,
661 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
663 int i, offset = max( 0, pos );
664 for (i = 0; i < count; i++)
666 if (ptrs && reg + i >= 19) (&ptrs->X19)[reg + i - 19] = (DWORD64 *)context->Sp + i + offset;
667 context->X[reg + i] = ((DWORD64 *)context->Sp)[i + offset];
669 if (pos < 0) context->Sp += -8 * pos;
673 /***********************************************************************
674 * restore_fpregs
676 static void restore_fpregs( int reg, int count, int pos, CONTEXT *context,
677 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
679 int i, offset = max( 0, pos );
680 for (i = 0; i < count; i++)
682 if (ptrs && reg + i >= 8) (&ptrs->D8)[reg + i - 8] = (DWORD64 *)context->Sp + i + offset;
683 context->V[reg + i].D[0] = ((double *)context->Sp)[i + offset];
685 if (pos < 0) context->Sp += -8 * pos;
689 static void do_pac_auth( CONTEXT *context )
691 register DWORD64 x17 __asm__( "x17" ) = context->Lr;
692 register DWORD64 x16 __asm__( "x16" ) = context->Sp;
694 /* This is the autib1716 instruction. The hint instruction is used here
695 * as gcc does not assemble autib1716 for pre armv8.3a targets. For
696 * pre-armv8.3a targets, this is just treated as a hint instruction, which
697 * is ignored. */
698 __asm__( "hint 0xe" : "+r"(x17) : "r"(x16) );
700 context->Lr = x17;
703 /***********************************************************************
704 * process_unwind_codes
706 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
707 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
709 unsigned int val, len, save_next = 2;
711 /* skip codes */
712 while (ptr < end && skip)
714 if (*ptr == 0xe4 || *ptr == 0xe5) break;
715 ptr += unwind_code_len[*ptr];
716 skip--;
719 while (ptr < end)
721 if ((len = unwind_code_len[*ptr]) > 1)
723 if (ptr + len > end) break;
724 val = ptr[0] * 0x100 + ptr[1];
726 else val = *ptr;
728 if (*ptr < 0x20) /* alloc_s */
729 context->Sp += 16 * (val & 0x1f);
730 else if (*ptr < 0x40) /* save_r19r20_x */
731 restore_regs( 19, save_next, -(val & 0x1f), context, ptrs );
732 else if (*ptr < 0x80) /* save_fplr */
733 restore_regs( 29, 2, val & 0x3f, context, ptrs );
734 else if (*ptr < 0xc0) /* save_fplr_x */
735 restore_regs( 29, 2, -(val & 0x3f) - 1, context, ptrs );
736 else if (*ptr < 0xc8) /* alloc_m */
737 context->Sp += 16 * (val & 0x7ff);
738 else if (*ptr < 0xcc) /* save_regp */
739 restore_regs( 19 + ((val >> 6) & 0xf), save_next, val & 0x3f, context, ptrs );
740 else if (*ptr < 0xd0) /* save_regp_x */
741 restore_regs( 19 + ((val >> 6) & 0xf), save_next, -(val & 0x3f) - 1, context, ptrs );
742 else if (*ptr < 0xd4) /* save_reg */
743 restore_regs( 19 + ((val >> 6) & 0xf), 1, val & 0x3f, context, ptrs );
744 else if (*ptr < 0xd6) /* save_reg_x */
745 restore_regs( 19 + ((val >> 5) & 0xf), 1, -(val & 0x1f) - 1, context, ptrs );
746 else if (*ptr < 0xd8) /* save_lrpair */
748 restore_regs( 19 + 2 * ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
749 restore_regs( 30, 1, (val & 0x3f) + 1, context, ptrs );
751 else if (*ptr < 0xda) /* save_fregp */
752 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, val & 0x3f, context, ptrs );
753 else if (*ptr < 0xdc) /* save_fregp_x */
754 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, -(val & 0x3f) - 1, context, ptrs );
755 else if (*ptr < 0xde) /* save_freg */
756 restore_fpregs( 8 + ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
757 else if (*ptr == 0xde) /* save_freg_x */
758 restore_fpregs( 8 + ((val >> 5) & 0x7), 1, -(val & 0x3f) - 1, context, ptrs );
759 else if (*ptr == 0xe0) /* alloc_l */
760 context->Sp += 16 * ((ptr[1] << 16) + (ptr[2] << 8) + ptr[3]);
761 else if (*ptr == 0xe1) /* set_fp */
762 context->Sp = context->Fp;
763 else if (*ptr == 0xe2) /* add_fp */
764 context->Sp = context->Fp - 8 * (val & 0xff);
765 else if (*ptr == 0xe3) /* nop */
766 /* nop */ ;
767 else if (*ptr == 0xe4) /* end */
768 break;
769 else if (*ptr == 0xe5) /* end_c */
770 break;
771 else if (*ptr == 0xe6) /* save_next */
773 save_next += 2;
774 ptr += len;
775 continue;
777 else if (*ptr == 0xe9) /* MSFT_OP_MACHINE_FRAME */
779 context->Pc = ((DWORD64 *)context->Sp)[1];
780 context->Sp = ((DWORD64 *)context->Sp)[0];
782 else if (*ptr == 0xea) /* MSFT_OP_CONTEXT */
784 memcpy( context, (DWORD64 *)context->Sp, sizeof(CONTEXT) );
786 else if (*ptr == 0xfc) /* pac_sign_lr */
788 do_pac_auth( context );
790 else
792 WARN( "unsupported code %02x\n", *ptr );
793 return;
795 save_next = 2;
796 ptr += len;
801 /***********************************************************************
802 * unwind_packed_data
804 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
805 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
807 int i;
808 unsigned int len, offset, skip = 0;
809 unsigned int int_size = func->RegI * 8, fp_size = func->RegF * 8, regsave, local_size;
810 unsigned int int_regs, fp_regs, saved_regs, local_size_regs;
812 TRACE( "function %I64x-%I64x: len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
813 base + func->BeginAddress, base + func->BeginAddress + func->FunctionLength * 4,
814 func->FunctionLength, func->Flag, func->RegF, func->RegI, func->H, func->CR, func->FrameSize );
816 if (func->CR == 1) int_size += 8;
817 if (func->RegF) fp_size += 8;
819 regsave = ((int_size + fp_size + 8 * 8 * func->H) + 0xf) & ~0xf;
820 local_size = func->FrameSize * 16 - regsave;
822 int_regs = int_size / 8;
823 fp_regs = fp_size / 8;
824 saved_regs = regsave / 8;
825 local_size_regs = local_size / 8;
827 /* check for prolog/epilog */
828 if (func->Flag == 1)
830 offset = ((pc - base) - func->BeginAddress) / 4;
831 if (offset < 17 || offset >= func->FunctionLength - 15)
833 len = (int_size + 8) / 16 + (fp_size + 8) / 16;
834 switch (func->CR)
836 case 2:
837 len++; /* pacibsp */
838 /* fall through */
839 case 3:
840 len++; /* mov x29,sp */
841 len++; /* stp x29,lr,[sp,0] */
842 if (local_size <= 512) break;
843 /* fall through */
844 case 0:
845 case 1:
846 if (local_size) len++; /* sub sp,sp,#local_size */
847 if (local_size > 4088) len++; /* sub sp,sp,#4088 */
848 break;
850 len += 4 * func->H;
851 if (offset < len) /* prolog */
853 skip = len - offset;
855 else if (offset >= func->FunctionLength - (len + 1)) /* epilog */
857 skip = offset - (func->FunctionLength - (len + 1));
862 if (!skip)
864 if (func->CR == 3 || func->CR == 2)
866 DWORD64 *fp = (DWORD64 *) context->Fp; /* X[29] */
867 context->Sp = context->Fp;
868 context->X[29] = fp[0];
869 context->X[30] = fp[1];
871 context->Sp += local_size;
872 if (fp_size) restore_fpregs( 8, fp_regs, int_regs, context, ptrs );
873 if (func->CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
874 restore_regs( 19, func->RegI, -saved_regs, context, ptrs );
876 else
878 unsigned int pos = 0;
880 switch (func->CR)
882 case 3:
883 case 2:
884 /* mov x29,sp */
885 if (pos++ >= skip) context->Sp = context->Fp;
886 if (local_size <= 512)
888 /* stp x29,lr,[sp,-#local_size]! */
889 if (pos++ >= skip) restore_regs( 29, 2, -local_size_regs, context, ptrs );
890 break;
892 /* stp x29,lr,[sp,0] */
893 if (pos++ >= skip) restore_regs( 29, 2, 0, context, ptrs );
894 /* fall through */
895 case 0:
896 case 1:
897 if (!local_size) break;
898 /* sub sp,sp,#local_size */
899 if (pos++ >= skip) context->Sp += (local_size - 1) % 4088 + 1;
900 if (local_size > 4088 && pos++ >= skip) context->Sp += 4088;
901 break;
904 if (func->H) pos += 4;
906 if (fp_size)
908 if (func->RegF % 2 == 0 && pos++ >= skip)
909 /* str d%u,[sp,#fp_size] */
910 restore_fpregs( 8 + func->RegF, 1, int_regs + fp_regs - 1, context, ptrs );
911 for (i = (func->RegF + 1) / 2 - 1; i >= 0; i--)
913 if (pos++ < skip) continue;
914 if (!i && !int_size)
915 /* stp d8,d9,[sp,-#regsave]! */
916 restore_fpregs( 8, 2, -saved_regs, context, ptrs );
917 else
918 /* stp dn,dn+1,[sp,#offset] */
919 restore_fpregs( 8 + 2 * i, 2, int_regs + 2 * i, context, ptrs );
923 if (func->RegI % 2)
925 if (pos++ >= skip)
927 /* stp xn,lr,[sp,#offset] */
928 if (func->CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
929 /* str xn,[sp,#offset] */
930 restore_regs( 18 + func->RegI, 1,
931 (func->RegI > 1) ? func->RegI - 1 : -saved_regs,
932 context, ptrs );
935 else if (func->CR == 1)
937 /* str lr,[sp,#offset] */
938 if (pos++ >= skip) restore_regs( 30, 1, func->RegI ? int_regs - 1 : -saved_regs, context, ptrs );
941 for (i = func->RegI / 2 - 1; i >= 0; i--)
943 if (pos++ < skip) continue;
944 if (i)
945 /* stp xn,xn+1,[sp,#offset] */
946 restore_regs( 19 + 2 * i, 2, 2 * i, context, ptrs );
947 else
948 /* stp x19,x20,[sp,-#regsave]! */
949 restore_regs( 19, 2, -saved_regs, context, ptrs );
952 if (func->CR == 2) do_pac_auth( context );
953 return NULL;
957 /***********************************************************************
958 * unwind_full_data
960 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
961 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
963 struct unwind_info *info;
964 struct unwind_info_epilog *info_epilog;
965 unsigned int i, codes, epilogs, len, offset;
966 void *data;
967 BYTE *end;
969 info = (struct unwind_info *)((char *)base + func->UnwindData);
970 data = info + 1;
971 epilogs = info->epilog;
972 codes = info->codes;
973 if (!codes && !epilogs)
975 struct unwind_info_ext *infoex = data;
976 codes = infoex->codes;
977 epilogs = infoex->epilog;
978 data = infoex + 1;
980 info_epilog = data;
981 if (!info->e) data = info_epilog + epilogs;
983 offset = ((pc - base) - func->BeginAddress) / 4;
984 end = (BYTE *)data + codes * 4;
986 TRACE( "function %I64x-%I64x: len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
987 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 4,
988 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
990 /* check for prolog */
991 if (offset < codes * 4)
993 len = get_sequence_len( data, end );
994 if (offset < len)
996 process_unwind_codes( data, end, context, ptrs, len - offset );
997 return NULL;
1001 /* check for epilog */
1002 if (!info->e)
1004 for (i = 0; i < epilogs; i++)
1006 if (offset < info_epilog[i].offset) break;
1007 if (offset - info_epilog[i].offset < codes * 4 - info_epilog[i].index)
1009 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
1010 len = get_sequence_len( ptr, end );
1011 if (offset <= info_epilog[i].offset + len)
1013 process_unwind_codes( ptr, end, context, ptrs, offset - info_epilog[i].offset );
1014 return NULL;
1019 else if (info->function_length - offset <= codes * 4 - epilogs)
1021 BYTE *ptr = (BYTE *)data + epilogs;
1022 len = get_sequence_len( ptr, end ) + 1;
1023 if (offset >= info->function_length - len)
1025 process_unwind_codes( ptr, end, context, ptrs, offset - (info->function_length - len) );
1026 return NULL;
1030 process_unwind_codes( data, end, context, ptrs, 0 );
1032 /* get handler since we are inside the main code */
1033 if (info->x)
1035 DWORD *handler_rva = (DWORD *)data + codes;
1036 *handler_data = handler_rva + 1;
1037 return (char *)base + *handler_rva;
1039 return NULL;
1043 /**********************************************************************
1044 * RtlVirtualUnwind (NTDLL.@)
1046 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
1047 RUNTIME_FUNCTION *func, CONTEXT *context,
1048 PVOID *handler_data, ULONG_PTR *frame_ret,
1049 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
1051 void *handler;
1053 TRACE( "type %lx pc %I64x sp %I64x func %I64x\n", type, pc, context->Sp, base + func->BeginAddress );
1055 *handler_data = NULL;
1057 context->Pc = 0;
1058 if (func->Flag)
1059 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1060 else
1061 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1063 TRACE( "ret: lr=%I64x sp=%I64x handler=%p\n", context->Lr, context->Sp, handler );
1064 if (!context->Pc)
1065 context->Pc = context->Lr;
1066 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1067 *frame_ret = context->Sp;
1068 return handler;
1071 /**********************************************************************
1072 * call_consolidate_callback
1074 * Wrapper function to call a consolidate callback from a fake frame.
1075 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1076 * we have to skip all frames which were already processed. To do that we
1077 * trick the unwinding functions into thinking the call came from somewhere
1078 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1079 * DW_CFA_expression, and the expressions have the following format:
1081 * DW_OP_breg31; sleb128 <OFFSET> | Load x31 + struct member offset
1082 * [DW_OP_deref] | Dereference, only for CFA
1084 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1085 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1086 EXCEPTION_RECORD *rec,
1087 TEB *teb );
1088 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1089 "stp x29, x30, [sp, #-0x30]!\n\t"
1090 __ASM_CFI(".cfi_def_cfa_offset 48\n\t")
1091 __ASM_CFI(".cfi_offset 29, -48\n\t")
1092 __ASM_CFI(".cfi_offset 30, -40\n\t")
1093 __ASM_SEH(".seh_nop\n\t")
1094 "stp x1, x2, [sp, #0x10]\n\t"
1095 __ASM_SEH(".seh_nop\n\t")
1096 "str x3, [sp, #0x20]\n\t"
1097 __ASM_SEH(".seh_nop\n\t")
1098 "mov x29, sp\n\t"
1099 __ASM_CFI(".cfi_def_cfa_register 29\n\t")
1100 __ASM_SEH(".seh_nop\n\t")
1101 __ASM_CFI(".cfi_remember_state\n\t")
1102 /* Memcpy the context onto the stack */
1103 "sub sp, sp, #0x390\n\t"
1104 __ASM_SEH(".seh_nop\n\t")
1105 "mov x1, x0\n\t"
1106 __ASM_SEH(".seh_nop\n\t")
1107 "mov x0, sp\n\t"
1108 __ASM_SEH(".seh_nop\n\t")
1109 "mov x2, #0x390\n\t"
1110 __ASM_SEH(".seh_nop\n\t")
1111 "bl " __ASM_NAME("memcpy") "\n\t"
1112 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1113 __ASM_CFI(".cfi_escape 0x0f,0x04,0x8f,0x80,0x02,0x06\n\t") /* CFA, DW_OP_breg31 + 0x100, DW_OP_deref */
1114 __ASM_CFI(".cfi_escape 0x10,0x13,0x03,0x8f,0xa0,0x01\n\t") /* x19, DW_OP_breg31 + 0xA0 */
1115 __ASM_CFI(".cfi_escape 0x10,0x14,0x03,0x8f,0xa8,0x01\n\t") /* x20 */
1116 __ASM_CFI(".cfi_escape 0x10,0x15,0x03,0x8f,0xb0,0x01\n\t") /* x21 */
1117 __ASM_CFI(".cfi_escape 0x10,0x16,0x03,0x8f,0xb8,0x01\n\t") /* x22 */
1118 __ASM_CFI(".cfi_escape 0x10,0x17,0x03,0x8f,0xc0,0x01\n\t") /* x23 */
1119 __ASM_CFI(".cfi_escape 0x10,0x18,0x03,0x8f,0xc8,0x01\n\t") /* x24 */
1120 __ASM_CFI(".cfi_escape 0x10,0x19,0x03,0x8f,0xd0,0x01\n\t") /* x25 */
1121 __ASM_CFI(".cfi_escape 0x10,0x1a,0x03,0x8f,0xd8,0x01\n\t") /* x26 */
1122 __ASM_CFI(".cfi_escape 0x10,0x1b,0x03,0x8f,0xe0,0x01\n\t") /* x27 */
1123 __ASM_CFI(".cfi_escape 0x10,0x1c,0x03,0x8f,0xe8,0x01\n\t") /* x28 */
1124 __ASM_CFI(".cfi_escape 0x10,0x1d,0x03,0x8f,0xf0,0x01\n\t") /* x29 */
1125 __ASM_CFI(".cfi_escape 0x10,0x1e,0x03,0x8f,0xf8,0x01\n\t") /* x30 */
1126 __ASM_CFI(".cfi_escape 0x10,0x48,0x03,0x8f,0x90,0x03\n\t") /* d8 */
1127 __ASM_CFI(".cfi_escape 0x10,0x49,0x03,0x8f,0x98,0x03\n\t") /* d9 */
1128 __ASM_CFI(".cfi_escape 0x10,0x4a,0x03,0x8f,0xa0,0x03\n\t") /* d10 */
1129 __ASM_CFI(".cfi_escape 0x10,0x4b,0x03,0x8f,0xa8,0x03\n\t") /* d11 */
1130 __ASM_CFI(".cfi_escape 0x10,0x4c,0x03,0x8f,0xb0,0x03\n\t") /* d12 */
1131 __ASM_CFI(".cfi_escape 0x10,0x4d,0x03,0x8f,0xb8,0x03\n\t") /* d13 */
1132 __ASM_CFI(".cfi_escape 0x10,0x4e,0x03,0x8f,0xc0,0x03\n\t") /* d14 */
1133 __ASM_CFI(".cfi_escape 0x10,0x4f,0x03,0x8f,0xc8,0x03\n\t") /* d15 */
1134 __ASM_SEH(".seh_context\n\t")
1135 __ASM_SEH(".seh_endprologue\n\t")
1136 "ldp x1, x2, [x29, #0x10]\n\t"
1137 "ldr x18, [x29, #0x20]\n\t"
1138 "mov x0, x2\n\t"
1139 "blr x1\n\t"
1140 "mov sp, x29\n\t"
1141 __ASM_CFI(".cfi_restore_state\n\t")
1142 "ldp x29, x30, [sp], #48\n\t"
1143 __ASM_CFI(".cfi_restore 30\n\t")
1144 __ASM_CFI(".cfi_restore 29\n\t")
1145 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1146 "ret")
1148 /*******************************************************************
1149 * RtlRestoreContext (NTDLL.@)
1151 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1153 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1155 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1157 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1158 int i;
1160 context->X19 = jmp->X19;
1161 context->X20 = jmp->X20;
1162 context->X21 = jmp->X21;
1163 context->X22 = jmp->X22;
1164 context->X23 = jmp->X23;
1165 context->X24 = jmp->X24;
1166 context->X25 = jmp->X25;
1167 context->X26 = jmp->X26;
1168 context->X27 = jmp->X27;
1169 context->X28 = jmp->X28;
1170 context->Fp = jmp->Fp;
1171 context->Lr = jmp->Lr;
1172 context->Sp = jmp->Sp;
1173 context->Fpcr = jmp->Fpcr;
1174 context->Fpsr = jmp->Fpsr;
1176 for (i = 0; i < 8; i++)
1177 context->V[8+i].D[0] = jmp->D[i];
1179 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1181 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1182 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1183 rec->ExceptionInformation[10] = (ULONG_PTR)&context->X19;
1185 context->Pc = (ULONG64)call_consolidate_callback( context, consolidate, rec, NtCurrentTeb() );
1188 /* hack: remove no longer accessible TEB frames */
1189 while ((ULONG64)teb_frame < context->Sp)
1191 TRACE( "removing TEB frame: %p\n", teb_frame );
1192 teb_frame = __wine_pop_frame( teb_frame );
1195 TRACE( "returning to %I64x stack %I64x\n", context->Pc, context->Sp );
1196 NtContinue( context, FALSE );
1199 /*******************************************************************
1200 * RtlUnwindEx (NTDLL.@)
1202 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1203 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1205 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1206 EXCEPTION_RECORD record;
1207 DISPATCHER_CONTEXT dispatch;
1208 CONTEXT new_context;
1209 NTSTATUS status;
1210 DWORD i;
1212 RtlCaptureContext( context );
1213 new_context = *context;
1215 /* build an exception record, if we do not have one */
1216 if (!rec)
1218 record.ExceptionCode = STATUS_UNWIND;
1219 record.ExceptionFlags = 0;
1220 record.ExceptionRecord = NULL;
1221 record.ExceptionAddress = (void *)context->Pc;
1222 record.NumberParameters = 0;
1223 rec = &record;
1226 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1228 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%016I64x\n",
1229 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1230 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1231 TRACE( " info[%ld]=%016I64x\n", i, rec->ExceptionInformation[i] );
1232 TRACE(" x0=%016I64x x1=%016I64x x2=%016I64x x3=%016I64x\n",
1233 context->X0, context->X1, context->X2, context->X3 );
1234 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
1235 context->X4, context->X5, context->X6, context->X7 );
1236 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
1237 context->X8, context->X9, context->X10, context->X11 );
1238 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
1239 context->X12, context->X13, context->X14, context->X15 );
1240 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
1241 context->X16, context->X17, context->X18, context->X19 );
1242 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
1243 context->X20, context->X21, context->X22, context->X23 );
1244 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
1245 context->X24, context->X25, context->X26, context->X27 );
1246 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
1247 context->X28, context->Fp, context->Lr, context->Sp );
1249 dispatch.TargetPc = (ULONG64)target_ip;
1250 dispatch.ContextRecord = context;
1251 dispatch.HistoryTable = table;
1252 dispatch.NonVolatileRegisters = (BYTE *)&context->X19;
1254 for (;;)
1256 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1257 if (status != STATUS_SUCCESS) raise_status( status, rec );
1259 unwind_done:
1260 if (!dispatch.EstablisherFrame) break;
1262 if (!is_valid_frame( dispatch.EstablisherFrame ))
1264 ERR( "invalid frame %I64x (%p-%p)\n", dispatch.EstablisherFrame,
1265 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1266 rec->ExceptionFlags |= EH_STACK_INVALID;
1267 break;
1270 if (dispatch.LanguageHandler)
1272 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
1274 ERR( "invalid end frame %I64x/%p\n", 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)
1280 ULONG64 frame;
1282 *context = new_context = *dispatch.ContextRecord;
1283 dispatch.ContextRecord = context;
1284 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1285 dispatch.ControlPc, dispatch.FunctionEntry,
1286 &new_context, &dispatch.HandlerData, &frame,
1287 NULL );
1288 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1289 goto unwind_done;
1291 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1293 else /* hack: call builtin handlers registered in the tib list */
1295 DWORD64 backup_frame = dispatch.EstablisherFrame;
1296 while ((ULONG64)teb_frame < new_context.Sp && (ULONG64)teb_frame < (ULONG64)end_frame)
1298 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1299 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1300 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1302 ULONG64 frame;
1304 teb_frame = __wine_pop_frame( teb_frame );
1306 *context = new_context = *dispatch.ContextRecord;
1307 dispatch.ContextRecord = context;
1308 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1309 dispatch.ControlPc, dispatch.FunctionEntry,
1310 &new_context, &dispatch.HandlerData,
1311 &frame, NULL );
1312 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1313 goto unwind_done;
1315 teb_frame = __wine_pop_frame( teb_frame );
1317 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Sp) break;
1318 dispatch.EstablisherFrame = backup_frame;
1321 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
1322 *context = new_context;
1325 context->X0 = (ULONG64)retval;
1326 context->Pc = (ULONG64)target_ip;
1327 RtlRestoreContext(context, rec);
1331 /***********************************************************************
1332 * RtlUnwind (NTDLL.@)
1334 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1336 CONTEXT context;
1337 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1340 /*******************************************************************
1341 * _local_unwind (NTDLL.@)
1343 void WINAPI _local_unwind( void *frame, void *target_ip )
1345 CONTEXT context;
1346 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1349 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1350 PEXCEPTION_FILTER filter,
1351 PUCHAR nonvolatile);
1352 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1353 "stp x29, x30, [sp, #-96]!\n\t"
1354 __ASM_SEH(".seh_save_fplr_x 96\n\t")
1355 "stp x19, x20, [sp, #16]\n\t"
1356 __ASM_SEH(".seh_save_regp x19, 16\n\t")
1357 "stp x21, x22, [sp, #32]\n\t"
1358 __ASM_SEH(".seh_save_regp x21, 32\n\t")
1359 "stp x23, x24, [sp, #48]\n\t"
1360 __ASM_SEH(".seh_save_regp x23, 48\n\t")
1361 "stp x25, x26, [sp, #64]\n\t"
1362 __ASM_SEH(".seh_save_regp x25, 64\n\t")
1363 "stp x27, x28, [sp, #80]\n\t"
1364 __ASM_SEH(".seh_save_regp x27, 80\n\t")
1365 "mov x29, sp\n\t"
1366 __ASM_SEH(".seh_set_fp\n\t")
1367 __ASM_SEH(".seh_endprologue\n\t")
1369 __ASM_CFI(".cfi_def_cfa x29, 96\n\t")
1370 __ASM_CFI(".cfi_offset x29, -96\n\t")
1371 __ASM_CFI(".cfi_offset x30, -88\n\t")
1372 __ASM_CFI(".cfi_offset x19, -80\n\t")
1373 __ASM_CFI(".cfi_offset x20, -72\n\t")
1374 __ASM_CFI(".cfi_offset x21, -64\n\t")
1375 __ASM_CFI(".cfi_offset x22, -56\n\t")
1376 __ASM_CFI(".cfi_offset x23, -48\n\t")
1377 __ASM_CFI(".cfi_offset x24, -40\n\t")
1378 __ASM_CFI(".cfi_offset x25, -32\n\t")
1379 __ASM_CFI(".cfi_offset x26, -24\n\t")
1380 __ASM_CFI(".cfi_offset x27, -16\n\t")
1381 __ASM_CFI(".cfi_offset x28, -8\n\t")
1383 "ldp x19, x20, [x3, #0]\n\t"
1384 "ldp x21, x22, [x3, #16]\n\t"
1385 "ldp x23, x24, [x3, #32]\n\t"
1386 "ldp x25, x26, [x3, #48]\n\t"
1387 "ldp x27, x28, [x3, #64]\n\t"
1388 /* Overwrite the frame parameter with Fp from the
1389 * nonvolatile regs */
1390 "ldr x1, [x3, #80]\n\t"
1391 "blr x2\n\t"
1392 "ldp x19, x20, [sp, #16]\n\t"
1393 "ldp x21, x22, [sp, #32]\n\t"
1394 "ldp x23, x24, [sp, #48]\n\t"
1395 "ldp x25, x26, [sp, #64]\n\t"
1396 "ldp x27, x28, [sp, #80]\n\t"
1397 "ldp x29, x30, [sp], #96\n\t"
1398 "ret")
1400 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1401 PTERMINATION_HANDLER handler,
1402 PUCHAR nonvolatile);
1403 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1404 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1405 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1407 /*******************************************************************
1408 * __C_specific_handler (NTDLL.@)
1410 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1411 void *frame,
1412 CONTEXT *context,
1413 struct _DISPATCHER_CONTEXT *dispatch )
1415 SCOPE_TABLE *table = dispatch->HandlerData;
1416 ULONG i;
1417 DWORD64 ControlPc = dispatch->ControlPc;
1419 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1420 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1422 if (dispatch->ControlPcIsUnwound)
1423 ControlPc -= 4;
1425 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1427 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1429 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1430 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1432 PTERMINATION_HANDLER handler;
1434 if (table->ScopeRecord[i].JumpTarget) continue;
1436 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1437 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1438 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1440 break;
1443 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1444 dispatch->ScopeIndex = i+1;
1446 TRACE( "calling __finally %p frame %p\n", handler, frame );
1447 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1448 dispatch->NonVolatileRegisters );
1451 return ExceptionContinueSearch;
1454 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1456 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1457 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1459 if (!table->ScopeRecord[i].JumpTarget) continue;
1460 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1462 EXCEPTION_POINTERS ptrs;
1463 PEXCEPTION_FILTER filter;
1465 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1466 ptrs.ExceptionRecord = rec;
1467 ptrs.ContextRecord = context;
1468 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1469 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1470 dispatch->NonVolatileRegisters ))
1472 case EXCEPTION_EXECUTE_HANDLER:
1473 break;
1474 case EXCEPTION_CONTINUE_SEARCH:
1475 continue;
1476 case EXCEPTION_CONTINUE_EXECUTION:
1477 return ExceptionContinueExecution;
1480 TRACE( "unwinding to target %I64x\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1481 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1482 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1485 return ExceptionContinueSearch;
1489 /***********************************************************************
1490 * RtlRaiseException (NTDLL.@)
1492 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
1493 "sub sp, sp, #0x3b0\n\t" /* 0x390 (context) + 0x20 */
1494 "stp x29, x30, [sp]\n\t"
1495 __ASM_SEH(".seh_stackalloc 0x3b0\n\t")
1496 __ASM_SEH(".seh_save_fplr 0\n\t")
1497 __ASM_SEH(".seh_endprologue\n\t")
1498 __ASM_CFI(".cfi_def_cfa x29, 944\n\t")
1499 __ASM_CFI(".cfi_offset x30, -936\n\t")
1500 __ASM_CFI(".cfi_offset x29, -944\n\t")
1501 "mov x29, sp\n\t"
1502 "str x0, [sp, #0x10]\n\t"
1503 "add x0, sp, #0x20\n\t"
1504 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1505 "add x1, sp, #0x20\n\t" /* context pointer */
1506 "add x2, sp, #0x3b0\n\t" /* orig stack pointer */
1507 "str x2, [x1, #0x100]\n\t" /* context->Sp */
1508 "ldr x0, [sp, #0x10]\n\t" /* original first parameter */
1509 "str x0, [x1, #0x08]\n\t" /* context->X0 */
1510 "ldp x4, x5, [sp]\n\t" /* frame pointer, return address */
1511 "stp x4, x5, [x1, #0xf0]\n\t" /* context->Fp, Lr */
1512 "str x5, [x1, #0x108]\n\t" /* context->Pc */
1513 "str x5, [x0, #0x10]\n\t" /* rec->ExceptionAddress */
1514 "mov x2, #1\n\t"
1515 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1516 "bl " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1518 /*************************************************************************
1519 * RtlCaptureStackBackTrace (NTDLL.@)
1521 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1523 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip, count, buffer, hash );
1524 return 0;
1527 /***********************************************************************
1528 * signal_start_thread
1530 __ASM_GLOBAL_FUNC( signal_start_thread,
1531 "mov sp, x0\n\t" /* context */
1532 "mov x1, #1\n\t"
1533 "b " __ASM_NAME("NtContinue") )
1535 /**********************************************************************
1536 * DbgBreakPoint (NTDLL.@)
1538 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "brk #0xf000; ret"
1539 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1540 "\n\tnop; nop; nop; nop; nop; nop" );
1542 /**********************************************************************
1543 * DbgUserBreakPoint (NTDLL.@)
1545 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "brk #0xf000; ret"
1546 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1547 "\n\tnop; nop; nop; nop; nop; nop" );
1549 #endif /* __aarch64__ */