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
, reg_t pc
, insn_t insn
)
128 #ifdef RISCV_ENABLE_COMMITLOG
129 if (state
->sr
& SR_EI
) {
130 if (state
->log_reg_write
.addr
) {
131 uint64_t mask
= (insn
.length() == 8 ? uint64_t(0) : (uint64_t(1) << (insn
.length() * 8))) - 1;
132 fprintf(stderr
, "0x%016" PRIx64
" (0x%08" PRIx64
") %c%2" PRIx64
" 0x%016" PRIx64
"\n",
133 pc
, insn
.bits() & mask
,
134 state
->log_reg_write
.addr
& 1 ? 'f' : 'x',
135 state
->log_reg_write
.addr
>> 1, state
->log_reg_write
.data
);
137 fprintf(stderr
, "0x%016" PRIx64
" (0x%08" PRIx64
")\n", pc
, insn
.bits());
140 state
->log_reg_write
.addr
= 0;
144 inline void processor_t::update_histogram(size_t pc
)
146 #ifdef RISCV_ENABLE_HISTOGRAM
147 size_t idx
= pc
>> 2;
152 static reg_t
execute_insn(processor_t
* p
, reg_t pc
, insn_fetch_t fetch
)
154 reg_t npc
= fetch
.func(p
, fetch
.insn
, pc
);
155 commit_log(p
->get_state(), pc
, fetch
.insn
);
156 p
->update_histogram(pc
);
160 static void update_timer(state_t
* state
, size_t instret
)
162 uint64_t count0
= (uint64_t)(uint32_t)state
->count
;
163 state
->count
+= instret
;
164 uint64_t before
= count0
- state
->compare
;
165 if (int64_t(before
^ (before
+ instret
)) < 0)
166 state
->sr
|= (1 << (IRQ_TIMER
+ SR_IP_SHIFT
));
169 static size_t next_timer(state_t
* state
)
171 return state
->compare
- (uint32_t)state
->count
;
174 void processor_t::step(size_t n
)
180 if (unlikely(!run
|| !n
))
182 n
= std::min(n
, next_timer(&state
) | 1U);
190 while (instret
++ < n
)
192 insn_fetch_t fetch
= mmu
->load_insn(pc
);
194 pc
= execute_insn(this, pc
, fetch
);
197 else while (instret
< n
)
199 size_t idx
= _mmu
->icache_index(pc
);
200 auto ic_entry
= _mmu
->access_icache(pc
);
202 #define ICACHE_ACCESS(idx) { \
203 insn_fetch_t fetch = ic_entry->data; \
205 pc = execute_insn(this, pc, fetch); \
207 if (idx == mmu_t::ICACHE_ENTRIES-1) break; \
208 if (unlikely(ic_entry->tag != pc)) break; \
218 pc
= take_trap(t
, pc
);
220 catch(serialize_t
& s
) {}
223 update_timer(&state
, instret
);
226 reg_t
processor_t::take_trap(trap_t
& t
, reg_t epc
)
229 fprintf(stderr
, "core %3d: exception %s, epc 0x%016" PRIx64
"\n",
232 // switch to supervisor, set previous supervisor bit, disable interrupts
233 set_pcr(CSR_STATUS
, (((state
.sr
& ~SR_EI
) | SR_S
) & ~SR_PS
& ~SR_PEI
) |
234 ((state
.sr
& SR_S
) ? SR_PS
: 0) |
235 ((state
.sr
& SR_EI
) ? SR_PEI
: 0));
237 yield_load_reservation();
238 state
.cause
= t
.cause();
240 t
.side_effects(&state
); // might set badvaddr etc.
244 void processor_t::deliver_ipi()
247 set_pcr(CSR_CLEAR_IPI
, 1);
250 void processor_t::disasm(insn_t insn
)
252 uint64_t bits
= insn
.bits() & ((1ULL << (8 * insn_length(insn
.bits()))) - 1);
253 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIx64
") %s\n",
254 id
, state
.pc
, bits
, disassembler
->disassemble(insn
).c_str());
257 void processor_t::set_pcr(int which
, reg_t val
)
262 state
.fflags
= val
& (FSR_AEXC
>> FSR_AEXC_SHIFT
);
265 state
.frm
= val
& (FSR_RD
>> FSR_RD_SHIFT
);
268 state
.fflags
= (val
& FSR_AEXC
) >> FSR_AEXC_SHIFT
;
269 state
.frm
= (val
& FSR_RD
) >> FSR_RD_SHIFT
;
272 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
273 #ifndef RISCV_ENABLE_64BIT
274 state
.sr
&= ~(SR_S64
| SR_U64
);
276 #ifndef RISCV_ENABLE_FPU
281 state
.sr
&= ~SR_ZERO
;
282 rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
289 state
.evec
= val
& ~3;
295 state
.count
= (val
<< 32) | (uint32_t)state
.count
;
299 set_interrupt(IRQ_TIMER
, false);
303 state
.ptbr
= val
& ~(PGSIZE
-1);
309 set_interrupt(IRQ_IPI
, val
& 1);
318 if (state
.tohost
== 0)
327 void processor_t::set_fromhost(reg_t val
)
329 set_interrupt(IRQ_HOST
, val
!= 0);
330 state
.fromhost
= val
;
333 reg_t
processor_t::get_pcr(int which
)
345 return (state
.fflags
<< FSR_AEXC_SHIFT
) | (state
.frm
<< FSR_RD_SHIFT
);
351 return state
.badvaddr
;
367 return state
.count
>> 32;
369 return state
.compare
;
391 sim
->get_htif()->tick(); // not necessary, but faster
394 sim
->get_htif()->tick(); // not necessary, but faster
395 return state
.fromhost
;
414 throw trap_illegal_instruction();
417 void processor_t::set_interrupt(int which
, bool on
)
419 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
426 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
428 throw trap_illegal_instruction();
431 insn_func_t
processor_t::decode_insn(insn_t insn
)
433 size_t mask
= opcode_map
.size()-1;
434 insn_desc_t
* desc
= opcode_map
[insn
.bits() & mask
];
436 while ((insn
.bits() & desc
->mask
) != desc
->match
)
439 return rv64
? desc
->rv64
: desc
->rv32
;
442 void processor_t::register_insn(insn_desc_t desc
)
444 assert(desc
.mask
& 1);
445 instructions
.push_back(desc
);
448 void processor_t::build_opcode_map()
451 for (auto& inst
: instructions
)
452 while ((inst
.mask
& buckets
) != buckets
)
457 decltype(insn_desc_t::match
) mask
;
458 cmp(decltype(mask
) mask
) : mask(mask
) {}
459 bool operator()(const insn_desc_t
& lhs
, const insn_desc_t
& rhs
) {
460 if ((lhs
.match
& mask
) != (rhs
.match
& mask
))
461 return (lhs
.match
& mask
) < (rhs
.match
& mask
);
462 return lhs
.match
< rhs
.match
;
465 std::sort(instructions
.begin(), instructions
.end(), cmp(buckets
-1));
467 opcode_map
.resize(buckets
);
468 opcode_store
.resize(instructions
.size() + 1);
471 for (size_t b
= 0, i
= 0; b
< buckets
; b
++)
473 opcode_map
[b
] = &opcode_store
[j
];
474 while (i
< instructions
.size() && b
== (instructions
[i
].match
& (buckets
-1)))
475 opcode_store
[j
++] = instructions
[i
++];
478 assert(j
== opcode_store
.size()-1);
479 opcode_store
[j
].match
= opcode_store
[j
].mask
= 0;
480 opcode_store
[j
].rv32
= &illegal_instruction
;
481 opcode_store
[j
].rv64
= &illegal_instruction
;
484 void processor_t::register_extension(extension_t
* x
)
486 for (auto insn
: x
->get_instructions())
489 for (auto disasm_insn
: x
->get_disasms())
490 disassembler
->add_insn(disasm_insn
);
492 throw std::logic_error("only one extension may be registered");
494 x
->set_processor(this);