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) {
108 void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env)
110 target_ulong mstatus_mask = MSTATUS_MXR | MSTATUS_SUM | MSTATUS_FS |
111 MSTATUS_SPP | MSTATUS_SPIE | MSTATUS_SIE;
112 bool current_virt = riscv_cpu_virt_enabled(env);
114 g_assert(riscv_has_ext(env, RVH));
116 #if defined(TARGET_RISCV64)
117 mstatus_mask |= MSTATUS64_UXL;
121 /* Current V=1 and we are about to change to V=0 */
122 env->vsstatus = env->mstatus & mstatus_mask;
123 env->mstatus &= ~mstatus_mask;
124 env->mstatus |= env->mstatus_hs;
126 env->vstvec = env->stvec;
127 env->stvec = env->stvec_hs;
129 env->vsscratch = env->sscratch;
130 env->sscratch = env->sscratch_hs;
132 env->vsepc = env->sepc;
133 env->sepc = env->sepc_hs;
135 env->vscause = env->scause;
136 env->scause = env->scause_hs;
138 env->vstval = env->sbadaddr;
139 env->sbadaddr = env->stval_hs;
141 env->vsatp = env->satp;
142 env->satp = env->satp_hs;
144 /* Current V=0 and we are about to change to V=1 */
145 env->mstatus_hs = env->mstatus & mstatus_mask;
146 env->mstatus &= ~mstatus_mask;
147 env->mstatus |= env->vsstatus;
149 env->stvec_hs = env->stvec;
150 env->stvec = env->vstvec;
152 env->sscratch_hs = env->sscratch;
153 env->sscratch = env->vsscratch;
155 env->sepc_hs = env->sepc;
156 env->sepc = env->vsepc;
158 env->scause_hs = env->scause;
159 env->scause = env->vscause;
161 env->stval_hs = env->sbadaddr;
162 env->sbadaddr = env->vstval;
164 env->satp_hs = env->satp;
165 env->satp = env->vsatp;
169 bool riscv_cpu_virt_enabled(CPURISCVState *env)
171 if (!riscv_has_ext(env, RVH)) {
175 return get_field(env->virt, VIRT_ONOFF);
178 void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable)
180 if (!riscv_has_ext(env, RVH)) {
184 env->virt = set_field(env->virt, VIRT_ONOFF, enable);
187 bool riscv_cpu_force_hs_excep_enabled(CPURISCVState *env)
189 if (!riscv_has_ext(env, RVH)) {
193 return get_field(env->virt, FORCE_HS_EXCEP);
196 void riscv_cpu_set_force_hs_excep(CPURISCVState *env, bool enable)
198 if (!riscv_has_ext(env, RVH)) {
202 env->virt = set_field(env->virt, FORCE_HS_EXCEP, enable);
205 int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts)
207 CPURISCVState *env = &cpu->env;
208 if (env->miclaim & interrupts) {
211 env->miclaim |= interrupts;
216 uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value)
218 CPURISCVState *env = &cpu->env;
219 CPUState *cs = CPU(cpu);
220 uint32_t old = env->mip;
223 if (!qemu_mutex_iothread_locked()) {
225 qemu_mutex_lock_iothread();
228 env->mip = (env->mip & ~mask) | (value & mask);
231 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
233 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
237 qemu_mutex_unlock_iothread();
243 void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv)
245 if (newpriv > PRV_M) {
246 g_assert_not_reached();
248 if (newpriv == PRV_H) {
251 /* tlb_flush is unnecessary as mode is contained in mmu_idx */
255 * Clear the load reservation - otherwise a reservation placed in one
256 * context/process can be used by another, resulting in an SC succeeding
257 * incorrectly. Version 2.2 of the ISA specification explicitly requires
258 * this behaviour, while later revisions say that the kernel "should" use
259 * an SC instruction to force the yielding of a load reservation on a
260 * preemptive context switch. As a result, do both.
265 /* get_physical_address - get the physical address for this virtual address
267 * Do a page table walk to obtain the physical address corresponding to a
268 * virtual address. Returns 0 if the translation was successful
270 * Adapted from Spike's mmu_t::translate and mmu_t::walk
273 static int get_physical_address(CPURISCVState *env, hwaddr *physical,
274 int *prot, target_ulong addr,
275 int access_type, int mmu_idx)
277 /* NOTE: the env->pc value visible here will not be
278 * correct, but the value visible to the exception handler
279 * (riscv_cpu_do_interrupt) is correct */
281 MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
284 if (mode == PRV_M && access_type != MMU_INST_FETCH) {
285 if (get_field(env->mstatus, MSTATUS_MPRV)) {
286 mode = get_field(env->mstatus, MSTATUS_MPP);
290 if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) {
292 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
293 return TRANSLATE_SUCCESS;
299 int levels, ptidxbits, ptesize, vm, sum;
300 int mxr = get_field(env->mstatus, MSTATUS_MXR);
302 if (env->priv_ver >= PRIV_VERSION_1_10_0) {
303 base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT;
304 sum = get_field(env->mstatus, MSTATUS_SUM);
305 vm = get_field(env->satp, SATP_MODE);
308 levels = 2; ptidxbits = 10; ptesize = 4; break;
310 levels = 3; ptidxbits = 9; ptesize = 8; break;
312 levels = 4; ptidxbits = 9; ptesize = 8; break;
314 levels = 5; ptidxbits = 9; ptesize = 8; break;
317 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
318 return TRANSLATE_SUCCESS;
320 g_assert_not_reached();
323 base = (hwaddr)(env->sptbr) << PGSHIFT;
324 sum = !get_field(env->mstatus, MSTATUS_PUM);
325 vm = get_field(env->mstatus, MSTATUS_VM);
328 levels = 2; ptidxbits = 10; ptesize = 4; break;
330 levels = 3; ptidxbits = 9; ptesize = 8; break;
332 levels = 4; ptidxbits = 9; ptesize = 8; break;
335 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
336 return TRANSLATE_SUCCESS;
338 g_assert_not_reached();
342 CPUState *cs = env_cpu(env);
343 int va_bits = PGSHIFT + levels * ptidxbits;
344 target_ulong mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1;
345 target_ulong masked_msbs = (addr >> (va_bits - 1)) & mask;
346 if (masked_msbs != 0 && masked_msbs != mask) {
347 return TRANSLATE_FAIL;
350 int ptshift = (levels - 1) * ptidxbits;
353 #if !TCG_OVERSIZED_GUEST
356 for (i = 0; i < levels; i++, ptshift -= ptidxbits) {
357 target_ulong idx = (addr >> (PGSHIFT + ptshift)) &
358 ((1 << ptidxbits) - 1);
360 /* check that physical address of PTE is legal */
361 hwaddr pte_addr = base + idx * ptesize;
363 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
364 !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong),
365 1 << MMU_DATA_LOAD, PRV_S)) {
366 return TRANSLATE_PMP_FAIL;
369 #if defined(TARGET_RISCV32)
370 target_ulong pte = address_space_ldl(cs->as, pte_addr, attrs, &res);
371 #elif defined(TARGET_RISCV64)
372 target_ulong pte = address_space_ldq(cs->as, pte_addr, attrs, &res);
374 if (res != MEMTX_OK) {
375 return TRANSLATE_FAIL;
378 hwaddr ppn = pte >> PTE_PPN_SHIFT;
380 if (!(pte & PTE_V)) {
382 return TRANSLATE_FAIL;
383 } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
384 /* Inner PTE, continue walking */
385 base = ppn << PGSHIFT;
386 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
387 /* Reserved leaf PTE flags: PTE_W */
388 return TRANSLATE_FAIL;
389 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) {
390 /* Reserved leaf PTE flags: PTE_W + PTE_X */
391 return TRANSLATE_FAIL;
392 } else if ((pte & PTE_U) && ((mode != PRV_U) &&
393 (!sum || access_type == MMU_INST_FETCH))) {
394 /* User PTE flags when not U mode and mstatus.SUM is not set,
395 or the access type is an instruction fetch */
396 return TRANSLATE_FAIL;
397 } else if (!(pte & PTE_U) && (mode != PRV_S)) {
398 /* Supervisor PTE flags when not S mode */
399 return TRANSLATE_FAIL;
400 } else if (ppn & ((1ULL << ptshift) - 1)) {
402 return TRANSLATE_FAIL;
403 } else if (access_type == MMU_DATA_LOAD && !((pte & PTE_R) ||
404 ((pte & PTE_X) && mxr))) {
405 /* Read access check failed */
406 return TRANSLATE_FAIL;
407 } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) {
408 /* Write access check failed */
409 return TRANSLATE_FAIL;
410 } else if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) {
411 /* Fetch access check failed */
412 return TRANSLATE_FAIL;
414 /* if necessary, set accessed and dirty bits. */
415 target_ulong updated_pte = pte | PTE_A |
416 (access_type == MMU_DATA_STORE ? PTE_D : 0);
418 /* Page table updates need to be atomic with MTTCG enabled */
419 if (updated_pte != pte) {
421 * - if accessed or dirty bits need updating, and the PTE is
422 * in RAM, then we do so atomically with a compare and swap.
423 * - if the PTE is in IO space or ROM, then it can't be updated
424 * and we return TRANSLATE_FAIL.
425 * - if the PTE changed by the time we went to update it, then
426 * it is no longer valid and we must re-walk the page table.
429 hwaddr l = sizeof(target_ulong), addr1;
430 mr = address_space_translate(cs->as, pte_addr,
431 &addr1, &l, false, MEMTXATTRS_UNSPECIFIED);
432 if (memory_region_is_ram(mr)) {
433 target_ulong *pte_pa =
434 qemu_map_ram_ptr(mr->ram_block, addr1);
435 #if TCG_OVERSIZED_GUEST
436 /* MTTCG is not enabled on oversized TCG guests so
437 * page table updates do not need to be atomic */
438 *pte_pa = pte = updated_pte;
440 target_ulong old_pte =
441 atomic_cmpxchg(pte_pa, pte, updated_pte);
442 if (old_pte != pte) {
449 /* misconfigured PTE in ROM (AD bits are not preset) or
450 * PTE is in IO space and can't be updated atomically */
451 return TRANSLATE_FAIL;
455 /* for superpage mappings, make a fake leaf PTE for the TLB's
457 target_ulong vpn = addr >> PGSHIFT;
458 *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT;
460 /* set permissions on the TLB entry */
461 if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) {
467 /* add write permission on stores or if the page is already dirty,
468 so that we TLB miss on later writes to update the dirty bit */
470 (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
473 return TRANSLATE_SUCCESS;
476 return TRANSLATE_FAIL;
479 static void raise_mmu_exception(CPURISCVState *env, target_ulong address,
480 MMUAccessType access_type, bool pmp_violation)
482 CPUState *cs = env_cpu(env);
483 int page_fault_exceptions =
484 (env->priv_ver >= PRIV_VERSION_1_10_0) &&
485 get_field(env->satp, SATP_MODE) != VM_1_10_MBARE &&
487 switch (access_type) {
489 cs->exception_index = page_fault_exceptions ?
490 RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT;
493 cs->exception_index = page_fault_exceptions ?
494 RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT;
497 cs->exception_index = page_fault_exceptions ?
498 RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
501 g_assert_not_reached();
503 env->badaddr = address;
506 hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
508 RISCVCPU *cpu = RISCV_CPU(cs);
511 int mmu_idx = cpu_mmu_index(&cpu->env, false);
513 if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0, mmu_idx)) {
519 void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
520 vaddr addr, unsigned size,
521 MMUAccessType access_type,
522 int mmu_idx, MemTxAttrs attrs,
523 MemTxResult response, uintptr_t retaddr)
525 RISCVCPU *cpu = RISCV_CPU(cs);
526 CPURISCVState *env = &cpu->env;
528 if (access_type == MMU_DATA_STORE) {
529 cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
531 cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT;
535 riscv_raise_exception(&cpu->env, cs->exception_index, retaddr);
538 void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
539 MMUAccessType access_type, int mmu_idx,
542 RISCVCPU *cpu = RISCV_CPU(cs);
543 CPURISCVState *env = &cpu->env;
544 switch (access_type) {
546 cs->exception_index = RISCV_EXCP_INST_ADDR_MIS;
549 cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS;
552 cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS;
555 g_assert_not_reached();
558 riscv_raise_exception(env, cs->exception_index, retaddr);
562 bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
563 MMUAccessType access_type, int mmu_idx,
564 bool probe, uintptr_t retaddr)
566 RISCVCPU *cpu = RISCV_CPU(cs);
567 CPURISCVState *env = &cpu->env;
568 #ifndef CONFIG_USER_ONLY
571 bool pmp_violation = false;
572 int ret = TRANSLATE_FAIL;
575 qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
576 __func__, address, access_type, mmu_idx);
578 ret = get_physical_address(env, &pa, &prot, address, access_type, mmu_idx);
580 if (mode == PRV_M && access_type != MMU_INST_FETCH) {
581 if (get_field(env->mstatus, MSTATUS_MPRV)) {
582 mode = get_field(env->mstatus, MSTATUS_MPP);
586 qemu_log_mask(CPU_LOG_MMU,
587 "%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx
588 " prot %d\n", __func__, address, ret, pa, prot);
590 if (riscv_feature(env, RISCV_FEATURE_PMP) &&
591 (ret == TRANSLATE_SUCCESS) &&
592 !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) {
593 ret = TRANSLATE_PMP_FAIL;
595 if (ret == TRANSLATE_PMP_FAIL) {
596 pmp_violation = true;
598 if (ret == TRANSLATE_SUCCESS) {
599 tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK,
600 prot, mmu_idx, TARGET_PAGE_SIZE);
605 raise_mmu_exception(env, address, access_type, pmp_violation);
606 riscv_raise_exception(env, cs->exception_index, retaddr);
609 switch (access_type) {
611 cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT;
614 cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT;
617 cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT;
620 g_assert_not_reached();
622 env->badaddr = address;
623 cpu_loop_exit_restore(cs, retaddr);
630 * Adapted from Spike's processor_t::take_trap.
633 void riscv_cpu_do_interrupt(CPUState *cs)
635 #if !defined(CONFIG_USER_ONLY)
637 RISCVCPU *cpu = RISCV_CPU(cs);
638 CPURISCVState *env = &cpu->env;
640 /* cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide
641 * so we mask off the MSB and separate into trap type and cause.
643 bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG);
644 target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK;
645 target_ulong deleg = async ? env->mideleg : env->medeleg;
646 target_ulong tval = 0;
648 static const int ecall_cause_map[] = {
649 [PRV_U] = RISCV_EXCP_U_ECALL,
650 [PRV_S] = RISCV_EXCP_S_ECALL,
651 [PRV_H] = RISCV_EXCP_VS_ECALL,
652 [PRV_M] = RISCV_EXCP_M_ECALL
656 /* set tval to badaddr for traps with address information */
658 case RISCV_EXCP_INST_GUEST_PAGE_FAULT:
659 case RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT:
660 case RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT:
661 case RISCV_EXCP_INST_ADDR_MIS:
662 case RISCV_EXCP_INST_ACCESS_FAULT:
663 case RISCV_EXCP_LOAD_ADDR_MIS:
664 case RISCV_EXCP_STORE_AMO_ADDR_MIS:
665 case RISCV_EXCP_LOAD_ACCESS_FAULT:
666 case RISCV_EXCP_STORE_AMO_ACCESS_FAULT:
667 case RISCV_EXCP_INST_PAGE_FAULT:
668 case RISCV_EXCP_LOAD_PAGE_FAULT:
669 case RISCV_EXCP_STORE_PAGE_FAULT:
675 /* ecall is dispatched as one cause so translate based on mode */
676 if (cause == RISCV_EXCP_U_ECALL) {
677 assert(env->priv <= 3);
678 cause = ecall_cause_map[env->priv];
682 trace_riscv_trap(env->mhartid, async, cause, env->pc, tval, cause < 23 ?
683 (async ? riscv_intr_names : riscv_excp_names)[cause] : "(unknown)");
685 if (env->priv <= PRV_S &&
686 cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) {
687 /* handle the trap in S-mode */
688 target_ulong s = env->mstatus;
689 s = set_field(s, MSTATUS_SPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ?
690 get_field(s, MSTATUS_SIE) : get_field(s, MSTATUS_UIE << env->priv));
691 s = set_field(s, MSTATUS_SPP, env->priv);
692 s = set_field(s, MSTATUS_SIE, 0);
694 env->scause = cause | ((target_ulong)async << (TARGET_LONG_BITS - 1));
696 env->sbadaddr = tval;
697 env->pc = (env->stvec >> 2 << 2) +
698 ((async && (env->stvec & 3) == 1) ? cause * 4 : 0);
699 riscv_cpu_set_mode(env, PRV_S);
701 /* handle the trap in M-mode */
702 target_ulong s = env->mstatus;
703 s = set_field(s, MSTATUS_MPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ?
704 get_field(s, MSTATUS_MIE) : get_field(s, MSTATUS_UIE << env->priv));
705 s = set_field(s, MSTATUS_MPP, env->priv);
706 s = set_field(s, MSTATUS_MIE, 0);
708 env->mcause = cause | ~(((target_ulong)-1) >> async);
710 env->mbadaddr = tval;
711 env->pc = (env->mtvec >> 2 << 2) +
712 ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0);
713 riscv_cpu_set_mode(env, PRV_M);
716 /* NOTE: it is not necessary to yield load reservations here. It is only
717 * necessary for an SC from "another hart" to cause a load reservation
718 * to be yielded. Refer to the memory consistency model section of the
719 * RISC-V ISA Specification.
723 cs->exception_index = EXCP_NONE; /* mark handled to qemu */