Merge pull request #1319 from directhex/systemwide-per-arch-aot-cache
[mono.git] / mono / mini / exceptions-arm.c
index 8931dd8467e20dd0a7c77486a25ffdfac78112d7..2c9e748454e8d57a30fe6fa725542e64e4f01b5a 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <mono/arch/arm/arm-codegen.h>
 #include <mono/arch/arm/arm-vfp-codegen.h>
+#include <mono/metadata/abi-details.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/threads.h>
@@ -35,6 +36,7 @@
 #include "mini.h"
 #include "mini-arm.h"
 #include "mono/utils/mono-sigcontext.h"
+#include "mono/utils/mono-compiler.h"
 
 /*
  * arch_get_restore_context:
@@ -60,17 +62,17 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
 
        ctx_reg = ARMREG_R0;
 
-#if defined(ARM_FPU_VFP)
-       ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, fregs));
-       ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
-#endif
+       if (!mono_arch_is_soft_float ()) {
+               ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs));
+               ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
+       }
 
        /* move pc to PC */
-       ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, pc));
-       ARM_STR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
+       ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
+       ARM_STR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
 
        /* restore everything */
-       ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs));
+       ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs));
        ARM_LDM (code, ARMREG_IP, 0xffff);
 
        /* never reached */
@@ -111,8 +113,8 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
 
        /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
        ctx_reg = ARMREG_R0;
-       ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, pc));
-       ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
+       ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
+       ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
        ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
        /* call handler at eip (r1) and set the first arg with the exception (r2) */
        ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
@@ -135,14 +137,11 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
 void
 mono_arm_throw_exception (MonoObject *exc, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
 {
-       static void (*restore_context) (MonoContext *);
        MonoContext ctx;
-       gboolean rethrow = pc & 1;
+       gboolean rethrow = sp & 1;
 
-       if (!restore_context)
-               restore_context = mono_get_restore_context ();
-
-       pc &= ~1; /* clear the optional rethrow bit */
+       sp &= ~1; /* clear the optional rethrow bit */
+       pc &= ~1; /* clear the thumb bit */
        /* adjust eip so that it point into the call instruction */
        pc -= 4;
 
@@ -159,7 +158,7 @@ mono_arm_throw_exception (MonoObject *exc, mgreg_t pc, mgreg_t sp, mgreg_t *int_
                        mono_ex->stack_trace = NULL;
        }
        mono_handle_exception (&ctx, exc);
-       restore_context (&ctx);
+       mono_restore_context (&ctx);
        g_assert_not_reached ();
 }
 
@@ -220,12 +219,12 @@ get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm
        mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
 
        /* Save fp regs */
-       ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
-       cfa_offset += sizeof (double) * 16;
-       mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
-#if defined(ARM_FPU_VFP)
-       ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
-#endif
+       if (!mono_arch_is_soft_float ()) {
+               ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
+               cfa_offset += sizeof (double) * 16;
+               mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
+               ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
+       }
 
        /* Param area */
        ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
@@ -235,6 +234,12 @@ get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm
        /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
        /* caller sp */
        ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
+       /* we encode rethrow in sp */
+       if (rethrow) {
+               g_assert (!resume_unwind);
+               g_assert (!corlib);
+               ARM_ORR_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, rethrow);
+       }
        /* exc is already in place in r0 */
        if (corlib) {
                /* The caller ip is already in R1 */
@@ -246,8 +251,6 @@ get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm
        }
        /* int regs */
        ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
-       /* we encode rethrow in the ip */
-       ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
        /* fp regs */
        ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
        ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
@@ -371,8 +374,7 @@ mono_arch_exceptions_init (void)
                        MonoTrampInfo *info = l->data;
 
                        mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
-                       mono_save_trampoline_xdebug_info (info);
-                       mono_tramp_info_free (info);
+                       mono_tramp_info_register (info);
                }
                g_slist_free (tramps);
        }
@@ -399,35 +401,41 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
 
        if (ji != NULL) {
                int i;
-               gssize regs [MONO_MAX_IREGS + 1];
+               gssize regs [MONO_MAX_IREGS + 1 + 8];
                guint8 *cfa;
                guint32 unwind_info_len;
                guint8 *unwind_info;
 
                frame->type = FRAME_TYPE_MANAGED;
 
-               if (ji->from_aot)
-                       unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
-               else
-                       unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
+               unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
+
+               /*
+               printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
+               mono_print_unwind_info (unwind_info, unwind_info_len);
+               */
 
                for (i = 0; i < 16; ++i)
                        regs [i] = new_ctx->regs [i];
+#ifdef TARGET_IOS
+               /* On IOS, d8..d15 are callee saved. They are mapped to 8..15 in unwind.c */
+               for (i = 0; i < 8; ++i)
+                       regs [MONO_MAX_IREGS + i] = new_ctx->fregs [8 + i];
+#endif
 
                mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
                                                   (guint8*)ji->code_start + ji->code_size,
-                                                  ip, regs, MONO_MAX_IREGS,
+                                                  ip, NULL, regs, MONO_MAX_IREGS + 8,
                                                   save_locations, MONO_MAX_IREGS, &cfa);
 
                for (i = 0; i < 16; ++i)
                        new_ctx->regs [i] = regs [i];
                new_ctx->pc = regs [ARMREG_LR];
                new_ctx->regs [ARMREG_SP] = (gsize)cfa;
-
-               if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->sp)) {
-                       /* remove any unused lmf */
-                       *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
-               }
+#ifdef TARGET_IOS
+               for (i = 0; i < 8; ++i)
+                       new_ctx->fregs [8 + i] = regs [MONO_MAX_IREGS + i];
+#endif
 
                /* Clear thumb bit */
                new_ctx->pc &= ~1;
@@ -493,20 +501,6 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
        return FALSE;
 }
 
-#if MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
-void
-mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
-{
-       mono_sigctx_to_monoctx (sigctx, mctx);
-}
-
-void
-mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
-{
-       mono_monoctx_to_sigctx (mctx, ctx);
-}
-#endif /* MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX */
-
 /*
  * handle_exception:
  *
@@ -517,26 +511,19 @@ handle_signal_exception (gpointer obj)
 {
        MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoContext ctx;
-       static void (*restore_context) (MonoContext *);
-
-       if (!restore_context)
-               restore_context = mono_get_restore_context ();
 
        memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
 
        mono_handle_exception (&ctx, obj);
 
-       restore_context (&ctx);
+       mono_restore_context (&ctx);
 }
 
 /*
  * This works around a gcc 4.5 bug:
  * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
  */
-#if defined(__GNUC__)
-__attribute__((noinline))
-#endif
-static gpointer
+static MONO_NEVER_INLINE gpointer
 get_handle_signal_exception_addr (void)
 {
        return handle_signal_exception;
@@ -561,7 +548,7 @@ mono_arch_handle_exception (void *ctx, gpointer obj)
        guint64 sp = UCONTEXT_REG_SP (sigctx);
 
        /* Pass the ctx parameter in TLS */
-       mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
+       mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
        /* The others in registers */
        UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
 
@@ -584,13 +571,13 @@ mono_arch_handle_exception (void *ctx, gpointer obj)
        MonoContext mctx;
        gboolean result;
 
-       mono_arch_sigctx_to_monoctx (ctx, &mctx);
+       mono_sigctx_to_monoctx (ctx, &mctx);
 
        result = mono_handle_exception (&mctx, obj);
        /* restore the context so that returning from the signal handler will invoke
         * the catch clause 
         */
-       mono_arch_monoctx_to_sigctx (&mctx, ctx);
+       mono_monoctx_to_sigctx (&mctx, ctx);
        return result;
 #endif
 }