b12a8e0976f841daaaba2847e112cd7715f04b67
1 // See LICENSE for license details.
18 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
)
19 : sim(_sim
), mmu(_mmu
), ext(NULL
), disassembler(new disassembler_t
),
20 id(_id
), run(false), debug(false)
23 mmu
->set_processor(this);
25 #define DECLARE_INSN(name, match, mask) REGISTER_INSN(this, name, match, mask)
31 processor_t::~processor_t()
37 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
38 // is in supervisor mode, and in 64-bit mode, if supported, with traps
39 // and virtual memory disabled.
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
76 set_pcr(CSR_STATUS
, state
.sr
);
79 ext
->reset(); // reset the extension
82 void processor_t::take_interrupt()
84 uint32_t interrupts
= (state
.sr
& SR_IP
) >> SR_IP_SHIFT
;
85 interrupts
&= (state
.sr
& SR_IM
) >> SR_IM_SHIFT
;
87 if (interrupts
&& (state
.sr
& SR_EI
))
88 for (int i
= 0; ; i
++, interrupts
>>= 1)
90 throw trap_t((1ULL << ((state
.sr
& SR_S64
) ? 63 : 31)) + i
);
93 void processor_t::step(size_t n
)
106 // execute_insn fetches and executes one instruction
107 #define execute_insn(noisy) \
109 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
110 if(noisy) disasm(fetch.insn.insn); \
111 npc = fetch.func(this, fetch.insn.insn, npc); \
115 // special execute_insn for commit log dumping
116 #ifdef RISCV_ENABLE_COMMITLOG
117 //static disassembler disasmblr;
119 #define execute_insn(noisy) \
121 mmu_t::insn_fetch_t fetch = _mmu->load_insn(npc); \
122 if(noisy) disasm(fetch.insn.insn); \
123 bool in_spvr = state.sr & SR_S; \
124 if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") ", npc, fetch.insn.insn.bits()); \
125 /*if (!in_spvr) fprintf(stderr, "\n0x%016" PRIx64 " (0x%08" PRIx32 ") %s ", npc, fetch.insn.insn.bits(), disasmblr.disassemble(fetch.insn.insn).c_str());*/ \
126 npc = fetch.func(this, fetch.insn.insn, npc); \
130 if(debug
) for( ; i
< n
; i
++) // print out instructions as we go
134 // unrolled for speed
135 for( ; n
> 3 && i
< n
-3; i
+=4)
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
)
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(CSR_STATUS
, (((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(CSR_CLEAR_IPI
, 1);
185 void processor_t::disasm(insn_t insn
)
187 // the disassembler is stateless, so we share it
188 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIx32
") %s\n",
189 id
, state
.pc
, insn
.bits(), disassembler
->disassemble(insn
).c_str());
192 reg_t
processor_t::set_pcr(int which
, reg_t val
)
194 reg_t old_pcr
= get_pcr(which
);
199 state
.fflags
= val
& (FSR_AEXC
>> FSR_AEXC_SHIFT
);
202 state
.frm
= val
& (FSR_RD
>> FSR_RD_SHIFT
);
205 state
.fflags
= (val
& FSR_AEXC
) >> FSR_AEXC_SHIFT
;
206 state
.frm
= (val
& FSR_RD
) >> FSR_RD_SHIFT
;
209 state
.sr
= (val
& ~SR_IP
) | (state
.sr
& SR_IP
);
210 #ifndef RISCV_ENABLE_64BIT
211 state
.sr
&= ~(SR_S64
| SR_U64
);
213 #ifndef RISCV_ENABLE_FPU
218 state
.sr
&= ~SR_ZERO
;
219 rv64
= (state
.sr
& SR_S
) ? (state
.sr
& SR_S64
) : (state
.sr
& SR_U64
);
235 set_interrupt(IRQ_TIMER
, false);
239 state
.ptbr
= val
& ~(PGSIZE
-1);
245 set_interrupt(IRQ_IPI
, val
& 1);
254 if (state
.tohost
== 0)
258 set_interrupt(IRQ_HOST
, val
!= 0);
259 state
.fromhost
= val
;
266 reg_t
processor_t::get_pcr(int which
)
275 return (state
.fflags
<< FSR_AEXC_SHIFT
) | (state
.frm
<< FSR_RD_SHIFT
);
281 return state
.badvaddr
;
290 return state
.compare
;
311 return state
.fromhost
;
317 void processor_t::set_interrupt(int which
, bool on
)
319 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
326 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
)
328 throw trap_illegal_instruction();
331 insn_func_t
processor_t::decode_insn(insn_t insn
)
333 size_t mask
= opcode_map
.size()-1;
334 insn_desc_t
* desc
= opcode_map
[insn
.bits() & mask
];
336 while ((insn
.bits() & desc
->mask
) != desc
->match
)
339 return rv64
? desc
->rv64
: desc
->rv32
;
342 void processor_t::register_insn(insn_desc_t desc
)
344 assert(desc
.mask
& 1);
345 instructions
.push_back(desc
);
348 void processor_t::build_opcode_map()
351 for (auto& inst
: instructions
)
352 while ((inst
.mask
& buckets
) != buckets
)
357 decltype(insn_desc_t::match
) mask
;
358 cmp(decltype(mask
) mask
) : mask(mask
) {}
359 bool operator()(const insn_desc_t
& lhs
, const insn_desc_t
& rhs
) {
360 if ((lhs
.match
& mask
) != (rhs
.match
& mask
))
361 return (lhs
.match
& mask
) < (rhs
.match
& mask
);
362 return lhs
.match
< rhs
.match
;
365 std::sort(instructions
.begin(), instructions
.end(), cmp(buckets
-1));
367 opcode_map
.resize(buckets
);
368 opcode_store
.resize(instructions
.size() + 1);
371 for (size_t b
= 0, i
= 0; b
< buckets
; b
++)
373 opcode_map
[b
] = &opcode_store
[j
];
374 while (i
< instructions
.size() && b
== (instructions
[i
].match
& (buckets
-1)))
375 opcode_store
[j
++] = instructions
[i
++];
378 assert(j
== opcode_store
.size()-1);
379 opcode_store
[j
].match
= opcode_store
[j
].mask
= 0;
380 opcode_store
[j
].rv32
= &illegal_instruction
;
381 opcode_store
[j
].rv64
= &illegal_instruction
;
384 void processor_t::register_extension(extension_t
* x
)
386 for (auto insn
: x
->get_instructions())
389 for (auto disasm_insn
: x
->get_disasms())
390 disassembler
->add_insn(disasm_insn
);
392 throw std::logic_error("only one extension may be registered");
394 x
->set_processor(this);