2009-08-25 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions-x86.c
index caa88e58e2347e5ba350125523c96a77f134d6e7..f2d5eee568f9facc2bdf54308bd57c3fb9008ae0 100644 (file)
@@ -24,6 +24,8 @@
 
 #include "mini.h"
 #include "mini-x86.h"
+#include "tasklets.h"
+#include "debug-mini.h"
 
 #ifdef PLATFORM_WIN32
 static void (*restore_stack) (void *);
@@ -130,7 +132,7 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
        do {
                MonoContext new_ctx;
 
-               ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
+               ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, &lmf, NULL);
                if (!ji) {
                        g_warning ("Exception inside function without unwind info");
                        g_assert_not_reached ();
@@ -334,9 +336,19 @@ mono_arch_get_call_filter (void)
        x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esi), 4);
        x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, edi), 4);
 
+       /* align stack and save ESP */
+       x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
+       x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
+       g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
+       x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
+       x86_push_reg (code, X86_EDX);
+
        /* call the handler */
        x86_call_reg (code, X86_ECX);
 
+       /* restore ESP */
+       x86_pop_reg (code, X86_ESP);
+
        /* restore EBP */
        x86_pop_reg (code, X86_EBP);
 
@@ -373,26 +385,32 @@ throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsign
        ctx.ecx = ecx;
        ctx.eax = eax;
 
-       if (mono_debugger_throw_exception ((gpointer)(eip - 5), (gpointer)esp, exc)) {
-               /*
-                * The debugger wants us to stop on the `throw' instruction.
-                * By the time we get here, it already inserted a breakpoint on
-                * eip - 5 (which is the address of the call).
-                */
-               ctx.eip = eip - 5;
-               ctx.esp = esp + sizeof (gpointer);
-               restore_context (&ctx);
-               g_assert_not_reached ();
-       }
-
-       /* adjust eip so that it point into the call instruction */
-       ctx.eip -= 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)eip, 8, buf, sizeof (buf));
+               code = buf + 8;
+
+               if (buf [3] == 0xe8) {
+                       MonoContext ctx_cp = ctx;
+                       ctx_cp.eip = eip - 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.eip -= 1;
+
        mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
        restore_context (&ctx);
 
@@ -485,26 +503,15 @@ mono_arch_get_rethrow_exception (void)
 gpointer 
 mono_arch_get_throw_exception_by_name (void)
 {
-       static guint8* start;
-       static int inited = 0;
+       guint8* start;
        guint8 *code;
 
-       if (inited)
-               return start;
-
-       inited = 1;
-       code = start = mono_global_codeman_reserve (32);
+       start = code = mono_global_codeman_reserve (32);
 
-       x86_push_membase (code, X86_ESP, 4); /* exception name */
-       x86_push_imm (code, "System");
-       x86_push_imm (code, mono_defaults.exception_class->image);
-       x86_call_code (code, mono_exception_from_name);
-       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
-       /* save the newly create object (overwrite exception name)*/
-       x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
-       x86_jump_code (code, mono_arch_get_throw_exception ());
+       /* Not used */
+       x86_breakpoint (code);
 
-       g_assert ((code - start) < 32);
+       mono_arch_flush_icache (start, code - start);
 
        return start;
 }
@@ -566,8 +573,7 @@ mono_arch_get_throw_corlib_exception (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);
@@ -581,58 +587,58 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
        if (managed)
                *managed = FALSE;
 
-       if (ji != NULL) {
-               int offset;
+       *new_ctx = *ctx;
 
-               *new_ctx = *ctx;
+       if (ji != NULL) {
+               gssize regs [MONO_MAX_IREGS + 1];
+               guint8 *cfa;
+               guint32 unwind_info_len;
+               guint8 *unwind_info;
 
                if (managed)
                        if (!ji->method->wrapper_type)
                                *managed = TRUE;
 
-               /*
-                * Some managed methods like pinvoke wrappers might have save_lmf set.
-                * In this case, register save/restore code is not generated by the 
-                * JIT, so we have to restore callee saved registers from the lmf.
-                */
-               if (ji->method->save_lmf) {
-                       /* 
-                        * We only need to do this if the exception was raised in managed
-                        * code, since otherwise the lmf was already popped of the stack.
-                        */
-                       if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
-                               new_ctx->esi = (*lmf)->esi;
-                               new_ctx->edi = (*lmf)->edi;
-                               new_ctx->ebx = (*lmf)->ebx;
-                       }
-               }
-               else {
-                       offset = -1;
-                       /* restore caller saved registers */
-                       if (ji->used_regs & X86_EBX_MASK) {
-                               new_ctx->ebx = *((int *)ctx->ebp + offset);
-                               offset--;
-                       }
-                       if (ji->used_regs & X86_EDI_MASK) {
-                               new_ctx->edi = *((int *)ctx->ebp + offset);
-                               offset--;
-                       }
-                       if (ji->used_regs & X86_ESI_MASK) {
-                               new_ctx->esi = *((int *)ctx->ebp + offset);
-                       }
-               }
+               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);
+
+               regs [X86_EAX] = new_ctx->eax;
+               regs [X86_EBX] = new_ctx->ebx;
+               regs [X86_ECX] = new_ctx->ecx;
+               regs [X86_EDX] = new_ctx->edx;
+               regs [X86_ESP] = new_ctx->esp;
+               regs [X86_EBP] = new_ctx->ebp;
+               regs [X86_ESI] = new_ctx->esi;
+               regs [X86_EDI] = new_ctx->edi;
+               regs [X86_NREG] = new_ctx->eip;
+
+               mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
+                                                  (guint8*)ji->code_start + ji->code_size,
+                                                  ip, regs, MONO_MAX_IREGS + 1, &cfa);
+
+               new_ctx->eax = regs [X86_EAX];
+               new_ctx->ebx = regs [X86_EBX];
+               new_ctx->ecx = regs [X86_ECX];
+               new_ctx->edx = regs [X86_EDX];
+               new_ctx->esp = regs [X86_ESP];
+               new_ctx->ebp = regs [X86_EBP];
+               new_ctx->esi = regs [X86_ESI];
+               new_ctx->edi = regs [X86_EDI];
+               new_ctx->eip = regs [X86_NREG];
+
+               /* The CFA becomes the new SP value */
+               new_ctx->esp = (gssize)cfa;
+
+               /* Adjust IP */
+               new_ctx->eip --;
 
                if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
                        /* remove any unused lmf */
                        *lmf = (gpointer)(((guint32)(*lmf)->previous_lmf) & ~1);
                }
 
-               /* Pop EBP and the return address */
-               new_ctx->esp = ctx->ebp + (2 * sizeof (gpointer));
-               /* we substract 1, so that the IP points into the call instruction */
-               new_ctx->eip = *((int *)ctx->ebp + 1) - 1;
-               new_ctx->ebp = *((int *)ctx->ebp);
-
                /* Pop arguments off the stack */
                {
                        MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
@@ -648,11 +654,11 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
 
                if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
                } else {
-                       if (!(*lmf)->method)
+                       if (!((guint32)((*lmf)->previous_lmf) & 1))
                                /* Top LMF entry */
                                return (gpointer)-1;
                        /* Trampoline lmf frame */
-                       memset (res, 0, sizeof (MonoJitInfo));
+                       memset (res, 0, MONO_SIZEOF_JIT_INFO);
                        res->method = (*lmf)->method;
                }
 
@@ -669,7 +675,8 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
 
                        /* Pop arguments off the stack */
                        /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
-                       if ((*lmf)->method) {
+                       /* FIXME: Handle the IMT/vtable case too */
+                       if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
                                MonoMethod *method = (*lmf)->method;
                                MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
 
@@ -781,6 +788,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, (gpointer)mctx.eip, test_only);
 
        mono_arch_monoctx_to_sigctx (&mctx, sigctx);
@@ -822,6 +832,13 @@ altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
 
        restore_context = mono_arch_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, (gpointer)mctx.eip, FALSE);
        if (stack_ovf)
                prepare_for_guard_pages (&mctx);
@@ -883,3 +900,45 @@ mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean
 #endif
 }
 
+#if MONO_SUPPORT_TASKLETS
+MonoContinuationRestore
+mono_tasklets_arch_restore (void)
+{
+       static guint8* saved = NULL;
+       guint8 *code, *start;
+
+       if (saved)
+               return (MonoContinuationRestore)saved;
+       code = start = mono_global_codeman_reserve (48);
+       /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
+       /* put cont in edx */
+       x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
+       /* setup the copy of the stack */
+       x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
+       x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
+       x86_cld (code);
+       x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
+       x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
+       x86_prefix (code, X86_REP_PREFIX);
+       x86_movsl (code);
+
+       /* now restore the registers from the LMF */
+       x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
+       x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
+       x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
+       x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
+       x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
+
+       /* 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 in eax, so it's setup as the return value */
+       x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
+       x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
+       g_assert ((code - start) <= 48);
+       saved = start;
+       return (MonoContinuationRestore)saved;
+}
+#endif
+