Merge pull request #2306 from migueldeicaza/master
[mono.git] / mono / mini / exceptions-amd64.c
index e93b9942285c8440a1de49f4b8a40e0137e2a661..3f039f74b3e71ad570f7b0cbe5df185b6329566f 100644 (file)
@@ -28,7 +28,7 @@
 #include <mono/metadata/threads-types.h>
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/exception.h>
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/metadata/mono-debug.h>
 #include <mono/utils/mono-mmap.h>
 
@@ -167,7 +167,7 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
 
        /* restore_contect (MonoContext *ctx) */
 
-       start = code = mono_global_codeman_reserve (256);
+       start = code = (guint8 *)mono_global_codeman_reserve (256);
 
        amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG1, 8);
 
@@ -226,7 +226,7 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        GSList *unwind_ops = NULL;
        const guint kMaxCodeSize = NACL_SIZE (128, 256);
 
-       start = code = mono_global_codeman_reserve (kMaxCodeSize);
+       start = code = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize);
 
        /* call_filter (MonoContext *ctx, unsigned long eip) */
        code = start;
@@ -345,7 +345,7 @@ mono_amd64_throw_corlib_exception (guint64 dummy1, guint64 dummy2, guint64 dummy
        mono_amd64_throw_exception (dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, mctx, (MonoObject*)ex, FALSE);
 }
 
-static void
+void
 mono_amd64_resume_unwind (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
                                                  guint64 dummy5, guint64 dummy6,
                                                  MonoContext *mctx, guint32 dummy7, gint64 dummy8)
@@ -381,7 +381,7 @@ get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, g
        dummy_stack_space = 0;
 #endif
 
-       start = code = mono_global_codeman_reserve (kMaxCodeSize);
+       start = code = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize);
 
        /* The stack is unaligned on entry */
        stack_size = ALIGN_TO (sizeof (MonoContext) + 64 + dummy_stack_space, MONO_ARCH_FRAME_ALIGNMENT) + 8;
@@ -445,7 +445,15 @@ get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, g
        }
 
        if (aot) {
-               ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? "mono_amd64_throw_corlib_exception" : "mono_amd64_throw_exception");
+               const char *icall_name;
+
+               if (resume_unwind)
+                       icall_name = "mono_amd64_resume_unwind";
+               else if (corlib)
+                       icall_name = "mono_amd64_throw_corlib_exception";
+               else
+                       icall_name = "mono_amd64_throw_exception";
+               ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
                amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
        } else {
                amd64_mov_reg_imm (code, AMD64_R11, resume_unwind ? ((gpointer)mono_amd64_resume_unwind) : (corlib ? (gpointer)mono_amd64_throw_corlib_exception : (gpointer)mono_amd64_throw_exception));
@@ -503,7 +511,7 @@ mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
 }
 
 /*
- * mono_arch_find_jit_info:
+ * mono_arch_unwind_frame:
  *
  * This function is used to gather information from @ctx, and store it in @frame_info.
  * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
@@ -511,7 +519,7 @@ mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
  * Returns TRUE on success, FALSE otherwise.
  */
 gboolean
-mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
+mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
                                                         MonoJitInfo *ji, MonoContext *ctx, 
                                                         MonoContext *new_ctx, MonoLMF **lmf,
                                                         mgreg_t **save_locations,
@@ -532,7 +540,10 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
                guint8 *unwind_info;
                guint8 *epilog = NULL;
 
-               frame->type = FRAME_TYPE_MANAGED;
+               if (ji->is_trampoline)
+                       frame->type = FRAME_TYPE_TRAMPOLINE;
+               else
+                       frame->type = FRAME_TYPE_MANAGED;
 
                unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
 
@@ -550,9 +561,9 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
                for (i = 0; i < AMD64_NREG; ++i)
                        regs [i] = new_ctx->gregs [i];
 
-               mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
+               mono_unwind_frame (unwind_info, unwind_info_len, (guint8 *)ji->code_start,
                                                   (guint8*)ji->code_start + ji->code_size,
-                                                  ip, epilog ? &epilog : NULL, regs, MONO_MAX_IREGS + 1,
+                                                  (guint8 *)ip, epilog ? &epilog : NULL, regs, MONO_MAX_IREGS + 1,
                                                   save_locations, MONO_MAX_IREGS, &cfa);
 
                for (i = 0; i < AMD64_NREG; ++i)
@@ -579,7 +590,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
 
                        memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
 
-                       *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~7);
+                       *lmf = (MonoLMF *)(((guint64)(*lmf)->previous_lmf) & ~7);
 
                        frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
 
@@ -604,7 +615,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
                        rip = *(guint64*)((*lmf)->rsp - sizeof(mgreg_t));
                }
 
-               ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
+               ji = mini_jit_info_table_find (domain, (char *)rip, NULL);
                /*
                 * FIXME: ji == NULL can happen when a managed-to-native wrapper is interrupted
                 * in the soft debugger suspend code, since (*lmf)->rsp no longer points to the
@@ -641,7 +652,7 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
                        }
                }
 
-               *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~7);
+               *lmf = (MonoLMF *)(((guint64)(*lmf)->previous_lmf) & ~7);
 
                return TRUE;
        }
@@ -657,12 +668,12 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
 static void
 handle_signal_exception (gpointer obj)
 {
-       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
        MonoContext ctx;
 
        memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
 
-       mono_handle_exception (&ctx, obj);
+       mono_handle_exception (&ctx, (MonoObject *)obj);
 
        mono_restore_context (&ctx);
 }
@@ -704,7 +715,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj)
         * 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 = mono_native_tls_get_value (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
 
        /* Pass the ctx parameter in TLS */
        mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
@@ -745,7 +756,7 @@ mono_arch_ip_from_context (void *sigctx)
 static void
 restore_soft_guard_pages (void)
 {
-       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
        if (jit_tls->stack_ovf_guard_base)
                mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
 }
@@ -760,7 +771,7 @@ static void
 prepare_for_guard_pages (MonoContext *mctx)
 {
        gpointer *sp;
-       sp = (gpointer)(mctx->gregs [AMD64_RSP]);
+       sp = (gpointer *)(mctx->gregs [AMD64_RSP]);
        sp -= 1;
        /* the return addr */
        sp [0] = (gpointer)(mctx->gregs [AMD64_RIP]);
@@ -769,7 +780,7 @@ prepare_for_guard_pages (MonoContext *mctx)
 }
 
 static void
-altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
+altstack_handle_and_restore (MonoContext *ctx, MonoObject *obj, gboolean stack_ovf)
 {
        MonoContext mctx;
 
@@ -786,7 +797,7 @@ mono_arch_handle_altstack_exception (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *s
 {
 #if defined(MONO_ARCH_USE_SIGACTION)
        MonoException *exc = NULL;
-       MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_RIP (sigctx), NULL);
+       MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (char *)UCONTEXT_REG_RIP (sigctx), NULL);
        gpointer *sp;
        int frame_size;
        MonoContext *copied_ctx;
@@ -807,8 +818,8 @@ mono_arch_handle_altstack_exception (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *s
        frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4 + 128;
        frame_size += 15;
        frame_size &= ~15;
-       sp = (gpointer)(UCONTEXT_REG_RSP (sigctx) & ~15);
-       sp = (gpointer)((char*)sp - frame_size);
+       sp = (gpointer *)(UCONTEXT_REG_RSP (sigctx) & ~15);
+       sp = (gpointer *)((char*)sp - frame_size);
        copied_ctx = (MonoContext*)(sp + 4);
        /* the arguments must be aligned */
        sp [-1] = (gpointer)UCONTEXT_REG_RIP (sigctx);
@@ -835,170 +846,12 @@ mono_amd64_get_original_ip (void)
        return lmf->rip;
 }
 
-gpointer
-mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
-{
-       guint8 *code, *start;
-       guint8 *br[1];
-       gpointer throw_trampoline;
-       MonoJumpInfo *ji = NULL;
-       GSList *unwind_ops = NULL;
-       const guint kMaxCodeSize = NACL_SIZE (128, 256);
-
-       start = code = mono_global_codeman_reserve (kMaxCodeSize);
-
-       /* We are in the frame of a managed method after a call */
-       /* 
-        * We would like to throw the pending exception in such a way that it looks to
-        * be thrown from the managed method.
-        */
-
-       /* Save registers which might contain the return value of the call */
-       amd64_push_reg (code, AMD64_RAX);
-       amd64_push_reg (code, AMD64_RDX);
-
-       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
-       amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
-
-       /* Align stack */
-       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
-
-       /* Obtain the pending exception */
-       if (aot) {
-               ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
-       } else {
-               amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
-       }
-       amd64_call_reg (code, AMD64_R11);
-
-       /* Check if it is NULL, and branch */
-       amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
-       br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
-
-       /* exc != NULL branch */
-
-       /* Save the exc on the stack */
-       amd64_push_reg (code, AMD64_RAX);
-       /* Align stack */
-       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
-
-       /* Obtain the original ip and clear the flag in previous_lmf */
-       if (aot) {
-               ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
-       } else {
-               amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
-       }
-       amd64_call_reg (code, AMD64_R11);       
-
-       /* Load exc */
-       amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, 8, 8);
-
-       /* Pop saved stuff from the stack */
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 6 * 8);
-
-       /* Setup arguments for the throw trampoline */
-       /* Exception */
-       amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_R11, 8);
-       /* The trampoline expects the caller ip to be pushed on the stack */
-       amd64_push_reg (code, AMD64_RAX);
-
-       /* Call the throw trampoline */
-       if (aot) {
-               ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_throw_exception");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
-       } else {
-               throw_trampoline = mono_get_throw_exception ();
-               amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
-       }
-       /* We use a jump instead of a call so we can push the original ip on the stack */
-       amd64_jump_reg (code, AMD64_R11);
-
-       /* ex == NULL branch */
-       mono_amd64_patch (br [0], code);
-
-       /* Obtain the original ip and clear the flag in previous_lmf */
-       if (aot) {
-               ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
-               amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
-       } else {
-               amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
-       }
-       amd64_call_reg (code, AMD64_R11);       
-       amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
-
-       /* Restore registers */
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
-       amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
-       amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
-       amd64_pop_reg (code, AMD64_RDX);
-       amd64_pop_reg (code, AMD64_RAX);
-
-       /* Return to original code */
-       amd64_jump_reg (code, AMD64_R11);
-
-       g_assert ((code - start) < kMaxCodeSize);
-
-       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
-       mono_arch_flush_icache (start, code - start);
-       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
-
-       if (info)
-               *info = mono_tramp_info_create ("throw_pending_exception", start, code - start, ji, unwind_ops);
-
-       return start;
-}
-
-static gpointer throw_pending_exception;
-
-/*
- * Called when a thread receives an async exception while executing unmanaged code.
- * Instead of checking for this exception in the managed-to-native wrapper, we hijack 
- * the return address on the stack to point to a helper routine which throws the
- * exception.
- */
-void
-mono_arch_notify_pending_exc (MonoThreadInfo *info)
-{
-       MonoLMF *lmf = mono_get_lmf ();
-
-       if (!info) {
-               lmf = mono_get_lmf ();
-       } else {
-               g_assert (mono_thread_info_get_suspend_state (info)->valid);
-               lmf = mono_thread_info_get_suspend_state (info)->unwind_data [MONO_UNWIND_DATA_LMF];
-       }
-
-       if (!lmf)
-               /* Not yet started */
-               return;
-
-       if (lmf->rsp == 0)
-               /* Initial LMF */
-               return;
-
-       if ((guint64)lmf->previous_lmf & 5)
-               /* Already hijacked or trampoline LMF entry */
-               return;
-
-       /* lmf->rsp is set just before making the call which transitions to unmanaged code */
-       lmf->rip = *(guint64*)(lmf->rsp - 8);
-       /* Signal that lmf->rip is set */
-       lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
-
-       *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
-}
-
 GSList*
 mono_amd64_get_exception_trampolines (gboolean aot)
 {
        MonoTrampInfo *info;
        GSList *tramps = NULL;
 
-       mono_arch_get_throw_pending_exception (&info, aot);
-       tramps = g_slist_prepend (tramps, info);
-
        /* LLVM needs different throw trampolines */
        get_throw_trampoline (&info, FALSE, TRUE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", aot);
        tramps = g_slist_prepend (tramps, info);
@@ -1006,7 +859,7 @@ mono_amd64_get_exception_trampolines (gboolean aot)
        get_throw_trampoline (&info, FALSE, TRUE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", aot);
        tramps = g_slist_prepend (tramps, info);
 
-       get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", FALSE);
+       get_throw_trampoline (&info, FALSE, TRUE, TRUE, TRUE, "llvm_resume_unwind_trampoline", aot);
        tramps = g_slist_prepend (tramps, info);
 
        return tramps;
@@ -1019,7 +872,6 @@ mono_arch_exceptions_init (void)
        gpointer tramp;
 
        if (mono_aot_only) {
-               throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
                tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
                mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
                tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
@@ -1028,14 +880,12 @@ mono_arch_exceptions_init (void)
                mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
        } else {
                /* Call this to avoid initialization races */
-               throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
-
                tramps = mono_amd64_get_exception_trampolines (FALSE);
                for (l = tramps; l; l = l->next) {
-                       MonoTrampInfo *info = l->data;
+                       MonoTrampInfo *info = (MonoTrampInfo *)l->data;
 
                        mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
-                       mono_tramp_info_register (info);
+                       mono_tramp_info_register (info, NULL);
                }
                g_slist_free (tramps);
        }
@@ -1295,7 +1145,7 @@ mono_tasklets_arch_restore (void)
 
        if (saved)
                return (MonoContinuationRestore)saved;
-       code = start = mono_global_codeman_reserve (kMaxCodeSize);
+       code = start = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize);
        /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
        /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
         * state is in AMD64_ARG_REG2 ($rdx or $rsi)