OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc1/] [or1ksim/] [cpu/] [or32/] [op_support.c] - Diff between revs 1452 and 1481

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 1452 Rev 1481
Line 43... Line 43...
#include "op_support.h"
#include "op_support.h"
 
 
#include "rec_i386.h"
#include "rec_i386.h"
 
 
/* Stuff that is really a `micro' operation but is rather big (or for some other
/* Stuff that is really a `micro' operation but is rather big (or for some other
 * reason (like calling exit()) */
 * reason like calling exit()) */
 
 
void upd_reg_from_t(oraddr_t pc)
void upd_reg_from_t(oraddr_t pc, int bound)
{
{
  int reg;
  int reg;
 
 
  reg = cpu_state.curr_page->ts[(pc & (PAGE_SIZE - 1)) / 2];
  pc = ((pc & (PAGE_SIZE - 1)) / 4);
 
 
 
  if(bound) {
 
    reg = cpu_state.curr_page->ts_bound[pc + 1];
 
  } else
 
    reg = cpu_state.curr_page->ts_during[pc];
 
 
  if(reg & 0x1f)
  if(reg & 0x1f)
    cpu_state.reg[reg & 0x1f] = cpu_state.t0;
    cpu_state.reg[reg & 0x1f] = cpu_state.t0;
 
 
  if((reg >> 5) & 0x1f)
  if((reg >> 5) & 0x1f)
Line 63... Line 68...
    cpu_state.reg[(reg >> 10) & 0x1f] = cpu_state.t2;
    cpu_state.reg[(reg >> 10) & 0x1f] = cpu_state.t2;
}
}
 
 
void op_support_nop_exit(void)
void op_support_nop_exit(void)
{
{
  upd_reg_from_t(get_pc());
  upd_reg_from_t(get_pc(), 0);
  PRINTF("exit(%"PRIdREG")\n", cpu_state.reg[3]);
  PRINTF("exit(%"PRIxREG")\n", cpu_state.reg[3]);
  fprintf(stderr, "@reset : cycles %lld, insn #%lld\n",
  fprintf(stderr, "@reset : cycles %lld, insn #%lld\n",
          runtime.sim.reset_cycles, runtime.cpu.reset_instructions);
          runtime.sim.reset_cycles, runtime.cpu.reset_instructions);
  fprintf(stderr, "@exit  : cycles %lld, insn #%lld\n", runtime.sim.cycles,
  fprintf(stderr, "@exit  : cycles %lld, insn #%lld\n", runtime.sim.cycles,
          runtime.cpu.instructions);
          runtime.cpu.instructions);
  fprintf(stderr, " diff  : cycles %lld, insn #%lld\n",
  fprintf(stderr, " diff  : cycles %lld, insn #%lld\n",
          runtime.sim.cycles - runtime.sim.reset_cycles,
          runtime.sim.cycles - runtime.sim.reset_cycles,
          runtime.cpu.instructions - runtime.cpu.reset_instructions);
          runtime.cpu.instructions - runtime.cpu.reset_instructions);
  /* FIXME: Implement emulation of a stalled cpu
  /* FIXME: Implement emulation of a stalled cpu
  if (config.debug.gdb_enabled)
  if (config.debug.gdb_enabled)
    set_stall_state (1);
    set_stall_state (1);
  else
  else {
    runtime.sim.cont_run = 0;
    handle_sim_command();
 
    sim_done();
 
  }
  */
  */
  exit(0);
  exit(0);
}
}
 
 
void op_support_nop_reset(void)
void op_support_nop_reset(void)
Line 92... Line 99...
  runtime.cpu.reset_instructions = runtime.cpu.instructions;
  runtime.cpu.reset_instructions = runtime.cpu.instructions;
}
}
 
 
void op_support_nop_printf(void)
void op_support_nop_printf(void)
{
{
  upd_reg_from_t(get_pc());
  upd_reg_from_t(get_pc(), 0);
  simprintf(cpu_state.reg[4], cpu_state.reg[3]);
  simprintf(cpu_state.reg[4], cpu_state.reg[3]);
}
}
 
 
void op_support_nop_report(void)
void op_support_nop_report(void)
{
{
  upd_reg_from_t(get_pc());
  upd_reg_from_t(get_pc(), 0);
  PRINTF("report(0x%"PRIxREG");\n", cpu_state.reg[3]);
  PRINTF("report(0x%"PRIxREG");\n", cpu_state.reg[3]);
}
}
 
 
void op_support_nop_report_imm(int imm)
void op_support_nop_report_imm(int imm)
{
{
  upd_reg_from_t(get_pc());
  upd_reg_from_t(get_pc(), 0);
  PRINTF("report %i (0x%"PRIxREG");\n", imm, cpu_state.reg[3]);
  PRINTF("report %i (0x%"PRIxREG");\n", imm, cpu_state.reg[3]);
}
}
 
 
/* Handles a jump */
/* Handles a jump */
/* addr is a VIRTUAL address */
/* addr is a VIRTUAL address */
Line 116... Line 123...
 * simulator (most likely without -fomit-frame-pointer) and thus env will point
 * simulator (most likely without -fomit-frame-pointer) and thus env will point
 * to some bogus value. */
 * to some bogus value. */
void do_jump(oraddr_t addr)
void do_jump(oraddr_t addr)
{
{
  struct dyn_page *target_dp;
  struct dyn_page *target_dp;
  struct x_ref *xref;
 
  oraddr_t phys_page;
  oraddr_t phys_page;
 
 
 
  /* Temporaries are always shipped out */
 
  cpu_state.ts_current = 1;
 
 
  /* The pc is set to the location of the jump in op_set_pc_preemt(_check) and
  /* The pc is set to the location of the jump in op_set_pc_preemt(_check) and
   * then it is incermented by 4 when the scheduler is run.  If a scheduled job
   * then it is incermented by 4 when the scheduler is run.  If a scheduled job
   * so happens to raise an exception cpu_state.delay_insn will still be set and
   * so happens to raise an exception cpu_state.delay_insn will still be set and
   * so except_handle will do its pc adjusting magic (ie. -4 from it) and every-
   * so except_handle will do its pc adjusting magic (ie. -4 from it) and every-
   * thing ends up just working right, except when a scheduled job does not
   * thing ends up just working right, except when a scheduled job does not
Line 154... Line 163...
  /* Since writes to the 0x0-0xff range do not dirtyfy a page recompile the 0x0
  /* Since writes to the 0x0-0xff range do not dirtyfy a page recompile the 0x0
   * page if the jump is to that location */
   * page if the jump is to that location */
  if(phys_page < 0x100)
  if(phys_page < 0x100)
    target_dp->dirty = 1;
    target_dp->dirty = 1;
 
 
  /* Check if this location is cross-referenced */
 
  if(!(xref = find_host_x_ref(target_dp->xrefs, phys_page))) {
 
    target_dp->dirty = 1;
 
    xref = add_to_xrefs(target_dp, phys_page);
 
    if(cpu_state.curr_page)
 
      add_to_held_xrefs(cpu_state.curr_page, xref);
 
  } else {
 
    /* Only increment reference count if this page didn't already */
 
    if(cpu_state.curr_page && !find_held_x_ref(cpu_state.curr_page->held_xrefs,
 
                                               phys_page)) {
 
      xref->ref++;
 
      add_to_held_xrefs(cpu_state.curr_page, xref);
 
    }
 
  }
 
 
 
  if(target_dp->dirty)
  if(target_dp->dirty)
    recompile_page(target_dp);
    recompile_page(target_dp);
 
 
  cpu_state.curr_page = target_dp;
  cpu_state.curr_page = target_dp;
 
 
Line 184... Line 178...
  else
  else
    upd_cycles_dec(target_dp->delayr);
    upd_cycles_dec(target_dp->delayr);
 
 
  cpu_state.ts_current = 0;
  cpu_state.ts_current = 0;
 
 
  /* Initially this (and do_rfe/handle_except) returned the address that we
  /* Initially this returned the address that we should jump to and then the
   * should jump to and then the recompiled code performed the jump.  This was
   * recompiled code performed the jump.  This was no problem if the jump was
   * no problem if the jump was trully an interpage jump or if the location
   * trully an interpage jump or if the location didn't need recompileation.  If
   * didn't need recompileation.  If the jump is page local and the page needs
   * the jump is page local and the page needs recompileation there is a very
   * recompileation there is a very high probability that the page will move in
   * high probability that the page will move in memory and then the return
   * memory and then the return address that is on the stack will point to
   * address that is on the stack will point to memory that has already been
   * memory that has already been freed, sometimes leading to crashes */
   * freed, sometimes leading to crashes */
  /* This looks like it could really be simpler, but no it can't.  The only
  /* This looks like it could really be simpler, but no it can't.  The only
   * issue here is the stack: it has to be unwound.  This function is called
   * issue here is the stack: it has to be unwound.  This function is called
   * from except_handle, which generally ends up quite high on the stack... */
   * from except_handle, which generally ends up quite high on the stack... */
  or_longjmp(xref->dyn_addr);
  enter_dyn_code(phys_page, target_dp);
}
}
 
 
/* l.rfe is a hard instruction to emulate.  One could just call
/* Wrapper around analysis() that contains all the recompiler specific stuff */
 * do_jump(cpu_state.sprs[SPR_EPCR_BASE]), but then the location that we jump to
void op_support_analysis(void)
 * will get cross referenced and because the page that contains the exception
 
 * handlers is very rearly marked as dirty it will accumulate alot of held
 
 * cross references over time. */
 
void do_rfe(void)
 
{
{
  struct dyn_page *target_dp;
  upd_sim_cycles();
  struct x_ref *xref;
  if(ADDR_PAGE(cpu_state.pc) != cpu_state.pc)
  oraddr_t phys_page;
    upd_reg_from_t(cpu_state.pc - (cpu_state.delay_insn ? 4 : 0), 0);
  int already_held = 0;
 
 
 
  set_pc(cpu_state.sprs[SPR_EPCR_BASE]);
 
 
 
  phys_page = immu_translate(cpu_state.sprs[SPR_EPCR_BASE]);
 
 
 
  /* Same reason as in do_jump() */
 
  runtime.sim.mem_cycles = 0;
 
 
 
  /* op_do_sched has run by the time this is run, which makes the pc point to
 
   * the instruction after l.rfe. */
 
  printf("Returning from exception to %"PRIxADDR" from %"PRIxADDR"\n",
 
         phys_page, cpu_state.sprs[SPR_PPC]);
 
 
 
  target_dp = find_dynd_page(phys_page);
 
 
 
  if(!target_dp)
 
    target_dp = new_dp(phys_page);
 
 
 
  /* Since writes to the 0x0-0xff range do not dirtyfy a page recompile the 0x0
 
   * page if the jump is to that location */
 
  if(phys_page < 0x100)
 
    target_dp->dirty = 1;
 
 
 
  /* Check if this location is cross-referenced */
 
  if(!(xref = find_host_x_ref(target_dp->xrefs, phys_page))) {
 
    xref = add_to_xrefs(target_dp, phys_page);
 
    /* Calling dirtyfy_page is real tempting but if we get to the situation were
 
     * the l.rfe instruction and the location to which it returns to are on the
 
     * same page then all the exception cross references will get removed and
 
     * this will result in excessive recompileations of this page */
 
    target_dp->dirty = 1;
 
 
 
    /* There is alot of code (especially in linux) that do loops like this:
 
     * int a;
 
     * // Stuff such that b gets on another page than a
 
     * int b;
 
     * for(i = 0; i < (some big value); i++) {
 
     *   a = b;
 
     *   // Some more stuff
 
     * }
 
     * Here a DTLB miss will happen on every acess to a and b and l.rfe will
 
     * always return to the same locations but since the previous l.rfe to this
 
     * page was to a different location the page will get recompiled each time a
 
     * or b is acessed.  This is why the last NUM_RFE_HELD returns are `cached'.
 
     */
 
    if(++cpu_state.rfe_held_xref_pos == NUM_RFE_HELD)
 
      cpu_state.rfe_held_xref_pos = 0;
 
 
 
    if(cpu_state.rfe_held_xrefs[cpu_state.rfe_held_xref_pos])
 
      cpu_state.rfe_held_xrefs[cpu_state.rfe_held_xref_pos]->ref--;
 
 
 
    cpu_state.rfe_held_xrefs[cpu_state.rfe_held_xref_pos] = xref;
 
  } else {
 
    /* Make sure we increase this cross reference's reference count, since it is
 
     * decremented below. */
 
    xref->ref++;
 
    already_held = 1;
 
  }
 
 
 
  if(target_dp->dirty)
 
    recompile_page(target_dp);
 
 
 
  if(already_held)
 
    xref->ref--;
 
 
 
  cpu_state.curr_page = target_dp;
 
 
 
  /* FIXME: If the page is backed by more than one type of memory, this will
 
   * produce wrong results */
 
  if(cpu_state.sprs[SPR_SR] & SPR_SR_IME)
 
    /* Add the mmu hit delay to the cycle counter */
 
    upd_cycles_dec(target_dp->delayr - config.immu.hitdelay);
 
  else
  else
    upd_cycles_dec(target_dp->delayr);
    upd_reg_from_t(cpu_state.pc, 0);
 
  runtime.cpu.instructions++;
  cpu_state.ts_current = 0;
  analysis(&cpu_state.iqueue);
 
 
  /* See the comment at the end of do_jump */
 
  or_longjmp(xref->dyn_addr);
 
}
 
 
 
/* Handles an exception. */
 
void handle_except(oraddr_t except)
 
{
 
  struct dyn_page *target_dp;
 
  struct x_ref *xref;
 
 
 
  /* NOTE: It is known when this code will be run.  It is therefore not
 
   * necessary to have to plough through cpu_state.curr_page->ts to store the
 
   * temporaries.  On the other hand, except_handle is also called from the
 
   * scheduler, therefore we don't know when it is called and we can't move the
 
   * temporaries to their permanent storeage in the recompiled code. */
 
 
 
  /* op_do_sched has run by the time we run this, which makes the pc point to
 
   * the next instruction. */
 
  printf("Exception %"PRIxADDR" (%s) from %"PRIxADDR"\n", except,
 
         except_name(except), get_pc() - 4);
 
 
 
  set_pc(except);
 
 
 
  target_dp = find_dynd_page(except);
 
 
 
  if(!target_dp)
 
    target_dp = new_dp(except);
 
 
 
  /* Check if this location is cross-referenced */
 
  if(!(xref = find_host_x_ref(target_dp->xrefs, except))) {
 
    /* See the comment in do_rfe for why dirtyfy page is not called */
 
    target_dp->dirty = 1;
 
    xref = add_to_xrefs(target_dp, except);
 
  } else {
 
    /* If this cross reference is scheduled for removal increment its reference
 
     * count */
 
    if(!xref->ref)
 
      xref->ref++;
 
  }
 
 
 
  if(target_dp->dirty)
 
    recompile_page(target_dp);
 
 
 
  cpu_state.curr_page = target_dp;
 
 
 
  /* FIXME: If the page is backed by more than one type of memory, this will
 
   * produce wrong results */
 
  /* Address translation is disabled above (no need to add hitdelay) */
 
  upd_cycles_dec(target_dp->delayr);
 
 
 
  cpu_state.ts_current = 0;
 
 
 
  /* See the comment at the end of do_jump */
 
  or_longjmp(xref->dyn_addr);
 
}
}
 
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.