[runtime] Avoid crashing in mini_jit_info_table_find () if the current thread is...
[mono.git] / mono / mini / exceptions-x86.c
index 46370e18f01b3a5ba3ef8b17091ac9a8d2af0da8..669f18330234eac828c89402bac5c8405efadfaa 100644 (file)
@@ -8,6 +8,13 @@
  */
 
 #include <config.h>
+
+#if _WIN32_WINNT < 0x0501
+/* Required for Vectored Exception Handling. */
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif /* _WIN32_WINNT < 0x0501 */
+
 #include <glib.h>
 #include <signal.h>
 #include <string.h>
@@ -39,14 +46,30 @@ 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;
+gpointer mono_win_vectored_exception_handle;
+extern gboolean mono_win_chained_exception_needs_run;
+extern int (*gUnhandledExceptionHandler)(EXCEPTION_POINTERS*);
 
 #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)
+
+LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
+{
+#ifndef MONO_CROSS_COMPILE
+       if (mono_old_win_toplevel_exception_filter) {
+               return (*mono_old_win_toplevel_exception_filter)(ep);
+       }
+#endif
+
+       mono_handle_native_sigsegv (SIGSEGV, NULL);
+
+       return EXCEPTION_CONTINUE_SEARCH;
+}
 
 /*
  * mono_win32_get_handle_stackoverflow (void):
@@ -81,8 +104,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);
@@ -116,7 +138,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;
@@ -142,7 +164,7 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
        do {
                MonoContext new_ctx;
 
-               mono_arch_find_jit_info (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 ();
@@ -169,13 +191,14 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
  * Unhandled Exception Filter
  * Top-level per-process exception handler.
  */
-LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
+LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
 {
        EXCEPTION_RECORD* er;
        CONTEXT* ctx;
        struct sigcontext* sctx;
        LONG res;
 
+       mono_win_chained_exception_needs_run = FALSE;
        res = EXCEPTION_CONTINUE_EXECUTION;
 
        er = ep->ExceptionRecord;
@@ -215,18 +238,30 @@ LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
                break;
        }
 
-       /* Copy context back */
-       ctx->Eax = sctx->eax;
-       ctx->Ebx = sctx->ebx;
-       ctx->Ecx = sctx->ecx;
-       ctx->Edx = sctx->edx;
-       ctx->Ebp = sctx->ebp;
-       ctx->Esp = sctx->esp;
-       ctx->Esi = sctx->esi;
-       ctx->Edi = sctx->edi;
-       ctx->Eip = sctx->eip;
+       if (mono_win_chained_exception_needs_run) {
+               /* Don't copy context back if we chained exception
+               * as the handler may have modfied the EXCEPTION_POINTERS
+               * directly. We don't pass sigcontext to chained handlers.
+               * Return continue search so the UnhandledExceptionFilter
+               * can correctly chain the exception.
+               */
+               res = EXCEPTION_CONTINUE_SEARCH;
+       } else {
+               /* Copy context back */
+               ctx->Eax = sctx->eax;
+               ctx->Ebx = sctx->ebx;
+               ctx->Ecx = sctx->ecx;
+               ctx->Edx = sctx->edx;
+               ctx->Ebp = sctx->ebp;
+               ctx->Esp = sctx->esp;
+               ctx->Esi = sctx->esi;
+               ctx->Edi = sctx->edi;
+               ctx->Eip = sctx->eip;
+       }
 
-       g_free (sctx);
+       /* TODO: Find right place to free this in stack overflow case */
+       if (er->ExceptionCode != EXCEPTION_STACK_OVERFLOW)
+               g_free (sctx);
 
        return res;
 }
@@ -237,12 +272,14 @@ 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_unhandled_exception_filter);
+       mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_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);
+       RemoveVectoredExceptionHandler (seh_unhandled_exception_filter);
 }
 
 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
@@ -284,36 +321,60 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
        /* load ctx */
        x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
 
-       /* get return address, stored in ECX */
-       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eip), 4);
        /* restore EBX */
        x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebx), 4);
+
        /* restore EDI */
        x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, edi), 4);
+
        /* restore ESI */
        x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esi), 4);
-       /* restore ESP */
-       x86_mov_reg_membase (code, X86_ESP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esp), 4);
-       /* save the return addr to the restored stack */
-       x86_push_reg (code, X86_ECX);
-       /* restore EBP */
-       x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebp), 4);
-       /* restore ECX */
-       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ecx), 4);
+
        /* restore EDX */
        x86_mov_reg_membase (code, X86_EDX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, edx), 4);
-       /* restore EAX */
-       x86_mov_reg_membase (code, X86_EAX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eax), 4);
 
+       /*
+        * The context resides on the stack, in the stack frame of the
+        * caller of this function.  The stack pointer that we need to
+        * restore is potentially many stack frames higher up, so the
+        * distance between them can easily be more than the red zone
+        * size.  Hence the stack pointer can be restored only after
+        * we have finished loading everything from the context.
+        */
+
+       /* load ESP into EBP */
+       x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esp), 4);
+       /* load return address into ECX */
+       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eip), 4);
+       /* save the return addr to the restored stack - 4 */
+       x86_mov_membase_reg (code, X86_EBP, -4, X86_ECX, 4);
+
+       /* load EBP into ECX */
+       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebp), 4);
+       /* save EBP to the restored stack - 8 */
+       x86_mov_membase_reg (code, X86_EBP, -8, X86_ECX, 4);
+
+       /* load EAX into ECX */
+       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eax), 4);
+       /* save EAX to the restored stack - 12 */
+       x86_mov_membase_reg (code, X86_EBP, -12, X86_ECX, 4);
+
+       /* restore ECX */
+       x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ecx), 4);
+
+       /* restore ESP - 12 */
+       x86_lea_membase (code, X86_ESP, X86_EBP, -12);
+       /* restore EAX */
+       x86_pop_reg (code, X86_EAX);
+       /* restore EBP */
+       x86_pop_reg (code, X86_EBP);
        /* jump to the saved IP */
        x86_ret (code);
 
-#if defined(__native_client_codegen__) && defined(__native_client__)
        nacl_global_codeman_validate(&start, 128, &code);
-#endif
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
+               *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
        else {
                GSList *l;
 
@@ -387,12 +448,10 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
        x86_leave (code);
        x86_ret (code);
 
-#if defined(__native_client_codegen__) && defined(__native_client__)
        nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
-#endif
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
+               *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
        else {
                GSList *l;
 
@@ -461,7 +520,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);
 
@@ -626,18 +685,16 @@ get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolea
                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, resume_unwind ? (mono_x86_resume_unwind) : (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);
 
-#if defined(__native_client_codegen__) && defined(__native_client__)
        nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
-#endif
 
        g_assert ((code - start) < kMaxCodeSize);
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup (name), start, code - start, ji, unwind_ops);
+               *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
        else {
                GSList *l;
 
@@ -747,14 +804,14 @@ mono_arch_exceptions_init (void)
 gboolean
 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;
 
@@ -766,9 +823,6 @@ mono_arch_find_jit_info (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
@@ -786,7 +840,8 @@ mono_arch_find_jit_info (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];
@@ -812,15 +867,11 @@ mono_arch_find_jit_info (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;
@@ -874,14 +925,16 @@ mono_arch_find_jit_info (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) {
                                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
@@ -897,94 +950,17 @@ mono_arch_find_jit_info (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)
 {
-#if defined (__native_client__)
-       printf("WARNING: mono_arch_sigctx_to_monoctx() called!\n");
-       mctx->eax = 0xDEADBEEF;
-       mctx->ebx = 0xDEADBEEF;
-       mctx->ecx = 0xDEADBEEF;
-       mctx->edx = 0xDEADBEEF;
-       mctx->ebp = 0xDEADBEEF;
-       mctx->esp = 0xDEADBEEF;
-       mctx->esi = 0xDEADBEEF;
-       mctx->edi = 0xDEADBEEF;
-       mctx->eip = 0xDEADBEEF;
-#else
-#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
-#endif /* if defined(__native_client__) */
+       mono_sigctx_to_monoctx (sigctx, mctx);
 }
 
 void
 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
 {
-#if defined(__native_client__)
-       printf("WARNING: mono_arch_monoctx_to_sigctx() called!\n");
-#else
-#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
-#endif /* __native_client__ */
-}      
+       mono_monoctx_to_sigctx (mctx, sigctx);
+}
 
 gpointer
 mono_arch_ip_from_context (void *sigctx)
@@ -1011,7 +987,7 @@ mono_arch_ip_from_context (void *sigctx)
 static void
 handle_signal_exception (gpointer obj)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoContext ctx;
        static void (*restore_context) (MonoContext *);
 
@@ -1023,7 +999,7 @@ handle_signal_exception (gpointer obj)
        if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
                return;
 
-       mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), FALSE);
+       mono_handle_exception (&ctx, obj);
 
        restore_context (&ctx);
 }
@@ -1063,7 +1039,7 @@ mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
        g_assert ((code - start) < 128);
 
        if (info)
-               *info = mono_tramp_info_create (g_strdup ("x86_signal_exception_trampoline"), start, code - start, ji, unwind_ops);
+               *info = mono_tramp_info_create ("x86_signal_exception_trampoline", start, code - start, ji, unwind_ops);
        else {
                GSList *l;
 
@@ -1075,10 +1051,30 @@ mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
        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;
 
        /*
@@ -1086,54 +1082,26 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
         * 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 = TlsGetValue (mono_jit_tls_id);
-       guint64 sp = UCONTEXT_REG_ESP (ctx);
+       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);
-       /*
-        * 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.
-        */
-       g_assert (!test_only);
-       UCONTEXT_REG_EAX (ctx) = (gsize)obj;
-       UCONTEXT_REG_ECX (ctx) = UCONTEXT_REG_EIP (ctx);
-       UCONTEXT_REG_EDX (ctx) = (gsize)handle_signal_exception;
-
-       /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
-       sp -= 16;
-       sp &= ~15;
-       UCONTEXT_REG_ESP (ctx) = sp;
 
-       UCONTEXT_REG_EIP (ctx) = (gsize)signal_exception_trampoline;
+       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)
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoContext mctx;
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        struct sigcontext *ctx = (struct sigcontext *)sigctx;
-       guint64 sp = ctx->SC_ESP;
 
        mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
 
-       /*
-        * 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.
-        */
-       g_assert (!test_only);
-       ctx->SC_EAX = (gsize)obj;
-       ctx->SC_ECX = ctx->SC_EIP;
-       ctx->SC_EDX = (gsize)handle_signal_exception;
-
-       /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
-       sp -= 16;
-       sp &= ~15;
-       ctx->SC_ESP = sp;
-
-       ctx->SC_EIP = (gsize)signal_exception_trampoline;
+       mctx = jit_tls->ex_ctx;
+       mono_setup_async_callback (&mctx, handle_signal_exception, obj);
+       mono_monoctx_to_sigctx (&mctx, sigctx);
 
        return TRUE;
 #else
@@ -1144,7 +1112,7 @@ 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);
 
@@ -1155,7 +1123,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
 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);
 }
@@ -1179,13 +1147,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)
@@ -1193,7 +1161,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);
@@ -1234,7 +1202,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);
@@ -1246,8 +1215,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);
@@ -1270,6 +1238,9 @@ mono_tasklets_arch_restore (void)
        /* 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);
@@ -1290,8 +1261,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;
@@ -1299,3 +1268,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);
+}