mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / ntdll / signal_arm64.c
blobdc6b072fa6c4dc888b5b215de0481d9c8c023139
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);
42 typedef struct _SCOPE_TABLE
44 ULONG Count;
45 struct
47 ULONG BeginAddress;
48 ULONG EndAddress;
49 ULONG HandlerAddress;
50 ULONG JumpTarget;
51 } ScopeRecord[1];
52 } SCOPE_TABLE, *PSCOPE_TABLE;
55 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
56 struct MSVCRT_JUMP_BUFFER
58 unsigned __int64 Frame;
59 unsigned __int64 Reserved;
60 unsigned __int64 X19;
61 unsigned __int64 X20;
62 unsigned __int64 X21;
63 unsigned __int64 X22;
64 unsigned __int64 X23;
65 unsigned __int64 X24;
66 unsigned __int64 X25;
67 unsigned __int64 X26;
68 unsigned __int64 X27;
69 unsigned __int64 X28;
70 unsigned __int64 Fp;
71 unsigned __int64 Lr;
72 unsigned __int64 Sp;
73 ULONG Fpcr;
74 ULONG Fpsr;
75 double D[8];
79 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
81 unsigned int i;
83 TRACE( "scope table at %p\n", table );
84 for (i = 0; i < table->Count; i++)
85 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
86 base + table->ScopeRecord[i].BeginAddress,
87 base + table->ScopeRecord[i].EndAddress,
88 base + table->ScopeRecord[i].HandlerAddress,
89 base + table->ScopeRecord[i].JumpTarget );
92 /*******************************************************************
93 * is_valid_frame
95 static inline BOOL is_valid_frame( ULONG_PTR frame )
97 if (frame & 7) return FALSE;
98 return ((void *)frame >= NtCurrentTeb()->Tib.StackLimit &&
99 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
103 /**************************************************************************
104 * __chkstk (NTDLL.@)
106 * Supposed to touch all the stack pages, but we shouldn't need that.
108 __ASM_GLOBAL_FUNC( __chkstk, "ret")
111 /***********************************************************************
112 * RtlCaptureContext (NTDLL.@)
114 __ASM_STDCALL_FUNC( RtlCaptureContext, 8,
115 "str x0, [x0, #0x8]\n\t" /* context->X0 */
116 "stp x1, x2, [x0, #0x10]\n\t" /* context->X1,X2 */
117 "stp x3, x4, [x0, #0x20]\n\t" /* context->X3,X4 */
118 "stp x5, x6, [x0, #0x30]\n\t" /* context->X5,X6 */
119 "stp x7, x8, [x0, #0x40]\n\t" /* context->X7,X8 */
120 "stp x9, x10, [x0, #0x50]\n\t" /* context->X9,X10 */
121 "stp x11, x12, [x0, #0x60]\n\t" /* context->X11,X12 */
122 "stp x13, x14, [x0, #0x70]\n\t" /* context->X13,X14 */
123 "stp x15, x16, [x0, #0x80]\n\t" /* context->X15,X16 */
124 "stp x17, x18, [x0, #0x90]\n\t" /* context->X17,X18 */
125 "stp x19, x20, [x0, #0xa0]\n\t" /* context->X19,X20 */
126 "stp x21, x22, [x0, #0xb0]\n\t" /* context->X21,X22 */
127 "stp x23, x24, [x0, #0xc0]\n\t" /* context->X23,X24 */
128 "stp x25, x26, [x0, #0xd0]\n\t" /* context->X25,X26 */
129 "stp x27, x28, [x0, #0xe0]\n\t" /* context->X27,X28 */
130 "stp x29, x30, [x0, #0xf0]\n\t" /* context->Fp,Lr */
131 "mov x1, sp\n\t"
132 "stp x1, x30, [x0, #0x100]\n\t" /* context->Sp,Pc */
133 "stp q0, q1, [x0, #0x110]\n\t" /* context->V[0-1] */
134 "stp q2, q3, [x0, #0x130]\n\t" /* context->V[2-3] */
135 "stp q4, q5, [x0, #0x150]\n\t" /* context->V[4-5] */
136 "stp q6, q7, [x0, #0x170]\n\t" /* context->V[6-7] */
137 "stp q8, q9, [x0, #0x190]\n\t" /* context->V[8-9] */
138 "stp q10, q11, [x0, #0x1a0]\n\t" /* context->V[10-11] */
139 "stp q12, q13, [x0, #0x1c0]\n\t" /* context->V[12-13] */
140 "stp q14, q15, [x0, #0x1e0]\n\t" /* context->V[14-15] */
141 "stp q16, q17, [x0, #0x210]\n\t" /* context->V[16-17] */
142 "stp q18, q19, [x0, #0x230]\n\t" /* context->V[18-19] */
143 "stp q20, q21, [x0, #0x250]\n\t" /* context->V[20-21] */
144 "stp q22, q23, [x0, #0x270]\n\t" /* context->V[22-23] */
145 "stp q24, q25, [x0, #0x290]\n\t" /* context->V[24-25] */
146 "stp q26, q27, [x0, #0x2a0]\n\t" /* context->V[26-27] */
147 "stp q28, q29, [x0, #0x2c0]\n\t" /* context->V[28-29] */
148 "stp q30, q31, [x0, #0x2e0]\n\t" /* context->V[30-31] */
149 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
150 "movk w1, #0x7\n\t" /* CONTEXT_FULL */
151 "str w1, [x0]\n\t" /* context->ContextFlags */
152 "mrs x1, NZCV\n\t"
153 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
154 "ret" )
157 /**********************************************************************
158 * virtual_unwind
160 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
162 LDR_DATA_TABLE_ENTRY *module;
163 NTSTATUS status;
165 dispatch->ImageBase = 0;
166 dispatch->ScopeIndex = 0;
167 dispatch->EstablisherFrame = 0;
168 dispatch->ControlPc = context->Pc;
170 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
171 * signal frame.
173 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
175 /* first look for PE exception information */
177 if ((dispatch->FunctionEntry = lookup_function_info(
178 context->Pc - (dispatch->ControlPcIsUnwound ? 4 : 0),
179 &dispatch->ImageBase, &module )))
181 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, context->Pc,
182 dispatch->FunctionEntry, context,
183 &dispatch->HandlerData, &dispatch->EstablisherFrame,
184 NULL );
185 return STATUS_SUCCESS;
188 /* then look for host system exception information */
190 if (!module || (module->Flags & LDR_WINE_INTERNAL))
192 status = unix_funcs->unwind_builtin_dll( type, dispatch, context );
193 if (status != STATUS_SUCCESS) return status;
195 if (dispatch->EstablisherFrame)
197 dispatch->FunctionEntry = NULL;
198 if (dispatch->LanguageHandler && !module)
200 FIXME( "calling personality routine in system library not supported yet\n" );
201 dispatch->LanguageHandler = NULL;
203 return STATUS_SUCCESS;
206 else
208 status = context->Pc != context->u.s.Lr ?
209 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
210 WARN( "exception data not found in %s for %p, LR %p, status %x\n",
211 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
212 (void*) context->u.s.Lr, status );
213 dispatch->EstablisherFrame = context->Sp;
214 dispatch->LanguageHandler = NULL;
215 context->Pc = context->u.s.Lr;
216 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
217 return status;
220 dispatch->EstablisherFrame = context->u.s.Fp;
221 dispatch->LanguageHandler = NULL;
222 context->Pc = context->u.s.Lr;
223 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
224 return STATUS_SUCCESS;
228 struct unwind_exception_frame
230 EXCEPTION_REGISTRATION_RECORD frame;
231 DISPATCHER_CONTEXT *dispatch;
234 /**********************************************************************
235 * unwind_exception_handler
237 * Handler for exceptions happening while calling an unwind handler.
239 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
240 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
242 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
243 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
245 /* copy the original dispatcher into the current one, except for the TargetIp */
246 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
247 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
248 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
249 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
250 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
251 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
252 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
253 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
254 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
255 TRACE( "detected collided unwind\n" );
256 return ExceptionCollidedUnwind;
259 /**********************************************************************
260 * call_unwind_handler
262 * Call a single unwind handler.
264 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
266 struct unwind_exception_frame frame;
267 DWORD res;
269 frame.frame.Handler = unwind_exception_handler;
270 frame.dispatch = dispatch;
271 __wine_push_frame( &frame.frame );
273 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
274 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
275 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
276 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
278 __wine_pop_frame( &frame.frame );
280 switch (res)
282 case ExceptionContinueSearch:
283 case ExceptionCollidedUnwind:
284 break;
285 default:
286 raise_status( STATUS_INVALID_DISPOSITION, rec );
287 break;
290 return res;
294 /**********************************************************************
295 * call_teb_unwind_handler
297 * Call a single unwind handler from the TEB chain.
299 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
300 EXCEPTION_REGISTRATION_RECORD *teb_frame )
302 DWORD res;
304 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
305 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
306 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
307 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
309 switch (res)
311 case ExceptionContinueSearch:
312 case ExceptionCollidedUnwind:
313 break;
314 default:
315 raise_status( STATUS_INVALID_DISPOSITION, rec );
316 break;
319 return res;
323 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
324 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
326 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
327 rec->ExceptionFlags |= EH_NESTED_CALL;
329 return ExceptionContinueSearch;
333 /**********************************************************************
334 * call_handler
336 * Call a single exception handler.
337 * FIXME: Handle nested exceptions.
339 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
341 EXCEPTION_REGISTRATION_RECORD frame;
342 DWORD res;
344 frame.Handler = nested_exception_handler;
345 __wine_push_frame( &frame );
347 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
348 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
349 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
350 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
352 rec->ExceptionFlags &= EH_NONCONTINUABLE;
353 __wine_pop_frame( &frame );
354 return res;
358 /**********************************************************************
359 * call_teb_handler
361 * Call a single exception handler from the TEB chain.
362 * FIXME: Handle nested exceptions.
364 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
365 EXCEPTION_REGISTRATION_RECORD *teb_frame )
367 DWORD res;
369 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
370 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
371 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
372 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
373 return res;
377 /**********************************************************************
378 * call_function_handlers
380 * Call the per-function handlers.
382 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
384 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
385 UNWIND_HISTORY_TABLE table;
386 DISPATCHER_CONTEXT dispatch;
387 CONTEXT context, prev_context;
388 NTSTATUS status;
390 context = *orig_context;
391 dispatch.TargetPc = 0;
392 dispatch.ContextRecord = &context;
393 dispatch.HistoryTable = &table;
394 prev_context = context;
395 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.u.s.X19;
397 for (;;)
399 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
400 if (status != STATUS_SUCCESS) return status;
402 unwind_done:
403 if (!dispatch.EstablisherFrame) break;
405 if (!is_valid_frame( dispatch.EstablisherFrame ))
407 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
408 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
409 rec->ExceptionFlags |= EH_STACK_INVALID;
410 break;
413 if (dispatch.LanguageHandler)
415 switch (call_handler( rec, orig_context, &dispatch ))
417 case ExceptionContinueExecution:
418 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
419 return STATUS_SUCCESS;
420 case ExceptionContinueSearch:
421 break;
422 case ExceptionNestedException:
423 FIXME( "nested exception\n" );
424 break;
425 case ExceptionCollidedUnwind: {
426 ULONG64 frame;
428 context = *dispatch.ContextRecord;
429 dispatch.ContextRecord = &context;
430 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
431 dispatch.ControlPc, dispatch.FunctionEntry,
432 &context, &dispatch.HandlerData, &frame, NULL );
433 goto unwind_done;
435 default:
436 return STATUS_INVALID_DISPOSITION;
439 /* hack: call wine handlers registered in the tib list */
440 else while ((ULONG64)teb_frame < context.Sp)
442 TRACE( "found wine frame %p rsp %lx handler %p\n",
443 teb_frame, context.Sp, teb_frame->Handler );
444 dispatch.EstablisherFrame = (ULONG64)teb_frame;
445 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
447 case ExceptionContinueExecution:
448 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
449 return STATUS_SUCCESS;
450 case ExceptionContinueSearch:
451 break;
452 case ExceptionNestedException:
453 FIXME( "nested exception\n" );
454 break;
455 case ExceptionCollidedUnwind: {
456 ULONG64 frame;
458 context = *dispatch.ContextRecord;
459 dispatch.ContextRecord = &context;
460 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
461 dispatch.ControlPc, dispatch.FunctionEntry,
462 &context, &dispatch.HandlerData, &frame, NULL );
463 teb_frame = teb_frame->Prev;
464 goto unwind_done;
466 default:
467 return STATUS_INVALID_DISPOSITION;
469 teb_frame = teb_frame->Prev;
472 if (context.Sp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
473 prev_context = context;
475 return STATUS_UNHANDLED_EXCEPTION;
479 /*******************************************************************
480 * KiUserExceptionDispatcher (NTDLL.@)
482 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
484 NTSTATUS status;
485 DWORD c;
487 TRACE( "code=%x flags=%x addr=%p pc=%lx tid=%04x\n",
488 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
489 context->Pc, GetCurrentThreadId() );
490 for (c = 0; c < rec->NumberParameters; c++)
491 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
493 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
495 if (rec->ExceptionInformation[1] >> 16)
496 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
497 rec->ExceptionAddress,
498 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
499 else
500 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
501 rec->ExceptionAddress,
502 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
504 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
506 WARN( "Thread %04x renamed to %s\n", (DWORD)rec->ExceptionInformation[2], debugstr_a((char *)rec->ExceptionInformation[1]) );
508 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
510 WARN( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
512 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
514 WARN( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
516 else
518 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
519 ERR( "%s exception (code=%x) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
520 else
521 WARN( "%s exception (code=%x) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
523 TRACE(" x0=%016lx x1=%016lx x2=%016lx x3=%016lx\n",
524 context->u.s.X0, context->u.s.X1, context->u.s.X2, context->u.s.X3 );
525 TRACE(" x4=%016lx x5=%016lx x6=%016lx x7=%016lx\n",
526 context->u.s.X4, context->u.s.X5, context->u.s.X6, context->u.s.X7 );
527 TRACE(" x8=%016lx x9=%016lx x10=%016lx x11=%016lx\n",
528 context->u.s.X8, context->u.s.X9, context->u.s.X10, context->u.s.X11 );
529 TRACE(" x12=%016lx x13=%016lx x14=%016lx x15=%016lx\n",
530 context->u.s.X12, context->u.s.X13, context->u.s.X14, context->u.s.X15 );
531 TRACE(" x16=%016lx x17=%016lx x18=%016lx x19=%016lx\n",
532 context->u.s.X16, context->u.s.X17, context->u.s.X18, context->u.s.X19 );
533 TRACE(" x20=%016lx x21=%016lx x22=%016lx x23=%016lx\n",
534 context->u.s.X20, context->u.s.X21, context->u.s.X22, context->u.s.X23 );
535 TRACE(" x24=%016lx x25=%016lx x26=%016lx x27=%016lx\n",
536 context->u.s.X24, context->u.s.X25, context->u.s.X26, context->u.s.X27 );
537 TRACE(" x28=%016lx fp=%016lx lr=%016lx sp=%016lx\n",
538 context->u.s.X28, context->u.s.Fp, context->u.s.Lr, context->Sp );
541 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
542 NtContinue( context, FALSE );
544 if ((status = call_function_handlers( rec, context )) == STATUS_SUCCESS)
545 NtContinue( context, FALSE );
547 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
548 return NtRaiseException( rec, context, FALSE );
552 /*******************************************************************
553 * KiUserApcDispatcher (NTDLL.@)
555 void WINAPI KiUserApcDispatcher( CONTEXT *context, ULONG_PTR ctx, ULONG_PTR arg1, ULONG_PTR arg2,
556 PNTAPCFUNC func )
558 func( ctx, arg1, arg2 );
559 NtContinue( context, TRUE );
563 /***********************************************************************
564 * Definitions for Win32 unwind tables
567 struct unwind_info
569 DWORD function_length : 18;
570 DWORD version : 2;
571 DWORD x : 1;
572 DWORD e : 1;
573 DWORD epilog : 5;
574 DWORD codes : 5;
577 struct unwind_info_ext
579 WORD epilog;
580 BYTE codes;
581 BYTE reserved;
584 struct unwind_info_epilog
586 DWORD offset : 18;
587 DWORD res : 4;
588 DWORD index : 10;
591 static const BYTE unwind_code_len[256] =
593 /* 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,
594 /* 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,
595 /* 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,
596 /* 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,
597 /* 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,
598 /* 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,
599 /* 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,
600 /* 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
603 /***********************************************************************
604 * get_sequence_len
606 static unsigned int get_sequence_len( BYTE *ptr, BYTE *end )
608 unsigned int ret = 0;
610 while (ptr < end)
612 if (*ptr == 0xe4 || *ptr == 0xe5) break;
613 ptr += unwind_code_len[*ptr];
614 ret++;
616 return ret;
620 /***********************************************************************
621 * restore_regs
623 static void restore_regs( int reg, int count, int pos, CONTEXT *context,
624 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
626 int i, offset = max( 0, pos );
627 for (i = 0; i < count; i++)
629 if (ptrs && reg + i >= 19) (&ptrs->X19)[reg + i - 19] = (DWORD64 *)context->Sp + i + offset;
630 context->u.X[reg + i] = ((DWORD64 *)context->Sp)[i + offset];
632 if (pos < 0) context->Sp += -8 * pos;
636 /***********************************************************************
637 * restore_fpregs
639 static void restore_fpregs( int reg, int count, int pos, CONTEXT *context,
640 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
642 int i, offset = max( 0, pos );
643 for (i = 0; i < count; i++)
645 if (ptrs && reg + i >= 8) (&ptrs->D8)[reg + i - 8] = (DWORD64 *)context->Sp + i + offset;
646 context->V[reg + i].D[0] = ((double *)context->Sp)[i + offset];
648 if (pos < 0) context->Sp += -8 * pos;
652 /***********************************************************************
653 * process_unwind_codes
655 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
656 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
658 unsigned int val, len, save_next = 2;
660 /* skip codes */
661 while (ptr < end && skip)
663 if (*ptr == 0xe4 || *ptr == 0xe5) break;
664 ptr += unwind_code_len[*ptr];
665 skip--;
668 while (ptr < end)
670 if ((len = unwind_code_len[*ptr]) > 1)
672 if (ptr + len > end) break;
673 val = ptr[0] * 0x100 + ptr[1];
675 else val = *ptr;
677 if (*ptr < 0x20) /* alloc_s */
678 context->Sp += 16 * (val & 0x1f);
679 else if (*ptr < 0x40) /* save_r19r20_x */
680 restore_regs( 19, save_next, -(val & 0x1f), context, ptrs );
681 else if (*ptr < 0x80) /* save_fplr */
682 restore_regs( 29, 2, val & 0x3f, context, ptrs );
683 else if (*ptr < 0xc0) /* save_fplr_x */
684 restore_regs( 29, 2, -(val & 0x3f) - 1, context, ptrs );
685 else if (*ptr < 0xc8) /* alloc_m */
686 context->Sp += 16 * (val & 0x7ff);
687 else if (*ptr < 0xcc) /* save_regp */
688 restore_regs( 19 + ((val >> 6) & 0xf), save_next, val & 0x3f, context, ptrs );
689 else if (*ptr < 0xd0) /* save_regp_x */
690 restore_regs( 19 + ((val >> 6) & 0xf), save_next, -(val & 0x3f) - 1, context, ptrs );
691 else if (*ptr < 0xd4) /* save_reg */
692 restore_regs( 19 + ((val >> 6) & 0xf), 1, val & 0x3f, context, ptrs );
693 else if (*ptr < 0xd6) /* save_reg_x */
694 restore_regs( 19 + ((val >> 5) & 0xf), 1, -(val & 0x1f) - 1, context, ptrs );
695 else if (*ptr < 0xd8) /* save_lrpair */
697 restore_regs( 19 + 2 * ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
698 restore_regs( 30, 1, (val & 0x3f) + 1, context, ptrs );
700 else if (*ptr < 0xda) /* save_fregp */
701 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, val & 0x3f, context, ptrs );
702 else if (*ptr < 0xdc) /* save_fregp_x */
703 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, -(val & 0x3f) - 1, context, ptrs );
704 else if (*ptr < 0xde) /* save_freg */
705 restore_fpregs( 8 + ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
706 else if (*ptr == 0xde) /* save_freg_x */
707 restore_fpregs( 8 + ((val >> 5) & 0x7), 1, -(val & 0x3f) - 1, context, ptrs );
708 else if (*ptr == 0xe0) /* alloc_l */
709 context->Sp += 16 * ((ptr[1] << 16) + (ptr[2] << 8) + ptr[3]);
710 else if (*ptr == 0xe1) /* set_fp */
711 context->Sp = context->u.s.Fp;
712 else if (*ptr == 0xe2) /* add_fp */
713 context->Sp = context->u.s.Fp - 8 * (val & 0xff);
714 else if (*ptr == 0xe3) /* nop */
715 /* nop */ ;
716 else if (*ptr == 0xe4) /* end */
717 break;
718 else if (*ptr == 0xe5) /* end_c */
719 break;
720 else if (*ptr == 0xe6) /* save_next */
722 save_next += 2;
723 ptr += len;
724 continue;
726 else if (*ptr == 0xe9) /* MSFT_OP_MACHINE_FRAME */
728 context->Pc = ((DWORD64 *)context->Sp)[1];
729 context->Sp = ((DWORD64 *)context->Sp)[0];
731 else if (*ptr == 0xea) /* MSFT_OP_CONTEXT */
733 memcpy( context, (DWORD64 *)context->Sp, sizeof(CONTEXT) );
735 else
737 WARN( "unsupported code %02x\n", *ptr );
738 return;
740 save_next = 2;
741 ptr += len;
746 /***********************************************************************
747 * unwind_packed_data
749 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
750 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
752 int i;
753 unsigned int len, offset, skip = 0;
754 unsigned int int_size = func->u.s.RegI * 8, fp_size = func->u.s.RegF * 8, regsave, local_size;
755 unsigned int int_regs, fp_regs, saved_regs, local_size_regs;
757 TRACE( "function %lx-%lx: len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
758 base + func->BeginAddress, base + func->BeginAddress + func->u.s.FunctionLength * 4,
759 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
760 func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
762 if (func->u.s.CR == 1) int_size += 8;
763 if (func->u.s.RegF) fp_size += 8;
765 regsave = ((int_size + fp_size + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
766 local_size = func->u.s.FrameSize * 16 - regsave;
768 int_regs = int_size / 8;
769 fp_regs = fp_size / 8;
770 saved_regs = regsave / 8;
771 local_size_regs = local_size / 8;
773 /* check for prolog/epilog */
774 if (func->u.s.Flag == 1)
776 offset = ((pc - base) - func->BeginAddress) / 4;
777 if (offset < 17 || offset >= func->u.s.FunctionLength - 15)
779 len = (int_size + 8) / 16 + (fp_size + 8) / 16;
780 switch (func->u.s.CR)
782 case 3:
783 len++; /* mov x29,sp */
784 len++; /* stp x29,lr,[sp,0] */
785 if (local_size <= 512) break;
786 /* fall through */
787 case 0:
788 case 1:
789 if (local_size) len++; /* sub sp,sp,#local_size */
790 if (local_size > 4088) len++; /* sub sp,sp,#4088 */
791 break;
793 len += 4 * func->u.s.H;
794 if (offset < len) /* prolog */
796 skip = len - offset;
798 else if (offset >= func->u.s.FunctionLength - (len + 1)) /* epilog */
800 skip = offset - (func->u.s.FunctionLength - (len + 1));
805 if (!skip)
807 if (func->u.s.CR == 3)
809 DWORD64 *fp = (DWORD64 *) context->u.s.Fp; /* u.X[29] */
810 context->Sp = context->u.s.Fp;
811 context->u.X[29] = fp[0];
812 context->u.X[30] = fp[1];
814 context->Sp += local_size;
815 if (fp_size) restore_fpregs( 8, fp_regs, int_regs, context, ptrs );
816 if (func->u.s.CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
817 restore_regs( 19, func->u.s.RegI, -saved_regs, context, ptrs );
819 else
821 unsigned int pos = 0;
823 switch (func->u.s.CR)
825 case 3:
826 /* mov x29,sp */
827 if (pos++ >= skip) context->Sp = context->u.s.Fp;
828 if (local_size <= 512)
830 /* stp x29,lr,[sp,-#local_size]! */
831 if (pos++ >= skip) restore_regs( 29, 2, -local_size_regs, context, ptrs );
832 break;
834 /* stp x29,lr,[sp,0] */
835 if (pos++ >= skip) restore_regs( 29, 2, 0, context, ptrs );
836 /* fall through */
837 case 0:
838 case 1:
839 if (!local_size) break;
840 /* sub sp,sp,#local_size */
841 if (pos++ >= skip) context->Sp += (local_size - 1) % 4088 + 1;
842 if (local_size > 4088 && pos++ >= skip) context->Sp += 4088;
843 break;
846 if (func->u.s.H) pos += 4;
848 if (fp_size)
850 if (func->u.s.RegF % 2 == 0 && pos++ >= skip)
851 /* str d%u,[sp,#fp_size] */
852 restore_fpregs( 8 + func->u.s.RegF, 1, int_regs + fp_regs - 1, context, ptrs );
853 for (i = (func->u.s.RegF + 1) / 2 - 1; i >= 0; i--)
855 if (pos++ < skip) continue;
856 if (!i && !int_size)
857 /* stp d8,d9,[sp,-#regsave]! */
858 restore_fpregs( 8, 2, -saved_regs, context, ptrs );
859 else
860 /* stp dn,dn+1,[sp,#offset] */
861 restore_fpregs( 8 + 2 * i, 2, int_regs + 2 * i, context, ptrs );
865 if (func->u.s.RegI % 2)
867 if (pos++ >= skip)
869 /* stp xn,lr,[sp,#offset] */
870 if (func->u.s.CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
871 /* str xn,[sp,#offset] */
872 restore_regs( 18 + func->u.s.RegI, 1,
873 (func->u.s.RegI > 1) ? func->u.s.RegI - 1 : -saved_regs,
874 context, ptrs );
877 else if (func->u.s.CR == 1)
879 /* str lr,[sp,#offset] */
880 if (pos++ >= skip) restore_regs( 30, 1, func->u.s.RegI ? int_regs - 1 : -saved_regs, context, ptrs );
883 for (i = func->u.s.RegI/ 2 - 1; i >= 0; i--)
885 if (pos++ < skip) continue;
886 if (i)
887 /* stp xn,xn+1,[sp,#offset] */
888 restore_regs( 19 + 2 * i, 2, 2 * i, context, ptrs );
889 else
890 /* stp x19,x20,[sp,-#regsave]! */
891 restore_regs( 19, 2, -saved_regs, context, ptrs );
894 return NULL;
898 /***********************************************************************
899 * unwind_full_data
901 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
902 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
904 struct unwind_info *info;
905 struct unwind_info_epilog *info_epilog;
906 unsigned int i, codes, epilogs, len, offset;
907 void *data;
908 BYTE *end;
910 info = (struct unwind_info *)((char *)base + func->u.UnwindData);
911 data = info + 1;
912 epilogs = info->epilog;
913 codes = info->codes;
914 if (!codes && !epilogs)
916 struct unwind_info_ext *infoex = data;
917 codes = infoex->codes;
918 epilogs = infoex->epilog;
919 data = infoex + 1;
921 info_epilog = data;
922 if (!info->e) data = info_epilog + epilogs;
924 offset = ((pc - base) - func->BeginAddress) / 4;
925 end = (BYTE *)data + codes * 4;
927 TRACE( "function %lx-%lx: len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
928 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 4,
929 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
931 /* check for prolog */
932 if (offset < codes * 4)
934 len = get_sequence_len( data, end );
935 if (offset < len)
937 process_unwind_codes( data, end, context, ptrs, len - offset );
938 return NULL;
942 /* check for epilog */
943 if (!info->e)
945 for (i = 0; i < epilogs; i++)
947 if (offset < info_epilog[i].offset) break;
948 if (offset - info_epilog[i].offset < codes * 4 - info_epilog[i].index)
950 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
951 len = get_sequence_len( ptr, end );
952 if (offset <= info_epilog[i].offset + len)
954 process_unwind_codes( ptr, end, context, ptrs, offset - info_epilog[i].offset );
955 return NULL;
960 else if (info->function_length - offset <= codes * 4 - epilogs)
962 BYTE *ptr = (BYTE *)data + epilogs;
963 len = get_sequence_len( ptr, end ) + 1;
964 if (offset >= info->function_length - len)
966 process_unwind_codes( ptr, end, context, ptrs, offset - (info->function_length - len) );
967 return NULL;
971 process_unwind_codes( data, end, context, ptrs, 0 );
973 /* get handler since we are inside the main code */
974 if (info->x)
976 DWORD *handler_rva = (DWORD *)data + codes;
977 *handler_data = handler_rva + 1;
978 return (char *)base + *handler_rva;
980 return NULL;
984 /**********************************************************************
985 * RtlVirtualUnwind (NTDLL.@)
987 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
988 RUNTIME_FUNCTION *func, CONTEXT *context,
989 PVOID *handler_data, ULONG_PTR *frame_ret,
990 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
992 void *handler;
994 TRACE( "type %x pc %lx sp %lx func %lx\n", type, pc, context->Sp, base + func->BeginAddress );
996 *handler_data = NULL;
998 context->Pc = 0;
999 if (func->u.s.Flag)
1000 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1001 else
1002 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1004 TRACE( "ret: lr=%lx sp=%lx handler=%p\n", context->u.s.Lr, context->Sp, handler );
1005 if (!context->Pc)
1006 context->Pc = context->u.s.Lr;
1007 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1008 *frame_ret = context->Sp;
1009 return handler;
1012 /**********************************************************************
1013 * call_consolidate_callback
1015 * Wrapper function to call a consolidate callback from a fake frame.
1016 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1017 * we have to skip all frames which were already processed. To do that we
1018 * trick the unwinding functions into thinking the call came from somewhere
1019 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1020 * DW_CFA_expression, and the expressions have the following format:
1022 * DW_OP_breg31; sleb128 <OFFSET> | Load x31 + struct member offset
1023 * [DW_OP_deref] | Dereference, only for CFA
1025 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1026 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1027 EXCEPTION_RECORD *rec,
1028 TEB *teb );
1029 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1030 "stp x29, x30, [sp, #-0x30]!\n\t"
1031 __ASM_CFI(".cfi_def_cfa_offset 48\n\t")
1032 __ASM_CFI(".cfi_offset 29, -48\n\t")
1033 __ASM_CFI(".cfi_offset 30, -40\n\t")
1034 __ASM_SEH(".seh_nop\n\t")
1035 "stp x1, x2, [sp, #0x10]\n\t"
1036 __ASM_SEH(".seh_nop\n\t")
1037 "str x3, [sp, #0x20]\n\t"
1038 __ASM_SEH(".seh_nop\n\t")
1039 "mov x29, sp\n\t"
1040 __ASM_CFI(".cfi_def_cfa_register 29\n\t")
1041 __ASM_SEH(".seh_nop\n\t")
1042 __ASM_CFI(".cfi_remember_state\n\t")
1043 /* Memcpy the context onto the stack */
1044 "sub sp, sp, #0x390\n\t"
1045 __ASM_SEH(".seh_nop\n\t")
1046 "mov x1, x0\n\t"
1047 __ASM_SEH(".seh_nop\n\t")
1048 "mov x0, sp\n\t"
1049 __ASM_SEH(".seh_nop\n\t")
1050 "mov x2, #0x390\n\t"
1051 __ASM_SEH(".seh_nop\n\t")
1052 "bl " __ASM_NAME("memcpy") "\n\t"
1053 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1054 __ASM_CFI(".cfi_escape 0x0f,0x04,0x8f,0x80,0x02,0x06\n\t") /* CFA, DW_OP_breg31 + 0x100, DW_OP_deref */
1055 __ASM_CFI(".cfi_escape 0x10,0x13,0x03,0x8f,0xa0,0x01\n\t") /* x19, DW_OP_breg31 + 0xA0 */
1056 __ASM_CFI(".cfi_escape 0x10,0x14,0x03,0x8f,0xa8,0x01\n\t") /* x20 */
1057 __ASM_CFI(".cfi_escape 0x10,0x15,0x03,0x8f,0xb0,0x01\n\t") /* x21 */
1058 __ASM_CFI(".cfi_escape 0x10,0x16,0x03,0x8f,0xb8,0x01\n\t") /* x22 */
1059 __ASM_CFI(".cfi_escape 0x10,0x17,0x03,0x8f,0xc0,0x01\n\t") /* x23 */
1060 __ASM_CFI(".cfi_escape 0x10,0x18,0x03,0x8f,0xc8,0x01\n\t") /* x24 */
1061 __ASM_CFI(".cfi_escape 0x10,0x19,0x03,0x8f,0xd0,0x01\n\t") /* x25 */
1062 __ASM_CFI(".cfi_escape 0x10,0x1a,0x03,0x8f,0xd8,0x01\n\t") /* x26 */
1063 __ASM_CFI(".cfi_escape 0x10,0x1b,0x03,0x8f,0xe0,0x01\n\t") /* x27 */
1064 __ASM_CFI(".cfi_escape 0x10,0x1c,0x03,0x8f,0xe8,0x01\n\t") /* x28 */
1065 __ASM_CFI(".cfi_escape 0x10,0x1d,0x03,0x8f,0xf0,0x01\n\t") /* x29 */
1066 __ASM_CFI(".cfi_escape 0x10,0x1e,0x03,0x8f,0xf8,0x01\n\t") /* x30 */
1067 __ASM_CFI(".cfi_escape 0x10,0x48,0x03,0x8f,0x90,0x03\n\t") /* d8 */
1068 __ASM_CFI(".cfi_escape 0x10,0x49,0x03,0x8f,0x98,0x03\n\t") /* d9 */
1069 __ASM_CFI(".cfi_escape 0x10,0x4a,0x03,0x8f,0xa0,0x03\n\t") /* d10 */
1070 __ASM_CFI(".cfi_escape 0x10,0x4b,0x03,0x8f,0xa8,0x03\n\t") /* d11 */
1071 __ASM_CFI(".cfi_escape 0x10,0x4c,0x03,0x8f,0xb0,0x03\n\t") /* d12 */
1072 __ASM_CFI(".cfi_escape 0x10,0x4d,0x03,0x8f,0xb8,0x03\n\t") /* d13 */
1073 __ASM_CFI(".cfi_escape 0x10,0x4e,0x03,0x8f,0xc0,0x03\n\t") /* d14 */
1074 __ASM_CFI(".cfi_escape 0x10,0x4f,0x03,0x8f,0xc8,0x03\n\t") /* d15 */
1075 __ASM_SEH(".seh_context\n\t")
1076 __ASM_SEH(".seh_endprologue\n\t")
1077 "ldp x1, x2, [x29, #0x10]\n\t"
1078 "ldr x18, [x29, #0x20]\n\t"
1079 "mov x0, x2\n\t"
1080 "blr x1\n\t"
1081 "mov sp, x29\n\t"
1082 __ASM_CFI(".cfi_restore_state\n\t")
1083 "ldp x29, x30, [sp], #48\n\t"
1084 __ASM_CFI(".cfi_restore 30\n\t")
1085 __ASM_CFI(".cfi_restore 29\n\t")
1086 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1087 "ret")
1089 /*******************************************************************
1090 * RtlRestoreContext (NTDLL.@)
1092 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1094 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1096 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1098 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1099 int i;
1101 context->u.s.X19 = jmp->X19;
1102 context->u.s.X20 = jmp->X20;
1103 context->u.s.X21 = jmp->X21;
1104 context->u.s.X22 = jmp->X22;
1105 context->u.s.X23 = jmp->X23;
1106 context->u.s.X24 = jmp->X24;
1107 context->u.s.X25 = jmp->X25;
1108 context->u.s.X26 = jmp->X26;
1109 context->u.s.X27 = jmp->X27;
1110 context->u.s.X28 = jmp->X28;
1111 context->u.s.Fp = jmp->Fp;
1112 context->u.s.Lr = jmp->Lr;
1113 context->Sp = jmp->Sp;
1114 context->Fpcr = jmp->Fpcr;
1115 context->Fpsr = jmp->Fpsr;
1117 for (i = 0; i < 8; i++)
1118 context->V[8+i].D[0] = jmp->D[i];
1120 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1122 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1123 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1124 rec->ExceptionInformation[10] = (ULONG_PTR)&context->u.s.X19;
1126 context->Pc = (ULONG64)call_consolidate_callback( context, consolidate, rec, NtCurrentTeb() );
1129 /* hack: remove no longer accessible TEB frames */
1130 while ((ULONG64)teb_frame < context->Sp)
1132 TRACE( "removing TEB frame: %p\n", teb_frame );
1133 teb_frame = __wine_pop_frame( teb_frame );
1136 TRACE( "returning to %lx stack %lx\n", context->Pc, context->Sp );
1137 NtContinue( context, FALSE );
1140 /*******************************************************************
1141 * RtlUnwindEx (NTDLL.@)
1143 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1144 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1146 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1147 EXCEPTION_RECORD record;
1148 DISPATCHER_CONTEXT dispatch;
1149 CONTEXT new_context;
1150 NTSTATUS status;
1151 DWORD i;
1153 RtlCaptureContext( context );
1154 new_context = *context;
1156 /* build an exception record, if we do not have one */
1157 if (!rec)
1159 record.ExceptionCode = STATUS_UNWIND;
1160 record.ExceptionFlags = 0;
1161 record.ExceptionRecord = NULL;
1162 record.ExceptionAddress = (void *)context->Pc;
1163 record.NumberParameters = 0;
1164 rec = &record;
1167 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1169 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p pc=%016lx\n",
1170 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1171 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1172 TRACE( " info[%d]=%016lx\n", i, rec->ExceptionInformation[i] );
1173 TRACE(" x0=%016lx x1=%016lx x2=%016lx x3=%016lx\n",
1174 context->u.s.X0, context->u.s.X1, context->u.s.X2, context->u.s.X3 );
1175 TRACE(" x4=%016lx x5=%016lx x6=%016lx x7=%016lx\n",
1176 context->u.s.X4, context->u.s.X5, context->u.s.X6, context->u.s.X7 );
1177 TRACE(" x8=%016lx x9=%016lx x10=%016lx x11=%016lx\n",
1178 context->u.s.X8, context->u.s.X9, context->u.s.X10, context->u.s.X11 );
1179 TRACE(" x12=%016lx x13=%016lx x14=%016lx x15=%016lx\n",
1180 context->u.s.X12, context->u.s.X13, context->u.s.X14, context->u.s.X15 );
1181 TRACE(" x16=%016lx x17=%016lx x18=%016lx x19=%016lx\n",
1182 context->u.s.X16, context->u.s.X17, context->u.s.X18, context->u.s.X19 );
1183 TRACE(" x20=%016lx x21=%016lx x22=%016lx x23=%016lx\n",
1184 context->u.s.X20, context->u.s.X21, context->u.s.X22, context->u.s.X23 );
1185 TRACE(" x24=%016lx x25=%016lx x26=%016lx x27=%016lx\n",
1186 context->u.s.X24, context->u.s.X25, context->u.s.X26, context->u.s.X27 );
1187 TRACE(" x28=%016lx fp=%016lx lr=%016lx sp=%016lx\n",
1188 context->u.s.X28, context->u.s.Fp, context->u.s.Lr, context->Sp );
1190 dispatch.TargetPc = (ULONG64)target_ip;
1191 dispatch.ContextRecord = context;
1192 dispatch.HistoryTable = table;
1193 dispatch.NonVolatileRegisters = (BYTE *)&context->u.s.X19;
1195 for (;;)
1197 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1198 if (status != STATUS_SUCCESS) raise_status( status, rec );
1200 unwind_done:
1201 if (!dispatch.EstablisherFrame) break;
1203 if (!is_valid_frame( dispatch.EstablisherFrame ))
1205 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1206 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1207 rec->ExceptionFlags |= EH_STACK_INVALID;
1208 break;
1211 if (dispatch.LanguageHandler)
1213 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
1215 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
1216 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1218 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1219 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1221 ULONG64 frame;
1223 *context = new_context = *dispatch.ContextRecord;
1224 dispatch.ContextRecord = context;
1225 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1226 dispatch.ControlPc, dispatch.FunctionEntry,
1227 &new_context, &dispatch.HandlerData, &frame,
1228 NULL );
1229 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1230 goto unwind_done;
1232 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1234 else /* hack: call builtin handlers registered in the tib list */
1236 DWORD64 backup_frame = dispatch.EstablisherFrame;
1237 while ((ULONG64)teb_frame < new_context.Sp && (ULONG64)teb_frame < (ULONG64)end_frame)
1239 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1240 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1241 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1243 ULONG64 frame;
1245 teb_frame = __wine_pop_frame( teb_frame );
1247 *context = new_context = *dispatch.ContextRecord;
1248 dispatch.ContextRecord = context;
1249 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1250 dispatch.ControlPc, dispatch.FunctionEntry,
1251 &new_context, &dispatch.HandlerData,
1252 &frame, NULL );
1253 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1254 goto unwind_done;
1256 teb_frame = __wine_pop_frame( teb_frame );
1258 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Sp) break;
1259 dispatch.EstablisherFrame = backup_frame;
1262 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
1263 *context = new_context;
1266 context->u.s.X0 = (ULONG64)retval;
1267 context->Pc = (ULONG64)target_ip;
1268 RtlRestoreContext(context, rec);
1272 /***********************************************************************
1273 * RtlUnwind (NTDLL.@)
1275 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1277 CONTEXT context;
1278 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1281 /*******************************************************************
1282 * _local_unwind (NTDLL.@)
1284 void WINAPI _local_unwind( void *frame, void *target_ip )
1286 CONTEXT context;
1287 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1290 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1291 PEXCEPTION_FILTER filter,
1292 PUCHAR nonvolatile);
1293 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1294 "stp x29, x30, [sp, #-96]!\n\t"
1295 __ASM_SEH(".seh_save_fplr_x 96\n\t")
1296 "stp x19, x20, [sp, #16]\n\t"
1297 __ASM_SEH(".seh_save_regp x19, 16\n\t")
1298 "stp x21, x22, [sp, #32]\n\t"
1299 __ASM_SEH(".seh_save_regp x21, 32\n\t")
1300 "stp x23, x24, [sp, #48]\n\t"
1301 __ASM_SEH(".seh_save_regp x23, 48\n\t")
1302 "stp x25, x26, [sp, #64]\n\t"
1303 __ASM_SEH(".seh_save_regp x25, 64\n\t")
1304 "stp x27, x28, [sp, #80]\n\t"
1305 __ASM_SEH(".seh_save_regp x27, 80\n\t")
1306 "mov x29, sp\n\t"
1307 __ASM_SEH(".seh_set_fp\n\t")
1308 __ASM_SEH(".seh_endprologue\n\t")
1310 __ASM_CFI(".cfi_def_cfa x29, 96\n\t")
1311 __ASM_CFI(".cfi_offset x29, -96\n\t")
1312 __ASM_CFI(".cfi_offset x30, -88\n\t")
1313 __ASM_CFI(".cfi_offset x19, -80\n\t")
1314 __ASM_CFI(".cfi_offset x20, -72\n\t")
1315 __ASM_CFI(".cfi_offset x21, -64\n\t")
1316 __ASM_CFI(".cfi_offset x22, -56\n\t")
1317 __ASM_CFI(".cfi_offset x23, -48\n\t")
1318 __ASM_CFI(".cfi_offset x24, -40\n\t")
1319 __ASM_CFI(".cfi_offset x25, -32\n\t")
1320 __ASM_CFI(".cfi_offset x26, -24\n\t")
1321 __ASM_CFI(".cfi_offset x27, -16\n\t")
1322 __ASM_CFI(".cfi_offset x28, -8\n\t")
1324 "ldp x19, x20, [x3, #0]\n\t"
1325 "ldp x21, x22, [x3, #16]\n\t"
1326 "ldp x23, x24, [x3, #32]\n\t"
1327 "ldp x25, x26, [x3, #48]\n\t"
1328 "ldp x27, x28, [x3, #64]\n\t"
1329 /* Overwrite the frame parameter with Fp from the
1330 * nonvolatile regs */
1331 "ldr x1, [x3, #80]\n\t"
1332 "blr x2\n\t"
1333 "ldp x19, x20, [sp, #16]\n\t"
1334 "ldp x21, x22, [sp, #32]\n\t"
1335 "ldp x23, x24, [sp, #48]\n\t"
1336 "ldp x25, x26, [sp, #64]\n\t"
1337 "ldp x27, x28, [sp, #80]\n\t"
1338 "ldp x29, x30, [sp], #96\n\t"
1339 "ret")
1341 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1342 PTERMINATION_HANDLER handler,
1343 PUCHAR nonvolatile);
1344 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1345 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1346 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1348 /*******************************************************************
1349 * __C_specific_handler (NTDLL.@)
1351 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1352 void *frame,
1353 CONTEXT *context,
1354 struct _DISPATCHER_CONTEXT *dispatch )
1356 SCOPE_TABLE *table = dispatch->HandlerData;
1357 ULONG i;
1358 DWORD64 ControlPc = dispatch->ControlPc;
1360 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1361 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1363 if (dispatch->ControlPcIsUnwound)
1364 ControlPc -= 4;
1366 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1368 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1370 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1371 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1373 PTERMINATION_HANDLER handler;
1375 if (table->ScopeRecord[i].JumpTarget) continue;
1377 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1378 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1379 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1381 break;
1384 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1385 dispatch->ScopeIndex = i+1;
1387 TRACE( "calling __finally %p frame %p\n", handler, frame );
1388 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1389 dispatch->NonVolatileRegisters );
1392 return ExceptionContinueSearch;
1395 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1397 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1398 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1400 if (!table->ScopeRecord[i].JumpTarget) continue;
1401 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1403 EXCEPTION_POINTERS ptrs;
1404 PEXCEPTION_FILTER filter;
1406 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1407 ptrs.ExceptionRecord = rec;
1408 ptrs.ContextRecord = context;
1409 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1410 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1411 dispatch->NonVolatileRegisters ))
1413 case EXCEPTION_EXECUTE_HANDLER:
1414 break;
1415 case EXCEPTION_CONTINUE_SEARCH:
1416 continue;
1417 case EXCEPTION_CONTINUE_EXECUTION:
1418 return ExceptionContinueExecution;
1421 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1422 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1423 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1426 return ExceptionContinueSearch;
1430 /***********************************************************************
1431 * RtlRaiseException (NTDLL.@)
1433 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
1434 "sub sp, sp, #0x3b0\n\t" /* 0x390 (context) + 0x20 */
1435 "stp x29, x30, [sp]\n\t"
1436 __ASM_SEH(".seh_stackalloc 0x3b0\n\t")
1437 __ASM_SEH(".seh_save_fplr 0\n\t")
1438 __ASM_SEH(".seh_endprologue\n\t")
1439 __ASM_CFI(".cfi_def_cfa x29, 944\n\t")
1440 __ASM_CFI(".cfi_offset x30, -936\n\t")
1441 __ASM_CFI(".cfi_offset x29, -944\n\t")
1442 "mov x29, sp\n\t"
1443 "str x0, [sp, #0x10]\n\t"
1444 "add x0, sp, #0x20\n\t"
1445 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1446 "add x1, sp, #0x20\n\t" /* context pointer */
1447 "add x2, sp, #0x3b0\n\t" /* orig stack pointer */
1448 "str x2, [x1, #0x100]\n\t" /* context->Sp */
1449 "ldr x0, [sp, #0x10]\n\t" /* original first parameter */
1450 "str x0, [x1, #0x08]\n\t" /* context->X0 */
1451 "ldp x4, x5, [sp]\n\t" /* frame pointer, return address */
1452 "stp x4, x5, [x1, #0xf0]\n\t" /* context->Fp, Lr */
1453 "str x5, [x1, #0x108]\n\t" /* context->Pc */
1454 "str x5, [x0, #0x10]\n\t" /* rec->ExceptionAddress */
1455 "mov x2, #1\n\t"
1456 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1457 "bl " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1459 /*************************************************************************
1460 * RtlCaptureStackBackTrace (NTDLL.@)
1462 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1464 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
1465 return 0;
1468 /***********************************************************************
1469 * signal_start_thread
1471 __ASM_GLOBAL_FUNC( signal_start_thread,
1472 "mov sp, x0\n\t" /* context */
1473 "mov x1, #1\n\t"
1474 "b " __ASM_NAME("NtContinue") )
1476 /**********************************************************************
1477 * DbgBreakPoint (NTDLL.@)
1479 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "brk #0; ret"
1480 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1481 "\n\tnop; nop; nop; nop; nop; nop" );
1483 /**********************************************************************
1484 * DbgUserBreakPoint (NTDLL.@)
1486 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "brk #0; ret"
1487 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1488 "\n\tnop; nop; nop; nop; nop; nop" );
1490 /**********************************************************************
1491 * NtCurrentTeb (NTDLL.@)
1493 TEB * WINAPI NtCurrentTeb(void)
1495 return unix_funcs->NtCurrentTeb();
1498 #endif /* __aarch64__ */