Fix crashes in the debugger stack walk code if a gshared method calls another using...
[mono.git] / mono / mini / tramp-amd64.c
index 7611b48d46250eddfdea141450036dbe6c82e752..cd42591d8769c0b6bfc61d79b41d996410ffc3ab 100644 (file)
@@ -16,6 +16,8 @@
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/mono-debug-debugger.h>
 #include <mono/metadata/monitor.h>
+#include <mono/metadata/monitor.h>
+#include <mono/metadata/gc-internal.h>
 #include <mono/arch/amd64/amd64-codegen.h>
 
 #include <mono/utils/memcheck.h>
@@ -29,7 +31,6 @@ static guint8* nullified_class_init_trampoline;
 
 /*
  * mono_arch_get_unbox_trampoline:
- * @gsctx: the generic sharing context
  * @m: method pointer
  * @addr: pointer to native code for @m
  *
@@ -38,14 +39,14 @@ static guint8* nullified_class_init_trampoline;
  * unboxing before calling the method
  */
 gpointer
-mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
+mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 {
        guint8 *code, *start;
        int this_reg;
 
        MonoDomain *domain = mono_domain_get ();
 
-       this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), gsctx, NULL);
+       this_reg = mono_arch_get_this_arg_reg (NULL);
 
        start = code = mono_domain_code_reserve (domain, 20);
 
@@ -105,7 +106,7 @@ mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_off
 
        start = code = mono_domain_code_reserve (domain, buf_len);
 
-       this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), NULL, NULL);
+       this_reg = mono_arch_get_this_arg_reg (NULL);
 
        /* Set imt arg */
        amd64_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
@@ -202,6 +203,48 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
        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)
 {
@@ -220,8 +263,6 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
        if (!can_write)
                return;
 
-       code -= 3;
-
        /* 
         * 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
@@ -231,7 +272,7 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
                gpointer *vtable_slot;
 
                /* call *<OFFSET>(%rip) */
-               vtable_slot = mono_get_vcall_slot_addr (code + 3, regs);
+               vtable_slot = get_vcall_slot_addr (code, regs);
                g_assert (vtable_slot);
 
                *vtable_slot = nullified_class_init_trampoline;
@@ -252,15 +293,11 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
                buf [4] = 0x90;
                */
 
-               mono_arch_patch_callsite (code - 2, code - 2 + 5, nullified_class_init_trampoline);
+               mono_arch_patch_callsite (code - 5, code, nullified_class_init_trampoline);
        } 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 */
-               guint8* buf = code;
-
-               /* FIXME: Not thread safe */
-               buf [1] = 0x90;
-               buf [2] = 0x90;
+               mono_arch_patch_callsite (code - 13, code, nullified_class_init_trampoline);
        } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
                /* Already changed by another thread */
                ;
@@ -275,47 +312,29 @@ void
 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
 {
        if (mono_aot_only && !nullified_class_init_trampoline)
-               nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
+               nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
 
        mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
 }
 
 guchar*
-mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
-{
-       MonoJumpInfo *ji;
-       guint32 code_size;
-       guchar *code;
-       GSList *unwind_ops, *l;
-
-       code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, FALSE);
-
-       mono_save_trampoline_xdebug_info ("<generic_trampoline>", code, code_size, unwind_ops);
-
-       for (l = unwind_ops; l; l = l->next)
-               g_free (l->data);
-       g_slist_free (unwind_ops);
-
-       return code;
-}
-
-guchar*
-mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, GSList **out_unwind_ops, gboolean aot)
+mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
 {
        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 i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset;
+       int buf_len, saved_regs_offset;
        int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
        gboolean has_caller;
        GSList *unwind_ops = NULL;
+       MonoJumpInfo *ji = NULL;
 
        if (tramp_type == MONO_TRAMPOLINE_JUMP)
                has_caller = FALSE;
        else
                has_caller = TRUE;
 
-       code = buf = mono_global_codeman_reserve (538);
-
-       *ji = NULL;
+       buf_len = 548;
+       code = buf = mono_global_codeman_reserve (buf_len);
 
        framesize = 538 + sizeof (MonoLMF);
        framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
@@ -472,8 +491,7 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
        amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
 
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
        } else {
                amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
        }
@@ -509,13 +527,12 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
 
        if (aot) {
                char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
-               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
        } else {
                tramp = (guint8*)mono_get_trampoline_func (tramp_type);
-               amd64_mov_reg_imm (code, AMD64_RAX, tramp);
+               amd64_mov_reg_imm (code, AMD64_R11, tramp);
        }
-       amd64_call_reg (code, AMD64_RAX);
+       amd64_call_reg (code, AMD64_R11);
 
        /* Check for thread interruption */
        /* This is not perf critical code so no need to check the interrupt flag */
@@ -524,12 +541,12 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
         */
        amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
-               amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
        } else {
-               amd64_mov_reg_imm (code, AMD64_RAX, (guint8*)mono_thread_force_interruption_checkpoint);
+               amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint);
        }
-       amd64_call_reg (code, AMD64_RAX);
+       amd64_call_reg (code, AMD64_R11);
+
        amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);      
 
        /* Restore LMF */
@@ -545,11 +562,10 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
         */
        amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, 8);
 
-       /* Restore argument registers, r10 (needed to pass rgctx to
-          static shared generic methods), r11 (imt register for
-          interface calls), and rax (needed for direct calls to C vararg functions). */
+       /* Restore argument registers, r10 (imt method/rgxtx)
+          and rax (needed for direct calls to C vararg functions). */
        for (i = 0; i < AMD64_NREG; ++i)
-               if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_R11 || i == AMD64_RAX)
+               if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
                        amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
 
        for (i = 0; i < 8; ++i)
@@ -567,26 +583,23 @@ mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *c
                amd64_jump_membase (code, AMD64_RSP, rax_offset - 0x8);
        }
 
-       g_assert ((code - buf) <= 538);
+       g_assert ((code - buf) <= buf_len);
 
        mono_arch_flush_icache (buf, code - buf);
 
-       *code_size = code - buf;
-
        if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
-               guint32 code_len;
-
                /* Initialize the nullified class init trampoline used in the AOT case */
-               nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (&code_len);
+               nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
        }
 
-       *out_unwind_ops = unwind_ops;
-       
+       if (info)
+               *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
+
        return buf;
 }
 
 gpointer
-mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
+mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
 {
        guint8 *code, *buf;
 
@@ -595,7 +608,11 @@ mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
 
        mono_arch_flush_icache (buf, code - buf);
 
-       *code_len = code - buf;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
+
+       if (mono_jit_map_is_enabled ())
+               mono_emit_jit_tramp (buf, code - buf, "nullified_class_init_trampoline");
 
        return buf;
 }
@@ -638,16 +655,7 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
 }      
 
 gpointer
-mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
-{
-       guint32 code_size;
-       MonoJumpInfo *ji;
-
-       return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
-}
-
-gpointer
-mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
 {
        guint8 *tramp;
        guint8 *code, *buf;
@@ -656,8 +664,8 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
        int depth, index;
        int i;
        gboolean mrgctx;
-
-       *ji = NULL;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
        mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
        index = MONO_RGCTX_SLOT_INDEX (slot);
@@ -675,6 +683,8 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
+       unwind_ops = mono_arch_get_cie_program ();
+
        rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
 
        if (mrgctx) {
@@ -722,8 +732,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
        amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
 
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
                amd64_jump_reg (code, AMD64_R11);
        } else {
                tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
@@ -736,22 +745,14 @@ mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_s
 
        g_assert (code - buf <= tramp_size);
 
-       *code_size = code - buf;
+       if (info)
+               *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
 
 gpointer
-mono_arch_create_generic_class_init_trampoline (void)
-{
-       guint32 code_size;
-       MonoJumpInfo *ji;
-
-       return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
-}
-
-gpointer
-mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
 {
        guint8 *tramp;
        guint8 *code, *buf;
@@ -759,8 +760,8 @@ mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJum
        static guint8 bitmask;
        guint8 *jump;
        int tramp_size;
-
-       *ji = NULL;
+       GSList *unwind_ops = NULL;
+       MonoJumpInfo *ji = NULL;
 
        tramp_size = 64;
 
@@ -778,8 +779,7 @@ mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJum
        x86_patch (jump, code);
 
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+               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);
@@ -792,7 +792,8 @@ mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJum
 
        g_assert (code - buf <= tramp_size);
 
-       *code_size = code - buf;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
@@ -800,25 +801,15 @@ mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJum
 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
 
 gpointer
-mono_arch_create_monitor_enter_trampoline (void)
-{
-       guint32 code_size;
-       MonoJumpInfo *ji;
-
-       return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
-}
-
-gpointer
-mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
 {
-
        guint8 *tramp;
        guint8 *code, *buf;
-       guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid;
+       guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid, *jump_sync_thin_hash = NULL;
        int tramp_size;
        int owner_offset, nest_offset, dummy;
-
-       *ji = NULL;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
        g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
 
@@ -832,6 +823,8 @@ mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
+       unwind_ops = mono_arch_get_cie_program ();
+
        if (mono_thread_get_tls_offset () != -1) {
                /* MonoObject* obj is in RDI */
                /* is obj null? */
@@ -842,6 +835,18 @@ mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo
 
                /* load obj->synchronization to RCX */
                amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_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, AMD64_RCX, 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, AMD64_RCX, ~0x3);
+               }
+
                /* is synchronization null? */
                amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
                /* if yes, jump to actual trampoline */
@@ -884,6 +889,8 @@ mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo
                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);
@@ -895,8 +902,7 @@ mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo
 #endif
 
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+               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 {
                tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
@@ -908,31 +914,23 @@ mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo
        mono_arch_flush_icache (code, code - buf);
        g_assert (code - buf <= tramp_size);
 
-       *code_size = code - buf;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
 
 gpointer
-mono_arch_create_monitor_exit_trampoline (void)
-{
-       guint32 code_size;
-       MonoJumpInfo *ji;
-
-       return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
-}
-
-gpointer
-mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
 {
        guint8 *tramp;
        guint8 *code, *buf;
-       guint8 *jump_obj_null, *jump_have_waiters;
+       guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
        guint8 *jump_next;
        int tramp_size;
        int owner_offset, nest_offset, entry_count_offset;
-
-       *ji = NULL;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
        g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
 
@@ -944,10 +942,12 @@ mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo
        nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
        entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
 
-       tramp_size = 94;
+       tramp_size = 112;
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
+       unwind_ops = mono_arch_get_cie_program ();
+
        if (mono_thread_get_tls_offset () != -1) {
                /* MonoObject* obj is in RDI */
                /* is obj null? */
@@ -958,30 +958,36 @@ mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo
 
                /* load obj->synchronization to RCX */
                amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_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, AMD64_RCX, 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, AMD64_RCX, ~0x3);
+               }
+
                /* is synchronization null? */
                amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
-               /* if not, jump to next case */
-               jump_next = code;
-               amd64_branch8 (code, X86_CC_NZ, -1, 1);
-               /* if yes, just return */
-               amd64_ret (code);
+               /* if yes, jump to actual trampoline */
+               jump_sync_null = code;
+               amd64_branch8 (code, X86_CC_Z, -1, 1);
 
                /* next case: synchronization is not null */
-               x86_patch (jump_next, code);
                /* load MonoInternalThread* into RDX */
                code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
                /* load TID into RDX */
                amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
                /* is synchronization->owner == TID */
                amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
-               /* if yes, jump to next case */
-               jump_next = code;
-               amd64_branch8 (code, X86_CC_Z, -1, 1);
-               /* if not, just return */
-               amd64_ret (code);
+               /* if no, jump to actual trampoline */
+               jump_not_owned = code;
+               amd64_branch8 (code, X86_CC_NZ, -1, 1);
 
                /* next case: synchronization->owner == TID */
-               x86_patch (jump_next, code);
                /* is synchronization->nest == 1 */
                amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
                /* if not, jump to next case */
@@ -1004,6 +1010,8 @@ mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo
 
                x86_patch (jump_obj_null, code);
                x86_patch (jump_have_waiters, code);
+               x86_patch (jump_not_owned, code);
+               x86_patch (jump_sync_null, code);
        }
 
        /* jump to the actual trampoline */
@@ -1012,8 +1020,7 @@ mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo
 #endif
 
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+               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);
@@ -1023,7 +1030,8 @@ mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo
        mono_arch_flush_icache (code, code - buf);
        g_assert (code - buf <= tramp_size);
 
-       *code_size = code - buf;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
@@ -1041,3 +1049,74 @@ mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
        x86_push_imm (code, (guint64)func_arg);
        amd64_call_reg (code, AMD64_R11);
 }
+
+
+static void
+handler_block_trampoline_helper (gpointer *ptr)
+{
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       *ptr = jit_tls->handler_block_return_address;
+}
+
+gpointer
+mono_arch_create_handler_block_trampoline (void)
+{
+       guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
+       guint8 *code, *buf;
+       int tramp_size = 64;
+       code = buf = mono_global_codeman_reserve (tramp_size);
+
+       /*
+       This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
+       */
+
+       if (mono_get_jit_tls_offset () != -1) {
+               code = mono_amd64_emit_tls_get (code, AMD64_RDI, mono_get_jit_tls_offset ());
+               /*simulate a call*/
+               amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RDI, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 8);
+               amd64_jump_code (code, tramp);
+       } else {
+               /*Slow path uses a c helper*/
+               amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
+               amd64_mov_reg_imm (code, AMD64_RAX, tramp);
+               amd64_push_reg (code, AMD64_RAX);
+               amd64_jump_code (code, handler_block_trampoline_helper);
+       }
+
+       mono_arch_flush_icache (buf, code - buf);
+       g_assert (code - buf <= tramp_size);
+
+       if (mono_jit_map_is_enabled ())
+               mono_emit_jit_tramp (buf, code - buf, "handler_block_trampoline");
+
+       return buf;
+}
+
+/*
+ * mono_arch_get_call_target:
+ *
+ *   Return the address called by the code before CODE if exists.
+ */
+guint8*
+mono_arch_get_call_target (guint8 *code)
+{
+       if (code [-5] == 0xe8) {
+               guint32 disp = *(guint32*)(code - 4);
+               guint8 *target = code + disp;
+
+               return target;
+       } else {
+               return NULL;
+       }
+}
+
+/*
+ * mono_arch_get_plt_info_offset:
+ *
+ *   Return the PLT info offset belonging to the plt entry PLT_ENTRY.
+ */
+guint32
+mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
+{
+       return *(guint32*)(plt_entry + 6);
+}