]> git.proxmox.com Git - mirror_qemu.git/blame - target/riscv/cpu_helper.c
target/riscv: Fix update of hstatus.SPVP
[mirror_qemu.git] / target / riscv / cpu_helper.c
CommitLineData
0c3e702a 1/*
df354dd4 2 * RISC-V CPU helpers for qemu.
0c3e702a
MC
3 *
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
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
28int 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 38static int riscv_cpu_local_irq_pending(CPURISCVState *env)
0c3e702a 39{
3ef10a09
AF
40 target_ulong irqs;
41
efbdbc26
MC
42 target_ulong mstatus_mie = get_field(env->mstatus, MSTATUS_MIE);
43 target_ulong mstatus_sie = get_field(env->mstatus, MSTATUS_SIE);
3ef10a09
AF
44 target_ulong hs_mstatus_sie = get_field(env->mstatus_hs, MSTATUS_SIE);
45
46 target_ulong pending = env->mip & env->mie &
47 ~(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP);
48 target_ulong vspending = (env->mip & env->mie &
c5969a3a 49 (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP));
3ef10a09
AF
50
51 target_ulong mie = env->priv < PRV_M ||
52 (env->priv == PRV_M && mstatus_mie);
53 target_ulong sie = env->priv < PRV_S ||
54 (env->priv == PRV_S && mstatus_sie);
55 target_ulong hs_sie = env->priv < PRV_S ||
56 (env->priv == PRV_S && hs_mstatus_sie);
57
58 if (riscv_cpu_virt_enabled(env)) {
59 target_ulong pending_hs_irq = pending & -hs_sie;
60
61 if (pending_hs_irq) {
62 riscv_cpu_set_force_hs_excep(env, FORCE_HS_EXCEP);
63 return ctz64(pending_hs_irq);
64 }
65
66 pending = vspending;
67 }
68
69 irqs = (pending & ~env->mideleg & -mie) | (pending & env->mideleg & -sie);
0c3e702a 70
efbdbc26
MC
71 if (irqs) {
72 return ctz64(irqs); /* since non-zero */
0c3e702a
MC
73 } else {
74 return EXCP_NONE; /* indicates no pending interrupt */
75 }
76}
77#endif
78
79bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
80{
81#if !defined(CONFIG_USER_ONLY)
82 if (interrupt_request & CPU_INTERRUPT_HARD) {
83 RISCVCPU *cpu = RISCV_CPU(cs);
84 CPURISCVState *env = &cpu->env;
efbdbc26 85 int interruptno = riscv_cpu_local_irq_pending(env);
0c3e702a
MC
86 if (interruptno >= 0) {
87 cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno;
88 riscv_cpu_do_interrupt(cs);
89 return true;
90 }
91 }
92#endif
93 return false;
94}
95
96#if !defined(CONFIG_USER_ONLY)
97
b345b480
AF
98/* Return true is floating point support is currently enabled */
99bool riscv_cpu_fp_enabled(CPURISCVState *env)
100{
101 if (env->mstatus & MSTATUS_FS) {
29409c1d
AF
102 if (riscv_cpu_virt_enabled(env) && !(env->mstatus_hs & MSTATUS_FS)) {
103 return false;
104 }
b345b480
AF
105 return true;
106 }
107
108 return false;
109}
110
66e594f2
AF
111void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env)
112{
113 target_ulong mstatus_mask = MSTATUS_MXR | MSTATUS_SUM | MSTATUS_FS |
114 MSTATUS_SPP | MSTATUS_SPIE | MSTATUS_SIE;
115 bool current_virt = riscv_cpu_virt_enabled(env);
116
117 g_assert(riscv_has_ext(env, RVH));
118
119#if defined(TARGET_RISCV64)
120 mstatus_mask |= MSTATUS64_UXL;
121#endif
122
123 if (current_virt) {
124 /* Current V=1 and we are about to change to V=0 */
125 env->vsstatus = env->mstatus & mstatus_mask;
126 env->mstatus &= ~mstatus_mask;
127 env->mstatus |= env->mstatus_hs;
128
551fa7e8
AF
129#if defined(TARGET_RISCV32)
130 env->vsstatush = env->mstatush;
131 env->mstatush |= env->mstatush_hs;
132#endif
133
66e594f2
AF
134 env->vstvec = env->stvec;
135 env->stvec = env->stvec_hs;
136
137 env->vsscratch = env->sscratch;
138 env->sscratch = env->sscratch_hs;
139
140 env->vsepc = env->sepc;
141 env->sepc = env->sepc_hs;
142
143 env->vscause = env->scause;
144 env->scause = env->scause_hs;
145
146 env->vstval = env->sbadaddr;
147 env->sbadaddr = env->stval_hs;
148
149 env->vsatp = env->satp;
150 env->satp = env->satp_hs;
151 } else {
152 /* Current V=0 and we are about to change to V=1 */
153 env->mstatus_hs = env->mstatus & mstatus_mask;
154 env->mstatus &= ~mstatus_mask;
155 env->mstatus |= env->vsstatus;
156
551fa7e8
AF
157#if defined(TARGET_RISCV32)
158 env->mstatush_hs = env->mstatush;
159 env->mstatush |= env->vsstatush;
160#endif
161
66e594f2
AF
162 env->stvec_hs = env->stvec;
163 env->stvec = env->vstvec;
164
165 env->sscratch_hs = env->sscratch;
166 env->sscratch = env->vsscratch;
167
168 env->sepc_hs = env->sepc;
169 env->sepc = env->vsepc;
170
171 env->scause_hs = env->scause;
172 env->scause = env->vscause;
173
174 env->stval_hs = env->sbadaddr;
175 env->sbadaddr = env->vstval;
176
177 env->satp_hs = env->satp;
178 env->satp = env->vsatp;
179 }
180}
181
ef6bb7b6
AF
182bool riscv_cpu_virt_enabled(CPURISCVState *env)
183{
184 if (!riscv_has_ext(env, RVH)) {
185 return false;
186 }
187
188 return get_field(env->virt, VIRT_ONOFF);
189}
190
191void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable)
192{
193 if (!riscv_has_ext(env, RVH)) {
194 return;
195 }
196
eccc5a12
AF
197 /* Flush the TLB on all virt mode changes. */
198 if (get_field(env->virt, VIRT_ONOFF) != enable) {
199 tlb_flush(env_cpu(env));
200 }
201
ef6bb7b6
AF
202 env->virt = set_field(env->virt, VIRT_ONOFF, enable);
203}
204
c7b1bbc8
AF
205bool riscv_cpu_force_hs_excep_enabled(CPURISCVState *env)
206{
207 if (!riscv_has_ext(env, RVH)) {
208 return false;
209 }
210
211 return get_field(env->virt, FORCE_HS_EXCEP);
212}
213
214void riscv_cpu_set_force_hs_excep(CPURISCVState *env, bool enable)
215{
216 if (!riscv_has_ext(env, RVH)) {
217 return;
218 }
219
220 env->virt = set_field(env->virt, FORCE_HS_EXCEP, enable);
221}
222
5a894dd7
AF
223bool riscv_cpu_two_stage_lookup(CPURISCVState *env)
224{
225 if (!riscv_has_ext(env, RVH)) {
226 return false;
227 }
228
229 return get_field(env->virt, HS_TWO_STAGE);
230}
231
232void riscv_cpu_set_two_stage_lookup(CPURISCVState *env, bool enable)
233{
234 if (!riscv_has_ext(env, RVH)) {
235 return;
236 }
237
238 env->virt = set_field(env->virt, HS_TWO_STAGE, enable);
239}
240
e3e7039c
MC
241int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts)
242{
243 CPURISCVState *env = &cpu->env;
244 if (env->miclaim & interrupts) {
245 return -1;
246 } else {
247 env->miclaim |= interrupts;
248 return 0;
249 }
250}
251
df354dd4
MC
252uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value)
253{
254 CPURISCVState *env = &cpu->env;
0a01f2ee 255 CPUState *cs = CPU(cpu);
7ec5d303
AF
256 uint32_t old = env->mip;
257 bool locked = false;
258
259 if (!qemu_mutex_iothread_locked()) {
260 locked = true;
261 qemu_mutex_lock_iothread();
262 }
df354dd4 263
7ec5d303 264 env->mip = (env->mip & ~mask) | (value & mask);
df354dd4 265
7ec5d303
AF
266 if (env->mip) {
267 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
268 } else {
269 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
270 }
0a01f2ee 271
7ec5d303
AF
272 if (locked) {
273 qemu_mutex_unlock_iothread();
274 }
df354dd4
MC
275
276 return old;
277}
278
a47ef6e9
BM
279void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(uint32_t),
280 uint32_t arg)
c6957248
AP
281{
282 env->rdtime_fn = fn;
a47ef6e9 283 env->rdtime_fn_arg = arg;
c6957248
AP
284}
285
fb738839 286void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv)
df354dd4
MC
287{
288 if (newpriv > PRV_M) {
289 g_assert_not_reached();
290 }
291 if (newpriv == PRV_H) {
292 newpriv = PRV_U;
293 }
294 /* tlb_flush is unnecessary as mode is contained in mmu_idx */
295 env->priv = newpriv;
c13b169f
JS
296
297 /*
298 * Clear the load reservation - otherwise a reservation placed in one
299 * context/process can be used by another, resulting in an SC succeeding
300 * incorrectly. Version 2.2 of the ISA specification explicitly requires
301 * this behaviour, while later revisions say that the kernel "should" use
302 * an SC instruction to force the yielding of a load reservation on a
303 * preemptive context switch. As a result, do both.
304 */
305 env->load_res = -1;
df354dd4
MC
306}
307
0c3e702a
MC
308/* get_physical_address - get the physical address for this virtual address
309 *
310 * Do a page table walk to obtain the physical address corresponding to a
311 * virtual address. Returns 0 if the translation was successful
312 *
313 * Adapted from Spike's mmu_t::translate and mmu_t::walk
314 *
1448689c
AF
315 * @env: CPURISCVState
316 * @physical: This will be set to the calculated physical address
317 * @prot: The returned protection attributes
318 * @addr: The virtual address to be translated
319 * @access_type: The type of MMU access
320 * @mmu_idx: Indicates current privilege level
321 * @first_stage: Are we in first stage translation?
322 * Second stage is used for hypervisor guest translation
36a18664 323 * @two_stage: Are we going to perform two stage translation
0c3e702a
MC
324 */
325static int get_physical_address(CPURISCVState *env, hwaddr *physical,
326 int *prot, target_ulong addr,
1448689c 327 int access_type, int mmu_idx,
36a18664 328 bool first_stage, bool two_stage)
0c3e702a
MC
329{
330 /* NOTE: the env->pc value visible here will not be
331 * correct, but the value visible to the exception handler
332 * (riscv_cpu_do_interrupt) is correct */
aacb578f
PD
333 MemTxResult res;
334 MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
0c3e702a 335 int mode = mmu_idx;
36a18664 336 bool use_background = false;
0c3e702a 337
36a18664
AF
338 /*
339 * Check if we should use the background registers for the two
340 * stage translation. We don't need to check if we actually need
341 * two stage translation as that happened before this function
342 * was called. Background registers will be used if the guest has
343 * forced a two stage translation to be on (in HS or M mode).
344 */
29b3361b
AF
345 if (riscv_cpu_two_stage_lookup(env) && access_type != MMU_INST_FETCH) {
346 use_background = true;
347 }
348
0c3e702a
MC
349 if (mode == PRV_M && access_type != MMU_INST_FETCH) {
350 if (get_field(env->mstatus, MSTATUS_MPRV)) {
351 mode = get_field(env->mstatus, MSTATUS_MPP);
352 }
353 }
354
36a18664
AF
355 if (first_stage == false) {
356 /* We are in stage 2 translation, this is similar to stage 1. */
357 /* Stage 2 is always taken as U-mode */
358 mode = PRV_U;
359 }
360
0c3e702a
MC
361 if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) {
362 *physical = addr;
363 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
364 return TRANSLATE_SUCCESS;
365 }
366
367 *prot = 0;
368
ddf78132 369 hwaddr base;
36a18664
AF
370 int levels, ptidxbits, ptesize, vm, sum, mxr, widened;
371
372 if (first_stage == true) {
373 mxr = get_field(env->mstatus, MSTATUS_MXR);
374 } else {
375 mxr = get_field(env->vsstatus, MSTATUS_MXR);
376 }
0c3e702a 377
1a9540d1
AF
378 if (first_stage == true) {
379 if (use_background) {
380 base = (hwaddr)get_field(env->vsatp, SATP_PPN) << PGSHIFT;
381 vm = get_field(env->vsatp, SATP_MODE);
36a18664 382 } else {
1a9540d1
AF
383 base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT;
384 vm = get_field(env->satp, SATP_MODE);
0c3e702a 385 }
36a18664 386 widened = 0;
1a9540d1
AF
387 } else {
388 base = (hwaddr)get_field(env->hgatp, HGATP_PPN) << PGSHIFT;
389 vm = get_field(env->hgatp, HGATP_MODE);
390 widened = 2;
391 }
392 sum = get_field(env->mstatus, MSTATUS_SUM);
393 switch (vm) {
394 case VM_1_10_SV32:
395 levels = 2; ptidxbits = 10; ptesize = 4; break;
396 case VM_1_10_SV39:
397 levels = 3; ptidxbits = 9; ptesize = 8; break;
398 case VM_1_10_SV48:
399 levels = 4; ptidxbits = 9; ptesize = 8; break;
400 case VM_1_10_SV57:
401 levels = 5; ptidxbits = 9; ptesize = 8; break;
402 case VM_1_10_MBARE:
403 *physical = addr;
404 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
405 return TRANSLATE_SUCCESS;
406 default:
407 g_assert_not_reached();
0c3e702a
MC
408 }
409
3109cd98 410 CPUState *cs = env_cpu(env);
36a18664
AF
411 int va_bits = PGSHIFT + levels * ptidxbits + widened;
412 target_ulong mask, masked_msbs;
413
414 if (TARGET_LONG_BITS > (va_bits - 1)) {
415 mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1;
416 } else {
417 mask = 0;
418 }
419 masked_msbs = (addr >> (va_bits - 1)) & mask;
420
0c3e702a
MC
421 if (masked_msbs != 0 && masked_msbs != mask) {
422 return TRANSLATE_FAIL;
423 }
424
425 int ptshift = (levels - 1) * ptidxbits;
426 int i;
427
428#if !TCG_OVERSIZED_GUEST
429restart:
430#endif
431 for (i = 0; i < levels; i++, ptshift -= ptidxbits) {
36a18664
AF
432 target_ulong idx;
433 if (i == 0) {
434 idx = (addr >> (PGSHIFT + ptshift)) &
435 ((1 << (ptidxbits + widened)) - 1);
436 } else {
437 idx = (addr >> (PGSHIFT + ptshift)) &
0c3e702a 438 ((1 << ptidxbits) - 1);
36a18664 439 }
0c3e702a
MC
440
441 /* check that physical address of PTE is legal */
36a18664
AF
442 hwaddr pte_addr;
443
444 if (two_stage && first_stage) {
38472890 445 int vbase_prot;
36a18664
AF
446 hwaddr vbase;
447
448 /* Do the second stage translation on the base PTE address. */
88914473
AF
449 int vbase_ret = get_physical_address(env, &vbase, &vbase_prot,
450 base, MMU_DATA_LOAD,
451 mmu_idx, false, true);
452
453 if (vbase_ret != TRANSLATE_SUCCESS) {
454 return vbase_ret;
455 }
36a18664
AF
456
457 pte_addr = vbase + idx * ptesize;
458 } else {
459 pte_addr = base + idx * ptesize;
460 }
1f447aec
HA
461
462 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
463 !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong),
464 1 << MMU_DATA_LOAD, PRV_S)) {
465 return TRANSLATE_PMP_FAIL;
466 }
aacb578f 467
0c3e702a 468#if defined(TARGET_RISCV32)
aacb578f 469 target_ulong pte = address_space_ldl(cs->as, pte_addr, attrs, &res);
0c3e702a 470#elif defined(TARGET_RISCV64)
aacb578f 471 target_ulong pte = address_space_ldq(cs->as, pte_addr, attrs, &res);
0c3e702a 472#endif
aacb578f
PD
473 if (res != MEMTX_OK) {
474 return TRANSLATE_FAIL;
475 }
476
ddf78132 477 hwaddr ppn = pte >> PTE_PPN_SHIFT;
0c3e702a 478
c3b03e58
MC
479 if (!(pte & PTE_V)) {
480 /* Invalid PTE */
481 return TRANSLATE_FAIL;
482 } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
483 /* Inner PTE, continue walking */
0c3e702a 484 base = ppn << PGSHIFT;
c3b03e58
MC
485 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
486 /* Reserved leaf PTE flags: PTE_W */
487 return TRANSLATE_FAIL;
488 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) {
489 /* Reserved leaf PTE flags: PTE_W + PTE_X */
490 return TRANSLATE_FAIL;
491 } else if ((pte & PTE_U) && ((mode != PRV_U) &&
492 (!sum || access_type == MMU_INST_FETCH))) {
493 /* User PTE flags when not U mode and mstatus.SUM is not set,
494 or the access type is an instruction fetch */
495 return TRANSLATE_FAIL;
496 } else if (!(pte & PTE_U) && (mode != PRV_S)) {
497 /* Supervisor PTE flags when not S mode */
498 return TRANSLATE_FAIL;
499 } else if (ppn & ((1ULL << ptshift) - 1)) {
500 /* Misaligned PPN */
501 return TRANSLATE_FAIL;
502 } else if (access_type == MMU_DATA_LOAD && !((pte & PTE_R) ||
503 ((pte & PTE_X) && mxr))) {
504 /* Read access check failed */
505 return TRANSLATE_FAIL;
506 } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) {
507 /* Write access check failed */
508 return TRANSLATE_FAIL;
509 } else if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) {
510 /* Fetch access check failed */
511 return TRANSLATE_FAIL;
0c3e702a
MC
512 } else {
513 /* if necessary, set accessed and dirty bits. */
514 target_ulong updated_pte = pte | PTE_A |
515 (access_type == MMU_DATA_STORE ? PTE_D : 0);
516
517 /* Page table updates need to be atomic with MTTCG enabled */
518 if (updated_pte != pte) {
c3b03e58
MC
519 /*
520 * - if accessed or dirty bits need updating, and the PTE is
521 * in RAM, then we do so atomically with a compare and swap.
522 * - if the PTE is in IO space or ROM, then it can't be updated
523 * and we return TRANSLATE_FAIL.
524 * - if the PTE changed by the time we went to update it, then
525 * it is no longer valid and we must re-walk the page table.
526 */
0c3e702a
MC
527 MemoryRegion *mr;
528 hwaddr l = sizeof(target_ulong), addr1;
529 mr = address_space_translate(cs->as, pte_addr,
bc6b1cec 530 &addr1, &l, false, MEMTXATTRS_UNSPECIFIED);
c3b03e58 531 if (memory_region_is_ram(mr)) {
0c3e702a
MC
532 target_ulong *pte_pa =
533 qemu_map_ram_ptr(mr->ram_block, addr1);
534#if TCG_OVERSIZED_GUEST
535 /* MTTCG is not enabled on oversized TCG guests so
536 * page table updates do not need to be atomic */
537 *pte_pa = pte = updated_pte;
538#else
539 target_ulong old_pte =
d73415a3 540 qatomic_cmpxchg(pte_pa, pte, updated_pte);
0c3e702a
MC
541 if (old_pte != pte) {
542 goto restart;
543 } else {
544 pte = updated_pte;
545 }
546#endif
547 } else {
548 /* misconfigured PTE in ROM (AD bits are not preset) or
549 * PTE is in IO space and can't be updated atomically */
550 return TRANSLATE_FAIL;
551 }
552 }
553
554 /* for superpage mappings, make a fake leaf PTE for the TLB's
555 benefit. */
556 target_ulong vpn = addr >> PGSHIFT;
9ef82119
ZL
557 *physical = ((ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT) |
558 (addr & ~TARGET_PAGE_MASK);
0c3e702a 559
c3b03e58
MC
560 /* set permissions on the TLB entry */
561 if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) {
0c3e702a
MC
562 *prot |= PAGE_READ;
563 }
564 if ((pte & PTE_X)) {
565 *prot |= PAGE_EXEC;
566 }
c3b03e58
MC
567 /* add write permission on stores or if the page is already dirty,
568 so that we TLB miss on later writes to update the dirty bit */
0c3e702a
MC
569 if ((pte & PTE_W) &&
570 (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
571 *prot |= PAGE_WRITE;
572 }
573 return TRANSLATE_SUCCESS;
574 }
575 }
576 return TRANSLATE_FAIL;
577}
578
579static void raise_mmu_exception(CPURISCVState *env, target_ulong address,
1448689c
AF
580 MMUAccessType access_type, bool pmp_violation,
581 bool first_stage)
0c3e702a 582{
3109cd98 583 CPUState *cs = env_cpu(env);
1448689c
AF
584 int page_fault_exceptions;
585 if (first_stage) {
586 page_fault_exceptions =
1448689c
AF
587 get_field(env->satp, SATP_MODE) != VM_1_10_MBARE &&
588 !pmp_violation;
589 } else {
590 page_fault_exceptions =
591 get_field(env->hgatp, HGATP_MODE) != VM_1_10_MBARE &&
592 !pmp_violation;
593 }
0c3e702a
MC
594 switch (access_type) {
595 case MMU_INST_FETCH:
b2ef6ab9
AF
596 if (riscv_cpu_virt_enabled(env) && !first_stage) {
597 cs->exception_index = RISCV_EXCP_INST_GUEST_PAGE_FAULT;
598 } else {
599 cs->exception_index = page_fault_exceptions ?
600 RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT;
601 }
0c3e702a
MC
602 break;
603 case MMU_DATA_LOAD:
29b3361b
AF
604 if ((riscv_cpu_virt_enabled(env) || riscv_cpu_two_stage_lookup(env)) &&
605 !first_stage) {
b2ef6ab9
AF
606 cs->exception_index = RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT;
607 } else {
608 cs->exception_index = page_fault_exceptions ?
609 RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT;
610 }
0c3e702a
MC
611 break;
612 case MMU_DATA_STORE:
29b3361b
AF
613 if ((riscv_cpu_virt_enabled(env) || riscv_cpu_two_stage_lookup(env)) &&
614 !first_stage) {
b2ef6ab9
AF
615 cs->exception_index = RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT;
616 } else {
617 cs->exception_index = page_fault_exceptions ?
618 RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
619 }
0c3e702a
MC
620 break;
621 default:
622 g_assert_not_reached();
623 }
624 env->badaddr = address;
625}
626
627hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
628{
629 RISCVCPU *cpu = RISCV_CPU(cs);
36a18664 630 CPURISCVState *env = &cpu->env;
0c3e702a
MC
631 hwaddr phys_addr;
632 int prot;
633 int mmu_idx = cpu_mmu_index(&cpu->env, false);
634
36a18664
AF
635 if (get_physical_address(env, &phys_addr, &prot, addr, 0, mmu_idx,
636 true, riscv_cpu_virt_enabled(env))) {
0c3e702a
MC
637 return -1;
638 }
36a18664
AF
639
640 if (riscv_cpu_virt_enabled(env)) {
641 if (get_physical_address(env, &phys_addr, &prot, phys_addr,
642 0, mmu_idx, false, true)) {
643 return -1;
644 }
645 }
646
9ef82119 647 return phys_addr & TARGET_PAGE_MASK;
0c3e702a
MC
648}
649
37207e12
PD
650void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
651 vaddr addr, unsigned size,
652 MMUAccessType access_type,
653 int mmu_idx, MemTxAttrs attrs,
654 MemTxResult response, uintptr_t retaddr)
cbf58276
MC
655{
656 RISCVCPU *cpu = RISCV_CPU(cs);
657 CPURISCVState *env = &cpu->env;
658
37207e12 659 if (access_type == MMU_DATA_STORE) {
cbf58276
MC
660 cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
661 } else {
662 cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT;
663 }
664
665 env->badaddr = addr;
37207e12 666 riscv_raise_exception(&cpu->env, cs->exception_index, retaddr);
cbf58276
MC
667}
668
0c3e702a
MC
669void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
670 MMUAccessType access_type, int mmu_idx,
671 uintptr_t retaddr)
672{
673 RISCVCPU *cpu = RISCV_CPU(cs);
674 CPURISCVState *env = &cpu->env;
675 switch (access_type) {
676 case MMU_INST_FETCH:
677 cs->exception_index = RISCV_EXCP_INST_ADDR_MIS;
678 break;
679 case MMU_DATA_LOAD:
680 cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS;
681 break;
682 case MMU_DATA_STORE:
683 cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS;
684 break;
685 default:
686 g_assert_not_reached();
687 }
688 env->badaddr = addr;
fb738839 689 riscv_raise_exception(env, cs->exception_index, retaddr);
0c3e702a 690}
0c3e702a
MC
691#endif
692
8a4ca3c1
RH
693bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
694 MMUAccessType access_type, int mmu_idx,
695 bool probe, uintptr_t retaddr)
0c3e702a
MC
696{
697 RISCVCPU *cpu = RISCV_CPU(cs);
698 CPURISCVState *env = &cpu->env;
2921343b 699#ifndef CONFIG_USER_ONLY
36a18664 700 vaddr im_address;
0c3e702a 701 hwaddr pa = 0;
8f67cd6d 702 int prot, prot2;
635b0b0e 703 bool pmp_violation = false;
36a18664 704 bool first_stage_error = true;
0c3e702a 705 int ret = TRANSLATE_FAIL;
cc0fdb29 706 int mode = mmu_idx;
af3fc195 707 target_ulong tlb_size = 0;
0c3e702a 708
36a18664
AF
709 env->guest_phys_fault_addr = 0;
710
8a4ca3c1
RH
711 qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
712 __func__, address, access_type, mmu_idx);
713
cc0fdb29
HA
714 if (mode == PRV_M && access_type != MMU_INST_FETCH) {
715 if (get_field(env->mstatus, MSTATUS_MPRV)) {
716 mode = get_field(env->mstatus, MSTATUS_MPP);
717 }
718 }
719
29b3361b
AF
720 if (riscv_has_ext(env, RVH) && env->priv == PRV_M &&
721 access_type != MMU_INST_FETCH &&
722 get_field(env->mstatus, MSTATUS_MPRV) &&
723 MSTATUS_MPV_ISSET(env)) {
724 riscv_cpu_set_two_stage_lookup(env, true);
725 }
726
727 if (riscv_cpu_virt_enabled(env) ||
728 (riscv_cpu_two_stage_lookup(env) && access_type != MMU_INST_FETCH)) {
36a18664
AF
729 /* Two stage lookup */
730 ret = get_physical_address(env, &pa, &prot, address, access_type,
731 mmu_idx, true, true);
732
733 qemu_log_mask(CPU_LOG_MMU,
734 "%s 1st-stage address=%" VADDR_PRIx " ret %d physical "
735 TARGET_FMT_plx " prot %d\n",
736 __func__, address, ret, pa, prot);
737
738 if (ret != TRANSLATE_FAIL) {
739 /* Second stage lookup */
740 im_address = pa;
741
8f67cd6d 742 ret = get_physical_address(env, &pa, &prot2, im_address,
36a18664
AF
743 access_type, mmu_idx, false, true);
744
745 qemu_log_mask(CPU_LOG_MMU,
746 "%s 2nd-stage address=%" VADDR_PRIx " ret %d physical "
747 TARGET_FMT_plx " prot %d\n",
8f67cd6d
AF
748 __func__, im_address, ret, pa, prot2);
749
750 prot &= prot2;
36a18664
AF
751
752 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
753 (ret == TRANSLATE_SUCCESS) &&
754 !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) {
755 ret = TRANSLATE_PMP_FAIL;
756 }
757
758 if (ret != TRANSLATE_SUCCESS) {
759 /*
760 * Guest physical address translation failed, this is a HS
761 * level exception
762 */
763 first_stage_error = false;
764 env->guest_phys_fault_addr = (im_address |
765 (address &
766 (TARGET_PAGE_SIZE - 1))) >> 2;
767 }
768 }
769 } else {
770 /* Single stage lookup */
771 ret = get_physical_address(env, &pa, &prot, address, access_type,
772 mmu_idx, true, false);
773
774 qemu_log_mask(CPU_LOG_MMU,
775 "%s address=%" VADDR_PRIx " ret %d physical "
776 TARGET_FMT_plx " prot %d\n",
777 __func__, address, ret, pa, prot);
778 }
8a4ca3c1 779
29b3361b
AF
780 /* We did the two stage lookup based on MPRV, unset the lookup */
781 if (riscv_has_ext(env, RVH) && env->priv == PRV_M &&
782 access_type != MMU_INST_FETCH &&
783 get_field(env->mstatus, MSTATUS_MPRV) &&
784 MSTATUS_MPV_ISSET(env)) {
785 riscv_cpu_set_two_stage_lookup(env, false);
786 }
787
a88365c1 788 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
e0f8fa72 789 (ret == TRANSLATE_SUCCESS) &&
db21e6f7 790 !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) {
1f447aec
HA
791 ret = TRANSLATE_PMP_FAIL;
792 }
793 if (ret == TRANSLATE_PMP_FAIL) {
635b0b0e 794 pmp_violation = true;
0c3e702a 795 }
36a18664 796
0c3e702a 797 if (ret == TRANSLATE_SUCCESS) {
af3fc195
ZL
798 if (pmp_is_range_in_tlb(env, pa & TARGET_PAGE_MASK, &tlb_size)) {
799 tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1),
800 prot, mmu_idx, tlb_size);
801 } else {
802 tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK,
803 prot, mmu_idx, TARGET_PAGE_SIZE);
804 }
8a4ca3c1
RH
805 return true;
806 } else if (probe) {
807 return false;
808 } else {
36a18664 809 raise_mmu_exception(env, address, access_type, pmp_violation, first_stage_error);
8a4ca3c1 810 riscv_raise_exception(env, cs->exception_index, retaddr);
0c3e702a 811 }
36a18664
AF
812
813 return true;
814
0c3e702a 815#else
8a4ca3c1 816 switch (access_type) {
0c3e702a
MC
817 case MMU_INST_FETCH:
818 cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT;
819 break;
820 case MMU_DATA_LOAD:
821 cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT;
822 break;
823 case MMU_DATA_STORE:
824 cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT;
825 break;
2921343b
GM
826 default:
827 g_assert_not_reached();
0c3e702a 828 }
2921343b 829 env->badaddr = address;
8a4ca3c1 830 cpu_loop_exit_restore(cs, retaddr);
0c3e702a 831#endif
0c3e702a
MC
832}
833
834/*
835 * Handle Traps
836 *
837 * Adapted from Spike's processor_t::take_trap.
838 *
839 */
840void riscv_cpu_do_interrupt(CPUState *cs)
841{
842#if !defined(CONFIG_USER_ONLY)
843
844 RISCVCPU *cpu = RISCV_CPU(cs);
845 CPURISCVState *env = &cpu->env;
5eb9e782
AF
846 bool force_hs_execp = riscv_cpu_force_hs_excep_enabled(env);
847 target_ulong s;
0c3e702a 848
acbbb94e
MC
849 /* cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide
850 * so we mask off the MSB and separate into trap type and cause.
851 */
852 bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG);
853 target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK;
854 target_ulong deleg = async ? env->mideleg : env->medeleg;
855 target_ulong tval = 0;
30675539
AF
856 target_ulong htval = 0;
857 target_ulong mtval2 = 0;
acbbb94e 858
acbbb94e
MC
859 if (!async) {
860 /* set tval to badaddr for traps with address information */
861 switch (cause) {
ab67a1d0
AF
862 case RISCV_EXCP_INST_GUEST_PAGE_FAULT:
863 case RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT:
864 case RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT:
5eb9e782
AF
865 force_hs_execp = true;
866 /* fallthrough */
acbbb94e
MC
867 case RISCV_EXCP_INST_ADDR_MIS:
868 case RISCV_EXCP_INST_ACCESS_FAULT:
869 case RISCV_EXCP_LOAD_ADDR_MIS:
870 case RISCV_EXCP_STORE_AMO_ADDR_MIS:
871 case RISCV_EXCP_LOAD_ACCESS_FAULT:
872 case RISCV_EXCP_STORE_AMO_ACCESS_FAULT:
873 case RISCV_EXCP_INST_PAGE_FAULT:
874 case RISCV_EXCP_LOAD_PAGE_FAULT:
875 case RISCV_EXCP_STORE_PAGE_FAULT:
876 tval = env->badaddr;
877 break;
878 default:
879 break;
0c3e702a 880 }
acbbb94e
MC
881 /* ecall is dispatched as one cause so translate based on mode */
882 if (cause == RISCV_EXCP_U_ECALL) {
883 assert(env->priv <= 3);
5eb9e782
AF
884
885 if (env->priv == PRV_M) {
886 cause = RISCV_EXCP_M_ECALL;
887 } else if (env->priv == PRV_S && riscv_cpu_virt_enabled(env)) {
888 cause = RISCV_EXCP_VS_ECALL;
889 } else if (env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) {
890 cause = RISCV_EXCP_S_ECALL;
891 } else if (env->priv == PRV_U) {
892 cause = RISCV_EXCP_U_ECALL;
893 }
0c3e702a
MC
894 }
895 }
896
c51a3f5d 897 trace_riscv_trap(env->mhartid, async, cause, env->pc, tval,
69430111
AF
898 riscv_cpu_get_trap_name(cause, async));
899
900 qemu_log_mask(CPU_LOG_INT,
901 "%s: hart:"TARGET_FMT_ld", async:%d, cause:"TARGET_FMT_lx", "
902 "epc:0x"TARGET_FMT_lx", tval:0x"TARGET_FMT_lx", desc=%s\n",
903 __func__, env->mhartid, async, cause, env->pc, tval,
904 riscv_cpu_get_trap_name(cause, async));
0c3e702a 905
acbbb94e
MC
906 if (env->priv <= PRV_S &&
907 cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) {
0c3e702a 908 /* handle the trap in S-mode */
5eb9e782
AF
909 if (riscv_has_ext(env, RVH)) {
910 target_ulong hdeleg = async ? env->hideleg : env->hedeleg;
911
9034e90a
AF
912 if ((riscv_cpu_virt_enabled(env) ||
913 riscv_cpu_two_stage_lookup(env)) && tval) {
914 /*
915 * If we are writing a guest virtual address to stval, set
916 * this to 1. If we are trapping to VS we will set this to 0
917 * later.
918 */
919 env->hstatus = set_field(env->hstatus, HSTATUS_GVA, 1);
920 } else {
921 /* For other HS-mode traps, we set this to 0. */
922 env->hstatus = set_field(env->hstatus, HSTATUS_GVA, 0);
923 }
924
5eb9e782
AF
925 if (riscv_cpu_virt_enabled(env) && ((hdeleg >> cause) & 1) &&
926 !force_hs_execp) {
84b1c04b 927 /* Trap to VS mode */
c5969a3a
RK
928 /*
929 * See if we need to adjust cause. Yes if its VS mode interrupt
930 * no if hypervisor has delegated one of hs mode's interrupt
931 */
932 if (cause == IRQ_VS_TIMER || cause == IRQ_VS_SOFT ||
84b1c04b 933 cause == IRQ_VS_EXT) {
c5969a3a 934 cause = cause - 1;
84b1c04b 935 }
9034e90a 936 env->hstatus = set_field(env->hstatus, HSTATUS_GVA, 0);
5eb9e782
AF
937 } else if (riscv_cpu_virt_enabled(env)) {
938 /* Trap into HS mode, from virt */
939 riscv_cpu_swap_hypervisor_regs(env);
f2d5850f 940 env->hstatus = set_field(env->hstatus, HSTATUS_SPVP,
ace54453 941 env->priv);
5eb9e782
AF
942 env->hstatus = set_field(env->hstatus, HSTATUS_SPV,
943 riscv_cpu_virt_enabled(env));
944
30675539
AF
945 htval = env->guest_phys_fault_addr;
946
5eb9e782
AF
947 riscv_cpu_set_virt_enabled(env, 0);
948 riscv_cpu_set_force_hs_excep(env, 0);
949 } else {
950 /* Trap into HS mode */
f2d5850f
AF
951 if (!riscv_cpu_two_stage_lookup(env)) {
952 env->hstatus = set_field(env->hstatus, HSTATUS_SPV,
953 riscv_cpu_virt_enabled(env));
954 }
955 riscv_cpu_set_two_stage_lookup(env, false);
30675539 956 htval = env->guest_phys_fault_addr;
5eb9e782
AF
957 }
958 }
959
960 s = env->mstatus;
1a9540d1 961 s = set_field(s, MSTATUS_SPIE, get_field(s, MSTATUS_SIE));
0c3e702a
MC
962 s = set_field(s, MSTATUS_SPP, env->priv);
963 s = set_field(s, MSTATUS_SIE, 0);
c7b95171 964 env->mstatus = s;
16fdb8ff 965 env->scause = cause | ((target_ulong)async << (TARGET_LONG_BITS - 1));
acbbb94e
MC
966 env->sepc = env->pc;
967 env->sbadaddr = tval;
30675539 968 env->htval = htval;
acbbb94e
MC
969 env->pc = (env->stvec >> 2 << 2) +
970 ((async && (env->stvec & 3) == 1) ? cause * 4 : 0);
fb738839 971 riscv_cpu_set_mode(env, PRV_S);
0c3e702a 972 } else {
acbbb94e 973 /* handle the trap in M-mode */
5eb9e782
AF
974 if (riscv_has_ext(env, RVH)) {
975 if (riscv_cpu_virt_enabled(env)) {
976 riscv_cpu_swap_hypervisor_regs(env);
977 }
551fa7e8
AF
978#ifdef TARGET_RISCV32
979 env->mstatush = set_field(env->mstatush, MSTATUS_MPV,
980 riscv_cpu_virt_enabled(env));
9034e90a
AF
981 if (riscv_cpu_virt_enabled(env) && tval) {
982 env->mstatush = set_field(env->mstatush, MSTATUS_GVA, 1);
983 }
551fa7e8 984#else
5eb9e782
AF
985 env->mstatus = set_field(env->mstatus, MSTATUS_MPV,
986 riscv_cpu_virt_enabled(env));
9034e90a
AF
987 if (riscv_cpu_virt_enabled(env) && tval) {
988 env->mstatus = set_field(env->mstatus, MSTATUS_GVA, 1);
989 }
551fa7e8 990#endif
5eb9e782 991
30675539
AF
992 mtval2 = env->guest_phys_fault_addr;
993
5eb9e782
AF
994 /* Trapping to M mode, virt is disabled */
995 riscv_cpu_set_virt_enabled(env, 0);
996 riscv_cpu_set_force_hs_excep(env, 0);
997 }
998
999 s = env->mstatus;
1a9540d1 1000 s = set_field(s, MSTATUS_MPIE, get_field(s, MSTATUS_MIE));
0c3e702a
MC
1001 s = set_field(s, MSTATUS_MPP, env->priv);
1002 s = set_field(s, MSTATUS_MIE, 0);
c7b95171 1003 env->mstatus = s;
acbbb94e
MC
1004 env->mcause = cause | ~(((target_ulong)-1) >> async);
1005 env->mepc = env->pc;
1006 env->mbadaddr = tval;
30675539 1007 env->mtval2 = mtval2;
acbbb94e
MC
1008 env->pc = (env->mtvec >> 2 << 2) +
1009 ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0);
fb738839 1010 riscv_cpu_set_mode(env, PRV_M);
0c3e702a 1011 }
d9360e96
MC
1012
1013 /* NOTE: it is not necessary to yield load reservations here. It is only
1014 * necessary for an SC from "another hart" to cause a load reservation
1015 * to be yielded. Refer to the memory consistency model section of the
1016 * RISC-V ISA Specification.
1017 */
1018
0c3e702a
MC
1019#endif
1020 cs->exception_index = EXCP_NONE; /* mark handled to qemu */
1021}