2009-12-07 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions-amd64.c
index 30358ea413b51102f7fc9efd43252e97fdcfc5db..73c8bf8c83c79a890a3f619bdea4b71489d44f24 100644 (file)
 
 #include "mini.h"
 #include "mini-amd64.h"
+#include "tasklets.h"
+#include "debug-mini.h"
 
 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
 
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
 static MonoW32ExceptionHandler fpe_handler;
 static MonoW32ExceptionHandler ill_handler;
 static MonoW32ExceptionHandler segv_handler;
@@ -39,7 +41,7 @@ static MonoW32ExceptionHandler segv_handler;
 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
 
 #define W32_SEH_HANDLE_EX(_ex) \
-       if (_ex##_handler) _ex##_handler((int)sctx)
+       if (_ex##_handler) _ex##_handler(0, er, sctx)
 
 /*
  * Unhandled Exception Filter
@@ -142,7 +144,7 @@ void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
        }
 }
 
-#endif /* PLATFORM_WIN32 */
+#endif /* TARGET_WIN32 */
 
 /*
  * mono_arch_get_restore_context:
@@ -250,7 +252,7 @@ mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean
        amd64_mov_reg_membase (code, AMD64_R13, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r13), 8);
        amd64_mov_reg_membase (code, AMD64_R14, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r14), 8);
        amd64_mov_reg_membase (code, AMD64_R15, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r15), 8);
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        amd64_mov_reg_membase (code, AMD64_RDI, AMD64_ARG_REG1,  G_STRUCT_OFFSET (MonoContext, rdi), 8);
        amd64_mov_reg_membase (code, AMD64_RSI, AMD64_ARG_REG1,  G_STRUCT_OFFSET (MonoContext, rsi), 8);
 #endif
@@ -314,37 +316,32 @@ mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guin
        ctx.rcx = rcx;
        ctx.rdx = rdx;
 
-       if (!rethrow && mono_debugger_throw_exception ((gpointer)(rip - 8), (gpointer)rsp, exc)) {
-               /*
-                * The debugger wants us to stop on the `throw' instruction.
-                * By the time we get here, it already inserted a breakpoint on
-                * eip - 8 (which is the address of the `mov %r15,%rdi ; callq throw').
-                */
-
-               /* FIXME FIXME
-                *
-                * In case of a rethrow, the JIT is emitting code like this:
-                *
-                *    mov    0xffffffffffffffd0(%rbp),%rax'
-                *    mov    %rax,%rdi
-                *    callq  throw
-                *
-                * Here, restore_context() wouldn't restore the %rax register correctly.
-                */
-               ctx.rip = rip - 8;
-               ctx.rsp = rsp + 8;
-               restore_context (&ctx);
-               g_assert_not_reached ();
-       }
-
-       /* adjust eip so that it point into the call instruction */
-       ctx.rip -= 1;
-
        if (mono_object_isinst (exc, mono_defaults.exception_class)) {
                MonoException *mono_ex = (MonoException*)exc;
                if (!rethrow)
                        mono_ex->stack_trace = NULL;
        }
+
+       if (mono_debug_using_mono_debugger ()) {
+               guint8 buf [16], *code;
+
+               mono_breakpoint_clean_code (NULL, (gpointer)rip, 8, buf, sizeof (buf));
+               code = buf + 8;
+
+               if (buf [3] == 0xe8) {
+                       MonoContext ctx_cp = ctx;
+                       ctx_cp.rip = rip - 5;
+
+                       if (mono_debugger_handle_exception (&ctx_cp, exc)) {
+                               restore_context (&ctx_cp);
+                               g_assert_not_reached ();
+                       }
+               }
+       }
+
+       /* adjust eip so that it point into the call instruction */
+       ctx.rip -= 1;
+
        mono_handle_exception (&ctx, exc, (gpointer)rip, FALSE);
        restore_context (&ctx);
 
@@ -389,7 +386,7 @@ get_throw_trampoline (gboolean rethrow, guint32 *code_size, MonoJumpInfo **ji, g
        /* Exception */
        amd64_push_reg (code, AMD64_ARG_REG1);
 
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        /* align stack */
        amd64_push_imm (code, 0);
        amd64_push_imm (code, 0);
@@ -492,11 +489,11 @@ mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji
                amd64_mov_reg_imm (code, AMD64_ARG_REG1, mono_defaults.exception_class->image);
                amd64_mov_reg_imm (code, AMD64_R11, mono_exception_from_token);
        }
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
 #endif
        amd64_call_reg (code, AMD64_R11);
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
 #endif
 
@@ -531,30 +528,27 @@ mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji
        return start;
 }
 
-/* mono_arch_find_jit_info:
+/*
+ * mono_arch_find_jit_info_ext:
  *
- * 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.
+ * 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
+ * is modified if needed.
+ * Returns TRUE on success, FALSE otherwise.
+ * This function is a version of mono_arch_find_jit_info () where all the results are
+ * returned in a StackFrameInfo structure.
  */
-MonoJitInfo *
-mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
-                        MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
+gboolean
+mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
+                                                        MonoJitInfo *ji, MonoContext *ctx, 
+                                                        MonoContext *new_ctx, MonoLMF **lmf, 
+                                                        StackFrameInfo *frame)
 {
-       MonoJitInfo *ji;
        gpointer ip = MONO_CONTEXT_GET_IP (ctx);
 
-       /* 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);
-
-       if (managed)
-               *managed = FALSE;
+       memset (frame, 0, sizeof (StackFrameInfo));
+       frame->ji = ji;
+       frame->managed = FALSE;
 
        *new_ctx = *ctx;
 
@@ -564,9 +558,10 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                guint32 unwind_info_len;
                guint8 *unwind_info;
 
-               if (managed)
-                       if (!ji->method->wrapper_type)
-                               *managed = TRUE;
+               frame->type = FRAME_TYPE_MANAGED;
+
+               if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
+                       frame->managed = TRUE;
 
                if (ji->from_aot)
                        unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
@@ -613,9 +608,10 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
 
                if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
                        /* remove any unused lmf */
-                       *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~1);
+                       *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
                }
 
+#ifndef MONO_AMD64_NO_PUSHES
                /* Pop arguments off the stack */
                {
                        MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
@@ -623,17 +619,36 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                        guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
                        new_ctx->rsp += stack_to_pop;
                }
+#endif
 
-               return ji;
+               return TRUE;
        } else if (*lmf) {
                guint64 rip;
 
+               if (((guint64)(*lmf)->previous_lmf) & 2) {
+                       /* 
+                        * This LMF entry is created by the soft debug code to mark transitions to
+                        * managed code done during invokes.
+                        */
+                       MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
+
+                       g_assert (ext->debugger_invoke);
+
+                       memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
+
+                       *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
+
+                       frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
+
+                       return TRUE;
+               }
+
                if (((guint64)(*lmf)->previous_lmf) & 1) {
                        /* This LMF has the rip field set */
                        rip = (*lmf)->rip;
                } else if ((*lmf)->rsp == 0) {
                        /* Top LMF entry */
-                       return (gpointer)-1;
+                       return FALSE;
                } else {
                        /* 
                         * The rsp field is set just before the call which transitioned to native 
@@ -642,12 +657,15 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                        rip = *(guint64*)((*lmf)->rsp - sizeof (gpointer));
                }
 
-               ji = mono_jit_info_table_find (domain, (gpointer)rip);
+               ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
                if (!ji) {
                        // FIXME: This can happen with multiple appdomains (bug #444383)
-                       return (gpointer)-1;
+                       return FALSE;
                }
 
+               frame->ji = ji;
+               frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
+
                new_ctx->rip = rip;
                new_ctx->rbp = (*lmf)->rbp;
                new_ctx->rsp = (*lmf)->rsp;
@@ -657,17 +675,17 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                new_ctx->r13 = (*lmf)->r13;
                new_ctx->r14 = (*lmf)->r14;
                new_ctx->r15 = (*lmf)->r15;
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                new_ctx->rdi = (*lmf)->rdi;
                new_ctx->rsi = (*lmf)->rsi;
 #endif
 
-               *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~1);
+               *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
 
-               return ji ? ji : res;
+               return TRUE;
        }
 
-       return NULL;
+       return FALSE;
 }
 
 /**
@@ -683,6 +701,9 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 
        mono_arch_sigctx_to_monoctx (sigctx, &mctx);
 
+       if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
+               return TRUE;
+
        mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), test_only);
 
        mono_arch_monoctx_to_sigctx (&mctx, sigctx);
@@ -694,13 +715,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 static inline guint64*
 gregs_from_ucontext (ucontext_t *ctx)
 {
-#ifdef __FreeBSD__
-    guint64 *gregs = (guint64 *) &ctx->uc_mcontext;
-#else
-    guint64 *gregs = (guint64 *) &ctx->uc_mcontext.gregs;
-#endif
-
-       return gregs;
+       return (guint64 *) UCONTEXT_GREGS (ctx);
 }
 #endif
 void
@@ -834,6 +849,13 @@ altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
 
        restore_context = mono_get_restore_context ();
        mono_arch_sigctx_to_monoctx (sigctx, &mctx);
+
+       if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
+               if (stack_ovf)
+                       prepare_for_guard_pages (&mctx);
+               restore_context (&mctx);
+       }
+
        mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), FALSE);
        if (stack_ovf)
                prepare_for_guard_pages (&mctx);
@@ -847,7 +869,7 @@ mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean
        MonoException *exc = NULL;
        ucontext_t *ctx = (ucontext_t*)sigctx;
        guint64 *gregs = gregs_from_ucontext (ctx);
-       MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)gregs [REG_RIP]);
+       MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)gregs [REG_RIP], NULL);
        gpointer *sp;
        int frame_size;
 
@@ -882,8 +904,8 @@ mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean
 #endif
 }
 
-static guint64
-get_original_ip (void)
+guint64
+mono_amd64_get_original_ip (void)
 {
        MonoLMF *lmf = mono_get_lmf ();
 
@@ -904,8 +926,6 @@ mono_arch_get_throw_pending_exception_full (guint32 *code_size, MonoJumpInfo **j
 
        *ji = NULL;
 
-       g_assert (!aot);
-
        start = code = mono_global_codeman_reserve (128);
 
        /* We are in the frame of a managed method after a call */
@@ -925,7 +945,12 @@ mono_arch_get_throw_pending_exception_full (guint32 *code_size, MonoJumpInfo **j
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
 
        /* Obtain the pending exception */
-       amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_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 */
@@ -940,7 +965,12 @@ mono_arch_get_throw_pending_exception_full (guint32 *code_size, MonoJumpInfo **j
        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
 
        /* Obtain the original ip and clear the flag in previous_lmf */
-       amd64_mov_reg_imm (code, AMD64_R11, get_original_ip);
+       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 */
@@ -956,8 +986,13 @@ mono_arch_get_throw_pending_exception_full (guint32 *code_size, MonoJumpInfo **j
        amd64_push_reg (code, AMD64_RAX);
 
        /* Call the throw trampoline */
-       throw_trampoline = mono_get_throw_exception ();
-       amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
+       if (aot) {
+               *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_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);
 
@@ -965,7 +1000,12 @@ mono_arch_get_throw_pending_exception_full (guint32 *code_size, MonoJumpInfo **j
        mono_amd64_patch (br [0], code);
 
        /* Obtain the original ip and clear the flag in previous_lmf */
-       amd64_mov_reg_imm (code, AMD64_R11, get_original_ip);
+       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);
 
@@ -1021,11 +1061,15 @@ mono_arch_exceptions_init (void)
        guint32 code_size;
        MonoJumpInfo *ji;
 
-       /* Call this to avoid initialization races */
-       throw_pending_exception = mono_arch_get_throw_pending_exception_full (&code_size, &ji, FALSE);
+       if (mono_aot_only) {
+               throw_pending_exception = mono_aot_get_named_code ("throw_pending_exception");
+       } else {
+               /* Call this to avoid initialization races */
+               throw_pending_exception = mono_arch_get_throw_pending_exception_full (&code_size, &ji, FALSE);
+       }
 }
 
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
 
 /*
  * The mono_arch_unwindinfo* methods are used to build and add
@@ -1222,7 +1266,7 @@ MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
        PMonoUnwindInfo targetinfo;
        MonoDomain *domain = mono_domain_get ();
 
-       ji = mono_jit_info_table_find (domain, (char*)ControlPc);
+       ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
        if (!ji)
                return 0;
 
@@ -1267,5 +1311,58 @@ mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint
 
 #endif
 
+#if MONO_SUPPORT_TASKLETS
+MonoContinuationRestore
+mono_tasklets_arch_restore (void)
+{
+       static guint8* saved = NULL;
+       guint8 *code, *start;
+       int cont_reg = AMD64_R9; /* register usable on both call conventions */
+
+       if (saved)
+               return (MonoContinuationRestore)saved;
+       code = start = mono_global_codeman_reserve (64);
+       /* 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)
+        * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
+        * We move cont to cont_reg since we need both rcx and rdi for the copy
+        * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
+        */
+       amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
+       amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
+       /* setup the copy of the stack */
+       amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
+       amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
+       x86_cld (code);
+       amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
+       amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
+       amd64_prefix (code, X86_REP_PREFIX);
+       amd64_movsl (code);
+
+       /* now restore the registers from the LMF */
+       amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, lmf), 8);
+       amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbx), 8);
+       amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbp), 8);
+       amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
+       amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
+       amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
+       amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
+#ifdef TARGET_WIN32
+       amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
+       amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
+#endif
+       amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsp), 8);
+
+       /* restore the lmf chain */
+       /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
+       x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
 
+       /* state is already in rax */
+       amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
+       g_assert ((code - start) <= 64);
+       saved = start;
+       return (MonoContinuationRestore)saved;
+}
+#endif