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)
15 #ifdef HAVE_UCONTEXT_H
19 #include <mono/arch/amd64/amd64-codegen.h>
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/threads.h>
23 #include <mono/metadata/threads-types.h>
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/gc-internal.h>
27 #include <mono/metadata/mono-debug.h>
28 #include <mono/utils/mono-mmap.h>
31 #include "mini-amd64.h"
33 #include "debug-mini.h"
35 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
38 static MonoW32ExceptionHandler fpe_handler;
39 static MonoW32ExceptionHandler ill_handler;
40 static MonoW32ExceptionHandler segv_handler;
42 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
43 guint64 mono_win_chained_exception_filter_result;
44 gboolean mono_win_chained_exception_filter_didrun;
46 #define W32_SEH_HANDLE_EX(_ex) \
47 if (_ex##_handler) _ex##_handler(0, ep, sctx)
50 * Unhandled Exception Filter
51 * Top-level per-process exception handler.
53 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
60 mono_win_chained_exception_filter_didrun = FALSE;
61 res = EXCEPTION_CONTINUE_EXECUTION;
63 er = ep->ExceptionRecord;
64 ctx = ep->ContextRecord;
65 sctx = g_malloc(sizeof(MonoContext));
67 /* Copy Win32 context to UNIX style context */
82 switch (er->ExceptionCode) {
83 case EXCEPTION_ACCESS_VIOLATION:
84 W32_SEH_HANDLE_EX(segv);
86 case EXCEPTION_ILLEGAL_INSTRUCTION:
87 W32_SEH_HANDLE_EX(ill);
89 case EXCEPTION_INT_DIVIDE_BY_ZERO:
90 case EXCEPTION_INT_OVERFLOW:
91 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
92 case EXCEPTION_FLT_OVERFLOW:
93 case EXCEPTION_FLT_UNDERFLOW:
94 case EXCEPTION_FLT_INEXACT_RESULT:
95 W32_SEH_HANDLE_EX(fpe);
101 /* Copy context back */
103 ctx->Rsp = sctx->rsp;
104 ctx->Rdi = sctx->rdi;
105 ctx->Rsi = sctx->rsi;
106 ctx->Rbx = sctx->rbx;
107 ctx->Rbp = sctx->rbp;
108 ctx->R12 = sctx->r12;
109 ctx->R13 = sctx->r13;
110 ctx->R14 = sctx->r14;
111 ctx->R15 = sctx->r15;
112 ctx->Rip = sctx->rip;
114 /* Volatile But should not matter?*/
115 ctx->Rax = sctx->rax;
116 ctx->Rcx = sctx->rcx;
117 ctx->Rdx = sctx->rdx;
121 if (mono_win_chained_exception_filter_didrun)
122 res = mono_win_chained_exception_filter_result;
127 void win32_seh_init()
129 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_handler);
132 void win32_seh_cleanup()
134 if (mono_old_win_toplevel_exception_filter) SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
137 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
141 fpe_handler = handler;
144 ill_handler = handler;
147 segv_handler = handler;
154 #endif /* TARGET_WIN32 */
157 * mono_arch_get_restore_context:
159 * Returns a pointer to a method which restores a previously saved sigcontext.
162 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
164 guint8 *start = NULL;
166 MonoJumpInfo *ji = NULL;
167 GSList *unwind_ops = NULL;
169 /* restore_contect (MonoContext *ctx) */
171 start = code = mono_global_codeman_reserve (256);
173 amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG1, 8);
175 /* Restore all registers except %rip and %r11 */
176 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rax), 8);
177 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rcx), 8);
178 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rdx), 8);
179 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rbx), 8);
180 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rbp), 8);
181 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsi), 8);
182 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rdi), 8);
183 //amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r8), 8);
184 //amd64_mov_reg_membase (code, AMD64_R9, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r9), 8);
185 //amd64_mov_reg_membase (code, AMD64_R10, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r10), 8);
186 amd64_mov_reg_membase (code, AMD64_R12, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r12), 8);
187 amd64_mov_reg_membase (code, AMD64_R13, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r13), 8);
188 amd64_mov_reg_membase (code, AMD64_R14, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r14), 8);
189 #if !defined(__native_client_codegen__)
190 amd64_mov_reg_membase (code, AMD64_R15, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r15), 8);
193 if (mono_running_on_valgrind ()) {
194 /* Prevent 'Address 0x... is just below the stack ptr.' errors */
195 amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
196 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
197 amd64_mov_reg_reg (code, AMD64_RSP, AMD64_R8, 8);
199 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
200 /* get return address */
201 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
204 /* jump to the saved IP */
205 amd64_jump_reg (code, AMD64_R11);
207 nacl_global_codeman_validate(&start, 256, &code);
209 mono_arch_flush_icache (start, code - start);
212 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
218 * mono_arch_get_call_filter:
220 * Returns a pointer to a method which calls an exception filter. We
221 * also use this function to call finally handlers (we pass NULL as
222 * @exc object in this case).
225 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
231 MonoJumpInfo *ji = NULL;
232 GSList *unwind_ops = NULL;
233 const guint kMaxCodeSize = NACL_SIZE (128, 256);
235 start = code = mono_global_codeman_reserve (kMaxCodeSize);
237 /* call_filter (MonoContext *ctx, unsigned long eip) */
240 /* Alloc new frame */
241 amd64_push_reg (code, AMD64_RBP);
242 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
244 /* Save callee saved regs */
246 for (i = 0; i < AMD64_NREG; ++i)
247 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
248 amd64_push_reg (code, i);
254 amd64_push_reg (code, AMD64_RBP);
256 /* Make stack misaligned, the call will make it aligned again */
258 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
261 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbp), 8);
262 /* load callee saved regs */
263 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbx), 8);
264 amd64_mov_reg_membase (code, AMD64_R12, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r12), 8);
265 amd64_mov_reg_membase (code, AMD64_R13, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r13), 8);
266 amd64_mov_reg_membase (code, AMD64_R14, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r14), 8);
267 #if !defined(__native_client_codegen__)
268 amd64_mov_reg_membase (code, AMD64_R15, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r15), 8);
271 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rdi), 8);
272 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rsi), 8);
275 /* call the handler */
276 amd64_call_reg (code, AMD64_ARG_REG2);
279 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
282 amd64_pop_reg (code, AMD64_RBP);
284 /* Restore callee saved regs */
285 for (i = AMD64_NREG; i >= 0; --i)
286 if (AMD64_IS_CALLEE_SAVED_REG (i))
287 amd64_pop_reg (code, i);
292 g_assert ((code - start) < kMaxCodeSize);
294 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
296 mono_arch_flush_icache (start, code - start);
299 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
305 * The first few arguments are dummy, to force the other arguments to be passed on
306 * the stack, this avoids overwriting the argument registers in the throw trampoline.
309 mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
310 guint64 dummy5, guint64 dummy6,
311 mgreg_t *regs, mgreg_t rip,
312 MonoObject *exc, gboolean rethrow)
314 static void (*restore_context) (MonoContext *);
317 if (!restore_context)
318 restore_context = mono_get_restore_context ();
320 ctx.rsp = regs [AMD64_RSP];
322 ctx.rbx = regs [AMD64_RBX];
323 ctx.rbp = regs [AMD64_RBP];
324 ctx.r12 = regs [AMD64_R12];
325 ctx.r13 = regs [AMD64_R13];
326 ctx.r14 = regs [AMD64_R14];
327 ctx.r15 = regs [AMD64_R15];
328 ctx.rdi = regs [AMD64_RDI];
329 ctx.rsi = regs [AMD64_RSI];
330 ctx.rax = regs [AMD64_RAX];
331 ctx.rcx = regs [AMD64_RCX];
332 ctx.rdx = regs [AMD64_RDX];
334 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
335 MonoException *mono_ex = (MonoException*)exc;
337 mono_ex->stack_trace = NULL;
340 if (mono_debug_using_mono_debugger ()) {
343 mono_breakpoint_clean_code (NULL, (gpointer)rip, 8, buf, sizeof (buf));
345 if (buf [3] == 0xe8) {
346 MonoContext ctx_cp = ctx;
347 ctx_cp.rip = rip - 5;
349 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
350 restore_context (&ctx_cp);
351 g_assert_not_reached ();
356 /* adjust eip so that it point into the call instruction */
359 mono_handle_exception (&ctx, exc);
360 restore_context (&ctx);
362 g_assert_not_reached ();
366 mono_amd64_throw_corlib_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
367 guint64 dummy5, guint64 dummy6,
368 mgreg_t *regs, mgreg_t rip,
369 guint32 ex_token_index, gint64 pc_offset)
371 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
374 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
378 /* Negate the ip adjustment done in mono_amd64_throw_exception () */
381 mono_amd64_throw_exception (dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, regs, rip, (MonoObject*)ex, FALSE);
385 mono_amd64_resume_unwind (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
386 guint64 dummy5, guint64 dummy6,
387 mgreg_t *regs, mgreg_t rip,
388 guint32 dummy7, gint64 dummy8)
390 /* Only the register parameters are valid */
393 ctx.rsp = regs [AMD64_RSP];
395 ctx.rbx = regs [AMD64_RBX];
396 ctx.rbp = regs [AMD64_RBP];
397 ctx.r12 = regs [AMD64_R12];
398 ctx.r13 = regs [AMD64_R13];
399 ctx.r14 = regs [AMD64_R14];
400 ctx.r15 = regs [AMD64_R15];
401 ctx.rdi = regs [AMD64_RDI];
402 ctx.rsi = regs [AMD64_RSI];
403 ctx.rax = regs [AMD64_RAX];
404 ctx.rcx = regs [AMD64_RCX];
405 ctx.rdx = regs [AMD64_RDX];
407 mono_resume_unwind (&ctx);
411 * get_throw_trampoline:
413 * Generate a call to mono_amd64_throw_exception/
414 * mono_amd64_throw_corlib_exception.
417 get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, const char *tramp_name, gboolean aot)
421 MonoJumpInfo *ji = NULL;
422 GSList *unwind_ops = NULL;
423 int i, stack_size, arg_offsets [16], regs_offset, dummy_stack_space;
424 const guint kMaxCodeSize = NACL_SIZE (256, 512);
427 dummy_stack_space = 6 * sizeof(mgreg_t); /* Windows expects stack space allocated for all 6 dummy args. */
429 dummy_stack_space = 0;
432 start = code = mono_global_codeman_reserve (kMaxCodeSize);
434 /* The stack is unaligned on entry */
435 stack_size = 192 + 8 + dummy_stack_space;
440 unwind_ops = mono_arch_get_cie_program ();
443 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_size);
445 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 8);
448 * To hide linux/windows calling convention differences, we pass all arguments on
449 * the stack by passing 6 dummy values in registers.
452 arg_offsets [0] = dummy_stack_space + 0;
453 arg_offsets [1] = dummy_stack_space + sizeof(mgreg_t);
454 arg_offsets [2] = dummy_stack_space + sizeof(mgreg_t) * 2;
455 arg_offsets [3] = dummy_stack_space + sizeof(mgreg_t) * 3;
456 regs_offset = dummy_stack_space + sizeof(mgreg_t) * 4;
459 for (i = 0; i < AMD64_NREG; ++i)
461 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
463 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, stack_size + sizeof(mgreg_t));
464 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (AMD64_RSP * sizeof(mgreg_t)), X86_EAX, sizeof(mgreg_t));
465 /* Set arg1 == regs */
466 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, regs_offset);
467 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [0], AMD64_RAX, sizeof(mgreg_t));
468 /* Set arg2 == eip */
470 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
472 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, stack_size, sizeof(mgreg_t));
473 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [1], AMD64_RAX, sizeof(mgreg_t));
474 /* Set arg3 == exc/ex_token_index */
476 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [2], 0, sizeof(mgreg_t));
478 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [2], AMD64_ARG_REG1, sizeof(mgreg_t));
479 /* Set arg4 == rethrow/pc offset */
481 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], 0, sizeof(mgreg_t));
483 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [3], AMD64_ARG_REG2, sizeof(mgreg_t));
486 * The caller is LLVM code which passes the absolute address not a pc offset,
487 * so compensate by passing 0 as 'rip' and passing the negated abs address as
490 amd64_neg_membase (code, AMD64_RSP, arg_offsets [3]);
492 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], rethrow, sizeof(mgreg_t));
496 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");
497 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
499 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));
501 amd64_call_reg (code, AMD64_R11);
502 amd64_breakpoint (code);
504 mono_arch_flush_icache (start, code - start);
506 g_assert ((code - start) < kMaxCodeSize);
508 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
511 *info = mono_tramp_info_create (g_strdup (tramp_name), start, code - start, ji, unwind_ops);
517 * mono_arch_get_throw_exception:
519 * Returns a function pointer which can be used to raise
520 * exceptions. The returned function has the following
521 * signature: void (*func) (MonoException *exc);
525 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
527 return get_throw_trampoline (info, FALSE, FALSE, FALSE, FALSE, "throw_exception", aot);
531 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
533 return get_throw_trampoline (info, TRUE, FALSE, FALSE, FALSE, "rethrow_exception", aot);
537 * mono_arch_get_throw_corlib_exception:
539 * Returns a function pointer which can be used to raise
540 * corlib exceptions. The returned function has the following
541 * signature: void (*func) (guint32 ex_token, guint32 offset);
542 * Here, offset is the offset which needs to be substracted from the caller IP
543 * to get the IP of the throw. Passing the offset has the advantage that it
544 * needs no relocations in the caller.
547 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
549 return get_throw_trampoline (info, FALSE, TRUE, FALSE, FALSE, "throw_corlib_exception", aot);
553 * mono_arch_find_jit_info:
555 * This function is used to gather information from @ctx, and store it in @frame_info.
556 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
557 * is modified if needed.
558 * Returns TRUE on success, FALSE otherwise.
561 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
562 MonoJitInfo *ji, MonoContext *ctx,
563 MonoContext *new_ctx, MonoLMF **lmf,
564 mgreg_t **save_locations,
565 StackFrameInfo *frame)
567 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
569 memset (frame, 0, sizeof (StackFrameInfo));
575 mgreg_t regs [MONO_MAX_IREGS + 1];
577 guint32 unwind_info_len;
580 frame->type = FRAME_TYPE_MANAGED;
583 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
585 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
587 frame->unwind_info = unwind_info;
588 frame->unwind_info_len = unwind_info_len;
590 regs [AMD64_RAX] = new_ctx->rax;
591 regs [AMD64_RBX] = new_ctx->rbx;
592 regs [AMD64_RCX] = new_ctx->rcx;
593 regs [AMD64_RDX] = new_ctx->rdx;
594 regs [AMD64_RBP] = new_ctx->rbp;
595 regs [AMD64_RSP] = new_ctx->rsp;
596 regs [AMD64_RSI] = new_ctx->rsi;
597 regs [AMD64_RDI] = new_ctx->rdi;
598 regs [AMD64_RIP] = new_ctx->rip;
599 regs [AMD64_R12] = new_ctx->r12;
600 regs [AMD64_R13] = new_ctx->r13;
601 regs [AMD64_R14] = new_ctx->r14;
602 regs [AMD64_R15] = new_ctx->r15;
604 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
605 (guint8*)ji->code_start + ji->code_size,
606 ip, regs, MONO_MAX_IREGS + 1,
607 save_locations, MONO_MAX_IREGS, &cfa);
609 new_ctx->rax = regs [AMD64_RAX];
610 new_ctx->rbx = regs [AMD64_RBX];
611 new_ctx->rcx = regs [AMD64_RCX];
612 new_ctx->rdx = regs [AMD64_RDX];
613 new_ctx->rbp = regs [AMD64_RBP];
614 new_ctx->rsp = regs [AMD64_RSP];
615 new_ctx->rsi = regs [AMD64_RSI];
616 new_ctx->rdi = regs [AMD64_RDI];
617 new_ctx->rip = regs [AMD64_RIP];
618 new_ctx->r12 = regs [AMD64_R12];
619 new_ctx->r13 = regs [AMD64_R13];
620 new_ctx->r14 = regs [AMD64_R14];
621 new_ctx->r15 = regs [AMD64_R15];
623 /* The CFA becomes the new SP value */
624 new_ctx->rsp = (mgreg_t)cfa;
629 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
630 /* remove any unused lmf */
631 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
634 #ifndef MONO_AMD64_NO_PUSHES
635 /* Pop arguments off the stack */
636 if (ji->has_arch_eh_info)
637 new_ctx->rsp += mono_jit_info_get_arch_eh_info (ji)->stack_size;
644 if (((guint64)(*lmf)->previous_lmf) & 2) {
646 * This LMF entry is created by the soft debug code to mark transitions to
647 * managed code done during invokes.
649 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
651 g_assert (ext->debugger_invoke);
653 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
655 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
657 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
662 if (((guint64)(*lmf)->previous_lmf) & 1) {
663 /* This LMF has the rip field set */
665 } else if ((*lmf)->rsp == 0) {
670 * The rsp field is set just before the call which transitioned to native
671 * code. Obtain the rip from the stack.
673 rip = *(guint64*)((*lmf)->rsp - sizeof(mgreg_t));
676 ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
678 * FIXME: ji == NULL can happen when a managed-to-native wrapper is interrupted
679 * in the soft debugger suspend code, since (*lmf)->rsp no longer points to the
690 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
693 new_ctx->rbp = (*lmf)->rbp;
694 new_ctx->rsp = (*lmf)->rsp;
696 new_ctx->rbx = (*lmf)->rbx;
697 new_ctx->r12 = (*lmf)->r12;
698 new_ctx->r13 = (*lmf)->r13;
699 new_ctx->r14 = (*lmf)->r14;
700 new_ctx->r15 = (*lmf)->r15;
702 new_ctx->rdi = (*lmf)->rdi;
703 new_ctx->rsi = (*lmf)->rsi;
706 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
717 * Called by resuming from a signal handler.
720 handle_signal_exception (gpointer obj)
722 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
724 static void (*restore_context) (MonoContext *);
726 if (!restore_context)
727 restore_context = mono_get_restore_context ();
729 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
731 if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
734 mono_handle_exception (&ctx, obj);
736 restore_context (&ctx);
740 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
742 guint64 sp = ctx->rsp;
744 ctx->rdi = (guint64)user_data;
746 /* Allocate a stack frame below the red zone */
748 /* The stack should be unaligned */
752 ctx->rip = (guint64)async_cb;
756 * mono_arch_handle_exception:
758 * @ctx: saved processor state
759 * @obj: the exception object
762 mono_arch_handle_exception (void *sigctx, gpointer obj)
764 #if defined(MONO_ARCH_USE_SIGACTION)
768 * Handling the exception in the signal handler is problematic, since the original
769 * signal is disabled, and we could run arbitrary code though the debugger. So
770 * resume into the normal stack and do most work there if possible.
772 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
774 /* Pass the ctx parameter in TLS */
775 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
777 mctx = jit_tls->ex_ctx;
778 mono_arch_setup_async_callback (&mctx, handle_signal_exception, obj);
779 mono_monoctx_to_sigctx (&mctx, sigctx);
785 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
787 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
790 mono_handle_exception (&mctx, obj);
792 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
799 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
801 mono_sigctx_to_monoctx (sigctx, mctx);
805 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
807 mono_monoctx_to_sigctx (mctx, sigctx);
811 mono_arch_ip_from_context (void *sigctx)
813 #if defined(MONO_ARCH_USE_SIGACTION)
814 ucontext_t *ctx = (ucontext_t*)sigctx;
816 return (gpointer)UCONTEXT_REG_RIP (ctx);
818 MonoContext *ctx = sigctx;
819 return (gpointer)ctx->rip;
824 restore_soft_guard_pages (void)
826 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
827 if (jit_tls->stack_ovf_guard_base)
828 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
832 * this function modifies mctx so that when it is restored, it
833 * won't execcute starting at mctx.eip, but in a function that
834 * will restore the protection on the soft-guard pages and return back to
835 * continue at mctx.eip.
838 prepare_for_guard_pages (MonoContext *mctx)
841 sp = (gpointer)(mctx->rsp);
843 /* the return addr */
844 sp [0] = (gpointer)(mctx->rip);
845 mctx->rip = (guint64)restore_soft_guard_pages;
846 mctx->rsp = (guint64)sp;
850 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
852 void (*restore_context) (MonoContext *);
855 restore_context = mono_get_restore_context ();
856 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
858 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
860 prepare_for_guard_pages (&mctx);
861 restore_context (&mctx);
864 mono_handle_exception (&mctx, obj);
866 prepare_for_guard_pages (&mctx);
867 restore_context (&mctx);
871 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
873 #if defined(MONO_ARCH_USE_SIGACTION)
874 MonoException *exc = NULL;
875 ucontext_t *ctx = (ucontext_t*)sigctx;
876 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_RIP (sigctx), NULL);
879 ucontext_t *copied_ctx;
882 exc = mono_domain_get ()->stack_overflow_ex;
884 mono_handle_native_sigsegv (SIGSEGV, sigctx);
886 /* setup a call frame on the real stack so that control is returned there
887 * and exception handling can continue.
888 * The frame looks like:
892 * 128 is the size of the red zone
894 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4 + 128;
896 frame_size += sizeof (*ctx->uc_mcontext);
900 sp = (gpointer)(UCONTEXT_REG_RSP (sigctx) & ~15);
901 sp = (gpointer)((char*)sp - frame_size);
902 copied_ctx = (ucontext_t*)(sp + 4);
903 /* the arguments must be aligned */
904 sp [-1] = (gpointer)UCONTEXT_REG_RIP (sigctx);
905 /* may need to adjust pointers in the new struct copy, depending on the OS */
906 memcpy (copied_ctx, ctx, sizeof (ucontext_t));
909 guint8 * copied_mcontext = (guint8*)copied_ctx + sizeof (ucontext_t);
910 /* uc_mcontext is a pointer, so make a copy which is stored after the ctx */
911 memcpy (copied_mcontext, ctx->uc_mcontext, sizeof (*ctx->uc_mcontext));
912 copied_ctx->uc_mcontext = (void*)copied_mcontext;
915 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
916 UCONTEXT_REG_RIP (sigctx) = (unsigned long)altstack_handle_and_restore;
917 UCONTEXT_REG_RSP (sigctx) = (unsigned long)(sp - 1);
918 UCONTEXT_REG_RDI (sigctx) = (unsigned long)(copied_ctx);
919 UCONTEXT_REG_RSI (sigctx) = (guint64)exc;
920 UCONTEXT_REG_RDX (sigctx) = stack_ovf;
925 mono_amd64_get_original_ip (void)
927 MonoLMF *lmf = mono_get_lmf ();
931 /* Reset the change to previous_lmf */
932 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf & ~1);
938 mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
940 guint8 *code, *start;
942 gpointer throw_trampoline;
943 MonoJumpInfo *ji = NULL;
944 GSList *unwind_ops = NULL;
945 const guint kMaxCodeSize = NACL_SIZE (128, 256);
947 start = code = mono_global_codeman_reserve (kMaxCodeSize);
949 /* We are in the frame of a managed method after a call */
951 * We would like to throw the pending exception in such a way that it looks to
952 * be thrown from the managed method.
955 /* Save registers which might contain the return value of the call */
956 amd64_push_reg (code, AMD64_RAX);
957 amd64_push_reg (code, AMD64_RDX);
959 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
960 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
963 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
965 /* Obtain the pending exception */
967 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
968 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
970 amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
972 amd64_call_reg (code, AMD64_R11);
974 /* Check if it is NULL, and branch */
975 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
976 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
978 /* exc != NULL branch */
980 /* Save the exc on the stack */
981 amd64_push_reg (code, AMD64_RAX);
983 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
985 /* Obtain the original ip and clear the flag in previous_lmf */
987 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
988 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
990 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
992 amd64_call_reg (code, AMD64_R11);
995 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, 8, 8);
997 /* Pop saved stuff from the stack */
998 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 6 * 8);
1000 /* Setup arguments for the throw trampoline */
1002 amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_R11, 8);
1003 /* The trampoline expects the caller ip to be pushed on the stack */
1004 amd64_push_reg (code, AMD64_RAX);
1006 /* Call the throw trampoline */
1008 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_throw_exception");
1009 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1011 throw_trampoline = mono_get_throw_exception ();
1012 amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
1014 /* We use a jump instead of a call so we can push the original ip on the stack */
1015 amd64_jump_reg (code, AMD64_R11);
1017 /* ex == NULL branch */
1018 mono_amd64_patch (br [0], code);
1020 /* Obtain the original ip and clear the flag in previous_lmf */
1022 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
1023 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1025 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
1027 amd64_call_reg (code, AMD64_R11);
1028 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
1030 /* Restore registers */
1031 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1032 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
1033 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1034 amd64_pop_reg (code, AMD64_RDX);
1035 amd64_pop_reg (code, AMD64_RAX);
1037 /* Return to original code */
1038 amd64_jump_reg (code, AMD64_R11);
1040 g_assert ((code - start) < kMaxCodeSize);
1042 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1045 *info = mono_tramp_info_create (g_strdup_printf ("throw_pending_exception"), start, code - start, ji, unwind_ops);
1050 static gpointer throw_pending_exception;
1053 * Called when a thread receives an async exception while executing unmanaged code.
1054 * Instead of checking for this exception in the managed-to-native wrapper, we hijack
1055 * the return address on the stack to point to a helper routine which throws the
1059 mono_arch_notify_pending_exc (void)
1061 MonoLMF *lmf = mono_get_lmf ();
1064 /* Not yet started */
1071 if ((guint64)lmf->previous_lmf & 1)
1072 /* Already hijacked or trampoline LMF entry */
1075 /* lmf->rsp is set just before making the call which transitions to unmanaged code */
1076 lmf->rip = *(guint64*)(lmf->rsp - 8);
1077 /* Signal that lmf->rip is set */
1078 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
1080 *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
1084 mono_amd64_get_exception_trampolines (gboolean aot)
1086 MonoTrampInfo *info;
1087 GSList *tramps = NULL;
1089 mono_arch_get_throw_pending_exception (&info, aot);
1090 tramps = g_slist_prepend (tramps, info);
1092 /* LLVM needs different throw trampolines */
1093 get_throw_trampoline (&info, FALSE, TRUE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", aot);
1094 tramps = g_slist_prepend (tramps, info);
1096 get_throw_trampoline (&info, FALSE, TRUE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", aot);
1097 tramps = g_slist_prepend (tramps, info);
1099 get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", FALSE);
1100 tramps = g_slist_prepend (tramps, info);
1106 mono_arch_exceptions_init (void)
1111 if (mono_aot_only) {
1112 throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
1113 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
1114 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
1115 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
1116 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
1117 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
1118 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
1120 /* Call this to avoid initialization races */
1121 throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
1123 tramps = mono_amd64_get_exception_trampolines (FALSE);
1124 for (l = tramps; l; l = l->next) {
1125 MonoTrampInfo *info = l->data;
1127 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
1128 mono_save_trampoline_xdebug_info (info);
1129 mono_tramp_info_free (info);
1131 g_slist_free (tramps);
1138 * The mono_arch_unwindinfo* methods are used to build and add
1139 * function table info for each emitted method from mono. On Winx64
1140 * the seh handler will not be called if the mono methods are not
1141 * added to the function table.
1143 * We should not need to add non-volatile register info to the
1144 * table since mono stores that info elsewhere. (Except for the register
1148 #define MONO_MAX_UNWIND_CODES 22
1150 typedef union _UNWIND_CODE {
1153 guchar UnwindOp : 4;
1156 gushort FrameOffset;
1157 } UNWIND_CODE, *PUNWIND_CODE;
1159 typedef struct _UNWIND_INFO {
1162 guchar SizeOfProlog;
1163 guchar CountOfCodes;
1164 guchar FrameRegister : 4;
1165 guchar FrameOffset : 4;
1166 /* custom size for mono allowing for mono allowing for*/
1167 /*UWOP_PUSH_NONVOL ebp offset = 21*/
1168 /*UWOP_ALLOC_LARGE : requires 2 or 3 offset = 20*/
1169 /*UWOP_SET_FPREG : requires 2 offset = 17*/
1170 /*UWOP_PUSH_NONVOL offset = 15-0*/
1171 UNWIND_CODE UnwindCode[MONO_MAX_UNWIND_CODES];
1173 /* UNWIND_CODE MoreUnwindCode[((CountOfCodes + 1) & ~1) - 1];
1175 * OPTIONAL ULONG ExceptionHandler;
1176 * OPTIONAL ULONG FunctionEntry;
1178 * OPTIONAL ULONG ExceptionData[]; */
1179 } UNWIND_INFO, *PUNWIND_INFO;
1183 RUNTIME_FUNCTION runtimeFunction;
1184 UNWIND_INFO unwindInfo;
1185 } MonoUnwindInfo, *PMonoUnwindInfo;
1188 mono_arch_unwindinfo_create (gpointer* monoui)
1190 PMonoUnwindInfo newunwindinfo;
1191 *monoui = newunwindinfo = g_new0 (MonoUnwindInfo, 1);
1192 newunwindinfo->unwindInfo.Version = 1;
1196 mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1198 PMonoUnwindInfo unwindinfo;
1199 PUNWIND_CODE unwindcode;
1202 mono_arch_unwindinfo_create (monoui);
1204 unwindinfo = (MonoUnwindInfo*)*monoui;
1206 if (unwindinfo->unwindInfo.CountOfCodes >= MONO_MAX_UNWIND_CODES)
1207 g_error ("Larger allocation needed for the unwind information.");
1209 codeindex = MONO_MAX_UNWIND_CODES - (++unwindinfo->unwindInfo.CountOfCodes);
1210 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1211 unwindcode->UnwindOp = 0; /*UWOP_PUSH_NONVOL*/
1212 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1213 unwindcode->OpInfo = reg;
1215 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1216 g_error ("Adding unwind info in wrong order.");
1218 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1222 mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1224 PMonoUnwindInfo unwindinfo;
1225 PUNWIND_CODE unwindcode;
1228 mono_arch_unwindinfo_create (monoui);
1230 unwindinfo = (MonoUnwindInfo*)*monoui;
1232 if (unwindinfo->unwindInfo.CountOfCodes + 1 >= MONO_MAX_UNWIND_CODES)
1233 g_error ("Larger allocation needed for the unwind information.");
1235 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += 2);
1236 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1237 unwindcode->FrameOffset = 0; /*Assuming no frame pointer offset for mono*/
1239 unwindcode->UnwindOp = 3; /*UWOP_SET_FPREG*/
1240 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1241 unwindcode->OpInfo = reg;
1243 unwindinfo->unwindInfo.FrameRegister = reg;
1245 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1246 g_error ("Adding unwind info in wrong order.");
1248 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1252 mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size )
1254 PMonoUnwindInfo unwindinfo;
1255 PUNWIND_CODE unwindcode;
1259 mono_arch_unwindinfo_create (monoui);
1261 unwindinfo = (MonoUnwindInfo*)*monoui;
1264 g_error ("Stack allocation must be equal to or greater than 0x8.");
1268 else if (size <= 0x7FFF8)
1273 if (unwindinfo->unwindInfo.CountOfCodes + codesneeded > MONO_MAX_UNWIND_CODES)
1274 g_error ("Larger allocation needed for the unwind information.");
1276 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += codesneeded);
1277 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1279 if (codesneeded == 1) {
1280 /*The size of the allocation is
1281 (the number in the OpInfo member) times 8 plus 8*/
1282 unwindcode->OpInfo = (size - 8)/8;
1283 unwindcode->UnwindOp = 2; /*UWOP_ALLOC_SMALL*/
1286 if (codesneeded == 3) {
1287 /*the unscaled size of the allocation is recorded
1288 in the next two slots in little-endian format*/
1289 *((unsigned int*)(&unwindcode->FrameOffset)) = size;
1291 unwindcode->OpInfo = 1;
1294 /*the size of the allocation divided by 8
1295 is recorded in the next slot*/
1296 unwindcode->FrameOffset = size/8;
1298 unwindcode->OpInfo = 0;
1301 unwindcode->UnwindOp = 1; /*UWOP_ALLOC_LARGE*/
1304 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1306 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1307 g_error ("Adding unwind info in wrong order.");
1309 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1313 mono_arch_unwindinfo_get_size (gpointer monoui)
1315 PMonoUnwindInfo unwindinfo;
1319 unwindinfo = (MonoUnwindInfo*)monoui;
1320 return (8 + sizeof (MonoUnwindInfo)) -
1321 (sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
1325 MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
1329 PMonoUnwindInfo targetinfo;
1330 MonoDomain *domain = mono_domain_get ();
1332 ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
1336 pos = (guint64)(((char*)ji->code_start) + ji->code_size);
1338 targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
1340 targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
1342 return &targetinfo->runtimeFunction;
1346 mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
1348 PMonoUnwindInfo unwindinfo, targetinfo;
1350 guint64 targetlocation;
1354 unwindinfo = (MonoUnwindInfo*)*monoui;
1355 targetlocation = (guint64)&(((guchar*)code)[code_size]);
1356 targetinfo = (PMonoUnwindInfo) ALIGN_TO(targetlocation, 8);
1358 unwindinfo->runtimeFunction.EndAddress = code_size;
1359 unwindinfo->runtimeFunction.UnwindData = ((guchar*)&targetinfo->unwindInfo) - ((guchar*)code);
1361 memcpy (targetinfo, unwindinfo, sizeof (MonoUnwindInfo) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
1363 codecount = unwindinfo->unwindInfo.CountOfCodes;
1365 memcpy (&targetinfo->unwindInfo.UnwindCode[0], &unwindinfo->unwindInfo.UnwindCode[MONO_MAX_UNWIND_CODES-codecount],
1366 sizeof (UNWIND_CODE) * unwindinfo->unwindInfo.CountOfCodes);
1369 g_free (unwindinfo);
1372 RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
1377 #if MONO_SUPPORT_TASKLETS
1378 MonoContinuationRestore
1379 mono_tasklets_arch_restore (void)
1381 static guint8* saved = NULL;
1382 guint8 *code, *start;
1383 int cont_reg = AMD64_R9; /* register usable on both call conventions */
1384 const guint kMaxCodeSize = NACL_SIZE (64, 128);
1388 return (MonoContinuationRestore)saved;
1389 code = start = mono_global_codeman_reserve (kMaxCodeSize);
1390 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1391 /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
1392 * state is in AMD64_ARG_REG2 ($rdx or $rsi)
1393 * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
1394 * We move cont to cont_reg since we need both rcx and rdi for the copy
1395 * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
1397 amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
1398 amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
1399 /* setup the copy of the stack */
1400 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
1401 amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
1403 amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
1404 amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
1405 amd64_prefix (code, X86_REP_PREFIX);
1408 /* now restore the registers from the LMF */
1409 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, lmf), 8);
1410 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbx), 8);
1411 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbp), 8);
1412 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
1413 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
1414 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
1415 #if !defined(__native_client_codegen__)
1416 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
1419 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
1420 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
1422 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsp), 8);
1424 /* restore the lmf chain */
1425 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1426 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1428 /* state is already in rax */
1429 amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1430 g_assert ((code - start) <= kMaxCodeSize);
1432 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1435 return (MonoContinuationRestore)saved;
1440 * mono_arch_setup_resume_sighandler_ctx:
1442 * Setup CTX so execution continues at FUNC.
1445 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1448 * When resuming from a signal handler, the stack should be misaligned, just like right after
1451 if ((((guint64)MONO_CONTEXT_GET_SP (ctx)) % 16) == 0)
1452 MONO_CONTEXT_SET_SP (ctx, (guint64)MONO_CONTEXT_GET_SP (ctx) - 8);
1453 MONO_CONTEXT_SET_IP (ctx, func);