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 * mono_x86_llvm_throw_exception:
432 * Same as throw_exception, but called from LLVM compiled code.
433 * Moved into a separate function to avoid breaking the alignment stuff in
434 * throw_exception, which is hard to test.
437 mono_x86_llvm_throw_exception (mgreg_t *regs, MonoObject *exc,
438 mgreg_t eip, gboolean rethrow)
440 static void (*restore_context) (MonoContext *);
443 if (!restore_context)
444 restore_context = mono_arch_get_restore_context ();
446 /* Pop alignment added in get_throw_exception (), the return address, plus the argument and the alignment added at the call site */
447 ctx.esp = regs [X86_ESP];
449 ctx.ebp = regs [X86_EBP];
450 ctx.edi = regs [X86_EDI];
451 ctx.esi = regs [X86_ESI];
452 ctx.ebx = regs [X86_EBX];
453 ctx.edx = regs [X86_EDX];
454 ctx.ecx = regs [X86_ECX];
455 ctx.eax = regs [X86_EAX];
458 /* The OSX ABI specifies 16 byte alignment at call sites */
459 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
462 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
463 MonoException *mono_ex = (MonoException*)exc;
465 mono_ex->stack_trace = NULL;
468 if (mono_debug_using_mono_debugger ()) {
469 guint8 buf [16], *code;
471 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
474 if (buf [3] == 0xe8) {
475 MonoContext ctx_cp = ctx;
476 ctx_cp.eip = eip - 5;
478 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
479 restore_context (&ctx_cp);
480 g_assert_not_reached ();
485 /* adjust eip so that it point into the call instruction */
488 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
490 restore_context (&ctx);
492 g_assert_not_reached ();
496 mono_x86_llvm_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
497 mgreg_t eip, guint32 pc_offset)
499 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
502 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
506 mono_x86_llvm_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
510 get_throw_exception (gboolean rethrow)
512 guint8 *start, *code;
514 start = code = mono_global_codeman_reserve (64);
517 * Align the stack on apple, since we push 10 args, and the call pushed 4 bytes.
519 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
520 x86_push_reg (code, X86_ESP);
521 x86_push_membase (code, X86_ESP, 8); /* IP */
522 x86_push_membase (code, X86_ESP, 16); /* exception */
523 x86_push_reg (code, X86_EBP);
524 x86_push_reg (code, X86_EDI);
525 x86_push_reg (code, X86_ESI);
526 x86_push_reg (code, X86_EBX);
527 x86_push_reg (code, X86_EDX);
528 x86_push_reg (code, X86_ECX);
529 x86_push_reg (code, X86_EAX);
530 x86_call_code (code, throw_exception);
531 /* we should never reach this breakpoint */
532 x86_breakpoint (code);
534 g_assert ((code - start) < 64);
540 * get_llvm_throw_exception:
542 * Generate a call to mono_x86_llvm_throw_exception/
543 * mono_x86_llvm_throw_corlib_exception.
546 get_llvm_throw_exception (gboolean rethrow, gboolean corlib)
548 guint8 *start, *code;
549 GSList *unwind_ops = NULL;
550 int i, stack_size, arg_offsets [5], regs_offset;
552 start = code = mono_global_codeman_reserve (128);
557 * The stack looks like this:
558 * <pc offset> (only if corlib is TRUE)
559 * <exception object>/<type token>
560 * <return addr> <- esp
563 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
564 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
567 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
568 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
573 arg_offsets [3] = 12;
577 for (i = 0; i < X86_NREG; ++i)
579 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
581 /* LLVM doesn't push the exception object */
582 x86_lea_membase (code, X86_EAX, X86_ESP, stack_size + 4);
583 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
585 /* Set arg1 == regs */
586 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
587 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
588 /* Set arg2 == exc */
589 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
590 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
591 /* Set arg3 == eip */
592 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
593 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
595 /* Set arg4 == offset */
596 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
597 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
599 /* Set arg4 == rethrow */
600 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
603 x86_call_code (code, corlib ? (gpointer)mono_x86_llvm_throw_corlib_exception : (gpointer)mono_x86_llvm_throw_exception);
604 x86_breakpoint (code);
606 g_assert ((code - start) < 128);
608 mono_save_trampoline_xdebug_info (corlib ? "llvm_throw_corlib_exception_trampoline" : "llvm_throw_exception_trampoline", start, code - start, unwind_ops);
614 * mono_arch_get_throw_exception:
616 * Returns a function pointer which can be used to raise
617 * exceptions. The returned function has the following
618 * signature: void (*func) (MonoException *exc);
619 * For example to raise an arithmetic exception you can use:
621 * x86_push_imm (code, mono_get_exception_arithmetic ());
622 * x86_call_code (code, arch_get_throw_exception ());
626 mono_arch_get_throw_exception (void)
628 static guint8 *start;
629 static int inited = 0;
634 start = get_throw_exception (FALSE);
642 mono_arch_get_rethrow_exception (void)
644 static guint8 *start;
645 static int inited = 0;
650 start = get_throw_exception (TRUE);
658 * mono_arch_get_throw_exception_by_name:
660 * Returns a function pointer which can be used to raise
661 * corlib exceptions. The returned function has the following
662 * signature: void (*func) (gpointer ip, char *exc_name);
663 * For example to raise an arithmetic exception you can use:
665 * x86_push_imm (code, "ArithmeticException");
666 * x86_push_imm (code, <IP>)
667 * x86_jump_code (code, arch_get_throw_exception_by_name ());
671 mono_arch_get_throw_exception_by_name (void)
676 start = code = mono_global_codeman_reserve (32);
679 x86_breakpoint (code);
681 mono_arch_flush_icache (start, code - start);
687 * mono_arch_get_throw_corlib_exception:
689 * Returns a function pointer which can be used to raise
690 * corlib exceptions. The returned function has the following
691 * signature: void (*func) (guint32 ex_token, guint32 offset);
692 * Here, offset is the offset which needs to be substracted from the caller IP
693 * to get the IP of the throw. Passing the offset has the advantage that it
694 * needs no relocations in the caller.
697 mono_arch_get_throw_corlib_exception (void)
699 static guint8* start;
700 static int inited = 0;
702 GSList *unwind_ops = NULL;
708 code = start = mono_global_codeman_reserve (64);
711 * Align the stack on apple, the caller doesn't do this to save space,
712 * two arguments + the return addr are already on the stack.
714 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
715 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4 + 4, 4); /* token */
716 x86_alu_reg_imm (code, X86_ADD, X86_EAX, MONO_TOKEN_TYPE_DEF);
717 /* Align the stack on apple */
718 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
719 x86_push_reg (code, X86_EAX);
720 x86_push_imm (code, mono_defaults.exception_class->image);
721 x86_call_code (code, mono_exception_from_token);
722 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
723 /* Compute caller ip */
724 x86_mov_reg_membase (code, X86_ECX, X86_ESP, 4, 4);
726 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4 + 4 + 4, 4);
728 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4 + 4 + 4 + 4);
729 x86_alu_reg_reg (code, X86_SUB, X86_ECX, X86_EDX);
730 /* Align the stack on apple, mirrors the sub in OP_THROW. */
731 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
732 /* Push exception object */
733 x86_push_reg (code, X86_EAX);
735 x86_push_reg (code, X86_ECX);
736 x86_jump_code (code, mono_arch_get_throw_exception ());
738 g_assert ((code - start) < 64);
740 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
741 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
743 mono_save_trampoline_xdebug_info ("throw_corlib_exception_trampoline", start, code - start, unwind_ops);
749 mono_arch_exceptions_init (void)
753 tramp = get_llvm_throw_exception (FALSE, FALSE);
755 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_exception", NULL, TRUE);
757 tramp = get_llvm_throw_exception (FALSE, TRUE);
759 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
763 * mono_arch_find_jit_info_ext:
765 * See exceptions-amd64.c for docs.
768 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
769 MonoJitInfo *ji, MonoContext *ctx,
770 MonoContext *new_ctx, MonoLMF **lmf,
771 StackFrameInfo *frame)
773 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
775 memset (frame, 0, sizeof (StackFrameInfo));
777 frame->managed = FALSE;
782 gssize regs [MONO_MAX_IREGS + 1];
784 guint32 unwind_info_len;
787 frame->type = FRAME_TYPE_MANAGED;
789 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
790 frame->managed = TRUE;
793 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
795 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
797 regs [X86_EAX] = new_ctx->eax;
798 regs [X86_EBX] = new_ctx->ebx;
799 regs [X86_ECX] = new_ctx->ecx;
800 regs [X86_EDX] = new_ctx->edx;
801 regs [X86_ESP] = new_ctx->esp;
802 regs [X86_EBP] = new_ctx->ebp;
803 regs [X86_ESI] = new_ctx->esi;
804 regs [X86_EDI] = new_ctx->edi;
805 regs [X86_NREG] = new_ctx->eip;
807 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
808 (guint8*)ji->code_start + ji->code_size,
809 ip, regs, MONO_MAX_IREGS + 1, &cfa);
811 new_ctx->eax = regs [X86_EAX];
812 new_ctx->ebx = regs [X86_EBX];
813 new_ctx->ecx = regs [X86_ECX];
814 new_ctx->edx = regs [X86_EDX];
815 new_ctx->esp = regs [X86_ESP];
816 new_ctx->ebp = regs [X86_EBP];
817 new_ctx->esi = regs [X86_ESI];
818 new_ctx->edi = regs [X86_EDI];
819 new_ctx->eip = regs [X86_NREG];
821 /* The CFA becomes the new SP value */
822 new_ctx->esp = (gssize)cfa;
827 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
828 /* remove any unused lmf */
829 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
832 /* Pop arguments off the stack */
834 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
839 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
841 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
842 new_ctx->esp += stack_to_pop;
849 if (((guint64)(*lmf)->previous_lmf) & 2) {
851 * This LMF entry is created by the soft debug code to mark transitions to
852 * managed code done during invokes.
854 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
856 g_assert (ext->debugger_invoke);
858 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
860 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
862 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
867 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
869 if (!((guint32)((*lmf)->previous_lmf) & 1))
872 /* Trampoline lmf frame */
873 frame->method = (*lmf)->method;
876 new_ctx->esi = (*lmf)->esi;
877 new_ctx->edi = (*lmf)->edi;
878 new_ctx->ebx = (*lmf)->ebx;
879 new_ctx->ebp = (*lmf)->ebp;
880 new_ctx->eip = (*lmf)->eip;
883 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
885 /* Check if we are in a trampoline LMF frame */
886 if ((guint32)((*lmf)->previous_lmf) & 1) {
887 /* lmf->esp is set by the trampoline code */
888 new_ctx->esp = (*lmf)->esp;
890 /* Pop arguments off the stack */
891 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
892 /* FIXME: Handle the IMT/vtable case too */
894 if ((*lmf)->method && (*lmf)->method != MONO_FAKE_IMT_METHOD && (*lmf)->method != MONO_FAKE_VTABLE_METHOD) {
895 MonoMethod *method = (*lmf)->method;
896 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
898 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
899 new_ctx->esp += stack_to_pop;
904 /* the lmf is always stored on the stack, so the following
905 * expression points to a stack location which can be used as ESP */
906 new_ctx->esp = (unsigned long)&((*lmf)->eip);
908 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
929 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
931 #ifdef MONO_ARCH_USE_SIGACTION
932 ucontext_t *ctx = (ucontext_t*)sigctx;
934 mctx->eax = UCONTEXT_REG_EAX (ctx);
935 mctx->ebx = UCONTEXT_REG_EBX (ctx);
936 mctx->ecx = UCONTEXT_REG_ECX (ctx);
937 mctx->edx = UCONTEXT_REG_EDX (ctx);
938 mctx->ebp = UCONTEXT_REG_EBP (ctx);
939 mctx->esp = UCONTEXT_REG_ESP (ctx);
940 mctx->esi = UCONTEXT_REG_ESI (ctx);
941 mctx->edi = UCONTEXT_REG_EDI (ctx);
942 mctx->eip = UCONTEXT_REG_EIP (ctx);
944 struct sigcontext *ctx = (struct sigcontext *)sigctx;
946 mctx->eax = ctx->SC_EAX;
947 mctx->ebx = ctx->SC_EBX;
948 mctx->ecx = ctx->SC_ECX;
949 mctx->edx = ctx->SC_EDX;
950 mctx->ebp = ctx->SC_EBP;
951 mctx->esp = ctx->SC_ESP;
952 mctx->esi = ctx->SC_ESI;
953 mctx->edi = ctx->SC_EDI;
954 mctx->eip = ctx->SC_EIP;
959 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
961 #ifdef MONO_ARCH_USE_SIGACTION
962 ucontext_t *ctx = (ucontext_t*)sigctx;
964 UCONTEXT_REG_EAX (ctx) = mctx->eax;
965 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
966 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
967 UCONTEXT_REG_EDX (ctx) = mctx->edx;
968 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
969 UCONTEXT_REG_ESP (ctx) = mctx->esp;
970 UCONTEXT_REG_ESI (ctx) = mctx->esi;
971 UCONTEXT_REG_EDI (ctx) = mctx->edi;
972 UCONTEXT_REG_EIP (ctx) = mctx->eip;
974 struct sigcontext *ctx = (struct sigcontext *)sigctx;
976 ctx->SC_EAX = mctx->eax;
977 ctx->SC_EBX = mctx->ebx;
978 ctx->SC_ECX = mctx->ecx;
979 ctx->SC_EDX = mctx->edx;
980 ctx->SC_EBP = mctx->ebp;
981 ctx->SC_ESP = mctx->esp;
982 ctx->SC_ESI = mctx->esi;
983 ctx->SC_EDI = mctx->edi;
984 ctx->SC_EIP = mctx->eip;
989 mono_arch_ip_from_context (void *sigctx)
991 #ifdef MONO_ARCH_USE_SIGACTION
992 ucontext_t *ctx = (ucontext_t*)sigctx;
993 return (gpointer)UCONTEXT_REG_EIP (ctx);
995 struct sigcontext *ctx = sigctx;
996 return (gpointer)ctx->SC_EIP;
1001 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
1005 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1007 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
1010 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
1012 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
1018 restore_soft_guard_pages (void)
1020 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1021 if (jit_tls->stack_ovf_guard_base)
1022 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1026 * this function modifies mctx so that when it is restored, it
1027 * won't execcute starting at mctx.eip, but in a function that
1028 * will restore the protection on the soft-guard pages and return back to
1029 * continue at mctx.eip.
1032 prepare_for_guard_pages (MonoContext *mctx)
1035 sp = (gpointer)(mctx->esp);
1037 /* the resturn addr */
1038 sp [0] = (gpointer)(mctx->eip);
1039 mctx->eip = (unsigned long)restore_soft_guard_pages;
1040 mctx->esp = (unsigned long)sp;
1044 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
1046 void (*restore_context) (MonoContext *);
1049 restore_context = mono_arch_get_restore_context ();
1050 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1052 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
1054 prepare_for_guard_pages (&mctx);
1055 restore_context (&mctx);
1058 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
1060 prepare_for_guard_pages (&mctx);
1061 restore_context (&mctx);
1065 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
1067 #ifdef MONO_ARCH_USE_SIGACTION
1068 MonoException *exc = NULL;
1069 ucontext_t *ctx = (ucontext_t*)sigctx;
1070 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1074 /* if we didn't find a managed method for the ip address and it matches the fault
1075 * address, we assume we followed a broken pointer during an indirect call, so
1076 * we try the lookup again with the return address pushed on the stack
1078 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1079 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1080 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1082 UCONTEXT_REG_EIP (ctx) = sp [0];
1085 exc = mono_domain_get ()->stack_overflow_ex;
1087 mono_handle_native_sigsegv (SIGSEGV, sigctx);
1088 /* setup a call frame on the real stack so that control is returned there
1089 * and exception handling can continue.
1090 * If this was a stack overflow the caller already ensured the stack pages
1091 * needed have been unprotected.
1092 * The frame looks like:
1099 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
1102 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1103 sp = (gpointer)((char*)sp - frame_size);
1104 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1107 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1110 sp [2] = (gpointer)stack_ovf;
1111 /* may need to adjust pointers in the new struct copy, depending on the OS */
1112 memcpy (sp + 4, ctx, sizeof (ucontext_t));
1113 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1114 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1115 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1119 #if MONO_SUPPORT_TASKLETS
1120 MonoContinuationRestore
1121 mono_tasklets_arch_restore (void)
1123 static guint8* saved = NULL;
1124 guint8 *code, *start;
1127 return (MonoContinuationRestore)saved;
1128 code = start = mono_global_codeman_reserve (48);
1129 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1130 /* put cont in edx */
1131 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1132 /* setup the copy of the stack */
1133 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1134 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1136 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1137 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1138 x86_prefix (code, X86_REP_PREFIX);
1141 /* now restore the registers from the LMF */
1142 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1143 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1144 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1145 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1146 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1148 /* restore the lmf chain */
1149 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1150 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1152 /* state in eax, so it's setup as the return value */
1153 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1154 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1155 g_assert ((code - start) <= 48);
1157 return (MonoContinuationRestore)saved;