/* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
- Copyright (C) 1996-2005, 2006, 2007, 2008
+ Copyright (C) 1996-2005, 2006, 2007, 2008, 2009, 2010
CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
This file is part of CACAO.
#include "vm/jit/x86_64/codegen.h"
#include "vm/jit/x86_64/md.h"
-#include "mm/memory.h"
+#include "mm/memory.hpp"
#include "native/native.hpp"
md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
}
+/**
+ * Check if the trap instruction at the given PC is valid.
+ *
+ * @param pc Program counter.
+ *
+ * @return true if valid, false otherwise.
+ */
+bool patcher_is_valid_trap_instruction_at(void* pc)
+{
+ uint16_t mcode = *((uint16_t*) pc);
+
+ // Check for the undefined instruction we use.
+ return (mcode == 0x0b0f);
+}
+
+/**
+ * Overwrites the MFENCE instruction at the indicated address with a 3-byte
+ * NOP. The MFENCE instruction is not allowed to cross a (4-byte) word
+ * boundary.
+ *
+ * @param pc Program counter.
+ */
+static void patch_out_mfence(void *pc)
+{
+ uint32_t *p = (uint32_t*) (((uintptr_t) pc) & ~3);
+
+ assert((((uintptr_t) pc) & 3) < 2);
+ if (((uintptr_t) pc) & 1)
+ *p = (*p & 0x000000ff) | 0x001f0f00;
+ else
+ *p = (*p & 0xff000000) | 0x00001f0f;
+
+ md_icacheflush(p, 4);
+}
/* patcher_resolve_classref_to_classinfo ***************************************
if (c == NULL)
return false;
+ ra += PATCHER_CALL_SIZE;
+
// Patch class flags.
/* *datap = c->flags; */
*((int32_t*) (ra + 2)) = c->flags;
{
unresolved_field* uf = (unresolved_field*) pr->ref;
uintptr_t* datap = (uintptr_t*) pr->datap;
+ uint8_t* ra = (uint8_t*) pr->mpc;
// Resolve the field.
fieldinfo* fi = resolve_field_eager(uf);
if (fi == NULL)
return false;
+ ra += PATCHER_CALL_SIZE;
+
// Check if the field's class is initialized/
if (!(fi->clazz->state & CLASS_INITIALIZED))
if (!initialize_class(fi->clazz))
// Patch the field value's address.
*datap = (uintptr_t) fi->value;
+ if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
+ patch_out_mfence(ra + pr->disp_mb - 2);
+
// Synchronize data cache.
md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
if (fi == NULL)
return false;
+ pc += PATCHER_CALL_SIZE;
+
// Patch the field's offset: we check for the field type, because
// the instructions have different lengths.
if (IS_INT_LNG_TYPE(fi->type)) {
*((int32_t*) (pc + 5)) = fi->offset;
}
+ if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
+ patch_out_mfence(pc + pr->disp_mb - 2);
+
// Synchronize instruction cache.
md_icacheflush(pc, 6 + sizeof(int32_t));
if (fi == NULL)
return false;
+ pc += PATCHER_CALL_SIZE;
+
// Patch the field's offset.
if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
// Handle special case when the base register is %r12.
*((uint32_t*) (pc + 3)) = fi->offset;
}
+ if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
+ patch_out_mfence(pc + pr->disp_mb - 2);
+
// Synchronize instruction cache.
md_icacheflush(pc, 14 + sizeof(int32_t));
if (m == NULL)
return false;
+ pc += PATCHER_CALL_SIZE;
+
// Patch vftbl index.
*((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
if (m == NULL)
return false;
+ pc += PATCHER_CALL_SIZE;
+
// Patch interfacetable index.
*((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
if (c == NULL)
return false;
+ pc += PATCHER_CALL_SIZE;
+
// Patch super class index.
*((int32_t*) (pc + 7 + 3)) = c->index;
if (c == NULL)
return false;
+ pc += PATCHER_CALL_SIZE;
+
// Patch super class index.
*((int32_t*) (pc + 7 + 3)) = c->index;