1 // See LICENSE for license details.
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)
27 mmu
->set_processor(this);
29 #define DECLARE_INSN(name, match, mask) REGISTER_INSN(this, name, match, mask)
35 processor_t::~processor_t()
37 #ifdef RISCV_ENABLE_HISTOGRAM
38 if (histogram_enabled
)
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
);
52 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
53 // is in supervisor mode, and in 64-bit mode, if supported, with traps
54 // and virtual memory disabled.
55 sr
= SR_S
| SR_S64
| SR_U64
;
58 // the following state is undefined upon boot-up,
59 // but we zero it for determinism
77 load_reservation
= -1;
80 void processor_t::set_debug(bool value
)
84 ext
->set_debug(value
);
87 void processor_t::set_histogram(bool value
)
89 histogram_enabled
= value
;
92 void processor_t::reset(bool value
)
98 state
.reset(); // reset the core
99 set_pcr(CSR_STATUS
, state
.sr
);
102 ext
->reset(); // reset the extension
105 struct serialize_t
{};
107 void processor_t::serialize()
112 serialized
= true, throw serialize_t();
115 void processor_t::take_interrupt()
117 int irqs
= ((state
.sr
& SR_IP
) >> SR_IP_SHIFT
) & (state
.sr
>> SR_IM_SHIFT
);
118 if (likely(!irqs
) || likely(!(state
.sr
& SR_EI
)))
121 for (int i
= 0; ; i
++)
123 throw trap_t((1ULL << ((state
.sr
& SR_S64
) ? 63 : 31)) + i
);
126 static void commit_log(state_t
* state
, insn_t insn
)
128 #ifdef RISCV_ENABLE_COMMITLOG
129 if (state
->sr
& SR_EI
) {
130 if (state
->log_reg_write
.addr
) {
131 fprintf(stderr
, "0x%016" PRIx64
" (0x%08" PRIx64
") %c%2u 0x%016" PRIx64
"\n",
132 state
->pc
, insn
.bits(),
133 state
->log_reg_write
.addr
& 1 ? 'f' : 'x',
134 state
->log_reg_write
.addr
>> 1, state
->log_reg_write
.data
);
137 fprintf(stderr
, "0x%016" PRIx64
" (0x%08" PRIx64
")\n",
138 state
->pc
, insn
.bits());
141 state
->log_reg_write
.addr
= 0;
145 inline void processor_t::update_histogram(size_t pc
)
147 #ifdef RISCV_ENABLE_HISTOGRAM
148 size_t idx
= pc
>> 2;
153 static reg_t
execute_insn(processor_t
* p
, reg_t pc
, insn_fetch_t fetch
)
155 reg_t npc
= fetch
.func(p
, fetch
.insn
, pc
);
156 commit_log(p
->get_state(), fetch
.insn
);
157 p
->update_histogram(pc
);
161 static void update_timer(state_t
* state
, size_t instret
)
163 uint64_t count0
= (uint64_t)(uint32_t)state
->count
;
164 state
->count
+= instret
;
165 uint64_t before
= count0
- state
->compare
;
166 if (int64_t(before
^ (before
+ instret
)) < 0)
167 state
->sr
|= (1 << (IRQ_TIMER
+ SR_IP_SHIFT
));
170 static size_t next_timer(state_t
* state
)
172 return state
->compare
- (uint32_t)state
->count
;
175 void processor_t::step(size_t n
)
181 if (unlikely(!run
|| !n
))
183 n
= std::min(n
, next_timer(&state
) | 1U);
191 while (instret
++ < n
)
193 insn_fetch_t fetch
= mmu
->load_insn(pc
);
195 pc
= execute_insn(this, pc
, fetch
);
198 else while (instret
< n
)
200 size_t idx
= _mmu
->icache_index(pc
);
201 auto ic_entry
= _mmu
->access_icache(pc
);
203 #define ICACHE_ACCESS(idx) { \
204 insn_fetch_t fetch = ic_entry->data; \
206 pc = execute_insn(this, pc, fetch); \
208 if (idx == mmu_t::ICACHE_ENTRIES-1) break; \
209 if (unlikely(ic_entry->tag != pc)) break; \
219 pc
= take_trap(t
, pc
);
221 catch(serialize_t
& s
) {}
224 update_timer(&state
, instret
);
227 reg_t
processor_t::take_trap(trap_t
& t
, reg_t epc
)
230 fprintf(stderr
, "core %3d: exception %s, epc 0x%016" PRIx64
"\n",
233 // switch to supervisor, set previous supervisor bit, disable interrupts
234 set_pcr(CSR_STATUS
, (((state
.sr
& ~SR_EI
) | SR_S
) & ~SR_PS
& ~SR_PEI
) |
235 ((state
.sr
& SR_S
) ? SR_PS
: 0) |
236 ((state
.sr
& SR_EI
) ? SR_PEI
: 0));
238 yield_load_reservation();
239 state
.cause
= t
.cause();
241 t
.side_effects(&state
); // might set badvaddr etc.
245 void processor_t::deliver_ipi()
248 set_pcr(CSR_CLEAR_IPI
, 1);
251 void processor_t::disasm(insn_t insn
)
253 uint64_t bits
= insn
.bits() & ((1ULL << (8 * insn_length(insn
.bits()))) - 1);
254 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIx64
") %s\n",
255 id
, state
.pc
, bits
, disassembler
->disassemble(insn
).c_str());
258 void processor_t::set_pcr(int which
, reg_t val
)
263 state
.fflags
= val
& (FSR_AEXC
>> FSR_AEXC_SHIFT
);
266 state
.frm
= val
& (FSR_RD
>> FSR_RD_SHIFT
);
269 state
.fflags
= (val
& FSR_AEXC
) >> FSR_AEXC_SHIFT
;
270 state
.frm
= (val
& FSR_RD
) >> FSR_RD_SHIFT
;
273 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
274 #ifndef RISCV_ENABLE_64BIT
275 state
.sr
&= ~(SR_S64
| SR_U64
);
277 #ifndef RISCV_ENABLE_FPU
282 state
.sr
&= ~SR_ZERO
;
283 rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
290 state
.evec
= val
& ~3;
296 state
.count
= (val
<< 32) | (uint32_t)state
.count
;
300 set_interrupt(IRQ_TIMER
, false);
304 state
.ptbr
= val
& ~(PGSIZE
-1);
310 set_interrupt(IRQ_IPI
, val
& 1);
319 if (state
.tohost
== 0)
328 void processor_t::set_fromhost(reg_t val
)
330 set_interrupt(IRQ_HOST
, val
!= 0);
331 state
.fromhost
= val
;
334 reg_t
processor_t::get_pcr(int which
)
346 return (state
.fflags
<< FSR_AEXC_SHIFT
) | (state
.frm
<< FSR_RD_SHIFT
);
352 return state
.badvaddr
;
368 return state
.count
>> 32;
370 return state
.compare
;
392 sim
->get_htif()->tick(); // not necessary, but faster
395 sim
->get_htif()->tick(); // not necessary, but faster
396 return state
.fromhost
;
415 throw trap_illegal_instruction();
418 void processor_t::set_interrupt(int which
, bool on
)
420 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
427 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
429 throw trap_illegal_instruction();
432 insn_func_t
processor_t::decode_insn(insn_t insn
)
434 size_t mask
= opcode_map
.size()-1;
435 insn_desc_t
* desc
= opcode_map
[insn
.bits() & mask
];
437 while ((insn
.bits() & desc
->mask
) != desc
->match
)
440 return rv64
? desc
->rv64
: desc
->rv32
;
443 void processor_t::register_insn(insn_desc_t desc
)
445 assert(desc
.mask
& 1);
446 instructions
.push_back(desc
);
449 void processor_t::build_opcode_map()
452 for (auto& inst
: instructions
)
453 while ((inst
.mask
& buckets
) != buckets
)
458 decltype(insn_desc_t::match
) mask
;
459 cmp(decltype(mask
) mask
) : mask(mask
) {}
460 bool operator()(const insn_desc_t
& lhs
, const insn_desc_t
& rhs
) {
461 if ((lhs
.match
& mask
) != (rhs
.match
& mask
))
462 return (lhs
.match
& mask
) < (rhs
.match
& mask
);
463 return lhs
.match
< rhs
.match
;
466 std::sort(instructions
.begin(), instructions
.end(), cmp(buckets
-1));
468 opcode_map
.resize(buckets
);
469 opcode_store
.resize(instructions
.size() + 1);
472 for (size_t b
= 0, i
= 0; b
< buckets
; b
++)
474 opcode_map
[b
] = &opcode_store
[j
];
475 while (i
< instructions
.size() && b
== (instructions
[i
].match
& (buckets
-1)))
476 opcode_store
[j
++] = instructions
[i
++];
479 assert(j
== opcode_store
.size()-1);
480 opcode_store
[j
].match
= opcode_store
[j
].mask
= 0;
481 opcode_store
[j
].rv32
= &illegal_instruction
;
482 opcode_store
[j
].rv64
= &illegal_instruction
;
485 void processor_t::register_extension(extension_t
* x
)
487 for (auto insn
: x
->get_instructions())
490 for (auto disasm_insn
: x
->get_disasms())
491 disassembler
->add_insn(disasm_insn
);
493 throw std::logic_error("only one extension may be registered");
495 x
->set_processor(this);