839c846f3dd61b6ab431fbaeba6c3486a62f5f78
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); \
110 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
114 // unrolled for speed
115 for( ; n
> 3 && i
< n
-3; i
+=4)
130 take_trap(npc
, t
, noisy
);
135 // update timer and possibly register a timer interrupt
136 uint32_t old_count
= state
.count
;
138 if(old_count
< state
.compare
&& uint64_t(old_count
) + i
>= state
.compare
)
139 set_interrupt(IRQ_TIMER
, true);
142 void processor_t::take_trap(reg_t pc
, trap_t
& t
, bool noisy
)
145 fprintf(stderr
, "core %3d: exception %s, epc 0x%016" PRIx64
"\n",
148 // switch to supervisor, set previous supervisor bit, disable interrupts
149 set_pcr(PCR_SR
, (((state
.sr
& ~SR_EI
) | SR_S
) & ~SR_PS
& ~SR_PEI
) |
150 ((state
.sr
& SR_S
) ? SR_PS
: 0) |
151 ((state
.sr
& SR_EI
) ? SR_PEI
: 0));
153 yield_load_reservation();
154 state
.cause
= t
.cause();
156 state
.pc
= state
.evec
;
158 t
.side_effects(&state
); // might set badvaddr etc.
161 void processor_t::deliver_ipi()
164 set_pcr(PCR_CLR_IPI
, 1);
167 void processor_t::disasm(insn_t insn
, reg_t pc
)
169 // the disassembler is stateless, so we share it
170 static disassembler disasm
;
171 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIx32
") %s\n",
172 id
, state
.pc
, insn
.bits(), disasm
.disassemble(insn
).c_str());
175 reg_t
processor_t::set_pcr(int which
, reg_t val
)
177 reg_t old_pcr
= get_pcr(which
);
182 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
183 #ifndef RISCV_ENABLE_64BIT
184 state
.sr
&= ~(SR_S64
| SR_U64
);
186 #ifndef RISCV_ENABLE_FPU
189 #ifndef RISCV_ENABLE_VEC
192 state
.sr
&= ~SR_ZERO
;
205 set_interrupt(IRQ_TIMER
, false);
209 state
.ptbr
= val
& ~(PGSIZE
-1);
215 set_interrupt(IRQ_IPI
, val
& 1);
224 if (state
.tohost
== 0)
228 set_interrupt(IRQ_HOST
, val
!= 0);
229 state
.fromhost
= val
;
236 reg_t
processor_t::get_pcr(int which
)
245 return state
.badvaddr
;
251 return state
.compare
;
272 return state
.fromhost
;
277 void processor_t::set_interrupt(int which
, bool on
)
279 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
286 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
288 throw trap_illegal_instruction();
291 insn_func_t
processor_t::decode_insn(insn_t insn
)
293 bool rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
295 auto key
= insn
.bits() & ((1L << opcode_bits
)-1);
296 for (auto it
= opcode_map
.find(key
); it
!= opcode_map
.end() && it
->first
== key
; ++it
)
297 if ((insn
.bits() & it
->second
.mask
) == it
->second
.match
)
298 return rv64
? it
->second
.rv64
: it
->second
.rv32
;
300 return &illegal_instruction
;
303 void processor_t::register_insn(insn_desc_t desc
)
305 assert(desc
.mask
& 1);
306 if (opcode_bits
== 0 || (desc
.mask
& ((1L << opcode_bits
)-1)) != ((1L << opcode_bits
)-1))
309 while ((desc
.mask
& ((1L << (x
+1))-1)) == ((1L << (x
+1))-1) &&
310 (opcode_bits
== 0 || x
<= opcode_bits
))
314 decltype(opcode_map
) new_map
;
315 for (auto it
= opcode_map
.begin(); it
!= opcode_map
.end(); ++it
)
316 new_map
.insert(std::make_pair(it
->second
.match
& ((1L<<x
)-1), it
->second
));
317 opcode_map
= new_map
;
320 opcode_map
.insert(std::make_pair(desc
.match
& ((1L<<opcode_bits
)-1), desc
));
323 void processor_t::register_extension(extension_t
* x
)
325 for (auto insn
: x
->get_instructions())
328 throw std::logic_error("only one extension may be registered");
330 x
->set_processor(this);