Implement gsharedvt support for constrained calls to Object.GetHashCode ().
[mono.git] / mono / mini / exceptions-s390x.c
index abb48d2a0516ca70fe6382fcc59a9614d7ba8bdc..c46265b7fe45818adcf2a739abf8958b35d5ea70 100644 (file)
 #define S390_THROWSTACK_ACCREGS                (S390_THROWSTACK_FLTREGS+(16*sizeof(gdouble)))
 #define S390_THROWSTACK_SIZE           (S390_THROWSTACK_ACCREGS+(16*sizeof(gint32)))
 
+#define S390_REG_SAVE_R13              (S390_REG_SAVE_OFFSET+(7*sizeof(gulong)))
+
 #define SZ_THROW       384
 
+#define setup_context(ctx)
+
 /*========================= End of Defines =========================*/
 
 /*------------------------------------------------------------------*/
@@ -65,8 +69,7 @@
 /*------------------------------------------------------------------*/
 
 gboolean mono_arch_handle_exception (void     *ctx,
-                                    gpointer obj, 
-                                    gboolean test_only);
+                                    gpointer obj);
 
 /*========================= End of Prototypes ======================*/
 
@@ -76,29 +79,11 @@ gboolean mono_arch_handle_exception (void     *ctx,
 
 typedef enum {
        by_none,
-       by_name,
        by_token
 } throwType;
 
 /*====================== End of Global Variables ===================*/
 
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - mono_arch_has_unwind_info                         */
-/*                                                                  */
-/* Function    - Tests if a function has a DWARF exception table   */
-/*               that is able to restore all caller saved registers*/
-/*                                                                  */
-/*------------------------------------------------------------------*/
-
-gboolean
-mono_arch_has_unwind_info (gconstpointer addr)
-{
-       return FALSE;
-}
-
-/*========================= End of Function ========================*/
-
 /*------------------------------------------------------------------*/
 /*                                                                  */
 /* Name                - mono_arch_get_call_filter                         */
@@ -111,12 +96,16 @@ mono_arch_has_unwind_info (gconstpointer addr)
 /*------------------------------------------------------------------*/
 
 gpointer
-mono_arch_get_call_filter (void)
+mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
 {
        static guint8 *start;
        static int inited = 0;
        guint8 *code;
        int alloc_size, pos, i;
+       GSList *unwind_ops = NULL;
+       MonoJumpInfo *ji = NULL;
+
+       g_assert (!aot);
 
        if (inited)
                return start;
@@ -175,7 +164,18 @@ mono_arch_get_call_filter (void)
                s390_ld  (code, i, 0, s390_r13, pos);
                pos += sizeof(gdouble);
        }
-       
+
+#if 0
+       /*------------------------------------------------------*/
+       /* We need to preserve current SP before calling filter */
+       /* with SP from the context                             */
+       /*------------------------------------------------------*/
+       s390_lgr  (code, s390_r14, STK_BASE);
+       s390_lg   (code, STK_BASE, 0, s390_r13,
+                  G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[15]));
+       s390_lgr  (code, s390_r13, s390_r14);
+#endif
+
        /*------------------------------------------------------*/
        /* Go call filter                                       */
        /*------------------------------------------------------*/
@@ -187,6 +187,13 @@ mono_arch_get_call_filter (void)
        /*------------------------------------------------------*/
        s390_lgr  (code, s390_r14, s390_r2);
 
+#if 0
+       /*------------------------------------------------------*/
+       /* Reload our stack register with value saved in context*/
+       /*------------------------------------------------------*/
+       s390_lgr  (code, STK_BASE, s390_r13);
+#endif
+
        /*------------------------------------------------------*/
        /* Restore all the regs from the stack                  */
        /*------------------------------------------------------*/
@@ -204,6 +211,12 @@ mono_arch_get_call_filter (void)
        s390_br   (code, s390_r14);
 
        g_assert ((code - start) < SZ_THROW); 
+
+       if (info)
+               *info = mono_tramp_info_create (g_strdup_printf("call_filter"),
+                                               start, code - start, ji,
+                                               unwind_ops);
+
        return start;
 }
 
@@ -227,11 +240,11 @@ throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
        static void (*restore_context) (MonoContext *);
 
        if (!restore_context)
-               restore_context = mono_arch_get_restore_context();
+               restore_context = mono_get_restore_context();
        
        memset(&ctx, 0, sizeof(ctx));
 
-       getcontext(&ctx);
+       setup_context(&ctx);
 
        /* adjust eip so that it point into the call instruction */
        ip -= 2;
@@ -252,7 +265,8 @@ throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
                if (!rethrow)
                        mono_ex->stack_trace = NULL;
        }
-       mono_arch_handle_exception (&ctx, exc, FALSE);
+//     mono_arch_handle_exception (&ctx, exc, FALSE);
+       mono_handle_exception (&ctx, exc);
        restore_context(&ctx);
 
        g_assert_not_reached ();
@@ -273,36 +287,23 @@ throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
 /*------------------------------------------------------------------*/
 
 static gpointer 
-get_throw_exception_generic (guint8 *start, int size
-                            throwType type, gboolean rethrow)
+mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info
+                               int corlib, gboolean rethrow, gboolean aot)
 {
-       guint8 *code;
+       guint8 *code, *start;
        int alloc_size, pos, i;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
 
-       code = start;
+       code = start = mono_global_codeman_reserve(size);
 
        s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
        alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
        s390_lgr  (code, s390_r14, STK_BASE);
        s390_aghi (code, STK_BASE, -alloc_size);
        s390_stg  (code, s390_r14, 0, STK_BASE, 0);
-       switch (type) {
-       case by_name : 
-               s390_lgr  (code, s390_r4, s390_r2);
-               s390_lg   (code, s390_r3, 0, s390_r2, G_STRUCT_OFFSET(MonoException, object));
-               s390_basr (code, s390_r13, 0);
-               s390_j    (code, 10);
-               s390_llong(code, mono_defaults.corlib);
-               s390_llong(code, mono_exception_from_name);
-               s390_lg   (code, s390_r3, 0, s390_r3, G_STRUCT_OFFSET(MonoVTable, klass));
-               s390_lg   (code, s390_r2, 0, s390_r13, 4);
-               s390_lg   (code, s390_r1, 0, s390_r13, 12);
-               s390_lg   (code, s390_r4, 0, s390_r3, G_STRUCT_OFFSET(MonoClass, name));
-               s390_lg   (code, s390_r3, 0, s390_r3, G_STRUCT_OFFSET(MonoClass, name_space));
-               s390_basr (code, s390_r14, s390_r1);
-               break;
-       case by_token : 
-               s390_lgr  (code, s390_r3, s390_r2);
+       s390_lgr  (code, s390_r3, s390_r2);
+       if (corlib) {
                s390_basr (code, s390_r13, 0);
                s390_j    (code, 10);
                s390_llong(code, mono_defaults.exception_class->image);
@@ -310,10 +311,8 @@ get_throw_exception_generic (guint8 *start, int size,
                s390_lg   (code, s390_r2, 0, s390_r13, 4);
                s390_lg   (code, s390_r1, 0, s390_r13, 12);
                s390_basr (code, s390_r14, s390_r1);
-               break;
-       case by_none :
-               break;
        }
+
        /*------------------------------------------------------*/
        /* save the general registers on the stack              */
        /*------------------------------------------------------*/
@@ -363,6 +362,13 @@ get_throw_exception_generic (guint8 *start, int size,
        /* we should never reach this breakpoint */
        s390_break (code);
        g_assert ((code - start) < size);
+
+       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;
 }
 
@@ -379,18 +385,15 @@ get_throw_exception_generic (guint8 *start, int size,
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
-gpointer 
-mono_arch_get_throw_exception (void)
+gpointer
+mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
 {
-       static guint8 *start;
-       static int inited = 0;
 
-       if (inited)
-               return start;
-       start = mono_global_codeman_reserve (SZ_THROW);
-       get_throw_exception_generic (start, SZ_THROW, by_none, FALSE);
-       inited = 1;
-       return start;
+       g_assert (!aot);
+       if (info)
+               *info = NULL;
+
+       return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
 }
 
 /*========================= End of Function ========================*/
@@ -407,17 +410,13 @@ mono_arch_get_throw_exception (void)
 /*------------------------------------------------------------------*/
 
 gpointer 
-mono_arch_get_rethrow_exception (void)
+mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
 {
-       static guint8 *start;
-       static int inited = 0;
+       g_assert (!aot);
+       if (info)
+               *info = NULL;
 
-       if (inited)
-               return start;
-       start = mono_global_codeman_reserve (SZ_THROW);
-       get_throw_exception_generic (start, SZ_THROW, FALSE, TRUE);
-       inited = 1;
-       return start;
+       return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
 }
 
 /*========================= End of Function ========================*/
@@ -433,45 +432,14 @@ mono_arch_get_rethrow_exception (void)
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
-gpointer 
-mono_arch_get_throw_corlib_exception(void)
-{
-       static guint8 *start;
-       static int inited = 0;
-
-       if (inited)
-               return start;
-       start = mono_global_codeman_reserve (SZ_THROW);
-       get_throw_exception_generic (start, SZ_THROW, by_token, FALSE);
-       inited = 1;
-       return start;
-}      
-
-/*========================= End of Function ========================*/
-
-/*------------------------------------------------------------------*/
-/*                                                                  */
-/* Name                - arch_get_throw_exception_by_name                  */
-/*                                                                  */
-/* Function    - Return a function pointer which can be used to    */
-/*                raise corlib exceptions. The return function has  */
-/*                the following signature:                          */
-/*                void (*func) (char *exc_name);                    */
-/*                                                                  */
-/*------------------------------------------------------------------*/
-
-gpointer 
-mono_arch_get_throw_exception_by_name (void)
+gpointer
+mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
 {
-       static guint8 *start;
-       static int inited = 0;
+       g_assert (!aot);
+       if (info)
+               *info = NULL;
 
-       if (inited)
-               return start;
-       start = mono_global_codeman_reserve (SZ_THROW);
-       get_throw_exception_generic (start, SZ_THROW, by_name, FALSE);
-       inited = 1;
-       return start;
+       return (mono_arch_get_throw_exception_generic (SZ_THROW, info, TRUE, FALSE, aot));
 }      
 
 /*========================= End of Function ========================*/
@@ -480,84 +448,84 @@ mono_arch_get_throw_exception_by_name (void)
 /*                                                                  */
 /* Name                - mono_arch_find_jit_info                           */
 /*                                                                  */
-/* Function    - This function is used to gather information from  */
-/*                @ctx. It returns the MonoJitInfo of the corres-   */
-/*                ponding 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 returns the IP off-  */
-/*                set from the start of the function or -1 if that  */
-/*                information is not available.                     */
+/* Function    - See exceptions-amd64.c for docs.                  */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
-MonoJitInfo *
+gboolean
 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
-                        MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
-                        MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
+                        MonoJitInfo *ji, MonoContext *ctx, 
+                        MonoContext *new_ctx, MonoLMF **lmf,
+                        mgreg_t **save_locations,
+                        StackFrameInfo *frame)
 {
-       MonoJitInfo *ji;
        gpointer ip = (gpointer) MONO_CONTEXT_GET_IP (ctx);
        MonoS390StackFrame *sframe;
 
-       if (prev_ji && 
-           (ip >= prev_ji->code_start && 
-           ((guint8 *) ip <= ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
-               ji = prev_ji;
-       else
-               ji = mini_jit_info_table_find (domain, ip, NULL);
+       memset (frame, 0, sizeof (StackFrameInfo));
+       frame->ji = ji;
 
-       if (managed)
-               *managed = FALSE;
+       *new_ctx = *ctx;
 
        if (ji != NULL) {
                gint64 address;
+               guint8 *cfa;
+               guint32 unwind_info_len;
+               guint8 *unwind_info;
+               mgreg_t regs[16];
 
-               *new_ctx = *ctx;
+               frame->type = FRAME_TYPE_MANAGED;
 
-               if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
+               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);
+
+               if (*lmf && ((*lmf) != jit_tls->first_lmf) && 
+                   (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
                        /* remove any unused lmf */
                        *lmf = (*lmf)->previous_lmf;
                }
 
                address = (char *)ip - (char *)ji->code_start;
 
-               if (managed)
-                       if (!ji->method->wrapper_type)
-                               *managed = TRUE;
-
-               sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_SP (ctx);
-               MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
-               sframe = (MonoS390StackFrame *) sframe->prev;
-               MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
-               memcpy (&new_ctx->uc_mcontext.gregs[6], sframe->regs, (8*sizeof(gint64)));
-               return ji;
-
+               memcpy(&regs, &ctx->uc_mcontext.gregs, sizeof(regs));
+               mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
+                               (guint8 *) ji->code_start + ji->code_size,
+                               ip, regs, 16, save_locations, 
+                               MONO_MAX_IREGS, &cfa);
+               memcpy (&new_ctx->uc_mcontext.gregs, &regs, sizeof(regs));
+               MONO_CONTEXT_SET_IP(new_ctx, regs[14] - 2);
+               MONO_CONTEXT_SET_BP(new_ctx, cfa);
+       
+               if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
+                       /* remove any unused lmf */
+                       *lmf = (*lmf)->previous_lmf;
+               }
+               return TRUE;
        } else if (*lmf) {
-               
-               *new_ctx = *ctx;
 
-               if (!(*lmf)->method)
-                       return (gpointer)-1;
-
-               if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
-               } else {
-                       memset (res, 0, MONO_SIZEOF_JIT_INFO);
-                       res->method = (*lmf)->method;
+               ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
+               if (!ji) {
+                       if (!(*lmf)->method)
+                               return FALSE;
+               
+                       frame->method = (*lmf)->method;
                }
 
+               frame->ji = ji;
+               frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
+
                memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
                memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
-
                MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
-               MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
+               MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip - 2);
                *lmf = (*lmf)->previous_lmf;
 
-               return ji ? ji : res;
+               return TRUE;
        }
 
-       return NULL;
+       return FALSE;
 }
 
 /*========================= End of Function ========================*/
@@ -570,15 +538,46 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
 /*                                                                  */
 /* Parameters   - ctx       - Saved processor state                 */
 /*                obj       - The exception object                  */
-/*                test_only - Only test if the exception is caught, */
-/*                            but don't call handlers               */
 /*                                                                  */
 /*------------------------------------------------------------------*/
 
 gboolean
-mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
+mono_arch_handle_exception (void *uc, gpointer obj)
 {
-       return mono_handle_exception (uc, obj, mono_arch_ip_from_context(uc), test_only);
+       return mono_handle_exception (uc, obj);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_sigctx_to_monoctx.                      */
+/*                                                                  */
+/* Function    - Called from the signal handler to convert signal  */
+/*                context to MonoContext.                           */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
+{
+       mono_sigctx_to_monoctx(ctx, mctx);
+}
+
+/*========================= End of Function ========================*/
+
+/*------------------------------------------------------------------*/
+/*                                                                  */
+/* Name                - mono_arch_monoctx_to_sigctx.                      */
+/*                                                                  */
+/* Function    - Convert MonoContext structure to signal context.  */
+/*                                                                  */
+/*------------------------------------------------------------------*/
+
+void
+mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
+{
+       mono_monoctx_to_sigctx(mctx, ctx);
 }
 
 /*========================= End of Function ========================*/
@@ -604,7 +603,7 @@ mono_arch_ip_from_context (void *sigctx)
 
 /*------------------------------------------------------------------*/
 /*                                                                  */
-/* Name                - mono_arch_get_restore_context                     */
+/* Name                - mono_arch_get_restore_context                    */
 /*                                                                  */
 /* Function    - Return the address of the routine that will rest- */
 /*                ore the context.                                  */
@@ -612,12 +611,15 @@ mono_arch_ip_from_context (void *sigctx)
 /*------------------------------------------------------------------*/
 
 gpointer
-mono_arch_get_restore_context ()
+mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
 {
+       g_assert (!aot);
+       if (info)
+               *info = NULL;
+
        return setcontext;
 }
 
-
 /*========================= End of Function ========================*/
 
 /*------------------------------------------------------------------*/