Fix I$ simulator not making forward progress
[riscv-isa-sim.git] / riscv / processor.cc
1 // See LICENSE for license details.
2
3 #include "processor.h"
4 #include "extension.h"
5 #include "common.h"
6 #include "config.h"
7 #include "sim.h"
8 #include "htif.h"
9 #include "disasm.h"
10 #include "icache.h"
11 #include <cinttypes>
12 #include <cmath>
13 #include <cstdlib>
14 #include <iostream>
15 #include <assert.h>
16 #include <limits.h>
17 #include <stdexcept>
18 #include <algorithm>
19
20 processor_t::processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id)
21 : sim(_sim), mmu(_mmu), ext(NULL), disassembler(new disassembler_t),
22 id(_id), run(false), debug(false)
23 {
24 reset(true);
25 mmu->set_processor(this);
26
27 #define DECLARE_INSN(name, match, mask) REGISTER_INSN(this, name, match, mask)
28 #include "encoding.h"
29 #undef DECLARE_INSN
30 build_opcode_map();
31 }
32
33 processor_t::~processor_t()
34 {
35 }
36
37 void state_t::reset()
38 {
39 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
40 // is in supervisor mode, and in 64-bit mode, if supported, with traps
41 // and virtual memory disabled.
42 sr = SR_S | SR_S64;
43 pc = 0x2000;
44
45 // the following state is undefined upon boot-up,
46 // but we zero it for determinism
47 XPR.reset();
48 FPR.reset();
49
50 epc = 0;
51 badvaddr = 0;
52 evec = 0;
53 ptbr = 0;
54 pcr_k0 = 0;
55 pcr_k1 = 0;
56 cause = 0;
57 tohost = 0;
58 fromhost = 0;
59 count = 0;
60 compare = 0;
61 fflags = 0;
62 frm = 0;
63
64 load_reservation = -1;
65 }
66
67 void processor_t::set_debug(bool value)
68 {
69 debug = value;
70 if (ext)
71 ext->set_debug(value);
72 }
73
74 void processor_t::reset(bool value)
75 {
76 if (run == !value)
77 return;
78 run = !value;
79
80 state.reset(); // reset the core
81 set_pcr(CSR_STATUS, state.sr);
82
83 if (ext)
84 ext->reset(); // reset the extension
85 }
86
87 void processor_t::take_interrupt()
88 {
89 uint32_t interrupts = (state.sr & SR_IP) >> SR_IP_SHIFT;
90 interrupts &= (state.sr & SR_IM) >> SR_IM_SHIFT;
91
92 if (interrupts && (state.sr & SR_EI))
93 for (int i = 0; ; i++, interrupts >>= 1)
94 if (interrupts & 1)
95 throw trap_t((1ULL << ((state.sr & SR_S64) ? 63 : 31)) + i);
96 }
97
98 static void commit_log(state_t* state, insn_t insn)
99 {
100 #ifdef RISCV_ENABLE_COMMITLOG
101 if (!(state->sr & SR_S))
102 fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") ", state->pc, insn.bits());
103 #endif
104 }
105
106 void processor_t::step(size_t n)
107 {
108 if(!run)
109 return;
110
111 mmu_t* _mmu = mmu;
112 auto count32 = decltype(state.compare)(state.count);
113 bool count_le_compare = count32 <= state.compare;
114 n = std::min(n, size_t(state.compare - count32) | 1);
115
116 try
117 {
118 take_interrupt();
119
120 if (debug) // print out instructions as we go
121 {
122 for (size_t i = 0; i < n; state.count++, i++)
123 {
124 insn_fetch_t fetch = mmu->load_insn(state.pc);
125 disasm(fetch.insn.insn);
126 commit_log(&state, fetch.insn.insn);
127 state.pc = fetch.func(this, fetch.insn.insn, state.pc);
128 }
129 }
130 else while (n > 0)
131 {
132 size_t idx = (state.pc / sizeof(insn_t)) % ICACHE_SIZE;
133 auto ic_entry = _mmu->access_icache(state.pc), ic_entry_init = ic_entry;
134
135 #define ICACHE_ACCESS(idx) { \
136 insn_t insn = ic_entry->data.insn.insn; \
137 insn_func_t func = ic_entry->data.func; \
138 commit_log(&state, insn); \
139 ic_entry++; \
140 state.pc = func(this, insn, state.pc); \
141 if (idx < ICACHE_SIZE-1 && unlikely(ic_entry->tag != state.pc)) break; \
142 }
143
144 switch (idx)
145 {
146 ICACHE_SWITCH; // auto-generated into icache.h
147 }
148
149 size_t i = ic_entry - ic_entry_init;
150 state.count += i;
151 if (i >= n)
152 break;
153 n -= i;
154 }
155 }
156 catch(trap_t& t)
157 {
158 take_trap(t);
159 }
160
161 bool count_ge_compare =
162 uint64_t(n) + decltype(state.compare)(state.count) >= state.compare;
163 if (count_le_compare && count_ge_compare)
164 set_interrupt(IRQ_TIMER, true);
165 }
166
167 void processor_t::take_trap(trap_t& t)
168 {
169 if (debug)
170 fprintf(stderr, "core %3d: exception %s, epc 0x%016" PRIx64 "\n",
171 id, t.name(), state.pc);
172
173 // switch to supervisor, set previous supervisor bit, disable interrupts
174 set_pcr(CSR_STATUS, (((state.sr & ~SR_EI) | SR_S) & ~SR_PS & ~SR_PEI) |
175 ((state.sr & SR_S) ? SR_PS : 0) |
176 ((state.sr & SR_EI) ? SR_PEI : 0));
177
178 yield_load_reservation();
179 state.cause = t.cause();
180 state.epc = state.pc;
181 state.pc = state.evec;
182
183 t.side_effects(&state); // might set badvaddr etc.
184 }
185
186 void processor_t::deliver_ipi()
187 {
188 if (run)
189 set_pcr(CSR_CLEAR_IPI, 1);
190 }
191
192 void processor_t::disasm(insn_t insn)
193 {
194 // the disassembler is stateless, so we share it
195 fprintf(stderr, "core %3d: 0x%016" PRIx64 " (0x%08" PRIx32 ") %s\n",
196 id, state.pc, insn.bits(), disassembler->disassemble(insn).c_str());
197 }
198
199 reg_t processor_t::set_pcr(int which, reg_t val)
200 {
201 reg_t old_pcr = get_pcr(which);
202
203 switch (which)
204 {
205 case CSR_FFLAGS:
206 state.fflags = val & (FSR_AEXC >> FSR_AEXC_SHIFT);
207 break;
208 case CSR_FRM:
209 state.frm = val & (FSR_RD >> FSR_RD_SHIFT);
210 break;
211 case CSR_FCSR:
212 state.fflags = (val & FSR_AEXC) >> FSR_AEXC_SHIFT;
213 state.frm = (val & FSR_RD) >> FSR_RD_SHIFT;
214 break;
215 case CSR_STATUS:
216 state.sr = (val & ~SR_IP) | (state.sr & SR_IP);
217 #ifndef RISCV_ENABLE_64BIT
218 state.sr &= ~(SR_S64 | SR_U64);
219 #endif
220 #ifndef RISCV_ENABLE_FPU
221 state.sr &= ~SR_EF;
222 #endif
223 if (!ext)
224 state.sr &= ~SR_EA;
225 state.sr &= ~SR_ZERO;
226 rv64 = (state.sr & SR_S) ? (state.sr & SR_S64) : (state.sr & SR_U64);
227 mmu->flush_tlb();
228 break;
229 case CSR_EPC:
230 state.epc = val;
231 break;
232 case CSR_EVEC:
233 state.evec = val & ~3;
234 break;
235 case CSR_CYCLE:
236 case CSR_TIME:
237 case CSR_INSTRET:
238 case CSR_COUNT:
239 state.count = val;
240 break;
241 case CSR_COMPARE:
242 set_interrupt(IRQ_TIMER, false);
243 state.compare = val;
244 break;
245 case CSR_PTBR:
246 state.ptbr = val & ~(PGSIZE-1);
247 break;
248 case CSR_SEND_IPI:
249 sim->send_ipi(val);
250 break;
251 case CSR_CLEAR_IPI:
252 set_interrupt(IRQ_IPI, val & 1);
253 break;
254 case CSR_SUP0:
255 state.pcr_k0 = val;
256 break;
257 case CSR_SUP1:
258 state.pcr_k1 = val;
259 break;
260 case CSR_TOHOST:
261 if (state.tohost == 0)
262 state.tohost = val;
263 break;
264 case CSR_FROMHOST:
265 set_fromhost(val);
266 break;
267 }
268
269 return old_pcr;
270 }
271
272 void processor_t::set_fromhost(reg_t val)
273 {
274 set_interrupt(IRQ_HOST, val != 0);
275 state.fromhost = val;
276 }
277
278 reg_t processor_t::get_pcr(int which)
279 {
280 switch (which)
281 {
282 case CSR_FFLAGS:
283 return state.fflags;
284 case CSR_FRM:
285 return state.frm;
286 case CSR_FCSR:
287 return (state.fflags << FSR_AEXC_SHIFT) | (state.frm << FSR_RD_SHIFT);
288 case CSR_STATUS:
289 return state.sr;
290 case CSR_EPC:
291 return state.epc;
292 case CSR_BADVADDR:
293 return state.badvaddr;
294 case CSR_EVEC:
295 return state.evec;
296 case CSR_CYCLE:
297 case CSR_TIME:
298 case CSR_INSTRET:
299 case CSR_COUNT:
300 return state.count;
301 case CSR_COMPARE:
302 return state.compare;
303 case CSR_CAUSE:
304 return state.cause;
305 case CSR_PTBR:
306 return state.ptbr;
307 case CSR_SEND_IPI:
308 case CSR_CLEAR_IPI:
309 return 0;
310 case CSR_ASID:
311 return 0;
312 case CSR_FATC:
313 mmu->flush_tlb();
314 return 0;
315 case CSR_HARTID:
316 return id;
317 case CSR_IMPL:
318 return 1;
319 case CSR_SUP0:
320 return state.pcr_k0;
321 case CSR_SUP1:
322 return state.pcr_k1;
323 case CSR_TOHOST:
324 sim->get_htif()->tick(); // not necessary, but faster
325 return state.tohost;
326 case CSR_FROMHOST:
327 sim->get_htif()->tick(); // not necessary, but faster
328 return state.fromhost;
329 default:
330 throw trap_illegal_instruction();
331 }
332 }
333
334 void processor_t::set_interrupt(int which, bool on)
335 {
336 uint32_t mask = (1 << (which + SR_IP_SHIFT)) & SR_IP;
337 if (on)
338 state.sr |= mask;
339 else
340 state.sr &= ~mask;
341 }
342
343 reg_t illegal_instruction(processor_t* p, insn_t insn, reg_t pc)
344 {
345 throw trap_illegal_instruction();
346 }
347
348 insn_func_t processor_t::decode_insn(insn_t insn)
349 {
350 size_t mask = opcode_map.size()-1;
351 insn_desc_t* desc = opcode_map[insn.bits() & mask];
352
353 while ((insn.bits() & desc->mask) != desc->match)
354 desc++;
355
356 return rv64 ? desc->rv64 : desc->rv32;
357 }
358
359 void processor_t::register_insn(insn_desc_t desc)
360 {
361 assert(desc.mask & 1);
362 instructions.push_back(desc);
363 }
364
365 void processor_t::build_opcode_map()
366 {
367 size_t buckets = -1;
368 for (auto& inst : instructions)
369 while ((inst.mask & buckets) != buckets)
370 buckets /= 2;
371 buckets++;
372
373 struct cmp {
374 decltype(insn_desc_t::match) mask;
375 cmp(decltype(mask) mask) : mask(mask) {}
376 bool operator()(const insn_desc_t& lhs, const insn_desc_t& rhs) {
377 if ((lhs.match & mask) != (rhs.match & mask))
378 return (lhs.match & mask) < (rhs.match & mask);
379 return lhs.match < rhs.match;
380 }
381 };
382 std::sort(instructions.begin(), instructions.end(), cmp(buckets-1));
383
384 opcode_map.resize(buckets);
385 opcode_store.resize(instructions.size() + 1);
386
387 size_t j = 0;
388 for (size_t b = 0, i = 0; b < buckets; b++)
389 {
390 opcode_map[b] = &opcode_store[j];
391 while (i < instructions.size() && b == (instructions[i].match & (buckets-1)))
392 opcode_store[j++] = instructions[i++];
393 }
394
395 assert(j == opcode_store.size()-1);
396 opcode_store[j].match = opcode_store[j].mask = 0;
397 opcode_store[j].rv32 = &illegal_instruction;
398 opcode_store[j].rv64 = &illegal_instruction;
399 }
400
401 void processor_t::register_extension(extension_t* x)
402 {
403 for (auto insn : x->get_instructions())
404 register_insn(insn);
405 build_opcode_map();
406 for (auto disasm_insn : x->get_disasms())
407 disassembler->add_insn(disasm_insn);
408 if (ext != NULL)
409 throw std::logic_error("only one extension may be registered");
410 ext = x;
411 x->set_processor(this);
412 }