Merge pull request #1991 from esdrubal/seq_test_fix
[mono.git] / mono / mini / tramp-amd64.c
old mode 100755 (executable)
new mode 100644 (file)
index d9c2ee0..edeb4d3
@@ -18,7 +18,6 @@
 #include <mono/metadata/marshal.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/mono-debug-debugger.h>
-#include <mono/metadata/monitor.h>
 #include <mono/metadata/profiler-private.h>
 #include <mono/metadata/gc-internal.h>
 #include <mono/arch/amd64/amd64-codegen.h>
 
 #include "mini.h"
 #include "mini-amd64.h"
+#include "debugger-agent.h"
 
 #if defined(__native_client_codegen__) && defined(__native_client__)
 #include <malloc.h>
 #include <nacl/nacl_dyncode.h>
 #endif
 
+#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
+
 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
 
 /*
@@ -48,6 +50,7 @@ gpointer
 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 {
        guint8 *code, *start;
+       GSList *unwind_ops;
        int this_reg, size = NACL_SIZE (20, 32);
 
        MonoDomain *domain = mono_domain_get ();
@@ -56,6 +59,8 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 
        start = code = mono_domain_code_reserve (domain, size);
 
+       unwind_ops = mono_arch_get_cie_program ();
+
        amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
        /* FIXME: Optimize this */
        amd64_mov_reg_imm (code, AMD64_RAX, addr);
@@ -67,6 +72,8 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
        mono_arch_flush_icache (start, code - start);
        mono_profiler_code_buffer_new (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);
+
        return start;
 }
 
@@ -79,6 +86,7 @@ gpointer
 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
 {
        guint8 *code, *start;
+       GSList *unwind_ops;
        int buf_len;
 
        MonoDomain *domain = mono_domain_get ();
@@ -95,6 +103,8 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
 
        start = code = mono_domain_code_reserve (domain, buf_len);
 
+       unwind_ops = mono_arch_get_cie_program ();
+
        amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
        amd64_jump_code (code, addr);
        g_assert ((code - start) < buf_len);
@@ -103,6 +113,8 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
        mono_arch_flush_icache (start, code - start);
        mono_profiler_code_buffer_new (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);
+
        return start;
 }
 
@@ -136,6 +148,11 @@ mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_off
        return start;
 }
 
+#ifdef _WIN64
+// Workaround lack of Valgrind support for 64-bit Windows
+#define VALGRIND_DISCARD_TRANSLATIONS(...)
+#endif
+
 /*
  * mono_arch_patch_callsite:
  *
@@ -163,19 +180,8 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
                        gboolean disp_32bit = ((((gint64)addr - (gint64)orig_code)) < (1 << 30)) && ((((gint64)addr - (gint64)orig_code)) > -(1 << 30));
 
                        if ((((guint64)(addr)) >> 32) != 0 && !disp_32bit) {
-#ifdef MONO_ARCH_NOMAP32BIT
-                               /* Print some diagnostics */
-                               MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
-                               if (ji)
-                                       fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (jinfo_get_method (ji), TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
-                               fprintf (stderr, "Addr: %p\n", addr);
-                               ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
-                               if (ji)
-                                       fprintf (stderr, "Callee: %s\n", mono_method_full_name (jinfo_get_method (ji), TRUE));
-                               g_assert_not_reached ();
-#else
                                /* 
-                                * This might happen when calling AOTed code. Create a thunk.
+                                * This might happen with LLVM or when calling AOTed code. Create a thunk.
                                 */
                                guint8 *thunk_start, *thunk_code;
 
@@ -186,7 +192,6 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
                                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);
-#endif
                        }
                        if (can_write) {
                                InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
@@ -288,116 +293,9 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
 
 #endif /* __native_client_codegen__ */
 
-
        InterlockedExchangePointer (plt_jump_table_entry, addr);
 }
 
-static gpointer
-get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
-{
-       guint8 buf [10];
-       gint32 disp;
-       MonoJitInfo *ji = NULL;
-
-#ifdef ENABLE_LLVM
-       /* code - 9 might be before the start of the method */
-       /* FIXME: Avoid this expensive call somehow */
-       ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
-#endif
-
-       mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 9, buf, sizeof (buf));
-       code = buf + 9;
-
-       *displacement = 0;
-
-       code -= 7;
-
-       if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
-               /* call OFFSET(%rip) */
-               g_assert_not_reached ();
-               *displacement = *(guint32*)(code + 3);
-               return (gpointer*)(code + disp + 7);
-       } else {
-               g_assert_not_reached ();
-               return NULL;
-       }
-}
-
-static gpointer*
-get_vcall_slot_addr (guint8* code, mgreg_t *regs)
-{
-       gpointer vt;
-       int displacement;
-       vt = get_vcall_slot (code, regs, &displacement);
-       if (!vt)
-               return NULL;
-       return (gpointer*)((char*)vt + displacement);
-}
-
-void
-mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
-{
-       guint8 buf [16];
-       MonoJitInfo *ji = NULL;
-       gboolean can_write;
-       gpointer tramp = mini_get_nullified_class_init_trampoline ();
-
-       if (mono_use_llvm) {
-               /* code - 7 might be before the start of the method */
-               /* FIXME: Avoid this expensive call somehow */
-               ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
-       }
-
-       can_write = mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 7, buf, sizeof (buf));
-
-       if (!can_write)
-               return;
-
-       /* 
-        * A given byte sequence can match more than case here, so we have to be
-        * really careful about the ordering of the cases. Longer sequences
-        * come first.
-        */
-       if ((buf [0] == 0x41) && (buf [1] == 0xff) && (buf [2] == 0x15)) {
-               gpointer *vtable_slot;
-
-               /* call *<OFFSET>(%rip) */
-               vtable_slot = get_vcall_slot_addr (code, regs);
-               g_assert (vtable_slot);
-
-               *vtable_slot = tramp;
-       } else if (buf [2] == 0xe8) {
-               /* call <TARGET> */
-               //guint8 *buf = code - 2;
-
-               /* 
-                * It would be better to replace the call with nops, but that doesn't seem
-                * to work on SMP machines even when the whole call is inside a cache line.
-                * Patching the call address seems to work.
-                */
-               /*
-               buf [0] = 0x66;
-               buf [1] = 0x66;
-               buf [2] = 0x90;
-               buf [3] = 0x66;
-               buf [4] = 0x90;
-               */
-
-               mono_arch_patch_callsite (code - 5, code, tramp);
-       } else if ((buf [5] == 0xff) && x86_modrm_mod (buf [6]) == 3 && x86_modrm_reg (buf [6]) == 2) {
-               /* call *<reg> */
-               /* Generated by the LLVM JIT or on platforms without MAP_32BIT set */
-               mono_arch_patch_callsite (code - 13, code, tramp);
-       } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
-               /* Already changed by another thread */
-               ;
-       } else {
-               printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", buf [0], buf [1], buf [2], buf [3],
-                       buf [4], buf [5], buf [6]);
-               g_assert_not_reached ();
-       }
-}
-
 static void
 stack_unaligned (MonoTrampolineType tramp_type)
 {
@@ -410,8 +308,8 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 {
        char *tramp_name;
        guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
-       int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, saved_regs_offset;
-       int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
+       int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, ctx_offset, saved_regs_offset;
+       int r11_save_offset, saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
        gboolean has_caller;
        GSList *unwind_ops = NULL;
        MonoJumpInfo *ji = NULL;
@@ -428,14 +326,39 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        code = buf = mono_global_codeman_reserve (kMaxCodeSize);
 
-       framesize = kMaxCodeSize + sizeof (MonoLMFTramp);
-       framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
+       /* Compute stack frame size and offsets */
+       offset = 0;
+       rbp_offset = -offset;
 
-       orig_rsp_to_rbp_offset = 0;
-       r11_save_code = code;
-       /* Reserve 5 bytes for the mov_membase_reg to save R11 */
-       code += 5;
-       after_r11_save_code = code;
+       offset += sizeof(mgreg_t);
+       rax_offset = -offset;
+
+       offset += sizeof(mgreg_t);
+       r11_save_offset = -offset;
+
+       offset += sizeof(mgreg_t);
+       tramp_offset = -offset;
+
+       offset += sizeof(gpointer);
+       arg_offset = -offset;
+
+       offset += sizeof(mgreg_t);
+       res_offset = -offset;
+
+       offset += sizeof (MonoContext);
+       ctx_offset = -offset;
+       saved_regs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
+       saved_fpregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, fregs);
+
+       offset += sizeof (MonoLMFTramp);
+       lmf_offset = -offset;
+
+#ifdef TARGET_WIN32
+       /* Reserve space where the callee can save the argument registers */
+       offset += 4 * sizeof (mgreg_t);
+#endif
+
+       framesize = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
 
        // CFA = sp + 16 (the trampoline address is on the stack)
        cfa_offset = 16;
@@ -443,6 +366,12 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        // IP saved at CFA - 8
        mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
 
+       orig_rsp_to_rbp_offset = 0;
+       r11_save_code = code;
+       /* Reserve space for the mov_membase_reg to save R11 */
+       code += 5;
+       after_r11_save_code = code;
+
        /* Pop the return address off the stack */
        amd64_pop_reg (code, AMD64_R11);
        orig_rsp_to_rbp_offset += sizeof(mgreg_t);
@@ -463,18 +392,6 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
 
-       offset = 0;
-       rbp_offset = - offset;
-
-       offset += sizeof(mgreg_t);
-       rax_offset = - offset;
-
-       offset += sizeof(mgreg_t);
-       tramp_offset = - offset;
-
-       offset += sizeof(gpointer);
-       arg_offset = - offset;
-
        /* Compute the trampoline address from the return address */
        if (aot) {
 #if defined(__default_codegen__)
@@ -489,30 +406,37 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        }
        amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
 
-       offset += sizeof(mgreg_t);
-       res_offset = - offset;
-
        /* Save all registers */
-
-       offset += AMD64_NREG * sizeof(mgreg_t);
-       saved_regs_offset = - offset;
        for (i = 0; i < AMD64_NREG; ++i) {
                if (i == AMD64_RBP) {
                        /* RAX is already saved */
                        amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, sizeof(mgreg_t));
                        amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_RAX, sizeof(mgreg_t));
+               } else if (i == AMD64_RIP) {
+                       if (has_caller)
+                               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
+                       else
+                               amd64_mov_reg_imm (code, AMD64_R11, 0);
+                       amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
+               } else if (i == AMD64_RSP) {
+                       amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
+                       amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
+                       amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
                } else if (i != AMD64_R11) {
                        amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
                } else {
                        /* We have to save R11 right at the start of
                           the trampoline code because it's used as a
                           scratch register */
-                       amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
+                       /* This happens before the frame is set up, so it goes into the redzone */
+                       amd64_mov_membase_reg (r11_save_code, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, i, sizeof(mgreg_t));
                        g_assert (r11_save_code == after_r11_save_code);
+
+                       /* Copy from the save slot into the register array slot */
+                       amd64_mov_reg_membase (code, i, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, sizeof(mgreg_t));
+                       amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
                }
        }
-       offset += 8 * sizeof(mgreg_t);
-       saved_fpregs_offset = - offset;
        for (i = 0; i < 8; ++i)
                amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)), i);
 
@@ -535,11 +459,7 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        //amd64_breakpoint (code);
 #endif
 
-       if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
-               tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
-               tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER_V4 &&
-               tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT &&
-               tramp_type != MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
+       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 */
@@ -590,25 +510,19 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        /* Save LMF begin */
 
-       offset += sizeof (MonoLMFTramp);
-       lmf_offset = - offset;
-
        /* Save ip */
        if (has_caller)
                amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
        else
                amd64_mov_reg_imm (code, AMD64_R11, 0);
        amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, sizeof(mgreg_t));
-       /* Save fp */
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, sizeof(mgreg_t));
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, sizeof(mgreg_t));
        /* Save sp */
        amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
        amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
        amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, sizeof(mgreg_t));
-       /* Save pointer to registers */
-       amd64_lea_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset);
-       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, regs), AMD64_R11, sizeof(mgreg_t));
+       /* Save pointer to context */
+       amd64_lea_membase (code, AMD64_R11, AMD64_RBP, ctx_offset);
+       amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, ctx), AMD64_R11, sizeof(mgreg_t));
 
        if (aot) {
                code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
@@ -687,12 +601,14 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        for (i = 0; i < AMD64_NREG; ++i)
                if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
                        amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), sizeof(mgreg_t));
-
        for (i = 0; i < 8; ++i)
                amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)));
 
        /* Restore stack */
        amd64_leave (code);
+       cfa_offset -= sizeof (mgreg_t);
+       mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
+
 
        if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
                /* Load result */
@@ -710,31 +626,9 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        mono_arch_flush_icache (buf, code - buf);
        mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
 
-       if (info) {
-               tramp_name = mono_get_generic_trampoline_name (tramp_type);
-               *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
-               g_free (tramp_name);
-       }
-
-       return buf;
-}
-
-gpointer
-mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
-{
-       guint8 *code, *buf;
-       int size = NACL_SIZE (16, 32);
-
-       code = buf = mono_global_codeman_reserve (size);
-       amd64_ret (code);
-
-       nacl_global_codeman_validate(&buf, size, &code);
-
-       mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
-
-       if (info)
-               *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
+       tramp_name = mono_get_generic_trampoline_name (tramp_type);
+       *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
+       g_free (tramp_name);
 
        return buf;
 }
@@ -822,7 +716,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        int i;
        gboolean mrgctx;
        MonoJumpInfo *ji = NULL;
-       GSList *unwind_ops = NULL;
+       GSList *unwind_ops;
 
        mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
        index = MONO_RGCTX_SLOT_INDEX (slot);
@@ -904,377 +798,13 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
 
        g_assert (code - buf <= tramp_size);
 
-       if (info) {
-               char *name = mono_get_rgctx_fetch_trampoline_name (slot);
-               *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
-               g_free (name);
-       }
+       char *name = mono_get_rgctx_fetch_trampoline_name (slot);
+       *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
+       g_free (name);
 
        return buf;
 }
 
-gpointer
-mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
-{
-       guint8 *tramp;
-       guint8 *code, *buf;
-       static int byte_offset = -1;
-       static guint8 bitmask;
-       guint8 *jump;
-       int tramp_size;
-       GSList *unwind_ops = NULL;
-       MonoJumpInfo *ji = NULL;
-
-       tramp_size = 64;
-
-       code = buf = mono_global_codeman_reserve (tramp_size);
-
-       if (byte_offset < 0)
-               mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
-
-       amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
-       jump = code;
-       amd64_branch8 (code, X86_CC_Z, -1, 1);
-
-       amd64_ret (code);
-
-       x86_patch (jump, code);
-
-       if (aot) {
-               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
-               amd64_jump_reg (code, AMD64_R11);
-       } else {
-               tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
-
-               /* jump to the actual trampoline */
-               amd64_jump_code (code, tramp);
-       }
-
-       nacl_global_codeman_validate (&buf, tramp_size, &code);
-
-       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);
-
-       if (info)
-               *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
-
-       return buf;
-}
-
-#ifdef MONO_ARCH_MONITOR_OBJECT_REG
-
-gpointer
-mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
-{
-       guint8 *tramp;
-       guint8 *code, *buf;
-       guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid, *jump_sync_thin_hash = NULL;
-       guint8 *jump_lock_taken_true = NULL;
-       int tramp_size;
-       int status_offset, nest_offset;
-       MonoJumpInfo *ji = NULL;
-       GSList *unwind_ops = NULL;
-       int obj_reg = MONO_AMD64_ARG_REG1;
-       int lock_taken_reg = MONO_AMD64_ARG_REG2;
-       int sync_reg = MONO_AMD64_ARG_REG3;
-       int tid_reg = MONO_AMD64_ARG_REG4;
-       int status_reg = AMD64_RAX;
-
-       g_assert (MONO_ARCH_MONITOR_OBJECT_REG == obj_reg);
-#ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
-       g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == lock_taken_reg);
-#else
-       g_assert (!is_v4);
-#endif
-
-       mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
-       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
-       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
-       status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
-       nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
-
-       tramp_size = 96;
-
-       code = buf = mono_global_codeman_reserve (tramp_size);
-
-       unwind_ops = mono_arch_get_cie_program ();
-
-       if (!aot && mono_thread_get_tls_offset () != -1) {
-               /* MonoObject* obj is in obj_reg */
-               /* is obj null? */
-               amd64_test_reg_reg (code, obj_reg, obj_reg);
-               /* if yes, jump to actual trampoline */
-               jump_obj_null = code;
-               amd64_branch8 (code, X86_CC_Z, -1, 1);
-
-               if (is_v4) {
-                       amd64_test_membase_imm (code, lock_taken_reg, 0, 1);
-                       /* if *lock_taken is 1, jump to actual trampoline */
-                       jump_lock_taken_true = code;
-                       x86_branch8 (code, X86_CC_NZ, -1, 1);
-               }
-
-               /* load obj->synchronization to sync_reg */
-               amd64_mov_reg_membase (code, sync_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 8);
-
-               if (mono_gc_is_moving ()) {
-                       /*if bit zero is set it's a thin hash*/
-                       /*FIXME use testb encoding*/
-                       amd64_test_reg_imm (code, sync_reg, 0x01);
-                       jump_sync_thin_hash = code;
-                       amd64_branch8 (code, X86_CC_NE, -1, 1);
-
-                       /*clear bits used by the gc*/
-                       amd64_alu_reg_imm (code, X86_AND, sync_reg, ~0x3);
-               }
-
-               /* is synchronization null? */
-               amd64_test_reg_reg (code, sync_reg, sync_reg);
-               /* if yes, jump to actual trampoline */
-               jump_sync_null = code;
-               amd64_branch8 (code, X86_CC_Z, -1, 1);
-
-               /* load MonoInternalThread* into tid_reg */
-               code = mono_amd64_emit_tls_get (code, tid_reg, mono_thread_get_tls_offset ());
-               /* load TID into tid_reg */
-               amd64_mov_reg_membase (code, tid_reg, tid_reg, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
-
-               /* is synchronization->owner free */
-               amd64_mov_reg_membase (code, status_reg, sync_reg, status_offset, 4);
-               amd64_test_reg_imm_size (code, status_reg, OWNER_MASK, 4);
-               /* if not, jump to next case */
-               jump_tid = code;
-               amd64_branch8 (code, X86_CC_NZ, -1, 1);
-
-               /* if yes, try a compare-exchange with the TID */
-               g_assert (tid_reg != X86_EAX);
-               /* Form new status in tid_reg */
-               amd64_alu_reg_reg_size (code, X86_OR, tid_reg, status_reg, 4);
-               /* compare and exchange */
-               amd64_prefix (code, X86_LOCK_PREFIX);
-               amd64_cmpxchg_membase_reg_size (code, sync_reg, status_offset, tid_reg, 4);
-               /* if not successful, jump to actual trampoline */
-               jump_cmpxchg_failed = code;
-               amd64_branch8 (code, X86_CC_NZ, -1, 1);
-               /* if successful, return */
-               if (is_v4)
-                       amd64_mov_membase_imm (code, lock_taken_reg, 0, 1, 1);
-               amd64_ret (code);
-
-               /* next case: synchronization->owner is not null */
-               x86_patch (jump_tid, code);
-               /* is synchronization->owner == TID? */
-               amd64_alu_reg_imm_size (code, X86_AND, status_reg, OWNER_MASK, 4);
-               amd64_alu_reg_reg_size (code, X86_CMP, status_reg, tid_reg, 4);
-               /* if not, jump to actual trampoline */
-               jump_other_owner = code;
-               amd64_branch8 (code, X86_CC_NZ, -1, 1);
-               /* if yes, increment nest */
-               amd64_inc_membase_size (code, sync_reg, nest_offset, 4);
-               /* return */
-               if (is_v4)
-                       amd64_mov_membase_imm (code, lock_taken_reg, 0, 1, 1);
-               amd64_ret (code);
-
-               x86_patch (jump_obj_null, code);
-               if (jump_sync_thin_hash)
-                       x86_patch (jump_sync_thin_hash, code);
-               x86_patch (jump_sync_null, code);
-               x86_patch (jump_cmpxchg_failed, code);
-               x86_patch (jump_other_owner, code);
-               if (is_v4)
-                       x86_patch (jump_lock_taken_true, code);
-       }
-
-       /* jump to the actual trampoline */
-       if (MONO_AMD64_ARG_REG1 != obj_reg)
-               amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, obj_reg, sizeof (mgreg_t));
-
-       if (aot) {
-               if (is_v4)
-                       code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter_v4");
-               else
-                       code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
-               amd64_jump_reg (code, AMD64_R11);
-       } else {
-               if (is_v4)
-                       tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
-               else
-                       tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
-
-               /* jump to the actual trampoline */
-               amd64_jump_code (code, tramp);
-       }
-
-       nacl_global_codeman_validate (&buf, tramp_size, &code);
-
-       mono_arch_flush_icache (code, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
-       g_assert (code - buf <= tramp_size);
-
-       if (info) {
-               if (is_v4)
-                       *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
-               else
-                       *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
-       }
-
-       return buf;
-}
-
-gpointer
-mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
-{
-       guint8 *tramp;
-       guint8 *code, *buf;
-       guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_cmpxchg_failed, *jump_sync_thin_hash = NULL;
-       guint8 *jump_next;
-       int tramp_size;
-       int status_offset, nest_offset;
-       MonoJumpInfo *ji = NULL;
-       GSList *unwind_ops = NULL;
-       int obj_reg = MONO_AMD64_ARG_REG1;
-       int sync_reg = MONO_AMD64_ARG_REG2;
-       int status_reg = MONO_AMD64_ARG_REG3;
-
-       g_assert (obj_reg == MONO_ARCH_MONITOR_OBJECT_REG);
-
-       mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
-       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
-       g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
-       status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
-       nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
-
-       tramp_size = 112;
-
-       code = buf = mono_global_codeman_reserve (tramp_size);
-
-       unwind_ops = mono_arch_get_cie_program ();
-
-       if (!aot && mono_thread_get_tls_offset () != -1) {
-               /* MonoObject* obj is in obj_reg */
-               /* is obj null? */
-               amd64_test_reg_reg (code, obj_reg, obj_reg);
-               /* if yes, jump to actual trampoline */
-               jump_obj_null = code;
-               amd64_branch8 (code, X86_CC_Z, -1, 1);
-
-               /* load obj->synchronization to RCX */
-               amd64_mov_reg_membase (code, sync_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 8);
-
-               if (mono_gc_is_moving ()) {
-                       /*if bit zero is set it's a thin hash*/
-                       /*FIXME use testb encoding*/
-                       amd64_test_reg_imm (code, sync_reg, 0x01);
-                       jump_sync_thin_hash = code;
-                       amd64_branch8 (code, X86_CC_NE, -1, 1);
-
-                       /*clear bits used by the gc*/
-                       amd64_alu_reg_imm (code, X86_AND, sync_reg, ~0x3);
-               }
-
-               /* is synchronization null? */
-               amd64_test_reg_reg (code, sync_reg, sync_reg);
-               /* if yes, jump to actual trampoline */
-               jump_sync_null = code;
-               amd64_branch8 (code, X86_CC_Z, -1, 1);
-
-               /* next case: synchronization is not null */
-               /* load MonoInternalThread* into RAX */
-               code = mono_amd64_emit_tls_get (code, AMD64_RAX, mono_thread_get_tls_offset ());
-               /* load TID into RAX */
-               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
-               /* is synchronization->owner == TID */
-               amd64_mov_reg_membase (code, status_reg, sync_reg, status_offset, 4);
-               amd64_alu_reg_reg_size (code, X86_XOR, AMD64_RAX, status_reg, 4);
-               amd64_test_reg_imm_size (code, AMD64_RAX, OWNER_MASK, 4);
-
-               /* if no, jump to actual trampoline */
-               jump_not_owned = code;
-               amd64_branch8 (code, X86_CC_NZ, -1, 1);
-
-               /* next case: synchronization->owner == TID */
-               /* is synchronization->nest == 1 */
-               amd64_alu_membase_imm_size (code, X86_CMP, sync_reg, nest_offset, 1, 4);
-               /* if not, jump to next case */
-               jump_next = code;
-               amd64_branch8 (code, X86_CC_NZ, -1, 1);
-               /* if yes, is synchronization->entry_count greater than zero */
-               amd64_test_reg_imm_size (code, status_reg, ENTRY_COUNT_WAITERS, 4);
-               /* if not, jump to actual trampoline */
-               jump_have_waiters = code;
-               amd64_branch8 (code, X86_CC_NZ, -1 , 1);
-               /* if yes, try to set synchronization->owner to null and return */
-               g_assert (status_reg != AMD64_RAX);
-               /* old status in RAX */
-               amd64_mov_reg_reg (code, AMD64_RAX, status_reg, 4);
-               /* form new status */
-               amd64_alu_reg_imm_size (code, X86_AND, status_reg, ENTRY_COUNT_MASK, 4);
-               /* compare and exchange */
-               amd64_prefix (code, X86_LOCK_PREFIX);
-               amd64_cmpxchg_membase_reg_size (code, sync_reg, status_offset, status_reg, 4);
-               /* if not successful, jump to actual trampoline */
-               jump_cmpxchg_failed = code;
-               amd64_branch8 (code, X86_CC_NZ, -1, 1);
-               amd64_ret (code);
-
-               /* next case: synchronization->nest is not 1 */
-               x86_patch (jump_next, code);
-               /* decrease synchronization->nest and return */
-               amd64_dec_membase_size (code, sync_reg, nest_offset, 4);
-               amd64_ret (code);
-
-               x86_patch (jump_obj_null, code);
-               x86_patch (jump_have_waiters, code);
-               x86_patch (jump_not_owned, code);
-               x86_patch (jump_cmpxchg_failed, code);
-               x86_patch (jump_sync_null, code);
-       }
-
-       /* jump to the actual trampoline */
-       if (MONO_AMD64_ARG_REG1 != obj_reg)
-               amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, obj_reg, sizeof (mgreg_t));
-
-       if (aot) {
-               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
-               amd64_jump_reg (code, AMD64_R11);
-       } else {
-               tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
-               amd64_jump_code (code, tramp);
-       }
-
-       nacl_global_codeman_validate (&buf, tramp_size, &code);
-
-       mono_arch_flush_icache (code, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
-       g_assert (code - buf <= tramp_size);
-
-       if (info)
-               *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
-
-       return buf;
-}
-
-#else
-
-gpointer
-mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
-{
-       g_assert_not_reached ();
-       return NULL;
-}
-
-gpointer
-mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
-{
-       g_assert_not_reached ();
-       return NULL;
-}
-
-#endif
-
 void
 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
 {
@@ -1303,12 +833,14 @@ mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
        guint8 *code, *buf;
        int tramp_size = 64;
        MonoJumpInfo *ji = NULL;
-       GSList *unwind_ops = NULL;
+       GSList *unwind_ops;
 
        g_assert (!aot);
 
        code = buf = 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.
        */
@@ -1317,13 +849,16 @@ mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
                amd64_mov_reg_membase (code, MONO_AMD64_ARG_REG1, MONO_AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 8);
                /* Simulate a call */
                amd64_push_reg (code, AMD64_RAX);
+               mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, 16);
                amd64_jump_code (code, tramp);
        } else {
                /*Slow path uses a c helper*/
                amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RSP, 8);
                amd64_mov_reg_imm (code, AMD64_RAX, tramp);
                amd64_push_reg (code, AMD64_RAX);
+               mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, 16);
                amd64_push_reg (code, AMD64_RAX);
+               mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, 24);
                amd64_jump_code (code, handler_block_trampoline_helper);
        }
 
@@ -1331,8 +866,7 @@ mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
        mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
        g_assert (code - buf <= tramp_size);
 
-       if (info)
-               *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
+       *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
@@ -1371,3 +905,101 @@ mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
        return *(guint32*)(plt_entry + 6);
 #endif
 }
+
+/*
+ * mono_arch_create_sdb_trampoline:
+ *
+ *   Return a trampoline which captures the current context, passes it to
+ * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
+ * then restores the (potentially changed) context.
+ */
+guint8*
+mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
+{
+       int tramp_size = 256;
+       int i, framesize, ctx_offset, cfa_offset, gregs_offset;
+       guint8 *code, *buf;
+       GSList *unwind_ops = NULL;
+       MonoJumpInfo *ji = NULL;
+
+       code = buf = mono_global_codeman_reserve (tramp_size);
+
+       framesize = 0;
+#ifdef TARGET_WIN32
+       /* Reserve space where the callee can save the argument registers */
+       framesize += 4 * sizeof (mgreg_t);
+#endif
+
+       ctx_offset = framesize;
+       framesize += sizeof (MonoContext);
+
+       framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
+
+       // CFA = sp + 8
+       cfa_offset = 8;
+       mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 8);
+       // IP saved at CFA - 8
+       mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -cfa_offset);
+
+       amd64_push_reg (code, AMD64_RBP);
+       cfa_offset += sizeof(mgreg_t);
+       mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
+       mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
+
+       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);
+       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
+
+       gregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
+
+       /* Initialize a MonoContext structure on the stack */
+       for (i = 0; i < AMD64_NREG; ++i) {
+               if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
+                       amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), i, sizeof (mgreg_t));
+       }
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 0, sizeof (mgreg_t));
+       amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
+       amd64_lea_membase (code, AMD64_R11, AMD64_RBP, 2 * sizeof (mgreg_t));
+       amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RSP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, sizeof (mgreg_t), sizeof (mgreg_t));
+       amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
+
+       /* Call the single step/breakpoint function in sdb */
+       amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RSP, ctx_offset);
+
+       if (aot) {
+               if (single_step)
+                       code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
+               else
+                       code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
+               amd64_call_reg (code, AMD64_R11);
+       } else {
+               if (single_step)
+                       amd64_call_code (code, debugger_agent_single_step_from_context);
+               else
+                       amd64_call_code (code, debugger_agent_breakpoint_from_context);
+       }
+
+       /* Restore registers from ctx */
+       for (i = 0; i < AMD64_NREG; ++i) {
+               if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
+                       amd64_mov_reg_membase (code, AMD64_RSP, i, gregs_offset + (i * sizeof (mgreg_t)), sizeof (mgreg_t));
+       }
+       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), sizeof (mgreg_t));
+       amd64_mov_membase_reg (code, AMD64_RBP, 0, AMD64_R11, sizeof (mgreg_t));
+       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));
+
+       amd64_leave (code);
+       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);
+       g_assert (code - buf <= tramp_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);
+
+       return buf;
+}