dinput: Introduce new dinput_device_internal_unacquire helper.
[wine.git] / dlls / ntdll / signal_x86_64.c
blobef32eba68b7fd5f0320093948ab23c596644b261
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 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winternl.h"
32 #include "wine/exception.h"
33 #include "wine/list.h"
34 #include "ntdll_misc.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(unwind);
38 WINE_DECLARE_DEBUG_CHANNEL(seh);
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 ULONG64 Spare;
68 M128A Xmm6;
69 M128A Xmm7;
70 M128A Xmm8;
71 M128A Xmm9;
72 M128A Xmm10;
73 M128A Xmm11;
74 M128A Xmm12;
75 M128A Xmm13;
76 M128A Xmm14;
77 M128A Xmm15;
80 /***********************************************************************
81 * Definitions for Win32 unwind tables
84 union handler_data
86 RUNTIME_FUNCTION chain;
87 ULONG handler;
90 struct opcode
92 BYTE offset;
93 BYTE code : 4;
94 BYTE info : 4;
97 struct UNWIND_INFO
99 BYTE version : 3;
100 BYTE flags : 5;
101 BYTE prolog;
102 BYTE count;
103 BYTE frame_reg : 4;
104 BYTE frame_offset : 4;
105 struct opcode opcodes[1]; /* info->count entries */
106 /* followed by handler_data */
109 #define UWOP_PUSH_NONVOL 0
110 #define UWOP_ALLOC_LARGE 1
111 #define UWOP_ALLOC_SMALL 2
112 #define UWOP_SET_FPREG 3
113 #define UWOP_SAVE_NONVOL 4
114 #define UWOP_SAVE_NONVOL_FAR 5
115 #define UWOP_EPILOG 6
116 #define UWOP_SAVE_XMM128 8
117 #define UWOP_SAVE_XMM128_FAR 9
118 #define UWOP_PUSH_MACHFRAME 10
120 static void dump_unwind_info( ULONG64 base, RUNTIME_FUNCTION *function )
122 static const char * const reg_names[16] =
123 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
124 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
126 union handler_data *handler_data;
127 struct UNWIND_INFO *info;
128 unsigned int i, count;
130 TRACE( "**** func %x-%x\n", function->BeginAddress, function->EndAddress );
131 for (;;)
133 if (function->UnwindData & 1)
135 RUNTIME_FUNCTION *next = (RUNTIME_FUNCTION *)((char *)base + (function->UnwindData & ~1));
136 TRACE( "unwind info for function %p-%p chained to function %p-%p\n",
137 (char *)base + function->BeginAddress, (char *)base + function->EndAddress,
138 (char *)base + next->BeginAddress, (char *)base + next->EndAddress );
139 function = next;
140 continue;
142 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
144 TRACE( "unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
145 info, info->flags, info->prolog,
146 (char *)base + function->BeginAddress, (char *)base + function->EndAddress );
148 if (info->frame_reg)
149 TRACE( " frame register %s offset 0x%x(%%rsp)\n",
150 reg_names[info->frame_reg], info->frame_offset * 16 );
152 for (i = 0; i < info->count; i++)
154 TRACE( " 0x%x: ", info->opcodes[i].offset );
155 switch (info->opcodes[i].code)
157 case UWOP_PUSH_NONVOL:
158 TRACE( "pushq %%%s\n", reg_names[info->opcodes[i].info] );
159 break;
160 case UWOP_ALLOC_LARGE:
161 if (info->opcodes[i].info)
163 count = *(DWORD *)&info->opcodes[i+1];
164 i += 2;
166 else
168 count = *(USHORT *)&info->opcodes[i+1] * 8;
169 i++;
171 TRACE( "subq $0x%x,%%rsp\n", count );
172 break;
173 case UWOP_ALLOC_SMALL:
174 count = (info->opcodes[i].info + 1) * 8;
175 TRACE( "subq $0x%x,%%rsp\n", count );
176 break;
177 case UWOP_SET_FPREG:
178 TRACE( "leaq 0x%x(%%rsp),%s\n",
179 info->frame_offset * 16, reg_names[info->frame_reg] );
180 break;
181 case UWOP_SAVE_NONVOL:
182 count = *(USHORT *)&info->opcodes[i+1] * 8;
183 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
184 i++;
185 break;
186 case UWOP_SAVE_NONVOL_FAR:
187 count = *(DWORD *)&info->opcodes[i+1];
188 TRACE( "movq %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
189 i += 2;
190 break;
191 case UWOP_SAVE_XMM128:
192 count = *(USHORT *)&info->opcodes[i+1] * 16;
193 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
194 i++;
195 break;
196 case UWOP_SAVE_XMM128_FAR:
197 count = *(DWORD *)&info->opcodes[i+1];
198 TRACE( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
199 i += 2;
200 break;
201 case UWOP_PUSH_MACHFRAME:
202 TRACE( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
203 break;
204 case UWOP_EPILOG:
205 if (info->version == 2)
207 unsigned int offset;
208 if (info->opcodes[i].info)
209 offset = info->opcodes[i].offset;
210 else
211 offset = (info->opcodes[i+1].info << 8) + info->opcodes[i+1].offset;
212 TRACE("epilog %p-%p\n", (char *)base + function->EndAddress - offset,
213 (char *)base + function->EndAddress - offset + info->opcodes[i].offset );
214 i += 1;
215 break;
217 default:
218 FIXME( "unknown code %u\n", info->opcodes[i].code );
219 break;
223 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
224 if (info->flags & UNW_FLAG_CHAININFO)
226 TRACE( " chained to function %p-%p\n",
227 (char *)base + handler_data->chain.BeginAddress,
228 (char *)base + handler_data->chain.EndAddress );
229 function = &handler_data->chain;
230 continue;
232 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
233 TRACE( " handler %p data at %p\n",
234 (char *)base + handler_data->handler, &handler_data->handler + 1 );
235 break;
239 static void dump_scope_table( ULONG64 base, const SCOPE_TABLE *table )
241 unsigned int i;
243 TRACE( "scope table at %p\n", table );
244 for (i = 0; i < table->Count; i++)
245 TRACE( " %u: %p-%p handler %p target %p\n", i,
246 (char *)base + table->ScopeRecord[i].BeginAddress,
247 (char *)base + table->ScopeRecord[i].EndAddress,
248 (char *)base + table->ScopeRecord[i].HandlerAddress,
249 (char *)base + table->ScopeRecord[i].JumpTarget );
253 /***********************************************************************
254 * virtual_unwind
256 static NTSTATUS virtual_unwind( ULONG type, DISPATCHER_CONTEXT *dispatch, CONTEXT *context )
258 LDR_DATA_TABLE_ENTRY *module;
259 NTSTATUS status;
261 dispatch->ImageBase = 0;
262 dispatch->ScopeIndex = 0;
263 dispatch->ControlPc = context->Rip;
265 /* first look for PE exception information */
267 if ((dispatch->FunctionEntry = lookup_function_info( context->Rip, &dispatch->ImageBase, &module )))
269 dispatch->LanguageHandler = RtlVirtualUnwind( type, dispatch->ImageBase, context->Rip,
270 dispatch->FunctionEntry, context,
271 &dispatch->HandlerData, &dispatch->EstablisherFrame,
272 NULL );
273 return STATUS_SUCCESS;
276 /* then look for host system exception information */
278 if (!module || (module->Flags & LDR_WINE_INTERNAL))
280 status = unix_funcs->unwind_builtin_dll( type, dispatch, context );
282 if (!status && dispatch->LanguageHandler && !module)
284 FIXME( "calling personality routine in system library not supported yet\n" );
285 dispatch->LanguageHandler = NULL;
287 if (status != STATUS_UNSUCCESSFUL) return status;
289 else WARN( "exception data not found in %s\n", debugstr_w(module->BaseDllName.Buffer) );
291 /* no exception information, treat as a leaf function */
293 dispatch->EstablisherFrame = context->Rsp;
294 dispatch->LanguageHandler = NULL;
295 context->Rip = *(ULONG64 *)context->Rsp;
296 context->Rsp = context->Rsp + sizeof(ULONG64);
297 return STATUS_SUCCESS;
301 /**************************************************************************
302 * __chkstk (NTDLL.@)
304 * Supposed to touch all the stack pages, but we shouldn't need that.
306 __ASM_GLOBAL_FUNC( __chkstk, "ret" );
309 /***********************************************************************
310 * RtlCaptureContext (NTDLL.@)
312 __ASM_GLOBAL_FUNC( RtlCaptureContext,
313 "pushfq\n\t"
314 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
315 "movl $0x10000f,0x30(%rcx)\n\t" /* context->ContextFlags */
316 "stmxcsr 0x34(%rcx)\n\t" /* context->MxCsr */
317 "movw %cs,0x38(%rcx)\n\t" /* context->SegCs */
318 "movw %ds,0x3a(%rcx)\n\t" /* context->SegDs */
319 "movw %es,0x3c(%rcx)\n\t" /* context->SegEs */
320 "movw %fs,0x3e(%rcx)\n\t" /* context->SegFs */
321 "movw %gs,0x40(%rcx)\n\t" /* context->SegGs */
322 "movw %ss,0x42(%rcx)\n\t" /* context->SegSs */
323 "popq 0x44(%rcx)\n\t" /* context->Eflags */
324 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
325 "movq %rax,0x78(%rcx)\n\t" /* context->Rax */
326 "movq %rcx,0x80(%rcx)\n\t" /* context->Rcx */
327 "movq %rdx,0x88(%rcx)\n\t" /* context->Rdx */
328 "movq %rbx,0x90(%rcx)\n\t" /* context->Rbx */
329 "leaq 8(%rsp),%rax\n\t"
330 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
331 "movq %rbp,0xa0(%rcx)\n\t" /* context->Rbp */
332 "movq %rsi,0xa8(%rcx)\n\t" /* context->Rsi */
333 "movq %rdi,0xb0(%rcx)\n\t" /* context->Rdi */
334 "movq %r8,0xb8(%rcx)\n\t" /* context->R8 */
335 "movq %r9,0xc0(%rcx)\n\t" /* context->R9 */
336 "movq %r10,0xc8(%rcx)\n\t" /* context->R10 */
337 "movq %r11,0xd0(%rcx)\n\t" /* context->R11 */
338 "movq %r12,0xd8(%rcx)\n\t" /* context->R12 */
339 "movq %r13,0xe0(%rcx)\n\t" /* context->R13 */
340 "movq %r14,0xe8(%rcx)\n\t" /* context->R14 */
341 "movq %r15,0xf0(%rcx)\n\t" /* context->R15 */
342 "movq (%rsp),%rax\n\t"
343 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
344 "fxsave 0x100(%rcx)\n\t" /* context->FltSave */
345 "ret" );
347 static DWORD __cdecl nested_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
348 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
350 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
351 rec->ExceptionFlags |= EH_NESTED_CALL;
353 return ExceptionContinueSearch;
356 /**********************************************************************
357 * call_handler
359 * Call a single exception handler.
360 * FIXME: Handle nested exceptions.
362 static DWORD call_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
364 EXCEPTION_REGISTRATION_RECORD frame;
365 DWORD res;
367 frame.Handler = nested_exception_handler;
368 __wine_push_frame( &frame );
370 TRACE_(seh)( "calling handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
371 dispatch->LanguageHandler, rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
372 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, context, dispatch );
373 TRACE_(seh)( "handler at %p returned %u\n", dispatch->LanguageHandler, res );
375 rec->ExceptionFlags &= EH_NONCONTINUABLE;
376 __wine_pop_frame( &frame );
377 return res;
381 /**********************************************************************
382 * call_teb_handler
384 * Call a single exception handler from the TEB chain.
385 * FIXME: Handle nested exceptions.
387 static DWORD call_teb_handler( EXCEPTION_RECORD *rec, CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
388 EXCEPTION_REGISTRATION_RECORD *teb_frame )
390 DWORD res;
392 TRACE_(seh)( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
393 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
394 res = teb_frame->Handler( rec, teb_frame, context, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
395 TRACE_(seh)( "handler at %p returned %u\n", teb_frame->Handler, res );
396 return res;
400 /**********************************************************************
401 * call_stack_handlers
403 * Call the stack handlers chain.
405 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *orig_context )
407 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
408 UNWIND_HISTORY_TABLE table;
409 DISPATCHER_CONTEXT dispatch;
410 CONTEXT context;
411 NTSTATUS status;
413 context = *orig_context;
414 context.ContextFlags &= ~0x40; /* Clear xstate flag. */
416 dispatch.TargetIp = 0;
417 dispatch.ContextRecord = &context;
418 dispatch.HistoryTable = &table;
419 for (;;)
421 status = virtual_unwind( UNW_FLAG_EHANDLER, &dispatch, &context );
422 if (status != STATUS_SUCCESS) return status;
424 unwind_done:
425 if (!dispatch.EstablisherFrame) break;
427 if ((dispatch.EstablisherFrame & 7) ||
428 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
429 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
431 ERR_(seh)( "invalid frame %p (%p-%p)\n", (void *)dispatch.EstablisherFrame,
432 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
433 rec->ExceptionFlags |= EH_STACK_INVALID;
434 break;
437 if (dispatch.LanguageHandler)
439 switch (call_handler( rec, orig_context, &dispatch ))
441 case ExceptionContinueExecution:
442 if (rec->ExceptionFlags & EH_NONCONTINUABLE) return STATUS_NONCONTINUABLE_EXCEPTION;
443 return STATUS_SUCCESS;
444 case ExceptionContinueSearch:
445 break;
446 case ExceptionNestedException:
447 FIXME_(seh)( "nested exception\n" );
448 break;
449 case ExceptionCollidedUnwind: {
450 ULONG64 frame;
452 context = *dispatch.ContextRecord;
453 dispatch.ContextRecord = &context;
454 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
455 dispatch.ControlPc, dispatch.FunctionEntry,
456 &context, NULL, &frame, NULL );
457 goto unwind_done;
459 default:
460 return STATUS_INVALID_DISPOSITION;
463 /* hack: call wine handlers registered in the tib list */
464 else while ((ULONG64)teb_frame < context.Rsp)
466 TRACE_(seh)( "found wine frame %p rsp %p handler %p\n",
467 teb_frame, (void *)context.Rsp, teb_frame->Handler );
468 dispatch.EstablisherFrame = (ULONG64)teb_frame;
469 switch (call_teb_handler( rec, orig_context, &dispatch, teb_frame ))
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 FIXME_(seh)( "nested exception\n" );
478 break;
479 case ExceptionCollidedUnwind: {
480 ULONG64 frame;
482 context = *dispatch.ContextRecord;
483 dispatch.ContextRecord = &context;
484 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
485 dispatch.ControlPc, dispatch.FunctionEntry,
486 &context, NULL, &frame, NULL );
487 teb_frame = teb_frame->Prev;
488 goto unwind_done;
490 default:
491 return STATUS_INVALID_DISPOSITION;
493 teb_frame = teb_frame->Prev;
496 if (context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
498 return STATUS_UNHANDLED_EXCEPTION;
502 NTSTATUS WINAPI dispatch_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
504 NTSTATUS status;
505 DWORD c;
507 TRACE_(seh)( "code=%x flags=%x addr=%p ip=%p tid=%04x\n",
508 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
509 (void *)context->Rip, GetCurrentThreadId() );
510 for (c = 0; c < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); c++)
511 TRACE( " info[%d]=%016I64x\n", c, rec->ExceptionInformation[c] );
513 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
515 if (rec->ExceptionInformation[1] >> 16)
516 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
517 rec->ExceptionAddress,
518 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
519 else
520 MESSAGE( "wine: Call from %p to unimplemented function %s.%I64d, aborting\n",
521 rec->ExceptionAddress,
522 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
524 else if (rec->ExceptionCode == EXCEPTION_WINE_NAME_THREAD && rec->ExceptionInformation[0] == 0x1000)
526 WARN_(seh)( "Thread %04x renamed to %s\n", (DWORD)rec->ExceptionInformation[2],
527 debugstr_a((char *)rec->ExceptionInformation[1]) );
529 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_C)
531 WARN_(seh)( "%s\n", debugstr_an((char *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
533 else if (rec->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
535 WARN_(seh)( "%s\n", debugstr_wn((WCHAR *)rec->ExceptionInformation[1], rec->ExceptionInformation[0] - 1) );
537 else
539 if (rec->ExceptionCode == STATUS_ASSERTION_FAILURE)
540 ERR_(seh)( "%s exception (code=%x) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
541 else
542 WARN_(seh)( "%s exception (code=%x) raised\n", debugstr_exception_code(rec->ExceptionCode), rec->ExceptionCode );
544 TRACE_(seh)( " rax=%016I64x rbx=%016I64x rcx=%016I64x rdx=%016I64x\n",
545 context->Rax, context->Rbx, context->Rcx, context->Rdx );
546 TRACE_(seh)( " rsi=%016I64x rdi=%016I64x rbp=%016I64x rsp=%016I64x\n",
547 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
548 TRACE_(seh)( " r8=%016I64x r9=%016I64x r10=%016I64x r11=%016I64x\n",
549 context->R8, context->R9, context->R10, context->R11 );
550 TRACE_(seh)( " r12=%016I64x r13=%016I64x r14=%016I64x r15=%016I64x\n",
551 context->R12, context->R13, context->R14, context->R15 );
554 /* Legends of Runeterra depends on having SegDs == SegSs in an exception
555 * handler. */
556 context->SegDs = context->SegSs;
558 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
559 NtContinue( context, FALSE );
561 if ((status = call_stack_handlers( rec, context )) == STATUS_SUCCESS)
562 NtContinue( context, FALSE );
564 if (status != STATUS_UNHANDLED_EXCEPTION) RtlRaiseStatus( status );
565 return NtRaiseException( rec, context, FALSE );
569 /*******************************************************************
570 * KiUserExceptionDispatcher (NTDLL.@)
572 __ASM_GLOBAL_FUNC( KiUserExceptionDispatcher,
573 "mov 0x98(%rsp),%rcx\n\t" /* context->Rsp */
574 "mov 0xf8(%rsp),%rdx\n\t" /* context->Rip */
575 "mov %rdx,-0x8(%rcx)\n\t"
576 "mov %rbp,-0x10(%rcx)\n\t"
577 "mov %rdi,-0x18(%rcx)\n\t"
578 "mov %rsi,-0x20(%rcx)\n\t"
579 "lea -0x20(%rcx),%rbp\n\t"
580 "mov %rsp,%rdx\n\t" /* context */
581 "lea 0x4f0(%rsp),%rcx\n\t" /* rec */
582 __ASM_SEH(".seh_pushreg %rbp\n\t")
583 __ASM_SEH(".seh_pushreg %rdi\n\t")
584 __ASM_SEH(".seh_pushreg %rsi\n\t")
585 __ASM_SEH(".seh_setframe %rbp,0\n\t")
586 __ASM_SEH(".seh_endprologue\n\t")
588 __ASM_CFI(".cfi_signal_frame\n\t")
589 __ASM_CFI(".cfi_adjust_cfa_offset 0x20\n\t")
590 __ASM_CFI(".cfi_def_cfa %rbp,0x20\n\t")
591 __ASM_CFI(".cfi_rel_offset %rip,0x18\n\t")
592 __ASM_CFI(".cfi_rel_offset %rbp,0x10\n\t")
593 __ASM_CFI(".cfi_rel_offset %rdi,0x8\n\t")
594 __ASM_CFI(".cfi_rel_offset %rsi,0\n\t")
595 "call " __ASM_NAME("dispatch_exception") "\n\t"
596 "int3")
599 /*******************************************************************
600 * KiUserApcDispatcher (NTDLL.@)
602 void WINAPI dispatch_apc( CONTEXT *context, ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3,
603 void (CALLBACK *func)(ULONG_PTR,ULONG_PTR,ULONG_PTR,CONTEXT*) )
605 func( arg1, arg2, arg3, context );
606 NtContinue( context, TRUE );
609 __ASM_GLOBAL_FUNC( KiUserApcDispatcher,
610 "addq $0x8,%rsp\n\t"
611 "mov 0x98(%rcx),%r10\n\t" /* context->Rsp */
612 "mov 0xf8(%rcx),%r11\n\t" /* context->Rip */
613 "mov %r11,-0x8(%r10)\n\t"
614 "mov %rbp,-0x10(%r10)\n\t"
615 "lea -0x10(%r10),%rbp\n\t"
616 __ASM_SEH(".seh_pushreg %rbp\n\t")
617 __ASM_SEH(".seh_setframe %rbp,0\n\t")
618 __ASM_SEH(".seh_endprologue\n\t")
619 __ASM_CFI(".cfi_signal_frame\n\t")
620 __ASM_CFI(".cfi_adjust_cfa_offset 0x10\n\t")
621 __ASM_CFI(".cfi_def_cfa %rbp,0x10\n\t")
622 __ASM_CFI(".cfi_rel_offset %rip,0x8\n\t")
623 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
624 "call " __ASM_NAME("dispatch_apc") "\n\t"
625 "int3")
628 /*******************************************************************
629 * KiUserCallbackDispatcher (NTDLL.@)
631 * FIXME: not binary compatible
633 void WINAPI KiUserCallbackDispatcher( ULONG id, void *args, ULONG len )
635 NTSTATUS (WINAPI *func)(void *, ULONG) = ((void **)NtCurrentTeb()->Peb->KernelCallbackTable)[id];
637 RtlRaiseStatus( NtCallbackReturn( NULL, 0, func( args, len )));
641 static ULONG64 get_int_reg( CONTEXT *context, int reg )
643 return *(&context->Rax + reg);
646 static void set_int_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, ULONG64 *val )
648 *(&context->Rax + reg) = *val;
649 if (ctx_ptr) ctx_ptr->u2.IntegerContext[reg] = val;
652 static void set_float_reg( CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr, int reg, M128A *val )
654 /* Use a memcpy() to avoid issues if val is misaligned. */
655 memcpy(&context->u.s.Xmm0 + reg, val, sizeof(*val));
656 if (ctx_ptr) ctx_ptr->u.FloatingContext[reg] = val;
659 static int get_opcode_size( struct opcode op )
661 switch (op.code)
663 case UWOP_ALLOC_LARGE:
664 return 2 + (op.info != 0);
665 case UWOP_SAVE_NONVOL:
666 case UWOP_SAVE_XMM128:
667 case UWOP_EPILOG:
668 return 2;
669 case UWOP_SAVE_NONVOL_FAR:
670 case UWOP_SAVE_XMM128_FAR:
671 return 3;
672 default:
673 return 1;
677 static BOOL is_inside_epilog( BYTE *pc, ULONG64 base, const RUNTIME_FUNCTION *function )
679 /* add or lea must be the first instruction, and it must have a rex.W prefix */
680 if ((pc[0] & 0xf8) == 0x48)
682 switch (pc[1])
684 case 0x81: /* add $nnnn,%rsp */
685 if (pc[0] == 0x48 && pc[2] == 0xc4)
687 pc += 7;
688 break;
690 return FALSE;
691 case 0x83: /* add $n,%rsp */
692 if (pc[0] == 0x48 && pc[2] == 0xc4)
694 pc += 4;
695 break;
697 return FALSE;
698 case 0x8d: /* lea n(reg),%rsp */
699 if (pc[0] & 0x06) return FALSE; /* rex.RX must be cleared */
700 if (((pc[2] >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
701 if ((pc[2] & 7) == 4) return FALSE; /* no SIB byte allowed */
702 if ((pc[2] >> 6) == 1) /* 8-bit offset */
704 pc += 4;
705 break;
707 if ((pc[2] >> 6) == 2) /* 32-bit offset */
709 pc += 7;
710 break;
712 return FALSE;
716 /* now check for various pop instructions */
718 for (;;)
720 if ((*pc & 0xf0) == 0x40) pc++; /* rex prefix */
722 switch (*pc)
724 case 0x58: /* pop %rax/%r8 */
725 case 0x59: /* pop %rcx/%r9 */
726 case 0x5a: /* pop %rdx/%r10 */
727 case 0x5b: /* pop %rbx/%r11 */
728 case 0x5c: /* pop %rsp/%r12 */
729 case 0x5d: /* pop %rbp/%r13 */
730 case 0x5e: /* pop %rsi/%r14 */
731 case 0x5f: /* pop %rdi/%r15 */
732 pc++;
733 continue;
734 case 0xc2: /* ret $nn */
735 case 0xc3: /* ret */
736 return TRUE;
737 case 0xe9: /* jmp nnnn */
738 pc += 5 + *(LONG *)(pc + 1);
739 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
740 continue;
741 break;
742 case 0xeb: /* jmp n */
743 pc += 2 + (signed char)pc[1];
744 if (pc - (BYTE *)base >= function->BeginAddress && pc - (BYTE *)base < function->EndAddress)
745 continue;
746 break;
747 case 0xf3: /* rep; ret (for amd64 prediction bug) */
748 return pc[1] == 0xc3;
750 return FALSE;
754 /* execute a function epilog, which must have been validated with is_inside_epilog() */
755 static void interpret_epilog( BYTE *pc, CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
757 for (;;)
759 BYTE rex = 0;
761 if ((*pc & 0xf0) == 0x40) rex = *pc++ & 0x0f; /* rex prefix */
763 switch (*pc)
765 case 0x58: /* pop %rax/r8 */
766 case 0x59: /* pop %rcx/r9 */
767 case 0x5a: /* pop %rdx/r10 */
768 case 0x5b: /* pop %rbx/r11 */
769 case 0x5c: /* pop %rsp/r12 */
770 case 0x5d: /* pop %rbp/r13 */
771 case 0x5e: /* pop %rsi/r14 */
772 case 0x5f: /* pop %rdi/r15 */
773 set_int_reg( context, ctx_ptr, *pc - 0x58 + (rex & 1) * 8, (ULONG64 *)context->Rsp );
774 context->Rsp += sizeof(ULONG64);
775 pc++;
776 continue;
777 case 0x81: /* add $nnnn,%rsp */
778 context->Rsp += *(LONG *)(pc + 2);
779 pc += 2 + sizeof(LONG);
780 continue;
781 case 0x83: /* add $n,%rsp */
782 context->Rsp += (signed char)pc[2];
783 pc += 3;
784 continue;
785 case 0x8d:
786 if ((pc[1] >> 6) == 1) /* lea n(reg),%rsp */
788 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + (signed char)pc[2];
789 pc += 3;
791 else /* lea nnnn(reg),%rsp */
793 context->Rsp = get_int_reg( context, (pc[1] & 7) + (rex & 1) * 8 ) + *(LONG *)(pc + 2);
794 pc += 2 + sizeof(LONG);
796 continue;
797 case 0xc2: /* ret $nn */
798 context->Rip = *(ULONG64 *)context->Rsp;
799 context->Rsp += sizeof(ULONG64) + *(WORD *)(pc + 1);
800 return;
801 case 0xc3: /* ret */
802 case 0xf3: /* rep; ret */
803 context->Rip = *(ULONG64 *)context->Rsp;
804 context->Rsp += sizeof(ULONG64);
805 return;
806 case 0xe9: /* jmp nnnn */
807 pc += 5 + *(LONG *)(pc + 1);
808 continue;
809 case 0xeb: /* jmp n */
810 pc += 2 + (signed char)pc[1];
811 continue;
813 return;
817 /**********************************************************************
818 * RtlVirtualUnwind (NTDLL.@)
820 PVOID WINAPI RtlVirtualUnwind( ULONG type, ULONG64 base, ULONG64 pc,
821 RUNTIME_FUNCTION *function, CONTEXT *context,
822 PVOID *data, ULONG64 *frame_ret,
823 KNONVOLATILE_CONTEXT_POINTERS *ctx_ptr )
825 union handler_data *handler_data;
826 ULONG64 frame, off;
827 struct UNWIND_INFO *info;
828 unsigned int i, prolog_offset;
829 BOOL mach_frame = FALSE;
831 TRACE( "type %x rip %p rsp %p\n", type, (void *)pc, (void *)context->Rsp );
832 if (TRACE_ON(seh)) dump_unwind_info( base, function );
834 frame = *frame_ret = context->Rsp;
835 for (;;)
837 info = (struct UNWIND_INFO *)((char *)base + function->UnwindData);
838 handler_data = (union handler_data *)&info->opcodes[(info->count + 1) & ~1];
840 if (info->version != 1 && info->version != 2)
842 FIXME( "unknown unwind info version %u at %p\n", info->version, info );
843 return NULL;
846 if (info->frame_reg)
847 frame = get_int_reg( context, info->frame_reg ) - info->frame_offset * 16;
849 /* check if in prolog */
850 if (pc >= base + function->BeginAddress && pc < base + function->BeginAddress + info->prolog)
852 TRACE("inside prolog.\n");
853 prolog_offset = pc - base - function->BeginAddress;
855 else
857 prolog_offset = ~0;
858 /* Since Win10 1809 epilogue does not have a special treatment in case of zero opcode count. */
859 if (info->count && is_inside_epilog( (BYTE *)pc, base, function ))
861 TRACE("inside epilog.\n");
862 interpret_epilog( (BYTE *)pc, context, ctx_ptr );
863 *frame_ret = frame;
864 return NULL;
868 for (i = 0; i < info->count; i += get_opcode_size(info->opcodes[i]))
870 if (prolog_offset < info->opcodes[i].offset) continue; /* skip it */
872 switch (info->opcodes[i].code)
874 case UWOP_PUSH_NONVOL: /* pushq %reg */
875 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)context->Rsp );
876 context->Rsp += sizeof(ULONG64);
877 break;
878 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
879 if (info->opcodes[i].info) context->Rsp += *(DWORD *)&info->opcodes[i+1];
880 else context->Rsp += *(USHORT *)&info->opcodes[i+1] * 8;
881 break;
882 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
883 context->Rsp += (info->opcodes[i].info + 1) * 8;
884 break;
885 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
886 context->Rsp = *frame_ret = frame;
887 break;
888 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
889 off = frame + *(USHORT *)&info->opcodes[i+1] * 8;
890 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)off );
891 break;
892 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
893 off = frame + *(DWORD *)&info->opcodes[i+1];
894 set_int_reg( context, ctx_ptr, info->opcodes[i].info, (ULONG64 *)off );
895 break;
896 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
897 off = frame + *(USHORT *)&info->opcodes[i+1] * 16;
898 set_float_reg( context, ctx_ptr, info->opcodes[i].info, (M128A *)off );
899 break;
900 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
901 off = frame + *(DWORD *)&info->opcodes[i+1];
902 set_float_reg( context, ctx_ptr, info->opcodes[i].info, (M128A *)off );
903 break;
904 case UWOP_PUSH_MACHFRAME:
905 if (info->flags & UNW_FLAG_CHAININFO)
907 FIXME("PUSH_MACHFRAME with chained unwind info.\n");
908 break;
910 if (i + get_opcode_size(info->opcodes[i]) < info->count )
912 FIXME("PUSH_MACHFRAME is not the last opcode.\n");
913 break;
916 if (info->opcodes[i].info)
917 context->Rsp += 0x8;
919 context->Rip = *(ULONG64 *)context->Rsp;
920 context->Rsp = *(ULONG64 *)(context->Rsp + 24);
921 mach_frame = TRUE;
922 break;
923 case UWOP_EPILOG:
924 if (info->version == 2)
925 break; /* nothing to do */
926 default:
927 FIXME( "unknown code %u\n", info->opcodes[i].code );
928 break;
932 if (!(info->flags & UNW_FLAG_CHAININFO)) break;
933 function = &handler_data->chain; /* restart with the chained info */
936 if (!mach_frame)
938 /* now pop return address */
939 context->Rip = *(ULONG64 *)context->Rsp;
940 context->Rsp += sizeof(ULONG64);
943 if (!(info->flags & type)) return NULL; /* no matching handler */
944 if (prolog_offset != ~0) return NULL; /* inside prolog */
946 *data = &handler_data->handler + 1;
947 return (char *)base + handler_data->handler;
950 struct unwind_exception_frame
952 EXCEPTION_REGISTRATION_RECORD frame;
953 DISPATCHER_CONTEXT *dispatch;
956 /**********************************************************************
957 * unwind_exception_handler
959 * Handler for exceptions happening while calling an unwind handler.
961 static DWORD __cdecl unwind_exception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
962 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
964 struct unwind_exception_frame *unwind_frame = (struct unwind_exception_frame *)frame;
965 DISPATCHER_CONTEXT *dispatch = (DISPATCHER_CONTEXT *)dispatcher;
967 /* copy the original dispatcher into the current one, except for the TargetIp */
968 dispatch->ControlPc = unwind_frame->dispatch->ControlPc;
969 dispatch->ImageBase = unwind_frame->dispatch->ImageBase;
970 dispatch->FunctionEntry = unwind_frame->dispatch->FunctionEntry;
971 dispatch->EstablisherFrame = unwind_frame->dispatch->EstablisherFrame;
972 dispatch->ContextRecord = unwind_frame->dispatch->ContextRecord;
973 dispatch->LanguageHandler = unwind_frame->dispatch->LanguageHandler;
974 dispatch->HandlerData = unwind_frame->dispatch->HandlerData;
975 dispatch->HistoryTable = unwind_frame->dispatch->HistoryTable;
976 dispatch->ScopeIndex = unwind_frame->dispatch->ScopeIndex;
977 TRACE( "detected collided unwind\n" );
978 return ExceptionCollidedUnwind;
981 /**********************************************************************
982 * call_unwind_handler
984 * Call a single unwind handler.
986 static DWORD call_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch )
988 struct unwind_exception_frame frame;
989 DWORD res;
991 frame.frame.Handler = unwind_exception_handler;
992 frame.dispatch = dispatch;
993 __wine_push_frame( &frame.frame );
995 TRACE( "calling handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
996 dispatch->LanguageHandler, rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
997 res = dispatch->LanguageHandler( rec, (void *)dispatch->EstablisherFrame, dispatch->ContextRecord, dispatch );
998 TRACE( "handler %p returned %x\n", dispatch->LanguageHandler, res );
1000 __wine_pop_frame( &frame.frame );
1002 switch (res)
1004 case ExceptionContinueSearch:
1005 case ExceptionCollidedUnwind:
1006 break;
1007 default:
1008 raise_status( STATUS_INVALID_DISPOSITION, rec );
1009 break;
1012 return res;
1016 /**********************************************************************
1017 * call_teb_unwind_handler
1019 * Call a single unwind handler from the TEB chain.
1021 static DWORD call_teb_unwind_handler( EXCEPTION_RECORD *rec, DISPATCHER_CONTEXT *dispatch,
1022 EXCEPTION_REGISTRATION_RECORD *teb_frame )
1024 DWORD res;
1026 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
1027 teb_frame->Handler, rec, teb_frame, dispatch->ContextRecord, dispatch );
1028 res = teb_frame->Handler( rec, teb_frame, dispatch->ContextRecord, (EXCEPTION_REGISTRATION_RECORD**)dispatch );
1029 TRACE( "handler at %p returned %u\n", teb_frame->Handler, res );
1031 switch (res)
1033 case ExceptionContinueSearch:
1034 case ExceptionCollidedUnwind:
1035 break;
1036 default:
1037 raise_status( STATUS_INVALID_DISPOSITION, rec );
1038 break;
1041 return res;
1045 /**********************************************************************
1046 * call_consolidate_callback
1048 * Wrapper function to call a consolidate callback from a fake frame.
1049 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1050 * we have to skip all frames which were already processed. To do that we
1051 * trick the unwinding functions into thinking the call came from the specified
1052 * context. All CFI instructions are either DW_CFA_def_cfa_expression or
1053 * DW_CFA_expression, and the expressions have the following format:
1055 * DW_OP_breg6; sleb128 0x10 | Load %rbp + 0x10
1056 * DW_OP_deref | Get *(%rbp + 0x10) == context
1057 * DW_OP_plus_uconst; uleb128 <OFFSET> | Add offset to get struct member
1058 * [DW_OP_deref] | Dereference, only for CFA
1060 extern void * WINAPI call_consolidate_callback( CONTEXT *context,
1061 void *(CALLBACK *callback)(EXCEPTION_RECORD *),
1062 EXCEPTION_RECORD *rec );
1063 __ASM_GLOBAL_FUNC( call_consolidate_callback,
1064 "pushq %rbp\n\t"
1065 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1066 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1067 "movq %rsp,%rbp\n\t"
1068 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1070 /* Setup SEH machine frame. */
1071 "subq $0x28,%rsp\n\t"
1072 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
1073 "movq 0xf8(%rcx),%rax\n\t" /* Context->Rip */
1074 "movq %rax,(%rsp)\n\t"
1075 "movq 0x98(%rcx),%rax\n\t" /* context->Rsp */
1076 "movq %rax,0x18(%rsp)\n\t"
1077 __ASM_SEH(".seh_pushframe\n\t")
1078 __ASM_SEH(".seh_endprologue\n\t")
1080 "subq $0x108,%rsp\n\t" /* 10*16 (float regs) + 8*8 (int regs) + 32 (shadow store) + 8 (align). */
1081 __ASM_SEH(".seh_stackalloc 0x108\n\t")
1082 __ASM_CFI(".cfi_adjust_cfa_offset 0x108\n\t")
1084 /* Setup CFI unwind to context. */
1085 "movq %rcx,0x10(%rbp)\n\t"
1086 __ASM_CFI(".cfi_remember_state\n\t")
1087 __ASM_CFI(".cfi_escape 0x0f,0x07,0x76,0x10,0x06,0x23,0x98,0x01,0x06\n\t") /* CFA */
1088 __ASM_CFI(".cfi_escape 0x10,0x03,0x06,0x76,0x10,0x06,0x23,0x90,0x01\n\t") /* %rbx */
1089 __ASM_CFI(".cfi_escape 0x10,0x04,0x06,0x76,0x10,0x06,0x23,0xa8,0x01\n\t") /* %rsi */
1090 __ASM_CFI(".cfi_escape 0x10,0x05,0x06,0x76,0x10,0x06,0x23,0xb0,0x01\n\t") /* %rdi */
1091 __ASM_CFI(".cfi_escape 0x10,0x06,0x06,0x76,0x10,0x06,0x23,0xa0,0x01\n\t") /* %rbp */
1092 __ASM_CFI(".cfi_escape 0x10,0x0c,0x06,0x76,0x10,0x06,0x23,0xd8,0x01\n\t") /* %r12 */
1093 __ASM_CFI(".cfi_escape 0x10,0x0d,0x06,0x76,0x10,0x06,0x23,0xe0,0x01\n\t") /* %r13 */
1094 __ASM_CFI(".cfi_escape 0x10,0x0e,0x06,0x76,0x10,0x06,0x23,0xe8,0x01\n\t") /* %r14 */
1095 __ASM_CFI(".cfi_escape 0x10,0x0f,0x06,0x76,0x10,0x06,0x23,0xf0,0x01\n\t") /* %r15 */
1096 __ASM_CFI(".cfi_escape 0x10,0x10,0x06,0x76,0x10,0x06,0x23,0xf8,0x01\n\t") /* %rip */
1097 __ASM_CFI(".cfi_escape 0x10,0x17,0x06,0x76,0x10,0x06,0x23,0x80,0x04\n\t") /* %xmm6 */
1098 __ASM_CFI(".cfi_escape 0x10,0x18,0x06,0x76,0x10,0x06,0x23,0x90,0x04\n\t") /* %xmm7 */
1099 __ASM_CFI(".cfi_escape 0x10,0x19,0x06,0x76,0x10,0x06,0x23,0xa0,0x04\n\t") /* %xmm8 */
1100 __ASM_CFI(".cfi_escape 0x10,0x1a,0x06,0x76,0x10,0x06,0x23,0xb0,0x04\n\t") /* %xmm9 */
1101 __ASM_CFI(".cfi_escape 0x10,0x1b,0x06,0x76,0x10,0x06,0x23,0xc0,0x04\n\t") /* %xmm10 */
1102 __ASM_CFI(".cfi_escape 0x10,0x1c,0x06,0x76,0x10,0x06,0x23,0xd0,0x04\n\t") /* %xmm11 */
1103 __ASM_CFI(".cfi_escape 0x10,0x1d,0x06,0x76,0x10,0x06,0x23,0xe0,0x04\n\t") /* %xmm12 */
1104 __ASM_CFI(".cfi_escape 0x10,0x1e,0x06,0x76,0x10,0x06,0x23,0xf0,0x04\n\t") /* %xmm13 */
1105 __ASM_CFI(".cfi_escape 0x10,0x1f,0x06,0x76,0x10,0x06,0x23,0x80,0x05\n\t") /* %xmm14 */
1106 __ASM_CFI(".cfi_escape 0x10,0x20,0x06,0x76,0x10,0x06,0x23,0x90,0x05\n\t") /* %xmm15 */
1108 /* Setup SEH unwind registers restore. */
1109 "movq 0xa0(%rcx),%rax\n\t" /* context->Rbp */
1110 "movq %rax,0x100(%rsp)\n\t"
1111 __ASM_SEH(".seh_savereg %rbp, 0x100\n\t")
1112 "movq 0x90(%rcx),%rax\n\t" /* context->Rbx */
1113 "movq %rax,0x20(%rsp)\n\t"
1114 __ASM_SEH(".seh_savereg %rbx, 0x20\n\t")
1115 "movq 0xa8(%rcx),%rax\n\t" /* context->Rsi */
1116 "movq %rax,0x28(%rsp)\n\t"
1117 __ASM_SEH(".seh_savereg %rsi, 0x28\n\t")
1118 "movq 0xb0(%rcx),%rax\n\t" /* context->Rdi */
1119 "movq %rax,0x30(%rsp)\n\t"
1120 __ASM_SEH(".seh_savereg %rdi, 0x30\n\t")
1122 "movq 0xd8(%rcx),%rax\n\t" /* context->R12 */
1123 "movq %rax,0x38(%rsp)\n\t"
1124 __ASM_SEH(".seh_savereg %r12, 0x38\n\t")
1125 "movq 0xe0(%rcx),%rax\n\t" /* context->R13 */
1126 "movq %rax,0x40(%rsp)\n\t"
1127 __ASM_SEH(".seh_savereg %r13, 0x40\n\t")
1128 "movq 0xe8(%rcx),%rax\n\t" /* context->R14 */
1129 "movq %rax,0x48(%rsp)\n\t"
1130 __ASM_SEH(".seh_savereg %r14, 0x48\n\t")
1131 "movq 0xf0(%rcx),%rax\n\t" /* context->R15 */
1132 "movq %rax,0x50(%rsp)\n\t"
1133 __ASM_SEH(".seh_savereg %r15, 0x50\n\t")
1134 "pushq %rsi\n\t"
1135 "pushq %rdi\n\t"
1136 "leaq 0x200(%rcx),%rsi\n\t"
1137 "leaq 0x70(%rsp),%rdi\n\t"
1138 "movq $0x14,%rcx\n\t"
1139 "cld\n\t"
1140 "rep; movsq\n\t"
1141 "popq %rdi\n\t"
1142 "popq %rsi\n\t"
1143 __ASM_SEH(".seh_savexmm %xmm6, 0x60\n\t")
1144 __ASM_SEH(".seh_savexmm %xmm7, 0x70\n\t")
1145 __ASM_SEH(".seh_savexmm %xmm8, 0x80\n\t")
1146 __ASM_SEH(".seh_savexmm %xmm9, 0x90\n\t")
1147 __ASM_SEH(".seh_savexmm %xmm10, 0xa0\n\t")
1148 __ASM_SEH(".seh_savexmm %xmm11, 0xb0\n\t")
1149 __ASM_SEH(".seh_savexmm %xmm12, 0xc0\n\t")
1150 __ASM_SEH(".seh_savexmm %xmm13, 0xd0\n\t")
1151 __ASM_SEH(".seh_savexmm %xmm14, 0xe0\n\t")
1152 __ASM_SEH(".seh_savexmm %xmm15, 0xf0\n\t")
1154 /* call the callback. */
1155 "movq %r8,%rcx\n\t"
1156 "callq *%rdx\n\t"
1157 __ASM_CFI(".cfi_restore_state\n\t")
1158 "nop\n\t" /* Otherwise RtlVirtualUnwind() will think we are inside epilogue and
1159 * interpret / execute the rest of opcodes here instead of unwind through
1160 * machine frame. */
1161 "leaq 0(%rbp),%rsp\n\t"
1162 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1163 "popq %rbp\n\t"
1164 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1165 __ASM_CFI(".cfi_same_value %rbp\n\t")
1166 "ret")
1168 /*******************************************************************
1169 * RtlRestoreContext (NTDLL.@)
1171 void CDECL RtlRestoreContext( CONTEXT *context, EXCEPTION_RECORD *rec )
1173 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1175 if (rec && rec->ExceptionCode == STATUS_LONGJUMP && rec->NumberParameters >= 1)
1177 struct MSVCRT_JUMP_BUFFER *jmp = (struct MSVCRT_JUMP_BUFFER *)rec->ExceptionInformation[0];
1178 context->Rbx = jmp->Rbx;
1179 context->Rsp = jmp->Rsp;
1180 context->Rbp = jmp->Rbp;
1181 context->Rsi = jmp->Rsi;
1182 context->Rdi = jmp->Rdi;
1183 context->R12 = jmp->R12;
1184 context->R13 = jmp->R13;
1185 context->R14 = jmp->R14;
1186 context->R15 = jmp->R15;
1187 context->Rip = jmp->Rip;
1188 context->u.s.Xmm6 = jmp->Xmm6;
1189 context->u.s.Xmm7 = jmp->Xmm7;
1190 context->u.s.Xmm8 = jmp->Xmm8;
1191 context->u.s.Xmm9 = jmp->Xmm9;
1192 context->u.s.Xmm10 = jmp->Xmm10;
1193 context->u.s.Xmm11 = jmp->Xmm11;
1194 context->u.s.Xmm12 = jmp->Xmm12;
1195 context->u.s.Xmm13 = jmp->Xmm13;
1196 context->u.s.Xmm14 = jmp->Xmm14;
1197 context->u.s.Xmm15 = jmp->Xmm15;
1199 else if (rec && rec->ExceptionCode == STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters >= 1)
1201 PVOID (CALLBACK *consolidate)(EXCEPTION_RECORD *) = (void *)rec->ExceptionInformation[0];
1202 TRACE_(seh)( "calling consolidate callback %p (rec=%p)\n", consolidate, rec );
1203 context->Rip = (ULONG64)call_consolidate_callback( context, consolidate, rec );
1206 /* hack: remove no longer accessible TEB frames */
1207 while ((ULONG64)teb_frame < context->Rsp)
1209 TRACE_(seh)( "removing TEB frame: %p\n", teb_frame );
1210 teb_frame = __wine_pop_frame( teb_frame );
1213 TRACE_(seh)( "returning to %p stack %p\n", (void *)context->Rip, (void *)context->Rsp );
1214 NtContinue( context, FALSE );
1218 /*******************************************************************
1219 * RtlUnwindEx (NTDLL.@)
1221 void WINAPI RtlUnwindEx( PVOID end_frame, PVOID target_ip, EXCEPTION_RECORD *rec,
1222 PVOID retval, CONTEXT *context, UNWIND_HISTORY_TABLE *table )
1224 EXCEPTION_REGISTRATION_RECORD *teb_frame = NtCurrentTeb()->Tib.ExceptionList;
1225 EXCEPTION_RECORD record;
1226 DISPATCHER_CONTEXT dispatch;
1227 CONTEXT new_context;
1228 NTSTATUS status;
1229 DWORD i;
1231 RtlCaptureContext( context );
1232 new_context = *context;
1234 /* build an exception record, if we do not have one */
1235 if (!rec)
1237 record.ExceptionCode = STATUS_UNWIND;
1238 record.ExceptionFlags = 0;
1239 record.ExceptionRecord = NULL;
1240 record.ExceptionAddress = (void *)context->Rip;
1241 record.NumberParameters = 0;
1242 rec = &record;
1245 rec->ExceptionFlags |= EH_UNWINDING | (end_frame ? 0 : EH_EXIT_UNWIND);
1247 TRACE( "code=%x flags=%x end_frame=%p target_ip=%p rip=%016I64x\n",
1248 rec->ExceptionCode, rec->ExceptionFlags, end_frame, target_ip, context->Rip );
1249 for (i = 0; i < min( EXCEPTION_MAXIMUM_PARAMETERS, rec->NumberParameters ); i++)
1250 TRACE( " info[%d]=%016I64x\n", i, rec->ExceptionInformation[i] );
1251 TRACE(" rax=%016I64x rbx=%016I64x rcx=%016I64x rdx=%016I64x\n",
1252 context->Rax, context->Rbx, context->Rcx, context->Rdx );
1253 TRACE(" rsi=%016I64x rdi=%016I64x rbp=%016I64x rsp=%016I64x\n",
1254 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
1255 TRACE(" r8=%016I64x r9=%016I64x r10=%016I64x r11=%016I64x\n",
1256 context->R8, context->R9, context->R10, context->R11 );
1257 TRACE(" r12=%016I64x r13=%016I64x r14=%016I64x r15=%016I64x\n",
1258 context->R12, context->R13, context->R14, context->R15 );
1260 dispatch.EstablisherFrame = context->Rsp;
1261 dispatch.TargetIp = (ULONG64)target_ip;
1262 dispatch.ContextRecord = context;
1263 dispatch.HistoryTable = table;
1265 for (;;)
1267 status = virtual_unwind( UNW_FLAG_UHANDLER, &dispatch, &new_context );
1268 if (status != STATUS_SUCCESS) raise_status( status, rec );
1270 unwind_done:
1271 if (!dispatch.EstablisherFrame) break;
1273 if ((dispatch.EstablisherFrame & 7) ||
1274 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
1275 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
1277 ERR( "invalid frame %p (%p-%p)\n", (void *)dispatch.EstablisherFrame,
1278 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1279 rec->ExceptionFlags |= EH_STACK_INVALID;
1280 break;
1283 if (dispatch.LanguageHandler)
1285 if (end_frame && (dispatch.EstablisherFrame > (ULONG64)end_frame))
1287 ERR( "invalid end frame %p/%p\n", (void *)dispatch.EstablisherFrame, end_frame );
1288 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1290 if (dispatch.EstablisherFrame == (ULONG64)end_frame) rec->ExceptionFlags |= EH_TARGET_UNWIND;
1291 if (call_unwind_handler( rec, &dispatch ) == ExceptionCollidedUnwind)
1293 ULONG64 frame;
1295 new_context = *dispatch.ContextRecord;
1296 new_context.ContextFlags &= ~0x40;
1297 *context = new_context;
1298 dispatch.ContextRecord = context;
1299 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1300 dispatch.ControlPc, dispatch.FunctionEntry,
1301 &new_context, NULL, &frame, NULL );
1302 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1303 goto unwind_done;
1305 rec->ExceptionFlags &= ~EH_COLLIDED_UNWIND;
1307 else /* hack: call builtin handlers registered in the tib list */
1309 DWORD64 backup_frame = dispatch.EstablisherFrame;
1310 while ((ULONG64)teb_frame < new_context.Rsp && (ULONG64)teb_frame < (ULONG64)end_frame)
1312 TRACE( "found builtin frame %p handler %p\n", teb_frame, teb_frame->Handler );
1313 dispatch.EstablisherFrame = (ULONG64)teb_frame;
1314 if (call_teb_unwind_handler( rec, &dispatch, teb_frame ) == ExceptionCollidedUnwind)
1316 ULONG64 frame;
1318 teb_frame = __wine_pop_frame( teb_frame );
1320 new_context = *dispatch.ContextRecord;
1321 new_context.ContextFlags &= ~0x40;
1322 *context = new_context;
1323 dispatch.ContextRecord = context;
1324 RtlVirtualUnwind( UNW_FLAG_NHANDLER, dispatch.ImageBase,
1325 dispatch.ControlPc, dispatch.FunctionEntry,
1326 &new_context, NULL, &frame, NULL );
1327 rec->ExceptionFlags |= EH_COLLIDED_UNWIND;
1328 goto unwind_done;
1330 teb_frame = __wine_pop_frame( teb_frame );
1332 if ((ULONG64)teb_frame == (ULONG64)end_frame && (ULONG64)end_frame < new_context.Rsp) break;
1333 dispatch.EstablisherFrame = backup_frame;
1336 if (dispatch.EstablisherFrame == (ULONG64)end_frame) break;
1337 *context = new_context;
1340 context->Rax = (ULONG64)retval;
1341 context->Rip = (ULONG64)target_ip;
1342 RtlRestoreContext(context, rec);
1346 /*******************************************************************
1347 * RtlUnwind (NTDLL.@)
1349 void WINAPI RtlUnwind( void *frame, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1351 CONTEXT context;
1352 RtlUnwindEx( frame, target_ip, rec, retval, &context, NULL );
1356 /*******************************************************************
1357 * _local_unwind (NTDLL.@)
1359 void WINAPI _local_unwind( void *frame, void *target_ip )
1361 CONTEXT context;
1362 RtlUnwindEx( frame, target_ip, NULL, NULL, &context, NULL );
1365 /*******************************************************************
1366 * __C_specific_handler (NTDLL.@)
1368 EXCEPTION_DISPOSITION WINAPI __C_specific_handler( EXCEPTION_RECORD *rec,
1369 void *frame,
1370 CONTEXT *context,
1371 struct _DISPATCHER_CONTEXT *dispatch )
1373 SCOPE_TABLE *table = dispatch->HandlerData;
1374 ULONG i;
1376 TRACE_(seh)( "%p %p %p %p\n", rec, frame, context, dispatch );
1377 if (TRACE_ON(seh)) dump_scope_table( dispatch->ImageBase, table );
1379 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
1381 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1383 if (dispatch->ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1384 dispatch->ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1386 PTERMINATION_HANDLER handler;
1388 if (table->ScopeRecord[i].JumpTarget) continue;
1390 if (rec->ExceptionFlags & EH_TARGET_UNWIND &&
1391 dispatch->TargetIp >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1392 dispatch->TargetIp < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1394 break;
1397 handler = (PTERMINATION_HANDLER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1398 dispatch->ScopeIndex = i+1;
1400 TRACE_(seh)( "calling __finally %p frame %p\n", handler, frame );
1401 handler( TRUE, frame );
1404 return ExceptionContinueSearch;
1407 for (i = dispatch->ScopeIndex; i < table->Count; i++)
1409 if (dispatch->ControlPc >= dispatch->ImageBase + table->ScopeRecord[i].BeginAddress &&
1410 dispatch->ControlPc < dispatch->ImageBase + table->ScopeRecord[i].EndAddress)
1412 if (!table->ScopeRecord[i].JumpTarget) continue;
1413 if (table->ScopeRecord[i].HandlerAddress != EXCEPTION_EXECUTE_HANDLER)
1415 EXCEPTION_POINTERS ptrs;
1416 PEXCEPTION_FILTER filter;
1418 filter = (PEXCEPTION_FILTER)(dispatch->ImageBase + table->ScopeRecord[i].HandlerAddress);
1419 ptrs.ExceptionRecord = rec;
1420 ptrs.ContextRecord = context;
1421 TRACE_(seh)( "calling filter %p ptrs %p frame %p\n", filter, &ptrs, frame );
1422 switch (filter( &ptrs, frame ))
1424 case EXCEPTION_EXECUTE_HANDLER:
1425 break;
1426 case EXCEPTION_CONTINUE_SEARCH:
1427 continue;
1428 case EXCEPTION_CONTINUE_EXECUTION:
1429 return ExceptionContinueExecution;
1432 TRACE( "unwinding to target %p\n", (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget );
1433 RtlUnwindEx( frame, (char *)dispatch->ImageBase + table->ScopeRecord[i].JumpTarget,
1434 rec, 0, dispatch->ContextRecord, dispatch->HistoryTable );
1437 return ExceptionContinueSearch;
1441 /***********************************************************************
1442 * RtlRaiseException (NTDLL.@)
1444 __ASM_GLOBAL_FUNC( RtlRaiseException,
1445 "sub $0x4f8,%rsp\n\t"
1446 __ASM_SEH(".seh_stackalloc 0x4f8\n\t")
1447 __ASM_SEH(".seh_endprologue\n\t")
1448 __ASM_CFI(".cfi_adjust_cfa_offset 0x4f8\n\t")
1449 "movq %rcx,0x500(%rsp)\n\t"
1450 "leaq 0x20(%rsp),%rcx\n\t"
1451 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
1452 "leaq 0x20(%rsp),%rdx\n\t" /* context pointer */
1453 "leaq 0x500(%rsp),%rax\n\t" /* orig stack pointer */
1454 "movq %rax,0x98(%rdx)\n\t" /* context->Rsp */
1455 "movq (%rax),%rcx\n\t" /* original first parameter */
1456 "movq %rcx,0x80(%rdx)\n\t" /* context->Rcx */
1457 "movq 0x4f8(%rsp),%rax\n\t" /* return address */
1458 "movq %rax,0xf8(%rdx)\n\t" /* context->Rip */
1459 "movq %rax,0x10(%rcx)\n\t" /* rec->ExceptionAddress */
1460 "movl $1,%r8d\n\t"
1461 "movq %gs:(0x30),%rax\n\t" /* Teb */
1462 "movq 0x60(%rax),%rax\n\t" /* Peb */
1463 "cmpb $0,0x02(%rax)\n\t" /* BeingDebugged */
1464 "jne 1f\n\t"
1465 "call " __ASM_NAME("dispatch_exception") "\n"
1466 "1:\tcall " __ASM_NAME("NtRaiseException") "\n\t"
1467 "movq %rax,%rcx\n\t"
1468 "call " __ASM_NAME("RtlRaiseStatus") /* does not return */ );
1471 static inline ULONG hash_pointers( void **ptrs, ULONG count )
1473 /* Based on MurmurHash2, which is in the public domain */
1474 static const ULONG m = 0x5bd1e995;
1475 static const ULONG r = 24;
1476 ULONG hash = count * sizeof(void*);
1477 for (; count > 0; ptrs++, count--)
1479 ULONG_PTR data = (ULONG_PTR)*ptrs;
1480 ULONG k1 = (ULONG)(data & 0xffffffff), k2 = (ULONG)(data >> 32);
1481 k1 *= m;
1482 k1 = (k1 ^ (k1 >> r)) * m;
1483 k2 *= m;
1484 k2 = (k2 ^ (k2 >> r)) * m;
1485 hash = (((hash * m) ^ k1) * m) ^ k2;
1487 hash = (hash ^ (hash >> 13)) * m;
1488 return hash ^ (hash >> 15);
1492 /*************************************************************************
1493 * RtlCaptureStackBackTrace (NTDLL.@)
1495 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1497 UNWIND_HISTORY_TABLE table;
1498 DISPATCHER_CONTEXT dispatch;
1499 CONTEXT context;
1500 NTSTATUS status;
1501 ULONG i;
1502 USHORT num_entries = 0;
1504 TRACE( "(%u, %u, %p, %p)\n", skip, count, buffer, hash );
1506 RtlCaptureContext( &context );
1507 dispatch.TargetIp = 0;
1508 dispatch.ContextRecord = &context;
1509 dispatch.HistoryTable = &table;
1510 if (hash) *hash = 0;
1511 for (i = 0; i < skip + count; i++)
1513 status = virtual_unwind( UNW_FLAG_NHANDLER, &dispatch, &context );
1514 if (status != STATUS_SUCCESS) return i;
1516 if (!dispatch.EstablisherFrame) break;
1518 if ((dispatch.EstablisherFrame & 7) ||
1519 dispatch.EstablisherFrame < (ULONG64)NtCurrentTeb()->Tib.StackLimit ||
1520 dispatch.EstablisherFrame > (ULONG64)NtCurrentTeb()->Tib.StackBase)
1522 ERR( "invalid frame %p (%p-%p)\n", (void *)dispatch.EstablisherFrame,
1523 NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase );
1524 break;
1527 if (context.Rsp == (ULONG64)NtCurrentTeb()->Tib.StackBase) break;
1529 if (i >= skip) buffer[num_entries++] = (void *)context.Rip;
1531 if (hash && num_entries > 0) *hash = hash_pointers( buffer, num_entries );
1532 TRACE( "captured %hu frames\n", num_entries );
1533 return num_entries;
1537 /***********************************************************************
1538 * signal_start_thread
1540 __ASM_GLOBAL_FUNC( signal_start_thread,
1541 "movq %rcx,%rbx\n\t" /* context */
1542 /* clear the thread stack */
1543 "andq $~0xfff,%rcx\n\t" /* round down to page size */
1544 "leaq -0xf0000(%rcx),%rdi\n\t"
1545 "movq %rdi,%rsp\n\t"
1546 "subq %rdi,%rcx\n\t"
1547 "xorl %eax,%eax\n\t"
1548 "shrq $3,%rcx\n\t"
1549 "rep; stosq\n\t"
1550 /* switch to the initial context */
1551 "leaq -32(%rbx),%rsp\n\t"
1552 "movq %rbx,%rcx\n\t"
1553 "movl $1,%edx\n\t"
1554 "call " __ASM_NAME("NtContinue") )
1557 /**********************************************************************
1558 * DbgBreakPoint (NTDLL.@)
1560 __ASM_STDCALL_FUNC( DbgBreakPoint, 0, "int $3; ret"
1561 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1562 "\n\tnop; nop; nop; nop; nop; nop" );
1564 /**********************************************************************
1565 * DbgUserBreakPoint (NTDLL.@)
1567 __ASM_STDCALL_FUNC( DbgUserBreakPoint, 0, "int $3; ret"
1568 "\n\tnop; nop; nop; nop; nop; nop; nop; nop"
1569 "\n\tnop; nop; nop; nop; nop; nop" );
1571 #endif /* __x86_64__ */