2 * exceptions-amd64.c: exception support for AMD64
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
14 #ifdef HAVE_UCONTEXT_H
18 #include <mono/arch/amd64/amd64-codegen.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/threads.h>
22 #include <mono/metadata/threads-types.h>
23 #include <mono/metadata/debug-helpers.h>
24 #include <mono/metadata/exception.h>
25 #include <mono/metadata/gc-internal.h>
26 #include <mono/metadata/mono-debug.h>
27 #include <mono/utils/mono-mmap.h>
30 #include "mini-amd64.h"
32 #include "debug-mini.h"
34 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
37 static MonoW32ExceptionHandler fpe_handler;
38 static MonoW32ExceptionHandler ill_handler;
39 static MonoW32ExceptionHandler segv_handler;
41 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
42 guint64 mono_win_chained_exception_filter_result;
43 gboolean mono_win_chained_exception_filter_didrun;
45 #define W32_SEH_HANDLE_EX(_ex) \
46 if (_ex##_handler) _ex##_handler(0, ep, sctx)
49 * Unhandled Exception Filter
50 * Top-level per-process exception handler.
52 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
59 mono_win_chained_exception_filter_didrun = FALSE;
60 res = EXCEPTION_CONTINUE_EXECUTION;
62 er = ep->ExceptionRecord;
63 ctx = ep->ContextRecord;
64 sctx = g_malloc(sizeof(MonoContext));
66 /* Copy Win32 context to UNIX style context */
81 switch (er->ExceptionCode) {
82 case EXCEPTION_ACCESS_VIOLATION:
83 W32_SEH_HANDLE_EX(segv);
85 case EXCEPTION_ILLEGAL_INSTRUCTION:
86 W32_SEH_HANDLE_EX(ill);
88 case EXCEPTION_INT_DIVIDE_BY_ZERO:
89 case EXCEPTION_INT_OVERFLOW:
90 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
91 case EXCEPTION_FLT_OVERFLOW:
92 case EXCEPTION_FLT_UNDERFLOW:
93 case EXCEPTION_FLT_INEXACT_RESULT:
94 W32_SEH_HANDLE_EX(fpe);
100 /* Copy context back */
102 ctx->Rsp = sctx->rsp;
103 ctx->Rdi = sctx->rdi;
104 ctx->Rsi = sctx->rsi;
105 ctx->Rbx = sctx->rbx;
106 ctx->Rbp = sctx->rbp;
107 ctx->R12 = sctx->r12;
108 ctx->R13 = sctx->r13;
109 ctx->R14 = sctx->r14;
110 ctx->R15 = sctx->r15;
111 ctx->Rip = sctx->rip;
113 /* Volatile But should not matter?*/
114 ctx->Rax = sctx->rax;
115 ctx->Rcx = sctx->rcx;
116 ctx->Rdx = sctx->rdx;
120 if (mono_win_chained_exception_filter_didrun)
121 res = mono_win_chained_exception_filter_result;
126 void win32_seh_init()
128 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_handler);
131 void win32_seh_cleanup()
133 if (mono_old_win_toplevel_exception_filter) SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
136 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
140 fpe_handler = handler;
143 ill_handler = handler;
146 segv_handler = handler;
153 #endif /* TARGET_WIN32 */
156 * mono_arch_get_restore_context:
158 * Returns a pointer to a method which restores a previously saved sigcontext.
161 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
163 guint8 *start = NULL;
165 MonoJumpInfo *ji = NULL;
166 GSList *unwind_ops = NULL;
168 /* restore_contect (MonoContext *ctx) */
170 start = code = mono_global_codeman_reserve (256);
172 amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG1, 8);
174 /* Restore all registers except %rip and %r11 */
175 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rax), 8);
176 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rcx), 8);
177 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rdx), 8);
178 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rbx), 8);
179 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rbp), 8);
180 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsi), 8);
181 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rdi), 8);
182 //amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r8), 8);
183 //amd64_mov_reg_membase (code, AMD64_R9, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r9), 8);
184 //amd64_mov_reg_membase (code, AMD64_R10, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r10), 8);
185 amd64_mov_reg_membase (code, AMD64_R12, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r12), 8);
186 amd64_mov_reg_membase (code, AMD64_R13, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r13), 8);
187 amd64_mov_reg_membase (code, AMD64_R14, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r14), 8);
188 #if !defined(__native_client_codegen__)
189 amd64_mov_reg_membase (code, AMD64_R15, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r15), 8);
192 if (mono_running_on_valgrind ()) {
193 /* Prevent 'Address 0x... is just below the stack ptr.' errors */
194 amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
195 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
196 amd64_mov_reg_reg (code, AMD64_RSP, AMD64_R8, 8);
198 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
199 /* get return address */
200 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
203 /* jump to the saved IP */
204 amd64_jump_reg (code, AMD64_R11);
206 nacl_global_codeman_validate(&start, 256, &code);
208 mono_arch_flush_icache (start, code - start);
211 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
217 * mono_arch_get_call_filter:
219 * Returns a pointer to a method which calls an exception filter. We
220 * also use this function to call finally handlers (we pass NULL as
221 * @exc object in this case).
224 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
230 MonoJumpInfo *ji = NULL;
231 GSList *unwind_ops = NULL;
232 const guint kMaxCodeSize = NACL_SIZE (128, 256);
234 start = code = mono_global_codeman_reserve (kMaxCodeSize);
236 /* call_filter (MonoContext *ctx, unsigned long eip) */
239 /* Alloc new frame */
240 amd64_push_reg (code, AMD64_RBP);
241 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
243 /* Save callee saved regs */
245 for (i = 0; i < AMD64_NREG; ++i)
246 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
247 amd64_push_reg (code, i);
253 amd64_push_reg (code, AMD64_RBP);
255 /* Make stack misaligned, the call will make it aligned again */
257 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
260 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbp), 8);
261 /* load callee saved regs */
262 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbx), 8);
263 amd64_mov_reg_membase (code, AMD64_R12, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r12), 8);
264 amd64_mov_reg_membase (code, AMD64_R13, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r13), 8);
265 amd64_mov_reg_membase (code, AMD64_R14, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r14), 8);
266 #if !defined(__native_client_codegen__)
267 amd64_mov_reg_membase (code, AMD64_R15, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r15), 8);
270 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rdi), 8);
271 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rsi), 8);
274 /* call the handler */
275 amd64_call_reg (code, AMD64_ARG_REG2);
278 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
281 amd64_pop_reg (code, AMD64_RBP);
283 /* Restore callee saved regs */
284 for (i = AMD64_NREG; i >= 0; --i)
285 if (AMD64_IS_CALLEE_SAVED_REG (i))
286 amd64_pop_reg (code, i);
291 g_assert ((code - start) < kMaxCodeSize);
293 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
295 mono_arch_flush_icache (start, code - start);
298 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
304 * The first few arguments are dummy, to force the other arguments to be passed on
305 * the stack, this avoids overwriting the argument registers in the throw trampoline.
308 mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
309 guint64 dummy5, guint64 dummy6,
310 mgreg_t *regs, mgreg_t rip,
311 MonoObject *exc, gboolean rethrow)
313 static void (*restore_context) (MonoContext *);
316 if (!restore_context)
317 restore_context = mono_get_restore_context ();
319 ctx.rsp = regs [AMD64_RSP];
321 ctx.rbx = regs [AMD64_RBX];
322 ctx.rbp = regs [AMD64_RBP];
323 ctx.r12 = regs [AMD64_R12];
324 ctx.r13 = regs [AMD64_R13];
325 ctx.r14 = regs [AMD64_R14];
326 ctx.r15 = regs [AMD64_R15];
327 ctx.rdi = regs [AMD64_RDI];
328 ctx.rsi = regs [AMD64_RSI];
329 ctx.rax = regs [AMD64_RAX];
330 ctx.rcx = regs [AMD64_RCX];
331 ctx.rdx = regs [AMD64_RDX];
333 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
334 MonoException *mono_ex = (MonoException*)exc;
336 mono_ex->stack_trace = NULL;
339 if (mono_debug_using_mono_debugger ()) {
340 guint8 buf [16], *code;
342 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, (gpointer)rip, FALSE);
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;
424 const guint kMaxCodeSize = NACL_SIZE (256, 512);
426 start = code = mono_global_codeman_reserve (kMaxCodeSize);
428 /* The stack is unaligned on entry */
429 stack_size = 192 + 8;
434 unwind_ops = mono_arch_get_cie_program ();
437 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_size);
439 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 8);
442 * To hide linux/windows calling convention differences, we pass all arguments on
443 * the stack by passing 6 dummy values in registers.
447 arg_offsets [1] = sizeof(mgreg_t);
448 arg_offsets [2] = sizeof(mgreg_t) * 2;
449 arg_offsets [3] = sizeof(mgreg_t) * 3;
450 regs_offset = sizeof(mgreg_t) * 4;
453 for (i = 0; i < AMD64_NREG; ++i)
455 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
457 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, stack_size + sizeof(mgreg_t));
458 amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (AMD64_RSP * sizeof(mgreg_t)), X86_EAX, sizeof(mgreg_t));
459 /* Set arg1 == regs */
460 amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, regs_offset);
461 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [0], AMD64_RAX, sizeof(mgreg_t));
462 /* Set arg2 == eip */
464 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
466 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, stack_size, sizeof(mgreg_t));
467 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [1], AMD64_RAX, sizeof(mgreg_t));
468 /* Set arg3 == exc/ex_token_index */
470 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [2], 0, sizeof(mgreg_t));
472 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [2], AMD64_ARG_REG1, sizeof(mgreg_t));
473 /* Set arg4 == rethrow/pc offset */
475 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], 0, sizeof(mgreg_t));
477 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [3], AMD64_ARG_REG2, sizeof(mgreg_t));
480 * The caller is LLVM code which passes the absolute address not a pc offset,
481 * so compensate by passing 0 as 'rip' and passing the negated abs address as
484 amd64_neg_membase (code, AMD64_RSP, arg_offsets [3]);
486 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], rethrow, sizeof(mgreg_t));
490 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");
491 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
493 amd64_mov_reg_imm (code, AMD64_R11, resume_unwind ? (mono_amd64_resume_unwind) : (corlib ? (gpointer)mono_amd64_throw_corlib_exception : (gpointer)mono_amd64_throw_exception));
495 amd64_call_reg (code, AMD64_R11);
496 amd64_breakpoint (code);
498 mono_arch_flush_icache (start, code - start);
500 g_assert ((code - start) < kMaxCodeSize);
502 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
505 *info = mono_tramp_info_create (g_strdup (tramp_name), start, code - start, ji, unwind_ops);
511 * mono_arch_get_throw_exception:
513 * Returns a function pointer which can be used to raise
514 * exceptions. The returned function has the following
515 * signature: void (*func) (MonoException *exc);
519 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
521 return get_throw_trampoline (info, FALSE, FALSE, FALSE, FALSE, "throw_exception", aot);
525 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
527 return get_throw_trampoline (info, TRUE, FALSE, FALSE, FALSE, "rethrow_exception", aot);
531 * mono_arch_get_throw_corlib_exception:
533 * Returns a function pointer which can be used to raise
534 * corlib exceptions. The returned function has the following
535 * signature: void (*func) (guint32 ex_token, guint32 offset);
536 * Here, offset is the offset which needs to be substracted from the caller IP
537 * to get the IP of the throw. Passing the offset has the advantage that it
538 * needs no relocations in the caller.
541 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
543 return get_throw_trampoline (info, FALSE, TRUE, FALSE, FALSE, "throw_corlib_exception", aot);
547 * mono_arch_find_jit_info:
549 * This function is used to gather information from @ctx, and store it in @frame_info.
550 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
551 * is modified if needed.
552 * Returns TRUE on success, FALSE otherwise.
555 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
556 MonoJitInfo *ji, MonoContext *ctx,
557 MonoContext *new_ctx, MonoLMF **lmf,
558 mgreg_t **save_locations,
559 StackFrameInfo *frame)
561 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
563 memset (frame, 0, sizeof (StackFrameInfo));
569 mgreg_t regs [MONO_MAX_IREGS + 1];
571 guint32 unwind_info_len;
574 frame->type = FRAME_TYPE_MANAGED;
577 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
579 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
581 frame->unwind_info = unwind_info;
582 frame->unwind_info_len = unwind_info_len;
584 regs [AMD64_RAX] = new_ctx->rax;
585 regs [AMD64_RBX] = new_ctx->rbx;
586 regs [AMD64_RCX] = new_ctx->rcx;
587 regs [AMD64_RDX] = new_ctx->rdx;
588 regs [AMD64_RBP] = new_ctx->rbp;
589 regs [AMD64_RSP] = new_ctx->rsp;
590 regs [AMD64_RSI] = new_ctx->rsi;
591 regs [AMD64_RDI] = new_ctx->rdi;
592 regs [AMD64_RIP] = new_ctx->rip;
593 regs [AMD64_R12] = new_ctx->r12;
594 regs [AMD64_R13] = new_ctx->r13;
595 regs [AMD64_R14] = new_ctx->r14;
596 regs [AMD64_R15] = new_ctx->r15;
598 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
599 (guint8*)ji->code_start + ji->code_size,
600 ip, regs, MONO_MAX_IREGS + 1,
601 save_locations, MONO_MAX_IREGS, &cfa);
603 new_ctx->rax = regs [AMD64_RAX];
604 new_ctx->rbx = regs [AMD64_RBX];
605 new_ctx->rcx = regs [AMD64_RCX];
606 new_ctx->rdx = regs [AMD64_RDX];
607 new_ctx->rbp = regs [AMD64_RBP];
608 new_ctx->rsp = regs [AMD64_RSP];
609 new_ctx->rsi = regs [AMD64_RSI];
610 new_ctx->rdi = regs [AMD64_RDI];
611 new_ctx->rip = regs [AMD64_RIP];
612 new_ctx->r12 = regs [AMD64_R12];
613 new_ctx->r13 = regs [AMD64_R13];
614 new_ctx->r14 = regs [AMD64_R14];
615 new_ctx->r15 = regs [AMD64_R15];
617 /* The CFA becomes the new SP value */
618 new_ctx->rsp = (mgreg_t)cfa;
623 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
624 /* remove any unused lmf */
625 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
628 #ifndef MONO_AMD64_NO_PUSHES
629 /* Pop arguments off the stack */
631 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
633 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
634 new_ctx->rsp += stack_to_pop;
642 if (((guint64)(*lmf)->previous_lmf) & 2) {
644 * This LMF entry is created by the soft debug code to mark transitions to
645 * managed code done during invokes.
647 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
649 g_assert (ext->debugger_invoke);
651 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
653 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
655 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
660 if (((guint64)(*lmf)->previous_lmf) & 1) {
661 /* This LMF has the rip field set */
663 } else if ((*lmf)->rsp == 0) {
668 * The rsp field is set just before the call which transitioned to native
669 * code. Obtain the rip from the stack.
671 rip = *(guint64*)((*lmf)->rsp - sizeof(mgreg_t));
674 ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
676 * FIXME: ji == NULL can happen when a managed-to-native wrapper is interrupted
677 * in the soft debugger suspend code, since (*lmf)->rsp no longer points to the
688 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
691 new_ctx->rbp = (*lmf)->rbp;
692 new_ctx->rsp = (*lmf)->rsp;
694 new_ctx->rbx = (*lmf)->rbx;
695 new_ctx->r12 = (*lmf)->r12;
696 new_ctx->r13 = (*lmf)->r13;
697 new_ctx->r14 = (*lmf)->r14;
698 new_ctx->r15 = (*lmf)->r15;
700 new_ctx->rdi = (*lmf)->rdi;
701 new_ctx->rsi = (*lmf)->rsi;
704 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
715 * Called by resuming from a signal handler.
718 handle_signal_exception (gpointer obj, gboolean test_only)
720 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
722 static void (*restore_context) (MonoContext *);
724 if (!restore_context)
725 restore_context = mono_get_restore_context ();
727 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
729 if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
732 mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), test_only);
734 restore_context (&ctx);
738 * mono_arch_handle_exception:
740 * @ctx: saved processor state
741 * @obj: the exception object
744 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
746 #if defined(MONO_ARCH_USE_SIGACTION)
747 ucontext_t *ctx = (ucontext_t*)sigctx;
750 * Handling the exception in the signal handler is problematic, since the original
751 * signal is disabled, and we could run arbitrary code though the debugger. So
752 * resume into the normal stack and do most work there if possible.
754 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
755 guint64 sp = UCONTEXT_REG_RSP (ctx);
757 /* Pass the ctx parameter in TLS */
758 mono_arch_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
759 /* The others in registers */
760 UCONTEXT_REG_RDI (ctx) = (guint64)obj;
761 UCONTEXT_REG_RSI (ctx) = test_only;
763 /* Allocate a stack frame below the red zone */
765 /* The stack should be unaligned */
768 UCONTEXT_REG_RSP (ctx) = sp;
770 UCONTEXT_REG_RIP (ctx) = (guint64)handle_signal_exception;
776 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
778 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
781 mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), test_only);
783 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
790 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
792 #if defined(__native_client_codegen__) || defined(__native_client__)
793 printf("WARNING: mono_arch_sigctx_to_monoctx() called!\n");
796 #if defined(MONO_ARCH_USE_SIGACTION)
797 ucontext_t *ctx = (ucontext_t*)sigctx;
799 mctx->rax = UCONTEXT_REG_RAX (ctx);
800 mctx->rbx = UCONTEXT_REG_RBX (ctx);
801 mctx->rcx = UCONTEXT_REG_RCX (ctx);
802 mctx->rdx = UCONTEXT_REG_RDX (ctx);
803 mctx->rbp = UCONTEXT_REG_RBP (ctx);
804 mctx->rsp = UCONTEXT_REG_RSP (ctx);
805 mctx->rsi = UCONTEXT_REG_RSI (ctx);
806 mctx->rdi = UCONTEXT_REG_RDI (ctx);
807 mctx->rip = UCONTEXT_REG_RIP (ctx);
808 mctx->r12 = UCONTEXT_REG_R12 (ctx);
809 mctx->r13 = UCONTEXT_REG_R13 (ctx);
810 mctx->r14 = UCONTEXT_REG_R14 (ctx);
811 mctx->r15 = UCONTEXT_REG_R15 (ctx);
813 MonoContext *ctx = (MonoContext *)sigctx;
815 mctx->rax = ctx->rax;
816 mctx->rbx = ctx->rbx;
817 mctx->rcx = ctx->rcx;
818 mctx->rdx = ctx->rdx;
819 mctx->rbp = ctx->rbp;
820 mctx->rsp = ctx->rsp;
821 mctx->rsi = ctx->rsi;
822 mctx->rdi = ctx->rdi;
823 mctx->rip = ctx->rip;
824 mctx->r12 = ctx->r12;
825 mctx->r13 = ctx->r13;
826 mctx->r14 = ctx->r14;
827 mctx->r15 = ctx->r15;
832 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
834 #if defined(__native_client__) || defined(__native_client_codegen__)
835 printf("WARNING: mono_arch_monoctx_to_sigctx() called!\n");
838 #if defined(MONO_ARCH_USE_SIGACTION)
839 ucontext_t *ctx = (ucontext_t*)sigctx;
841 UCONTEXT_REG_RAX (ctx) = mctx->rax;
842 UCONTEXT_REG_RBX (ctx) = mctx->rbx;
843 UCONTEXT_REG_RCX (ctx) = mctx->rcx;
844 UCONTEXT_REG_RDX (ctx) = mctx->rdx;
845 UCONTEXT_REG_RBP (ctx) = mctx->rbp;
846 UCONTEXT_REG_RSP (ctx) = mctx->rsp;
847 UCONTEXT_REG_RSI (ctx) = mctx->rsi;
848 UCONTEXT_REG_RDI (ctx) = mctx->rdi;
849 UCONTEXT_REG_RIP (ctx) = mctx->rip;
850 UCONTEXT_REG_R12 (ctx) = mctx->r12;
851 UCONTEXT_REG_R13 (ctx) = mctx->r13;
852 UCONTEXT_REG_R14 (ctx) = mctx->r14;
853 UCONTEXT_REG_R15 (ctx) = mctx->r15;
855 MonoContext *ctx = (MonoContext *)sigctx;
857 ctx->rax = mctx->rax;
858 ctx->rbx = mctx->rbx;
859 ctx->rcx = mctx->rcx;
860 ctx->rdx = mctx->rdx;
861 ctx->rbp = mctx->rbp;
862 ctx->rsp = mctx->rsp;
863 ctx->rsi = mctx->rsi;
864 ctx->rdi = mctx->rdi;
865 ctx->rip = mctx->rip;
866 ctx->r12 = mctx->r12;
867 ctx->r13 = mctx->r13;
868 ctx->r14 = mctx->r14;
869 ctx->r15 = mctx->r15;
874 mono_arch_ip_from_context (void *sigctx)
876 #if defined(MONO_ARCH_USE_SIGACTION)
877 ucontext_t *ctx = (ucontext_t*)sigctx;
879 return (gpointer)UCONTEXT_REG_RIP (ctx);
881 MonoContext *ctx = sigctx;
882 return (gpointer)ctx->rip;
887 restore_soft_guard_pages (void)
889 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
890 if (jit_tls->stack_ovf_guard_base)
891 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
895 * this function modifies mctx so that when it is restored, it
896 * won't execcute starting at mctx.eip, but in a function that
897 * will restore the protection on the soft-guard pages and return back to
898 * continue at mctx.eip.
901 prepare_for_guard_pages (MonoContext *mctx)
904 sp = (gpointer)(mctx->rsp);
906 /* the return addr */
907 sp [0] = (gpointer)(mctx->rip);
908 mctx->rip = (guint64)restore_soft_guard_pages;
909 mctx->rsp = (guint64)sp;
913 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
915 void (*restore_context) (MonoContext *);
918 restore_context = mono_get_restore_context ();
919 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
921 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
923 prepare_for_guard_pages (&mctx);
924 restore_context (&mctx);
927 mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), FALSE);
929 prepare_for_guard_pages (&mctx);
930 restore_context (&mctx);
934 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
936 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_GREGS)
937 MonoException *exc = NULL;
938 ucontext_t *ctx = (ucontext_t*)sigctx;
939 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_RIP (sigctx), NULL);
944 exc = mono_domain_get ()->stack_overflow_ex;
946 mono_handle_native_sigsegv (SIGSEGV, sigctx);
948 /* setup a call frame on the real stack so that control is returned there
949 * and exception handling can continue.
950 * The frame looks like:
954 * 128 is the size of the red zone
956 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4 + 128;
959 sp = (gpointer)(UCONTEXT_REG_RSP (sigctx) & ~15);
960 sp = (gpointer)((char*)sp - frame_size);
961 /* the arguments must be aligned */
962 sp [-1] = (gpointer)UCONTEXT_REG_RIP (sigctx);
963 /* may need to adjust pointers in the new struct copy, depending on the OS */
964 memcpy (sp + 4, ctx, sizeof (ucontext_t));
965 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
966 UCONTEXT_REG_RIP (sigctx) = (unsigned long)altstack_handle_and_restore;
967 UCONTEXT_REG_RSP (sigctx) = (unsigned long)(sp - 1);
968 UCONTEXT_REG_RDI (sigctx) = (unsigned long)(sp + 4);
969 UCONTEXT_REG_RSI (sigctx) = (guint64)exc;
970 UCONTEXT_REG_RDX (sigctx) = stack_ovf;
975 mono_amd64_get_original_ip (void)
977 MonoLMF *lmf = mono_get_lmf ();
981 /* Reset the change to previous_lmf */
982 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf & ~1);
988 mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
990 guint8 *code, *start;
992 gpointer throw_trampoline;
993 MonoJumpInfo *ji = NULL;
994 GSList *unwind_ops = NULL;
995 const guint kMaxCodeSize = NACL_SIZE (128, 256);
997 start = code = mono_global_codeman_reserve (kMaxCodeSize);
999 /* We are in the frame of a managed method after a call */
1001 * We would like to throw the pending exception in such a way that it looks to
1002 * be thrown from the managed method.
1005 /* Save registers which might contain the return value of the call */
1006 amd64_push_reg (code, AMD64_RAX);
1007 amd64_push_reg (code, AMD64_RDX);
1009 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
1010 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
1013 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
1015 /* Obtain the pending exception */
1017 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
1018 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1020 amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
1022 amd64_call_reg (code, AMD64_R11);
1024 /* Check if it is NULL, and branch */
1025 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
1026 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
1028 /* exc != NULL branch */
1030 /* Save the exc on the stack */
1031 amd64_push_reg (code, AMD64_RAX);
1033 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
1035 /* Obtain the original ip and clear the flag in previous_lmf */
1037 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
1038 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1040 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
1042 amd64_call_reg (code, AMD64_R11);
1045 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, 8, 8);
1047 /* Pop saved stuff from the stack */
1048 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 6 * 8);
1050 /* Setup arguments for the throw trampoline */
1052 amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_R11, 8);
1053 /* The trampoline expects the caller ip to be pushed on the stack */
1054 amd64_push_reg (code, AMD64_RAX);
1056 /* Call the throw trampoline */
1058 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_throw_exception");
1059 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1061 throw_trampoline = mono_get_throw_exception ();
1062 amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
1064 /* We use a jump instead of a call so we can push the original ip on the stack */
1065 amd64_jump_reg (code, AMD64_R11);
1067 /* ex == NULL branch */
1068 mono_amd64_patch (br [0], code);
1070 /* Obtain the original ip and clear the flag in previous_lmf */
1072 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
1073 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1075 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
1077 amd64_call_reg (code, AMD64_R11);
1078 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
1080 /* Restore registers */
1081 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1082 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
1083 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1084 amd64_pop_reg (code, AMD64_RDX);
1085 amd64_pop_reg (code, AMD64_RAX);
1087 /* Return to original code */
1088 amd64_jump_reg (code, AMD64_R11);
1090 g_assert ((code - start) < kMaxCodeSize);
1092 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1095 *info = mono_tramp_info_create (g_strdup_printf ("throw_pending_exception"), start, code - start, ji, unwind_ops);
1100 static gpointer throw_pending_exception;
1103 * Called when a thread receives an async exception while executing unmanaged code.
1104 * Instead of checking for this exception in the managed-to-native wrapper, we hijack
1105 * the return address on the stack to point to a helper routine which throws the
1109 mono_arch_notify_pending_exc (void)
1111 MonoLMF *lmf = mono_get_lmf ();
1114 /* Not yet started */
1121 if ((guint64)lmf->previous_lmf & 1)
1122 /* Already hijacked or trampoline LMF entry */
1125 /* lmf->rsp is set just before making the call which transitions to unmanaged code */
1126 lmf->rip = *(guint64*)(lmf->rsp - 8);
1127 /* Signal that lmf->rip is set */
1128 lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
1130 *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
1134 mono_amd64_get_exception_trampolines (gboolean aot)
1136 MonoTrampInfo *info;
1137 GSList *tramps = NULL;
1139 mono_arch_get_throw_pending_exception (&info, aot);
1140 tramps = g_slist_prepend (tramps, info);
1142 /* LLVM needs different throw trampolines */
1143 get_throw_trampoline (&info, FALSE, TRUE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", aot);
1144 tramps = g_slist_prepend (tramps, info);
1146 get_throw_trampoline (&info, FALSE, TRUE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", aot);
1147 tramps = g_slist_prepend (tramps, info);
1149 get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", FALSE);
1150 tramps = g_slist_prepend (tramps, info);
1156 mono_arch_exceptions_init (void)
1161 if (mono_aot_only) {
1162 throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
1163 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
1164 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
1165 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
1166 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
1167 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
1168 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
1170 /* Call this to avoid initialization races */
1171 throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
1173 tramps = mono_amd64_get_exception_trampolines (FALSE);
1174 for (l = tramps; l; l = l->next) {
1175 MonoTrampInfo *info = l->data;
1177 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
1178 mono_save_trampoline_xdebug_info (info);
1179 mono_tramp_info_free (info);
1181 g_slist_free (tramps);
1188 * The mono_arch_unwindinfo* methods are used to build and add
1189 * function table info for each emitted method from mono. On Winx64
1190 * the seh handler will not be called if the mono methods are not
1191 * added to the function table.
1193 * We should not need to add non-volatile register info to the
1194 * table since mono stores that info elsewhere. (Except for the register
1198 #define MONO_MAX_UNWIND_CODES 22
1200 typedef union _UNWIND_CODE {
1203 guchar UnwindOp : 4;
1206 gushort FrameOffset;
1207 } UNWIND_CODE, *PUNWIND_CODE;
1209 typedef struct _UNWIND_INFO {
1212 guchar SizeOfProlog;
1213 guchar CountOfCodes;
1214 guchar FrameRegister : 4;
1215 guchar FrameOffset : 4;
1216 /* custom size for mono allowing for mono allowing for*/
1217 /*UWOP_PUSH_NONVOL ebp offset = 21*/
1218 /*UWOP_ALLOC_LARGE : requires 2 or 3 offset = 20*/
1219 /*UWOP_SET_FPREG : requires 2 offset = 17*/
1220 /*UWOP_PUSH_NONVOL offset = 15-0*/
1221 UNWIND_CODE UnwindCode[MONO_MAX_UNWIND_CODES];
1223 /* UNWIND_CODE MoreUnwindCode[((CountOfCodes + 1) & ~1) - 1];
1225 * OPTIONAL ULONG ExceptionHandler;
1226 * OPTIONAL ULONG FunctionEntry;
1228 * OPTIONAL ULONG ExceptionData[]; */
1229 } UNWIND_INFO, *PUNWIND_INFO;
1233 RUNTIME_FUNCTION runtimeFunction;
1234 UNWIND_INFO unwindInfo;
1235 } MonoUnwindInfo, *PMonoUnwindInfo;
1238 mono_arch_unwindinfo_create (gpointer* monoui)
1240 PMonoUnwindInfo newunwindinfo;
1241 *monoui = newunwindinfo = g_new0 (MonoUnwindInfo, 1);
1242 newunwindinfo->unwindInfo.Version = 1;
1246 mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1248 PMonoUnwindInfo unwindinfo;
1249 PUNWIND_CODE unwindcode;
1252 mono_arch_unwindinfo_create (monoui);
1254 unwindinfo = (MonoUnwindInfo*)*monoui;
1256 if (unwindinfo->unwindInfo.CountOfCodes >= MONO_MAX_UNWIND_CODES)
1257 g_error ("Larger allocation needed for the unwind information.");
1259 codeindex = MONO_MAX_UNWIND_CODES - (++unwindinfo->unwindInfo.CountOfCodes);
1260 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1261 unwindcode->UnwindOp = 0; /*UWOP_PUSH_NONVOL*/
1262 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1263 unwindcode->OpInfo = reg;
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_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1274 PMonoUnwindInfo unwindinfo;
1275 PUNWIND_CODE unwindcode;
1278 mono_arch_unwindinfo_create (monoui);
1280 unwindinfo = (MonoUnwindInfo*)*monoui;
1282 if (unwindinfo->unwindInfo.CountOfCodes + 1 >= MONO_MAX_UNWIND_CODES)
1283 g_error ("Larger allocation needed for the unwind information.");
1285 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += 2);
1286 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1287 unwindcode->FrameOffset = 0; /*Assuming no frame pointer offset for mono*/
1289 unwindcode->UnwindOp = 3; /*UWOP_SET_FPREG*/
1290 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1291 unwindcode->OpInfo = reg;
1293 unwindinfo->unwindInfo.FrameRegister = reg;
1295 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1296 g_error ("Adding unwind info in wrong order.");
1298 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1302 mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size )
1304 PMonoUnwindInfo unwindinfo;
1305 PUNWIND_CODE unwindcode;
1309 mono_arch_unwindinfo_create (monoui);
1311 unwindinfo = (MonoUnwindInfo*)*monoui;
1314 g_error ("Stack allocation must be equal to or greater than 0x8.");
1318 else if (size <= 0x7FFF8)
1323 if (unwindinfo->unwindInfo.CountOfCodes + codesneeded > MONO_MAX_UNWIND_CODES)
1324 g_error ("Larger allocation needed for the unwind information.");
1326 codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += codesneeded);
1327 unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1329 if (codesneeded == 1) {
1330 /*The size of the allocation is
1331 (the number in the OpInfo member) times 8 plus 8*/
1332 unwindcode->OpInfo = (size - 8)/8;
1333 unwindcode->UnwindOp = 2; /*UWOP_ALLOC_SMALL*/
1336 if (codesneeded == 3) {
1337 /*the unscaled size of the allocation is recorded
1338 in the next two slots in little-endian format*/
1339 *((unsigned int*)(&unwindcode->FrameOffset)) = size;
1341 unwindcode->OpInfo = 1;
1344 /*the size of the allocation divided by 8
1345 is recorded in the next slot*/
1346 unwindcode->FrameOffset = size/8;
1348 unwindcode->OpInfo = 0;
1351 unwindcode->UnwindOp = 1; /*UWOP_ALLOC_LARGE*/
1354 unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1356 if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1357 g_error ("Adding unwind info in wrong order.");
1359 unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1363 mono_arch_unwindinfo_get_size (gpointer monoui)
1365 PMonoUnwindInfo unwindinfo;
1369 unwindinfo = (MonoUnwindInfo*)monoui;
1370 return (8 + sizeof (MonoUnwindInfo)) -
1371 (sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
1375 MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
1379 PMonoUnwindInfo targetinfo;
1380 MonoDomain *domain = mono_domain_get ();
1382 ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
1386 pos = (guint64)(((char*)ji->code_start) + ji->code_size);
1388 targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
1390 targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
1392 return &targetinfo->runtimeFunction;
1396 mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
1398 PMonoUnwindInfo unwindinfo, targetinfo;
1400 guint64 targetlocation;
1404 unwindinfo = (MonoUnwindInfo*)*monoui;
1405 targetlocation = (guint64)&(((guchar*)code)[code_size]);
1406 targetinfo = (PMonoUnwindInfo) ALIGN_TO(targetlocation, 8);
1408 unwindinfo->runtimeFunction.EndAddress = code_size;
1409 unwindinfo->runtimeFunction.UnwindData = ((guchar*)&targetinfo->unwindInfo) - ((guchar*)code);
1411 memcpy (targetinfo, unwindinfo, sizeof (MonoUnwindInfo) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
1413 codecount = unwindinfo->unwindInfo.CountOfCodes;
1415 memcpy (&targetinfo->unwindInfo.UnwindCode[0], &unwindinfo->unwindInfo.UnwindCode[MONO_MAX_UNWIND_CODES-codecount],
1416 sizeof (UNWIND_CODE) * unwindinfo->unwindInfo.CountOfCodes);
1419 g_free (unwindinfo);
1422 RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
1427 #if MONO_SUPPORT_TASKLETS
1428 MonoContinuationRestore
1429 mono_tasklets_arch_restore (void)
1431 static guint8* saved = NULL;
1432 guint8 *code, *start;
1433 int cont_reg = AMD64_R9; /* register usable on both call conventions */
1434 const guint kMaxCodeSize = NACL_SIZE (64, 128);
1438 return (MonoContinuationRestore)saved;
1439 code = start = mono_global_codeman_reserve (kMaxCodeSize);
1440 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1441 /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
1442 * state is in AMD64_ARG_REG2 ($rdx or $rsi)
1443 * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
1444 * We move cont to cont_reg since we need both rcx and rdi for the copy
1445 * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
1447 amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
1448 amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
1449 /* setup the copy of the stack */
1450 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
1451 amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
1453 amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
1454 amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
1455 amd64_prefix (code, X86_REP_PREFIX);
1458 /* now restore the registers from the LMF */
1459 amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, lmf), 8);
1460 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbx), 8);
1461 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbp), 8);
1462 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
1463 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
1464 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
1465 #if !defined(__native_client_codegen__)
1466 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
1469 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
1470 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
1472 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsp), 8);
1474 /* restore the lmf chain */
1475 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1476 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1478 /* state is already in rax */
1479 amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1480 g_assert ((code - start) <= kMaxCodeSize);
1482 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
1485 return (MonoContinuationRestore)saved;
1490 * mono_arch_setup_resume_sighandler_ctx:
1492 * Setup CTX so execution continues at FUNC.
1495 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1498 * When resuming from a signal handler, the stack should be misaligned, just like right after
1501 if ((((guint64)MONO_CONTEXT_GET_SP (ctx)) % 16) == 0)
1502 MONO_CONTEXT_SET_SP (ctx, (guint64)MONO_CONTEXT_GET_SP (ctx) - 8);
1503 MONO_CONTEXT_SET_IP (ctx, func);