2 * RISC-V CPU helpers for qemu.
5 * Copyright (c) 2017-2018 SiFive, Inc.
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.
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
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/>.
20 #include "qemu/osdep.h"
22 #include "qemu/main-loop.h"
24 #include "exec/exec-all.h"
25 #include "tcg/tcg-op.h"
28 int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch)
30 #ifdef CONFIG_USER_ONLY
37 #ifndef CONFIG_USER_ONLY
38 static int riscv_cpu_local_irq_pending(CPURISCVState *env)
42 target_ulong mstatus_mie = get_field(env->mstatus, MSTATUS_MIE);
43 target_ulong mstatus_sie = get_field(env->mstatus, MSTATUS_SIE);
44 target_ulong hs_mstatus_sie = get_field(env->mstatus_hs, MSTATUS_SIE);
46 target_ulong pending = env->mip & env->mie &
47 ~(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP);
48 target_ulong vspending = (env->mip & env->mie &
49 (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)) >> 1;
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);
58 if (riscv_cpu_virt_enabled(env)) {
59 target_ulong pending_hs_irq = pending & -hs_sie;
62 riscv_cpu_set_force_hs_excep(env, FORCE_HS_EXCEP);
63 return ctz64(pending_hs_irq);
69 irqs = (pending & ~env->mideleg & -mie) | (pending & env->mideleg & -sie);
72 return ctz64(irqs); /* since non-zero */
74 return EXCP_NONE; /* indicates no pending interrupt */
79 bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
81 #if !defined(CONFIG_USER_ONLY)
82 if (interrupt_request & CPU_INTERRUPT_HARD) {
83 RISCVCPU *cpu = RISCV_CPU(cs);
84 CPURISCVState *env = &cpu->env;
85 int interruptno = riscv_cpu_local_irq_pending(env);
86 if (interruptno >= 0) {
87 cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno;
88 riscv_cpu_do_interrupt(cs);
96 #if !defined(CONFIG_USER_ONLY)
98 /* Return true is floating point support is currently enabled */
99 bool riscv_cpu_fp_enabled(CPURISCVState *env)
101 if (env->mstatus & MSTATUS_FS) {
102 if (riscv_cpu_virt_enabled(env) && !(env->mstatus_hs & MSTATUS_FS)) {
111 void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env)
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);
117 g_assert(riscv_has_ext(env, RVH));
119 #if defined(TARGET_RISCV64)
120 mstatus_mask |= MSTATUS64_UXL;
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;
129 env->vstvec = env->stvec;
130 env->stvec = env->stvec_hs;
132 env->vsscratch = env->sscratch;
133 env->sscratch = env->sscratch_hs;
135 env->vsepc = env->sepc;
136 env->sepc = env->sepc_hs;
138 env->vscause = env->scause;
139 env->scause = env->scause_hs;
141 env->vstval = env->sbadaddr;
142 env->sbadaddr = env->stval_hs;
144 env->vsatp = env->satp;
145 env->satp = env->satp_hs;
147 /* Current V=0 and we are about to change to V=1 */
148 env->mstatus_hs = env->mstatus & mstatus_mask;
149 env->mstatus &= ~mstatus_mask;
150 env->mstatus |= env->vsstatus;
152 env->stvec_hs = env->stvec;
153 env->stvec = env->vstvec;
155 env->sscratch_hs = env->sscratch;
156 env->sscratch = env->vsscratch;
158 env->sepc_hs = env->sepc;
159 env->sepc = env->vsepc;
161 env->scause_hs = env->scause;
162 env->scause = env->vscause;
164 env->stval_hs = env->sbadaddr;
165 env->sbadaddr = env->vstval;
167 env->satp_hs = env->satp;
168 env->satp = env->vsatp;
172 bool riscv_cpu_virt_enabled(CPURISCVState *env)
174 if (!riscv_has_ext(env, RVH)) {
178 return get_field(env->virt, VIRT_ONOFF);
181 void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable)
183 if (!riscv_has_ext(env, RVH)) {
187 /* Flush the TLB on all virt mode changes. */
188 if (get_field(env->virt, VIRT_ONOFF) != enable) {
189 tlb_flush(env_cpu(env));
192 env->virt = set_field(env->virt, VIRT_ONOFF, enable);
195 bool riscv_cpu_force_hs_excep_enabled(CPURISCVState *env)
197 if (!riscv_has_ext(env, RVH)) {
201 return get_field(env->virt, FORCE_HS_EXCEP);
204 void riscv_cpu_set_force_hs_excep(CPURISCVState *env, bool enable)
206 if (!riscv_has_ext(env, RVH)) {
210 env->virt = set_field(env->virt, FORCE_HS_EXCEP, enable);
213 int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts)
215 CPURISCVState *env = &cpu->env;
216 if (env->miclaim & interrupts) {
219 env->miclaim |= interrupts;
224 uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value)
226 CPURISCVState *env = &cpu->env;
227 CPUState *cs = CPU(cpu);
228 uint32_t old = env->mip;
231 if (!qemu_mutex_iothread_locked()) {
233 qemu_mutex_lock_iothread();
236 env->mip = (env->mip & ~mask) | (value & mask);
239 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
241 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
245 qemu_mutex_unlock_iothread();
251 void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv)
253 if (newpriv > PRV_M) {
254 g_assert_not_reached();
256 if (newpriv == PRV_H) {
259 /* tlb_flush is unnecessary as mode is contained in mmu_idx */
263 * Clear the load reservation - otherwise a reservation placed in one
264 * context/process can be used by another, resulting in an SC succeeding
265 * incorrectly. Version 2.2 of the ISA specification explicitly requires
266 * this behaviour, while later revisions say that the kernel "should" use
267 * an SC instruction to force the yielding of a load reservation on a
268 * preemptive context switch. As a result, do both.
273 /* get_physical_address - get the physical address for this virtual address
275 * Do a page table walk to obtain the physical address corresponding to a
276 * virtual address. Returns 0 if the translation was successful
278 * Adapted from Spike's mmu_t::translate and mmu_t::walk
280 * @env: CPURISCVState
281 * @physical: This will be set to the calculated physical address
282 * @prot: The returned protection attributes
283 * @addr: The virtual address to be translated
284 * @access_type: The type of MMU access
285 * @mmu_idx: Indicates current privilege level
286 * @first_stage: Are we in first stage translation?
287 * Second stage is used for hypervisor guest translation
288 * @two_stage: Are we going to perform two stage translation
290 static int get_physical_address(CPURISCVState *env, hwaddr *physical,
291 int *prot, target_ulong addr,
292 int access_type, int mmu_idx,
293 bool first_stage, bool two_stage)
295 /* NOTE: the env->pc value visible here will not be
296 * correct, but the value visible to the exception handler
297 * (riscv_cpu_do_interrupt) is correct */
299 MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
301 bool use_background = false;
304 * Check if we should use the background registers for the two
305 * stage translation. We don't need to check if we actually need
306 * two stage translation as that happened before this function
307 * was called. Background registers will be used if the guest has
308 * forced a two stage translation to be on (in HS or M mode).
310 if (mode == PRV_M && access_type != MMU_INST_FETCH) {
311 if (get_field(env->mstatus, MSTATUS_MPRV)) {
312 mode = get_field(env->mstatus, MSTATUS_MPP);
314 if (riscv_has_ext(env, RVH) &&
315 get_field(env->mstatus, MSTATUS_MPV)) {
316 use_background = true;
321 if (mode == PRV_S && access_type != MMU_INST_FETCH &&
322 riscv_has_ext(env, RVH) && !riscv_cpu_virt_enabled(env)) {
323 if (get_field(env->hstatus, HSTATUS_SPRV)) {
324 mode = get_field(env->mstatus, SSTATUS_SPP);
325 use_background = true;
329 if (first_stage == false) {
330 /* We are in stage 2 translation, this is similar to stage 1. */
331 /* Stage 2 is always taken as U-mode */
335 if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) {
337 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
338 return TRANSLATE_SUCCESS;
344 int levels, ptidxbits, ptesize, vm, sum, mxr, widened;
346 if (first_stage == true) {
347 mxr = get_field(env->mstatus, MSTATUS_MXR);
349 mxr = get_field(env->vsstatus, MSTATUS_MXR);
352 if (env->priv_ver >= PRIV_VERSION_1_10_0) {
353 if (first_stage == true) {
354 if (use_background) {
355 base = (hwaddr)get_field(env->vsatp, SATP_PPN) << PGSHIFT;
356 vm = get_field(env->vsatp, SATP_MODE);
358 base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT;
359 vm = get_field(env->satp, SATP_MODE);
363 base = (hwaddr)get_field(env->hgatp, HGATP_PPN) << PGSHIFT;
364 vm = get_field(env->hgatp, HGATP_MODE);
367 sum = get_field(env->mstatus, MSTATUS_SUM);
370 levels = 2; ptidxbits = 10; ptesize = 4; break;
372 levels = 3; ptidxbits = 9; ptesize = 8; break;
374 levels = 4; ptidxbits = 9; ptesize = 8; break;
376 levels = 5; ptidxbits = 9; ptesize = 8; break;
379 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
380 return TRANSLATE_SUCCESS;
382 g_assert_not_reached();
386 base = (hwaddr)(env->sptbr) << PGSHIFT;
387 sum = !get_field(env->mstatus, MSTATUS_PUM);
388 vm = get_field(env->mstatus, MSTATUS_VM);
391 levels = 2; ptidxbits = 10; ptesize = 4; break;
393 levels = 3; ptidxbits = 9; ptesize = 8; break;
395 levels = 4; ptidxbits = 9; ptesize = 8; break;
398 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
399 return TRANSLATE_SUCCESS;
401 g_assert_not_reached();
405 CPUState *cs = env_cpu(env);
406 int va_bits = PGSHIFT + levels * ptidxbits + widened;
407 target_ulong mask, masked_msbs;
409 if (TARGET_LONG_BITS > (va_bits - 1)) {
410 mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1;
414 masked_msbs = (addr >> (va_bits - 1)) & mask;
416 if (masked_msbs != 0 && masked_msbs != mask) {
417 return TRANSLATE_FAIL;
420 int ptshift = (levels - 1) * ptidxbits;
423 #if !TCG_OVERSIZED_GUEST
426 for (i = 0; i < levels; i++, ptshift -= ptidxbits) {
429 idx = (addr >> (PGSHIFT + ptshift)) &
430 ((1 << (ptidxbits + widened)) - 1);
432 idx = (addr >> (PGSHIFT + ptshift)) &
433 ((1 << ptidxbits) - 1);
436 /* check that physical address of PTE is legal */
439 if (two_stage && first_stage) {
442 /* Do the second stage translation on the base PTE address. */
443 get_physical_address(env, &vbase, prot, base, access_type,
444 mmu_idx, false, true);
446 pte_addr = vbase + idx * ptesize;
448 pte_addr = base + idx * ptesize;
451 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
452 !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong),
453 1 << MMU_DATA_LOAD, PRV_S)) {
454 return TRANSLATE_PMP_FAIL;
457 #if defined(TARGET_RISCV32)
458 target_ulong pte = address_space_ldl(cs->as, pte_addr, attrs, &res);
459 #elif defined(TARGET_RISCV64)
460 target_ulong pte = address_space_ldq(cs->as, pte_addr, attrs, &res);
462 if (res != MEMTX_OK) {
463 return TRANSLATE_FAIL;
466 hwaddr ppn = pte >> PTE_PPN_SHIFT;
468 if (!(pte & PTE_V)) {
470 return TRANSLATE_FAIL;
471 } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
472 /* Inner PTE, continue walking */
473 base = ppn << PGSHIFT;
474 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
475 /* Reserved leaf PTE flags: PTE_W */
476 return TRANSLATE_FAIL;
477 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) {
478 /* Reserved leaf PTE flags: PTE_W + PTE_X */
479 return TRANSLATE_FAIL;
480 } else if ((pte & PTE_U) && ((mode != PRV_U) &&
481 (!sum || access_type == MMU_INST_FETCH))) {
482 /* User PTE flags when not U mode and mstatus.SUM is not set,
483 or the access type is an instruction fetch */
484 return TRANSLATE_FAIL;
485 } else if (!(pte & PTE_U) && (mode != PRV_S)) {
486 /* Supervisor PTE flags when not S mode */
487 return TRANSLATE_FAIL;
488 } else if (ppn & ((1ULL << ptshift) - 1)) {
490 return TRANSLATE_FAIL;
491 } else if (access_type == MMU_DATA_LOAD && !((pte & PTE_R) ||
492 ((pte & PTE_X) && mxr))) {
493 /* Read access check failed */
494 return TRANSLATE_FAIL;
495 } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) {
496 /* Write access check failed */
497 return TRANSLATE_FAIL;
498 } else if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) {
499 /* Fetch access check failed */
500 return TRANSLATE_FAIL;
502 /* if necessary, set accessed and dirty bits. */
503 target_ulong updated_pte = pte | PTE_A |
504 (access_type == MMU_DATA_STORE ? PTE_D : 0);
506 /* Page table updates need to be atomic with MTTCG enabled */
507 if (updated_pte != pte) {
509 * - if accessed or dirty bits need updating, and the PTE is
510 * in RAM, then we do so atomically with a compare and swap.
511 * - if the PTE is in IO space or ROM, then it can't be updated
512 * and we return TRANSLATE_FAIL.
513 * - if the PTE changed by the time we went to update it, then
514 * it is no longer valid and we must re-walk the page table.
517 hwaddr l = sizeof(target_ulong), addr1;
518 mr = address_space_translate(cs->as, pte_addr,
519 &addr1, &l, false, MEMTXATTRS_UNSPECIFIED);
520 if (memory_region_is_ram(mr)) {
521 target_ulong *pte_pa =
522 qemu_map_ram_ptr(mr->ram_block, addr1);
523 #if TCG_OVERSIZED_GUEST
524 /* MTTCG is not enabled on oversized TCG guests so
525 * page table updates do not need to be atomic */
526 *pte_pa = pte = updated_pte;
528 target_ulong old_pte =
529 atomic_cmpxchg(pte_pa, pte, updated_pte);
530 if (old_pte != pte) {
537 /* misconfigured PTE in ROM (AD bits are not preset) or
538 * PTE is in IO space and can't be updated atomically */
539 return TRANSLATE_FAIL;
543 /* for superpage mappings, make a fake leaf PTE for the TLB's
545 target_ulong vpn = addr >> PGSHIFT;
547 *physical = (ppn | (vpn & ((1L << (ptshift + widened)) - 1))) <<
550 *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT;
553 /* set permissions on the TLB entry */
554 if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) {
560 /* add write permission on stores or if the page is already dirty,
561 so that we TLB miss on later writes to update the dirty bit */
563 (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
566 return TRANSLATE_SUCCESS;
569 return TRANSLATE_FAIL;
572 static void raise_mmu_exception(CPURISCVState *env, target_ulong address,
573 MMUAccessType access_type, bool pmp_violation,
576 CPUState *cs = env_cpu(env);
577 int page_fault_exceptions;
579 page_fault_exceptions =
580 (env->priv_ver >= PRIV_VERSION_1_10_0) &&
581 get_field(env->satp, SATP_MODE) != VM_1_10_MBARE &&
584 page_fault_exceptions =
585 get_field(env->hgatp, HGATP_MODE) != VM_1_10_MBARE &&
588 switch (access_type) {
590 cs->exception_index = page_fault_exceptions ?
591 RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT;
594 cs->exception_index = page_fault_exceptions ?
595 RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT;
598 cs->exception_index = page_fault_exceptions ?
599 RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
602 g_assert_not_reached();
604 env->badaddr = address;
607 hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
609 RISCVCPU *cpu = RISCV_CPU(cs);
610 CPURISCVState *env = &cpu->env;
613 int mmu_idx = cpu_mmu_index(&cpu->env, false);
615 if (get_physical_address(env, &phys_addr, &prot, addr, 0, mmu_idx,
616 true, riscv_cpu_virt_enabled(env))) {
620 if (riscv_cpu_virt_enabled(env)) {
621 if (get_physical_address(env, &phys_addr, &prot, phys_addr,
622 0, mmu_idx, false, true)) {
630 void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
631 vaddr addr, unsigned size,
632 MMUAccessType access_type,
633 int mmu_idx, MemTxAttrs attrs,
634 MemTxResult response, uintptr_t retaddr)
636 RISCVCPU *cpu = RISCV_CPU(cs);
637 CPURISCVState *env = &cpu->env;
639 if (access_type == MMU_DATA_STORE) {
640 cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
642 cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT;
646 riscv_raise_exception(&cpu->env, cs->exception_index, retaddr);
649 void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
650 MMUAccessType access_type, int mmu_idx,
653 RISCVCPU *cpu = RISCV_CPU(cs);
654 CPURISCVState *env = &cpu->env;
655 switch (access_type) {
657 cs->exception_index = RISCV_EXCP_INST_ADDR_MIS;
660 cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS;
663 cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS;
666 g_assert_not_reached();
669 riscv_raise_exception(env, cs->exception_index, retaddr);
673 bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
674 MMUAccessType access_type, int mmu_idx,
675 bool probe, uintptr_t retaddr)
677 RISCVCPU *cpu = RISCV_CPU(cs);
678 CPURISCVState *env = &cpu->env;
679 #ifndef CONFIG_USER_ONLY
683 bool pmp_violation = false;
684 bool m_mode_two_stage = false;
685 bool hs_mode_two_stage = false;
686 bool first_stage_error = true;
687 int ret = TRANSLATE_FAIL;
690 env->guest_phys_fault_addr = 0;
692 qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
693 __func__, address, access_type, mmu_idx);
696 * Determine if we are in M mode and MPRV is set or in HS mode and SPRV is
697 * set and we want to access a virtulisation address.
699 if (riscv_has_ext(env, RVH)) {
700 m_mode_two_stage = env->priv == PRV_M &&
701 access_type != MMU_INST_FETCH &&
702 get_field(env->mstatus, MSTATUS_MPRV) &&
703 get_field(env->mstatus, MSTATUS_MPV);
705 hs_mode_two_stage = env->priv == PRV_S &&
706 !riscv_cpu_virt_enabled(env) &&
707 access_type != MMU_INST_FETCH &&
708 get_field(env->hstatus, HSTATUS_SPRV) &&
709 get_field(env->hstatus, HSTATUS_SPV);
712 if (mode == PRV_M && access_type != MMU_INST_FETCH) {
713 if (get_field(env->mstatus, MSTATUS_MPRV)) {
714 mode = get_field(env->mstatus, MSTATUS_MPP);
718 if (riscv_cpu_virt_enabled(env) || m_mode_two_stage || hs_mode_two_stage) {
719 /* Two stage lookup */
720 ret = get_physical_address(env, &pa, &prot, address, access_type,
721 mmu_idx, true, true);
723 qemu_log_mask(CPU_LOG_MMU,
724 "%s 1st-stage address=%" VADDR_PRIx " ret %d physical "
725 TARGET_FMT_plx " prot %d\n",
726 __func__, address, ret, pa, prot);
728 if (ret != TRANSLATE_FAIL) {
729 /* Second stage lookup */
732 ret = get_physical_address(env, &pa, &prot, im_address,
733 access_type, mmu_idx, false, true);
735 qemu_log_mask(CPU_LOG_MMU,
736 "%s 2nd-stage address=%" VADDR_PRIx " ret %d physical "
737 TARGET_FMT_plx " prot %d\n",
738 __func__, im_address, ret, pa, prot);
740 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
741 (ret == TRANSLATE_SUCCESS) &&
742 !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) {
743 ret = TRANSLATE_PMP_FAIL;
746 if (ret != TRANSLATE_SUCCESS) {
748 * Guest physical address translation failed, this is a HS
751 first_stage_error = false;
752 env->guest_phys_fault_addr = (im_address |
754 (TARGET_PAGE_SIZE - 1))) >> 2;
758 /* Single stage lookup */
759 ret = get_physical_address(env, &pa, &prot, address, access_type,
760 mmu_idx, true, false);
762 qemu_log_mask(CPU_LOG_MMU,
763 "%s address=%" VADDR_PRIx " ret %d physical "
764 TARGET_FMT_plx " prot %d\n",
765 __func__, address, ret, pa, prot);
768 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
769 (ret == TRANSLATE_SUCCESS) &&
770 !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) {
771 ret = TRANSLATE_PMP_FAIL;
773 if (ret == TRANSLATE_PMP_FAIL) {
774 pmp_violation = true;
777 if (ret == TRANSLATE_SUCCESS) {
778 tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK,
779 prot, mmu_idx, TARGET_PAGE_SIZE);
784 raise_mmu_exception(env, address, access_type, pmp_violation, first_stage_error);
785 riscv_raise_exception(env, cs->exception_index, retaddr);
791 switch (access_type) {
793 cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT;
796 cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT;
799 cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT;
802 g_assert_not_reached();
804 env->badaddr = address;
805 cpu_loop_exit_restore(cs, retaddr);
812 * Adapted from Spike's processor_t::take_trap.
815 void riscv_cpu_do_interrupt(CPUState *cs)
817 #if !defined(CONFIG_USER_ONLY)
819 RISCVCPU *cpu = RISCV_CPU(cs);
820 CPURISCVState *env = &cpu->env;
821 bool force_hs_execp = riscv_cpu_force_hs_excep_enabled(env);
824 /* cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide
825 * so we mask off the MSB and separate into trap type and cause.
827 bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG);
828 target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK;
829 target_ulong deleg = async ? env->mideleg : env->medeleg;
830 target_ulong tval = 0;
833 /* set tval to badaddr for traps with address information */
835 case RISCV_EXCP_INST_GUEST_PAGE_FAULT:
836 case RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT:
837 case RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT:
838 force_hs_execp = true;
840 case RISCV_EXCP_INST_ADDR_MIS:
841 case RISCV_EXCP_INST_ACCESS_FAULT:
842 case RISCV_EXCP_LOAD_ADDR_MIS:
843 case RISCV_EXCP_STORE_AMO_ADDR_MIS:
844 case RISCV_EXCP_LOAD_ACCESS_FAULT:
845 case RISCV_EXCP_STORE_AMO_ACCESS_FAULT:
846 case RISCV_EXCP_INST_PAGE_FAULT:
847 case RISCV_EXCP_LOAD_PAGE_FAULT:
848 case RISCV_EXCP_STORE_PAGE_FAULT:
854 /* ecall is dispatched as one cause so translate based on mode */
855 if (cause == RISCV_EXCP_U_ECALL) {
856 assert(env->priv <= 3);
858 if (env->priv == PRV_M) {
859 cause = RISCV_EXCP_M_ECALL;
860 } else if (env->priv == PRV_S && riscv_cpu_virt_enabled(env)) {
861 cause = RISCV_EXCP_VS_ECALL;
862 } else if (env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) {
863 cause = RISCV_EXCP_S_ECALL;
864 } else if (env->priv == PRV_U) {
865 cause = RISCV_EXCP_U_ECALL;
870 trace_riscv_trap(env->mhartid, async, cause, env->pc, tval, cause < 23 ?
871 (async ? riscv_intr_names : riscv_excp_names)[cause] : "(unknown)");
873 if (env->priv <= PRV_S &&
874 cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) {
875 /* handle the trap in S-mode */
876 if (riscv_has_ext(env, RVH)) {
877 target_ulong hdeleg = async ? env->hideleg : env->hedeleg;
879 if (riscv_cpu_virt_enabled(env) && ((hdeleg >> cause) & 1) &&
881 /* Trap to VS mode */
882 } else if (riscv_cpu_virt_enabled(env)) {
883 /* Trap into HS mode, from virt */
884 riscv_cpu_swap_hypervisor_regs(env);
885 env->hstatus = set_field(env->hstatus, HSTATUS_SP2V,
886 get_field(env->hstatus, HSTATUS_SPV));
887 env->hstatus = set_field(env->hstatus, HSTATUS_SP2P,
888 get_field(env->mstatus, SSTATUS_SPP));
889 env->hstatus = set_field(env->hstatus, HSTATUS_SPV,
890 riscv_cpu_virt_enabled(env));
892 riscv_cpu_set_virt_enabled(env, 0);
893 riscv_cpu_set_force_hs_excep(env, 0);
895 /* Trap into HS mode */
896 env->hstatus = set_field(env->hstatus, HSTATUS_SP2V,
897 get_field(env->hstatus, HSTATUS_SPV));
898 env->hstatus = set_field(env->hstatus, HSTATUS_SP2P,
899 get_field(env->mstatus, SSTATUS_SPP));
900 env->hstatus = set_field(env->hstatus, HSTATUS_SPV,
901 riscv_cpu_virt_enabled(env));
906 s = set_field(s, MSTATUS_SPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ?
907 get_field(s, MSTATUS_SIE) : get_field(s, MSTATUS_UIE << env->priv));
908 s = set_field(s, MSTATUS_SPP, env->priv);
909 s = set_field(s, MSTATUS_SIE, 0);
911 env->scause = cause | ((target_ulong)async << (TARGET_LONG_BITS - 1));
913 env->sbadaddr = tval;
914 env->pc = (env->stvec >> 2 << 2) +
915 ((async && (env->stvec & 3) == 1) ? cause * 4 : 0);
916 riscv_cpu_set_mode(env, PRV_S);
918 /* handle the trap in M-mode */
919 if (riscv_has_ext(env, RVH)) {
920 if (riscv_cpu_virt_enabled(env)) {
921 riscv_cpu_swap_hypervisor_regs(env);
923 env->mstatus = set_field(env->mstatus, MSTATUS_MPV,
924 riscv_cpu_virt_enabled(env));
925 env->mstatus = set_field(env->mstatus, MSTATUS_MTL,
926 riscv_cpu_force_hs_excep_enabled(env));
928 /* Trapping to M mode, virt is disabled */
929 riscv_cpu_set_virt_enabled(env, 0);
930 riscv_cpu_set_force_hs_excep(env, 0);
934 s = set_field(s, MSTATUS_MPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ?
935 get_field(s, MSTATUS_MIE) : get_field(s, MSTATUS_UIE << env->priv));
936 s = set_field(s, MSTATUS_MPP, env->priv);
937 s = set_field(s, MSTATUS_MIE, 0);
939 env->mcause = cause | ~(((target_ulong)-1) >> async);
941 env->mbadaddr = tval;
942 env->pc = (env->mtvec >> 2 << 2) +
943 ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0);
944 riscv_cpu_set_mode(env, PRV_M);
947 /* NOTE: it is not necessary to yield load reservations here. It is only
948 * necessary for an SC from "another hart" to cause a load reservation
949 * to be yielded. Refer to the memory consistency model section of the
950 * RISC-V ISA Specification.
954 cs->exception_index = EXCP_NONE; /* mark handled to qemu */