l3codeca.acm: Avoid mpg123 functions with suffix.
[wine.git] / dlls / ntdll / signal_arm64.c
blob034c78f6b4d0712d0321f3309caf3a3dcf524254
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 <= NtCurrentTeb()->Tib.StackBase);
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 xzr, [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, xzr, [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, #0x1b0]\n\t" /* context->V[10-11] */
139 "stp q12, q13, [x0, #0x1d0]\n\t" /* context->V[12-13] */
140 "stp q14, q15, [x0, #0x1f0]\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, #0x2b0]\n\t" /* context->V[26-27] */
147 "stp q28, q29, [x0, #0x2d0]\n\t" /* context->V[28-29] */
148 "stp q30, q31, [x0, #0x2f0]\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 "mrs x1, FPCR\n\t"
155 "str w1, [x0, #0x310]\n\t" /* context->Fpcr */
156 "mrs x1, FPSR\n\t"
157 "str w1, [x0, #0x314]\n\t" /* context->Fpsr */
158 "ret" )
161 /**********************************************************************
162 * virtual_unwind
164 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
166 LDR_DATA_TABLE_ENTRY *module;
167 NTSTATUS status;
169 dispatch->ImageBase = 0;
170 dispatch->ScopeIndex = 0;
171 dispatch->EstablisherFrame = 0;
172 dispatch->ControlPc = context->Pc;
174 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
175 * signal frame.
177 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
179 /* first look for PE exception information */
181 if ((dispatch->FunctionEntry = lookup_function_info(
182 context->Pc - (dispatch->ControlPcIsUnwound ? 4 : 0),
183 &dispatch->ImageBase, &module )))
185 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, context->Pc,
186 dispatch->FunctionEntry, context,
187 &dispatch->HandlerData, &dispatch->EstablisherFrame,
188 NULL );
189 return STATUS_SUCCESS;
192 /* then look for host system exception information */
194 if (!module || (module->Flags & LDR_WINE_INTERNAL))
196 status = unix_funcs->unwind_builtin_dll( type, dispatch, context );
197 if (status != STATUS_SUCCESS) return status;
199 if (dispatch->EstablisherFrame)
201 dispatch->FunctionEntry = NULL;
202 if (dispatch->LanguageHandler && !module)
204 FIXME( "calling personality routine in system library not supported yet\n" );
205 dispatch->LanguageHandler = NULL;
207 return STATUS_SUCCESS;
210 else
212 status = context->Pc != context->u.s.Lr ?
213 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
214 WARN( "exception data not found in %s for %p, LR %p, status %x\n",
215 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
216 (void*) context->u.s.Lr, status );
217 dispatch->EstablisherFrame = context->Sp;
218 dispatch->LanguageHandler = NULL;
219 context->Pc = context->u.s.Lr;
220 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
221 return status;
224 dispatch->EstablisherFrame = context->u.s.Fp;
225 dispatch->LanguageHandler = NULL;
226 context->Pc = context->u.s.Lr;
227 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
228 return STATUS_SUCCESS;
232 struct unwind_exception_frame
234 EXCEPTION_REGISTRATION_RECORD frame;
235 DISPATCHER_CONTEXT *dispatch;
238 /**********************************************************************
239 * unwind_exception_handler
241 * Handler for exceptions happening while calling an unwind handler.
243 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
244 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
246 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
247 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
249 /* copy the original dispatcher into the current one, except for the TargetIp */
250 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
251 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
252 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
253 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
254 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
255 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
256 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
257 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
258 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
259 TRACE( "detected collided unwind\n" );
260 return ExceptionCollidedUnwind;
263 /**********************************************************************
264 * call_unwind_handler
266 * Call a single unwind handler.
268 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
270 struct unwind_exception_frame frame;
271 DWORD res;
273 frame.frame.Handler = unwind_exception_handler;
274 frame.dispatch = dispatch;
275 __wine_push_frame( &frame.frame );
277 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
278 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
279 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
280 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
282 __wine_pop_frame( &frame.frame );
284 switch (res)
286 case ExceptionContinueSearch:
287 case ExceptionCollidedUnwind:
288 break;
289 default:
290 raise_status( STATUS_INVALID_DISPOSITION, rec );
291 break;
294 return res;
298 /**********************************************************************
299 * call_teb_unwind_handler
301 * Call a single unwind handler from the TEB chain.
303 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
304 EXCEPTION_REGISTRATION_RECORD *teb_frame )
306 DWORD res;
308 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
309 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
310 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
311 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
313 switch (res)
315 case ExceptionContinueSearch:
316 case ExceptionCollidedUnwind:
317 break;
318 default:
319 raise_status( STATUS_INVALID_DISPOSITION, rec );
320 break;
323 return res;
327 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
328 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
330 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
331 rec->ExceptionFlags |= EH_NESTED_CALL;
333 return ExceptionContinueSearch;
337 /**********************************************************************
338 * call_handler
340 * Call a single exception handler.
341 * FIXME: Handle nested exceptions.
343 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
345 EXCEPTION_REGISTRATION_RECORD frame;
346 DWORD res;
348 frame.Handler = nested_exception_handler;
349 __wine_push_frame( &frame );
351 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
352 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
353 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
354 TRACE( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
356 rec->ExceptionFlags &= EH_NONCONTINUABLE;
357 __wine_pop_frame( &frame );
358 return res;
362 /**********************************************************************
363 * call_teb_handler
365 * Call a single exception handler from the TEB chain.
366 * FIXME: Handle nested exceptions.
368 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
369 EXCEPTION_REGISTRATION_RECORD *teb_frame )
371 DWORD res;
373 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
374 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
375 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
376 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
377 return res;
381 /**********************************************************************
382 * call_function_handlers
384 * Call the per-function handlers.
386 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
388 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
389 UNWIND_HISTORY_TABLE table;
390 DISPATCHER_CONTEXT dispatch;
391 CONTEXT context, prev_context;
392 NTSTATUS status;
394 context = *orig_context;
395 dispatch.TargetPc = 0;
396 dispatch.ContextRecord = &context;
397 dispatch.HistoryTable = &table;
398 prev_context = context;
399 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.u.s.X19;
401 for (;;)
403 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
404 if (status != STATUS_SUCCESS) return status;
406 unwind_done:
407 if (!dispatch.EstablisherFrame) break;
409 if (!is_valid_frame( dispatch.EstablisherFrame ))
411 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
412 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
413 rec->ExceptionFlags |= EH_STACK_INVALID;
414 break;
417 if (dispatch.LanguageHandler)
419 switch (call_handler( rec, orig_context, &dispatch ))
421 case ExceptionContinueExecution:
422 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
423 return STATUS_SUCCESS;
424 case ExceptionContinueSearch:
425 break;
426 case ExceptionNestedException:
427 FIXME( "nested exception\n" );
428 break;
429 case ExceptionCollidedUnwind: {
430 ULONG64 frame;
432 context = *dispatch.ContextRecord;
433 dispatch.ContextRecord = &context;
434 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
435 dispatch.ControlPc, dispatch.FunctionEntry,
436 &context, &dispatch.HandlerData, &frame, NULL );
437 goto unwind_done;
439 default:
440 return STATUS_INVALID_DISPOSITION;
443 /* hack: call wine handlers registered in the tib list */
444 else while ((ULONG64)teb_frame < context.Sp)
446 TRACE( "found wine frame %p rsp %lx handler %p\n",
447 teb_frame, context.Sp, teb_frame->Handler );
448 dispatch.EstablisherFrame = (ULONG64)teb_frame;
449 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
451 case ExceptionContinueExecution:
452 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
453 return STATUS_SUCCESS;
454 case ExceptionContinueSearch:
455 break;
456 case ExceptionNestedException:
457 FIXME( "nested exception\n" );
458 break;
459 case ExceptionCollidedUnwind: {
460 ULONG64 frame;
462 context = *dispatch.ContextRecord;
463 dispatch.ContextRecord = &context;
464 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
465 dispatch.ControlPc, dispatch.FunctionEntry,
466 &context, &dispatch.HandlerData, &frame, NULL );
467 teb_frame = teb_frame->Prev;
468 goto unwind_done;
470 default:
471 return STATUS_INVALID_DISPOSITION;
473 teb_frame = teb_frame->Prev;
476 if (context.Sp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
477 prev_context = context;
479 return STATUS_UNHANDLED_EXCEPTION;
483 /*******************************************************************
484 * KiUserExceptionDispatcher (NTDLL.@)
486 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
488 NTSTATUS status;
489 DWORD c;
491 TRACE( "code=%x flags=%x addr=%p pc=%lx tid=%04x\n",
492 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
493 context->Pc, GetCurrentThreadId() );
494 for (c = 0; c < rec->NumberParameters; c++)
495 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
497 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
499 if (rec->ExceptionInformation[1] >> 16)
500 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
501 rec->ExceptionAddress,
502 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
503 else
504 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
505 rec->ExceptionAddress,
506 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
508 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
510 WARN( "Thread %04x renamed to %s\n", (DWORD)rec->ExceptionInformation[2], debugstr_a((char *)rec->ExceptionInformation[1]) );
512 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
514 WARN( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
516 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
518 WARN( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
520 else
522 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
523 ERR( "%s exception (code=%x) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
524 else
525 WARN( "%s exception (code=%x) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
527 TRACE(" x0=%016lx x1=%016lx x2=%016lx x3=%016lx\n",
528 context->u.s.X0, context->u.s.X1, context->u.s.X2, context->u.s.X3 );
529 TRACE(" x4=%016lx x5=%016lx x6=%016lx x7=%016lx\n",
530 context->u.s.X4, context->u.s.X5, context->u.s.X6, context->u.s.X7 );
531 TRACE(" x8=%016lx x9=%016lx x10=%016lx x11=%016lx\n",
532 context->u.s.X8, context->u.s.X9, context->u.s.X10, context->u.s.X11 );
533 TRACE(" x12=%016lx x13=%016lx x14=%016lx x15=%016lx\n",
534 context->u.s.X12, context->u.s.X13, context->u.s.X14, context->u.s.X15 );
535 TRACE(" x16=%016lx x17=%016lx x18=%016lx x19=%016lx\n",
536 context->u.s.X16, context->u.s.X17, context->u.s.X18, context->u.s.X19 );
537 TRACE(" x20=%016lx x21=%016lx x22=%016lx x23=%016lx\n",
538 context->u.s.X20, context->u.s.X21, context->u.s.X22, context->u.s.X23 );
539 TRACE(" x24=%016lx x25=%016lx x26=%016lx x27=%016lx\n",
540 context->u.s.X24, context->u.s.X25, context->u.s.X26, context->u.s.X27 );
541 TRACE(" x28=%016lx fp=%016lx lr=%016lx sp=%016lx\n",
542 context->u.s.X28, context->u.s.Fp, context->u.s.Lr, context->Sp );
545 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
546 NtContinue( context, FALSE );
548 if ((status = call_function_handlers( rec, context )) == STATUS_SUCCESS)
549 NtContinue( context, FALSE );
551 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
552 return NtRaiseException( rec, context, FALSE );
556 /*******************************************************************
557 * KiUserApcDispatcher (NTDLL.@)
559 void WINAPI KiUserApcDispatcher( CONTEXT *context, ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3,
560 PNTAPCFUNC apc )
562 void (CALLBACK *func)(ULONG_PTR,ULONG_PTR,ULONG_PTR,CONTEXT*) = (void *)apc;
563 func( arg1, arg2, arg3, context );
564 NtContinue( context, TRUE );
568 /*******************************************************************
569 * KiUserCallbackDispatcher (NTDLL.@)
571 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
573 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
575 RtlRaiseStatus( NtCallbackReturn( NULL, 0, func( args, len )));
579 /***********************************************************************
580 * Definitions for Win32 unwind tables
583 struct unwind_info
585 DWORD function_length : 18;
586 DWORD version : 2;
587 DWORD x : 1;
588 DWORD e : 1;
589 DWORD epilog : 5;
590 DWORD codes : 5;
593 struct unwind_info_ext
595 WORD epilog;
596 BYTE codes;
597 BYTE reserved;
600 struct unwind_info_epilog
602 DWORD offset : 18;
603 DWORD res : 4;
604 DWORD index : 10;
607 static const BYTE unwind_code_len[256] =
609 /* 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,
610 /* 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,
611 /* 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,
612 /* 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,
613 /* 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,
614 /* 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,
615 /* 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,
616 /* 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
619 /***********************************************************************
620 * get_sequence_len
622 static unsigned int get_sequence_len( BYTE *ptr, BYTE *end )
624 unsigned int ret = 0;
626 while (ptr < end)
628 if (*ptr == 0xe4 || *ptr == 0xe5) break;
629 ptr += unwind_code_len[*ptr];
630 ret++;
632 return ret;
636 /***********************************************************************
637 * restore_regs
639 static void restore_regs( 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 >= 19) (&ptrs->X19)[reg + i - 19] = (DWORD64 *)context->Sp + i + offset;
646 context->u.X[reg + i] = ((DWORD64 *)context->Sp)[i + offset];
648 if (pos < 0) context->Sp += -8 * pos;
652 /***********************************************************************
653 * restore_fpregs
655 static void restore_fpregs( int reg, int count, int pos, CONTEXT *context,
656 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
658 int i, offset = max( 0, pos );
659 for (i = 0; i < count; i++)
661 if (ptrs && reg + i >= 8) (&ptrs->D8)[reg + i - 8] = (DWORD64 *)context->Sp + i + offset;
662 context->V[reg + i].D[0] = ((double *)context->Sp)[i + offset];
664 if (pos < 0) context->Sp += -8 * pos;
668 /***********************************************************************
669 * process_unwind_codes
671 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
672 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
674 unsigned int val, len, save_next = 2;
676 /* skip codes */
677 while (ptr < end && skip)
679 if (*ptr == 0xe4 || *ptr == 0xe5) break;
680 ptr += unwind_code_len[*ptr];
681 skip--;
684 while (ptr < end)
686 if ((len = unwind_code_len[*ptr]) > 1)
688 if (ptr + len > end) break;
689 val = ptr[0] * 0x100 + ptr[1];
691 else val = *ptr;
693 if (*ptr < 0x20) /* alloc_s */
694 context->Sp += 16 * (val & 0x1f);
695 else if (*ptr < 0x40) /* save_r19r20_x */
696 restore_regs( 19, save_next, -(val & 0x1f), context, ptrs );
697 else if (*ptr < 0x80) /* save_fplr */
698 restore_regs( 29, 2, val & 0x3f, context, ptrs );
699 else if (*ptr < 0xc0) /* save_fplr_x */
700 restore_regs( 29, 2, -(val & 0x3f) - 1, context, ptrs );
701 else if (*ptr < 0xc8) /* alloc_m */
702 context->Sp += 16 * (val & 0x7ff);
703 else if (*ptr < 0xcc) /* save_regp */
704 restore_regs( 19 + ((val >> 6) & 0xf), save_next, val & 0x3f, context, ptrs );
705 else if (*ptr < 0xd0) /* save_regp_x */
706 restore_regs( 19 + ((val >> 6) & 0xf), save_next, -(val & 0x3f) - 1, context, ptrs );
707 else if (*ptr < 0xd4) /* save_reg */
708 restore_regs( 19 + ((val >> 6) & 0xf), 1, val & 0x3f, context, ptrs );
709 else if (*ptr < 0xd6) /* save_reg_x */
710 restore_regs( 19 + ((val >> 5) & 0xf), 1, -(val & 0x1f) - 1, context, ptrs );
711 else if (*ptr < 0xd8) /* save_lrpair */
713 restore_regs( 19 + 2 * ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
714 restore_regs( 30, 1, (val & 0x3f) + 1, context, ptrs );
716 else if (*ptr < 0xda) /* save_fregp */
717 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, val & 0x3f, context, ptrs );
718 else if (*ptr < 0xdc) /* save_fregp_x */
719 restore_fpregs( 8 + ((val >> 6) & 0x7), save_next, -(val & 0x3f) - 1, context, ptrs );
720 else if (*ptr < 0xde) /* save_freg */
721 restore_fpregs( 8 + ((val >> 6) & 0x7), 1, val & 0x3f, context, ptrs );
722 else if (*ptr == 0xde) /* save_freg_x */
723 restore_fpregs( 8 + ((val >> 5) & 0x7), 1, -(val & 0x3f) - 1, context, ptrs );
724 else if (*ptr == 0xe0) /* alloc_l */
725 context->Sp += 16 * ((ptr[1] << 16) + (ptr[2] << 8) + ptr[3]);
726 else if (*ptr == 0xe1) /* set_fp */
727 context->Sp = context->u.s.Fp;
728 else if (*ptr == 0xe2) /* add_fp */
729 context->Sp = context->u.s.Fp - 8 * (val & 0xff);
730 else if (*ptr == 0xe3) /* nop */
731 /* nop */ ;
732 else if (*ptr == 0xe4) /* end */
733 break;
734 else if (*ptr == 0xe5) /* end_c */
735 break;
736 else if (*ptr == 0xe6) /* save_next */
738 save_next += 2;
739 ptr += len;
740 continue;
742 else if (*ptr == 0xe9) /* MSFT_OP_MACHINE_FRAME */
744 context->Pc = ((DWORD64 *)context->Sp)[1];
745 context->Sp = ((DWORD64 *)context->Sp)[0];
747 else if (*ptr == 0xea) /* MSFT_OP_CONTEXT */
749 memcpy( context, (DWORD64 *)context->Sp, sizeof(CONTEXT) );
751 else
753 WARN( "unsupported code %02x\n", *ptr );
754 return;
756 save_next = 2;
757 ptr += len;
762 /***********************************************************************
763 * unwind_packed_data
765 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
766 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
768 int i;
769 unsigned int len, offset, skip = 0;
770 unsigned int int_size = func->u.s.RegI * 8, fp_size = func->u.s.RegF * 8, regsave, local_size;
771 unsigned int int_regs, fp_regs, saved_regs, local_size_regs;
773 TRACE( "function %lx-%lx: len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
774 base + func->BeginAddress, base + func->BeginAddress + func->u.s.FunctionLength * 4,
775 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
776 func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
778 if (func->u.s.CR == 1) int_size += 8;
779 if (func->u.s.RegF) fp_size += 8;
781 regsave = ((int_size + fp_size + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
782 local_size = func->u.s.FrameSize * 16 - regsave;
784 int_regs = int_size / 8;
785 fp_regs = fp_size / 8;
786 saved_regs = regsave / 8;
787 local_size_regs = local_size / 8;
789 /* check for prolog/epilog */
790 if (func->u.s.Flag == 1)
792 offset = ((pc - base) - func->BeginAddress) / 4;
793 if (offset < 17 || offset >= func->u.s.FunctionLength - 15)
795 len = (int_size + 8) / 16 + (fp_size + 8) / 16;
796 switch (func->u.s.CR)
798 case 3:
799 len++; /* mov x29,sp */
800 len++; /* stp x29,lr,[sp,0] */
801 if (local_size <= 512) break;
802 /* fall through */
803 case 0:
804 case 1:
805 if (local_size) len++; /* sub sp,sp,#local_size */
806 if (local_size > 4088) len++; /* sub sp,sp,#4088 */
807 break;
809 len += 4 * func->u.s.H;
810 if (offset < len) /* prolog */
812 skip = len - offset;
814 else if (offset >= func->u.s.FunctionLength - (len + 1)) /* epilog */
816 skip = offset - (func->u.s.FunctionLength - (len + 1));
821 if (!skip)
823 if (func->u.s.CR == 3)
825 DWORD64 *fp = (DWORD64 *) context->u.s.Fp; /* u.X[29] */
826 context->Sp = context->u.s.Fp;
827 context->u.X[29] = fp[0];
828 context->u.X[30] = fp[1];
830 context->Sp += local_size;
831 if (fp_size) restore_fpregs( 8, fp_regs, int_regs, context, ptrs );
832 if (func->u.s.CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
833 restore_regs( 19, func->u.s.RegI, -saved_regs, context, ptrs );
835 else
837 unsigned int pos = 0;
839 switch (func->u.s.CR)
841 case 3:
842 /* mov x29,sp */
843 if (pos++ >= skip) context->Sp = context->u.s.Fp;
844 if (local_size <= 512)
846 /* stp x29,lr,[sp,-#local_size]! */
847 if (pos++ >= skip) restore_regs( 29, 2, -local_size_regs, context, ptrs );
848 break;
850 /* stp x29,lr,[sp,0] */
851 if (pos++ >= skip) restore_regs( 29, 2, 0, context, ptrs );
852 /* fall through */
853 case 0:
854 case 1:
855 if (!local_size) break;
856 /* sub sp,sp,#local_size */
857 if (pos++ >= skip) context->Sp += (local_size - 1) % 4088 + 1;
858 if (local_size > 4088 && pos++ >= skip) context->Sp += 4088;
859 break;
862 if (func->u.s.H) pos += 4;
864 if (fp_size)
866 if (func->u.s.RegF % 2 == 0 && pos++ >= skip)
867 /* str d%u,[sp,#fp_size] */
868 restore_fpregs( 8 + func->u.s.RegF, 1, int_regs + fp_regs - 1, context, ptrs );
869 for (i = (func->u.s.RegF + 1) / 2 - 1; i >= 0; i--)
871 if (pos++ < skip) continue;
872 if (!i && !int_size)
873 /* stp d8,d9,[sp,-#regsave]! */
874 restore_fpregs( 8, 2, -saved_regs, context, ptrs );
875 else
876 /* stp dn,dn+1,[sp,#offset] */
877 restore_fpregs( 8 + 2 * i, 2, int_regs + 2 * i, context, ptrs );
881 if (func->u.s.RegI % 2)
883 if (pos++ >= skip)
885 /* stp xn,lr,[sp,#offset] */
886 if (func->u.s.CR == 1) restore_regs( 30, 1, int_regs - 1, context, ptrs );
887 /* str xn,[sp,#offset] */
888 restore_regs( 18 + func->u.s.RegI, 1,
889 (func->u.s.RegI > 1) ? func->u.s.RegI - 1 : -saved_regs,
890 context, ptrs );
893 else if (func->u.s.CR == 1)
895 /* str lr,[sp,#offset] */
896 if (pos++ >= skip) restore_regs( 30, 1, func->u.s.RegI ? int_regs - 1 : -saved_regs, context, ptrs );
899 for (i = func->u.s.RegI/ 2 - 1; i >= 0; i--)
901 if (pos++ < skip) continue;
902 if (i)
903 /* stp xn,xn+1,[sp,#offset] */
904 restore_regs( 19 + 2 * i, 2, 2 * i, context, ptrs );
905 else
906 /* stp x19,x20,[sp,-#regsave]! */
907 restore_regs( 19, 2, -saved_regs, context, ptrs );
910 return NULL;
914 /***********************************************************************
915 * unwind_full_data
917 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
918 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
920 struct unwind_info *info;
921 struct unwind_info_epilog *info_epilog;
922 unsigned int i, codes, epilogs, len, offset;
923 void *data;
924 BYTE *end;
926 info = (struct unwind_info *)((char *)base + func->u.UnwindData);
927 data = info + 1;
928 epilogs = info->epilog;
929 codes = info->codes;
930 if (!codes && !epilogs)
932 struct unwind_info_ext *infoex = data;
933 codes = infoex->codes;
934 epilogs = infoex->epilog;
935 data = infoex + 1;
937 info_epilog = data;
938 if (!info->e) data = info_epilog + epilogs;
940 offset = ((pc - base) - func->BeginAddress) / 4;
941 end = (BYTE *)data + codes * 4;
943 TRACE( "function %lx-%lx: len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
944 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 4,
945 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
947 /* check for prolog */
948 if (offset < codes * 4)
950 len = get_sequence_len( data, end );
951 if (offset < len)
953 process_unwind_codes( data, end, context, ptrs, len - offset );
954 return NULL;
958 /* check for epilog */
959 if (!info->e)
961 for (i = 0; i < epilogs; i++)
963 if (offset < info_epilog[i].offset) break;
964 if (offset - info_epilog[i].offset < codes * 4 - info_epilog[i].index)
966 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
967 len = get_sequence_len( ptr, end );
968 if (offset <= info_epilog[i].offset + len)
970 process_unwind_codes( ptr, end, context, ptrs, offset - info_epilog[i].offset );
971 return NULL;
976 else if (info->function_length - offset <= codes * 4 - epilogs)
978 BYTE *ptr = (BYTE *)data + epilogs;
979 len = get_sequence_len( ptr, end ) + 1;
980 if (offset >= info->function_length - len)
982 process_unwind_codes( ptr, end, context, ptrs, offset - (info->function_length - len) );
983 return NULL;
987 process_unwind_codes( data, end, context, ptrs, 0 );
989 /* get handler since we are inside the main code */
990 if (info->x)
992 DWORD *handler_rva = (DWORD *)data + codes;
993 *handler_data = handler_rva + 1;
994 return (char *)base + *handler_rva;
996 return NULL;
1000 /**********************************************************************
1001 * RtlVirtualUnwind (NTDLL.@)
1003 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
1004 RUNTIME_FUNCTION *func, CONTEXT *context,
1005 PVOID *handler_data, ULONG_PTR *frame_ret,
1006 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
1008 void *handler;
1010 TRACE( "type %x pc %lx sp %lx func %lx\n", type, pc, context->Sp, base + func->BeginAddress );
1012 *handler_data = NULL;
1014 context->Pc = 0;
1015 if (func->u.s.Flag)
1016 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1017 else
1018 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1020 TRACE( "ret: lr=%lx sp=%lx handler=%p\n", context->u.s.Lr, context->Sp, handler );
1021 if (!context->Pc)
1022 context->Pc = context->u.s.Lr;
1023 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1024 *frame_ret = context->Sp;
1025 return handler;
1028 /**********************************************************************
1029 * call_consolidate_callback
1031 * Wrapper function to call a consolidate callback from a fake frame.
1032 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1033 * we have to skip all frames which were already processed. To do that we
1034 * trick the unwinding functions into thinking the call came from somewhere
1035 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1036 * DW_CFA_expression, and the expressions have the following format:
1038 * DW_OP_breg31; sleb128 <OFFSET> | Load x31 + struct member offset
1039 * [DW_OP_deref] | Dereference, only for CFA
1041 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1042 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1043 EXCEPTION_RECORD *rec,
1044 TEB *teb );
1045 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1046 "stp x29, x30, [sp, #-0x30]!\n\t"
1047 __ASM_CFI(".cfi_def_cfa_offset 48\n\t")
1048 __ASM_CFI(".cfi_offset 29, -48\n\t")
1049 __ASM_CFI(".cfi_offset 30, -40\n\t")
1050 __ASM_SEH(".seh_nop\n\t")
1051 "stp x1, x2, [sp, #0x10]\n\t"
1052 __ASM_SEH(".seh_nop\n\t")
1053 "str x3, [sp, #0x20]\n\t"
1054 __ASM_SEH(".seh_nop\n\t")
1055 "mov x29, sp\n\t"
1056 __ASM_CFI(".cfi_def_cfa_register 29\n\t")
1057 __ASM_SEH(".seh_nop\n\t")
1058 __ASM_CFI(".cfi_remember_state\n\t")
1059 /* Memcpy the context onto the stack */
1060 "sub sp, sp, #0x390\n\t"
1061 __ASM_SEH(".seh_nop\n\t")
1062 "mov x1, x0\n\t"
1063 __ASM_SEH(".seh_nop\n\t")
1064 "mov x0, sp\n\t"
1065 __ASM_SEH(".seh_nop\n\t")
1066 "mov x2, #0x390\n\t"
1067 __ASM_SEH(".seh_nop\n\t")
1068 "bl " __ASM_NAME("memcpy") "\n\t"
1069 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1070 __ASM_CFI(".cfi_escape 0x0f,0x04,0x8f,0x80,0x02,0x06\n\t") /* CFA, DW_OP_breg31 + 0x100, DW_OP_deref */
1071 __ASM_CFI(".cfi_escape 0x10,0x13,0x03,0x8f,0xa0,0x01\n\t") /* x19, DW_OP_breg31 + 0xA0 */
1072 __ASM_CFI(".cfi_escape 0x10,0x14,0x03,0x8f,0xa8,0x01\n\t") /* x20 */
1073 __ASM_CFI(".cfi_escape 0x10,0x15,0x03,0x8f,0xb0,0x01\n\t") /* x21 */
1074 __ASM_CFI(".cfi_escape 0x10,0x16,0x03,0x8f,0xb8,0x01\n\t") /* x22 */
1075 __ASM_CFI(".cfi_escape 0x10,0x17,0x03,0x8f,0xc0,0x01\n\t") /* x23 */
1076 __ASM_CFI(".cfi_escape 0x10,0x18,0x03,0x8f,0xc8,0x01\n\t") /* x24 */
1077 __ASM_CFI(".cfi_escape 0x10,0x19,0x03,0x8f,0xd0,0x01\n\t") /* x25 */
1078 __ASM_CFI(".cfi_escape 0x10,0x1a,0x03,0x8f,0xd8,0x01\n\t") /* x26 */
1079 __ASM_CFI(".cfi_escape 0x10,0x1b,0x03,0x8f,0xe0,0x01\n\t") /* x27 */
1080 __ASM_CFI(".cfi_escape 0x10,0x1c,0x03,0x8f,0xe8,0x01\n\t") /* x28 */
1081 __ASM_CFI(".cfi_escape 0x10,0x1d,0x03,0x8f,0xf0,0x01\n\t") /* x29 */
1082 __ASM_CFI(".cfi_escape 0x10,0x1e,0x03,0x8f,0xf8,0x01\n\t") /* x30 */
1083 __ASM_CFI(".cfi_escape 0x10,0x48,0x03,0x8f,0x90,0x03\n\t") /* d8 */
1084 __ASM_CFI(".cfi_escape 0x10,0x49,0x03,0x8f,0x98,0x03\n\t") /* d9 */
1085 __ASM_CFI(".cfi_escape 0x10,0x4a,0x03,0x8f,0xa0,0x03\n\t") /* d10 */
1086 __ASM_CFI(".cfi_escape 0x10,0x4b,0x03,0x8f,0xa8,0x03\n\t") /* d11 */
1087 __ASM_CFI(".cfi_escape 0x10,0x4c,0x03,0x8f,0xb0,0x03\n\t") /* d12 */
1088 __ASM_CFI(".cfi_escape 0x10,0x4d,0x03,0x8f,0xb8,0x03\n\t") /* d13 */
1089 __ASM_CFI(".cfi_escape 0x10,0x4e,0x03,0x8f,0xc0,0x03\n\t") /* d14 */
1090 __ASM_CFI(".cfi_escape 0x10,0x4f,0x03,0x8f,0xc8,0x03\n\t") /* d15 */
1091 __ASM_SEH(".seh_context\n\t")
1092 __ASM_SEH(".seh_endprologue\n\t")
1093 "ldp x1, x2, [x29, #0x10]\n\t"
1094 "ldr x18, [x29, #0x20]\n\t"
1095 "mov x0, x2\n\t"
1096 "blr x1\n\t"
1097 "mov sp, x29\n\t"
1098 __ASM_CFI(".cfi_restore_state\n\t")
1099 "ldp x29, x30, [sp], #48\n\t"
1100 __ASM_CFI(".cfi_restore 30\n\t")
1101 __ASM_CFI(".cfi_restore 29\n\t")
1102 __ASM_CFI(".cfi_def_cfa 31, 0\n\t")
1103 "ret")
1105 /*******************************************************************
1106 * RtlRestoreContext (NTDLL.@)
1108 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1110 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1112 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1114 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1115 int i;
1117 context->u.s.X19 = jmp->X19;
1118 context->u.s.X20 = jmp->X20;
1119 context->u.s.X21 = jmp->X21;
1120 context->u.s.X22 = jmp->X22;
1121 context->u.s.X23 = jmp->X23;
1122 context->u.s.X24 = jmp->X24;
1123 context->u.s.X25 = jmp->X25;
1124 context->u.s.X26 = jmp->X26;
1125 context->u.s.X27 = jmp->X27;
1126 context->u.s.X28 = jmp->X28;
1127 context->u.s.Fp = jmp->Fp;
1128 context->u.s.Lr = jmp->Lr;
1129 context->Sp = jmp->Sp;
1130 context->Fpcr = jmp->Fpcr;
1131 context->Fpsr = jmp->Fpsr;
1133 for (i = 0; i < 8; i++)
1134 context->V[8+i].D[0] = jmp->D[i];
1136 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1138 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1139 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1140 rec->ExceptionInformation[10] = (ULONG_PTR)&context->u.s.X19;
1142 context->Pc = (ULONG64)call_consolidate_callback( context, consolidate, rec, NtCurrentTeb() );
1145 /* hack: remove no longer accessible TEB frames */
1146 while ((ULONG64)teb_frame < context->Sp)
1148 TRACE( "removing TEB frame: %p\n", teb_frame );
1149 teb_frame = __wine_pop_frame( teb_frame );
1152 TRACE( "returning to %lx stack %lx\n", context->Pc, context->Sp );
1153 NtContinue( context, FALSE );
1156 /*******************************************************************
1157 * RtlUnwindEx (NTDLL.@)
1159 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1160 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1162 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1163 EXCEPTION_RECORD record;
1164 DISPATCHER_CONTEXT dispatch;
1165 CONTEXT new_context;
1166 NTSTATUS status;
1167 DWORD i;
1169 RtlCaptureContext( context );
1170 new_context = *context;
1172 /* build an exception record, if we do not have one */
1173 if (!rec)
1175 record.ExceptionCode = STATUS_UNWIND;
1176 record.ExceptionFlags = 0;
1177 record.ExceptionRecord = NULL;
1178 record.ExceptionAddress = (void *)context->Pc;
1179 record.NumberParameters = 0;
1180 rec = &record;
1183 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1185 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p pc=%016lx\n",
1186 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1187 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1188 TRACE( " info[%d]=%016lx\n", i, rec->ExceptionInformation[i] );
1189 TRACE(" x0=%016lx x1=%016lx x2=%016lx x3=%016lx\n",
1190 context->u.s.X0, context->u.s.X1, context->u.s.X2, context->u.s.X3 );
1191 TRACE(" x4=%016lx x5=%016lx x6=%016lx x7=%016lx\n",
1192 context->u.s.X4, context->u.s.X5, context->u.s.X6, context->u.s.X7 );
1193 TRACE(" x8=%016lx x9=%016lx x10=%016lx x11=%016lx\n",
1194 context->u.s.X8, context->u.s.X9, context->u.s.X10, context->u.s.X11 );
1195 TRACE(" x12=%016lx x13=%016lx x14=%016lx x15=%016lx\n",
1196 context->u.s.X12, context->u.s.X13, context->u.s.X14, context->u.s.X15 );
1197 TRACE(" x16=%016lx x17=%016lx x18=%016lx x19=%016lx\n",
1198 context->u.s.X16, context->u.s.X17, context->u.s.X18, context->u.s.X19 );
1199 TRACE(" x20=%016lx x21=%016lx x22=%016lx x23=%016lx\n",
1200 context->u.s.X20, context->u.s.X21, context->u.s.X22, context->u.s.X23 );
1201 TRACE(" x24=%016lx x25=%016lx x26=%016lx x27=%016lx\n",
1202 context->u.s.X24, context->u.s.X25, context->u.s.X26, context->u.s.X27 );
1203 TRACE(" x28=%016lx fp=%016lx lr=%016lx sp=%016lx\n",
1204 context->u.s.X28, context->u.s.Fp, context->u.s.Lr, context->Sp );
1206 dispatch.TargetPc = (ULONG64)target_ip;
1207 dispatch.ContextRecord = context;
1208 dispatch.HistoryTable = table;
1209 dispatch.NonVolatileRegisters = (BYTE *)&context->u.s.X19;
1211 for (;;)
1213 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1214 if (status != STATUS_SUCCESS) raise_status( status, rec );
1216 unwind_done:
1217 if (!dispatch.EstablisherFrame) break;
1219 if (!is_valid_frame( dispatch.EstablisherFrame ))
1221 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1222 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1223 rec->ExceptionFlags |= EH_STACK_INVALID;
1224 break;
1227 if (dispatch.LanguageHandler)
1229 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
1231 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
1232 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1234 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1235 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1237 ULONG64 frame;
1239 *context = new_context = *dispatch.ContextRecord;
1240 dispatch.ContextRecord = context;
1241 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1242 dispatch.ControlPc, dispatch.FunctionEntry,
1243 &new_context, &dispatch.HandlerData, &frame,
1244 NULL );
1245 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1246 goto unwind_done;
1248 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1250 else /* hack: call builtin handlers registered in the tib list */
1252 DWORD64 backup_frame = dispatch.EstablisherFrame;
1253 while ((ULONG64)teb_frame < new_context.Sp && (ULONG64)teb_frame < (ULONG64)end_frame)
1255 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1256 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1257 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1259 ULONG64 frame;
1261 teb_frame = __wine_pop_frame( teb_frame );
1263 *context = new_context = *dispatch.ContextRecord;
1264 dispatch.ContextRecord = context;
1265 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1266 dispatch.ControlPc, dispatch.FunctionEntry,
1267 &new_context, &dispatch.HandlerData,
1268 &frame, NULL );
1269 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1270 goto unwind_done;
1272 teb_frame = __wine_pop_frame( teb_frame );
1274 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Sp) break;
1275 dispatch.EstablisherFrame = backup_frame;
1278 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
1279 *context = new_context;
1282 context->u.s.X0 = (ULONG64)retval;
1283 context->Pc = (ULONG64)target_ip;
1284 RtlRestoreContext(context, rec);
1288 /***********************************************************************
1289 * RtlUnwind (NTDLL.@)
1291 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1293 CONTEXT context;
1294 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1297 /*******************************************************************
1298 * _local_unwind (NTDLL.@)
1300 void WINAPI _local_unwind( void *frame, void *target_ip )
1302 CONTEXT context;
1303 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1306 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1307 PEXCEPTION_FILTER filter,
1308 PUCHAR nonvolatile);
1309 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1310 "stp x29, x30, [sp, #-96]!\n\t"
1311 __ASM_SEH(".seh_save_fplr_x 96\n\t")
1312 "stp x19, x20, [sp, #16]\n\t"
1313 __ASM_SEH(".seh_save_regp x19, 16\n\t")
1314 "stp x21, x22, [sp, #32]\n\t"
1315 __ASM_SEH(".seh_save_regp x21, 32\n\t")
1316 "stp x23, x24, [sp, #48]\n\t"
1317 __ASM_SEH(".seh_save_regp x23, 48\n\t")
1318 "stp x25, x26, [sp, #64]\n\t"
1319 __ASM_SEH(".seh_save_regp x25, 64\n\t")
1320 "stp x27, x28, [sp, #80]\n\t"
1321 __ASM_SEH(".seh_save_regp x27, 80\n\t")
1322 "mov x29, sp\n\t"
1323 __ASM_SEH(".seh_set_fp\n\t")
1324 __ASM_SEH(".seh_endprologue\n\t")
1326 __ASM_CFI(".cfi_def_cfa x29, 96\n\t")
1327 __ASM_CFI(".cfi_offset x29, -96\n\t")
1328 __ASM_CFI(".cfi_offset x30, -88\n\t")
1329 __ASM_CFI(".cfi_offset x19, -80\n\t")
1330 __ASM_CFI(".cfi_offset x20, -72\n\t")
1331 __ASM_CFI(".cfi_offset x21, -64\n\t")
1332 __ASM_CFI(".cfi_offset x22, -56\n\t")
1333 __ASM_CFI(".cfi_offset x23, -48\n\t")
1334 __ASM_CFI(".cfi_offset x24, -40\n\t")
1335 __ASM_CFI(".cfi_offset x25, -32\n\t")
1336 __ASM_CFI(".cfi_offset x26, -24\n\t")
1337 __ASM_CFI(".cfi_offset x27, -16\n\t")
1338 __ASM_CFI(".cfi_offset x28, -8\n\t")
1340 "ldp x19, x20, [x3, #0]\n\t"
1341 "ldp x21, x22, [x3, #16]\n\t"
1342 "ldp x23, x24, [x3, #32]\n\t"
1343 "ldp x25, x26, [x3, #48]\n\t"
1344 "ldp x27, x28, [x3, #64]\n\t"
1345 /* Overwrite the frame parameter with Fp from the
1346 * nonvolatile regs */
1347 "ldr x1, [x3, #80]\n\t"
1348 "blr x2\n\t"
1349 "ldp x19, x20, [sp, #16]\n\t"
1350 "ldp x21, x22, [sp, #32]\n\t"
1351 "ldp x23, x24, [sp, #48]\n\t"
1352 "ldp x25, x26, [sp, #64]\n\t"
1353 "ldp x27, x28, [sp, #80]\n\t"
1354 "ldp x29, x30, [sp], #96\n\t"
1355 "ret")
1357 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1358 PTERMINATION_HANDLER handler,
1359 PUCHAR nonvolatile);
1360 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1361 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1362 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1364 /*******************************************************************
1365 * __C_specific_handler (NTDLL.@)
1367 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1368 void *frame,
1369 CONTEXT *context,
1370 struct _DISPATCHER_CONTEXT *dispatch )
1372 SCOPE_TABLE *table = dispatch->HandlerData;
1373 ULONG i;
1374 DWORD64 ControlPc = dispatch->ControlPc;
1376 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1377 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1379 if (dispatch->ControlPcIsUnwound)
1380 ControlPc -= 4;
1382 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1384 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1386 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1387 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1389 PTERMINATION_HANDLER handler;
1391 if (table->ScopeRecord[i].JumpTarget) continue;
1393 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1394 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1395 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1397 break;
1400 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1401 dispatch->ScopeIndex = i+1;
1403 TRACE( "calling __finally %p frame %p\n", handler, frame );
1404 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1405 dispatch->NonVolatileRegisters );
1408 return ExceptionContinueSearch;
1411 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1413 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1414 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1416 if (!table->ScopeRecord[i].JumpTarget) continue;
1417 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1419 EXCEPTION_POINTERS ptrs;
1420 PEXCEPTION_FILTER filter;
1422 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1423 ptrs.ExceptionRecord = rec;
1424 ptrs.ContextRecord = context;
1425 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1426 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1427 dispatch->NonVolatileRegisters ))
1429 case EXCEPTION_EXECUTE_HANDLER:
1430 break;
1431 case EXCEPTION_CONTINUE_SEARCH:
1432 continue;
1433 case EXCEPTION_CONTINUE_EXECUTION:
1434 return ExceptionContinueExecution;
1437 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1438 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1439 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1442 return ExceptionContinueSearch;
1446 /***********************************************************************
1447 * RtlRaiseException (NTDLL.@)
1449 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
1450 "sub sp, sp, #0x3b0\n\t" /* 0x390 (context) + 0x20 */
1451 "stp x29, x30, [sp]\n\t"
1452 __ASM_SEH(".seh_stackalloc 0x3b0\n\t")
1453 __ASM_SEH(".seh_save_fplr 0\n\t")
1454 __ASM_SEH(".seh_endprologue\n\t")
1455 __ASM_CFI(".cfi_def_cfa x29, 944\n\t")
1456 __ASM_CFI(".cfi_offset x30, -936\n\t")
1457 __ASM_CFI(".cfi_offset x29, -944\n\t")
1458 "mov x29, sp\n\t"
1459 "str x0, [sp, #0x10]\n\t"
1460 "add x0, sp, #0x20\n\t"
1461 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1462 "add x1, sp, #0x20\n\t" /* context pointer */
1463 "add x2, sp, #0x3b0\n\t" /* orig stack pointer */
1464 "str x2, [x1, #0x100]\n\t" /* context->Sp */
1465 "ldr x0, [sp, #0x10]\n\t" /* original first parameter */
1466 "str x0, [x1, #0x08]\n\t" /* context->X0 */
1467 "ldp x4, x5, [sp]\n\t" /* frame pointer, return address */
1468 "stp x4, x5, [x1, #0xf0]\n\t" /* context->Fp, Lr */
1469 "str x5, [x1, #0x108]\n\t" /* context->Pc */
1470 "str x5, [x0, #0x10]\n\t" /* rec->ExceptionAddress */
1471 "mov x2, #1\n\t"
1472 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1473 "bl " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1475 /*************************************************************************
1476 * RtlCaptureStackBackTrace (NTDLL.@)
1478 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1480 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
1481 return 0;
1484 /***********************************************************************
1485 * signal_start_thread
1487 __ASM_GLOBAL_FUNC( signal_start_thread,
1488 "mov sp, x0\n\t" /* context */
1489 "mov x1, #1\n\t"
1490 "b " __ASM_NAME("NtContinue") )
1492 /**********************************************************************
1493 * DbgBreakPoint (NTDLL.@)
1495 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "brk #0xf000; ret"
1496 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1497 "\n\tnop; nop; nop; nop; nop; nop" );
1499 /**********************************************************************
1500 * DbgUserBreakPoint (NTDLL.@)
1502 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "brk #0xf000; ret"
1503 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1504 "\n\tnop; nop; nop; nop; nop; nop" );
1506 /**********************************************************************
1507 * NtCurrentTeb (NTDLL.@)
1509 TEB * WINAPI NtCurrentTeb(void)
1511 return unix_funcs->NtCurrentTeb();
1514 #endif /* __aarch64__ */