12 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
)
13 : sim(*_sim
), mmu(*_mmu
), id(_id
), utidx(0)
17 // create microthreads
18 for (int i
=0; i
<MAX_UTS
; i
++)
19 uts
[i
] = new processor_t(&sim
, &mmu
, id
, i
);
22 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
,
24 : sim(*_sim
), mmu(*_mmu
), id(_id
)
27 set_pcr(PCR_SR
, sr
| SR_EF
| SR_EV
);
30 // microthreads don't possess their own microthreads
31 for (int i
=0; i
<MAX_UTS
; i
++)
35 processor_t::~processor_t()
39 void processor_t::reset()
43 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
44 // is in supervisor mode, and in 64-bit mode, if supported, with traps
45 // and virtual memory disabled. we accomplish this by setting EVEC to
46 // 0x2000 and *enabling* traps, then sending the core an IPI.
47 set_pcr(PCR_SR
, SR_S
| SR_S64
| SR_ET
| SR_IM
);
50 // the following state is undefined upon boot-up,
51 // but we zero it for determinism
77 void processor_t::set_fsr(uint32_t val
)
79 fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
82 void processor_t::vcfg()
84 if (nxpr_use
+ nfpr_use
< 2)
85 vlmax
= nxfpr_bank
* vecbanks_count
;
87 vlmax
= (nxfpr_bank
/ (nxpr_use
+ nfpr_use
- 1)) * vecbanks_count
;
89 vlmax
= std::min(vlmax
, MAX_UTS
);
92 void processor_t::setvl(int vlapp
)
94 vl
= std::min(vlmax
, vlapp
);
97 void processor_t::take_interrupt()
99 uint32_t interrupts
= interrupts_pending
;
100 interrupts
&= (sr
& SR_IM
) >> SR_IM_SHIFT
;
102 if(interrupts
&& (sr
& SR_ET
))
103 for(int i
= 0; ; i
++, interrupts
>>= 1)
105 throw interrupt_t(i
);
108 void processor_t::step(size_t n
, bool noisy
)
123 // execute_insn fetches and executes one instruction
124 #define execute_insn(noisy) \
126 insn = _mmu.load_insn(npc, sr & SR_EC, &func); \
127 if(noisy) disasm(insn,pc); \
128 npc = func(this, insn, npc); \
132 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
136 // unrolled for speed
137 for( ; n
> 3 && i
< n
-3; i
+=4)
152 // an exception occurred in the target processor
159 take_trap((1ULL << (8*sizeof(reg_t
)-1)) + t
.i
, noisy
);
161 catch(vt_command_t cmd
)
163 // this microthread has finished
165 assert(cmd
== vt_command_stop
);
177 // update timer and possibly register a timer interrupt
178 uint32_t old_count
= count
;
180 if(old_count
< compare
&& uint64_t(old_count
) + i
>= compare
)
181 interrupts_pending
|= 1 << IRQ_TIMER
;
184 void processor_t::take_trap(reg_t t
, bool noisy
)
189 printf("core %3d: interrupt %lld, pc 0x%016llx\n",
190 id
, (long long)(t
<< 1 >> 1), (unsigned long long)pc
);
192 printf("core %3d: trap %s, pc 0x%016llx\n",
193 id
, trap_name(trap_t(t
)), (unsigned long long)pc
);
196 // switch to supervisor, set previous supervisor bit, disable traps
197 set_pcr(PCR_SR
, (((sr
& ~SR_ET
) | SR_S
) & ~SR_PS
) | ((sr
& SR_S
) ? SR_PS
: 0));
201 badvaddr
= mmu
.get_badvaddr();
204 void processor_t::deliver_ipi()
206 set_pcr(PCR_CLR_IPI
, 1);
210 void processor_t::disasm(insn_t insn
, reg_t pc
)
212 // the disassembler is stateless, so we share it
213 static disassembler disasm
;
214 printf("core %3d: 0x%016llx (0x%08x) %s\n", id
, (unsigned long long)pc
,
215 insn
.bits
, disasm
.disassemble(insn
).c_str());
218 void processor_t::set_pcr(int which
, reg_t val
)
223 sr
= val
& ~SR_ZERO
; // clear SR bits that read as zero
224 #ifndef RISCV_ENABLE_64BIT
225 sr
&= ~(SR_S64
| SR_U64
);
227 #ifndef RISCV_ENABLE_FPU
230 #ifndef RISCV_ENABLE_RVC
233 #ifndef RISCV_ENABLE_VEC
236 // update MMU state and flush TLB
237 mmu
.set_vm_enabled(sr
& SR_VM
);
238 mmu
.set_supervisor(sr
& SR_S
);
240 // set the fixed-point register length
241 xprlen
= ((sr
& SR_S
) ? (sr
& SR_S64
) : (sr
& SR_U64
)) ? 64 : 32;
253 interrupts_pending
&= ~(1 << IRQ_TIMER
);
264 interrupts_pending
|= (1 << IRQ_IPI
);
266 interrupts_pending
&= ~(1 << IRQ_IPI
);
275 vecbanks
= val
& 0xff;
276 vecbanks_count
= __builtin_popcountll(vecbanks
);
289 reg_t
processor_t::get_pcr(int which
)
308 return mmu
.get_ptbr();