1 // See LICENSE for license details.
17 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
)
18 : sim(_sim
), mmu(_mmu
), ext(NULL
), id(_id
), debug(false), opcode_bits(0)
21 mmu
->set_processor(this);
23 #define DECLARE_INSN(name, match, mask) REGISTER_INSN(this, name, match, mask)
28 processor_t::~processor_t()
34 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
35 // is in supervisor mode, and in 64-bit mode, if supported, with traps
36 // and virtual memory disabled.
38 #ifdef RISCV_ENABLE_64BIT
43 // the following state is undefined upon boot-up,
44 // but we zero it for determinism
59 load_reservation
= -1;
62 void processor_t::set_debug(bool value
)
66 ext
->set_debug(value
);
69 void processor_t::reset(bool value
)
75 state
.reset(); // reset the core
77 ext
->reset(); // reset the extension
80 uint32_t processor_t::set_fsr(uint32_t val
)
82 uint32_t old_fsr
= state
.fsr
;
83 state
.fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
87 void processor_t::take_interrupt()
89 uint32_t interrupts
= (state
.sr
& SR_IP
) >> SR_IP_SHIFT
;
90 interrupts
&= (state
.sr
& SR_IM
) >> SR_IM_SHIFT
;
92 if (interrupts
&& (state
.sr
& SR_EI
))
93 for (int i
= 0; ; i
++, interrupts
>>= 1)
95 throw trap_t((1ULL << ((state
.sr
& SR_S64
) ? 63 : 31)) + i
);
98 void processor_t::step(size_t n
)
104 reg_t npc
= state
.pc
;
111 // execute_insn fetches and executes one instruction
112 #define execute_insn(noisy) \
114 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
115 if(noisy) disasm(fetch.insn.insn); \
116 npc = fetch.func(this, fetch.insn.insn, npc); \
120 // special execute_insn for commit log dumping
121 #ifdef RISCV_ENABLE_COMMITLOG
122 //static disassembler disasmblr;
124 #define execute_insn(noisy) \
126 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
127 if(noisy) disasm(fetch.insn.insn); \
128 bool in_spvr = state.sr & SR_S; \
129 if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") ", npc, fetch.insn.insn.bits()); \
130 /*if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") %s ", npc, fetch.insn.insn.bits(), disasmblr.disassemble(fetch.insn.insn).c_str());*/ \
131 npc = fetch.func(this, fetch.insn.insn, npc); \
135 if(debug
) for( ; i
< n
; i
++) // print out instructions as we go
139 // unrolled for speed
140 for( ; n
> 3 && i
< n
-3; i
+=4)
160 // update timer and possibly register a timer interrupt
161 uint32_t old_count
= state
.count
;
163 if(old_count
< state
.compare
&& uint64_t(old_count
) + i
>= state
.compare
)
164 set_interrupt(IRQ_TIMER
, true);
167 void processor_t::take_trap(reg_t pc
, trap_t
& t
)
170 fprintf(stderr
, "core %3d: exception %s, epc 0x%016" PRIx64
"\n",
173 // switch to supervisor, set previous supervisor bit, disable interrupts
174 set_pcr(PCR_SR
, (((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));
178 yield_load_reservation();
179 state
.cause
= t
.cause();
181 state
.pc
= state
.evec
;
183 t
.side_effects(&state
); // might set badvaddr etc.
186 void processor_t::deliver_ipi()
189 set_pcr(PCR_CLR_IPI
, 1);
192 void processor_t::disasm(insn_t insn
)
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());
199 reg_t
processor_t::set_pcr(int which
, reg_t val
)
201 reg_t old_pcr
= get_pcr(which
);
206 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
207 #ifndef RISCV_ENABLE_64BIT
208 state
.sr
&= ~(SR_S64
| SR_U64
);
210 #ifndef RISCV_ENABLE_FPU
213 #ifndef RISCV_ENABLE_VEC
216 state
.sr
&= ~SR_ZERO
;
229 set_interrupt(IRQ_TIMER
, false);
233 state
.ptbr
= val
& ~(PGSIZE
-1);
239 set_interrupt(IRQ_IPI
, val
& 1);
248 if (state
.tohost
== 0)
252 set_interrupt(IRQ_HOST
, val
!= 0);
253 state
.fromhost
= val
;
260 reg_t
processor_t::get_pcr(int which
)
269 return state
.badvaddr
;
275 return state
.compare
;
296 return state
.fromhost
;
301 void processor_t::set_interrupt(int which
, bool on
)
303 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
310 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
312 throw trap_illegal_instruction();
315 insn_func_t
processor_t::decode_insn(insn_t insn
)
317 bool rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
319 auto key
= insn
.bits() & ((1L << opcode_bits
)-1);
320 for (auto it
= opcode_map
.find(key
); it
!= opcode_map
.end() && it
->first
== key
; ++it
)
321 if ((insn
.bits() & it
->second
.mask
) == it
->second
.match
)
322 return rv64
? it
->second
.rv64
: it
->second
.rv32
;
324 return &illegal_instruction
;
327 void processor_t::register_insn(insn_desc_t desc
)
329 assert(desc
.mask
& 1);
330 if (opcode_bits
== 0 || (desc
.mask
& ((1L << opcode_bits
)-1)) != ((1L << opcode_bits
)-1))
333 while ((desc
.mask
& ((1L << (x
+1))-1)) == ((1L << (x
+1))-1) &&
334 (opcode_bits
== 0 || x
<= opcode_bits
))
338 decltype(opcode_map
) new_map
;
339 for (auto it
= opcode_map
.begin(); it
!= opcode_map
.end(); ++it
)
340 new_map
.insert(std::make_pair(it
->second
.match
& ((1L<<x
)-1), it
->second
));
341 opcode_map
= new_map
;
344 opcode_map
.insert(std::make_pair(desc
.match
& ((1L<<opcode_bits
)-1), desc
));
347 void processor_t::register_extension(extension_t
* x
)
349 for (auto insn
: x
->get_instructions())
351 for (auto disasm_insn
: x
->get_disasms())
352 disassembler
.add_insn(disasm_insn
);
354 throw std::logic_error("only one extension may be registered");
356 x
->set_processor(this);