2008-12-08 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / mini / exceptions-arm.c
index 4631659d7497b559df9137b257b3796003d9e531..270b16c7877040309d92a6f401952cbc881807f7 100644 (file)
@@ -25,8 +25,6 @@
 #include "mini.h"
 #include "mini-arm.h"
 
-static gboolean arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
-
 /*
 
 struct sigcontext {
@@ -82,6 +80,9 @@ typedef struct ucontext {
  * We define our own version here and use it instead.
  */
 
+#if __APPLE__
+#define my_ucontext ucontext_t
+#else
 typedef struct my_ucontext {
        unsigned long       uc_flags;
        struct my_ucontext *uc_link;
@@ -95,9 +96,9 @@ typedef struct my_ucontext {
         * we don't use them for now...
         */
 } my_ucontext;
+#endif
 
 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
-               int reg;        \
                ARM_LDR_IMM (code, ip_reg, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip));        \
                ARM_ADD_REG_IMM8 (code, tmp_reg, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs));  \
                ARM_LDMIA (code, tmp_reg, MONO_ARM_REGSAVE_MASK);       \
@@ -113,27 +114,31 @@ typedef struct my_ucontext {
  * The first argument in r0 is the pointer to the context.
  */
 gpointer
-mono_arch_get_restore_context (void)
+mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
        guint8 *code;
-       static guint8 start [128];
-       static int inited = 0;
+       guint8 *start;
 
-       if (inited)
-               return start;
-       inited = 1;
+       *ji = NULL;
+
+       start = code = mono_global_codeman_reserve (128);
 
-       code = start;
        restore_regs_from_context (ARMREG_R0, ARMREG_R1, ARMREG_R2);
        /* restore also the stack pointer, FIXME: handle sp != fp */
        ARM_LDR_IMM (code, ARMREG_SP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, ebp));
+       ARM_LDR_IMM (code, ARMREG_FP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, esp));
+
        /* jump to the saved IP */
        ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
        /* never reached */
        ARM_DBRK (code);
 
-       g_assert ((code - start) < sizeof(start));
+       g_assert ((code - start) < 128);
+
        mono_arch_flush_icache (start, code - start);
+
+       *code_size = code - start;
+
        return start;
 }
 
@@ -145,19 +150,15 @@ mono_arch_get_restore_context (void)
  * @exc object in this case).
  */
 gpointer
-mono_arch_get_call_filter (void)
+mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8 start [320];
-       static int inited = 0;
        guint8 *code;
-       int alloc_size, pos, i;
+       guint8* start;
 
-       if (inited)
-               return start;
+       *ji = NULL;
 
-       inited = 1;
        /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
-       code = start;
+       start = code = mono_global_codeman_reserve (320);
 
        /* save all the regs on the stack */
        ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
@@ -173,20 +174,24 @@ mono_arch_get_call_filter (void)
        /* epilog */
        ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
 
-       g_assert ((code - start) < sizeof(start));
+       g_assert ((code - start) < 320);
+
        mono_arch_flush_icache (start, code - start);
+
+       *code_size = code - start;
+
        return start;
 }
 
-static void
-throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
+void
+mono_arm_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
 {
        static void (*restore_context) (MonoContext *);
        MonoContext ctx;
        gboolean rethrow = eip & 1;
 
        if (!restore_context)
-               restore_context = mono_arch_get_restore_context ();
+               restore_context = mono_get_restore_context ();
 
        eip &= ~1; /* clear the optional rethrow bit */
        /* adjust eip so that it point into the call instruction */
@@ -196,6 +201,7 @@ throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *
 
        /*printf ("stack in throw: %p\n", esp);*/
        MONO_CONTEXT_SET_BP (&ctx, esp);
+       MONO_CONTEXT_SET_SP (&ctx, esp);
        MONO_CONTEXT_SET_IP (&ctx, eip);
        memcpy (&ctx.regs, int_regs, sizeof (gulong) * 8);
        /* memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS); */
@@ -216,39 +222,43 @@ throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *
  * Returns a function pointer which can be used to raise 
  * exceptions. The returned function has the following 
  * signature: void (*func) (MonoException *exc); or
- * void (*func) (char *exc_name);
+ * void (*func) (guint32 ex_token, guint8* ip);
  *
  */
 static gpointer 
-mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
+mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
+       guint8 *start;
        guint8 *code;
-       int alloc_size, pos, i;
 
-       code = start;
+       *ji = NULL;
+
+       code = start = mono_global_codeman_reserve (size);
 
        /* save all the regs on the stack */
        ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
        ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
 
-       if (by_name) {
-               /* r0 has the name of the exception: get the object */
-               ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R0);
+       if (by_token) {
+               /* r0 has the type token of the exception: get the object */
+               ARM_PUSH1 (code, ARMREG_R1);
+               ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
                code = mono_arm_emit_load_imm (code, ARMREG_R0, GPOINTER_TO_UINT (mono_defaults.corlib));
-               code = mono_arm_emit_load_imm (code, ARMREG_R1, GPOINTER_TO_UINT ("System"));
-               code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_exception_from_name));
+               code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_exception_from_token));
                ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
                ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
+               ARM_POP1 (code, ARMREG_R1);
        }
 
        /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
        /* caller sp */
        ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
        /* exc is already in place in r0 */
-       if (by_name)
-               ARM_LDR_IMM (code, ARMREG_R1, ARMREG_SP, 9 * 4); /* pos on the stack were lr was saved */
-       else
+       if (by_token) {
+               /* The caller ip is already in R1 */
+       } else {
                ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
+       }
        /* FIXME: pointer to the saved fp regs */
        /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
        ppc_addi (code, ppc_r7, ppc_sp, pos);*/
@@ -257,13 +267,25 @@ mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gbo
        /* we encode rethrow in the ip, so we avoid args on the stack */
        ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
 
-       code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (throw_exception));
+       if (aot) {
+               *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_throw_exception");
+               ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
+               ARM_B (code, 0);
+               *(gpointer*)(gpointer)code = NULL;
+               code += 4;
+               ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
+       } else {
+               code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_arm_throw_exception));
+       }
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
        ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
        /* we should never reach this breakpoint */
        ARM_DBRK (code);
        g_assert ((code - start) < size);
        mono_arch_flush_icache (start, code - start);
+
+       *code_size = code - start;
+
        return start;
 }
 
@@ -276,17 +298,11 @@ mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gbo
  *
  */
 gpointer
-mono_arch_get_rethrow_exception (void)
+mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8 start [132];
-       static int inited = 0;
-
-       if (inited)
-               return start;
-       mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
-       inited = 1;
-       return start;
+       return mono_arch_get_throw_exception_generic (132, FALSE, TRUE, code_size, ji, aot);
 }
+
 /**
  * arch_get_throw_exception:
  *
@@ -300,62 +316,46 @@ mono_arch_get_rethrow_exception (void)
  *
  */
 gpointer 
-mono_arch_get_throw_exception (void)
+mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8 start [132];
-       static int inited = 0;
+       return mono_arch_get_throw_exception_generic (132, FALSE, FALSE, code_size, ji, aot);
+}
+
+gpointer 
+mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+{      
+       guint8* start;
+       guint8 *code;
+
+       *ji = NULL;
+
+       start = code = mono_global_codeman_reserve (64);
+
+       /* Not used on ARM */
+       ARM_DBRK (code);
+
+       *code_size = code - start;
 
-       if (inited)
-               return start;
-       mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
-       inited = 1;
        return start;
 }
 
 /**
- * arch_get_throw_exception_by_name:
+ * mono_arch_get_throw_corlib_exception:
  *
  * Returns a function pointer which can be used to raise 
  * corlib exceptions. The returned function has the following 
- * signature: void (*func) (char *exc_name); 
- * For example to raise an arithmetic exception you can use:
- *
- * x86_push_imm (code, "ArithmeticException"); 
- * x86_call_code (code, arch_get_throw_exception_by_name ()); 
- *
+ * signature: void (*func) (guint32 ex_token, guint32 offset); 
+ * Here, offset is the offset which needs to be substracted from the caller IP 
+ * to get the IP of the throw. Passing the offset has the advantage that it 
+ * needs no relocations in the caller.
+ * On ARM, the ip is passed instead of an offset.
  */
 gpointer 
-mono_arch_get_throw_exception_by_name (void)
+mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
 {
-       static guint8 start [168];
-       static int inited = 0;
-
-       if (inited)
-               return start;
-       mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
-       inited = 1;
-       return start;
+       return mono_arch_get_throw_exception_generic (168, TRUE, FALSE, code_size, ji, aot);
 }      
 
-static MonoArray *
-glist_to_array (GList *list, MonoClass *eclass) 
-{
-       MonoDomain *domain = mono_domain_get ();
-       MonoArray *res;
-       int len, i;
-
-       if (!list)
-               return NULL;
-
-       len = g_list_length (list);
-       res = mono_array_new (domain, eclass, len);
-
-       for (i = 0; list; list = list->next, i++)
-               mono_array_set (res, gpointer, i, list->data);
-
-       return res;
-}
-
 /* mono_arch_find_jit_info:
  *
  * This function is used to gather information from @ctx. It return the 
@@ -367,8 +367,7 @@ glist_to_array (GList *list, MonoClass *eclass)
  */
 MonoJitInfo *
 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
-                        MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
-                        int *native_offset, gboolean *managed)
+                        MonoContext *ctx, MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
 {
        MonoJitInfo *ji;
        gpointer ip = MONO_CONTEXT_GET_IP (ctx);
@@ -428,6 +427,8 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                        new_ctx->eip = *(gulong*)sp;
                        sp += sizeof (gulong);
                        new_ctx->ebp = new_ctx->esp;
+                       /* Needed by get_exception_catch_class () */
+                       new_ctx->regs [ARMREG_R11] = new_ctx->esp;
                }
 
                if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
@@ -443,11 +444,10 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                
                *new_ctx = *ctx;
 
-               if (!(*lmf)->method)
-                       return (gpointer)-1;
-
                if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
                } else {
+                       if (!(*lmf)->method)
+                               return (gpointer)-1;
                        memset (res, 0, sizeof (MonoJitInfo));
                        res->method = (*lmf)->method;
                }
@@ -465,45 +465,60 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
        return NULL;
 }
 
-/*
- * This is the function called from the signal handler
- */
-gboolean
-mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
+void
+mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
+{
+#if BROKEN_LINUX
+       struct ucontext *uc = sigctx;
+
+       mctx->eip = uc->uc_mcontext.gregs [ARMREG_PC];
+       mctx->ebp = uc->uc_mcontext.gregs [ARMREG_SP];
+       memcpy (&mctx->regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
+       /* memcpy (&mctx->fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
+#else
+       my_ucontext *my_uc = sigctx;
+
+       mctx->eip = UCONTEXT_REG_PC (my_uc);
+       mctx->ebp = UCONTEXT_REG_SP (my_uc);
+       memcpy (&mctx->regs, &UCONTEXT_REG_R4 (my_uc), sizeof (gulong) * 8);
+#endif
+}
+
+void
+mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
 {
 #if BROKEN_LINUX
        struct ucontext *uc = ctx;
+
+       uc->uc_mcontext.gregs [ARMREG_PC] = mctx->eip;
+       uc->uc_mcontext.gregs [ARMREG_SP] = mctx->ebp;
+       memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx->regs, sizeof (gulong) * 8);
+       /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);*/
 #else
        my_ucontext *my_uc = ctx;
+
+       UCONTEXT_REG_PC (my_uc) = mctx->eip;
+       UCONTEXT_REG_SP (my_uc) = mctx->ebp;
+       memcpy (&UCONTEXT_REG_R4 (my_uc), &mctx->regs, sizeof (gulong) * 8);
 #endif
+}
+
+/*
+ * This is the function called from the signal handler
+ */
+gboolean
+mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
+{
        MonoContext mctx;
        gboolean result;
 
-#if BROKEN_LINUX
-       mctx.eip = uc->uc_mcontext.gregs [ARMREG_PC];
-       mctx.ebp = uc->uc_mcontext.gregs [ARMREG_SP];
-       memcpy (&mctx.regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
-       /* memcpy (&mctx.fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
-#else
-       mctx.eip = my_uc->sig_ctx.arm_pc;
-       mctx.ebp = my_uc->sig_ctx.arm_sp;
-       memcpy (&mctx.regs, &my_uc->sig_ctx.arm_r4, sizeof (gulong) * 8);
-#endif
+       mono_arch_sigctx_to_monoctx (ctx, &mctx);
 
        result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
        /* restore the context so that returning from the signal handler will invoke
         * the catch clause 
         */
-#if BROKEN_LINUX
-       uc->uc_mcontext.gregs [ARMREG_PC] = mctx.eip;
-       uc->uc_mcontext.gregs [ARMREG_SP] = mctx.ebp;
-       memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx.regs, sizeof (gulong) * 8);
-       /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);*/
-#else
-       my_uc->sig_ctx.arm_pc = mctx.eip;
-       my_uc->sig_ctx.arm_sp = mctx.ebp;
-       memcpy (&my_uc->sig_ctx.arm_r4, &mctx.regs, sizeof (gulong) * 8);
-#endif
+       mono_arch_monoctx_to_sigctx (&mctx, ctx);
        return result;
 }
 
@@ -515,7 +530,7 @@ mono_arch_ip_from_context (void *sigctx)
        return (gpointer)uc->uc_mcontext.gregs [ARMREG_PC];
 #else
        my_ucontext *my_uc = sigctx;
-       return (void*)my_uc->sig_ctx.arm_pc;
+       return (void*) UCONTEXT_REG_PC (my_uc);
 #endif
 }