2 * exceptions-x86.c: exception support for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
12 #if _WIN32_WINNT < 0x0501
13 /* Required for Vectored Exception Handling. */
15 #define _WIN32_WINNT 0x0501
16 #endif /* _WIN32_WINNT < 0x0501 */
22 #include <mono/arch/x86/x86-codegen.h>
23 #include <mono/metadata/appdomain.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/threads.h>
26 #include <mono/metadata/debug-helpers.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/gc-internal.h>
29 #include <mono/metadata/mono-debug.h>
30 #include <mono/utils/mono-mmap.h>
35 #include "debug-mini.h"
37 static gpointer signal_exception_trampoline;
40 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
43 static void (*restore_stack) (void *);
45 static MonoW32ExceptionHandler fpe_handler;
46 static MonoW32ExceptionHandler ill_handler;
47 static MonoW32ExceptionHandler segv_handler;
49 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
50 gpointer mono_win_vectored_exception_handle;
51 extern gboolean mono_win_chained_exception_needs_run;
52 extern int (*gUnhandledExceptionHandler)(EXCEPTION_POINTERS*);
54 #ifndef PROCESS_CALLBACK_FILTER_ENABLED
55 # define PROCESS_CALLBACK_FILTER_ENABLED 1
58 #define W32_SEH_HANDLE_EX(_ex) \
59 if (_ex##_handler) _ex##_handler(0, ep, sctx)
61 LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
63 #ifndef MONO_CROSS_COMPILE
64 if (mono_old_win_toplevel_exception_filter) {
65 return (*mono_old_win_toplevel_exception_filter)(ep);
69 mono_handle_native_sigsegv (SIGSEGV, NULL);
71 return EXCEPTION_CONTINUE_SEARCH;
75 * mono_win32_get_handle_stackoverflow (void):
77 * Returns a pointer to a method which restores the current context stack
78 * and calls handle_exceptions, when done restores the original stack.
81 mono_win32_get_handle_stackoverflow (void)
83 static guint8 *start = NULL;
89 /* restore_contect (void *sigctx) */
90 start = code = mono_global_codeman_reserve (128);
92 /* load context into ebx */
93 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
95 /* move current stack into edi for later restore */
96 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
98 /* use the new freed stack from sigcontext */
99 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
101 /* get the current domain */
102 x86_call_code (code, mono_domain_get);
104 /* get stack overflow exception from domain object */
105 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
107 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
108 x86_push_reg (code, X86_EAX);
109 x86_push_reg (code, X86_EBX);
110 x86_call_code (code, mono_arch_handle_exception);
112 /* restore the SEH handler stack */
113 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
121 /* Special hack to workaround the fact that the
122 * when the SEH handler is called the stack is
123 * to small to recover.
125 * Stack walking part of this method is from mono_handle_exception
127 * The idea is simple;
128 * - walk the stack to free some space (64k)
129 * - set esp to new stack location
130 * - call mono_arch_handle_exception with stack overflow exception
131 * - set esp to SEH handlers stack
135 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
139 MonoDomain *domain = mono_domain_get ();
141 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
142 MonoLMF *lmf = jit_tls->lmf;
143 MonoContext initial_ctx;
145 guint32 free_stack = 0;
146 StackFrameInfo frame;
148 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
149 mono_arch_sigctx_to_monoctx (sctx, &ctx);
151 /* get our os page size */
153 page_size = si.dwPageSize;
155 /* Let's walk the stack to recover
156 * the needed stack space (if possible)
158 memset (&rji, 0, sizeof (rji));
161 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
163 /* try to free 64kb from our stack */
167 mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, NULL, &frame);
169 g_warning ("Exception inside function without unwind info");
170 g_assert_not_reached ();
173 if (frame.ji != (gpointer)-1) {
174 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
177 /* todo: we should call abort if ji is -1 */
179 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
181 /* convert into sigcontext to be used in mono_arch_handle_exception */
182 mono_arch_monoctx_to_sigctx (&ctx, sctx);
184 /* todo: install new stack-guard page */
186 /* use the new stack and call mono_arch_handle_exception () */
187 restore_stack (sctx);
191 * Unhandled Exception Filter
192 * Top-level per-process exception handler.
194 LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
196 EXCEPTION_RECORD* er;
198 struct sigcontext* sctx;
201 mono_win_chained_exception_needs_run = FALSE;
202 res = EXCEPTION_CONTINUE_EXECUTION;
204 er = ep->ExceptionRecord;
205 ctx = ep->ContextRecord;
206 sctx = g_malloc(sizeof(struct sigcontext));
208 /* Copy Win32 context to UNIX style context */
209 sctx->eax = ctx->Eax;
210 sctx->ebx = ctx->Ebx;
211 sctx->ecx = ctx->Ecx;
212 sctx->edx = ctx->Edx;
213 sctx->ebp = ctx->Ebp;
214 sctx->esp = ctx->Esp;
215 sctx->esi = ctx->Esi;
216 sctx->edi = ctx->Edi;
217 sctx->eip = ctx->Eip;
219 switch (er->ExceptionCode) {
220 case EXCEPTION_STACK_OVERFLOW:
221 win32_handle_stack_overflow (ep, sctx);
223 case EXCEPTION_ACCESS_VIOLATION:
224 W32_SEH_HANDLE_EX(segv);
226 case EXCEPTION_ILLEGAL_INSTRUCTION:
227 W32_SEH_HANDLE_EX(ill);
229 case EXCEPTION_INT_DIVIDE_BY_ZERO:
230 case EXCEPTION_INT_OVERFLOW:
231 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
232 case EXCEPTION_FLT_OVERFLOW:
233 case EXCEPTION_FLT_UNDERFLOW:
234 case EXCEPTION_FLT_INEXACT_RESULT:
235 W32_SEH_HANDLE_EX(fpe);
241 if (mono_win_chained_exception_needs_run) {
242 /* Don't copy context back if we chained exception
243 * as the handler may have modfied the EXCEPTION_POINTERS
244 * directly. We don't pass sigcontext to chained handlers.
245 * Return continue search so the UnhandledExceptionFilter
246 * can correctly chain the exception.
248 res = EXCEPTION_CONTINUE_SEARCH;
250 /* Copy context back */
251 ctx->Eax = sctx->eax;
252 ctx->Ebx = sctx->ebx;
253 ctx->Ecx = sctx->ecx;
254 ctx->Edx = sctx->edx;
255 ctx->Ebp = sctx->ebp;
256 ctx->Esp = sctx->esp;
257 ctx->Esi = sctx->esi;
258 ctx->Edi = sctx->edi;
259 ctx->Eip = sctx->eip;
262 /* TODO: Find right place to free this in stack overflow case */
263 if (er->ExceptionCode != EXCEPTION_STACK_OVERFLOW)
269 void win32_seh_init()
271 /* install restore stack helper */
273 restore_stack = mono_win32_get_handle_stackoverflow ();
275 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_unhandled_exception_filter);
276 mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_handler);
279 void win32_seh_cleanup()
281 if (mono_old_win_toplevel_exception_filter) SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
282 RemoveVectoredExceptionHandler (seh_unhandled_exception_filter);
285 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
289 fpe_handler = handler;
292 ill_handler = handler;
295 segv_handler = handler;
302 #endif /* TARGET_WIN32 */
305 * mono_arch_get_restore_context:
307 * Returns a pointer to a method which restores a previously saved sigcontext.
310 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
312 guint8 *start = NULL;
314 MonoJumpInfo *ji = NULL;
315 GSList *unwind_ops = NULL;
317 /* restore_contect (MonoContext *ctx) */
319 start = code = mono_global_codeman_reserve (128);
322 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
324 /* get return address, stored in ECX */
325 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
327 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
329 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
331 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
333 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
334 /* save the return addr to the restored stack */
335 x86_push_reg (code, X86_ECX);
337 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
339 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ecx), 4);
341 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, edx), 4);
343 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eax), 4);
345 /* jump to the saved IP */
348 nacl_global_codeman_validate(&start, 128, &code);
351 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
355 for (l = unwind_ops; l; l = l->next)
357 g_slist_free (unwind_ops);
364 * mono_arch_get_call_filter:
366 * Returns a pointer to a method which calls an exception filter. We
367 * also use this function to call finally handlers (we pass NULL as
368 * @exc object in this case).
371 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
375 MonoJumpInfo *ji = NULL;
376 GSList *unwind_ops = NULL;
377 guint kMaxCodeSize = NACL_SIZE (64, 128);
379 /* call_filter (MonoContext *ctx, unsigned long eip) */
380 start = code = mono_global_codeman_reserve (kMaxCodeSize);
382 x86_push_reg (code, X86_EBP);
383 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
384 x86_push_reg (code, X86_EBX);
385 x86_push_reg (code, X86_EDI);
386 x86_push_reg (code, X86_ESI);
389 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
391 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
393 x86_push_reg (code, X86_EBP);
396 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
397 /* restore registers used by global register allocation (EBX & ESI) */
398 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
399 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
400 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
402 /* align stack and save ESP */
403 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
404 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
405 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
406 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
407 x86_push_reg (code, X86_EDX);
409 /* call the handler */
410 x86_call_reg (code, X86_ECX);
413 x86_pop_reg (code, X86_ESP);
416 x86_pop_reg (code, X86_EBP);
418 /* restore saved regs */
419 x86_pop_reg (code, X86_ESI);
420 x86_pop_reg (code, X86_EDI);
421 x86_pop_reg (code, X86_EBX);
425 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
428 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
432 for (l = unwind_ops; l; l = l->next)
434 g_slist_free (unwind_ops);
437 g_assert ((code - start) < kMaxCodeSize);
442 * mono_x86_throw_exception:
444 * C function called from the throw trampolines.
447 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
448 mgreg_t eip, gboolean rethrow)
450 static void (*restore_context) (MonoContext *);
453 if (!restore_context)
454 restore_context = mono_get_restore_context ();
456 ctx.esp = regs [X86_ESP];
458 ctx.ebp = regs [X86_EBP];
459 ctx.edi = regs [X86_EDI];
460 ctx.esi = regs [X86_ESI];
461 ctx.ebx = regs [X86_EBX];
462 ctx.edx = regs [X86_EDX];
463 ctx.ecx = regs [X86_ECX];
464 ctx.eax = regs [X86_EAX];
467 /* The OSX ABI specifies 16 byte alignment at call sites */
468 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
471 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
472 MonoException *mono_ex = (MonoException*)exc;
474 mono_ex->stack_trace = NULL;
477 if (mono_debug_using_mono_debugger ()) {
478 guint8 buf [16], *code;
480 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
483 if (buf [3] == 0xe8) {
484 MonoContext ctx_cp = ctx;
485 ctx_cp.eip = eip - 5;
487 if (mono_debugger_handle_exception (&ctx_cp, exc)) {
488 restore_context (&ctx_cp);
489 g_assert_not_reached ();
494 /* adjust eip so that it point into the call instruction */
497 mono_handle_exception (&ctx, exc);
499 restore_context (&ctx);
501 g_assert_not_reached ();
505 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
506 mgreg_t eip, gint32 pc_offset)
508 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
511 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
515 /* Negate the ip adjustment done in mono_x86_throw_exception () */
518 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
522 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc,
523 mgreg_t eip, gboolean rethrow)
527 ctx.esp = regs [X86_ESP];
529 ctx.ebp = regs [X86_EBP];
530 ctx.edi = regs [X86_EDI];
531 ctx.esi = regs [X86_ESI];
532 ctx.ebx = regs [X86_EBX];
533 ctx.edx = regs [X86_EDX];
534 ctx.ecx = regs [X86_ECX];
535 ctx.eax = regs [X86_EAX];
537 mono_resume_unwind (&ctx);
541 * get_throw_trampoline:
543 * Generate a call to mono_x86_throw_exception/
544 * mono_x86_throw_corlib_exception.
545 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
546 * which doesn't push the arguments.
549 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
551 guint8 *start, *code;
552 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
553 MonoJumpInfo *ji = NULL;
554 GSList *unwind_ops = NULL;
555 guint kMaxCodeSize = NACL_SIZE (128, 256);
557 start = code = mono_global_codeman_reserve (kMaxCodeSize);
562 * On apple, the stack is misaligned by the pushing of the return address.
565 /* On OSX, we don't generate alignment code to save space */
568 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
571 * The stack looks like this:
572 * <pc offset> (only if corlib is TRUE)
573 * <exception object>/<type token>
574 * <return addr> <- esp (unaligned on apple)
577 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
578 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
581 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
582 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
587 arg_offsets [3] = 12;
591 for (i = 0; i < X86_NREG; ++i)
593 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
594 /* Calculate the offset between the current sp and the sp of the caller */
596 /* LLVM doesn't push the arguments */
597 stack_offset = stack_size + 4;
601 stack_offset = stack_size + 4 + 8;
603 /* We don't generate stack alignment code on osx to save space */
606 /* One argument + stack alignment */
607 stack_offset = stack_size + 4 + 4;
609 /* Pop the alignment added by OP_THROW too */
610 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
612 if (mono_do_x86_stack_align)
613 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
618 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
619 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
621 /* Set arg1 == regs */
622 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
623 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
624 /* Set arg2 == exc/ex_token_index */
626 x86_mov_reg_imm (code, X86_EAX, 0);
628 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
629 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
630 /* Set arg3 == eip */
632 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
634 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
635 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
636 /* Set arg4 == rethrow/pc_offset */
638 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
640 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
643 * The caller is LLVM code which passes the absolute address not a pc offset,
644 * so compensate by passing 0 as 'ip' and passing the negated abs address as
647 x86_neg_reg (code, X86_EAX);
649 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
651 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
655 // This can be called from runtime code, which can't guarantee that
656 // ebx contains the got address.
657 // So emit the got address loading code too
658 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
659 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");
660 x86_call_reg (code, X86_EAX);
662 x86_call_code (code, resume_unwind ? (gpointer)(mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
664 x86_breakpoint (code);
666 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
668 g_assert ((code - start) < kMaxCodeSize);
671 *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
675 for (l = unwind_ops; l; l = l->next)
677 g_slist_free (unwind_ops);
684 * mono_arch_get_throw_exception:
686 * Returns a function pointer which can be used to raise
687 * exceptions. The returned function has the following
688 * signature: void (*func) (MonoException *exc);
689 * For example to raise an arithmetic exception you can use:
691 * x86_push_imm (code, mono_get_exception_arithmetic ());
692 * x86_call_code (code, arch_get_throw_exception ());
696 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
698 return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
702 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
704 return get_throw_trampoline ("rethrow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
708 * mono_arch_get_throw_corlib_exception:
710 * Returns a function pointer which can be used to raise
711 * corlib exceptions. The returned function has the following
712 * signature: void (*func) (guint32 ex_token, guint32 offset);
713 * Here, offset is the offset which needs to be substracted from the caller IP
714 * to get the IP of the throw. Passing the offset has the advantage that it
715 * needs no relocations in the caller.
718 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
720 return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
724 mono_arch_exceptions_init (void)
729 * If we're running WoW64, we need to set the usermode exception policy
730 * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
731 * or (eventually) Windows 7 SP1.
737 HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
740 getter = GetProcAddress (kernel32, "GetProcessUserModeExceptionPolicy");
741 setter = GetProcAddress (kernel32, "SetProcessUserModeExceptionPolicy");
742 if (getter && setter) {
744 setter (flags & ~PROCESS_CALLBACK_FILTER_ENABLED);
750 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
754 /* LLVM needs different throw trampolines */
755 tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
756 mono_register_jit_icall (tramp, "llvm_throw_exception_trampoline", NULL, TRUE);
758 tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
759 mono_register_jit_icall (tramp, "llvm_rethrow_exception_trampoline", NULL, TRUE);
761 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
762 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
764 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
765 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
767 tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
768 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
770 signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
774 * mono_arch_find_jit_info:
776 * See exceptions-amd64.c for docs.
779 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
780 MonoJitInfo *ji, MonoContext *ctx,
781 MonoContext *new_ctx, MonoLMF **lmf,
782 mgreg_t **save_locations,
783 StackFrameInfo *frame)
785 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
787 memset (frame, 0, sizeof (StackFrameInfo));
793 gssize regs [MONO_MAX_IREGS + 1];
795 guint32 unwind_info_len;
798 frame->type = FRAME_TYPE_MANAGED;
801 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
803 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
805 regs [X86_EAX] = new_ctx->eax;
806 regs [X86_EBX] = new_ctx->ebx;
807 regs [X86_ECX] = new_ctx->ecx;
808 regs [X86_EDX] = new_ctx->edx;
809 regs [X86_ESP] = new_ctx->esp;
810 regs [X86_EBP] = new_ctx->ebp;
811 regs [X86_ESI] = new_ctx->esi;
812 regs [X86_EDI] = new_ctx->edi;
813 regs [X86_NREG] = new_ctx->eip;
815 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
816 (guint8*)ji->code_start + ji->code_size,
817 ip, regs, MONO_MAX_IREGS + 1,
818 save_locations, MONO_MAX_IREGS, &cfa);
820 new_ctx->eax = regs [X86_EAX];
821 new_ctx->ebx = regs [X86_EBX];
822 new_ctx->ecx = regs [X86_ECX];
823 new_ctx->edx = regs [X86_EDX];
824 new_ctx->esp = regs [X86_ESP];
825 new_ctx->ebp = regs [X86_EBP];
826 new_ctx->esi = regs [X86_ESI];
827 new_ctx->edi = regs [X86_EDI];
828 new_ctx->eip = regs [X86_NREG];
830 /* The CFA becomes the new SP value */
831 new_ctx->esp = (gssize)cfa;
836 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
837 /* remove any unused lmf */
838 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
841 /* Pop arguments off the stack */
843 * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
847 if (ji->has_arch_eh_info)
848 new_ctx->esp += mono_jit_info_get_arch_eh_info (ji)->stack_size;
854 if (((guint64)(*lmf)->previous_lmf) & 2) {
856 * This LMF entry is created by the soft debug code to mark transitions to
857 * managed code done during invokes.
859 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
861 g_assert (ext->debugger_invoke);
863 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
865 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
867 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
872 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
874 if (!((guint32)((*lmf)->previous_lmf) & 1))
877 g_assert_not_reached ();
878 /* Trampoline lmf frame */
879 frame->method = (*lmf)->method;
882 new_ctx->esi = (*lmf)->esi;
883 new_ctx->edi = (*lmf)->edi;
884 new_ctx->ebx = (*lmf)->ebx;
885 new_ctx->ebp = (*lmf)->ebp;
886 new_ctx->eip = (*lmf)->eip;
892 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
894 /* Check if we are in a trampoline LMF frame */
895 if ((guint32)((*lmf)->previous_lmf) & 1) {
896 /* lmf->esp is set by the trampoline code */
897 new_ctx->esp = (*lmf)->esp;
899 /* Pop arguments off the stack */
900 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
901 /* FIXME: Handle the IMT/vtable case too */
904 if ((*lmf)->method) {
905 MonoMethod *method = (*lmf)->method;
906 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
908 guint32 stack_to_pop = mono_arch_get_argument_info (NULL, mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
909 new_ctx->esp += stack_to_pop;
915 /* the lmf is always stored on the stack, so the following
916 * expression points to a stack location which can be used as ESP */
917 new_ctx->esp = (unsigned long)&((*lmf)->eip);
919 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
928 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
930 mono_sigctx_to_monoctx (sigctx, mctx);
934 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
936 mono_monoctx_to_sigctx (mctx, sigctx);
940 mono_arch_ip_from_context (void *sigctx)
942 #if defined(__native_client__)
943 printf("WARNING: mono_arch_ip_from_context() called!\n");
946 #ifdef MONO_ARCH_USE_SIGACTION
947 ucontext_t *ctx = (ucontext_t*)sigctx;
948 return (gpointer)UCONTEXT_REG_EIP (ctx);
950 struct sigcontext *ctx = sigctx;
951 return (gpointer)ctx->SC_EIP;
953 #endif /* __native_client__ */
959 * Called by resuming from a signal handler.
962 handle_signal_exception (gpointer obj)
964 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
966 static void (*restore_context) (MonoContext *);
968 if (!restore_context)
969 restore_context = mono_get_restore_context ();
971 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
973 if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
976 mono_handle_exception (&ctx, obj);
978 restore_context (&ctx);
982 * mono_x86_get_signal_exception_trampoline:
984 * This x86 specific trampoline is used to call handle_signal_exception.
987 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
989 guint8 *start, *code;
990 MonoJumpInfo *ji = NULL;
991 GSList *unwind_ops = NULL;
994 start = code = mono_global_codeman_reserve (128);
997 x86_push_reg (code, X86_ECX);
999 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
1000 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
1002 /* Fix the alignment to be what apple expects */
1005 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
1006 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
1009 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
1010 /* Branch to target */
1011 x86_call_reg (code, X86_EDX);
1013 g_assert ((code - start) < 128);
1016 *info = mono_tramp_info_create ("x86_signal_exception_trampoline", start, code - start, ji, unwind_ops);
1020 for (l = unwind_ops; l; l = l->next)
1022 g_slist_free (unwind_ops);
1030 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
1033 * Can't pass the obj on the stack, since we are executing on the
1034 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1035 * So put it into a register, and branch to a trampoline which
1038 ctx->eax = (mgreg_t)user_data;
1039 ctx->ecx = ctx->eip;
1040 ctx->edx = (mgreg_t)async_cb;
1043 ctx->esp = (ctx->esp - 16) & ~15;
1044 ctx->eip = (mgreg_t)signal_exception_trampoline;
1048 mono_arch_handle_exception (void *sigctx, gpointer obj)
1050 #if defined(MONO_ARCH_USE_SIGACTION)
1052 ucontext_t *ctx = (ucontext_t*)sigctx;
1055 * Handling the exception in the signal handler is problematic, since the original
1056 * signal is disabled, and we could run arbitrary code though the debugger. So
1057 * resume into the normal stack and do most work there if possible.
1059 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1061 /* Pass the ctx parameter in TLS */
1062 mono_arch_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
1064 mctx = jit_tls->ex_ctx;
1065 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1066 mono_monoctx_to_sigctx (&mctx, sigctx);
1069 #elif defined (TARGET_WIN32)
1071 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1072 struct sigcontext *ctx = (struct sigcontext *)sigctx;
1074 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1076 mctx = jit_tls->ex_ctx;
1077 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1078 mono_monoctx_to_sigctx (&mctx, sigctx);
1084 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1086 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
1089 mono_handle_exception (&mctx, obj);
1091 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
1098 restore_soft_guard_pages (void)
1100 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1101 if (jit_tls->stack_ovf_guard_base)
1102 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1106 * this function modifies mctx so that when it is restored, it
1107 * won't execcute starting at mctx.eip, but in a function that
1108 * will restore the protection on the soft-guard pages and return back to
1109 * continue at mctx.eip.
1112 prepare_for_guard_pages (MonoContext *mctx)
1115 sp = (gpointer)(mctx->esp);
1117 /* the resturn addr */
1118 sp [0] = (gpointer)(mctx->eip);
1119 mctx->eip = (unsigned long)restore_soft_guard_pages;
1120 mctx->esp = (unsigned long)sp;
1124 altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
1126 void (*restore_context) (MonoContext *);
1129 restore_context = mono_get_restore_context ();
1132 if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
1134 prepare_for_guard_pages (&mctx);
1135 restore_context (&mctx);
1138 mono_handle_exception (&mctx, obj);
1140 prepare_for_guard_pages (&mctx);
1141 restore_context (&mctx);
1145 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
1147 #ifdef MONO_ARCH_USE_SIGACTION
1148 MonoException *exc = NULL;
1149 ucontext_t *ctx = (ucontext_t*)sigctx;
1150 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1154 /* if we didn't find a managed method for the ip address and it matches the fault
1155 * address, we assume we followed a broken pointer during an indirect call, so
1156 * we try the lookup again with the return address pushed on the stack
1158 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1159 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1160 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1162 UCONTEXT_REG_EIP (ctx) = sp [0];
1165 exc = mono_domain_get ()->stack_overflow_ex;
1167 mono_handle_native_sigsegv (SIGSEGV, sigctx);
1168 /* setup a call frame on the real stack so that control is returned there
1169 * and exception handling can continue.
1170 * If this was a stack overflow the caller already ensured the stack pages
1171 * needed have been unprotected.
1172 * The frame looks like:
1179 // FIXME: test_only is no more.
1180 frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4;
1183 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1184 sp = (gpointer)((char*)sp - frame_size);
1185 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1188 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1191 sp [2] = (gpointer)stack_ovf;
1192 mono_sigctx_to_monoctx (sigctx, (MonoContext*)(sp + 4));
1193 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1194 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1195 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1199 #if MONO_SUPPORT_TASKLETS
1200 MonoContinuationRestore
1201 mono_tasklets_arch_restore (void)
1203 static guint8* saved = NULL;
1204 guint8 *code, *start;
1206 #ifdef __native_client_codegen__
1207 g_print("mono_tasklets_arch_restore needs to be aligned for Native Client\n");
1210 return (MonoContinuationRestore)saved;
1211 code = start = mono_global_codeman_reserve (48);
1212 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1213 /* put cont in edx */
1214 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1215 /* state in eax, so it's setup as the return value */
1216 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1218 /* setup the copy of the stack */
1219 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1220 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1222 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1223 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1224 x86_prefix (code, X86_REP_PREFIX);
1227 /* now restore the registers from the LMF */
1228 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1229 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1230 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1231 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1232 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1234 /* restore the lmf chain */
1235 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1236 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1238 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1239 g_assert ((code - start) <= 48);
1241 return (MonoContinuationRestore)saved;
1246 * mono_arch_setup_resume_sighandler_ctx:
1248 * Setup CTX so execution continues at FUNC.
1251 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1253 int align = (((gint32)MONO_CONTEXT_GET_SP (ctx)) % MONO_ARCH_FRAME_ALIGNMENT + 4);
1256 MONO_CONTEXT_SET_SP (ctx, (gsize)MONO_CONTEXT_GET_SP (ctx) - align);
1258 MONO_CONTEXT_SET_IP (ctx, func);