include: Be consistent in naming regarding MSF's block.
[wine.git] / dlls / ntdll / signal_arm64.c
blobbdf58dfba0187d999a0a2ae6ce180ef51277c046
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 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35 #include "wine/exception.h"
36 #include "ntdll_misc.h"
37 #include "wine/debug.h"
38 #include "winnt.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
41 WINE_DECLARE_DEBUG_CHANNEL(threadname);
43 TEB * (* WINAPI __wine_current_teb)(void) = NULL;
45 typedef struct _SCOPE_TABLE
47 ULONG Count;
48 struct
50 ULONG BeginAddress;
51 ULONG EndAddress;
52 ULONG HandlerAddress;
53 ULONG JumpTarget;
54 } ScopeRecord[1];
55 } SCOPE_TABLE, *PSCOPE_TABLE;
58 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
59 struct MSVCRT_JUMP_BUFFER
61 unsigned __int64 Frame;
62 unsigned __int64 Reserved;
63 unsigned __int64 X19;
64 unsigned __int64 X20;
65 unsigned __int64 X21;
66 unsigned __int64 X22;
67 unsigned __int64 X23;
68 unsigned __int64 X24;
69 unsigned __int64 X25;
70 unsigned __int64 X26;
71 unsigned __int64 X27;
72 unsigned __int64 X28;
73 unsigned __int64 Fp;
74 unsigned __int64 Lr;
75 unsigned __int64 Sp;
76 ULONG Fpcr;
77 ULONG Fpsr;
78 double D[8];
82 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
84 unsigned int i;
86 TRACE( "scope table at %p\n", table );
87 for (i = 0; i < table->Count; i++)
88 TRACE( " %u: %I64x-%I64x handler %I64x target %I64x\n", i,
89 base + table->ScopeRecord[i].BeginAddress,
90 base + table->ScopeRecord[i].EndAddress,
91 base + table->ScopeRecord[i].HandlerAddress,
92 base + table->ScopeRecord[i].JumpTarget );
95 /*******************************************************************
96 * is_valid_frame
98 static inline BOOL is_valid_frame( ULONG_PTR frame )
100 if (frame & 7) return FALSE;
101 return ((void *)frame >= NtCurrentTeb()->Tib.StackLimit &&
102 (void *)frame <= NtCurrentTeb()->Tib.StackBase);
106 /**************************************************************************
107 * __chkstk (NTDLL.@)
109 * Supposed to touch all the stack pages, but we shouldn't need that.
111 __ASM_GLOBAL_FUNC( __chkstk, "ret")
114 /***********************************************************************
115 * RtlCaptureContext (NTDLL.@)
117 __ASM_STDCALL_FUNC( RtlCaptureContext, 8,
118 "str xzr, [x0, #0x8]\n\t" /* context->X0 */
119 "stp x1, x2, [x0, #0x10]\n\t" /* context->X1,X2 */
120 "stp x3, x4, [x0, #0x20]\n\t" /* context->X3,X4 */
121 "stp x5, x6, [x0, #0x30]\n\t" /* context->X5,X6 */
122 "stp x7, x8, [x0, #0x40]\n\t" /* context->X7,X8 */
123 "stp x9, x10, [x0, #0x50]\n\t" /* context->X9,X10 */
124 "stp x11, x12, [x0, #0x60]\n\t" /* context->X11,X12 */
125 "stp x13, x14, [x0, #0x70]\n\t" /* context->X13,X14 */
126 "stp x15, x16, [x0, #0x80]\n\t" /* context->X15,X16 */
127 "stp x17, x18, [x0, #0x90]\n\t" /* context->X17,X18 */
128 "stp x19, x20, [x0, #0xa0]\n\t" /* context->X19,X20 */
129 "stp x21, x22, [x0, #0xb0]\n\t" /* context->X21,X22 */
130 "stp x23, x24, [x0, #0xc0]\n\t" /* context->X23,X24 */
131 "stp x25, x26, [x0, #0xd0]\n\t" /* context->X25,X26 */
132 "stp x27, x28, [x0, #0xe0]\n\t" /* context->X27,X28 */
133 "stp x29, xzr, [x0, #0xf0]\n\t" /* context->Fp,Lr */
134 "mov x1, sp\n\t"
135 "stp x1, x30, [x0, #0x100]\n\t" /* context->Sp,Pc */
136 "stp q0, q1, [x0, #0x110]\n\t" /* context->V[0-1] */
137 "stp q2, q3, [x0, #0x130]\n\t" /* context->V[2-3] */
138 "stp q4, q5, [x0, #0x150]\n\t" /* context->V[4-5] */
139 "stp q6, q7, [x0, #0x170]\n\t" /* context->V[6-7] */
140 "stp q8, q9, [x0, #0x190]\n\t" /* context->V[8-9] */
141 "stp q10, q11, [x0, #0x1b0]\n\t" /* context->V[10-11] */
142 "stp q12, q13, [x0, #0x1d0]\n\t" /* context->V[12-13] */
143 "stp q14, q15, [x0, #0x1f0]\n\t" /* context->V[14-15] */
144 "stp q16, q17, [x0, #0x210]\n\t" /* context->V[16-17] */
145 "stp q18, q19, [x0, #0x230]\n\t" /* context->V[18-19] */
146 "stp q20, q21, [x0, #0x250]\n\t" /* context->V[20-21] */
147 "stp q22, q23, [x0, #0x270]\n\t" /* context->V[22-23] */
148 "stp q24, q25, [x0, #0x290]\n\t" /* context->V[24-25] */
149 "stp q26, q27, [x0, #0x2b0]\n\t" /* context->V[26-27] */
150 "stp q28, q29, [x0, #0x2d0]\n\t" /* context->V[28-29] */
151 "stp q30, q31, [x0, #0x2f0]\n\t" /* context->V[30-31] */
152 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
153 "movk w1, #0x7\n\t" /* CONTEXT_FULL */
154 "str w1, [x0]\n\t" /* context->ContextFlags */
155 "mrs x1, NZCV\n\t"
156 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
157 "mrs x1, FPCR\n\t"
158 "str w1, [x0, #0x310]\n\t" /* context->Fpcr */
159 "mrs x1, FPSR\n\t"
160 "str w1, [x0, #0x314]\n\t" /* context->Fpsr */
161 "ret" )
164 /**********************************************************************
165 * virtual_unwind
167 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
169 LDR_DATA_TABLE_ENTRY *module;
170 NTSTATUS status;
171 DWORD64 pc;
173 dispatch->ImageBase = 0;
174 dispatch->ScopeIndex = 0;
175 dispatch->EstablisherFrame = 0;
176 dispatch->ControlPc = context->Pc;
178 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
179 * signal frame.
181 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
182 pc = context->Pc - (dispatch->ControlPcIsUnwound ? 4 : 0);
184 /* first look for PE exception information */
186 if ((dispatch->FunctionEntry = lookup_function_info(pc,
187 &dispatch->ImageBase, &module )))
189 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, pc,
190 dispatch->FunctionEntry, context,
191 &dispatch->HandlerData, &dispatch->EstablisherFrame,
192 NULL );
193 return STATUS_SUCCESS;
196 /* then look for host system exception information */
198 if (!module || (module->Flags & LDR_WINE_INTERNAL))
200 struct unwind_builtin_dll_params params = { type, dispatch, context };
202 status = NTDLL_UNIX_CALL( unwind_builtin_dll, &params );
203 if (status != STATUS_SUCCESS) return status;
205 if (dispatch->EstablisherFrame)
207 dispatch->FunctionEntry = NULL;
208 if (dispatch->LanguageHandler && !module)
210 FIXME( "calling personality routine in system library not supported yet\n" );
211 dispatch->LanguageHandler = NULL;
213 return STATUS_SUCCESS;
216 else
218 status = context->Pc != context->u.s.Lr ?
219 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
220 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
221 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
222 (void*) context->u.s.Lr, status );
223 dispatch->EstablisherFrame = context->Sp;
224 dispatch->LanguageHandler = NULL;
225 context->Pc = context->u.s.Lr;
226 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
227 return status;
230 dispatch->EstablisherFrame = context->u.s.Fp;
231 dispatch->LanguageHandler = NULL;
232 context->Pc = context->u.s.Lr;
233 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
234 return STATUS_SUCCESS;
238 struct unwind_exception_frame
240 EXCEPTION_REGISTRATION_RECORD frame;
241 DISPATCHER_CONTEXT *dispatch;
244 /**********************************************************************
245 * unwind_exception_handler
247 * Handler for exceptions happening while calling an unwind handler.
249 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
250 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
252 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
253 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
255 /* copy the original dispatcher into the current one, except for the TargetIp */
256 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
257 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
258 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
259 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
260 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
261 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
262 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
263 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
264 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
265 TRACE( "detected collided unwind\n" );
266 return ExceptionCollidedUnwind;
269 /**********************************************************************
270 * call_unwind_handler
272 * Call a single unwind handler.
274 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
276 struct unwind_exception_frame frame;
277 DWORD res;
279 frame.frame.Handler = unwind_exception_handler;
280 frame.dispatch = dispatch;
281 __wine_push_frame( &frame.frame );
283 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
284 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
285 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
286 TRACE( "handler %p returned %lx\n", dispatch->LanguageHandler, res );
288 __wine_pop_frame( &frame.frame );
290 switch (res)
292 case ExceptionContinueSearch:
293 case ExceptionCollidedUnwind:
294 break;
295 default:
296 raise_status( STATUS_INVALID_DISPOSITION, rec );
297 break;
300 return res;
304 /**********************************************************************
305 * call_teb_unwind_handler
307 * Call a single unwind handler from the TEB chain.
309 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
310 EXCEPTION_REGISTRATION_RECORD *teb_frame )
312 DWORD res;
314 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
315 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
316 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
317 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
319 switch (res)
321 case ExceptionContinueSearch:
322 case ExceptionCollidedUnwind:
323 break;
324 default:
325 raise_status( STATUS_INVALID_DISPOSITION, rec );
326 break;
329 return res;
333 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
334 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
336 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
337 rec->ExceptionFlags |= EH_NESTED_CALL;
339 return ExceptionContinueSearch;
343 /**********************************************************************
344 * call_handler
346 * Call a single exception handler.
347 * FIXME: Handle nested exceptions.
349 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
351 EXCEPTION_REGISTRATION_RECORD frame;
352 DWORD res;
354 frame.Handler = nested_exception_handler;
355 __wine_push_frame( &frame );
357 TRACE( "calling handler %p (rec=%p, frame=%I64x context=%p, dispatch=%p)\n",
358 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
359 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
360 TRACE( "handler at %p returned %lu\n", dispatch->LanguageHandler, res );
362 rec->ExceptionFlags &= EH_NONCONTINUABLE;
363 __wine_pop_frame( &frame );
364 return res;
368 /**********************************************************************
369 * call_teb_handler
371 * Call a single exception handler from the TEB chain.
372 * FIXME: Handle nested exceptions.
374 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
375 EXCEPTION_REGISTRATION_RECORD *teb_frame )
377 DWORD res;
379 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
380 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
381 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
382 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
383 return res;
387 /**********************************************************************
388 * call_function_handlers
390 * Call the per-function handlers.
392 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
394 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
395 UNWIND_HISTORY_TABLE table;
396 DISPATCHER_CONTEXT dispatch;
397 CONTEXT context, prev_context;
398 NTSTATUS status;
400 context = *orig_context;
401 dispatch.TargetPc = 0;
402 dispatch.ContextRecord = &context;
403 dispatch.HistoryTable = &table;
404 prev_context = context;
405 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.u.s.X19;
407 for (;;)
409 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
410 if (status != STATUS_SUCCESS) return status;
412 unwind_done:
413 if (!dispatch.EstablisherFrame) break;
415 if (!is_valid_frame( dispatch.EstablisherFrame ))
417 ERR( "invalid frame %I64x (%p-%p)\n", dispatch.EstablisherFrame,
418 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
419 rec->ExceptionFlags |= EH_STACK_INVALID;
420 break;
423 if (dispatch.LanguageHandler)
425 switch (call_handler( rec, orig_context, &dispatch ))
427 case ExceptionContinueExecution:
428 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
429 return STATUS_SUCCESS;
430 case ExceptionContinueSearch:
431 break;
432 case ExceptionNestedException:
433 FIXME( "nested exception\n" );
434 break;
435 case ExceptionCollidedUnwind: {
436 ULONG64 frame;
438 context = *dispatch.ContextRecord;
439 dispatch.ContextRecord = &context;
440 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
441 dispatch.ControlPc, dispatch.FunctionEntry,
442 &context, &dispatch.HandlerData, &frame, NULL );
443 goto unwind_done;
445 default:
446 return STATUS_INVALID_DISPOSITION;
449 /* hack: call wine handlers registered in the tib list */
450 else while ((ULONG64)teb_frame < context.Sp)
452 TRACE( "found wine frame %p rsp %I64x handler %p\n",
453 teb_frame, context.Sp, teb_frame->Handler );
454 dispatch.EstablisherFrame = (ULONG64)teb_frame;
455 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
457 case ExceptionContinueExecution:
458 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
459 return STATUS_SUCCESS;
460 case ExceptionContinueSearch:
461 break;
462 case ExceptionNestedException:
463 FIXME( "nested exception\n" );
464 break;
465 case ExceptionCollidedUnwind: {
466 ULONG64 frame;
468 context = *dispatch.ContextRecord;
469 dispatch.ContextRecord = &context;
470 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
471 dispatch.ControlPc, dispatch.FunctionEntry,
472 &context, &dispatch.HandlerData, &frame, NULL );
473 teb_frame = teb_frame->Prev;
474 goto unwind_done;
476 default:
477 return STATUS_INVALID_DISPOSITION;
479 teb_frame = teb_frame->Prev;
482 if (context.Sp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
483 prev_context = context;
485 return STATUS_UNHANDLED_EXCEPTION;
489 /*******************************************************************
490 * KiUserExceptionDispatcher (NTDLL.@)
492 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
494 NTSTATUS status;
495 DWORD c;
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)
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->u.s.X0, context->u.s.X1, context->u.s.X2, context->u.s.X3 );
540 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
541 context->u.s.X4, context->u.s.X5, context->u.s.X6, context->u.s.X7 );
542 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
543 context->u.s.X8, context->u.s.X9, context->u.s.X10, context->u.s.X11 );
544 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
545 context->u.s.X12, context->u.s.X13, context->u.s.X14, context->u.s.X15 );
546 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
547 context->u.s.X16, context->u.s.X17, context->u.s.X18, context->u.s.X19 );
548 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
549 context->u.s.X20, context->u.s.X21, context->u.s.X22, context->u.s.X23 );
550 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
551 context->u.s.X24, context->u.s.X25, context->u.s.X26, context->u.s.X27 );
552 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
553 context->u.s.X28, context->u.s.Fp, context->u.s.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->u.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 /***********************************************************************
692 * process_unwind_codes
694 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
695 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
697 unsigned int val, len, save_next = 2;
699 /* skip codes */
700 while (ptr < end && skip)
702 if (*ptr == 0xe4 || *ptr == 0xe5) break;
703 ptr += unwind_code_len[*ptr];
704 skip--;
707 while (ptr < end)
709 if ((len = unwind_code_len[*ptr]) > 1)
711 if (ptr + len > end) break;
712 val = ptr[0] * 0x100 + ptr[1];
714 else val = *ptr;
716 if (*ptr < 0x20) /* alloc_s */
717 context->Sp += 16 * (val & 0x1f);
718 else if (*ptr < 0x40) /* save_r19r20_x */
719 restore_regs( 19, save_next, -(val & 0x1f), context, ptrs );
720 else if (*ptr < 0x80) /* save_fplr */
721 restore_regs( 29, 2, val & 0x3f, context, ptrs );
722 else if (*ptr < 0xc0) /* save_fplr_x */
723 restore_regs( 29, 2, -(val & 0x3f) - 1, context, ptrs );
724 else if (*ptr < 0xc8) /* alloc_m */
725 context->Sp += 16 * (val & 0x7ff);
726 else if (*ptr < 0xcc) /* save_regp */
727 restore_regs( 19 + ((val >> 6) & 0xf), save_next, val & 0x3f, context, ptrs );
728 else if (*ptr < 0xd0) /* save_regp_x */
729 restore_regs( 19 + ((val >> 6) & 0xf), save_next, -(val & 0x3f) - 1, context, ptrs );
730 else if (*ptr < 0xd4) /* save_reg */
731 restore_regs( 19 + ((val >> 6) & 0xf), 1, val & 0x3f, context, ptrs );
732 else if (*ptr < 0xd6) /* save_reg_x */
733 restore_regs( 19 + ((val >> 5) & 0xf), 1, -(val & 0x1f) - 1, context, ptrs );
734 else if (*ptr < 0xd8) /* save_lrpair */
736 restore_regs( 19 + 2 * ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
737 restore_regs( 30, 1, (val & 0x3f) + 1, context, ptrs );
739 else if (*ptr < 0xda) /* save_fregp */
740 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, val & 0x3f, context, ptrs );
741 else if (*ptr < 0xdc) /* save_fregp_x */
742 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, -(val & 0x3f) - 1, context, ptrs );
743 else if (*ptr < 0xde) /* save_freg */
744 restore_fpregs( 8 + ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
745 else if (*ptr == 0xde) /* save_freg_x */
746 restore_fpregs( 8 + ((val >> 5) & 0x7), 1, -(val & 0x3f) - 1, context, ptrs );
747 else if (*ptr == 0xe0) /* alloc_l */
748 context->Sp += 16 * ((ptr[1] << 16) + (ptr[2] << 8) + ptr[3]);
749 else if (*ptr == 0xe1) /* set_fp */
750 context->Sp = context->u.s.Fp;
751 else if (*ptr == 0xe2) /* add_fp */
752 context->Sp = context->u.s.Fp - 8 * (val & 0xff);
753 else if (*ptr == 0xe3) /* nop */
754 /* nop */ ;
755 else if (*ptr == 0xe4) /* end */
756 break;
757 else if (*ptr == 0xe5) /* end_c */
758 break;
759 else if (*ptr == 0xe6) /* save_next */
761 save_next += 2;
762 ptr += len;
763 continue;
765 else if (*ptr == 0xe9) /* MSFT_OP_MACHINE_FRAME */
767 context->Pc = ((DWORD64 *)context->Sp)[1];
768 context->Sp = ((DWORD64 *)context->Sp)[0];
770 else if (*ptr == 0xea) /* MSFT_OP_CONTEXT */
772 memcpy( context, (DWORD64 *)context->Sp, sizeof(CONTEXT) );
774 else
776 WARN( "unsupported code %02x\n", *ptr );
777 return;
779 save_next = 2;
780 ptr += len;
785 /***********************************************************************
786 * unwind_packed_data
788 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
789 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
791 int i;
792 unsigned int len, offset, skip = 0;
793 unsigned int int_size = func->u.s.RegI * 8, fp_size = func->u.s.RegF * 8, regsave, local_size;
794 unsigned int int_regs, fp_regs, saved_regs, local_size_regs;
796 TRACE( "function %I64x-%I64x: len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
797 base + func->BeginAddress, base + func->BeginAddress + func->u.s.FunctionLength * 4,
798 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
799 func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
801 if (func->u.s.CR == 1) int_size += 8;
802 if (func->u.s.RegF) fp_size += 8;
804 regsave = ((int_size + fp_size + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
805 local_size = func->u.s.FrameSize * 16 - regsave;
807 int_regs = int_size / 8;
808 fp_regs = fp_size / 8;
809 saved_regs = regsave / 8;
810 local_size_regs = local_size / 8;
812 /* check for prolog/epilog */
813 if (func->u.s.Flag == 1)
815 offset = ((pc - base) - func->BeginAddress) / 4;
816 if (offset < 17 || offset >= func->u.s.FunctionLength - 15)
818 len = (int_size + 8) / 16 + (fp_size + 8) / 16;
819 switch (func->u.s.CR)
821 case 3:
822 len++; /* mov x29,sp */
823 len++; /* stp x29,lr,[sp,0] */
824 if (local_size <= 512) break;
825 /* fall through */
826 case 0:
827 case 1:
828 if (local_size) len++; /* sub sp,sp,#local_size */
829 if (local_size > 4088) len++; /* sub sp,sp,#4088 */
830 break;
832 len += 4 * func->u.s.H;
833 if (offset < len) /* prolog */
835 skip = len - offset;
837 else if (offset >= func->u.s.FunctionLength - (len + 1)) /* epilog */
839 skip = offset - (func->u.s.FunctionLength - (len + 1));
844 if (!skip)
846 if (func->u.s.CR == 3)
848 DWORD64 *fp = (DWORD64 *) context->u.s.Fp; /* u.X[29] */
849 context->Sp = context->u.s.Fp;
850 context->u.X[29] = fp[0];
851 context->u.X[30] = fp[1];
853 context->Sp += local_size;
854 if (fp_size) restore_fpregs( 8, fp_regs, int_regs, context, ptrs );
855 if (func->u.s.CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
856 restore_regs( 19, func->u.s.RegI, -saved_regs, context, ptrs );
858 else
860 unsigned int pos = 0;
862 switch (func->u.s.CR)
864 case 3:
865 /* mov x29,sp */
866 if (pos++ >= skip) context->Sp = context->u.s.Fp;
867 if (local_size <= 512)
869 /* stp x29,lr,[sp,-#local_size]! */
870 if (pos++ >= skip) restore_regs( 29, 2, -local_size_regs, context, ptrs );
871 break;
873 /* stp x29,lr,[sp,0] */
874 if (pos++ >= skip) restore_regs( 29, 2, 0, context, ptrs );
875 /* fall through */
876 case 0:
877 case 1:
878 if (!local_size) break;
879 /* sub sp,sp,#local_size */
880 if (pos++ >= skip) context->Sp += (local_size - 1) % 4088 + 1;
881 if (local_size > 4088 && pos++ >= skip) context->Sp += 4088;
882 break;
885 if (func->u.s.H) pos += 4;
887 if (fp_size)
889 if (func->u.s.RegF % 2 == 0 && pos++ >= skip)
890 /* str d%u,[sp,#fp_size] */
891 restore_fpregs( 8 + func->u.s.RegF, 1, int_regs + fp_regs - 1, context, ptrs );
892 for (i = (func->u.s.RegF + 1) / 2 - 1; i >= 0; i--)
894 if (pos++ < skip) continue;
895 if (!i && !int_size)
896 /* stp d8,d9,[sp,-#regsave]! */
897 restore_fpregs( 8, 2, -saved_regs, context, ptrs );
898 else
899 /* stp dn,dn+1,[sp,#offset] */
900 restore_fpregs( 8 + 2 * i, 2, int_regs + 2 * i, context, ptrs );
904 if (func->u.s.RegI % 2)
906 if (pos++ >= skip)
908 /* stp xn,lr,[sp,#offset] */
909 if (func->u.s.CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
910 /* str xn,[sp,#offset] */
911 restore_regs( 18 + func->u.s.RegI, 1,
912 (func->u.s.RegI > 1) ? func->u.s.RegI - 1 : -saved_regs,
913 context, ptrs );
916 else if (func->u.s.CR == 1)
918 /* str lr,[sp,#offset] */
919 if (pos++ >= skip) restore_regs( 30, 1, func->u.s.RegI ? int_regs - 1 : -saved_regs, context, ptrs );
922 for (i = func->u.s.RegI/ 2 - 1; i >= 0; i--)
924 if (pos++ < skip) continue;
925 if (i)
926 /* stp xn,xn+1,[sp,#offset] */
927 restore_regs( 19 + 2 * i, 2, 2 * i, context, ptrs );
928 else
929 /* stp x19,x20,[sp,-#regsave]! */
930 restore_regs( 19, 2, -saved_regs, context, ptrs );
933 return NULL;
937 /***********************************************************************
938 * unwind_full_data
940 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
941 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
943 struct unwind_info *info;
944 struct unwind_info_epilog *info_epilog;
945 unsigned int i, codes, epilogs, len, offset;
946 void *data;
947 BYTE *end;
949 info = (struct unwind_info *)((char *)base + func->u.UnwindData);
950 data = info + 1;
951 epilogs = info->epilog;
952 codes = info->codes;
953 if (!codes && !epilogs)
955 struct unwind_info_ext *infoex = data;
956 codes = infoex->codes;
957 epilogs = infoex->epilog;
958 data = infoex + 1;
960 info_epilog = data;
961 if (!info->e) data = info_epilog + epilogs;
963 offset = ((pc - base) - func->BeginAddress) / 4;
964 end = (BYTE *)data + codes * 4;
966 TRACE( "function %I64x-%I64x: len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
967 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 4,
968 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
970 /* check for prolog */
971 if (offset < codes * 4)
973 len = get_sequence_len( data, end );
974 if (offset < len)
976 process_unwind_codes( data, end, context, ptrs, len - offset );
977 return NULL;
981 /* check for epilog */
982 if (!info->e)
984 for (i = 0; i < epilogs; i++)
986 if (offset < info_epilog[i].offset) break;
987 if (offset - info_epilog[i].offset < codes * 4 - info_epilog[i].index)
989 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
990 len = get_sequence_len( ptr, end );
991 if (offset <= info_epilog[i].offset + len)
993 process_unwind_codes( ptr, end, context, ptrs, offset - info_epilog[i].offset );
994 return NULL;
999 else if (info->function_length - offset <= codes * 4 - epilogs)
1001 BYTE *ptr = (BYTE *)data + epilogs;
1002 len = get_sequence_len( ptr, end ) + 1;
1003 if (offset >= info->function_length - len)
1005 process_unwind_codes( ptr, end, context, ptrs, offset - (info->function_length - len) );
1006 return NULL;
1010 process_unwind_codes( data, end, context, ptrs, 0 );
1012 /* get handler since we are inside the main code */
1013 if (info->x)
1015 DWORD *handler_rva = (DWORD *)data + codes;
1016 *handler_data = handler_rva + 1;
1017 return (char *)base + *handler_rva;
1019 return NULL;
1023 /**********************************************************************
1024 * RtlVirtualUnwind (NTDLL.@)
1026 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
1027 RUNTIME_FUNCTION *func, CONTEXT *context,
1028 PVOID *handler_data, ULONG_PTR *frame_ret,
1029 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
1031 void *handler;
1033 TRACE( "type %lx pc %I64x sp %I64x func %I64x\n", type, pc, context->Sp, base + func->BeginAddress );
1035 *handler_data = NULL;
1037 context->Pc = 0;
1038 if (func->u.s.Flag)
1039 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1040 else
1041 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1043 TRACE( "ret: lr=%I64x sp=%I64x handler=%p\n", context->u.s.Lr, context->Sp, handler );
1044 if (!context->Pc)
1045 context->Pc = context->u.s.Lr;
1046 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1047 *frame_ret = context->Sp;
1048 return handler;
1051 /**********************************************************************
1052 * call_consolidate_callback
1054 * Wrapper function to call a consolidate callback from a fake frame.
1055 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1056 * we have to skip all frames which were already processed. To do that we
1057 * trick the unwinding functions into thinking the call came from somewhere
1058 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1059 * DW_CFA_expression, and the expressions have the following format:
1061 * DW_OP_breg31; sleb128 <OFFSET> | Load x31 + struct member offset
1062 * [DW_OP_deref] | Dereference, only for CFA
1064 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1065 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1066 EXCEPTION_RECORD *rec,
1067 TEB *teb );
1068 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1069 "stp x29, x30, [sp, #-0x30]!\n\t"
1070 __ASM_CFI(".cfi_def_cfa_offset 48\n\t")
1071 __ASM_CFI(".cfi_offset 29, -48\n\t")
1072 __ASM_CFI(".cfi_offset 30, -40\n\t")
1073 __ASM_SEH(".seh_nop\n\t")
1074 "stp x1, x2, [sp, #0x10]\n\t"
1075 __ASM_SEH(".seh_nop\n\t")
1076 "str x3, [sp, #0x20]\n\t"
1077 __ASM_SEH(".seh_nop\n\t")
1078 "mov x29, sp\n\t"
1079 __ASM_CFI(".cfi_def_cfa_register 29\n\t")
1080 __ASM_SEH(".seh_nop\n\t")
1081 __ASM_CFI(".cfi_remember_state\n\t")
1082 /* Memcpy the context onto the stack */
1083 "sub sp, sp, #0x390\n\t"
1084 __ASM_SEH(".seh_nop\n\t")
1085 "mov x1, x0\n\t"
1086 __ASM_SEH(".seh_nop\n\t")
1087 "mov x0, sp\n\t"
1088 __ASM_SEH(".seh_nop\n\t")
1089 "mov x2, #0x390\n\t"
1090 __ASM_SEH(".seh_nop\n\t")
1091 "bl " __ASM_NAME("memcpy") "\n\t"
1092 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1093 __ASM_CFI(".cfi_escape 0x0f,0x04,0x8f,0x80,0x02,0x06\n\t") /* CFA, DW_OP_breg31 + 0x100, DW_OP_deref */
1094 __ASM_CFI(".cfi_escape 0x10,0x13,0x03,0x8f,0xa0,0x01\n\t") /* x19, DW_OP_breg31 + 0xA0 */
1095 __ASM_CFI(".cfi_escape 0x10,0x14,0x03,0x8f,0xa8,0x01\n\t") /* x20 */
1096 __ASM_CFI(".cfi_escape 0x10,0x15,0x03,0x8f,0xb0,0x01\n\t") /* x21 */
1097 __ASM_CFI(".cfi_escape 0x10,0x16,0x03,0x8f,0xb8,0x01\n\t") /* x22 */
1098 __ASM_CFI(".cfi_escape 0x10,0x17,0x03,0x8f,0xc0,0x01\n\t") /* x23 */
1099 __ASM_CFI(".cfi_escape 0x10,0x18,0x03,0x8f,0xc8,0x01\n\t") /* x24 */
1100 __ASM_CFI(".cfi_escape 0x10,0x19,0x03,0x8f,0xd0,0x01\n\t") /* x25 */
1101 __ASM_CFI(".cfi_escape 0x10,0x1a,0x03,0x8f,0xd8,0x01\n\t") /* x26 */
1102 __ASM_CFI(".cfi_escape 0x10,0x1b,0x03,0x8f,0xe0,0x01\n\t") /* x27 */
1103 __ASM_CFI(".cfi_escape 0x10,0x1c,0x03,0x8f,0xe8,0x01\n\t") /* x28 */
1104 __ASM_CFI(".cfi_escape 0x10,0x1d,0x03,0x8f,0xf0,0x01\n\t") /* x29 */
1105 __ASM_CFI(".cfi_escape 0x10,0x1e,0x03,0x8f,0xf8,0x01\n\t") /* x30 */
1106 __ASM_CFI(".cfi_escape 0x10,0x48,0x03,0x8f,0x90,0x03\n\t") /* d8 */
1107 __ASM_CFI(".cfi_escape 0x10,0x49,0x03,0x8f,0x98,0x03\n\t") /* d9 */
1108 __ASM_CFI(".cfi_escape 0x10,0x4a,0x03,0x8f,0xa0,0x03\n\t") /* d10 */
1109 __ASM_CFI(".cfi_escape 0x10,0x4b,0x03,0x8f,0xa8,0x03\n\t") /* d11 */
1110 __ASM_CFI(".cfi_escape 0x10,0x4c,0x03,0x8f,0xb0,0x03\n\t") /* d12 */
1111 __ASM_CFI(".cfi_escape 0x10,0x4d,0x03,0x8f,0xb8,0x03\n\t") /* d13 */
1112 __ASM_CFI(".cfi_escape 0x10,0x4e,0x03,0x8f,0xc0,0x03\n\t") /* d14 */
1113 __ASM_CFI(".cfi_escape 0x10,0x4f,0x03,0x8f,0xc8,0x03\n\t") /* d15 */
1114 __ASM_SEH(".seh_context\n\t")
1115 __ASM_SEH(".seh_endprologue\n\t")
1116 "ldp x1, x2, [x29, #0x10]\n\t"
1117 "ldr x18, [x29, #0x20]\n\t"
1118 "mov x0, x2\n\t"
1119 "blr x1\n\t"
1120 "mov sp, x29\n\t"
1121 __ASM_CFI(".cfi_restore_state\n\t")
1122 "ldp x29, x30, [sp], #48\n\t"
1123 __ASM_CFI(".cfi_restore 30\n\t")
1124 __ASM_CFI(".cfi_restore 29\n\t")
1125 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1126 "ret")
1128 /*******************************************************************
1129 * RtlRestoreContext (NTDLL.@)
1131 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1133 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1135 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1137 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1138 int i;
1140 context->u.s.X19 = jmp->X19;
1141 context->u.s.X20 = jmp->X20;
1142 context->u.s.X21 = jmp->X21;
1143 context->u.s.X22 = jmp->X22;
1144 context->u.s.X23 = jmp->X23;
1145 context->u.s.X24 = jmp->X24;
1146 context->u.s.X25 = jmp->X25;
1147 context->u.s.X26 = jmp->X26;
1148 context->u.s.X27 = jmp->X27;
1149 context->u.s.X28 = jmp->X28;
1150 context->u.s.Fp = jmp->Fp;
1151 context->u.s.Lr = jmp->Lr;
1152 context->Sp = jmp->Sp;
1153 context->Fpcr = jmp->Fpcr;
1154 context->Fpsr = jmp->Fpsr;
1156 for (i = 0; i < 8; i++)
1157 context->V[8+i].D[0] = jmp->D[i];
1159 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1161 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1162 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1163 rec->ExceptionInformation[10] = (ULONG_PTR)&context->u.s.X19;
1165 context->Pc = (ULONG64)call_consolidate_callback( context, consolidate, rec, NtCurrentTeb() );
1168 /* hack: remove no longer accessible TEB frames */
1169 while ((ULONG64)teb_frame < context->Sp)
1171 TRACE( "removing TEB frame: %p\n", teb_frame );
1172 teb_frame = __wine_pop_frame( teb_frame );
1175 TRACE( "returning to %I64x stack %I64x\n", context->Pc, context->Sp );
1176 NtContinue( context, FALSE );
1179 /*******************************************************************
1180 * RtlUnwindEx (NTDLL.@)
1182 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1183 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1185 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1186 EXCEPTION_RECORD record;
1187 DISPATCHER_CONTEXT dispatch;
1188 CONTEXT new_context;
1189 NTSTATUS status;
1190 DWORD i;
1192 RtlCaptureContext( context );
1193 new_context = *context;
1195 /* build an exception record, if we do not have one */
1196 if (!rec)
1198 record.ExceptionCode = STATUS_UNWIND;
1199 record.ExceptionFlags = 0;
1200 record.ExceptionRecord = NULL;
1201 record.ExceptionAddress = (void *)context->Pc;
1202 record.NumberParameters = 0;
1203 rec = &record;
1206 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1208 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%016I64x\n",
1209 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1210 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1211 TRACE( " info[%ld]=%016I64x\n", i, rec->ExceptionInformation[i] );
1212 TRACE(" x0=%016I64x x1=%016I64x x2=%016I64x x3=%016I64x\n",
1213 context->u.s.X0, context->u.s.X1, context->u.s.X2, context->u.s.X3 );
1214 TRACE(" x4=%016I64x x5=%016I64x x6=%016I64x x7=%016I64x\n",
1215 context->u.s.X4, context->u.s.X5, context->u.s.X6, context->u.s.X7 );
1216 TRACE(" x8=%016I64x x9=%016I64x x10=%016I64x x11=%016I64x\n",
1217 context->u.s.X8, context->u.s.X9, context->u.s.X10, context->u.s.X11 );
1218 TRACE(" x12=%016I64x x13=%016I64x x14=%016I64x x15=%016I64x\n",
1219 context->u.s.X12, context->u.s.X13, context->u.s.X14, context->u.s.X15 );
1220 TRACE(" x16=%016I64x x17=%016I64x x18=%016I64x x19=%016I64x\n",
1221 context->u.s.X16, context->u.s.X17, context->u.s.X18, context->u.s.X19 );
1222 TRACE(" x20=%016I64x x21=%016I64x x22=%016I64x x23=%016I64x\n",
1223 context->u.s.X20, context->u.s.X21, context->u.s.X22, context->u.s.X23 );
1224 TRACE(" x24=%016I64x x25=%016I64x x26=%016I64x x27=%016I64x\n",
1225 context->u.s.X24, context->u.s.X25, context->u.s.X26, context->u.s.X27 );
1226 TRACE(" x28=%016I64x fp=%016I64x lr=%016I64x sp=%016I64x\n",
1227 context->u.s.X28, context->u.s.Fp, context->u.s.Lr, context->Sp );
1229 dispatch.TargetPc = (ULONG64)target_ip;
1230 dispatch.ContextRecord = context;
1231 dispatch.HistoryTable = table;
1232 dispatch.NonVolatileRegisters = (BYTE *)&context->u.s.X19;
1234 for (;;)
1236 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1237 if (status != STATUS_SUCCESS) raise_status( status, rec );
1239 unwind_done:
1240 if (!dispatch.EstablisherFrame) break;
1242 if (!is_valid_frame( dispatch.EstablisherFrame ))
1244 ERR( "invalid frame %I64x (%p-%p)\n", dispatch.EstablisherFrame,
1245 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1246 rec->ExceptionFlags |= EH_STACK_INVALID;
1247 break;
1250 if (dispatch.LanguageHandler)
1252 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
1254 ERR( "invalid end frame %I64x/%p\n", dispatch.EstablisherFrame, end_frame );
1255 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1257 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1258 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1260 ULONG64 frame;
1262 *context = new_context = *dispatch.ContextRecord;
1263 dispatch.ContextRecord = context;
1264 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1265 dispatch.ControlPc, dispatch.FunctionEntry,
1266 &new_context, &dispatch.HandlerData, &frame,
1267 NULL );
1268 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1269 goto unwind_done;
1271 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1273 else /* hack: call builtin handlers registered in the tib list */
1275 DWORD64 backup_frame = dispatch.EstablisherFrame;
1276 while ((ULONG64)teb_frame < new_context.Sp && (ULONG64)teb_frame < (ULONG64)end_frame)
1278 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1279 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1280 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1282 ULONG64 frame;
1284 teb_frame = __wine_pop_frame( teb_frame );
1286 *context = new_context = *dispatch.ContextRecord;
1287 dispatch.ContextRecord = context;
1288 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1289 dispatch.ControlPc, dispatch.FunctionEntry,
1290 &new_context, &dispatch.HandlerData,
1291 &frame, NULL );
1292 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1293 goto unwind_done;
1295 teb_frame = __wine_pop_frame( teb_frame );
1297 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Sp) break;
1298 dispatch.EstablisherFrame = backup_frame;
1301 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
1302 *context = new_context;
1305 context->u.s.X0 = (ULONG64)retval;
1306 context->Pc = (ULONG64)target_ip;
1307 RtlRestoreContext(context, rec);
1311 /***********************************************************************
1312 * RtlUnwind (NTDLL.@)
1314 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1316 CONTEXT context;
1317 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1320 /*******************************************************************
1321 * _local_unwind (NTDLL.@)
1323 void WINAPI _local_unwind( void *frame, void *target_ip )
1325 CONTEXT context;
1326 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1329 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1330 PEXCEPTION_FILTER filter,
1331 PUCHAR nonvolatile);
1332 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1333 "stp x29, x30, [sp, #-96]!\n\t"
1334 __ASM_SEH(".seh_save_fplr_x 96\n\t")
1335 "stp x19, x20, [sp, #16]\n\t"
1336 __ASM_SEH(".seh_save_regp x19, 16\n\t")
1337 "stp x21, x22, [sp, #32]\n\t"
1338 __ASM_SEH(".seh_save_regp x21, 32\n\t")
1339 "stp x23, x24, [sp, #48]\n\t"
1340 __ASM_SEH(".seh_save_regp x23, 48\n\t")
1341 "stp x25, x26, [sp, #64]\n\t"
1342 __ASM_SEH(".seh_save_regp x25, 64\n\t")
1343 "stp x27, x28, [sp, #80]\n\t"
1344 __ASM_SEH(".seh_save_regp x27, 80\n\t")
1345 "mov x29, sp\n\t"
1346 __ASM_SEH(".seh_set_fp\n\t")
1347 __ASM_SEH(".seh_endprologue\n\t")
1349 __ASM_CFI(".cfi_def_cfa x29, 96\n\t")
1350 __ASM_CFI(".cfi_offset x29, -96\n\t")
1351 __ASM_CFI(".cfi_offset x30, -88\n\t")
1352 __ASM_CFI(".cfi_offset x19, -80\n\t")
1353 __ASM_CFI(".cfi_offset x20, -72\n\t")
1354 __ASM_CFI(".cfi_offset x21, -64\n\t")
1355 __ASM_CFI(".cfi_offset x22, -56\n\t")
1356 __ASM_CFI(".cfi_offset x23, -48\n\t")
1357 __ASM_CFI(".cfi_offset x24, -40\n\t")
1358 __ASM_CFI(".cfi_offset x25, -32\n\t")
1359 __ASM_CFI(".cfi_offset x26, -24\n\t")
1360 __ASM_CFI(".cfi_offset x27, -16\n\t")
1361 __ASM_CFI(".cfi_offset x28, -8\n\t")
1363 "ldp x19, x20, [x3, #0]\n\t"
1364 "ldp x21, x22, [x3, #16]\n\t"
1365 "ldp x23, x24, [x3, #32]\n\t"
1366 "ldp x25, x26, [x3, #48]\n\t"
1367 "ldp x27, x28, [x3, #64]\n\t"
1368 /* Overwrite the frame parameter with Fp from the
1369 * nonvolatile regs */
1370 "ldr x1, [x3, #80]\n\t"
1371 "blr x2\n\t"
1372 "ldp x19, x20, [sp, #16]\n\t"
1373 "ldp x21, x22, [sp, #32]\n\t"
1374 "ldp x23, x24, [sp, #48]\n\t"
1375 "ldp x25, x26, [sp, #64]\n\t"
1376 "ldp x27, x28, [sp, #80]\n\t"
1377 "ldp x29, x30, [sp], #96\n\t"
1378 "ret")
1380 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1381 PTERMINATION_HANDLER handler,
1382 PUCHAR nonvolatile);
1383 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1384 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1385 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1387 /*******************************************************************
1388 * __C_specific_handler (NTDLL.@)
1390 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1391 void *frame,
1392 CONTEXT *context,
1393 struct _DISPATCHER_CONTEXT *dispatch )
1395 SCOPE_TABLE *table = dispatch->HandlerData;
1396 ULONG i;
1397 DWORD64 ControlPc = dispatch->ControlPc;
1399 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1400 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1402 if (dispatch->ControlPcIsUnwound)
1403 ControlPc -= 4;
1405 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1407 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1409 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1410 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1412 PTERMINATION_HANDLER handler;
1414 if (table->ScopeRecord[i].JumpTarget) continue;
1416 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1417 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1418 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1420 break;
1423 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1424 dispatch->ScopeIndex = i+1;
1426 TRACE( "calling __finally %p frame %p\n", handler, frame );
1427 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1428 dispatch->NonVolatileRegisters );
1431 return ExceptionContinueSearch;
1434 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1436 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1437 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1439 if (!table->ScopeRecord[i].JumpTarget) continue;
1440 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1442 EXCEPTION_POINTERS ptrs;
1443 PEXCEPTION_FILTER filter;
1445 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1446 ptrs.ExceptionRecord = rec;
1447 ptrs.ContextRecord = context;
1448 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1449 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1450 dispatch->NonVolatileRegisters ))
1452 case EXCEPTION_EXECUTE_HANDLER:
1453 break;
1454 case EXCEPTION_CONTINUE_SEARCH:
1455 continue;
1456 case EXCEPTION_CONTINUE_EXECUTION:
1457 return ExceptionContinueExecution;
1460 TRACE( "unwinding to target %I64x\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1461 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1462 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1465 return ExceptionContinueSearch;
1469 /***********************************************************************
1470 * RtlRaiseException (NTDLL.@)
1472 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
1473 "sub sp, sp, #0x3b0\n\t" /* 0x390 (context) + 0x20 */
1474 "stp x29, x30, [sp]\n\t"
1475 __ASM_SEH(".seh_stackalloc 0x3b0\n\t")
1476 __ASM_SEH(".seh_save_fplr 0\n\t")
1477 __ASM_SEH(".seh_endprologue\n\t")
1478 __ASM_CFI(".cfi_def_cfa x29, 944\n\t")
1479 __ASM_CFI(".cfi_offset x30, -936\n\t")
1480 __ASM_CFI(".cfi_offset x29, -944\n\t")
1481 "mov x29, sp\n\t"
1482 "str x0, [sp, #0x10]\n\t"
1483 "add x0, sp, #0x20\n\t"
1484 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1485 "add x1, sp, #0x20\n\t" /* context pointer */
1486 "add x2, sp, #0x3b0\n\t" /* orig stack pointer */
1487 "str x2, [x1, #0x100]\n\t" /* context->Sp */
1488 "ldr x0, [sp, #0x10]\n\t" /* original first parameter */
1489 "str x0, [x1, #0x08]\n\t" /* context->X0 */
1490 "ldp x4, x5, [sp]\n\t" /* frame pointer, return address */
1491 "stp x4, x5, [x1, #0xf0]\n\t" /* context->Fp, Lr */
1492 "str x5, [x1, #0x108]\n\t" /* context->Pc */
1493 "str x5, [x0, #0x10]\n\t" /* rec->ExceptionAddress */
1494 "mov x2, #1\n\t"
1495 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1496 "bl " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1498 /*************************************************************************
1499 * RtlCaptureStackBackTrace (NTDLL.@)
1501 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1503 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip, count, buffer, hash );
1504 return 0;
1507 /***********************************************************************
1508 * signal_start_thread
1510 __ASM_GLOBAL_FUNC( signal_start_thread,
1511 "mov sp, x0\n\t" /* context */
1512 "mov x1, #1\n\t"
1513 "b " __ASM_NAME("NtContinue") )
1515 /**********************************************************************
1516 * DbgBreakPoint (NTDLL.@)
1518 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "brk #0xf000; ret"
1519 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1520 "\n\tnop; nop; nop; nop; nop; nop" );
1522 /**********************************************************************
1523 * DbgUserBreakPoint (NTDLL.@)
1525 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "brk #0xf000; ret"
1526 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1527 "\n\tnop; nop; nop; nop; nop; nop" );
1529 /**********************************************************************
1530 * NtCurrentTeb (NTDLL.@)
1532 TEB * WINAPI NtCurrentTeb(void)
1534 return __wine_current_teb();
1537 #endif /* __aarch64__ */