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