2 * exceptions-x86.c: exception support for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #include <mono/arch/x86/x86-codegen.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/exception.h>
21 #include <mono/metadata/gc-internal.h>
22 #include <mono/metadata/mono-debug.h>
23 #include <mono/utils/mono-mmap.h>
28 #include "debug-mini.h"
30 static gpointer signal_exception_trampoline;
33 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
36 static void (*restore_stack) (void *);
38 static MonoW32ExceptionHandler fpe_handler;
39 static MonoW32ExceptionHandler ill_handler;
40 static MonoW32ExceptionHandler segv_handler;
42 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
44 #define W32_SEH_HANDLE_EX(_ex) \
45 if (_ex##_handler) _ex##_handler(0, er, sctx)
48 * mono_win32_get_handle_stackoverflow (void):
50 * Returns a pointer to a method which restores the current context stack
51 * and calls handle_exceptions, when done restores the original stack.
54 mono_win32_get_handle_stackoverflow (void)
56 static guint8 *start = NULL;
62 /* restore_contect (void *sigctx) */
63 start = code = mono_global_codeman_reserve (128);
65 /* load context into ebx */
66 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
68 /* move current stack into edi for later restore */
69 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
71 /* use the new freed stack from sigcontext */
72 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
74 /* get the current domain */
75 x86_call_code (code, mono_domain_get);
77 /* get stack overflow exception from domain object */
78 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
80 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
81 x86_push_imm (code, 0);
82 x86_push_reg (code, X86_EAX);
83 x86_push_reg (code, X86_EBX);
84 x86_call_code (code, mono_arch_handle_exception);
86 /* restore the SEH handler stack */
87 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
95 /* Special hack to workaround the fact that the
96 * when the SEH handler is called the stack is
97 * to small to recover.
99 * Stack walking part of this method is from mono_handle_exception
101 * The idea is simple;
102 * - walk the stack to free some space (64k)
103 * - set esp to new stack location
104 * - call mono_arch_handle_exception with stack overflow exception
105 * - set esp to SEH handlers stack
109 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
113 MonoDomain *domain = mono_domain_get ();
115 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
116 MonoLMF *lmf = jit_tls->lmf;
117 MonoContext initial_ctx;
119 guint32 free_stack = 0;
120 StackFrameInfo frame;
122 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
123 mono_arch_sigctx_to_monoctx (sctx, &ctx);
125 /* get our os page size */
127 page_size = si.dwPageSize;
129 /* Let's walk the stack to recover
130 * the needed stack space (if possible)
132 memset (&rji, 0, sizeof (rji));
135 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
137 /* try to free 64kb from our stack */
141 mono_arch_find_jit_info_ext (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, &frame);
143 g_warning ("Exception inside function without unwind info");
144 g_assert_not_reached ();
147 if (frame.ji != (gpointer)-1) {
148 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
151 /* todo: we should call abort if ji is -1 */
153 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
155 /* convert into sigcontext to be used in mono_arch_handle_exception */
156 mono_arch_monoctx_to_sigctx (&ctx, sctx);
158 /* todo: install new stack-guard page */
160 /* use the new stack and call mono_arch_handle_exception () */
161 restore_stack (sctx);
165 * Unhandled Exception Filter
166 * Top-level per-process exception handler.
168 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
170 EXCEPTION_RECORD* er;
172 struct sigcontext* sctx;
175 res = EXCEPTION_CONTINUE_EXECUTION;
177 er = ep->ExceptionRecord;
178 ctx = ep->ContextRecord;
179 sctx = g_malloc(sizeof(struct sigcontext));
181 /* Copy Win32 context to UNIX style context */
182 sctx->eax = ctx->Eax;
183 sctx->ebx = ctx->Ebx;
184 sctx->ecx = ctx->Ecx;
185 sctx->edx = ctx->Edx;
186 sctx->ebp = ctx->Ebp;
187 sctx->esp = ctx->Esp;
188 sctx->esi = ctx->Esi;
189 sctx->edi = ctx->Edi;
190 sctx->eip = ctx->Eip;
192 switch (er->ExceptionCode) {
193 case EXCEPTION_STACK_OVERFLOW:
194 win32_handle_stack_overflow (ep, sctx);
196 case EXCEPTION_ACCESS_VIOLATION:
197 W32_SEH_HANDLE_EX(segv);
199 case EXCEPTION_ILLEGAL_INSTRUCTION:
200 W32_SEH_HANDLE_EX(ill);
202 case EXCEPTION_INT_DIVIDE_BY_ZERO:
203 case EXCEPTION_INT_OVERFLOW:
204 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
205 case EXCEPTION_FLT_OVERFLOW:
206 case EXCEPTION_FLT_UNDERFLOW:
207 case EXCEPTION_FLT_INEXACT_RESULT:
208 W32_SEH_HANDLE_EX(fpe);
214 /* Copy context back */
215 ctx->Eax = sctx->eax;
216 ctx->Ebx = sctx->ebx;
217 ctx->Ecx = sctx->ecx;
218 ctx->Edx = sctx->edx;
219 ctx->Ebp = sctx->ebp;
220 ctx->Esp = sctx->esp;
221 ctx->Esi = sctx->esi;
222 ctx->Edi = sctx->edi;
223 ctx->Eip = sctx->eip;
230 void win32_seh_init()
232 /* install restore stack helper */
234 restore_stack = mono_win32_get_handle_stackoverflow ();
236 old_handler = SetUnhandledExceptionFilter(seh_handler);
239 void win32_seh_cleanup()
241 if (old_handler) SetUnhandledExceptionFilter(old_handler);
244 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
248 fpe_handler = handler;
251 ill_handler = handler;
254 segv_handler = handler;
261 #endif /* TARGET_WIN32 */
264 * mono_arch_get_restore_context:
266 * Returns a pointer to a method which restores a previously saved sigcontext.
269 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
271 guint8 *start = NULL;
273 MonoJumpInfo *ji = NULL;
274 GSList *unwind_ops = NULL;
276 /* restore_contect (MonoContext *ctx) */
278 start = code = mono_global_codeman_reserve (128);
281 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
283 /* get return address, stored in ECX */
284 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
286 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
288 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
290 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
292 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
293 /* save the return addr to the restored stack */
294 x86_push_reg (code, X86_ECX);
296 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
298 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ecx), 4);
300 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, edx), 4);
302 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eax), 4);
304 /* jump to the saved IP */
308 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
314 * mono_arch_get_call_filter:
316 * Returns a pointer to a method which calls an exception filter. We
317 * also use this function to call finally handlers (we pass NULL as
318 * @exc object in this case).
321 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
325 MonoJumpInfo *ji = NULL;
326 GSList *unwind_ops = NULL;
328 /* call_filter (MonoContext *ctx, unsigned long eip) */
329 start = code = mono_global_codeman_reserve (64);
331 x86_push_reg (code, X86_EBP);
332 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
333 x86_push_reg (code, X86_EBX);
334 x86_push_reg (code, X86_EDI);
335 x86_push_reg (code, X86_ESI);
338 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
340 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
342 x86_push_reg (code, X86_EBP);
345 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
346 /* restore registers used by global register allocation (EBX & ESI) */
347 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
348 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
349 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
351 /* align stack and save ESP */
352 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
353 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
354 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
355 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
356 x86_push_reg (code, X86_EDX);
358 /* call the handler */
359 x86_call_reg (code, X86_ECX);
362 x86_pop_reg (code, X86_ESP);
365 x86_pop_reg (code, X86_EBP);
367 /* restore saved regs */
368 x86_pop_reg (code, X86_ESI);
369 x86_pop_reg (code, X86_EDI);
370 x86_pop_reg (code, X86_EBX);
375 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
377 g_assert ((code - start) < 64);
382 * mono_x86_throw_exception:
384 * C function called from the throw trampolines.
387 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
388 mgreg_t eip, gboolean rethrow)
390 static void (*restore_context) (MonoContext *);
393 if (!restore_context)
394 restore_context = mono_get_restore_context ();
396 ctx.esp = regs [X86_ESP];
398 ctx.ebp = regs [X86_EBP];
399 ctx.edi = regs [X86_EDI];
400 ctx.esi = regs [X86_ESI];
401 ctx.ebx = regs [X86_EBX];
402 ctx.edx = regs [X86_EDX];
403 ctx.ecx = regs [X86_ECX];
404 ctx.eax = regs [X86_EAX];
407 /* The OSX ABI specifies 16 byte alignment at call sites */
408 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
411 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
412 MonoException *mono_ex = (MonoException*)exc;
414 mono_ex->stack_trace = NULL;
417 if (mono_debug_using_mono_debugger ()) {
418 guint8 buf [16], *code;
420 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
423 if (buf [3] == 0xe8) {
424 MonoContext ctx_cp = ctx;
425 ctx_cp.eip = eip - 5;
427 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
428 restore_context (&ctx_cp);
429 g_assert_not_reached ();
434 /* adjust eip so that it point into the call instruction */
437 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
439 restore_context (&ctx);
441 g_assert_not_reached ();
445 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
446 mgreg_t eip, gint32 pc_offset)
448 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
451 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
455 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
459 * get_throw_exception:
461 * Generate a call to mono_x86_throw_exception/
462 * mono_x86_throw_corlib_exception.
463 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
464 * which doesn't push the arguments.
467 get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, MonoTrampInfo **info, gboolean aot)
469 guint8 *start, *code;
470 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
471 MonoJumpInfo *ji = NULL;
472 GSList *unwind_ops = NULL;
474 start = code = mono_global_codeman_reserve (128);
479 * On apple, the stack is misaligned by the pushing of the return address.
482 /* On OSX, we don't generate alignment code to save space */
485 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
488 * The stack looks like this:
489 * <pc offset> (only if corlib is TRUE)
490 * <exception object>/<type token>
491 * <return addr> <- esp (unaligned on apple)
494 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
495 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
498 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
499 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
504 arg_offsets [3] = 12;
508 for (i = 0; i < X86_NREG; ++i)
510 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
511 /* Calculate the offset between the current sp and the sp of the caller */
513 /* LLVM doesn't push the arguments */
514 stack_offset = stack_size + 4;
518 stack_offset = stack_size + 4 + 8;
520 /* We don't generate stack alignment code on osx to save space */
524 stack_offset = stack_size + 4 + 4;
526 /* Pop the alignment added by OP_THROW too */
527 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
532 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
533 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
535 /* Set arg1 == regs */
536 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
537 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
538 /* Set arg2 == exc */
539 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
540 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
541 /* Set arg3 == eip */
542 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
543 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
545 /* Set arg4 == offset */
546 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
547 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
549 /* Set arg4 == rethrow */
550 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
554 // This can be called from runtime code, which can't guarantee that
555 // ebx contains the got address.
556 // So emit the got address loading code too
557 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
558 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");
559 x86_call_reg (code, X86_EAX);
561 x86_call_code (code, corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception);
563 x86_breakpoint (code);
565 g_assert ((code - start) < 128);
567 mono_save_trampoline_xdebug_info (corlib ? "llvm_throw_corlib_exception_trampoline" : "llvm_throw_exception_trampoline", start, code - start, unwind_ops);
570 *info = mono_tramp_info_create (g_strdup_printf (corlib ? "throw_corlib_exception" : (rethrow ? "rethrow_exception" : "throw_exception")), start, code - start, ji, unwind_ops);
576 * mono_arch_get_throw_exception:
578 * Returns a function pointer which can be used to raise
579 * exceptions. The returned function has the following
580 * signature: void (*func) (MonoException *exc);
581 * For example to raise an arithmetic exception you can use:
583 * x86_push_imm (code, mono_get_exception_arithmetic ());
584 * x86_call_code (code, arch_get_throw_exception ());
588 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
590 return get_throw_exception ("throw_exception_trampoline", FALSE, FALSE, FALSE, info, aot);
594 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
596 return get_throw_exception ("rethow_exception_trampoline", TRUE, FALSE, FALSE, info, aot);
600 * mono_arch_get_throw_corlib_exception:
602 * Returns a function pointer which can be used to raise
603 * corlib exceptions. The returned function has the following
604 * signature: void (*func) (guint32 ex_token, guint32 offset);
605 * Here, offset is the offset which needs to be substracted from the caller IP
606 * to get the IP of the throw. Passing the offset has the advantage that it
607 * needs no relocations in the caller.
610 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
612 return get_throw_exception ("throw_corlib_exception_trampoline", FALSE, FALSE, TRUE, info, aot);
616 mono_arch_exceptions_init (void)
621 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
625 /* LLVM needs different throw trampolines */
626 tramp = get_throw_exception ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, NULL, FALSE);
628 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_exception", NULL, TRUE);
630 tramp = get_throw_exception ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, NULL, FALSE);
632 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
634 signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
638 * mono_arch_find_jit_info_ext:
640 * See exceptions-amd64.c for docs.
643 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
644 MonoJitInfo *ji, MonoContext *ctx,
645 MonoContext *new_ctx, MonoLMF **lmf,
646 StackFrameInfo *frame)
648 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
650 memset (frame, 0, sizeof (StackFrameInfo));
652 frame->managed = FALSE;
657 gssize regs [MONO_MAX_IREGS + 1];
659 guint32 unwind_info_len;
662 frame->type = FRAME_TYPE_MANAGED;
664 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
665 frame->managed = TRUE;
668 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
670 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
672 regs [X86_EAX] = new_ctx->eax;
673 regs [X86_EBX] = new_ctx->ebx;
674 regs [X86_ECX] = new_ctx->ecx;
675 regs [X86_EDX] = new_ctx->edx;
676 regs [X86_ESP] = new_ctx->esp;
677 regs [X86_EBP] = new_ctx->ebp;
678 regs [X86_ESI] = new_ctx->esi;
679 regs [X86_EDI] = new_ctx->edi;
680 regs [X86_NREG] = new_ctx->eip;
682 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
683 (guint8*)ji->code_start + ji->code_size,
684 ip, regs, MONO_MAX_IREGS + 1, &cfa);
686 new_ctx->eax = regs [X86_EAX];
687 new_ctx->ebx = regs [X86_EBX];
688 new_ctx->ecx = regs [X86_ECX];
689 new_ctx->edx = regs [X86_EDX];
690 new_ctx->esp = regs [X86_ESP];
691 new_ctx->ebp = regs [X86_EBP];
692 new_ctx->esi = regs [X86_ESI];
693 new_ctx->edi = regs [X86_EDI];
694 new_ctx->eip = regs [X86_NREG];
696 /* The CFA becomes the new SP value */
697 new_ctx->esp = (gssize)cfa;
702 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
703 /* remove any unused lmf */
704 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
707 /* Pop arguments off the stack */
709 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
714 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
716 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
717 new_ctx->esp += stack_to_pop;
724 if (((guint64)(*lmf)->previous_lmf) & 2) {
726 * This LMF entry is created by the soft debug code to mark transitions to
727 * managed code done during invokes.
729 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
731 g_assert (ext->debugger_invoke);
733 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
735 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
737 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
742 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
744 if (!((guint32)((*lmf)->previous_lmf) & 1))
747 /* Trampoline lmf frame */
748 frame->method = (*lmf)->method;
751 new_ctx->esi = (*lmf)->esi;
752 new_ctx->edi = (*lmf)->edi;
753 new_ctx->ebx = (*lmf)->ebx;
754 new_ctx->ebp = (*lmf)->ebp;
755 new_ctx->eip = (*lmf)->eip;
758 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
760 /* Check if we are in a trampoline LMF frame */
761 if ((guint32)((*lmf)->previous_lmf) & 1) {
762 /* lmf->esp is set by the trampoline code */
763 new_ctx->esp = (*lmf)->esp;
765 /* Pop arguments off the stack */
766 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
767 /* FIXME: Handle the IMT/vtable case too */
769 if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
770 MonoMethod *method = (*lmf)->method;
771 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
773 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
774 new_ctx->esp += stack_to_pop;
779 /* the lmf is always stored on the stack, so the following
780 * expression points to a stack location which can be used as ESP */
781 new_ctx->esp = (unsigned long)&((*lmf)->eip);
783 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
804 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
806 #ifdef MONO_ARCH_USE_SIGACTION
807 ucontext_t *ctx = (ucontext_t*)sigctx;
809 mctx->eax = UCONTEXT_REG_EAX (ctx);
810 mctx->ebx = UCONTEXT_REG_EBX (ctx);
811 mctx->ecx = UCONTEXT_REG_ECX (ctx);
812 mctx->edx = UCONTEXT_REG_EDX (ctx);
813 mctx->ebp = UCONTEXT_REG_EBP (ctx);
814 mctx->esp = UCONTEXT_REG_ESP (ctx);
815 mctx->esi = UCONTEXT_REG_ESI (ctx);
816 mctx->edi = UCONTEXT_REG_EDI (ctx);
817 mctx->eip = UCONTEXT_REG_EIP (ctx);
819 struct sigcontext *ctx = (struct sigcontext *)sigctx;
821 mctx->eax = ctx->SC_EAX;
822 mctx->ebx = ctx->SC_EBX;
823 mctx->ecx = ctx->SC_ECX;
824 mctx->edx = ctx->SC_EDX;
825 mctx->ebp = ctx->SC_EBP;
826 mctx->esp = ctx->SC_ESP;
827 mctx->esi = ctx->SC_ESI;
828 mctx->edi = ctx->SC_EDI;
829 mctx->eip = ctx->SC_EIP;
834 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
836 #ifdef MONO_ARCH_USE_SIGACTION
837 ucontext_t *ctx = (ucontext_t*)sigctx;
839 UCONTEXT_REG_EAX (ctx) = mctx->eax;
840 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
841 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
842 UCONTEXT_REG_EDX (ctx) = mctx->edx;
843 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
844 UCONTEXT_REG_ESP (ctx) = mctx->esp;
845 UCONTEXT_REG_ESI (ctx) = mctx->esi;
846 UCONTEXT_REG_EDI (ctx) = mctx->edi;
847 UCONTEXT_REG_EIP (ctx) = mctx->eip;
849 struct sigcontext *ctx = (struct sigcontext *)sigctx;
851 ctx->SC_EAX = mctx->eax;
852 ctx->SC_EBX = mctx->ebx;
853 ctx->SC_ECX = mctx->ecx;
854 ctx->SC_EDX = mctx->edx;
855 ctx->SC_EBP = mctx->ebp;
856 ctx->SC_ESP = mctx->esp;
857 ctx->SC_ESI = mctx->esi;
858 ctx->SC_EDI = mctx->edi;
859 ctx->SC_EIP = mctx->eip;
864 mono_arch_ip_from_context (void *sigctx)
866 #ifdef MONO_ARCH_USE_SIGACTION
867 ucontext_t *ctx = (ucontext_t*)sigctx;
868 return (gpointer)UCONTEXT_REG_EIP (ctx);
870 struct sigcontext *ctx = sigctx;
871 return (gpointer)ctx->SC_EIP;
878 * Called by resuming from a signal handler.
881 handle_signal_exception (gpointer obj)
883 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
885 static void (*restore_context) (MonoContext *);
887 if (!restore_context)
888 restore_context = mono_get_restore_context ();
890 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
892 if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
895 mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), FALSE);
897 restore_context (&ctx);
901 * mono_x86_get_signal_exception_trampoline:
903 * This x86 specific trampoline is used to call handle_signal_exception.
906 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
908 guint8 *start, *code;
909 MonoJumpInfo *ji = NULL;
910 GSList *unwind_ops = NULL;
913 start = code = mono_global_codeman_reserve (128);
916 x86_push_reg (code, X86_ECX);
918 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
919 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
921 /* Fix the alignment to be what apple expects */
924 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
925 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
928 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
929 /* Branch to target */
930 x86_call_reg (code, X86_EDX);
932 g_assert ((code - start) < 128);
934 mono_save_trampoline_xdebug_info ("x86_signal_exception_trampoline", start, code - start, unwind_ops);
937 *info = mono_tramp_info_create (g_strdup ("x86_signal_exception_trampoline"), start, code - start, ji, unwind_ops);
943 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
945 #if defined(MONO_ARCH_USE_SIGACTION)
947 * Handling the exception in the signal handler is problematic, since the original
948 * signal is disabled, and we could run arbitrary code though the debugger. So
949 * resume into the normal stack and do most work there if possible.
951 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
952 guint64 sp = UCONTEXT_REG_ESP (sigctx);
954 /* Pass the ctx parameter in TLS */
955 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
957 * Can't pass the obj on the stack, since we are executing on the
958 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
959 * So put it into a register, and branch to a trampoline which
962 g_assert (!test_only);
963 UCONTEXT_REG_EAX (sigctx) = (gsize)obj;
964 UCONTEXT_REG_ECX (sigctx) = UCONTEXT_REG_EIP (sigctx);
965 UCONTEXT_REG_EDX (sigctx) = (gsize)handle_signal_exception;
967 /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
970 UCONTEXT_REG_ESP (sigctx) = sp;
972 UCONTEXT_REG_EIP (sigctx) = (gsize)signal_exception_trampoline;
978 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
980 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
983 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
985 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
992 restore_soft_guard_pages (void)
994 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
995 if (jit_tls->stack_ovf_guard_base)
996 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1000 * this function modifies mctx so that when it is restored, it
1001 * won't execcute starting at mctx.eip, but in a function that
1002 * will restore the protection on the soft-guard pages and return back to
1003 * continue at mctx.eip.
1006 prepare_for_guard_pages (MonoContext *mctx)
1009 sp = (gpointer)(mctx->esp);
1011 /* the resturn addr */
1012 sp [0] = (gpointer)(mctx->eip);
1013 mctx->eip = (unsigned long)restore_soft_guard_pages;
1014 mctx->esp = (unsigned long)sp;
1018 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
1020 void (*restore_context) (MonoContext *);
1023 restore_context = mono_get_restore_context ();
1024 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1026 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
1028 prepare_for_guard_pages (&mctx);
1029 restore_context (&mctx);
1032 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
1034 prepare_for_guard_pages (&mctx);
1035 restore_context (&mctx);
1039 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
1041 #ifdef MONO_ARCH_USE_SIGACTION
1042 MonoException *exc = NULL;
1043 ucontext_t *ctx = (ucontext_t*)sigctx;
1044 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1048 /* if we didn't find a managed method for the ip address and it matches the fault
1049 * address, we assume we followed a broken pointer during an indirect call, so
1050 * we try the lookup again with the return address pushed on the stack
1052 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1053 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1054 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1056 UCONTEXT_REG_EIP (ctx) = sp [0];
1059 exc = mono_domain_get ()->stack_overflow_ex;
1061 mono_handle_native_sigsegv (SIGSEGV, sigctx);
1062 /* setup a call frame on the real stack so that control is returned there
1063 * and exception handling can continue.
1064 * If this was a stack overflow the caller already ensured the stack pages
1065 * needed have been unprotected.
1066 * The frame looks like:
1073 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
1076 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1077 sp = (gpointer)((char*)sp - frame_size);
1078 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1081 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1084 sp [2] = (gpointer)stack_ovf;
1085 /* may need to adjust pointers in the new struct copy, depending on the OS */
1086 memcpy (sp + 4, ctx, sizeof (ucontext_t));
1087 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1088 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1089 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1093 #if MONO_SUPPORT_TASKLETS
1094 MonoContinuationRestore
1095 mono_tasklets_arch_restore (void)
1097 static guint8* saved = NULL;
1098 guint8 *code, *start;
1101 return (MonoContinuationRestore)saved;
1102 code = start = mono_global_codeman_reserve (48);
1103 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1104 /* put cont in edx */
1105 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1106 /* setup the copy of the stack */
1107 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1108 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1110 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1111 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1112 x86_prefix (code, X86_REP_PREFIX);
1115 /* now restore the registers from the LMF */
1116 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1117 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1118 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1119 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1120 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1122 /* restore the lmf chain */
1123 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1124 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1126 /* state in eax, so it's setup as the return value */
1127 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1128 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1129 g_assert ((code - start) <= 48);
1131 return (MonoContinuationRestore)saved;