11 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
)
12 : sim(*_sim
), mmu(*_mmu
), id(_id
), utidx(0)
16 // create microthreads
17 for (int i
=0; i
<MAX_UTS
; i
++)
18 uts
[i
] = new processor_t(&sim
, &mmu
, id
, i
);
21 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
,
23 : sim(*_sim
), mmu(*_mmu
), id(_id
)
26 set_pcr(PCR_SR
, sr
| SR_EF
| SR_EV
);
29 // microthreads don't possess their own microthreads
30 for (int i
=0; i
<MAX_UTS
; i
++)
34 processor_t::~processor_t()
38 void processor_t::reset()
42 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
43 // is in supervisor mode, and in 64-bit mode, if supported, with traps
44 // and virtual memory disabled. we accomplish this by setting EVEC to
45 // 0x2000 and *enabling* traps, then sending the core an IPI.
46 set_pcr(PCR_SR
, SR_S
| SR_S64
| SR_ET
| SR_IM
);
49 // the following state is undefined upon boot-up,
50 // but we zero it for determinism
76 void processor_t::set_fsr(uint32_t val
)
78 fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
81 void processor_t::vcfg()
83 if (nxpr_use
+ nfpr_use
< 2)
84 vlmax
= nxfpr_bank
* vecbanks_count
;
86 vlmax
= (nxfpr_bank
/ (nxpr_use
+ nfpr_use
- 1)) * vecbanks_count
;
88 vlmax
= std::min(vlmax
, MAX_UTS
);
91 void processor_t::setvl(int vlapp
)
93 vl
= std::min(vlmax
, vlapp
);
96 void processor_t::take_interrupt()
98 uint32_t interrupts
= interrupts_pending
;
99 interrupts
&= (sr
& SR_IM
) >> SR_IM_SHIFT
;
101 if(interrupts
&& (sr
& SR_ET
))
102 for(int i
= 0; ; i
++, interrupts
>>= 1)
104 throw interrupt_t(i
);
107 void processor_t::step(size_t n
, bool noisy
)
122 // execute_insn fetches and executes one instruction
123 #define execute_insn(noisy) \
125 insn = _mmu.load_insn(npc, sr & SR_EC, &func); \
126 if(noisy) disasm(insn,pc); \
127 npc = func(this, insn, npc); \
131 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
135 // unrolled for speed
136 for( ; n
> 3 && i
< n
-3; i
+=4)
151 // an exception occurred in the target processor
158 take_trap((1ULL << (8*sizeof(reg_t
)-1)) + t
.i
, noisy
);
160 catch(vt_command_t cmd
)
162 // this microthread has finished
164 assert(cmd
== vt_command_stop
);
176 // update timer and possibly register a timer interrupt
177 uint32_t old_count
= count
;
179 if(old_count
< compare
&& uint64_t(old_count
) + i
>= compare
)
180 interrupts_pending
|= 1 << IRQ_TIMER
;
183 void processor_t::take_trap(reg_t t
, bool noisy
)
186 printf("core %3d: trap %s, pc 0x%016llx\n",
187 id
, trap_name(trap_t(t
)), (unsigned long long)pc
);
189 // switch to supervisor, set previous supervisor bit, disable traps
190 set_pcr(PCR_SR
, (((sr
& ~SR_ET
) | SR_S
) & ~SR_PS
) | ((sr
& SR_S
) ? SR_PS
: 0));
194 badvaddr
= mmu
.get_badvaddr();
197 void processor_t::deliver_ipi()
199 interrupts_pending
|= 1 << IRQ_IPI
;
203 void processor_t::disasm(insn_t insn
, reg_t pc
)
205 // the disassembler is stateless, so we share it
206 static disassembler disasm
;
207 printf("core %3d: 0x%016llx (0x%08x) %s\n", id
, (unsigned long long)pc
,
208 insn
.bits
, disasm
.disassemble(insn
).c_str());
211 void processor_t::set_pcr(int which
, reg_t val
)
216 sr
= val
& ~SR_ZERO
; // clear SR bits that read as zero
217 #ifndef RISCV_ENABLE_64BIT
218 sr
&= ~(SR_S64
| SR_U64
);
220 #ifndef RISCV_ENABLE_FPU
223 #ifndef RISCV_ENABLE_RVC
226 #ifndef RISCV_ENABLE_VEC
229 // update MMU state and flush TLB
230 mmu
.set_vm_enabled(sr
& SR_VM
);
231 mmu
.set_supervisor(sr
& SR_S
);
233 // set the fixed-point register length
234 xprlen
= ((sr
& SR_S
) ? (sr
& SR_S64
) : (sr
& SR_U64
)) ? 64 : 32;
246 interrupts_pending
&= ~(1 << IRQ_TIMER
);
256 interrupts_pending
&= ~(1 << IRQ_IPI
);
265 vecbanks
= val
& 0xff;
266 vecbanks_count
= __builtin_popcountll(vecbanks
);
274 reg_t
processor_t::get_pcr(int which
)
293 return mmu
.get_ptbr();
305 return sim
.get_tohost();
307 return sim
.get_fromhost();