/* src/vm/jit/replace.c - on-stack replacement of methods
- Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
- C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
- E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
- J. Wenninger, Institut f. Computersprachen - TU Wien
+ Copyright (C) 1996-2005, 2006, 2007, 2008
+ CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
This file is part of CACAO.
#include "vm/types.h"
#include <assert.h>
+#include <stdint.h>
#include <stdlib.h>
#include "arch.h"
+#include "md.h"
#if defined(ENABLE_GC_CACAO)
# include "mm/cacao-gc/gc.h"
#include "mm/memory.h"
-#include "threads/threads-common.h"
+#include "threads/thread.hpp"
#include "toolbox/logging.h"
-#include "vm/stringlocal.h"
+#include "vm/classcache.h"
+#include "vm/globals.hpp"
+#include "vm/options.h"
+#include "vm/string.hpp"
+
+#if defined(ENABLE_RT_TIMING)
+# include "vm/rt-timing.h"
+#endif
#include "vm/jit/abi.h"
#include "vm/jit/asmpart.h"
#include "vm/jit/disass.h"
+#include "vm/jit/executionstate.h"
#include "vm/jit/jit.h"
-#include "vm/jit/md.h"
#include "vm/jit/methodheader.h"
#include "vm/jit/replace.h"
#include "vm/jit/show.h"
#include "vm/jit/stack.h"
-#include "vmcore/options.h"
-#include "vmcore/classcache.h"
-
#define REPLACE_PATCH_DYNAMIC_CALL
/*#define REPLACE_PATCH_ALL*/
#undef REPLACE_RA_TOP_OF_FRAME
#undef REPLACE_RA_LINKAGE_AREA
#undef REPLACE_LEAFMETHODS_RA_REGISTER
-#undef REPLACE_REG_RA
/* i386, x86_64 and m68k */
#if defined(__I386__) || defined(__X86_64__) || defined(__M68K__)
#elif defined(__ALPHA__)
#define REPLACE_RA_TOP_OF_FRAME
#define REPLACE_LEAFMETHODS_RA_REGISTER
-#define REPLACE_REG_RA REG_RA
/* powerpc */
#elif defined(__POWERPC__)
#define REPLACE_RA_LINKAGE_AREA
#define REPLACE_LEAFMETHODS_RA_REGISTER
-#define REPLACE_REG_RA REG_ITMP3 /* the execution state has the LR in itmp3 */
/* s390 */
#elif defined(__S390__)
#define REPLACE_RA_TOP_OF_FRAME
-#define REPLACE_REG_RA REG_ITMP3
#endif
#if !defined(NDEBUG)
static void java_value_print(s4 type, replace_val_t value);
-static void replace_stackframeinfo_println(stackframeinfo *sfi);
+static void replace_stackframeinfo_println(stackframeinfo_t *sfi);
#endif
#if !defined(NDEBUG)
m = code->m;
- /* set codeinfo flags */
-
- if (jd->isleafmethod)
- CODE_SETFLAG_LEAFMETHOD(code);
-
/* in instance methods, we may need a rplpoint at the method entry */
#if defined(REPLACE_PATCH_DYNAMIC_CALL)
rplpoint *rp;
s4 i;
s4 count;
- s4 index;
u1 *savedmcode;
assert(code->savedmcode == NULL);
/* count trappable replacement points */
count = 0;
- index = 0;
i = code->rplpointcount;
rp = code->rplpoints;
for (; i--; rp++) {
if (rp->flags & RPLPOINT_FLAG_NOTRAP)
continue;
- index++;
-
if (mappable && (rp->type == RPLPOINT_TYPE_RETURN))
continue;
if (rp->flags & RPLPOINT_FLAG_NOTRAP)
continue;
- index--;
-
if (mappable && (rp->type == RPLPOINT_TYPE_RETURN))
continue;
savedmcode -= REPLACEMENT_PATCH_SIZE;
-#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__) || defined(__S390__)) && defined(ENABLE_JIT)
- md_patch_replacement_point(code, index, rp, savedmcode);
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_DISASSEMBLER)
+ DOLOG( printf("\tinstruction before: ");
+ disassinstr(rp->pc); fflush(stdout); );
+# endif
+
+ md_patch_replacement_point(rp->pc, savedmcode, false);
+
+# if defined(ENABLE_DISASSEMBLER)
+ DOLOG( printf("\tinstruction after : ");
+ disassinstr(rp->pc); fflush(stdout); );
+# endif
#endif
+
rp->flags |= RPLPOINT_FLAG_ACTIVE;
}
DOLOG( printf("deactivate replacement point:\n");
replace_replacement_point_println(rp, 1); fflush(stdout); );
-#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__) || defined(__S390__)) && defined(ENABLE_JIT)
- md_patch_replacement_point(code, -1, rp, savedmcode);
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_DISASSEMBLER)
+ DOLOG( printf("\tinstruction before: ");
+ disassinstr(rp->pc); fflush(stdout); );
+# endif
+
+ md_patch_replacement_point(rp->pc, savedmcode, true);
+
+# if defined(ENABLE_DISASSEMBLER)
+ DOLOG( printf("\tinstruction before: ");
+ disassinstr(rp->pc); fflush(stdout); );
+# endif
#endif
rp->flags &= ~RPLPOINT_FLAG_ACTIVE;
replace_read_value(es, &instra, &(frame->instance));
#endif
}
+#if defined(__I386__)
+ else if (!(rp->method->flags & ACC_STATIC)) {
+ /* On i386 we always pass the first argument on stack. */
+ frame->instance.a = *(java_object_t **)(basesp + 1);
+ }
+#endif
#endif /* defined(REPLACE_PATCH_DYNAMIC_CALL) */
/* read stack slots */
if (!topframe && ra->index == RPLALLOC_PARAM) {
/* skip it */
+ /*
+ ra->index = RPLALLOC_PARAM;
+ replace_val_t v;
+ v.l = 0;
+ replace_write_value(es,ra,&v);
+ */
}
else {
assert(i < frame->javastackdepth);
}
-/* replace_pop_activation_record ***********************************************
+/* md_pop_stackframe ***********************************************************
+
+ Restore callee-saved registers (including the RA register),
+ set the stack pointer to the next stackframe,
+ set the PC to the return address of the popped frame.
- Peel a stack frame from the execution state.
-
*** This function imitates the effects of the method epilog ***
*** and returning from the method call. ***
IN:
- es...............execution state
- frame............source frame, receives synchronization slots
+ es...............execution state
OUT:
*es..............the execution state after popping the stack frame
-
+ NOTE: es->code and es->pv are NOT updated.
+
*******************************************************************************/
-u1* replace_pop_activation_record(executionstate_t *es,
- sourceframe_t *frame)
+void md_pop_stackframe(executionstate_t *es)
{
u1 *ra;
- u1 *pv;
+ s4 ra_align_off;
s4 reg;
s4 i;
- s4 count;
- codeinfo *code;
stackslot_t *basesp;
stackslot_t *sp;
assert(es->code);
- assert(frame);
+
+ /* alignment offset of RA */
+
+ ra_align_off = 0;
+#if defined(REPLACE_RA_BETWEEN_FRAMES)
+ if (es->code->stackframesize)
+ ra_align_off = SIZE_OF_STACKSLOT - SIZEOF_VOID_P;
+#endif
/* read the return address */
#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
- if (CODE_IS_LEAFMETHOD(es->code))
- ra = (u1*) (ptrint) es->intregs[REPLACE_REG_RA];
+ if (code_is_leafmethod(es->code))
+ ra = es->ra;
else
#endif
ra = md_stacktrace_get_returnaddress(es->sp,
- SIZE_OF_STACKSLOT * es->code->stackframesize);
-
- DOLOG( printf("return address: %p\n", (void*)ra); );
-
- assert(ra);
+ SIZE_OF_STACKSLOT * es->code->stackframesize + ra_align_off);
/* calculate the base of the stack frame */
sp = (stackslot_t *) es->sp;
basesp = sp + es->code->stackframesize;
- /* read slots used for synchronization */
-
- assert(frame->syncslotcount == 0);
- assert(frame->syncslots == NULL);
- count = code_get_sync_slot_count(es->code);
- frame->syncslotcount = count;
- frame->syncslots = DMNEW(replace_val_t, count);
- for (i=0; i<count; ++i) {
- frame->syncslots[i].p = sp[es->code->memuse + i]; /* XXX */
- }
-
/* restore return address, if part of frame */
#if defined(REPLACE_RA_TOP_OF_FRAME)
#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
- if (!CODE_IS_LEAFMETHOD(es->code))
+ if (!code_is_leafmethod(es->code))
#endif
- es->intregs[REPLACE_REG_RA] = *--basesp;
+ es->ra = (u1*) (ptrint) *--basesp;
#endif /* REPLACE_RA_TOP_OF_FRAME */
#if defined(REPLACE_RA_LINKAGE_AREA)
#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
- if (!CODE_IS_LEAFMETHOD(es->code))
+ if (!code_is_leafmethod(es->code))
#endif
- es->intregs[REPLACE_REG_RA] = basesp[LA_LR_OFFSET / sizeof(stackslot_t)];
+ es->ra = (u1*) (ptrint) basesp[LA_LR_OFFSET / sizeof(stackslot_t)];
#endif /* REPLACE_RA_LINKAGE_AREA */
/* restore saved int registers */
es->sp += SIZE_OF_STACKSLOT * es->code->stackframesize;
#if defined(REPLACE_RA_BETWEEN_FRAMES)
- es->sp += SIZE_OF_STACKSLOT; /* skip return address */
+ es->sp += ra_align_off + SIZEOF_VOID_P; /* skip return address */
#endif
- /* Set the new pc. Subtract one so we do not hit the replacement point */
- /* of the instruction following the call, if there is one. */
-
- es->pc = ra - 1;
+ /* set the program counter to the return address */
- /* find the new codeinfo */
-
- pv = md_codegen_get_pv_from_pc(ra);
-
- DOLOG( printf("PV = %p\n", (void*) pv); );
-
- if (pv == NULL) /* XXX can this really happen? */
- return NULL;
-
- code = *(codeinfo **)(pv + CodeinfoPointer);
-
- DOLOG( printf("CODE = %p\n", (void*) code); );
-
- /* return NULL if we reached native code */
-
- es->pv = pv;
- es->code = code;
+ es->pc = ra;
/* in debugging mode clobber non-saved registers */
#if !defined(NDEBUG)
/* for debugging */
for (i=0; i<INT_REG_CNT; ++i)
- if ((nregdescint[i] != REG_SAV)
-#if defined(REG_RA)
- && (i != REPLACE_REG_RA)
-#endif
- )
+ if (nregdescint[i] != REG_SAV)
es->intregs[i] = (ptrint) 0x33dead3333dead33ULL;
for (i=0; i<FLT_REG_CNT; ++i)
if (nregdescfloat[i] != REG_SAV)
es->adrregs[i] = (ptrint) 0x33dead3333dead33ULL;
# endif
#endif /* !defined(NDEBUG) */
+}
+
+
+/* md_push_stackframe **********************************************************
+
+ Save the given return address, build the new stackframe,
+ and store callee-saved registers.
+
+ *** This function imitates the effects of a call and the ***
+ *** method prolog of the callee. ***
+
+ IN:
+ es...............execution state
+ calleecode.......the code we are "calling"
+ ra...............the return address to save
+
+ OUT:
+ *es..............the execution state after pushing the stack frame
+ NOTE: es->pc, es->code, and es->pv are NOT updated.
+
+*******************************************************************************/
+
+void md_push_stackframe(executionstate_t *es, codeinfo *calleecode, u1 *ra)
+{
+ s4 reg;
+ s4 i;
+ stackslot_t *basesp;
+ stackslot_t *sp;
+
+ assert(es);
+ assert(calleecode);
+
+ /* write the return address */
+
+#if defined(REPLACE_RA_BETWEEN_FRAMES)
+ es->sp -= SIZEOF_VOID_P;
+ *((void **)es->sp) = (void *) ra;
+ if (calleecode->stackframesize)
+ es->sp -= (SIZE_OF_STACKSLOT - SIZEOF_VOID_P);
+#endif /* REPLACE_RA_BETWEEN_FRAMES */
+
+ es->ra = (u1*) (ptrint) ra;
+
+ /* build the stackframe */
+
+ DOLOG( printf("building stackframe of %d words at %p\n",
+ calleecode->stackframesize, (void*)es->sp); );
+
+ sp = (stackslot_t *) es->sp;
+ basesp = sp;
+
+ sp -= calleecode->stackframesize;
+ es->sp = (u1*) sp;
+
+ /* in debug mode, invalidate stack frame first */
+
+ /* XXX may not invalidate linkage area used by native code! */
+
+#if !defined(NDEBUG) && 0
+ for (i=0; i< (basesp - sp) && i < 1; ++i) {
+ sp[i] = 0xdeaddeadU;
+ }
+#endif
+
+#if defined(__I386__)
+ /* Stackslot 0 may contain the object instance for vftbl patching.
+ Destroy it, so there's no undefined value used. */
+ if ((basesp - sp) > 0) {
+ sp[0] = 0;
+ }
+#endif
+
+ /* save the return address register */
+
+#if defined(REPLACE_RA_TOP_OF_FRAME)
+#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
+ if (!code_is_leafmethod(calleecode))
+#endif
+ *--basesp = (ptrint) ra;
+#endif /* REPLACE_RA_TOP_OF_FRAME */
+
+#if defined(REPLACE_RA_LINKAGE_AREA)
+#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
+ if (!code_is_leafmethod(calleecode))
+#endif
+ basesp[LA_LR_OFFSET / sizeof(stackslot_t)] = (ptrint) ra;
+#endif /* REPLACE_RA_LINKAGE_AREA */
+
+ /* save int registers */
+
+ reg = INT_REG_CNT;
+ for (i=0; i<calleecode->savedintcount; ++i) {
+ while (nregdescint[--reg] != REG_SAV)
+ ;
+ *--basesp = es->intregs[reg];
+
+ /* XXX may not clobber saved regs used by native code! */
+#if !defined(NDEBUG) && 0
+ es->intregs[reg] = (ptrint) 0x44dead4444dead44ULL;
+#endif
+ }
+
+ /* save flt registers */
+
+ /* XXX align? */
+ reg = FLT_REG_CNT;
+ for (i=0; i<calleecode->savedfltcount; ++i) {
+ while (nregdescfloat[--reg] != REG_SAV)
+ ;
+ basesp -= STACK_SLOTS_PER_FLOAT;
+ *(double*)basesp = es->fltregs[reg];
+
+ /* XXX may not clobber saved regs used by native code! */
+#if !defined(NDEBUG) && 0
+ *(u8*)&(es->fltregs[reg]) = 0x44dead4444dead44ULL;
+#endif
+ }
+
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+ /* save adr registers */
+
+ reg = ADR_REG_CNT;
+ for (i=0; i<calleecode->savedadrcount; ++i) {
+ while (nregdescadr[--reg] != REG_SAV)
+ ;
+ *--basesp = es->adrregs[reg];
+
+ /* XXX may not clobber saved regs used by native code! */
+#if !defined(NDEBUG) && 0
+ es->adrregs[reg] = (ptrint) 0x44dead4444dead44ULL;
+#endif
+ }
+#endif
+}
+
+
+/* replace_pop_activation_record ***********************************************
+
+ Peel a stack frame from the execution state.
+
+ *** This function imitates the effects of the method epilog ***
+ *** and returning from the method call. ***
+
+ IN:
+ es...............execution state
+ frame............source frame, receives synchronization slots
+
+ OUT:
+ *es..............the execution state after popping the stack frame
+
+ RETURN VALUE:
+ the return address of the poped activation record
+
+*******************************************************************************/
+
+u1* replace_pop_activation_record(executionstate_t *es,
+ sourceframe_t *frame)
+{
+ u1 *ra;
+ u1 *pv;
+ s4 i;
+ s4 count;
+ codeinfo *code;
+ stackslot_t *sp;
+
+ assert(es->code);
+ assert(frame);
+
+ /* calculate the base of the stack frame */
+
+ sp = (stackslot_t *) es->sp;
+ assert(frame->syncslotcount == 0);
+ assert(frame->syncslots == NULL);
+ count = code_get_sync_slot_count(es->code);
+ frame->syncslotcount = count;
+ frame->syncslots = DMNEW(replace_val_t, count);
+ for (i=0; i<count; ++i) {
+ frame->syncslots[i].p = sp[es->code->memuse + i]; /* XXX md_ function */
+ }
+
+ /* pop the stackframe */
+
+ md_pop_stackframe(es);
+
+ ra = es->pc;
+
+ DOLOG( printf("RA = %p\n", (void*)ra); );
+
+ /* Subtract one from the PC so we do not hit the replacement point */
+ /* of the instruction following the call, if there is one. */
+
+ es->pc--;
+
+ /* find the new codeinfo */
+
+ pv = md_codegen_get_pv_from_pc(ra);
+ DOLOG( printf("PV = %p\n", (void*) pv); );
+
+ code = code_get_codeinfo_for_pv(pv);
+ DOLOG( printf("CODE = %p\n", (void*) code); );
+
+ /* return NULL if we reached native code */
+
+ es->pv = pv;
+ es->code = code;
return (code) ? ra : NULL;
}
(void*) *mpp, (void*)entrypoint); );
#if !defined(NDEBUG)
- oldcode = *(codeinfo **)((u1*)(*mpp) + CodeinfoPointer);
- newcode = *(codeinfo **)((u1*)(entrypoint) + CodeinfoPointer);
+ oldcode = code_get_codeinfo_for_pv(*mpp);
+ newcode = code_get_codeinfo_for_pv(entrypoint);
DOLOG_SHORT( printf("\tpatch %s %p ", kind, (void*) oldcode);
method_println(oldcode->m);
u1 *entrypoint;
};
-#define CODEINFO_OF_CODE(entrypoint) \
- (*(codeinfo **)((u1*)(entrypoint) + CodeinfoPointer))
-
-#define METHOD_OF_CODE(entrypoint) \
- (CODEINFO_OF_CODE(entrypoint)->m)
-
void replace_patch_callback(classinfo *c, struct replace_patch_data_t *pd)
{
vftbl_t *vftbl = c->vftbl;
if (vftbl != NULL
&& vftbl->vftbllength > pd->m->vftblindex
&& vftbl->table[pd->m->vftblindex] != &asm_abstractmethoderror
- && METHOD_OF_CODE(vftbl->table[pd->m->vftblindex]) == pd->m)
+ && code_get_methodinfo_for_pv(vftbl->table[pd->m->vftblindex]) == pd->m)
{
replace_patch_class(c->vftbl, pd->m, pd->oldentrypoint, pd->entrypoint);
}
sourceframe_t *callerframe,
sourceframe_t *calleeframe)
{
- u1 *patchpos;
- methodptr entrypoint;
- methodptr oldentrypoint;
- bool atentry;
- stackframeinfo sfi;
- codeinfo *calleecode;
- methodinfo *calleem;
- java_object_t *obj;
- vftbl_t *vftbl;
+ u1 *patchpos;
+ methodptr entrypoint;
+ methodptr oldentrypoint;
+ bool atentry;
+ void *pv;
+ codeinfo *calleecode;
+ methodinfo *calleem;
+ java_object_t *obj;
+ vftbl_t *vftbl;
assert(ra);
assert(callerframe->down == calleeframe);
/* get the position to patch, in case it was a statically bound call */
- sfi.pv = callerframe->fromcode->entrypoint;
- patchpos = md_get_method_patch_address(ra, &sfi, NULL);
+ pv = callerframe->fromcode->entrypoint;
+ patchpos = md_jit_method_patch_address(pv, ra, NULL);
if (patchpos == NULL) {
/* the call was dispatched dynamically */
/* we can only patch such calls if we are at the entry point */
+#if !defined(__I386__)
+ /* On i386 we always know the instance argument. */
if (!atentry)
return;
+#endif
assert((calleem->flags & ACC_STATIC) == 0);
obj = calleeframe->instance.a;
vftbl = obj->vftbl;
- assert(vftbl->class->vftbl == vftbl);
+ assert(vftbl->clazz->vftbl == vftbl);
- DOLOG_SHORT( printf("\tclass: "); class_println(vftbl->class); );
+ DOLOG_SHORT( printf("\tclass: "); class_println(vftbl->clazz); );
replace_patch_class(vftbl, calleem, oldentrypoint, entrypoint);
}
else {
/* the call was statically bound */
+#if defined(__I386__)
+ /* It happens that there is a patcher trap. (pm) */
+ if (*(u2 *)(patchpos - 1) == 0x0b0f) {
+ } else
+#endif
replace_patch_method_pointer((methodptr *) patchpos, entrypoint, "static ");
}
}
sourceframe_t *callerframe,
sourceframe_t *calleeframe)
{
- s4 reg;
s4 i;
s4 count;
- stackslot_t *basesp;
stackslot_t *sp;
u1 *ra;
codeinfo *calleecode;
else
ra = es->pc + 1 /* XXX this is ugly */;
- /* write the return address */
-
-#if defined(REPLACE_RA_BETWEEN_FRAMES)
- es->sp -= SIZE_OF_STACKSLOT;
-
- *((stackslot_t *)es->sp) = (stackslot_t) ra;
-#endif /* REPLACE_RA_BETWEEN_FRAMES */
+ /* push the stackframe */
-#if defined(REPLACE_REG_RA)
- es->intregs[REPLACE_REG_RA] = (ptrint) ra;
-#endif
+ md_push_stackframe(es, calleecode, ra);
- /* we move into a new code unit */
+ /* we move into a new code unit, set code, PC, PV */
es->code = calleecode;
+ es->pc = calleecode->entrypoint; /* XXX not needed? */
+ es->pv = calleecode->entrypoint;
- /* set the new pc XXX not needed? */
-
- es->pc = calleecode->entrypoint;
-
- /* build the stackframe */
-
- DOLOG( printf("building stackframe of %d words at %p\n",
- calleecode->stackframesize, (void*)es->sp); );
+ /* write slots used for synchronization */
sp = (stackslot_t *) es->sp;
- basesp = sp;
-
- sp -= calleecode->stackframesize;
- es->sp = (u1*) sp;
-
- /* in debug mode, invalidate stack frame first */
-
- /* XXX may not invalidate linkage area used by native code! */
-#if !defined(NDEBUG) && 0
- for (i=0; i<(basesp - sp); ++i) {
- sp[i] = 0xdeaddeadU;
- }
-#endif
-
- /* save the return address register */
-
-#if defined(REPLACE_RA_TOP_OF_FRAME)
-#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
- if (!CODE_IS_LEAFMETHOD(calleecode))
-#endif
- *--basesp = (ptrint) ra;
-#endif /* REPLACE_RA_TOP_OF_FRAME */
-
-#if defined(REPLACE_RA_LINKAGE_AREA)
-#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
- if (!CODE_IS_LEAFMETHOD(calleecode))
-#endif
- basesp[LA_LR_OFFSET / sizeof(stackslot_t)] = (ptrint) ra;
-#endif /* REPLACE_RA_LINKAGE_AREA */
-
- /* save int registers */
-
- reg = INT_REG_CNT;
- for (i=0; i<calleecode->savedintcount; ++i) {
- while (nregdescint[--reg] != REG_SAV)
- ;
- *--basesp = es->intregs[reg];
-
- /* XXX may not clobber saved regs used by native code! */
-#if !defined(NDEBUG) && 0
- es->intregs[reg] = (ptrint) 0x44dead4444dead44ULL;
-#endif
- }
-
- /* save flt registers */
-
- /* XXX align? */
- reg = FLT_REG_CNT;
- for (i=0; i<calleecode->savedfltcount; ++i) {
- while (nregdescfloat[--reg] != REG_SAV)
- ;
- basesp -= STACK_SLOTS_PER_FLOAT;
- *(double*)basesp = es->fltregs[reg];
-
- /* XXX may not clobber saved regs used by native code! */
-#if !defined(NDEBUG) && 0
- *(u8*)&(es->fltregs[reg]) = 0x44dead4444dead44ULL;
-#endif
- }
-
-#if defined(HAS_ADDRESS_REGISTER_FILE)
- /* save adr registers */
-
- reg = ADR_REG_CNT;
- for (i=0; i<calleecode->savedadrcount; ++i) {
- while (nregdescadr[--reg] != REG_SAV)
- ;
- *--basesp = es->adrregs[reg];
-
- /* XXX may not clobber saved regs used by native code! */
-#if !defined(NDEBUG) && 0
- es->adrregs[reg] = (ptrint) 0x44dead4444dead44ULL;
-#endif
- }
-#endif
-
- /* write slots used for synchronization */
-
count = code_get_sync_slot_count(calleecode);
assert(count == calleeframe->syncslotcount);
for (i=0; i<count; ++i) {
sp[calleecode->memuse + i] = calleeframe->syncslots[i].p;
}
- /* set the PV */
-
- es->pv = calleecode->entrypoint;
-
/* redirect future invocations */
if (callerframe && rpcall) {
/* replace_find_replacement_point_for_pc ***************************************
- Find the nearest replacement point at or before the given PC.
+ Find the nearest replacement point at or before the given PC. The
+ given PC has to be between (rp->pc) and (rp->pc+rp->callsize) for
+ the replacement point to be found.
IN:
code.............compilation unit the PC is in
*******************************************************************************/
-rplpoint *replace_find_replacement_point_for_pc(codeinfo *code, u1 *pc)
+rplpoint *replace_find_replacement_point_for_pc(codeinfo *code, u1 *pc, unsigned desired_flags)
{
rplpoint *found;
rplpoint *rp;
s4 i;
- DOLOG( printf("searching for rp in %p ", (void*)code);
- method_println(code->m);
- printf("PC = %p\n", (void*)pc); );
+ DOLOG( printf("searching for rp at pc:%p in %p ", (void*)pc, (void*)code);
+ method_println(code->m); );
found = NULL;
rp = code->rplpoints;
for (i=0; i<code->rplpointcount; ++i, ++rp) {
DOLOG( replace_replacement_point_println(rp, 2); );
- if (rp->pc <= pc)
- found = rp;
+ if (rp->pc <= pc && rp->pc + rp->callsize >= pc) {
+ if (desired_flags) {
+ if (rp->flags & desired_flags) {
+ found = rp;
+ }
+ } else {
+ found = rp;
+ }
+ }
}
- assert(found == NULL || found->pc + found->callsize >= pc);
-
return found;
}
-
/* replace_pop_native_frame ****************************************************
Unroll a native frame in the execution state and create a source frame
static void replace_pop_native_frame(executionstate_t *es,
sourcestate_t *ss,
- stackframeinfo *sfi)
+ stackframeinfo_t *sfi)
{
sourceframe_t *frame;
codeinfo *code;
/* remember pc and size of native frame */
frame->nativepc = es->pc;
- frame->nativeframesize = (es->sp != 0) ? (sfi->sp - es->sp) : 0;
+ frame->nativeframesize = (es->sp != 0) ? (((uintptr_t) sfi->sp) - ((uintptr_t) es->sp)) : 0;
assert(frame->nativeframesize >= 0);
/* remember values of saved registers */
# endif
#endif
- /* restore pv, pc, and sp */
+ /* restore codeinfo of the native stub */
- if (sfi->pv == NULL) {
- /* frame of a native function call */
- es->pv = md_codegen_get_pv_from_pc(sfi->ra);
- }
- else {
- es->pv = sfi->pv;
- }
- es->pc = ((sfi->xpc) ? sfi->xpc : sfi->ra) - 1;
- es->sp = sfi->sp;
-
- /* find the new codeinfo */
-
- DOLOG( printf("PV = %p\n", (void*) es->pv); );
+ code = code_get_codeinfo_for_pv(sfi->pv);
- assert(es->pv != NULL);
+ /* restore sp, pv, pc and codeinfo of the parent method */
- code = *(codeinfo **)(es->pv + CodeinfoPointer);
-
- DOLOG( printf("CODE = %p\n", (void*) code); );
-
- es->code = code;
+ /* XXX michi: use this instead:
+ es->sp = sfi->sp + code->stackframesize; */
+ es->sp = (void*) (((uintptr_t) sfi->sp) + (*(s4 *) (((uintptr_t) sfi->pv) + FrameSize)));
+#if defined(REPLACE_RA_BETWEEN_FRAMES)
+ es->sp += SIZE_OF_STACKSLOT; /* skip return address */
+#endif
+ es->pv = md_codegen_get_pv_from_pc(sfi->ra);
+ es->pc = (void*) (((uintptr_t) ((sfi->xpc) ? sfi->xpc : sfi->ra)) - 1);
+ es->code = code_get_codeinfo_for_pv(es->pv);
}
ss->frames = frame->down;
+ /* skip sp for the native stub */
+
+ es->sp -= (*(s4 *) (((uintptr_t) frame->sfi->pv) + FrameSize));
+#if defined(REPLACE_RA_BETWEEN_FRAMES)
+ es->sp -= SIZE_OF_STACKSLOT; /* skip return address */
+#endif
+
/* assert that the native frame has not moved */
assert(es->sp == frame->sfi->sp);
*******************************************************************************/
sourcestate_t *replace_recover_source_state(rplpoint *rp,
- stackframeinfo *sfi,
+ stackframeinfo_t *sfi,
executionstate_t *es)
{
sourcestate_t *ss;
while (rp || sfi) {
- DOLOG( replace_executionstate_println(es); );
+ DOLOG( executionstate_println(es); );
/* if we are not at a replacement point, it is a native frame */
/* find the replacement point at the call site */
after_machine_frame:
- rp = replace_find_replacement_point_for_pc(es->code, es->pc);
+ rp = replace_find_replacement_point_for_pc(es->code, es->pc, 0);
if (rp == NULL)
vm_abort("could not find replacement point while unrolling call");
#endif
-/* replace_build_execution_state_intern ****************************************
+/* replace_build_execution_state ***********************************************
Build an execution state for the given (mapped) source state.
*******************************************************************************/
-void replace_build_execution_state_intern(sourcestate_t *ss,
- executionstate_t *es)
+static void replace_build_execution_state(sourcestate_t *ss,
+ executionstate_t *es)
{
rplpoint *rp;
sourceframe_t *prevframe;
replace_push_activation_record(es, rp, prevframe, ss->frames);
- DOLOG( replace_executionstate_println(es); );
+ DOLOG( executionstate_println(es); );
}
rp = ss->frames->torp;
replace_write_executionstate(rp, es, ss, ss->frames->down == NULL);
- DOLOG( replace_executionstate_println(es); );
+ DOLOG( executionstate_println(es); );
if (rp->type == RPLPOINT_TYPE_CALL) {
parent = NULL;
}
-/* replace_build_execution_state ***********************************************
-
- This function contains the final phase of replacement. It builds the new
- execution state, releases dump memory, and returns to the calling
- assembler function which finishes replacement.
-
- NOTE: This function is called from asm_replacement_in, with the stack
- pointer at the start of the safe stack area.
-
- THIS FUNCTION MUST BE CALLED USING A SAFE STACK AREA!
-
- CAUTION: This function and its children must not use a lot of stack!
- There are only REPLACE_SAFESTACK_SIZE bytes of C stack
- available.
-
- IN:
- st...............the safestack contained the necessary data
-
-*******************************************************************************/
-
-void replace_build_execution_state(replace_safestack_t *st)
-{
- replace_build_execution_state_intern(st->ss, &(st->es));
-
- DOLOG( replace_executionstate_println(&(st->es)); );
-
- /* release dump area */
-
- dump_release(st->dumpsize);
-
- /* new code is entered after returning */
-
- DOLOG( printf("JUMPING IN!\n"); fflush(stdout); );
-}
-
-
-/* replace_alloc_safestack *****************************************************
-
- Allocate a safe stack area to use during the final phase of replacement.
- The returned area is not initialized. This must be done by the caller.
-
- RETURN VALUE:
- a newly allocated replace_safestack_t *
-
-*******************************************************************************/
-
-static replace_safestack_t *replace_alloc_safestack()
-{
- u1 *mem;
- replace_safestack_t *st;
-
- mem = MNEW(u1, sizeof(replace_safestack_t) + REPLACE_STACK_ALIGNMENT - 1);
-
- st = (replace_safestack_t *) ((ptrint)(mem + REPLACE_STACK_ALIGNMENT - 1)
- & ~(REPLACE_STACK_ALIGNMENT - 1));
-
-#if !defined(NDEBUG)
- memset(st, 0xa5, sizeof(replace_safestack_t));
-#endif
-
- st->mem = mem;
-
- return st;
-}
-
-
-/* replace_free_safestack ******************************************************
-
- Free the given safestack structure, making a copy of the contained
- execution state before freeing it.
-
- NOTE: This function is called from asm_replacement_in.
-
- IN:
- st...............the safestack to free
- tmpes............where to copy the execution state to
-
- OUT:
- *tmpes...........receives a copy of st->es
-
-*******************************************************************************/
-
-void replace_free_safestack(replace_safestack_t *st, executionstate_t *tmpes)
-{
- u1 *mem;
-
- /* copy the executionstate_t to the temporary location */
-
- *tmpes = st->es;
-
- /* get the memory address to free */
-
- mem = st->mem;
-
- /* destroy memory (in debug mode) */
-
-#if !defined(NDEBUG)
- memset(st, 0xa5, sizeof(replace_safestack_t));
-#endif
-
- /* free the safe stack struct */
-
- MFREE(mem, u1, sizeof(replace_safestack_t) + REPLACE_STACK_ALIGNMENT - 1);
-}
-
-
-/* replace_me_wrapper **********************************************************
-
- TODO: Document me!
-
-*******************************************************************************/
-
-bool replace_me_wrapper(u1 *pc)
-{
- codeinfo *code;
- rplpoint *rp;
- executionstate_t es;
-
- /* search the codeinfo for the given PC */
-
- code = code_find_codeinfo_for_pc(pc);
- assert(code);
-
- /* search for a replacement point at the given PC */
-
-#if 0
- rp = replace_find_replacement_point_for_pc(code, pc);
- assert(rp == NULL || rp->pc == pc);
-#else
- {
- int i;
- rplpoint *rp2;
- rp = NULL;
- for (i=0,rp2=code->rplpoints; i<code->rplpointcount; i++,rp2++) {
- if (rp2->pc == pc)
- rp = rp2;
- }
- }
-#endif
-
- /* check if the replacement point is active */
-
- if (rp != NULL && (rp->flags & RPLPOINT_FLAG_ACTIVE)) {
-
- /*md_replace_executionstate_read(&es, context);*/
-
- replace_me(rp, &es);
-
- return true;
- }
- else
- return false;
-}
-
-
/* replace_me ******************************************************************
- This function is called by asm_replacement_out when a thread reaches
+ This function is called by the signal handler when a thread reaches
a replacement point. `replace_me` must map the execution state to the
target replacement point and let execution continue there.
- This function never returns!
-
+ THIS FUNCTION MUST BE CALLED USING A SAFE STACK AREA!
+
IN:
rp...............replacement point that has been reached
- es...............execution state read by asm_replacement_out
+ es...............execution state read by signal handler
*******************************************************************************/
-void replace_me(rplpoint *rp, executionstate_t *es)
+static void replace_me(rplpoint *rp, executionstate_t *es)
{
- stackframeinfo *sfi;
+ stackframeinfo_t *sfi;
sourcestate_t *ss;
sourceframe_t *frame;
- s4 dumpsize;
+ codeinfo *origcode;
rplpoint *origrp;
- replace_safestack_t *safestack;
#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
threadobject *thread;
#endif
+ int32_t dumpmarker;
+
+ origcode = es->code;
+ origrp = rp;
+
+#if defined(ENABLE_TLH)
+ /*printf("Replacing in %s/%s\n", rp->method->clazz->name->text, rp->method->name->text);*/
+#endif
- origrp = rp;
- es->code = code_find_codeinfo_for_pc(rp->pc);
+ /*if (strcmp(rp->method->clazz->name->text, "antlr/AlternativeElement") == 0 && strcmp(rp->method->name->text, "getAutoGenType") ==0) opt_TraceReplacement = 2; else opt_TraceReplacement = 0;*/
DOLOG_SHORT( printf("REPLACING(%d %p): (id %d %p) ",
stat_replacements, (void*)THREADOBJECT,
rp->id, (void*)rp);
method_println(es->code->m); );
- DOLOG( replace_replacement_point_println(rp, 1);
- replace_executionstate_println(es); );
+ DOLOG( replace_replacement_point_println(rp, 1); );
REPLACE_COUNT(stat_replacements);
/* mark start of dump memory area */
- dumpsize = dump_size();
+ DMARKER;
- /* get the stackframeinfo for the current thread */
+ /* Get the stackframeinfo for the current thread. */
- sfi = STACKFRAMEINFO;
+ sfi = threads_get_current_stackframeinfo();
/* recover source state */
/* since we enter the same method again, we turn off rps now */
/* XXX michi: can we really do this? what if the rp was active before
we activated it for the gc? */
- frame = ss->frames;
- while (frame->down)
- frame = frame->down;
- replace_deactivate_replacement_points(frame->tocode);
+ replace_deactivate_replacement_points(origcode);
/* remember executionstate and sourcestate for this thread */
GC_EXECUTIONSTATE = es;
DOLOG_SHORT( replace_sourcestate_println_short(ss); );
- /* avoid infinite loops by self-replacement */
+#if !defined(NDEBUG)
+ /* avoid infinite loops by self-replacement, only if not in testing mode */
- frame = ss->frames;
- while (frame->down)
- frame = frame->down;
+ if (!opt_TestReplacement) {
+ frame = ss->frames;
+ while (frame->down)
+ frame = frame->down;
- if (frame->torp == origrp) {
- DOLOG_SHORT(
- printf("WARNING: identity replacement, turning off rps to avoid infinite loop\n");
- );
- replace_deactivate_replacement_points(frame->tocode);
+ if (frame->torp == origrp) {
+ DOLOG_SHORT(
+ printf("WARNING: identity replacement, turning off rps to avoid infinite loop\n");
+ );
+ replace_deactivate_replacement_points(origcode);
+ }
}
+#endif
#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
}
#endif
- /* write execution state of new code */
+ /* build the new execution state */
- DOLOG( replace_executionstate_println(es); );
+ replace_build_execution_state(ss, es);
- /* allocate a safe stack area and copy all needed data there */
+#if !defined(NDEBUG)
+ /* continue execution after patched machine code, if testing mode enabled */
- safestack = replace_alloc_safestack();
+ if (opt_TestReplacement)
+ es->pc += REPLACEMENT_PATCH_SIZE;
+#endif
+
+ /* release dump area */
+
+ DRELEASE;
+}
- safestack->es = *es;
- safestack->ss = ss;
- safestack->dumpsize = dumpsize;
- /* call the assembler code for the last phase of replacement */
+/* replace_me_wrapper **********************************************************
+
+ This function is called by the signal handler. It determines if there
+ is an active replacement point pending at the given PC and returns
+ accordingly.
+
+ THIS FUNCTION MUST BE CALLED USING A SAFE STACK AREA!
+
+ IN:
+ pc...............the program counter that triggered the replacement.
+ context..........the context (machine state) to which the
+ replacement should be applied.
+
+ OUT:
+ context..........the context after replacement finished.
+
+ RETURN VALUE:
+ true.............replacement done, everything went ok
+ false............no replacement done, context unchanged
-#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__) || defined(__S390__)) && defined(ENABLE_JIT)
- /*asm_replacement_in(&(safestack->es), safestack);*/
+*******************************************************************************/
+
+bool replace_me_wrapper(u1 *pc, void *context)
+{
+ codeinfo *code;
+ rplpoint *rp;
+ executionstate_t es;
+#if defined(ENABLE_RT_TIMING)
+ struct timespec time_start, time_end;
+#endif
+
+ /* search the codeinfo for the given PC */
+
+ code = code_find_codeinfo_for_pc(pc);
+ assert(code);
+
+ /* search for a replacement point at the given PC */
+
+ rp = replace_find_replacement_point_for_pc(code, pc, (RPLPOINT_FLAG_ACTIVE | RPLPOINT_FLAG_COUNTDOWN));
+
+ /* check if the replacement point belongs to given PC and is active */
+
+ if ((rp != NULL) && (rp->pc == pc)
+ && (rp->flags & (RPLPOINT_FLAG_ACTIVE | RPLPOINT_FLAG_COUNTDOWN))) {
+
+ DOLOG( printf("valid replacement point\n"); );
+
+#if !defined(NDEBUG)
+ executionstate_sanity_check(context);
#endif
- abort(); /* NOT REACHED */
+ /* set codeinfo pointer in execution state */
+
+ es.code = code;
+
+ /* read execution state from current context */
+
+ md_executionstate_read(&es, context);
+
+ DOLOG( printf("REPLACEMENT READ: ");
+ executionstate_println(&es); );
+
+ /* do the actual replacement */
+
+#if defined(ENABLE_RT_TIMING)
+ RT_TIMING_GET_TIME(time_start);
+#endif
+
+ replace_me(rp, &es);
+
+#if defined(ENABLE_RT_TIMING)
+ RT_TIMING_GET_TIME(time_end);
+ RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_REPLACE);
+#endif
+
+ /* write execution state to current context */
+
+ md_executionstate_write(&es, context);
+
+ DOLOG( printf("REPLACEMENT WRITE: ");
+ executionstate_println(&es); );
+
+ /* new code is entered after returning */
+
+ DOLOG( printf("JUMPING IN!\n"); fflush(stdout); );
+ return true;
+ }
+ else
+ return false;
}
#if defined(ENABLE_GC_CACAO)
void replace_gc_from_native(threadobject *thread, u1 *pc, u1 *sp)
{
- stackframeinfo *sfi;
+ stackframeinfo_t *sfi;
executionstate_t *es;
sourcestate_t *ss;
- /* get the stackframeinfo of this thread */
+ /* Get the stackframeinfo of this thread. */
+
assert(thread == THREADOBJECT);
- sfi = STACKFRAMEINFO;
+
+ sfi = threads_get_current_stackframeinfo();
/* create the execution state */
es = DNEW(executionstate_t);
}
#endif
+#if defined(ENABLE_GC_CACAO)
+void replace_gc_into_native(threadobject *thread)
+{
+ executionstate_t *es;
+ sourcestate_t *ss;
+
+ /* get the executionstate and sourcestate for the given thread */
+ es = GC_EXECUTIONSTATE;
+ ss = GC_SOURCESTATE;
+
+ /* rebuild the stack of the given thread */
+ replace_build_execution_state(ss, es);
+}
+#endif
+
/******************************************************************************/
/* NOTE: No important code below. */
#endif
-/* replace_executionstate_println **********************************************
-
- Print execution state
-
- IN:
- es...............the execution state to print
-
-*******************************************************************************/
-
-#if !defined(NDEBUG)
-void replace_executionstate_println(executionstate_t *es)
-{
- int i;
- int slots;
- stackslot_t *sp;
- int extraslots;
-
- if (!es) {
- printf("(executionstate_t *)NULL\n");
- return;
- }
-
- printf("executionstate_t:\n");
- printf("\tpc = %p",(void*)es->pc);
- printf(" sp = %p",(void*)es->sp);
- printf(" pv = %p\n",(void*)es->pv);
-#if defined(ENABLE_DISASSEMBLER)
- for (i=0; i<INT_REG_CNT; ++i) {
- if (i%4 == 0)
- printf("\t");
- else
- printf(" ");
-#if SIZEOF_VOID_P == 8
- printf("%-3s = %016llx",abi_registers_integer_name[i],(unsigned long long)es->intregs[i]);
-#else
- printf("%-3s = %08lx",abi_registers_integer_name[i],(unsigned long)es->intregs[i]);
-#endif
- if (i%4 == 3)
- printf("\n");
- }
- for (i=0; i<FLT_REG_CNT; ++i) {
- if (i%4 == 0)
- printf("\t");
- else
- printf(" ");
- printf("F%02d = %016llx",i,(unsigned long long)es->fltregs[i]);
- if (i%4 == 3)
- printf("\n");
- }
-# if defined(HAS_ADDRESS_REGISTER_FILE)
- for (i=0; i<ADR_REG_CNT; ++i) {
- if (i%4 == 0)
- printf("\t");
- else
- printf(" ");
- printf("A%02d = %016llx",i,(unsigned long long)es->adrregs[i]);
- if (i%4 == 3)
- printf("\n");
- }
-# endif
-#endif
-
- sp = (stackslot_t *) es->sp;
-
- extraslots = 2;
-
- if (es->code) {
- methoddesc *md = es->code->m->parseddesc;
- slots = es->code->stackframesize;
- extraslots = 1 + md->memuse;
- }
- else
- slots = 0;
-
-
- if (slots) {
- printf("\tstack slots(+%d) at sp:", extraslots);
- for (i=0; i<slots+extraslots; ++i) {
- if (i%4 == 0)
- printf("\n\t\t");
- printf("M%02d%c", i, (i >= slots) ? '(' : ' ');
-#ifdef HAS_4BYTE_STACKSLOT
- printf("%08lx",(unsigned long)*sp++);
-#else
- printf("%016llx",(unsigned long long)*sp++);
-#endif
- printf("%c", (i >= slots) ? ')' : ' ');
- }
- printf("\n");
- }
-
- printf("\tcode: %p", (void*)es->code);
- if (es->code != NULL) {
- printf(" stackframesize=%d ", es->code->stackframesize);
- method_print(es->code->m);
- }
- printf("\n");
-
- printf("\n");
-}
-#endif
-
#if !defined(NDEBUG)
static void java_value_print(s4 type, replace_val_t value)
{
if (type == TYPE_ADR && value.a != NULL) {
obj = value.a;
putchar(' ');
- utf_display_printable_ascii_classname(obj->vftbl->class->name);
+ utf_display_printable_ascii_classname(obj->vftbl->clazz->name);
- if (obj->vftbl->class == class_java_lang_String) {
+ if (obj->vftbl->clazz == class_java_lang_String) {
printf(" \"");
u = javastring_toutf(obj, false);
utf_display_printable_ascii(u);
if (REPLACE_IS_NATIVE_FRAME(frame)) {
printf("NATIVE (pc %p size %d) ",
- (void*)frame->nativepc, frame->nativeframesize);
+ (void*)frame->nativepc, frame->nativeframesize);
replace_stackframeinfo_println(frame->sfi);
continue;
}
#endif
#if !defined(NDEBUG)
-static void replace_stackframeinfo_println(stackframeinfo *sfi)
+static void replace_stackframeinfo_println(stackframeinfo_t *sfi)
{
printf("prev=%p pv=%p sp=%p ra=%p xpc=%p method=",
(void*)sfi->prev, (void*)sfi->pv, (void*)sfi->sp,
(void*)sfi->ra, (void*)sfi->xpc);
- if (sfi->method)
- method_println(sfi->method);
+ if (sfi->code)
+ method_println(sfi->code->m);
else
printf("(nil)\n");
}
#endif
+
/*
* These are local overrides for various environment variables in Emacs.
* Please do not remove this and leave it at the end of the file, where