2 * exceptions-amd64.c: exception support for AMD64
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
14 #include <sys/ucontext.h>
16 #include <mono/arch/amd64/amd64-codegen.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/threads.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/gc-internal.h>
23 #include <mono/metadata/mono-debug.h>
26 #include "mini-amd64.h"
28 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
31 static MonoW32ExceptionHandler fpe_handler;
32 static MonoW32ExceptionHandler ill_handler;
33 static MonoW32ExceptionHandler segv_handler;
35 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
37 #define W32_SEH_HANDLE_EX(_ex) \
38 if (_ex##_handler) _ex##_handler((int)sctx)
41 * Unhandled Exception Filter
42 * Top-level per-process exception handler.
44 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
51 res = EXCEPTION_CONTINUE_EXECUTION;
53 er = ep->ExceptionRecord;
54 ctx = ep->ContextRecord;
55 sctx = g_malloc(sizeof(MonoContext));
57 /* Copy Win32 context to UNIX style context */
68 switch (er->ExceptionCode) {
69 case EXCEPTION_ACCESS_VIOLATION:
70 W32_SEH_HANDLE_EX(segv);
72 case EXCEPTION_ILLEGAL_INSTRUCTION:
73 W32_SEH_HANDLE_EX(ill);
75 case EXCEPTION_INT_DIVIDE_BY_ZERO:
76 case EXCEPTION_INT_OVERFLOW:
77 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
78 case EXCEPTION_FLT_OVERFLOW:
79 case EXCEPTION_FLT_UNDERFLOW:
80 case EXCEPTION_FLT_INEXACT_RESULT:
81 W32_SEH_HANDLE_EX(fpe);
87 /* Copy context back */
103 void win32_seh_init()
105 old_handler = SetUnhandledExceptionFilter(seh_handler);
108 void win32_seh_cleanup()
110 if (old_handler) SetUnhandledExceptionFilter(old_handler);
113 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
117 fpe_handler = handler;
120 ill_handler = handler;
123 segv_handler = handler;
130 #endif /* PLATFORM_WIN32 */
133 * mono_arch_get_restore_context:
135 * Returns a pointer to a method which restores a previously saved sigcontext.
138 mono_arch_get_restore_context (void)
140 static guint8 *start = NULL;
141 static gboolean inited = FALSE;
147 /* restore_contect (MonoContext *ctx) */
149 start = code = mono_global_codeman_reserve (256);
151 /* get return address */
152 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rip), 8);
154 /* Restore registers */
155 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbp), 8);
156 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbx), 8);
157 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r12), 8);
158 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r13), 8);
159 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r14), 8);
160 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r15), 8);
162 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rsp), 8);
164 /* jump to the saved IP */
165 amd64_jump_reg (code, AMD64_RAX);
173 * mono_arch_get_call_filter:
175 * Returns a pointer to a method which calls an exception filter. We
176 * also use this function to call finally handlers (we pass NULL as
177 * @exc object in this case).
180 mono_arch_get_call_filter (void)
182 static guint8 *start;
183 static gboolean inited = FALSE;
191 start = code = mono_global_codeman_reserve (64);
193 /* call_filter (MonoContext *ctx, unsigned long eip) */
196 /* Alloc new frame */
197 amd64_push_reg (code, AMD64_RBP);
198 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
200 /* Save callee saved regs */
202 for (i = 0; i < AMD64_NREG; ++i)
203 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
204 amd64_push_reg (code, i);
210 amd64_push_reg (code, AMD64_RBP);
212 /* Make stack misaligned, the call will make it aligned again */
214 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
217 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbp), 8);
218 /* load callee saved regs */
219 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbx), 8);
220 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r12), 8);
221 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r13), 8);
222 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r14), 8);
223 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r15), 8);
225 /* call the handler */
226 amd64_call_reg (code, AMD64_RSI);
229 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
232 amd64_pop_reg (code, AMD64_RBP);
234 /* Restore callee saved regs */
235 for (i = AMD64_NREG; i >= 0; --i)
236 if (AMD64_IS_CALLEE_SAVED_REG (i))
237 amd64_pop_reg (code, i);
242 g_assert ((code - start) < 64);
250 throw_exception (MonoObject *exc, guint64 rip, guint64 rsp,
251 guint64 rbx, guint64 rbp, guint64 r12, guint64 r13,
252 guint64 r14, guint64 r15, guint64 rethrow)
254 static void (*restore_context) (MonoContext *);
257 if (!restore_context)
258 restore_context = mono_arch_get_restore_context ();
269 if (!rethrow && mono_debugger_throw_exception ((gpointer)(rip - 8), (gpointer)rsp, exc)) {
271 * The debugger wants us to stop on the `throw' instruction.
272 * By the time we get here, it already inserted a breakpoint on
273 * eip - 8 (which is the address of the `mov %r15,%rdi ; callq throw').
278 * In case of a rethrow, the JIT is emitting code like this:
280 * mov 0xffffffffffffffd0(%rbp),%rax'
284 * Here, restore_context() wouldn't restore the %rax register correctly.
288 restore_context (&ctx);
289 g_assert_not_reached ();
292 /* adjust eip so that it point into the call instruction */
295 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
296 MonoException *mono_ex = (MonoException*)exc;
298 mono_ex->stack_trace = NULL;
300 mono_handle_exception (&ctx, exc, (gpointer)rip, FALSE);
301 restore_context (&ctx);
303 g_assert_not_reached ();
307 get_throw_trampoline (gboolean rethrow)
312 start = code = mono_global_codeman_reserve (64);
317 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 8);
319 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RSP, 0, 8);
321 amd64_lea_membase (code, AMD64_RDX, AMD64_RSP, 8);
322 /* Callee saved regs */
323 amd64_mov_reg_reg (code, AMD64_RCX, AMD64_RBX, 8);
324 amd64_mov_reg_reg (code, AMD64_R8, AMD64_RBP, 8);
325 amd64_mov_reg_reg (code, AMD64_R9, AMD64_R12, 8);
327 amd64_push_imm (code, 0);
329 amd64_push_imm (code, rethrow);
330 amd64_push_reg (code, AMD64_R15);
331 amd64_push_reg (code, AMD64_R14);
332 amd64_push_reg (code, AMD64_R13);
334 amd64_mov_reg_imm (code, AMD64_R11, throw_exception);
335 amd64_call_reg (code, AMD64_R11);
336 amd64_breakpoint (code);
338 g_assert ((code - start) < 64);
344 * mono_arch_get_throw_exception:
346 * Returns a function pointer which can be used to raise
347 * exceptions. The returned function has the following
348 * signature: void (*func) (MonoException *exc);
352 mono_arch_get_throw_exception (void)
354 static guint8* start;
355 static gboolean inited = FALSE;
360 start = get_throw_trampoline (FALSE);
368 mono_arch_get_rethrow_exception (void)
370 static guint8* start;
371 static gboolean inited = FALSE;
376 start = get_throw_trampoline (TRUE);
384 mono_arch_get_throw_exception_by_name (void)
386 static guint8* start;
387 static gboolean inited = FALSE;
393 start = code = mono_global_codeman_reserve (64);
395 /* Not used on amd64 */
396 amd64_breakpoint (code);
402 * mono_arch_get_throw_corlib_exception:
404 * Returns a function pointer which can be used to raise
405 * corlib exceptions. The returned function has the following
406 * signature: void (*func) (guint32 ex_token, guint32 offset);
407 * Here, offset is the offset which needs to be substracted from the caller IP
408 * to get the IP of the throw. Passing the offset has the advantage that it
409 * needs no relocations in the caller.
412 mono_arch_get_throw_corlib_exception (void)
414 static guint8* start;
415 static gboolean inited = FALSE;
422 start = code = mono_global_codeman_reserve (64);
425 amd64_push_reg (code, AMD64_RSI);
427 /* Call exception_from_token */
428 amd64_mov_reg_reg (code, AMD64_RSI, AMD64_RDI, 8);
429 amd64_mov_reg_imm (code, AMD64_RDI, mono_defaults.exception_class->image);
430 amd64_mov_reg_imm (code, AMD64_R11, mono_exception_from_token);
431 amd64_call_reg (code, AMD64_R11);
433 /* Compute throw_ip */
434 amd64_pop_reg (code, AMD64_RSI);
436 amd64_pop_reg (code, AMD64_RDX);
437 amd64_alu_reg_reg (code, X86_SUB, AMD64_RDX, AMD64_RSI);
439 /* Put the throw_ip at the top of the misaligned stack */
440 amd64_push_reg (code, AMD64_RDX);
442 throw_ex = (guint64)mono_arch_get_throw_exception ();
444 /* Call throw_exception */
445 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RAX, 8);
446 amd64_mov_reg_imm (code, AMD64_R11, throw_ex);
447 /* The original IP is on the stack */
448 amd64_jump_reg (code, AMD64_R11);
450 g_assert ((code - start) < 64);
457 /* mono_arch_find_jit_info:
459 * This function is used to gather information from @ctx. It return the
460 * MonoJitInfo of the corresponding function, unwinds one stack frame and
461 * stores the resulting context into @new_ctx. It also stores a string
462 * describing the stack location into @trace (if not NULL), and modifies
463 * the @lmf if necessary. @native_offset return the IP offset from the
464 * start of the function or -1 if that info is not available.
467 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
468 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
473 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
475 /* Avoid costly table lookup during stack overflow */
476 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
479 ji = mono_jit_info_table_find (domain, ip);
486 gboolean omit_fp = (ji->used_regs & (1 << 31)) > 0;
491 if (!ji->method->wrapper_type)
495 * Some managed methods like pinvoke wrappers might have save_lmf set.
496 * In this case, register save/restore code is not generated by the
497 * JIT, so we have to restore callee saved registers from the lmf.
499 if (ji->method->save_lmf) {
501 * We only need to do this if the exception was raised in managed
502 * code, since otherwise the lmf was already popped of the stack.
504 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
505 new_ctx->rbp = (*lmf)->ebp;
506 new_ctx->rbx = (*lmf)->rbx;
507 new_ctx->rsp = (*lmf)->rsp;
508 new_ctx->r12 = (*lmf)->r12;
509 new_ctx->r13 = (*lmf)->r13;
510 new_ctx->r14 = (*lmf)->r14;
511 new_ctx->r15 = (*lmf)->r15;
515 offset = omit_fp ? 0 : -1;
516 /* restore caller saved registers */
517 for (i = 0; i < AMD64_NREG; i ++)
518 if (AMD64_IS_CALLEE_SAVED_REG (i) && (ji->used_regs & (1 << i))) {
522 reg = *((guint64*)ctx->rsp + offset);
526 reg = *((guint64 *)ctx->rbp + offset);
550 g_assert_not_reached ();
555 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
556 /* remove any unused lmf */
557 *lmf = (*lmf)->previous_lmf;
562 new_ctx->rsp += (ji->used_regs >> 16) & (0x7fff);
563 new_ctx->rip = *((guint64 *)new_ctx->rsp) - 1;
564 /* Pop return address */
568 /* Pop EBP and the return address */
569 new_ctx->rsp = ctx->rbp + (2 * sizeof (gpointer));
570 /* we substract 1, so that the IP points into the call instruction */
571 new_ctx->rip = *((guint64 *)ctx->rbp + 1) - 1;
572 new_ctx->rbp = *((guint64 *)ctx->rbp);
575 /* Pop arguments off the stack */
577 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
579 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
580 new_ctx->rsp += stack_to_pop;
588 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->rip))) {
593 /* Trampoline lmf frame */
594 memset (res, 0, sizeof (MonoJitInfo));
595 res->method = (*lmf)->method;
598 new_ctx->rip = (*lmf)->rip;
599 new_ctx->rbp = (*lmf)->ebp;
600 new_ctx->rsp = (*lmf)->rsp;
602 new_ctx->rbx = (*lmf)->rbx;
603 new_ctx->r12 = (*lmf)->r12;
604 new_ctx->r13 = (*lmf)->r13;
605 new_ctx->r14 = (*lmf)->r14;
606 new_ctx->r15 = (*lmf)->r15;
608 *lmf = (*lmf)->previous_lmf;
610 return ji ? ji : res;
617 * mono_arch_handle_exception:
619 * @ctx: saved processor state
620 * @obj: the exception object
623 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
627 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
629 mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), test_only);
631 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
636 static inline guint64*
637 gregs_from_ucontext (ucontext_t *ctx)
640 guint64 *gregs = (guint64 *) &ctx->uc_mcontext;
642 guint64 *gregs = (guint64 *) &ctx->uc_mcontext.gregs;
649 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
651 ucontext_t *ctx = (ucontext_t*)sigctx;
653 guint64 *gregs = gregs_from_ucontext (ctx);
655 mctx->rax = gregs [REG_RAX];
656 mctx->rbx = gregs [REG_RBX];
657 mctx->rcx = gregs [REG_RCX];
658 mctx->rdx = gregs [REG_RDX];
659 mctx->rbp = gregs [REG_RBP];
660 mctx->rsp = gregs [REG_RSP];
661 mctx->rsi = gregs [REG_RSI];
662 mctx->rdi = gregs [REG_RDI];
663 mctx->rip = gregs [REG_RIP];
664 mctx->r12 = gregs [REG_R12];
665 mctx->r13 = gregs [REG_R13];
666 mctx->r14 = gregs [REG_R14];
667 mctx->r15 = gregs [REG_R15];
671 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
673 ucontext_t *ctx = (ucontext_t*)sigctx;
675 guint64 *gregs = gregs_from_ucontext (ctx);
677 gregs [REG_RAX] = mctx->rax;
678 gregs [REG_RBX] = mctx->rbx;
679 gregs [REG_RCX] = mctx->rcx;
680 gregs [REG_RDX] = mctx->rdx;
681 gregs [REG_RBP] = mctx->rbp;
682 gregs [REG_RSP] = mctx->rsp;
683 gregs [REG_RSI] = mctx->rsi;
684 gregs [REG_RDI] = mctx->rdi;
685 gregs [REG_RIP] = mctx->rip;
686 gregs [REG_R12] = mctx->r12;
687 gregs [REG_R13] = mctx->r13;
688 gregs [REG_R14] = mctx->r14;
689 gregs [REG_R15] = mctx->r15;
693 mono_arch_ip_from_context (void *sigctx)
695 ucontext_t *ctx = (ucontext_t*)sigctx;
697 guint64 *gregs = gregs_from_ucontext (ctx);
699 return (gpointer)gregs [REG_RIP];