9f87f75c0300779d52390d120a41e2285e3cff3f
[riscv-isa-sim.git] / riscv / processor.cc
1 #include "processor.h"
2 #include "common.h"
3 #include "config.h"
4 #include "sim.h"
5 #include "disasm.h"
6 #include <inttypes.h>
7 #include <cmath>
8 #include <cstdlib>
9 #include <iostream>
10 #include <assert.h>
11
12 processor_t::processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id)
13 : sim(*_sim), mmu(*_mmu), id(_id), utidx(0)
14 {
15 reset(true);
16
17 // create microthreads
18 for (int i=0; i<MAX_UTS; i++)
19 uts[i] = new processor_t(&sim, &mmu, id, i);
20 }
21
22 processor_t::processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id,
23 uint32_t _utidx)
24 : sim(*_sim), mmu(*_mmu), id(_id)
25 {
26 reset(true);
27 set_pcr(PCR_SR, SR_U64 | SR_EF | SR_EV);
28 utidx = _utidx;
29
30 // microthreads don't possess their own microthreads
31 for (int i=0; i<MAX_UTS; i++)
32 uts[i] = NULL;
33 }
34
35 processor_t::~processor_t()
36 {
37 }
38
39 void processor_t::reset(bool value)
40 {
41 if (run == !value)
42 return;
43 run = !value;
44
45 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
46 // is in supervisor mode, and in 64-bit mode, if supported, with traps
47 // and virtual memory disabled.
48 set_pcr(PCR_SR, SR_S | SR_S64 | SR_IM);
49 pc = 0x2000;
50
51 // the following state is undefined upon boot-up,
52 // but we zero it for determinism
53 XPR.reset();
54 FPR.reset();
55
56 evec = 0;
57 epc = 0;
58 badvaddr = 0;
59 cause = 0;
60 pcr_k0 = 0;
61 pcr_k1 = 0;
62 count = 0;
63 compare = 0;
64 cycle = 0;
65 set_fsr(0);
66
67 // vector stuff
68 vecbanks = 0xff;
69 vecbanks_count = 8;
70 utidx = -1;
71 vlmax = 32;
72 vl = 0;
73 nxfpr_bank = 256;
74 nxpr_use = 32;
75 nfpr_use = 32;
76 }
77
78 void processor_t::set_fsr(uint32_t val)
79 {
80 fsr = val & ~FSR_ZERO; // clear FSR bits that read as zero
81 }
82
83 void processor_t::vcfg()
84 {
85 if (nxpr_use + nfpr_use < 2)
86 vlmax = nxfpr_bank * vecbanks_count;
87 else
88 vlmax = (nxfpr_bank / (nxpr_use + nfpr_use - 1)) * vecbanks_count;
89
90 vlmax = std::min(vlmax, MAX_UTS);
91 }
92
93 void processor_t::setvl(int vlapp)
94 {
95 vl = std::min(vlmax, vlapp);
96 }
97
98 void processor_t::take_interrupt()
99 {
100 uint32_t interrupts = (sr & SR_IP) >> SR_IP_SHIFT;
101 interrupts &= (sr & SR_IM) >> SR_IM_SHIFT;
102
103 if(interrupts && (sr & SR_ET))
104 for(int i = 0; ; i++, interrupts >>= 1)
105 if(interrupts & 1)
106 throw interrupt_t(i);
107 }
108
109 void processor_t::step(size_t n, bool noisy)
110 {
111 if(!run)
112 return;
113
114 size_t i = 0;
115 try
116 {
117 take_interrupt();
118
119 mmu_t& _mmu = mmu;
120 reg_t npc = pc;
121
122 // execute_insn fetches and executes one instruction
123 #define execute_insn(noisy) \
124 do { \
125 mmu_t::insn_fetch_t fetch = _mmu.load_insn(npc, sr & SR_EC); \
126 if(noisy) disasm(fetch.insn, npc); \
127 npc = fetch.func(this, fetch.insn, npc); \
128 pc = npc; \
129 } while(0)
130
131 if(noisy) for( ; i < n; i++) // print out instructions as we go
132 execute_insn(true);
133 else
134 {
135 // unrolled for speed
136 for( ; n > 3 && i < n-3; i+=4)
137 {
138 execute_insn(false);
139 execute_insn(false);
140 execute_insn(false);
141 execute_insn(false);
142 }
143 for( ; i < n; i++)
144 execute_insn(false);
145 }
146 }
147 catch(trap_t t)
148 {
149 // an exception occurred in the target processor
150 take_trap(t,noisy);
151 }
152 catch(interrupt_t t)
153 {
154 take_trap((1ULL << (8*sizeof(reg_t)-1)) + t.i, noisy);
155 }
156 catch(vt_command_t cmd)
157 {
158 // this microthread has finished
159 assert(cmd == vt_command_stop);
160 }
161
162 cycle += i;
163
164 // update timer and possibly register a timer interrupt
165 uint32_t old_count = count;
166 count += i;
167 if(old_count < compare && uint64_t(old_count) + i >= compare)
168 set_interrupt(IRQ_TIMER, true);
169 }
170
171 void processor_t::take_trap(reg_t t, bool noisy)
172 {
173 if(noisy)
174 {
175 if ((sreg_t)t < 0)
176 printf("core %3d: interrupt %lld, pc 0x%016llx\n",
177 id, (long long)(t << 1 >> 1), (unsigned long long)pc);
178 else
179 printf("core %3d: trap %s, pc 0x%016llx\n",
180 id, trap_name(trap_t(t)), (unsigned long long)pc);
181 }
182
183 // switch to supervisor, set previous supervisor bit, disable traps
184 set_pcr(PCR_SR, (((sr & ~SR_ET) | SR_S) & ~SR_PS) | ((sr & SR_S) ? SR_PS : 0));
185 cause = t;
186 epc = pc;
187 pc = evec;
188 badvaddr = mmu.get_badvaddr();
189 }
190
191 void processor_t::deliver_ipi()
192 {
193 if (run)
194 set_pcr(PCR_CLR_IPI, 1);
195 }
196
197 void processor_t::disasm(insn_t insn, reg_t pc)
198 {
199 // the disassembler is stateless, so we share it
200 static disassembler disasm;
201 printf("core %3d: 0x%016llx (0x%08x) %s\n", id, (unsigned long long)pc,
202 insn.bits, disasm.disassemble(insn).c_str());
203 }
204
205 void processor_t::set_pcr(int which, reg_t val)
206 {
207 switch (which)
208 {
209 case PCR_SR:
210 sr = val & ~SR_ZERO; // clear SR bits that read as zero
211 #ifndef RISCV_ENABLE_64BIT
212 sr &= ~(SR_S64 | SR_U64);
213 #endif
214 #ifndef RISCV_ENABLE_FPU
215 sr &= ~SR_EF;
216 #endif
217 #ifndef RISCV_ENABLE_RVC
218 sr &= ~SR_EC;
219 #endif
220 #ifndef RISCV_ENABLE_VEC
221 sr &= ~SR_EV;
222 #endif
223 // update MMU state and flush TLB
224 mmu.set_sr(sr);
225 mmu.flush_tlb();
226 // set the fixed-point register length
227 xprlen = ((sr & SR_S) ? (sr & SR_S64) : (sr & SR_U64)) ? 64 : 32;
228 break;
229 case PCR_EPC:
230 epc = val;
231 break;
232 case PCR_EVEC:
233 evec = val;
234 break;
235 case PCR_COUNT:
236 count = val;
237 break;
238 case PCR_COMPARE:
239 set_interrupt(IRQ_TIMER, false);
240 compare = val;
241 break;
242 case PCR_PTBR:
243 mmu.set_ptbr(val);
244 break;
245 case PCR_SEND_IPI:
246 sim.send_ipi(val);
247 break;
248 case PCR_CLR_IPI:
249 set_interrupt(IRQ_IPI, val & 1);
250 break;
251 case PCR_K0:
252 pcr_k0 = val;
253 break;
254 case PCR_K1:
255 pcr_k1 = val;
256 break;
257 case PCR_VECBANK:
258 vecbanks = val & 0xff;
259 vecbanks_count = __builtin_popcountll(vecbanks);
260 break;
261 case PCR_TOHOST:
262 if (tohost == 0)
263 tohost = val;
264 break;
265 case PCR_FROMHOST:
266 set_interrupt(IRQ_HOST, val != 0);
267 fromhost = val;
268 break;
269 }
270 }
271
272 reg_t processor_t::get_pcr(int which)
273 {
274 switch (which)
275 {
276 case PCR_SR:
277 return sr;
278 case PCR_EPC:
279 return epc;
280 case PCR_BADVADDR:
281 return badvaddr;
282 case PCR_EVEC:
283 return evec;
284 case PCR_COUNT:
285 return count;
286 case PCR_COMPARE:
287 return compare;
288 case PCR_CAUSE:
289 return cause;
290 case PCR_PTBR:
291 return mmu.get_ptbr();
292 case PCR_COREID:
293 return id;
294 case PCR_IMPL:
295 return 1;
296 case PCR_K0:
297 return pcr_k0;
298 case PCR_K1:
299 return pcr_k1;
300 case PCR_VECBANK:
301 return vecbanks;
302 case PCR_TOHOST:
303 return tohost;
304 case PCR_FROMHOST:
305 return fromhost;
306 }
307 return -1;
308 }
309
310 void processor_t::set_interrupt(int which, bool on)
311 {
312 uint32_t mask = (1 << (which + SR_IP_SHIFT)) & SR_IP;
313 if (on)
314 sr |= mask;
315 else
316 sr &= ~mask;
317 }