2 * exceptions-amd64.c: exception support for AMD64
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
14 #ifndef PLATFORM_WIN32
15 #include <sys/ucontext.h>
18 #include <mono/arch/amd64/amd64-codegen.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/threads.h>
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/gc-internal.h>
25 #include <mono/metadata/mono-debug.h>
26 #include <mono/utils/mono-mmap.h>
29 #include "mini-amd64.h"
31 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
34 static MonoW32ExceptionHandler fpe_handler;
35 static MonoW32ExceptionHandler ill_handler;
36 static MonoW32ExceptionHandler segv_handler;
38 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
40 #define W32_SEH_HANDLE_EX(_ex) \
41 if (_ex##_handler) _ex##_handler((int)sctx)
44 * Unhandled Exception Filter
45 * Top-level per-process exception handler.
47 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
54 res = EXCEPTION_CONTINUE_EXECUTION;
56 er = ep->ExceptionRecord;
57 ctx = ep->ContextRecord;
58 sctx = g_malloc(sizeof(MonoContext));
60 /* Copy Win32 context to UNIX style context */
75 switch (er->ExceptionCode) {
76 case EXCEPTION_ACCESS_VIOLATION:
77 W32_SEH_HANDLE_EX(segv);
79 case EXCEPTION_ILLEGAL_INSTRUCTION:
80 W32_SEH_HANDLE_EX(ill);
82 case EXCEPTION_INT_DIVIDE_BY_ZERO:
83 case EXCEPTION_INT_OVERFLOW:
84 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
85 case EXCEPTION_FLT_OVERFLOW:
86 case EXCEPTION_FLT_UNDERFLOW:
87 case EXCEPTION_FLT_INEXACT_RESULT:
88 W32_SEH_HANDLE_EX(fpe);
94 /* Copy context back */
100 ctx->Rsp = sctx->rsp;
101 ctx->Rsi = sctx->rsi;
102 ctx->Rdi = sctx->rdi;
103 ctx->Rip = sctx->rip;
110 void win32_seh_init()
112 old_handler = SetUnhandledExceptionFilter(seh_handler);
115 void win32_seh_cleanup()
117 if (old_handler) SetUnhandledExceptionFilter(old_handler);
120 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
124 fpe_handler = handler;
127 ill_handler = handler;
130 segv_handler = handler;
137 #endif /* PLATFORM_WIN32 */
140 * mono_arch_get_restore_context:
142 * Returns a pointer to a method which restores a previously saved sigcontext.
145 mono_arch_get_restore_context (void)
147 static guint8 *start = NULL;
148 static gboolean inited = FALSE;
154 /* restore_contect (MonoContext *ctx) */
156 start = code = mono_global_codeman_reserve (256);
158 /* get return address */
159 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rip), 8);
161 /* Restore registers */
162 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbp), 8);
163 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbx), 8);
164 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r12), 8);
165 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r13), 8);
166 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r14), 8);
167 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r15), 8);
169 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rsp), 8);
171 /* jump to the saved IP */
172 amd64_jump_reg (code, AMD64_RAX);
180 * mono_arch_get_call_filter:
182 * Returns a pointer to a method which calls an exception filter. We
183 * also use this function to call finally handlers (we pass NULL as
184 * @exc object in this case).
187 mono_arch_get_call_filter (void)
189 static guint8 *start;
190 static gboolean inited = FALSE;
198 start = code = mono_global_codeman_reserve (64);
200 /* call_filter (MonoContext *ctx, unsigned long eip) */
203 /* Alloc new frame */
204 amd64_push_reg (code, AMD64_RBP);
205 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
207 /* Save callee saved regs */
209 for (i = 0; i < AMD64_NREG; ++i)
210 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
211 amd64_push_reg (code, i);
217 amd64_push_reg (code, AMD64_RBP);
219 /* Make stack misaligned, the call will make it aligned again */
221 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
224 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbp), 8);
225 /* load callee saved regs */
226 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbx), 8);
227 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r12), 8);
228 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r13), 8);
229 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r14), 8);
230 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r15), 8);
232 /* call the handler */
233 amd64_call_reg (code, AMD64_RSI);
236 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
239 amd64_pop_reg (code, AMD64_RBP);
241 /* Restore callee saved regs */
242 for (i = AMD64_NREG; i >= 0; --i)
243 if (AMD64_IS_CALLEE_SAVED_REG (i))
244 amd64_pop_reg (code, i);
249 g_assert ((code - start) < 64);
257 throw_exception (MonoObject *exc, guint64 rip, guint64 rsp,
258 guint64 rbx, guint64 rbp, guint64 r12, guint64 r13,
259 guint64 r14, guint64 r15, guint64 rethrow)
261 static void (*restore_context) (MonoContext *);
264 if (!restore_context)
265 restore_context = mono_arch_get_restore_context ();
276 if (!rethrow && mono_debugger_throw_exception ((gpointer)(rip - 8), (gpointer)rsp, exc)) {
278 * The debugger wants us to stop on the `throw' instruction.
279 * By the time we get here, it already inserted a breakpoint on
280 * eip - 8 (which is the address of the `mov %r15,%rdi ; callq throw').
285 * In case of a rethrow, the JIT is emitting code like this:
287 * mov 0xffffffffffffffd0(%rbp),%rax'
291 * Here, restore_context() wouldn't restore the %rax register correctly.
295 restore_context (&ctx);
296 g_assert_not_reached ();
299 /* adjust eip so that it point into the call instruction */
302 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
303 MonoException *mono_ex = (MonoException*)exc;
305 mono_ex->stack_trace = NULL;
307 mono_handle_exception (&ctx, exc, (gpointer)rip, FALSE);
308 restore_context (&ctx);
310 g_assert_not_reached ();
314 get_throw_trampoline (gboolean rethrow)
319 start = code = mono_global_codeman_reserve (64);
324 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 8);
326 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RSP, 0, 8);
328 amd64_lea_membase (code, AMD64_RDX, AMD64_RSP, 8);
329 /* Callee saved regs */
330 amd64_mov_reg_reg (code, AMD64_RCX, AMD64_RBX, 8);
331 amd64_mov_reg_reg (code, AMD64_R8, AMD64_RBP, 8);
332 amd64_mov_reg_reg (code, AMD64_R9, AMD64_R12, 8);
334 amd64_push_imm (code, 0);
336 amd64_push_imm (code, rethrow);
337 amd64_push_reg (code, AMD64_R15);
338 amd64_push_reg (code, AMD64_R14);
339 amd64_push_reg (code, AMD64_R13);
341 amd64_mov_reg_imm (code, AMD64_R11, throw_exception);
342 amd64_call_reg (code, AMD64_R11);
343 amd64_breakpoint (code);
345 g_assert ((code - start) < 64);
351 * mono_arch_get_throw_exception:
353 * Returns a function pointer which can be used to raise
354 * exceptions. The returned function has the following
355 * signature: void (*func) (MonoException *exc);
359 mono_arch_get_throw_exception (void)
361 static guint8* start;
362 static gboolean inited = FALSE;
367 start = get_throw_trampoline (FALSE);
375 mono_arch_get_rethrow_exception (void)
377 static guint8* start;
378 static gboolean inited = FALSE;
383 start = get_throw_trampoline (TRUE);
391 mono_arch_get_throw_exception_by_name (void)
393 static guint8* start;
394 static gboolean inited = FALSE;
400 start = code = mono_global_codeman_reserve (64);
402 /* Not used on amd64 */
403 amd64_breakpoint (code);
409 * mono_arch_get_throw_corlib_exception:
411 * Returns a function pointer which can be used to raise
412 * corlib exceptions. The returned function has the following
413 * signature: void (*func) (guint32 ex_token, guint32 offset);
414 * Here, offset is the offset which needs to be substracted from the caller IP
415 * to get the IP of the throw. Passing the offset has the advantage that it
416 * needs no relocations in the caller.
419 mono_arch_get_throw_corlib_exception (void)
421 static guint8* start;
422 static gboolean inited = FALSE;
429 start = code = mono_global_codeman_reserve (64);
432 amd64_push_reg (code, AMD64_RSI);
434 /* Call exception_from_token */
435 amd64_mov_reg_reg (code, AMD64_RSI, AMD64_RDI, 8);
436 amd64_mov_reg_imm (code, AMD64_RDI, mono_defaults.exception_class->image);
437 amd64_mov_reg_imm (code, AMD64_R11, mono_exception_from_token);
438 amd64_call_reg (code, AMD64_R11);
440 /* Compute throw_ip */
441 amd64_pop_reg (code, AMD64_RSI);
443 amd64_pop_reg (code, AMD64_RDX);
444 amd64_alu_reg_reg (code, X86_SUB, AMD64_RDX, AMD64_RSI);
446 /* Put the throw_ip at the top of the misaligned stack */
447 amd64_push_reg (code, AMD64_RDX);
449 throw_ex = (guint64)mono_arch_get_throw_exception ();
451 /* Call throw_exception */
452 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RAX, 8);
453 amd64_mov_reg_imm (code, AMD64_R11, throw_ex);
454 /* The original IP is on the stack */
455 amd64_jump_reg (code, AMD64_R11);
457 g_assert ((code - start) < 64);
464 /* mono_arch_find_jit_info:
466 * This function is used to gather information from @ctx. It return the
467 * MonoJitInfo of the corresponding function, unwinds one stack frame and
468 * stores the resulting context into @new_ctx. It also stores a string
469 * describing the stack location into @trace (if not NULL), and modifies
470 * the @lmf if necessary. @native_offset return the IP offset from the
471 * start of the function or -1 if that info is not available.
474 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
475 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
480 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
482 /* Avoid costly table lookup during stack overflow */
483 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
486 ji = mono_jit_info_table_find (domain, ip);
493 gboolean omit_fp = (ji->used_regs & (1 << 31)) > 0;
498 if (!ji->method->wrapper_type)
502 * Some managed methods like pinvoke wrappers might have save_lmf set.
503 * In this case, register save/restore code is not generated by the
504 * JIT, so we have to restore callee saved registers from the lmf.
506 if (ji->method->save_lmf) {
508 * We only need to do this if the exception was raised in managed
509 * code, since otherwise the lmf was already popped of the stack.
511 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
512 new_ctx->rbp = (*lmf)->ebp;
513 new_ctx->rbx = (*lmf)->rbx;
514 new_ctx->rsp = (*lmf)->rsp;
515 new_ctx->r12 = (*lmf)->r12;
516 new_ctx->r13 = (*lmf)->r13;
517 new_ctx->r14 = (*lmf)->r14;
518 new_ctx->r15 = (*lmf)->r15;
522 offset = omit_fp ? 0 : -1;
523 /* restore caller saved registers */
524 for (i = 0; i < AMD64_NREG; i ++)
525 if (AMD64_IS_CALLEE_SAVED_REG (i) && (ji->used_regs & (1 << i))) {
529 reg = *((guint64*)ctx->rsp + offset);
533 reg = *((guint64 *)ctx->rbp + offset);
557 g_assert_not_reached ();
562 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
563 /* remove any unused lmf */
564 *lmf = (*lmf)->previous_lmf;
569 new_ctx->rsp += (ji->used_regs >> 16) & (0x7fff);
570 new_ctx->rip = *((guint64 *)new_ctx->rsp) - 1;
571 /* Pop return address */
575 /* Pop EBP and the return address */
576 new_ctx->rsp = ctx->rbp + (2 * sizeof (gpointer));
577 /* we substract 1, so that the IP points into the call instruction */
578 new_ctx->rip = *((guint64 *)ctx->rbp + 1) - 1;
579 new_ctx->rbp = *((guint64 *)ctx->rbp);
582 /* Pop arguments off the stack */
584 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
586 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
587 new_ctx->rsp += stack_to_pop;
595 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->rip))) {
600 /* Trampoline lmf frame */
601 memset (res, 0, sizeof (MonoJitInfo));
602 res->method = (*lmf)->method;
605 new_ctx->rip = (*lmf)->rip;
606 new_ctx->rbp = (*lmf)->ebp;
607 new_ctx->rsp = (*lmf)->rsp;
609 new_ctx->rbx = (*lmf)->rbx;
610 new_ctx->r12 = (*lmf)->r12;
611 new_ctx->r13 = (*lmf)->r13;
612 new_ctx->r14 = (*lmf)->r14;
613 new_ctx->r15 = (*lmf)->r15;
615 *lmf = (*lmf)->previous_lmf;
617 return ji ? ji : res;
624 * mono_arch_handle_exception:
626 * @ctx: saved processor state
627 * @obj: the exception object
630 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
634 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
636 mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), test_only);
638 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
643 #ifdef MONO_ARCH_USE_SIGACTION
644 static inline guint64*
645 gregs_from_ucontext (ucontext_t *ctx)
648 guint64 *gregs = (guint64 *) &ctx->uc_mcontext;
650 guint64 *gregs = (guint64 *) &ctx->uc_mcontext.gregs;
657 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
659 #ifdef MONO_ARCH_USE_SIGACTION
660 ucontext_t *ctx = (ucontext_t*)sigctx;
662 guint64 *gregs = gregs_from_ucontext (ctx);
664 mctx->rax = gregs [REG_RAX];
665 mctx->rbx = gregs [REG_RBX];
666 mctx->rcx = gregs [REG_RCX];
667 mctx->rdx = gregs [REG_RDX];
668 mctx->rbp = gregs [REG_RBP];
669 mctx->rsp = gregs [REG_RSP];
670 mctx->rsi = gregs [REG_RSI];
671 mctx->rdi = gregs [REG_RDI];
672 mctx->rip = gregs [REG_RIP];
673 mctx->r12 = gregs [REG_R12];
674 mctx->r13 = gregs [REG_R13];
675 mctx->r14 = gregs [REG_R14];
676 mctx->r15 = gregs [REG_R15];
678 MonoContext *ctx = (MonoContext *)sigctx;
680 mctx->rax = ctx->rax;
681 mctx->rbx = ctx->rbx;
682 mctx->rcx = ctx->rcx;
683 mctx->rdx = ctx->rdx;
684 mctx->rbp = ctx->rbp;
685 mctx->rsp = ctx->rsp;
686 mctx->rsi = ctx->rsi;
687 mctx->rdi = ctx->rdi;
688 mctx->rip = ctx->rip;
689 mctx->r12 = ctx->r12;
690 mctx->r13 = ctx->r13;
691 mctx->r14 = ctx->r14;
692 mctx->r15 = ctx->r15;
697 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
699 #ifdef MONO_ARCH_USE_SIGACTION
700 ucontext_t *ctx = (ucontext_t*)sigctx;
702 guint64 *gregs = gregs_from_ucontext (ctx);
704 gregs [REG_RAX] = mctx->rax;
705 gregs [REG_RBX] = mctx->rbx;
706 gregs [REG_RCX] = mctx->rcx;
707 gregs [REG_RDX] = mctx->rdx;
708 gregs [REG_RBP] = mctx->rbp;
709 gregs [REG_RSP] = mctx->rsp;
710 gregs [REG_RSI] = mctx->rsi;
711 gregs [REG_RDI] = mctx->rdi;
712 gregs [REG_RIP] = mctx->rip;
713 gregs [REG_R12] = mctx->r12;
714 gregs [REG_R13] = mctx->r13;
715 gregs [REG_R14] = mctx->r14;
716 gregs [REG_R15] = mctx->r15;
718 MonoContext *ctx = (MonoContext *)sigctx;
720 ctx->rax = mctx->rax;
721 ctx->rbx = mctx->rbx;
722 ctx->rcx = mctx->rcx;
723 ctx->rdx = mctx->rdx;
724 ctx->rbp = mctx->rbp;
725 ctx->rsp = mctx->rsp;
726 ctx->rsi = mctx->rsi;
727 ctx->rdi = mctx->rdi;
728 ctx->rip = mctx->rip;
729 ctx->r12 = mctx->r12;
730 ctx->r13 = mctx->r13;
731 ctx->r14 = mctx->r14;
732 ctx->r15 = mctx->r15;
737 mono_arch_ip_from_context (void *sigctx)
740 #ifdef MONO_ARCH_USE_SIGACTION
742 ucontext_t *ctx = (ucontext_t*)sigctx;
744 guint64 *gregs = gregs_from_ucontext (ctx);
746 return (gpointer)gregs [REG_RIP];
748 MonoContext *ctx = sigctx;
749 return (gpointer)ctx->rip;
754 restore_soft_guard_pages (void)
756 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
757 if (jit_tls->stack_ovf_guard_base)
758 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
762 * this function modifies mctx so that when it is restored, it
763 * won't execcute starting at mctx.eip, but in a function that
764 * will restore the protection on the soft-guard pages and return back to
765 * continue at mctx.eip.
768 prepare_for_guard_pages (MonoContext *mctx)
771 sp = (gpointer)(mctx->rsp);
773 /* the return addr */
774 sp [0] = (gpointer)(mctx->rip);
775 mctx->rip = (unsigned long)restore_soft_guard_pages;
776 mctx->rsp = (unsigned long)sp;
780 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
782 void (*restore_context) (MonoContext *);
785 restore_context = mono_arch_get_restore_context ();
786 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
787 mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), FALSE);
789 prepare_for_guard_pages (&mctx);
790 restore_context (&mctx);
794 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
796 #ifdef MONO_ARCH_USE_SIGACTION
797 MonoException *exc = NULL;
798 ucontext_t *ctx = (ucontext_t*)sigctx;
799 guint64 *gregs = gregs_from_ucontext (ctx);
800 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)gregs [REG_RIP]);
805 exc = mono_domain_get ()->stack_overflow_ex;
807 mono_handle_native_sigsegv (SIGSEGV, sigctx);
809 /* setup a call frame on the real stack so that control is returned there
810 * and exception handling can continue.
811 * The frame looks like:
815 * 128 is the size of the red zone
817 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4 + 128;
820 sp = (gpointer)(gregs [REG_RSP] & ~15);
821 sp = (gpointer)((char*)sp - frame_size);
822 /* the arguments must be aligned */
823 sp [-1] = (gpointer)gregs [REG_RIP];
824 /* may need to adjust pointers in the new struct copy, depending on the OS */
825 memcpy (sp + 4, ctx, sizeof (ucontext_t));
826 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
827 gregs [REG_RIP] = (unsigned long)altstack_handle_and_restore;
828 gregs [REG_RSP] = (unsigned long)(sp - 1);
829 gregs [REG_RDI] = (unsigned long)(sp + 4);
830 gregs [REG_RSI] = (guint64)exc;
831 gregs [REG_RDX] = stack_ovf;