2009-10-24 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions-sparc.c
index aa4b46a9a963978c3f52d6161abdb3e0968da260..39ffeae899c97958b6598723f388d3cf78c3966e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * exceptions-sparc.c: exception support for 64 bit sparc
+ * exceptions-sparc.c: exception support for sparc
  *
  * Authors:
  *   Mark Crichton (crichton@gimp.org)
@@ -22,6 +22,7 @@
 #include <mono/metadata/exception.h>
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/gc-internal.h>
+#include <mono/metadata/tokentype.h>
 
 #include "mini.h"
 #include "mini-sparc.h"
 gpointer
 mono_arch_get_restore_context (void)
 {
-       static guint32 start [32];
+       static guint32 *start;
        static int inited = 0;
        guint32 *code;
 
        if (inited)
                return start;
 
-       code = start;
+       code = start = mono_global_codeman_reserve (32 * sizeof (guint32));
 
        sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
        sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
@@ -58,6 +59,8 @@ mono_arch_get_restore_context (void)
 
        g_assert ((code - start) < 32);
 
+       mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
+
        inited = 1;
 
        return start;
@@ -75,7 +78,7 @@ mono_arch_get_restore_context (void)
 gpointer
 mono_arch_get_call_filter (void)
 {
-       static guint32 start [64];
+       static guint32 *start;
        static int inited = 0;
        guint32 *code;
        int i;
@@ -83,7 +86,7 @@ mono_arch_get_call_filter (void)
        if (inited)
                return start;
 
-       code = start;
+       code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
 
        /*
         * There are two frames here:
@@ -145,13 +148,15 @@ mono_arch_get_call_filter (void)
 
        g_assert ((code - start) < 64);
 
+       mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
+
        inited = 1;
 
        return start;
 }
 
 static void
-throw_exception (MonoObject *ex, gpointer sp, gpointer ip)
+throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow)
 {
        MonoContext ctx;
        static void (*restore_context) (MonoContext *);
@@ -167,45 +172,75 @@ throw_exception (MonoObject *ex, gpointer sp, gpointer ip)
 
        if (mono_object_isinst (exc, mono_defaults.exception_class)) {
                MonoException *mono_ex = (MonoException*)exc;
-               mono_ex->stack_trace = NULL;
+               if (!rethrow)
+                       mono_ex->stack_trace = NULL;
        }
-       mono_handle_exception (&ctx, ex, ip, FALSE);
+       mono_handle_exception (&ctx, exc, ip, FALSE);
        restore_context (&ctx);
 
        g_assert_not_reached ();
 }
 
+static gpointer 
+get_throw_exception (gboolean rethrow)
+{
+       guint32 *start, *code;
+
+       code = start = mono_global_codeman_reserve (16 * sizeof (guint32));
+
+       sparc_save_imm (code, sparc_sp, -512, sparc_sp);
+
+       sparc_flushw (code);
+       sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
+       sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
+       sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
+       sparc_set (code, rethrow, sparc_o3);
+       sparc_set (code, throw_exception, sparc_o7);
+       sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
+       sparc_nop (code);
+
+       g_assert ((code - start) <= 16);
+
+       mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
+
+       return start;
+}
+
 /**
- * mono_arch_get_throw_exception_by_name:
+ * mono_arch_get_throw_exception:
  *
  * Returns a function pointer which can be used to raise exceptions.
  * The returned function has the following 
- * signature: void (*func) (char *exc_name); 
+ * signature: void (*func) (MonoException *exc); 
  */
 gpointer 
 mono_arch_get_throw_exception (void)
 {
-       static guint32 start [32];
+       static guint32* start;
        static int inited = 0;
-       guint32 *code;
 
        if (inited)
                return start;
 
        inited = 1;
-       code = start;
 
-       sparc_save_imm (code, sparc_sp, -512, sparc_sp);
+       start = get_throw_exception (FALSE);
 
-       sparc_flushw (code);
-       sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
-       sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
-       sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
-       sparc_set (code, throw_exception, sparc_o7);
-       sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
-       sparc_nop (code);
+       return start;
+}
 
-       g_assert ((code - start) < 32);
+gpointer 
+mono_arch_get_rethrow_exception (void)
+{
+       static guint32* start;
+       static int inited = 0;
+
+       if (inited)
+               return start;
+
+       inited = 1;
+
+       start = get_throw_exception (TRUE);
 
        return start;
 }
@@ -220,7 +255,7 @@ mono_arch_get_throw_exception (void)
 gpointer 
 mono_arch_get_throw_exception_by_name (void)
 {
-       static guint32 start [64];
+       static guint32 *start;
        static int inited = 0;
        guint32 *code;
        int reg;
@@ -229,7 +264,7 @@ mono_arch_get_throw_exception_by_name (void)
                return start;
 
        inited = 1;
-       code = start;
+       code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
 
 #ifdef SPARCV9
        reg = sparc_g4;
@@ -258,8 +293,69 @@ mono_arch_get_throw_exception_by_name (void)
 
        g_assert ((code - start) < 32);
 
+       mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
+
        return start;
-}      
+}
+
+/**
+ * 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) (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.
+ */
+gpointer 
+mono_arch_get_throw_corlib_exception (void)
+{
+       static guint32 *start;
+       static int inited = 0;
+       guint32 *code;
+       int reg;
+
+       if (inited)
+               return start;
+
+       inited = 1;
+       code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
+
+#ifdef SPARCV9
+       reg = sparc_g4;
+#else
+       reg = sparc_g1;
+#endif
+
+       sparc_mov_reg_reg (code, sparc_o7, sparc_o2);
+       sparc_save_imm (code, sparc_sp, -160, sparc_sp);
+
+       sparc_set (code, MONO_TOKEN_TYPE_DEF, sparc_o7);
+       sparc_add (code, FALSE, sparc_i0, sparc_o7, sparc_o1);
+       sparc_set (code, mono_defaults.exception_class->image, sparc_o0);
+       sparc_set (code, mono_exception_from_token, sparc_o7);
+       sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
+       sparc_nop (code);
+
+       /* Return to the caller, so exception handling does not see this frame */
+       sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
+
+       /* Compute throw ip */
+       sparc_sll_imm (code, sparc_o1, 2, sparc_o1);
+       sparc_sub (code, 0, sparc_o2, sparc_o1, sparc_o7);
+
+       sparc_set (code, mono_arch_get_throw_exception (), reg);
+       /* Use a jmp instead of a call so o7 is preserved */
+       sparc_jmpl_imm (code, reg, 0, sparc_g0);
+       sparc_nop (code);
+
+       g_assert ((code - start) < 32);
+
+       mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
+
+       return start;
+}
 
 /* mono_arch_find_jit_info:
  *
@@ -272,8 +368,7 @@ mono_arch_get_throw_exception_by_name (void)
  */
 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 *new_ctx, MonoLMF **lmf, gboolean *managed)
 {
        MonoJitInfo *ji;
        gpointer ip = MONO_CONTEXT_GET_IP (ctx);
@@ -283,53 +378,18 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
        if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
                ji = prev_ji;
        else
-               ji = mono_jit_info_table_find (domain, ip);
-
-       if (trace)
-               *trace = NULL;
-
-       if (native_offset)
-               *native_offset = -1;
+               ji = mini_jit_info_table_find (domain, ip, NULL);
 
        if (managed)
                *managed = FALSE;
 
        if (ji != NULL) {
-               char *source_location, *tmpaddr, *fname;
-               gint32 address, iloffset;
-
                *new_ctx = *ctx;
 
-               address = (char *)ip - (char *)ji->code_start;
-
-               if (native_offset)
-                       *native_offset = address;
-
                if (managed)
                        if (!ji->method->wrapper_type)
                                *managed = TRUE;
 
-               if (trace) {
-                       source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
-                       iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
-
-                       if (iloffset < 0)
-                               tmpaddr = g_strdup_printf ("<0x%05x>", address);
-                       else
-                               tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
-               
-                       fname = mono_method_full_name (ji->method, TRUE);
-
-                       if (source_location)
-                               *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
-                       else
-                               *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
-
-                       g_free (fname);
-                       g_free (source_location);
-                       g_free (tmpaddr);
-               }
-
                if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
                        /* remove any unused lmf */
                        *lmf = (*lmf)->previous_lmf;
@@ -341,8 +401,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
                new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
 
-               *res = *ji;
-               return res;
+               return ji;
        }
        else {
                if (!(*lmf))
@@ -353,13 +412,9 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                if (!(*lmf)->method)
                        return (gpointer)-1;
 
-               if (trace)
-                       *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
-               
-               if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->ip))) {
-                       *res = *ji;
+               if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
                } else {
-                       memset (res, 0, sizeof (MonoJitInfo));
+                       memset (res, 0, MONO_SIZEOF_JIT_INFO);
                        res->method = (*lmf)->method;
                }
 
@@ -369,7 +424,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
 
                *lmf = (*lmf)->previous_lmf;
 
-               return res;
+               return ji ? ji : res;
        }
 }
 
@@ -379,6 +434,61 @@ mono_arch_has_unwind_info (gconstpointer addr)
        return FALSE;
 }
 
+#ifdef __linux__
+
+gboolean
+mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
+{
+       MonoContext mctx;
+       struct sigcontext *sc = sigctx;
+       gpointer *window;
+
+#ifdef SPARCV9
+       mctx.ip = (gpointer) sc->sigc_regs.tpc;
+       mctx.sp = (gpointer) sc->sigc_regs.u_regs[14];
+#else
+       mctx.ip = (gpointer) sc->si_regs.pc;
+       mctx.sp = (gpointer) sc->si_regs.u_regs[14];
+#endif
+
+       window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
+       mctx.fp = window [sparc_fp - 16];
+
+       mono_handle_exception (&mctx, obj, mctx.ip, test_only);
+
+#ifdef SPARCV9
+       sc->sigc_regs.tpc = (unsigned long) mctx.ip;
+       sc->sigc_regs.tnpc = (unsigned long) (mctx.ip + 4);
+       sc->sigc_regs.u_regs[14] = (unsigned long) mctx.sp;
+#else
+       sc->si_regs.pc = (unsigned long) mctx.ip;
+       sc->si_regs.npc = (unsigned long) (mctx.ip + 4);
+       sc->si_regs.u_regs[14] = (unsigned long) mctx.sp;
+#endif
+
+       window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
+       window [sparc_fp - 16] = mctx.fp;
+
+       return TRUE;
+}
+
+gpointer
+mono_arch_ip_from_context (void *sigctx)
+{
+       struct sigcontext *sc = sigctx;
+       gpointer *ret;
+
+#ifdef SPARCV9
+       ret = (gpointer) sc->sigc_regs.tpc;
+#else
+       ret = (gpointer) sc->si_regs.pc;
+#endif
+
+       return ret;
+}
+
+#else /* !__linux__ */
+
 gboolean
 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 {
@@ -391,12 +501,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
         * under documented under solaris. The code below seems to work under
         * Solaris 9.
         */
-#ifndef __linux__
        g_assert (!ctx->uc_mcontext.gwins);
-#else
-       /* better, but doesn't work all the time.  need to rethink! */
-       g_assert (!ctx->uc_mcontext.gregs);
-#endif
 
        mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
        mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
@@ -422,3 +527,4 @@ mono_arch_ip_from_context (void *sigctx)
        return (gpointer)ctx->uc_mcontext.gregs [REG_PC];
 }
 
+#endif