[runtime] Use mono_restore_context () to reduce code duplication in the back ends.
[mono.git] / mono / mini / tramp-amd64.c
index ee2c8c410c0d279b8ef4292bf084e51a6a9461bb..fc65bc5a3ec127ad857200715c2cc91f55561e7f 100644 (file)
 
 #if defined(__native_client_codegen__) && defined(__native_client__)
 #include <malloc.h>
-#include <sys/nacl_syscalls.h>
+#include <nacl/nacl_dyncode.h>
 #endif
 
 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
 
-static guint8* nullified_class_init_trampoline;
-
 /*
  * mono_arch_get_unbox_trampoline:
  * @m: method pointer
@@ -49,21 +47,21 @@ gpointer
 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 {
        guint8 *code, *start;
-       int this_reg;
+       int this_reg, size = NACL_SIZE (20, 32);
 
        MonoDomain *domain = mono_domain_get ();
 
        this_reg = mono_arch_get_this_arg_reg (NULL);
 
-       start = code = mono_domain_code_reserve (domain, 20);
+       start = code = mono_domain_code_reserve (domain, size);
 
        amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
        /* FIXME: Optimize this */
        amd64_mov_reg_imm (code, AMD64_RAX, addr);
        amd64_jump_reg (code, AMD64_RAX);
-       g_assert ((code - start) < 20);
+       g_assert ((code - start) < size);
 
-       nacl_domain_code_validate (domain, &start, 20, &code);
+       nacl_domain_code_validate (domain, &start, size, &code);
 
        mono_arch_flush_icache (start, code - start);
 
@@ -88,9 +86,9 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
 #else
        /* AOTed code could still have a non-32 bit address */
        if ((((guint64)addr) >> 32) == 0)
-               buf_len = 16;
+               buf_len = NACL_SIZE (16, 32);
        else
-               buf_len = 30;
+               buf_len = NACL_SIZE (30, 32);
 #endif
 
        start = code = mono_domain_code_reserve (domain, buf_len);
@@ -165,11 +163,11 @@ mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
                                /* 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 (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
+                                       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 (ji->method, TRUE));
+                                       fprintf (stderr, "Callee: %s\n", mono_method_full_name (jinfo_get_method (ji), TRUE));
                                g_assert_not_reached ();
 #else
                                /* 
@@ -336,6 +334,7 @@ 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 */
@@ -360,7 +359,7 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
                vtable_slot = get_vcall_slot_addr (code, regs);
                g_assert (vtable_slot);
 
-               *vtable_slot = nullified_class_init_trampoline;
+               *vtable_slot = tramp;
        } else if (buf [2] == 0xe8) {
                /* call <TARGET> */
                //guint8 *buf = code - 2;
@@ -378,11 +377,11 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
                buf [4] = 0x90;
                */
 
-               mono_arch_patch_callsite (code - 5, code, nullified_class_init_trampoline);
+               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, nullified_class_init_trampoline);
+               mono_arch_patch_callsite (code - 13, code, tramp);
        } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
                /* Already changed by another thread */
                ;
@@ -396,10 +395,7 @@ mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
 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_trampoline ("nullified_class_init_trampoline");
-
-       mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
+       mono_arch_patch_plt_entry (code, NULL, regs, mini_get_nullified_class_init_trampoline ());
 }
 
 static void
@@ -412,6 +408,7 @@ stack_unaligned (MonoTrampolineType tramp_type)
 guchar*
 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
 {
+       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;
@@ -719,14 +716,12 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        mono_arch_flush_icache (buf, code - buf);
 
-       if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
-               /* Initialize the nullified class init trampoline used in the AOT case */
-               nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (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);
        }
 
-       if (info)
-               *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
-
        return buf;
 }
 
@@ -734,16 +729,17 @@ 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 (16);
+       code = buf = mono_global_codeman_reserve (size);
        amd64_ret (code);
 
-       nacl_global_codeman_validate(&buf, 16, &code);
+       nacl_global_codeman_validate(&buf, size, &code);
 
        mono_arch_flush_icache (buf, code - buf);
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
+               *info = mono_tramp_info_create ("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");
@@ -781,6 +777,7 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
        /* Aligning the call site below could */
        /* add up to kNaClAlignment-1 bytes   */
        size += (kNaClAlignment-1);
+       size = NACL_BUNDLE_ALIGN_UP (size);
        buf = mono_domain_code_reserve_align (domain, size, kNaClAlignment);
        code = buf;
 #endif
@@ -913,8 +910,11 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
 
        g_assert (code - buf <= tramp_size);
 
-       if (info)
-               *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
+       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);
+       }
 
        return buf;
 }
@@ -963,7 +963,7 @@ mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean a
        g_assert (code - buf <= tramp_size);
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
+               *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
@@ -1087,7 +1087,7 @@ mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
        g_assert (code - buf <= tramp_size);
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
+               *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
 
        return buf;
 }
@@ -1205,7 +1205,7 @@ mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
        g_assert (code - buf <= tramp_size);
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
+               *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
 
        return buf;
 }