Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / tramp-amd64.c
index 8269edcb1014ff3bde67995a82684f394ca284e3..365b0ae81630cb6ef7d753bb016931ba1d2bcaf6 100644 (file)
@@ -1,9 +1,11 @@
-/*
- * tramp-amd64.c: JIT trampoline code for amd64
+/**
+ * \file
+ * JIT trampoline code for amd64
  *
  * Authors:
  *   Dietmar Maurer (dietmar@ximian.com)
  *   Zoltan Varga (vargaz@gmail.com)
+ *   Johan Lorensson (lateralusx.github@gmail.com)
  *
  * (C) 2001 Ximian, Inc.
  * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
@@ -18,7 +20,6 @@
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/marshal.h>
 #include <mono/metadata/tabledefs.h>
-#include <mono/metadata/mono-debug-debugger.h>
 #include <mono/metadata/profiler-private.h>
 #include <mono/metadata/gc-internals.h>
 #include <mono/arch/amd64/amd64-codegen.h>
 #include "mini-amd64.h"
 #include "debugger-agent.h"
 
+#ifdef ENABLE_INTERPRETER
+#include "interp/interp.h"
+#endif
+
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
@@ -54,7 +59,7 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 
        this_reg = mono_arch_get_this_arg_reg (NULL);
 
-       start = code = (guint8 *)mono_domain_code_reserve (domain, size);
+       start = code = (guint8 *)mono_domain_code_reserve (domain, size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
 
        unwind_ops = mono_arch_get_cie_program ();
 
@@ -63,9 +68,10 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
        amd64_mov_reg_imm (code, AMD64_RAX, addr);
        amd64_jump_reg (code, AMD64_RAX);
        g_assert ((code - start) < size);
+       g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
 
        mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
+       MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m));
 
        mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
 
@@ -75,10 +81,10 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 /*
  * mono_arch_get_static_rgctx_trampoline:
  *
- *   Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
+ *   Create a trampoline which sets RGCTX_REG to ARG, then jumps to ADDR.
  */
 gpointer
-mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
+mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
 {
        guint8 *code, *start;
        GSList *unwind_ops;
@@ -96,16 +102,17 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
                buf_len = 30;
 #endif
 
-       start = code = (guint8 *)mono_domain_code_reserve (domain, buf_len);
+       start = code = (guint8 *)mono_domain_code_reserve (domain, buf_len + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
 
        unwind_ops = mono_arch_get_cie_program ();
 
-       amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
+       amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, arg);
        amd64_jump_code (code, addr);
        g_assert ((code - start) < buf_len);
+       g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
 
        mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
 
        mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
 
@@ -137,6 +144,7 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
        if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
                if (code [-5] != 0xe8) {
                        if (can_write) {
+                               g_assert ((guint64)(orig_code - 11) % 8 == 0);
                                InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
                                VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
                        }
@@ -155,7 +163,7 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
                                addr = thunk_start;
                                g_assert ((((guint64)(addr)) >> 32) == 0);
                                mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
-                               mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+                               MONO_PROFILER_RAISE (jit_code_buffer, (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
                        }
                        if (can_write) {
                                InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
@@ -190,7 +198,7 @@ mono_arch_create_llvm_native_thunk (MonoDomain *domain, guint8 *addr)
        *(guint64*)thunk_code = (guint64)addr;
        addr = thunk_start;
        mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
-       mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
        return addr;
 }
 #endif /* !DISABLE_JIT */
@@ -232,12 +240,12 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        MonoJumpInfo *ji = NULL;
        const guint kMaxCodeSize = 630;
 
-       if (tramp_type == MONO_TRAMPOLINE_JUMP || tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
+       if (tramp_type == MONO_TRAMPOLINE_JUMP)
                has_caller = FALSE;
        else
                has_caller = TRUE;
 
-       code = buf = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize);
+       code = buf = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
 
        /* Compute stack frame size and offsets */
        offset = 0;
@@ -306,6 +314,7 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        orig_rsp_to_rbp_offset -= sizeof(mgreg_t);
        amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
        mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
+       mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
 
        /* Compute the trampoline address from the return address */
@@ -371,43 +380,38 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        mono_amd64_patch (br [0], code);
        //amd64_breakpoint (code);
 
-       if (tramp_type != MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
-               /* Obtain the trampoline argument which is encoded in the instruction stream */
-               if (aot) {
-                       /* Load the GOT offset */
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
-                       /*
-                        * r11 points to a call *<offset>(%rip) instruction, load the
-                        * pc-relative offset from the instruction itself.
-                        */
-                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 3, 4);
-                       /* 7 is the length of the call, 8 is the offset to the next got slot */
-                       amd64_alu_reg_imm_size (code, X86_ADD, AMD64_RAX, 7 + sizeof (gpointer), sizeof(gpointer));
-                       /* Compute the address of the GOT slot */
-                       amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
-                       /* Load the value */
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
-               } else {                        
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
-                       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
-                       amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
-                       amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
-                       br [0] = code;
-                       x86_branch8 (code, X86_CC_NE, 6, FALSE);
-                       /* 32 bit immediate */
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
-                       br [1] = code;
-                       x86_jump8 (code, 10);
-                       /* 64 bit immediate */
-                       mono_amd64_patch (br [0], code);
-                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
-                       mono_amd64_patch (br [1], code);
-               }
-               amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
+       /* Obtain the trampoline argument which is encoded in the instruction stream */
+       if (aot) {
+               /* Load the GOT offset */
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
+               /*
+                * r11 points to a call *<offset>(%rip) instruction, load the
+                * pc-relative offset from the instruction itself.
+                */
+               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 3, 4);
+               /* 7 is the length of the call, 8 is the offset to the next got slot */
+               amd64_alu_reg_imm_size (code, X86_ADD, AMD64_RAX, 7 + sizeof (gpointer), sizeof(gpointer));
+               /* Compute the address of the GOT slot */
+               amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
+               /* Load the value */
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
        } else {
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * sizeof(mgreg_t)), sizeof(mgreg_t));
-               amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
+               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
+               amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
+               amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
+               br [0] = code;
+               x86_branch8 (code, X86_CC_NE, 6, FALSE);
+               /* 32 bit immediate */
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
+               br [1] = code;
+               x86_jump8 (code, 10);
+               /* 64 bit immediate */
+               mono_amd64_patch (br [0], code);
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
+               mono_amd64_patch (br [1], code);
        }
+       amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
 
        /* Save LMF begin */
 
@@ -505,7 +509,13 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
         * We have an exception we want to throw in the caller's frame, so pop
         * the trampoline frame and throw from the caller.
         */
+#if TARGET_WIN32
+       amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
+       amd64_pop_reg (code, AMD64_RBP);
+       mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
+#else
        amd64_leave (code);
+#endif
        /* We are in the parent frame, the exception is in rax */
        /*
         * EH is initialized after trampolines, so get the address of the variable
@@ -537,7 +547,13 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
                amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)));
 
        /* Restore stack */
+#if TARGET_WIN32
+       amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
+       amd64_pop_reg (code, AMD64_RBP);
+       mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
+#else
        amd64_leave (code);
+#endif
        cfa_offset -= sizeof (mgreg_t);
        mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
 
@@ -551,9 +567,10 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        }
 
        g_assert ((code - buf) <= kMaxCodeSize);
+       g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE));
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
 
        tramp_name = mono_get_generic_trampoline_name (tramp_type);
        *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
@@ -610,7 +627,7 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
                *code_len = size;
 
        mono_arch_flush_icache (buf, size);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type)));
 
        return buf;
 }      
@@ -642,7 +659,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
 
        tramp_size = 64 + 8 * depth;
 
-       code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size);
+       code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
 
        unwind_ops = mono_arch_get_cie_program ();
 
@@ -689,8 +706,10 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
 
        g_free (rgctx_null_jumps);
 
-       /* move the rgctx pointer to the VTABLE register */
-       amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
+       if (MONO_ARCH_VTABLE_REG != AMD64_ARG_REG1) {
+               /* move the rgctx pointer to the VTABLE register */
+               amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
+       }
 
        if (aot) {
                code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
@@ -703,9 +722,10 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        }
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
 
        g_assert (code - buf <= tramp_size);
+       g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
 
        char *name = mono_get_rgctx_fetch_trampoline_name (slot);
        *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
@@ -725,7 +745,7 @@ mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboo
        g_assert (aot);
        tramp_size = 64;
 
-       code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size);
+       code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
 
        unwind_ops = mono_arch_get_cie_program ();
 
@@ -739,9 +759,10 @@ mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboo
        amd64_jump_reg (code, AMD64_R11);
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
 
        g_assert (code - buf <= tramp_size);
+       g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
 
        if (info)
                *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
@@ -763,84 +784,6 @@ mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
 }
 #endif /* !DISABLE_JIT */
 
-gpointer
-mono_amd64_handler_block_trampoline_helper (void)
-{
-       MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
-       return jit_tls->handler_block_return_address;
-}
-
-#ifndef DISABLE_JIT
-gpointer
-mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
-{
-       guint8 *code, *buf;
-       int tramp_size = 64;
-       MonoJumpInfo *ji = NULL;
-       GSList *unwind_ops;
-
-       code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size);
-
-       unwind_ops = mono_arch_get_cie_program ();
-
-       /*
-        * This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
-        * We get here from the ret emitted by CEE_ENDFINALLY.
-        * The stack is misaligned.
-        */
-       /* Align the stack before the call to mono_amd64_handler_block_trampoline_helper() */
-#ifdef TARGET_WIN32
-       /* Also make room for the "register parameter stack area" as specified by the Windows x64 ABI (4 64-bit registers) */
-       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8 + 4 * 8);
-#else
-       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
-#endif
-       if (aot) {
-               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_handler_block_trampoline_helper");
-               amd64_call_reg (code, AMD64_R11);
-       } else {
-               amd64_mov_reg_imm (code, AMD64_RAX, mono_amd64_handler_block_trampoline_helper);
-               amd64_call_reg (code, AMD64_RAX);
-       }
-       /* Undo stack alignment */
-#ifdef TARGET_WIN32
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8 + 4 * 8);
-#else
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
-#endif
-       /* Save the result to the stack */
-       amd64_push_reg (code, AMD64_RAX);
-#ifdef TARGET_WIN32
-       /* Make room for the "register parameter stack area" as specified by the Windows x64 ABI (4 64-bit registers) */
-       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 4 * 8);
-#endif
-       if (aot) {
-               char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
-               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
-               amd64_mov_reg_reg (code, AMD64_RAX, AMD64_R11, 8);
-       } else {
-               amd64_mov_reg_imm (code, AMD64_RAX, mono_get_trampoline_func (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD));
-       }
-       /* The stack is aligned */
-       amd64_call_reg (code, AMD64_RAX);
-#ifdef TARGET_WIN32
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 4 * 8);
-#endif
-       /* Load return address */
-       amd64_pop_reg (code, AMD64_RAX);
-       /* The stack is misaligned, thats what the code we branch to expects */
-       amd64_jump_reg (code, AMD64_RAX);
-
-       mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
-       g_assert (code - buf <= tramp_size);
-
-       *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
-
-       return buf;
-}
-#endif /* !DISABLE_JIT */
-
 /*
  * mono_arch_get_call_target:
  *
@@ -887,7 +830,7 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        GSList *unwind_ops = NULL;
        MonoJumpInfo *ji = NULL;
 
-       code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size);
+       code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
 
        framesize = 0;
 #ifdef TARGET_WIN32
@@ -913,6 +856,7 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
 
        amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
        mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
+       mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
 
        gregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
@@ -955,14 +899,21 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), sizeof (mgreg_t));
        amd64_mov_membase_reg (code, AMD64_RBP, sizeof (mgreg_t), AMD64_R11, sizeof (mgreg_t));
 
+#if TARGET_WIN32
+       amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
+       amd64_pop_reg (code, AMD64_RBP);
+       mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
+#else
        amd64_leave (code);
+#endif
        cfa_offset -= sizeof (mgreg_t);
        mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
        amd64_ret (code);
 
        mono_arch_flush_icache (code, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
        g_assert (code - buf <= tramp_size);
+       g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE));
 
        const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
        *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
@@ -970,87 +921,165 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        return buf;
 }
 
+/*
+ * mono_arch_get_enter_icall_trampoline:
+ *
+ *   A trampoline that handles the transition from interpreter into native
+ *   world. It requiers to set up a descriptor (InterpMethodArguments), so the
+ *   trampoline can translate the arguments into the native calling convention.
+ *
+ *   See also `build_args_from_sig ()` in interp.c.
+ */
 gpointer
 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
 {
-       guint8 *start = NULL;
-       guint8 *code;
+#ifdef ENABLE_INTERPRETER
+       const int gregs_num = INTERP_ICALL_TRAMP_IARGS;
+       const int fregs_num = INTERP_ICALL_TRAMP_FARGS;
+       guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
-       guint8 *exits[4];
-       int arg_regs[] = {AMD64_ARG_REG1, AMD64_ARG_REG2, AMD64_ARG_REG3, AMD64_ARG_REG4};
-       int i, gregs_offset;
+       static int farg_regs[] = {AMD64_XMM0, AMD64_XMM1, AMD64_XMM2};
+       int buf_len, i, framesize = 0, off_rbp, off_methodargs, off_targetaddr;
+
+       buf_len = 512 + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0);
+       start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
+
+       off_rbp = -framesize;
+
+       framesize += sizeof (mgreg_t);
+       off_methodargs = -framesize;
 
-       start = code = (guint8 *) mono_global_codeman_reserve (256);
+       framesize += sizeof (mgreg_t);
+       off_targetaddr = -framesize;
 
-       // amd64_breakpoint (code);
+       framesize += (gregs_num - PARAM_REGS) * sizeof (mgreg_t);
+
+       amd64_push_reg (code, AMD64_RBP);
+       amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (mgreg_t));
+       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
+
+       /* save InterpMethodArguments* onto stack */
+       amd64_mov_membase_reg (code, AMD64_RBP, off_methodargs, AMD64_ARG_REG2, sizeof (mgreg_t));
 
        /* save target address on stack */
-       amd64_push_reg (code, AMD64_ARG_REG1);
-       amd64_push_reg (code, AMD64_RAX);
+       amd64_mov_membase_reg (code, AMD64_RBP, off_targetaddr, AMD64_ARG_REG1, sizeof (mgreg_t));
 
-       /* load pointer to MethodArguments* into R11 */
+       /* load pointer to InterpMethodArguments* into R11 */
        amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, 8);
        
-       /* TODO: do float stuff first */
-
-       /* move ilen into RAX */ // TODO: struct offset
-       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 0, 8);
-       /* load pointer to iregs into R11 */ // TODO: struct offset
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 8, 8);
+       /* move flen into RAX */
+       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, flen), sizeof (mgreg_t));
+       /* load pointer to fargs into R11 */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, fargs), sizeof (mgreg_t));
 
-       for (int i = 0; i < 4; i++) {
+       for (i = 0; i < fregs_num; ++i) {
                amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
-               exits [i] = code;
+               label_fexits [i] = code;
                x86_branch8 (code, X86_CC_Z, 0, FALSE);
 
-               amd64_mov_reg_membase (code, arg_regs [i], AMD64_R11, i * sizeof (gpointer), 8);
+               amd64_sse_movsd_reg_membase (code, farg_regs [i], AMD64_R11, i * sizeof (double));
                amd64_dec_reg_size (code, AMD64_RAX, 1);
        }
 
-       for (int i = 0; i < 4; i++) {
-               x86_patch (exits [i], code);
-       }
+       for (i = 0; i < fregs_num; i++)
+               x86_patch (label_fexits [i], code);
 
+       /* load pointer to InterpMethodArguments* into R11 */
+       amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, sizeof (mgreg_t));
+       /* move ilen into RAX */
+       amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, ilen), sizeof (mgreg_t));
 
-       amd64_pop_reg (code, AMD64_RAX);
-       amd64_pop_reg (code, AMD64_R11);
+       int stack_offset = 0;
+       for (i = 0; i < gregs_num; i++) {
+               amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
+               label_gexits [i] = code;
+               x86_branch32 (code, X86_CC_Z, 0, FALSE);
 
-       /* tail call into native function */
-       amd64_jump_reg (code, AMD64_R11);
+               /* load pointer to InterpMethodArguments* into R11 */
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
+               /* load pointer to iargs into R11 */
+               amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, iargs), sizeof (mgreg_t));
 
-#if 0
-       /* Restore all registers except %rip and %r11 */
-       gregs_offset = MONO_STRUCT_OFFSET (MonoContext, gregs);
-       for (i = 0; i < AMD64_NREG; ++i) {
-               if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_R8 && i != AMD64_R9 && i != AMD64_R10 && i != AMD64_R11)
-                       amd64_mov_reg_membase (code, i, AMD64_R11, gregs_offset + (i * 8), 8);
+               if (i < PARAM_REGS) {
+                       amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
+               } else {
+                       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
+                       amd64_mov_membase_reg (code, AMD64_RSP, stack_offset, AMD64_R11, sizeof (mgreg_t));
+                       stack_offset += sizeof (mgreg_t);
+               }
+               amd64_dec_reg_size (code, AMD64_RAX, 1);
        }
 
-       /*
-        * The context resides on the stack, in the stack frame of the
-        * caller of this function.  The stack pointer that we need to
-        * restore is potentially many stack frames higher up, so the
-        * distance between them can easily be more than the red zone
-        * size.  Hence the stack pointer can be restored only after
-        * we have finished loading everything from the context.
-        */
-       amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11,  gregs_offset + (AMD64_RSP * 8), 8);
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11,  gregs_offset + (AMD64_RIP * 8), 8);
-       amd64_mov_reg_reg (code, AMD64_RSP, AMD64_R8, 8);
+       for (i = 0; i < gregs_num; i++)
+               x86_patch (label_gexits [i], code);
 
-       /* jump to the saved IP */
-       amd64_jump_reg (code, AMD64_R11);
-#endif
+       /* load target addr */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_targetaddr, sizeof (mgreg_t));
+
+       /* call into native function */
+       amd64_call_reg (code, AMD64_R11);
+
+       /* load InterpMethodArguments */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
+
+       /* load is_float_ret */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, is_float_ret), sizeof (mgreg_t));
+
+       /* check if a float return value is expected */
+       amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
+
+       label_is_float_ret = code;
+       x86_branch8 (code, X86_CC_NZ, 0, FALSE);
+
+       /* greg return */
+       /* load InterpMethodArguments */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
+       /* load retval */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, retval), sizeof (mgreg_t));
+
+       amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
+       label_leave_tramp [0] = code;
+       x86_branch8 (code, X86_CC_Z, 0, FALSE);
+
+       amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RAX, sizeof (mgreg_t));
+
+       label_leave_tramp [1] = code;
+       x86_jump8 (code, 0);
 
+       /* freg return */
+       x86_patch (label_is_float_ret, code);
+       /* load InterpMethodArguments */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
+       /* load retval */
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, retval), sizeof (mgreg_t));
+
+       amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
+       label_leave_tramp [2] = code;
+       x86_branch8 (code, X86_CC_Z, 0, FALSE);
+
+       amd64_sse_movsd_membase_reg (code, AMD64_R11, 0, AMD64_XMM0);
+
+       for (i = 0; i < 3; i++)
+               x86_patch (label_leave_tramp [i], code);
+
+       amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
+       amd64_pop_reg (code, AMD64_RBP);
+       amd64_ret (code);
+
+       g_assert (code - start < buf_len);
 
        mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
 
        if (info)
                *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
 
        return start;
+#else
+       g_assert_not_reached ();
+       return NULL;
+#endif /* ENABLE_INTERPRETER */
 }
 #endif /* !DISABLE_JIT */
 
@@ -1063,7 +1092,7 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 }
 
 gpointer
-mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
+mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
 {
        g_assert_not_reached ();
        return NULL;
@@ -1097,13 +1126,6 @@ mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboo
        return NULL;
 }
 
-gpointer
-mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
-{
-        g_assert_not_reached ();
-        return NULL;
-}
-
 void
 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
 {