Merge pull request #487 from mayerwin/patch-1
[mono.git] / mono / mini / exceptions-x86.c
index 9d6f9cadc737fadd9de56c36c56cec1fc8fdd4b7..a8c8c0f2bd4c94c129c7645923f4a0668af3a874 100644 (file)
 #include "tasklets.h"
 #include "debug-mini.h"
 
+static gpointer signal_exception_trampoline;
+
+gpointer
+mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
+
 #ifdef TARGET_WIN32
 static void (*restore_stack) (void *);
 
@@ -34,10 +39,16 @@ static MonoW32ExceptionHandler fpe_handler;
 static MonoW32ExceptionHandler ill_handler;
 static MonoW32ExceptionHandler segv_handler;
 
-static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
+LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
+guint64 mono_win_chained_exception_filter_result;
+gboolean mono_win_chained_exception_filter_didrun;
+
+#ifndef PROCESS_CALLBACK_FILTER_ENABLED
+#      define PROCESS_CALLBACK_FILTER_ENABLED 1
+#endif
 
 #define W32_SEH_HANDLE_EX(_ex) \
-       if (_ex##_handler) _ex##_handler(0, er, sctx)
+       if (_ex##_handler) _ex##_handler(0, ep, sctx)
 
 /*
  * mono_win32_get_handle_stackoverflow (void):
@@ -72,8 +83,7 @@ mono_win32_get_handle_stackoverflow (void)
        /* get stack overflow exception from domain object */
        x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
 
-       /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
-       x86_push_imm (code, 0);
+       /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
        x86_push_reg (code, X86_EAX);
        x86_push_reg (code, X86_EBX);
        x86_call_code (code, mono_arch_handle_exception);
@@ -107,7 +117,7 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
        DWORD page_size;
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo rji;
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = jit_tls->lmf;            
        MonoContext initial_ctx;
        MonoContext ctx;
@@ -133,7 +143,7 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
        do {
                MonoContext new_ctx;
 
-               mono_arch_find_jit_info_ext (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, &frame);
+               mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, NULL, &frame);
                if (!frame.ji) {
                        g_warning ("Exception inside function without unwind info");
                        g_assert_not_reached ();
@@ -167,6 +177,7 @@ LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
        struct sigcontext* sctx;
        LONG res;
 
+       mono_win_chained_exception_filter_didrun = FALSE;
        res = EXCEPTION_CONTINUE_EXECUTION;
 
        er = ep->ExceptionRecord;
@@ -219,6 +230,9 @@ LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
 
        g_free (sctx);
 
+       if (mono_win_chained_exception_filter_didrun)
+               res = mono_win_chained_exception_filter_result;
+
        return res;
 }
 
@@ -228,12 +242,12 @@ void win32_seh_init()
        if (!restore_stack)
                restore_stack = mono_win32_get_handle_stackoverflow ();
 
-       old_handler = SetUnhandledExceptionFilter(seh_handler);
+       mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_handler);
 }
 
 void win32_seh_cleanup()
 {
-       if (old_handler) SetUnhandledExceptionFilter(old_handler);
+       if (mono_old_win_toplevel_exception_filter) SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
 }
 
 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
@@ -299,8 +313,17 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
        /* jump to the saved IP */
        x86_ret (code);
 
+       nacl_global_codeman_validate(&start, 128, &code);
+
        if (info)
                *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
+       else {
+               GSList *l;
+
+               for (l = unwind_ops; l; l = l->next)
+                       g_free (l->data);
+               g_slist_free (unwind_ops);
+       }
 
        return start;
 }
@@ -319,9 +342,10 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        guint8 *code;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
+       guint kMaxCodeSize = NACL_SIZE (64, 128);
 
        /* call_filter (MonoContext *ctx, unsigned long eip) */
-       start = code = mono_global_codeman_reserve (64);
+       start = code = mono_global_codeman_reserve (kMaxCodeSize);
 
        x86_push_reg (code, X86_EBP);
        x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
@@ -366,10 +390,19 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        x86_leave (code);
        x86_ret (code);
 
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
+
        if (info)
                *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
+       else {
+               GSList *l;
 
-       g_assert ((code - start) < 64);
+               for (l = unwind_ops; l; l = l->next)
+                       g_free (l->data);
+               g_slist_free (unwind_ops);
+       }
+
+       g_assert ((code - start) < kMaxCodeSize);
        return start;
 }
 
@@ -429,7 +462,7 @@ mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
        /* adjust eip so that it point into the call instruction */
        ctx.eip -= 1;
 
-       mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
+       mono_handle_exception (&ctx, exc);
 
        restore_context (&ctx);
 
@@ -447,11 +480,33 @@ mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
 
        eip -= pc_offset;
 
+       /* Negate the ip adjustment done in mono_x86_throw_exception () */
+       eip += 1;
+
        mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
 }
 
+static void
+mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc, 
+                                               mgreg_t eip, gboolean rethrow)
+{
+       MonoContext ctx;
+
+       ctx.esp = regs [X86_ESP];
+       ctx.eip = eip;
+       ctx.ebp = regs [X86_EBP];
+       ctx.edi = regs [X86_EDI];
+       ctx.esi = regs [X86_ESI];
+       ctx.ebx = regs [X86_EBX];
+       ctx.edx = regs [X86_EDX];
+       ctx.ecx = regs [X86_ECX];
+       ctx.eax = regs [X86_EAX];
+
+       mono_resume_unwind (&ctx);
+}
+
 /*
- * get_throw_exception:
+ * get_throw_trampoline:
  *
  *  Generate a call to mono_x86_throw_exception/
  * mono_x86_throw_corlib_exception.
@@ -459,14 +514,15 @@ mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
  * which doesn't push the arguments.
  */
 static guint8*
-get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, MonoTrampInfo **info, gboolean aot)
+get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
 {
        guint8 *start, *code;
        int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
        MonoJumpInfo *ji = NULL;
        GSList *unwind_ops = NULL;
+       guint kMaxCodeSize = NACL_SIZE (128, 256);
 
-       start = code = mono_global_codeman_reserve (128);
+       start = code = mono_global_codeman_reserve (kMaxCodeSize);
 
        stack_size = 128;
 
@@ -515,11 +571,14 @@ get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean
                        /* We don't generate stack alignment code on osx to save space */
 #endif
                } else {
-                       /* One argument */
+                       /* One argument + stack alignment */
                        stack_offset = stack_size + 4 + 4;
 #ifdef __APPLE__
                        /* Pop the alignment added by OP_THROW too */
                        stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
+#else
+                       if (mono_do_x86_stack_align)
+                               stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
 #endif
                }
        }
@@ -530,18 +589,33 @@ get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean
        /* Set arg1 == regs */
        x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
        x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
-       /* Set arg2 == exc */
-       x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
+       /* Set arg2 == exc/ex_token_index */
+       if (resume_unwind)
+               x86_mov_reg_imm (code, X86_EAX, 0);
+       else
+               x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
        x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
        /* Set arg3 == eip */
-       x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
+       if (llvm_abs)
+               x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
+       else
+               x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
        x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
-       if (corlib) {
-               /* Set arg4 == offset */
+       /* Set arg4 == rethrow/pc_offset */
+       if (resume_unwind) {
+               x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
+       } else if (corlib) {
                x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
+               if (llvm_abs) {
+                       /* 
+                        * The caller is LLVM code which passes the absolute address not a pc offset,
+                        * so compensate by passing 0 as 'ip' and passing the negated abs address as
+                        * the pc offset.
+                        */
+                       x86_neg_reg (code, X86_EAX);
+               }
                x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
        } else {
-               /* Set arg4 == rethrow */
                x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
        }
        /* Make the call */
@@ -553,16 +627,23 @@ get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean
                code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? "mono_x86_throw_corlib_exception" : "mono_x86_throw_exception");
                x86_call_reg (code, X86_EAX);
        } else {
-               x86_call_code (code, corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception);
+               x86_call_code (code, resume_unwind ? (gpointer)(mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
        }
        x86_breakpoint (code);
 
-       g_assert ((code - start) < 128);
+       nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
 
-       mono_save_trampoline_xdebug_info (corlib ? "llvm_throw_corlib_exception_trampoline" : "llvm_throw_exception_trampoline", start, code - start, unwind_ops);
+       g_assert ((code - start) < kMaxCodeSize);
 
        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);
+               *info = mono_tramp_info_create (g_strdup (name), start, code - start, ji, unwind_ops);
+       else {
+               GSList *l;
+
+               for (l = unwind_ops; l; l = l->next)
+                       g_free (l->data);
+               g_slist_free (unwind_ops);
+       }
 
        return start;
 }
@@ -582,13 +663,13 @@ get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean
 gpointer 
 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
 {
-       return get_throw_exception ("throw_exception_trampoline", FALSE, FALSE, FALSE, info, aot);
+       return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
 }
 
 gpointer 
 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
 {
-       return get_throw_exception ("rethow_exception_trampoline", TRUE, FALSE, FALSE, info, aot);
+       return get_throw_trampoline ("rethrow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
 }
 
 /**
@@ -604,7 +685,7 @@ mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
 gpointer 
 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
 {
-       return get_throw_exception ("throw_corlib_exception_trampoline", FALSE, FALSE, TRUE, info, aot);
+       return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
 }
 
 void
@@ -612,35 +693,67 @@ mono_arch_exceptions_init (void)
 {
        guint8 *tramp;
 
-       if (mono_aot_only)
+/* 
+ * If we're running WoW64, we need to set the usermode exception policy 
+ * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
+ * or (eventually) Windows 7 SP1.
+ */
+#ifdef HOST_WIN32
+       DWORD flags;
+       FARPROC getter;
+       FARPROC setter;
+       HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
+
+       if (kernel32) {
+               getter = GetProcAddress (kernel32, "GetProcessUserModeExceptionPolicy");
+               setter = GetProcAddress (kernel32, "SetProcessUserModeExceptionPolicy");
+               if (getter && setter) {
+                       if (getter (&flags))
+                               setter (flags & ~PROCESS_CALLBACK_FILTER_ENABLED);
+               }
+       }
+#endif
+
+       if (mono_aot_only) {
+               signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
                return;
+       }
 
        /* LLVM needs different throw trampolines */
-       tramp = get_throw_exception ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, NULL, FALSE);
+       tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
+       mono_register_jit_icall (tramp, "llvm_throw_exception_trampoline", NULL, TRUE);
+
+       tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
+       mono_register_jit_icall (tramp, "llvm_rethrow_exception_trampoline", NULL, TRUE);
+
+       tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
+       mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
 
-       mono_register_jit_icall (tramp, "mono_arch_llvm_throw_exception", NULL, TRUE);
+       tramp = get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
+       mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
 
-       tramp = get_throw_exception ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, NULL, FALSE);
+       tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
+       mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
 
-       mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
+       signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
 }
 
 /*
- * mono_arch_find_jit_info_ext:
+ * mono_arch_find_jit_info:
  *
  * See exceptions-amd64.c for docs.
  */
 gboolean
-mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
+mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
                                                         MonoJitInfo *ji, MonoContext *ctx, 
-                                                        MonoContext *new_ctx, MonoLMF **lmf, 
+                                                        MonoContext *new_ctx, MonoLMF **lmf,
+                                                        mgreg_t **save_locations,
                                                         StackFrameInfo *frame)
 {
        gpointer ip = MONO_CONTEXT_GET_IP (ctx);
 
        memset (frame, 0, sizeof (StackFrameInfo));
        frame->ji = ji;
-       frame->managed = FALSE;
 
        *new_ctx = *ctx;
 
@@ -652,9 +765,6 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
 
                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);
                else
@@ -672,7 +782,8 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
 
                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);
+                                                  ip, regs, MONO_MAX_IREGS + 1,
+                                                  save_locations, MONO_MAX_IREGS, &cfa);
 
                new_ctx->eax = regs [X86_EAX];
                new_ctx->ebx = regs [X86_EBX];
@@ -698,15 +809,11 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
                /* Pop arguments off the stack */
                /* 
                 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
-                * the caller.
+                * the callee.
                 */
 #ifndef ENABLE_LLVM
-               {
-                       MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
-
-                       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->esp += stack_to_pop;
-               }
+               if (ji->has_arch_eh_info)
+                       new_ctx->esp += mono_jit_info_get_arch_eh_info (ji)->stack_size;
 #endif
 
                return TRUE;
@@ -735,6 +842,7 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
                        if (!((guint32)((*lmf)->previous_lmf) & 1))
                                /* Top LMF entry */
                                return FALSE;
+                       g_assert_not_reached ();
                        /* Trampoline lmf frame */
                        frame->method = (*lmf)->method;
                }
@@ -745,6 +853,9 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
                new_ctx->ebp = (*lmf)->ebp;
                new_ctx->eip = (*lmf)->eip;
 
+               /* Adjust IP */
+               new_ctx->eip --;
+
                frame->ji = ji;
                frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
 
@@ -756,14 +867,16 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
                        /* Pop arguments off the stack */
                        /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
                        /* FIXME: Handle the IMT/vtable case too */
+#if 0
 #ifndef ENABLE_LLVM
-                       if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
+                       if ((*lmf)->method) {
                                MonoMethod *method = (*lmf)->method;
                                MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
 
-                               guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
+                               guint32 stack_to_pop = mono_arch_get_argument_info (NULL, mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
                                new_ctx->esp += stack_to_pop;
                        }
+#endif
 #endif
                }
                else
@@ -779,93 +892,161 @@ mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
        return FALSE;
 }
 
-#ifdef __sun
-#define REG_EAX EAX
-#define REG_EBX EBX
-#define REG_ECX ECX
-#define REG_EDX EDX
-#define REG_EBP EBP
-#define REG_ESP ESP
-#define REG_ESI ESI
-#define REG_EDI EDI
-#define REG_EIP EIP
-#endif
-
 void
 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
 {
-#ifdef MONO_ARCH_USE_SIGACTION
-       ucontext_t *ctx = (ucontext_t*)sigctx;
-       
-       mctx->eax = UCONTEXT_REG_EAX (ctx);
-       mctx->ebx = UCONTEXT_REG_EBX (ctx);
-       mctx->ecx = UCONTEXT_REG_ECX (ctx);
-       mctx->edx = UCONTEXT_REG_EDX (ctx);
-       mctx->ebp = UCONTEXT_REG_EBP (ctx);
-       mctx->esp = UCONTEXT_REG_ESP (ctx);
-       mctx->esi = UCONTEXT_REG_ESI (ctx);
-       mctx->edi = UCONTEXT_REG_EDI (ctx);
-       mctx->eip = UCONTEXT_REG_EIP (ctx);
-#else  
-       struct sigcontext *ctx = (struct sigcontext *)sigctx;
-
-       mctx->eax = ctx->SC_EAX;
-       mctx->ebx = ctx->SC_EBX;
-       mctx->ecx = ctx->SC_ECX;
-       mctx->edx = ctx->SC_EDX;
-       mctx->ebp = ctx->SC_EBP;
-       mctx->esp = ctx->SC_ESP;
-       mctx->esi = ctx->SC_ESI;
-       mctx->edi = ctx->SC_EDI;
-       mctx->eip = ctx->SC_EIP;
-#endif
+       mono_sigctx_to_monoctx (sigctx, mctx);
 }
 
 void
 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
 {
-#ifdef MONO_ARCH_USE_SIGACTION
-       ucontext_t *ctx = (ucontext_t*)sigctx;
-
-       UCONTEXT_REG_EAX (ctx) = mctx->eax;
-       UCONTEXT_REG_EBX (ctx) = mctx->ebx;
-       UCONTEXT_REG_ECX (ctx) = mctx->ecx;
-       UCONTEXT_REG_EDX (ctx) = mctx->edx;
-       UCONTEXT_REG_EBP (ctx) = mctx->ebp;
-       UCONTEXT_REG_ESP (ctx) = mctx->esp;
-       UCONTEXT_REG_ESI (ctx) = mctx->esi;
-       UCONTEXT_REG_EDI (ctx) = mctx->edi;
-       UCONTEXT_REG_EIP (ctx) = mctx->eip;
-#else
-       struct sigcontext *ctx = (struct sigcontext *)sigctx;
-
-       ctx->SC_EAX = mctx->eax;
-       ctx->SC_EBX = mctx->ebx;
-       ctx->SC_ECX = mctx->ecx;
-       ctx->SC_EDX = mctx->edx;
-       ctx->SC_EBP = mctx->ebp;
-       ctx->SC_ESP = mctx->esp;
-       ctx->SC_ESI = mctx->esi;
-       ctx->SC_EDI = mctx->edi;
-       ctx->SC_EIP = mctx->eip;
-#endif
-}      
+       mono_monoctx_to_sigctx (mctx, sigctx);
+}
 
 gpointer
 mono_arch_ip_from_context (void *sigctx)
 {
+#if defined(__native_client__)
+       printf("WARNING: mono_arch_ip_from_context() called!\n");
+       return (NULL);
+#else
 #ifdef MONO_ARCH_USE_SIGACTION
        ucontext_t *ctx = (ucontext_t*)sigctx;
        return (gpointer)UCONTEXT_REG_EIP (ctx);
 #else
        struct sigcontext *ctx = sigctx;
        return (gpointer)ctx->SC_EIP;
-#endif 
+#endif
+#endif /* __native_client__ */
+}
+
+/*
+ * handle_exception:
+ *
+ *   Called by resuming from a signal handler.
+ */
+static void
+handle_signal_exception (gpointer obj)
+{
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+       MonoContext ctx;
+       static void (*restore_context) (MonoContext *);
+
+       if (!restore_context)
+               restore_context = mono_get_restore_context ();
+
+       memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
+
+       if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
+               return;
+
+       mono_handle_exception (&ctx, obj);
+
+       restore_context (&ctx);
+}
+
+/*
+ * mono_x86_get_signal_exception_trampoline:
+ *
+ *   This x86 specific trampoline is used to call handle_signal_exception.
+ */
+gpointer
+mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
+{
+       guint8 *start, *code;
+       MonoJumpInfo *ji = NULL;
+       GSList *unwind_ops = NULL;
+       int stack_size;
+
+       start = code = mono_global_codeman_reserve (128);
+
+       /* Caller ip */
+       x86_push_reg (code, X86_ECX);
+
+       mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
+       mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
+
+       /* Fix the alignment to be what apple expects */
+       stack_size = 12;
+
+       x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
+       mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
+
+       /* Arg1 */
+       x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
+       /* Branch to target */
+       x86_call_reg (code, X86_EDX);
+
+       g_assert ((code - start) < 128);
+
+       if (info)
+               *info = mono_tramp_info_create (g_strdup ("x86_signal_exception_trampoline"), start, code - start, ji, unwind_ops);
+       else {
+               GSList *l;
+
+               for (l = unwind_ops; l; l = l->next)
+                       g_free (l->data);
+               g_slist_free (unwind_ops);
+       }
+
+       return start;
+}
+
+
+void
+mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
+{
+       /*
+        * Can't pass the obj on the stack, since we are executing on the
+        * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
+        * So put it into a register, and branch to a trampoline which
+        * pushes it.
+        */
+       ctx->eax = (mgreg_t)user_data;
+       ctx->ecx = ctx->eip;
+       ctx->edx = (mgreg_t)async_cb;
+
+       /*align the stack*/
+       ctx->esp = (ctx->esp - 16) & ~15;
+       ctx->eip = (mgreg_t)signal_exception_trampoline;
 }
 
 gboolean
-mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
+mono_arch_handle_exception (void *sigctx, gpointer obj)
 {
+#if defined(MONO_ARCH_USE_SIGACTION)
+       MonoContext mctx;
+       ucontext_t *ctx = (ucontext_t*)sigctx;
+
+       /*
+        * Handling the exception in the signal handler is problematic, since the original
+        * 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);
+
+       /* Pass the ctx parameter in TLS */
+       mono_arch_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
+
+       mctx = jit_tls->ex_ctx;
+       mono_setup_async_callback (&mctx, handle_signal_exception, obj);
+       mono_monoctx_to_sigctx (&mctx, sigctx);
+
+       return TRUE;
+#elif defined (TARGET_WIN32)
+       MonoContext mctx;
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+       struct sigcontext *ctx = (struct sigcontext *)sigctx;
+
+       mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
+
+       mctx = jit_tls->ex_ctx;
+       mono_setup_async_callback (&mctx, handle_signal_exception, obj);
+       mono_monoctx_to_sigctx (&mctx, sigctx);
+
+       return TRUE;
+#else
        MonoContext mctx;
 
        mono_arch_sigctx_to_monoctx (sigctx, &mctx);
@@ -873,17 +1054,18 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
        if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
                return TRUE;
 
-       mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
+       mono_handle_exception (&mctx, obj);
 
        mono_arch_monoctx_to_sigctx (&mctx, sigctx);
 
        return TRUE;
+#endif
 }
 
 static void
 restore_soft_guard_pages (void)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = 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);
 }
@@ -907,13 +1089,13 @@ prepare_for_guard_pages (MonoContext *mctx)
 }
 
 static void
-altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
+altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
 {
        void (*restore_context) (MonoContext *);
        MonoContext mctx;
 
        restore_context = mono_get_restore_context ();
-       mono_arch_sigctx_to_monoctx (sigctx, &mctx);
+       mctx = *ctx;
 
        if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
                if (stack_ovf)
@@ -921,7 +1103,7 @@ altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
                restore_context (&mctx);
        }
 
-       mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
+       mono_handle_exception (&mctx, obj);
        if (stack_ovf)
                prepare_for_guard_pages (&mctx);
        restore_context (&mctx);
@@ -962,7 +1144,8 @@ mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean
         *   ctx arg
         *   return ip
         */
-       frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
+       // FIXME: test_only is no more.
+       frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4;
        frame_size += 15;
        frame_size &= ~15;
        sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
@@ -974,8 +1157,7 @@ mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean
        sp [0] = sp + 4;
        sp [1] = exc;
        sp [2] = (gpointer)stack_ovf;
-       /* may need to adjust pointers in the new struct copy, depending on the OS */
-       memcpy (sp + 4, ctx, sizeof (ucontext_t));
+       mono_sigctx_to_monoctx (sigctx, (MonoContext*)(sp + 4));
        /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
        UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
        UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
@@ -989,12 +1171,18 @@ mono_tasklets_arch_restore (void)
        static guint8* saved = NULL;
        guint8 *code, *start;
 
+#ifdef __native_client_codegen__
+       g_print("mono_tasklets_arch_restore needs to be aligned for Native Client\n");
+#endif
        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);
+        /* state in eax, so it's setup as the return value */
+        x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 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);
@@ -1015,8 +1203,6 @@ mono_tasklets_arch_restore (void)
        /*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;
@@ -1024,3 +1210,18 @@ mono_tasklets_arch_restore (void)
 }
 #endif
 
+/*
+ * mono_arch_setup_resume_sighandler_ctx:
+ *
+ *   Setup CTX so execution continues at FUNC.
+ */
+void
+mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
+{
+       int align = (((gint32)MONO_CONTEXT_GET_SP (ctx)) % MONO_ARCH_FRAME_ALIGNMENT + 4);
+
+       if (align != 0)
+               MONO_CONTEXT_SET_SP (ctx, (gsize)MONO_CONTEXT_GET_SP (ctx) - align);
+
+       MONO_CONTEXT_SET_IP (ctx, func);
+}