Added commit logging (--enable-commitlog). Also fixed disasm bug.
[riscv-isa-sim.git] / riscv / processor.cc
index cce526bc74407d0abafa975be1251ba5fe8dfe26..5c0d784c3aa51ee72877dcc8875352c4f69249a9 100644 (file)
@@ -1,57 +1,51 @@
+// See LICENSE for license details.
+
 #include "processor.h"
+#include "extension.h"
 #include "common.h"
 #include "config.h"
 #include "sim.h"
-#include <bfd.h>
-#include <dis-asm.h>
+#include "disasm.h"
+#include <cinttypes>
 #include <cmath>
 #include <cstdlib>
 #include <iostream>
 #include <assert.h>
+#include <limits.h>
+#include <stdexcept>
 
 processor_t::processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id)
-  : sim(*_sim), mmu(*_mmu), id(_id), utidx(0)
-{
-  reset();
-
-  // create microthreads
-  for (int i=0; i<MAX_UTS; i++)
-    uts[i] = new processor_t(&sim, &mmu, id, i);
-}
-
-processor_t::processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id,
-                         uint32_t _utidx)
-  : sim(*_sim), mmu(*_mmu), id(_id), utidx(_utidx)
+  : sim(_sim), mmu(_mmu), ext(NULL), id(_id), opcode_bits(0)
 {
-  reset();
-  set_sr(sr | SR_EF | SR_EV);
+  reset(true);
+  mmu->set_processor(this);
 
-  // microthreads don't possess their own microthreads
-  for (int i=0; i<MAX_UTS; i++)
-    uts[i] = NULL;
+  #define DECLARE_INSN(name, match, mask) REGISTER_INSN(this, name, match, mask)
+  #include "opcodes.h"
+  #undef DECLARE_INSN
 }
 
 processor_t::~processor_t()
 {
 }
 
-void processor_t::reset()
+void state_t::reset()
 {
-  run = false;
-
   // the ISA guarantees on boot that the PC is 0x2000 and the the processor
   // is in supervisor mode, and in 64-bit mode, if supported, with traps
-  // and virtual memory disabled.  we accomplish this by setting EVEC to
-  // 0x2000 and *enabling* traps, then sending the core an IPI.
-  set_sr(SR_S | SR_SX | SR_ET | SR_IM);
-  evec = 0x2000;
+  // and virtual memory disabled.
+  sr = SR_S;
+#ifdef RISCV_ENABLE_64BIT
+  sr |= SR_S64;
+#endif
+  pc = 0x2000;
 
   // the following state is undefined upon boot-up,
   // but we zero it for determinism
-  memset(XPR,0,sizeof(XPR));
-  memset(FPR,0,sizeof(FPR));
+  XPR.reset();
+  FPR.reset();
 
-  pc = 0;
+  evec = 0;
   epc = 0;
   badvaddr = 0;
   cause = 0;
@@ -60,74 +54,36 @@ void processor_t::reset()
   count = 0;
   compare = 0;
   cycle = 0;
-  set_fsr(0);
-
-  // vector stuff
-  vecbanks = 0xff;
-  vecbanks_count = 8;
-  utidx = -1;
-  vlmax = 32;
-  vl = 0;
-  nxfpr_bank = 256;
-  nxpr_use = 32;
-  nfpr_use = 32;
-}
-
-void processor_t::set_sr(uint32_t val)
-{
-  sr = val & ~SR_ZERO; // clear SR bits that read as zero
-
-#ifndef RISCV_ENABLE_64BIT
-  sr &= ~(SR_SX | SR_UX); // SX=UX=0 for RV32 implementations
-#endif
-#ifndef RISCV_ENABLE_FPU
-  sr &= ~SR_EF;
-#endif
-#ifndef RISCV_ENABLE_RVC
-  sr &= ~SR_EC;
-#endif
-#ifndef RISCV_ENABLE_VEC
-  sr &= ~SR_EV;
-#endif
+  fsr = 0;
 
-  // update MMU state and flush TLB
-  mmu.set_vm_enabled(sr & SR_VM);
-  mmu.set_supervisor(sr & SR_S);
-  mmu.flush_tlb();
-
-  // set the fixed-point register length
-  xprlen = ((sr & SR_S) ? (sr & SR_SX) : (sr & SR_UX)) ? 64 : 32;
+  load_reservation = -1;
 }
 
-void processor_t::set_fsr(uint32_t val)
+void processor_t::reset(bool value)
 {
-  fsr = val & ~FSR_ZERO; // clear FSR bits that read as zero
-}
-
-void processor_t::vcfg()
-{
-  if (nxpr_use + nfpr_use < 2)
-    vlmax = nxfpr_bank * vecbanks_count;
-  else
-    vlmax = (nxfpr_bank / (nxpr_use + nfpr_use - 1)) * vecbanks_count;
+  if (run == !value)
+    return;
+  run = !value;
 
-  vlmax = std::min(vlmax, MAX_UTS);
+  state.reset();
 }
 
-void processor_t::setvl(int vlapp)
+uint32_t processor_t::set_fsr(uint32_t val)
 {
-  vl = std::min(vlmax, vlapp);
+  uint32_t old_fsr = state.fsr;
+  state.fsr = val & ~FSR_ZERO; // clear FSR bits that read as zero
+  return old_fsr;
 }
 
 void processor_t::take_interrupt()
 {
-  uint32_t interrupts = interrupts_pending;
-  interrupts &= (sr & SR_IM) >> SR_IM_SHIFT;
+  uint32_t interrupts = (state.sr & SR_IP) >> SR_IP_SHIFT;
+  interrupts &= (state.sr & SR_IM) >> SR_IM_SHIFT;
 
-  if(interrupts && (sr & SR_ET))
-    for(int i = 0; ; i++, interrupts >>= 1)
-      if(interrupts & 1)
-        throw (trap_t)(trap_irq0 + i);
+  if (interrupts && (state.sr & SR_EI))
+    for (int i = 0; ; i++, interrupts >>= 1)
+      if (interrupts & 1)
+        throw trap_t((1ULL << ((state.sr & SR_S64) ? 63 : 31)) + i);
 }
 
 void processor_t::step(size_t n, bool noisy)
@@ -136,24 +92,37 @@ void processor_t::step(size_t n, bool noisy)
     return;
 
   size_t i = 0;
-  while(1) try
+  reg_t npc = state.pc;
+  mmu_t* _mmu = mmu;
+
+  try
   {
     take_interrupt();
 
-    mmu_t& _mmu = mmu;
-    insn_t insn;
-    insn_func_t func;
-    reg_t npc = pc;
-
     // execute_insn fetches and executes one instruction
     #define execute_insn(noisy) \
       do { \
-        insn = _mmu.load_insn(npc, sr & SR_EC, &func); \
-        if(noisy) disasm(insn,pc); \
-        npc = func(this, insn, npc); \
-        pc = npc; \
+        mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
+        if(noisy) disasm(fetch.insn.insn, npc); \
+        npc = fetch.func(this, fetch.insn.insn, npc); \
       } while(0)
 
+    
+    // special execute_insn  for commit log dumping
+#ifdef RISCV_ENABLE_COMMITLOG
+    //static disassembler disasmblr; 
+    #undef execute_insn 
+    #define execute_insn(noisy) \
+      do { \
+        mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
+        if(noisy) disasm(fetch.insn.insn, npc); \
+        bool in_spvr = state.sr & SR_S; \
+        if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") ", npc, fetch.insn.insn.bits()); \
+        /*if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") %s  ", npc, fetch.insn.insn.bits(), disasmblr.disassemble(fetch.insn.insn).c_str());*/ \
+        npc = fetch.func(this, fetch.insn.insn, npc); \
+      } while(0)
+#endif
+
     if(noisy) for( ; i < n; i++) // print out instructions as we go
       execute_insn(true);
     else 
@@ -170,76 +139,209 @@ void processor_t::step(size_t n, bool noisy)
         execute_insn(false);
     }
 
-    break;
+    state.pc = npc;
   }
-  catch(trap_t t)
+  catch(trap_t& t)
   {
-    // an exception occurred in the target processor
-    i++;
-    take_trap(t,noisy);
+    take_trap(npc, t, noisy);
   }
-  catch(vt_command_t cmd)
+
+  state.cycle += i;
+
+  // update timer and possibly register a timer interrupt
+  uint32_t old_count = state.count;
+  state.count += i;
+  if(old_count < state.compare && uint64_t(old_count) + i >= state.compare)
+    set_interrupt(IRQ_TIMER, true);
+}
+
+void processor_t::take_trap(reg_t pc, trap_t& t, bool noisy)
+{
+  if (noisy)
+    fprintf(stderr, "core %3d: exception %s, epc 0x%016" PRIx64 "\n",
+            id, t.name(), pc);
+
+  // switch to supervisor, set previous supervisor bit, disable interrupts
+  set_pcr(PCR_SR, (((state.sr & ~SR_EI) | SR_S) & ~SR_PS & ~SR_PEI) |
+                  ((state.sr & SR_S) ? SR_PS : 0) |
+                  ((state.sr & SR_EI) ? SR_PEI : 0));
+
+  yield_load_reservation();
+  state.cause = t.cause();
+  state.epc = pc;
+  state.pc = state.evec;
+
+  t.side_effects(&state); // might set badvaddr etc.
+}
+
+void processor_t::deliver_ipi()
+{
+  if (run)
+    set_pcr(PCR_CLR_IPI, 1);
+}
+
+void processor_t::disasm(insn_t insn, reg_t pc)
+{
+  // the disassembler is stateless, so we share it
+  static disassembler disasm;
+  fprintf(stderr, "core %3d: 0x%016" PRIx64 " (0x%08" PRIx32 ") %s\n",
+          id, state.pc, insn.bits(), disasm.disassemble(insn).c_str());
+}
+
+reg_t processor_t::set_pcr(int which, reg_t val)
+{
+  reg_t old_pcr = get_pcr(which);
+
+  switch (which)
   {
-    // this microthread has finished
-    i++;
-    assert(cmd == vt_command_stop);
-    break;
+    case PCR_SR:
+      state.sr = (val & ~SR_IP) | (state.sr & SR_IP);
+#ifndef RISCV_ENABLE_64BIT
+      state.sr &= ~(SR_S64 | SR_U64);
+#endif
+#ifndef RISCV_ENABLE_FPU
+      state.sr &= ~SR_EF;
+#endif
+#ifndef RISCV_ENABLE_VEC
+      state.sr &= ~SR_EV;
+#endif
+      state.sr &= ~SR_ZERO;
+      mmu->flush_tlb();
+      break;
+    case PCR_EPC:
+      state.epc = val;
+      break;
+    case PCR_EVEC: 
+      state.evec = val;
+      break;
+    case PCR_COUNT:
+      state.count = val;
+      break;
+    case PCR_COMPARE:
+      set_interrupt(IRQ_TIMER, false);
+      state.compare = val;
+      break;
+    case PCR_PTBR:
+      state.ptbr = val & ~(PGSIZE-1);
+      break;
+    case PCR_SEND_IPI:
+      sim->send_ipi(val);
+      break;
+    case PCR_CLR_IPI:
+      set_interrupt(IRQ_IPI, val & 1);
+      break;
+    case PCR_SUP0:
+      state.pcr_k0 = val;
+      break;
+    case PCR_SUP1:
+      state.pcr_k1 = val;
+      break;
+    case PCR_TOHOST:
+      if (state.tohost == 0)
+        state.tohost = val;
+      break;
+    case PCR_FROMHOST:
+      set_interrupt(IRQ_HOST, val != 0);
+      state.fromhost = val;
+      break;
   }
-  catch(halt_t t)
+
+  return old_pcr;
+}
+
+reg_t processor_t::get_pcr(int which)
+{
+  switch (which)
   {
-    // sleep until IPI
-    reset();
-    return;
+    case PCR_SR:
+      return state.sr;
+    case PCR_EPC:
+      return state.epc;
+    case PCR_BADVADDR:
+      return state.badvaddr;
+    case PCR_EVEC:
+      return state.evec;
+    case PCR_COUNT:
+      return state.count;
+    case PCR_COMPARE:
+      return state.compare;
+    case PCR_CAUSE:
+      return state.cause;
+    case PCR_PTBR:
+      return state.ptbr;
+    case PCR_ASID:
+      return 0;
+    case PCR_FATC:
+      mmu->flush_tlb();
+      return 0;
+    case PCR_HARTID:
+      return id;
+    case PCR_IMPL:
+      return 1;
+    case PCR_SUP0:
+      return state.pcr_k0;
+    case PCR_SUP1:
+      return state.pcr_k1;
+    case PCR_TOHOST:
+      return state.tohost;
+    case PCR_FROMHOST:
+      return state.fromhost;
   }
+  return -1;
+}
 
-  cycle += i;
+void processor_t::set_interrupt(int which, bool on)
+{
+  uint32_t mask = (1 << (which + SR_IP_SHIFT)) & SR_IP;
+  if (on)
+    state.sr |= mask;
+  else
+    state.sr &= ~mask;
+}
 
-  // update timer and possibly register a timer interrupt
-  uint32_t old_count = count;
-  count += i;
-  if(old_count < compare && uint64_t(old_count) + i >= compare)
-    interrupts_pending |= 1 << TIMER_IRQ;
+reg_t illegal_instruction(processor_t* p, insn_t insn, reg_t pc)
+{
+  throw trap_illegal_instruction();
 }
 
-void processor_t::take_trap(trap_t t, bool noisy)
+insn_func_t processor_t::decode_insn(insn_t insn)
 {
-  if(noisy)
-    printf("core %3d: trap %s, pc 0x%016llx\n",
-           id, trap_name(t), (unsigned long long)pc);
+  bool rv64 = (state.sr & SR_S) ? (state.sr & SR_S64) : (state.sr & SR_U64);
 
-  // switch to supervisor, set previous supervisor bit, disable traps
-  set_sr((((sr & ~SR_ET) | SR_S) & ~SR_PS) | ((sr & SR_S) ? SR_PS : 0));
-  cause = t;
-  epc = pc;
-  pc = evec;
-  badvaddr = mmu.get_badvaddr();
+  auto key = insn.bits() & ((1L << opcode_bits)-1);
+  for (auto it = opcode_map.find(key); it != opcode_map.end() && it->first == key; ++it)
+    if ((insn.bits() & it->second.mask) == it->second.match)
+      return rv64 ? it->second.rv64 : it->second.rv32;
+
+  return &illegal_instruction;
 }
 
-void processor_t::deliver_ipi()
+void processor_t::register_insn(insn_desc_t desc)
 {
-  interrupts_pending |= 1 << IPI_IRQ;
-  run = true;
+  assert(desc.mask & 1);
+  if (opcode_bits == 0 || (desc.mask & ((1L << opcode_bits)-1)) != ((1L << opcode_bits)-1))
+  {
+    unsigned x = 0;
+    while ((desc.mask & ((1L << (x+1))-1)) == ((1L << (x+1))-1) &&
+           (opcode_bits == 0 || x <= opcode_bits))
+      x++;
+    opcode_bits = x;
+
+    decltype(opcode_map) new_map;
+    for (auto it = opcode_map.begin(); it != opcode_map.end(); ++it)
+      new_map.insert(std::make_pair(it->second.match & ((1L<<x)-1), it->second));
+    opcode_map = new_map;
+  }
+
+  opcode_map.insert(std::make_pair(desc.match & ((1L<<opcode_bits)-1), desc));
 }
 
-void processor_t::disasm(insn_t insn, reg_t pc)
+void processor_t::register_extension(extension_t* x)
 {
-  printf("core %3d: 0x%016llx (0x%08x) ",id,(unsigned long long)pc,insn.bits);
-
-  #ifdef RISCV_HAVE_LIBOPCODES
-  disassemble_info info;
-  INIT_DISASSEMBLE_INFO(info, stdout, fprintf);
-  info.flavour = bfd_target_unknown_flavour;
-  info.arch = bfd_arch_mips;
-  info.mach = 101; // XXX bfd_mach_mips_riscv requires modified bfd.h
-  info.endian = BFD_ENDIAN_LITTLE;
-  info.buffer = (bfd_byte*)&insn;
-  info.buffer_length = sizeof(insn);
-  info.buffer_vma = pc;
-
-  int ret = print_insn_little_mips(pc, &info);
-  assert(ret == insn_length(insn.bits));
-  #else
-  printf("unknown");
-  #endif
-  printf("\n");
+  for (auto insn : x->get_instructions())
+    register_insn(insn);
+  if (ext != NULL)
+    throw std::logic_error("only one extension may be registered");
+  ext = x;
+  x->set_processor(this);
 }