[runtime] New profiler API.
[mono.git] / mono / mini / tramp-arm.c
index d24ddf4a72c1a259902f130e133db595ff606c9d..82d3e95868cbc7cfeb73749282c80fbe1a644594 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * tramp-arm.c: JIT trampoline code for ARM
+/**
+ * \file
+ * JIT trampoline code for ARM
  *
  * Authors:
  *   Paolo Molaro (lupus@ximian.com)
 #include "debugger-agent.h"
 #include "jit-icalls.h"
 
+#ifdef ENABLE_INTERPRETER
+#include "interp/interp.h"
+#endif
+
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
 void
@@ -84,6 +89,13 @@ mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *a
        *(guint8**)jump_entry = addr;
 }
 
+gpointer
+mono_arm_handler_block_trampoline_helper (gpointer *ptr)
+{
+       MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
+       return jit_tls->handler_block_return_address;
+}
+
 #ifndef DISABLE_JIT
 
 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
@@ -383,7 +395,7 @@ mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInf
 
        /* Flush instruction cache, since we've generated code */
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
 
        /* Sanity check */
        g_assert ((code - buf) <= buf_len);
@@ -453,7 +465,7 @@ mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_ty
 
        /* Flush instruction cache, since we've generated code */
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type)));
 
        g_assert ((code - buf) <= size);
 
@@ -490,7 +502,7 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
        *(guint32*)code = (guint32)addr;
        code += 4;
        mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
+       MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m));
        g_assert ((code - start) <= size);
        /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
        g_print ("unbox code is at %p for method at %p\n", start, addr);*/
@@ -501,7 +513,7 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 }
 
 gpointer
-mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
+mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
 {
        guint8 *code, *start;
        GSList *unwind_ops;
@@ -514,7 +526,7 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
 
        ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
        ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
-       *(guint32*)code = (guint32)mrgctx;
+       *(guint32*)code = (guint32)arg;
        code += 4;
        *(guint32*)code = (guint32)addr;
        code += 4;
@@ -522,7 +534,7 @@ mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericCo
        g_assert ((code - start) <= buf_len);
 
        mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (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);
 
@@ -636,7 +648,7 @@ mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info
        }
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
 
        g_assert (code - buf <= tramp_size);
 
@@ -671,7 +683,7 @@ mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboo
        code = emit_bx (code, ARMREG_R1);
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
 
        g_assert (code - buf <= tramp_size);
 
@@ -680,13 +692,6 @@ mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboo
        return buf;
 }
 
-static gpointer
-handler_block_trampoline_helper (gpointer *ptr)
-{
-       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
-       return jit_tls->handler_block_return_address;
-}
-
 gpointer
 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
 {
@@ -696,8 +701,6 @@ mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
 
-       g_assert (!aot);
-
        code = buf = mono_global_codeman_reserve (tramp_size);
 
        unwind_ops = mono_arch_get_cie_program ();
@@ -712,22 +715,33 @@ mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
         * We are in a method frame after the call emitted by OP_CALL_HANDLER.
         */
        /* Obtain jit_tls->handler_block_return_address */
-       ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
-       ARM_B (code, 0);
-       *(gpointer*)code = handler_block_trampoline_helper;
-       code += 4;
+       if (aot) {
+               code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_R0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_handler_block_trampoline_helper");
+               ARM_B (code, 0);
+       } else {
+               ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
+               ARM_B (code, 0);
+               *(gpointer*)code = mono_arm_handler_block_trampoline_helper;
+               code += 4;
+       }
 
        /* Set it as the return address so the trampoline will return to it */
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_R0);
 
        /* Call the trampoline */
-       ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
-       code = emit_bx (code, ARMREG_R0);
-       *(gpointer*)code = tramp;
-       code += 4;
+       if (aot) {
+               char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
+               code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_R0, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
+               code = emit_bx (code, ARMREG_R0);
+       } else {
+               ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
+               code = emit_bx (code, ARMREG_R0);
+               *(gpointer*)code = tramp;
+               code += 4;
+       }
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
        g_assert (code - buf <= tramp_size);
 
        *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
@@ -778,15 +792,27 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_FP);
 
        /* call */
-       // FIXME: AOT
-       ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
-       ARM_B (code, 0);
-       if (single_step)
-               *(gpointer*)code = debugger_agent_single_step_from_context;
-       else
-               *(gpointer*)code = debugger_agent_breakpoint_from_context;
-       code += 4;
-       ARM_BLX_REG (code, ARMREG_IP);
+       if (aot) {
+               if (single_step)
+                       ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
+               else
+                       ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
+               ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
+               ARM_B (code, 0);
+               *(gpointer*)code = NULL;
+               code += 4;
+               ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
+               ARM_BLX_REG (code, ARMREG_IP);
+       } else {
+               ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
+               ARM_B (code, 0);
+               if (single_step)
+                       *(gpointer*)code = debugger_agent_single_step_from_context;
+               else
+                       *(gpointer*)code = debugger_agent_breakpoint_from_context;
+               code += 4;
+               ARM_BLX_REG (code, ARMREG_IP);
+       }
 
        /* we're back; save ctx.eip and ctx.esp into the corresponding regs slots. */
        ARM_LDR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, pc));
@@ -798,7 +824,7 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        ARM_LDM (code, ARMREG_IP, 0xffff);
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
 
        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);
@@ -806,6 +832,166 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        return buf;
 }
 
+/*
+ * mono_arch_get_enter_icall_trampoline:
+ *
+ *   See tramp-amd64.c for documentation.
+ */
+gpointer
+mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
+{
+#ifdef ENABLE_INTERPRETER
+       const int gregs_num = INTERP_ICALL_TRAMP_IARGS;
+       const int fregs_num = INTERP_ICALL_TRAMP_FARGS;
+
+       guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
+       int buf_len, i, framesize, off_methodargs, off_targetaddr;
+       const int fp_reg = ARMREG_R7;
+
+       buf_len = 512 + 1024;
+       start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
+
+       framesize = 5 * sizeof (mgreg_t); /* lr, r4, r8, r6 and plus one */
+
+       off_methodargs = -framesize;
+       framesize += sizeof (mgreg_t);
+
+       off_targetaddr = -framesize;
+       framesize += sizeof (mgreg_t);
+
+       framesize = ALIGN_TO (framesize + 4 * sizeof (mgreg_t), MONO_ARCH_FRAME_ALIGNMENT);
+
+       /* allocate space on stack for argument passing */
+       const int stack_space = ALIGN_TO (((gregs_num - ARMREG_R3) * sizeof (mgreg_t)), MONO_ARCH_FRAME_ALIGNMENT);
+
+       /* iOS ABI */
+       ARM_PUSH (code, (1 << fp_reg) | (1 << ARMREG_LR));
+       ARM_MOV_REG_REG (code, fp_reg, ARMREG_SP);
+
+       /* use r4, r8 and r6 as scratch registers */
+       ARM_PUSH (code, (1 << ARMREG_R4) | (1 << ARMREG_R8) | (1 << ARMREG_R6));
+       ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, stack_space + framesize);
+
+       /* save InterpMethodArguments* onto stack */
+       ARM_STR_IMM (code, ARMREG_R1, fp_reg, off_methodargs);
+
+       /* save target address onto stack */
+       ARM_STR_IMM (code, ARMREG_R0, fp_reg, off_targetaddr);
+
+       /* load pointer to InterpMethodArguments* into r4 */
+       ARM_MOV_REG_REG (code, ARMREG_R4, ARMREG_R1);
+
+       /* move flen into r8 */
+       ARM_LDR_IMM (code, ARMREG_R8, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, flen));
+       /* load pointer to fargs into r6 */
+       ARM_LDR_IMM (code, ARMREG_R6, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, fargs));
+
+       for (i = 0; i < fregs_num; ++i) {
+               ARM_CMP_REG_IMM (code, ARMREG_R8, 0, 0);
+               label_fexits [i] = code;
+               ARM_B_COND (code, ARMCOND_EQ, 0);
+
+               g_assert (i <= ARM_VFP_D7); /* otherwise, need to pass args on stack */
+               ARM_FLDD (code, i, ARMREG_R6, i * sizeof (double));
+               ARM_SUB_REG_IMM8 (code, ARMREG_R8, ARMREG_R8, 1);
+       }
+
+       for (i = 0; i < fregs_num; i++)
+               arm_patch (label_fexits [i], code);
+
+       /* move ilen into r8 */
+       ARM_LDR_IMM (code, ARMREG_R8, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, ilen));
+       /* load pointer to iargs into r6 */
+       ARM_LDR_IMM (code, ARMREG_R6, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, iargs));
+
+       int stack_offset = 0;
+       for (i = 0; i < gregs_num; i++) {
+               ARM_CMP_REG_IMM (code, ARMREG_R8, 0, 0);
+               label_gexits [i] = code;
+               ARM_B_COND (code, ARMCOND_EQ, 0);
+
+               if (i <= ARMREG_R3) {
+                       ARM_LDR_IMM (code, i, ARMREG_R6, i * sizeof (mgreg_t));
+               } else {
+                       ARM_LDR_IMM (code, ARMREG_R4, ARMREG_R6, i * sizeof (mgreg_t));
+                       ARM_STR_IMM (code, ARMREG_R4, ARMREG_SP, stack_offset);
+                       stack_offset += sizeof (mgreg_t);
+               }
+               ARM_SUB_REG_IMM8 (code, ARMREG_R8, ARMREG_R8, 1);
+       }
+
+       for (i = 0; i < gregs_num; i++)
+               arm_patch (label_gexits [i], code);
+
+       /* load target addr */
+       ARM_LDR_IMM (code, ARMREG_R4, fp_reg, off_targetaddr);
+
+       /* call into native function */
+       ARM_BLX_REG (code, ARMREG_R4);
+
+       /* load InterpMethodArguments */
+       ARM_LDR_IMM (code, ARMREG_R4, fp_reg, off_methodargs);
+
+       /* load is_float_ret */
+       ARM_LDR_IMM (code, ARMREG_R8, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, is_float_ret));
+
+       /* check if a float return value is expected */
+       ARM_CMP_REG_IMM (code, ARMREG_R8, 0, 0);
+       label_is_float_ret = code;
+       ARM_B_COND (code, ARMCOND_NE, 0);
+
+       /* greg return */
+       /* load retval */
+       ARM_LDR_IMM (code, ARMREG_R8, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
+
+       ARM_CMP_REG_IMM (code, ARMREG_R8, 0, 0);
+       label_leave_tramp [0] = code;
+       ARM_B_COND (code, ARMCOND_EQ, 0);
+
+       /* store greg result, always write back 64bit */
+       ARM_STR_IMM (code, ARMREG_R0, ARMREG_R8, 0);
+       ARM_STR_IMM (code, ARMREG_R1, ARMREG_R8, 4);
+
+       label_leave_tramp [1] = code;
+       ARM_B_COND (code, ARMCOND_AL, 0);
+
+       /* freg return */
+       arm_patch (label_is_float_ret, code);
+       /* load retval */
+       ARM_LDR_IMM (code, ARMREG_R8, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
+
+       ARM_CMP_REG_IMM (code, ARMREG_R8, 0, 0);
+       label_leave_tramp [2] = code;
+       ARM_B_COND (code, ARMCOND_EQ, 0);
+
+       /* store freg result */
+       ARM_FSTD (code, ARM_VFP_F0, ARMREG_R8, 0);
+
+       for (i = 0; i < 3; i++)
+               arm_patch (label_leave_tramp [i], code);
+
+       ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, stack_space + framesize);
+       ARM_POP (code, (1 << ARMREG_R4) | (1 << ARMREG_R8) | (1 << ARMREG_R6));
+       ARM_MOV_REG_REG (code, ARMREG_SP, fp_reg);
+       ARM_POP (code, (1 << fp_reg) | (1 << ARMREG_PC));
+
+       g_assert (code - start < buf_len);
+
+       mono_arch_flush_icache (start, code - start);
+       MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
+
+       if (info)
+               *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
+
+       return start;
+#else
+       g_assert_not_reached ();
+       return NULL;
+#endif /* ENABLE_INTERPRETER */
+}
+
 #else
 
 guchar*
@@ -830,7 +1016,7 @@ mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
 }
 
 gpointer
-mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
+mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
 {
        g_assert_not_reached ();
        return NULL;
@@ -856,7 +1042,13 @@ mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gbo
        g_assert_not_reached ();
        return NULL;
 }
-       
+
+gpointer
+mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
+{
+       g_assert_not_reached ();
+       return NULL;
+}
 #endif /* DISABLE_JIT */
 
 guint8*
@@ -864,13 +1056,8 @@ mono_arch_get_call_target (guint8 *code)
 {
        guint32 ins = ((guint32*)(gpointer)code) [-1];
 
-#if MONOTOUCH
        /* Should be a 'bl' or a 'b' */
        if (((ins >> 25) & 0x7) == 0x5) {
-#else
-       /* Should be a 'bl' */
-       if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
-#endif
                gint32 disp = ((((gint32)ins) & 0xffffff) << 8) >> 8;
                guint8 *target = code - 4 + 8 + (disp * 4);
 
@@ -969,7 +1156,7 @@ mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpoint
        g_assert ((code - buf) <= buf_len);
 
        mono_arch_flush_icache (buf, code - buf);
-       mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
+       MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
 
        mono_tramp_info_register (mono_tramp_info_create (NULL, buf, code - buf, NULL, NULL), domain);