Merge pull request #3028 from lateralusX/jlorenss/threadpool_warning
[mono.git] / mono / mini / tramp-x86.c
index 6e79922512c9f3b165e62ebace0af1d2de63b81f..017fe2f84e38edbcb6724c6fad022712538cfe60 100644 (file)
@@ -18,7 +18,7 @@
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/mono-debug-debugger.h>
 #include <mono/metadata/profiler-private.h>
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/arch/x86/x86-codegen.h>
 
 #include <mono/utils/memcheck.h>
@@ -26,6 +26,7 @@
 #include "mini.h"
 #include "mini-x86.h"
 #include "debugger-agent.h"
+#include "jit-icalls.h"
 
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
@@ -42,7 +43,7 @@ gpointer
 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 {
        guint8 *code, *start;
-       int this_pos = 4, size = NACL_SIZE(16, 32);
+       int this_pos = 4, size = 16;
        MonoDomain *domain = mono_domain_get ();
        GSList *unwind_ops;
 
@@ -54,7 +55,6 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
        x86_jump_code (code, addr);
        g_assert ((code - start) < size);
 
-       nacl_domain_code_validate (domain, &start, size, &code);
        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);
@@ -71,7 +71,7 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
 
        MonoDomain *domain = mono_domain_get ();
 
-       buf_len = NACL_SIZE (10, 32);
+       buf_len = 10;
 
        start = code = mono_domain_code_reserve (domain, buf_len);
 
@@ -81,7 +81,6 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
        x86_jump_code (code, addr);
        g_assert ((code - start) <= buf_len);
 
-       nacl_domain_code_validate (domain, &start, buf_len, &code);
        mono_arch_flush_icache (start, code - start);
        mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
 
@@ -90,41 +89,9 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
        return start;
 }
 
-gpointer
-mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
-{
-       guint8 *code, *start;
-       int buf_len;
-       int this_offset;
-
-       buf_len = 32;
-
-       start = code = mono_domain_code_reserve (domain, buf_len);
-
-       this_offset = mono_x86_get_this_arg_offset (mono_method_signature (m));
-
-       /* Set imt arg */
-       x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
-       /* Load this */
-       x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
-       /* Load vtable address */
-       x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
-       x86_jump_membase (code, X86_EAX, vt_offset);
-
-       g_assert ((code - start) < buf_len);
-
-       nacl_domain_code_validate (domain, &start, buf_len, &code);
-
-       mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
-
-       return start;
-}
-
 void
 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
 {
-#if defined(__default_codegen__)
        guint8 *code;
        guint8 buf [8];
        gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
@@ -156,23 +123,6 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
                                code [4], code [5], code [6]);
                g_assert_not_reached ();
        }
-#elif defined(__native_client__)
-       /* Target must be bundle-aligned */
-       g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
-
-       /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
-       if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
-               int ret;
-               gint32 offset = (gint32)addr - (gint32)orig_code;
-               guint8 buf[sizeof(gint32)];
-               *((gint32*)(buf)) = offset;
-               ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
-               g_assert (ret == 0);
-       } else {
-               printf ("Invalid trampoline sequence %p: %02x %02x %02x %02x %02x\n", orig_code, orig_code [-5], orig_code [-4], orig_code [-3], orig_code [-2], orig_code[-1]);
-               g_assert_not_reached ();
-       }
-#endif
 }
 
 void
@@ -182,23 +132,11 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
 
        /* Patch the jump table entry used by the plt entry */
 
-#if defined(__native_client_codegen__) || defined(__native_client__)
-       /* for both compiler and runtime      */
-       /* A PLT entry:                       */
-       /*        mov <DISP>(%ebx), %ecx      */
-       /*        and 0xffffffe0, %ecx        */
-       /*        jmp *%ecx                   */
-       g_assert (code [0] == 0x8b);
-       g_assert (code [1] == 0x8b);
-
-       offset = *(guint32*)(code + 2);
-#elif defined(__default_codegen__)
        /* A PLT entry: jmp *<DISP>(%ebx) */
        g_assert (code [0] == 0xff);
        g_assert (code [1] == 0xa3);
 
        offset = *(guint32*)(code + 2);
-#endif  /* __native_client_codegen__ */
        if (!got)
                got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
        *(guint8**)((guint8*)got + offset) = addr;
@@ -207,7 +145,7 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
 static gpointer
 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
 {
-       const int kBufSize = NACL_SIZE (8, 16);
+       const int kBufSize = 8;
        guint8 buf [64];
        guint8 reg = 0;
        gint32 disp = 0;
@@ -220,15 +158,6 @@ get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
        if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
                reg = code [1] & 0x07;
                disp = *((gint32*)(code + 2));
-#if defined(__native_client_codegen__) || defined(__native_client__)
-       } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
-                          (code[5] == 0xd1) && (code[-5] == 0x8b)) {
-               disp = *((gint32*)(code - 3));
-               reg = code[-4] & 0x07;
-       } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
-               reg = code[-1] & 0x07;
-               disp = (signed char)code[0];
-#endif
        } else {
                g_assert_not_reached ();
                return NULL;
@@ -253,7 +182,7 @@ guchar*
 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
 {
        char *tramp_name;
-       guint8 *buf, *code, *tramp;
+       guint8 *buf, *code, *tramp, *br_ex_check;
        GSList *unwind_ops = NULL;
        MonoJumpInfo *ji = NULL;
        int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
@@ -407,19 +336,60 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
         */
        x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
 
+       /* Restore LMF */
+       x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
+       x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
+       x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
+       x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
+
        /* Check for interruptions */
        if (aot) {
-               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
                x86_call_reg (code, X86_EAX);
        } else {
-               x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
+               x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint_noraise);
        }
 
-       /* Restore LMF */
-       x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
-       x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
-       x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
-       x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
+       x86_test_reg_reg (code, X86_EAX, X86_EAX);
+       br_ex_check = code;
+       x86_branch8 (code, X86_CC_Z, -1, 1);
+
+       /*
+        * Exception case:
+        * We have an exception we want to throw in the caller's frame, so pop
+        * the trampoline frame and throw from the caller.
+        */
+       x86_leave (code);
+       /*
+        * The exception is in eax.
+        * We are calling the throw trampoline used by OP_THROW, so we have to setup the
+        * stack to look the same.
+        * The stack contains the ret addr, and the trampoline argument, the throw trampoline
+        * expects it to contain the ret addr and the exception. It also needs to be aligned
+        * after the exception is pushed.
+        */
+       /* Align stack */
+       x86_push_reg (code, X86_EAX);
+       /* Push the exception */
+       x86_push_reg (code, X86_EAX);
+       //x86_breakpoint (code);
+       /* Push the original return value */
+       x86_push_membase (code, X86_ESP, 3 * 4);
+       /*
+        * EH is initialized after trampolines, so get the address of the variable
+        * which contains throw_exception, and load it from there.
+        */
+       if (aot) {
+               /* Not really a jit icall */
+               code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
+       } else {
+               x86_mov_reg_imm (code, X86_ECX, (guint8*)mono_get_throw_exception_addr ());
+       }
+       x86_mov_reg_membase (code, X86_ECX, X86_ECX, 0, sizeof(gpointer));
+       x86_jump_reg (code, X86_ECX);
+
+       /* Normal case */
+       mono_x86_patch (br_ex_check, code);
 
        /* Restore registers */
        for (i = X86_EAX; i <= X86_EDI; ++i) {
@@ -457,7 +427,6 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
                }
        }
 
-       nacl_global_codeman_validate (&buf, 256, &code);
        g_assert ((code - buf) <= 256);
        mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
 
@@ -477,14 +446,12 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        
        tramp = mono_get_trampoline_code (tramp_type);
 
-       code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
+       code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
 
        x86_push_imm (buf, arg1);
        x86_jump_code (buf, tramp);
        g_assert ((buf - code) <= TRAMPOLINE_SIZE);
 
-       nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
-
        mono_arch_flush_icache (code, buf - code);
        mono_profiler_code_buffer_new (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
 
@@ -521,12 +488,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                index -= size - 1;
        }
 
-#if defined(__default_codegen__)
        tramp_size = (aot ? 64 : 36) + 6 * depth;
-#elif defined(__native_client_codegen__)
-       tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
-         6 * (depth + kNaClAlignment);
-#endif
 
        code = buf = mono_global_codeman_reserve (tramp_size);
 
@@ -584,7 +546,6 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
                x86_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_GENERICS_TRAMPOLINE, NULL);
 
@@ -628,7 +589,6 @@ mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboo
 
        x86_jump_reg (code, X86_EAX);
 
-       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_GENERICS_TRAMPOLINE, NULL);
 
@@ -704,8 +664,6 @@ mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
        mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
        x86_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);
@@ -731,7 +689,7 @@ mono_arch_get_call_target (guint8 *code)
 guint32
 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
 {
-       return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
+       return *(guint32*)(plt_entry + 6);
 }
 
 /*
@@ -757,7 +715,6 @@ mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpoint
        x86_jump_code (code, addr);
        g_assert ((code - start) <= buf_len);
 
-       nacl_domain_code_validate (domain, &start, buf_len, &code);
        mono_arch_flush_icache (start, code - start);
        mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
 
@@ -789,6 +746,7 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        /* Argument area */
        framesize += sizeof (mgreg_t);
 
+       framesize = ALIGN_TO (framesize, 8);
        ctx_offset = framesize;
        framesize += sizeof (MonoContext);
 
@@ -866,17 +824,3 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        return buf;
 }
 
-#if defined(ENABLE_GSHAREDVT)
-
-#include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
-
-#else
-
-gpointer
-mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
-{
-       *info = NULL;
-       return NULL;
-}
-
-#endif /* !MONOTOUCH */