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"
30 static gpointer signal_exception_trampoline;
33 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
36 static void (*restore_stack) (void *);
38 static MonoW32ExceptionHandler fpe_handler;
39 static MonoW32ExceptionHandler ill_handler;
40 static MonoW32ExceptionHandler segv_handler;
42 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
44 #define W32_SEH_HANDLE_EX(_ex) \
45 if (_ex##_handler) _ex##_handler(0, er, sctx)
48 * mono_win32_get_handle_stackoverflow (void):
50 * Returns a pointer to a method which restores the current context stack
51 * and calls handle_exceptions, when done restores the original stack.
54 mono_win32_get_handle_stackoverflow (void)
56 static guint8 *start = NULL;
62 /* restore_contect (void *sigctx) */
63 start = code = mono_global_codeman_reserve (128);
65 /* load context into ebx */
66 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
68 /* move current stack into edi for later restore */
69 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
71 /* use the new freed stack from sigcontext */
72 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
74 /* get the current domain */
75 x86_call_code (code, mono_domain_get);
77 /* get stack overflow exception from domain object */
78 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
80 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
81 x86_push_imm (code, 0);
82 x86_push_reg (code, X86_EAX);
83 x86_push_reg (code, X86_EBX);
84 x86_call_code (code, mono_arch_handle_exception);
86 /* restore the SEH handler stack */
87 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
95 /* Special hack to workaround the fact that the
96 * when the SEH handler is called the stack is
97 * to small to recover.
99 * Stack walking part of this method is from mono_handle_exception
101 * The idea is simple;
102 * - walk the stack to free some space (64k)
103 * - set esp to new stack location
104 * - call mono_arch_handle_exception with stack overflow exception
105 * - set esp to SEH handlers stack
109 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
113 MonoDomain *domain = mono_domain_get ();
115 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
116 MonoLMF *lmf = jit_tls->lmf;
117 MonoContext initial_ctx;
119 guint32 free_stack = 0;
120 StackFrameInfo frame;
122 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
123 mono_arch_sigctx_to_monoctx (sctx, &ctx);
125 /* get our os page size */
127 page_size = si.dwPageSize;
129 /* Let's walk the stack to recover
130 * the needed stack space (if possible)
132 memset (&rji, 0, sizeof (rji));
135 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
137 /* try to free 64kb from our stack */
141 mono_arch_find_jit_info_ext (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, &frame);
143 g_warning ("Exception inside function without unwind info");
144 g_assert_not_reached ();
147 if (frame.ji != (gpointer)-1) {
148 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
151 /* todo: we should call abort if ji is -1 */
153 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
155 /* convert into sigcontext to be used in mono_arch_handle_exception */
156 mono_arch_monoctx_to_sigctx (&ctx, sctx);
158 /* todo: install new stack-guard page */
160 /* use the new stack and call mono_arch_handle_exception () */
161 restore_stack (sctx);
165 * Unhandled Exception Filter
166 * Top-level per-process exception handler.
168 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
170 EXCEPTION_RECORD* er;
172 struct sigcontext* sctx;
175 res = EXCEPTION_CONTINUE_EXECUTION;
177 er = ep->ExceptionRecord;
178 ctx = ep->ContextRecord;
179 sctx = g_malloc(sizeof(struct sigcontext));
181 /* Copy Win32 context to UNIX style context */
182 sctx->eax = ctx->Eax;
183 sctx->ebx = ctx->Ebx;
184 sctx->ecx = ctx->Ecx;
185 sctx->edx = ctx->Edx;
186 sctx->ebp = ctx->Ebp;
187 sctx->esp = ctx->Esp;
188 sctx->esi = ctx->Esi;
189 sctx->edi = ctx->Edi;
190 sctx->eip = ctx->Eip;
192 switch (er->ExceptionCode) {
193 case EXCEPTION_STACK_OVERFLOW:
194 win32_handle_stack_overflow (ep, sctx);
196 case EXCEPTION_ACCESS_VIOLATION:
197 W32_SEH_HANDLE_EX(segv);
199 case EXCEPTION_ILLEGAL_INSTRUCTION:
200 W32_SEH_HANDLE_EX(ill);
202 case EXCEPTION_INT_DIVIDE_BY_ZERO:
203 case EXCEPTION_INT_OVERFLOW:
204 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
205 case EXCEPTION_FLT_OVERFLOW:
206 case EXCEPTION_FLT_UNDERFLOW:
207 case EXCEPTION_FLT_INEXACT_RESULT:
208 W32_SEH_HANDLE_EX(fpe);
214 /* Copy context back */
215 ctx->Eax = sctx->eax;
216 ctx->Ebx = sctx->ebx;
217 ctx->Ecx = sctx->ecx;
218 ctx->Edx = sctx->edx;
219 ctx->Ebp = sctx->ebp;
220 ctx->Esp = sctx->esp;
221 ctx->Esi = sctx->esi;
222 ctx->Edi = sctx->edi;
223 ctx->Eip = sctx->eip;
230 void win32_seh_init()
232 /* install restore stack helper */
234 restore_stack = mono_win32_get_handle_stackoverflow ();
236 old_handler = SetUnhandledExceptionFilter(seh_handler);
239 void win32_seh_cleanup()
241 if (old_handler) SetUnhandledExceptionFilter(old_handler);
244 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
248 fpe_handler = handler;
251 ill_handler = handler;
254 segv_handler = handler;
261 #endif /* TARGET_WIN32 */
264 * mono_arch_get_restore_context:
266 * Returns a pointer to a method which restores a previously saved sigcontext.
269 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
271 guint8 *start = NULL;
273 MonoJumpInfo *ji = NULL;
274 GSList *unwind_ops = NULL;
276 /* restore_contect (MonoContext *ctx) */
278 start = code = mono_global_codeman_reserve (128);
281 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
283 /* get return address, stored in ECX */
284 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
286 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
288 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
290 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
292 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
293 /* save the return addr to the restored stack */
294 x86_push_reg (code, X86_ECX);
296 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
298 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ecx), 4);
300 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, edx), 4);
302 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eax), 4);
304 /* jump to the saved IP */
308 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
314 * mono_arch_get_call_filter:
316 * Returns a pointer to a method which calls an exception filter. We
317 * also use this function to call finally handlers (we pass NULL as
318 * @exc object in this case).
321 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
325 MonoJumpInfo *ji = NULL;
326 GSList *unwind_ops = NULL;
328 /* call_filter (MonoContext *ctx, unsigned long eip) */
329 start = code = mono_global_codeman_reserve (64);
331 x86_push_reg (code, X86_EBP);
332 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
333 x86_push_reg (code, X86_EBX);
334 x86_push_reg (code, X86_EDI);
335 x86_push_reg (code, X86_ESI);
338 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
340 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
342 x86_push_reg (code, X86_EBP);
345 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
346 /* restore registers used by global register allocation (EBX & ESI) */
347 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
348 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
349 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
351 /* align stack and save ESP */
352 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
353 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
354 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
355 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
356 x86_push_reg (code, X86_EDX);
358 /* call the handler */
359 x86_call_reg (code, X86_ECX);
362 x86_pop_reg (code, X86_ESP);
365 x86_pop_reg (code, X86_EBP);
367 /* restore saved regs */
368 x86_pop_reg (code, X86_ESI);
369 x86_pop_reg (code, X86_EDI);
370 x86_pop_reg (code, X86_EBX);
375 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
377 g_assert ((code - start) < 64);
382 * mono_x86_throw_exception:
384 * C function called from the throw trampolines.
387 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
388 mgreg_t eip, gboolean rethrow)
390 static void (*restore_context) (MonoContext *);
393 if (!restore_context)
394 restore_context = mono_get_restore_context ();
396 ctx.esp = regs [X86_ESP];
398 ctx.ebp = regs [X86_EBP];
399 ctx.edi = regs [X86_EDI];
400 ctx.esi = regs [X86_ESI];
401 ctx.ebx = regs [X86_EBX];
402 ctx.edx = regs [X86_EDX];
403 ctx.ecx = regs [X86_ECX];
404 ctx.eax = regs [X86_EAX];
407 /* The OSX ABI specifies 16 byte alignment at call sites */
408 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
411 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
412 MonoException *mono_ex = (MonoException*)exc;
414 mono_ex->stack_trace = NULL;
417 if (mono_debug_using_mono_debugger ()) {
418 guint8 buf [16], *code;
420 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
423 if (buf [3] == 0xe8) {
424 MonoContext ctx_cp = ctx;
425 ctx_cp.eip = eip - 5;
427 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
428 restore_context (&ctx_cp);
429 g_assert_not_reached ();
434 /* adjust eip so that it point into the call instruction */
437 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
439 restore_context (&ctx);
441 g_assert_not_reached ();
445 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
446 mgreg_t eip, gint32 pc_offset)
448 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
451 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
455 /* Negate the ip adjustment done in mono_x86_throw_exception () */
458 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
462 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc,
463 mgreg_t eip, gboolean rethrow)
467 ctx.esp = regs [X86_ESP];
469 ctx.ebp = regs [X86_EBP];
470 ctx.edi = regs [X86_EDI];
471 ctx.esi = regs [X86_ESI];
472 ctx.ebx = regs [X86_EBX];
473 ctx.edx = regs [X86_EDX];
474 ctx.ecx = regs [X86_ECX];
475 ctx.eax = regs [X86_EAX];
477 mono_resume_unwind (&ctx);
481 * get_throw_trampoline:
483 * Generate a call to mono_x86_throw_exception/
484 * mono_x86_throw_corlib_exception.
485 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
486 * which doesn't push the arguments.
489 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
491 guint8 *start, *code;
492 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
493 MonoJumpInfo *ji = NULL;
494 GSList *unwind_ops = NULL;
496 start = code = mono_global_codeman_reserve (128);
501 * On apple, the stack is misaligned by the pushing of the return address.
504 /* On OSX, we don't generate alignment code to save space */
507 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
510 * The stack looks like this:
511 * <pc offset> (only if corlib is TRUE)
512 * <exception object>/<type token>
513 * <return addr> <- esp (unaligned on apple)
516 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
517 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
520 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
521 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
526 arg_offsets [3] = 12;
530 for (i = 0; i < X86_NREG; ++i)
532 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
533 /* Calculate the offset between the current sp and the sp of the caller */
535 /* LLVM doesn't push the arguments */
536 stack_offset = stack_size + 4;
540 stack_offset = stack_size + 4 + 8;
542 /* We don't generate stack alignment code on osx to save space */
546 stack_offset = stack_size + 4 + 4;
548 /* Pop the alignment added by OP_THROW too */
549 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
554 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
555 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
557 /* Set arg1 == regs */
558 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
559 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
560 /* Set arg2 == exc/ex_token_index */
562 x86_mov_reg_imm (code, X86_EAX, 0);
564 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
565 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
566 /* Set arg3 == eip */
568 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
570 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
571 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
572 /* Set arg4 == rethrow/pc_offset */
574 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
576 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
579 * The caller is LLVM code which passes the absolute address not a pc offset,
580 * so compensate by passing 0 as 'ip' and passing the negated abs address as
583 x86_neg_reg (code, X86_EAX);
585 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
587 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
591 // This can be called from runtime code, which can't guarantee that
592 // ebx contains the got address.
593 // So emit the got address loading code too
594 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
595 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? "mono_x86_throw_corlib_exception" : "mono_x86_throw_exception");
596 x86_call_reg (code, X86_EAX);
598 x86_call_code (code, resume_unwind ? (mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
600 x86_breakpoint (code);
602 g_assert ((code - start) < 128);
604 mono_save_trampoline_xdebug_info (name, start, code - start, unwind_ops);
607 *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
613 * mono_arch_get_throw_exception:
615 * Returns a function pointer which can be used to raise
616 * exceptions. The returned function has the following
617 * signature: void (*func) (MonoException *exc);
618 * For example to raise an arithmetic exception you can use:
620 * x86_push_imm (code, mono_get_exception_arithmetic ());
621 * x86_call_code (code, arch_get_throw_exception ());
625 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
627 return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
631 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
633 return get_throw_trampoline ("rethow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
637 * mono_arch_get_throw_corlib_exception:
639 * Returns a function pointer which can be used to raise
640 * corlib exceptions. The returned function has the following
641 * signature: void (*func) (guint32 ex_token, guint32 offset);
642 * Here, offset is the offset which needs to be substracted from the caller IP
643 * to get the IP of the throw. Passing the offset has the advantage that it
644 * needs no relocations in the caller.
647 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
649 return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
653 mono_arch_exceptions_init (void)
658 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
662 /* LLVM needs different throw trampolines */
663 tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
664 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_exception", NULL, TRUE);
666 tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
667 mono_register_jit_icall (tramp, "mono_arch_llvm_rethrow_exception", NULL, TRUE);
669 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
670 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
672 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline_abs", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
673 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception_abs", NULL, TRUE);
675 tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
676 mono_register_jit_icall (tramp, "mono_llvm_resume_unwind_trampoline", NULL, TRUE);
678 signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
682 * mono_arch_find_jit_info_ext:
684 * See exceptions-amd64.c for docs.
687 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
688 MonoJitInfo *ji, MonoContext *ctx,
689 MonoContext *new_ctx, MonoLMF **lmf,
690 StackFrameInfo *frame)
692 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
694 memset (frame, 0, sizeof (StackFrameInfo));
696 frame->managed = FALSE;
701 gssize regs [MONO_MAX_IREGS + 1];
703 guint32 unwind_info_len;
706 frame->type = FRAME_TYPE_MANAGED;
708 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
709 frame->managed = TRUE;
712 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
714 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
716 regs [X86_EAX] = new_ctx->eax;
717 regs [X86_EBX] = new_ctx->ebx;
718 regs [X86_ECX] = new_ctx->ecx;
719 regs [X86_EDX] = new_ctx->edx;
720 regs [X86_ESP] = new_ctx->esp;
721 regs [X86_EBP] = new_ctx->ebp;
722 regs [X86_ESI] = new_ctx->esi;
723 regs [X86_EDI] = new_ctx->edi;
724 regs [X86_NREG] = new_ctx->eip;
726 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
727 (guint8*)ji->code_start + ji->code_size,
728 ip, regs, MONO_MAX_IREGS + 1, &cfa);
730 new_ctx->eax = regs [X86_EAX];
731 new_ctx->ebx = regs [X86_EBX];
732 new_ctx->ecx = regs [X86_ECX];
733 new_ctx->edx = regs [X86_EDX];
734 new_ctx->esp = regs [X86_ESP];
735 new_ctx->ebp = regs [X86_EBP];
736 new_ctx->esi = regs [X86_ESI];
737 new_ctx->edi = regs [X86_EDI];
738 new_ctx->eip = regs [X86_NREG];
740 /* The CFA becomes the new SP value */
741 new_ctx->esp = (gssize)cfa;
746 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
747 /* remove any unused lmf */
748 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
751 /* Pop arguments off the stack */
753 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
758 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
760 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
761 new_ctx->esp += stack_to_pop;
768 if (((guint64)(*lmf)->previous_lmf) & 2) {
770 * This LMF entry is created by the soft debug code to mark transitions to
771 * managed code done during invokes.
773 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
775 g_assert (ext->debugger_invoke);
777 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
779 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
781 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
786 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
788 if (!((guint32)((*lmf)->previous_lmf) & 1))
791 /* Trampoline lmf frame */
792 frame->method = (*lmf)->method;
795 new_ctx->esi = (*lmf)->esi;
796 new_ctx->edi = (*lmf)->edi;
797 new_ctx->ebx = (*lmf)->ebx;
798 new_ctx->ebp = (*lmf)->ebp;
799 new_ctx->eip = (*lmf)->eip;
805 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
807 /* Check if we are in a trampoline LMF frame */
808 if ((guint32)((*lmf)->previous_lmf) & 1) {
809 /* lmf->esp is set by the trampoline code */
810 new_ctx->esp = (*lmf)->esp;
812 /* Pop arguments off the stack */
813 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
814 /* FIXME: Handle the IMT/vtable case too */
816 if ((*lmf)->method) {
817 MonoMethod *method = (*lmf)->method;
818 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
820 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
821 new_ctx->esp += stack_to_pop;
826 /* the lmf is always stored on the stack, so the following
827 * expression points to a stack location which can be used as ESP */
828 new_ctx->esp = (unsigned long)&((*lmf)->eip);
830 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
851 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
853 #ifdef MONO_ARCH_USE_SIGACTION
854 ucontext_t *ctx = (ucontext_t*)sigctx;
856 mctx->eax = UCONTEXT_REG_EAX (ctx);
857 mctx->ebx = UCONTEXT_REG_EBX (ctx);
858 mctx->ecx = UCONTEXT_REG_ECX (ctx);
859 mctx->edx = UCONTEXT_REG_EDX (ctx);
860 mctx->ebp = UCONTEXT_REG_EBP (ctx);
861 mctx->esp = UCONTEXT_REG_ESP (ctx);
862 mctx->esi = UCONTEXT_REG_ESI (ctx);
863 mctx->edi = UCONTEXT_REG_EDI (ctx);
864 mctx->eip = UCONTEXT_REG_EIP (ctx);
866 struct sigcontext *ctx = (struct sigcontext *)sigctx;
868 mctx->eax = ctx->SC_EAX;
869 mctx->ebx = ctx->SC_EBX;
870 mctx->ecx = ctx->SC_ECX;
871 mctx->edx = ctx->SC_EDX;
872 mctx->ebp = ctx->SC_EBP;
873 mctx->esp = ctx->SC_ESP;
874 mctx->esi = ctx->SC_ESI;
875 mctx->edi = ctx->SC_EDI;
876 mctx->eip = ctx->SC_EIP;
881 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
883 #ifdef MONO_ARCH_USE_SIGACTION
884 ucontext_t *ctx = (ucontext_t*)sigctx;
886 UCONTEXT_REG_EAX (ctx) = mctx->eax;
887 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
888 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
889 UCONTEXT_REG_EDX (ctx) = mctx->edx;
890 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
891 UCONTEXT_REG_ESP (ctx) = mctx->esp;
892 UCONTEXT_REG_ESI (ctx) = mctx->esi;
893 UCONTEXT_REG_EDI (ctx) = mctx->edi;
894 UCONTEXT_REG_EIP (ctx) = mctx->eip;
896 struct sigcontext *ctx = (struct sigcontext *)sigctx;
898 ctx->SC_EAX = mctx->eax;
899 ctx->SC_EBX = mctx->ebx;
900 ctx->SC_ECX = mctx->ecx;
901 ctx->SC_EDX = mctx->edx;
902 ctx->SC_EBP = mctx->ebp;
903 ctx->SC_ESP = mctx->esp;
904 ctx->SC_ESI = mctx->esi;
905 ctx->SC_EDI = mctx->edi;
906 ctx->SC_EIP = mctx->eip;
911 mono_arch_ip_from_context (void *sigctx)
913 #ifdef MONO_ARCH_USE_SIGACTION
914 ucontext_t *ctx = (ucontext_t*)sigctx;
915 return (gpointer)UCONTEXT_REG_EIP (ctx);
917 struct sigcontext *ctx = sigctx;
918 return (gpointer)ctx->SC_EIP;
925 * Called by resuming from a signal handler.
928 handle_signal_exception (gpointer obj)
930 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
932 static void (*restore_context) (MonoContext *);
934 if (!restore_context)
935 restore_context = mono_get_restore_context ();
937 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
939 if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
942 mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), FALSE);
944 restore_context (&ctx);
948 * mono_x86_get_signal_exception_trampoline:
950 * This x86 specific trampoline is used to call handle_signal_exception.
953 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
955 guint8 *start, *code;
956 MonoJumpInfo *ji = NULL;
957 GSList *unwind_ops = NULL;
960 start = code = mono_global_codeman_reserve (128);
963 x86_push_reg (code, X86_ECX);
965 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
966 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
968 /* Fix the alignment to be what apple expects */
971 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
972 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
975 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
976 /* Branch to target */
977 x86_call_reg (code, X86_EDX);
979 g_assert ((code - start) < 128);
981 mono_save_trampoline_xdebug_info ("x86_signal_exception_trampoline", start, code - start, unwind_ops);
984 *info = mono_tramp_info_create (g_strdup ("x86_signal_exception_trampoline"), start, code - start, ji, unwind_ops);
990 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
992 #if defined(MONO_ARCH_USE_SIGACTION)
994 * Handling the exception in the signal handler is problematic, since the original
995 * signal is disabled, and we could run arbitrary code though the debugger. So
996 * resume into the normal stack and do most work there if possible.
998 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
999 guint64 sp = UCONTEXT_REG_ESP (sigctx);
1001 /* Pass the ctx parameter in TLS */
1002 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1004 * Can't pass the obj on the stack, since we are executing on the
1005 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1006 * So put it into a register, and branch to a trampoline which
1009 g_assert (!test_only);
1010 UCONTEXT_REG_EAX (sigctx) = (gsize)obj;
1011 UCONTEXT_REG_ECX (sigctx) = UCONTEXT_REG_EIP (sigctx);
1012 UCONTEXT_REG_EDX (sigctx) = (gsize)handle_signal_exception;
1014 /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
1017 UCONTEXT_REG_ESP (sigctx) = sp;
1019 UCONTEXT_REG_EIP (sigctx) = (gsize)signal_exception_trampoline;
1022 #elif defined (TARGET_WIN32)
1023 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1024 struct sigcontext *ctx = (struct sigcontext *)sigctx;
1025 guint64 sp = ctx->SC_ESP;
1027 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1030 * Can't pass the obj on the stack, since we are executing on the
1031 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1032 * So put it into a register, and branch to a trampoline which
1035 g_assert (!test_only);
1036 ctx->SC_EAX = (gsize)obj;
1037 ctx->SC_ECX = ctx->SC_EIP;
1038 ctx->SC_EDX = (gsize)handle_signal_exception;
1040 /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
1045 ctx->SC_EIP = (gsize)signal_exception_trampoline;
1051 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1053 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
1056 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
1058 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
1065 restore_soft_guard_pages (void)
1067 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1068 if (jit_tls->stack_ovf_guard_base)
1069 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1073 * this function modifies mctx so that when it is restored, it
1074 * won't execcute starting at mctx.eip, but in a function that
1075 * will restore the protection on the soft-guard pages and return back to
1076 * continue at mctx.eip.
1079 prepare_for_guard_pages (MonoContext *mctx)
1082 sp = (gpointer)(mctx->esp);
1084 /* the resturn addr */
1085 sp [0] = (gpointer)(mctx->eip);
1086 mctx->eip = (unsigned long)restore_soft_guard_pages;
1087 mctx->esp = (unsigned long)sp;
1091 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
1093 void (*restore_context) (MonoContext *);
1096 restore_context = mono_get_restore_context ();
1097 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1099 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
1101 prepare_for_guard_pages (&mctx);
1102 restore_context (&mctx);
1105 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
1107 prepare_for_guard_pages (&mctx);
1108 restore_context (&mctx);
1112 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
1114 #ifdef MONO_ARCH_USE_SIGACTION
1115 MonoException *exc = NULL;
1116 ucontext_t *ctx = (ucontext_t*)sigctx;
1117 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1121 /* if we didn't find a managed method for the ip address and it matches the fault
1122 * address, we assume we followed a broken pointer during an indirect call, so
1123 * we try the lookup again with the return address pushed on the stack
1125 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1126 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1127 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1129 UCONTEXT_REG_EIP (ctx) = sp [0];
1132 exc = mono_domain_get ()->stack_overflow_ex;
1134 mono_handle_native_sigsegv (SIGSEGV, sigctx);
1135 /* setup a call frame on the real stack so that control is returned there
1136 * and exception handling can continue.
1137 * If this was a stack overflow the caller already ensured the stack pages
1138 * needed have been unprotected.
1139 * The frame looks like:
1146 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
1149 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1150 sp = (gpointer)((char*)sp - frame_size);
1151 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1154 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1157 sp [2] = (gpointer)stack_ovf;
1158 /* may need to adjust pointers in the new struct copy, depending on the OS */
1159 memcpy (sp + 4, ctx, sizeof (ucontext_t));
1160 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1161 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1162 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1166 #if MONO_SUPPORT_TASKLETS
1167 MonoContinuationRestore
1168 mono_tasklets_arch_restore (void)
1170 static guint8* saved = NULL;
1171 guint8 *code, *start;
1174 return (MonoContinuationRestore)saved;
1175 code = start = mono_global_codeman_reserve (48);
1176 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1177 /* put cont in edx */
1178 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1179 /* setup the copy of the stack */
1180 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1181 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1183 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1184 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1185 x86_prefix (code, X86_REP_PREFIX);
1188 /* now restore the registers from the LMF */
1189 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1190 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1191 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1192 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1193 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1195 /* restore the lmf chain */
1196 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1197 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1199 /* state in eax, so it's setup as the return value */
1200 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1201 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1202 g_assert ((code - start) <= 48);
1204 return (MonoContinuationRestore)saved;