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"
31 static void (*restore_stack) (void *);
33 static MonoW32ExceptionHandler fpe_handler;
34 static MonoW32ExceptionHandler ill_handler;
35 static MonoW32ExceptionHandler segv_handler;
37 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
39 #define W32_SEH_HANDLE_EX(_ex) \
40 if (_ex##_handler) _ex##_handler(0, er, sctx)
43 * mono_win32_get_handle_stackoverflow (void):
45 * Returns a pointer to a method which restores the current context stack
46 * and calls handle_exceptions, when done restores the original stack.
49 mono_win32_get_handle_stackoverflow (void)
51 static guint8 *start = NULL;
57 /* restore_contect (void *sigctx) */
58 start = code = mono_global_codeman_reserve (128);
60 /* load context into ebx */
61 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
63 /* move current stack into edi for later restore */
64 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
66 /* use the new freed stack from sigcontext */
67 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
69 /* get the current domain */
70 x86_call_code (code, mono_domain_get);
72 /* get stack overflow exception from domain object */
73 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
75 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
76 x86_push_imm (code, 0);
77 x86_push_reg (code, X86_EAX);
78 x86_push_reg (code, X86_EBX);
79 x86_call_code (code, mono_arch_handle_exception);
81 /* restore the SEH handler stack */
82 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
90 /* Special hack to workaround the fact that the
91 * when the SEH handler is called the stack is
92 * to small to recover.
94 * Stack walking part of this method is from mono_handle_exception
97 * - walk the stack to free some space (64k)
98 * - set esp to new stack location
99 * - call mono_arch_handle_exception with stack overflow exception
100 * - set esp to SEH handlers stack
104 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
108 MonoDomain *domain = mono_domain_get ();
110 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
111 MonoLMF *lmf = jit_tls->lmf;
112 MonoContext initial_ctx;
114 guint32 free_stack = 0;
115 StackFrameInfo frame;
117 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
118 mono_arch_sigctx_to_monoctx (sctx, &ctx);
120 /* get our os page size */
122 page_size = si.dwPageSize;
124 /* Let's walk the stack to recover
125 * the needed stack space (if possible)
127 memset (&rji, 0, sizeof (rji));
130 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
132 /* try to free 64kb from our stack */
136 mono_arch_find_jit_info_ext (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, &frame);
138 g_warning ("Exception inside function without unwind info");
139 g_assert_not_reached ();
142 if (frame.ji != (gpointer)-1) {
143 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
146 /* todo: we should call abort if ji is -1 */
148 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
150 /* convert into sigcontext to be used in mono_arch_handle_exception */
151 mono_arch_monoctx_to_sigctx (&ctx, sctx);
153 /* todo: install new stack-guard page */
155 /* use the new stack and call mono_arch_handle_exception () */
156 restore_stack (sctx);
160 * Unhandled Exception Filter
161 * Top-level per-process exception handler.
163 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
165 EXCEPTION_RECORD* er;
167 struct sigcontext* sctx;
170 res = EXCEPTION_CONTINUE_EXECUTION;
172 er = ep->ExceptionRecord;
173 ctx = ep->ContextRecord;
174 sctx = g_malloc(sizeof(struct sigcontext));
176 /* Copy Win32 context to UNIX style context */
177 sctx->eax = ctx->Eax;
178 sctx->ebx = ctx->Ebx;
179 sctx->ecx = ctx->Ecx;
180 sctx->edx = ctx->Edx;
181 sctx->ebp = ctx->Ebp;
182 sctx->esp = ctx->Esp;
183 sctx->esi = ctx->Esi;
184 sctx->edi = ctx->Edi;
185 sctx->eip = ctx->Eip;
187 switch (er->ExceptionCode) {
188 case EXCEPTION_STACK_OVERFLOW:
189 win32_handle_stack_overflow (ep, sctx);
191 case EXCEPTION_ACCESS_VIOLATION:
192 W32_SEH_HANDLE_EX(segv);
194 case EXCEPTION_ILLEGAL_INSTRUCTION:
195 W32_SEH_HANDLE_EX(ill);
197 case EXCEPTION_INT_DIVIDE_BY_ZERO:
198 case EXCEPTION_INT_OVERFLOW:
199 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
200 case EXCEPTION_FLT_OVERFLOW:
201 case EXCEPTION_FLT_UNDERFLOW:
202 case EXCEPTION_FLT_INEXACT_RESULT:
203 W32_SEH_HANDLE_EX(fpe);
209 /* Copy context back */
210 ctx->Eax = sctx->eax;
211 ctx->Ebx = sctx->ebx;
212 ctx->Ecx = sctx->ecx;
213 ctx->Edx = sctx->edx;
214 ctx->Ebp = sctx->ebp;
215 ctx->Esp = sctx->esp;
216 ctx->Esi = sctx->esi;
217 ctx->Edi = sctx->edi;
218 ctx->Eip = sctx->eip;
225 void win32_seh_init()
227 /* install restore stack helper */
229 restore_stack = mono_win32_get_handle_stackoverflow ();
231 old_handler = SetUnhandledExceptionFilter(seh_handler);
234 void win32_seh_cleanup()
236 if (old_handler) SetUnhandledExceptionFilter(old_handler);
239 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
243 fpe_handler = handler;
246 ill_handler = handler;
249 segv_handler = handler;
256 #endif /* TARGET_WIN32 */
259 * mono_arch_get_restore_context:
261 * Returns a pointer to a method which restores a previously saved sigcontext.
264 mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
266 guint8 *start = NULL;
269 /* restore_contect (MonoContext *ctx) */
273 start = code = mono_global_codeman_reserve (128);
276 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
278 /* get return address, stored in ECX */
279 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
281 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
283 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
285 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
287 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
288 /* save the return addr to the restored stack */
289 x86_push_reg (code, X86_ECX);
291 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
293 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ecx), 4);
295 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, edx), 4);
297 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eax), 4);
299 /* jump to the saved IP */
302 *code_size = code - start;
308 * mono_arch_get_call_filter:
310 * Returns a pointer to a method which calls an exception filter. We
311 * also use this function to call finally handlers (we pass NULL as
312 * @exc object in this case).
315 mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
322 /* call_filter (MonoContext *ctx, unsigned long eip) */
323 start = code = mono_global_codeman_reserve (64);
325 x86_push_reg (code, X86_EBP);
326 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
327 x86_push_reg (code, X86_EBX);
328 x86_push_reg (code, X86_EDI);
329 x86_push_reg (code, X86_ESI);
332 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
334 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
336 x86_push_reg (code, X86_EBP);
339 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
340 /* restore registers used by global register allocation (EBX & ESI) */
341 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
342 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
343 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
345 /* align stack and save ESP */
346 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
347 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
348 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
349 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
350 x86_push_reg (code, X86_EDX);
352 /* call the handler */
353 x86_call_reg (code, X86_ECX);
356 x86_pop_reg (code, X86_ESP);
359 x86_pop_reg (code, X86_EBP);
361 /* restore saved regs */
362 x86_pop_reg (code, X86_ESI);
363 x86_pop_reg (code, X86_EDI);
364 x86_pop_reg (code, X86_EBX);
368 *code_size = code - start;
370 g_assert ((code - start) < 64);
375 * mono_x86_throw_exception:
377 * C function called from the throw trampolines.
380 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
381 mgreg_t eip, gboolean rethrow)
383 static void (*restore_context) (MonoContext *);
386 if (!restore_context)
387 restore_context = mono_get_restore_context ();
389 ctx.esp = regs [X86_ESP];
391 ctx.ebp = regs [X86_EBP];
392 ctx.edi = regs [X86_EDI];
393 ctx.esi = regs [X86_ESI];
394 ctx.ebx = regs [X86_EBX];
395 ctx.edx = regs [X86_EDX];
396 ctx.ecx = regs [X86_ECX];
397 ctx.eax = regs [X86_EAX];
400 /* The OSX ABI specifies 16 byte alignment at call sites */
401 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
404 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
405 MonoException *mono_ex = (MonoException*)exc;
407 mono_ex->stack_trace = NULL;
410 if (mono_debug_using_mono_debugger ()) {
411 guint8 buf [16], *code;
413 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
416 if (buf [3] == 0xe8) {
417 MonoContext ctx_cp = ctx;
418 ctx_cp.eip = eip - 5;
420 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
421 restore_context (&ctx_cp);
422 g_assert_not_reached ();
427 /* adjust eip so that it point into the call instruction */
430 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
432 restore_context (&ctx);
434 g_assert_not_reached ();
438 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
439 mgreg_t eip, gint32 pc_offset)
441 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
444 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
448 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
452 * get_throw_exception:
454 * Generate a call to mono_x86_throw_exception/
455 * mono_x86_throw_corlib_exception.
456 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
457 * which doesn't push the arguments.
460 get_throw_exception (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
462 guint8 *start, *code;
463 GSList *unwind_ops = NULL;
464 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
469 start = code = mono_global_codeman_reserve (128);
474 * On apple, the stack is misaligned by the pushing of the return address.
477 /* On OSX, we don't generate alignment code to save space */
480 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
483 * The stack looks like this:
484 * <pc offset> (only if corlib is TRUE)
485 * <exception object>/<type token>
486 * <return addr> <- esp (unaligned on apple)
489 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
490 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
493 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
494 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
499 arg_offsets [3] = 12;
503 for (i = 0; i < X86_NREG; ++i)
505 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
506 /* Calculate the offset between the current sp and the sp of the caller */
508 /* LLVM doesn't push the arguments */
509 stack_offset = stack_size + 4;
513 stack_offset = stack_size + 4 + 8;
515 /* We don't generate stack alignment code on osx to save space */
519 stack_offset = stack_size + 4 + 4;
521 /* Pop the alignment added by OP_THROW too */
522 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
527 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
528 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
530 /* Set arg1 == regs */
531 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
532 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
533 /* Set arg2 == exc */
534 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
535 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
536 /* Set arg3 == eip */
537 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
538 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
540 /* Set arg4 == offset */
541 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
542 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
544 /* Set arg4 == rethrow */
545 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
549 // This can be called from runtime code, which can't guarantee that
550 // ebx contains the got address.
551 // So emit the got address loading code too
552 code = mono_arch_emit_load_got_addr (start, code, NULL, ji);
553 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");
554 x86_call_reg (code, X86_EAX);
556 x86_call_code (code, corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception);
558 x86_breakpoint (code);
560 g_assert ((code - start) < 128);
563 *code_size = code - start;
565 mono_save_trampoline_xdebug_info (corlib ? "llvm_throw_corlib_exception_trampoline" : "llvm_throw_exception_trampoline", start, code - start, unwind_ops);
571 * mono_arch_get_throw_exception:
573 * Returns a function pointer which can be used to raise
574 * exceptions. The returned function has the following
575 * signature: void (*func) (MonoException *exc);
576 * For example to raise an arithmetic exception you can use:
578 * x86_push_imm (code, mono_get_exception_arithmetic ());
579 * x86_call_code (code, arch_get_throw_exception ());
583 mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
585 return get_throw_exception ("throw_exception_trampoline", FALSE, FALSE, FALSE, code_size, ji, aot);
589 mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
591 return get_throw_exception ("rethow_exception_trampoline", TRUE, FALSE, FALSE, code_size, ji, aot);
595 * mono_arch_get_throw_corlib_exception:
597 * Returns a function pointer which can be used to raise
598 * corlib exceptions. The returned function has the following
599 * signature: void (*func) (guint32 ex_token, guint32 offset);
600 * Here, offset is the offset which needs to be substracted from the caller IP
601 * to get the IP of the throw. Passing the offset has the advantage that it
602 * needs no relocations in the caller.
605 mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
607 return get_throw_exception ("throw_corlib_exception_trampoline", FALSE, FALSE, TRUE, code_size, ji, aot);
611 mono_arch_exceptions_init (void)
618 /* LLVM needs different throw trampolines */
619 tramp = get_throw_exception ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, NULL, NULL, FALSE);
621 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_exception", NULL, TRUE);
623 tramp = get_throw_exception ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, NULL, NULL, FALSE);
625 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
629 * mono_arch_find_jit_info_ext:
631 * See exceptions-amd64.c for docs.
634 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
635 MonoJitInfo *ji, MonoContext *ctx,
636 MonoContext *new_ctx, MonoLMF **lmf,
637 StackFrameInfo *frame)
639 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
641 memset (frame, 0, sizeof (StackFrameInfo));
643 frame->managed = FALSE;
648 gssize regs [MONO_MAX_IREGS + 1];
650 guint32 unwind_info_len;
653 frame->type = FRAME_TYPE_MANAGED;
655 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
656 frame->managed = TRUE;
659 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
661 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
663 regs [X86_EAX] = new_ctx->eax;
664 regs [X86_EBX] = new_ctx->ebx;
665 regs [X86_ECX] = new_ctx->ecx;
666 regs [X86_EDX] = new_ctx->edx;
667 regs [X86_ESP] = new_ctx->esp;
668 regs [X86_EBP] = new_ctx->ebp;
669 regs [X86_ESI] = new_ctx->esi;
670 regs [X86_EDI] = new_ctx->edi;
671 regs [X86_NREG] = new_ctx->eip;
673 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
674 (guint8*)ji->code_start + ji->code_size,
675 ip, regs, MONO_MAX_IREGS + 1, &cfa);
677 new_ctx->eax = regs [X86_EAX];
678 new_ctx->ebx = regs [X86_EBX];
679 new_ctx->ecx = regs [X86_ECX];
680 new_ctx->edx = regs [X86_EDX];
681 new_ctx->esp = regs [X86_ESP];
682 new_ctx->ebp = regs [X86_EBP];
683 new_ctx->esi = regs [X86_ESI];
684 new_ctx->edi = regs [X86_EDI];
685 new_ctx->eip = regs [X86_NREG];
687 /* The CFA becomes the new SP value */
688 new_ctx->esp = (gssize)cfa;
693 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
694 /* remove any unused lmf */
695 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
698 /* Pop arguments off the stack */
700 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
705 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
707 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
708 new_ctx->esp += stack_to_pop;
715 if (((guint64)(*lmf)->previous_lmf) & 2) {
717 * This LMF entry is created by the soft debug code to mark transitions to
718 * managed code done during invokes.
720 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
722 g_assert (ext->debugger_invoke);
724 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
726 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
728 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
733 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
735 if (!((guint32)((*lmf)->previous_lmf) & 1))
738 /* Trampoline lmf frame */
739 frame->method = (*lmf)->method;
742 new_ctx->esi = (*lmf)->esi;
743 new_ctx->edi = (*lmf)->edi;
744 new_ctx->ebx = (*lmf)->ebx;
745 new_ctx->ebp = (*lmf)->ebp;
746 new_ctx->eip = (*lmf)->eip;
749 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
751 /* Check if we are in a trampoline LMF frame */
752 if ((guint32)((*lmf)->previous_lmf) & 1) {
753 /* lmf->esp is set by the trampoline code */
754 new_ctx->esp = (*lmf)->esp;
756 /* Pop arguments off the stack */
757 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
758 /* FIXME: Handle the IMT/vtable case too */
760 if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
761 MonoMethod *method = (*lmf)->method;
762 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
764 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
765 new_ctx->esp += stack_to_pop;
770 /* the lmf is always stored on the stack, so the following
771 * expression points to a stack location which can be used as ESP */
772 new_ctx->esp = (unsigned long)&((*lmf)->eip);
774 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
795 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
797 #ifdef MONO_ARCH_USE_SIGACTION
798 ucontext_t *ctx = (ucontext_t*)sigctx;
800 mctx->eax = UCONTEXT_REG_EAX (ctx);
801 mctx->ebx = UCONTEXT_REG_EBX (ctx);
802 mctx->ecx = UCONTEXT_REG_ECX (ctx);
803 mctx->edx = UCONTEXT_REG_EDX (ctx);
804 mctx->ebp = UCONTEXT_REG_EBP (ctx);
805 mctx->esp = UCONTEXT_REG_ESP (ctx);
806 mctx->esi = UCONTEXT_REG_ESI (ctx);
807 mctx->edi = UCONTEXT_REG_EDI (ctx);
808 mctx->eip = UCONTEXT_REG_EIP (ctx);
810 struct sigcontext *ctx = (struct sigcontext *)sigctx;
812 mctx->eax = ctx->SC_EAX;
813 mctx->ebx = ctx->SC_EBX;
814 mctx->ecx = ctx->SC_ECX;
815 mctx->edx = ctx->SC_EDX;
816 mctx->ebp = ctx->SC_EBP;
817 mctx->esp = ctx->SC_ESP;
818 mctx->esi = ctx->SC_ESI;
819 mctx->edi = ctx->SC_EDI;
820 mctx->eip = ctx->SC_EIP;
825 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
827 #ifdef MONO_ARCH_USE_SIGACTION
828 ucontext_t *ctx = (ucontext_t*)sigctx;
830 UCONTEXT_REG_EAX (ctx) = mctx->eax;
831 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
832 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
833 UCONTEXT_REG_EDX (ctx) = mctx->edx;
834 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
835 UCONTEXT_REG_ESP (ctx) = mctx->esp;
836 UCONTEXT_REG_ESI (ctx) = mctx->esi;
837 UCONTEXT_REG_EDI (ctx) = mctx->edi;
838 UCONTEXT_REG_EIP (ctx) = mctx->eip;
840 struct sigcontext *ctx = (struct sigcontext *)sigctx;
842 ctx->SC_EAX = mctx->eax;
843 ctx->SC_EBX = mctx->ebx;
844 ctx->SC_ECX = mctx->ecx;
845 ctx->SC_EDX = mctx->edx;
846 ctx->SC_EBP = mctx->ebp;
847 ctx->SC_ESP = mctx->esp;
848 ctx->SC_ESI = mctx->esi;
849 ctx->SC_EDI = mctx->edi;
850 ctx->SC_EIP = mctx->eip;
855 mono_arch_ip_from_context (void *sigctx)
857 #ifdef MONO_ARCH_USE_SIGACTION
858 ucontext_t *ctx = (ucontext_t*)sigctx;
859 return (gpointer)UCONTEXT_REG_EIP (ctx);
861 struct sigcontext *ctx = sigctx;
862 return (gpointer)ctx->SC_EIP;
867 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
871 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
873 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
876 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
878 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
884 restore_soft_guard_pages (void)
886 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
887 if (jit_tls->stack_ovf_guard_base)
888 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
892 * this function modifies mctx so that when it is restored, it
893 * won't execcute starting at mctx.eip, but in a function that
894 * will restore the protection on the soft-guard pages and return back to
895 * continue at mctx.eip.
898 prepare_for_guard_pages (MonoContext *mctx)
901 sp = (gpointer)(mctx->esp);
903 /* the resturn addr */
904 sp [0] = (gpointer)(mctx->eip);
905 mctx->eip = (unsigned long)restore_soft_guard_pages;
906 mctx->esp = (unsigned long)sp;
910 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
912 void (*restore_context) (MonoContext *);
915 restore_context = mono_get_restore_context ();
916 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
918 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
920 prepare_for_guard_pages (&mctx);
921 restore_context (&mctx);
924 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
926 prepare_for_guard_pages (&mctx);
927 restore_context (&mctx);
931 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
933 #ifdef MONO_ARCH_USE_SIGACTION
934 MonoException *exc = NULL;
935 ucontext_t *ctx = (ucontext_t*)sigctx;
936 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
940 /* if we didn't find a managed method for the ip address and it matches the fault
941 * address, we assume we followed a broken pointer during an indirect call, so
942 * we try the lookup again with the return address pushed on the stack
944 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
945 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
946 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
948 UCONTEXT_REG_EIP (ctx) = sp [0];
951 exc = mono_domain_get ()->stack_overflow_ex;
953 mono_handle_native_sigsegv (SIGSEGV, sigctx);
954 /* setup a call frame on the real stack so that control is returned there
955 * and exception handling can continue.
956 * If this was a stack overflow the caller already ensured the stack pages
957 * needed have been unprotected.
958 * The frame looks like:
965 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
968 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
969 sp = (gpointer)((char*)sp - frame_size);
970 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
973 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
976 sp [2] = (gpointer)stack_ovf;
977 /* may need to adjust pointers in the new struct copy, depending on the OS */
978 memcpy (sp + 4, ctx, sizeof (ucontext_t));
979 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
980 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
981 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
985 #if MONO_SUPPORT_TASKLETS
986 MonoContinuationRestore
987 mono_tasklets_arch_restore (void)
989 static guint8* saved = NULL;
990 guint8 *code, *start;
993 return (MonoContinuationRestore)saved;
994 code = start = mono_global_codeman_reserve (48);
995 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
996 /* put cont in edx */
997 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
998 /* setup the copy of the stack */
999 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1000 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1002 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1003 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1004 x86_prefix (code, X86_REP_PREFIX);
1007 /* now restore the registers from the LMF */
1008 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1009 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1010 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1011 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1012 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1014 /* restore the lmf chain */
1015 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1016 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1018 /* state in eax, so it's setup as the return value */
1019 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1020 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1021 g_assert ((code - start) <= 48);
1023 return (MonoContinuationRestore)saved;