77a81b3cce66c625cc0f225e119d09159a095b2c
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
)
68 state
.reset(); // reset the core
70 ext
->reset(); // reset the extension
73 uint32_t processor_t::set_fsr(uint32_t val
)
75 uint32_t old_fsr
= state
.fsr
;
76 state
.fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
80 void processor_t::take_interrupt()
82 uint32_t interrupts
= (state
.sr
& SR_IP
) >> SR_IP_SHIFT
;
83 interrupts
&= (state
.sr
& SR_IM
) >> SR_IM_SHIFT
;
85 if (interrupts
&& (state
.sr
& SR_EI
))
86 for (int i
= 0; ; i
++, interrupts
>>= 1)
88 throw trap_t((1ULL << ((state
.sr
& SR_S64
) ? 63 : 31)) + i
);
91 void processor_t::step(size_t n
, bool noisy
)
104 // execute_insn fetches and executes one instruction
105 #define execute_insn(noisy) \
107 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
108 if(noisy) disasm(fetch.insn.insn, npc); \
109 npc = fetch.func(this, fetch.insn.insn, npc); \
113 // special execute_insn for commit log dumping
114 #ifdef RISCV_ENABLE_COMMITLOG
115 //static disassembler disasmblr;
117 #define execute_insn(noisy) \
119 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
120 if(noisy) disasm(fetch.insn.insn, npc); \
121 bool in_spvr = state.sr & SR_S; \
122 if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") ", npc, fetch.insn.insn.bits()); \
123 /*if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") %s ", npc, fetch.insn.insn.bits(), disasmblr.disassemble(fetch.insn.insn).c_str());*/ \
124 npc = fetch.func(this, fetch.insn.insn, npc); \
128 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
132 // unrolled for speed
133 for( ; n
> 3 && i
< n
-3; i
+=4)
148 take_trap(npc
, t
, noisy
);
153 // update timer and possibly register a timer interrupt
154 uint32_t old_count
= state
.count
;
156 if(old_count
< state
.compare
&& uint64_t(old_count
) + i
>= state
.compare
)
157 set_interrupt(IRQ_TIMER
, true);
160 void processor_t::take_trap(reg_t pc
, trap_t
& t
, bool noisy
)
163 fprintf(stderr
, "core %3d: exception %s, epc 0x%016" PRIx64
"\n",
166 // switch to supervisor, set previous supervisor bit, disable interrupts
167 set_pcr(PCR_SR
, (((state
.sr
& ~SR_EI
) | SR_S
) & ~SR_PS
& ~SR_PEI
) |
168 ((state
.sr
& SR_S
) ? SR_PS
: 0) |
169 ((state
.sr
& SR_EI
) ? SR_PEI
: 0));
171 yield_load_reservation();
172 state
.cause
= t
.cause();
174 state
.pc
= state
.evec
;
176 t
.side_effects(&state
); // might set badvaddr etc.
179 void processor_t::deliver_ipi()
182 set_pcr(PCR_CLR_IPI
, 1);
185 void processor_t::disasm(insn_t insn
, reg_t pc
)
187 // the disassembler is stateless, so we share it
188 static disassembler disasm
;
189 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIx32
") %s\n",
190 id
, state
.pc
, insn
.bits(), disasm
.disassemble(insn
).c_str());
193 reg_t
processor_t::set_pcr(int which
, reg_t val
)
195 reg_t old_pcr
= get_pcr(which
);
200 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
201 #ifndef RISCV_ENABLE_64BIT
202 state
.sr
&= ~(SR_S64
| SR_U64
);
204 #ifndef RISCV_ENABLE_FPU
207 #ifndef RISCV_ENABLE_VEC
210 state
.sr
&= ~SR_ZERO
;
223 set_interrupt(IRQ_TIMER
, false);
227 state
.ptbr
= val
& ~(PGSIZE
-1);
233 set_interrupt(IRQ_IPI
, val
& 1);
242 if (state
.tohost
== 0)
246 set_interrupt(IRQ_HOST
, val
!= 0);
247 state
.fromhost
= val
;
254 reg_t
processor_t::get_pcr(int which
)
263 return state
.badvaddr
;
269 return state
.compare
;
290 return state
.fromhost
;
295 void processor_t::set_interrupt(int which
, bool on
)
297 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
304 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
306 throw trap_illegal_instruction();
309 insn_func_t
processor_t::decode_insn(insn_t insn
)
311 bool rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
313 auto key
= insn
.bits() & ((1L << opcode_bits
)-1);
314 for (auto it
= opcode_map
.find(key
); it
!= opcode_map
.end() && it
->first
== key
; ++it
)
315 if ((insn
.bits() & it
->second
.mask
) == it
->second
.match
)
316 return rv64
? it
->second
.rv64
: it
->second
.rv32
;
318 return &illegal_instruction
;
321 void processor_t::register_insn(insn_desc_t desc
)
323 assert(desc
.mask
& 1);
324 if (opcode_bits
== 0 || (desc
.mask
& ((1L << opcode_bits
)-1)) != ((1L << opcode_bits
)-1))
327 while ((desc
.mask
& ((1L << (x
+1))-1)) == ((1L << (x
+1))-1) &&
328 (opcode_bits
== 0 || x
<= opcode_bits
))
332 decltype(opcode_map
) new_map
;
333 for (auto it
= opcode_map
.begin(); it
!= opcode_map
.end(); ++it
)
334 new_map
.insert(std::make_pair(it
->second
.match
& ((1L<<x
)-1), it
->second
));
335 opcode_map
= new_map
;
338 opcode_map
.insert(std::make_pair(desc
.match
& ((1L<<opcode_bits
)-1), desc
));
341 void processor_t::register_extension(extension_t
* x
)
343 for (auto insn
: x
->get_instructions())
346 throw std::logic_error("only one extension may be registered");
348 x
->set_processor(this);