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;
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 epilog = (guint8*)ji->code_start + ji->code_size - mono_jinfo_get_epilog_size (ji);
570 regs [AMD64_RAX] = new_ctx->rax;
571 regs [AMD64_RBX] = new_ctx->rbx;
572 regs [AMD64_RCX] = new_ctx->rcx;
573 regs [AMD64_RDX] = new_ctx->rdx;
574 regs [AMD64_RBP] = new_ctx->rbp;
575 regs [AMD64_RSP] = new_ctx->rsp;
576 regs [AMD64_RSI] = new_ctx->rsi;
577 regs [AMD64_RDI] = new_ctx->rdi;
578 regs [AMD64_RIP] = new_ctx->rip;
579 regs [AMD64_R12] = new_ctx->r12;
580 regs [AMD64_R13] = new_ctx->r13;
581 regs [AMD64_R14] = new_ctx->r14;
582 regs [AMD64_R15] = new_ctx->r15;
584 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
585 (guint8*)ji->code_start + ji->code_size,
586 ip, &epilog, regs, MONO_MAX_IREGS + 1,
587 save_locations, MONO_MAX_IREGS, &cfa);
589 new_ctx->rax = regs [AMD64_RAX];
590 new_ctx->rbx = regs [AMD64_RBX];
591 new_ctx->rcx = regs [AMD64_RCX];
592 new_ctx->rdx = regs [AMD64_RDX];
593 new_ctx->rbp = regs [AMD64_RBP];
594 new_ctx->rsp = regs [AMD64_RSP];
595 new_ctx->rsi = regs [AMD64_RSI];
596 new_ctx->rdi = regs [AMD64_RDI];
597 new_ctx->rip = regs [AMD64_RIP];
598 new_ctx->r12 = regs [AMD64_R12];
599 new_ctx->r13 = regs [AMD64_R13];
600 new_ctx->r14 = regs [AMD64_R14];
601 new_ctx->r15 = regs [AMD64_R15];
603 /* The CFA becomes the new SP value */
604 new_ctx->rsp = (mgreg_t)cfa;
609 #ifndef MONO_AMD64_NO_PUSHES
610 /* Pop arguments off the stack */
611 if (ji->has_arch_eh_info)
612 new_ctx->rsp += mono_jit_info_get_arch_eh_info (ji)->stack_size;
619 if (((guint64)(*lmf)->previous_lmf) & 2) {
621 * This LMF entry is created by the soft debug code to mark transitions to
622 * managed code done during invokes.
624 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
626 g_assert (ext->debugger_invoke);
628 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
630 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~7);
632 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
637 if (((guint64)(*lmf)->previous_lmf) & 1) {
638 /* This LMF has the rip field set */
640 } else if ((*lmf)->rsp == 0) {
645 * The rsp field is set just before the call which transitioned to native
646 * code. Obtain the rip from the stack.
648 rip = *(guint64*)((*lmf)->rsp - sizeof(mgreg_t));
651 ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
653 * FIXME: ji == NULL can happen when a managed-to-native wrapper is interrupted
654 * in the soft debugger suspend code, since (*lmf)->rsp no longer points to the
665 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
668 new_ctx->rbp = (*lmf)->rbp;
669 new_ctx->rsp = (*lmf)->rsp;
671 if (((guint64)(*lmf)->previous_lmf) & 4) {
672 MonoLMFTramp *ext = (MonoLMFTramp*)(*lmf);
674 /* Trampoline frame */
675 new_ctx->rbx = ext->regs [AMD64_RBX];
676 new_ctx->r12 = ext->regs [AMD64_R12];
677 new_ctx->r13 = ext->regs [AMD64_R13];
678 new_ctx->r14 = ext->regs [AMD64_R14];
679 new_ctx->r15 = ext->regs [AMD64_R15];
681 new_ctx->rdi = ext->regs [AMD64_RDI];
682 new_ctx->rsi = ext->regs [AMD64_RSI];
686 * The registers saved in the LMF will be restored using the normal unwind info,
687 * when the wrapper frame is processed.
700 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~7);
711 * Called by resuming from a signal handler.
714 handle_signal_exception (gpointer obj)
716 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
719 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
721 mono_handle_exception (&ctx, obj);
723 mono_restore_context (&ctx);
727 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
729 guint64 sp = ctx->rsp;
731 ctx->rdi = (guint64)user_data;
733 /* Allocate a stack frame below the red zone */
735 /* The stack should be unaligned */
739 /* Preserve the call chain to prevent crashes in the libgcc unwinder (#15969) */
740 *(guint64*)sp = ctx->rip;
743 ctx->rip = (guint64)async_cb;
747 * mono_arch_handle_exception:
749 * @ctx: saved processor state
750 * @obj: the exception object
753 mono_arch_handle_exception (void *sigctx, gpointer obj)
755 #if defined(MONO_ARCH_USE_SIGACTION)
759 * Handling the exception in the signal handler is problematic, since the original
760 * signal is disabled, and we could run arbitrary code though the debugger. So
761 * resume into the normal stack and do most work there if possible.
763 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
765 /* Pass the ctx parameter in TLS */
766 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
768 mctx = jit_tls->ex_ctx;
769 mono_arch_setup_async_callback (&mctx, handle_signal_exception, obj);
770 mono_monoctx_to_sigctx (&mctx, sigctx);
776 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
778 mono_handle_exception (&mctx, obj);
780 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
787 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
789 mono_sigctx_to_monoctx (sigctx, mctx);
793 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
795 mono_monoctx_to_sigctx (mctx, sigctx);
799 mono_arch_ip_from_context (void *sigctx)
801 #if defined(MONO_ARCH_USE_SIGACTION)
802 ucontext_t *ctx = (ucontext_t*)sigctx;
804 return (gpointer)UCONTEXT_REG_RIP (ctx);
805 #elif defined(HOST_WIN32)
806 return ((CONTEXT*)sigctx)->Rip;
808 MonoContext *ctx = sigctx;
809 return (gpointer)ctx->rip;
814 restore_soft_guard_pages (void)
816 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
817 if (jit_tls->stack_ovf_guard_base)
818 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
822 * this function modifies mctx so that when it is restored, it
823 * won't execcute starting at mctx.eip, but in a function that
824 * will restore the protection on the soft-guard pages and return back to
825 * continue at mctx.eip.
828 prepare_for_guard_pages (MonoContext *mctx)
831 sp = (gpointer)(mctx->rsp);
833 /* the return addr */
834 sp [0] = (gpointer)(mctx->rip);
835 mctx->rip = (guint64)restore_soft_guard_pages;
836 mctx->rsp = (guint64)sp;
840 altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
846 mono_handle_exception (&mctx, obj);
848 prepare_for_guard_pages (&mctx);
849 mono_restore_context (&mctx);
853 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
855 #if defined(MONO_ARCH_USE_SIGACTION)
856 MonoException *exc = NULL;
857 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_RIP (sigctx), NULL);
860 MonoContext *copied_ctx;
863 exc = mono_domain_get ()->stack_overflow_ex;
865 mono_handle_native_sigsegv (SIGSEGV, sigctx);
867 /* setup a call frame on the real stack so that control is returned there
868 * and exception handling can continue.
869 * The frame looks like:
873 * 128 is the size of the red zone
875 frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4 + 128;
878 sp = (gpointer)(UCONTEXT_REG_RSP (sigctx) & ~15);
879 sp = (gpointer)((char*)sp - frame_size);
880 copied_ctx = (MonoContext*)(sp + 4);
881 /* the arguments must be aligned */
882 sp [-1] = (gpointer)UCONTEXT_REG_RIP (sigctx);
883 mono_sigctx_to_monoctx (sigctx, copied_ctx);
884 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
885 UCONTEXT_REG_RIP (sigctx) = (unsigned long)altstack_handle_and_restore;
886 UCONTEXT_REG_RSP (sigctx) = (unsigned long)(sp - 1);
887 UCONTEXT_REG_RDI (sigctx) = (unsigned long)(copied_ctx);
888 UCONTEXT_REG_RSI (sigctx) = (guint64)exc;
889 UCONTEXT_REG_RDX (sigctx) = stack_ovf;
894 mono_amd64_get_original_ip (void)
896 MonoLMF *lmf = mono_get_lmf ();
900 /* Reset the change to previous_lmf */
901 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf & ~1);
907 mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
909 guint8 *code, *start;
911 gpointer throw_trampoline;
912 MonoJumpInfo *ji = NULL;
913 GSList *unwind_ops = NULL;
914 const guint kMaxCodeSize = NACL_SIZE (128, 256);
916 start = code = mono_global_codeman_reserve (kMaxCodeSize);
918 /* We are in the frame of a managed method after a call */
920 * We would like to throw the pending exception in such a way that it looks to
921 * be thrown from the managed method.
924 /* Save registers which might contain the return value of the call */
925 amd64_push_reg (code, AMD64_RAX);
926 amd64_push_reg (code, AMD64_RDX);
928 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
929 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
932 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
934 /* Obtain the pending exception */
936 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
937 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
939 amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
941 amd64_call_reg (code, AMD64_R11);
943 /* Check if it is NULL, and branch */
944 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
945 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
947 /* exc != NULL branch */
949 /* Save the exc on the stack */
950 amd64_push_reg (code, AMD64_RAX);
952 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
954 /* Obtain the original ip and clear the flag in previous_lmf */
956 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
957 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
959 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
961 amd64_call_reg (code, AMD64_R11);
964 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, 8, 8);
966 /* Pop saved stuff from the stack */
967 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 6 * 8);
969 /* Setup arguments for the throw trampoline */
971 amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_R11, 8);
972 /* The trampoline expects the caller ip to be pushed on the stack */
973 amd64_push_reg (code, AMD64_RAX);
975 /* Call the throw trampoline */
977 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_throw_exception");
978 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
980 throw_trampoline = mono_get_throw_exception ();
981 amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
983 /* We use a jump instead of a call so we can push the original ip on the stack */
984 amd64_jump_reg (code, AMD64_R11);
986 /* ex == NULL branch */
987 mono_amd64_patch (br [0], code);
989 /* Obtain the original ip and clear the flag in previous_lmf */
991 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
992 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
994 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
996 amd64_call_reg (code, AMD64_R11);
997 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
999 /* Restore registers */
1000 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1001 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
1002 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1003 amd64_pop_reg (code, AMD64_RDX);
1004 amd64_pop_reg (code, AMD64_RAX);
1006 /* Return to original code */
1007 amd64_jump_reg (code, AMD64_R11);
1009 g_assert ((code - start) < kMaxCodeSize);
1011 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1014 *info = mono_tramp_info_create ("throw_pending_exception", start, code - start, ji, unwind_ops);
1019 static gpointer throw_pending_exception;
1022 * Called when a thread receives an async exception while executing unmanaged code.
1023 * Instead of checking for this exception in the managed-to-native wrapper, we hijack
1024 * the return address on the stack to point to a helper routine which throws the
1028 mono_arch_notify_pending_exc (void)
1030 MonoLMF *lmf = mono_get_lmf ();
1033 /* Not yet started */
1040 if ((guint64)lmf->previous_lmf & 1)
1041 /* Already hijacked or trampoline LMF entry */
1044 /* lmf->rsp is set just before making the call which transitions to unmanaged code */
1045 lmf->rip = *(guint64*)(lmf->rsp - 8);
1046 /* Signal that lmf->rip is set */
1047 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
1049 *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
1053 mono_amd64_get_exception_trampolines (gboolean aot)
1055 MonoTrampInfo *info;
1056 GSList *tramps = NULL;
1058 mono_arch_get_throw_pending_exception (&info, aot);
1059 tramps = g_slist_prepend (tramps, info);
1061 /* LLVM needs different throw trampolines */
1062 get_throw_trampoline (&info, FALSE, TRUE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", aot);
1063 tramps = g_slist_prepend (tramps, info);
1065 get_throw_trampoline (&info, FALSE, TRUE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", aot);
1066 tramps = g_slist_prepend (tramps, info);
1068 get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", FALSE);
1069 tramps = g_slist_prepend (tramps, info);
1075 mono_arch_exceptions_init (void)
1080 if (mono_aot_only) {
1081 throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
1082 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
1083 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
1084 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
1085 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
1086 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
1087 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
1089 /* Call this to avoid initialization races */
1090 throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
1092 tramps = mono_amd64_get_exception_trampolines (FALSE);
1093 for (l = tramps; l; l = l->next) {
1094 MonoTrampInfo *info = l->data;
1096 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
1097 mono_tramp_info_register (info);
1099 g_slist_free (tramps);
1106 * The mono_arch_unwindinfo* methods are used to build and add
1107 * function table info for each emitted method from mono. On Winx64
1108 * the seh handler will not be called if the mono methods are not
1109 * added to the function table.
1111 * We should not need to add non-volatile register info to the
1112 * table since mono stores that info elsewhere. (Except for the register
1116 #define MONO_MAX_UNWIND_CODES 22
1118 typedef union _UNWIND_CODE {
1121 guchar UnwindOp : 4;
1124 gushort FrameOffset;
1125 } UNWIND_CODE, *PUNWIND_CODE;
1127 typedef struct _UNWIND_INFO {
1130 guchar SizeOfProlog;
1131 guchar CountOfCodes;
1132 guchar FrameRegister : 4;
1133 guchar FrameOffset : 4;
1134 /* custom size for mono allowing for mono allowing for*/
1135 /*UWOP_PUSH_NONVOL ebp offset = 21*/
1136 /*UWOP_ALLOC_LARGE : requires 2 or 3 offset = 20*/
1137 /*UWOP_SET_FPREG : requires 2 offset = 17*/
1138 /*UWOP_PUSH_NONVOL offset = 15-0*/
1139 UNWIND_CODE UnwindCode[MONO_MAX_UNWIND_CODES];
1141 /* UNWIND_CODE MoreUnwindCode[((CountOfCodes + 1) & ~1) - 1];
1143 * OPTIONAL ULONG ExceptionHandler;
1144 * OPTIONAL ULONG FunctionEntry;
1146 * OPTIONAL ULONG ExceptionData[]; */
1147 } UNWIND_INFO, *PUNWIND_INFO;
1151 RUNTIME_FUNCTION runtimeFunction;
1152 UNWIND_INFO unwindInfo;
1153 } MonoUnwindInfo, *PMonoUnwindInfo;
1156 mono_arch_unwindinfo_create (gpointer* monoui)
1158 PMonoUnwindInfo newunwindinfo;
1159 *monoui = newunwindinfo = g_new0 (MonoUnwindInfo, 1);
1160 newunwindinfo->unwindInfo.Version = 1;
1164 mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1166 PMonoUnwindInfo unwindinfo;
1167 PUNWIND_CODE unwindcode;
1170 mono_arch_unwindinfo_create (monoui);
1172 unwindinfo = (MonoUnwindInfo*)*monoui;
1174 if (unwindinfo->unwindInfo.CountOfCodes >= MONO_MAX_UNWIND_CODES)
1175 g_error ("Larger allocation needed for the unwind information.");
1177 codeindex = MONO_MAX_UNWIND_CODES - (++unwindinfo->unwindInfo.CountOfCodes);
1178 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1179 unwindcode->UnwindOp = 0; /*UWOP_PUSH_NONVOL*/
1180 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1181 unwindcode->OpInfo = reg;
1183 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1184 g_error ("Adding unwind info in wrong order.");
1186 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1190 mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1192 PMonoUnwindInfo unwindinfo;
1193 PUNWIND_CODE unwindcode;
1196 mono_arch_unwindinfo_create (monoui);
1198 unwindinfo = (MonoUnwindInfo*)*monoui;
1200 if (unwindinfo->unwindInfo.CountOfCodes + 1 >= MONO_MAX_UNWIND_CODES)
1201 g_error ("Larger allocation needed for the unwind information.");
1203 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += 2);
1204 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1205 unwindcode->FrameOffset = 0; /*Assuming no frame pointer offset for mono*/
1207 unwindcode->UnwindOp = 3; /*UWOP_SET_FPREG*/
1208 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1209 unwindcode->OpInfo = reg;
1211 unwindinfo->unwindInfo.FrameRegister = reg;
1213 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1214 g_error ("Adding unwind info in wrong order.");
1216 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1220 mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size )
1222 PMonoUnwindInfo unwindinfo;
1223 PUNWIND_CODE unwindcode;
1227 mono_arch_unwindinfo_create (monoui);
1229 unwindinfo = (MonoUnwindInfo*)*monoui;
1232 g_error ("Stack allocation must be equal to or greater than 0x8.");
1236 else if (size <= 0x7FFF8)
1241 if (unwindinfo->unwindInfo.CountOfCodes + codesneeded > MONO_MAX_UNWIND_CODES)
1242 g_error ("Larger allocation needed for the unwind information.");
1244 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += codesneeded);
1245 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1247 if (codesneeded == 1) {
1248 /*The size of the allocation is
1249 (the number in the OpInfo member) times 8 plus 8*/
1250 unwindcode->OpInfo = (size - 8)/8;
1251 unwindcode->UnwindOp = 2; /*UWOP_ALLOC_SMALL*/
1254 if (codesneeded == 3) {
1255 /*the unscaled size of the allocation is recorded
1256 in the next two slots in little-endian format*/
1257 *((unsigned int*)(&unwindcode->FrameOffset)) = size;
1259 unwindcode->OpInfo = 1;
1262 /*the size of the allocation divided by 8
1263 is recorded in the next slot*/
1264 unwindcode->FrameOffset = size/8;
1266 unwindcode->OpInfo = 0;
1269 unwindcode->UnwindOp = 1; /*UWOP_ALLOC_LARGE*/
1272 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1274 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1275 g_error ("Adding unwind info in wrong order.");
1277 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1281 mono_arch_unwindinfo_get_size (gpointer monoui)
1283 PMonoUnwindInfo unwindinfo;
1287 unwindinfo = (MonoUnwindInfo*)monoui;
1288 return (8 + sizeof (MonoUnwindInfo)) -
1289 (sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
1292 static PRUNTIME_FUNCTION
1293 MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
1297 PMonoUnwindInfo targetinfo;
1298 MonoDomain *domain = mono_domain_get ();
1300 ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
1304 pos = (guint64)(((char*)ji->code_start) + ji->code_size);
1306 targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
1308 targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
1310 return &targetinfo->runtimeFunction;
1314 mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
1316 PMonoUnwindInfo unwindinfo, targetinfo;
1318 guint64 targetlocation;
1322 unwindinfo = (MonoUnwindInfo*)*monoui;
1323 targetlocation = (guint64)&(((guchar*)code)[code_size]);
1324 targetinfo = (PMonoUnwindInfo) ALIGN_TO(targetlocation, 8);
1326 unwindinfo->runtimeFunction.EndAddress = code_size;
1327 unwindinfo->runtimeFunction.UnwindData = ((guchar*)&targetinfo->unwindInfo) - ((guchar*)code);
1329 memcpy (targetinfo, unwindinfo, sizeof (MonoUnwindInfo) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
1331 codecount = unwindinfo->unwindInfo.CountOfCodes;
1333 memcpy (&targetinfo->unwindInfo.UnwindCode[0], &unwindinfo->unwindInfo.UnwindCode[MONO_MAX_UNWIND_CODES-codecount],
1334 sizeof (UNWIND_CODE) * unwindinfo->unwindInfo.CountOfCodes);
1337 g_free (unwindinfo);
1340 RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
1345 #if MONO_SUPPORT_TASKLETS
1346 MonoContinuationRestore
1347 mono_tasklets_arch_restore (void)
1349 static guint8* saved = NULL;
1350 guint8 *code, *start;
1351 int cont_reg = AMD64_R9; /* register usable on both call conventions */
1352 const guint kMaxCodeSize = NACL_SIZE (64, 128);
1356 return (MonoContinuationRestore)saved;
1357 code = start = mono_global_codeman_reserve (kMaxCodeSize);
1358 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1359 /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
1360 * state is in AMD64_ARG_REG2 ($rdx or $rsi)
1361 * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
1362 * We move cont to cont_reg since we need both rcx and rdi for the copy
1363 * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
1365 amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
1366 amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
1367 /* setup the copy of the stack */
1368 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
1369 amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
1371 amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
1372 amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
1373 amd64_prefix (code, X86_REP_PREFIX);
1376 /* now restore the registers from the LMF */
1378 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, lmf), 8);
1379 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, MONO_STRUCT_OFFSET (MonoLMF, rsp), 8);
1381 /* restore the lmf chain */
1382 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1383 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1385 /* state is already in rax */
1386 amd64_jump_membase (code, cont_reg, MONO_STRUCT_OFFSET (MonoContinuation, return_ip));
1387 g_assert ((code - start) <= kMaxCodeSize);
1389 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1392 return (MonoContinuationRestore)saved;
1397 * mono_arch_setup_resume_sighandler_ctx:
1399 * Setup CTX so execution continues at FUNC.
1402 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1405 * When resuming from a signal handler, the stack should be misaligned, just like right after
1408 if ((((guint64)MONO_CONTEXT_GET_SP (ctx)) % 16) == 0)
1409 MONO_CONTEXT_SET_SP (ctx, (guint64)MONO_CONTEXT_GET_SP (ctx) - 8);
1410 MONO_CONTEXT_SET_IP (ctx, func);