]>
Commit | Line | Data |
---|---|---|
0c3e702a | 1 | /* |
df354dd4 | 2 | * RISC-V CPU helpers for qemu. |
0c3e702a MC |
3 | * |
4 | * Copyright (c) 2016-2017 Sagar Karandikar, [email protected] | |
5 | * Copyright (c) 2017-2018 SiFive, Inc. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms and conditions of the GNU General Public License, | |
9 | * version 2 or later, as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | * more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along with | |
17 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "qemu/osdep.h" | |
21 | #include "qemu/log.h" | |
7ec5d303 | 22 | #include "qemu/main-loop.h" |
0c3e702a MC |
23 | #include "cpu.h" |
24 | #include "exec/exec-all.h" | |
dcb32f1d | 25 | #include "tcg/tcg-op.h" |
929f0a7f | 26 | #include "trace.h" |
0c3e702a MC |
27 | |
28 | int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch) | |
29 | { | |
30 | #ifdef CONFIG_USER_ONLY | |
31 | return 0; | |
32 | #else | |
33 | return env->priv; | |
34 | #endif | |
35 | } | |
36 | ||
37 | #ifndef CONFIG_USER_ONLY | |
efbdbc26 | 38 | static int riscv_cpu_local_irq_pending(CPURISCVState *env) |
0c3e702a | 39 | { |
efbdbc26 MC |
40 | target_ulong mstatus_mie = get_field(env->mstatus, MSTATUS_MIE); |
41 | target_ulong mstatus_sie = get_field(env->mstatus, MSTATUS_SIE); | |
7ec5d303 | 42 | target_ulong pending = env->mip & env->mie; |
efbdbc26 MC |
43 | target_ulong mie = env->priv < PRV_M || (env->priv == PRV_M && mstatus_mie); |
44 | target_ulong sie = env->priv < PRV_S || (env->priv == PRV_S && mstatus_sie); | |
45 | target_ulong irqs = (pending & ~env->mideleg & -mie) | | |
46 | (pending & env->mideleg & -sie); | |
0c3e702a | 47 | |
efbdbc26 MC |
48 | if (irqs) { |
49 | return ctz64(irqs); /* since non-zero */ | |
0c3e702a MC |
50 | } else { |
51 | return EXCP_NONE; /* indicates no pending interrupt */ | |
52 | } | |
53 | } | |
54 | #endif | |
55 | ||
56 | bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) | |
57 | { | |
58 | #if !defined(CONFIG_USER_ONLY) | |
59 | if (interrupt_request & CPU_INTERRUPT_HARD) { | |
60 | RISCVCPU *cpu = RISCV_CPU(cs); | |
61 | CPURISCVState *env = &cpu->env; | |
efbdbc26 | 62 | int interruptno = riscv_cpu_local_irq_pending(env); |
0c3e702a MC |
63 | if (interruptno >= 0) { |
64 | cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno; | |
65 | riscv_cpu_do_interrupt(cs); | |
66 | return true; | |
67 | } | |
68 | } | |
69 | #endif | |
70 | return false; | |
71 | } | |
72 | ||
73 | #if !defined(CONFIG_USER_ONLY) | |
74 | ||
b345b480 AF |
75 | /* Return true is floating point support is currently enabled */ |
76 | bool riscv_cpu_fp_enabled(CPURISCVState *env) | |
77 | { | |
78 | if (env->mstatus & MSTATUS_FS) { | |
79 | return true; | |
80 | } | |
81 | ||
82 | return false; | |
83 | } | |
84 | ||
66e594f2 AF |
85 | void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) |
86 | { | |
87 | target_ulong mstatus_mask = MSTATUS_MXR | MSTATUS_SUM | MSTATUS_FS | | |
88 | MSTATUS_SPP | MSTATUS_SPIE | MSTATUS_SIE; | |
89 | bool current_virt = riscv_cpu_virt_enabled(env); | |
90 | ||
91 | g_assert(riscv_has_ext(env, RVH)); | |
92 | ||
93 | #if defined(TARGET_RISCV64) | |
94 | mstatus_mask |= MSTATUS64_UXL; | |
95 | #endif | |
96 | ||
97 | if (current_virt) { | |
98 | /* Current V=1 and we are about to change to V=0 */ | |
99 | env->vsstatus = env->mstatus & mstatus_mask; | |
100 | env->mstatus &= ~mstatus_mask; | |
101 | env->mstatus |= env->mstatus_hs; | |
102 | ||
103 | env->vstvec = env->stvec; | |
104 | env->stvec = env->stvec_hs; | |
105 | ||
106 | env->vsscratch = env->sscratch; | |
107 | env->sscratch = env->sscratch_hs; | |
108 | ||
109 | env->vsepc = env->sepc; | |
110 | env->sepc = env->sepc_hs; | |
111 | ||
112 | env->vscause = env->scause; | |
113 | env->scause = env->scause_hs; | |
114 | ||
115 | env->vstval = env->sbadaddr; | |
116 | env->sbadaddr = env->stval_hs; | |
117 | ||
118 | env->vsatp = env->satp; | |
119 | env->satp = env->satp_hs; | |
120 | } else { | |
121 | /* Current V=0 and we are about to change to V=1 */ | |
122 | env->mstatus_hs = env->mstatus & mstatus_mask; | |
123 | env->mstatus &= ~mstatus_mask; | |
124 | env->mstatus |= env->vsstatus; | |
125 | ||
126 | env->stvec_hs = env->stvec; | |
127 | env->stvec = env->vstvec; | |
128 | ||
129 | env->sscratch_hs = env->sscratch; | |
130 | env->sscratch = env->vsscratch; | |
131 | ||
132 | env->sepc_hs = env->sepc; | |
133 | env->sepc = env->vsepc; | |
134 | ||
135 | env->scause_hs = env->scause; | |
136 | env->scause = env->vscause; | |
137 | ||
138 | env->stval_hs = env->sbadaddr; | |
139 | env->sbadaddr = env->vstval; | |
140 | ||
141 | env->satp_hs = env->satp; | |
142 | env->satp = env->vsatp; | |
143 | } | |
144 | } | |
145 | ||
ef6bb7b6 AF |
146 | bool riscv_cpu_virt_enabled(CPURISCVState *env) |
147 | { | |
148 | if (!riscv_has_ext(env, RVH)) { | |
149 | return false; | |
150 | } | |
151 | ||
152 | return get_field(env->virt, VIRT_ONOFF); | |
153 | } | |
154 | ||
155 | void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable) | |
156 | { | |
157 | if (!riscv_has_ext(env, RVH)) { | |
158 | return; | |
159 | } | |
160 | ||
161 | env->virt = set_field(env->virt, VIRT_ONOFF, enable); | |
162 | } | |
163 | ||
c7b1bbc8 AF |
164 | bool riscv_cpu_force_hs_excep_enabled(CPURISCVState *env) |
165 | { | |
166 | if (!riscv_has_ext(env, RVH)) { | |
167 | return false; | |
168 | } | |
169 | ||
170 | return get_field(env->virt, FORCE_HS_EXCEP); | |
171 | } | |
172 | ||
173 | void riscv_cpu_set_force_hs_excep(CPURISCVState *env, bool enable) | |
174 | { | |
175 | if (!riscv_has_ext(env, RVH)) { | |
176 | return; | |
177 | } | |
178 | ||
179 | env->virt = set_field(env->virt, FORCE_HS_EXCEP, enable); | |
180 | } | |
181 | ||
e3e7039c MC |
182 | int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts) |
183 | { | |
184 | CPURISCVState *env = &cpu->env; | |
185 | if (env->miclaim & interrupts) { | |
186 | return -1; | |
187 | } else { | |
188 | env->miclaim |= interrupts; | |
189 | return 0; | |
190 | } | |
191 | } | |
192 | ||
df354dd4 MC |
193 | uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) |
194 | { | |
195 | CPURISCVState *env = &cpu->env; | |
0a01f2ee | 196 | CPUState *cs = CPU(cpu); |
7ec5d303 AF |
197 | uint32_t old = env->mip; |
198 | bool locked = false; | |
199 | ||
200 | if (!qemu_mutex_iothread_locked()) { | |
201 | locked = true; | |
202 | qemu_mutex_lock_iothread(); | |
203 | } | |
df354dd4 | 204 | |
7ec5d303 | 205 | env->mip = (env->mip & ~mask) | (value & mask); |
df354dd4 | 206 | |
7ec5d303 AF |
207 | if (env->mip) { |
208 | cpu_interrupt(cs, CPU_INTERRUPT_HARD); | |
209 | } else { | |
210 | cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); | |
211 | } | |
0a01f2ee | 212 | |
7ec5d303 AF |
213 | if (locked) { |
214 | qemu_mutex_unlock_iothread(); | |
215 | } | |
df354dd4 MC |
216 | |
217 | return old; | |
218 | } | |
219 | ||
fb738839 | 220 | void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv) |
df354dd4 MC |
221 | { |
222 | if (newpriv > PRV_M) { | |
223 | g_assert_not_reached(); | |
224 | } | |
225 | if (newpriv == PRV_H) { | |
226 | newpriv = PRV_U; | |
227 | } | |
228 | /* tlb_flush is unnecessary as mode is contained in mmu_idx */ | |
229 | env->priv = newpriv; | |
c13b169f JS |
230 | |
231 | /* | |
232 | * Clear the load reservation - otherwise a reservation placed in one | |
233 | * context/process can be used by another, resulting in an SC succeeding | |
234 | * incorrectly. Version 2.2 of the ISA specification explicitly requires | |
235 | * this behaviour, while later revisions say that the kernel "should" use | |
236 | * an SC instruction to force the yielding of a load reservation on a | |
237 | * preemptive context switch. As a result, do both. | |
238 | */ | |
239 | env->load_res = -1; | |
df354dd4 MC |
240 | } |
241 | ||
0c3e702a MC |
242 | /* get_physical_address - get the physical address for this virtual address |
243 | * | |
244 | * Do a page table walk to obtain the physical address corresponding to a | |
245 | * virtual address. Returns 0 if the translation was successful | |
246 | * | |
247 | * Adapted from Spike's mmu_t::translate and mmu_t::walk | |
248 | * | |
249 | */ | |
250 | static int get_physical_address(CPURISCVState *env, hwaddr *physical, | |
251 | int *prot, target_ulong addr, | |
252 | int access_type, int mmu_idx) | |
253 | { | |
254 | /* NOTE: the env->pc value visible here will not be | |
255 | * correct, but the value visible to the exception handler | |
256 | * (riscv_cpu_do_interrupt) is correct */ | |
aacb578f PD |
257 | MemTxResult res; |
258 | MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; | |
0c3e702a MC |
259 | int mode = mmu_idx; |
260 | ||
261 | if (mode == PRV_M && access_type != MMU_INST_FETCH) { | |
262 | if (get_field(env->mstatus, MSTATUS_MPRV)) { | |
263 | mode = get_field(env->mstatus, MSTATUS_MPP); | |
264 | } | |
265 | } | |
266 | ||
267 | if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) { | |
268 | *physical = addr; | |
269 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
270 | return TRANSLATE_SUCCESS; | |
271 | } | |
272 | ||
273 | *prot = 0; | |
274 | ||
ddf78132 | 275 | hwaddr base; |
0c3e702a MC |
276 | int levels, ptidxbits, ptesize, vm, sum; |
277 | int mxr = get_field(env->mstatus, MSTATUS_MXR); | |
278 | ||
279 | if (env->priv_ver >= PRIV_VERSION_1_10_0) { | |
ddf78132 | 280 | base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT; |
0c3e702a MC |
281 | sum = get_field(env->mstatus, MSTATUS_SUM); |
282 | vm = get_field(env->satp, SATP_MODE); | |
283 | switch (vm) { | |
284 | case VM_1_10_SV32: | |
285 | levels = 2; ptidxbits = 10; ptesize = 4; break; | |
286 | case VM_1_10_SV39: | |
287 | levels = 3; ptidxbits = 9; ptesize = 8; break; | |
288 | case VM_1_10_SV48: | |
289 | levels = 4; ptidxbits = 9; ptesize = 8; break; | |
290 | case VM_1_10_SV57: | |
291 | levels = 5; ptidxbits = 9; ptesize = 8; break; | |
292 | case VM_1_10_MBARE: | |
293 | *physical = addr; | |
294 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
295 | return TRANSLATE_SUCCESS; | |
296 | default: | |
297 | g_assert_not_reached(); | |
298 | } | |
299 | } else { | |
ddf78132 | 300 | base = (hwaddr)(env->sptbr) << PGSHIFT; |
0c3e702a MC |
301 | sum = !get_field(env->mstatus, MSTATUS_PUM); |
302 | vm = get_field(env->mstatus, MSTATUS_VM); | |
303 | switch (vm) { | |
304 | case VM_1_09_SV32: | |
305 | levels = 2; ptidxbits = 10; ptesize = 4; break; | |
306 | case VM_1_09_SV39: | |
307 | levels = 3; ptidxbits = 9; ptesize = 8; break; | |
308 | case VM_1_09_SV48: | |
309 | levels = 4; ptidxbits = 9; ptesize = 8; break; | |
310 | case VM_1_09_MBARE: | |
311 | *physical = addr; | |
312 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
313 | return TRANSLATE_SUCCESS; | |
314 | default: | |
315 | g_assert_not_reached(); | |
316 | } | |
317 | } | |
318 | ||
3109cd98 | 319 | CPUState *cs = env_cpu(env); |
0c3e702a MC |
320 | int va_bits = PGSHIFT + levels * ptidxbits; |
321 | target_ulong mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1; | |
322 | target_ulong masked_msbs = (addr >> (va_bits - 1)) & mask; | |
323 | if (masked_msbs != 0 && masked_msbs != mask) { | |
324 | return TRANSLATE_FAIL; | |
325 | } | |
326 | ||
327 | int ptshift = (levels - 1) * ptidxbits; | |
328 | int i; | |
329 | ||
330 | #if !TCG_OVERSIZED_GUEST | |
331 | restart: | |
332 | #endif | |
333 | for (i = 0; i < levels; i++, ptshift -= ptidxbits) { | |
334 | target_ulong idx = (addr >> (PGSHIFT + ptshift)) & | |
335 | ((1 << ptidxbits) - 1); | |
336 | ||
337 | /* check that physical address of PTE is legal */ | |
ddf78132 | 338 | hwaddr pte_addr = base + idx * ptesize; |
1f447aec HA |
339 | |
340 | if (riscv_feature(env, RISCV_FEATURE_PMP) && | |
341 | !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong), | |
342 | 1 << MMU_DATA_LOAD, PRV_S)) { | |
343 | return TRANSLATE_PMP_FAIL; | |
344 | } | |
aacb578f | 345 | |
0c3e702a | 346 | #if defined(TARGET_RISCV32) |
aacb578f | 347 | target_ulong pte = address_space_ldl(cs->as, pte_addr, attrs, &res); |
0c3e702a | 348 | #elif defined(TARGET_RISCV64) |
aacb578f | 349 | target_ulong pte = address_space_ldq(cs->as, pte_addr, attrs, &res); |
0c3e702a | 350 | #endif |
aacb578f PD |
351 | if (res != MEMTX_OK) { |
352 | return TRANSLATE_FAIL; | |
353 | } | |
354 | ||
ddf78132 | 355 | hwaddr ppn = pte >> PTE_PPN_SHIFT; |
0c3e702a | 356 | |
c3b03e58 MC |
357 | if (!(pte & PTE_V)) { |
358 | /* Invalid PTE */ | |
359 | return TRANSLATE_FAIL; | |
360 | } else if (!(pte & (PTE_R | PTE_W | PTE_X))) { | |
361 | /* Inner PTE, continue walking */ | |
0c3e702a | 362 | base = ppn << PGSHIFT; |
c3b03e58 MC |
363 | } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) { |
364 | /* Reserved leaf PTE flags: PTE_W */ | |
365 | return TRANSLATE_FAIL; | |
366 | } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) { | |
367 | /* Reserved leaf PTE flags: PTE_W + PTE_X */ | |
368 | return TRANSLATE_FAIL; | |
369 | } else if ((pte & PTE_U) && ((mode != PRV_U) && | |
370 | (!sum || access_type == MMU_INST_FETCH))) { | |
371 | /* User PTE flags when not U mode and mstatus.SUM is not set, | |
372 | or the access type is an instruction fetch */ | |
373 | return TRANSLATE_FAIL; | |
374 | } else if (!(pte & PTE_U) && (mode != PRV_S)) { | |
375 | /* Supervisor PTE flags when not S mode */ | |
376 | return TRANSLATE_FAIL; | |
377 | } else if (ppn & ((1ULL << ptshift) - 1)) { | |
378 | /* Misaligned PPN */ | |
379 | return TRANSLATE_FAIL; | |
380 | } else if (access_type == MMU_DATA_LOAD && !((pte & PTE_R) || | |
381 | ((pte & PTE_X) && mxr))) { | |
382 | /* Read access check failed */ | |
383 | return TRANSLATE_FAIL; | |
384 | } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) { | |
385 | /* Write access check failed */ | |
386 | return TRANSLATE_FAIL; | |
387 | } else if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) { | |
388 | /* Fetch access check failed */ | |
389 | return TRANSLATE_FAIL; | |
0c3e702a MC |
390 | } else { |
391 | /* if necessary, set accessed and dirty bits. */ | |
392 | target_ulong updated_pte = pte | PTE_A | | |
393 | (access_type == MMU_DATA_STORE ? PTE_D : 0); | |
394 | ||
395 | /* Page table updates need to be atomic with MTTCG enabled */ | |
396 | if (updated_pte != pte) { | |
c3b03e58 MC |
397 | /* |
398 | * - if accessed or dirty bits need updating, and the PTE is | |
399 | * in RAM, then we do so atomically with a compare and swap. | |
400 | * - if the PTE is in IO space or ROM, then it can't be updated | |
401 | * and we return TRANSLATE_FAIL. | |
402 | * - if the PTE changed by the time we went to update it, then | |
403 | * it is no longer valid and we must re-walk the page table. | |
404 | */ | |
0c3e702a MC |
405 | MemoryRegion *mr; |
406 | hwaddr l = sizeof(target_ulong), addr1; | |
407 | mr = address_space_translate(cs->as, pte_addr, | |
bc6b1cec | 408 | &addr1, &l, false, MEMTXATTRS_UNSPECIFIED); |
c3b03e58 | 409 | if (memory_region_is_ram(mr)) { |
0c3e702a MC |
410 | target_ulong *pte_pa = |
411 | qemu_map_ram_ptr(mr->ram_block, addr1); | |
412 | #if TCG_OVERSIZED_GUEST | |
413 | /* MTTCG is not enabled on oversized TCG guests so | |
414 | * page table updates do not need to be atomic */ | |
415 | *pte_pa = pte = updated_pte; | |
416 | #else | |
417 | target_ulong old_pte = | |
418 | atomic_cmpxchg(pte_pa, pte, updated_pte); | |
419 | if (old_pte != pte) { | |
420 | goto restart; | |
421 | } else { | |
422 | pte = updated_pte; | |
423 | } | |
424 | #endif | |
425 | } else { | |
426 | /* misconfigured PTE in ROM (AD bits are not preset) or | |
427 | * PTE is in IO space and can't be updated atomically */ | |
428 | return TRANSLATE_FAIL; | |
429 | } | |
430 | } | |
431 | ||
432 | /* for superpage mappings, make a fake leaf PTE for the TLB's | |
433 | benefit. */ | |
434 | target_ulong vpn = addr >> PGSHIFT; | |
435 | *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT; | |
436 | ||
c3b03e58 MC |
437 | /* set permissions on the TLB entry */ |
438 | if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) { | |
0c3e702a MC |
439 | *prot |= PAGE_READ; |
440 | } | |
441 | if ((pte & PTE_X)) { | |
442 | *prot |= PAGE_EXEC; | |
443 | } | |
c3b03e58 MC |
444 | /* add write permission on stores or if the page is already dirty, |
445 | so that we TLB miss on later writes to update the dirty bit */ | |
0c3e702a MC |
446 | if ((pte & PTE_W) && |
447 | (access_type == MMU_DATA_STORE || (pte & PTE_D))) { | |
448 | *prot |= PAGE_WRITE; | |
449 | } | |
450 | return TRANSLATE_SUCCESS; | |
451 | } | |
452 | } | |
453 | return TRANSLATE_FAIL; | |
454 | } | |
455 | ||
456 | static void raise_mmu_exception(CPURISCVState *env, target_ulong address, | |
635b0b0e | 457 | MMUAccessType access_type, bool pmp_violation) |
0c3e702a | 458 | { |
3109cd98 | 459 | CPUState *cs = env_cpu(env); |
0c3e702a MC |
460 | int page_fault_exceptions = |
461 | (env->priv_ver >= PRIV_VERSION_1_10_0) && | |
635b0b0e HA |
462 | get_field(env->satp, SATP_MODE) != VM_1_10_MBARE && |
463 | !pmp_violation; | |
0c3e702a MC |
464 | switch (access_type) { |
465 | case MMU_INST_FETCH: | |
466 | cs->exception_index = page_fault_exceptions ? | |
467 | RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT; | |
468 | break; | |
469 | case MMU_DATA_LOAD: | |
470 | cs->exception_index = page_fault_exceptions ? | |
471 | RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT; | |
472 | break; | |
473 | case MMU_DATA_STORE: | |
474 | cs->exception_index = page_fault_exceptions ? | |
475 | RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT; | |
476 | break; | |
477 | default: | |
478 | g_assert_not_reached(); | |
479 | } | |
480 | env->badaddr = address; | |
481 | } | |
482 | ||
483 | hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) | |
484 | { | |
485 | RISCVCPU *cpu = RISCV_CPU(cs); | |
486 | hwaddr phys_addr; | |
487 | int prot; | |
488 | int mmu_idx = cpu_mmu_index(&cpu->env, false); | |
489 | ||
490 | if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0, mmu_idx)) { | |
491 | return -1; | |
492 | } | |
493 | return phys_addr; | |
494 | } | |
495 | ||
37207e12 PD |
496 | void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, |
497 | vaddr addr, unsigned size, | |
498 | MMUAccessType access_type, | |
499 | int mmu_idx, MemTxAttrs attrs, | |
500 | MemTxResult response, uintptr_t retaddr) | |
cbf58276 MC |
501 | { |
502 | RISCVCPU *cpu = RISCV_CPU(cs); | |
503 | CPURISCVState *env = &cpu->env; | |
504 | ||
37207e12 | 505 | if (access_type == MMU_DATA_STORE) { |
cbf58276 MC |
506 | cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT; |
507 | } else { | |
508 | cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT; | |
509 | } | |
510 | ||
511 | env->badaddr = addr; | |
37207e12 | 512 | riscv_raise_exception(&cpu->env, cs->exception_index, retaddr); |
cbf58276 MC |
513 | } |
514 | ||
0c3e702a MC |
515 | void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, |
516 | MMUAccessType access_type, int mmu_idx, | |
517 | uintptr_t retaddr) | |
518 | { | |
519 | RISCVCPU *cpu = RISCV_CPU(cs); | |
520 | CPURISCVState *env = &cpu->env; | |
521 | switch (access_type) { | |
522 | case MMU_INST_FETCH: | |
523 | cs->exception_index = RISCV_EXCP_INST_ADDR_MIS; | |
524 | break; | |
525 | case MMU_DATA_LOAD: | |
526 | cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS; | |
527 | break; | |
528 | case MMU_DATA_STORE: | |
529 | cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS; | |
530 | break; | |
531 | default: | |
532 | g_assert_not_reached(); | |
533 | } | |
534 | env->badaddr = addr; | |
fb738839 | 535 | riscv_raise_exception(env, cs->exception_index, retaddr); |
0c3e702a | 536 | } |
0c3e702a MC |
537 | #endif |
538 | ||
8a4ca3c1 RH |
539 | bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, |
540 | MMUAccessType access_type, int mmu_idx, | |
541 | bool probe, uintptr_t retaddr) | |
0c3e702a MC |
542 | { |
543 | RISCVCPU *cpu = RISCV_CPU(cs); | |
544 | CPURISCVState *env = &cpu->env; | |
2921343b | 545 | #ifndef CONFIG_USER_ONLY |
0c3e702a MC |
546 | hwaddr pa = 0; |
547 | int prot; | |
635b0b0e | 548 | bool pmp_violation = false; |
0c3e702a | 549 | int ret = TRANSLATE_FAIL; |
cc0fdb29 | 550 | int mode = mmu_idx; |
0c3e702a | 551 | |
8a4ca3c1 RH |
552 | qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n", |
553 | __func__, address, access_type, mmu_idx); | |
554 | ||
555 | ret = get_physical_address(env, &pa, &prot, address, access_type, mmu_idx); | |
0c3e702a | 556 | |
cc0fdb29 HA |
557 | if (mode == PRV_M && access_type != MMU_INST_FETCH) { |
558 | if (get_field(env->mstatus, MSTATUS_MPRV)) { | |
559 | mode = get_field(env->mstatus, MSTATUS_MPP); | |
560 | } | |
561 | } | |
562 | ||
0c3e702a | 563 | qemu_log_mask(CPU_LOG_MMU, |
8a4ca3c1 RH |
564 | "%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx |
565 | " prot %d\n", __func__, address, ret, pa, prot); | |
566 | ||
a88365c1 | 567 | if (riscv_feature(env, RISCV_FEATURE_PMP) && |
e0f8fa72 | 568 | (ret == TRANSLATE_SUCCESS) && |
db21e6f7 | 569 | !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) { |
1f447aec HA |
570 | ret = TRANSLATE_PMP_FAIL; |
571 | } | |
572 | if (ret == TRANSLATE_PMP_FAIL) { | |
635b0b0e | 573 | pmp_violation = true; |
0c3e702a MC |
574 | } |
575 | if (ret == TRANSLATE_SUCCESS) { | |
576 | tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK, | |
577 | prot, mmu_idx, TARGET_PAGE_SIZE); | |
8a4ca3c1 RH |
578 | return true; |
579 | } else if (probe) { | |
580 | return false; | |
581 | } else { | |
635b0b0e | 582 | raise_mmu_exception(env, address, access_type, pmp_violation); |
8a4ca3c1 | 583 | riscv_raise_exception(env, cs->exception_index, retaddr); |
0c3e702a MC |
584 | } |
585 | #else | |
8a4ca3c1 | 586 | switch (access_type) { |
0c3e702a MC |
587 | case MMU_INST_FETCH: |
588 | cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT; | |
589 | break; | |
590 | case MMU_DATA_LOAD: | |
591 | cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT; | |
592 | break; | |
593 | case MMU_DATA_STORE: | |
594 | cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT; | |
595 | break; | |
2921343b GM |
596 | default: |
597 | g_assert_not_reached(); | |
0c3e702a | 598 | } |
2921343b | 599 | env->badaddr = address; |
8a4ca3c1 | 600 | cpu_loop_exit_restore(cs, retaddr); |
0c3e702a | 601 | #endif |
0c3e702a MC |
602 | } |
603 | ||
604 | /* | |
605 | * Handle Traps | |
606 | * | |
607 | * Adapted from Spike's processor_t::take_trap. | |
608 | * | |
609 | */ | |
610 | void riscv_cpu_do_interrupt(CPUState *cs) | |
611 | { | |
612 | #if !defined(CONFIG_USER_ONLY) | |
613 | ||
614 | RISCVCPU *cpu = RISCV_CPU(cs); | |
615 | CPURISCVState *env = &cpu->env; | |
616 | ||
acbbb94e MC |
617 | /* cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide |
618 | * so we mask off the MSB and separate into trap type and cause. | |
619 | */ | |
620 | bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG); | |
621 | target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK; | |
622 | target_ulong deleg = async ? env->mideleg : env->medeleg; | |
623 | target_ulong tval = 0; | |
624 | ||
625 | static const int ecall_cause_map[] = { | |
626 | [PRV_U] = RISCV_EXCP_U_ECALL, | |
627 | [PRV_S] = RISCV_EXCP_S_ECALL, | |
ab67a1d0 | 628 | [PRV_H] = RISCV_EXCP_VS_ECALL, |
acbbb94e MC |
629 | [PRV_M] = RISCV_EXCP_M_ECALL |
630 | }; | |
631 | ||
632 | if (!async) { | |
633 | /* set tval to badaddr for traps with address information */ | |
634 | switch (cause) { | |
ab67a1d0 AF |
635 | case RISCV_EXCP_INST_GUEST_PAGE_FAULT: |
636 | case RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT: | |
637 | case RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT: | |
acbbb94e MC |
638 | case RISCV_EXCP_INST_ADDR_MIS: |
639 | case RISCV_EXCP_INST_ACCESS_FAULT: | |
640 | case RISCV_EXCP_LOAD_ADDR_MIS: | |
641 | case RISCV_EXCP_STORE_AMO_ADDR_MIS: | |
642 | case RISCV_EXCP_LOAD_ACCESS_FAULT: | |
643 | case RISCV_EXCP_STORE_AMO_ACCESS_FAULT: | |
644 | case RISCV_EXCP_INST_PAGE_FAULT: | |
645 | case RISCV_EXCP_LOAD_PAGE_FAULT: | |
646 | case RISCV_EXCP_STORE_PAGE_FAULT: | |
647 | tval = env->badaddr; | |
648 | break; | |
649 | default: | |
650 | break; | |
0c3e702a | 651 | } |
acbbb94e MC |
652 | /* ecall is dispatched as one cause so translate based on mode */ |
653 | if (cause == RISCV_EXCP_U_ECALL) { | |
654 | assert(env->priv <= 3); | |
655 | cause = ecall_cause_map[env->priv]; | |
0c3e702a MC |
656 | } |
657 | } | |
658 | ||
ab67a1d0 | 659 | trace_riscv_trap(env->mhartid, async, cause, env->pc, tval, cause < 23 ? |
929f0a7f | 660 | (async ? riscv_intr_names : riscv_excp_names)[cause] : "(unknown)"); |
0c3e702a | 661 | |
acbbb94e MC |
662 | if (env->priv <= PRV_S && |
663 | cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) { | |
0c3e702a | 664 | /* handle the trap in S-mode */ |
0c3e702a MC |
665 | target_ulong s = env->mstatus; |
666 | s = set_field(s, MSTATUS_SPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? | |
667 | get_field(s, MSTATUS_SIE) : get_field(s, MSTATUS_UIE << env->priv)); | |
668 | s = set_field(s, MSTATUS_SPP, env->priv); | |
669 | s = set_field(s, MSTATUS_SIE, 0); | |
c7b95171 | 670 | env->mstatus = s; |
16fdb8ff | 671 | env->scause = cause | ((target_ulong)async << (TARGET_LONG_BITS - 1)); |
acbbb94e MC |
672 | env->sepc = env->pc; |
673 | env->sbadaddr = tval; | |
674 | env->pc = (env->stvec >> 2 << 2) + | |
675 | ((async && (env->stvec & 3) == 1) ? cause * 4 : 0); | |
fb738839 | 676 | riscv_cpu_set_mode(env, PRV_S); |
0c3e702a | 677 | } else { |
acbbb94e | 678 | /* handle the trap in M-mode */ |
0c3e702a MC |
679 | target_ulong s = env->mstatus; |
680 | s = set_field(s, MSTATUS_MPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? | |
681 | get_field(s, MSTATUS_MIE) : get_field(s, MSTATUS_UIE << env->priv)); | |
682 | s = set_field(s, MSTATUS_MPP, env->priv); | |
683 | s = set_field(s, MSTATUS_MIE, 0); | |
c7b95171 | 684 | env->mstatus = s; |
acbbb94e MC |
685 | env->mcause = cause | ~(((target_ulong)-1) >> async); |
686 | env->mepc = env->pc; | |
687 | env->mbadaddr = tval; | |
688 | env->pc = (env->mtvec >> 2 << 2) + | |
689 | ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); | |
fb738839 | 690 | riscv_cpu_set_mode(env, PRV_M); |
0c3e702a | 691 | } |
d9360e96 MC |
692 | |
693 | /* NOTE: it is not necessary to yield load reservations here. It is only | |
694 | * necessary for an SC from "another hart" to cause a load reservation | |
695 | * to be yielded. Refer to the memory consistency model section of the | |
696 | * RISC-V ISA Specification. | |
697 | */ | |
698 | ||
0c3e702a MC |
699 | #endif |
700 | cs->exception_index = EXCP_NONE; /* mark handled to qemu */ | |
701 | } |