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.
50 set_pcr(PCR_SR
, SR_S
| SR_S64
| SR_IM
);
53 // the following state is undefined upon boot-up,
54 // but we zero it for determinism
80 void processor_t::set_fsr(uint32_t val
)
82 fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
85 void processor_t::vcfg()
87 if (nxpr_use
+ nfpr_use
< 2)
88 vlmax
= nxfpr_bank
* vecbanks_count
;
90 vlmax
= (nxfpr_bank
/ (nxpr_use
+ nfpr_use
- 1)) * vecbanks_count
;
92 vlmax
= std::min(vlmax
, MAX_UTS
);
95 void processor_t::setvl(int vlapp
)
97 vl
= std::min(vlmax
, vlapp
);
100 void processor_t::take_interrupt()
102 uint32_t interrupts
= (sr
& SR_IP
) >> SR_IP_SHIFT
;
103 interrupts
&= (sr
& SR_IM
) >> SR_IM_SHIFT
;
105 if(interrupts
&& (sr
& SR_ET
))
106 for(int i
= 0; ; i
++, interrupts
>>= 1)
108 throw interrupt_t(i
);
111 void processor_t::step(size_t n
, bool noisy
)
124 // execute_insn fetches and executes one instruction
125 #define execute_insn(noisy) \
127 mmu_t::insn_fetch_t fetch = _mmu.load_insn(npc, sr & SR_EC); \
128 if(noisy) disasm(fetch.insn, npc); \
129 npc = fetch.func(this, fetch.insn, npc); \
133 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
137 // unrolled for speed
138 for( ; n
> 3 && i
< n
-3; i
+=4)
151 // an exception occurred in the target processor
156 take_trap((1ULL << (8*sizeof(reg_t
)-1)) + t
.i
, noisy
);
158 catch(vt_command_t cmd
)
160 // this microthread has finished
161 assert(cmd
== vt_command_stop
);
166 // update timer and possibly register a timer interrupt
167 uint32_t old_count
= count
;
169 if(old_count
< compare
&& uint64_t(old_count
) + i
>= compare
)
170 set_interrupt(IRQ_TIMER
, true);
173 void processor_t::take_trap(reg_t t
, bool noisy
)
178 printf("core %3d: interrupt %lld, pc 0x%016llx\n",
179 id
, (long long)(t
<< 1 >> 1), (unsigned long long)pc
);
181 printf("core %3d: trap %s, pc 0x%016llx\n",
182 id
, trap_name(trap_t(t
)), (unsigned long long)pc
);
185 // switch to supervisor, set previous supervisor bit, disable traps
186 set_pcr(PCR_SR
, (((sr
& ~SR_ET
) | SR_S
) & ~SR_PS
) | ((sr
& SR_S
) ? SR_PS
: 0));
190 badvaddr
= mmu
.get_badvaddr();
193 void processor_t::deliver_ipi()
196 set_pcr(PCR_CLR_IPI
, 1);
199 void processor_t::disasm(insn_t insn
, reg_t pc
)
201 // the disassembler is stateless, so we share it
202 static disassembler disasm
;
203 printf("core %3d: 0x%016llx (0x%08x) %s\n", id
, (unsigned long long)pc
,
204 insn
.bits
, disasm
.disassemble(insn
).c_str());
207 void processor_t::set_pcr(int which
, reg_t val
)
212 sr
= val
& ~SR_ZERO
; // clear SR bits that read as zero
213 #ifndef RISCV_ENABLE_64BIT
214 sr
&= ~(SR_S64
| SR_U64
);
216 #ifndef RISCV_ENABLE_FPU
219 #ifndef RISCV_ENABLE_RVC
222 #ifndef RISCV_ENABLE_VEC
225 // update MMU state and flush TLB
228 // set the fixed-point register length
229 xprlen
= ((sr
& SR_S
) ? (sr
& SR_S64
) : (sr
& SR_U64
)) ? 64 : 32;
241 set_interrupt(IRQ_TIMER
, false);
251 set_interrupt(IRQ_IPI
, val
& 1);
260 vecbanks
= val
& 0xff;
261 vecbanks_count
= __builtin_popcountll(vecbanks
);
268 set_interrupt(IRQ_HOST
, val
!= 0);
274 reg_t
processor_t::get_pcr(int which
)
293 return mmu
.get_ptbr();
312 void processor_t::set_interrupt(int which
, bool on
)
314 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;