gdiplus: In GdipImageSelectActiveFrame rely on codec->select_func() to fail.
[wine.git] / dlls / ntdll / signal_arm.c
blobf5b2f8c125262fd433ec8cde65088131dc6bcb5f
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(threadname);
39 typedef struct _SCOPE_TABLE
41 ULONG Count;
42 struct
44 ULONG BeginAddress;
45 ULONG EndAddress;
46 ULONG HandlerAddress;
47 ULONG JumpTarget;
48 } ScopeRecord[1];
49 } SCOPE_TABLE, *PSCOPE_TABLE;
52 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
53 struct MSVCRT_JUMP_BUFFER
55 unsigned long Frame;
56 unsigned long R4;
57 unsigned long R5;
58 unsigned long R6;
59 unsigned long R7;
60 unsigned long R8;
61 unsigned long R9;
62 unsigned long R10;
63 unsigned long R11;
64 unsigned long Sp;
65 unsigned long Pc;
66 unsigned long Fpscr;
67 unsigned long long D[8];
71 static void dump_scope_table( ULONG base, const SCOPE_TABLE *table )
73 unsigned int i;
75 TRACE( "scope table at %p\n", table );
76 for (i = 0; i < table->Count; i++)
77 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
78 base + table->ScopeRecord[i].BeginAddress,
79 base + table->ScopeRecord[i].EndAddress,
80 base + table->ScopeRecord[i].HandlerAddress,
81 base + table->ScopeRecord[i].JumpTarget );
84 /*******************************************************************
85 * is_valid_frame
87 static inline BOOL is_valid_frame( ULONG_PTR frame )
89 if (frame & 3) return FALSE;
90 return ((void *)frame >= NtCurrentTeb()->Tib.StackLimit &&
91 (void *)frame <= NtCurrentTeb()->Tib.StackBase);
95 /**************************************************************************
96 * __chkstk (NTDLL.@)
98 * Incoming r4 contains words to allocate, converting to bytes then return
100 __ASM_GLOBAL_FUNC( __chkstk, "lsl r4, r4, #2\n\t"
101 "bx lr" )
103 /***********************************************************************
104 * RtlCaptureContext (NTDLL.@)
106 __ASM_GLOBAL_FUNC( RtlCaptureContext,
107 "str r1, [r0, #0x8]\n\t" /* context->R1 */
108 "mov r1, #0x0200000\n\t" /* CONTEXT_ARM */
109 "add r1, r1, #0x7\n\t" /* CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT */
110 "str r1, [r0]\n\t" /* context->ContextFlags */
111 "str SP, [r0, #0x38]\n\t" /* context->Sp */
112 "str LR, [r0, #0x40]\n\t" /* context->Pc */
113 "mrs r1, CPSR\n\t"
114 "bfi r1, lr, #5, #1\n\t" /* Thumb bit */
115 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
116 "mov r1, #0\n\t"
117 "str r1, [r0, #0x4]\n\t" /* context->R0 */
118 "str r1, [r0, #0x3c]\n\t" /* context->Lr */
119 "add r0, #0x0c\n\t"
120 "stm r0, {r2-r12}\n\t" /* context->R2..R12 */
121 #ifndef __SOFTFP__
122 "add r0, #0x44\n\t" /* 0x50 - 0x0c */
123 "vstm r0, {d0-d15}\n\t" /* context->D0-D15 */
124 #endif
125 "bx lr" )
128 /**********************************************************************
129 * virtual_unwind
131 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
133 LDR_DATA_TABLE_ENTRY *module;
134 NTSTATUS status;
135 DWORD pc;
137 dispatch->ImageBase = 0;
138 dispatch->ScopeIndex = 0;
139 dispatch->EstablisherFrame = 0;
140 dispatch->ControlPc = context->Pc;
142 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
143 * signal frame.
145 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
146 pc = context->Pc - (dispatch->ControlPcIsUnwound ? 2 : 0);
148 /* first look for PE exception information */
150 if ((dispatch->FunctionEntry = lookup_function_info(pc,
151 (ULONG_PTR*)&dispatch->ImageBase, &module )))
153 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, pc,
154 dispatch->FunctionEntry, context,
155 &dispatch->HandlerData, (ULONG_PTR *)&dispatch->EstablisherFrame,
156 NULL );
157 return STATUS_SUCCESS;
160 /* then look for host system exception information */
162 if (!module || (module->Flags & LDR_WINE_INTERNAL))
164 struct unwind_builtin_dll_params params = { type, dispatch, context };
166 status = WINE_UNIX_CALL( unix_unwind_builtin_dll, &params );
167 if (status != STATUS_SUCCESS) return status;
169 if (dispatch->EstablisherFrame)
171 dispatch->FunctionEntry = NULL;
172 if (dispatch->LanguageHandler && !module)
174 FIXME( "calling personality routine in system library not supported yet\n" );
175 dispatch->LanguageHandler = NULL;
177 return STATUS_SUCCESS;
180 else
182 status = context->Pc != context->Lr ?
183 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
184 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
185 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
186 (void*) context->Lr, status );
187 dispatch->EstablisherFrame = context->Sp;
188 dispatch->LanguageHandler = NULL;
189 context->Pc = context->Lr;
190 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
191 return status;
194 dispatch->EstablisherFrame = context->Sp;
195 dispatch->LanguageHandler = NULL;
196 context->Pc = context->Lr;
197 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
198 return STATUS_SUCCESS;
202 struct unwind_exception_frame
204 EXCEPTION_REGISTRATION_RECORD frame;
205 DISPATCHER_CONTEXT *dispatch;
208 /**********************************************************************
209 * unwind_exception_handler
211 * Handler for exceptions happening while calling an unwind handler.
213 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
214 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
216 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
217 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
219 /* copy the original dispatcher into the current one, except for the TargetIp */
220 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
221 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
222 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
223 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
224 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
225 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
226 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
227 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
228 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
229 TRACE( "detected collided unwind\n" );
230 return ExceptionCollidedUnwind;
233 /**********************************************************************
234 * call_unwind_handler
236 * Call a single unwind handler.
238 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
240 struct unwind_exception_frame frame;
241 DWORD res;
243 frame.frame.Handler = unwind_exception_handler;
244 frame.dispatch = dispatch;
245 __wine_push_frame( &frame.frame );
247 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
248 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
249 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
250 TRACE( "handler %p returned %lx\n", dispatch->LanguageHandler, res );
252 __wine_pop_frame( &frame.frame );
254 switch (res)
256 case ExceptionContinueSearch:
257 case ExceptionCollidedUnwind:
258 break;
259 default:
260 raise_status( STATUS_INVALID_DISPOSITION, rec );
261 break;
264 return res;
268 /**********************************************************************
269 * call_teb_unwind_handler
271 * Call a single unwind handler from the TEB chain.
273 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
274 EXCEPTION_REGISTRATION_RECORD *teb_frame )
276 DWORD res;
278 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
279 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
280 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
281 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
283 switch (res)
285 case ExceptionContinueSearch:
286 case ExceptionCollidedUnwind:
287 break;
288 default:
289 raise_status( STATUS_INVALID_DISPOSITION, rec );
290 break;
293 return res;
297 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
298 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
300 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
301 rec->ExceptionFlags |= EH_NESTED_CALL;
303 return ExceptionContinueSearch;
307 /**********************************************************************
308 * call_handler
310 * Call a single exception handler.
311 * FIXME: Handle nested exceptions.
313 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
315 EXCEPTION_REGISTRATION_RECORD frame;
316 DWORD res;
318 frame.Handler = nested_exception_handler;
319 __wine_push_frame( &frame );
321 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
322 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
323 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
324 TRACE( "handler at %p returned %lu\n", dispatch->LanguageHandler, res );
326 rec->ExceptionFlags &= EH_NONCONTINUABLE;
327 __wine_pop_frame( &frame );
328 return res;
332 /**********************************************************************
333 * call_teb_handler
335 * Call a single exception handler from the TEB chain.
336 * FIXME: Handle nested exceptions.
338 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
339 EXCEPTION_REGISTRATION_RECORD *teb_frame )
341 DWORD res;
343 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
344 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
345 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
346 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
347 return res;
351 /**********************************************************************
352 * call_function_handlers
354 * Call the per-function handlers.
356 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
358 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
359 UNWIND_HISTORY_TABLE table;
360 DISPATCHER_CONTEXT dispatch;
361 CONTEXT context, prev_context;
362 NTSTATUS status;
364 context = *orig_context;
365 dispatch.TargetPc = 0;
366 dispatch.ContextRecord = &context;
367 dispatch.HistoryTable = &table;
368 prev_context = context;
369 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.R4;
371 for (;;)
373 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
374 if (status != STATUS_SUCCESS) return status;
376 unwind_done:
377 if (!dispatch.EstablisherFrame) break;
379 if (!is_valid_frame( dispatch.EstablisherFrame ))
381 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
382 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
383 rec->ExceptionFlags |= EH_STACK_INVALID;
384 break;
387 if (dispatch.LanguageHandler)
389 switch (call_handler( rec, orig_context, &dispatch ))
391 case ExceptionContinueExecution:
392 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
393 return STATUS_SUCCESS;
394 case ExceptionContinueSearch:
395 break;
396 case ExceptionNestedException:
397 FIXME( "nested exception\n" );
398 break;
399 case ExceptionCollidedUnwind: {
400 ULONG_PTR frame;
402 context = *dispatch.ContextRecord;
403 dispatch.ContextRecord = &context;
404 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
405 dispatch.ControlPc, dispatch.FunctionEntry,
406 &context, (PVOID *)&dispatch.HandlerData, &frame, NULL );
407 goto unwind_done;
409 default:
410 return STATUS_INVALID_DISPOSITION;
413 /* hack: call wine handlers registered in the tib list */
414 else while ((DWORD)teb_frame < context.Sp)
416 TRACE( "found wine frame %p rsp %lx handler %p\n",
417 teb_frame, context.Sp, teb_frame->Handler );
418 dispatch.EstablisherFrame = (DWORD)teb_frame;
419 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
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 ULONG_PTR frame;
432 context = *dispatch.ContextRecord;
433 dispatch.ContextRecord = &context;
434 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
435 dispatch.ControlPc, dispatch.FunctionEntry,
436 &context, (PVOID *)&dispatch.HandlerData, &frame, NULL );
437 teb_frame = teb_frame->Prev;
438 goto unwind_done;
440 default:
441 return STATUS_INVALID_DISPOSITION;
443 teb_frame = teb_frame->Prev;
446 if (context.Sp == (DWORD)NtCurrentTeb()->Tib.StackBase) break;
447 prev_context = context;
449 return STATUS_UNHANDLED_EXCEPTION;
453 /*******************************************************************
454 * KiUserExceptionDispatcher (NTDLL.@)
456 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
458 NTSTATUS status;
459 DWORD c;
461 TRACE( "code=%lx flags=%lx addr=%p pc=%08lx\n",
462 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Pc );
463 for (c = 0; c < rec->NumberParameters; c++)
464 TRACE( " info[%ld]=%08Ix\n", c, rec->ExceptionInformation[c] );
466 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
468 if (rec->ExceptionInformation[1] >> 16)
469 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
470 rec->ExceptionAddress,
471 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
472 else
473 MESSAGE( "wine: Call from %p to unimplemented function %s.%Id, aborting\n",
474 rec->ExceptionAddress,
475 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
477 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
479 if ((DWORD)rec->ExceptionInformation[2] == -1 || (DWORD)rec->ExceptionInformation[2] == GetCurrentThreadId())
480 WARN_(threadname)( "Thread renamed to %s\n", debugstr_a((char *)rec->ExceptionInformation[1]) );
481 else
482 WARN_(threadname)( "Thread ID %04lx renamed to %s\n", (DWORD)rec->ExceptionInformation[2],
483 debugstr_a((char *)rec->ExceptionInformation[1]) );
485 set_native_thread_name((DWORD)rec->ExceptionInformation[2], (char *)rec->ExceptionInformation[1]);
487 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
489 WARN( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
491 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
493 WARN( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
495 else
497 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
498 ERR( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
499 else
500 WARN( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
502 TRACE( " r0=%08lx r1=%08lx r2=%08lx r3=%08lx r4=%08lx r5=%08lx\n",
503 context->R0, context->R1, context->R2, context->R3, context->R4, context->R5 );
504 TRACE( " r6=%08lx r7=%08lx r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
505 context->R6, context->R7, context->R8, context->R9, context->R10, context->R11 );
506 TRACE( " r12=%08lx sp=%08lx lr=%08lx pc=%08lx cpsr=%08lx\n",
507 context->R12, context->Sp, context->Lr, context->Pc, context->Cpsr );
510 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
511 NtContinue( context, FALSE );
513 if ((status = call_function_handlers( rec, context )) == STATUS_SUCCESS)
514 NtContinue( context, FALSE );
516 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
517 return NtRaiseException( rec, context, FALSE );
521 /*******************************************************************
522 * KiUserApcDispatcher (NTDLL.@)
524 void WINAPI KiUserApcDispatcher( CONTEXT *context, ULONG_PTR ctx, ULONG_PTR arg1, ULONG_PTR arg2,
525 PNTAPCFUNC func )
527 func( ctx, arg1, arg2 );
528 NtContinue( context, TRUE );
532 /*******************************************************************
533 * KiUserCallbackDispatcher (NTDLL.@)
535 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
537 NTSTATUS status;
539 __TRY
541 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
542 status = NtCallbackReturn( NULL, 0, func( args, len ));
544 __EXCEPT_ALL
546 ERR_(seh)( "ignoring exception\n" );
547 status = NtCallbackReturn( 0, 0, 0 );
549 __ENDTRY
551 RtlRaiseStatus( status );
555 /***********************************************************************
556 * Definitions for Win32 unwind tables
559 struct unwind_info
561 DWORD function_length : 18;
562 DWORD version : 2;
563 DWORD x : 1;
564 DWORD e : 1;
565 DWORD f : 1;
566 DWORD epilog : 5;
567 DWORD codes : 4;
570 struct unwind_info_ext
572 WORD epilog;
573 BYTE codes;
574 BYTE reserved;
577 struct unwind_info_epilog
579 DWORD offset : 18;
580 DWORD res : 2;
581 DWORD cond : 4;
582 DWORD index : 8;
585 static const BYTE unwind_code_len[256] =
587 /* 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,
588 /* 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,
589 /* 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,
590 /* 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,
591 /* 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,
592 /* 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,
593 /* 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,
594 /* 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
597 static const BYTE unwind_instr_len[256] =
599 /* 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,
600 /* 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,
601 /* 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,
602 /* 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,
603 /* 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,
604 /* 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,
605 /* 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,
606 /* 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
609 /***********************************************************************
610 * get_sequence_len
612 static unsigned int get_sequence_len( BYTE *ptr, BYTE *end, int include_end )
614 unsigned int ret = 0;
616 while (ptr < end)
618 if (*ptr >= 0xfd)
620 if (*ptr <= 0xfe && include_end)
621 ret += unwind_instr_len[*ptr];
622 break;
624 ret += unwind_instr_len[*ptr];
625 ptr += unwind_code_len[*ptr];
627 return ret;
631 /***********************************************************************
632 * pop_regs_mask
634 static void pop_regs_mask( int mask, CONTEXT *context,
635 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
637 int i;
638 for (i = 0; i <= 12; i++)
640 if (!(mask & (1 << i))) continue;
641 if (ptrs && i >= 4 && i <= 11) (&ptrs->R4)[i - 4] = (DWORD *)context->Sp;
642 if (i >= 4) (&context->R0)[i] = *(DWORD *)context->Sp;
643 context->Sp += 4;
648 /***********************************************************************
649 * pop_regs_range
651 static void pop_regs_range( int last, CONTEXT *context,
652 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
654 int i;
655 for (i = 4; i <= last; i++)
657 if (ptrs) (&ptrs->R4)[i - 4] = (DWORD *)context->Sp;
658 (&context->R0)[i] = *(DWORD *)context->Sp;
659 context->Sp += 4;
664 /***********************************************************************
665 * pop_lr
667 static void pop_lr( int increment, CONTEXT *context,
668 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
670 if (ptrs) ptrs->Lr = (DWORD *)context->Sp;
671 context->Lr = *(DWORD *)context->Sp;
672 context->Sp += increment;
676 /***********************************************************************
677 * pop_fpregs_range
679 static void pop_fpregs_range( int first, int last, CONTEXT *context,
680 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
682 int i;
683 for (i = first; i <= last; i++)
685 if (ptrs && i >= 8 && i <= 15) (&ptrs->D8)[i - 8] = (ULONGLONG *)context->Sp;
686 context->D[i] = *(ULONGLONG *)context->Sp;
687 context->Sp += 8;
692 /***********************************************************************
693 * process_unwind_codes
695 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
696 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
698 unsigned int val, len;
699 unsigned int i;
701 /* skip codes */
702 while (ptr < end && skip)
704 if (*ptr >= 0xfd) break;
705 skip -= unwind_instr_len[*ptr];
706 ptr += unwind_code_len[*ptr];
709 while (ptr < end)
711 len = unwind_code_len[*ptr];
712 if (ptr + len > end) break;
713 val = 0;
714 for (i = 0; i < len; i++)
715 val = (val << 8) | ptr[i];
717 if (*ptr <= 0x7f) /* add sp, sp, #x */
718 context->Sp += 4 * (val & 0x7f);
719 else if (*ptr <= 0xbf) /* pop {r0-r12,lr} */
721 pop_regs_mask( val & 0x1fff, context, ptrs );
722 if (val & 0x2000)
723 pop_lr( 4, context, ptrs );
725 else if (*ptr <= 0xcf) /* mov sp, rX */
726 context->Sp = (&context->R0)[val & 0x0f];
727 else if (*ptr <= 0xd7) /* pop {r4-rX,lr} */
729 pop_regs_range( (val & 0x03) + 4, context, ptrs );
730 if (val & 0x04)
731 pop_lr( 4, context, ptrs );
733 else if (*ptr <= 0xdf) /* pop {r4-rX,lr} */
735 pop_regs_range( (val & 0x03) + 8, context, ptrs );
736 if (val & 0x04)
737 pop_lr( 4, context, ptrs );
739 else if (*ptr <= 0xe7) /* vpop {d8-dX} */
740 pop_fpregs_range( 8, (val & 0x07) + 8, context, ptrs );
741 else if (*ptr <= 0xeb) /* add sp, sp, #x */
742 context->Sp += 4 * (val & 0x3ff);
743 else if (*ptr <= 0xed) /* pop {r0-r12,lr} */
745 pop_regs_mask( val & 0xff, context, ptrs );
746 if (val & 0x100)
747 pop_lr( 4, context, ptrs );
749 else if (*ptr <= 0xee) /* Microsoft-specific 0x00-0x0f, Available 0x10-0xff */
750 WARN( "unsupported code %02x\n", *ptr );
751 else if (*ptr <= 0xef && ((val & 0xff) <= 0x0f)) /* ldr lr, [sp], #x */
752 pop_lr( 4 * (val & 0x0f), context, ptrs );
753 else if (*ptr == 0xf4) /* Custom private (unallocated) opcode, saved a full CONTEXT on the stack */
754 memcpy( context, (DWORD *)context->Sp, sizeof(CONTEXT) );
755 else if (*ptr <= 0xf4) /* Available */
756 WARN( "unsupported code %02x\n", *ptr );
757 else if (*ptr <= 0xf5) /* vpop {dS-dE} */
758 pop_fpregs_range( (val & 0xf0) >> 4, (val & 0x0f), context, ptrs );
759 else if (*ptr <= 0xf6) /* vpop {dS-dE} */
760 pop_fpregs_range( ((val & 0xf0) >> 4) + 16, (val & 0x0f) + 16, context, ptrs );
761 else if (*ptr == 0xf7 || *ptr == 0xf9) /* add sp, sp, #x */
762 context->Sp += 4 * (val & 0xffff);
763 else if (*ptr == 0xf8 || *ptr == 0xfa) /* add sp, sp, #x */
764 context->Sp += 4 * (val & 0xffffff);
765 else if (*ptr <= 0xfc) /* nop */
766 /* nop */ ;
767 else /* end */
768 break;
770 ptr += len;
775 /***********************************************************************
776 * unwind_packed_data
778 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
779 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
781 int i, pos = 0;
782 int pf = 0, ef = 0, fpoffset = 0, stack = func->StackAdjust;
783 int prologue_regmask = 0;
784 int epilogue_regmask = 0;
785 unsigned int offset, len;
786 BYTE prologue[10], *prologue_end, epilogue[20], *epilogue_end;
788 TRACE( "function %lx-%lx: len=%#x flag=%x ret=%u H=%u reg=%u R=%u L=%u C=%u stackadjust=%x\n",
789 base + func->BeginAddress, base + func->BeginAddress + func->FunctionLength * 2,
790 func->FunctionLength, func->Flag, func->Ret,
791 func->H, func->Reg, func->R, func->L, func->C, func->StackAdjust );
793 offset = (pc - base) - func->BeginAddress;
794 if (func->StackAdjust >= 0x03f4)
796 pf = func->StackAdjust & 0x04;
797 ef = func->StackAdjust & 0x08;
798 stack = (func->StackAdjust & 3) + 1;
801 if (!func->R || pf)
803 int first = 4, last = func->Reg + 4;
804 if (pf)
806 first = (~func->StackAdjust) & 3;
807 if (func->R)
808 last = 3;
810 for (i = first; i <= last; i++)
811 prologue_regmask |= 1 << i;
812 fpoffset = last + 1 - first;
815 if (!func->R || ef)
817 int first = 4, last = func->Reg + 4;
818 if (ef)
820 first = (~func->StackAdjust) & 3;
821 if (func->R)
822 last = 3;
824 for (i = first; i <= last; i++)
825 epilogue_regmask |= 1 << i;
828 if (func->C)
830 prologue_regmask |= 1 << 11;
831 epilogue_regmask |= 1 << 11;
834 if (func->L)
836 prologue_regmask |= 1 << 14; /* lr */
837 if (func->Ret != 0)
838 epilogue_regmask |= 1 << 14; /* lr */
839 else if (!func->H)
840 epilogue_regmask |= 1 << 15; /* pc */
843 /* Synthesize prologue opcodes */
844 if (stack && !pf)
846 if (stack <= 0x7f)
848 prologue[pos++] = stack; /* sub sp, sp, #x */
850 else
852 prologue[pos++] = 0xe8 | (stack >> 8); /* sub.w sp, sp, #x */
853 prologue[pos++] = stack & 0xff;
857 if (func->R && func->Reg != 7)
858 prologue[pos++] = 0xe0 | func->Reg; /* vpush {d8-dX} */
860 if (func->C && fpoffset == 0)
861 prologue[pos++] = 0xfb; /* mov r11, sp - handled as nop16 */
862 else if (func->C)
863 prologue[pos++] = 0xfc; /* add r11, sp, #x - handled as nop32 */
865 if (prologue_regmask & 0xf00) /* r8-r11 set */
867 int bitmask = prologue_regmask & 0x1fff;
868 if (prologue_regmask & (1 << 14)) /* lr */
869 bitmask |= 0x2000;
870 prologue[pos++] = 0x80 | (bitmask >> 8); /* push.w {r0-r12,lr} */
871 prologue[pos++] = bitmask & 0xff;
873 else if (prologue_regmask) /* r0-r7, lr set */
875 int bitmask = prologue_regmask & 0xff;
876 if (prologue_regmask & (1 << 14)) /* lr */
877 bitmask |= 0x100;
878 prologue[pos++] = 0xec | (bitmask >> 8); /* push {r0-r7,lr} */
879 prologue[pos++] = bitmask & 0xff;
882 if (func->H)
883 prologue[pos++] = 0x04; /* push {r0-r3} - handled as sub sp, sp, #16 */
885 prologue[pos++] = 0xff; /* end */
886 prologue_end = &prologue[pos];
888 /* Synthesize epilogue opcodes */
889 pos = 0;
890 if (stack && !ef)
892 if (stack <= 0x7f)
894 epilogue[pos++] = stack; /* sub sp, sp, #x */
896 else
898 epilogue[pos++] = 0xe8 | (stack >> 8); /* sub.w sp, sp, #x */
899 epilogue[pos++] = stack & 0xff;
903 if (func->R && func->Reg != 7)
904 epilogue[pos++] = 0xe0 | func->Reg; /* vpush {d8-dX} */
906 if (epilogue_regmask & 0x7f00) /* r8-r11, lr set */
908 int bitmask = epilogue_regmask & 0x1fff;
909 if (epilogue_regmask & (3 << 14)) /* lr or pc */
910 bitmask |= 0x2000;
911 epilogue[pos++] = 0x80 | (bitmask >> 8); /* push.w {r0-r12,lr} */
912 epilogue[pos++] = bitmask & 0xff;
914 else if (epilogue_regmask) /* r0-r7, pc set */
916 int bitmask = epilogue_regmask & 0xff;
917 if (epilogue_regmask & (1 << 15)) /* pc */
918 bitmask |= 0x100; /* lr */
919 epilogue[pos++] = 0xec | (bitmask >> 8); /* push {r0-r7,lr} */
920 epilogue[pos++] = bitmask & 0xff;
923 if (func->H && !(func->L && func->Ret == 0))
924 epilogue[pos++] = 0x04; /* add sp, sp, #16 */
925 else if (func->H && (func->L && func->Ret == 0))
927 epilogue[pos++] = 0xef; /* ldr lr, [sp], #20 */
928 epilogue[pos++] = 5;
931 if (func->Ret == 1)
932 epilogue[pos++] = 0xfd; /* bx lr */
933 else if (func->Ret == 2)
934 epilogue[pos++] = 0xfe; /* b address */
935 else
936 epilogue[pos++] = 0xff; /* end */
937 epilogue_end = &epilogue[pos];
939 if (func->Flag == 1 && offset < 4 * (prologue_end - prologue)) {
940 /* Check prologue */
941 len = get_sequence_len( prologue, prologue_end, 0 );
942 if (offset < len)
944 process_unwind_codes( prologue, prologue_end, context, ptrs, len - offset );
945 return NULL;
949 if (func->Ret != 3 && 2 * func->FunctionLength - offset <= 4 * (epilogue_end - epilogue)) {
950 /* Check epilogue */
951 len = get_sequence_len( epilogue, epilogue_end, 1 );
952 if (offset >= 2 * func->FunctionLength - len)
954 process_unwind_codes( epilogue, epilogue_end, context, ptrs, offset - (2 * func->FunctionLength - len) );
955 return NULL;
959 /* Execute full prologue */
960 process_unwind_codes( prologue, prologue_end, context, ptrs, 0 );
962 return NULL;
966 /***********************************************************************
967 * unwind_full_data
969 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
970 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
972 struct unwind_info *info;
973 struct unwind_info_epilog *info_epilog;
974 unsigned int i, codes, epilogs, len, offset;
975 void *data;
976 BYTE *end;
978 info = (struct unwind_info *)((char *)base + func->UnwindData);
979 data = info + 1;
980 epilogs = info->epilog;
981 codes = info->codes;
982 if (!codes && !epilogs)
984 struct unwind_info_ext *infoex = data;
985 codes = infoex->codes;
986 epilogs = infoex->epilog;
987 data = infoex + 1;
989 info_epilog = data;
990 if (!info->e) data = info_epilog + epilogs;
992 offset = (pc - base) - func->BeginAddress;
993 end = (BYTE *)data + codes * 4;
995 TRACE( "function %lx-%lx: len=%#x ver=%u X=%u E=%u F=%u epilogs=%u codes=%u\n",
996 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 2,
997 info->function_length, info->version, info->x, info->e, info->f, epilogs, codes * 4 );
999 /* check for prolog */
1000 if (offset < codes * 4 * 4 && !info->f)
1002 len = get_sequence_len( data, end, 0 );
1003 if (offset < len)
1005 process_unwind_codes( data, end, context, ptrs, len - offset );
1006 return NULL;
1010 /* check for epilog */
1011 if (!info->e)
1013 for (i = 0; i < epilogs; i++)
1015 /* TODO: Currently not checking epilogue conditions. */
1016 if (offset < 2 * info_epilog[i].offset) break;
1017 if (offset - 2 * info_epilog[i].offset < (codes * 4 - info_epilog[i].index) * 4)
1019 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
1020 len = get_sequence_len( ptr, end, 1 );
1021 if (offset <= 2 * info_epilog[i].offset + len)
1023 process_unwind_codes( ptr, end, context, ptrs, offset - 2 * info_epilog[i].offset );
1024 return NULL;
1029 else if (2 * info->function_length - offset <= (codes * 4 - epilogs) * 4)
1031 BYTE *ptr = (BYTE *)data + epilogs;
1032 len = get_sequence_len( ptr, end, 1 );
1033 if (offset >= 2 * info->function_length - len)
1035 process_unwind_codes( ptr, end, context, ptrs, offset - (2 * info->function_length - len) );
1036 return NULL;
1040 process_unwind_codes( data, end, context, ptrs, 0 );
1042 /* get handler since we are inside the main code */
1043 if (info->x)
1045 DWORD *handler_rva = (DWORD *)data + codes;
1046 *handler_data = handler_rva + 1;
1047 return (char *)base + *handler_rva;
1049 return NULL;
1052 /***********************************************************************
1053 * RtlVirtualUnwind (NTDLL.@)
1055 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
1056 RUNTIME_FUNCTION *func, CONTEXT *context,
1057 PVOID *handler_data, ULONG_PTR *frame_ret,
1058 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
1060 void *handler;
1062 TRACE( "type %lx pc %Ix sp %lx func %lx\n", type, pc, context->Sp, base + func->BeginAddress );
1064 *handler_data = NULL;
1066 context->Pc = 0;
1067 if (func->Flag)
1068 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1069 else
1070 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1072 TRACE( "ret: lr=%lx sp=%lx handler=%p\n", context->Lr, context->Sp, handler );
1073 if (!context->Pc)
1074 context->Pc = context->Lr;
1075 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1076 *frame_ret = context->Sp;
1077 return handler;
1081 /**********************************************************************
1082 * call_consolidate_callback
1084 * Wrapper function to call a consolidate callback from a fake frame.
1085 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1086 * we have to skip all frames which were already processed. To do that we
1087 * trick the unwinding functions into thinking the call came from somewhere
1088 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1089 * DW_CFA_expression, and the expressions have the following format:
1091 * DW_OP_breg13; sleb128 <OFFSET> | Load SP + struct member offset
1092 * [DW_OP_deref] | Dereference, only for CFA
1094 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1095 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1096 EXCEPTION_RECORD *rec );
1097 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1098 "push {r0-r2,lr}\n\t"
1099 __ASM_SEH(".seh_nop\n\t")
1100 "sub sp, sp, #0x1a0\n\t"
1101 __ASM_SEH(".seh_nop\n\t")
1102 "mov r1, r0\n\t"
1103 __ASM_SEH(".seh_nop\n\t")
1104 "mov r0, sp\n\t"
1105 __ASM_SEH(".seh_nop\n\t")
1106 "mov r2, #0x1a0\n\t"
1107 __ASM_SEH(".seh_nop_w\n\t")
1108 "bl " __ASM_NAME("memcpy") "\n\t"
1109 __ASM_SEH(".seh_custom 0xf4\n\t") /* A custom (unallocated) SEH opcode for CONTEXT on stack */
1110 __ASM_SEH(".seh_endprologue\n\t")
1111 __ASM_CFI(".cfi_def_cfa 13, 0\n\t")
1112 __ASM_CFI(".cfi_escape 0x0f,0x04,0x7d,0xb8,0x00,0x06\n\t") /* DW_CFA_def_cfa_expression: DW_OP_breg13 + 56, DW_OP_deref */
1113 __ASM_CFI(".cfi_escape 0x10,0x04,0x02,0x7d,0x14\n\t") /* DW_CFA_expression: R4 DW_OP_breg13 + 20 */
1114 __ASM_CFI(".cfi_escape 0x10,0x05,0x02,0x7d,0x18\n\t") /* DW_CFA_expression: R5 DW_OP_breg13 + 24 */
1115 __ASM_CFI(".cfi_escape 0x10,0x06,0x02,0x7d,0x1c\n\t") /* DW_CFA_expression: R6 DW_OP_breg13 + 28 */
1116 __ASM_CFI(".cfi_escape 0x10,0x07,0x02,0x7d,0x20\n\t") /* DW_CFA_expression: R7 DW_OP_breg13 + 32 */
1117 __ASM_CFI(".cfi_escape 0x10,0x08,0x02,0x7d,0x24\n\t") /* DW_CFA_expression: R8 DW_OP_breg13 + 36 */
1118 __ASM_CFI(".cfi_escape 0x10,0x09,0x02,0x7d,0x28\n\t") /* DW_CFA_expression: R9 DW_OP_breg13 + 40 */
1119 __ASM_CFI(".cfi_escape 0x10,0x0a,0x02,0x7d,0x2c\n\t") /* DW_CFA_expression: R10 DW_OP_breg13 + 44 */
1120 __ASM_CFI(".cfi_escape 0x10,0x0b,0x02,0x7d,0x30\n\t") /* DW_CFA_expression: R11 DW_OP_breg13 + 48 */
1121 __ASM_CFI(".cfi_escape 0x10,0x0e,0x03,0x7d,0xc0,0x00\n\t") /* DW_CFA_expression: LR DW_OP_breg13 + 64 (PC) */
1122 /* Libunwind doesn't support the registers D8-D15 like this */
1123 #if 0
1124 __ASM_CFI(".cfi_escape 0x10,0x88,0x02,0x03,0x7d,0x90,0x01\n\t") /* DW_CFA_expression: D8 DW_OP_breg13 + 144 */
1125 __ASM_CFI(".cfi_escape 0x10,0x89,0x02,0x03,0x7d,0x98,0x01\n\t") /* DW_CFA_expression: D9 DW_OP_breg13 + 152 */
1126 __ASM_CFI(".cfi_escape 0x10,0x8a,0x02,0x03,0x7d,0xa0,0x01\n\t") /* DW_CFA_expression: D10 DW_OP_breg13 + 160 */
1127 __ASM_CFI(".cfi_escape 0x10,0x8b,0x02,0x03,0x7d,0xa8,0x01\n\t") /* DW_CFA_expression: D11 DW_OP_breg13 + 168 */
1128 __ASM_CFI(".cfi_escape 0x10,0x8c,0x02,0x03,0x7d,0xb0,0x01\n\t") /* DW_CFA_expression: D12 DW_OP_breg13 + 176 */
1129 __ASM_CFI(".cfi_escape 0x10,0x8d,0x02,0x03,0x7d,0xb8,0x01\n\t") /* DW_CFA_expression: D13 DW_OP_breg13 + 184 */
1130 __ASM_CFI(".cfi_escape 0x10,0x8e,0x02,0x03,0x7d,0xc0,0x01\n\t") /* DW_CFA_expression: D14 DW_OP_breg13 + 192 */
1131 __ASM_CFI(".cfi_escape 0x10,0x8f,0x02,0x03,0x7d,0xc8,0x01\n\t") /* DW_CFA_expression: D15 DW_OP_breg13 + 200 */
1132 #endif
1133 /* These EHABI opcodes are to be read bottom up - they
1134 * restore relevant registers from the CONTEXT. */
1135 __ASM_EHABI(".save {sp}\n\t") /* Restore Sp last */
1136 __ASM_EHABI(".pad #-(0x80 + 0x0c + 0x0c)\n\t") /* Move back across D0-D15, Cpsr, Fpscr, Padding, Pc, Lr and Sp */
1137 __ASM_EHABI(".vsave {d8-d15}\n\t")
1138 __ASM_EHABI(".pad #0x40\n\t") /* Skip past D0-D7 */
1139 __ASM_EHABI(".pad #0x0c\n\t") /* Skip past Cpsr, Fpscr and Padding */
1140 __ASM_EHABI(".save {lr, pc}\n\t")
1141 __ASM_EHABI(".pad #0x08\n\t") /* Skip past R12 and Sp - Sp is restored last */
1142 __ASM_EHABI(".save {r4-r11}\n\t")
1143 __ASM_EHABI(".pad #0x14\n\t") /* Skip past ContextFlags and R0-R3 */
1145 "ldrd r1, r2, [sp, #0x1a4]\n\t"
1146 "mov r0, r2\n\t"
1147 "blx r1\n\t"
1148 "add sp, sp, #0x1ac\n\t"
1149 "pop {pc}\n\t")
1153 /*******************************************************************
1154 * RtlRestoreContext (NTDLL.@)
1156 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1158 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1160 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1162 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1163 int i;
1165 for (i = 4; i <= 11; i++)
1166 (&context->R4)[i-4] = (&jmp->R4)[i-4];
1167 context->Lr = jmp->Pc;
1168 context->Sp = jmp->Sp;
1169 context->Fpscr = jmp->Fpscr;
1171 for (i = 0; i < 8; i++)
1172 context->D[8+i] = jmp->D[i];
1174 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1176 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1177 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1178 rec->ExceptionInformation[10] = (ULONG_PTR)&context->R4;
1180 context->Pc = (DWORD)call_consolidate_callback( context, consolidate, rec );
1183 /* hack: remove no longer accessible TEB frames */
1184 while ((DWORD)teb_frame < context->Sp)
1186 TRACE( "removing TEB frame: %p\n", teb_frame );
1187 teb_frame = __wine_pop_frame( teb_frame );
1190 TRACE( "returning to %lx stack %lx\n", context->Pc, context->Sp );
1191 NtContinue( context, FALSE );
1195 /***********************************************************************
1196 * RtlUnwindEx (NTDLL.@)
1198 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1199 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1201 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1202 EXCEPTION_RECORD record;
1203 DISPATCHER_CONTEXT dispatch;
1204 CONTEXT new_context;
1205 NTSTATUS status;
1206 DWORD i;
1208 RtlCaptureContext( context );
1209 new_context = *context;
1211 /* build an exception record, if we do not have one */
1212 if (!rec)
1214 record.ExceptionCode = STATUS_UNWIND;
1215 record.ExceptionFlags = 0;
1216 record.ExceptionRecord = NULL;
1217 record.ExceptionAddress = (void *)context->Pc;
1218 record.NumberParameters = 0;
1219 rec = &record;
1222 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1224 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%08lx\n",
1225 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1226 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1227 TRACE( " info[%ld]=%08Ix\n", i, rec->ExceptionInformation[i] );
1228 TRACE(" r0=%08lx r1=%08lx r2=%08lx r3=%08lx\n",
1229 context->R0, context->R1, context->R2, context->R3 );
1230 TRACE(" r4=%08lx r5=%08lx r6=%08lx r7=%08lx\n",
1231 context->R4, context->R5, context->R6, context->R7 );
1232 TRACE(" r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
1233 context->R8, context->R9, context->R10, context->R11 );
1234 TRACE(" r12=%08lx sp=%08lx lr=%08lx pc=%08lx\n",
1235 context->R12, context->Sp, context->Lr, context->Pc );
1237 dispatch.TargetPc = (ULONG_PTR)target_ip;
1238 dispatch.ContextRecord = context;
1239 dispatch.HistoryTable = table;
1240 dispatch.NonVolatileRegisters = (BYTE *)&context->R4;
1242 for (;;)
1244 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1245 if (status != STATUS_SUCCESS) raise_status( status, rec );
1247 unwind_done:
1248 if (!dispatch.EstablisherFrame) break;
1250 if (!is_valid_frame( dispatch.EstablisherFrame ))
1252 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1253 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1254 rec->ExceptionFlags |= EH_STACK_INVALID;
1255 break;
1258 if (dispatch.LanguageHandler)
1260 if (end_frame && (dispatch.EstablisherFrame > (DWORD)end_frame))
1262 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
1263 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1265 if (dispatch.EstablisherFrame == (DWORD)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1266 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1268 ULONG_PTR frame;
1270 *context = new_context = *dispatch.ContextRecord;
1271 dispatch.ContextRecord = context;
1272 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1273 dispatch.ControlPc, dispatch.FunctionEntry,
1274 &new_context, &dispatch.HandlerData, &frame,
1275 NULL );
1276 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1277 goto unwind_done;
1279 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1281 else /* hack: call builtin handlers registered in the tib list */
1283 DWORD backup_frame = dispatch.EstablisherFrame;
1284 while ((DWORD)teb_frame < new_context.Sp && (DWORD)teb_frame < (DWORD)end_frame)
1286 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1287 dispatch.EstablisherFrame = (DWORD)teb_frame;
1288 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1290 ULONG_PTR frame;
1292 teb_frame = __wine_pop_frame( teb_frame );
1294 *context = new_context = *dispatch.ContextRecord;
1295 dispatch.ContextRecord = context;
1296 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1297 dispatch.ControlPc, dispatch.FunctionEntry,
1298 &new_context, &dispatch.HandlerData,
1299 &frame, NULL );
1300 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1301 goto unwind_done;
1303 teb_frame = __wine_pop_frame( teb_frame );
1305 if ((DWORD)teb_frame == (DWORD)end_frame && (DWORD)end_frame < new_context.Sp) break;
1306 dispatch.EstablisherFrame = backup_frame;
1309 if (dispatch.EstablisherFrame == (DWORD)end_frame) break;
1310 *context = new_context;
1313 context->R0 = (DWORD)retval;
1314 context->Pc = (DWORD)target_ip;
1315 RtlRestoreContext(context, rec);
1319 /***********************************************************************
1320 * RtlUnwind (NTDLL.@)
1322 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1324 CONTEXT context;
1325 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1329 /*******************************************************************
1330 * __jump_unwind (NTDLL.@)
1332 void WINAPI __jump_unwind( void *frame, void *target_ip )
1334 CONTEXT context;
1335 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1338 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1339 PEXCEPTION_FILTER filter,
1340 PUCHAR nonvolatile);
1341 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1342 "push {r4-r11,lr}\n\t"
1343 __ASM_EHABI(".save {r4-r11,lr}\n\t")
1344 __ASM_SEH(".seh_save_regs_w {r4-r11,lr}\n\t")
1345 __ASM_SEH(".seh_endprologue\n\t")
1347 __ASM_CFI(".cfi_def_cfa 13, 36\n\t")
1348 __ASM_CFI(".cfi_offset r4, -36\n\t")
1349 __ASM_CFI(".cfi_offset r5, -32\n\t")
1350 __ASM_CFI(".cfi_offset r6, -28\n\t")
1351 __ASM_CFI(".cfi_offset r7, -24\n\t")
1352 __ASM_CFI(".cfi_offset r8, -20\n\t")
1353 __ASM_CFI(".cfi_offset r9, -16\n\t")
1354 __ASM_CFI(".cfi_offset r10, -12\n\t")
1355 __ASM_CFI(".cfi_offset r11, -8\n\t")
1356 __ASM_CFI(".cfi_offset lr, -4\n\t")
1358 "ldm r3, {r4-r11,lr}\n\t"
1359 "blx r2\n\t"
1360 "pop {r4-r11,pc}\n\t" )
1362 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1363 PTERMINATION_HANDLER handler,
1364 PUCHAR nonvolatile);
1365 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1366 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1367 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1369 /*******************************************************************
1370 * __C_specific_handler (NTDLL.@)
1372 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1373 void *frame,
1374 CONTEXT *context,
1375 struct _DISPATCHER_CONTEXT *dispatch )
1377 SCOPE_TABLE *table = dispatch->HandlerData;
1378 ULONG i;
1379 DWORD ControlPc = dispatch->ControlPc;
1381 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1382 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1384 if (dispatch->ControlPcIsUnwound)
1385 ControlPc -= 2;
1387 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1389 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1391 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1392 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1394 PTERMINATION_HANDLER handler;
1396 if (table->ScopeRecord[i].JumpTarget) continue;
1398 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1399 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1400 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1402 break;
1405 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1406 dispatch->ScopeIndex = i+1;
1408 TRACE( "calling __finally %p frame %p\n", handler, frame );
1409 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1410 dispatch->NonVolatileRegisters );
1413 return ExceptionContinueSearch;
1416 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1418 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1419 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1421 if (!table->ScopeRecord[i].JumpTarget) continue;
1422 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1424 EXCEPTION_POINTERS ptrs;
1425 PEXCEPTION_FILTER filter;
1427 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1428 ptrs.ExceptionRecord = rec;
1429 ptrs.ContextRecord = context;
1430 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1431 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1432 dispatch->NonVolatileRegisters ))
1434 case EXCEPTION_EXECUTE_HANDLER:
1435 break;
1436 case EXCEPTION_CONTINUE_SEARCH:
1437 continue;
1438 case EXCEPTION_CONTINUE_EXECUTION:
1439 return ExceptionContinueExecution;
1442 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1443 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1444 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1447 return ExceptionContinueSearch;
1451 /***********************************************************************
1452 * RtlRaiseException (NTDLL.@)
1454 __ASM_GLOBAL_FUNC( RtlRaiseException,
1455 "push {r0, lr}\n\t"
1456 __ASM_EHABI(".save {r0, lr}\n\t")
1457 __ASM_SEH(".seh_save_regs {r0, lr}\n\t")
1458 "sub sp, sp, #0x1a0\n\t" /* sizeof(CONTEXT) */
1459 __ASM_EHABI(".pad #0x1a0\n\t")
1460 __ASM_SEH(".seh_stackalloc 0x1a0\n\t")
1461 __ASM_SEH(".seh_endprologue\n\t")
1462 __ASM_CFI(".cfi_adjust_cfa_offset 424\n\t")
1463 __ASM_CFI(".cfi_offset lr, -4\n\t")
1464 "mov r0, sp\n\t" /* context */
1465 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1466 "ldr r0, [sp, #0x1a0]\n\t" /* rec */
1467 "ldr r1, [sp, #0x1a4]\n\t"
1468 "str r1, [sp, #0x3c]\n\t" /* context->Lr */
1469 "str r1, [sp, #0x40]\n\t" /* context->Pc */
1470 "mrs r2, CPSR\n\t"
1471 "bfi r2, r1, #5, #1\n\t" /* Thumb bit */
1472 "str r2, [sp, #0x44]\n\t" /* context->Cpsr */
1473 "str r1, [r0, #12]\n\t" /* rec->ExceptionAddress */
1474 "add r1, sp, #0x1a8\n\t"
1475 "str r1, [sp, #0x38]\n\t" /* context->Sp */
1476 "mov r1, sp\n\t"
1477 "mov r2, #1\n\t"
1478 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1479 "bl " __ASM_NAME("RtlRaiseStatus") )
1481 /*************************************************************************
1482 * RtlCaptureStackBackTrace (NTDLL.@)
1484 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1486 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip, count, buffer, hash );
1487 return 0;
1490 /***********************************************************************
1491 * signal_start_thread
1493 __ASM_GLOBAL_FUNC( signal_start_thread,
1494 "mov sp, r0\n\t" /* context */
1495 "mov r1, #1\n\t"
1496 "b " __ASM_NAME("NtContinue") )
1498 /**********************************************************************
1499 * DbgBreakPoint (NTDLL.@)
1501 __ASM_GLOBAL_FUNC( DbgBreakPoint, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1503 /**********************************************************************
1504 * DbgUserBreakPoint (NTDLL.@)
1506 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1508 #endif /* __arm__ */