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)
17 #ifdef HAVE_UCONTEXT_H
21 #include <mono/arch/amd64/amd64-codegen.h>
22 #include <mono/metadata/abi-details.h>
23 #include <mono/metadata/appdomain.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/threads.h>
26 #include <mono/metadata/threads-types.h>
27 #include <mono/metadata/debug-helpers.h>
28 #include <mono/metadata/exception.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/metadata/mono-debug.h>
31 #include <mono/utils/mono-mmap.h>
34 #include "mini-amd64.h"
37 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
40 static MonoW32ExceptionHandler fpe_handler;
41 static MonoW32ExceptionHandler ill_handler;
42 static MonoW32ExceptionHandler segv_handler;
44 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
45 void *mono_win_vectored_exception_handle;
47 #define W32_SEH_HANDLE_EX(_ex) \
48 if (_ex##_handler) _ex##_handler(0, ep, ctx)
50 static LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
52 #ifndef MONO_CROSS_COMPILE
53 if (mono_old_win_toplevel_exception_filter) {
54 return (*mono_old_win_toplevel_exception_filter)(ep);
58 mono_handle_native_sigsegv (SIGSEGV, NULL);
60 return EXCEPTION_CONTINUE_SEARCH;
64 * Unhandled Exception Filter
65 * Top-level per-process exception handler.
67 static LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
72 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
74 /* If the thread is not managed by the runtime return early */
76 return EXCEPTION_CONTINUE_SEARCH;
78 jit_tls->mono_win_chained_exception_needs_run = FALSE;
79 res = EXCEPTION_CONTINUE_EXECUTION;
81 er = ep->ExceptionRecord;
82 ctx = ep->ContextRecord;
84 switch (er->ExceptionCode) {
85 case EXCEPTION_ACCESS_VIOLATION:
86 W32_SEH_HANDLE_EX(segv);
88 case EXCEPTION_ILLEGAL_INSTRUCTION:
89 W32_SEH_HANDLE_EX(ill);
91 case EXCEPTION_INT_DIVIDE_BY_ZERO:
92 case EXCEPTION_INT_OVERFLOW:
93 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
94 case EXCEPTION_FLT_OVERFLOW:
95 case EXCEPTION_FLT_UNDERFLOW:
96 case EXCEPTION_FLT_INEXACT_RESULT:
97 W32_SEH_HANDLE_EX(fpe);
100 jit_tls->mono_win_chained_exception_needs_run = TRUE;
104 if (jit_tls->mono_win_chained_exception_needs_run) {
105 /* Don't copy context back if we chained exception
106 * as the handler may have modfied the EXCEPTION_POINTERS
107 * directly. We don't pass sigcontext to chained handlers.
108 * Return continue search so the UnhandledExceptionFilter
109 * can correctly chain the exception.
111 res = EXCEPTION_CONTINUE_SEARCH;
117 void win32_seh_init()
119 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_unhandled_exception_filter);
120 mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_handler);
123 void win32_seh_cleanup()
127 if (mono_old_win_toplevel_exception_filter) SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
129 ret = RemoveVectoredExceptionHandler (mono_win_vectored_exception_handle);
133 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
137 fpe_handler = handler;
140 ill_handler = handler;
143 segv_handler = handler;
150 #endif /* TARGET_WIN32 */
153 * mono_arch_get_restore_context:
155 * Returns a pointer to a method which restores a previously saved sigcontext.
158 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
160 guint8 *start = NULL;
162 MonoJumpInfo *ji = NULL;
163 GSList *unwind_ops = NULL;
165 /* restore_contect (MonoContext *ctx) */
167 start = code = mono_global_codeman_reserve (256);
169 amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG1, 8);
171 /* Restore all registers except %rip and %r11 */
172 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rax), 8);
173 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rcx), 8);
174 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rdx), 8);
175 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rbx), 8);
176 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rbp), 8);
177 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rsi), 8);
178 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rdi), 8);
179 //amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, r8), 8);
180 //amd64_mov_reg_membase (code, AMD64_R9, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, r9), 8);
181 //amd64_mov_reg_membase (code, AMD64_R10, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, r10), 8);
182 amd64_mov_reg_membase (code, AMD64_R12, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, r12), 8);
183 amd64_mov_reg_membase (code, AMD64_R13, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, r13), 8);
184 amd64_mov_reg_membase (code, AMD64_R14, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, r14), 8);
185 #if !defined(__native_client_codegen__)
186 amd64_mov_reg_membase (code, AMD64_R15, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, r15), 8);
190 * The context resides on the stack, in the stack frame of the
191 * caller of this function. The stack pointer that we need to
192 * restore is potentially many stack frames higher up, so the
193 * distance between them can easily be more than the red zone
194 * size. Hence the stack pointer can be restored only after
195 * we have finished loading everything from the context.
197 amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rsp), 8);
198 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (MonoContext, rip), 8);
199 amd64_mov_reg_reg (code, AMD64_RSP, AMD64_R8, 8);
201 /* jump to the saved IP */
202 amd64_jump_reg (code, AMD64_R11);
204 nacl_global_codeman_validate(&start, 256, &code);
206 mono_arch_flush_icache (start, code - start);
209 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
215 * mono_arch_get_call_filter:
217 * Returns a pointer to a method which calls an exception filter. We
218 * also use this function to call finally handlers (we pass NULL as
219 * @exc object in this case).
222 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
228 MonoJumpInfo *ji = NULL;
229 GSList *unwind_ops = NULL;
230 const guint kMaxCodeSize = NACL_SIZE (128, 256);
232 start = code = mono_global_codeman_reserve (kMaxCodeSize);
234 /* call_filter (MonoContext *ctx, unsigned long eip) */
237 /* Alloc new frame */
238 amd64_push_reg (code, AMD64_RBP);
239 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
241 /* Save callee saved regs */
243 for (i = 0; i < AMD64_NREG; ++i)
244 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
245 amd64_push_reg (code, i);
251 amd64_push_reg (code, AMD64_RBP);
253 /* Make stack misaligned, the call will make it aligned again */
255 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
258 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, rbp), 8);
259 /* load callee saved regs */
260 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, rbx), 8);
261 amd64_mov_reg_membase (code, AMD64_R12, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, r12), 8);
262 amd64_mov_reg_membase (code, AMD64_R13, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, r13), 8);
263 amd64_mov_reg_membase (code, AMD64_R14, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, r14), 8);
264 #if !defined(__native_client_codegen__)
265 amd64_mov_reg_membase (code, AMD64_R15, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, r15), 8);
268 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, rdi), 8);
269 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoContext, rsi), 8);
272 /* call the handler */
273 amd64_call_reg (code, AMD64_ARG_REG2);
276 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
279 amd64_pop_reg (code, AMD64_RBP);
281 /* Restore callee saved regs */
282 for (i = AMD64_NREG; i >= 0; --i)
283 if (AMD64_IS_CALLEE_SAVED_REG (i))
284 amd64_pop_reg (code, i);
289 g_assert ((code - start) < kMaxCodeSize);
291 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
293 mono_arch_flush_icache (start, code - start);
296 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
302 * The first few arguments are dummy, to force the other arguments to be passed on
303 * the stack, this avoids overwriting the argument registers in the throw trampoline.
306 mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
307 guint64 dummy5, guint64 dummy6,
308 mgreg_t *regs, mgreg_t rip,
309 MonoObject *exc, gboolean rethrow)
313 ctx.rsp = regs [AMD64_RSP];
315 ctx.rbx = regs [AMD64_RBX];
316 ctx.rbp = regs [AMD64_RBP];
317 ctx.r12 = regs [AMD64_R12];
318 ctx.r13 = regs [AMD64_R13];
319 ctx.r14 = regs [AMD64_R14];
320 ctx.r15 = regs [AMD64_R15];
321 ctx.rdi = regs [AMD64_RDI];
322 ctx.rsi = regs [AMD64_RSI];
323 ctx.rax = regs [AMD64_RAX];
324 ctx.rcx = regs [AMD64_RCX];
325 ctx.rdx = regs [AMD64_RDX];
327 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
328 MonoException *mono_ex = (MonoException*)exc;
330 mono_ex->stack_trace = NULL;
333 /* adjust eip so that it point into the call instruction */
336 mono_handle_exception (&ctx, exc);
337 mono_restore_context (&ctx);
338 g_assert_not_reached ();
342 mono_amd64_throw_corlib_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
343 guint64 dummy5, guint64 dummy6,
344 mgreg_t *regs, mgreg_t rip,
345 guint32 ex_token_index, gint64 pc_offset)
347 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
350 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
354 /* Negate the ip adjustment done in mono_amd64_throw_exception () */
357 mono_amd64_throw_exception (dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, regs, rip, (MonoObject*)ex, FALSE);
361 mono_amd64_resume_unwind (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
362 guint64 dummy5, guint64 dummy6,
363 mgreg_t *regs, mgreg_t rip,
364 guint32 dummy7, gint64 dummy8)
366 /* Only the register parameters are valid */
369 ctx.rsp = regs [AMD64_RSP];
371 ctx.rbx = regs [AMD64_RBX];
372 ctx.rbp = regs [AMD64_RBP];
373 ctx.r12 = regs [AMD64_R12];
374 ctx.r13 = regs [AMD64_R13];
375 ctx.r14 = regs [AMD64_R14];
376 ctx.r15 = regs [AMD64_R15];
377 ctx.rdi = regs [AMD64_RDI];
378 ctx.rsi = regs [AMD64_RSI];
379 ctx.rax = regs [AMD64_RAX];
380 ctx.rcx = regs [AMD64_RCX];
381 ctx.rdx = regs [AMD64_RDX];
383 mono_resume_unwind (&ctx);
387 * get_throw_trampoline:
389 * Generate a call to mono_amd64_throw_exception/
390 * mono_amd64_throw_corlib_exception.
393 get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, const char *tramp_name, gboolean aot)
397 MonoJumpInfo *ji = NULL;
398 GSList *unwind_ops = NULL;
399 int i, stack_size, arg_offsets [16], regs_offset, dummy_stack_space;
400 const guint kMaxCodeSize = NACL_SIZE (256, 512);
403 dummy_stack_space = 6 * sizeof(mgreg_t); /* Windows expects stack space allocated for all 6 dummy args. */
405 dummy_stack_space = 0;
408 start = code = mono_global_codeman_reserve (kMaxCodeSize);
410 /* The stack is unaligned on entry */
411 stack_size = 192 + 8 + dummy_stack_space;
416 unwind_ops = mono_arch_get_cie_program ();
419 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_size);
421 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 8);
424 * To hide linux/windows calling convention differences, we pass all arguments on
425 * the stack by passing 6 dummy values in registers.
428 arg_offsets [0] = dummy_stack_space + 0;
429 arg_offsets [1] = dummy_stack_space + sizeof(mgreg_t);
430 arg_offsets [2] = dummy_stack_space + sizeof(mgreg_t) * 2;
431 arg_offsets [3] = dummy_stack_space + sizeof(mgreg_t) * 3;
432 regs_offset = dummy_stack_space + sizeof(mgreg_t) * 4;
435 for (i = 0; i < AMD64_NREG; ++i)
437 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
439 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, stack_size + sizeof(mgreg_t));
440 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (AMD64_RSP * sizeof(mgreg_t)), X86_EAX, sizeof(mgreg_t));
441 /* Set arg1 == regs */
442 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, regs_offset);
443 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [0], AMD64_RAX, sizeof(mgreg_t));
444 /* Set arg2 == eip */
446 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
448 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, stack_size, sizeof(mgreg_t));
449 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [1], AMD64_RAX, sizeof(mgreg_t));
450 /* Set arg3 == exc/ex_token_index */
452 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [2], 0, sizeof(mgreg_t));
454 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [2], AMD64_ARG_REG1, sizeof(mgreg_t));
455 /* Set arg4 == rethrow/pc offset */
457 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], 0, sizeof(mgreg_t));
459 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [3], AMD64_ARG_REG2, sizeof(mgreg_t));
462 * The caller is LLVM code which passes the absolute address not a pc offset,
463 * so compensate by passing 0 as 'rip' and passing the negated abs address as
466 amd64_neg_membase (code, AMD64_RSP, arg_offsets [3]);
468 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], rethrow, sizeof(mgreg_t));
472 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");
473 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
475 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));
477 amd64_call_reg (code, AMD64_R11);
478 amd64_breakpoint (code);
480 mono_arch_flush_icache (start, code - start);
482 g_assert ((code - start) < kMaxCodeSize);
484 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
487 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
493 * mono_arch_get_throw_exception:
495 * Returns a function pointer which can be used to raise
496 * exceptions. The returned function has the following
497 * signature: void (*func) (MonoException *exc);
501 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
503 return get_throw_trampoline (info, FALSE, FALSE, FALSE, FALSE, "throw_exception", aot);
507 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
509 return get_throw_trampoline (info, TRUE, FALSE, FALSE, FALSE, "rethrow_exception", aot);
513 * mono_arch_get_throw_corlib_exception:
515 * Returns a function pointer which can be used to raise
516 * corlib exceptions. The returned function has the following
517 * signature: void (*func) (guint32 ex_token, guint32 offset);
518 * Here, offset is the offset which needs to be substracted from the caller IP
519 * to get the IP of the throw. Passing the offset has the advantage that it
520 * needs no relocations in the caller.
523 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
525 return get_throw_trampoline (info, FALSE, TRUE, FALSE, FALSE, "throw_corlib_exception", aot);
529 * mono_arch_find_jit_info:
531 * This function is used to gather information from @ctx, and store it in @frame_info.
532 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
533 * is modified if needed.
534 * Returns TRUE on success, FALSE otherwise.
537 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
538 MonoJitInfo *ji, MonoContext *ctx,
539 MonoContext *new_ctx, MonoLMF **lmf,
540 mgreg_t **save_locations,
541 StackFrameInfo *frame)
543 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
545 memset (frame, 0, sizeof (StackFrameInfo));
551 mgreg_t regs [MONO_MAX_IREGS + 1];
553 guint32 unwind_info_len;
555 guint8 *epilog = NULL;
557 frame->type = FRAME_TYPE_MANAGED;
559 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
561 frame->unwind_info = unwind_info;
562 frame->unwind_info_len = unwind_info_len;
565 printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
566 mono_print_unwind_info (unwind_info, unwind_info_len);
568 /* LLVM compiled code doesn't have this info */
569 if (ji->has_arch_eh_info)
570 epilog = (guint8*)ji->code_start + ji->code_size - mono_jinfo_get_epilog_size (ji);
572 regs [AMD64_RAX] = new_ctx->rax;
573 regs [AMD64_RBX] = new_ctx->rbx;
574 regs [AMD64_RCX] = new_ctx->rcx;
575 regs [AMD64_RDX] = new_ctx->rdx;
576 regs [AMD64_RBP] = new_ctx->rbp;
577 regs [AMD64_RSP] = new_ctx->rsp;
578 regs [AMD64_RSI] = new_ctx->rsi;
579 regs [AMD64_RDI] = new_ctx->rdi;
580 regs [AMD64_RIP] = new_ctx->rip;
581 regs [AMD64_R12] = new_ctx->r12;
582 regs [AMD64_R13] = new_ctx->r13;
583 regs [AMD64_R14] = new_ctx->r14;
584 regs [AMD64_R15] = new_ctx->r15;
586 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
587 (guint8*)ji->code_start + ji->code_size,
588 ip, epilog ? &epilog : NULL, regs, MONO_MAX_IREGS + 1,
589 save_locations, MONO_MAX_IREGS, &cfa);
591 new_ctx->rax = regs [AMD64_RAX];
592 new_ctx->rbx = regs [AMD64_RBX];
593 new_ctx->rcx = regs [AMD64_RCX];
594 new_ctx->rdx = regs [AMD64_RDX];
595 new_ctx->rbp = regs [AMD64_RBP];
596 new_ctx->rsp = regs [AMD64_RSP];
597 new_ctx->rsi = regs [AMD64_RSI];
598 new_ctx->rdi = regs [AMD64_RDI];
599 new_ctx->rip = regs [AMD64_RIP];
600 new_ctx->r12 = regs [AMD64_R12];
601 new_ctx->r13 = regs [AMD64_R13];
602 new_ctx->r14 = regs [AMD64_R14];
603 new_ctx->r15 = regs [AMD64_R15];
605 /* The CFA becomes the new SP value */
606 new_ctx->rsp = (mgreg_t)cfa;
615 if (((guint64)(*lmf)->previous_lmf) & 2) {
617 * This LMF entry is created by the soft debug code to mark transitions to
618 * managed code done during invokes.
620 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
622 g_assert (ext->debugger_invoke);
624 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
626 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~7);
628 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
633 if (((guint64)(*lmf)->previous_lmf) & 1) {
634 /* This LMF has the rip field set */
636 } else if ((*lmf)->rsp == 0) {
641 * The rsp field is set just before the call which transitioned to native
642 * code. Obtain the rip from the stack.
644 rip = *(guint64*)((*lmf)->rsp - sizeof(mgreg_t));
647 ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
649 * FIXME: ji == NULL can happen when a managed-to-native wrapper is interrupted
650 * in the soft debugger suspend code, since (*lmf)->rsp no longer points to the
661 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
664 new_ctx->rbp = (*lmf)->rbp;
665 new_ctx->rsp = (*lmf)->rsp;
667 if (((guint64)(*lmf)->previous_lmf) & 4) {
668 MonoLMFTramp *ext = (MonoLMFTramp*)(*lmf);
670 /* Trampoline frame */
671 new_ctx->rbx = ext->regs [AMD64_RBX];
672 new_ctx->r12 = ext->regs [AMD64_R12];
673 new_ctx->r13 = ext->regs [AMD64_R13];
674 new_ctx->r14 = ext->regs [AMD64_R14];
675 new_ctx->r15 = ext->regs [AMD64_R15];
677 new_ctx->rdi = ext->regs [AMD64_RDI];
678 new_ctx->rsi = ext->regs [AMD64_RSI];
682 * The registers saved in the LMF will be restored using the normal unwind info,
683 * when the wrapper frame is processed.
696 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~7);
707 * Called by resuming from a signal handler.
710 handle_signal_exception (gpointer obj)
712 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
715 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
717 mono_handle_exception (&ctx, obj);
719 mono_restore_context (&ctx);
723 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
725 guint64 sp = ctx->rsp;
727 ctx->rdi = (guint64)user_data;
729 /* Allocate a stack frame below the red zone */
731 /* The stack should be unaligned */
735 /* Preserve the call chain to prevent crashes in the libgcc unwinder (#15969) */
736 *(guint64*)sp = ctx->rip;
739 ctx->rip = (guint64)async_cb;
743 * mono_arch_handle_exception:
745 * @ctx: saved processor state
746 * @obj: the exception object
749 mono_arch_handle_exception (void *sigctx, gpointer obj)
751 #if defined(MONO_ARCH_USE_SIGACTION)
755 * Handling the exception in the signal handler is problematic, since the original
756 * signal is disabled, and we could run arbitrary code though the debugger. So
757 * resume into the normal stack and do most work there if possible.
759 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
761 /* Pass the ctx parameter in TLS */
762 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
764 mctx = jit_tls->ex_ctx;
765 mono_arch_setup_async_callback (&mctx, handle_signal_exception, obj);
766 mono_monoctx_to_sigctx (&mctx, sigctx);
772 mono_sigctx_to_monoctx (sigctx, &mctx);
774 mono_handle_exception (&mctx, obj);
776 mono_monoctx_to_sigctx (&mctx, sigctx);
783 mono_arch_ip_from_context (void *sigctx)
785 #if defined(MONO_ARCH_USE_SIGACTION)
786 ucontext_t *ctx = (ucontext_t*)sigctx;
788 return (gpointer)UCONTEXT_REG_RIP (ctx);
789 #elif defined(HOST_WIN32)
790 return ((CONTEXT*)sigctx)->Rip;
792 MonoContext *ctx = sigctx;
793 return (gpointer)ctx->rip;
798 restore_soft_guard_pages (void)
800 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
801 if (jit_tls->stack_ovf_guard_base)
802 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
806 * this function modifies mctx so that when it is restored, it
807 * won't execcute starting at mctx.eip, but in a function that
808 * will restore the protection on the soft-guard pages and return back to
809 * continue at mctx.eip.
812 prepare_for_guard_pages (MonoContext *mctx)
815 sp = (gpointer)(mctx->rsp);
817 /* the return addr */
818 sp [0] = (gpointer)(mctx->rip);
819 mctx->rip = (guint64)restore_soft_guard_pages;
820 mctx->rsp = (guint64)sp;
824 altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
830 mono_handle_exception (&mctx, obj);
832 prepare_for_guard_pages (&mctx);
833 mono_restore_context (&mctx);
837 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
839 #if defined(MONO_ARCH_USE_SIGACTION)
840 MonoException *exc = NULL;
841 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_RIP (sigctx), NULL);
844 MonoContext *copied_ctx;
847 exc = mono_domain_get ()->stack_overflow_ex;
849 mono_handle_native_sigsegv (SIGSEGV, sigctx);
851 /* setup a call frame on the real stack so that control is returned there
852 * and exception handling can continue.
853 * The frame looks like:
857 * 128 is the size of the red zone
859 frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4 + 128;
862 sp = (gpointer)(UCONTEXT_REG_RSP (sigctx) & ~15);
863 sp = (gpointer)((char*)sp - frame_size);
864 copied_ctx = (MonoContext*)(sp + 4);
865 /* the arguments must be aligned */
866 sp [-1] = (gpointer)UCONTEXT_REG_RIP (sigctx);
867 mono_sigctx_to_monoctx (sigctx, copied_ctx);
868 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
869 UCONTEXT_REG_RIP (sigctx) = (unsigned long)altstack_handle_and_restore;
870 UCONTEXT_REG_RSP (sigctx) = (unsigned long)(sp - 1);
871 UCONTEXT_REG_RDI (sigctx) = (unsigned long)(copied_ctx);
872 UCONTEXT_REG_RSI (sigctx) = (guint64)exc;
873 UCONTEXT_REG_RDX (sigctx) = stack_ovf;
878 mono_amd64_get_original_ip (void)
880 MonoLMF *lmf = mono_get_lmf ();
884 /* Reset the change to previous_lmf */
885 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf & ~1);
891 mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
893 guint8 *code, *start;
895 gpointer throw_trampoline;
896 MonoJumpInfo *ji = NULL;
897 GSList *unwind_ops = NULL;
898 const guint kMaxCodeSize = NACL_SIZE (128, 256);
900 start = code = mono_global_codeman_reserve (kMaxCodeSize);
902 /* We are in the frame of a managed method after a call */
904 * We would like to throw the pending exception in such a way that it looks to
905 * be thrown from the managed method.
908 /* Save registers which might contain the return value of the call */
909 amd64_push_reg (code, AMD64_RAX);
910 amd64_push_reg (code, AMD64_RDX);
912 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
913 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
916 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
918 /* Obtain the pending exception */
920 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
921 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
923 amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
925 amd64_call_reg (code, AMD64_R11);
927 /* Check if it is NULL, and branch */
928 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
929 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
931 /* exc != NULL branch */
933 /* Save the exc on the stack */
934 amd64_push_reg (code, AMD64_RAX);
936 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
938 /* Obtain the original ip and clear the flag in previous_lmf */
940 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
941 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
943 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
945 amd64_call_reg (code, AMD64_R11);
948 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, 8, 8);
950 /* Pop saved stuff from the stack */
951 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 6 * 8);
953 /* Setup arguments for the throw trampoline */
955 amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_R11, 8);
956 /* The trampoline expects the caller ip to be pushed on the stack */
957 amd64_push_reg (code, AMD64_RAX);
959 /* Call the throw trampoline */
961 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_throw_exception");
962 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
964 throw_trampoline = mono_get_throw_exception ();
965 amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
967 /* We use a jump instead of a call so we can push the original ip on the stack */
968 amd64_jump_reg (code, AMD64_R11);
970 /* ex == NULL branch */
971 mono_amd64_patch (br [0], code);
973 /* Obtain the original ip and clear the flag in previous_lmf */
975 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
976 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
978 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
980 amd64_call_reg (code, AMD64_R11);
981 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
983 /* Restore registers */
984 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
985 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
986 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
987 amd64_pop_reg (code, AMD64_RDX);
988 amd64_pop_reg (code, AMD64_RAX);
990 /* Return to original code */
991 amd64_jump_reg (code, AMD64_R11);
993 g_assert ((code - start) < kMaxCodeSize);
995 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
998 *info = mono_tramp_info_create ("throw_pending_exception", start, code - start, ji, unwind_ops);
1003 static gpointer throw_pending_exception;
1006 * Called when a thread receives an async exception while executing unmanaged code.
1007 * Instead of checking for this exception in the managed-to-native wrapper, we hijack
1008 * the return address on the stack to point to a helper routine which throws the
1012 mono_arch_notify_pending_exc (MonoThreadInfo *info)
1014 MonoLMF *lmf = mono_get_lmf ();
1017 lmf = mono_get_lmf ();
1019 g_assert (info->suspend_state.valid);
1020 lmf = info->suspend_state.unwind_data [MONO_UNWIND_DATA_LMF];
1024 /* Not yet started */
1031 if ((guint64)lmf->previous_lmf & 1)
1032 /* Already hijacked or trampoline LMF entry */
1035 /* lmf->rsp is set just before making the call which transitions to unmanaged code */
1036 lmf->rip = *(guint64*)(lmf->rsp - 8);
1037 /* Signal that lmf->rip is set */
1038 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
1040 *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
1044 mono_amd64_get_exception_trampolines (gboolean aot)
1046 MonoTrampInfo *info;
1047 GSList *tramps = NULL;
1049 mono_arch_get_throw_pending_exception (&info, aot);
1050 tramps = g_slist_prepend (tramps, info);
1052 /* LLVM needs different throw trampolines */
1053 get_throw_trampoline (&info, FALSE, TRUE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", aot);
1054 tramps = g_slist_prepend (tramps, info);
1056 get_throw_trampoline (&info, FALSE, TRUE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", aot);
1057 tramps = g_slist_prepend (tramps, info);
1059 get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", FALSE);
1060 tramps = g_slist_prepend (tramps, info);
1066 mono_arch_exceptions_init (void)
1071 if (mono_aot_only) {
1072 throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
1073 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
1074 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
1075 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
1076 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
1077 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
1078 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
1080 /* Call this to avoid initialization races */
1081 throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
1083 tramps = mono_amd64_get_exception_trampolines (FALSE);
1084 for (l = tramps; l; l = l->next) {
1085 MonoTrampInfo *info = l->data;
1087 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
1088 mono_tramp_info_register (info);
1090 g_slist_free (tramps);
1097 * The mono_arch_unwindinfo* methods are used to build and add
1098 * function table info for each emitted method from mono. On Winx64
1099 * the seh handler will not be called if the mono methods are not
1100 * added to the function table.
1102 * We should not need to add non-volatile register info to the
1103 * table since mono stores that info elsewhere. (Except for the register
1107 #define MONO_MAX_UNWIND_CODES 22
1109 typedef union _UNWIND_CODE {
1112 guchar UnwindOp : 4;
1115 gushort FrameOffset;
1116 } UNWIND_CODE, *PUNWIND_CODE;
1118 typedef struct _UNWIND_INFO {
1121 guchar SizeOfProlog;
1122 guchar CountOfCodes;
1123 guchar FrameRegister : 4;
1124 guchar FrameOffset : 4;
1125 /* custom size for mono allowing for mono allowing for*/
1126 /*UWOP_PUSH_NONVOL ebp offset = 21*/
1127 /*UWOP_ALLOC_LARGE : requires 2 or 3 offset = 20*/
1128 /*UWOP_SET_FPREG : requires 2 offset = 17*/
1129 /*UWOP_PUSH_NONVOL offset = 15-0*/
1130 UNWIND_CODE UnwindCode[MONO_MAX_UNWIND_CODES];
1132 /* UNWIND_CODE MoreUnwindCode[((CountOfCodes + 1) & ~1) - 1];
1134 * OPTIONAL ULONG ExceptionHandler;
1135 * OPTIONAL ULONG FunctionEntry;
1137 * OPTIONAL ULONG ExceptionData[]; */
1138 } UNWIND_INFO, *PUNWIND_INFO;
1142 RUNTIME_FUNCTION runtimeFunction;
1143 UNWIND_INFO unwindInfo;
1144 } MonoUnwindInfo, *PMonoUnwindInfo;
1147 mono_arch_unwindinfo_create (gpointer* monoui)
1149 PMonoUnwindInfo newunwindinfo;
1150 *monoui = newunwindinfo = g_new0 (MonoUnwindInfo, 1);
1151 newunwindinfo->unwindInfo.Version = 1;
1155 mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1157 PMonoUnwindInfo unwindinfo;
1158 PUNWIND_CODE unwindcode;
1161 mono_arch_unwindinfo_create (monoui);
1163 unwindinfo = (MonoUnwindInfo*)*monoui;
1165 if (unwindinfo->unwindInfo.CountOfCodes >= MONO_MAX_UNWIND_CODES)
1166 g_error ("Larger allocation needed for the unwind information.");
1168 codeindex = MONO_MAX_UNWIND_CODES - (++unwindinfo->unwindInfo.CountOfCodes);
1169 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1170 unwindcode->UnwindOp = 0; /*UWOP_PUSH_NONVOL*/
1171 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1172 unwindcode->OpInfo = reg;
1174 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1175 g_error ("Adding unwind info in wrong order.");
1177 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1181 mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1183 PMonoUnwindInfo unwindinfo;
1184 PUNWIND_CODE unwindcode;
1187 mono_arch_unwindinfo_create (monoui);
1189 unwindinfo = (MonoUnwindInfo*)*monoui;
1191 if (unwindinfo->unwindInfo.CountOfCodes + 1 >= MONO_MAX_UNWIND_CODES)
1192 g_error ("Larger allocation needed for the unwind information.");
1194 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += 2);
1195 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1196 unwindcode->FrameOffset = 0; /*Assuming no frame pointer offset for mono*/
1198 unwindcode->UnwindOp = 3; /*UWOP_SET_FPREG*/
1199 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1200 unwindcode->OpInfo = reg;
1202 unwindinfo->unwindInfo.FrameRegister = reg;
1204 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1205 g_error ("Adding unwind info in wrong order.");
1207 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1211 mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size )
1213 PMonoUnwindInfo unwindinfo;
1214 PUNWIND_CODE unwindcode;
1218 mono_arch_unwindinfo_create (monoui);
1220 unwindinfo = (MonoUnwindInfo*)*monoui;
1223 g_error ("Stack allocation must be equal to or greater than 0x8.");
1227 else if (size <= 0x7FFF8)
1232 if (unwindinfo->unwindInfo.CountOfCodes + codesneeded > MONO_MAX_UNWIND_CODES)
1233 g_error ("Larger allocation needed for the unwind information.");
1235 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += codesneeded);
1236 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1238 if (codesneeded == 1) {
1239 /*The size of the allocation is
1240 (the number in the OpInfo member) times 8 plus 8*/
1241 unwindcode->OpInfo = (size - 8)/8;
1242 unwindcode->UnwindOp = 2; /*UWOP_ALLOC_SMALL*/
1245 if (codesneeded == 3) {
1246 /*the unscaled size of the allocation is recorded
1247 in the next two slots in little-endian format*/
1248 *((unsigned int*)(&unwindcode->FrameOffset)) = size;
1250 unwindcode->OpInfo = 1;
1253 /*the size of the allocation divided by 8
1254 is recorded in the next slot*/
1255 unwindcode->FrameOffset = size/8;
1257 unwindcode->OpInfo = 0;
1260 unwindcode->UnwindOp = 1; /*UWOP_ALLOC_LARGE*/
1263 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1265 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1266 g_error ("Adding unwind info in wrong order.");
1268 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1272 mono_arch_unwindinfo_get_size (gpointer monoui)
1274 PMonoUnwindInfo unwindinfo;
1278 unwindinfo = (MonoUnwindInfo*)monoui;
1279 return (8 + sizeof (MonoUnwindInfo)) -
1280 (sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
1283 static PRUNTIME_FUNCTION
1284 MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
1288 PMonoUnwindInfo targetinfo;
1289 MonoDomain *domain = mono_domain_get ();
1291 ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
1295 pos = (guint64)(((char*)ji->code_start) + ji->code_size);
1297 targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
1299 targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
1301 return &targetinfo->runtimeFunction;
1305 mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
1307 PMonoUnwindInfo unwindinfo, targetinfo;
1309 guint64 targetlocation;
1313 unwindinfo = (MonoUnwindInfo*)*monoui;
1314 targetlocation = (guint64)&(((guchar*)code)[code_size]);
1315 targetinfo = (PMonoUnwindInfo) ALIGN_TO(targetlocation, 8);
1317 unwindinfo->runtimeFunction.EndAddress = code_size;
1318 unwindinfo->runtimeFunction.UnwindData = ((guchar*)&targetinfo->unwindInfo) - ((guchar*)code);
1320 memcpy (targetinfo, unwindinfo, sizeof (MonoUnwindInfo) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
1322 codecount = unwindinfo->unwindInfo.CountOfCodes;
1324 memcpy (&targetinfo->unwindInfo.UnwindCode[0], &unwindinfo->unwindInfo.UnwindCode[MONO_MAX_UNWIND_CODES-codecount],
1325 sizeof (UNWIND_CODE) * unwindinfo->unwindInfo.CountOfCodes);
1328 g_free (unwindinfo);
1331 RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
1336 #if MONO_SUPPORT_TASKLETS
1337 MonoContinuationRestore
1338 mono_tasklets_arch_restore (void)
1340 static guint8* saved = NULL;
1341 guint8 *code, *start;
1342 int cont_reg = AMD64_R9; /* register usable on both call conventions */
1343 const guint kMaxCodeSize = NACL_SIZE (64, 128);
1347 return (MonoContinuationRestore)saved;
1348 code = start = mono_global_codeman_reserve (kMaxCodeSize);
1349 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1350 /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
1351 * state is in AMD64_ARG_REG2 ($rdx or $rsi)
1352 * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
1353 * We move cont to cont_reg since we need both rcx and rdi for the copy
1354 * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
1356 amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
1357 amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
1358 /* setup the copy of the stack */
1359 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
1360 amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
1362 amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
1363 amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
1364 amd64_prefix (code, X86_REP_PREFIX);
1367 /* now restore the registers from the LMF */
1369 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, lmf), 8);
1370 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, MONO_STRUCT_OFFSET (MonoLMF, rsp), 8);
1372 /* restore the lmf chain */
1373 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1374 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1376 /* state is already in rax */
1377 amd64_jump_membase (code, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, return_ip));
1378 g_assert ((code - start) <= kMaxCodeSize);
1380 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1383 return (MonoContinuationRestore)saved;
1388 * mono_arch_setup_resume_sighandler_ctx:
1390 * Setup CTX so execution continues at FUNC.
1393 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1396 * When resuming from a signal handler, the stack should be misaligned, just like right after
1399 if ((((guint64)MONO_CONTEXT_GET_SP (ctx)) % 16) == 0)
1400 MONO_CONTEXT_SET_SP (ctx, (guint64)MONO_CONTEXT_GET_SP (ctx) - 8);
1401 MONO_CONTEXT_SET_IP (ctx, func);