ntdll: Validate blocks in the heap pending free request list.
[wine.git] / dlls / ntdll / signal_arm.c
blobb426e03ff0ee084ab9004ab8acbaa4d2d7af1ad6
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 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33 #include "wine/exception.h"
34 #include "ntdll_misc.h"
35 #include "wine/debug.h"
36 #include "winnt.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
39 WINE_DECLARE_DEBUG_CHANNEL(threadname);
41 typedef struct _SCOPE_TABLE
43 ULONG Count;
44 struct
46 ULONG BeginAddress;
47 ULONG EndAddress;
48 ULONG HandlerAddress;
49 ULONG JumpTarget;
50 } ScopeRecord[1];
51 } SCOPE_TABLE, *PSCOPE_TABLE;
54 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
55 struct MSVCRT_JUMP_BUFFER
57 unsigned long Frame;
58 unsigned long R4;
59 unsigned long R5;
60 unsigned long R6;
61 unsigned long R7;
62 unsigned long R8;
63 unsigned long R9;
64 unsigned long R10;
65 unsigned long R11;
66 unsigned long Sp;
67 unsigned long Pc;
68 unsigned long Fpscr;
69 unsigned long long D[8];
73 static void dump_scope_table( ULONG base, const SCOPE_TABLE *table )
75 unsigned int i;
77 TRACE( "scope table at %p\n", table );
78 for (i = 0; i < table->Count; i++)
79 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i,
80 base + table->ScopeRecord[i].BeginAddress,
81 base + table->ScopeRecord[i].EndAddress,
82 base + table->ScopeRecord[i].HandlerAddress,
83 base + table->ScopeRecord[i].JumpTarget );
86 /*******************************************************************
87 * is_valid_frame
89 static inline BOOL is_valid_frame( ULONG_PTR frame )
91 if (frame & 3) return FALSE;
92 return ((void *)frame >= NtCurrentTeb()->Tib.StackLimit &&
93 (void *)frame <= NtCurrentTeb()->Tib.StackBase);
97 /**************************************************************************
98 * __chkstk (NTDLL.@)
100 * Incoming r4 contains words to allocate, converting to bytes then return
102 __ASM_GLOBAL_FUNC( __chkstk, "lsl r4, r4, #2\n\t"
103 "bx lr" )
105 /***********************************************************************
106 * RtlCaptureContext (NTDLL.@)
108 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
109 "str r1, [r0, #0x8]\n\t" /* context->R1 */
110 "mov r1, #0x0200000\n\t" /* CONTEXT_ARM */
111 "add r1, r1, #0x7\n\t" /* CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT */
112 "str r1, [r0]\n\t" /* context->ContextFlags */
113 "str SP, [r0, #0x38]\n\t" /* context->Sp */
114 "str LR, [r0, #0x40]\n\t" /* context->Pc */
115 "mrs r1, CPSR\n\t"
116 "bfi r1, lr, #5, #1\n\t" /* Thumb bit */
117 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
118 "mov r1, #0\n\t"
119 "str r1, [r0, #0x4]\n\t" /* context->R0 */
120 "str r1, [r0, #0x3c]\n\t" /* context->Lr */
121 "add r0, #0x0c\n\t"
122 "stm r0, {r2-r12}\n\t" /* context->R2..R12 */
123 #ifndef __SOFTFP__
124 "add r0, #0x44\n\t" /* 0x50 - 0x0c */
125 "vstm r0, {d0-d15}\n\t" /* context->D0-D15 */
126 #endif
127 "bx lr" )
130 /**********************************************************************
131 * virtual_unwind
133 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
135 LDR_DATA_TABLE_ENTRY *module;
136 NTSTATUS status;
137 DWORD pc;
139 dispatch->ImageBase = 0;
140 dispatch->ScopeIndex = 0;
141 dispatch->EstablisherFrame = 0;
142 dispatch->ControlPc = context->Pc;
144 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
145 * signal frame.
147 dispatch->ControlPcIsUnwound = (context->ContextFlags & CONTEXT_UNWOUND_TO_CALL) != 0;
148 pc = context->Pc - (dispatch->ControlPcIsUnwound ? 2 : 0);
150 /* first look for PE exception information */
152 if ((dispatch->FunctionEntry = lookup_function_info(pc,
153 (ULONG_PTR*)&dispatch->ImageBase, &module )))
155 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, pc,
156 dispatch->FunctionEntry, context,
157 &dispatch->HandlerData, (ULONG_PTR *)&dispatch->EstablisherFrame,
158 NULL );
159 return STATUS_SUCCESS;
162 /* then look for host system exception information */
164 if (!module || (module->Flags & LDR_WINE_INTERNAL))
166 struct unwind_builtin_dll_params params = { type, dispatch, context };
168 status = NTDLL_UNIX_CALL( unwind_builtin_dll, &params );
169 if (status != STATUS_SUCCESS) return status;
171 if (dispatch->EstablisherFrame)
173 dispatch->FunctionEntry = NULL;
174 if (dispatch->LanguageHandler && !module)
176 FIXME( "calling personality routine in system library not supported yet\n" );
177 dispatch->LanguageHandler = NULL;
179 return STATUS_SUCCESS;
182 else
184 status = context->Pc != context->Lr ?
185 STATUS_SUCCESS : STATUS_INVALID_DISPOSITION;
186 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
187 debugstr_w(module->BaseDllName.Buffer), (void*) context->Pc,
188 (void*) context->Lr, status );
189 dispatch->EstablisherFrame = context->Sp;
190 dispatch->LanguageHandler = NULL;
191 context->Pc = context->Lr;
192 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
193 return status;
196 dispatch->EstablisherFrame = context->Sp;
197 dispatch->LanguageHandler = NULL;
198 context->Pc = context->Lr;
199 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
200 return STATUS_SUCCESS;
204 struct unwind_exception_frame
206 EXCEPTION_REGISTRATION_RECORD frame;
207 DISPATCHER_CONTEXT *dispatch;
210 /**********************************************************************
211 * unwind_exception_handler
213 * Handler for exceptions happening while calling an unwind handler.
215 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
216 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
218 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
219 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
221 /* copy the original dispatcher into the current one, except for the TargetIp */
222 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
223 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
224 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
225 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
226 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
227 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
228 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
229 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
230 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
231 TRACE( "detected collided unwind\n" );
232 return ExceptionCollidedUnwind;
235 /**********************************************************************
236 * call_unwind_handler
238 * Call a single unwind handler.
240 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
242 struct unwind_exception_frame frame;
243 DWORD res;
245 frame.frame.Handler = unwind_exception_handler;
246 frame.dispatch = dispatch;
247 __wine_push_frame( &frame.frame );
249 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
250 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
251 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
252 TRACE( "handler %p returned %lx\n", dispatch->LanguageHandler, res );
254 __wine_pop_frame( &frame.frame );
256 switch (res)
258 case ExceptionContinueSearch:
259 case ExceptionCollidedUnwind:
260 break;
261 default:
262 raise_status( STATUS_INVALID_DISPOSITION, rec );
263 break;
266 return res;
270 /**********************************************************************
271 * call_teb_unwind_handler
273 * Call a single unwind handler from the TEB chain.
275 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
276 EXCEPTION_REGISTRATION_RECORD *teb_frame )
278 DWORD res;
280 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
281 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
282 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
283 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
285 switch (res)
287 case ExceptionContinueSearch:
288 case ExceptionCollidedUnwind:
289 break;
290 default:
291 raise_status( STATUS_INVALID_DISPOSITION, rec );
292 break;
295 return res;
299 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
300 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
302 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
303 rec->ExceptionFlags |= EH_NESTED_CALL;
305 return ExceptionContinueSearch;
309 /**********************************************************************
310 * call_handler
312 * Call a single exception handler.
313 * FIXME: Handle nested exceptions.
315 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
317 EXCEPTION_REGISTRATION_RECORD frame;
318 DWORD res;
320 frame.Handler = nested_exception_handler;
321 __wine_push_frame( &frame );
323 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
324 dispatch->LanguageHandler, rec, dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
325 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
326 TRACE( "handler at %p returned %lu\n", dispatch->LanguageHandler, res );
328 rec->ExceptionFlags &= EH_NONCONTINUABLE;
329 __wine_pop_frame( &frame );
330 return res;
334 /**********************************************************************
335 * call_teb_handler
337 * Call a single exception handler from the TEB chain.
338 * FIXME: Handle nested exceptions.
340 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
341 EXCEPTION_REGISTRATION_RECORD *teb_frame )
343 DWORD res;
345 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
346 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
347 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
348 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
349 return res;
353 /**********************************************************************
354 * call_function_handlers
356 * Call the per-function handlers.
358 static NTSTATUS call_function_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
360 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
361 UNWIND_HISTORY_TABLE table;
362 DISPATCHER_CONTEXT dispatch;
363 CONTEXT context, prev_context;
364 NTSTATUS status;
366 context = *orig_context;
367 dispatch.TargetPc = 0;
368 dispatch.ContextRecord = &context;
369 dispatch.HistoryTable = &table;
370 prev_context = context;
371 dispatch.NonVolatileRegisters = (BYTE *)&prev_context.R4;
373 for (;;)
375 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
376 if (status != STATUS_SUCCESS) return status;
378 unwind_done:
379 if (!dispatch.EstablisherFrame) break;
381 if (!is_valid_frame( dispatch.EstablisherFrame ))
383 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
384 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
385 rec->ExceptionFlags |= EH_STACK_INVALID;
386 break;
389 if (dispatch.LanguageHandler)
391 switch (call_handler( rec, orig_context, &dispatch ))
393 case ExceptionContinueExecution:
394 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
395 return STATUS_SUCCESS;
396 case ExceptionContinueSearch:
397 break;
398 case ExceptionNestedException:
399 FIXME( "nested exception\n" );
400 break;
401 case ExceptionCollidedUnwind: {
402 ULONG_PTR frame;
404 context = *dispatch.ContextRecord;
405 dispatch.ContextRecord = &context;
406 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
407 dispatch.ControlPc, dispatch.FunctionEntry,
408 &context, (PVOID *)&dispatch.HandlerData, &frame, NULL );
409 goto unwind_done;
411 default:
412 return STATUS_INVALID_DISPOSITION;
415 /* hack: call wine handlers registered in the tib list */
416 else while ((DWORD)teb_frame < context.Sp)
418 TRACE( "found wine frame %p rsp %lx handler %p\n",
419 teb_frame, context.Sp, teb_frame->Handler );
420 dispatch.EstablisherFrame = (DWORD)teb_frame;
421 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
423 case ExceptionContinueExecution:
424 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
425 return STATUS_SUCCESS;
426 case ExceptionContinueSearch:
427 break;
428 case ExceptionNestedException:
429 FIXME( "nested exception\n" );
430 break;
431 case ExceptionCollidedUnwind: {
432 ULONG_PTR frame;
434 context = *dispatch.ContextRecord;
435 dispatch.ContextRecord = &context;
436 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
437 dispatch.ControlPc, dispatch.FunctionEntry,
438 &context, (PVOID *)&dispatch.HandlerData, &frame, NULL );
439 teb_frame = teb_frame->Prev;
440 goto unwind_done;
442 default:
443 return STATUS_INVALID_DISPOSITION;
445 teb_frame = teb_frame->Prev;
448 if (context.Sp == (DWORD)NtCurrentTeb()->Tib.StackBase) break;
449 prev_context = context;
451 return STATUS_UNHANDLED_EXCEPTION;
455 /*******************************************************************
456 * KiUserExceptionDispatcher (NTDLL.@)
458 NTSTATUS WINAPI KiUserExceptionDispatcher( EXCEPTION_RECORD *rec, CONTEXT *context )
460 NTSTATUS status;
461 DWORD c;
463 TRACE( "code=%lx flags=%lx addr=%p pc=%08lx\n",
464 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Pc );
465 for (c = 0; c < rec->NumberParameters; c++)
466 TRACE( " info[%ld]=%08Ix\n", c, rec->ExceptionInformation[c] );
468 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
470 if (rec->ExceptionInformation[1] >> 16)
471 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
472 rec->ExceptionAddress,
473 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
474 else
475 MESSAGE( "wine: Call from %p to unimplemented function %s.%Id, aborting\n",
476 rec->ExceptionAddress,
477 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
479 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
481 if ((DWORD)rec->ExceptionInformation[2] == -1)
482 WARN_(threadname)( "Thread renamed to %s\n", debugstr_a((char *)rec->ExceptionInformation[1]) );
483 else
484 WARN_(threadname)( "Thread ID %04lx renamed to %s\n", (DWORD)rec->ExceptionInformation[2],
485 debugstr_a((char *)rec->ExceptionInformation[1]) );
487 set_native_thread_name((DWORD)rec->ExceptionInformation[2], (char *)rec->ExceptionInformation[1]);
489 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
491 WARN( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
493 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
495 WARN( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
497 else
499 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
500 ERR( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
501 else
502 WARN( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
504 TRACE( " r0=%08lx r1=%08lx r2=%08lx r3=%08lx r4=%08lx r5=%08lx\n",
505 context->R0, context->R1, context->R2, context->R3, context->R4, context->R5 );
506 TRACE( " r6=%08lx r7=%08lx r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
507 context->R6, context->R7, context->R8, context->R9, context->R10, context->R11 );
508 TRACE( " r12=%08lx sp=%08lx lr=%08lx pc=%08lx cpsr=%08lx\n",
509 context->R12, context->Sp, context->Lr, context->Pc, context->Cpsr );
512 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
513 NtContinue( context, FALSE );
515 if ((status = call_function_handlers( rec, context )) == STATUS_SUCCESS)
516 NtContinue( context, FALSE );
518 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
519 return NtRaiseException( rec, context, FALSE );
523 /*******************************************************************
524 * KiUserApcDispatcher (NTDLL.@)
526 void WINAPI KiUserApcDispatcher( CONTEXT *context, ULONG_PTR ctx, ULONG_PTR arg1, ULONG_PTR arg2,
527 PNTAPCFUNC func )
529 func( ctx, arg1, arg2 );
530 NtContinue( context, TRUE );
534 /*******************************************************************
535 * KiUserCallbackDispatcher (NTDLL.@)
537 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
539 NTSTATUS status;
541 __TRY
543 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
544 status = NtCallbackReturn( NULL, 0, func( args, len ));
546 __EXCEPT_ALL
548 ERR_(seh)( "ignoring exception\n" );
549 status = NtCallbackReturn( 0, 0, 0 );
551 __ENDTRY
553 RtlRaiseStatus( status );
557 /***********************************************************************
558 * Definitions for Win32 unwind tables
561 struct unwind_info
563 DWORD function_length : 18;
564 DWORD version : 2;
565 DWORD x : 1;
566 DWORD e : 1;
567 DWORD f : 1;
568 DWORD epilog : 5;
569 DWORD codes : 4;
572 struct unwind_info_ext
574 WORD epilog;
575 BYTE codes;
576 BYTE reserved;
579 struct unwind_info_epilog
581 DWORD offset : 18;
582 DWORD res : 2;
583 DWORD cond : 4;
584 DWORD index : 8;
587 static const BYTE unwind_code_len[256] =
589 /* 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,
590 /* 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,
591 /* 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,
592 /* 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,
593 /* 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,
594 /* 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,
595 /* 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,
596 /* 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
599 static const BYTE unwind_instr_len[256] =
601 /* 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,
602 /* 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,
603 /* 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,
604 /* 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,
605 /* 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,
606 /* 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,
607 /* 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,
608 /* 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
611 /***********************************************************************
612 * get_sequence_len
614 static unsigned int get_sequence_len( BYTE *ptr, BYTE *end, int include_end )
616 unsigned int ret = 0;
618 while (ptr < end)
620 if (*ptr >= 0xfd)
622 if (*ptr <= 0xfe && include_end)
623 ret += unwind_instr_len[*ptr];
624 break;
626 ret += unwind_instr_len[*ptr];
627 ptr += unwind_code_len[*ptr];
629 return ret;
633 /***********************************************************************
634 * pop_regs_mask
636 static void pop_regs_mask( int mask, CONTEXT *context,
637 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
639 int i;
640 for (i = 0; i <= 12; i++)
642 if (!(mask & (1 << i))) continue;
643 if (ptrs && i >= 4 && i <= 11) (&ptrs->R4)[i - 4] = (DWORD *)context->Sp;
644 if (i >= 4) (&context->R0)[i] = *(DWORD *)context->Sp;
645 context->Sp += 4;
650 /***********************************************************************
651 * pop_regs_range
653 static void pop_regs_range( int last, CONTEXT *context,
654 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
656 int i;
657 for (i = 4; i <= last; i++)
659 if (ptrs) (&ptrs->R4)[i - 4] = (DWORD *)context->Sp;
660 (&context->R0)[i] = *(DWORD *)context->Sp;
661 context->Sp += 4;
666 /***********************************************************************
667 * pop_lr
669 static void pop_lr( int increment, CONTEXT *context,
670 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
672 if (ptrs) ptrs->Lr = (DWORD *)context->Sp;
673 context->Lr = *(DWORD *)context->Sp;
674 context->Sp += increment;
678 /***********************************************************************
679 * pop_fpregs_range
681 static void pop_fpregs_range( int first, int last, CONTEXT *context,
682 KNONVOLATILE_CONTEXT_POINTERS *ptrs )
684 int i;
685 for (i = first; i <= last; i++)
687 if (ptrs && i >= 8 && i <= 15) (&ptrs->D8)[i - 8] = (ULONGLONG *)context->Sp;
688 context->u.D[i] = *(ULONGLONG *)context->Sp;
689 context->Sp += 8;
694 /***********************************************************************
695 * process_unwind_codes
697 static void process_unwind_codes( BYTE *ptr, BYTE *end, CONTEXT *context,
698 KNONVOLATILE_CONTEXT_POINTERS *ptrs, int skip )
700 unsigned int val, len;
701 unsigned int i;
703 /* skip codes */
704 while (ptr < end && skip)
706 if (*ptr >= 0xfd) break;
707 skip -= unwind_instr_len[*ptr];
708 ptr += unwind_code_len[*ptr];
711 while (ptr < end)
713 len = unwind_code_len[*ptr];
714 if (ptr + len > end) break;
715 val = 0;
716 for (i = 0; i < len; i++)
717 val = (val << 8) | ptr[i];
719 if (*ptr <= 0x7f) /* add sp, sp, #x */
720 context->Sp += 4 * (val & 0x7f);
721 else if (*ptr <= 0xbf) /* pop {r0-r12,lr} */
723 pop_regs_mask( val & 0x1fff, context, ptrs );
724 if (val & 0x2000)
725 pop_lr( 4, context, ptrs );
727 else if (*ptr <= 0xcf) /* mov sp, rX */
728 context->Sp = (&context->R0)[val & 0x0f];
729 else if (*ptr <= 0xd7) /* pop {r4-rX,lr} */
731 pop_regs_range( (val & 0x03) + 4, context, ptrs );
732 if (val & 0x04)
733 pop_lr( 4, context, ptrs );
735 else if (*ptr <= 0xdf) /* pop {r4-rX,lr} */
737 pop_regs_range( (val & 0x03) + 8, context, ptrs );
738 if (val & 0x04)
739 pop_lr( 4, context, ptrs );
741 else if (*ptr <= 0xe7) /* vpop {d8-dX} */
742 pop_fpregs_range( 8, (val & 0x07) + 8, context, ptrs );
743 else if (*ptr <= 0xeb) /* add sp, sp, #x */
744 context->Sp += 4 * (val & 0x3ff);
745 else if (*ptr <= 0xed) /* pop {r0-r12,lr} */
747 pop_regs_mask( val & 0xff, context, ptrs );
748 if (val & 0x100)
749 pop_lr( 4, context, ptrs );
751 else if (*ptr <= 0xee) /* Microsoft-specific 0x00-0x0f, Available 0x10-0xff */
752 WARN( "unsupported code %02x\n", *ptr );
753 else if (*ptr <= 0xef && ((val & 0xff) <= 0x0f)) /* ldr lr, [sp], #x */
754 pop_lr( 4 * (val & 0x0f), context, ptrs );
755 else if (*ptr == 0xf4) /* Custom private (unallocated) opcode, saved a full CONTEXT on the stack */
756 memcpy( context, (DWORD *)context->Sp, sizeof(CONTEXT) );
757 else if (*ptr <= 0xf4) /* Available */
758 WARN( "unsupported code %02x\n", *ptr );
759 else if (*ptr <= 0xf5) /* vpop {dS-dE} */
760 pop_fpregs_range( (val & 0xf0) >> 4, (val & 0x0f), context, ptrs );
761 else if (*ptr <= 0xf6) /* vpop {dS-dE} */
762 pop_fpregs_range( ((val & 0xf0) >> 4) + 16, (val & 0x0f) + 16, context, ptrs );
763 else if (*ptr == 0xf7 || *ptr == 0xf9) /* add sp, sp, #x */
764 context->Sp += 4 * (val & 0xffff);
765 else if (*ptr == 0xf8 || *ptr == 0xfa) /* add sp, sp, #x */
766 context->Sp += 4 * (val & 0xffffff);
767 else if (*ptr <= 0xfc) /* nop */
768 /* nop */ ;
769 else /* end */
770 break;
772 ptr += len;
777 /***********************************************************************
778 * unwind_packed_data
780 static void *unwind_packed_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
781 CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
783 int i, pos = 0;
784 int pf = 0, ef = 0, fpoffset = 0, stack = func->u.s.StackAdjust;
785 int prologue_regmask = 0;
786 int epilogue_regmask = 0;
787 unsigned int offset, len;
788 BYTE prologue[10], *prologue_end, epilogue[20], *epilogue_end;
790 TRACE( "function %lx-%lx: len=%#x flag=%x ret=%u H=%u reg=%u R=%u L=%u C=%u stackadjust=%x\n",
791 base + func->BeginAddress, base + func->BeginAddress + func->u.s.FunctionLength * 2,
792 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.Ret,
793 func->u.s.H, func->u.s.Reg, func->u.s.R, func->u.s.L, func->u.s.C, func->u.s.StackAdjust );
795 offset = (pc - base) - func->BeginAddress;
796 if (func->u.s.StackAdjust >= 0x03f4)
798 pf = func->u.s.StackAdjust & 0x04;
799 ef = func->u.s.StackAdjust & 0x08;
800 stack = (func->u.s.StackAdjust & 3) + 1;
803 if (!func->u.s.R || pf)
805 int first = 4, last = func->u.s.Reg + 4;
806 if (pf)
808 first = (~func->u.s.StackAdjust) & 3;
809 if (func->u.s.R)
810 last = 3;
812 for (i = first; i <= last; i++)
813 prologue_regmask |= 1 << i;
814 fpoffset = last + 1 - first;
817 if (!func->u.s.R || ef)
819 int first = 4, last = func->u.s.Reg + 4;
820 if (ef)
822 first = (~func->u.s.StackAdjust) & 3;
823 if (func->u.s.R)
824 last = 3;
826 for (i = first; i <= last; i++)
827 epilogue_regmask |= 1 << i;
830 if (func->u.s.C)
832 prologue_regmask |= 1 << 11;
833 epilogue_regmask |= 1 << 11;
836 if (func->u.s.L)
838 prologue_regmask |= 1 << 14; /* lr */
839 if (func->u.s.Ret != 0)
840 epilogue_regmask |= 1 << 14; /* lr */
841 else if (!func->u.s.H)
842 epilogue_regmask |= 1 << 15; /* pc */
845 /* Synthesize prologue opcodes */
846 if (stack && !pf)
848 if (stack <= 0x7f)
850 prologue[pos++] = stack; /* sub sp, sp, #x */
852 else
854 prologue[pos++] = 0xe8 | (stack >> 8); /* sub.w sp, sp, #x */
855 prologue[pos++] = stack & 0xff;
859 if (func->u.s.R && func->u.s.Reg != 7)
860 prologue[pos++] = 0xe0 | func->u.s.Reg; /* vpush {d8-dX} */
862 if (func->u.s.C && fpoffset == 0)
863 prologue[pos++] = 0xfb; /* mov r11, sp - handled as nop16 */
864 else if (func->u.s.C)
865 prologue[pos++] = 0xfc; /* add r11, sp, #x - handled as nop32 */
867 if (prologue_regmask & 0xf00) /* r8-r11 set */
869 int bitmask = prologue_regmask & 0x1fff;
870 if (prologue_regmask & (1 << 14)) /* lr */
871 bitmask |= 0x2000;
872 prologue[pos++] = 0x80 | (bitmask >> 8); /* push.w {r0-r12,lr} */
873 prologue[pos++] = bitmask & 0xff;
875 else if (prologue_regmask) /* r0-r7, lr set */
877 int bitmask = prologue_regmask & 0xff;
878 if (prologue_regmask & (1 << 14)) /* lr */
879 bitmask |= 0x100;
880 prologue[pos++] = 0xec | (bitmask >> 8); /* push {r0-r7,lr} */
881 prologue[pos++] = bitmask & 0xff;
884 if (func->u.s.H)
885 prologue[pos++] = 0x04; /* push {r0-r3} - handled as sub sp, sp, #16 */
887 prologue[pos++] = 0xff; /* end */
888 prologue_end = &prologue[pos];
890 /* Synthesize epilogue opcodes */
891 pos = 0;
892 if (stack && !ef)
894 if (stack <= 0x7f)
896 epilogue[pos++] = stack; /* sub sp, sp, #x */
898 else
900 epilogue[pos++] = 0xe8 | (stack >> 8); /* sub.w sp, sp, #x */
901 epilogue[pos++] = stack & 0xff;
905 if (func->u.s.R && func->u.s.Reg != 7)
906 epilogue[pos++] = 0xe0 | func->u.s.Reg; /* vpush {d8-dX} */
908 if (epilogue_regmask & 0x7f00) /* r8-r11, lr set */
910 int bitmask = epilogue_regmask & 0x1fff;
911 if (epilogue_regmask & (3 << 14)) /* lr or pc */
912 bitmask |= 0x2000;
913 epilogue[pos++] = 0x80 | (bitmask >> 8); /* push.w {r0-r12,lr} */
914 epilogue[pos++] = bitmask & 0xff;
916 else if (epilogue_regmask) /* r0-r7, pc set */
918 int bitmask = epilogue_regmask & 0xff;
919 if (epilogue_regmask & (1 << 15)) /* pc */
920 bitmask |= 0x100; /* lr */
921 epilogue[pos++] = 0xec | (bitmask >> 8); /* push {r0-r7,lr} */
922 epilogue[pos++] = bitmask & 0xff;
925 if (func->u.s.H && !(func->u.s.L && func->u.s.Ret == 0))
926 epilogue[pos++] = 0x04; /* add sp, sp, #16 */
927 else if (func->u.s.H && (func->u.s.L && func->u.s.Ret == 0))
929 epilogue[pos++] = 0xef; /* ldr lr, [sp], #20 */
930 epilogue[pos++] = 5;
933 if (func->u.s.Ret == 1)
934 epilogue[pos++] = 0xfd; /* bx lr */
935 else if (func->u.s.Ret == 2)
936 epilogue[pos++] = 0xfe; /* b address */
937 else
938 epilogue[pos++] = 0xff; /* end */
939 epilogue_end = &epilogue[pos];
941 if (func->u.s.Flag == 1 && offset < 4 * (prologue_end - prologue)) {
942 /* Check prologue */
943 len = get_sequence_len( prologue, prologue_end, 0 );
944 if (offset < len)
946 process_unwind_codes( prologue, prologue_end, context, ptrs, len - offset );
947 return NULL;
951 if (func->u.s.Ret != 3 && 2 * func->u.s.FunctionLength - offset <= 4 * (epilogue_end - epilogue)) {
952 /* Check epilogue */
953 len = get_sequence_len( epilogue, epilogue_end, 1 );
954 if (offset >= 2 * func->u.s.FunctionLength - len)
956 process_unwind_codes( epilogue, epilogue_end, context, ptrs, offset - (2 * func->u.s.FunctionLength - len) );
957 return NULL;
961 /* Execute full prologue */
962 process_unwind_codes( prologue, prologue_end, context, ptrs, 0 );
964 return NULL;
968 /***********************************************************************
969 * unwind_full_data
971 static void *unwind_full_data( ULONG_PTR base, ULONG_PTR pc, RUNTIME_FUNCTION *func,
972 CONTEXT *context, PVOID *handler_data, KNONVOLATILE_CONTEXT_POINTERS *ptrs )
974 struct unwind_info *info;
975 struct unwind_info_epilog *info_epilog;
976 unsigned int i, codes, epilogs, len, offset;
977 void *data;
978 BYTE *end;
980 info = (struct unwind_info *)((char *)base + func->u.UnwindData);
981 data = info + 1;
982 epilogs = info->epilog;
983 codes = info->codes;
984 if (!codes && !epilogs)
986 struct unwind_info_ext *infoex = data;
987 codes = infoex->codes;
988 epilogs = infoex->epilog;
989 data = infoex + 1;
991 info_epilog = data;
992 if (!info->e) data = info_epilog + epilogs;
994 offset = (pc - base) - func->BeginAddress;
995 end = (BYTE *)data + codes * 4;
997 TRACE( "function %lx-%lx: len=%#x ver=%u X=%u E=%u F=%u epilogs=%u codes=%u\n",
998 base + func->BeginAddress, base + func->BeginAddress + info->function_length * 2,
999 info->function_length, info->version, info->x, info->e, info->f, epilogs, codes * 4 );
1001 /* check for prolog */
1002 if (offset < codes * 4 * 4 && !info->f)
1004 len = get_sequence_len( data, end, 0 );
1005 if (offset < len)
1007 process_unwind_codes( data, end, context, ptrs, len - offset );
1008 return NULL;
1012 /* check for epilog */
1013 if (!info->e)
1015 for (i = 0; i < epilogs; i++)
1017 /* TODO: Currently not checking epilogue conditions. */
1018 if (offset < 2 * info_epilog[i].offset) break;
1019 if (offset - 2 * info_epilog[i].offset < (codes * 4 - info_epilog[i].index) * 4)
1021 BYTE *ptr = (BYTE *)data + info_epilog[i].index;
1022 len = get_sequence_len( ptr, end, 1 );
1023 if (offset <= 2 * info_epilog[i].offset + len)
1025 process_unwind_codes( ptr, end, context, ptrs, offset - 2 * info_epilog[i].offset );
1026 return NULL;
1031 else if (2 * info->function_length - offset <= (codes * 4 - epilogs) * 4)
1033 BYTE *ptr = (BYTE *)data + epilogs;
1034 len = get_sequence_len( ptr, end, 1 );
1035 if (offset >= 2 * info->function_length - len)
1037 process_unwind_codes( ptr, end, context, ptrs, offset - (2 * info->function_length - len) );
1038 return NULL;
1042 process_unwind_codes( data, end, context, ptrs, 0 );
1044 /* get handler since we are inside the main code */
1045 if (info->x)
1047 DWORD *handler_rva = (DWORD *)data + codes;
1048 *handler_data = handler_rva + 1;
1049 return (char *)base + *handler_rva;
1051 return NULL;
1054 /***********************************************************************
1055 * RtlVirtualUnwind (NTDLL.@)
1057 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG_PTR base, ULONG_PTR pc,
1058 RUNTIME_FUNCTION *func, CONTEXT *context,
1059 PVOID *handler_data, ULONG_PTR *frame_ret,
1060 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
1062 void *handler;
1064 TRACE( "type %lx pc %Ix sp %lx func %lx\n", type, pc, context->Sp, base + func->BeginAddress );
1066 *handler_data = NULL;
1068 context->Pc = 0;
1069 if (func->u.s.Flag)
1070 handler = unwind_packed_data( base, pc, func, context, ctx_ptr );
1071 else
1072 handler = unwind_full_data( base, pc, func, context, handler_data, ctx_ptr );
1074 TRACE( "ret: lr=%lx sp=%lx handler=%p\n", context->Lr, context->Sp, handler );
1075 if (!context->Pc)
1076 context->Pc = context->Lr;
1077 context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
1078 *frame_ret = context->Sp;
1079 return handler;
1083 /**********************************************************************
1084 * call_consolidate_callback
1086 * Wrapper function to call a consolidate callback from a fake frame.
1087 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1088 * we have to skip all frames which were already processed. To do that we
1089 * trick the unwinding functions into thinking the call came from somewhere
1090 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1091 * DW_CFA_expression, and the expressions have the following format:
1093 * DW_OP_breg13; sleb128 <OFFSET> | Load SP + struct member offset
1094 * [DW_OP_deref] | Dereference, only for CFA
1096 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1097 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1098 EXCEPTION_RECORD *rec );
1099 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1100 "push {r0-r2,lr}\n\t"
1101 __ASM_SEH(".seh_nop\n\t")
1102 "sub sp, sp, #0x1a0\n\t"
1103 __ASM_SEH(".seh_nop\n\t")
1104 "mov r1, r0\n\t"
1105 __ASM_SEH(".seh_nop\n\t")
1106 "mov r0, sp\n\t"
1107 __ASM_SEH(".seh_nop\n\t")
1108 "mov r2, #0x1a0\n\t"
1109 __ASM_SEH(".seh_nop_w\n\t")
1110 "bl " __ASM_NAME("memcpy") "\n\t"
1111 __ASM_SEH(".seh_custom 0xf4\n\t") /* A custom (unallocated) SEH opcode for CONTEXT on stack */
1112 __ASM_SEH(".seh_endprologue\n\t")
1113 __ASM_CFI(".cfi_def_cfa 13, 0\n\t")
1114 __ASM_CFI(".cfi_escape 0x0f,0x04,0x7d,0xb8,0x00,0x06\n\t") /* DW_CFA_def_cfa_expression: DW_OP_breg13 + 56, DW_OP_deref */
1115 __ASM_CFI(".cfi_escape 0x10,0x04,0x02,0x7d,0x14\n\t") /* DW_CFA_expression: R4 DW_OP_breg13 + 20 */
1116 __ASM_CFI(".cfi_escape 0x10,0x05,0x02,0x7d,0x18\n\t") /* DW_CFA_expression: R5 DW_OP_breg13 + 24 */
1117 __ASM_CFI(".cfi_escape 0x10,0x06,0x02,0x7d,0x1c\n\t") /* DW_CFA_expression: R6 DW_OP_breg13 + 28 */
1118 __ASM_CFI(".cfi_escape 0x10,0x07,0x02,0x7d,0x20\n\t") /* DW_CFA_expression: R7 DW_OP_breg13 + 32 */
1119 __ASM_CFI(".cfi_escape 0x10,0x08,0x02,0x7d,0x24\n\t") /* DW_CFA_expression: R8 DW_OP_breg13 + 36 */
1120 __ASM_CFI(".cfi_escape 0x10,0x09,0x02,0x7d,0x28\n\t") /* DW_CFA_expression: R9 DW_OP_breg13 + 40 */
1121 __ASM_CFI(".cfi_escape 0x10,0x0a,0x02,0x7d,0x2c\n\t") /* DW_CFA_expression: R10 DW_OP_breg13 + 44 */
1122 __ASM_CFI(".cfi_escape 0x10,0x0b,0x02,0x7d,0x30\n\t") /* DW_CFA_expression: R11 DW_OP_breg13 + 48 */
1123 __ASM_CFI(".cfi_escape 0x10,0x0e,0x03,0x7d,0xc0,0x00\n\t") /* DW_CFA_expression: LR DW_OP_breg13 + 64 (PC) */
1124 /* Libunwind doesn't support the registers D8-D15 like this */
1125 #if 0
1126 __ASM_CFI(".cfi_escape 0x10,0x88,0x02,0x03,0x7d,0x90,0x01\n\t") /* DW_CFA_expression: D8 DW_OP_breg13 + 144 */
1127 __ASM_CFI(".cfi_escape 0x10,0x89,0x02,0x03,0x7d,0x98,0x01\n\t") /* DW_CFA_expression: D9 DW_OP_breg13 + 152 */
1128 __ASM_CFI(".cfi_escape 0x10,0x8a,0x02,0x03,0x7d,0xa0,0x01\n\t") /* DW_CFA_expression: D10 DW_OP_breg13 + 160 */
1129 __ASM_CFI(".cfi_escape 0x10,0x8b,0x02,0x03,0x7d,0xa8,0x01\n\t") /* DW_CFA_expression: D11 DW_OP_breg13 + 168 */
1130 __ASM_CFI(".cfi_escape 0x10,0x8c,0x02,0x03,0x7d,0xb0,0x01\n\t") /* DW_CFA_expression: D12 DW_OP_breg13 + 176 */
1131 __ASM_CFI(".cfi_escape 0x10,0x8d,0x02,0x03,0x7d,0xb8,0x01\n\t") /* DW_CFA_expression: D13 DW_OP_breg13 + 184 */
1132 __ASM_CFI(".cfi_escape 0x10,0x8e,0x02,0x03,0x7d,0xc0,0x01\n\t") /* DW_CFA_expression: D14 DW_OP_breg13 + 192 */
1133 __ASM_CFI(".cfi_escape 0x10,0x8f,0x02,0x03,0x7d,0xc8,0x01\n\t") /* DW_CFA_expression: D15 DW_OP_breg13 + 200 */
1134 #endif
1135 /* These EHABI opcodes are to be read bottom up - they
1136 * restore relevant registers from the CONTEXT. */
1137 __ASM_EHABI(".save {sp}\n\t") /* Restore Sp last */
1138 __ASM_EHABI(".pad #-(0x80 + 0x0c + 0x0c)\n\t") /* Move back across D0-D15, Cpsr, Fpscr, Padding, Pc, Lr and Sp */
1139 __ASM_EHABI(".vsave {d8-d15}\n\t")
1140 __ASM_EHABI(".pad #0x40\n\t") /* Skip past D0-D7 */
1141 __ASM_EHABI(".pad #0x0c\n\t") /* Skip past Cpsr, Fpscr and Padding */
1142 __ASM_EHABI(".save {lr, pc}\n\t")
1143 __ASM_EHABI(".pad #0x08\n\t") /* Skip past R12 and Sp - Sp is restored last */
1144 __ASM_EHABI(".save {r4-r11}\n\t")
1145 __ASM_EHABI(".pad #0x14\n\t") /* Skip past ContextFlags and R0-R3 */
1147 "ldrd r1, r2, [sp, #0x1a4]\n\t"
1148 "mov r0, r2\n\t"
1149 "blx r1\n\t"
1150 "add sp, sp, #0x1ac\n\t"
1151 "pop {pc}\n\t")
1155 /*******************************************************************
1156 * RtlRestoreContext (NTDLL.@)
1158 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1160 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1162 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1164 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1165 int i;
1167 for (i = 4; i <= 11; i++)
1168 (&context->R4)[i-4] = (&jmp->R4)[i-4];
1169 context->Lr = jmp->Pc;
1170 context->Sp = jmp->Sp;
1171 context->Fpscr = jmp->Fpscr;
1173 for (i = 0; i < 8; i++)
1174 context->u.D[8+i] = jmp->D[i];
1176 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1178 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1179 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1180 rec->ExceptionInformation[10] = (ULONG_PTR)&context->R4;
1182 context->Pc = (DWORD)call_consolidate_callback( context, consolidate, rec );
1185 /* hack: remove no longer accessible TEB frames */
1186 while ((DWORD)teb_frame < context->Sp)
1188 TRACE( "removing TEB frame: %p\n", teb_frame );
1189 teb_frame = __wine_pop_frame( teb_frame );
1192 TRACE( "returning to %lx stack %lx\n", context->Pc, context->Sp );
1193 NtContinue( context, FALSE );
1197 /***********************************************************************
1198 * RtlUnwindEx (NTDLL.@)
1200 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1201 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1203 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1204 EXCEPTION_RECORD record;
1205 DISPATCHER_CONTEXT dispatch;
1206 CONTEXT new_context;
1207 NTSTATUS status;
1208 DWORD i;
1210 RtlCaptureContext( context );
1211 new_context = *context;
1213 /* build an exception record, if we do not have one */
1214 if (!rec)
1216 record.ExceptionCode = STATUS_UNWIND;
1217 record.ExceptionFlags = 0;
1218 record.ExceptionRecord = NULL;
1219 record.ExceptionAddress = (void *)context->Pc;
1220 record.NumberParameters = 0;
1221 rec = &record;
1224 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1226 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%08lx\n",
1227 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Pc );
1228 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1229 TRACE( " info[%ld]=%08Ix\n", i, rec->ExceptionInformation[i] );
1230 TRACE(" r0=%08lx r1=%08lx r2=%08lx r3=%08lx\n",
1231 context->R0, context->R1, context->R2, context->R3 );
1232 TRACE(" r4=%08lx r5=%08lx r6=%08lx r7=%08lx\n",
1233 context->R4, context->R5, context->R6, context->R7 );
1234 TRACE(" r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
1235 context->R8, context->R9, context->R10, context->R11 );
1236 TRACE(" r12=%08lx sp=%08lx lr=%08lx pc=%08lx\n",
1237 context->R12, context->Sp, context->Lr, context->Pc );
1239 dispatch.TargetPc = (ULONG_PTR)target_ip;
1240 dispatch.ContextRecord = context;
1241 dispatch.HistoryTable = table;
1242 dispatch.NonVolatileRegisters = (BYTE *)&context->R4;
1244 for (;;)
1246 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1247 if (status != STATUS_SUCCESS) raise_status( status, rec );
1249 unwind_done:
1250 if (!dispatch.EstablisherFrame) break;
1252 if (!is_valid_frame( dispatch.EstablisherFrame ))
1254 ERR( "invalid frame %lx (%p-%p)\n", dispatch.EstablisherFrame,
1255 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1256 rec->ExceptionFlags |= EH_STACK_INVALID;
1257 break;
1260 if (dispatch.LanguageHandler)
1262 if (end_frame && (dispatch.EstablisherFrame > (DWORD)end_frame))
1264 ERR( "invalid end frame %lx/%p\n", dispatch.EstablisherFrame, end_frame );
1265 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1267 if (dispatch.EstablisherFrame == (DWORD)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1268 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1270 ULONG_PTR frame;
1272 *context = new_context = *dispatch.ContextRecord;
1273 dispatch.ContextRecord = context;
1274 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1275 dispatch.ControlPc, dispatch.FunctionEntry,
1276 &new_context, &dispatch.HandlerData, &frame,
1277 NULL );
1278 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1279 goto unwind_done;
1281 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1283 else /* hack: call builtin handlers registered in the tib list */
1285 DWORD backup_frame = dispatch.EstablisherFrame;
1286 while ((DWORD)teb_frame < new_context.Sp && (DWORD)teb_frame < (DWORD)end_frame)
1288 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1289 dispatch.EstablisherFrame = (DWORD)teb_frame;
1290 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1292 ULONG_PTR frame;
1294 teb_frame = __wine_pop_frame( teb_frame );
1296 *context = new_context = *dispatch.ContextRecord;
1297 dispatch.ContextRecord = context;
1298 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1299 dispatch.ControlPc, dispatch.FunctionEntry,
1300 &new_context, &dispatch.HandlerData,
1301 &frame, NULL );
1302 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1303 goto unwind_done;
1305 teb_frame = __wine_pop_frame( teb_frame );
1307 if ((DWORD)teb_frame == (DWORD)end_frame && (DWORD)end_frame < new_context.Sp) break;
1308 dispatch.EstablisherFrame = backup_frame;
1311 if (dispatch.EstablisherFrame == (DWORD)end_frame) break;
1312 *context = new_context;
1315 context->R0 = (DWORD)retval;
1316 context->Pc = (DWORD)target_ip;
1317 RtlRestoreContext(context, rec);
1321 /***********************************************************************
1322 * RtlUnwind (NTDLL.@)
1324 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1326 CONTEXT context;
1327 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1331 /*******************************************************************
1332 * __jump_unwind (NTDLL.@)
1334 void WINAPI __jump_unwind( void *frame, void *target_ip )
1336 CONTEXT context;
1337 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1340 extern LONG __C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs, PVOID frame,
1341 PEXCEPTION_FILTER filter,
1342 PUCHAR nonvolatile);
1343 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter,
1344 "push {r4-r11,lr}\n\t"
1345 __ASM_EHABI(".save {r4-r11,lr}\n\t")
1346 __ASM_SEH(".seh_save_regs_w {r4-r11,lr}\n\t")
1347 __ASM_SEH(".seh_endprologue\n\t")
1349 __ASM_CFI(".cfi_def_cfa 13, 36\n\t")
1350 __ASM_CFI(".cfi_offset r4, -36\n\t")
1351 __ASM_CFI(".cfi_offset r5, -32\n\t")
1352 __ASM_CFI(".cfi_offset r6, -28\n\t")
1353 __ASM_CFI(".cfi_offset r7, -24\n\t")
1354 __ASM_CFI(".cfi_offset r8, -20\n\t")
1355 __ASM_CFI(".cfi_offset r9, -16\n\t")
1356 __ASM_CFI(".cfi_offset r10, -12\n\t")
1357 __ASM_CFI(".cfi_offset r11, -8\n\t")
1358 __ASM_CFI(".cfi_offset lr, -4\n\t")
1360 "ldm r3, {r4-r11,lr}\n\t"
1361 "blx r2\n\t"
1362 "pop {r4-r11,pc}\n\t" )
1364 extern void __C_ExecuteTerminationHandler(BOOL abnormal, PVOID frame,
1365 PTERMINATION_HANDLER handler,
1366 PUCHAR nonvolatile);
1367 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1368 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler,
1369 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1371 /*******************************************************************
1372 * __C_specific_handler (NTDLL.@)
1374 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1375 void *frame,
1376 CONTEXT *context,
1377 struct _DISPATCHER_CONTEXT *dispatch )
1379 SCOPE_TABLE *table = dispatch->HandlerData;
1380 ULONG i;
1381 DWORD ControlPc = dispatch->ControlPc;
1383 TRACE( "%p %p %p %p\n", rec, frame, context, dispatch );
1384 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1386 if (dispatch->ControlPcIsUnwound)
1387 ControlPc -= 2;
1389 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1391 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1393 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1394 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1396 PTERMINATION_HANDLER handler;
1398 if (table->ScopeRecord[i].JumpTarget) continue;
1400 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1401 dispatch->TargetPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1402 dispatch->TargetPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1404 break;
1407 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1408 dispatch->ScopeIndex = i+1;
1410 TRACE( "calling __finally %p frame %p\n", handler, frame );
1411 __C_ExecuteTerminationHandler( TRUE, frame, handler,
1412 dispatch->NonVolatileRegisters );
1415 return ExceptionContinueSearch;
1418 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1420 if (ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1421 ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1423 if (!table->ScopeRecord[i].JumpTarget) continue;
1424 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1426 EXCEPTION_POINTERS ptrs;
1427 PEXCEPTION_FILTER filter;
1429 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1430 ptrs.ExceptionRecord = rec;
1431 ptrs.ContextRecord = context;
1432 TRACE( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1433 switch (__C_ExecuteExceptionFilter( &ptrs, frame, filter,
1434 dispatch->NonVolatileRegisters ))
1436 case EXCEPTION_EXECUTE_HANDLER:
1437 break;
1438 case EXCEPTION_CONTINUE_SEARCH:
1439 continue;
1440 case EXCEPTION_CONTINUE_EXECUTION:
1441 return ExceptionContinueExecution;
1444 TRACE( "unwinding to target %lx\n", dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1445 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1446 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1449 return ExceptionContinueSearch;
1453 /***********************************************************************
1454 * RtlRaiseException (NTDLL.@)
1456 __ASM_STDCALL_FUNC( RtlRaiseException, 4,
1457 "push {r0, lr}\n\t"
1458 __ASM_EHABI(".save {r0, lr}\n\t")
1459 __ASM_SEH(".seh_save_regs {r0, lr}\n\t")
1460 "sub sp, sp, #0x1a0\n\t" /* sizeof(CONTEXT) */
1461 __ASM_EHABI(".pad #0x1a0\n\t")
1462 __ASM_SEH(".seh_stackalloc 0x1a0\n\t")
1463 __ASM_SEH(".seh_endprologue\n\t")
1464 __ASM_CFI(".cfi_adjust_cfa_offset 424\n\t")
1465 __ASM_CFI(".cfi_offset lr, -4\n\t")
1466 "mov r0, sp\n\t" /* context */
1467 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1468 "ldr r0, [sp, #0x1a0]\n\t" /* rec */
1469 "ldr r1, [sp, #0x1a4]\n\t"
1470 "str r1, [sp, #0x3c]\n\t" /* context->Lr */
1471 "str r1, [sp, #0x40]\n\t" /* context->Pc */
1472 "mrs r2, CPSR\n\t"
1473 "bfi r2, r1, #5, #1\n\t" /* Thumb bit */
1474 "str r2, [sp, #0x44]\n\t" /* context->Cpsr */
1475 "str r1, [r0, #12]\n\t" /* rec->ExceptionAddress */
1476 "add r1, sp, #0x1a8\n\t"
1477 "str r1, [sp, #0x38]\n\t" /* context->Sp */
1478 "mov r1, sp\n\t"
1479 "mov r2, #1\n\t"
1480 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1481 "bl " __ASM_NAME("RtlRaiseStatus") )
1483 /*************************************************************************
1484 * RtlCaptureStackBackTrace (NTDLL.@)
1486 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1488 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip, count, buffer, hash );
1489 return 0;
1492 /***********************************************************************
1493 * signal_start_thread
1495 __ASM_GLOBAL_FUNC( signal_start_thread,
1496 "mov sp, r0\n\t" /* context */
1497 "mov r1, #1\n\t"
1498 "b " __ASM_NAME("NtContinue") )
1500 /**********************************************************************
1501 * DbgBreakPoint (NTDLL.@)
1503 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1505 /**********************************************************************
1506 * DbgUserBreakPoint (NTDLL.@)
1508 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1510 #endif /* __arm__ */