mfplat/sample: Optimize copying to 2d buffer.
[wine.git] / dlls / ntdll / signal_arm.c
blob9a9fc6483f1a1a31e4dcce0667f601874b253437
1 /*
2 * ARM signal handling routines
4 * Copyright 2002 Marcus Meissner, SuSE Linux AG
5 * Copyright 2010-2013, 2015 André Hentschel
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifdef __arm__
24 #include <stdlib.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winternl.h"
31 #include "wine/exception.h"
32 #include "ntdll_misc.h"
33 #include "wine/debug.h"
34 #include "winnt.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(seh);
37 WINE_DECLARE_DEBUG_CHANNEL(relay);
38 WINE_DECLARE_DEBUG_CHANNEL(threadname);
40 typedef struct _SCOPE_TABLE
42 ULONG Count;
43 struct
45 ULONG BeginAddress;
46 ULONG EndAddress;
47 ULONG HandlerAddress;
48 ULONG JumpTarget;
49 } ScopeRecord[1];
50 } SCOPE_TABLE, *PSCOPE_TABLE;
53 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
54 struct MSVCRT_JUMP_BUFFER
56 unsigned long Frame;
57 unsigned long R4;
58 unsigned long R5;
59 unsigned long R6;
60 unsigned long R7;
61 unsigned long R8;
62 unsigned long R9;
63 unsigned long R10;
64 unsigned long R11;
65 unsigned long Sp;
66 unsigned long Pc;
67 unsigned long Fpscr;
68 unsigned long long D[8];
72 static void dump_scope_table( ULONG base, const SCOPE_TABLE *table )
74 unsigned int i;
76 TRACE( "scope table at %p\n", table );
77 for (i = 0; i < table->Count; i++)
78 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
79 base + table->ScopeRecord[i].BeginAddress,
80 base + table->ScopeRecord[i].EndAddress,
81 base + table->ScopeRecord[i].HandlerAddress,
82 base + table->ScopeRecord[i].JumpTarget );
85 /*******************************************************************
86 * is_valid_frame
88 static inline BOOL is_valid_frame( ULONG_PTR frame )
90 if (frame & 3) return FALSE;
91 return ((void *)frame >= NtCurrentTeb()->Tib.StackLimit &&
92 (void *)frame <= NtCurrentTeb()->Tib.StackBase);
96 /**************************************************************************
97 * __chkstk (NTDLL.@)
99 * Incoming r4 contains words to allocate, converting to bytes then return
101 __ASM_GLOBAL_FUNC( __chkstk, "lsl r4, r4, #2\n\t"
102 "bx lr" )
104 /***********************************************************************
105 * RtlCaptureContext (NTDLL.@)
107 __ASM_GLOBAL_FUNC( RtlCaptureContext,
108 "str r1, [r0, #0x8]\n\t" /* context->R1 */
109 "mov r1, #0x0200000\n\t" /* CONTEXT_ARM */
110 "add r1, r1, #0x7\n\t" /* CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT */
111 "str r1, [r0]\n\t" /* context->ContextFlags */
112 "str SP, [r0, #0x38]\n\t" /* context->Sp */
113 "str LR, [r0, #0x40]\n\t" /* context->Pc */
114 "mrs r1, CPSR\n\t"
115 "bfi r1, lr, #5, #1\n\t" /* Thumb bit */
116 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
117 "mov r1, #0\n\t"
118 "str r1, [r0, #0x4]\n\t" /* context->R0 */
119 "str r1, [r0, #0x3c]\n\t" /* context->Lr */
120 "add r0, #0x0c\n\t"
121 "stm r0, {r2-r12}\n\t" /* context->R2..R12 */
122 #ifndef __SOFTFP__
123 "add r0, #0x44\n\t" /* 0x50 - 0x0c */
124 "vstm r0, {d0-d15}\n\t" /* context->D0-D15 */
125 #endif
126 "bx lr" )
129 /**********************************************************************
130 * virtual_unwind
132 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
134 LDR_DATA_TABLE_ENTRY *module;
135 NTSTATUS status;
136 DWORD pc;
138 dispatch->ImageBase = 0;
139 dispatch->ScopeIndex = 0;
140 dispatch->EstablisherFrame = 0;
141 dispatch->ControlPc = context->Pc;
143 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
144 * signal frame.
146 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
147 pc = context->Pc - (dispatch->ControlPcIsUnwound ? 2 : 0);
149 /* first look for PE exception information */
151 if ((dispatch->FunctionEntry = lookup_function_info(pc,
152 (ULONG_PTR*)&dispatch->ImageBase, &module )))
154 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, pc,
155 dispatch->FunctionEntry, context,
156 &dispatch->HandlerData, (ULONG_PTR *)&dispatch->EstablisherFrame,
157 NULL );
158 return STATUS_SUCCESS;
161 /* then look for host system exception information */
163 if (!module || (module->Flags & LDR_WINE_INTERNAL))
165 struct unwind_builtin_dll_params params = { type, dispatch, context };
167 status = WINE_UNIX_CALL( unix_unwind_builtin_dll, &params );
168 if (status != STATUS_SUCCESS) return status;
170 if (dispatch->EstablisherFrame)
172 dispatch->FunctionEntry = NULL;
173 if (dispatch->LanguageHandler && !module)
175 FIXME( "calling personality routine in system library not supported yet\n" );
176 dispatch->LanguageHandler = NULL;
178 return STATUS_SUCCESS;
181 else
183 status = context->Pc != context->Lr ?
184 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
185 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
186 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
187 (void*) context->Lr, status );
188 dispatch->EstablisherFrame = context->Sp;
189 dispatch->LanguageHandler = NULL;
190 context->Pc = context->Lr;
191 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
192 return status;
195 dispatch->EstablisherFrame = context->Sp;
196 dispatch->LanguageHandler = NULL;
197 context->Pc = context->Lr;
198 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
199 return STATUS_SUCCESS;
203 struct unwind_exception_frame
205 EXCEPTION_REGISTRATION_RECORD frame;
206 DISPATCHER_CONTEXT *dispatch;
209 /**********************************************************************
210 * unwind_exception_handler
212 * Handler for exceptions happening while calling an unwind handler.
214 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
215 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
217 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
218 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
220 /* copy the original dispatcher into the current one, except for the TargetIp */
221 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
222 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
223 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
224 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
225 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
226 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
227 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
228 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
229 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
230 TRACE( "detected collided unwind\n" );
231 return ExceptionCollidedUnwind;
234 /**********************************************************************
235 * call_unwind_handler
237 * Call a single unwind handler.
239 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
241 struct unwind_exception_frame frame;
242 DWORD res;
244 frame.frame.Handler = unwind_exception_handler;
245 frame.dispatch = dispatch;
246 __wine_push_frame( &frame.frame );
248 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
249 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
250 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
251 TRACE( "handler %p returned %lx\n", dispatch->LanguageHandler, res );
253 __wine_pop_frame( &frame.frame );
255 switch (res)
257 case ExceptionContinueSearch:
258 case ExceptionCollidedUnwind:
259 break;
260 default:
261 raise_status( STATUS_INVALID_DISPOSITION, rec );
262 break;
265 return res;
269 /**********************************************************************
270 * call_teb_unwind_handler
272 * Call a single unwind handler from the TEB chain.
274 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
275 EXCEPTION_REGISTRATION_RECORD *teb_frame )
277 DWORD res;
279 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
280 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
281 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
282 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
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 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
299 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
301 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
302 rec->ExceptionFlags |= EH_NESTED_CALL;
304 return ExceptionContinueSearch;
308 /**********************************************************************
309 * call_handler
311 * Call a single exception handler.
312 * FIXME: Handle nested exceptions.
314 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
316 EXCEPTION_REGISTRATION_RECORD frame;
317 DWORD res;
319 frame.Handler = nested_exception_handler;
320 __wine_push_frame( &frame );
322 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
323 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
324 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
325 TRACE( "handler at %p returned %lu\n", dispatch->LanguageHandler, res );
327 rec->ExceptionFlags &= EH_NONCONTINUABLE;
328 __wine_pop_frame( &frame );
329 return res;
333 /**********************************************************************
334 * call_teb_handler
336 * Call a single exception handler from the TEB chain.
337 * FIXME: Handle nested exceptions.
339 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
340 EXCEPTION_REGISTRATION_RECORD *teb_frame )
342 DWORD res;
344 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
345 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
346 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
347 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
348 return res;
352 /**********************************************************************
353 * call_function_handlers
355 * Call the per-function handlers.
357 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
359 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
360 UNWIND_HISTORY_TABLE table;
361 DISPATCHER_CONTEXT dispatch;
362 CONTEXT context, prev_context;
363 NTSTATUS status;
365 context = *orig_context;
366 dispatch.TargetPc = 0;
367 dispatch.ContextRecord = &context;
368 dispatch.HistoryTable = &table;
369 prev_context = context;
370 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.R4;
372 for (;;)
374 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
375 if (status != STATUS_SUCCESS) return status;
377 unwind_done:
378 if (!dispatch.EstablisherFrame) break;
380 if (!is_valid_frame( dispatch.EstablisherFrame ))
382 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
383 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
384 rec->ExceptionFlags |= EH_STACK_INVALID;
385 break;
388 if (dispatch.LanguageHandler)
390 switch (call_handler( rec, orig_context, &dispatch ))
392 case ExceptionContinueExecution:
393 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
394 return STATUS_SUCCESS;
395 case ExceptionContinueSearch:
396 break;
397 case ExceptionNestedException:
398 FIXME( "nested exception\n" );
399 break;
400 case ExceptionCollidedUnwind: {
401 ULONG_PTR frame;
403 context = *dispatch.ContextRecord;
404 dispatch.ContextRecord = &context;
405 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
406 dispatch.ControlPc, dispatch.FunctionEntry,
407 &context, (PVOID *)&dispatch.HandlerData, &frame, NULL );
408 goto unwind_done;
410 default:
411 return STATUS_INVALID_DISPOSITION;
414 /* hack: call wine handlers registered in the tib list */
415 else while ((DWORD)teb_frame < context.Sp)
417 TRACE( "found wine frame %p rsp %lx handler %p\n",
418 teb_frame, context.Sp, teb_frame->Handler );
419 dispatch.EstablisherFrame = (DWORD)teb_frame;
420 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
422 case ExceptionContinueExecution:
423 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
424 return STATUS_SUCCESS;
425 case ExceptionContinueSearch:
426 break;
427 case ExceptionNestedException:
428 FIXME( "nested exception\n" );
429 break;
430 case ExceptionCollidedUnwind: {
431 ULONG_PTR frame;
433 context = *dispatch.ContextRecord;
434 dispatch.ContextRecord = &context;
435 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
436 dispatch.ControlPc, dispatch.FunctionEntry,
437 &context, (PVOID *)&dispatch.HandlerData, &frame, NULL );
438 teb_frame = teb_frame->Prev;
439 goto unwind_done;
441 default:
442 return STATUS_INVALID_DISPOSITION;
444 teb_frame = teb_frame->Prev;
447 if (context.Sp == (DWORD)NtCurrentTeb()->Tib.StackBase) break;
448 prev_context = context;
450 return STATUS_UNHANDLED_EXCEPTION;
454 /*******************************************************************
455 * KiUserExceptionDispatcher (NTDLL.@)
457 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
459 NTSTATUS status;
460 DWORD c;
462 TRACE( "code=%lx flags=%lx addr=%p pc=%08lx\n",
463 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Pc );
464 for (c = 0; c < rec->NumberParameters; c++)
465 TRACE( " info[%ld]=%08Ix\n", c, rec->ExceptionInformation[c] );
467 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
469 if (rec->ExceptionInformation[1] >> 16)
470 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
471 rec->ExceptionAddress,
472 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
473 else
474 MESSAGE( "wine: Call from %p to unimplemented function %s.%Id, aborting\n",
475 rec->ExceptionAddress,
476 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
478 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
480 if ((DWORD)rec->ExceptionInformation[2] == -1 || (DWORD)rec->ExceptionInformation[2] == GetCurrentThreadId())
481 WARN_(threadname)( "Thread renamed to %s\n", debugstr_a((char *)rec->ExceptionInformation[1]) );
482 else
483 WARN_(threadname)( "Thread ID %04lx renamed to %s\n", (DWORD)rec->ExceptionInformation[2],
484 debugstr_a((char *)rec->ExceptionInformation[1]) );
486 set_native_thread_name((DWORD)rec->ExceptionInformation[2], (char *)rec->ExceptionInformation[1]);
488 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
490 WARN( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
492 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
494 WARN( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
496 else
498 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
499 ERR( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
500 else
501 WARN( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
503 TRACE( " r0=%08lx r1=%08lx r2=%08lx r3=%08lx r4=%08lx r5=%08lx\n",
504 context->R0, context->R1, context->R2, context->R3, context->R4, context->R5 );
505 TRACE( " r6=%08lx r7=%08lx r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
506 context->R6, context->R7, context->R8, context->R9, context->R10, context->R11 );
507 TRACE( " r12=%08lx sp=%08lx lr=%08lx pc=%08lx cpsr=%08lx\n",
508 context->R12, context->Sp, context->Lr, context->Pc, context->Cpsr );
511 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
512 NtContinue( context, FALSE );
514 if ((status = call_function_handlers( rec, context )) == STATUS_SUCCESS)
515 NtContinue( context, FALSE );
517 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
518 return NtRaiseException( rec, context, FALSE );
522 /*******************************************************************
523 * KiUserApcDispatcher (NTDLL.@)
525 void WINAPI KiUserApcDispatcher( CONTEXT *context, ULONG_PTR ctx, ULONG_PTR arg1, ULONG_PTR arg2,
526 PNTAPCFUNC func )
528 func( ctx, arg1, arg2 );
529 NtContinue( context, TRUE );
533 /*******************************************************************
534 * KiUserCallbackDispatcher (NTDLL.@)
536 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
538 NTSTATUS status;
540 __TRY
542 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
543 status = NtCallbackReturn( NULL, 0, func( args, len ));
545 __EXCEPT_ALL
547 ERR_(seh)( "ignoring exception\n" );
548 status = NtCallbackReturn( 0, 0, 0 );
550 __ENDTRY
552 RtlRaiseStatus( status );
556 /***********************************************************************
557 * Definitions for Win32 unwind tables
560 struct unwind_info
562 DWORD function_length : 18;
563 DWORD version : 2;
564 DWORD x : 1;
565 DWORD e : 1;
566 DWORD f : 1;
567 DWORD epilog : 5;
568 DWORD codes : 4;
571 struct unwind_info_ext
573 WORD epilog;
574 BYTE codes;
575 BYTE reserved;
578 struct unwind_info_epilog
580 DWORD offset : 18;
581 DWORD res : 2;
582 DWORD cond : 4;
583 DWORD index : 8;
586 static const BYTE unwind_code_len[256] =
588 /* 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,
589 /* 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,
590 /* 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,
591 /* 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,
592 /* 80 */ 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,
593 /* a0 */ 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,
594 /* c0 */ 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 /* e0 */ 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,4,3,4,1,1,1,1,1
598 static const BYTE unwind_instr_len[256] =
600 /* 00 */ 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,
601 /* 20 */ 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,
602 /* 40 */ 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,
603 /* 60 */ 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,
604 /* 80 */ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
605 /* a0 */ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
606 /* 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,4,4,4,4,4,4,4,4,
607 /* e0 */ 4,4,4,4,4,4,4,4,4,4,4,4,2,2,2,4,0,0,0,0,0,4,4,2,2,4,4,2,4,2,4,0
610 /***********************************************************************
611 * get_sequence_len
613 static unsigned int get_sequence_len( BYTE *ptr, BYTE *end, int include_end )
615 unsigned int ret = 0;
617 while (ptr < end)
619 if (*ptr >= 0xfd)
621 if (*ptr <= 0xfe && include_end)
622 ret += unwind_instr_len[*ptr];
623 break;
625 ret += unwind_instr_len[*ptr];
626 ptr += unwind_code_len[*ptr];
628 return ret;
632 /***********************************************************************
633 * pop_regs_mask
635 static void pop_regs_mask( int mask, CONTEXT *context,
636 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
638 int i;
639 for (i = 0; i <= 12; i++)
641 if (!(mask & (1 << i))) continue;
642 if (ptrs && i >= 4 && i <= 11) (&ptrs->R4)[i - 4] = (DWORD *)context->Sp;
643 if (i >= 4) (&context->R0)[i] = *(DWORD *)context->Sp;
644 context->Sp += 4;
649 /***********************************************************************
650 * pop_regs_range
652 static void pop_regs_range( int last, CONTEXT *context,
653 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
655 int i;
656 for (i = 4; i <= last; i++)
658 if (ptrs) (&ptrs->R4)[i - 4] = (DWORD *)context->Sp;
659 (&context->R0)[i] = *(DWORD *)context->Sp;
660 context->Sp += 4;
665 /***********************************************************************
666 * pop_lr
668 static void pop_lr( int increment, CONTEXT *context,
669 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
671 if (ptrs) ptrs->Lr = (DWORD *)context->Sp;
672 context->Lr = *(DWORD *)context->Sp;
673 context->Sp += increment;
677 /***********************************************************************
678 * pop_fpregs_range
680 static void pop_fpregs_range( int first, int last, CONTEXT *context,
681 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
683 int i;
684 for (i = first; i <= last; i++)
686 if (ptrs && i >= 8 && i <= 15) (&ptrs->D8)[i - 8] = (ULONGLONG *)context->Sp;
687 context->D[i] = *(ULONGLONG *)context->Sp;
688 context->Sp += 8;
693 /***********************************************************************
694 * process_unwind_codes
696 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
697 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
699 unsigned int val, len;
700 unsigned int i;
702 /* skip codes */
703 while (ptr < end && skip)
705 if (*ptr >= 0xfd) break;
706 skip -= unwind_instr_len[*ptr];
707 ptr += unwind_code_len[*ptr];
710 while (ptr < end)
712 len = unwind_code_len[*ptr];
713 if (ptr + len > end) break;
714 val = 0;
715 for (i = 0; i < len; i++)
716 val = (val << 8) | ptr[i];
718 if (*ptr <= 0x7f) /* add sp, sp, #x */
719 context->Sp += 4 * (val & 0x7f);
720 else if (*ptr <= 0xbf) /* pop {r0-r12,lr} */
722 pop_regs_mask( val & 0x1fff, context, ptrs );
723 if (val & 0x2000)
724 pop_lr( 4, context, ptrs );
726 else if (*ptr <= 0xcf) /* mov sp, rX */
727 context->Sp = (&context->R0)[val & 0x0f];
728 else if (*ptr <= 0xd7) /* pop {r4-rX,lr} */
730 pop_regs_range( (val & 0x03) + 4, context, ptrs );
731 if (val & 0x04)
732 pop_lr( 4, context, ptrs );
734 else if (*ptr <= 0xdf) /* pop {r4-rX,lr} */
736 pop_regs_range( (val & 0x03) + 8, context, ptrs );
737 if (val & 0x04)
738 pop_lr( 4, context, ptrs );
740 else if (*ptr <= 0xe7) /* vpop {d8-dX} */
741 pop_fpregs_range( 8, (val & 0x07) + 8, context, ptrs );
742 else if (*ptr <= 0xeb) /* add sp, sp, #x */
743 context->Sp += 4 * (val & 0x3ff);
744 else if (*ptr <= 0xed) /* pop {r0-r12,lr} */
746 pop_regs_mask( val & 0xff, context, ptrs );
747 if (val & 0x100)
748 pop_lr( 4, context, ptrs );
750 else if (*ptr <= 0xee) /* Microsoft-specific 0x00-0x0f, Available 0x10-0xff */
751 WARN( "unsupported code %02x\n", *ptr );
752 else if (*ptr <= 0xef && ((val & 0xff) <= 0x0f)) /* ldr lr, [sp], #x */
753 pop_lr( 4 * (val & 0x0f), context, ptrs );
754 else if (*ptr == 0xf4) /* Custom private (unallocated) opcode, saved a full CONTEXT on the stack */
755 memcpy( context, (DWORD *)context->Sp, sizeof(CONTEXT) );
756 else if (*ptr <= 0xf4) /* Available */
757 WARN( "unsupported code %02x\n", *ptr );
758 else if (*ptr <= 0xf5) /* vpop {dS-dE} */
759 pop_fpregs_range( (val & 0xf0) >> 4, (val & 0x0f), context, ptrs );
760 else if (*ptr <= 0xf6) /* vpop {dS-dE} */
761 pop_fpregs_range( ((val & 0xf0) >> 4) + 16, (val & 0x0f) + 16, context, ptrs );
762 else if (*ptr == 0xf7 || *ptr == 0xf9) /* add sp, sp, #x */
763 context->Sp += 4 * (val & 0xffff);
764 else if (*ptr == 0xf8 || *ptr == 0xfa) /* add sp, sp, #x */
765 context->Sp += 4 * (val & 0xffffff);
766 else if (*ptr <= 0xfc) /* nop */
767 /* nop */ ;
768 else /* end */
769 break;
771 ptr += len;
776 /***********************************************************************
777 * unwind_packed_data
779 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
780 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
782 int i, pos = 0;
783 int pf = 0, ef = 0, fpoffset = 0, stack = func->StackAdjust;
784 int prologue_regmask = 0;
785 int epilogue_regmask = 0;
786 unsigned int offset, len;
787 BYTE prologue[10], *prologue_end, epilogue[20], *epilogue_end;
789 TRACE( "function %lx-%lx: len=%#x flag=%x ret=%u H=%u reg=%u R=%u L=%u C=%u stackadjust=%x\n",
790 base + func->BeginAddress, base + func->BeginAddress + func->FunctionLength * 2,
791 func->FunctionLength, func->Flag, func->Ret,
792 func->H, func->Reg, func->R, func->L, func->C, func->StackAdjust );
794 offset = (pc - base) - func->BeginAddress;
795 if (func->StackAdjust >= 0x03f4)
797 pf = func->StackAdjust & 0x04;
798 ef = func->StackAdjust & 0x08;
799 stack = (func->StackAdjust & 3) + 1;
802 if (!func->R || pf)
804 int first = 4, last = func->Reg + 4;
805 if (pf)
807 first = (~func->StackAdjust) & 3;
808 if (func->R)
809 last = 3;
811 for (i = first; i <= last; i++)
812 prologue_regmask |= 1 << i;
813 fpoffset = last + 1 - first;
816 if (!func->R || ef)
818 int first = 4, last = func->Reg + 4;
819 if (ef)
821 first = (~func->StackAdjust) & 3;
822 if (func->R)
823 last = 3;
825 for (i = first; i <= last; i++)
826 epilogue_regmask |= 1 << i;
829 if (func->C)
831 prologue_regmask |= 1 << 11;
832 epilogue_regmask |= 1 << 11;
835 if (func->L)
837 prologue_regmask |= 1 << 14; /* lr */
838 if (func->Ret != 0)
839 epilogue_regmask |= 1 << 14; /* lr */
840 else if (!func->H)
841 epilogue_regmask |= 1 << 15; /* pc */
844 /* Synthesize prologue opcodes */
845 if (stack && !pf)
847 if (stack <= 0x7f)
849 prologue[pos++] = stack; /* sub sp, sp, #x */
851 else
853 prologue[pos++] = 0xe8 | (stack >> 8); /* sub.w sp, sp, #x */
854 prologue[pos++] = stack & 0xff;
858 if (func->R && func->Reg != 7)
859 prologue[pos++] = 0xe0 | func->Reg; /* vpush {d8-dX} */
861 if (func->C && fpoffset == 0)
862 prologue[pos++] = 0xfb; /* mov r11, sp - handled as nop16 */
863 else if (func->C)
864 prologue[pos++] = 0xfc; /* add r11, sp, #x - handled as nop32 */
866 if (prologue_regmask & 0xf00) /* r8-r11 set */
868 int bitmask = prologue_regmask & 0x1fff;
869 if (prologue_regmask & (1 << 14)) /* lr */
870 bitmask |= 0x2000;
871 prologue[pos++] = 0x80 | (bitmask >> 8); /* push.w {r0-r12,lr} */
872 prologue[pos++] = bitmask & 0xff;
874 else if (prologue_regmask) /* r0-r7, lr set */
876 int bitmask = prologue_regmask & 0xff;
877 if (prologue_regmask & (1 << 14)) /* lr */
878 bitmask |= 0x100;
879 prologue[pos++] = 0xec | (bitmask >> 8); /* push {r0-r7,lr} */
880 prologue[pos++] = bitmask & 0xff;
883 if (func->H)
884 prologue[pos++] = 0x04; /* push {r0-r3} - handled as sub sp, sp, #16 */
886 prologue[pos++] = 0xff; /* end */
887 prologue_end = &prologue[pos];
889 /* Synthesize epilogue opcodes */
890 pos = 0;
891 if (stack && !ef)
893 if (stack <= 0x7f)
895 epilogue[pos++] = stack; /* sub sp, sp, #x */
897 else
899 epilogue[pos++] = 0xe8 | (stack >> 8); /* sub.w sp, sp, #x */
900 epilogue[pos++] = stack & 0xff;
904 if (func->R && func->Reg != 7)
905 epilogue[pos++] = 0xe0 | func->Reg; /* vpush {d8-dX} */
907 if (epilogue_regmask & 0x7f00) /* r8-r11, lr set */
909 int bitmask = epilogue_regmask & 0x1fff;
910 if (epilogue_regmask & (3 << 14)) /* lr or pc */
911 bitmask |= 0x2000;
912 epilogue[pos++] = 0x80 | (bitmask >> 8); /* push.w {r0-r12,lr} */
913 epilogue[pos++] = bitmask & 0xff;
915 else if (epilogue_regmask) /* r0-r7, pc set */
917 int bitmask = epilogue_regmask & 0xff;
918 if (epilogue_regmask & (1 << 15)) /* pc */
919 bitmask |= 0x100; /* lr */
920 epilogue[pos++] = 0xec | (bitmask >> 8); /* push {r0-r7,lr} */
921 epilogue[pos++] = bitmask & 0xff;
924 if (func->H && !(func->L && func->Ret == 0))
925 epilogue[pos++] = 0x04; /* add sp, sp, #16 */
926 else if (func->H && (func->L && func->Ret == 0))
928 epilogue[pos++] = 0xef; /* ldr lr, [sp], #20 */
929 epilogue[pos++] = 5;
932 if (func->Ret == 1)
933 epilogue[pos++] = 0xfd; /* bx lr */
934 else if (func->Ret == 2)
935 epilogue[pos++] = 0xfe; /* b address */
936 else
937 epilogue[pos++] = 0xff; /* end */
938 epilogue_end = &epilogue[pos];
940 if (func->Flag == 1 && offset < 4 * (prologue_end - prologue)) {
941 /* Check prologue */
942 len = get_sequence_len( prologue, prologue_end, 0 );
943 if (offset < len)
945 process_unwind_codes( prologue, prologue_end, context, ptrs, len - offset );
946 return NULL;
950 if (func->Ret != 3 && 2 * func->FunctionLength - offset <= 4 * (epilogue_end - epilogue)) {
951 /* Check epilogue */
952 len = get_sequence_len( epilogue, epilogue_end, 1 );
953 if (offset >= 2 * func->FunctionLength - len)
955 process_unwind_codes( epilogue, epilogue_end, context, ptrs, offset - (2 * func->FunctionLength - len) );
956 return NULL;
960 /* Execute full prologue */
961 process_unwind_codes( prologue, prologue_end, context, ptrs, 0 );
963 return NULL;
967 /***********************************************************************
968 * unwind_full_data
970 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
971 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
973 struct unwind_info *info;
974 struct unwind_info_epilog *info_epilog;
975 unsigned int i, codes, epilogs, len, offset;
976 void *data;
977 BYTE *end;
979 info = (struct unwind_info *)((char *)base + func->UnwindData);
980 data = info + 1;
981 epilogs = info->epilog;
982 codes = info->codes;
983 if (!codes && !epilogs)
985 struct unwind_info_ext *infoex = data;
986 codes = infoex->codes;
987 epilogs = infoex->epilog;
988 data = infoex + 1;
990 info_epilog = data;
991 if (!info->e) data = info_epilog + epilogs;
993 offset = (pc - base) - func->BeginAddress;
994 end = (BYTE *)data + codes * 4;
996 TRACE( "function %lx-%lx: len=%#x ver=%u X=%u E=%u F=%u epilogs=%u codes=%u\n",
997 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 2,
998 info->function_length, info->version, info->x, info->e, info->f, epilogs, codes * 4 );
1000 /* check for prolog */
1001 if (offset < codes * 4 * 4 && !info->f)
1003 len = get_sequence_len( data, end, 0 );
1004 if (offset < len)
1006 process_unwind_codes( data, end, context, ptrs, len - offset );
1007 return NULL;
1011 /* check for epilog */
1012 if (!info->e)
1014 for (i = 0; i < epilogs; i++)
1016 /* TODO: Currently not checking epilogue conditions. */
1017 if (offset < 2 * info_epilog[i].offset) break;
1018 if (offset - 2 * info_epilog[i].offset < (codes * 4 - info_epilog[i].index) * 4)
1020 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
1021 len = get_sequence_len( ptr, end, 1 );
1022 if (offset <= 2 * info_epilog[i].offset + len)
1024 process_unwind_codes( ptr, end, context, ptrs, offset - 2 * info_epilog[i].offset );
1025 return NULL;
1030 else if (2 * info->function_length - offset <= (codes * 4 - epilogs) * 4)
1032 BYTE *ptr = (BYTE *)data + epilogs;
1033 len = get_sequence_len( ptr, end, 1 );
1034 if (offset >= 2 * info->function_length - len)
1036 process_unwind_codes( ptr, end, context, ptrs, offset - (2 * info->function_length - len) );
1037 return NULL;
1041 process_unwind_codes( data, end, context, ptrs, 0 );
1043 /* get handler since we are inside the main code */
1044 if (info->x)
1046 DWORD *handler_rva = (DWORD *)data + codes;
1047 *handler_data = handler_rva + 1;
1048 return (char *)base + *handler_rva;
1050 return NULL;
1053 /***********************************************************************
1054 * RtlVirtualUnwind (NTDLL.@)
1056 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
1057 RUNTIME_FUNCTION *func, CONTEXT *context,
1058 PVOID *handler_data, ULONG_PTR *frame_ret,
1059 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
1061 void *handler;
1063 TRACE( "type %lx pc %Ix sp %lx func %lx\n", type, pc, context->Sp, base + func->BeginAddress );
1065 *handler_data = NULL;
1067 context->Pc = 0;
1068 if (func->Flag)
1069 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1070 else
1071 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1073 TRACE( "ret: lr=%lx sp=%lx handler=%p\n", context->Lr, context->Sp, handler );
1074 if (!context->Pc)
1075 context->Pc = context->Lr;
1076 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1077 *frame_ret = context->Sp;
1078 return handler;
1082 /**********************************************************************
1083 * call_consolidate_callback
1085 * Wrapper function to call a consolidate callback from a fake frame.
1086 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1087 * we have to skip all frames which were already processed. To do that we
1088 * trick the unwinding functions into thinking the call came from somewhere
1089 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1090 * DW_CFA_expression, and the expressions have the following format:
1092 * DW_OP_breg13; sleb128 <OFFSET> | Load SP + struct member offset
1093 * [DW_OP_deref] | Dereference, only for CFA
1095 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1096 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1097 EXCEPTION_RECORD *rec );
1098 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1099 "push {r0-r2,lr}\n\t"
1100 __ASM_SEH(".seh_nop\n\t")
1101 "sub sp, sp, #0x1a0\n\t"
1102 __ASM_SEH(".seh_nop\n\t")
1103 "mov r1, r0\n\t"
1104 __ASM_SEH(".seh_nop\n\t")
1105 "mov r0, sp\n\t"
1106 __ASM_SEH(".seh_nop\n\t")
1107 "mov r2, #0x1a0\n\t"
1108 __ASM_SEH(".seh_nop_w\n\t")
1109 "bl " __ASM_NAME("memcpy") "\n\t"
1110 __ASM_SEH(".seh_custom 0xf4\n\t") /* A custom (unallocated) SEH opcode for CONTEXT on stack */
1111 __ASM_SEH(".seh_endprologue\n\t")
1112 __ASM_CFI(".cfi_def_cfa 13, 0\n\t")
1113 __ASM_CFI(".cfi_escape 0x0f,0x04,0x7d,0xb8,0x00,0x06\n\t") /* DW_CFA_def_cfa_expression: DW_OP_breg13 + 56, DW_OP_deref */
1114 __ASM_CFI(".cfi_escape 0x10,0x04,0x02,0x7d,0x14\n\t") /* DW_CFA_expression: R4 DW_OP_breg13 + 20 */
1115 __ASM_CFI(".cfi_escape 0x10,0x05,0x02,0x7d,0x18\n\t") /* DW_CFA_expression: R5 DW_OP_breg13 + 24 */
1116 __ASM_CFI(".cfi_escape 0x10,0x06,0x02,0x7d,0x1c\n\t") /* DW_CFA_expression: R6 DW_OP_breg13 + 28 */
1117 __ASM_CFI(".cfi_escape 0x10,0x07,0x02,0x7d,0x20\n\t") /* DW_CFA_expression: R7 DW_OP_breg13 + 32 */
1118 __ASM_CFI(".cfi_escape 0x10,0x08,0x02,0x7d,0x24\n\t") /* DW_CFA_expression: R8 DW_OP_breg13 + 36 */
1119 __ASM_CFI(".cfi_escape 0x10,0x09,0x02,0x7d,0x28\n\t") /* DW_CFA_expression: R9 DW_OP_breg13 + 40 */
1120 __ASM_CFI(".cfi_escape 0x10,0x0a,0x02,0x7d,0x2c\n\t") /* DW_CFA_expression: R10 DW_OP_breg13 + 44 */
1121 __ASM_CFI(".cfi_escape 0x10,0x0b,0x02,0x7d,0x30\n\t") /* DW_CFA_expression: R11 DW_OP_breg13 + 48 */
1122 __ASM_CFI(".cfi_escape 0x10,0x0e,0x03,0x7d,0xc0,0x00\n\t") /* DW_CFA_expression: LR DW_OP_breg13 + 64 (PC) */
1123 /* Libunwind doesn't support the registers D8-D15 like this */
1124 #if 0
1125 __ASM_CFI(".cfi_escape 0x10,0x88,0x02,0x03,0x7d,0x90,0x01\n\t") /* DW_CFA_expression: D8 DW_OP_breg13 + 144 */
1126 __ASM_CFI(".cfi_escape 0x10,0x89,0x02,0x03,0x7d,0x98,0x01\n\t") /* DW_CFA_expression: D9 DW_OP_breg13 + 152 */
1127 __ASM_CFI(".cfi_escape 0x10,0x8a,0x02,0x03,0x7d,0xa0,0x01\n\t") /* DW_CFA_expression: D10 DW_OP_breg13 + 160 */
1128 __ASM_CFI(".cfi_escape 0x10,0x8b,0x02,0x03,0x7d,0xa8,0x01\n\t") /* DW_CFA_expression: D11 DW_OP_breg13 + 168 */
1129 __ASM_CFI(".cfi_escape 0x10,0x8c,0x02,0x03,0x7d,0xb0,0x01\n\t") /* DW_CFA_expression: D12 DW_OP_breg13 + 176 */
1130 __ASM_CFI(".cfi_escape 0x10,0x8d,0x02,0x03,0x7d,0xb8,0x01\n\t") /* DW_CFA_expression: D13 DW_OP_breg13 + 184 */
1131 __ASM_CFI(".cfi_escape 0x10,0x8e,0x02,0x03,0x7d,0xc0,0x01\n\t") /* DW_CFA_expression: D14 DW_OP_breg13 + 192 */
1132 __ASM_CFI(".cfi_escape 0x10,0x8f,0x02,0x03,0x7d,0xc8,0x01\n\t") /* DW_CFA_expression: D15 DW_OP_breg13 + 200 */
1133 #endif
1134 /* These EHABI opcodes are to be read bottom up - they
1135 * restore relevant registers from the CONTEXT. */
1136 __ASM_EHABI(".save {sp}\n\t") /* Restore Sp last */
1137 __ASM_EHABI(".pad #-(0x80 + 0x0c + 0x0c)\n\t") /* Move back across D0-D15, Cpsr, Fpscr, Padding, Pc, Lr and Sp */
1138 __ASM_EHABI(".vsave {d8-d15}\n\t")
1139 __ASM_EHABI(".pad #0x40\n\t") /* Skip past D0-D7 */
1140 __ASM_EHABI(".pad #0x0c\n\t") /* Skip past Cpsr, Fpscr and Padding */
1141 __ASM_EHABI(".save {lr, pc}\n\t")
1142 __ASM_EHABI(".pad #0x08\n\t") /* Skip past R12 and Sp - Sp is restored last */
1143 __ASM_EHABI(".save {r4-r11}\n\t")
1144 __ASM_EHABI(".pad #0x14\n\t") /* Skip past ContextFlags and R0-R3 */
1146 "ldrd r1, r2, [sp, #0x1a4]\n\t"
1147 "mov r0, r2\n\t"
1148 "blx r1\n\t"
1149 "add sp, sp, #0x1ac\n\t"
1150 "pop {pc}\n\t")
1154 /*******************************************************************
1155 * RtlRestoreContext (NTDLL.@)
1157 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1159 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1161 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1163 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1164 int i;
1166 for (i = 4; i <= 11; i++)
1167 (&context->R4)[i-4] = (&jmp->R4)[i-4];
1168 context->Lr = jmp->Pc;
1169 context->Sp = jmp->Sp;
1170 context->Fpscr = jmp->Fpscr;
1172 for (i = 0; i < 8; i++)
1173 context->D[8+i] = jmp->D[i];
1175 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1177 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1178 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1179 rec->ExceptionInformation[10] = (ULONG_PTR)&context->R4;
1181 context->Pc = (DWORD)call_consolidate_callback( context, consolidate, rec );
1184 /* hack: remove no longer accessible TEB frames */
1185 while ((DWORD)teb_frame < context->Sp)
1187 TRACE( "removing TEB frame: %p\n", teb_frame );
1188 teb_frame = __wine_pop_frame( teb_frame );
1191 TRACE( "returning to %lx stack %lx\n", context->Pc, context->Sp );
1192 NtContinue( context, FALSE );
1196 /***********************************************************************
1197 * RtlUnwindEx (NTDLL.@)
1199 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1200 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1202 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1203 EXCEPTION_RECORD record;
1204 DISPATCHER_CONTEXT dispatch;
1205 CONTEXT new_context;
1206 NTSTATUS status;
1207 DWORD i;
1209 RtlCaptureContext( context );
1210 new_context = *context;
1212 /* build an exception record, if we do not have one */
1213 if (!rec)
1215 record.ExceptionCode = STATUS_UNWIND;
1216 record.ExceptionFlags = 0;
1217 record.ExceptionRecord = NULL;
1218 record.ExceptionAddress = (void *)context->Pc;
1219 record.NumberParameters = 0;
1220 rec = &record;
1223 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1225 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%08lx\n",
1226 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1227 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1228 TRACE( " info[%ld]=%08Ix\n", i, rec->ExceptionInformation[i] );
1229 TRACE(" r0=%08lx r1=%08lx r2=%08lx r3=%08lx\n",
1230 context->R0, context->R1, context->R2, context->R3 );
1231 TRACE(" r4=%08lx r5=%08lx r6=%08lx r7=%08lx\n",
1232 context->R4, context->R5, context->R6, context->R7 );
1233 TRACE(" r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
1234 context->R8, context->R9, context->R10, context->R11 );
1235 TRACE(" r12=%08lx sp=%08lx lr=%08lx pc=%08lx\n",
1236 context->R12, context->Sp, context->Lr, context->Pc );
1238 dispatch.TargetPc = (ULONG_PTR)target_ip;
1239 dispatch.ContextRecord = context;
1240 dispatch.HistoryTable = table;
1241 dispatch.NonVolatileRegisters = (BYTE *)&context->R4;
1243 for (;;)
1245 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1246 if (status != STATUS_SUCCESS) raise_status( status, rec );
1248 unwind_done:
1249 if (!dispatch.EstablisherFrame) break;
1251 if (!is_valid_frame( dispatch.EstablisherFrame ))
1253 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1254 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1255 rec->ExceptionFlags |= EH_STACK_INVALID;
1256 break;
1259 if (dispatch.LanguageHandler)
1261 if (end_frame && (dispatch.EstablisherFrame > (DWORD)end_frame))
1263 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
1264 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1266 if (dispatch.EstablisherFrame == (DWORD)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1267 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1269 ULONG_PTR frame;
1271 *context = new_context = *dispatch.ContextRecord;
1272 dispatch.ContextRecord = context;
1273 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1274 dispatch.ControlPc, dispatch.FunctionEntry,
1275 &new_context, &dispatch.HandlerData, &frame,
1276 NULL );
1277 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1278 goto unwind_done;
1280 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1282 else /* hack: call builtin handlers registered in the tib list */
1284 DWORD backup_frame = dispatch.EstablisherFrame;
1285 while ((DWORD)teb_frame < new_context.Sp && (DWORD)teb_frame < (DWORD)end_frame)
1287 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1288 dispatch.EstablisherFrame = (DWORD)teb_frame;
1289 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1291 ULONG_PTR frame;
1293 teb_frame = __wine_pop_frame( teb_frame );
1295 *context = new_context = *dispatch.ContextRecord;
1296 dispatch.ContextRecord = context;
1297 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1298 dispatch.ControlPc, dispatch.FunctionEntry,
1299 &new_context, &dispatch.HandlerData,
1300 &frame, NULL );
1301 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1302 goto unwind_done;
1304 teb_frame = __wine_pop_frame( teb_frame );
1306 if ((DWORD)teb_frame == (DWORD)end_frame && (DWORD)end_frame < new_context.Sp) break;
1307 dispatch.EstablisherFrame = backup_frame;
1310 if (dispatch.EstablisherFrame == (DWORD)end_frame) break;
1311 *context = new_context;
1314 context->R0 = (DWORD)retval;
1315 context->Pc = (DWORD)target_ip;
1316 RtlRestoreContext(context, rec);
1320 /***********************************************************************
1321 * RtlUnwind (NTDLL.@)
1323 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1325 CONTEXT context;
1326 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1330 /*******************************************************************
1331 * __jump_unwind (NTDLL.@)
1333 void WINAPI __jump_unwind( void *frame, void *target_ip )
1335 CONTEXT context;
1336 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1339 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1340 PEXCEPTION_FILTER filter,
1341 PUCHAR nonvolatile);
1342 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1343 "push {r4-r11,lr}\n\t"
1344 __ASM_EHABI(".save {r4-r11,lr}\n\t")
1345 __ASM_SEH(".seh_save_regs_w {r4-r11,lr}\n\t")
1346 __ASM_SEH(".seh_endprologue\n\t")
1348 __ASM_CFI(".cfi_def_cfa 13, 36\n\t")
1349 __ASM_CFI(".cfi_offset r4, -36\n\t")
1350 __ASM_CFI(".cfi_offset r5, -32\n\t")
1351 __ASM_CFI(".cfi_offset r6, -28\n\t")
1352 __ASM_CFI(".cfi_offset r7, -24\n\t")
1353 __ASM_CFI(".cfi_offset r8, -20\n\t")
1354 __ASM_CFI(".cfi_offset r9, -16\n\t")
1355 __ASM_CFI(".cfi_offset r10, -12\n\t")
1356 __ASM_CFI(".cfi_offset r11, -8\n\t")
1357 __ASM_CFI(".cfi_offset lr, -4\n\t")
1359 "ldm r3, {r4-r11,lr}\n\t"
1360 "blx r2\n\t"
1361 "pop {r4-r11,pc}\n\t" )
1363 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1364 PTERMINATION_HANDLER handler,
1365 PUCHAR nonvolatile);
1366 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1367 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1368 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1370 /*******************************************************************
1371 * __C_specific_handler (NTDLL.@)
1373 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1374 void *frame,
1375 CONTEXT *context,
1376 struct _DISPATCHER_CONTEXT *dispatch )
1378 SCOPE_TABLE *table = dispatch->HandlerData;
1379 ULONG i;
1380 DWORD ControlPc = dispatch->ControlPc;
1382 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1383 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1385 if (dispatch->ControlPcIsUnwound)
1386 ControlPc -= 2;
1388 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1390 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1392 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1393 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1395 PTERMINATION_HANDLER handler;
1397 if (table->ScopeRecord[i].JumpTarget) continue;
1399 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1400 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1401 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1403 break;
1406 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1407 dispatch->ScopeIndex = i+1;
1409 TRACE( "calling __finally %p frame %p\n", handler, frame );
1410 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1411 dispatch->NonVolatileRegisters );
1414 return ExceptionContinueSearch;
1417 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1419 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1420 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1422 if (!table->ScopeRecord[i].JumpTarget) continue;
1423 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1425 EXCEPTION_POINTERS ptrs;
1426 PEXCEPTION_FILTER filter;
1428 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1429 ptrs.ExceptionRecord = rec;
1430 ptrs.ContextRecord = context;
1431 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1432 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1433 dispatch->NonVolatileRegisters ))
1435 case EXCEPTION_EXECUTE_HANDLER:
1436 break;
1437 case EXCEPTION_CONTINUE_SEARCH:
1438 continue;
1439 case EXCEPTION_CONTINUE_EXECUTION:
1440 return ExceptionContinueExecution;
1443 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1444 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1445 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1448 return ExceptionContinueSearch;
1452 /***********************************************************************
1453 * RtlRaiseException (NTDLL.@)
1455 __ASM_GLOBAL_FUNC( RtlRaiseException,
1456 "push {r0, lr}\n\t"
1457 __ASM_EHABI(".save {r0, lr}\n\t")
1458 __ASM_SEH(".seh_save_regs {r0, lr}\n\t")
1459 "sub sp, sp, #0x1a0\n\t" /* sizeof(CONTEXT) */
1460 __ASM_EHABI(".pad #0x1a0\n\t")
1461 __ASM_SEH(".seh_stackalloc 0x1a0\n\t")
1462 __ASM_SEH(".seh_endprologue\n\t")
1463 __ASM_CFI(".cfi_adjust_cfa_offset 424\n\t")
1464 __ASM_CFI(".cfi_offset lr, -4\n\t")
1465 "mov r0, sp\n\t" /* context */
1466 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1467 "ldr r0, [sp, #0x1a0]\n\t" /* rec */
1468 "ldr r1, [sp, #0x1a4]\n\t"
1469 "str r1, [sp, #0x3c]\n\t" /* context->Lr */
1470 "str r1, [sp, #0x40]\n\t" /* context->Pc */
1471 "mrs r2, CPSR\n\t"
1472 "bfi r2, r1, #5, #1\n\t" /* Thumb bit */
1473 "str r2, [sp, #0x44]\n\t" /* context->Cpsr */
1474 "str r1, [r0, #12]\n\t" /* rec->ExceptionAddress */
1475 "add r1, sp, #0x1a8\n\t"
1476 "str r1, [sp, #0x38]\n\t" /* context->Sp */
1477 "mov r1, sp\n\t"
1478 "mov r2, #1\n\t"
1479 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1480 "bl " __ASM_NAME("RtlRaiseStatus") )
1482 /*************************************************************************
1483 * RtlCaptureStackBackTrace (NTDLL.@)
1485 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1487 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip, count, buffer, hash );
1488 return 0;
1491 /***********************************************************************
1492 * RtlUserThreadStart (NTDLL.@)
1494 void WINAPI RtlUserThreadStart( PRTL_THREAD_START_ROUTINE entry, void *arg )
1496 __TRY
1498 pBaseThreadInitThunk( 0, (LPTHREAD_START_ROUTINE)entry, arg );
1500 __EXCEPT(call_unhandled_exception_filter)
1502 NtTerminateProcess( GetCurrentProcess(), GetExceptionCode() );
1504 __ENDTRY
1507 /******************************************************************
1508 * LdrInitializeThunk (NTDLL.@)
1510 void WINAPI LdrInitializeThunk( CONTEXT *context, ULONG_PTR unk2, ULONG_PTR unk3, ULONG_PTR unk4 )
1512 loader_init( context, (void **)&context->R0 );
1513 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", (void *)context->R0, (void *)context->R1 );
1514 NtContinue( context, TRUE );
1517 /**********************************************************************
1518 * DbgBreakPoint (NTDLL.@)
1520 __ASM_GLOBAL_FUNC( DbgBreakPoint, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1522 /**********************************************************************
1523 * DbgUserBreakPoint (NTDLL.@)
1525 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1527 #endif /* __arm__ */