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((int)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 ();
109 MonoJitInfo *ji, rji;
110 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
111 MonoLMF *lmf = jit_tls->lmf;
112 MonoContext initial_ctx;
114 guint32 free_stack = 0;
116 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
117 mono_arch_sigctx_to_monoctx (sctx, &ctx);
119 /* get our os page size */
121 page_size = si.dwPageSize;
123 /* Let's walk the stack to recover
124 * the needed stack space (if possible)
126 memset (&rji, 0, sizeof (rji));
129 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
131 /* try to free 64kb from our stack */
135 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, &lmf, NULL);
137 g_warning ("Exception inside function without unwind info");
138 g_assert_not_reached ();
141 if (ji != (gpointer)-1) {
142 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
145 /* todo: we should call abort if ji is -1 */
147 } while (free_stack < 64 * 1024 && ji != (gpointer) -1);
149 /* convert into sigcontext to be used in mono_arch_handle_exception */
150 mono_arch_monoctx_to_sigctx (&ctx, sctx);
152 /* todo: install new stack-guard page */
154 /* use the new stack and call mono_arch_handle_exception () */
155 restore_stack (sctx);
159 * Unhandled Exception Filter
160 * Top-level per-process exception handler.
162 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
164 EXCEPTION_RECORD* er;
166 struct sigcontext* sctx;
169 res = EXCEPTION_CONTINUE_EXECUTION;
171 er = ep->ExceptionRecord;
172 ctx = ep->ContextRecord;
173 sctx = g_malloc(sizeof(struct sigcontext));
175 /* Copy Win32 context to UNIX style context */
176 sctx->eax = ctx->Eax;
177 sctx->ebx = ctx->Ebx;
178 sctx->ecx = ctx->Ecx;
179 sctx->edx = ctx->Edx;
180 sctx->ebp = ctx->Ebp;
181 sctx->esp = ctx->Esp;
182 sctx->esi = ctx->Esi;
183 sctx->edi = ctx->Edi;
184 sctx->eip = ctx->Eip;
186 switch (er->ExceptionCode) {
187 case EXCEPTION_STACK_OVERFLOW:
188 win32_handle_stack_overflow (ep, sctx);
190 case EXCEPTION_ACCESS_VIOLATION:
191 W32_SEH_HANDLE_EX(segv);
193 case EXCEPTION_ILLEGAL_INSTRUCTION:
194 W32_SEH_HANDLE_EX(ill);
196 case EXCEPTION_INT_DIVIDE_BY_ZERO:
197 case EXCEPTION_INT_OVERFLOW:
198 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
199 case EXCEPTION_FLT_OVERFLOW:
200 case EXCEPTION_FLT_UNDERFLOW:
201 case EXCEPTION_FLT_INEXACT_RESULT:
202 W32_SEH_HANDLE_EX(fpe);
208 /* Copy context back */
209 ctx->Eax = sctx->eax;
210 ctx->Ebx = sctx->ebx;
211 ctx->Ecx = sctx->ecx;
212 ctx->Edx = sctx->edx;
213 ctx->Ebp = sctx->ebp;
214 ctx->Esp = sctx->esp;
215 ctx->Esi = sctx->esi;
216 ctx->Edi = sctx->edi;
217 ctx->Eip = sctx->eip;
224 void win32_seh_init()
226 /* install restore stack helper */
228 restore_stack = mono_win32_get_handle_stackoverflow ();
230 old_handler = SetUnhandledExceptionFilter(seh_handler);
233 void win32_seh_cleanup()
235 if (old_handler) SetUnhandledExceptionFilter(old_handler);
238 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
242 fpe_handler = handler;
245 ill_handler = handler;
248 segv_handler = handler;
255 #endif /* PLATFORM_WIN32 */
258 * mono_arch_get_restore_context:
260 * Returns a pointer to a method which restores a previously saved sigcontext.
263 mono_arch_get_restore_context (void)
265 static guint8 *start = NULL;
271 /* restore_contect (MonoContext *ctx) */
272 /* we do not restore X86_EAX, X86_EDX */
274 start = code = mono_global_codeman_reserve (128);
277 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
279 /* get return address, stored in EDX */
280 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
282 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
284 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
286 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
288 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
290 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
292 /* jump to the saved IP */
293 x86_jump_reg (code, X86_EDX);
299 * mono_arch_get_call_filter:
301 * Returns a pointer to a method which calls an exception filter. We
302 * also use this function to call finally handlers (we pass NULL as
303 * @exc object in this case).
306 mono_arch_get_call_filter (void)
308 static guint8* start;
309 static int inited = 0;
316 /* call_filter (MonoContext *ctx, unsigned long eip) */
317 start = code = mono_global_codeman_reserve (64);
319 x86_push_reg (code, X86_EBP);
320 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
321 x86_push_reg (code, X86_EBX);
322 x86_push_reg (code, X86_EDI);
323 x86_push_reg (code, X86_ESI);
326 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
328 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
330 x86_push_reg (code, X86_EBP);
333 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
334 /* restore registers used by global register allocation (EBX & ESI) */
335 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
336 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
337 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
339 /* align stack and save ESP */
340 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
341 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
342 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
343 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
344 x86_push_reg (code, X86_EDX);
346 /* call the handler */
347 x86_call_reg (code, X86_ECX);
350 x86_pop_reg (code, X86_ESP);
353 x86_pop_reg (code, X86_EBP);
355 /* restore saved regs */
356 x86_pop_reg (code, X86_ESI);
357 x86_pop_reg (code, X86_EDI);
358 x86_pop_reg (code, X86_EBX);
362 g_assert ((code - start) < 64);
367 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
368 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
369 unsigned long eip, unsigned long esp, gboolean rethrow)
371 static void (*restore_context) (MonoContext *);
374 if (!restore_context)
375 restore_context = mono_arch_get_restore_context ();
377 /* Pop argument and return address */
378 ctx.esp = esp + (2 * sizeof (gpointer));
388 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
389 MonoException *mono_ex = (MonoException*)exc;
391 mono_ex->stack_trace = NULL;
394 if (mono_debug_using_mono_debugger ()) {
395 guint8 buf [16], *code;
397 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
400 if (buf [3] == 0xe8) {
401 MonoContext ctx_cp = ctx;
402 ctx_cp.eip = eip - 5;
404 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
405 restore_context (&ctx_cp);
406 g_assert_not_reached ();
411 /* adjust eip so that it point into the call instruction */
414 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
415 restore_context (&ctx);
417 g_assert_not_reached ();
421 get_throw_exception (gboolean rethrow)
423 guint8 *start, *code;
425 start = code = mono_global_codeman_reserve (64);
427 x86_push_reg (code, X86_ESP);
428 x86_push_membase (code, X86_ESP, 4); /* IP */
429 x86_push_membase (code, X86_ESP, 12); /* exception */
430 x86_push_reg (code, X86_EBP);
431 x86_push_reg (code, X86_EDI);
432 x86_push_reg (code, X86_ESI);
433 x86_push_reg (code, X86_EBX);
434 x86_push_reg (code, X86_EDX);
435 x86_push_reg (code, X86_ECX);
436 x86_push_reg (code, X86_EAX);
437 x86_call_code (code, throw_exception);
438 /* we should never reach this breakpoint */
439 x86_breakpoint (code);
441 g_assert ((code - start) < 64);
447 * mono_arch_get_throw_exception:
449 * Returns a function pointer which can be used to raise
450 * exceptions. The returned function has the following
451 * signature: void (*func) (MonoException *exc);
452 * For example to raise an arithmetic exception you can use:
454 * x86_push_imm (code, mono_get_exception_arithmetic ());
455 * x86_call_code (code, arch_get_throw_exception ());
459 mono_arch_get_throw_exception (void)
461 static guint8 *start;
462 static int inited = 0;
467 start = get_throw_exception (FALSE);
475 mono_arch_get_rethrow_exception (void)
477 static guint8 *start;
478 static int inited = 0;
483 start = get_throw_exception (TRUE);
491 * mono_arch_get_throw_exception_by_name:
493 * Returns a function pointer which can be used to raise
494 * corlib exceptions. The returned function has the following
495 * signature: void (*func) (gpointer ip, char *exc_name);
496 * For example to raise an arithmetic exception you can use:
498 * x86_push_imm (code, "ArithmeticException");
499 * x86_push_imm (code, <IP>)
500 * x86_jump_code (code, arch_get_throw_exception_by_name ());
504 mono_arch_get_throw_exception_by_name (void)
506 static guint8* start;
507 static int inited = 0;
514 code = start = mono_global_codeman_reserve (32);
516 x86_push_membase (code, X86_ESP, 4); /* exception name */
517 x86_push_imm (code, "System");
518 x86_push_imm (code, mono_defaults.exception_class->image);
519 x86_call_code (code, mono_exception_from_name);
520 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
521 /* save the newly create object (overwrite exception name)*/
522 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
523 x86_jump_code (code, mono_arch_get_throw_exception ());
525 g_assert ((code - start) < 32);
531 * mono_arch_get_throw_corlib_exception:
533 * Returns a function pointer which can be used to raise
534 * corlib exceptions. The returned function has the following
535 * signature: void (*func) (guint32 ex_token, guint32 offset);
536 * Here, offset is the offset which needs to be substracted from the caller IP
537 * to get the IP of the throw. Passing the offset has the advantage that it
538 * needs no relocations in the caller.
541 mono_arch_get_throw_corlib_exception (void)
543 static guint8* start;
544 static int inited = 0;
551 code = start = mono_global_codeman_reserve (64);
553 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4); /* token */
554 x86_alu_reg_imm (code, X86_ADD, X86_EAX, MONO_TOKEN_TYPE_DEF);
555 x86_push_reg (code, X86_EAX);
556 x86_push_imm (code, mono_defaults.exception_class->image);
557 x86_call_code (code, mono_exception_from_token);
558 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
559 /* Compute caller ip */
560 x86_pop_reg (code, X86_ECX);
562 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
563 x86_pop_reg (code, X86_EDX);
564 x86_alu_reg_reg (code, X86_SUB, X86_ECX, X86_EDX);
565 /* Push exception object */
566 x86_push_reg (code, X86_EAX);
568 x86_push_reg (code, X86_ECX);
569 x86_jump_code (code, mono_arch_get_throw_exception ());
571 g_assert ((code - start) < 64);
576 /* mono_arch_find_jit_info:
578 * This function is used to gather information from @ctx. It return the
579 * MonoJitInfo of the corresponding function, unwinds one stack frame and
580 * stores the resulting context into @new_ctx. It also stores a string
581 * describing the stack location into @trace (if not NULL), and modifies
582 * the @lmf if necessary. @native_offset return the IP offset from the
583 * start of the function or -1 if that info is not available.
586 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
587 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
590 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
592 /* Avoid costly table lookup during stack overflow */
593 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
596 ji = mono_jit_info_table_find (domain, ip);
604 gssize regs [MONO_MAX_IREGS + 1];
606 guint32 unwind_info_len;
610 if (!ji->method->wrapper_type)
614 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
616 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
618 regs [X86_EAX] = new_ctx->eax;
619 regs [X86_EBX] = new_ctx->ebx;
620 regs [X86_ECX] = new_ctx->ecx;
621 regs [X86_EDX] = new_ctx->edx;
622 regs [X86_ESP] = new_ctx->esp;
623 regs [X86_EBP] = new_ctx->ebp;
624 regs [X86_ESI] = new_ctx->esi;
625 regs [X86_EDI] = new_ctx->edi;
626 regs [X86_NREG] = new_ctx->eip;
628 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
629 (guint8*)ji->code_start + ji->code_size,
630 ip, regs, MONO_MAX_IREGS + 1, &cfa);
632 new_ctx->eax = regs [X86_EAX];
633 new_ctx->ebx = regs [X86_EBX];
634 new_ctx->ecx = regs [X86_ECX];
635 new_ctx->edx = regs [X86_EDX];
636 new_ctx->esp = regs [X86_ESP];
637 new_ctx->ebp = regs [X86_EBP];
638 new_ctx->esi = regs [X86_ESI];
639 new_ctx->edi = regs [X86_EDI];
640 new_ctx->eip = regs [X86_NREG];
642 /* The CFA becomes the new SP value */
643 new_ctx->esp = (gssize)cfa;
648 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
649 /* remove any unused lmf */
650 *lmf = (gpointer)(((guint32)(*lmf)->previous_lmf) & ~1);
653 /* Pop arguments off the stack */
655 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
657 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
658 new_ctx->esp += stack_to_pop;
666 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
668 if (!((guint32)((*lmf)->previous_lmf) & 1))
671 /* Trampoline lmf frame */
672 memset (res, 0, sizeof (MonoJitInfo));
673 res->method = (*lmf)->method;
676 new_ctx->esi = (*lmf)->esi;
677 new_ctx->edi = (*lmf)->edi;
678 new_ctx->ebx = (*lmf)->ebx;
679 new_ctx->ebp = (*lmf)->ebp;
680 new_ctx->eip = (*lmf)->eip;
682 /* Check if we are in a trampoline LMF frame */
683 if ((guint32)((*lmf)->previous_lmf) & 1) {
684 /* lmf->esp is set by the trampoline code */
685 new_ctx->esp = (*lmf)->esp;
687 /* Pop arguments off the stack */
688 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
689 /* FIXME: Handle the IMT/vtable case too */
690 if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
691 MonoMethod *method = (*lmf)->method;
692 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
694 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
695 new_ctx->esp += stack_to_pop;
699 /* the lmf is always stored on the stack, so the following
700 * expression points to a stack location which can be used as ESP */
701 new_ctx->esp = (unsigned long)&((*lmf)->eip);
703 *lmf = (gpointer)(((guint32)(*lmf)->previous_lmf) & ~1);
705 return ji ? ji : res;
724 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
726 #ifdef MONO_ARCH_USE_SIGACTION
727 ucontext_t *ctx = (ucontext_t*)sigctx;
729 mctx->eax = UCONTEXT_REG_EAX (ctx);
730 mctx->ebx = UCONTEXT_REG_EBX (ctx);
731 mctx->ecx = UCONTEXT_REG_ECX (ctx);
732 mctx->edx = UCONTEXT_REG_EDX (ctx);
733 mctx->ebp = UCONTEXT_REG_EBP (ctx);
734 mctx->esp = UCONTEXT_REG_ESP (ctx);
735 mctx->esi = UCONTEXT_REG_ESI (ctx);
736 mctx->edi = UCONTEXT_REG_EDI (ctx);
737 mctx->eip = UCONTEXT_REG_EIP (ctx);
739 struct sigcontext *ctx = (struct sigcontext *)sigctx;
741 mctx->eax = ctx->SC_EAX;
742 mctx->ebx = ctx->SC_EBX;
743 mctx->ecx = ctx->SC_ECX;
744 mctx->edx = ctx->SC_EDX;
745 mctx->ebp = ctx->SC_EBP;
746 mctx->esp = ctx->SC_ESP;
747 mctx->esi = ctx->SC_ESI;
748 mctx->edi = ctx->SC_EDI;
749 mctx->eip = ctx->SC_EIP;
754 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
756 #ifdef MONO_ARCH_USE_SIGACTION
757 ucontext_t *ctx = (ucontext_t*)sigctx;
759 UCONTEXT_REG_EAX (ctx) = mctx->eax;
760 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
761 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
762 UCONTEXT_REG_EDX (ctx) = mctx->edx;
763 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
764 UCONTEXT_REG_ESP (ctx) = mctx->esp;
765 UCONTEXT_REG_ESI (ctx) = mctx->esi;
766 UCONTEXT_REG_EDI (ctx) = mctx->edi;
767 UCONTEXT_REG_EIP (ctx) = mctx->eip;
769 struct sigcontext *ctx = (struct sigcontext *)sigctx;
771 ctx->SC_EAX = mctx->eax;
772 ctx->SC_EBX = mctx->ebx;
773 ctx->SC_ECX = mctx->ecx;
774 ctx->SC_EDX = mctx->edx;
775 ctx->SC_EBP = mctx->ebp;
776 ctx->SC_ESP = mctx->esp;
777 ctx->SC_ESI = mctx->esi;
778 ctx->SC_EDI = mctx->edi;
779 ctx->SC_EIP = mctx->eip;
784 mono_arch_ip_from_context (void *sigctx)
786 #ifdef MONO_ARCH_USE_SIGACTION
787 ucontext_t *ctx = (ucontext_t*)sigctx;
788 return (gpointer)UCONTEXT_REG_EIP (ctx);
790 struct sigcontext *ctx = sigctx;
791 return (gpointer)ctx->SC_EIP;
796 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
800 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
802 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
805 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
807 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
813 restore_soft_guard_pages (void)
815 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
816 if (jit_tls->stack_ovf_guard_base)
817 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
821 * this function modifies mctx so that when it is restored, it
822 * won't execcute starting at mctx.eip, but in a function that
823 * will restore the protection on the soft-guard pages and return back to
824 * continue at mctx.eip.
827 prepare_for_guard_pages (MonoContext *mctx)
830 sp = (gpointer)(mctx->esp);
832 /* the resturn addr */
833 sp [0] = (gpointer)(mctx->eip);
834 mctx->eip = (unsigned long)restore_soft_guard_pages;
835 mctx->esp = (unsigned long)sp;
839 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
841 void (*restore_context) (MonoContext *);
844 restore_context = mono_arch_get_restore_context ();
845 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
847 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
849 prepare_for_guard_pages (&mctx);
850 restore_context (&mctx);
853 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
855 prepare_for_guard_pages (&mctx);
856 restore_context (&mctx);
860 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
862 #ifdef MONO_ARCH_USE_SIGACTION
863 MonoException *exc = NULL;
864 ucontext_t *ctx = (ucontext_t*)sigctx;
865 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx));
869 /* if we didn't find a managed method for the ip address and it matches the fault
870 * address, we assume we followed a broken pointer during an indirect call, so
871 * we try the lookup again with the return address pushed on the stack
873 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
874 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
875 ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)sp [0]);
877 UCONTEXT_REG_EIP (ctx) = sp [0];
880 exc = mono_domain_get ()->stack_overflow_ex;
882 mono_handle_native_sigsegv (SIGSEGV, sigctx);
883 /* setup a call frame on the real stack so that control is returned there
884 * and exception handling can continue.
885 * If this was a stack overflow the caller already ensured the stack pages
886 * needed have been unprotected.
887 * The frame looks like:
894 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
897 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
898 sp = (gpointer)((char*)sp - frame_size);
899 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
902 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
905 sp [2] = (gpointer)stack_ovf;
906 /* may need to adjust pointers in the new struct copy, depending on the OS */
907 memcpy (sp + 4, ctx, sizeof (ucontext_t));
908 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
909 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
910 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
914 #if MONO_SUPPORT_TASKLETS
915 MonoContinuationRestore
916 mono_tasklets_arch_restore (void)
918 static guint8* saved = NULL;
919 guint8 *code, *start;
922 return (MonoContinuationRestore)saved;
923 code = start = mono_global_codeman_reserve (48);
924 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
925 /* put cont in edx */
926 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
927 /* setup the copy of the stack */
928 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
929 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
931 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
932 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
933 x86_prefix (code, X86_REP_PREFIX);
936 /* now restore the registers from the LMF */
937 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
938 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
939 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
940 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
941 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
943 /* restore the lmf chain */
944 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
945 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
947 /* state in eax, so it's setup as the return value */
948 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
949 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
950 g_assert ((code - start) <= 48);
952 return (MonoContinuationRestore)saved;