ntdll: Handle the extended memory attributes in NtAllocateVirtualMemoryEx().
[wine.git] / dlls / ntdll / signal_arm64.c
blob231660367b730186c56dbfd686a682b5ef530ebc
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 TEB * (* WINAPI __wine_current_teb)(void) = NULL;
43 typedef struct _SCOPE_TABLE
45 ULONG Count;
46 struct
48 ULONG BeginAddress;
49 ULONG EndAddress;
50 ULONG HandlerAddress;
51 ULONG JumpTarget;
52 } ScopeRecord[1];
53 } SCOPE_TABLE, *PSCOPE_TABLE;
56 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
57 struct MSVCRT_JUMP_BUFFER
59 unsigned __int64 Frame;
60 unsigned __int64 Reserved;
61 unsigned __int64 X19;
62 unsigned __int64 X20;
63 unsigned __int64 X21;
64 unsigned __int64 X22;
65 unsigned __int64 X23;
66 unsigned __int64 X24;
67 unsigned __int64 X25;
68 unsigned __int64 X26;
69 unsigned __int64 X27;
70 unsigned __int64 X28;
71 unsigned __int64 Fp;
72 unsigned __int64 Lr;
73 unsigned __int64 Sp;
74 ULONG Fpcr;
75 ULONG Fpsr;
76 double D[8];
80 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
82 unsigned int i;
84 TRACE( "scope table at %p\n", table );
85 for (i = 0; i < table->Count; i++)
86 TRACE( " %u: %I64x-%I64x handler %I64x target %I64x\n", i,
87 base + table->ScopeRecord[i].BeginAddress,
88 base + table->ScopeRecord[i].EndAddress,
89 base + table->ScopeRecord[i].HandlerAddress,
90 base + table->ScopeRecord[i].JumpTarget );
93 /*******************************************************************
94 * is_valid_frame
96 static inline BOOL is_valid_frame( ULONG_PTR frame )
98 if (frame & 7) return FALSE;
99 return ((void *)frame >= NtCurrentTeb()->Tib.StackLimit &&
100 (void *)frame <= NtCurrentTeb()->Tib.StackBase);
104 /**************************************************************************
105 * __chkstk (NTDLL.@)
107 * Supposed to touch all the stack pages, but we shouldn't need that.
109 __ASM_GLOBAL_FUNC( __chkstk, "ret")
112 /***********************************************************************
113 * RtlCaptureContext (NTDLL.@)
115 __ASM_STDCALL_FUNC( RtlCaptureContext, 8,
116 "str xzr, [x0, #0x8]\n\t" /* context->X0 */
117 "stp x1, x2, [x0, #0x10]\n\t" /* context->X1,X2 */
118 "stp x3, x4, [x0, #0x20]\n\t" /* context->X3,X4 */
119 "stp x5, x6, [x0, #0x30]\n\t" /* context->X5,X6 */
120 "stp x7, x8, [x0, #0x40]\n\t" /* context->X7,X8 */
121 "stp x9, x10, [x0, #0x50]\n\t" /* context->X9,X10 */
122 "stp x11, x12, [x0, #0x60]\n\t" /* context->X11,X12 */
123 "stp x13, x14, [x0, #0x70]\n\t" /* context->X13,X14 */
124 "stp x15, x16, [x0, #0x80]\n\t" /* context->X15,X16 */
125 "stp x17, x18, [x0, #0x90]\n\t" /* context->X17,X18 */
126 "stp x19, x20, [x0, #0xa0]\n\t" /* context->X19,X20 */
127 "stp x21, x22, [x0, #0xb0]\n\t" /* context->X21,X22 */
128 "stp x23, x24, [x0, #0xc0]\n\t" /* context->X23,X24 */
129 "stp x25, x26, [x0, #0xd0]\n\t" /* context->X25,X26 */
130 "stp x27, x28, [x0, #0xe0]\n\t" /* context->X27,X28 */
131 "stp x29, xzr, [x0, #0xf0]\n\t" /* context->Fp,Lr */
132 "mov x1, sp\n\t"
133 "stp x1, x30, [x0, #0x100]\n\t" /* context->Sp,Pc */
134 "stp q0, q1, [x0, #0x110]\n\t" /* context->V[0-1] */
135 "stp q2, q3, [x0, #0x130]\n\t" /* context->V[2-3] */
136 "stp q4, q5, [x0, #0x150]\n\t" /* context->V[4-5] */
137 "stp q6, q7, [x0, #0x170]\n\t" /* context->V[6-7] */
138 "stp q8, q9, [x0, #0x190]\n\t" /* context->V[8-9] */
139 "stp q10, q11, [x0, #0x1b0]\n\t" /* context->V[10-11] */
140 "stp q12, q13, [x0, #0x1d0]\n\t" /* context->V[12-13] */
141 "stp q14, q15, [x0, #0x1f0]\n\t" /* context->V[14-15] */
142 "stp q16, q17, [x0, #0x210]\n\t" /* context->V[16-17] */
143 "stp q18, q19, [x0, #0x230]\n\t" /* context->V[18-19] */
144 "stp q20, q21, [x0, #0x250]\n\t" /* context->V[20-21] */
145 "stp q22, q23, [x0, #0x270]\n\t" /* context->V[22-23] */
146 "stp q24, q25, [x0, #0x290]\n\t" /* context->V[24-25] */
147 "stp q26, q27, [x0, #0x2b0]\n\t" /* context->V[26-27] */
148 "stp q28, q29, [x0, #0x2d0]\n\t" /* context->V[28-29] */
149 "stp q30, q31, [x0, #0x2f0]\n\t" /* context->V[30-31] */
150 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
151 "movk w1, #0x7\n\t" /* CONTEXT_FULL */
152 "str w1, [x0]\n\t" /* context->ContextFlags */
153 "mrs x1, NZCV\n\t"
154 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
155 "mrs x1, FPCR\n\t"
156 "str w1, [x0, #0x310]\n\t" /* context->Fpcr */
157 "mrs x1, FPSR\n\t"
158 "str w1, [x0, #0x314]\n\t" /* context->Fpsr */
159 "ret" )
162 /**********************************************************************
163 * virtual_unwind
165 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
167 LDR_DATA_TABLE_ENTRY *module;
168 NTSTATUS status;
169 DWORD64 pc;
171 dispatch->ImageBase = 0;
172 dispatch->ScopeIndex = 0;
173 dispatch->EstablisherFrame = 0;
174 dispatch->ControlPc = context->Pc;
176 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
177 * signal frame.
179 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
180 pc = context->Pc - (dispatch->ControlPcIsUnwound ? 4 : 0);
182 /* first look for PE exception information */
184 if ((dispatch->FunctionEntry = lookup_function_info(pc,
185 &dispatch->ImageBase, &module )))
187 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, pc,
188 dispatch->FunctionEntry, context,
189 &dispatch->HandlerData, &dispatch->EstablisherFrame,
190 NULL );
191 return STATUS_SUCCESS;
194 /* then look for host system exception information */
196 if (!module || (module->Flags & LDR_WINE_INTERNAL))
198 struct unwind_builtin_dll_params params = { type, dispatch, context };
200 status = WINE_UNIX_CALL( unix_unwind_builtin_dll, &params );
201 if (status != STATUS_SUCCESS) return status;
203 if (dispatch->EstablisherFrame)
205 dispatch->FunctionEntry = NULL;
206 if (dispatch->LanguageHandler && !module)
208 FIXME( "calling personality routine in system library not supported yet\n" );
209 dispatch->LanguageHandler = NULL;
211 return STATUS_SUCCESS;
214 else
216 status = context->Pc != context->Lr ?
217 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
218 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
219 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
220 (void*) context->Lr, status );
221 dispatch->EstablisherFrame = context->Sp;
222 dispatch->LanguageHandler = NULL;
223 context->Pc = context->Lr;
224 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
225 return status;
228 dispatch->EstablisherFrame = context->Fp;
229 dispatch->LanguageHandler = NULL;
230 context->Pc = context->Lr;
231 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
232 return STATUS_SUCCESS;
236 struct unwind_exception_frame
238 EXCEPTION_REGISTRATION_RECORD frame;
239 DISPATCHER_CONTEXT *dispatch;
242 /**********************************************************************
243 * unwind_exception_handler
245 * Handler for exceptions happening while calling an unwind handler.
247 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
248 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
250 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
251 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
253 /* copy the original dispatcher into the current one, except for the TargetIp */
254 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
255 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
256 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
257 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
258 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
259 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
260 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
261 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
262 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
263 TRACE( "detected collided unwind\n" );
264 return ExceptionCollidedUnwind;
267 /**********************************************************************
268 * call_unwind_handler
270 * Call a single unwind handler.
272 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
274 struct unwind_exception_frame frame;
275 DWORD res;
277 frame.frame.Handler = unwind_exception_handler;
278 frame.dispatch = dispatch;
279 __wine_push_frame( &frame.frame );
281 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
282 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
283 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
284 TRACE( "handler %p returned %lx\n", dispatch->LanguageHandler, res );
286 __wine_pop_frame( &frame.frame );
288 switch (res)
290 case ExceptionContinueSearch:
291 case ExceptionCollidedUnwind:
292 break;
293 default:
294 raise_status( STATUS_INVALID_DISPOSITION, rec );
295 break;
298 return res;
302 /**********************************************************************
303 * call_teb_unwind_handler
305 * Call a single unwind handler from the TEB chain.
307 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
308 EXCEPTION_REGISTRATION_RECORD *teb_frame )
310 DWORD res;
312 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
313 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
314 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
315 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
317 switch (res)
319 case ExceptionContinueSearch:
320 case ExceptionCollidedUnwind:
321 break;
322 default:
323 raise_status( STATUS_INVALID_DISPOSITION, rec );
324 break;
327 return res;
331 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
332 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
334 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
335 rec->ExceptionFlags |= EH_NESTED_CALL;
337 return ExceptionContinueSearch;
341 /**********************************************************************
342 * call_handler
344 * Call a single exception handler.
345 * FIXME: Handle nested exceptions.
347 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
349 EXCEPTION_REGISTRATION_RECORD frame;
350 DWORD res;
352 frame.Handler = nested_exception_handler;
353 __wine_push_frame( &frame );
355 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
356 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
357 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
358 TRACE( "handler at %p returned %lu\n", dispatch->LanguageHandler, res );
360 rec->ExceptionFlags &= EH_NONCONTINUABLE;
361 __wine_pop_frame( &frame );
362 return res;
366 /**********************************************************************
367 * call_teb_handler
369 * Call a single exception handler from the TEB chain.
370 * FIXME: Handle nested exceptions.
372 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
373 EXCEPTION_REGISTRATION_RECORD *teb_frame )
375 DWORD res;
377 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
378 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
379 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
380 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
381 return res;
385 /**********************************************************************
386 * call_function_handlers
388 * Call the per-function handlers.
390 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
392 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
393 UNWIND_HISTORY_TABLE table;
394 DISPATCHER_CONTEXT dispatch;
395 CONTEXT context, prev_context;
396 NTSTATUS status;
398 context = *orig_context;
399 dispatch.TargetPc = 0;
400 dispatch.ContextRecord = &context;
401 dispatch.HistoryTable = &table;
402 prev_context = context;
403 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.X19;
405 for (;;)
407 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
408 if (status != STATUS_SUCCESS) return status;
410 unwind_done:
411 if (!dispatch.EstablisherFrame) break;
413 if (!is_valid_frame( dispatch.EstablisherFrame ))
415 ERR( "invalid frame %I64x (%p-%p)\n", dispatch.EstablisherFrame,
416 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
417 rec->ExceptionFlags |= EH_STACK_INVALID;
418 break;
421 if (dispatch.LanguageHandler)
423 switch (call_handler( rec, orig_context, &dispatch ))
425 case ExceptionContinueExecution:
426 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
427 return STATUS_SUCCESS;
428 case ExceptionContinueSearch:
429 break;
430 case ExceptionNestedException:
431 FIXME( "nested exception\n" );
432 break;
433 case ExceptionCollidedUnwind: {
434 ULONG64 frame;
436 context = *dispatch.ContextRecord;
437 dispatch.ContextRecord = &context;
438 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
439 dispatch.ControlPc, dispatch.FunctionEntry,
440 &context, &dispatch.HandlerData, &frame, NULL );
441 goto unwind_done;
443 default:
444 return STATUS_INVALID_DISPOSITION;
447 /* hack: call wine handlers registered in the tib list */
448 else while ((ULONG64)teb_frame < context.Sp)
450 TRACE( "found wine frame %p rsp %I64x handler %p\n",
451 teb_frame, context.Sp, teb_frame->Handler );
452 dispatch.EstablisherFrame = (ULONG64)teb_frame;
453 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
455 case ExceptionContinueExecution:
456 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
457 return STATUS_SUCCESS;
458 case ExceptionContinueSearch:
459 break;
460 case ExceptionNestedException:
461 FIXME( "nested exception\n" );
462 break;
463 case ExceptionCollidedUnwind: {
464 ULONG64 frame;
466 context = *dispatch.ContextRecord;
467 dispatch.ContextRecord = &context;
468 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
469 dispatch.ControlPc, dispatch.FunctionEntry,
470 &context, &dispatch.HandlerData, &frame, NULL );
471 teb_frame = teb_frame->Prev;
472 goto unwind_done;
474 default:
475 return STATUS_INVALID_DISPOSITION;
477 teb_frame = teb_frame->Prev;
480 if (context.Sp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
481 prev_context = context;
483 return STATUS_UNHANDLED_EXCEPTION;
487 /*******************************************************************
488 * KiUserExceptionDispatcher (NTDLL.@)
490 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
492 NTSTATUS status;
493 DWORD c;
495 if (pWow64PrepareForException) pWow64PrepareForException( rec, context );
497 TRACE( "code=%lx flags=%lx addr=%p pc=%016I64x\n",
498 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Pc );
499 for (c = 0; c < rec->NumberParameters; c++)
500 TRACE( " info[%ld]=%016I64x\n", c, rec->ExceptionInformation[c] );
502 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
504 if (rec->ExceptionInformation[1] >> 16)
505 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
506 rec->ExceptionAddress,
507 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
508 else
509 MESSAGE( "wine: Call from %p to unimplemented function %s.%Id, aborting\n",
510 rec->ExceptionAddress,
511 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
513 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
515 if ((DWORD)rec->ExceptionInformation[2] == -1 || (DWORD)rec->ExceptionInformation[2] == GetCurrentThreadId())
516 WARN_(threadname)( "Thread renamed to %s\n", debugstr_a((char *)rec->ExceptionInformation[1]) );
517 else
518 WARN_(threadname)( "Thread ID %04lx renamed to %s\n", (DWORD)rec->ExceptionInformation[2],
519 debugstr_a((char *)rec->ExceptionInformation[1]) );
521 set_native_thread_name((DWORD)rec->ExceptionInformation[2], (char *)rec->ExceptionInformation[1]);
523 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
525 WARN( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
527 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
529 WARN( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
531 else
533 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
534 ERR( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
535 else
536 WARN( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
538 TRACE(" x0=%016I64x x1=%016I64x x2=%016I64x x3=%016I64x\n",
539 context->X0, context->X1, context->X2, context->X3 );
540 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
541 context->X4, context->X5, context->X6, context->X7 );
542 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
543 context->X8, context->X9, context->X10, context->X11 );
544 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
545 context->X12, context->X13, context->X14, context->X15 );
546 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
547 context->X16, context->X17, context->X18, context->X19 );
548 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
549 context->X20, context->X21, context->X22, context->X23 );
550 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
551 context->X24, context->X25, context->X26, context->X27 );
552 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
553 context->X28, context->Fp, context->Lr, context->Sp );
556 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
557 NtContinue( context, FALSE );
559 if ((status = call_function_handlers( rec, context )) == STATUS_SUCCESS)
560 NtContinue( context, FALSE );
562 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
563 return NtRaiseException( rec, context, FALSE );
567 /*******************************************************************
568 * KiUserApcDispatcher (NTDLL.@)
570 void WINAPI KiUserApcDispatcher( CONTEXT *context, ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3,
571 PNTAPCFUNC apc )
573 void (CALLBACK *func)(ULONG_PTR,ULONG_PTR,ULONG_PTR,CONTEXT*) = (void *)apc;
574 func( arg1, arg2, arg3, context );
575 NtContinue( context, TRUE );
579 /*******************************************************************
580 * KiUserCallbackDispatcher (NTDLL.@)
582 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
584 NTSTATUS status;
586 __TRY
588 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
589 status = NtCallbackReturn( NULL, 0, func( args, len ));
591 __EXCEPT_ALL
593 ERR_(seh)( "ignoring exception\n" );
594 status = NtCallbackReturn( 0, 0, 0 );
596 __ENDTRY
598 RtlRaiseStatus( status );
602 /***********************************************************************
603 * Definitions for Win32 unwind tables
606 struct unwind_info
608 DWORD function_length : 18;
609 DWORD version : 2;
610 DWORD x : 1;
611 DWORD e : 1;
612 DWORD epilog : 5;
613 DWORD codes : 5;
616 struct unwind_info_ext
618 WORD epilog;
619 BYTE codes;
620 BYTE reserved;
623 struct unwind_info_epilog
625 DWORD offset : 18;
626 DWORD res : 4;
627 DWORD index : 10;
630 static const BYTE unwind_code_len[256] =
632 /* 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,
633 /* 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,
634 /* 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,
635 /* 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,
636 /* 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,
637 /* 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,
638 /* 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,
639 /* 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
642 /***********************************************************************
643 * get_sequence_len
645 static unsigned int get_sequence_len( BYTE *ptr, BYTE *end )
647 unsigned int ret = 0;
649 while (ptr < end)
651 if (*ptr == 0xe4 || *ptr == 0xe5) break;
652 ptr += unwind_code_len[*ptr];
653 ret++;
655 return ret;
659 /***********************************************************************
660 * restore_regs
662 static void restore_regs( int reg, int count, int pos, CONTEXT *context,
663 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
665 int i, offset = max( 0, pos );
666 for (i = 0; i < count; i++)
668 if (ptrs && reg + i >= 19) (&ptrs->X19)[reg + i - 19] = (DWORD64 *)context->Sp + i + offset;
669 context->X[reg + i] = ((DWORD64 *)context->Sp)[i + offset];
671 if (pos < 0) context->Sp += -8 * pos;
675 /***********************************************************************
676 * restore_fpregs
678 static void restore_fpregs( int reg, int count, int pos, CONTEXT *context,
679 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
681 int i, offset = max( 0, pos );
682 for (i = 0; i < count; i++)
684 if (ptrs && reg + i >= 8) (&ptrs->D8)[reg + i - 8] = (DWORD64 *)context->Sp + i + offset;
685 context->V[reg + i].D[0] = ((double *)context->Sp)[i + offset];
687 if (pos < 0) context->Sp += -8 * pos;
691 static void do_pac_auth( CONTEXT *context )
693 register DWORD64 x17 __asm__( "x17" ) = context->Lr;
694 register DWORD64 x16 __asm__( "x16" ) = context->Sp;
696 /* This is the autib1716 instruction. The hint instruction is used here
697 * as gcc does not assemble autib1716 for pre armv8.3a targets. For
698 * pre-armv8.3a targets, this is just treated as a hint instruction, which
699 * is ignored. */
700 __asm__( "hint 0xe" : "+r"(x17) : "r"(x16) );
702 context->Lr = x17;
705 /***********************************************************************
706 * process_unwind_codes
708 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
709 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
711 unsigned int val, len, save_next = 2;
713 /* skip codes */
714 while (ptr < end && skip)
716 if (*ptr == 0xe4 || *ptr == 0xe5) break;
717 ptr += unwind_code_len[*ptr];
718 skip--;
721 while (ptr < end)
723 if ((len = unwind_code_len[*ptr]) > 1)
725 if (ptr + len > end) break;
726 val = ptr[0] * 0x100 + ptr[1];
728 else val = *ptr;
730 if (*ptr < 0x20) /* alloc_s */
731 context->Sp += 16 * (val & 0x1f);
732 else if (*ptr < 0x40) /* save_r19r20_x */
733 restore_regs( 19, save_next, -(val & 0x1f), context, ptrs );
734 else if (*ptr < 0x80) /* save_fplr */
735 restore_regs( 29, 2, val & 0x3f, context, ptrs );
736 else if (*ptr < 0xc0) /* save_fplr_x */
737 restore_regs( 29, 2, -(val & 0x3f) - 1, context, ptrs );
738 else if (*ptr < 0xc8) /* alloc_m */
739 context->Sp += 16 * (val & 0x7ff);
740 else if (*ptr < 0xcc) /* save_regp */
741 restore_regs( 19 + ((val >> 6) & 0xf), save_next, val & 0x3f, context, ptrs );
742 else if (*ptr < 0xd0) /* save_regp_x */
743 restore_regs( 19 + ((val >> 6) & 0xf), save_next, -(val & 0x3f) - 1, context, ptrs );
744 else if (*ptr < 0xd4) /* save_reg */
745 restore_regs( 19 + ((val >> 6) & 0xf), 1, val & 0x3f, context, ptrs );
746 else if (*ptr < 0xd6) /* save_reg_x */
747 restore_regs( 19 + ((val >> 5) & 0xf), 1, -(val & 0x1f) - 1, context, ptrs );
748 else if (*ptr < 0xd8) /* save_lrpair */
750 restore_regs( 19 + 2 * ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
751 restore_regs( 30, 1, (val & 0x3f) + 1, context, ptrs );
753 else if (*ptr < 0xda) /* save_fregp */
754 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, val & 0x3f, context, ptrs );
755 else if (*ptr < 0xdc) /* save_fregp_x */
756 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, -(val & 0x3f) - 1, context, ptrs );
757 else if (*ptr < 0xde) /* save_freg */
758 restore_fpregs( 8 + ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
759 else if (*ptr == 0xde) /* save_freg_x */
760 restore_fpregs( 8 + ((val >> 5) & 0x7), 1, -(val & 0x3f) - 1, context, ptrs );
761 else if (*ptr == 0xe0) /* alloc_l */
762 context->Sp += 16 * ((ptr[1] << 16) + (ptr[2] << 8) + ptr[3]);
763 else if (*ptr == 0xe1) /* set_fp */
764 context->Sp = context->Fp;
765 else if (*ptr == 0xe2) /* add_fp */
766 context->Sp = context->Fp - 8 * (val & 0xff);
767 else if (*ptr == 0xe3) /* nop */
768 /* nop */ ;
769 else if (*ptr == 0xe4) /* end */
770 break;
771 else if (*ptr == 0xe5) /* end_c */
772 break;
773 else if (*ptr == 0xe6) /* save_next */
775 save_next += 2;
776 ptr += len;
777 continue;
779 else if (*ptr == 0xe9) /* MSFT_OP_MACHINE_FRAME */
781 context->Pc = ((DWORD64 *)context->Sp)[1];
782 context->Sp = ((DWORD64 *)context->Sp)[0];
784 else if (*ptr == 0xea) /* MSFT_OP_CONTEXT */
786 memcpy( context, (DWORD64 *)context->Sp, sizeof(CONTEXT) );
788 else if (*ptr == 0xfc) /* pac_sign_lr */
790 do_pac_auth( context );
792 else
794 WARN( "unsupported code %02x\n", *ptr );
795 return;
797 save_next = 2;
798 ptr += len;
803 /***********************************************************************
804 * unwind_packed_data
806 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
807 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
809 int i;
810 unsigned int len, offset, skip = 0;
811 unsigned int int_size = func->RegI * 8, fp_size = func->RegF * 8, regsave, local_size;
812 unsigned int int_regs, fp_regs, saved_regs, local_size_regs;
814 TRACE( "function %I64x-%I64x: len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
815 base + func->BeginAddress, base + func->BeginAddress + func->FunctionLength * 4,
816 func->FunctionLength, func->Flag, func->RegF, func->RegI, func->H, func->CR, func->FrameSize );
818 if (func->CR == 1) int_size += 8;
819 if (func->RegF) fp_size += 8;
821 regsave = ((int_size + fp_size + 8 * 8 * func->H) + 0xf) & ~0xf;
822 local_size = func->FrameSize * 16 - regsave;
824 int_regs = int_size / 8;
825 fp_regs = fp_size / 8;
826 saved_regs = regsave / 8;
827 local_size_regs = local_size / 8;
829 /* check for prolog/epilog */
830 if (func->Flag == 1)
832 offset = ((pc - base) - func->BeginAddress) / 4;
833 if (offset < 17 || offset >= func->FunctionLength - 15)
835 len = (int_size + 8) / 16 + (fp_size + 8) / 16;
836 switch (func->CR)
838 case 2:
839 len++; /* pacibsp */
840 /* fall through */
841 case 3:
842 len++; /* mov x29,sp */
843 len++; /* stp x29,lr,[sp,0] */
844 if (local_size <= 512) break;
845 /* fall through */
846 case 0:
847 case 1:
848 if (local_size) len++; /* sub sp,sp,#local_size */
849 if (local_size > 4088) len++; /* sub sp,sp,#4088 */
850 break;
852 len += 4 * func->H;
853 if (offset < len) /* prolog */
855 skip = len - offset;
857 else if (offset >= func->FunctionLength - (len + 1)) /* epilog */
859 skip = offset - (func->FunctionLength - (len + 1));
864 if (!skip)
866 if (func->CR == 3 || func->CR == 2)
868 DWORD64 *fp = (DWORD64 *) context->Fp; /* X[29] */
869 context->Sp = context->Fp;
870 context->X[29] = fp[0];
871 context->X[30] = fp[1];
873 context->Sp += local_size;
874 if (fp_size) restore_fpregs( 8, fp_regs, int_regs, context, ptrs );
875 if (func->CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
876 restore_regs( 19, func->RegI, -saved_regs, context, ptrs );
878 else
880 unsigned int pos = 0;
882 switch (func->CR)
884 case 3:
885 case 2:
886 /* mov x29,sp */
887 if (pos++ >= skip) context->Sp = context->Fp;
888 if (local_size <= 512)
890 /* stp x29,lr,[sp,-#local_size]! */
891 if (pos++ >= skip) restore_regs( 29, 2, -local_size_regs, context, ptrs );
892 break;
894 /* stp x29,lr,[sp,0] */
895 if (pos++ >= skip) restore_regs( 29, 2, 0, context, ptrs );
896 /* fall through */
897 case 0:
898 case 1:
899 if (!local_size) break;
900 /* sub sp,sp,#local_size */
901 if (pos++ >= skip) context->Sp += (local_size - 1) % 4088 + 1;
902 if (local_size > 4088 && pos++ >= skip) context->Sp += 4088;
903 break;
906 if (func->H) pos += 4;
908 if (fp_size)
910 if (func->RegF % 2 == 0 && pos++ >= skip)
911 /* str d%u,[sp,#fp_size] */
912 restore_fpregs( 8 + func->RegF, 1, int_regs + fp_regs - 1, context, ptrs );
913 for (i = (func->RegF + 1) / 2 - 1; i >= 0; i--)
915 if (pos++ < skip) continue;
916 if (!i && !int_size)
917 /* stp d8,d9,[sp,-#regsave]! */
918 restore_fpregs( 8, 2, -saved_regs, context, ptrs );
919 else
920 /* stp dn,dn+1,[sp,#offset] */
921 restore_fpregs( 8 + 2 * i, 2, int_regs + 2 * i, context, ptrs );
925 if (func->RegI % 2)
927 if (pos++ >= skip)
929 /* stp xn,lr,[sp,#offset] */
930 if (func->CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
931 /* str xn,[sp,#offset] */
932 restore_regs( 18 + func->RegI, 1,
933 (func->RegI > 1) ? func->RegI - 1 : -saved_regs,
934 context, ptrs );
937 else if (func->CR == 1)
939 /* str lr,[sp,#offset] */
940 if (pos++ >= skip) restore_regs( 30, 1, func->RegI ? int_regs - 1 : -saved_regs, context, ptrs );
943 for (i = func->RegI / 2 - 1; i >= 0; i--)
945 if (pos++ < skip) continue;
946 if (i)
947 /* stp xn,xn+1,[sp,#offset] */
948 restore_regs( 19 + 2 * i, 2, 2 * i, context, ptrs );
949 else
950 /* stp x19,x20,[sp,-#regsave]! */
951 restore_regs( 19, 2, -saved_regs, context, ptrs );
954 if (func->CR == 2) do_pac_auth( context );
955 return NULL;
959 /***********************************************************************
960 * unwind_full_data
962 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
963 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
965 struct unwind_info *info;
966 struct unwind_info_epilog *info_epilog;
967 unsigned int i, codes, epilogs, len, offset;
968 void *data;
969 BYTE *end;
971 info = (struct unwind_info *)((char *)base + func->UnwindData);
972 data = info + 1;
973 epilogs = info->epilog;
974 codes = info->codes;
975 if (!codes && !epilogs)
977 struct unwind_info_ext *infoex = data;
978 codes = infoex->codes;
979 epilogs = infoex->epilog;
980 data = infoex + 1;
982 info_epilog = data;
983 if (!info->e) data = info_epilog + epilogs;
985 offset = ((pc - base) - func->BeginAddress) / 4;
986 end = (BYTE *)data + codes * 4;
988 TRACE( "function %I64x-%I64x: len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
989 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 4,
990 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
992 /* check for prolog */
993 if (offset < codes * 4)
995 len = get_sequence_len( data, end );
996 if (offset < len)
998 process_unwind_codes( data, end, context, ptrs, len - offset );
999 return NULL;
1003 /* check for epilog */
1004 if (!info->e)
1006 for (i = 0; i < epilogs; i++)
1008 if (offset < info_epilog[i].offset) break;
1009 if (offset - info_epilog[i].offset < codes * 4 - info_epilog[i].index)
1011 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
1012 len = get_sequence_len( ptr, end );
1013 if (offset <= info_epilog[i].offset + len)
1015 process_unwind_codes( ptr, end, context, ptrs, offset - info_epilog[i].offset );
1016 return NULL;
1021 else if (info->function_length - offset <= codes * 4 - epilogs)
1023 BYTE *ptr = (BYTE *)data + epilogs;
1024 len = get_sequence_len( ptr, end ) + 1;
1025 if (offset >= info->function_length - len)
1027 process_unwind_codes( ptr, end, context, ptrs, offset - (info->function_length - len) );
1028 return NULL;
1032 process_unwind_codes( data, end, context, ptrs, 0 );
1034 /* get handler since we are inside the main code */
1035 if (info->x)
1037 DWORD *handler_rva = (DWORD *)data + codes;
1038 *handler_data = handler_rva + 1;
1039 return (char *)base + *handler_rva;
1041 return NULL;
1045 /**********************************************************************
1046 * RtlVirtualUnwind (NTDLL.@)
1048 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
1049 RUNTIME_FUNCTION *func, CONTEXT *context,
1050 PVOID *handler_data, ULONG_PTR *frame_ret,
1051 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
1053 void *handler;
1055 TRACE( "type %lx pc %I64x sp %I64x func %I64x\n", type, pc, context->Sp, base + func->BeginAddress );
1057 *handler_data = NULL;
1059 context->Pc = 0;
1060 if (func->Flag)
1061 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1062 else
1063 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1065 TRACE( "ret: lr=%I64x sp=%I64x handler=%p\n", context->Lr, context->Sp, handler );
1066 if (!context->Pc)
1067 context->Pc = context->Lr;
1068 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1069 *frame_ret = context->Sp;
1070 return handler;
1073 /**********************************************************************
1074 * call_consolidate_callback
1076 * Wrapper function to call a consolidate callback from a fake frame.
1077 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1078 * we have to skip all frames which were already processed. To do that we
1079 * trick the unwinding functions into thinking the call came from somewhere
1080 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1081 * DW_CFA_expression, and the expressions have the following format:
1083 * DW_OP_breg31; sleb128 <OFFSET> | Load x31 + struct member offset
1084 * [DW_OP_deref] | Dereference, only for CFA
1086 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1087 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1088 EXCEPTION_RECORD *rec,
1089 TEB *teb );
1090 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1091 "stp x29, x30, [sp, #-0x30]!\n\t"
1092 __ASM_CFI(".cfi_def_cfa_offset 48\n\t")
1093 __ASM_CFI(".cfi_offset 29, -48\n\t")
1094 __ASM_CFI(".cfi_offset 30, -40\n\t")
1095 __ASM_SEH(".seh_nop\n\t")
1096 "stp x1, x2, [sp, #0x10]\n\t"
1097 __ASM_SEH(".seh_nop\n\t")
1098 "str x3, [sp, #0x20]\n\t"
1099 __ASM_SEH(".seh_nop\n\t")
1100 "mov x29, sp\n\t"
1101 __ASM_CFI(".cfi_def_cfa_register 29\n\t")
1102 __ASM_SEH(".seh_nop\n\t")
1103 __ASM_CFI(".cfi_remember_state\n\t")
1104 /* Memcpy the context onto the stack */
1105 "sub sp, sp, #0x390\n\t"
1106 __ASM_SEH(".seh_nop\n\t")
1107 "mov x1, x0\n\t"
1108 __ASM_SEH(".seh_nop\n\t")
1109 "mov x0, sp\n\t"
1110 __ASM_SEH(".seh_nop\n\t")
1111 "mov x2, #0x390\n\t"
1112 __ASM_SEH(".seh_nop\n\t")
1113 "bl " __ASM_NAME("memcpy") "\n\t"
1114 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1115 __ASM_CFI(".cfi_escape 0x0f,0x04,0x8f,0x80,0x02,0x06\n\t") /* CFA, DW_OP_breg31 + 0x100, DW_OP_deref */
1116 __ASM_CFI(".cfi_escape 0x10,0x13,0x03,0x8f,0xa0,0x01\n\t") /* x19, DW_OP_breg31 + 0xA0 */
1117 __ASM_CFI(".cfi_escape 0x10,0x14,0x03,0x8f,0xa8,0x01\n\t") /* x20 */
1118 __ASM_CFI(".cfi_escape 0x10,0x15,0x03,0x8f,0xb0,0x01\n\t") /* x21 */
1119 __ASM_CFI(".cfi_escape 0x10,0x16,0x03,0x8f,0xb8,0x01\n\t") /* x22 */
1120 __ASM_CFI(".cfi_escape 0x10,0x17,0x03,0x8f,0xc0,0x01\n\t") /* x23 */
1121 __ASM_CFI(".cfi_escape 0x10,0x18,0x03,0x8f,0xc8,0x01\n\t") /* x24 */
1122 __ASM_CFI(".cfi_escape 0x10,0x19,0x03,0x8f,0xd0,0x01\n\t") /* x25 */
1123 __ASM_CFI(".cfi_escape 0x10,0x1a,0x03,0x8f,0xd8,0x01\n\t") /* x26 */
1124 __ASM_CFI(".cfi_escape 0x10,0x1b,0x03,0x8f,0xe0,0x01\n\t") /* x27 */
1125 __ASM_CFI(".cfi_escape 0x10,0x1c,0x03,0x8f,0xe8,0x01\n\t") /* x28 */
1126 __ASM_CFI(".cfi_escape 0x10,0x1d,0x03,0x8f,0xf0,0x01\n\t") /* x29 */
1127 __ASM_CFI(".cfi_escape 0x10,0x1e,0x03,0x8f,0xf8,0x01\n\t") /* x30 */
1128 __ASM_CFI(".cfi_escape 0x10,0x48,0x03,0x8f,0x90,0x03\n\t") /* d8 */
1129 __ASM_CFI(".cfi_escape 0x10,0x49,0x03,0x8f,0x98,0x03\n\t") /* d9 */
1130 __ASM_CFI(".cfi_escape 0x10,0x4a,0x03,0x8f,0xa0,0x03\n\t") /* d10 */
1131 __ASM_CFI(".cfi_escape 0x10,0x4b,0x03,0x8f,0xa8,0x03\n\t") /* d11 */
1132 __ASM_CFI(".cfi_escape 0x10,0x4c,0x03,0x8f,0xb0,0x03\n\t") /* d12 */
1133 __ASM_CFI(".cfi_escape 0x10,0x4d,0x03,0x8f,0xb8,0x03\n\t") /* d13 */
1134 __ASM_CFI(".cfi_escape 0x10,0x4e,0x03,0x8f,0xc0,0x03\n\t") /* d14 */
1135 __ASM_CFI(".cfi_escape 0x10,0x4f,0x03,0x8f,0xc8,0x03\n\t") /* d15 */
1136 __ASM_SEH(".seh_context\n\t")
1137 __ASM_SEH(".seh_endprologue\n\t")
1138 "ldp x1, x2, [x29, #0x10]\n\t"
1139 "ldr x18, [x29, #0x20]\n\t"
1140 "mov x0, x2\n\t"
1141 "blr x1\n\t"
1142 "mov sp, x29\n\t"
1143 __ASM_CFI(".cfi_restore_state\n\t")
1144 "ldp x29, x30, [sp], #48\n\t"
1145 __ASM_CFI(".cfi_restore 30\n\t")
1146 __ASM_CFI(".cfi_restore 29\n\t")
1147 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1148 "ret")
1150 /*******************************************************************
1151 * RtlRestoreContext (NTDLL.@)
1153 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1155 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1157 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1159 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1160 int i;
1162 context->X19 = jmp->X19;
1163 context->X20 = jmp->X20;
1164 context->X21 = jmp->X21;
1165 context->X22 = jmp->X22;
1166 context->X23 = jmp->X23;
1167 context->X24 = jmp->X24;
1168 context->X25 = jmp->X25;
1169 context->X26 = jmp->X26;
1170 context->X27 = jmp->X27;
1171 context->X28 = jmp->X28;
1172 context->Fp = jmp->Fp;
1173 context->Lr = jmp->Lr;
1174 context->Sp = jmp->Sp;
1175 context->Fpcr = jmp->Fpcr;
1176 context->Fpsr = jmp->Fpsr;
1178 for (i = 0; i < 8; i++)
1179 context->V[8+i].D[0] = jmp->D[i];
1181 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1183 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1184 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1185 rec->ExceptionInformation[10] = (ULONG_PTR)&context->X19;
1187 context->Pc = (ULONG64)call_consolidate_callback( context, consolidate, rec, NtCurrentTeb() );
1190 /* hack: remove no longer accessible TEB frames */
1191 while ((ULONG64)teb_frame < context->Sp)
1193 TRACE( "removing TEB frame: %p\n", teb_frame );
1194 teb_frame = __wine_pop_frame( teb_frame );
1197 TRACE( "returning to %I64x stack %I64x\n", context->Pc, context->Sp );
1198 NtContinue( context, FALSE );
1201 /*******************************************************************
1202 * RtlUnwindEx (NTDLL.@)
1204 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1205 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1207 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1208 EXCEPTION_RECORD record;
1209 DISPATCHER_CONTEXT dispatch;
1210 CONTEXT new_context;
1211 NTSTATUS status;
1212 DWORD i;
1214 RtlCaptureContext( context );
1215 new_context = *context;
1217 /* build an exception record, if we do not have one */
1218 if (!rec)
1220 record.ExceptionCode = STATUS_UNWIND;
1221 record.ExceptionFlags = 0;
1222 record.ExceptionRecord = NULL;
1223 record.ExceptionAddress = (void *)context->Pc;
1224 record.NumberParameters = 0;
1225 rec = &record;
1228 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1230 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%016I64x\n",
1231 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1232 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1233 TRACE( " info[%ld]=%016I64x\n", i, rec->ExceptionInformation[i] );
1234 TRACE(" x0=%016I64x x1=%016I64x x2=%016I64x x3=%016I64x\n",
1235 context->X0, context->X1, context->X2, context->X3 );
1236 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
1237 context->X4, context->X5, context->X6, context->X7 );
1238 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
1239 context->X8, context->X9, context->X10, context->X11 );
1240 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
1241 context->X12, context->X13, context->X14, context->X15 );
1242 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
1243 context->X16, context->X17, context->X18, context->X19 );
1244 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
1245 context->X20, context->X21, context->X22, context->X23 );
1246 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
1247 context->X24, context->X25, context->X26, context->X27 );
1248 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
1249 context->X28, context->Fp, context->Lr, context->Sp );
1251 dispatch.TargetPc = (ULONG64)target_ip;
1252 dispatch.ContextRecord = context;
1253 dispatch.HistoryTable = table;
1254 dispatch.NonVolatileRegisters = (BYTE *)&context->X19;
1256 for (;;)
1258 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1259 if (status != STATUS_SUCCESS) raise_status( status, rec );
1261 unwind_done:
1262 if (!dispatch.EstablisherFrame) break;
1264 if (!is_valid_frame( dispatch.EstablisherFrame ))
1266 ERR( "invalid frame %I64x (%p-%p)\n", dispatch.EstablisherFrame,
1267 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1268 rec->ExceptionFlags |= EH_STACK_INVALID;
1269 break;
1272 if (dispatch.LanguageHandler)
1274 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
1276 ERR( "invalid end frame %I64x/%p\n", dispatch.EstablisherFrame, end_frame );
1277 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1279 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1280 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1282 ULONG64 frame;
1284 *context = new_context = *dispatch.ContextRecord;
1285 dispatch.ContextRecord = context;
1286 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1287 dispatch.ControlPc, dispatch.FunctionEntry,
1288 &new_context, &dispatch.HandlerData, &frame,
1289 NULL );
1290 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1291 goto unwind_done;
1293 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1295 else /* hack: call builtin handlers registered in the tib list */
1297 DWORD64 backup_frame = dispatch.EstablisherFrame;
1298 while ((ULONG64)teb_frame < new_context.Sp && (ULONG64)teb_frame < (ULONG64)end_frame)
1300 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1301 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1302 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1304 ULONG64 frame;
1306 teb_frame = __wine_pop_frame( teb_frame );
1308 *context = new_context = *dispatch.ContextRecord;
1309 dispatch.ContextRecord = context;
1310 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1311 dispatch.ControlPc, dispatch.FunctionEntry,
1312 &new_context, &dispatch.HandlerData,
1313 &frame, NULL );
1314 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1315 goto unwind_done;
1317 teb_frame = __wine_pop_frame( teb_frame );
1319 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Sp) break;
1320 dispatch.EstablisherFrame = backup_frame;
1323 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
1324 *context = new_context;
1327 context->X0 = (ULONG64)retval;
1328 context->Pc = (ULONG64)target_ip;
1329 RtlRestoreContext(context, rec);
1333 /***********************************************************************
1334 * RtlUnwind (NTDLL.@)
1336 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1338 CONTEXT context;
1339 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1342 /*******************************************************************
1343 * _local_unwind (NTDLL.@)
1345 void WINAPI _local_unwind( void *frame, void *target_ip )
1347 CONTEXT context;
1348 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1351 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1352 PEXCEPTION_FILTER filter,
1353 PUCHAR nonvolatile);
1354 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1355 "stp x29, x30, [sp, #-96]!\n\t"
1356 __ASM_SEH(".seh_save_fplr_x 96\n\t")
1357 "stp x19, x20, [sp, #16]\n\t"
1358 __ASM_SEH(".seh_save_regp x19, 16\n\t")
1359 "stp x21, x22, [sp, #32]\n\t"
1360 __ASM_SEH(".seh_save_regp x21, 32\n\t")
1361 "stp x23, x24, [sp, #48]\n\t"
1362 __ASM_SEH(".seh_save_regp x23, 48\n\t")
1363 "stp x25, x26, [sp, #64]\n\t"
1364 __ASM_SEH(".seh_save_regp x25, 64\n\t")
1365 "stp x27, x28, [sp, #80]\n\t"
1366 __ASM_SEH(".seh_save_regp x27, 80\n\t")
1367 "mov x29, sp\n\t"
1368 __ASM_SEH(".seh_set_fp\n\t")
1369 __ASM_SEH(".seh_endprologue\n\t")
1371 __ASM_CFI(".cfi_def_cfa x29, 96\n\t")
1372 __ASM_CFI(".cfi_offset x29, -96\n\t")
1373 __ASM_CFI(".cfi_offset x30, -88\n\t")
1374 __ASM_CFI(".cfi_offset x19, -80\n\t")
1375 __ASM_CFI(".cfi_offset x20, -72\n\t")
1376 __ASM_CFI(".cfi_offset x21, -64\n\t")
1377 __ASM_CFI(".cfi_offset x22, -56\n\t")
1378 __ASM_CFI(".cfi_offset x23, -48\n\t")
1379 __ASM_CFI(".cfi_offset x24, -40\n\t")
1380 __ASM_CFI(".cfi_offset x25, -32\n\t")
1381 __ASM_CFI(".cfi_offset x26, -24\n\t")
1382 __ASM_CFI(".cfi_offset x27, -16\n\t")
1383 __ASM_CFI(".cfi_offset x28, -8\n\t")
1385 "ldp x19, x20, [x3, #0]\n\t"
1386 "ldp x21, x22, [x3, #16]\n\t"
1387 "ldp x23, x24, [x3, #32]\n\t"
1388 "ldp x25, x26, [x3, #48]\n\t"
1389 "ldp x27, x28, [x3, #64]\n\t"
1390 /* Overwrite the frame parameter with Fp from the
1391 * nonvolatile regs */
1392 "ldr x1, [x3, #80]\n\t"
1393 "blr x2\n\t"
1394 "ldp x19, x20, [sp, #16]\n\t"
1395 "ldp x21, x22, [sp, #32]\n\t"
1396 "ldp x23, x24, [sp, #48]\n\t"
1397 "ldp x25, x26, [sp, #64]\n\t"
1398 "ldp x27, x28, [sp, #80]\n\t"
1399 "ldp x29, x30, [sp], #96\n\t"
1400 "ret")
1402 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1403 PTERMINATION_HANDLER handler,
1404 PUCHAR nonvolatile);
1405 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1406 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1407 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1409 /*******************************************************************
1410 * __C_specific_handler (NTDLL.@)
1412 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1413 void *frame,
1414 CONTEXT *context,
1415 struct _DISPATCHER_CONTEXT *dispatch )
1417 SCOPE_TABLE *table = dispatch->HandlerData;
1418 ULONG i;
1419 DWORD64 ControlPc = dispatch->ControlPc;
1421 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1422 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1424 if (dispatch->ControlPcIsUnwound)
1425 ControlPc -= 4;
1427 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1429 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1431 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1432 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1434 PTERMINATION_HANDLER handler;
1436 if (table->ScopeRecord[i].JumpTarget) continue;
1438 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1439 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1440 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1442 break;
1445 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1446 dispatch->ScopeIndex = i+1;
1448 TRACE( "calling __finally %p frame %p\n", handler, frame );
1449 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1450 dispatch->NonVolatileRegisters );
1453 return ExceptionContinueSearch;
1456 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1458 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1459 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1461 if (!table->ScopeRecord[i].JumpTarget) continue;
1462 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1464 EXCEPTION_POINTERS ptrs;
1465 PEXCEPTION_FILTER filter;
1467 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1468 ptrs.ExceptionRecord = rec;
1469 ptrs.ContextRecord = context;
1470 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1471 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1472 dispatch->NonVolatileRegisters ))
1474 case EXCEPTION_EXECUTE_HANDLER:
1475 break;
1476 case EXCEPTION_CONTINUE_SEARCH:
1477 continue;
1478 case EXCEPTION_CONTINUE_EXECUTION:
1479 return ExceptionContinueExecution;
1482 TRACE( "unwinding to target %I64x\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1483 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1484 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1487 return ExceptionContinueSearch;
1491 /***********************************************************************
1492 * RtlRaiseException (NTDLL.@)
1494 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
1495 "sub sp, sp, #0x3b0\n\t" /* 0x390 (context) + 0x20 */
1496 "stp x29, x30, [sp]\n\t"
1497 __ASM_SEH(".seh_stackalloc 0x3b0\n\t")
1498 __ASM_SEH(".seh_save_fplr 0\n\t")
1499 __ASM_SEH(".seh_endprologue\n\t")
1500 __ASM_CFI(".cfi_def_cfa x29, 944\n\t")
1501 __ASM_CFI(".cfi_offset x30, -936\n\t")
1502 __ASM_CFI(".cfi_offset x29, -944\n\t")
1503 "mov x29, sp\n\t"
1504 "str x0, [sp, #0x10]\n\t"
1505 "add x0, sp, #0x20\n\t"
1506 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1507 "add x1, sp, #0x20\n\t" /* context pointer */
1508 "add x2, sp, #0x3b0\n\t" /* orig stack pointer */
1509 "str x2, [x1, #0x100]\n\t" /* context->Sp */
1510 "ldr x0, [sp, #0x10]\n\t" /* original first parameter */
1511 "str x0, [x1, #0x08]\n\t" /* context->X0 */
1512 "ldp x4, x5, [sp]\n\t" /* frame pointer, return address */
1513 "stp x4, x5, [x1, #0xf0]\n\t" /* context->Fp, Lr */
1514 "str x5, [x1, #0x108]\n\t" /* context->Pc */
1515 "str x5, [x0, #0x10]\n\t" /* rec->ExceptionAddress */
1516 "mov x2, #1\n\t"
1517 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1518 "bl " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1520 /*************************************************************************
1521 * RtlCaptureStackBackTrace (NTDLL.@)
1523 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1525 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip, count, buffer, hash );
1526 return 0;
1529 /***********************************************************************
1530 * signal_start_thread
1532 __ASM_GLOBAL_FUNC( signal_start_thread,
1533 "mov sp, x0\n\t" /* context */
1534 "mov x1, #1\n\t"
1535 "b " __ASM_NAME("NtContinue") )
1537 /**********************************************************************
1538 * DbgBreakPoint (NTDLL.@)
1540 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "brk #0xf000; ret"
1541 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1542 "\n\tnop; nop; nop; nop; nop; nop" );
1544 /**********************************************************************
1545 * DbgUserBreakPoint (NTDLL.@)
1547 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "brk #0xf000; ret"
1548 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1549 "\n\tnop; nop; nop; nop; nop; nop" );
1551 /**********************************************************************
1552 * NtCurrentTeb (NTDLL.@)
1554 TEB * WINAPI NtCurrentTeb(void)
1556 return __wine_current_teb();
1559 #endif /* __aarch64__ */