Wed Jun 16 14:33:22 CEST 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / exceptions-sparc.c
index e57994493630ab3eb345faedaa7bcba0ecd5a988..58e8a90f9fd2eba00c662a4fe76dcfb625b45cf8 100644 (file)
@@ -12,6 +12,7 @@
 #include <glib.h>
 #include <signal.h>
 #include <string.h>
+#include <sys/ucontext.h>
 
 #include <mono/arch/sparc/sparc-codegen.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/mono-debug.h>
+#include <mono/metadata/gc-internal.h>
 
 #include "mini.h"
 #include "mini-sparc.h"
 
-#warning NotReady
+#ifndef REG_SP
+#define REG_SP REG_O6
+#endif
 
-typedef struct sigcontext MonoContext;
+/*
+ * mono_arch_get_restore_context:
+ *
+ * Returns a pointer to a method which restores a previously saved sigcontext.
+ */
+gpointer
+mono_arch_get_restore_context (void)
+{
+       static guint32 start [32];
+       static int inited = 0;
+       guint32 *code;
 
-gboolean  mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
+       if (inited)
+               return start;
 
-#define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->SC_EIP = (long)ip; } while (0); 
-#define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->SC_EBP = (long)bp; } while (0); 
+       code = start;
 
-#define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->SC_EIP))
-#define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->SC_EBP))
+       sparc_ld_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
+       sparc_ld_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
 
-#ifdef MONO_USE_EXC_TABLES
+       sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
+       /* FIXME: This does not return to the correct window */
+       sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
 
-/*************************************/
-/*    STACK UNWINDING STUFF          */
-/*************************************/
+       g_assert ((code - start) < 32);
 
-/* These definitions are from unwind-dw2.c in glibc 2.2.5 */
+       inited = 1;
 
-/* For x86 */
-#define DWARF_FRAME_REGISTERS 17
+       return start;
+}
 
-typedef struct frame_state
+/*
+ * mono_arch_get_call_filter:
+ *
+ * Returns a pointer to a method which calls an exception filter. We
+ * also use this function to call finally handlers (we pass NULL as 
+ * @exc object in this case).
+ *
+ * call_filter (MonoContext *ctx, gpointer ip)
+ */
+gpointer
+mono_arch_get_call_filter (void)
 {
-  void *cfa;
-  void *eh_ptr;
-  long cfa_offset;
-  long args_size;
-  long reg_or_offset[DWARF_FRAME_REGISTERS+1];
-  unsigned short cfa_reg;
-  unsigned short retaddr_column;
-  char saved[DWARF_FRAME_REGISTERS+1];
-} frame_state;
+       static guint32 start [64];
+       static int inited = 0;
+       guint32 *code;
+       int i;
 
+       if (inited)
+               return start;
 
-typedef struct frame_state * (*framesf) (void *, struct frame_state *);
+       code = start;
 
-static framesf frame_state_for = NULL;
+       /*
+        * There are two frames here:
+        * - the first frame is used by call_filter
+        * - the second frame is used to run the filter code
+        */
 
-static gboolean inited = FALSE;
+       /* Create first frame */
+       sparc_save_imm (code, sparc_sp, -160, sparc_sp);
 
-typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
+       sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
+       sparc_ld_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
 
-static get_backtrace_symbols_type get_backtrace_symbols = NULL;
+       /* Create second frame */
+       sparc_save_imm (code, sparc_sp, -160, sparc_sp);
 
-static void
-init_frame_state_for (void)
-{
-       GModule *module;
+       sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
+       sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
 
        /*
-        * There are two versions of __frame_state_for: one in libgcc.a and the
-        * other in glibc.so. We need the version from glibc.
-        * For more info, see this:
-        * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
+        * We need to change %fp to point to the stack frame of the method
+        * containing the filter. But changing %fp also changes the %sp of
+        * the parent frame (the first frame), so if the OS saves the first frame,
+        * it saves it to the stack frame of the method, which is not good.
+        * So flush all register windows to memory before changing %fp.
         */
-       if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
-       
-               if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
-                       frame_state_for = NULL;
+       sparc_flushw (code);
 
-               if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
-                       get_backtrace_symbols = NULL;
-                       frame_state_for = NULL;
-               }
+       sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
 
-               g_module_close (module);
-       }
+       /* 
+        * Modify the second frame so it is identical to the one used in the
+        * method containing the filter.
+        */
+       for (i = 0; i < 16; ++i)
+               sparc_ld_imm (code, sparc_o1, i * 4, sparc_l0 + i);
 
-       inited = TRUE;
-}
+       /* Save %fp to a location reserved in mono_arch_allocate_vars */
+       sparc_st_imm (code, sparc_o7, sparc_fp, -4);
 
-/* mono_arch_has_unwind_info:
- *
- * Tests if a function has an DWARF exception table able to restore
- * all caller saved registers. 
- */
-gboolean
-mono_arch_has_unwind_info (gconstpointer addr)
-{
-       return FALSE;
+       /* Call the filter code, after this returns, %o0 will hold the result */
+       sparc_call_imm (code, sparc_o0, 0);
+       sparc_nop (code);
+
+       /* Restore original %fp */
+       sparc_ld_imm (code, sparc_fp, -4, sparc_fp);
+
+       sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
+
+       /* Return to first frame */
+       sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
+
+       /* FIXME: Save locals to the stack */
+
+       /* Return to caller */
+       sparc_ret (code);
+       /* Return result in delay slot */
+       sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
+
+       g_assert ((code - start) < 64);
+
+       inited = 1;
+
+       return start;
 }
 
-struct stack_frame
+static void
+throw_exception (MonoObject *ex, guint32 sp, guint32 ip)
 {
-  void *next;
-  void *return_address;
-};
-#endif
+       MonoContext ctx;
+       static void (*restore_context) (MonoContext *);
+
+       if (!restore_context)
+               restore_context = mono_arch_get_restore_context ();
+
+       ctx.sp = (guint32*)sp;
+       ctx.ip = ip;
+       ctx.fp = (guint32*)ctx.sp [sparc_i6 - 16];
+
+       mono_handle_exception (&ctx, ex, FALSE);
+       restore_context (&ctx);
+
+       g_assert_not_reached ();
+}
 
+/**
+ * mono_arch_get_throw_exception_by_name:
+ *
+ * Returns a function pointer which can be used to raise exceptions.
+ * The returned function has the following 
+ * signature: void (*func) (char *exc_name); 
+ */
 gpointer 
 mono_arch_get_throw_exception (void)
 {
-       return 0xdeadbeef;
+       static guint32 start [32];
+       static int inited = 0;
+       guint32 *code;
+
+       if (inited)
+               return start;
+
+       inited = 1;
+       code = start;
+
+       sparc_save_imm (code, sparc_sp, -160, 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, throw_exception, sparc_o7);
+       sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
+       sparc_nop (code);
+
+       g_assert ((code - start) < 32);
+
+       return start;
 }
 
 /**
- * arch_get_throw_exception_by_name:
+ * mono_arch_get_throw_exception_by_name:
  *
  * 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) (char *exc_name, gpointer ip); 
  */
 gpointer 
 mono_arch_get_throw_exception_by_name (void)
 {
-       return 0xdecafbad;
+       static guint32 start [32];
+       static int inited = 0;
+       guint32 *code;
+
+       if (inited)
+               return start;
+
+       inited = 1;
+       code = start;
+
+       sparc_save_imm (code, sparc_sp, -160, sparc_sp);
+
+       sparc_mov_reg_reg (code, sparc_i0, sparc_o2);
+       sparc_set (code, mono_defaults.corlib, sparc_o0);
+       sparc_set (code, "System", sparc_o1);
+       sparc_set (code, mono_exception_from_name, 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);
+
+       /* Put original return address into %o7 */
+       sparc_mov_reg_reg (code, sparc_o1, sparc_o7);
+       sparc_set (code, mono_arch_get_throw_exception (), sparc_g1);
+       /* Use a jmp instead of a call so o7 is preserved */
+       sparc_jmpl_imm (code, sparc_g1, 0, sparc_g0);
+       sparc_nop (code);
+
+       g_assert ((code - start) < 32);
+
+       return start;
 }      
 
-static MonoArray *
-glist_to_array (GList *list) 
+/* mono_arch_find_jit_info:
+ *
+ * This function is used to gather information from @ctx. It return the 
+ * MonoJitInfo of the corresponding function, unwinds one stack frame and
+ * stores the resulting context into @new_ctx. It also stores a string 
+ * describing the stack location into @trace (if not NULL), and modifies
+ * the @lmf if necessary. @native_offset return the IP offset from the 
+ * start of the function or -1 if that info is not available.
+ */
+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)
 {
-       MonoDomain *domain = mono_domain_get ();
-       MonoArray *res;
-       int len, i;
+       MonoJitInfo *ji;
+       gpointer ip = MONO_CONTEXT_GET_IP (ctx);
+       guint32 *window;
 
-       if (!list)
-               return NULL;
+       /* Avoid costly table lookup during stack overflow */
+       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);
 
-       len = g_list_length (list);
-       res = mono_array_new (domain, mono_defaults.int_class, len);
+       if (trace)
+               *trace = NULL;
 
-       for (i = 0; list; list = list->next, i++)
-               mono_array_set (res, gpointer, i, list->data);
+       if (native_offset)
+               *native_offset = -1;
 
-       return res;
-}
+       if (managed)
+               *managed = FALSE;
 
-MonoArray *
-ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
-{
-       return NULL;
-}
+       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;
+               }
 
-void
-mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
+               /* Restore ip and sp from the saved register window */
+               window = (guint32*)ctx->sp;
+               new_ctx->ip = window [sparc_i7 - 16];
+               new_ctx->sp = (guint32*)(window [sparc_i6 - 16]);
+               new_ctx->fp = (guint32*)(new_ctx->sp [sparc_i6 - 16]);
+
+               *res = *ji;
+               return res;
+       }
+       else {
+               if (!(*lmf))
+                       return NULL;
+
+               *new_ctx = *ctx;
+
+               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;
+               } else {
+                       memset (res, 0, sizeof (MonoJitInfo));
+                       res->method = (*lmf)->method;
+               }
+
+               new_ctx->ip = (*lmf)->ip;
+               new_ctx->sp = (*lmf)->sp;
+               new_ctx->fp = (*lmf)->ebp;
+
+               *lmf = (*lmf)->previous_lmf;
+
+               return res;
+       }
 }
 
-MonoBoolean
-ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
-                         MonoReflectionMethod **method, 
-                         gint32 *iloffset, gint32 *native_offset,
-                         MonoString **file, gint32 *line, gint32 *column)
+gboolean
+mono_arch_has_unwind_info (gconstpointer addr)
 {
        return FALSE;
 }
 
-/**
- * arch_handle_exception:
- * @ctx: saved processor state
- * @obj: the exception object
- * @test_only: only test if the exception is caught, but dont call handlers
- *
- *
- */
 gboolean
-mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
+mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 {
-       g_assert_not_reached ();
+       MonoContext mctx;
+       ucontext_t *ctx = (ucontext_t*)sigctx;
+
+       /*
+        * Access to the machine state using the ucontext_t parameter is somewhat
+        * 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];
+       mctx.fp = mctx.sp [sparc_fp - 16];
+
+       mono_handle_exception (&mctx, obj, test_only);
+       
+       /* We can't use restore_context to return from a signal handler */
+       ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
+       ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
+       ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
+       mctx.sp [sparc_fp - 16] = mctx.fp;
+
+       return TRUE;
+}
+
+gpointer
+mono_arch_ip_from_context (void *sigctx)
+{
+       ucontext_t *ctx = (ucontext_t*)sigctx;
+       return (gpointer)ctx->uc_mcontext.gregs [REG_PC];
 }
+