Merge pull request #62 from riscv/trigger
[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 "sim.h"
11 #include "processor.h"
12 #include "memtracer.h"
13 #include <stdlib.h>
14 #include <vector>
15
16 // virtual memory configuration
17 #define PGSHIFT 12
18 const reg_t PGSIZE = 1 << PGSHIFT;
19 const reg_t PGMASK = ~(PGSIZE-1);
20
21 struct insn_fetch_t
22 {
23 insn_func_t func;
24 insn_t insn;
25 };
26
27 struct icache_entry_t {
28 reg_t tag;
29 reg_t pad;
30 insn_fetch_t data;
31 };
32
33 class trigger_matched_t
34 {
35 public:
36 trigger_matched_t(int index,
37 trigger_operation_t operation, reg_t address, reg_t data) :
38 index(index), operation(operation), address(address), data(data) {}
39
40 int index;
41 trigger_operation_t operation;
42 reg_t address;
43 reg_t data;
44 };
45
46 // this class implements a processor's port into the virtual memory system.
47 // an MMU and instruction cache are maintained for simulator performance.
48 class mmu_t
49 {
50 public:
51 mmu_t(sim_t* sim, processor_t* proc);
52 ~mmu_t();
53
54 // template for functions that load an aligned value from memory
55 #define load_func(type) \
56 inline type##_t load_##type(reg_t addr) { \
57 if (addr & (sizeof(type##_t)-1)) \
58 throw trap_load_address_misaligned(addr); \
59 reg_t vpn = addr >> PGSHIFT; \
60 if (likely(tlb_load_tag[vpn % TLB_ENTRIES] == vpn)) \
61 return *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr); \
62 if (unlikely(tlb_load_tag[vpn % TLB_ENTRIES] == (vpn | TLB_CHECK_TRIGGERS))) { \
63 type##_t data = *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr); \
64 if (!matched_trigger) { \
65 matched_trigger = trigger_exception(OPERATION_LOAD, addr, data); \
66 if (matched_trigger) \
67 throw *matched_trigger; \
68 } \
69 return data; \
70 } \
71 type##_t res; \
72 load_slow_path(addr, sizeof(type##_t), (uint8_t*)&res); \
73 return res; \
74 }
75
76 // load value from memory at aligned address; zero extend to register width
77 load_func(uint8)
78 load_func(uint16)
79 load_func(uint32)
80 load_func(uint64)
81
82 // load value from memory at aligned address; sign extend to register width
83 load_func(int8)
84 load_func(int16)
85 load_func(int32)
86 load_func(int64)
87
88 // template for functions that store an aligned value to memory
89 #define store_func(type) \
90 void store_##type(reg_t addr, type##_t val) { \
91 if (addr & (sizeof(type##_t)-1)) \
92 throw trap_store_address_misaligned(addr); \
93 reg_t vpn = addr >> PGSHIFT; \
94 if (likely(tlb_store_tag[vpn % TLB_ENTRIES] == vpn)) \
95 *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr) = val; \
96 else if (unlikely(tlb_store_tag[vpn % TLB_ENTRIES] == (vpn | TLB_CHECK_TRIGGERS))) { \
97 if (!matched_trigger) { \
98 matched_trigger = trigger_exception(OPERATION_STORE, addr, val); \
99 if (matched_trigger) \
100 throw *matched_trigger; \
101 } \
102 *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr) = val; \
103 } \
104 else \
105 store_slow_path(addr, sizeof(type##_t), (const uint8_t*)&val); \
106 }
107
108 // store value to memory at aligned address
109 store_func(uint8)
110 store_func(uint16)
111 store_func(uint32)
112 store_func(uint64)
113
114 static const reg_t ICACHE_ENTRIES = 1024;
115
116 inline size_t icache_index(reg_t addr)
117 {
118 return (addr / PC_ALIGN) % ICACHE_ENTRIES;
119 }
120
121 inline icache_entry_t* refill_icache(reg_t addr, icache_entry_t* entry)
122 {
123 const uint16_t* iaddr = translate_insn_addr(addr);
124 insn_bits_t insn = *iaddr;
125 int length = insn_length(insn);
126
127 if (likely(length == 4)) {
128 insn |= (insn_bits_t)*(const int16_t*)translate_insn_addr(addr + 2) << 16;
129 } else if (length == 2) {
130 insn = (int16_t)insn;
131 } else if (length == 6) {
132 insn |= (insn_bits_t)*(const int16_t*)translate_insn_addr(addr + 4) << 32;
133 insn |= (insn_bits_t)*(const uint16_t*)translate_insn_addr(addr + 2) << 16;
134 } else {
135 static_assert(sizeof(insn_bits_t) == 8, "insn_bits_t must be uint64_t");
136 insn |= (insn_bits_t)*(const int16_t*)translate_insn_addr(addr + 6) << 48;
137 insn |= (insn_bits_t)*(const uint16_t*)translate_insn_addr(addr + 4) << 32;
138 insn |= (insn_bits_t)*(const uint16_t*)translate_insn_addr(addr + 2) << 16;
139 }
140
141 insn_fetch_t fetch = {proc->decode_insn(insn), insn};
142 entry->tag = addr;
143 entry->data = fetch;
144
145 reg_t paddr = sim->mem_to_addr((char*)iaddr);
146 if (tracer.interested_in_range(paddr, paddr + 1, FETCH)) {
147 entry->tag = -1;
148 tracer.trace(paddr, length, FETCH);
149 }
150 return entry;
151 }
152
153 inline icache_entry_t* access_icache(reg_t addr)
154 {
155 icache_entry_t* entry = &icache[icache_index(addr)];
156 if (likely(entry->tag == addr))
157 return entry;
158 return refill_icache(addr, entry);
159 }
160
161 inline insn_fetch_t load_insn(reg_t addr)
162 {
163 icache_entry_t entry;
164 return refill_icache(addr, &entry)->data;
165 }
166
167 void flush_tlb();
168 void flush_icache();
169
170 void register_memtracer(memtracer_t*);
171
172 private:
173 sim_t* sim;
174 processor_t* proc;
175 memtracer_list_t tracer;
176 uint16_t fetch_temp;
177
178 // implement an instruction cache for simulator performance
179 icache_entry_t icache[ICACHE_ENTRIES];
180
181 // implement a TLB for simulator performance
182 static const reg_t TLB_ENTRIES = 256;
183 // If a TLB tag has TLB_CHECK_TRIGGERS set, then the MMU must check for a
184 // trigger match before completing an access.
185 static const reg_t TLB_CHECK_TRIGGERS = 1L<<63;
186 char* tlb_data[TLB_ENTRIES];
187 reg_t tlb_insn_tag[TLB_ENTRIES];
188 reg_t tlb_load_tag[TLB_ENTRIES];
189 reg_t tlb_store_tag[TLB_ENTRIES];
190
191 // finish translation on a TLB miss and update the TLB
192 void refill_tlb(reg_t vaddr, reg_t paddr, access_type type);
193 const char* fill_from_mmio(reg_t vaddr, reg_t paddr);
194
195 // perform a page table walk for a given VA; set referenced/dirty bits
196 reg_t walk(reg_t addr, access_type type, reg_t prv);
197
198 // handle uncommon cases: TLB misses, page faults, MMIO
199 const uint16_t* fetch_slow_path(reg_t addr);
200 void load_slow_path(reg_t addr, reg_t len, uint8_t* bytes);
201 void store_slow_path(reg_t addr, reg_t len, const uint8_t* bytes);
202 reg_t translate(reg_t addr, access_type type);
203
204 // ITLB lookup
205 inline const uint16_t* translate_insn_addr(reg_t addr) {
206 reg_t vpn = addr >> PGSHIFT;
207 if (likely(tlb_insn_tag[vpn % TLB_ENTRIES] == vpn))
208 return (uint16_t*)(tlb_data[vpn % TLB_ENTRIES] + addr);
209 if (unlikely(tlb_insn_tag[vpn % TLB_ENTRIES] == (vpn | TLB_CHECK_TRIGGERS))) {
210 uint16_t* ptr = (uint16_t*)(tlb_data[vpn % TLB_ENTRIES] + addr);
211 int match = proc->trigger_match(OPERATION_EXECUTE, addr, *ptr);
212 if (match >= 0)
213 throw trigger_matched_t(match, OPERATION_EXECUTE, addr, *ptr);
214 return ptr;
215 }
216 return fetch_slow_path(addr);
217 }
218
219 inline trigger_matched_t *trigger_exception(trigger_operation_t operation,
220 reg_t address, reg_t data)
221 {
222 if (!proc) {
223 return NULL;
224 }
225 int match = proc->trigger_match(operation, address, data);
226 if (match == -1)
227 return NULL;
228 if (proc->state.mcontrol[match].timing == 0) {
229 throw trigger_matched_t(match, operation, address, data);
230 }
231 return new trigger_matched_t(match, operation, address, data);
232 }
233
234 bool check_triggers_fetch;
235 bool check_triggers_load;
236 bool check_triggers_store;
237 // The exception describing a matched trigger, or NULL.
238 trigger_matched_t *matched_trigger;
239
240 friend class processor_t;
241 };
242
243 #endif