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 #ifndef PROCESS_CALLBACK_FILTER_ENABLED
45 # define PROCESS_CALLBACK_FILTER_ENABLED 1
48 #define W32_SEH_HANDLE_EX(_ex) \
49 if (_ex##_handler) _ex##_handler(0, er, sctx)
52 * mono_win32_get_handle_stackoverflow (void):
54 * Returns a pointer to a method which restores the current context stack
55 * and calls handle_exceptions, when done restores the original stack.
58 mono_win32_get_handle_stackoverflow (void)
60 static guint8 *start = NULL;
66 /* restore_contect (void *sigctx) */
67 start = code = mono_global_codeman_reserve (128);
69 /* load context into ebx */
70 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
72 /* move current stack into edi for later restore */
73 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
75 /* use the new freed stack from sigcontext */
76 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
78 /* get the current domain */
79 x86_call_code (code, mono_domain_get);
81 /* get stack overflow exception from domain object */
82 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
84 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
85 x86_push_imm (code, 0);
86 x86_push_reg (code, X86_EAX);
87 x86_push_reg (code, X86_EBX);
88 x86_call_code (code, mono_arch_handle_exception);
90 /* restore the SEH handler stack */
91 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
99 /* Special hack to workaround the fact that the
100 * when the SEH handler is called the stack is
101 * to small to recover.
103 * Stack walking part of this method is from mono_handle_exception
105 * The idea is simple;
106 * - walk the stack to free some space (64k)
107 * - set esp to new stack location
108 * - call mono_arch_handle_exception with stack overflow exception
109 * - set esp to SEH handlers stack
113 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
117 MonoDomain *domain = mono_domain_get ();
119 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
120 MonoLMF *lmf = jit_tls->lmf;
121 MonoContext initial_ctx;
123 guint32 free_stack = 0;
124 StackFrameInfo frame;
126 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
127 mono_arch_sigctx_to_monoctx (sctx, &ctx);
129 /* get our os page size */
131 page_size = si.dwPageSize;
133 /* Let's walk the stack to recover
134 * the needed stack space (if possible)
136 memset (&rji, 0, sizeof (rji));
139 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
141 /* try to free 64kb from our stack */
145 mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, NULL, &frame);
147 g_warning ("Exception inside function without unwind info");
148 g_assert_not_reached ();
151 if (frame.ji != (gpointer)-1) {
152 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
155 /* todo: we should call abort if ji is -1 */
157 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
159 /* convert into sigcontext to be used in mono_arch_handle_exception */
160 mono_arch_monoctx_to_sigctx (&ctx, sctx);
162 /* todo: install new stack-guard page */
164 /* use the new stack and call mono_arch_handle_exception () */
165 restore_stack (sctx);
169 * Unhandled Exception Filter
170 * Top-level per-process exception handler.
172 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
174 EXCEPTION_RECORD* er;
176 struct sigcontext* sctx;
179 res = EXCEPTION_CONTINUE_EXECUTION;
181 er = ep->ExceptionRecord;
182 ctx = ep->ContextRecord;
183 sctx = g_malloc(sizeof(struct sigcontext));
185 /* Copy Win32 context to UNIX style context */
186 sctx->eax = ctx->Eax;
187 sctx->ebx = ctx->Ebx;
188 sctx->ecx = ctx->Ecx;
189 sctx->edx = ctx->Edx;
190 sctx->ebp = ctx->Ebp;
191 sctx->esp = ctx->Esp;
192 sctx->esi = ctx->Esi;
193 sctx->edi = ctx->Edi;
194 sctx->eip = ctx->Eip;
196 switch (er->ExceptionCode) {
197 case EXCEPTION_STACK_OVERFLOW:
198 win32_handle_stack_overflow (ep, sctx);
200 case EXCEPTION_ACCESS_VIOLATION:
201 W32_SEH_HANDLE_EX(segv);
203 case EXCEPTION_ILLEGAL_INSTRUCTION:
204 W32_SEH_HANDLE_EX(ill);
206 case EXCEPTION_INT_DIVIDE_BY_ZERO:
207 case EXCEPTION_INT_OVERFLOW:
208 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
209 case EXCEPTION_FLT_OVERFLOW:
210 case EXCEPTION_FLT_UNDERFLOW:
211 case EXCEPTION_FLT_INEXACT_RESULT:
212 W32_SEH_HANDLE_EX(fpe);
218 /* Copy context back */
219 ctx->Eax = sctx->eax;
220 ctx->Ebx = sctx->ebx;
221 ctx->Ecx = sctx->ecx;
222 ctx->Edx = sctx->edx;
223 ctx->Ebp = sctx->ebp;
224 ctx->Esp = sctx->esp;
225 ctx->Esi = sctx->esi;
226 ctx->Edi = sctx->edi;
227 ctx->Eip = sctx->eip;
234 void win32_seh_init()
236 /* install restore stack helper */
238 restore_stack = mono_win32_get_handle_stackoverflow ();
240 old_handler = SetUnhandledExceptionFilter(seh_handler);
243 void win32_seh_cleanup()
245 if (old_handler) SetUnhandledExceptionFilter(old_handler);
248 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
252 fpe_handler = handler;
255 ill_handler = handler;
258 segv_handler = handler;
265 #endif /* TARGET_WIN32 */
268 * mono_arch_get_restore_context:
270 * Returns a pointer to a method which restores a previously saved sigcontext.
273 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
275 guint8 *start = NULL;
277 MonoJumpInfo *ji = NULL;
278 GSList *unwind_ops = NULL;
280 /* restore_contect (MonoContext *ctx) */
282 start = code = mono_global_codeman_reserve (128);
285 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
287 /* get return address, stored in ECX */
288 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
290 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
292 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
294 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
296 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
297 /* save the return addr to the restored stack */
298 x86_push_reg (code, X86_ECX);
300 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
302 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ecx), 4);
304 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, edx), 4);
306 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eax), 4);
308 /* jump to the saved IP */
311 nacl_global_codeman_validate(&start, 128, &code);
314 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
318 for (l = unwind_ops; l; l = l->next)
320 g_slist_free (unwind_ops);
327 * mono_arch_get_call_filter:
329 * Returns a pointer to a method which calls an exception filter. We
330 * also use this function to call finally handlers (we pass NULL as
331 * @exc object in this case).
334 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
338 MonoJumpInfo *ji = NULL;
339 GSList *unwind_ops = NULL;
340 guint kMaxCodeSize = NACL_SIZE (64, 128);
342 /* call_filter (MonoContext *ctx, unsigned long eip) */
343 start = code = mono_global_codeman_reserve (kMaxCodeSize);
345 x86_push_reg (code, X86_EBP);
346 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
347 x86_push_reg (code, X86_EBX);
348 x86_push_reg (code, X86_EDI);
349 x86_push_reg (code, X86_ESI);
352 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
354 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
356 x86_push_reg (code, X86_EBP);
359 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
360 /* restore registers used by global register allocation (EBX & ESI) */
361 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
362 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
363 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
365 /* align stack and save ESP */
366 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
367 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
368 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
369 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
370 x86_push_reg (code, X86_EDX);
372 /* call the handler */
373 x86_call_reg (code, X86_ECX);
376 x86_pop_reg (code, X86_ESP);
379 x86_pop_reg (code, X86_EBP);
381 /* restore saved regs */
382 x86_pop_reg (code, X86_ESI);
383 x86_pop_reg (code, X86_EDI);
384 x86_pop_reg (code, X86_EBX);
388 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
391 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
395 for (l = unwind_ops; l; l = l->next)
397 g_slist_free (unwind_ops);
400 g_assert ((code - start) < kMaxCodeSize);
405 * mono_x86_throw_exception:
407 * C function called from the throw trampolines.
410 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
411 mgreg_t eip, gboolean rethrow)
413 static void (*restore_context) (MonoContext *);
416 if (!restore_context)
417 restore_context = mono_get_restore_context ();
419 ctx.esp = regs [X86_ESP];
421 ctx.ebp = regs [X86_EBP];
422 ctx.edi = regs [X86_EDI];
423 ctx.esi = regs [X86_ESI];
424 ctx.ebx = regs [X86_EBX];
425 ctx.edx = regs [X86_EDX];
426 ctx.ecx = regs [X86_ECX];
427 ctx.eax = regs [X86_EAX];
430 /* The OSX ABI specifies 16 byte alignment at call sites */
431 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
434 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
435 MonoException *mono_ex = (MonoException*)exc;
437 mono_ex->stack_trace = NULL;
440 if (mono_debug_using_mono_debugger ()) {
441 guint8 buf [16], *code;
443 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
446 if (buf [3] == 0xe8) {
447 MonoContext ctx_cp = ctx;
448 ctx_cp.eip = eip - 5;
450 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
451 restore_context (&ctx_cp);
452 g_assert_not_reached ();
457 /* adjust eip so that it point into the call instruction */
460 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
462 restore_context (&ctx);
464 g_assert_not_reached ();
468 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
469 mgreg_t eip, gint32 pc_offset)
471 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
474 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
478 /* Negate the ip adjustment done in mono_x86_throw_exception () */
481 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
485 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc,
486 mgreg_t eip, gboolean rethrow)
490 ctx.esp = regs [X86_ESP];
492 ctx.ebp = regs [X86_EBP];
493 ctx.edi = regs [X86_EDI];
494 ctx.esi = regs [X86_ESI];
495 ctx.ebx = regs [X86_EBX];
496 ctx.edx = regs [X86_EDX];
497 ctx.ecx = regs [X86_ECX];
498 ctx.eax = regs [X86_EAX];
500 mono_resume_unwind (&ctx);
504 * get_throw_trampoline:
506 * Generate a call to mono_x86_throw_exception/
507 * mono_x86_throw_corlib_exception.
508 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
509 * which doesn't push the arguments.
512 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
514 guint8 *start, *code;
515 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
516 MonoJumpInfo *ji = NULL;
517 GSList *unwind_ops = NULL;
518 guint kMaxCodeSize = NACL_SIZE (128, 256);
520 start = code = mono_global_codeman_reserve (kMaxCodeSize);
525 * On apple, the stack is misaligned by the pushing of the return address.
528 /* On OSX, we don't generate alignment code to save space */
531 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
534 * The stack looks like this:
535 * <pc offset> (only if corlib is TRUE)
536 * <exception object>/<type token>
537 * <return addr> <- esp (unaligned on apple)
540 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
541 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
544 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
545 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
550 arg_offsets [3] = 12;
554 for (i = 0; i < X86_NREG; ++i)
556 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
557 /* Calculate the offset between the current sp and the sp of the caller */
559 /* LLVM doesn't push the arguments */
560 stack_offset = stack_size + 4;
564 stack_offset = stack_size + 4 + 8;
566 /* We don't generate stack alignment code on osx to save space */
569 /* One argument + stack alignment */
570 stack_offset = stack_size + 4 + 4;
572 /* Pop the alignment added by OP_THROW too */
573 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
575 if (mono_do_x86_stack_align)
576 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
581 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
582 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
584 /* Set arg1 == regs */
585 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
586 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
587 /* Set arg2 == exc/ex_token_index */
589 x86_mov_reg_imm (code, X86_EAX, 0);
591 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
592 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
593 /* Set arg3 == eip */
595 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
597 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
598 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
599 /* Set arg4 == rethrow/pc_offset */
601 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
603 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
606 * The caller is LLVM code which passes the absolute address not a pc offset,
607 * so compensate by passing 0 as 'ip' and passing the negated abs address as
610 x86_neg_reg (code, X86_EAX);
612 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
614 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
618 // This can be called from runtime code, which can't guarantee that
619 // ebx contains the got address.
620 // So emit the got address loading code too
621 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
622 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");
623 x86_call_reg (code, X86_EAX);
625 x86_call_code (code, resume_unwind ? (mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
627 x86_breakpoint (code);
629 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
631 g_assert ((code - start) < kMaxCodeSize);
634 *info = mono_tramp_info_create (g_strdup (name), start, code - start, ji, unwind_ops);
638 for (l = unwind_ops; l; l = l->next)
640 g_slist_free (unwind_ops);
647 * mono_arch_get_throw_exception:
649 * Returns a function pointer which can be used to raise
650 * exceptions. The returned function has the following
651 * signature: void (*func) (MonoException *exc);
652 * For example to raise an arithmetic exception you can use:
654 * x86_push_imm (code, mono_get_exception_arithmetic ());
655 * x86_call_code (code, arch_get_throw_exception ());
659 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
661 return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
665 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
667 return get_throw_trampoline ("rethrow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
671 * mono_arch_get_throw_corlib_exception:
673 * Returns a function pointer which can be used to raise
674 * corlib exceptions. The returned function has the following
675 * signature: void (*func) (guint32 ex_token, guint32 offset);
676 * Here, offset is the offset which needs to be substracted from the caller IP
677 * to get the IP of the throw. Passing the offset has the advantage that it
678 * needs no relocations in the caller.
681 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
683 return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
687 mono_arch_exceptions_init (void)
692 * If we're running WoW64, we need to set the usermode exception policy
693 * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
694 * or (eventually) Windows 7 SP1.
700 HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
703 getter = GetProcAddress (kernel32, "GetProcessUserModeExceptionPolicy");
704 setter = GetProcAddress (kernel32, "SetProcessUserModeExceptionPolicy");
705 if (getter && setter) {
707 setter (flags & ~PROCESS_CALLBACK_FILTER_ENABLED);
713 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
717 /* LLVM needs different throw trampolines */
718 tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
719 mono_register_jit_icall (tramp, "llvm_throw_exception_trampoline", NULL, TRUE);
721 tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
722 mono_register_jit_icall (tramp, "llvm_rethrow_exception_trampoline", NULL, TRUE);
724 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
725 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
727 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
728 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
730 tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
731 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
733 signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
737 * mono_arch_find_jit_info:
739 * See exceptions-amd64.c for docs.
742 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
743 MonoJitInfo *ji, MonoContext *ctx,
744 MonoContext *new_ctx, MonoLMF **lmf,
745 mgreg_t **save_locations,
746 StackFrameInfo *frame)
748 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
750 memset (frame, 0, sizeof (StackFrameInfo));
752 frame->managed = FALSE;
757 gssize regs [MONO_MAX_IREGS + 1];
759 guint32 unwind_info_len;
762 frame->type = FRAME_TYPE_MANAGED;
764 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
765 frame->managed = TRUE;
768 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
770 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
772 regs [X86_EAX] = new_ctx->eax;
773 regs [X86_EBX] = new_ctx->ebx;
774 regs [X86_ECX] = new_ctx->ecx;
775 regs [X86_EDX] = new_ctx->edx;
776 regs [X86_ESP] = new_ctx->esp;
777 regs [X86_EBP] = new_ctx->ebp;
778 regs [X86_ESI] = new_ctx->esi;
779 regs [X86_EDI] = new_ctx->edi;
780 regs [X86_NREG] = new_ctx->eip;
782 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
783 (guint8*)ji->code_start + ji->code_size,
784 ip, regs, MONO_MAX_IREGS + 1,
785 save_locations, MONO_MAX_IREGS, &cfa);
787 new_ctx->eax = regs [X86_EAX];
788 new_ctx->ebx = regs [X86_EBX];
789 new_ctx->ecx = regs [X86_ECX];
790 new_ctx->edx = regs [X86_EDX];
791 new_ctx->esp = regs [X86_ESP];
792 new_ctx->ebp = regs [X86_EBP];
793 new_ctx->esi = regs [X86_ESI];
794 new_ctx->edi = regs [X86_EDI];
795 new_ctx->eip = regs [X86_NREG];
797 /* The CFA becomes the new SP value */
798 new_ctx->esp = (gssize)cfa;
803 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
804 /* remove any unused lmf */
805 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
808 /* Pop arguments off the stack */
810 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
815 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
817 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
818 new_ctx->esp += stack_to_pop;
825 if (((guint64)(*lmf)->previous_lmf) & 2) {
827 * This LMF entry is created by the soft debug code to mark transitions to
828 * managed code done during invokes.
830 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
832 g_assert (ext->debugger_invoke);
834 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
836 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
838 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
843 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
845 if (!((guint32)((*lmf)->previous_lmf) & 1))
848 g_assert_not_reached ();
849 /* Trampoline lmf frame */
850 frame->method = (*lmf)->method;
853 new_ctx->esi = (*lmf)->esi;
854 new_ctx->edi = (*lmf)->edi;
855 new_ctx->ebx = (*lmf)->ebx;
856 new_ctx->ebp = (*lmf)->ebp;
857 new_ctx->eip = (*lmf)->eip;
863 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
865 /* Check if we are in a trampoline LMF frame */
866 if ((guint32)((*lmf)->previous_lmf) & 1) {
867 /* lmf->esp is set by the trampoline code */
868 new_ctx->esp = (*lmf)->esp;
870 /* Pop arguments off the stack */
871 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
872 /* FIXME: Handle the IMT/vtable case too */
874 if ((*lmf)->method) {
875 MonoMethod *method = (*lmf)->method;
876 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
878 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
879 new_ctx->esp += stack_to_pop;
884 /* the lmf is always stored on the stack, so the following
885 * expression points to a stack location which can be used as ESP */
886 new_ctx->esp = (unsigned long)&((*lmf)->eip);
888 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
909 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
911 #if defined (__native_client__)
912 printf("WARNING: mono_arch_sigctx_to_monoctx() called!\n");
913 mctx->eax = 0xDEADBEEF;
914 mctx->ebx = 0xDEADBEEF;
915 mctx->ecx = 0xDEADBEEF;
916 mctx->edx = 0xDEADBEEF;
917 mctx->ebp = 0xDEADBEEF;
918 mctx->esp = 0xDEADBEEF;
919 mctx->esi = 0xDEADBEEF;
920 mctx->edi = 0xDEADBEEF;
921 mctx->eip = 0xDEADBEEF;
923 #ifdef MONO_ARCH_USE_SIGACTION
924 ucontext_t *ctx = (ucontext_t*)sigctx;
926 mctx->eax = UCONTEXT_REG_EAX (ctx);
927 mctx->ebx = UCONTEXT_REG_EBX (ctx);
928 mctx->ecx = UCONTEXT_REG_ECX (ctx);
929 mctx->edx = UCONTEXT_REG_EDX (ctx);
930 mctx->ebp = UCONTEXT_REG_EBP (ctx);
931 mctx->esp = UCONTEXT_REG_ESP (ctx);
932 mctx->esi = UCONTEXT_REG_ESI (ctx);
933 mctx->edi = UCONTEXT_REG_EDI (ctx);
934 mctx->eip = UCONTEXT_REG_EIP (ctx);
936 struct sigcontext *ctx = (struct sigcontext *)sigctx;
938 mctx->eax = ctx->SC_EAX;
939 mctx->ebx = ctx->SC_EBX;
940 mctx->ecx = ctx->SC_ECX;
941 mctx->edx = ctx->SC_EDX;
942 mctx->ebp = ctx->SC_EBP;
943 mctx->esp = ctx->SC_ESP;
944 mctx->esi = ctx->SC_ESI;
945 mctx->edi = ctx->SC_EDI;
946 mctx->eip = ctx->SC_EIP;
948 #endif /* if defined(__native_client__) */
952 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
954 #if defined(__native_client__)
955 printf("WARNING: mono_arch_monoctx_to_sigctx() called!\n");
957 #ifdef MONO_ARCH_USE_SIGACTION
958 ucontext_t *ctx = (ucontext_t*)sigctx;
960 UCONTEXT_REG_EAX (ctx) = mctx->eax;
961 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
962 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
963 UCONTEXT_REG_EDX (ctx) = mctx->edx;
964 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
965 UCONTEXT_REG_ESP (ctx) = mctx->esp;
966 UCONTEXT_REG_ESI (ctx) = mctx->esi;
967 UCONTEXT_REG_EDI (ctx) = mctx->edi;
968 UCONTEXT_REG_EIP (ctx) = mctx->eip;
970 struct sigcontext *ctx = (struct sigcontext *)sigctx;
972 ctx->SC_EAX = mctx->eax;
973 ctx->SC_EBX = mctx->ebx;
974 ctx->SC_ECX = mctx->ecx;
975 ctx->SC_EDX = mctx->edx;
976 ctx->SC_EBP = mctx->ebp;
977 ctx->SC_ESP = mctx->esp;
978 ctx->SC_ESI = mctx->esi;
979 ctx->SC_EDI = mctx->edi;
980 ctx->SC_EIP = mctx->eip;
982 #endif /* __native_client__ */
986 mono_arch_ip_from_context (void *sigctx)
988 #if defined(__native_client__)
989 printf("WARNING: mono_arch_ip_from_context() called!\n");
992 #ifdef MONO_ARCH_USE_SIGACTION
993 ucontext_t *ctx = (ucontext_t*)sigctx;
994 return (gpointer)UCONTEXT_REG_EIP (ctx);
996 struct sigcontext *ctx = sigctx;
997 return (gpointer)ctx->SC_EIP;
999 #endif /* __native_client__ */
1005 * Called by resuming from a signal handler.
1008 handle_signal_exception (gpointer obj)
1010 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1012 static void (*restore_context) (MonoContext *);
1014 if (!restore_context)
1015 restore_context = mono_get_restore_context ();
1017 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
1019 if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
1022 mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), FALSE);
1024 restore_context (&ctx);
1028 * mono_x86_get_signal_exception_trampoline:
1030 * This x86 specific trampoline is used to call handle_signal_exception.
1033 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
1035 guint8 *start, *code;
1036 MonoJumpInfo *ji = NULL;
1037 GSList *unwind_ops = NULL;
1040 start = code = mono_global_codeman_reserve (128);
1043 x86_push_reg (code, X86_ECX);
1045 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
1046 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
1048 /* Fix the alignment to be what apple expects */
1051 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
1052 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
1055 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
1056 /* Branch to target */
1057 x86_call_reg (code, X86_EDX);
1059 g_assert ((code - start) < 128);
1062 *info = mono_tramp_info_create (g_strdup ("x86_signal_exception_trampoline"), start, code - start, ji, unwind_ops);
1066 for (l = unwind_ops; l; l = l->next)
1068 g_slist_free (unwind_ops);
1075 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
1077 #if defined(MONO_ARCH_USE_SIGACTION)
1078 ucontext_t *ctx = (ucontext_t*)sigctx;
1081 * Handling the exception in the signal handler is problematic, since the original
1082 * signal is disabled, and we could run arbitrary code though the debugger. So
1083 * resume into the normal stack and do most work there if possible.
1085 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1086 guint64 sp = UCONTEXT_REG_ESP (ctx);
1088 /* Pass the ctx parameter in TLS */
1089 mono_arch_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
1091 * Can't pass the obj on the stack, since we are executing on the
1092 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1093 * So put it into a register, and branch to a trampoline which
1096 g_assert (!test_only);
1097 UCONTEXT_REG_EAX (ctx) = (gsize)obj;
1098 UCONTEXT_REG_ECX (ctx) = UCONTEXT_REG_EIP (ctx);
1099 UCONTEXT_REG_EDX (ctx) = (gsize)handle_signal_exception;
1101 /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
1104 UCONTEXT_REG_ESP (ctx) = sp;
1106 UCONTEXT_REG_EIP (ctx) = (gsize)signal_exception_trampoline;
1109 #elif defined (TARGET_WIN32)
1110 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1111 struct sigcontext *ctx = (struct sigcontext *)sigctx;
1112 guint64 sp = ctx->SC_ESP;
1114 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1117 * Can't pass the obj on the stack, since we are executing on the
1118 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1119 * So put it into a register, and branch to a trampoline which
1122 g_assert (!test_only);
1123 ctx->SC_EAX = (gsize)obj;
1124 ctx->SC_ECX = ctx->SC_EIP;
1125 ctx->SC_EDX = (gsize)handle_signal_exception;
1127 /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
1132 ctx->SC_EIP = (gsize)signal_exception_trampoline;
1138 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1140 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
1143 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
1145 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
1152 restore_soft_guard_pages (void)
1154 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1155 if (jit_tls->stack_ovf_guard_base)
1156 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1160 * this function modifies mctx so that when it is restored, it
1161 * won't execcute starting at mctx.eip, but in a function that
1162 * will restore the protection on the soft-guard pages and return back to
1163 * continue at mctx.eip.
1166 prepare_for_guard_pages (MonoContext *mctx)
1169 sp = (gpointer)(mctx->esp);
1171 /* the resturn addr */
1172 sp [0] = (gpointer)(mctx->eip);
1173 mctx->eip = (unsigned long)restore_soft_guard_pages;
1174 mctx->esp = (unsigned long)sp;
1178 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
1180 void (*restore_context) (MonoContext *);
1183 restore_context = mono_get_restore_context ();
1184 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1186 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
1188 prepare_for_guard_pages (&mctx);
1189 restore_context (&mctx);
1192 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
1194 prepare_for_guard_pages (&mctx);
1195 restore_context (&mctx);
1199 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
1201 #ifdef MONO_ARCH_USE_SIGACTION
1202 MonoException *exc = NULL;
1203 ucontext_t *ctx = (ucontext_t*)sigctx;
1204 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1208 /* if we didn't find a managed method for the ip address and it matches the fault
1209 * address, we assume we followed a broken pointer during an indirect call, so
1210 * we try the lookup again with the return address pushed on the stack
1212 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1213 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1214 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1216 UCONTEXT_REG_EIP (ctx) = sp [0];
1219 exc = mono_domain_get ()->stack_overflow_ex;
1221 mono_handle_native_sigsegv (SIGSEGV, sigctx);
1222 /* setup a call frame on the real stack so that control is returned there
1223 * and exception handling can continue.
1224 * If this was a stack overflow the caller already ensured the stack pages
1225 * needed have been unprotected.
1226 * The frame looks like:
1233 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
1236 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1237 sp = (gpointer)((char*)sp - frame_size);
1238 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1241 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1244 sp [2] = (gpointer)stack_ovf;
1245 /* may need to adjust pointers in the new struct copy, depending on the OS */
1246 memcpy (sp + 4, ctx, sizeof (ucontext_t));
1247 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1248 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1249 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1253 #if MONO_SUPPORT_TASKLETS
1254 MonoContinuationRestore
1255 mono_tasklets_arch_restore (void)
1257 static guint8* saved = NULL;
1258 guint8 *code, *start;
1260 #ifdef __native_client_codegen__
1261 g_print("mono_tasklets_arch_restore needs to be aligned for Native Client\n");
1264 return (MonoContinuationRestore)saved;
1265 code = start = mono_global_codeman_reserve (48);
1266 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1267 /* put cont in edx */
1268 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1269 /* setup the copy of the stack */
1270 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1271 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1273 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1274 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1275 x86_prefix (code, X86_REP_PREFIX);
1278 /* now restore the registers from the LMF */
1279 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1280 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1281 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1282 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1283 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1285 /* restore the lmf chain */
1286 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1287 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1289 /* state in eax, so it's setup as the return value */
1290 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1291 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1292 g_assert ((code - start) <= 48);
1294 return (MonoContinuationRestore)saved;