dmstyle: Rewrite style pref chunk parsing.
[wine.git] / dlls / ntdll / signal_x86_64.c
blobae658910868f0b5b7f4e8870281c66d4d0746603
1 /*
2 * x86-64 signal handling routines
4 * Copyright 1999, 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifdef __x86_64__
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30 #include "wine/exception.h"
31 #include "wine/list.h"
32 #include "ntdll_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(unwind);
36 WINE_DECLARE_DEBUG_CHANNEL(seh);
37 WINE_DECLARE_DEBUG_CHANNEL(relay);
38 WINE_DECLARE_DEBUG_CHANNEL(threadname);
40 typedef struct _SCOPE_TABLE
42 ULONG Count;
43 struct
45 ULONG BeginAddress;
46 ULONG EndAddress;
47 ULONG HandlerAddress;
48 ULONG JumpTarget;
49 } ScopeRecord[1];
50 } SCOPE_TABLE, *PSCOPE_TABLE;
53 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
54 struct MSVCRT_JUMP_BUFFER
56 ULONG64 Frame;
57 ULONG64 Rbx;
58 ULONG64 Rsp;
59 ULONG64 Rbp;
60 ULONG64 Rsi;
61 ULONG64 Rdi;
62 ULONG64 R12;
63 ULONG64 R13;
64 ULONG64 R14;
65 ULONG64 R15;
66 ULONG64 Rip;
67 ULONG MxCsr;
68 USHORT FpCsr;
69 USHORT Spare;
70 M128A Xmm6;
71 M128A Xmm7;
72 M128A Xmm8;
73 M128A Xmm9;
74 M128A Xmm10;
75 M128A Xmm11;
76 M128A Xmm12;
77 M128A Xmm13;
78 M128A Xmm14;
79 M128A Xmm15;
82 /***********************************************************************
83 * Definitions for Win32 unwind tables
86 union handler_data
88 RUNTIME_FUNCTION chain;
89 ULONG handler;
92 struct opcode
94 BYTE offset;
95 BYTE code : 4;
96 BYTE info : 4;
99 struct UNWIND_INFO
101 BYTE version : 3;
102 BYTE flags : 5;
103 BYTE prolog;
104 BYTE count;
105 BYTE frame_reg : 4;
106 BYTE frame_offset : 4;
107 struct opcode opcodes[1]; /* info->count entries */
108 /* followed by handler_data */
111 #define UWOP_PUSH_NONVOL 0
112 #define UWOP_ALLOC_LARGE 1
113 #define UWOP_ALLOC_SMALL 2
114 #define UWOP_SET_FPREG 3
115 #define UWOP_SAVE_NONVOL 4
116 #define UWOP_SAVE_NONVOL_FAR 5
117 #define UWOP_EPILOG 6
118 #define UWOP_SAVE_XMM128 8
119 #define UWOP_SAVE_XMM128_FAR 9
120 #define UWOP_PUSH_MACHFRAME 10
122 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
124 static const char * const reg_names[16] =
125 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
126 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
128 union handler_data *handler_data;
129 struct UNWIND_INFO *info;
130 unsigned int i, count;
132 TRACE( "**** func %lx-%lx\n", function->BeginAddress, function->EndAddress );
133 for (;;)
135 if (function->UnwindData & 1)
137 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
138 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
139 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
140 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
141 function = next;
142 continue;
144 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
146 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
147 info, info->flags, info->prolog,
148 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
150 if (info->frame_reg)
151 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
152 reg_names[info->frame_reg], info->frame_offset * 16 );
154 for (i = 0; i < info->count; i++)
156 TRACE( " 0x%x: ", info->opcodes[i].offset );
157 switch (info->opcodes[i].code)
159 case UWOP_PUSH_NONVOL:
160 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
161 break;
162 case UWOP_ALLOC_LARGE:
163 if (info->opcodes[i].info)
165 count = *(DWORD *)&info->opcodes[i+1];
166 i += 2;
168 else
170 count = *(USHORT *)&info->opcodes[i+1] * 8;
171 i++;
173 TRACE( "subq $0x%x,%%rsp\n", count );
174 break;
175 case UWOP_ALLOC_SMALL:
176 count = (info->opcodes[i].info + 1) * 8;
177 TRACE( "subq $0x%x,%%rsp\n", count );
178 break;
179 case UWOP_SET_FPREG:
180 TRACE( "leaq 0x%x(%%rsp),%s\n",
181 info->frame_offset * 16, reg_names[info->frame_reg] );
182 break;
183 case UWOP_SAVE_NONVOL:
184 count = *(USHORT *)&info->opcodes[i+1] * 8;
185 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
186 i++;
187 break;
188 case UWOP_SAVE_NONVOL_FAR:
189 count = *(DWORD *)&info->opcodes[i+1];
190 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
191 i += 2;
192 break;
193 case UWOP_SAVE_XMM128:
194 count = *(USHORT *)&info->opcodes[i+1] * 16;
195 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
196 i++;
197 break;
198 case UWOP_SAVE_XMM128_FAR:
199 count = *(DWORD *)&info->opcodes[i+1];
200 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
201 i += 2;
202 break;
203 case UWOP_PUSH_MACHFRAME:
204 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
205 break;
206 case UWOP_EPILOG:
207 if (info->version == 2)
209 unsigned int offset;
210 if (info->opcodes[i].info)
211 offset = info->opcodes[i].offset;
212 else
213 offset = (info->opcodes[i+1].info << 8) + info->opcodes[i+1].offset;
214 TRACE("epilog %p-%p\n", (char *)base + function->EndAddress - offset,
215 (char *)base + function->EndAddress - offset + info->opcodes[i].offset );
216 i += 1;
217 break;
219 default:
220 FIXME( "unknown code %u\n", info->opcodes[i].code );
221 break;
225 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
226 if (info->flags & UNW_FLAG_CHAININFO)
228 TRACE( " chained to function %p-%p\n",
229 (char *)base + handler_data->chain.BeginAddress,
230 (char *)base + handler_data->chain.EndAddress );
231 function = &handler_data->chain;
232 continue;
234 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
235 TRACE( " handler %p data at %p\n",
236 (char *)base + handler_data->handler, &handler_data->handler + 1 );
237 break;
241 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
243 unsigned int i;
245 TRACE( "scope table at %p\n", table );
246 for (i = 0; i < table->Count; i++)
247 TRACE( " %u: %p-%p handler %p target %p\n", i,
248 (char *)base + table->ScopeRecord[i].BeginAddress,
249 (char *)base + table->ScopeRecord[i].EndAddress,
250 (char *)base + table->ScopeRecord[i].HandlerAddress,
251 (char *)base + table->ScopeRecord[i].JumpTarget );
255 /***********************************************************************
256 * virtual_unwind
258 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
260 LDR_DATA_TABLE_ENTRY *module;
261 NTSTATUS status;
263 dispatch->ImageBase = 0;
264 dispatch->ScopeIndex = 0;
265 dispatch->ControlPc = context->Rip;
267 /* first look for PE exception information */
269 if ((dispatch->FunctionEntry = lookup_function_info( context->Rip, &dispatch->ImageBase, &module )))
271 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, context->Rip,
272 dispatch->FunctionEntry, context,
273 &dispatch->HandlerData, &dispatch->EstablisherFrame,
274 NULL );
275 return STATUS_SUCCESS;
278 /* then look for host system exception information */
280 if (!module || (module->Flags & LDR_WINE_INTERNAL))
282 struct unwind_builtin_dll_params params = { type, dispatch, context };
284 status = WINE_UNIX_CALL( unix_unwind_builtin_dll, &params );
285 if (!status && dispatch->LanguageHandler && !module)
287 FIXME( "calling personality routine in system library not supported yet\n" );
288 dispatch->LanguageHandler = NULL;
290 if (status != STATUS_UNSUCCESSFUL) return status;
292 else WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
294 /* no exception information, treat as a leaf function */
296 dispatch->EstablisherFrame = context->Rsp;
297 dispatch->LanguageHandler = NULL;
298 context->Rip = *(ULONG64 *)context->Rsp;
299 context->Rsp = context->Rsp + sizeof(ULONG64);
300 return STATUS_SUCCESS;
304 /**************************************************************************
305 * __chkstk (NTDLL.@)
307 * Supposed to touch all the stack pages, but we shouldn't need that.
309 __ASM_GLOBAL_FUNC( __chkstk, "ret" );
312 /***********************************************************************
313 * RtlCaptureContext (NTDLL.@)
315 __ASM_GLOBAL_FUNC( RtlCaptureContext,
316 "pushfq\n\t"
317 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
318 "movl $0x10000f,0x30(%rcx)\n\t" /* context->ContextFlags */
319 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
320 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
321 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
322 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
323 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
324 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
325 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
326 "popq 0x44(%rcx)\n\t" /* context->Eflags */
327 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
328 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
329 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
330 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
331 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
332 "leaq 8(%rsp),%rax\n\t"
333 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
334 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
335 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
336 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
337 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
338 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
339 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
340 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
341 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
342 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
343 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
344 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
345 "movq (%rsp),%rax\n\t"
346 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
347 "fxsave 0x100(%rcx)\n\t" /* context->FltSave */
348 "ret" );
350 DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
351 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
353 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
354 return ExceptionNestedException;
356 return ExceptionContinueSearch;
359 /***********************************************************************
360 * exception_handler_call_wrapper
362 #ifdef __ASM_SEH_SUPPORTED
363 DWORD WINAPI exception_handler_call_wrapper( EXCEPTION_RECORD *rec, void *frame,
364 CONTEXT *context, DISPATCHER_CONTEXT *dispatch );
366 C_ASSERT( offsetof(DISPATCHER_CONTEXT, LanguageHandler) == 0x30 );
368 __ASM_GLOBAL_FUNC( exception_handler_call_wrapper,
369 ".seh_endprologue\n\t"
370 "subq $0x28, %rsp\n\t"
371 ".seh_stackalloc 0x28\n\t"
372 "callq *0x30(%r9)\n\t" /* dispatch->LanguageHandler */
373 "nop\n\t" /* avoid epilogue so handler is called */
374 "addq $0x28, %rsp\n\t"
375 "ret\n\t"
376 ".seh_handler " __ASM_NAME("nested_exception_handler") ", @except\n\t" )
377 #else
378 static DWORD exception_handler_call_wrapper( EXCEPTION_RECORD *rec, void *frame,
379 CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
381 EXCEPTION_REGISTRATION_RECORD wrapper_frame;
382 DWORD res;
384 wrapper_frame.Handler = nested_exception_handler;
385 __wine_push_frame( &wrapper_frame );
386 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
387 __wine_pop_frame( &wrapper_frame );
388 return res;
390 #endif
392 /**********************************************************************
393 * call_handler
395 * Call a single exception handler.
397 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
399 DWORD res;
401 TRACE_(seh)( "calling handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
402 dispatch->LanguageHandler, rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
403 res = exception_handler_call_wrapper( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
404 TRACE_(seh)( "handler at %p returned %lu\n", dispatch->LanguageHandler, res );
406 rec->ExceptionFlags &= EH_NONCONTINUABLE;
407 return res;
411 /**********************************************************************
412 * call_teb_handler
414 * Call a single exception handler from the TEB chain.
415 * FIXME: Handle nested exceptions.
417 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
418 EXCEPTION_REGISTRATION_RECORD *teb_frame )
420 DWORD res;
422 TRACE_(seh)( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
423 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
424 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
425 TRACE_(seh)( "handler at %p returned %lu\n", teb_frame->Handler, res );
426 return res;
430 /**********************************************************************
431 * call_stack_handlers
433 * Call the stack handlers chain.
435 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
437 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
438 UNWIND_HISTORY_TABLE table;
439 DISPATCHER_CONTEXT dispatch;
440 CONTEXT context;
441 NTSTATUS status;
443 context = *orig_context;
444 context.ContextFlags &= ~0x40; /* Clear xstate flag. */
446 dispatch.TargetIp = 0;
447 dispatch.ContextRecord = &context;
448 dispatch.HistoryTable = &table;
449 for (;;)
451 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
452 if (status != STATUS_SUCCESS) return status;
454 unwind_done:
455 if (!dispatch.EstablisherFrame) break;
457 if ((dispatch.EstablisherFrame & 7) ||
458 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
459 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
461 ERR_(seh)( "invalid frame %p (%p-%p)\n", (void *)dispatch.EstablisherFrame,
462 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
463 rec->ExceptionFlags |= EH_STACK_INVALID;
464 break;
467 if (dispatch.LanguageHandler)
469 switch (call_handler( rec, orig_context, &dispatch ))
471 case ExceptionContinueExecution:
472 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
473 return STATUS_SUCCESS;
474 case ExceptionContinueSearch:
475 break;
476 case ExceptionNestedException:
477 rec->ExceptionFlags |= EH_NESTED_CALL;
478 TRACE_(seh)( "nested exception\n" );
479 break;
480 case ExceptionCollidedUnwind: {
481 ULONG64 frame;
483 context = *dispatch.ContextRecord;
484 dispatch.ContextRecord = &context;
485 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
486 dispatch.ControlPc, dispatch.FunctionEntry,
487 &context, NULL, &frame, NULL );
488 goto unwind_done;
490 default:
491 return STATUS_INVALID_DISPOSITION;
494 /* hack: call wine handlers registered in the tib list */
495 else while ((ULONG64)teb_frame < context.Rsp)
497 TRACE_(seh)( "found wine frame %p rsp %p handler %p\n",
498 teb_frame, (void *)context.Rsp, teb_frame->Handler );
499 dispatch.EstablisherFrame = (ULONG64)teb_frame;
500 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
502 case ExceptionContinueExecution:
503 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
504 return STATUS_SUCCESS;
505 case ExceptionContinueSearch:
506 break;
507 case ExceptionNestedException:
508 rec->ExceptionFlags |= EH_NESTED_CALL;
509 TRACE_(seh)( "nested exception\n" );
510 break;
511 case ExceptionCollidedUnwind: {
512 ULONG64 frame;
514 context = *dispatch.ContextRecord;
515 dispatch.ContextRecord = &context;
516 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
517 dispatch.ControlPc, dispatch.FunctionEntry,
518 &context, NULL, &frame, NULL );
519 teb_frame = teb_frame->Prev;
520 goto unwind_done;
522 default:
523 return STATUS_INVALID_DISPOSITION;
525 teb_frame = teb_frame->Prev;
528 if (context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
530 return STATUS_UNHANDLED_EXCEPTION;
534 NTSTATUS WINAPI dispatch_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
536 NTSTATUS status;
537 DWORD c;
539 if (pWow64PrepareForException) pWow64PrepareForException( rec, context );
541 TRACE_(seh)( "code=%lx flags=%lx addr=%p ip=%Ix\n",
542 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Rip );
543 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
544 TRACE_(seh)( " info[%ld]=%016I64x\n", c, rec->ExceptionInformation[c] );
546 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
548 if (rec->ExceptionInformation[1] >> 16)
549 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
550 rec->ExceptionAddress,
551 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
552 else
553 MESSAGE( "wine: Call from %p to unimplemented function %s.%I64d, aborting\n",
554 rec->ExceptionAddress,
555 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
557 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
559 if ((DWORD)rec->ExceptionInformation[2] == -1 || (DWORD)rec->ExceptionInformation[2] == GetCurrentThreadId())
560 WARN_(threadname)( "Thread renamed to %s\n", debugstr_a((char *)rec->ExceptionInformation[1]) );
561 else
562 WARN_(threadname)( "Thread ID %04lx renamed to %s\n", (DWORD)rec->ExceptionInformation[2],
563 debugstr_a((char *)rec->ExceptionInformation[1]) );
565 set_native_thread_name((DWORD)rec->ExceptionInformation[2], (char *)rec->ExceptionInformation[1]);
567 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
569 WARN_(seh)( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
571 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
573 WARN_(seh)( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
575 else
577 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
578 ERR_(seh)( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
579 else
580 WARN_(seh)( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
582 TRACE_(seh)( " rax=%016I64x rbx=%016I64x rcx=%016I64x rdx=%016I64x\n",
583 context->Rax, context->Rbx, context->Rcx, context->Rdx );
584 TRACE_(seh)( " rsi=%016I64x rdi=%016I64x rbp=%016I64x rsp=%016I64x\n",
585 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
586 TRACE_(seh)( " r8=%016I64x r9=%016I64x r10=%016I64x r11=%016I64x\n",
587 context->R8, context->R9, context->R10, context->R11 );
588 TRACE_(seh)( " r12=%016I64x r13=%016I64x r14=%016I64x r15=%016I64x\n",
589 context->R12, context->R13, context->R14, context->R15 );
592 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
593 NtContinue( context, FALSE );
595 if ((status = call_stack_handlers( rec, context )) == STATUS_SUCCESS)
596 NtContinue( context, FALSE );
598 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
599 return NtRaiseException( rec, context, FALSE );
603 NTSTATUS WINAPI dispatch_wow_exception( EXCEPTION_RECORD *rec_ptr, CONTEXT *context_ptr )
605 char buffer[sizeof(CONTEXT) + sizeof(CONTEXT_EX) + sizeof(XSTATE) + 128];
606 CONTEXT *context;
607 CONTEXT_EX *context_ex;
608 EXCEPTION_RECORD rec = *rec_ptr;
610 RtlInitializeExtendedContext( buffer, context_ptr->ContextFlags, &context_ex );
611 context = RtlLocateLegacyContext( context_ex, NULL );
612 RtlCopyContext( context, context_ptr->ContextFlags, context_ptr );
613 return dispatch_exception( &rec, context );
617 /*******************************************************************
618 * KiUserExceptionDispatcher (NTDLL.@)
620 __ASM_GLOBAL_FUNC( KiUserExceptionDispatcher,
621 "mov 0x98(%rsp),%rcx\n\t" /* context->Rsp */
622 "movw %cs,%ax\n\t"
623 "cmpw %ax,0x38(%rsp)\n\t" /* context->SegCs */
624 "je 1f\n\t"
625 "mov %rsp,%rdx\n\t" /* context */
626 "lea 0x4f0(%rsp),%rcx\n\t" /* rec */
627 "movq %r14,%rsp\n\t" /* switch to 64-bit stack */
628 "call " __ASM_NAME("dispatch_wow_exception") "\n\t"
629 "int3\n"
630 "1:\tmov 0xf8(%rsp),%rdx\n\t" /* context->Rip */
631 "mov %rdx,-0x8(%rcx)\n\t"
632 "mov %rbp,-0x10(%rcx)\n\t"
633 "mov %rdi,-0x18(%rcx)\n\t"
634 "mov %rsi,-0x20(%rcx)\n\t"
635 "lea -0x20(%rcx),%rbp\n\t"
636 "mov %rsp,%rdx\n\t" /* context */
637 "lea 0x4f0(%rsp),%rcx\n\t" /* rec */
638 __ASM_SEH(".seh_pushreg %rbp\n\t")
639 __ASM_SEH(".seh_pushreg %rdi\n\t")
640 __ASM_SEH(".seh_pushreg %rsi\n\t")
641 __ASM_SEH(".seh_setframe %rbp,0\n\t")
642 __ASM_SEH(".seh_endprologue\n\t")
644 __ASM_CFI(".cfi_signal_frame\n\t")
645 __ASM_CFI(".cfi_adjust_cfa_offset 0x20\n\t")
646 __ASM_CFI(".cfi_def_cfa %rbp,0x20\n\t")
647 __ASM_CFI(".cfi_rel_offset %rip,0x18\n\t")
648 __ASM_CFI(".cfi_rel_offset %rbp,0x10\n\t")
649 __ASM_CFI(".cfi_rel_offset %rdi,0x8\n\t")
650 __ASM_CFI(".cfi_rel_offset %rsi,0\n\t")
651 "call " __ASM_NAME("dispatch_exception") "\n\t"
652 "int3")
655 /*******************************************************************
656 * KiUserApcDispatcher (NTDLL.@)
658 void WINAPI dispatch_apc( CONTEXT *context, ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3,
659 void (CALLBACK *func)(ULONG_PTR,ULONG_PTR,ULONG_PTR,CONTEXT*) )
661 func( arg1, arg2, arg3, context );
662 NtContinue( context, TRUE );
665 __ASM_GLOBAL_FUNC( KiUserApcDispatcher,
666 "addq $0x8,%rsp\n\t"
667 "mov 0x98(%rcx),%r10\n\t" /* context->Rsp */
668 "mov 0xf8(%rcx),%r11\n\t" /* context->Rip */
669 "mov %r11,-0x8(%r10)\n\t"
670 "mov %rbp,-0x10(%r10)\n\t"
671 "lea -0x10(%r10),%rbp\n\t"
672 __ASM_SEH(".seh_pushreg %rbp\n\t")
673 __ASM_SEH(".seh_setframe %rbp,0\n\t")
674 __ASM_SEH(".seh_endprologue\n\t")
675 __ASM_CFI(".cfi_signal_frame\n\t")
676 __ASM_CFI(".cfi_adjust_cfa_offset 0x10\n\t")
677 __ASM_CFI(".cfi_def_cfa %rbp,0x10\n\t")
678 __ASM_CFI(".cfi_rel_offset %rip,0x8\n\t")
679 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
680 "call " __ASM_NAME("dispatch_apc") "\n\t"
681 "int3")
684 /*******************************************************************
685 * KiUserCallbackDispatcher (NTDLL.@)
687 * FIXME: not binary compatible
689 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
691 NTSTATUS status;
693 __TRY
695 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
696 status = NtCallbackReturn( NULL, 0, func( args, len ));
698 __EXCEPT_ALL
700 ERR_(seh)( "ignoring exception\n" );
701 status = NtCallbackReturn( 0, 0, 0 );
703 __ENDTRY
705 RtlRaiseStatus( status );
709 /**************************************************************************
710 * RtlIsEcCode (NTDLL.@)
712 BOOLEAN WINAPI RtlIsEcCode( const void *ptr )
714 const UINT64 *map = (const UINT64 *)NtCurrentTeb()->Peb->EcCodeBitMap;
715 ULONG_PTR page = (ULONG_PTR)ptr / page_size;
716 if (!map) return FALSE;
717 return (map[page / 64] >> (page & 63)) & 1;
721 static ULONG64 get_int_reg( CONTEXT *context, int reg )
723 return *(&context->Rax + reg);
726 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 *val )
728 *(&context->Rax + reg) = *val;
729 if (ctx_ptr) ctx_ptr->IntegerContext[reg] = val;
732 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A *val )
734 /* Use a memcpy() to avoid issues if val is misaligned. */
735 memcpy(&context->Xmm0 + reg, val, sizeof(*val));
736 if (ctx_ptr) ctx_ptr->FloatingContext[reg] = val;
739 static int get_opcode_size( struct opcode op )
741 switch (op.code)
743 case UWOP_ALLOC_LARGE:
744 return 2 + (op.info != 0);
745 case UWOP_SAVE_NONVOL:
746 case UWOP_SAVE_XMM128:
747 case UWOP_EPILOG:
748 return 2;
749 case UWOP_SAVE_NONVOL_FAR:
750 case UWOP_SAVE_XMM128_FAR:
751 return 3;
752 default:
753 return 1;
757 static BOOL is_inside_epilog( BYTE *pc, ULONG64 base, const RUNTIME_FUNCTION *function )
759 /* add or lea must be the first instruction, and it must have a rex.W prefix */
760 if ((pc[0] & 0xf8) == 0x48)
762 switch (pc[1])
764 case 0x81: /* add $nnnn,%rsp */
765 if (pc[0] == 0x48 && pc[2] == 0xc4)
767 pc += 7;
768 break;
770 return FALSE;
771 case 0x83: /* add $n,%rsp */
772 if (pc[0] == 0x48 && pc[2] == 0xc4)
774 pc += 4;
775 break;
777 return FALSE;
778 case 0x8d: /* lea n(reg),%rsp */
779 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
780 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
781 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
782 if ((pc[2] >> 6) == 1) /* 8-bit offset */
784 pc += 4;
785 break;
787 if ((pc[2] >> 6) == 2) /* 32-bit offset */
789 pc += 7;
790 break;
792 return FALSE;
796 /* now check for various pop instructions */
798 for (;;)
800 if ((*pc & 0xf0) == 0x40) pc++; /* rex prefix */
802 switch (*pc)
804 case 0x58: /* pop %rax/%r8 */
805 case 0x59: /* pop %rcx/%r9 */
806 case 0x5a: /* pop %rdx/%r10 */
807 case 0x5b: /* pop %rbx/%r11 */
808 case 0x5c: /* pop %rsp/%r12 */
809 case 0x5d: /* pop %rbp/%r13 */
810 case 0x5e: /* pop %rsi/%r14 */
811 case 0x5f: /* pop %rdi/%r15 */
812 pc++;
813 continue;
814 case 0xc2: /* ret $nn */
815 case 0xc3: /* ret */
816 return TRUE;
817 case 0xe9: /* jmp nnnn */
818 pc += 5 + *(LONG *)(pc + 1);
819 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
820 continue;
821 break;
822 case 0xeb: /* jmp n */
823 pc += 2 + (signed char)pc[1];
824 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
825 continue;
826 break;
827 case 0xf3: /* rep; ret (for amd64 prediction bug) */
828 return pc[1] == 0xc3;
830 return FALSE;
834 /* execute a function epilog, which must have been validated with is_inside_epilog() */
835 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
837 for (;;)
839 BYTE rex = 0;
841 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
843 switch (*pc)
845 case 0x58: /* pop %rax/r8 */
846 case 0x59: /* pop %rcx/r9 */
847 case 0x5a: /* pop %rdx/r10 */
848 case 0x5b: /* pop %rbx/r11 */
849 case 0x5c: /* pop %rsp/r12 */
850 case 0x5d: /* pop %rbp/r13 */
851 case 0x5e: /* pop %rsi/r14 */
852 case 0x5f: /* pop %rdi/r15 */
853 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, (ULONG64 *)context->Rsp );
854 context->Rsp += sizeof(ULONG64);
855 pc++;
856 continue;
857 case 0x81: /* add $nnnn,%rsp */
858 context->Rsp += *(LONG *)(pc + 2);
859 pc += 2 + sizeof(LONG);
860 continue;
861 case 0x83: /* add $n,%rsp */
862 context->Rsp += (signed char)pc[2];
863 pc += 3;
864 continue;
865 case 0x8d:
866 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
868 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
869 pc += 3;
871 else /* lea nnnn(reg),%rsp */
873 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
874 pc += 2 + sizeof(LONG);
876 continue;
877 case 0xc2: /* ret $nn */
878 context->Rip = *(ULONG64 *)context->Rsp;
879 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
880 return;
881 case 0xc3: /* ret */
882 case 0xf3: /* rep; ret */
883 context->Rip = *(ULONG64 *)context->Rsp;
884 context->Rsp += sizeof(ULONG64);
885 return;
886 case 0xe9: /* jmp nnnn */
887 pc += 5 + *(LONG *)(pc + 1);
888 continue;
889 case 0xeb: /* jmp n */
890 pc += 2 + (signed char)pc[1];
891 continue;
893 return;
897 /**********************************************************************
898 * RtlVirtualUnwind (NTDLL.@)
900 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
901 RUNTIME_FUNCTION *function, CONTEXT *context,
902 PVOID *data, ULONG64 *frame_ret,
903 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
905 union handler_data *handler_data;
906 ULONG64 frame, off;
907 struct UNWIND_INFO *info;
908 unsigned int i, prolog_offset;
909 BOOL mach_frame = FALSE;
911 TRACE( "type %lx rip %I64x rsp %I64x\n", type, pc, context->Rsp );
912 if (TRACE_ON(seh)) dump_unwind_info( base, function );
914 frame = *frame_ret = context->Rsp;
915 for (;;)
917 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
918 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
920 if (info->version != 1 && info->version != 2)
922 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
923 return NULL;
926 if (info->frame_reg)
927 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
929 /* check if in prolog */
930 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
932 TRACE("inside prolog.\n");
933 prolog_offset = pc - base - function->BeginAddress;
935 else
937 prolog_offset = ~0;
938 /* Since Win10 1809 epilogue does not have a special treatment in case of zero opcode count. */
939 if (info->count && is_inside_epilog( (BYTE *)pc, base, function ))
941 TRACE("inside epilog.\n");
942 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
943 *frame_ret = frame;
944 return NULL;
948 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
950 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
952 switch (info->opcodes[i].code)
954 case UWOP_PUSH_NONVOL: /* pushq %reg */
955 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)context->Rsp );
956 context->Rsp += sizeof(ULONG64);
957 break;
958 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
959 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
960 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
961 break;
962 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
963 context->Rsp += (info->opcodes[i].info + 1) * 8;
964 break;
965 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
966 context->Rsp = *frame_ret = frame;
967 break;
968 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
969 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
970 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)off );
971 break;
972 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
973 off = frame + *(DWORD *)&info->opcodes[i+1];
974 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)off );
975 break;
976 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
977 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
978 set_float_reg( context, ctx_ptr, info->opcodes[i].info, (M128A *)off );
979 break;
980 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
981 off = frame + *(DWORD *)&info->opcodes[i+1];
982 set_float_reg( context, ctx_ptr, info->opcodes[i].info, (M128A *)off );
983 break;
984 case UWOP_PUSH_MACHFRAME:
985 if (info->flags & UNW_FLAG_CHAININFO)
987 FIXME("PUSH_MACHFRAME with chained unwind info.\n");
988 break;
990 if (i + get_opcode_size(info->opcodes[i]) < info->count )
992 FIXME("PUSH_MACHFRAME is not the last opcode.\n");
993 break;
996 if (info->opcodes[i].info)
997 context->Rsp += 0x8;
999 context->Rip = *(ULONG64 *)context->Rsp;
1000 context->Rsp = *(ULONG64 *)(context->Rsp + 24);
1001 mach_frame = TRUE;
1002 break;
1003 case UWOP_EPILOG:
1004 if (info->version == 2)
1005 break; /* nothing to do */
1006 default:
1007 FIXME( "unknown code %u\n", info->opcodes[i].code );
1008 break;
1012 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
1013 function = &handler_data->chain; /* restart with the chained info */
1016 if (!mach_frame)
1018 /* now pop return address */
1019 context->Rip = *(ULONG64 *)context->Rsp;
1020 context->Rsp += sizeof(ULONG64);
1023 if (!(info->flags & type)) return NULL; /* no matching handler */
1024 if (prolog_offset != ~0) return NULL; /* inside prolog */
1026 *data = &handler_data->handler + 1;
1027 return (char *)base + handler_data->handler;
1030 struct unwind_exception_frame
1032 EXCEPTION_REGISTRATION_RECORD frame;
1033 char dummy[0x10]; /* Layout 'dispatch' accessed from unwind_exception_handler() so it is above register
1034 * save space when .seh handler is used. */
1035 DISPATCHER_CONTEXT *dispatch;
1038 /**********************************************************************
1039 * unwind_exception_handler
1041 * Handler for exceptions happening while calling an unwind handler.
1043 DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1044 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1046 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
1047 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
1049 /* copy the original dispatcher into the current one, except for the TargetIp */
1050 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
1051 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
1052 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
1053 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
1054 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
1055 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
1056 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
1057 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
1058 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
1059 TRACE( "detected collided unwind\n" );
1060 return ExceptionCollidedUnwind;
1063 /***********************************************************************
1064 * unwind_handler_call_wrapper
1066 #ifdef __ASM_SEH_SUPPORTED
1067 DWORD WINAPI unwind_handler_call_wrapper( EXCEPTION_RECORD *rec, void *frame,
1068 CONTEXT *context, DISPATCHER_CONTEXT *dispatch );
1070 C_ASSERT( sizeof(struct unwind_exception_frame) == 0x28 );
1071 C_ASSERT( offsetof(struct unwind_exception_frame, dispatch) == 0x20 );
1072 C_ASSERT( offsetof(DISPATCHER_CONTEXT, LanguageHandler) == 0x30 );
1074 __ASM_GLOBAL_FUNC( unwind_handler_call_wrapper,
1075 ".seh_endprologue\n\t"
1076 "subq $0x28,%rsp\n\t"
1077 ".seh_stackalloc 0x28\n\t"
1078 "movq %r9,0x20(%rsp)\n\t" /* unwind_exception_frame->dispatch */
1079 "callq *0x30(%r9)\n\t" /* dispatch->LanguageHandler */
1080 "nop\n\t" /* avoid epilogue so handler is called */
1081 "addq $0x28, %rsp\n\t"
1082 "ret\n\t"
1083 ".seh_handler " __ASM_NAME("unwind_exception_handler") ", @except, @unwind\n\t" )
1084 #else
1085 static DWORD unwind_handler_call_wrapper( EXCEPTION_RECORD *rec, void *frame,
1086 CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
1088 struct unwind_exception_frame wrapper_frame;
1089 DWORD res;
1091 wrapper_frame.frame.Handler = unwind_exception_handler;
1092 wrapper_frame.dispatch = dispatch;
1093 __wine_push_frame( &wrapper_frame.frame );
1094 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1095 __wine_pop_frame( &wrapper_frame.frame );
1096 return res;
1098 #endif
1100 /**********************************************************************
1101 * call_unwind_handler
1103 * Call a single unwind handler.
1105 DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
1107 DWORD res;
1109 TRACE( "calling handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
1110 dispatch->LanguageHandler, rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1111 res = unwind_handler_call_wrapper( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
1112 TRACE( "handler %p returned %lx\n", dispatch->LanguageHandler, res );
1114 switch (res)
1116 case ExceptionContinueSearch:
1117 case ExceptionCollidedUnwind:
1118 break;
1119 default:
1120 raise_status( STATUS_INVALID_DISPOSITION, rec );
1121 break;
1124 return res;
1128 /**********************************************************************
1129 * call_teb_unwind_handler
1131 * Call a single unwind handler from the TEB chain.
1133 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
1134 EXCEPTION_REGISTRATION_RECORD *teb_frame )
1136 DWORD res;
1138 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
1139 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
1140 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
1141 TRACE( "handler at %p returned %lu\n", teb_frame->Handler, res );
1143 switch (res)
1145 case ExceptionContinueSearch:
1146 case ExceptionCollidedUnwind:
1147 break;
1148 default:
1149 raise_status( STATUS_INVALID_DISPOSITION, rec );
1150 break;
1153 return res;
1157 /**********************************************************************
1158 * call_consolidate_callback
1160 * Wrapper function to call a consolidate callback from a fake frame.
1161 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1162 * we have to skip all frames which were already processed. To do that we
1163 * trick the unwinding functions into thinking the call came from the specified
1164 * context. All CFI instructions are either DW_CFA_def_cfa_expression or
1165 * DW_CFA_expression, and the expressions have the following format:
1167 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
1168 * DW_OP_deref | Get *(%rbp + 0x10) == context
1169 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
1170 * [DW_OP_deref] | Dereference, only for CFA
1172 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1173 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1174 EXCEPTION_RECORD *rec );
1175 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1176 "pushq %rbp\n\t"
1177 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1178 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1179 "movq %rsp,%rbp\n\t"
1180 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1182 /* Setup SEH machine frame. */
1183 "subq $0x28,%rsp\n\t"
1184 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
1185 "movq 0xf8(%rcx),%rax\n\t" /* Context->Rip */
1186 "movq %rax,(%rsp)\n\t"
1187 "movq 0x98(%rcx),%rax\n\t" /* context->Rsp */
1188 "movq %rax,0x18(%rsp)\n\t"
1189 __ASM_SEH(".seh_pushframe\n\t")
1190 __ASM_SEH(".seh_endprologue\n\t")
1192 "subq $0x108,%rsp\n\t" /* 10*16 (float regs) + 8*8 (int regs) + 32 (shadow store) + 8 (align). */
1193 __ASM_SEH(".seh_stackalloc 0x108\n\t")
1194 __ASM_CFI(".cfi_adjust_cfa_offset 0x108\n\t")
1196 /* Setup CFI unwind to context. */
1197 "movq %rcx,0x10(%rbp)\n\t"
1198 __ASM_CFI(".cfi_remember_state\n\t")
1199 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
1200 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
1201 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
1202 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
1203 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
1204 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
1205 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
1206 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
1207 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
1208 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
1209 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
1210 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
1211 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
1212 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
1213 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
1214 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
1215 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
1216 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
1217 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
1218 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
1220 /* Setup SEH unwind registers restore. */
1221 "movq 0xa0(%rcx),%rax\n\t" /* context->Rbp */
1222 "movq %rax,0x100(%rsp)\n\t"
1223 __ASM_SEH(".seh_savereg %rbp, 0x100\n\t")
1224 "movq 0x90(%rcx),%rax\n\t" /* context->Rbx */
1225 "movq %rax,0x20(%rsp)\n\t"
1226 __ASM_SEH(".seh_savereg %rbx, 0x20\n\t")
1227 "movq 0xa8(%rcx),%rax\n\t" /* context->Rsi */
1228 "movq %rax,0x28(%rsp)\n\t"
1229 __ASM_SEH(".seh_savereg %rsi, 0x28\n\t")
1230 "movq 0xb0(%rcx),%rax\n\t" /* context->Rdi */
1231 "movq %rax,0x30(%rsp)\n\t"
1232 __ASM_SEH(".seh_savereg %rdi, 0x30\n\t")
1234 "movq 0xd8(%rcx),%rax\n\t" /* context->R12 */
1235 "movq %rax,0x38(%rsp)\n\t"
1236 __ASM_SEH(".seh_savereg %r12, 0x38\n\t")
1237 "movq 0xe0(%rcx),%rax\n\t" /* context->R13 */
1238 "movq %rax,0x40(%rsp)\n\t"
1239 __ASM_SEH(".seh_savereg %r13, 0x40\n\t")
1240 "movq 0xe8(%rcx),%rax\n\t" /* context->R14 */
1241 "movq %rax,0x48(%rsp)\n\t"
1242 __ASM_SEH(".seh_savereg %r14, 0x48\n\t")
1243 "movq 0xf0(%rcx),%rax\n\t" /* context->R15 */
1244 "movq %rax,0x50(%rsp)\n\t"
1245 __ASM_SEH(".seh_savereg %r15, 0x50\n\t")
1246 "pushq %rsi\n\t"
1247 "pushq %rdi\n\t"
1248 "leaq 0x200(%rcx),%rsi\n\t"
1249 "leaq 0x70(%rsp),%rdi\n\t"
1250 "movq $0x14,%rcx\n\t"
1251 "cld\n\t"
1252 "rep; movsq\n\t"
1253 "popq %rdi\n\t"
1254 "popq %rsi\n\t"
1255 __ASM_SEH(".seh_savexmm %xmm6, 0x60\n\t")
1256 __ASM_SEH(".seh_savexmm %xmm7, 0x70\n\t")
1257 __ASM_SEH(".seh_savexmm %xmm8, 0x80\n\t")
1258 __ASM_SEH(".seh_savexmm %xmm9, 0x90\n\t")
1259 __ASM_SEH(".seh_savexmm %xmm10, 0xa0\n\t")
1260 __ASM_SEH(".seh_savexmm %xmm11, 0xb0\n\t")
1261 __ASM_SEH(".seh_savexmm %xmm12, 0xc0\n\t")
1262 __ASM_SEH(".seh_savexmm %xmm13, 0xd0\n\t")
1263 __ASM_SEH(".seh_savexmm %xmm14, 0xe0\n\t")
1264 __ASM_SEH(".seh_savexmm %xmm15, 0xf0\n\t")
1266 /* call the callback. */
1267 "movq %r8,%rcx\n\t"
1268 "callq *%rdx\n\t"
1269 __ASM_CFI(".cfi_restore_state\n\t")
1270 "nop\n\t" /* Otherwise RtlVirtualUnwind() will think we are inside epilogue and
1271 * interpret / execute the rest of opcodes here instead of unwind through
1272 * machine frame. */
1273 "leaq 0(%rbp),%rsp\n\t"
1274 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1275 "popq %rbp\n\t"
1276 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1277 __ASM_CFI(".cfi_same_value %rbp\n\t")
1278 "ret")
1280 /*******************************************************************
1281 * RtlRestoreContext (NTDLL.@)
1283 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1285 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1287 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1289 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1290 context->Rbx = jmp->Rbx;
1291 context->Rsp = jmp->Rsp;
1292 context->Rbp = jmp->Rbp;
1293 context->Rsi = jmp->Rsi;
1294 context->Rdi = jmp->Rdi;
1295 context->R12 = jmp->R12;
1296 context->R13 = jmp->R13;
1297 context->R14 = jmp->R14;
1298 context->R15 = jmp->R15;
1299 context->Rip = jmp->Rip;
1300 context->Xmm6 = jmp->Xmm6;
1301 context->Xmm7 = jmp->Xmm7;
1302 context->Xmm8 = jmp->Xmm8;
1303 context->Xmm9 = jmp->Xmm9;
1304 context->Xmm10 = jmp->Xmm10;
1305 context->Xmm11 = jmp->Xmm11;
1306 context->Xmm12 = jmp->Xmm12;
1307 context->Xmm13 = jmp->Xmm13;
1308 context->Xmm14 = jmp->Xmm14;
1309 context->Xmm15 = jmp->Xmm15;
1310 context->MxCsr = jmp->MxCsr;
1311 context->FltSave.MxCsr = jmp->MxCsr;
1312 context->FltSave.ControlWord = jmp->FpCsr;
1314 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1316 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1317 TRACE_(seh)( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1318 context->Rip = (ULONG64)call_consolidate_callback( context, consolidate, rec );
1321 /* hack: remove no longer accessible TEB frames */
1322 while ((ULONG64)teb_frame < context->Rsp)
1324 TRACE_(seh)( "removing TEB frame: %p\n", teb_frame );
1325 teb_frame = __wine_pop_frame( teb_frame );
1328 TRACE_(seh)( "returning to %p stack %p\n", (void *)context->Rip, (void *)context->Rsp );
1329 NtContinue( context, FALSE );
1333 /*******************************************************************
1334 * RtlUnwindEx (NTDLL.@)
1336 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1337 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1339 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1340 EXCEPTION_RECORD record;
1341 DISPATCHER_CONTEXT dispatch;
1342 CONTEXT new_context;
1343 NTSTATUS status;
1344 DWORD i;
1346 RtlCaptureContext( context );
1347 new_context = *context;
1349 /* build an exception record, if we do not have one */
1350 if (!rec)
1352 record.ExceptionCode = STATUS_UNWIND;
1353 record.ExceptionFlags = 0;
1354 record.ExceptionRecord = NULL;
1355 record.ExceptionAddress = (void *)context->Rip;
1356 record.NumberParameters = 0;
1357 rec = &record;
1360 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1362 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p rip=%016I64x\n",
1363 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Rip );
1364 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1365 TRACE( " info[%ld]=%016I64x\n", i, rec->ExceptionInformation[i] );
1366 TRACE(" rax=%016I64x rbx=%016I64x rcx=%016I64x rdx=%016I64x\n",
1367 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1368 TRACE(" rsi=%016I64x rdi=%016I64x rbp=%016I64x rsp=%016I64x\n",
1369 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1370 TRACE(" r8=%016I64x r9=%016I64x r10=%016I64x r11=%016I64x\n",
1371 context->R8, context->R9, context->R10, context->R11 );
1372 TRACE(" r12=%016I64x r13=%016I64x r14=%016I64x r15=%016I64x\n",
1373 context->R12, context->R13, context->R14, context->R15 );
1375 dispatch.EstablisherFrame = context->Rsp;
1376 dispatch.TargetIp = (ULONG64)target_ip;
1377 dispatch.ContextRecord = context;
1378 dispatch.HistoryTable = table;
1380 for (;;)
1382 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1383 if (status != STATUS_SUCCESS) raise_status( status, rec );
1385 unwind_done:
1386 if (!dispatch.EstablisherFrame) break;
1388 if ((dispatch.EstablisherFrame & 7) ||
1389 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
1390 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
1392 ERR( "invalid frame %p (%p-%p)\n", (void *)dispatch.EstablisherFrame,
1393 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1394 rec->ExceptionFlags |= EH_STACK_INVALID;
1395 break;
1398 if (dispatch.LanguageHandler)
1400 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
1402 ERR( "invalid end frame %p/%p\n", (void *)dispatch.EstablisherFrame, end_frame );
1403 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1405 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1406 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1408 ULONG64 frame;
1410 new_context = *dispatch.ContextRecord;
1411 new_context.ContextFlags &= ~0x40;
1412 *context = new_context;
1413 dispatch.ContextRecord = context;
1414 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1415 dispatch.ControlPc, dispatch.FunctionEntry,
1416 &new_context, NULL, &frame, NULL );
1417 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1418 goto unwind_done;
1420 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1422 else /* hack: call builtin handlers registered in the tib list */
1424 DWORD64 backup_frame = dispatch.EstablisherFrame;
1425 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < (ULONG64)end_frame)
1427 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1428 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1429 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1431 ULONG64 frame;
1433 teb_frame = __wine_pop_frame( teb_frame );
1435 new_context = *dispatch.ContextRecord;
1436 new_context.ContextFlags &= ~0x40;
1437 *context = new_context;
1438 dispatch.ContextRecord = context;
1439 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1440 dispatch.ControlPc, dispatch.FunctionEntry,
1441 &new_context, NULL, &frame, NULL );
1442 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1443 goto unwind_done;
1445 teb_frame = __wine_pop_frame( teb_frame );
1447 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Rsp) break;
1448 dispatch.EstablisherFrame = backup_frame;
1451 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
1452 *context = new_context;
1455 context->Rax = (ULONG64)retval;
1456 context->Rip = (ULONG64)target_ip;
1457 RtlRestoreContext(context, rec);
1461 /*******************************************************************
1462 * RtlUnwind (NTDLL.@)
1464 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1466 CONTEXT context;
1467 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1471 /*******************************************************************
1472 * _local_unwind (NTDLL.@)
1474 void WINAPI _local_unwind( void *frame, void *target_ip )
1476 CONTEXT context;
1477 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1480 /*******************************************************************
1481 * __C_specific_handler (NTDLL.@)
1483 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1484 void *frame,
1485 CONTEXT *context,
1486 struct _DISPATCHER_CONTEXT *dispatch )
1488 SCOPE_TABLE *table = dispatch->HandlerData;
1489 ULONG i;
1491 TRACE_(seh)( "%p %p %p %p\n", rec, frame, context, dispatch );
1492 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1494 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1496 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1498 if (dispatch->ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1499 dispatch->ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1501 PTERMINATION_HANDLER handler;
1503 if (table->ScopeRecord[i].JumpTarget) continue;
1505 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1506 dispatch->TargetIp >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1507 dispatch->TargetIp < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1509 break;
1512 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1513 dispatch->ScopeIndex = i+1;
1515 TRACE_(seh)( "calling __finally %p frame %p\n", handler, frame );
1516 handler( TRUE, frame );
1519 return ExceptionContinueSearch;
1522 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1524 if (dispatch->ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1525 dispatch->ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1527 if (!table->ScopeRecord[i].JumpTarget) continue;
1528 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1530 EXCEPTION_POINTERS ptrs;
1531 PEXCEPTION_FILTER filter;
1533 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1534 ptrs.ExceptionRecord = rec;
1535 ptrs.ContextRecord = context;
1536 TRACE_(seh)( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1537 switch (filter( &ptrs, frame ))
1539 case EXCEPTION_EXECUTE_HANDLER:
1540 break;
1541 case EXCEPTION_CONTINUE_SEARCH:
1542 continue;
1543 case EXCEPTION_CONTINUE_EXECUTION:
1544 return ExceptionContinueExecution;
1547 TRACE( "unwinding to target %p\n", (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1548 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1549 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1552 return ExceptionContinueSearch;
1556 /***********************************************************************
1557 * RtlRaiseException (NTDLL.@)
1559 __ASM_GLOBAL_FUNC( RtlRaiseException,
1560 "sub $0x4f8,%rsp\n\t"
1561 __ASM_SEH(".seh_stackalloc 0x4f8\n\t")
1562 __ASM_SEH(".seh_endprologue\n\t")
1563 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
1564 "movq %rcx,0x500(%rsp)\n\t"
1565 "leaq 0x20(%rsp),%rcx\n\t"
1566 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
1567 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
1568 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
1569 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
1570 "movq (%rax),%rcx\n\t" /* original first parameter */
1571 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
1572 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
1573 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
1574 "movq %rax,0x10(%rcx)\n\t" /* rec->ExceptionAddress */
1575 "movl $1,%r8d\n\t"
1576 "movq %gs:(0x30),%rax\n\t" /* Teb */
1577 "movq 0x60(%rax),%rax\n\t" /* Peb */
1578 "cmpb $0,0x02(%rax)\n\t" /* BeingDebugged */
1579 "jne 1f\n\t"
1580 "call " __ASM_NAME("dispatch_exception") "\n"
1581 "1:\tcall " __ASM_NAME("NtRaiseException") "\n\t"
1582 "movq %rax,%rcx\n\t"
1583 "call " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1586 static inline ULONG hash_pointers( void **ptrs, ULONG count )
1588 /* Based on MurmurHash2, which is in the public domain */
1589 static const ULONG m = 0x5bd1e995;
1590 static const ULONG r = 24;
1591 ULONG hash = count * sizeof(void*);
1592 for (; count > 0; ptrs++, count--)
1594 ULONG_PTR data = (ULONG_PTR)*ptrs;
1595 ULONG k1 = (ULONG)(data & 0xffffffff), k2 = (ULONG)(data >> 32);
1596 k1 *= m;
1597 k1 = (k1 ^ (k1 >> r)) * m;
1598 k2 *= m;
1599 k2 = (k2 ^ (k2 >> r)) * m;
1600 hash = (((hash * m) ^ k1) * m) ^ k2;
1602 hash = (hash ^ (hash >> 13)) * m;
1603 return hash ^ (hash >> 15);
1607 /*************************************************************************
1608 * RtlCaptureStackBackTrace (NTDLL.@)
1610 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1612 UNWIND_HISTORY_TABLE table;
1613 DISPATCHER_CONTEXT dispatch;
1614 CONTEXT context;
1615 NTSTATUS status;
1616 ULONG i;
1617 USHORT num_entries = 0;
1619 TRACE( "(%lu, %lu, %p, %p)\n", skip, count, buffer, hash );
1621 RtlCaptureContext( &context );
1622 dispatch.TargetIp = 0;
1623 dispatch.ContextRecord = &context;
1624 dispatch.HistoryTable = &table;
1625 if (hash) *hash = 0;
1626 for (i = 0; i < skip + count; i++)
1628 status = virtual_unwind( UNW_FLAG_NHANDLER, &dispatch, &context );
1629 if (status != STATUS_SUCCESS) return i;
1631 if (!dispatch.EstablisherFrame) break;
1633 if ((dispatch.EstablisherFrame & 7) ||
1634 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
1635 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
1637 ERR( "invalid frame %p (%p-%p)\n", (void *)dispatch.EstablisherFrame,
1638 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1639 break;
1642 if (context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
1644 if (i >= skip) buffer[num_entries++] = (void *)context.Rip;
1646 if (hash && num_entries > 0) *hash = hash_pointers( buffer, num_entries );
1647 TRACE( "captured %hu frames\n", num_entries );
1648 return num_entries;
1652 /***********************************************************************
1653 * RtlUserThreadStart (NTDLL.@)
1655 #ifdef __ASM_SEH_SUPPORTED
1656 __ASM_GLOBAL_FUNC( RtlUserThreadStart,
1657 "subq $0x28,%rsp\n\t"
1658 ".seh_stackalloc 0x28\n\t"
1659 ".seh_endprologue\n\t"
1660 "movq %rdx,%r8\n\t"
1661 "movq %rcx,%rdx\n\t"
1662 "xorq %rcx,%rcx\n\t"
1663 "movq " __ASM_NAME( "pBaseThreadInitThunk" ) "(%rip),%r9\n\t"
1664 "call *%r9\n\t"
1665 "int3\n\t"
1666 ".seh_handler " __ASM_NAME("call_unhandled_exception_handler") ", @except" )
1667 #else
1668 void WINAPI RtlUserThreadStart( PRTL_THREAD_START_ROUTINE entry, void *arg )
1670 __TRY
1672 pBaseThreadInitThunk( 0, (LPTHREAD_START_ROUTINE)entry, arg );
1674 __EXCEPT(call_unhandled_exception_filter)
1676 NtTerminateProcess( GetCurrentProcess(), GetExceptionCode() );
1678 __ENDTRY
1680 #endif
1683 /***********************************************************************
1684 * signal_start_thread
1686 extern void CDECL DECLSPEC_NORETURN signal_start_thread( CONTEXT *ctx ) DECLSPEC_HIDDEN;
1687 __ASM_GLOBAL_FUNC( signal_start_thread,
1688 "movq %rcx,%rbx\n\t" /* context */
1689 /* clear the thread stack */
1690 "andq $~0xfff,%rcx\n\t" /* round down to page size */
1691 "leaq -0xf0000(%rcx),%rdi\n\t"
1692 "movq %rdi,%rsp\n\t"
1693 "subq %rdi,%rcx\n\t"
1694 "xorl %eax,%eax\n\t"
1695 "shrq $3,%rcx\n\t"
1696 "rep; stosq\n\t"
1697 /* switch to the initial context */
1698 "leaq -32(%rbx),%rsp\n\t"
1699 "movq %rbx,%rcx\n\t"
1700 "movl $1,%edx\n\t"
1701 "call " __ASM_NAME("NtContinue") )
1704 /******************************************************************
1705 * LdrInitializeThunk (NTDLL.@)
1707 void WINAPI LdrInitializeThunk( CONTEXT *context, ULONG_PTR unk2, ULONG_PTR unk3, ULONG_PTR unk4 )
1709 loader_init( context, (void **)&context->Rcx );
1710 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", (void *)context->Rcx, (void *)context->Rdx );
1711 signal_start_thread( context );
1715 /**********************************************************************
1716 * DbgBreakPoint (NTDLL.@)
1718 __ASM_GLOBAL_FUNC( DbgBreakPoint, "int $3; ret"
1719 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1720 "\n\tnop; nop; nop; nop; nop; nop" );
1722 /**********************************************************************
1723 * DbgUserBreakPoint (NTDLL.@)
1725 __ASM_GLOBAL_FUNC( DbgUserBreakPoint, "int $3; ret"
1726 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1727 "\n\tnop; nop; nop; nop; nop; nop" );
1729 #endif /* __x86_64__ */