2010-06-02 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions-arm.c
index 2c60c3b749b7867e5753c69b26b594860512cf78..12a14f33218c9e119160b256a9ed7d8e22d73213 100644 (file)
 #include <glib.h>
 #include <signal.h>
 #include <string.h>
+#ifdef HAVE_ASM_SIGCONTEXT_H
+#include <asm/sigcontext.h>
+#endif  /* def HAVE_ASM_SIGCONTEXT_H */
+#ifdef HAVE_UCONTEXT_H
 #include <ucontext.h>
+#endif  /* def HAVE_UCONTEXT_H */
 
 #include <mono/arch/arm/arm-codegen.h>
 #include <mono/metadata/appdomain.h>
@@ -105,13 +110,13 @@ typedef struct my_ucontext {
  * The first argument in r0 is the pointer to the context.
  */
 gpointer
-mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
 {
        guint8 *code;
        guint8 *start;
        int ctx_reg;
-
-       *ji = NULL;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
        start = code = mono_global_codeman_reserve (128);
 
@@ -134,7 +139,7 @@ mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gbool
 
        /* restore everything */
        ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs));
-       ARM_LDMIA (code, ARMREG_IP, 0xffff);
+       ARM_LDM (code, ARMREG_IP, 0xffff);
 
        /* never reached */
        ARM_DBRK (code);
@@ -143,7 +148,8 @@ mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gbool
 
        mono_arch_flush_icache (start, code - start);
 
-       *code_size = code - start;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
 
        return start;
 }
@@ -156,13 +162,13 @@ mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gbool
  * @exc object in this case).
  */
 gpointer
-mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
 {
        guint8 *code;
        guint8* start;
        int ctx_reg;
-
-       *ji = NULL;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
        /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
        start = code = mono_global_codeman_reserve (320);
@@ -175,7 +181,7 @@ mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean
        ctx_reg = ARMREG_R0;
        ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip));
        ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs) + (4 * 4));
-       ARM_LDMIA (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
+       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);
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
@@ -188,7 +194,8 @@ mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean
 
        mono_arch_flush_icache (start, code - start);
 
-       *code_size = code - start;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
 
        return start;
 }
@@ -240,12 +247,12 @@ mono_arm_throw_exception_by_token (guint32 type_token, unsigned long eip, unsign
  *
  */
 static gpointer 
-mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_get_throw_exception_generic (int size, gboolean corlib, gboolean rethrow, MonoTrampInfo **info, gboolean aot)
 {
        guint8 *start;
        guint8 *code;
-
-       *ji = NULL;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
        code = start = mono_global_codeman_reserve (size);
 
@@ -257,7 +264,7 @@ mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow,
        /* 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_token) {
+       if (corlib) {
                /* The caller ip is already in R1 */
        } else {
                ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
@@ -271,14 +278,14 @@ mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow,
        ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
 
        if (aot) {
-               *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, by_token ? "mono_arm_throw_exception_by_token" : "mono_arm_throw_exception");
+               ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? "mono_arm_throw_exception_by_token" : "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 (by_token ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception));
+               code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (corlib ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception));
        }
        ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
        ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
@@ -287,7 +294,8 @@ mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow,
        g_assert ((code - start) < size);
        mono_arch_flush_icache (start, code - start);
 
-       *code_size = code - start;
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf (corlib ? "throw_corlib_exception" : (rethrow ? "rethrow_exception" : "throw_exception")), start, code - start, ji, unwind_ops);
 
        return start;
 }
@@ -301,9 +309,9 @@ mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow,
  *
  */
 gpointer
-mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
 {
-       return mono_arch_get_throw_exception_generic (132, FALSE, TRUE, code_size, ji, aot);
+       return mono_arch_get_throw_exception_generic (132, FALSE, TRUE, info, aot);
 }
 
 /**
@@ -319,27 +327,9 @@ mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gbo
  *
  */
 gpointer 
-mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
 {
-       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;
-
-       return start;
+       return mono_arch_get_throw_exception_generic (132, FALSE, FALSE, info, aot);
 }
 
 /**
@@ -354,9 +344,9 @@ mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **j
  * On ARM, the ip is passed instead of an offset.
  */
 gpointer 
-mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
+mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
 {
-       return mono_arch_get_throw_exception_generic (168, TRUE, FALSE, code_size, ji, aot);
+       return mono_arch_get_throw_exception_generic (168, TRUE, FALSE, info, aot);
 }      
 
 /* 
@@ -494,12 +484,60 @@ mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
 #endif
 }
 
+/*
+ * handle_exception:
+ *
+ *   Called by resuming from a signal handler.
+ */
+static void
+handle_signal_exception (gpointer obj, gboolean test_only)
+{
+       MonoJitTlsData *jit_tls = TlsGetValue (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, MONO_CONTEXT_GET_IP (&ctx), test_only);
+
+       restore_context (&ctx);
+}
+
 /*
  * This is the function called from the signal handler
  */
 gboolean
 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
 {
+#if defined(MONO_CROSS_COMPILE)
+       g_assert_not_reached ();
+#elif defined(MONO_ARCH_USE_SIGACTION)
+       my_ucontext *sigctx = ctx;
+       /*
+        * Handling the exception in the signal handler is problematic, since the original
+        * signal is disabled, and we could run arbitrary code though the debugger. So
+        * resume into the normal stack and do most work there if possible.
+        */
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       guint64 sp = UCONTEXT_REG_SP (sigctx);
+
+       /* Pass the ctx parameter in TLS */
+       mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
+       /* The others in registers */
+       UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
+       UCONTEXT_REG_R1 (sigctx) = test_only;
+
+       /* Allocate a stack frame */
+       sp -= 16;
+       UCONTEXT_REG_SP (sigctx) = sp;
+
+       UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
+
+       return TRUE;
+#else
        MonoContext mctx;
        gboolean result;
 
@@ -511,6 +549,7 @@ mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
         */
        mono_arch_monoctx_to_sigctx (&mctx, ctx);
        return result;
+#endif
 }
 
 gpointer