2 * exceptions-x86.c: exception support for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
16 #include <mono/metadata/abi-details.h>
17 #include <mono/arch/x86/x86-codegen.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/gc-internal.h>
24 #include <mono/metadata/mono-debug.h>
25 #include <mono/utils/mono-mmap.h>
31 static gpointer signal_exception_trampoline;
34 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
37 static void (*restore_stack) (void *);
39 static MonoW32ExceptionHandler fpe_handler;
40 static MonoW32ExceptionHandler ill_handler;
41 static MonoW32ExceptionHandler segv_handler;
43 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
44 gpointer mono_win_vectored_exception_handle;
45 extern int (*gUnhandledExceptionHandler)(EXCEPTION_POINTERS*);
47 #ifndef PROCESS_CALLBACK_FILTER_ENABLED
48 # define PROCESS_CALLBACK_FILTER_ENABLED 1
51 #define W32_SEH_HANDLE_EX(_ex) \
52 if (_ex##_handler) _ex##_handler(0, ep, ctx)
54 LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
56 #ifndef MONO_CROSS_COMPILE
57 if (mono_old_win_toplevel_exception_filter) {
58 return (*mono_old_win_toplevel_exception_filter)(ep);
62 mono_handle_native_sigsegv (SIGSEGV, NULL, NULL);
64 return EXCEPTION_CONTINUE_SEARCH;
68 * mono_win32_get_handle_stackoverflow (void):
70 * Returns a pointer to a method which restores the current context stack
71 * and calls handle_exceptions, when done restores the original stack.
74 mono_win32_get_handle_stackoverflow (void)
76 static guint8 *start = NULL;
82 /* restore_contect (void *sigctx) */
83 start = code = mono_global_codeman_reserve (128);
85 /* load context into ebx */
86 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
88 /* move current stack into edi for later restore */
89 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
91 /* use the new freed stack from sigcontext */
92 /* XXX replace usage of struct sigcontext with MonoContext so we can use MONO_STRUCT_OFFSET */
93 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
95 /* get the current domain */
96 x86_call_code (code, mono_domain_get);
98 /* get stack overflow exception from domain object */
99 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
101 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
102 x86_push_reg (code, X86_EAX);
103 x86_push_reg (code, X86_EBX);
104 x86_call_code (code, mono_arch_handle_exception);
106 /* restore the SEH handler stack */
107 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
112 mono_arch_flush_icache (start, code - start);
113 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
118 /* Special hack to workaround the fact that the
119 * when the SEH handler is called the stack is
120 * to small to recover.
122 * Stack walking part of this method is from mono_handle_exception
124 * The idea is simple;
125 * - walk the stack to free some space (64k)
126 * - set esp to new stack location
127 * - call mono_arch_handle_exception with stack overflow exception
128 * - set esp to SEH handlers stack
132 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
136 MonoDomain *domain = mono_domain_get ();
138 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
139 MonoLMF *lmf = jit_tls->lmf;
140 MonoContext initial_ctx;
142 guint32 free_stack = 0;
143 StackFrameInfo frame;
145 mono_sigctx_to_monoctx (sctx, &ctx);
147 /* get our os page size */
149 page_size = si.dwPageSize;
151 /* Let's walk the stack to recover
152 * the needed stack space (if possible)
154 memset (&rji, 0, sizeof (rji));
157 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
159 /* try to free 64kb from our stack */
163 mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, NULL, &frame);
165 g_warning ("Exception inside function without unwind info");
166 g_assert_not_reached ();
169 if (frame.ji != (gpointer)-1) {
170 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
173 /* todo: we should call abort if ji is -1 */
175 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
177 mono_monoctx_to_sigctx (&ctx, sctx);
179 /* todo: install new stack-guard page */
181 /* use the new stack and call mono_arch_handle_exception () */
182 restore_stack (sctx);
186 * Unhandled Exception Filter
187 * Top-level per-process exception handler.
189 LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
191 EXCEPTION_RECORD* er;
194 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
196 /* If the thread is not managed by the runtime return early */
198 return EXCEPTION_CONTINUE_SEARCH;
200 jit_tls->mono_win_chained_exception_needs_run = FALSE;
201 res = EXCEPTION_CONTINUE_EXECUTION;
203 er = ep->ExceptionRecord;
204 ctx = ep->ContextRecord;
206 switch (er->ExceptionCode) {
207 case EXCEPTION_STACK_OVERFLOW:
208 win32_handle_stack_overflow (ep, ctx);
210 case EXCEPTION_ACCESS_VIOLATION:
211 W32_SEH_HANDLE_EX(segv);
213 case EXCEPTION_ILLEGAL_INSTRUCTION:
214 W32_SEH_HANDLE_EX(ill);
216 case EXCEPTION_INT_DIVIDE_BY_ZERO:
217 case EXCEPTION_INT_OVERFLOW:
218 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
219 case EXCEPTION_FLT_OVERFLOW:
220 case EXCEPTION_FLT_UNDERFLOW:
221 case EXCEPTION_FLT_INEXACT_RESULT:
222 W32_SEH_HANDLE_EX(fpe);
225 jit_tls->mono_win_chained_exception_needs_run = TRUE;
229 if (jit_tls->mono_win_chained_exception_needs_run) {
230 /* Don't copy context back if we chained exception
231 * as the handler may have modfied the EXCEPTION_POINTERS
232 * directly. We don't pass sigcontext to chained handlers.
233 * Return continue search so the UnhandledExceptionFilter
234 * can correctly chain the exception.
236 res = EXCEPTION_CONTINUE_SEARCH;
242 void win32_seh_init()
244 /* install restore stack helper */
246 restore_stack = mono_win32_get_handle_stackoverflow ();
248 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_unhandled_exception_filter);
249 mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_handler);
252 void win32_seh_cleanup()
254 if (mono_old_win_toplevel_exception_filter)
255 SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
256 RemoveVectoredExceptionHandler (mono_win_vectored_exception_handle);
259 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
263 fpe_handler = handler;
266 ill_handler = handler;
269 segv_handler = handler;
276 #endif /* TARGET_WIN32 */
279 * mono_arch_get_restore_context:
281 * Returns a pointer to a method which restores a previously saved sigcontext.
284 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
286 guint8 *start = NULL;
288 MonoJumpInfo *ji = NULL;
289 GSList *unwind_ops = NULL;
291 /* restore_contect (MonoContext *ctx) */
293 start = code = mono_global_codeman_reserve (128);
296 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
299 x86_mov_reg_membase (code, X86_EBX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebx), 4);
302 x86_mov_reg_membase (code, X86_EDI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, edi), 4);
305 x86_mov_reg_membase (code, X86_ESI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, esi), 4);
308 x86_mov_reg_membase (code, X86_EDX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, edx), 4);
311 * The context resides on the stack, in the stack frame of the
312 * caller of this function. The stack pointer that we need to
313 * restore is potentially many stack frames higher up, so the
314 * distance between them can easily be more than the red zone
315 * size. Hence the stack pointer can be restored only after
316 * we have finished loading everything from the context.
319 /* load ESP into EBP */
320 x86_mov_reg_membase (code, X86_EBP, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, esp), 4);
321 /* load return address into ECX */
322 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, eip), 4);
323 /* save the return addr to the restored stack - 4 */
324 x86_mov_membase_reg (code, X86_EBP, -4, X86_ECX, 4);
326 /* load EBP into ECX */
327 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebp), 4);
328 /* save EBP to the restored stack - 8 */
329 x86_mov_membase_reg (code, X86_EBP, -8, X86_ECX, 4);
331 /* load EAX into ECX */
332 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, eax), 4);
333 /* save EAX to the restored stack - 12 */
334 x86_mov_membase_reg (code, X86_EBP, -12, X86_ECX, 4);
337 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ecx), 4);
339 /* restore ESP - 12 */
340 x86_lea_membase (code, X86_ESP, X86_EBP, -12);
342 x86_pop_reg (code, X86_EAX);
344 x86_pop_reg (code, X86_EBP);
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);
360 mono_arch_flush_icache (start, code - start);
361 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
367 * mono_arch_get_call_filter:
369 * Returns a pointer to a method which calls an exception filter. We
370 * also use this function to call finally handlers (we pass NULL as
371 * @exc object in this case).
374 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
378 MonoJumpInfo *ji = NULL;
379 GSList *unwind_ops = NULL;
380 guint kMaxCodeSize = NACL_SIZE (64, 128);
382 /* call_filter (MonoContext *ctx, unsigned long eip) */
383 start = code = mono_global_codeman_reserve (kMaxCodeSize);
385 x86_push_reg (code, X86_EBP);
386 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
387 x86_push_reg (code, X86_EBX);
388 x86_push_reg (code, X86_EDI);
389 x86_push_reg (code, X86_ESI);
392 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
394 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
396 x86_push_reg (code, X86_EBP);
399 x86_mov_reg_membase (code, X86_EBP, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebp), 4);
400 /* restore registers used by global register allocation (EBX & ESI) */
401 x86_mov_reg_membase (code, X86_EBX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebx), 4);
402 x86_mov_reg_membase (code, X86_ESI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, esi), 4);
403 x86_mov_reg_membase (code, X86_EDI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, edi), 4);
405 /* align stack and save ESP */
406 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
407 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
408 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
409 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
410 x86_push_reg (code, X86_EDX);
412 /* call the handler */
413 x86_call_reg (code, X86_ECX);
416 x86_pop_reg (code, X86_ESP);
419 x86_pop_reg (code, X86_EBP);
421 /* restore saved regs */
422 x86_pop_reg (code, X86_ESI);
423 x86_pop_reg (code, X86_EDI);
424 x86_pop_reg (code, X86_EBX);
428 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
431 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
435 for (l = unwind_ops; l; l = l->next)
437 g_slist_free (unwind_ops);
440 mono_arch_flush_icache (start, code - start);
441 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
443 g_assert ((code - start) < kMaxCodeSize);
448 * mono_x86_throw_exception:
450 * C function called from the throw trampolines.
453 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
454 mgreg_t eip, gboolean rethrow)
458 ctx.esp = regs [X86_ESP];
460 ctx.ebp = regs [X86_EBP];
461 ctx.edi = regs [X86_EDI];
462 ctx.esi = regs [X86_ESI];
463 ctx.ebx = regs [X86_EBX];
464 ctx.edx = regs [X86_EDX];
465 ctx.ecx = regs [X86_ECX];
466 ctx.eax = regs [X86_EAX];
469 /* The OSX ABI specifies 16 byte alignment at call sites */
470 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
473 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
474 MonoException *mono_ex = (MonoException*)exc;
476 mono_ex->stack_trace = NULL;
477 mono_ex->trace_ips = NULL;
481 /* adjust eip so that it point into the call instruction */
484 mono_handle_exception (&ctx, exc);
486 mono_restore_context (&ctx);
488 g_assert_not_reached ();
492 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
493 mgreg_t eip, gint32 pc_offset)
495 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
498 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
502 /* Negate the ip adjustment done in mono_x86_throw_exception () */
505 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
509 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc,
510 mgreg_t eip, gboolean rethrow)
514 ctx.esp = regs [X86_ESP];
516 ctx.ebp = regs [X86_EBP];
517 ctx.edi = regs [X86_EDI];
518 ctx.esi = regs [X86_ESI];
519 ctx.ebx = regs [X86_EBX];
520 ctx.edx = regs [X86_EDX];
521 ctx.ecx = regs [X86_ECX];
522 ctx.eax = regs [X86_EAX];
524 mono_resume_unwind (&ctx);
528 * get_throw_trampoline:
530 * Generate a call to mono_x86_throw_exception/
531 * mono_x86_throw_corlib_exception.
532 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
533 * which doesn't push the arguments.
536 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
538 guint8 *start, *code;
539 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
540 MonoJumpInfo *ji = NULL;
541 GSList *unwind_ops = NULL;
542 guint kMaxCodeSize = NACL_SIZE (128, 256);
544 start = code = mono_global_codeman_reserve (kMaxCodeSize);
549 * On apple, the stack is misaligned by the pushing of the return address.
552 /* On OSX, we don't generate alignment code to save space */
555 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
558 * The stack looks like this:
559 * <pc offset> (only if corlib is TRUE)
560 * <exception object>/<type token>
561 * <return addr> <- esp (unaligned on apple)
564 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
565 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
568 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
569 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
574 arg_offsets [3] = 12;
578 for (i = 0; i < X86_NREG; ++i)
580 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
581 /* Calculate the offset between the current sp and the sp of the caller */
583 /* LLVM doesn't push the arguments */
584 stack_offset = stack_size + 4;
588 stack_offset = stack_size + 4 + 8;
590 /* We don't generate stack alignment code on osx to save space */
593 /* One argument + stack alignment */
594 stack_offset = stack_size + 4 + 4;
596 /* Pop the alignment added by OP_THROW too */
597 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
599 if (mono_do_x86_stack_align)
600 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
605 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
606 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
608 /* Set arg1 == regs */
609 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
610 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
611 /* Set arg2 == exc/ex_token_index */
613 x86_mov_reg_imm (code, X86_EAX, 0);
615 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
616 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
617 /* Set arg3 == eip */
619 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
621 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
622 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
623 /* Set arg4 == rethrow/pc_offset */
625 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
627 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
630 * The caller is LLVM code which passes the absolute address not a pc offset,
631 * so compensate by passing 0 as 'ip' and passing the negated abs address as
634 x86_neg_reg (code, X86_EAX);
636 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
638 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
642 // This can be called from runtime code, which can't guarantee that
643 // ebx contains the got address.
644 // So emit the got address loading code too
645 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
646 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");
647 x86_call_reg (code, X86_EAX);
649 x86_call_code (code, resume_unwind ? (gpointer)(mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
651 x86_breakpoint (code);
653 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
655 g_assert ((code - start) < kMaxCodeSize);
658 *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
662 for (l = unwind_ops; l; l = l->next)
664 g_slist_free (unwind_ops);
667 mono_arch_flush_icache (start, code - start);
668 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
674 * mono_arch_get_throw_exception:
676 * Returns a function pointer which can be used to raise
677 * exceptions. The returned function has the following
678 * signature: void (*func) (MonoException *exc);
679 * For example to raise an arithmetic exception you can use:
681 * x86_push_imm (code, mono_get_exception_arithmetic ());
682 * x86_call_code (code, arch_get_throw_exception ());
686 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
688 return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
692 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
694 return get_throw_trampoline ("rethrow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
698 * mono_arch_get_throw_corlib_exception:
700 * Returns a function pointer which can be used to raise
701 * corlib exceptions. The returned function has the following
702 * signature: void (*func) (guint32 ex_token, guint32 offset);
703 * Here, offset is the offset which needs to be substracted from the caller IP
704 * to get the IP of the throw. Passing the offset has the advantage that it
705 * needs no relocations in the caller.
708 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
710 return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
714 mono_arch_exceptions_init (void)
719 * If we're running WoW64, we need to set the usermode exception policy
720 * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
721 * or (eventually) Windows 7 SP1.
727 HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
730 getter = GetProcAddress (kernel32, "GetProcessUserModeExceptionPolicy");
731 setter = GetProcAddress (kernel32, "SetProcessUserModeExceptionPolicy");
732 if (getter && setter) {
734 setter (flags & ~PROCESS_CALLBACK_FILTER_ENABLED);
740 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
744 /* LLVM needs different throw trampolines */
745 tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
746 mono_register_jit_icall (tramp, "llvm_throw_exception_trampoline", NULL, TRUE);
748 tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
749 mono_register_jit_icall (tramp, "llvm_rethrow_exception_trampoline", NULL, TRUE);
751 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
752 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
754 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
755 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
757 tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
758 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
760 signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
764 * mono_arch_find_jit_info:
766 * See exceptions-amd64.c for docs.
769 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
770 MonoJitInfo *ji, MonoContext *ctx,
771 MonoContext *new_ctx, MonoLMF **lmf,
772 mgreg_t **save_locations,
773 StackFrameInfo *frame)
775 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
777 memset (frame, 0, sizeof (StackFrameInfo));
783 gssize regs [MONO_MAX_IREGS + 1];
785 guint32 unwind_info_len;
788 frame->type = FRAME_TYPE_MANAGED;
790 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
792 regs [X86_EAX] = new_ctx->eax;
793 regs [X86_EBX] = new_ctx->ebx;
794 regs [X86_ECX] = new_ctx->ecx;
795 regs [X86_EDX] = new_ctx->edx;
796 regs [X86_ESP] = new_ctx->esp;
797 regs [X86_EBP] = new_ctx->ebp;
798 regs [X86_ESI] = new_ctx->esi;
799 regs [X86_EDI] = new_ctx->edi;
800 regs [X86_NREG] = new_ctx->eip;
802 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
803 (guint8*)ji->code_start + ji->code_size,
804 ip, NULL, regs, MONO_MAX_IREGS + 1,
805 save_locations, MONO_MAX_IREGS, &cfa);
807 new_ctx->eax = regs [X86_EAX];
808 new_ctx->ebx = regs [X86_EBX];
809 new_ctx->ecx = regs [X86_ECX];
810 new_ctx->edx = regs [X86_EDX];
811 new_ctx->esp = regs [X86_ESP];
812 new_ctx->ebp = regs [X86_EBP];
813 new_ctx->esi = regs [X86_ESI];
814 new_ctx->edi = regs [X86_EDI];
815 new_ctx->eip = regs [X86_NREG];
817 /* The CFA becomes the new SP value */
818 new_ctx->esp = (gssize)cfa;
826 if (((guint64)(*lmf)->previous_lmf) & 2) {
828 * This LMF entry is created by the soft debug code to mark transitions to
829 * managed code done during invokes.
831 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
833 g_assert (ext->debugger_invoke);
835 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
837 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
839 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
844 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
849 frame->method = (*lmf)->method;
852 new_ctx->esi = (*lmf)->esi;
853 new_ctx->edi = (*lmf)->edi;
854 new_ctx->ebx = (*lmf)->ebx;
855 new_ctx->ebp = (*lmf)->ebp;
856 new_ctx->eip = (*lmf)->eip;
861 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
863 /* Check if we are in a trampoline LMF frame */
864 if ((guint32)((*lmf)->previous_lmf) & 1) {
865 /* lmf->esp is set by the trampoline code */
866 new_ctx->esp = (*lmf)->esp;
869 /* the lmf is always stored on the stack, so the following
870 * expression points to a stack location which can be used as ESP */
871 new_ctx->esp = (unsigned long)&((*lmf)->eip);
873 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
882 mono_arch_ip_from_context (void *sigctx)
884 #if defined(__native_client__)
885 printf("WARNING: mono_arch_ip_from_context() called!\n");
887 #elif defined(MONO_ARCH_USE_SIGACTION)
888 ucontext_t *ctx = (ucontext_t*)sigctx;
889 return (gpointer)UCONTEXT_REG_EIP (ctx);
890 #elif defined(HOST_WIN32)
891 return ((CONTEXT*)sigctx)->Eip;
893 struct sigcontext *ctx = sigctx;
894 return (gpointer)ctx->SC_EIP;
901 * Called by resuming from a signal handler.
904 handle_signal_exception (gpointer obj)
906 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
909 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
911 mono_handle_exception (&ctx, obj);
913 mono_restore_context (&ctx);
917 * mono_x86_get_signal_exception_trampoline:
919 * This x86 specific trampoline is used to call handle_signal_exception.
922 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
924 guint8 *start, *code;
925 MonoJumpInfo *ji = NULL;
926 GSList *unwind_ops = NULL;
929 start = code = mono_global_codeman_reserve (128);
932 x86_push_reg (code, X86_ECX);
934 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
935 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
937 /* Fix the alignment to be what apple expects */
940 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
941 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
944 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
945 /* Branch to target */
946 x86_call_reg (code, X86_EDX);
948 g_assert ((code - start) < 128);
951 *info = mono_tramp_info_create ("x86_signal_exception_trampoline", start, code - start, ji, unwind_ops);
955 for (l = unwind_ops; l; l = l->next)
957 g_slist_free (unwind_ops);
960 mono_arch_flush_icache (start, code - start);
961 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
968 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
971 * Can't pass the obj on the stack, since we are executing on the
972 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
973 * So put it into a register, and branch to a trampoline which
976 ctx->eax = (mgreg_t)user_data;
978 ctx->edx = (mgreg_t)async_cb;
981 ctx->esp = (ctx->esp - 16) & ~15;
982 ctx->eip = (mgreg_t)signal_exception_trampoline;
986 mono_arch_handle_exception (void *sigctx, gpointer obj)
988 #if defined(MONO_ARCH_USE_SIGACTION)
990 ucontext_t *ctx = (ucontext_t*)sigctx;
993 * Handling the exception in the signal handler is problematic, since the original
994 * signal is disabled, and we could run arbitrary code though the debugger. So
995 * resume into the normal stack and do most work there if possible.
997 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
999 /* Pass the ctx parameter in TLS */
1000 mono_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
1002 mctx = jit_tls->ex_ctx;
1003 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1004 mono_monoctx_to_sigctx (&mctx, sigctx);
1007 #elif defined (TARGET_WIN32)
1009 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1010 struct sigcontext *ctx = (struct sigcontext *)sigctx;
1012 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1014 mctx = jit_tls->ex_ctx;
1015 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1016 mono_monoctx_to_sigctx (&mctx, sigctx);
1022 mono_sigctx_to_monoctx (sigctx, &mctx);
1024 mono_handle_exception (&mctx, obj);
1026 mono_monoctx_to_sigctx (&mctx, sigctx);
1033 restore_soft_guard_pages (void)
1035 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1036 if (jit_tls->stack_ovf_guard_base)
1037 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1041 * this function modifies mctx so that when it is restored, it
1042 * won't execcute starting at mctx.eip, but in a function that
1043 * will restore the protection on the soft-guard pages and return back to
1044 * continue at mctx.eip.
1047 prepare_for_guard_pages (MonoContext *mctx)
1050 sp = (gpointer)(mctx->esp);
1052 /* the resturn addr */
1053 sp [0] = (gpointer)(mctx->eip);
1054 mctx->eip = (unsigned long)restore_soft_guard_pages;
1055 mctx->esp = (unsigned long)sp;
1059 altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
1065 mono_handle_exception (&mctx, obj);
1067 prepare_for_guard_pages (&mctx);
1068 mono_restore_context (&mctx);
1072 mono_arch_handle_altstack_exception (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, gpointer fault_addr, gboolean stack_ovf)
1074 #ifdef MONO_ARCH_USE_SIGACTION
1075 MonoException *exc = NULL;
1076 ucontext_t *ctx = (ucontext_t*)sigctx;
1077 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1081 /* if we didn't find a managed method for the ip address and it matches the fault
1082 * address, we assume we followed a broken pointer during an indirect call, so
1083 * we try the lookup again with the return address pushed on the stack
1085 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1086 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1087 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1089 UCONTEXT_REG_EIP (ctx) = sp [0];
1092 exc = mono_domain_get ()->stack_overflow_ex;
1094 mono_handle_native_sigsegv (SIGSEGV, sigctx, siginfo);
1095 /* setup a call frame on the real stack so that control is returned there
1096 * and exception handling can continue.
1097 * If this was a stack overflow the caller already ensured the stack pages
1098 * needed have been unprotected.
1099 * The frame looks like:
1106 // FIXME: test_only is no more.
1107 frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4;
1110 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1111 sp = (gpointer)((char*)sp - frame_size);
1112 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1115 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1118 sp [2] = (gpointer)stack_ovf;
1119 mono_sigctx_to_monoctx (sigctx, (MonoContext*)(sp + 4));
1120 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1121 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1122 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1126 #if MONO_SUPPORT_TASKLETS
1127 MonoContinuationRestore
1128 mono_tasklets_arch_restore (void)
1130 static guint8* saved = NULL;
1131 guint8 *code, *start;
1133 #ifdef __native_client_codegen__
1134 g_print("mono_tasklets_arch_restore needs to be aligned for Native Client\n");
1137 return (MonoContinuationRestore)saved;
1138 code = start = mono_global_codeman_reserve (48);
1139 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1140 /* put cont in edx */
1141 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1142 /* state in eax, so it's setup as the return value */
1143 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1145 /* setup the copy of the stack */
1146 x86_mov_reg_membase (code, X86_ECX, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1147 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1149 x86_mov_reg_membase (code, X86_ESI, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1150 x86_mov_reg_membase (code, X86_EDI, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1151 x86_prefix (code, X86_REP_PREFIX);
1154 /* now restore the registers from the LMF */
1155 x86_mov_reg_membase (code, X86_ECX, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1156 x86_mov_reg_membase (code, X86_EBX, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, ebx), 4);
1157 x86_mov_reg_membase (code, X86_EBP, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, ebp), 4);
1158 x86_mov_reg_membase (code, X86_ESI, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, esi), 4);
1159 x86_mov_reg_membase (code, X86_EDI, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, edi), 4);
1161 /* restore the lmf chain */
1162 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1163 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1165 x86_jump_membase (code, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, return_ip));
1166 g_assert ((code - start) <= 48);
1168 return (MonoContinuationRestore)saved;
1173 * mono_arch_setup_resume_sighandler_ctx:
1175 * Setup CTX so execution continues at FUNC.
1178 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1180 int align = (((gint32)MONO_CONTEXT_GET_SP (ctx)) % MONO_ARCH_FRAME_ALIGNMENT + 4);
1183 MONO_CONTEXT_SET_SP (ctx, (gsize)MONO_CONTEXT_GET_SP (ctx) - align);
1185 MONO_CONTEXT_SET_IP (ctx, func);