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
), 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::reset(bool value
)
71 uint32_t processor_t::set_fsr(uint32_t val
)
73 uint32_t old_fsr
= state
.fsr
;
74 state
.fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
78 void processor_t::take_interrupt()
80 uint32_t interrupts
= (state
.sr
& SR_IP
) >> SR_IP_SHIFT
;
81 interrupts
&= (state
.sr
& SR_IM
) >> SR_IM_SHIFT
;
83 if (interrupts
&& (state
.sr
& SR_EI
))
84 for (int i
= 0; ; i
++, interrupts
>>= 1)
86 throw trap_t((1ULL << ((state
.sr
& SR_S64
) ? 63 : 31)) + i
);
89 void processor_t::step(size_t n
, bool noisy
)
102 // execute_insn fetches and executes one instruction
103 #define execute_insn(noisy) \
105 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
106 if(noisy) disasm(fetch.insn.insn, npc); \
107 npc = fetch.func(this, fetch.insn.insn, npc); \
111 // special execute_insn for commit log dumping
112 #ifdef RISCV_ENABLE_COMMITLOG
113 //static disassembler disasmblr;
115 #define execute_insn(noisy) \
117 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
118 if(noisy) disasm(fetch.insn.insn, npc); \
119 bool in_spvr = state.sr & SR_S; \
120 if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") ", npc, fetch.insn.insn.bits()); \
121 /*if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") %s ", npc, fetch.insn.insn.bits(), disasmblr.disassemble(fetch.insn.insn).c_str());*/ \
122 npc = fetch.func(this, fetch.insn.insn, npc); \
126 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
130 // unrolled for speed
131 for( ; n
> 3 && i
< n
-3; i
+=4)
146 take_trap(npc
, t
, noisy
);
151 // update timer and possibly register a timer interrupt
152 uint32_t old_count
= state
.count
;
154 if(old_count
< state
.compare
&& uint64_t(old_count
) + i
>= state
.compare
)
155 set_interrupt(IRQ_TIMER
, true);
158 void processor_t::take_trap(reg_t pc
, trap_t
& t
, bool noisy
)
161 fprintf(stderr
, "core %3d: exception %s, epc 0x%016" PRIx64
"\n",
164 // switch to supervisor, set previous supervisor bit, disable interrupts
165 set_pcr(PCR_SR
, (((state
.sr
& ~SR_EI
) | SR_S
) & ~SR_PS
& ~SR_PEI
) |
166 ((state
.sr
& SR_S
) ? SR_PS
: 0) |
167 ((state
.sr
& SR_EI
) ? SR_PEI
: 0));
169 yield_load_reservation();
170 state
.cause
= t
.cause();
172 state
.pc
= state
.evec
;
174 t
.side_effects(&state
); // might set badvaddr etc.
177 void processor_t::deliver_ipi()
180 set_pcr(PCR_CLR_IPI
, 1);
183 void processor_t::disasm(insn_t insn
, reg_t pc
)
185 // the disassembler is stateless, so we share it
186 static disassembler disasm
;
187 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIx32
") %s\n",
188 id
, state
.pc
, insn
.bits(), disasm
.disassemble(insn
).c_str());
191 reg_t
processor_t::set_pcr(int which
, reg_t val
)
193 reg_t old_pcr
= get_pcr(which
);
198 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
199 #ifndef RISCV_ENABLE_64BIT
200 state
.sr
&= ~(SR_S64
| SR_U64
);
202 #ifndef RISCV_ENABLE_FPU
205 #ifndef RISCV_ENABLE_VEC
208 state
.sr
&= ~SR_ZERO
;
221 set_interrupt(IRQ_TIMER
, false);
225 state
.ptbr
= val
& ~(PGSIZE
-1);
231 set_interrupt(IRQ_IPI
, val
& 1);
240 if (state
.tohost
== 0)
244 set_interrupt(IRQ_HOST
, val
!= 0);
245 state
.fromhost
= val
;
252 reg_t
processor_t::get_pcr(int which
)
261 return state
.badvaddr
;
267 return state
.compare
;
288 return state
.fromhost
;
293 void processor_t::set_interrupt(int which
, bool on
)
295 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
302 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
304 throw trap_illegal_instruction();
307 insn_func_t
processor_t::decode_insn(insn_t insn
)
309 bool rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
311 auto key
= insn
.bits() & ((1L << opcode_bits
)-1);
312 for (auto it
= opcode_map
.find(key
); it
!= opcode_map
.end() && it
->first
== key
; ++it
)
313 if ((insn
.bits() & it
->second
.mask
) == it
->second
.match
)
314 return rv64
? it
->second
.rv64
: it
->second
.rv32
;
316 return &illegal_instruction
;
319 void processor_t::register_insn(insn_desc_t desc
)
321 assert(desc
.mask
& 1);
322 if (opcode_bits
== 0 || (desc
.mask
& ((1L << opcode_bits
)-1)) != ((1L << opcode_bits
)-1))
325 while ((desc
.mask
& ((1L << (x
+1))-1)) == ((1L << (x
+1))-1) &&
326 (opcode_bits
== 0 || x
<= opcode_bits
))
330 decltype(opcode_map
) new_map
;
331 for (auto it
= opcode_map
.begin(); it
!= opcode_map
.end(); ++it
)
332 new_map
.insert(std::make_pair(it
->second
.match
& ((1L<<x
)-1), it
->second
));
333 opcode_map
= new_map
;
336 opcode_map
.insert(std::make_pair(desc
.match
& ((1L<<opcode_bits
)-1), desc
));
339 void processor_t::register_extension(extension_t
* x
)
341 for (auto insn
: x
->get_instructions())
344 throw std::logic_error("only one extension may be registered");
346 x
->set_processor(this);