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 (void)
266 static guint8 *start = NULL;
272 /* restore_contect (MonoContext *ctx) */
273 /* we do not restore X86_EDX */
275 start = code = mono_global_codeman_reserve (128);
278 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
280 /* get return address, stored in EDX */
281 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
283 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
285 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
287 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
289 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
291 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
293 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eax), 4);
295 /* jump to the saved IP */
296 x86_jump_reg (code, X86_EDX);
302 * mono_arch_get_call_filter:
304 * Returns a pointer to a method which calls an exception filter. We
305 * also use this function to call finally handlers (we pass NULL as
306 * @exc object in this case).
309 mono_arch_get_call_filter (void)
311 static guint8* start;
312 static int inited = 0;
319 /* call_filter (MonoContext *ctx, unsigned long eip) */
320 start = code = mono_global_codeman_reserve (64);
322 x86_push_reg (code, X86_EBP);
323 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
324 x86_push_reg (code, X86_EBX);
325 x86_push_reg (code, X86_EDI);
326 x86_push_reg (code, X86_ESI);
329 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
331 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
333 x86_push_reg (code, X86_EBP);
336 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
337 /* restore registers used by global register allocation (EBX & ESI) */
338 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
339 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
340 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
342 /* align stack and save ESP */
343 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
344 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
345 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
346 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
347 x86_push_reg (code, X86_EDX);
349 /* call the handler */
350 x86_call_reg (code, X86_ECX);
353 x86_pop_reg (code, X86_ESP);
356 x86_pop_reg (code, X86_EBP);
358 /* restore saved regs */
359 x86_pop_reg (code, X86_ESI);
360 x86_pop_reg (code, X86_EDI);
361 x86_pop_reg (code, X86_EBX);
365 g_assert ((code - start) < 64);
370 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
371 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
372 unsigned long eip, unsigned long esp, gboolean rethrow)
374 static void (*restore_context) (MonoContext *);
377 if (!restore_context)
378 restore_context = mono_arch_get_restore_context ();
380 /* Pop alignment added in get_throw_exception (), the return address, plus the argument and the alignment added at the call site */
381 ctx.esp = esp + 8 + MONO_ARCH_FRAME_ALIGNMENT;
392 /* The OSX ABI specifies 16 byte alignment at call sites */
393 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
396 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
397 MonoException *mono_ex = (MonoException*)exc;
399 mono_ex->stack_trace = NULL;
402 if (mono_debug_using_mono_debugger ()) {
403 guint8 buf [16], *code;
405 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
408 if (buf [3] == 0xe8) {
409 MonoContext ctx_cp = ctx;
410 ctx_cp.eip = eip - 5;
412 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
413 restore_context (&ctx_cp);
414 g_assert_not_reached ();
419 /* adjust eip so that it point into the call instruction */
422 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
424 restore_context (&ctx);
426 g_assert_not_reached ();
430 get_throw_exception (gboolean rethrow)
432 guint8 *start, *code;
434 start = code = mono_global_codeman_reserve (64);
437 * Align the stack on apple, since we push 10 args, and the call pushed 4 bytes.
439 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
440 x86_push_reg (code, X86_ESP);
441 x86_push_membase (code, X86_ESP, 8); /* IP */
442 x86_push_membase (code, X86_ESP, 16); /* exception */
443 x86_push_reg (code, X86_EBP);
444 x86_push_reg (code, X86_EDI);
445 x86_push_reg (code, X86_ESI);
446 x86_push_reg (code, X86_EBX);
447 x86_push_reg (code, X86_EDX);
448 x86_push_reg (code, X86_ECX);
449 x86_push_reg (code, X86_EAX);
450 x86_call_code (code, throw_exception);
451 /* we should never reach this breakpoint */
452 x86_breakpoint (code);
454 g_assert ((code - start) < 64);
460 * mono_arch_get_throw_exception:
462 * Returns a function pointer which can be used to raise
463 * exceptions. The returned function has the following
464 * signature: void (*func) (MonoException *exc);
465 * For example to raise an arithmetic exception you can use:
467 * x86_push_imm (code, mono_get_exception_arithmetic ());
468 * x86_call_code (code, arch_get_throw_exception ());
472 mono_arch_get_throw_exception (void)
474 static guint8 *start;
475 static int inited = 0;
480 start = get_throw_exception (FALSE);
488 mono_arch_get_rethrow_exception (void)
490 static guint8 *start;
491 static int inited = 0;
496 start = get_throw_exception (TRUE);
504 * mono_arch_get_throw_exception_by_name:
506 * Returns a function pointer which can be used to raise
507 * corlib exceptions. The returned function has the following
508 * signature: void (*func) (gpointer ip, char *exc_name);
509 * For example to raise an arithmetic exception you can use:
511 * x86_push_imm (code, "ArithmeticException");
512 * x86_push_imm (code, <IP>)
513 * x86_jump_code (code, arch_get_throw_exception_by_name ());
517 mono_arch_get_throw_exception_by_name (void)
522 start = code = mono_global_codeman_reserve (32);
525 x86_breakpoint (code);
527 mono_arch_flush_icache (start, code - start);
533 * mono_arch_get_throw_corlib_exception:
535 * Returns a function pointer which can be used to raise
536 * corlib exceptions. The returned function has the following
537 * signature: void (*func) (guint32 ex_token, guint32 offset);
538 * Here, offset is the offset which needs to be substracted from the caller IP
539 * to get the IP of the throw. Passing the offset has the advantage that it
540 * needs no relocations in the caller.
543 mono_arch_get_throw_corlib_exception (void)
545 static guint8* start;
546 static int inited = 0;
548 GSList *unwind_ops = NULL;
554 code = start = mono_global_codeman_reserve (64);
557 * Align the stack on apple, the caller doesn't do this to save space,
558 * two arguments + the return addr are already on the stack.
560 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
561 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4 + 4, 4); /* token */
562 x86_alu_reg_imm (code, X86_ADD, X86_EAX, MONO_TOKEN_TYPE_DEF);
563 /* Align the stack on apple */
564 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
565 x86_push_reg (code, X86_EAX);
566 x86_push_imm (code, mono_defaults.exception_class->image);
567 x86_call_code (code, mono_exception_from_token);
568 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
569 /* Compute caller ip */
570 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
572 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4 + 4 + 4, 4);
574 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4 + 4 + 4 + 4);
575 x86_alu_reg_reg (code, X86_SUB, X86_ECX, X86_EDX);
576 /* Align the stack on apple, mirrors the sub in OP_THROW. */
577 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
578 /* Push exception object */
579 x86_push_reg (code, X86_EAX);
581 x86_push_reg (code, X86_ECX);
582 x86_jump_code (code, mono_arch_get_throw_exception ());
584 g_assert ((code - start) < 64);
586 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
587 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
589 mono_save_trampoline_xdebug_info ("arch_throw_corlib_exception_trampoline", start, code - start, unwind_ops);
595 * mono_arch_find_jit_info_ext:
597 * See exceptions-amd64.c for docs.
600 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
601 MonoJitInfo *ji, MonoContext *ctx,
602 MonoContext *new_ctx, MonoLMF **lmf,
603 StackFrameInfo *frame)
605 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
607 memset (frame, 0, sizeof (StackFrameInfo));
609 frame->managed = FALSE;
614 gssize regs [MONO_MAX_IREGS + 1];
616 guint32 unwind_info_len;
619 frame->type = FRAME_TYPE_MANAGED;
621 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
622 frame->managed = TRUE;
625 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
627 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
629 regs [X86_EAX] = new_ctx->eax;
630 regs [X86_EBX] = new_ctx->ebx;
631 regs [X86_ECX] = new_ctx->ecx;
632 regs [X86_EDX] = new_ctx->edx;
633 regs [X86_ESP] = new_ctx->esp;
634 regs [X86_EBP] = new_ctx->ebp;
635 regs [X86_ESI] = new_ctx->esi;
636 regs [X86_EDI] = new_ctx->edi;
637 regs [X86_NREG] = new_ctx->eip;
639 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
640 (guint8*)ji->code_start + ji->code_size,
641 ip, regs, MONO_MAX_IREGS + 1, &cfa);
643 new_ctx->eax = regs [X86_EAX];
644 new_ctx->ebx = regs [X86_EBX];
645 new_ctx->ecx = regs [X86_ECX];
646 new_ctx->edx = regs [X86_EDX];
647 new_ctx->esp = regs [X86_ESP];
648 new_ctx->ebp = regs [X86_EBP];
649 new_ctx->esi = regs [X86_ESI];
650 new_ctx->edi = regs [X86_EDI];
651 new_ctx->eip = regs [X86_NREG];
653 /* The CFA becomes the new SP value */
654 new_ctx->esp = (gssize)cfa;
659 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
660 /* remove any unused lmf */
661 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
664 /* Pop arguments off the stack */
666 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
671 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
673 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
674 new_ctx->esp += stack_to_pop;
681 if (((guint64)(*lmf)->previous_lmf) & 2) {
683 * This LMF entry is created by the soft debug code to mark transitions to
684 * managed code done during invokes.
686 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
688 g_assert (ext->debugger_invoke);
690 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
692 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
694 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
699 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
701 if (!((guint32)((*lmf)->previous_lmf) & 1))
704 /* Trampoline lmf frame */
705 frame->method = (*lmf)->method;
708 new_ctx->esi = (*lmf)->esi;
709 new_ctx->edi = (*lmf)->edi;
710 new_ctx->ebx = (*lmf)->ebx;
711 new_ctx->ebp = (*lmf)->ebp;
712 new_ctx->eip = (*lmf)->eip;
715 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
717 /* Check if we are in a trampoline LMF frame */
718 if ((guint32)((*lmf)->previous_lmf) & 1) {
719 /* lmf->esp is set by the trampoline code */
720 new_ctx->esp = (*lmf)->esp;
722 /* Pop arguments off the stack */
723 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
724 /* FIXME: Handle the IMT/vtable case too */
726 if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
727 MonoMethod *method = (*lmf)->method;
728 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
730 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
731 new_ctx->esp += stack_to_pop;
736 /* the lmf is always stored on the stack, so the following
737 * expression points to a stack location which can be used as ESP */
738 new_ctx->esp = (unsigned long)&((*lmf)->eip);
740 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
761 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
763 #ifdef MONO_ARCH_USE_SIGACTION
764 ucontext_t *ctx = (ucontext_t*)sigctx;
766 mctx->eax = UCONTEXT_REG_EAX (ctx);
767 mctx->ebx = UCONTEXT_REG_EBX (ctx);
768 mctx->ecx = UCONTEXT_REG_ECX (ctx);
769 mctx->edx = UCONTEXT_REG_EDX (ctx);
770 mctx->ebp = UCONTEXT_REG_EBP (ctx);
771 mctx->esp = UCONTEXT_REG_ESP (ctx);
772 mctx->esi = UCONTEXT_REG_ESI (ctx);
773 mctx->edi = UCONTEXT_REG_EDI (ctx);
774 mctx->eip = UCONTEXT_REG_EIP (ctx);
776 struct sigcontext *ctx = (struct sigcontext *)sigctx;
778 mctx->eax = ctx->SC_EAX;
779 mctx->ebx = ctx->SC_EBX;
780 mctx->ecx = ctx->SC_ECX;
781 mctx->edx = ctx->SC_EDX;
782 mctx->ebp = ctx->SC_EBP;
783 mctx->esp = ctx->SC_ESP;
784 mctx->esi = ctx->SC_ESI;
785 mctx->edi = ctx->SC_EDI;
786 mctx->eip = ctx->SC_EIP;
791 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
793 #ifdef MONO_ARCH_USE_SIGACTION
794 ucontext_t *ctx = (ucontext_t*)sigctx;
796 UCONTEXT_REG_EAX (ctx) = mctx->eax;
797 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
798 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
799 UCONTEXT_REG_EDX (ctx) = mctx->edx;
800 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
801 UCONTEXT_REG_ESP (ctx) = mctx->esp;
802 UCONTEXT_REG_ESI (ctx) = mctx->esi;
803 UCONTEXT_REG_EDI (ctx) = mctx->edi;
804 UCONTEXT_REG_EIP (ctx) = mctx->eip;
806 struct sigcontext *ctx = (struct sigcontext *)sigctx;
808 ctx->SC_EAX = mctx->eax;
809 ctx->SC_EBX = mctx->ebx;
810 ctx->SC_ECX = mctx->ecx;
811 ctx->SC_EDX = mctx->edx;
812 ctx->SC_EBP = mctx->ebp;
813 ctx->SC_ESP = mctx->esp;
814 ctx->SC_ESI = mctx->esi;
815 ctx->SC_EDI = mctx->edi;
816 ctx->SC_EIP = mctx->eip;
821 mono_arch_ip_from_context (void *sigctx)
823 #ifdef MONO_ARCH_USE_SIGACTION
824 ucontext_t *ctx = (ucontext_t*)sigctx;
825 return (gpointer)UCONTEXT_REG_EIP (ctx);
827 struct sigcontext *ctx = sigctx;
828 return (gpointer)ctx->SC_EIP;
833 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
837 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
839 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
842 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
844 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
850 restore_soft_guard_pages (void)
852 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
853 if (jit_tls->stack_ovf_guard_base)
854 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
858 * this function modifies mctx so that when it is restored, it
859 * won't execcute starting at mctx.eip, but in a function that
860 * will restore the protection on the soft-guard pages and return back to
861 * continue at mctx.eip.
864 prepare_for_guard_pages (MonoContext *mctx)
867 sp = (gpointer)(mctx->esp);
869 /* the resturn addr */
870 sp [0] = (gpointer)(mctx->eip);
871 mctx->eip = (unsigned long)restore_soft_guard_pages;
872 mctx->esp = (unsigned long)sp;
876 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
878 void (*restore_context) (MonoContext *);
881 restore_context = mono_arch_get_restore_context ();
882 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
884 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
886 prepare_for_guard_pages (&mctx);
887 restore_context (&mctx);
890 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
892 prepare_for_guard_pages (&mctx);
893 restore_context (&mctx);
897 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
899 #ifdef MONO_ARCH_USE_SIGACTION
900 MonoException *exc = NULL;
901 ucontext_t *ctx = (ucontext_t*)sigctx;
902 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
906 /* if we didn't find a managed method for the ip address and it matches the fault
907 * address, we assume we followed a broken pointer during an indirect call, so
908 * we try the lookup again with the return address pushed on the stack
910 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
911 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
912 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
914 UCONTEXT_REG_EIP (ctx) = sp [0];
917 exc = mono_domain_get ()->stack_overflow_ex;
919 mono_handle_native_sigsegv (SIGSEGV, sigctx);
920 /* setup a call frame on the real stack so that control is returned there
921 * and exception handling can continue.
922 * If this was a stack overflow the caller already ensured the stack pages
923 * needed have been unprotected.
924 * The frame looks like:
931 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
934 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
935 sp = (gpointer)((char*)sp - frame_size);
936 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
939 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
942 sp [2] = (gpointer)stack_ovf;
943 /* may need to adjust pointers in the new struct copy, depending on the OS */
944 memcpy (sp + 4, ctx, sizeof (ucontext_t));
945 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
946 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
947 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
951 #if MONO_SUPPORT_TASKLETS
952 MonoContinuationRestore
953 mono_tasklets_arch_restore (void)
955 static guint8* saved = NULL;
956 guint8 *code, *start;
959 return (MonoContinuationRestore)saved;
960 code = start = mono_global_codeman_reserve (48);
961 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
962 /* put cont in edx */
963 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
964 /* setup the copy of the stack */
965 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
966 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
968 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
969 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
970 x86_prefix (code, X86_REP_PREFIX);
973 /* now restore the registers from the LMF */
974 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
975 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
976 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
977 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
978 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
980 /* restore the lmf chain */
981 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
982 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
984 /* state in eax, so it's setup as the return value */
985 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
986 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
987 g_assert ((code - start) <= 48);
989 return (MonoContinuationRestore)saved;