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;
553 code = start = mono_global_codeman_reserve (64);
556 * Align the stack on apple, the caller doesn't do this to save space,
557 * two arguments + the return addr are already on the stack.
559 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
560 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4 + 4, 4); /* token */
561 x86_alu_reg_imm (code, X86_ADD, X86_EAX, MONO_TOKEN_TYPE_DEF);
562 /* Align the stack on apple */
563 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
564 x86_push_reg (code, X86_EAX);
565 x86_push_imm (code, mono_defaults.exception_class->image);
566 x86_call_code (code, mono_exception_from_token);
567 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
568 /* Compute caller ip */
569 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
571 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4 + 4 + 4, 4);
573 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4 + 4 + 4 + 4);
574 x86_alu_reg_reg (code, X86_SUB, X86_ECX, X86_EDX);
575 /* Align the stack on apple, mirrors the sub in OP_THROW. */
576 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
577 /* Push exception object */
578 x86_push_reg (code, X86_EAX);
580 x86_push_reg (code, X86_ECX);
581 x86_jump_code (code, mono_arch_get_throw_exception ());
583 g_assert ((code - start) < 64);
589 * mono_arch_find_jit_info_ext:
591 * See exceptions-amd64.c for docs.
594 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
595 MonoJitInfo *ji, MonoContext *ctx,
596 MonoContext *new_ctx, MonoLMF **lmf,
597 StackFrameInfo *frame)
599 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
601 memset (frame, 0, sizeof (StackFrameInfo));
603 frame->managed = FALSE;
608 gssize regs [MONO_MAX_IREGS + 1];
610 guint32 unwind_info_len;
613 frame->type = FRAME_TYPE_MANAGED;
615 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
616 frame->managed = TRUE;
619 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
621 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
623 regs [X86_EAX] = new_ctx->eax;
624 regs [X86_EBX] = new_ctx->ebx;
625 regs [X86_ECX] = new_ctx->ecx;
626 regs [X86_EDX] = new_ctx->edx;
627 regs [X86_ESP] = new_ctx->esp;
628 regs [X86_EBP] = new_ctx->ebp;
629 regs [X86_ESI] = new_ctx->esi;
630 regs [X86_EDI] = new_ctx->edi;
631 regs [X86_NREG] = new_ctx->eip;
633 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
634 (guint8*)ji->code_start + ji->code_size,
635 ip, regs, MONO_MAX_IREGS + 1, &cfa);
637 new_ctx->eax = regs [X86_EAX];
638 new_ctx->ebx = regs [X86_EBX];
639 new_ctx->ecx = regs [X86_ECX];
640 new_ctx->edx = regs [X86_EDX];
641 new_ctx->esp = regs [X86_ESP];
642 new_ctx->ebp = regs [X86_EBP];
643 new_ctx->esi = regs [X86_ESI];
644 new_ctx->edi = regs [X86_EDI];
645 new_ctx->eip = regs [X86_NREG];
647 /* The CFA becomes the new SP value */
648 new_ctx->esp = (gssize)cfa;
653 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
654 /* remove any unused lmf */
655 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
658 /* Pop arguments off the stack */
660 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
662 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
663 new_ctx->esp += stack_to_pop;
669 if (((guint64)(*lmf)->previous_lmf) & 2) {
671 * This LMF entry is created by the soft debug code to mark transitions to
672 * managed code done during invokes.
674 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
676 g_assert (ext->debugger_invoke);
678 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
680 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
682 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
687 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
689 if (!((guint32)((*lmf)->previous_lmf) & 1))
692 /* Trampoline lmf frame */
693 frame->method = (*lmf)->method;
696 new_ctx->esi = (*lmf)->esi;
697 new_ctx->edi = (*lmf)->edi;
698 new_ctx->ebx = (*lmf)->ebx;
699 new_ctx->ebp = (*lmf)->ebp;
700 new_ctx->eip = (*lmf)->eip;
703 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
705 /* Check if we are in a trampoline LMF frame */
706 if ((guint32)((*lmf)->previous_lmf) & 1) {
707 /* lmf->esp is set by the trampoline code */
708 new_ctx->esp = (*lmf)->esp;
710 /* Pop arguments off the stack */
711 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
712 /* FIXME: Handle the IMT/vtable case too */
713 if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
714 MonoMethod *method = (*lmf)->method;
715 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
717 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
718 new_ctx->esp += stack_to_pop;
722 /* the lmf is always stored on the stack, so the following
723 * expression points to a stack location which can be used as ESP */
724 new_ctx->esp = (unsigned long)&((*lmf)->eip);
726 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
747 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
749 #ifdef MONO_ARCH_USE_SIGACTION
750 ucontext_t *ctx = (ucontext_t*)sigctx;
752 mctx->eax = UCONTEXT_REG_EAX (ctx);
753 mctx->ebx = UCONTEXT_REG_EBX (ctx);
754 mctx->ecx = UCONTEXT_REG_ECX (ctx);
755 mctx->edx = UCONTEXT_REG_EDX (ctx);
756 mctx->ebp = UCONTEXT_REG_EBP (ctx);
757 mctx->esp = UCONTEXT_REG_ESP (ctx);
758 mctx->esi = UCONTEXT_REG_ESI (ctx);
759 mctx->edi = UCONTEXT_REG_EDI (ctx);
760 mctx->eip = UCONTEXT_REG_EIP (ctx);
762 struct sigcontext *ctx = (struct sigcontext *)sigctx;
764 mctx->eax = ctx->SC_EAX;
765 mctx->ebx = ctx->SC_EBX;
766 mctx->ecx = ctx->SC_ECX;
767 mctx->edx = ctx->SC_EDX;
768 mctx->ebp = ctx->SC_EBP;
769 mctx->esp = ctx->SC_ESP;
770 mctx->esi = ctx->SC_ESI;
771 mctx->edi = ctx->SC_EDI;
772 mctx->eip = ctx->SC_EIP;
777 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
779 #ifdef MONO_ARCH_USE_SIGACTION
780 ucontext_t *ctx = (ucontext_t*)sigctx;
782 UCONTEXT_REG_EAX (ctx) = mctx->eax;
783 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
784 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
785 UCONTEXT_REG_EDX (ctx) = mctx->edx;
786 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
787 UCONTEXT_REG_ESP (ctx) = mctx->esp;
788 UCONTEXT_REG_ESI (ctx) = mctx->esi;
789 UCONTEXT_REG_EDI (ctx) = mctx->edi;
790 UCONTEXT_REG_EIP (ctx) = mctx->eip;
792 struct sigcontext *ctx = (struct sigcontext *)sigctx;
794 ctx->SC_EAX = mctx->eax;
795 ctx->SC_EBX = mctx->ebx;
796 ctx->SC_ECX = mctx->ecx;
797 ctx->SC_EDX = mctx->edx;
798 ctx->SC_EBP = mctx->ebp;
799 ctx->SC_ESP = mctx->esp;
800 ctx->SC_ESI = mctx->esi;
801 ctx->SC_EDI = mctx->edi;
802 ctx->SC_EIP = mctx->eip;
807 mono_arch_ip_from_context (void *sigctx)
809 #ifdef MONO_ARCH_USE_SIGACTION
810 ucontext_t *ctx = (ucontext_t*)sigctx;
811 return (gpointer)UCONTEXT_REG_EIP (ctx);
813 struct sigcontext *ctx = sigctx;
814 return (gpointer)ctx->SC_EIP;
819 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
823 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
825 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
828 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
830 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
836 restore_soft_guard_pages (void)
838 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
839 if (jit_tls->stack_ovf_guard_base)
840 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
844 * this function modifies mctx so that when it is restored, it
845 * won't execcute starting at mctx.eip, but in a function that
846 * will restore the protection on the soft-guard pages and return back to
847 * continue at mctx.eip.
850 prepare_for_guard_pages (MonoContext *mctx)
853 sp = (gpointer)(mctx->esp);
855 /* the resturn addr */
856 sp [0] = (gpointer)(mctx->eip);
857 mctx->eip = (unsigned long)restore_soft_guard_pages;
858 mctx->esp = (unsigned long)sp;
862 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
864 void (*restore_context) (MonoContext *);
867 restore_context = mono_arch_get_restore_context ();
868 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
870 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
872 prepare_for_guard_pages (&mctx);
873 restore_context (&mctx);
876 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
878 prepare_for_guard_pages (&mctx);
879 restore_context (&mctx);
883 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
885 #ifdef MONO_ARCH_USE_SIGACTION
886 MonoException *exc = NULL;
887 ucontext_t *ctx = (ucontext_t*)sigctx;
888 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
892 /* if we didn't find a managed method for the ip address and it matches the fault
893 * address, we assume we followed a broken pointer during an indirect call, so
894 * we try the lookup again with the return address pushed on the stack
896 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
897 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
898 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
900 UCONTEXT_REG_EIP (ctx) = sp [0];
903 exc = mono_domain_get ()->stack_overflow_ex;
905 mono_handle_native_sigsegv (SIGSEGV, sigctx);
906 /* setup a call frame on the real stack so that control is returned there
907 * and exception handling can continue.
908 * If this was a stack overflow the caller already ensured the stack pages
909 * needed have been unprotected.
910 * The frame looks like:
917 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
920 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
921 sp = (gpointer)((char*)sp - frame_size);
922 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
925 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
928 sp [2] = (gpointer)stack_ovf;
929 /* may need to adjust pointers in the new struct copy, depending on the OS */
930 memcpy (sp + 4, ctx, sizeof (ucontext_t));
931 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
932 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
933 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
937 #if MONO_SUPPORT_TASKLETS
938 MonoContinuationRestore
939 mono_tasklets_arch_restore (void)
941 static guint8* saved = NULL;
942 guint8 *code, *start;
945 return (MonoContinuationRestore)saved;
946 code = start = mono_global_codeman_reserve (48);
947 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
948 /* put cont in edx */
949 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
950 /* setup the copy of the stack */
951 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
952 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
954 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
955 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
956 x86_prefix (code, X86_REP_PREFIX);
959 /* now restore the registers from the LMF */
960 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
961 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
962 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
963 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
964 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
966 /* restore the lmf chain */
967 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
968 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
970 /* state in eax, so it's setup as the return value */
971 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
972 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
973 g_assert ((code - start) <= 48);
975 return (MonoContinuationRestore)saved;