fix clang compile error
[riscv-isa-sim.git] / riscv / mmu.h
1 // See LICENSE for license details.
2
3 #ifndef _RISCV_MMU_H
4 #define _RISCV_MMU_H
5
6 #include "decode.h"
7 #include "trap.h"
8 #include "common.h"
9 #include "config.h"
10 #include "processor.h"
11 #include "memtracer.h"
12 #include <stdlib.h>
13 #include <vector>
14
15 // virtual memory configuration
16 #define PGSHIFT 12
17 const reg_t PGSIZE = 1 << PGSHIFT;
18
19 struct insn_fetch_t
20 {
21 insn_func_t func;
22 insn_t insn;
23 };
24
25 struct icache_entry_t {
26 reg_t tag;
27 reg_t pad;
28 insn_fetch_t data;
29 };
30
31 // this class implements a processor's port into the virtual memory system.
32 // an MMU and instruction cache are maintained for simulator performance.
33 class mmu_t
34 {
35 public:
36 mmu_t(char* _mem, size_t _memsz);
37 ~mmu_t();
38
39 // template for functions that load an aligned value from memory
40 #define load_func(type) \
41 type##_t load_##type(reg_t addr) __attribute__((always_inline)) { \
42 void* paddr = translate(addr, sizeof(type##_t), false, false); \
43 return *(type##_t*)paddr; \
44 }
45
46 // load value from memory at aligned address; zero extend to register width
47 load_func(uint8)
48 load_func(uint16)
49 load_func(uint32)
50 load_func(uint64)
51
52 // load value from memory at aligned address; sign extend to register width
53 load_func(int8)
54 load_func(int16)
55 load_func(int32)
56 load_func(int64)
57
58 // template for functions that store an aligned value to memory
59 #define store_func(type) \
60 void store_##type(reg_t addr, type##_t val) { \
61 void* paddr = translate(addr, sizeof(type##_t), true, false); \
62 *(type##_t*)paddr = val; \
63 }
64
65 // store value to memory at aligned address
66 store_func(uint8)
67 store_func(uint16)
68 store_func(uint32)
69 store_func(uint64)
70
71 static const reg_t ICACHE_ENTRIES = 1024;
72
73 inline size_t icache_index(reg_t addr)
74 {
75 // for instruction sizes != 4, this hash still works but is suboptimal
76 return (addr / 4) % ICACHE_ENTRIES;
77 }
78
79 // load instruction from memory at aligned address.
80 icache_entry_t* access_icache(reg_t addr) __attribute__((always_inline))
81 {
82 reg_t idx = icache_index(addr);
83 icache_entry_t* entry = &icache[idx];
84 if (likely(entry->tag == addr))
85 return entry;
86
87 char* iaddr = (char*)translate(addr, 1, false, true);
88 insn_bits_t insn = *(uint16_t*)iaddr;
89 int length = insn_length(insn);
90
91 if (likely(length == 4)) {
92 if (likely(addr % PGSIZE < PGSIZE-2))
93 insn |= (insn_bits_t)*(int16_t*)(iaddr + 2) << 16;
94 else
95 insn |= (insn_bits_t)*(int16_t*)translate(addr + 2, 1, false, true) << 16;
96 } else if (length == 2) {
97 insn = (int16_t)insn;
98 } else if (length == 6) {
99 insn |= (insn_bits_t)*(int16_t*)translate(addr + 4, 1, false, true) << 32;
100 insn |= (insn_bits_t)*(uint16_t*)translate(addr + 2, 1, false, true) << 16;
101 } else {
102 static_assert(sizeof(insn_bits_t) == 8, "insn_bits_t must be uint64_t");
103 insn |= (insn_bits_t)*(int16_t*)translate(addr + 6, 1, false, true) << 48;
104 insn |= (insn_bits_t)*(uint16_t*)translate(addr + 4, 1, false, true) << 32;
105 insn |= (insn_bits_t)*(uint16_t*)translate(addr + 2, 1, false, true) << 16;
106 }
107
108 insn_fetch_t fetch = {proc->decode_insn(insn), insn};
109 icache[idx].tag = addr;
110 icache[idx].data = fetch;
111
112 reg_t paddr = iaddr - mem;
113 if (!tracer.empty() && tracer.interested_in_range(paddr, paddr + 1, false, true))
114 {
115 icache[idx].tag = -1;
116 tracer.trace(paddr, length, false, true);
117 }
118 return &icache[idx];
119 }
120
121 inline insn_fetch_t load_insn(reg_t addr)
122 {
123 return access_icache(addr)->data;
124 }
125
126 void set_processor(processor_t* p) { proc = p; flush_tlb(); }
127
128 void flush_tlb();
129 void flush_icache();
130
131 void register_memtracer(memtracer_t*);
132
133 private:
134 char* mem;
135 size_t memsz;
136 processor_t* proc;
137 memtracer_list_t tracer;
138
139 // implement an instruction cache for simulator performance
140 icache_entry_t icache[ICACHE_ENTRIES];
141
142 // implement a TLB for simulator performance
143 static const reg_t TLB_ENTRIES = 256;
144 char* tlb_data[TLB_ENTRIES];
145 reg_t tlb_insn_tag[TLB_ENTRIES];
146 reg_t tlb_load_tag[TLB_ENTRIES];
147 reg_t tlb_store_tag[TLB_ENTRIES];
148
149 // finish translation on a TLB miss and upate the TLB
150 void* refill_tlb(reg_t addr, reg_t bytes, bool store, bool fetch);
151
152 // perform a page table walk for a given VA; set referenced/dirty bits
153 reg_t walk(reg_t addr, bool supervisor, bool store, bool fetch);
154
155 // translate a virtual address to a physical address
156 void* translate(reg_t addr, reg_t bytes, bool store, bool fetch)
157 __attribute__((always_inline))
158 {
159 reg_t idx = (addr >> PGSHIFT) % TLB_ENTRIES;
160 reg_t expected_tag = addr >> PGSHIFT;
161 reg_t* tags = fetch ? tlb_insn_tag : store ? tlb_store_tag :tlb_load_tag;
162 reg_t tag = tags[idx];
163 void* data = tlb_data[idx] + addr;
164
165 if (unlikely(addr & (bytes-1)))
166 store ? throw trap_store_address_misaligned(addr) :
167 fetch ? throw trap_instruction_address_misaligned(addr) :
168 throw trap_load_address_misaligned(addr);
169
170 if (likely(tag == expected_tag))
171 return data;
172
173 return refill_tlb(addr, bytes, store, fetch);
174 }
175
176 friend class processor_t;
177 };
178
179 #endif