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 */
101 void win32_seh_init()
103 old_handler = SetUnhandledExceptionFilter(seh_handler);
106 void win32_seh_cleanup()
108 if (old_handler) SetUnhandledExceptionFilter(old_handler);
111 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
115 fpe_handler = handler;
118 ill_handler = handler;
121 segv_handler = handler;
128 #endif /* PLATFORM_WIN32 */
131 * mono_arch_get_restore_context:
133 * Returns a pointer to a method which restores a previously saved sigcontext.
136 mono_arch_get_restore_context (void)
138 static guint8 *start = NULL;
139 static gboolean inited = FALSE;
145 /* restore_contect (MonoContext *ctx) */
147 start = code = mono_global_codeman_reserve (256);
149 /* get return address */
150 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rip), 8);
152 /* Restore registers */
153 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbp), 8);
154 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbx), 8);
155 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r12), 8);
156 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r13), 8);
157 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r14), 8);
158 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r15), 8);
160 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rsp), 8);
162 /* jump to the saved IP */
163 amd64_jump_reg (code, AMD64_RAX);
171 * mono_arch_get_call_filter:
173 * Returns a pointer to a method which calls an exception filter. We
174 * also use this function to call finally handlers (we pass NULL as
175 * @exc object in this case).
178 mono_arch_get_call_filter (void)
180 static guint8 *start;
181 static gboolean inited = FALSE;
189 start = code = mono_global_codeman_reserve (64);
191 /* call_filter (MonoContext *ctx, unsigned long eip) */
194 /* Alloc new frame */
195 amd64_push_reg (code, AMD64_RBP);
196 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
198 /* Save callee saved regs */
200 for (i = 0; i < AMD64_NREG; ++i)
201 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
202 amd64_push_reg (code, i);
208 amd64_push_reg (code, AMD64_RBP);
210 /* Make stack misaligned, the call will make it aligned again */
212 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
215 amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbp), 8);
216 /* load callee saved regs */
217 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, rbx), 8);
218 amd64_mov_reg_membase (code, AMD64_R12, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r12), 8);
219 amd64_mov_reg_membase (code, AMD64_R13, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r13), 8);
220 amd64_mov_reg_membase (code, AMD64_R14, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r14), 8);
221 amd64_mov_reg_membase (code, AMD64_R15, AMD64_RDI, G_STRUCT_OFFSET (MonoContext, r15), 8);
223 /* call the handler */
224 amd64_call_reg (code, AMD64_RSI);
227 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
230 amd64_pop_reg (code, AMD64_RBP);
232 /* Restore callee saved regs */
233 for (i = AMD64_NREG; i >= 0; --i)
234 if (AMD64_IS_CALLEE_SAVED_REG (i))
235 amd64_pop_reg (code, i);
240 g_assert ((code - start) < 64);
248 throw_exception (MonoObject *exc, guint64 rip, guint64 rsp,
249 guint64 rbx, guint64 rbp, guint64 r12, guint64 r13,
250 guint64 r14, guint64 r15, guint64 rethrow)
252 static void (*restore_context) (MonoContext *);
255 if (!restore_context)
256 restore_context = mono_arch_get_restore_context ();
267 if (mono_debugger_throw_exception ((gpointer)(rip - 8), (gpointer)rsp, exc)) {
269 * The debugger wants us to stop on the `throw' instruction.
270 * By the time we get here, it already inserted a breakpoint on
271 * eip - 8 (which is the address of the `mov %r15,%rdi ; callq throw').
275 restore_context (&ctx);
276 g_assert_not_reached ();
279 /* adjust eip so that it point into the call instruction */
282 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
283 MonoException *mono_ex = (MonoException*)exc;
285 mono_ex->stack_trace = NULL;
287 mono_handle_exception (&ctx, exc, (gpointer)rip, FALSE);
288 restore_context (&ctx);
290 g_assert_not_reached ();
294 get_throw_trampoline (gboolean rethrow)
299 start = code = mono_global_codeman_reserve (64);
304 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 8);
306 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RSP, 0, 8);
308 amd64_lea_membase (code, AMD64_RDX, AMD64_RSP, 8);
309 /* Callee saved regs */
310 amd64_mov_reg_reg (code, AMD64_RCX, AMD64_RBX, 8);
311 amd64_mov_reg_reg (code, AMD64_R8, AMD64_RBP, 8);
312 amd64_mov_reg_reg (code, AMD64_R9, AMD64_R12, 8);
314 amd64_push_imm (code, 0);
316 amd64_push_imm (code, rethrow);
317 amd64_push_reg (code, AMD64_R15);
318 amd64_push_reg (code, AMD64_R14);
319 amd64_push_reg (code, AMD64_R13);
321 amd64_mov_reg_imm (code, AMD64_R11, throw_exception);
322 amd64_call_reg (code, AMD64_R11);
323 amd64_breakpoint (code);
325 g_assert ((code - start) < 64);
331 * mono_arch_get_throw_exception:
333 * Returns a function pointer which can be used to raise
334 * exceptions. The returned function has the following
335 * signature: void (*func) (MonoException *exc);
339 mono_arch_get_throw_exception (void)
341 static guint8* start;
342 static gboolean inited = FALSE;
347 start = get_throw_trampoline (FALSE);
355 mono_arch_get_rethrow_exception (void)
357 static guint8* start;
358 static gboolean inited = FALSE;
363 start = get_throw_trampoline (TRUE);
371 mono_arch_get_throw_exception_by_name (void)
373 static guint8* start;
374 static gboolean inited = FALSE;
380 start = code = mono_global_codeman_reserve (64);
382 /* Not used on amd64 */
383 amd64_breakpoint (code);
389 * mono_arch_get_throw_corlib_exception:
391 * Returns a function pointer which can be used to raise
392 * corlib exceptions. The returned function has the following
393 * signature: void (*func) (guint32 ex_token, guint32 offset);
394 * Here, offset is the offset which needs to be substracted from the caller IP
395 * to get the IP of the throw. Passing the offset has the advantage that it
396 * needs no relocations in the caller.
399 mono_arch_get_throw_corlib_exception (void)
401 static guint8* start;
402 static gboolean inited = FALSE;
409 start = code = mono_global_codeman_reserve (64);
412 amd64_push_reg (code, AMD64_RSI);
414 /* Call exception_from_token */
415 amd64_mov_reg_reg (code, AMD64_RSI, AMD64_RDI, 8);
416 amd64_mov_reg_imm (code, AMD64_RDI, mono_defaults.exception_class->image);
417 amd64_mov_reg_imm (code, AMD64_R11, mono_exception_from_token);
418 amd64_call_reg (code, AMD64_R11);
420 /* Compute throw_ip */
421 amd64_pop_reg (code, AMD64_RSI);
423 amd64_pop_reg (code, AMD64_RDX);
424 amd64_alu_reg_reg (code, X86_SUB, AMD64_RDX, AMD64_RSI);
426 /* Put the throw_ip at the top of the misaligned stack */
427 amd64_push_reg (code, AMD64_RDX);
429 throw_ex = (guint64)mono_arch_get_throw_exception ();
431 /* Call throw_exception */
432 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RAX, 8);
433 amd64_mov_reg_imm (code, AMD64_R11, throw_ex);
434 /* The original IP is on the stack */
435 amd64_jump_reg (code, AMD64_R11);
437 g_assert ((code - start) < 64);
444 /* mono_arch_find_jit_info:
446 * This function is used to gather information from @ctx. It return the
447 * MonoJitInfo of the corresponding function, unwinds one stack frame and
448 * stores the resulting context into @new_ctx. It also stores a string
449 * describing the stack location into @trace (if not NULL), and modifies
450 * the @lmf if necessary. @native_offset return the IP offset from the
451 * start of the function or -1 if that info is not available.
454 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
455 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
460 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
462 /* Avoid costly table lookup during stack overflow */
463 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
466 ji = mono_jit_info_table_find (domain, ip);
477 if (!ji->method->wrapper_type)
481 * Some managed methods like pinvoke wrappers might have save_lmf set.
482 * In this case, register save/restore code is not generated by the
483 * JIT, so we have to restore callee saved registers from the lmf.
485 if (ji->method->save_lmf) {
487 * We only need to do this if the exception was raised in managed
488 * code, since otherwise the lmf was already popped of the stack.
490 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
491 new_ctx->rbx = (*lmf)->rbx;
492 new_ctx->r12 = (*lmf)->r12;
493 new_ctx->r13 = (*lmf)->r13;
494 new_ctx->r14 = (*lmf)->r14;
495 new_ctx->r15 = (*lmf)->r15;
500 /* restore caller saved registers */
501 for (i = 0; i < AMD64_NREG; i ++)
502 if (AMD64_IS_CALLEE_SAVED_REG (i) && (ji->used_regs & (1 << i))) {
503 guint64 reg = *((guint64 *)ctx->SC_EBP + offset);
522 g_assert_not_reached ();
527 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
528 /* remove any unused lmf */
529 *lmf = (*lmf)->previous_lmf;
532 /* Pop EBP and the return address */
533 new_ctx->SC_ESP = ctx->SC_EBP + (2 * sizeof (gpointer));
534 /* we substract 1, so that the IP points into the call instruction */
535 new_ctx->SC_EIP = *((guint64 *)ctx->SC_EBP + 1) - 1;
536 new_ctx->SC_EBP = *((guint64 *)ctx->SC_EBP);
538 /* Pop arguments off the stack */
540 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
542 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
543 new_ctx->SC_ESP += stack_to_pop;
554 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->rip))) {
556 memset (res, 0, sizeof (MonoJitInfo));
557 res->method = (*lmf)->method;
560 new_ctx->SC_RIP = (*lmf)->rip;
561 new_ctx->SC_RBP = (*lmf)->ebp;
563 new_ctx->SC_RBX = (*lmf)->rbx;
564 new_ctx->SC_R12 = (*lmf)->r12;
565 new_ctx->SC_R13 = (*lmf)->r13;
566 new_ctx->SC_R14 = (*lmf)->r14;
567 new_ctx->SC_R15 = (*lmf)->r15;
569 /* the lmf is always stored on the stack, so the following
570 * expression points to a stack location which can be used as ESP */
571 new_ctx->SC_ESP = ALIGN_TO ((guint64)&((*lmf)->rip), 16);
573 *lmf = (*lmf)->previous_lmf;
575 return ji ? ji : res;
582 * mono_arch_handle_exception:
584 * @ctx: saved processor state
585 * @obj: the exception object
588 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
590 ucontext_t *ctx = (ucontext_t*)sigctx;
593 mctx.rax = ctx->uc_mcontext.gregs [REG_RAX];
594 mctx.rbx = ctx->uc_mcontext.gregs [REG_RBX];
595 mctx.rcx = ctx->uc_mcontext.gregs [REG_RCX];
596 mctx.rdx = ctx->uc_mcontext.gregs [REG_RDX];
597 mctx.rbp = ctx->uc_mcontext.gregs [REG_RBP];
598 mctx.rsp = ctx->uc_mcontext.gregs [REG_RSP];
599 mctx.rsi = ctx->uc_mcontext.gregs [REG_RSI];
600 mctx.rdi = ctx->uc_mcontext.gregs [REG_RDI];
601 mctx.rip = ctx->uc_mcontext.gregs [REG_RIP];
602 mctx.r12 = ctx->uc_mcontext.gregs [REG_R12];
603 mctx.r13 = ctx->uc_mcontext.gregs [REG_R13];
604 mctx.r14 = ctx->uc_mcontext.gregs [REG_R14];
605 mctx.r15 = ctx->uc_mcontext.gregs [REG_R15];
607 mono_handle_exception (&mctx, obj, (gpointer)mctx.rip, test_only);
609 ctx->uc_mcontext.gregs [REG_RAX] = mctx.rax;
610 ctx->uc_mcontext.gregs [REG_RBX] = mctx.rbx;
611 ctx->uc_mcontext.gregs [REG_RCX] = mctx.rcx;
612 ctx->uc_mcontext.gregs [REG_RDX] = mctx.rdx;
613 ctx->uc_mcontext.gregs [REG_RBP] = mctx.rbp;
614 ctx->uc_mcontext.gregs [REG_RSP] = mctx.rsp;
615 ctx->uc_mcontext.gregs [REG_RSI] = mctx.rsi;
616 ctx->uc_mcontext.gregs [REG_RDI] = mctx.rdi;
617 ctx->uc_mcontext.gregs [REG_RIP] = mctx.rip;
618 ctx->uc_mcontext.gregs [REG_R12] = mctx.r12;
619 ctx->uc_mcontext.gregs [REG_R13] = mctx.r13;
620 ctx->uc_mcontext.gregs [REG_R14] = mctx.r14;
621 ctx->uc_mcontext.gregs [REG_R15] = mctx.r15;
627 mono_arch_ip_from_context (void *sigctx)
629 ucontext_t *ctx = (ucontext_t*)sigctx;
630 return (gpointer)ctx->uc_mcontext.gregs [REG_RIP];