Move much closer to new platform-M memory map
[riscv-isa-sim.git] / riscv / mmu.h
index 551fa462d43a8258b1abe448e346dac5cb99d3e0..b6aa2cab91e87179fa90f0d6652769c9fd34f135 100644 (file)
@@ -7,35 +7,47 @@
 #include "trap.h"
 #include "common.h"
 #include "config.h"
+#include "sim.h"
 #include "processor.h"
 #include "memtracer.h"
+#include <stdlib.h>
 #include <vector>
 
 // virtual memory configuration
-typedef reg_t pte_t;
-const reg_t LEVELS = sizeof(pte_t) == 8 ? 3 : 2;
-const reg_t PTIDXBITS = 10;
-const reg_t PGSHIFT = PTIDXBITS + (sizeof(pte_t) == 8 ? 3 : 2);
+#define PGSHIFT 12
 const reg_t PGSIZE = 1 << PGSHIFT;
-const reg_t VPN_BITS = PTIDXBITS * LEVELS;
-const reg_t PPN_BITS = 8*sizeof(reg_t) - PGSHIFT;
-const reg_t VA_BITS = VPN_BITS + PGSHIFT;
+
+struct insn_fetch_t
+{
+  insn_func_t func;
+  insn_t insn;
+};
+
+struct icache_entry_t {
+  reg_t tag;
+  reg_t pad;
+  insn_fetch_t data;
+};
 
 // this class implements a processor's port into the virtual memory system.
 // an MMU and instruction cache are maintained for simulator performance.
 class mmu_t
 {
 public:
-  mmu_t(char* _mem, size_t _memsz);
+  mmu_t(sim_t* sim, processor_t* proc);
   ~mmu_t();
 
   // template for functions that load an aligned value from memory
   #define load_func(type) \
     type##_t load_##type(reg_t addr) __attribute__((always_inline)) { \
-      if(unlikely(addr % sizeof(type##_t))) \
+      if (addr & (sizeof(type##_t)-1)) \
         throw trap_load_address_misaligned(addr); \
-      void* paddr = translate(addr, sizeof(type##_t), false, false); \
-      return *(type##_t*)paddr; \
+      reg_t vpn = addr >> PGSHIFT; \
+      if (likely(tlb_load_tag[vpn % TLB_ENTRIES] == vpn)) \
+        return *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr); \
+      type##_t res; \
+      load_slow_path(addr, sizeof(type##_t), (uint8_t*)&res); \
+      return res; \
     }
 
   // load value from memory at aligned address; zero extend to register width
@@ -53,10 +65,13 @@ public:
   // template for functions that store an aligned value to memory
   #define store_func(type) \
     void store_##type(reg_t addr, type##_t val) { \
-      if(unlikely(addr % sizeof(type##_t))) \
+      if (addr & (sizeof(type##_t)-1)) \
         throw trap_store_address_misaligned(addr); \
-      void* paddr = translate(addr, sizeof(type##_t), true, false); \
-      *(type##_t*)paddr = val; \
+      reg_t vpn = addr >> PGSHIFT; \
+      if (likely(tlb_store_tag[vpn % TLB_ENTRIES] == vpn)) \
+        *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr) = val; \
+      else \
+        store_slow_path(addr, sizeof(type##_t), (const uint8_t*)&val); \
     }
 
   // store value to memory at aligned address
@@ -65,43 +80,57 @@ public:
   store_func(uint32)
   store_func(uint64)
 
-  struct insn_fetch_t
-  {
-    insn_func_t func;
-    union {
-      insn_t insn;
-      uint_fast32_t pad;
-    } insn;
-  };
-
-  // load instruction from memory at aligned address.
-  inline insn_fetch_t load_insn(reg_t addr)
+  static const reg_t ICACHE_ENTRIES = 1024;
+
+  inline size_t icache_index(reg_t addr)
   {
-    reg_t offset = addr & (sizeof(insn_t) * (ICACHE_ENTRIES-1));
-    offset *= sizeof(icache_entry_t) / sizeof(insn_t);
-    icache_entry_t* entry = (icache_entry_t*)((char*)icache + offset);
-    insn_fetch_t data = entry->data;
-    if (likely(entry->tag == addr))
-      return data;
+    return (addr / PC_ALIGN) % ICACHE_ENTRIES;
+  }
 
-    void* iaddr = translate(addr, sizeof(insn_t), false, true);
-    insn_fetch_t fetch;
-    fetch.insn.pad = *(decltype(fetch.insn.insn.bits())*)iaddr;
-    fetch.func = proc->decode_insn(fetch.insn.insn);
+  inline icache_entry_t* refill_icache(reg_t addr, icache_entry_t* entry)
+  {
+    const uint16_t* iaddr = translate_insn_addr(addr);
+    insn_bits_t insn = *iaddr;
+    int length = insn_length(insn);
+
+    if (likely(length == 4)) {
+      insn |= (insn_bits_t)*(const int16_t*)translate_insn_addr(addr + 2) << 16;
+    } else if (length == 2) {
+      insn = (int16_t)insn;
+    } else if (length == 6) {
+      insn |= (insn_bits_t)*(const int16_t*)translate_insn_addr(addr + 4) << 32;
+      insn |= (insn_bits_t)*(const uint16_t*)translate_insn_addr(addr + 2) << 16;
+    } else {
+      static_assert(sizeof(insn_bits_t) == 8, "insn_bits_t must be uint64_t");
+      insn |= (insn_bits_t)*(const int16_t*)translate_insn_addr(addr + 6) << 48;
+      insn |= (insn_bits_t)*(const uint16_t*)translate_insn_addr(addr + 4) << 32;
+      insn |= (insn_bits_t)*(const uint16_t*)translate_insn_addr(addr + 2) << 16;
+    }
 
+    insn_fetch_t fetch = {proc->decode_insn(insn), insn};
     entry->tag = addr;
     entry->data = fetch;
 
-    reg_t paddr = (char*)iaddr - mem;
-    if (!tracer.empty() && tracer.interested_in_range(paddr, paddr + sizeof(insn_t), false, true))
-    {
+    reg_t paddr = sim->mem_to_addr((char*)iaddr);
+    if (tracer.interested_in_range(paddr, paddr + 1, FETCH)) {
       entry->tag = -1;
-      tracer.trace(paddr, sizeof(insn_t), false, true);
+      tracer.trace(paddr, length, FETCH);
     }
-    return entry->data;
+    return entry;
   }
 
-  void set_processor(processor_t* p) { proc = p; flush_tlb(); }
+  inline icache_entry_t* access_icache(reg_t addr)
+  {
+    icache_entry_t* entry = &icache[icache_index(addr)];
+    if (likely(entry->tag == addr))
+      return entry;
+    return refill_icache(addr, entry);
+  }
+
+  inline insn_fetch_t load_insn(reg_t addr)
+  {
+    return access_icache(addr)->data;
+  }
 
   void flush_tlb();
   void flush_icache();
@@ -109,18 +138,12 @@ public:
   void register_memtracer(memtracer_t*);
 
 private:
-  char* mem;
-  size_t memsz;
+  sim_t* sim;
   processor_t* proc;
   memtracer_list_t tracer;
+  uint16_t fetch_temp;
 
   // implement an instruction cache for simulator performance
-  static const reg_t ICACHE_ENTRIES = 2048;
-  struct icache_entry_t {
-    reg_t tag;
-    reg_t pad;
-    insn_fetch_t data;
-  };
   icache_entry_t icache[ICACHE_ENTRIES];
 
   // implement a TLB for simulator performance
@@ -131,24 +154,23 @@ private:
   reg_t tlb_store_tag[TLB_ENTRIES];
 
   // finish translation on a TLB miss and upate the TLB
-  void* refill_tlb(reg_t addr, reg_t bytes, bool store, bool fetch);
-
-  // perform a page table walk for a given virtual address
-  pte_t walk(reg_t addr);
-
-  // translate a virtual address to a physical address
-  void* translate(reg_t addr, reg_t bytes, bool store, bool fetch)
-    __attribute__((always_inline))
-  {
-    reg_t idx = (addr >> PGSHIFT) % TLB_ENTRIES;
-    reg_t expected_tag = addr & ~(PGSIZE-1);
-
-    reg_t* tlb_tag = fetch ? tlb_insn_tag : store ? tlb_store_tag :tlb_load_tag;
-    void* data = tlb_data[idx] + addr;
-    if (likely(tlb_tag[idx] == expected_tag))
-      return data;
-
-    return refill_tlb(addr, bytes, store, fetch);
+  void refill_tlb(reg_t vaddr, reg_t paddr, access_type type);
+
+  // perform a page table walk for a given VA; set referenced/dirty bits
+  reg_t walk(reg_t addr, access_type type, bool supervisor, bool pum);
+
+  // handle uncommon cases: TLB misses, page faults, MMIO
+  const uint16_t* fetch_slow_path(reg_t addr);
+  void load_slow_path(reg_t addr, reg_t len, uint8_t* bytes);
+  void store_slow_path(reg_t addr, reg_t len, const uint8_t* bytes);
+  reg_t translate(reg_t addr, access_type type);
+
+  // ITLB lookup
+  const uint16_t* translate_insn_addr(reg_t addr) __attribute__((always_inline)) {
+    reg_t vpn = addr >> PGSHIFT;
+    if (likely(tlb_insn_tag[vpn % TLB_ENTRIES] == vpn))
+      return (uint16_t*)(tlb_data[vpn % TLB_ENTRIES] + addr);
+    return fetch_slow_path(addr);
   }
   
   friend class processor_t;