2 * exceptions-amd64.c: exception support for AMD64
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 #if _WIN32_WINNT < 0x0501
14 /* Required for Vectored Exception Handling. */
16 #define _WIN32_WINNT 0x0501
17 #endif /* _WIN32_WINNT < 0x0501 */
22 #ifdef HAVE_UCONTEXT_H
26 #include <mono/arch/amd64/amd64-codegen.h>
27 #include <mono/metadata/appdomain.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/threads.h>
30 #include <mono/metadata/threads-types.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/gc-internal.h>
34 #include <mono/metadata/mono-debug.h>
35 #include <mono/utils/mono-mmap.h>
38 #include "mini-amd64.h"
40 #include "debug-mini.h"
42 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
45 static MonoW32ExceptionHandler fpe_handler;
46 static MonoW32ExceptionHandler ill_handler;
47 static MonoW32ExceptionHandler segv_handler;
49 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
50 guint64 mono_win_vectored_exception_handle;
51 extern gboolean mono_win_chained_exception_needs_run;
53 #define W32_SEH_HANDLE_EX(_ex) \
54 if (_ex##_handler) _ex##_handler(0, ep, sctx)
56 LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
58 #ifndef MONO_CROSS_COMPILE
59 if (mono_old_win_toplevel_exception_filter) {
60 return (*mono_old_win_toplevel_exception_filter)(ep);
64 mono_handle_native_sigsegv (SIGSEGV, NULL);
66 return EXCEPTION_CONTINUE_SEARCH;
70 * Unhandled Exception Filter
71 * Top-level per-process exception handler.
73 LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
80 mono_win_chained_exception_needs_run = FALSE;
81 res = EXCEPTION_CONTINUE_EXECUTION;
83 er = ep->ExceptionRecord;
84 ctx = ep->ContextRecord;
85 sctx = g_malloc(sizeof(MonoContext));
87 /* Copy Win32 context to UNIX style context */
100 sctx->r15 = ctx->R15;
102 switch (er->ExceptionCode) {
103 case EXCEPTION_ACCESS_VIOLATION:
104 W32_SEH_HANDLE_EX(segv);
106 case EXCEPTION_ILLEGAL_INSTRUCTION:
107 W32_SEH_HANDLE_EX(ill);
109 case EXCEPTION_INT_DIVIDE_BY_ZERO:
110 case EXCEPTION_INT_OVERFLOW:
111 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
112 case EXCEPTION_FLT_OVERFLOW:
113 case EXCEPTION_FLT_UNDERFLOW:
114 case EXCEPTION_FLT_INEXACT_RESULT:
115 W32_SEH_HANDLE_EX(fpe);
121 if (mono_win_chained_exception_needs_run) {
122 /* Don't copy context back if we chained exception
123 * as the handler may have modfied the EXCEPTION_POINTERS
124 * directly. We don't pass sigcontext to chained handlers.
125 * Return continue search so the UnhandledExceptionFilter
126 * can correctly chain the exception.
128 res = EXCEPTION_CONTINUE_SEARCH;
130 /* Copy context back */
132 ctx->Rsp = sctx->rsp;
133 ctx->Rdi = sctx->rdi;
134 ctx->Rsi = sctx->rsi;
135 ctx->Rbx = sctx->rbx;
136 ctx->Rbp = sctx->rbp;
137 ctx->R12 = sctx->r12;
138 ctx->R13 = sctx->r13;
139 ctx->R14 = sctx->r14;
140 ctx->R15 = sctx->r15;
141 ctx->Rip = sctx->rip;
143 /* Volatile But should not matter?*/
144 ctx->Rax = sctx->rax;
145 ctx->Rcx = sctx->rcx;
146 ctx->Rdx = sctx->rdx;
149 /* TODO: Find right place to free this in stack overflow case */
150 if (er->ExceptionCode != EXCEPTION_STACK_OVERFLOW)
156 void win32_seh_init()
158 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_unhandled_exception_filter);
159 mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_handler);
162 void win32_seh_cleanup()
164 if (mono_old_win_toplevel_exception_filter) SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
168 ret = RemoveVectoredExceptionHandler (mono_win_vectored_exception_handle);
172 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
176 fpe_handler = handler;
179 ill_handler = handler;
182 segv_handler = handler;
189 #endif /* TARGET_WIN32 */
192 * mono_arch_get_restore_context:
194 * Returns a pointer to a method which restores a previously saved sigcontext.
197 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
199 guint8 *start = NULL;
201 MonoJumpInfo *ji = NULL;
202 GSList *unwind_ops = NULL;
204 /* restore_contect (MonoContext *ctx) */
206 start = code = mono_global_codeman_reserve (256);
208 amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG1, 8);
210 /* Restore all registers except %rip and %r11 */
211 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rax), 8);
212 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rcx), 8);
213 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rdx), 8);
214 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rbx), 8);
215 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rbp), 8);
216 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsi), 8);
217 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rdi), 8);
218 //amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r8), 8);
219 //amd64_mov_reg_membase (code, AMD64_R9, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r9), 8);
220 //amd64_mov_reg_membase (code, AMD64_R10, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r10), 8);
221 amd64_mov_reg_membase (code, AMD64_R12, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r12), 8);
222 amd64_mov_reg_membase (code, AMD64_R13, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r13), 8);
223 amd64_mov_reg_membase (code, AMD64_R14, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r14), 8);
224 #if !defined(__native_client_codegen__)
225 amd64_mov_reg_membase (code, AMD64_R15, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r15), 8);
229 * The context resides on the stack, in the stack frame of the
230 * caller of this function. The stack pointer that we need to
231 * restore is potentially many stack frames higher up, so the
232 * distance between them can easily be more than the red zone
233 * size. Hence the stack pointer can be restored only after
234 * we have finished loading everything from the context.
236 amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
237 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
238 amd64_mov_reg_reg (code, AMD64_RSP, AMD64_R8, 8);
240 /* jump to the saved IP */
241 amd64_jump_reg (code, AMD64_R11);
243 nacl_global_codeman_validate(&start, 256, &code);
245 mono_arch_flush_icache (start, code - start);
248 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
254 * mono_arch_get_call_filter:
256 * Returns a pointer to a method which calls an exception filter. We
257 * also use this function to call finally handlers (we pass NULL as
258 * @exc object in this case).
261 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
267 MonoJumpInfo *ji = NULL;
268 GSList *unwind_ops = NULL;
269 const guint kMaxCodeSize = NACL_SIZE (128, 256);
271 start = code = mono_global_codeman_reserve (kMaxCodeSize);
273 /* call_filter (MonoContext *ctx, unsigned long eip) */
276 /* Alloc new frame */
277 amd64_push_reg (code, AMD64_RBP);
278 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
280 /* Save callee saved regs */
282 for (i = 0; i < AMD64_NREG; ++i)
283 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
284 amd64_push_reg (code, i);
290 amd64_push_reg (code, AMD64_RBP);
292 /* Make stack misaligned, the call will make it aligned again */
294 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
297 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbp), 8);
298 /* load callee saved regs */
299 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbx), 8);
300 amd64_mov_reg_membase (code, AMD64_R12, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r12), 8);
301 amd64_mov_reg_membase (code, AMD64_R13, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r13), 8);
302 amd64_mov_reg_membase (code, AMD64_R14, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r14), 8);
303 #if !defined(__native_client_codegen__)
304 amd64_mov_reg_membase (code, AMD64_R15, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r15), 8);
307 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rdi), 8);
308 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rsi), 8);
311 /* call the handler */
312 amd64_call_reg (code, AMD64_ARG_REG2);
315 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
318 amd64_pop_reg (code, AMD64_RBP);
320 /* Restore callee saved regs */
321 for (i = AMD64_NREG; i >= 0; --i)
322 if (AMD64_IS_CALLEE_SAVED_REG (i))
323 amd64_pop_reg (code, i);
328 g_assert ((code - start) < kMaxCodeSize);
330 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
332 mono_arch_flush_icache (start, code - start);
335 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
341 * The first few arguments are dummy, to force the other arguments to be passed on
342 * the stack, this avoids overwriting the argument registers in the throw trampoline.
345 mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
346 guint64 dummy5, guint64 dummy6,
347 mgreg_t *regs, mgreg_t rip,
348 MonoObject *exc, gboolean rethrow)
352 ctx.rsp = regs [AMD64_RSP];
354 ctx.rbx = regs [AMD64_RBX];
355 ctx.rbp = regs [AMD64_RBP];
356 ctx.r12 = regs [AMD64_R12];
357 ctx.r13 = regs [AMD64_R13];
358 ctx.r14 = regs [AMD64_R14];
359 ctx.r15 = regs [AMD64_R15];
360 ctx.rdi = regs [AMD64_RDI];
361 ctx.rsi = regs [AMD64_RSI];
362 ctx.rax = regs [AMD64_RAX];
363 ctx.rcx = regs [AMD64_RCX];
364 ctx.rdx = regs [AMD64_RDX];
366 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
367 MonoException *mono_ex = (MonoException*)exc;
369 mono_ex->stack_trace = NULL;
372 if (mono_debug_using_mono_debugger ()) {
375 mono_breakpoint_clean_code (NULL, (gpointer)rip, 8, buf, sizeof (buf));
377 if (buf [3] == 0xe8) {
378 MonoContext ctx_cp = ctx;
379 ctx_cp.rip = rip - 5;
381 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
382 mono_restore_context (&ctx_cp);
383 g_assert_not_reached ();
388 /* adjust eip so that it point into the call instruction */
391 mono_handle_exception (&ctx, exc);
392 mono_restore_context (&ctx);
393 g_assert_not_reached ();
397 mono_amd64_throw_corlib_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
398 guint64 dummy5, guint64 dummy6,
399 mgreg_t *regs, mgreg_t rip,
400 guint32 ex_token_index, gint64 pc_offset)
402 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
405 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
409 /* Negate the ip adjustment done in mono_amd64_throw_exception () */
412 mono_amd64_throw_exception (dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, regs, rip, (MonoObject*)ex, FALSE);
416 mono_amd64_resume_unwind (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
417 guint64 dummy5, guint64 dummy6,
418 mgreg_t *regs, mgreg_t rip,
419 guint32 dummy7, gint64 dummy8)
421 /* Only the register parameters are valid */
424 ctx.rsp = regs [AMD64_RSP];
426 ctx.rbx = regs [AMD64_RBX];
427 ctx.rbp = regs [AMD64_RBP];
428 ctx.r12 = regs [AMD64_R12];
429 ctx.r13 = regs [AMD64_R13];
430 ctx.r14 = regs [AMD64_R14];
431 ctx.r15 = regs [AMD64_R15];
432 ctx.rdi = regs [AMD64_RDI];
433 ctx.rsi = regs [AMD64_RSI];
434 ctx.rax = regs [AMD64_RAX];
435 ctx.rcx = regs [AMD64_RCX];
436 ctx.rdx = regs [AMD64_RDX];
438 mono_resume_unwind (&ctx);
442 * get_throw_trampoline:
444 * Generate a call to mono_amd64_throw_exception/
445 * mono_amd64_throw_corlib_exception.
448 get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, const char *tramp_name, gboolean aot)
452 MonoJumpInfo *ji = NULL;
453 GSList *unwind_ops = NULL;
454 int i, stack_size, arg_offsets [16], regs_offset, dummy_stack_space;
455 const guint kMaxCodeSize = NACL_SIZE (256, 512);
458 dummy_stack_space = 6 * sizeof(mgreg_t); /* Windows expects stack space allocated for all 6 dummy args. */
460 dummy_stack_space = 0;
463 start = code = mono_global_codeman_reserve (kMaxCodeSize);
465 /* The stack is unaligned on entry */
466 stack_size = 192 + 8 + dummy_stack_space;
471 unwind_ops = mono_arch_get_cie_program ();
474 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_size);
476 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 8);
479 * To hide linux/windows calling convention differences, we pass all arguments on
480 * the stack by passing 6 dummy values in registers.
483 arg_offsets [0] = dummy_stack_space + 0;
484 arg_offsets [1] = dummy_stack_space + sizeof(mgreg_t);
485 arg_offsets [2] = dummy_stack_space + sizeof(mgreg_t) * 2;
486 arg_offsets [3] = dummy_stack_space + sizeof(mgreg_t) * 3;
487 regs_offset = dummy_stack_space + sizeof(mgreg_t) * 4;
490 for (i = 0; i < AMD64_NREG; ++i)
492 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
494 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, stack_size + sizeof(mgreg_t));
495 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (AMD64_RSP * sizeof(mgreg_t)), X86_EAX, sizeof(mgreg_t));
496 /* Set arg1 == regs */
497 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, regs_offset);
498 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [0], AMD64_RAX, sizeof(mgreg_t));
499 /* Set arg2 == eip */
501 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
503 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, stack_size, sizeof(mgreg_t));
504 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [1], AMD64_RAX, sizeof(mgreg_t));
505 /* Set arg3 == exc/ex_token_index */
507 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [2], 0, sizeof(mgreg_t));
509 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [2], AMD64_ARG_REG1, sizeof(mgreg_t));
510 /* Set arg4 == rethrow/pc offset */
512 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], 0, sizeof(mgreg_t));
514 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [3], AMD64_ARG_REG2, sizeof(mgreg_t));
517 * The caller is LLVM code which passes the absolute address not a pc offset,
518 * so compensate by passing 0 as 'rip' and passing the negated abs address as
521 amd64_neg_membase (code, AMD64_RSP, arg_offsets [3]);
523 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], rethrow, sizeof(mgreg_t));
527 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? "mono_amd64_throw_corlib_exception" : "mono_amd64_throw_exception");
528 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
530 amd64_mov_reg_imm (code, AMD64_R11, resume_unwind ? ((gpointer)mono_amd64_resume_unwind) : (corlib ? (gpointer)mono_amd64_throw_corlib_exception : (gpointer)mono_amd64_throw_exception));
532 amd64_call_reg (code, AMD64_R11);
533 amd64_breakpoint (code);
535 mono_arch_flush_icache (start, code - start);
537 g_assert ((code - start) < kMaxCodeSize);
539 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
542 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
548 * mono_arch_get_throw_exception:
550 * Returns a function pointer which can be used to raise
551 * exceptions. The returned function has the following
552 * signature: void (*func) (MonoException *exc);
556 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
558 return get_throw_trampoline (info, FALSE, FALSE, FALSE, FALSE, "throw_exception", aot);
562 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
564 return get_throw_trampoline (info, TRUE, FALSE, FALSE, FALSE, "rethrow_exception", aot);
568 * mono_arch_get_throw_corlib_exception:
570 * Returns a function pointer which can be used to raise
571 * corlib exceptions. The returned function has the following
572 * signature: void (*func) (guint32 ex_token, guint32 offset);
573 * Here, offset is the offset which needs to be substracted from the caller IP
574 * to get the IP of the throw. Passing the offset has the advantage that it
575 * needs no relocations in the caller.
578 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
580 return get_throw_trampoline (info, FALSE, TRUE, FALSE, FALSE, "throw_corlib_exception", aot);
584 * mono_arch_find_jit_info:
586 * This function is used to gather information from @ctx, and store it in @frame_info.
587 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
588 * is modified if needed.
589 * Returns TRUE on success, FALSE otherwise.
592 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
593 MonoJitInfo *ji, MonoContext *ctx,
594 MonoContext *new_ctx, MonoLMF **lmf,
595 mgreg_t **save_locations,
596 StackFrameInfo *frame)
598 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
600 memset (frame, 0, sizeof (StackFrameInfo));
606 mgreg_t regs [MONO_MAX_IREGS + 1];
608 guint32 unwind_info_len;
611 frame->type = FRAME_TYPE_MANAGED;
614 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
616 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
618 frame->unwind_info = unwind_info;
619 frame->unwind_info_len = unwind_info_len;
621 regs [AMD64_RAX] = new_ctx->rax;
622 regs [AMD64_RBX] = new_ctx->rbx;
623 regs [AMD64_RCX] = new_ctx->rcx;
624 regs [AMD64_RDX] = new_ctx->rdx;
625 regs [AMD64_RBP] = new_ctx->rbp;
626 regs [AMD64_RSP] = new_ctx->rsp;
627 regs [AMD64_RSI] = new_ctx->rsi;
628 regs [AMD64_RDI] = new_ctx->rdi;
629 regs [AMD64_RIP] = new_ctx->rip;
630 regs [AMD64_R12] = new_ctx->r12;
631 regs [AMD64_R13] = new_ctx->r13;
632 regs [AMD64_R14] = new_ctx->r14;
633 regs [AMD64_R15] = new_ctx->r15;
635 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
636 (guint8*)ji->code_start + ji->code_size,
637 ip, regs, MONO_MAX_IREGS + 1,
638 save_locations, MONO_MAX_IREGS, &cfa);
640 new_ctx->rax = regs [AMD64_RAX];
641 new_ctx->rbx = regs [AMD64_RBX];
642 new_ctx->rcx = regs [AMD64_RCX];
643 new_ctx->rdx = regs [AMD64_RDX];
644 new_ctx->rbp = regs [AMD64_RBP];
645 new_ctx->rsp = regs [AMD64_RSP];
646 new_ctx->rsi = regs [AMD64_RSI];
647 new_ctx->rdi = regs [AMD64_RDI];
648 new_ctx->rip = regs [AMD64_RIP];
649 new_ctx->r12 = regs [AMD64_R12];
650 new_ctx->r13 = regs [AMD64_R13];
651 new_ctx->r14 = regs [AMD64_R14];
652 new_ctx->r15 = regs [AMD64_R15];
654 /* The CFA becomes the new SP value */
655 new_ctx->rsp = (mgreg_t)cfa;
660 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
661 /* remove any unused lmf */
662 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
665 #ifndef MONO_AMD64_NO_PUSHES
666 /* Pop arguments off the stack */
667 if (ji->has_arch_eh_info)
668 new_ctx->rsp += mono_jit_info_get_arch_eh_info (ji)->stack_size;
675 if (((guint64)(*lmf)->previous_lmf) & 2) {
677 * This LMF entry is created by the soft debug code to mark transitions to
678 * managed code done during invokes.
680 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
682 g_assert (ext->debugger_invoke);
684 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
686 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
688 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
693 if (((guint64)(*lmf)->previous_lmf) & 1) {
694 /* This LMF has the rip field set */
696 } else if ((*lmf)->rsp == 0) {
701 * The rsp field is set just before the call which transitioned to native
702 * code. Obtain the rip from the stack.
704 rip = *(guint64*)((*lmf)->rsp - sizeof(mgreg_t));
707 ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
709 * FIXME: ji == NULL can happen when a managed-to-native wrapper is interrupted
710 * in the soft debugger suspend code, since (*lmf)->rsp no longer points to the
721 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
724 new_ctx->rbp = (*lmf)->rbp;
725 new_ctx->rsp = (*lmf)->rsp;
727 new_ctx->rbx = (*lmf)->rbx;
728 new_ctx->r12 = (*lmf)->r12;
729 new_ctx->r13 = (*lmf)->r13;
730 new_ctx->r14 = (*lmf)->r14;
731 new_ctx->r15 = (*lmf)->r15;
733 new_ctx->rdi = (*lmf)->rdi;
734 new_ctx->rsi = (*lmf)->rsi;
737 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
748 * Called by resuming from a signal handler.
751 handle_signal_exception (gpointer obj)
753 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
756 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
758 if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
761 mono_handle_exception (&ctx, obj);
763 mono_restore_context (&ctx);
767 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
769 guint64 sp = ctx->rsp;
771 ctx->rdi = (guint64)user_data;
773 /* Allocate a stack frame below the red zone */
775 /* The stack should be unaligned */
779 /* Preserve the call chain to prevent crashes in the libgcc unwinder (#15969) */
780 *(guint64*)sp = ctx->rip;
783 ctx->rip = (guint64)async_cb;
787 * mono_arch_handle_exception:
789 * @ctx: saved processor state
790 * @obj: the exception object
793 mono_arch_handle_exception (void *sigctx, gpointer obj)
795 #if defined(MONO_ARCH_USE_SIGACTION)
799 * Handling the exception in the signal handler is problematic, since the original
800 * signal is disabled, and we could run arbitrary code though the debugger. So
801 * resume into the normal stack and do most work there if possible.
803 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
805 /* Pass the ctx parameter in TLS */
806 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
808 mctx = jit_tls->ex_ctx;
809 mono_arch_setup_async_callback (&mctx, handle_signal_exception, obj);
810 mono_monoctx_to_sigctx (&mctx, sigctx);
816 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
818 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
821 mono_handle_exception (&mctx, obj);
823 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
830 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
832 mono_sigctx_to_monoctx (sigctx, mctx);
836 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
838 mono_monoctx_to_sigctx (mctx, sigctx);
842 mono_arch_ip_from_context (void *sigctx)
844 #if defined(MONO_ARCH_USE_SIGACTION)
845 ucontext_t *ctx = (ucontext_t*)sigctx;
847 return (gpointer)UCONTEXT_REG_RIP (ctx);
849 MonoContext *ctx = sigctx;
850 return (gpointer)ctx->rip;
855 restore_soft_guard_pages (void)
857 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
858 if (jit_tls->stack_ovf_guard_base)
859 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
863 * this function modifies mctx so that when it is restored, it
864 * won't execcute starting at mctx.eip, but in a function that
865 * will restore the protection on the soft-guard pages and return back to
866 * continue at mctx.eip.
869 prepare_for_guard_pages (MonoContext *mctx)
872 sp = (gpointer)(mctx->rsp);
874 /* the return addr */
875 sp [0] = (gpointer)(mctx->rip);
876 mctx->rip = (guint64)restore_soft_guard_pages;
877 mctx->rsp = (guint64)sp;
881 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
885 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
887 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
889 prepare_for_guard_pages (&mctx);
890 mono_restore_context (&mctx);
893 mono_handle_exception (&mctx, obj);
895 prepare_for_guard_pages (&mctx);
896 mono_restore_context (&mctx);
900 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
902 #if defined(MONO_ARCH_USE_SIGACTION)
903 MonoException *exc = NULL;
904 ucontext_t *ctx = (ucontext_t*)sigctx;
905 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_RIP (sigctx), NULL);
908 ucontext_t *copied_ctx;
911 exc = mono_domain_get ()->stack_overflow_ex;
913 mono_handle_native_sigsegv (SIGSEGV, sigctx);
915 /* setup a call frame on the real stack so that control is returned there
916 * and exception handling can continue.
917 * The frame looks like:
921 * 128 is the size of the red zone
923 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4 + 128;
925 frame_size += sizeof (*ctx->uc_mcontext);
929 sp = (gpointer)(UCONTEXT_REG_RSP (sigctx) & ~15);
930 sp = (gpointer)((char*)sp - frame_size);
931 copied_ctx = (ucontext_t*)(sp + 4);
932 /* the arguments must be aligned */
933 sp [-1] = (gpointer)UCONTEXT_REG_RIP (sigctx);
934 /* may need to adjust pointers in the new struct copy, depending on the OS */
935 memcpy (copied_ctx, ctx, sizeof (ucontext_t));
938 guint8 * copied_mcontext = (guint8*)copied_ctx + sizeof (ucontext_t);
939 /* uc_mcontext is a pointer, so make a copy which is stored after the ctx */
940 memcpy (copied_mcontext, ctx->uc_mcontext, sizeof (*ctx->uc_mcontext));
941 copied_ctx->uc_mcontext = (void*)copied_mcontext;
944 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
945 UCONTEXT_REG_RIP (sigctx) = (unsigned long)altstack_handle_and_restore;
946 UCONTEXT_REG_RSP (sigctx) = (unsigned long)(sp - 1);
947 UCONTEXT_REG_RDI (sigctx) = (unsigned long)(copied_ctx);
948 UCONTEXT_REG_RSI (sigctx) = (guint64)exc;
949 UCONTEXT_REG_RDX (sigctx) = stack_ovf;
954 mono_amd64_get_original_ip (void)
956 MonoLMF *lmf = mono_get_lmf ();
960 /* Reset the change to previous_lmf */
961 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf & ~1);
967 mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
969 guint8 *code, *start;
971 gpointer throw_trampoline;
972 MonoJumpInfo *ji = NULL;
973 GSList *unwind_ops = NULL;
974 const guint kMaxCodeSize = NACL_SIZE (128, 256);
976 start = code = mono_global_codeman_reserve (kMaxCodeSize);
978 /* We are in the frame of a managed method after a call */
980 * We would like to throw the pending exception in such a way that it looks to
981 * be thrown from the managed method.
984 /* Save registers which might contain the return value of the call */
985 amd64_push_reg (code, AMD64_RAX);
986 amd64_push_reg (code, AMD64_RDX);
988 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
989 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
992 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
994 /* Obtain the pending exception */
996 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
997 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
999 amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
1001 amd64_call_reg (code, AMD64_R11);
1003 /* Check if it is NULL, and branch */
1004 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
1005 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
1007 /* exc != NULL branch */
1009 /* Save the exc on the stack */
1010 amd64_push_reg (code, AMD64_RAX);
1012 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
1014 /* Obtain the original ip and clear the flag in previous_lmf */
1016 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
1017 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1019 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
1021 amd64_call_reg (code, AMD64_R11);
1024 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, 8, 8);
1026 /* Pop saved stuff from the stack */
1027 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 6 * 8);
1029 /* Setup arguments for the throw trampoline */
1031 amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_R11, 8);
1032 /* The trampoline expects the caller ip to be pushed on the stack */
1033 amd64_push_reg (code, AMD64_RAX);
1035 /* Call the throw trampoline */
1037 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_throw_exception");
1038 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1040 throw_trampoline = mono_get_throw_exception ();
1041 amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
1043 /* We use a jump instead of a call so we can push the original ip on the stack */
1044 amd64_jump_reg (code, AMD64_R11);
1046 /* ex == NULL branch */
1047 mono_amd64_patch (br [0], code);
1049 /* Obtain the original ip and clear the flag in previous_lmf */
1051 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
1052 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1054 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
1056 amd64_call_reg (code, AMD64_R11);
1057 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
1059 /* Restore registers */
1060 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1061 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
1062 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1063 amd64_pop_reg (code, AMD64_RDX);
1064 amd64_pop_reg (code, AMD64_RAX);
1066 /* Return to original code */
1067 amd64_jump_reg (code, AMD64_R11);
1069 g_assert ((code - start) < kMaxCodeSize);
1071 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1074 *info = mono_tramp_info_create ("throw_pending_exception", start, code - start, ji, unwind_ops);
1079 static gpointer throw_pending_exception;
1082 * Called when a thread receives an async exception while executing unmanaged code.
1083 * Instead of checking for this exception in the managed-to-native wrapper, we hijack
1084 * the return address on the stack to point to a helper routine which throws the
1088 mono_arch_notify_pending_exc (void)
1090 MonoLMF *lmf = mono_get_lmf ();
1093 /* Not yet started */
1100 if ((guint64)lmf->previous_lmf & 1)
1101 /* Already hijacked or trampoline LMF entry */
1104 /* lmf->rsp is set just before making the call which transitions to unmanaged code */
1105 lmf->rip = *(guint64*)(lmf->rsp - 8);
1106 /* Signal that lmf->rip is set */
1107 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
1109 *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
1113 mono_amd64_get_exception_trampolines (gboolean aot)
1115 MonoTrampInfo *info;
1116 GSList *tramps = NULL;
1118 mono_arch_get_throw_pending_exception (&info, aot);
1119 tramps = g_slist_prepend (tramps, info);
1121 /* LLVM needs different throw trampolines */
1122 get_throw_trampoline (&info, FALSE, TRUE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", aot);
1123 tramps = g_slist_prepend (tramps, info);
1125 get_throw_trampoline (&info, FALSE, TRUE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", aot);
1126 tramps = g_slist_prepend (tramps, info);
1128 get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", FALSE);
1129 tramps = g_slist_prepend (tramps, info);
1135 mono_arch_exceptions_init (void)
1140 if (mono_aot_only) {
1141 throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
1142 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
1143 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
1144 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
1145 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
1146 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
1147 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
1149 /* Call this to avoid initialization races */
1150 throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
1152 tramps = mono_amd64_get_exception_trampolines (FALSE);
1153 for (l = tramps; l; l = l->next) {
1154 MonoTrampInfo *info = l->data;
1156 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
1157 mono_tramp_info_register (info);
1159 g_slist_free (tramps);
1166 * The mono_arch_unwindinfo* methods are used to build and add
1167 * function table info for each emitted method from mono. On Winx64
1168 * the seh handler will not be called if the mono methods are not
1169 * added to the function table.
1171 * We should not need to add non-volatile register info to the
1172 * table since mono stores that info elsewhere. (Except for the register
1176 #define MONO_MAX_UNWIND_CODES 22
1178 typedef union _UNWIND_CODE {
1181 guchar UnwindOp : 4;
1184 gushort FrameOffset;
1185 } UNWIND_CODE, *PUNWIND_CODE;
1187 typedef struct _UNWIND_INFO {
1190 guchar SizeOfProlog;
1191 guchar CountOfCodes;
1192 guchar FrameRegister : 4;
1193 guchar FrameOffset : 4;
1194 /* custom size for mono allowing for mono allowing for*/
1195 /*UWOP_PUSH_NONVOL ebp offset = 21*/
1196 /*UWOP_ALLOC_LARGE : requires 2 or 3 offset = 20*/
1197 /*UWOP_SET_FPREG : requires 2 offset = 17*/
1198 /*UWOP_PUSH_NONVOL offset = 15-0*/
1199 UNWIND_CODE UnwindCode[MONO_MAX_UNWIND_CODES];
1201 /* UNWIND_CODE MoreUnwindCode[((CountOfCodes + 1) & ~1) - 1];
1203 * OPTIONAL ULONG ExceptionHandler;
1204 * OPTIONAL ULONG FunctionEntry;
1206 * OPTIONAL ULONG ExceptionData[]; */
1207 } UNWIND_INFO, *PUNWIND_INFO;
1211 RUNTIME_FUNCTION runtimeFunction;
1212 UNWIND_INFO unwindInfo;
1213 } MonoUnwindInfo, *PMonoUnwindInfo;
1216 mono_arch_unwindinfo_create (gpointer* monoui)
1218 PMonoUnwindInfo newunwindinfo;
1219 *monoui = newunwindinfo = g_new0 (MonoUnwindInfo, 1);
1220 newunwindinfo->unwindInfo.Version = 1;
1224 mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1226 PMonoUnwindInfo unwindinfo;
1227 PUNWIND_CODE unwindcode;
1230 mono_arch_unwindinfo_create (monoui);
1232 unwindinfo = (MonoUnwindInfo*)*monoui;
1234 if (unwindinfo->unwindInfo.CountOfCodes >= MONO_MAX_UNWIND_CODES)
1235 g_error ("Larger allocation needed for the unwind information.");
1237 codeindex = MONO_MAX_UNWIND_CODES - (++unwindinfo->unwindInfo.CountOfCodes);
1238 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1239 unwindcode->UnwindOp = 0; /*UWOP_PUSH_NONVOL*/
1240 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1241 unwindcode->OpInfo = reg;
1243 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1244 g_error ("Adding unwind info in wrong order.");
1246 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1250 mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1252 PMonoUnwindInfo unwindinfo;
1253 PUNWIND_CODE unwindcode;
1256 mono_arch_unwindinfo_create (monoui);
1258 unwindinfo = (MonoUnwindInfo*)*monoui;
1260 if (unwindinfo->unwindInfo.CountOfCodes + 1 >= MONO_MAX_UNWIND_CODES)
1261 g_error ("Larger allocation needed for the unwind information.");
1263 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += 2);
1264 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1265 unwindcode->FrameOffset = 0; /*Assuming no frame pointer offset for mono*/
1267 unwindcode->UnwindOp = 3; /*UWOP_SET_FPREG*/
1268 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1269 unwindcode->OpInfo = reg;
1271 unwindinfo->unwindInfo.FrameRegister = reg;
1273 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1274 g_error ("Adding unwind info in wrong order.");
1276 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1280 mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size )
1282 PMonoUnwindInfo unwindinfo;
1283 PUNWIND_CODE unwindcode;
1287 mono_arch_unwindinfo_create (monoui);
1289 unwindinfo = (MonoUnwindInfo*)*monoui;
1292 g_error ("Stack allocation must be equal to or greater than 0x8.");
1296 else if (size <= 0x7FFF8)
1301 if (unwindinfo->unwindInfo.CountOfCodes + codesneeded > MONO_MAX_UNWIND_CODES)
1302 g_error ("Larger allocation needed for the unwind information.");
1304 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += codesneeded);
1305 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1307 if (codesneeded == 1) {
1308 /*The size of the allocation is
1309 (the number in the OpInfo member) times 8 plus 8*/
1310 unwindcode->OpInfo = (size - 8)/8;
1311 unwindcode->UnwindOp = 2; /*UWOP_ALLOC_SMALL*/
1314 if (codesneeded == 3) {
1315 /*the unscaled size of the allocation is recorded
1316 in the next two slots in little-endian format*/
1317 *((unsigned int*)(&unwindcode->FrameOffset)) = size;
1319 unwindcode->OpInfo = 1;
1322 /*the size of the allocation divided by 8
1323 is recorded in the next slot*/
1324 unwindcode->FrameOffset = size/8;
1326 unwindcode->OpInfo = 0;
1329 unwindcode->UnwindOp = 1; /*UWOP_ALLOC_LARGE*/
1332 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1334 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1335 g_error ("Adding unwind info in wrong order.");
1337 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1341 mono_arch_unwindinfo_get_size (gpointer monoui)
1343 PMonoUnwindInfo unwindinfo;
1347 unwindinfo = (MonoUnwindInfo*)monoui;
1348 return (8 + sizeof (MonoUnwindInfo)) -
1349 (sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
1353 MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
1357 PMonoUnwindInfo targetinfo;
1358 MonoDomain *domain = mono_domain_get ();
1360 ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
1364 pos = (guint64)(((char*)ji->code_start) + ji->code_size);
1366 targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
1368 targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
1370 return &targetinfo->runtimeFunction;
1374 mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
1376 PMonoUnwindInfo unwindinfo, targetinfo;
1378 guint64 targetlocation;
1382 unwindinfo = (MonoUnwindInfo*)*monoui;
1383 targetlocation = (guint64)&(((guchar*)code)[code_size]);
1384 targetinfo = (PMonoUnwindInfo) ALIGN_TO(targetlocation, 8);
1386 unwindinfo->runtimeFunction.EndAddress = code_size;
1387 unwindinfo->runtimeFunction.UnwindData = ((guchar*)&targetinfo->unwindInfo) - ((guchar*)code);
1389 memcpy (targetinfo, unwindinfo, sizeof (MonoUnwindInfo) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
1391 codecount = unwindinfo->unwindInfo.CountOfCodes;
1393 memcpy (&targetinfo->unwindInfo.UnwindCode[0], &unwindinfo->unwindInfo.UnwindCode[MONO_MAX_UNWIND_CODES-codecount],
1394 sizeof (UNWIND_CODE) * unwindinfo->unwindInfo.CountOfCodes);
1397 g_free (unwindinfo);
1400 RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
1405 #if MONO_SUPPORT_TASKLETS
1406 MonoContinuationRestore
1407 mono_tasklets_arch_restore (void)
1409 static guint8* saved = NULL;
1410 guint8 *code, *start;
1411 int cont_reg = AMD64_R9; /* register usable on both call conventions */
1412 const guint kMaxCodeSize = NACL_SIZE (64, 128);
1416 return (MonoContinuationRestore)saved;
1417 code = start = mono_global_codeman_reserve (kMaxCodeSize);
1418 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1419 /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
1420 * state is in AMD64_ARG_REG2 ($rdx or $rsi)
1421 * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
1422 * We move cont to cont_reg since we need both rcx and rdi for the copy
1423 * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
1425 amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
1426 amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
1427 /* setup the copy of the stack */
1428 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
1429 amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
1431 amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
1432 amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
1433 amd64_prefix (code, X86_REP_PREFIX);
1436 /* now restore the registers from the LMF */
1437 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, lmf), 8);
1438 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbx), 8);
1439 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbp), 8);
1440 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
1441 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
1442 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
1443 #if !defined(__native_client_codegen__)
1444 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
1447 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
1448 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
1450 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsp), 8);
1452 /* restore the lmf chain */
1453 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1454 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1456 /* state is already in rax */
1457 amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1458 g_assert ((code - start) <= kMaxCodeSize);
1460 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1463 return (MonoContinuationRestore)saved;
1468 * mono_arch_setup_resume_sighandler_ctx:
1470 * Setup CTX so execution continues at FUNC.
1473 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1476 * When resuming from a signal handler, the stack should be misaligned, just like right after
1479 if ((((guint64)MONO_CONTEXT_GET_SP (ctx)) % 16) == 0)
1480 MONO_CONTEXT_SET_SP (ctx, (guint64)MONO_CONTEXT_GET_SP (ctx) - 8);
1481 MONO_CONTEXT_SET_IP (ctx, func);