1 // See LICENSE for license details.
14 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
)
15 : sim(*_sim
), mmu(*_mmu
), id(_id
), utidx(0)
19 // create microthreads
20 for (int i
=0; i
<MAX_UTS
; i
++)
21 uts
[i
] = new processor_t(&sim
, &mmu
, id
, i
);
24 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
,
26 : sim(*_sim
), mmu(*_mmu
), id(_id
)
29 set_pcr(PCR_SR
, SR_U64
| SR_EF
| SR_EV
);
32 // microthreads don't possess their own microthreads
33 for (int i
=0; i
<MAX_UTS
; i
++)
37 processor_t::~processor_t()
41 void processor_t::reset(bool value
)
47 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
48 // is in supervisor mode, and in 64-bit mode, if supported, with traps
49 // and virtual memory disabled.
51 set_pcr(PCR_SR
, SR_S
| SR_S64
| SR_IM
);
54 // the following state is undefined upon boot-up,
55 // but we zero it for determinism
81 void processor_t::set_fsr(uint32_t val
)
83 fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
86 void processor_t::vcfg()
88 if (nxpr_use
+ nfpr_use
< 2)
89 vlmax
= nxfpr_bank
* vecbanks_count
;
91 vlmax
= (nxfpr_bank
/ (nxpr_use
+ nfpr_use
- 1)) * vecbanks_count
;
93 vlmax
= std::min(vlmax
, MAX_UTS
);
96 void processor_t::setvl(int vlapp
)
98 vl
= std::min(vlmax
, vlapp
);
101 void processor_t::take_interrupt()
103 uint32_t interrupts
= (sr
& SR_IP
) >> SR_IP_SHIFT
;
104 interrupts
&= (sr
& SR_IM
) >> SR_IM_SHIFT
;
106 if(interrupts
&& (sr
& SR_ET
))
107 for(int i
= 0; ; i
++, interrupts
>>= 1)
109 throw interrupt_t(i
);
112 void processor_t::step(size_t n
, bool noisy
)
125 // execute_insn fetches and executes one instruction
126 #define execute_insn(noisy) \
128 mmu_t::insn_fetch_t fetch = _mmu.load_insn(npc, sr & SR_EC); \
129 if(noisy) disasm(fetch.insn, npc); \
130 npc = fetch.func(this, fetch.insn, npc); \
134 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
138 // unrolled for speed
139 for( ; n
> 3 && i
< n
-3; i
+=4)
152 // an exception occurred in the target processor
157 take_trap((1ULL << (8*sizeof(reg_t
)-1)) + t
.i
, noisy
);
159 catch(vt_command_t cmd
)
161 // this microthread has finished
162 assert(cmd
== vt_command_stop
);
167 // update timer and possibly register a timer interrupt
168 uint32_t old_count
= count
;
170 if(old_count
< compare
&& uint64_t(old_count
) + i
>= compare
)
171 set_interrupt(IRQ_TIMER
, true);
174 void processor_t::take_trap(reg_t t
, bool noisy
)
179 printf("core %3d: interrupt %lld, pc 0x%016llx\n",
180 id
, (long long)(t
<< 1 >> 1), (unsigned long long)pc
);
182 printf("core %3d: trap %s, pc 0x%016llx\n",
183 id
, trap_name(trap_t(t
)), (unsigned long long)pc
);
186 // switch to supervisor, set previous supervisor bit, disable traps
187 set_pcr(PCR_SR
, (((sr
& ~SR_ET
) | SR_S
) & ~SR_PS
) | ((sr
& SR_S
) ? SR_PS
: 0));
191 badvaddr
= mmu
.get_badvaddr();
194 void processor_t::deliver_ipi()
197 set_pcr(PCR_CLR_IPI
, 1);
200 void processor_t::disasm(insn_t insn
, reg_t pc
)
202 // the disassembler is stateless, so we share it
203 static disassembler disasm
;
204 printf("core %3d: 0x%016llx (0x%08x) %s\n", id
, (unsigned long long)pc
,
205 insn
.bits
, disasm
.disassemble(insn
).c_str());
208 void processor_t::set_pcr(int which
, reg_t val
)
213 sr
= (val
& ~SR_IP
) | (sr
& SR_IP
);
214 #ifndef RISCV_ENABLE_64BIT
215 sr
&= ~(SR_S64
| SR_U64
);
217 #ifndef RISCV_ENABLE_FPU
220 #ifndef RISCV_ENABLE_RVC
223 #ifndef RISCV_ENABLE_VEC
239 set_interrupt(IRQ_TIMER
, false);
249 set_interrupt(IRQ_IPI
, val
& 1);
258 vecbanks
= val
& 0xff;
259 vecbanks_count
= __builtin_popcountll(vecbanks
);
266 set_interrupt(IRQ_HOST
, val
!= 0);
272 reg_t
processor_t::get_pcr(int which
)
291 return mmu
.get_ptbr();
310 void processor_t::set_interrupt(int which
, bool on
)
312 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;