1 // See LICENSE for license details.
16 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
)
17 : sim(_sim
), mmu(_mmu
), ext(NULL
), id(_id
), opcode_bits(0)
20 mmu
->set_processor(this);
22 #define DECLARE_INSN(name, match, mask) REGISTER_INSN(this, name, match, mask)
27 processor_t::~processor_t()
33 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
34 // is in supervisor mode, and in 64-bit mode, if supported, with traps
35 // and virtual memory disabled.
37 #ifdef RISCV_ENABLE_64BIT
42 // the following state is undefined upon boot-up,
43 // but we zero it for determinism
58 load_reservation
= -1;
61 void processor_t::reset(bool value
)
70 uint32_t processor_t::set_fsr(uint32_t val
)
72 uint32_t old_fsr
= state
.fsr
;
73 state
.fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
77 void processor_t::take_interrupt()
79 uint32_t interrupts
= (state
.sr
& SR_IP
) >> SR_IP_SHIFT
;
80 interrupts
&= (state
.sr
& SR_IM
) >> SR_IM_SHIFT
;
82 if (interrupts
&& (state
.sr
& SR_EI
))
83 for (int i
= 0; ; i
++, interrupts
>>= 1)
85 throw trap_t((1ULL << ((state
.sr
& SR_S64
) ? 63 : 31)) + i
);
88 void processor_t::step(size_t n
, bool noisy
)
101 // execute_insn fetches and executes one instruction
102 #define execute_insn(noisy) \
104 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
105 if(noisy) disasm(fetch.insn, npc); \
106 npc = fetch.func(this, fetch.insn, npc); \
109 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
113 // unrolled for speed
114 for( ; n
> 3 && i
< n
-3; i
+=4)
129 take_trap(npc
, t
, noisy
);
134 // update timer and possibly register a timer interrupt
135 uint32_t old_count
= state
.count
;
137 if(old_count
< state
.compare
&& uint64_t(old_count
) + i
>= state
.compare
)
138 set_interrupt(IRQ_TIMER
, true);
141 void processor_t::take_trap(reg_t pc
, trap_t
& t
, bool noisy
)
144 fprintf(stderr
, "core %3d: exception %s, epc 0x%016" PRIx64
"\n",
147 // switch to supervisor, set previous supervisor bit, disable interrupts
148 set_pcr(PCR_SR
, (((state
.sr
& ~SR_EI
) | SR_S
) & ~SR_PS
& ~SR_PEI
) |
149 ((state
.sr
& SR_S
) ? SR_PS
: 0) |
150 ((state
.sr
& SR_EI
) ? SR_PEI
: 0));
152 yield_load_reservation();
153 state
.cause
= t
.cause();
155 state
.pc
= state
.evec
;
157 t
.side_effects(&state
); // might set badvaddr etc.
160 void processor_t::deliver_ipi()
163 set_pcr(PCR_CLR_IPI
, 1);
166 void processor_t::disasm(insn_t insn
, reg_t pc
)
168 // the disassembler is stateless, so we share it
169 static disassembler disasm
;
170 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIxFAST32
") %s\n",
171 id
, state
.pc
, insn
.bits
, disasm
.disassemble(insn
).c_str());
174 reg_t
processor_t::set_pcr(int which
, reg_t val
)
176 reg_t old_pcr
= get_pcr(which
);
181 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
182 #ifndef RISCV_ENABLE_64BIT
183 state
.sr
&= ~(SR_S64
| SR_U64
);
185 #ifndef RISCV_ENABLE_FPU
188 #ifndef RISCV_ENABLE_VEC
191 state
.sr
&= ~SR_ZERO
;
204 set_interrupt(IRQ_TIMER
, false);
208 state
.ptbr
= val
& ~(PGSIZE
-1);
214 set_interrupt(IRQ_IPI
, val
& 1);
223 if (state
.tohost
== 0)
227 set_interrupt(IRQ_HOST
, val
!= 0);
228 state
.fromhost
= val
;
235 reg_t
processor_t::get_pcr(int which
)
244 return state
.badvaddr
;
250 return state
.compare
;
271 return state
.fromhost
;
276 void processor_t::set_interrupt(int which
, bool on
)
278 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
285 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
287 throw trap_illegal_instruction();
290 insn_func_t
processor_t::decode_insn(insn_t insn
)
292 bool rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
294 auto key
= insn
.bits
& ((1L << opcode_bits
)-1);
295 for (auto it
= opcode_map
.find(key
); it
!= opcode_map
.end() && it
->first
== key
; ++it
)
296 if ((insn
.bits
& it
->second
.mask
) == it
->second
.match
)
297 return rv64
? it
->second
.rv64
: it
->second
.rv32
;
299 return &illegal_instruction
;
302 void processor_t::register_insn(insn_desc_t desc
)
304 assert(desc
.mask
& 1);
305 if (opcode_bits
== 0 || (desc
.mask
& ((1L << opcode_bits
)-1)) != ((1L << opcode_bits
)-1))
308 while ((desc
.mask
& ((1L << (x
+1))-1)) == ((1L << (x
+1))-1) &&
309 (opcode_bits
== 0 || x
<= opcode_bits
))
313 decltype(opcode_map
) new_map
;
314 for (auto it
= opcode_map
.begin(); it
!= opcode_map
.end(); ++it
)
315 new_map
.insert(std::make_pair(it
->second
.match
& ((1L<<x
)-1), it
->second
));
316 opcode_map
= new_map
;
319 opcode_map
.insert(std::make_pair(desc
.match
& ((1L<<opcode_bits
)-1), desc
));
322 void processor_t::register_extension(extension_t
* x
)
324 for (auto insn
: x
->get_instructions())
327 throw std::logic_error("only one extension may be registered");
329 x
->set_processor(this);