Merge pull request #1991 from esdrubal/seq_test_fix
[mono.git] / mono / mini / tramp-amd64.c
index 374e3a3dd4aea19b4c6dab41bf11d56cf5af2fa8..edeb4d3f5753d1f80c8df7e8063fed32c29cf781 100644 (file)
@@ -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>
@@ -149,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:
  *
@@ -455,10 +459,7 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
        //amd64_breakpoint (code);
 #endif
 
-       if (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 */
@@ -804,317 +805,6 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        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;
-       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 = 128;
-
-       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 (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;
-       guint8 *jump_next, *jump_sync_thin_hash = NULL;
-       int tramp_size;
-       int status_offset, nest_offset;
-       MonoJumpInfo *ji = NULL;
-       GSList *unwind_ops;
-       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);
-
-               if (jump_sync_thin_hash)
-                       x86_patch (jump_sync_thin_hash, 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);
-
-       *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)
 {
@@ -1234,7 +924,15 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
-       framesize = sizeof (MonoContext);
+       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
@@ -1252,7 +950,6 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
 
-       ctx_offset = 0;
        gregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
 
        /* Initialize a MonoContext structure on the stack */