Update state.pc on every instruction
[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 <cinttypes>
11 #include <cmath>
12 #include <cstdlib>
13 #include <iostream>
14 #include <assert.h>
15 #include <limits.h>
16 #include <stdexcept>
17 #include <algorithm>
18
19 #undef STATE
20 #define STATE state
21
22 processor_t::processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id)
23 : sim(_sim), mmu(_mmu), ext(NULL), disassembler(new disassembler_t),
24 id(_id), run(false), debug(false), serialized(false)
25 {
26 reset(true);
27 mmu->set_processor(this);
28
29 #define DECLARE_INSN(name, match, mask) REGISTER_INSN(this, name, match, mask)
30 #include "encoding.h"
31 #undef DECLARE_INSN
32 build_opcode_map();
33 }
34
35 processor_t::~processor_t()
36 {
37 #ifdef RISCV_ENABLE_HISTOGRAM
38 if (histogram_enabled)
39 {
40 fprintf(stderr, "PC Histogram size:%lu\n", pc_histogram.size());
41 for(auto iterator = pc_histogram.begin(); iterator != pc_histogram.end(); ++iterator) {
42 fprintf(stderr, "%0lx %lu\n", (iterator->first << 2), iterator->second);
43 }
44 }
45 #endif
46
47 delete disassembler;
48 }
49
50 void state_t::reset()
51 {
52 memset(this, 0, sizeof(*this));
53 mstatus = set_field(mstatus, MSTATUS_PRV, PRV_M);
54 mstatus = set_field(mstatus, MSTATUS_PRV1, PRV_S);
55 mstatus = set_field(mstatus, MSTATUS_PRV2, PRV_S);
56 #ifdef RISCV_ENABLE_64BIT
57 mstatus = set_field(mstatus, MSTATUS64_UA, UA_RV64);
58 mstatus = set_field(mstatus, MSTATUS64_SA, UA_RV64);
59 #endif
60 pc = 0x100;
61 load_reservation = -1;
62 }
63
64 void processor_t::set_debug(bool value)
65 {
66 debug = value;
67 if (ext)
68 ext->set_debug(value);
69 }
70
71 void processor_t::set_histogram(bool value)
72 {
73 histogram_enabled = value;
74 }
75
76 void processor_t::reset(bool value)
77 {
78 if (run == !value)
79 return;
80 run = !value;
81
82 state.reset(); // reset the core
83 set_csr(CSR_MSTATUS, state.mstatus);
84
85 if (ext)
86 ext->reset(); // reset the extension
87 }
88
89 struct serialize_t {};
90
91 void processor_t::serialize()
92 {
93 if (serialized)
94 serialized = false;
95 else
96 serialized = true, throw serialize_t();
97 }
98
99 void processor_t::raise_interrupt(reg_t which)
100 {
101 throw trap_t(((reg_t)1 << 63) | which);
102 }
103
104 void processor_t::take_interrupt()
105 {
106 int priv = get_field(state.mstatus, MSTATUS_PRV);
107 int ie = get_field(state.mstatus, MSTATUS_IE);
108
109 if (priv < PRV_M || (priv == PRV_M && ie)) {
110 if (get_field(state.mstatus, MSTATUS_MSIP))
111 raise_interrupt(IRQ_IPI);
112
113 if (state.fromhost != 0)
114 raise_interrupt(IRQ_HOST);
115 }
116
117 if (priv < PRV_S || (priv == PRV_S && ie)) {
118 if (get_field(state.mstatus, MSTATUS_SSIP))
119 raise_interrupt(IRQ_IPI);
120
121 if (state.stip && get_field(state.mstatus, MSTATUS_STIE))
122 raise_interrupt(IRQ_TIMER);
123 }
124 }
125
126 static void commit_log(state_t* state, reg_t pc, insn_t insn)
127 {
128 #ifdef RISCV_ENABLE_COMMITLOG
129 if (get_field(state->mstatus, MSTATUS_IE)) {
130 uint64_t mask = (insn.length() == 8 ? uint64_t(0) : (uint64_t(1) << (insn.length() * 8))) - 1;
131 if (state->log_reg_write.addr) {
132 fprintf(stderr, "0x%016" PRIx64 " (0x%08" PRIx64 ") %c%2" PRIu64 " 0x%016" PRIx64 "\n",
133 pc,
134 insn.bits() & mask,
135 state->log_reg_write.addr & 1 ? 'f' : 'x',
136 state->log_reg_write.addr >> 1,
137 state->log_reg_write.data);
138 } else {
139 fprintf(stderr, "0x%016" PRIx64 " (0x%08" PRIx64 ")\n", pc, insn.bits() & mask);
140 }
141 }
142 state->log_reg_write.addr = 0;
143 #endif
144 }
145
146 inline void processor_t::update_histogram(size_t pc)
147 {
148 #ifdef RISCV_ENABLE_HISTOGRAM
149 size_t idx = pc >> 2;
150 pc_histogram[idx]++;
151 #endif
152 }
153
154 static reg_t execute_insn(processor_t* p, reg_t pc, insn_fetch_t fetch)
155 {
156 reg_t npc = fetch.func(p, fetch.insn, pc);
157 commit_log(p->get_state(), pc, fetch.insn);
158 p->update_histogram(pc);
159 return npc;
160 }
161
162 static void update_timer(state_t* state, size_t instret)
163 {
164 uint64_t count0 = (uint64_t)(uint32_t)state->scount;
165 state->scount += instret;
166 uint64_t before = count0 - state->stimecmp;
167 if (int64_t(before ^ (before + instret)) < 0)
168 state->stip = true;
169 }
170
171 static size_t next_timer(state_t* state)
172 {
173 return state->stimecmp - (uint32_t)state->scount;
174 }
175
176 void processor_t::step(size_t n)
177 {
178 size_t instret = 0;
179 reg_t pc = state.pc;
180 mmu_t* _mmu = mmu;
181
182 if (unlikely(!run || !n))
183 return;
184 n = std::min(n, next_timer(&state) | 1U);
185
186 try
187 {
188 take_interrupt();
189
190 if (unlikely(debug))
191 {
192 while (instret++ < n)
193 {
194 insn_fetch_t fetch = mmu->load_insn(pc);
195 disasm(fetch.insn);
196 state.pc = pc = execute_insn(this, pc, fetch);
197 }
198 }
199 else while (instret < n)
200 {
201 size_t idx = _mmu->icache_index(pc);
202 auto ic_entry = _mmu->access_icache(pc);
203
204 #define ICACHE_ACCESS(idx) { \
205 insn_fetch_t fetch = ic_entry->data; \
206 ic_entry++; \
207 state.pc = pc = execute_insn(this, pc, fetch); \
208 instret++; \
209 if (idx == mmu_t::ICACHE_ENTRIES-1) break; \
210 if (unlikely(ic_entry->tag != pc)) break; \
211 }
212
213 switch (idx) {
214 #include "icache.h"
215 }
216 }
217 }
218 catch(trap_t& t)
219 {
220 state.pc = take_trap(t, pc);
221 }
222 catch(serialize_t& s) {}
223
224 update_timer(&state, instret);
225 }
226
227 void processor_t::push_privilege_stack()
228 {
229 reg_t s = state.mstatus;
230 s = set_field(s, MSTATUS_PRV2, get_field(state.mstatus, MSTATUS_PRV1));
231 s = set_field(s, MSTATUS_IE2, get_field(state.mstatus, MSTATUS_IE1));
232 s = set_field(s, MSTATUS_PRV1, get_field(state.mstatus, MSTATUS_PRV));
233 s = set_field(s, MSTATUS_IE1, get_field(state.mstatus, MSTATUS_IE));
234 s = set_field(s, MSTATUS_PRV, PRV_M);
235 s = set_field(s, MSTATUS_MPRV, PRV_M);
236 s = set_field(s, MSTATUS_IE, 0);
237 set_csr(CSR_MSTATUS, s);
238 }
239
240 void processor_t::pop_privilege_stack()
241 {
242 reg_t s = state.mstatus;
243 s = set_field(s, MSTATUS_PRV, get_field(state.mstatus, MSTATUS_PRV1));
244 s = set_field(s, MSTATUS_IE, get_field(state.mstatus, MSTATUS_IE1));
245 s = set_field(s, MSTATUS_PRV1, get_field(state.mstatus, MSTATUS_PRV2));
246 s = set_field(s, MSTATUS_IE1, get_field(state.mstatus, MSTATUS_IE2));
247 s = set_field(s, MSTATUS_PRV2, PRV_U);
248 s = set_field(s, MSTATUS_IE2, 1);
249 set_csr(CSR_MSTATUS, s);
250 }
251
252 reg_t processor_t::take_trap(trap_t& t, reg_t epc)
253 {
254 if (debug)
255 fprintf(stderr, "core %3d: exception %s, epc 0x%016" PRIx64 "\n",
256 id, t.name(), epc);
257
258 reg_t tvec = 0x40 * get_field(state.mstatus, MSTATUS_PRV);
259 push_privilege_stack();
260 yield_load_reservation();
261 state.mcause = t.cause();
262 state.mepc = epc;
263 t.side_effects(&state); // might set badvaddr etc.
264 return tvec;
265 }
266
267 void processor_t::deliver_ipi()
268 {
269 state.mstatus |= MSTATUS_MSIP;
270 }
271
272 void processor_t::disasm(insn_t insn)
273 {
274 uint64_t bits = insn.bits() & ((1ULL << (8 * insn_length(insn.bits()))) - 1);
275 fprintf(stderr, "core %3d: 0x%016" PRIx64 " (0x%08" PRIx64 ") %s\n",
276 id, state.pc, bits, disassembler->disassemble(insn).c_str());
277 }
278
279 static bool validate_priv(reg_t priv)
280 {
281 return priv == PRV_U || priv == PRV_S || priv == PRV_M;
282 }
283
284 static bool validate_arch(reg_t arch)
285 {
286 #ifdef RISCV_ENABLE_64BIT
287 if (arch == UA_RV64) return true;
288 #endif
289 return arch == UA_RV32;
290 }
291
292 static bool validate_vm(reg_t vm)
293 {
294 // TODO: VM_SV32 support
295 #ifdef RISCV_ENABLE_64BIT
296 if (vm == VM_SV43) return true;
297 #endif
298 return vm == VM_MBARE;
299 }
300
301 void processor_t::set_csr(int which, reg_t val)
302 {
303 switch (which)
304 {
305 case CSR_FFLAGS:
306 dirty_fp_state;
307 state.fflags = val & (FSR_AEXC >> FSR_AEXC_SHIFT);
308 break;
309 case CSR_FRM:
310 dirty_fp_state;
311 state.frm = val & (FSR_RD >> FSR_RD_SHIFT);
312 break;
313 case CSR_FCSR:
314 dirty_fp_state;
315 state.fflags = (val & FSR_AEXC) >> FSR_AEXC_SHIFT;
316 state.frm = (val & FSR_RD) >> FSR_RD_SHIFT;
317 break;
318 case CSR_SCYCLE:
319 case CSR_STIME:
320 case CSR_SINSTRET:
321 state.scount = val; break;
322 case CSR_SCYCLEH:
323 case CSR_STIMEH:
324 case CSR_SINSTRETH:
325 state.scount = (val << 32) | (uint32_t)state.scount;
326 break;
327 case CSR_MSTATUS:
328 {
329 if ((val ^ state.mstatus) & (MSTATUS_VM | MSTATUS_PRV | MSTATUS_MPRV))
330 mmu->flush_tlb();
331
332 reg_t mask = MSTATUS_SSIP | MSTATUS_MSIP | MSTATUS_IE | MSTATUS_IE1
333 | MSTATUS_IE2 | MSTATUS_IE3 | MSTATUS_STIE;
334 #ifdef RISCV_ENABLE_FPU
335 mask |= MSTATUS_FS;
336 #endif
337 if (ext)
338 mask |= MSTATUS_XS;
339 state.mstatus = (state.mstatus & ~mask) | (val & mask);
340
341 if (validate_vm(get_field(val, MSTATUS_VM)))
342 state.mstatus = (state.mstatus & ~MSTATUS_VM) | (val & MSTATUS_VM);
343 if (validate_priv(get_field(val, MSTATUS_MPRV)))
344 state.mstatus = (state.mstatus & ~MSTATUS_MPRV) | (val & MSTATUS_MPRV);
345 if (validate_priv(get_field(val, MSTATUS_PRV)))
346 state.mstatus = (state.mstatus & ~MSTATUS_PRV) | (val & MSTATUS_PRV);
347 if (validate_priv(get_field(val, MSTATUS_PRV1)))
348 state.mstatus = (state.mstatus & ~MSTATUS_PRV1) | (val & MSTATUS_PRV1);
349 if (validate_priv(get_field(val, MSTATUS_PRV2)))
350 state.mstatus = (state.mstatus & ~MSTATUS_PRV2) | (val & MSTATUS_PRV2);
351 if (validate_priv(get_field(val, MSTATUS_PRV3)))
352 state.mstatus = (state.mstatus & ~MSTATUS_PRV3) | (val & MSTATUS_PRV3);
353 xlen = 32;
354
355 bool dirty = (state.mstatus & MSTATUS_FS) == MSTATUS_FS;
356 dirty |= (state.mstatus & MSTATUS_XS) == MSTATUS_XS;
357 #ifndef RISCV_ENABLE_64BIT
358 state.mstatus = set_field(state.mstatus, MSTATUS32_SD, dirty);
359 #else
360 state.mstatus = set_field(state.mstatus, MSTATUS64_SD, dirty);
361
362 if (validate_arch(get_field(val, MSTATUS64_UA)))
363 state.mstatus = (state.mstatus & ~MSTATUS64_UA) | (val & MSTATUS64_UA);
364 if (validate_arch(get_field(val, MSTATUS64_SA)))
365 state.mstatus = (state.mstatus & ~MSTATUS64_SA) | (val & MSTATUS64_SA);
366 switch (get_field(state.mstatus, MSTATUS_PRV)) {
367 case PRV_U: if (get_field(state.mstatus, MSTATUS64_UA)) xlen = 64; break;
368 case PRV_S: if (get_field(state.mstatus, MSTATUS64_SA)) xlen = 64; break;
369 case PRV_M: xlen = 64; break;
370 default: abort();
371 }
372 #endif
373 break;
374 }
375 case CSR_SSTATUS:
376 {
377 reg_t ms = state.mstatus;
378 ms = set_field(ms, MSTATUS_SSIP, get_field(val, SSTATUS_SIP));
379 ms = set_field(ms, MSTATUS_IE, get_field(val, SSTATUS_IE));
380 ms = set_field(ms, MSTATUS_IE1, get_field(val, SSTATUS_PIE));
381 ms = set_field(ms, MSTATUS_PRV1, get_field(val, SSTATUS_PS));
382 ms = set_field(ms, MSTATUS64_UA, get_field(val, SSTATUS_UA));
383 ms = set_field(ms, MSTATUS_STIE, get_field(val, SSTATUS_TIE));
384 ms = set_field(ms, MSTATUS_FS, get_field(val, SSTATUS_FS));
385 ms = set_field(ms, MSTATUS_XS, get_field(val, SSTATUS_XS));
386 return set_csr(CSR_MSTATUS, ms);
387 }
388 case CSR_SEPC: state.sepc = val; break;
389 case CSR_STVEC: state.stvec = val & ~3; break;
390 case CSR_STIMECMP:
391 serialize();
392 state.stip = false;
393 state.stimecmp = val;
394 break;
395 case CSR_SPTBR: state.sptbr = val & ~(PGSIZE-1); break;
396 case CSR_SSCRATCH: state.sscratch = val; break;
397 case CSR_MEPC: state.mepc = val; break;
398 case CSR_MSCRATCH: state.mscratch = val; break;
399 case CSR_MCAUSE: state.mcause = val; break;
400 case CSR_MBADADDR: state.mbadaddr = val; break;
401 case CSR_SEND_IPI: sim->send_ipi(val); break;
402 case CSR_TOHOST:
403 if (state.tohost == 0)
404 state.tohost = val;
405 break;
406 case CSR_FROMHOST: state.fromhost = val; break;
407 }
408 }
409
410 reg_t processor_t::get_csr(int which)
411 {
412 switch (which)
413 {
414 case CSR_FFLAGS:
415 require_fp;
416 return state.fflags;
417 case CSR_FRM:
418 require_fp;
419 return state.frm;
420 case CSR_FCSR:
421 require_fp;
422 return (state.fflags << FSR_AEXC_SHIFT) | (state.frm << FSR_RD_SHIFT);
423 case CSR_CYCLE:
424 case CSR_TIME:
425 case CSR_INSTRET:
426 case CSR_SCYCLE:
427 case CSR_STIME:
428 case CSR_SINSTRET:
429 serialize();
430 return state.scount;
431 case CSR_CYCLEH:
432 case CSR_TIMEH:
433 case CSR_INSTRETH:
434 case CSR_SCYCLEH:
435 case CSR_STIMEH:
436 case CSR_SINSTRETH:
437 if (xlen == 64)
438 break;
439 serialize();
440 return state.scount >> 32;
441 case CSR_SSTATUS:
442 {
443 reg_t ss = 0;
444 ss = set_field(ss, SSTATUS_SIP, get_field(state.mstatus, MSTATUS_SSIP));
445 ss = set_field(ss, SSTATUS_IE, get_field(state.mstatus, MSTATUS_IE));
446 ss = set_field(ss, SSTATUS_PIE, get_field(state.mstatus, MSTATUS_IE1));
447 ss = set_field(ss, SSTATUS_PS, get_field(state.mstatus, MSTATUS_PRV1));
448 ss = set_field(ss, SSTATUS_UA, get_field(state.mstatus, MSTATUS64_UA));
449 ss = set_field(ss, SSTATUS_TIE, get_field(state.mstatus, MSTATUS_STIE));
450 ss = set_field(ss, SSTATUS_TIP, state.stip);
451 ss = set_field(ss, SSTATUS_FS, get_field(state.mstatus, MSTATUS_FS));
452 ss = set_field(ss, SSTATUS_XS, get_field(state.mstatus, MSTATUS_XS));
453 if (get_field(state.mstatus, MSTATUS64_SD))
454 ss = set_field(ss, (xlen == 32 ? SSTATUS32_SD : SSTATUS64_SD), 1);
455 return ss;
456 }
457 case CSR_SEPC: return state.sepc;
458 case CSR_SBADADDR: return state.sbadaddr;
459 case CSR_STVEC: return state.stvec;
460 case CSR_STIMECMP: return state.stimecmp;
461 case CSR_SCAUSE:
462 if (xlen == 32 && (state.scause >> 63) != 0)
463 return state.scause | ((reg_t)1 << 31);
464 return state.scause;
465 case CSR_SPTBR: return state.sptbr;
466 case CSR_SASID: return 0;
467 case CSR_SSCRATCH: return state.sscratch;
468 case CSR_MSTATUS: return state.mstatus;
469 case CSR_MEPC: return state.mepc;
470 case CSR_MSCRATCH: return state.mscratch;
471 case CSR_MCAUSE: return state.mcause;
472 case CSR_MBADADDR: return state.mbadaddr;
473 case CSR_TOHOST:
474 sim->get_htif()->tick(); // not necessary, but faster
475 return state.tohost;
476 case CSR_FROMHOST:
477 sim->get_htif()->tick(); // not necessary, but faster
478 return state.fromhost;
479 case CSR_SEND_IPI: return 0;
480 case CSR_HARTID: return id;
481 case CSR_UARCH0:
482 case CSR_UARCH1:
483 case CSR_UARCH2:
484 case CSR_UARCH3:
485 case CSR_UARCH4:
486 case CSR_UARCH5:
487 case CSR_UARCH6:
488 case CSR_UARCH7:
489 case CSR_UARCH8:
490 case CSR_UARCH9:
491 case CSR_UARCH10:
492 case CSR_UARCH11:
493 case CSR_UARCH12:
494 case CSR_UARCH13:
495 case CSR_UARCH14:
496 case CSR_UARCH15:
497 return 0;
498 }
499 throw trap_illegal_instruction();
500 }
501
502 reg_t illegal_instruction(processor_t* p, insn_t insn, reg_t pc)
503 {
504 throw trap_illegal_instruction();
505 }
506
507 insn_func_t processor_t::decode_insn(insn_t insn)
508 {
509 size_t mask = opcode_map.size()-1;
510 insn_desc_t* desc = opcode_map[insn.bits() & mask];
511
512 while ((insn.bits() & desc->mask) != desc->match)
513 desc++;
514
515 return xlen == 64 ? desc->rv64 : desc->rv32;
516 }
517
518 void processor_t::register_insn(insn_desc_t desc)
519 {
520 assert(desc.mask & 1);
521 instructions.push_back(desc);
522 }
523
524 void processor_t::build_opcode_map()
525 {
526 size_t buckets = -1;
527 for (auto& inst : instructions)
528 while ((inst.mask & buckets) != buckets)
529 buckets /= 2;
530 buckets++;
531
532 struct cmp {
533 decltype(insn_desc_t::match) mask;
534 cmp(decltype(mask) mask) : mask(mask) {}
535 bool operator()(const insn_desc_t& lhs, const insn_desc_t& rhs) {
536 if ((lhs.match & mask) != (rhs.match & mask))
537 return (lhs.match & mask) < (rhs.match & mask);
538 return lhs.match < rhs.match;
539 }
540 };
541 std::sort(instructions.begin(), instructions.end(), cmp(buckets-1));
542
543 opcode_map.resize(buckets);
544 opcode_store.resize(instructions.size() + 1);
545
546 size_t j = 0;
547 for (size_t b = 0, i = 0; b < buckets; b++)
548 {
549 opcode_map[b] = &opcode_store[j];
550 while (i < instructions.size() && b == (instructions[i].match & (buckets-1)))
551 opcode_store[j++] = instructions[i++];
552 }
553
554 assert(j == opcode_store.size()-1);
555 opcode_store[j].match = opcode_store[j].mask = 0;
556 opcode_store[j].rv32 = &illegal_instruction;
557 opcode_store[j].rv64 = &illegal_instruction;
558 }
559
560 void processor_t::register_extension(extension_t* x)
561 {
562 for (auto insn : x->get_instructions())
563 register_insn(insn);
564 build_opcode_map();
565 for (auto disasm_insn : x->get_disasms())
566 disassembler->add_insn(disasm_insn);
567 if (ext != NULL)
568 throw std::logic_error("only one extension may be registered");
569 ext = x;
570 x->set_processor(this);
571 }