#include <glib.h>
#include <signal.h>
#include <string.h>
-#ifndef PLATFORM_WIN32
-#include <sys/ucontext.h>
+#ifdef HAVE_UCONTEXT_H
+#include <ucontext.h>
#endif
#include <mono/arch/amd64/amd64-codegen.h>
#include "mini.h"
#include "mini-amd64.h"
+#include "tasklets.h"
+#include "debug-mini.h"
#define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
}
/* Copy context back */
- ctx->Rax = sctx->rax;
- ctx->Rbx = sctx->rbx;
- ctx->Rcx = sctx->rcx;
- ctx->Rdx = sctx->rdx;
+ /* Nonvolatile */
+ ctx->Rsp = sctx->rsp;
+ ctx->Rdi = sctx->rdi;
+ ctx->Rsi = sctx->rsi;
+ ctx->Rbx = sctx->rbx;
ctx->Rbp = sctx->rbp;
- ctx->Rsp = sctx->rsp;
- ctx->Rsi = sctx->rsi;
- ctx->Rdi = sctx->rdi;
- ctx->Rip = sctx->rip;
+ ctx->R12 = sctx->r12;
+ ctx->R13 = sctx->r13;
+ ctx->R14 = sctx->r14;
+ ctx->R15 = sctx->r15;
+ ctx->Rip = sctx->rip;
+
+ /* Volatile But should not matter?*/
+ ctx->Rax = sctx->rax;
+ ctx->Rcx = sctx->rcx;
+ ctx->Rdx = sctx->rdx;
g_free (sctx);
amd64_mov_reg_membase (code, AMD64_R14, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r14), 8);
amd64_mov_reg_membase (code, AMD64_R15, AMD64_R11, G_STRUCT_OFFSET (MonoContext, r15), 8);
- amd64_mov_reg_membase (code, AMD64_RSP, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
-
- /* get return address */
- amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
+ if (mono_running_on_valgrind ()) {
+ /* Prevent 'Address 0x... is just below the stack ptr.' errors */
+ amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
+ amd64_mov_reg_reg (code, AMD64_RSP, AMD64_R8, 8);
+ } else {
+ amd64_mov_reg_membase (code, AMD64_RSP, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rsp), 8);
+ /* get return address */
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, G_STRUCT_OFFSET (MonoContext, rip), 8);
+ }
/* jump to the saved IP */
amd64_jump_reg (code, AMD64_R11);
ctx.rcx = rcx;
ctx.rdx = rdx;
- if (!rethrow && mono_debugger_throw_exception ((gpointer)(rip - 8), (gpointer)rsp, exc)) {
- /*
- * The debugger wants us to stop on the `throw' instruction.
- * By the time we get here, it already inserted a breakpoint on
- * eip - 8 (which is the address of the `mov %r15,%rdi ; callq throw').
- */
-
- /* FIXME FIXME
- *
- * In case of a rethrow, the JIT is emitting code like this:
- *
- * mov 0xffffffffffffffd0(%rbp),%rax'
- * mov %rax,%rdi
- * callq throw
- *
- * Here, restore_context() wouldn't restore the %rax register correctly.
- */
- ctx.rip = rip - 8;
- ctx.rsp = rsp + 8;
- restore_context (&ctx);
- g_assert_not_reached ();
- }
-
- /* adjust eip so that it point into the call instruction */
- ctx.rip -= 1;
-
if (mono_object_isinst (exc, mono_defaults.exception_class)) {
MonoException *mono_ex = (MonoException*)exc;
if (!rethrow)
mono_ex->stack_trace = NULL;
}
+
+ if (mono_debug_using_mono_debugger ()) {
+ guint8 buf [16], *code;
+
+ mono_breakpoint_clean_code (NULL, (gpointer)rip, 8, buf, sizeof (buf));
+ code = buf + 8;
+
+ if (buf [3] == 0xe8) {
+ MonoContext ctx_cp = ctx;
+ ctx_cp.rip = rip - 5;
+
+ if (mono_debugger_handle_exception (&ctx_cp, exc)) {
+ restore_context (&ctx_cp);
+ g_assert_not_reached ();
+ }
+ }
+ }
+
+ /* adjust eip so that it point into the call instruction */
+ ctx.rip -= 1;
+
mono_handle_exception (&ctx, exc, (gpointer)rip, FALSE);
restore_context (&ctx);
*/
MonoJitInfo *
mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
- MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
- gboolean *managed)
+ MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
{
MonoJitInfo *ji;
- int i;
gpointer ip = MONO_CONTEXT_GET_IP (ctx);
/* Avoid costly table lookup during stack overflow */
*new_ctx = *ctx;
if (ji != NULL) {
- int offset;
- gboolean omit_fp = (ji->used_regs & (1 << 31)) > 0;
+ gssize regs [MONO_MAX_IREGS + 1];
+ guint8 *cfa;
+ guint32 unwind_info_len;
+ guint8 *unwind_info;
if (managed)
if (!ji->method->wrapper_type)
*managed = TRUE;
- /*
- * If a method has save_lmf set, then register save/restore code is not generated
- * by the JIT, so we have to restore callee saved registers from the lmf.
- */
- if (ji->method->save_lmf) {
- MonoLMF *lmf_addr;
-
- /*
- * *lmf might not point to the LMF pushed by this method, so compute the LMF
- * address instead.
- */
- if (omit_fp)
- lmf_addr = (MonoLMF*)ctx->rsp;
- else
- lmf_addr = (MonoLMF*)(ctx->rbp - sizeof (MonoLMF));
-
- new_ctx->rbp = lmf_addr->rbp;
- new_ctx->rbx = lmf_addr->rbx;
- new_ctx->r12 = lmf_addr->r12;
- new_ctx->r13 = lmf_addr->r13;
- new_ctx->r14 = lmf_addr->r14;
- new_ctx->r15 = lmf_addr->r15;
- }
- else {
- offset = omit_fp ? 0 : -1;
- /* restore caller saved registers */
- for (i = 0; i < AMD64_NREG; i ++)
- if (AMD64_IS_CALLEE_SAVED_REG (i) && (ji->used_regs & (1 << i))) {
- guint64 reg;
-
- if (omit_fp) {
- reg = *((guint64*)ctx->rsp + offset);
- offset ++;
- }
- else {
- reg = *((guint64 *)ctx->rbp + offset);
- offset --;
- }
-
- switch (i) {
- case AMD64_RBX:
- new_ctx->rbx = reg;
- break;
- case AMD64_R12:
- new_ctx->r12 = reg;
- break;
- case AMD64_R13:
- new_ctx->r13 = reg;
- break;
- case AMD64_R14:
- new_ctx->r14 = reg;
- break;
- case AMD64_R15:
- new_ctx->r15 = reg;
- break;
- case AMD64_RBP:
- new_ctx->rbp = reg;
- break;
- default:
- g_assert_not_reached ();
- }
- }
- }
+ if (ji->from_aot)
+ unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
+ else
+ unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
+
+ regs [AMD64_RAX] = new_ctx->rax;
+ regs [AMD64_RBX] = new_ctx->rbx;
+ regs [AMD64_RCX] = new_ctx->rcx;
+ regs [AMD64_RDX] = new_ctx->rdx;
+ regs [AMD64_RBP] = new_ctx->rbp;
+ regs [AMD64_RSP] = new_ctx->rsp;
+ regs [AMD64_RSI] = new_ctx->rsi;
+ regs [AMD64_RDI] = new_ctx->rdi;
+ regs [AMD64_RIP] = new_ctx->rip;
+ regs [AMD64_R12] = new_ctx->r12;
+ regs [AMD64_R13] = new_ctx->r13;
+ regs [AMD64_R14] = new_ctx->r14;
+ regs [AMD64_R15] = new_ctx->r15;
+
+ mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
+ (guint8*)ji->code_start + ji->code_size,
+ ip, regs, MONO_MAX_IREGS + 1, &cfa);
+
+ new_ctx->rax = regs [AMD64_RAX];
+ new_ctx->rbx = regs [AMD64_RBX];
+ new_ctx->rcx = regs [AMD64_RCX];
+ new_ctx->rdx = regs [AMD64_RDX];
+ new_ctx->rbp = regs [AMD64_RBP];
+ new_ctx->rsp = regs [AMD64_RSP];
+ new_ctx->rsi = regs [AMD64_RSI];
+ new_ctx->rdi = regs [AMD64_RDI];
+ new_ctx->rip = regs [AMD64_RIP];
+ new_ctx->r12 = regs [AMD64_R12];
+ new_ctx->r13 = regs [AMD64_R13];
+ new_ctx->r14 = regs [AMD64_R14];
+ new_ctx->r15 = regs [AMD64_R15];
+
+ /* The CFA becomes the new SP value */
+ new_ctx->rsp = (gssize)cfa;
+
+ /* Adjust IP */
+ new_ctx->rip --;
if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
/* remove any unused lmf */
*lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~1);
}
- if (omit_fp) {
- /* Pop frame */
- new_ctx->rsp += (ji->used_regs >> 16) & (0x7fff);
- new_ctx->rip = *((guint64 *)new_ctx->rsp) - 1;
- /* Pop return address */
- new_ctx->rsp += 8;
- }
- else {
- /* Pop EBP and the return address */
- new_ctx->rsp = ctx->rbp + (2 * sizeof (gpointer));
- /* we substract 1, so that the IP points into the call instruction */
- new_ctx->rip = *((guint64 *)ctx->rbp + 1) - 1;
- new_ctx->rbp = *((guint64 *)ctx->rbp);
- }
-
+#ifndef MONO_AMD64_NO_PUSHES
/* Pop arguments off the stack */
{
MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
new_ctx->rsp += stack_to_pop;
}
+#endif
return ji;
} else if (*lmf) {
ji = mono_jit_info_table_find (domain, (gpointer)rip);
if (!ji) {
- if (!(*lmf)->method)
- /* Top LMF entry */
- return (gpointer)-1;
- /* Trampoline lmf frame */
- memset (res, 0, sizeof (MonoJitInfo));
- res->method = (*lmf)->method;
+ // FIXME: This can happen with multiple appdomains (bug #444383)
+ return (gpointer)-1;
}
new_ctx->rip = rip;
new_ctx->r13 = (*lmf)->r13;
new_ctx->r14 = (*lmf)->r14;
new_ctx->r15 = (*lmf)->r15;
+#ifdef PLATFORM_WIN32
+ new_ctx->rdi = (*lmf)->rdi;
+ new_ctx->rsi = (*lmf)->rsi;
+#endif
*lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~1);
mono_arch_sigctx_to_monoctx (sigctx, &mctx);
+ if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
+ return TRUE;
+
mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), test_only);
mono_arch_monoctx_to_sigctx (&mctx, sigctx);
restore_context = mono_get_restore_context ();
mono_arch_sigctx_to_monoctx (sigctx, &mctx);
+
+ if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
+ if (stack_ovf)
+ prepare_for_guard_pages (&mctx);
+ restore_context (&mctx);
+ }
+
mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), FALSE);
if (stack_ovf)
prepare_for_guard_pages (&mctx);
#endif
}
-static guint64
-get_original_ip (void)
+guint64
+mono_amd64_get_original_ip (void)
{
MonoLMF *lmf = mono_get_lmf ();
return lmf->rip;
}
-static gpointer
-get_throw_pending_exception (void)
+gpointer
+mono_arch_get_throw_pending_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
{
- static guint8* start;
- static gboolean inited = FALSE;
- guint8 *code;
+ guint8 *code, *start;
guint8 *br[1];
gpointer throw_trampoline;
- if (inited)
- return start;
+ *ji = NULL;
start = code = mono_global_codeman_reserve (128);
amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
/* Obtain the pending exception */
- amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ } else {
+ amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
+ }
amd64_call_reg (code, AMD64_R11);
/* Check if it is NULL, and branch */
amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
/* Obtain the original ip and clear the flag in previous_lmf */
- amd64_mov_reg_imm (code, AMD64_R11, get_original_ip);
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ } else {
+ amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
+ }
amd64_call_reg (code, AMD64_R11);
/* Load exc */
amd64_push_reg (code, AMD64_RAX);
/* Call the throw trampoline */
- throw_trampoline = mono_get_throw_exception ();
- amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_throw_exception");
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ } else {
+ throw_trampoline = mono_get_throw_exception ();
+ amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
+ }
/* We use a jump instead of a call so we can push the original ip on the stack */
amd64_jump_reg (code, AMD64_R11);
mono_amd64_patch (br [0], code);
/* Obtain the original ip and clear the flag in previous_lmf */
- amd64_mov_reg_imm (code, AMD64_R11, get_original_ip);
+ if (aot) {
+ *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
+ amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
+ } else {
+ amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
+ }
amd64_call_reg (code, AMD64_R11);
amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
g_assert ((code - start) < 128);
- inited = TRUE;
+ *code_size = code - start;
return start;
}
+static gpointer throw_pending_exception;
+
/*
* Called when a thread receives an async exception while executing unmanaged code.
* Instead of checking for this exception in the managed-to-native wrapper, we hijack
/* Signal that lmf->rip is set */
lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
- *(gpointer*)(lmf->rsp - 8) = get_throw_pending_exception ();
+ *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
+}
+
+void
+mono_arch_exceptions_init (void)
+{
+ guint32 code_size;
+ MonoJumpInfo *ji;
+
+ if (mono_aot_only) {
+ throw_pending_exception = mono_aot_get_named_code ("throw_pending_exception");
+ } else {
+ /* Call this to avoid initialization races */
+ throw_pending_exception = mono_arch_get_throw_pending_exception_full (&code_size, &ji, FALSE);
+ }
}
#ifdef PLATFORM_WIN32
(sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
}
+PRUNTIME_FUNCTION
+MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
+{
+ MonoJitInfo *ji;
+ guint64 pos;
+ PMonoUnwindInfo targetinfo;
+ MonoDomain *domain = mono_domain_get ();
+
+ ji = mono_jit_info_table_find (domain, (char*)ControlPc);
+ if (!ji)
+ return 0;
+
+ pos = (guint64)(((char*)ji->code_start) + ji->code_size);
+
+ targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
+
+ targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
+
+ return &targetinfo->runtimeFunction;
+}
+
void
mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
{
g_free (unwindinfo);
*monoui = 0;
- RtlAddFunctionTable (&targetinfo->runtimeFunction, 1, (DWORD64)code);
+ RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
}
#endif
+#if MONO_SUPPORT_TASKLETS
+MonoContinuationRestore
+mono_tasklets_arch_restore (void)
+{
+ static guint8* saved = NULL;
+ guint8 *code, *start;
+ int cont_reg = AMD64_R9; /* register usable on both call conventions */
+
+ if (saved)
+ return (MonoContinuationRestore)saved;
+ code = start = mono_global_codeman_reserve (64);
+ /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
+ /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
+ * state is in AMD64_ARG_REG2 ($rdx or $rsi)
+ * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
+ * We move cont to cont_reg since we need both rcx and rdi for the copy
+ * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
+ */
+ amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
+ amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
+ /* setup the copy of the stack */
+ amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
+ amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
+ x86_cld (code);
+ amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
+ amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
+ amd64_prefix (code, X86_REP_PREFIX);
+ amd64_movsl (code);
+
+ /* now restore the registers from the LMF */
+ amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, lmf), 8);
+ amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbx), 8);
+ amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbp), 8);
+ amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
+ amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
+ amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
+ amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
+#ifdef PLATFORM_WIN32
+ amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
+ amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
+#endif
+ amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsp), 8);
+
+ /* restore the lmf chain */
+ /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
+ x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
+ /* state is already in rax */
+ amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
+ g_assert ((code - start) <= 64);
+ saved = start;
+ return (MonoContinuationRestore)saved;
+}
+#endif