new tohost/fromhost semantics
[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 | 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 = interrupts_pending;
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 while(1) try
116 {
117 take_interrupt();
118
119 mmu_t& _mmu = mmu;
120 insn_t insn;
121 insn_func_t func;
122 reg_t npc = pc;
123
124 // execute_insn fetches and executes one instruction
125 #define execute_insn(noisy) \
126 do { \
127 insn = _mmu.load_insn(npc, sr & SR_EC, &func); \
128 if(noisy) disasm(insn,pc); \
129 npc = func(this, insn, npc); \
130 pc = npc; \
131 } while(0)
132
133 if(noisy) for( ; i < n; i++) // print out instructions as we go
134 execute_insn(true);
135 else
136 {
137 // unrolled for speed
138 for( ; n > 3 && i < n-3; i+=4)
139 {
140 execute_insn(false);
141 execute_insn(false);
142 execute_insn(false);
143 execute_insn(false);
144 }
145 for( ; i < n; i++)
146 execute_insn(false);
147 }
148
149 break;
150 }
151 catch(trap_t t)
152 {
153 // an exception occurred in the target processor
154 i++;
155 take_trap(t,noisy);
156 }
157 catch(interrupt_t t)
158 {
159 i++;
160 take_trap((1ULL << (8*sizeof(reg_t)-1)) + t.i, noisy);
161 }
162 catch(vt_command_t cmd)
163 {
164 // this microthread has finished
165 i++;
166 assert(cmd == vt_command_stop);
167 break;
168 }
169
170 cycle += i;
171
172 // update timer and possibly register a timer interrupt
173 uint32_t old_count = count;
174 count += i;
175 if(old_count < compare && uint64_t(old_count) + i >= compare)
176 interrupts_pending |= 1 << IRQ_TIMER;
177 }
178
179 void processor_t::take_trap(reg_t t, bool noisy)
180 {
181 if(noisy)
182 {
183 if ((sreg_t)t < 0)
184 printf("core %3d: interrupt %lld, pc 0x%016llx\n",
185 id, (long long)(t << 1 >> 1), (unsigned long long)pc);
186 else
187 printf("core %3d: trap %s, pc 0x%016llx\n",
188 id, trap_name(trap_t(t)), (unsigned long long)pc);
189 }
190
191 // switch to supervisor, set previous supervisor bit, disable traps
192 set_pcr(PCR_SR, (((sr & ~SR_ET) | SR_S) & ~SR_PS) | ((sr & SR_S) ? SR_PS : 0));
193 cause = t;
194 epc = pc;
195 pc = evec;
196 badvaddr = mmu.get_badvaddr();
197 }
198
199 void processor_t::deliver_ipi()
200 {
201 if (run)
202 set_pcr(PCR_CLR_IPI, 1);
203 }
204
205 void processor_t::disasm(insn_t insn, reg_t pc)
206 {
207 // the disassembler is stateless, so we share it
208 static disassembler disasm;
209 printf("core %3d: 0x%016llx (0x%08x) %s\n", id, (unsigned long long)pc,
210 insn.bits, disasm.disassemble(insn).c_str());
211 }
212
213 void processor_t::set_pcr(int which, reg_t val)
214 {
215 switch (which)
216 {
217 case PCR_SR:
218 sr = val & ~SR_ZERO; // clear SR bits that read as zero
219 #ifndef RISCV_ENABLE_64BIT
220 sr &= ~(SR_S64 | SR_U64);
221 #endif
222 #ifndef RISCV_ENABLE_FPU
223 sr &= ~SR_EF;
224 #endif
225 #ifndef RISCV_ENABLE_RVC
226 sr &= ~SR_EC;
227 #endif
228 #ifndef RISCV_ENABLE_VEC
229 sr &= ~SR_EV;
230 #endif
231 // update MMU state and flush TLB
232 mmu.set_vm_enabled(sr & SR_VM);
233 mmu.set_supervisor(sr & SR_S);
234 mmu.flush_tlb();
235 // set the fixed-point register length
236 xprlen = ((sr & SR_S) ? (sr & SR_S64) : (sr & SR_U64)) ? 64 : 32;
237 break;
238 case PCR_EPC:
239 epc = val;
240 break;
241 case PCR_EVEC:
242 evec = val;
243 break;
244 case PCR_COUNT:
245 count = val;
246 break;
247 case PCR_COMPARE:
248 interrupts_pending &= ~(1 << IRQ_TIMER);
249 compare = val;
250 break;
251 case PCR_PTBR:
252 mmu.set_ptbr(val);
253 break;
254 case PCR_SEND_IPI:
255 sim.send_ipi(val);
256 break;
257 case PCR_CLR_IPI:
258 if (val & 1)
259 interrupts_pending |= (1 << IRQ_IPI);
260 else
261 interrupts_pending &= ~(1 << IRQ_IPI);
262 break;
263 case PCR_K0:
264 pcr_k0 = val;
265 break;
266 case PCR_K1:
267 pcr_k1 = val;
268 break;
269 case PCR_VECBANK:
270 vecbanks = val & 0xff;
271 vecbanks_count = __builtin_popcountll(vecbanks);
272 break;
273 case PCR_TOHOST:
274 if (tohost == 0)
275 tohost = val;
276 break;
277 case PCR_FROMHOST:
278 fromhost = val;
279 break;
280 }
281 }
282
283 reg_t processor_t::get_pcr(int which)
284 {
285 switch (which)
286 {
287 case PCR_SR:
288 return sr;
289 case PCR_EPC:
290 return epc;
291 case PCR_BADVADDR:
292 return badvaddr;
293 case PCR_EVEC:
294 return evec;
295 case PCR_COUNT:
296 return count;
297 case PCR_COMPARE:
298 return compare;
299 case PCR_CAUSE:
300 return cause;
301 case PCR_PTBR:
302 return mmu.get_ptbr();
303 case PCR_COREID:
304 return id;
305 case PCR_IMPL:
306 return 1;
307 case PCR_K0:
308 return pcr_k0;
309 case PCR_K1:
310 return pcr_k1;
311 case PCR_VECBANK:
312 return vecbanks;
313 case PCR_TOHOST:
314 return tohost;
315 case PCR_FROMHOST:
316 return fromhost;
317 }
318 return -1;
319 }