When a G-stage fault is taken during a VS-stage page-table walk, riscv_cpu_do_interrupt() writes a pseudoinstruction to mtinst/htinst. This instruction says if the walk's access was a read (0x3000) or a write (0x3020) of the PTE [1]. Commit 366b4fba0fae made the A/D update check G-stage write permission, and sets the write bit when the trap cause is a store guest-page fault.
The trap cause is the wrong signal. The cause follows the original access [2], but the pseudoinstruction describes the implicit access that failed. The two disagree in two cases: original access failed access Spike reports QEMU reports load or fetch A/D write 0x3020 0x3000 store PTE read 0x3000 0x3020 They agree only for a store that fails on the A/D write, which is the case 366b4fba0fae was tested with. Record in the CPU state that the G-stage fault came from the A/D write, and use that for both the data and the instruction tinst sites. Clear it on entry to riscv_cpu_tlb_fill() and after the trap. [1] Privileged ISA v1.12, 8.6.3, Table 8.12 [2] Privileged ISA v1.12, 8.5.1 "Guest Physical Address Translation" https://github.com/riscv/riscv-isa-manual/releases/tag/Priv-v1.12 Fixes: 366b4fba0fae ("target/riscv: check G-stage write permission for VS-stage A/D updates") Signed-off-by: tewaro <[email protected]> --- target/riscv/cpu.h | 1 + target/riscv/tcg/cpu_helper.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 49a1186bdb..d56566d8c8 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -445,6 +445,7 @@ struct CPUArchState { * address translation for the VS-stage page table walk. */ bool two_stage_indirect_lookup; + bool two_stage_indirect_write; uint32_t scounteren; uint32_t mcounteren; diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 58e98e975c..063525cfc5 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -1544,6 +1544,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, if (fault_pte_addr) { *fault_pte_addr = pte_gpa >> 2; } + env->two_stage_indirect_write = true; return TRANSLATE_G_STAGE_FAIL; } } @@ -1785,6 +1786,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, hwaddr tlb_size = TARGET_PAGE_SIZE; env->guest_phys_fault_addr = 0; + env->two_stage_indirect_write = false; qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n", __func__, address, access_type, mmu_idx); @@ -2233,7 +2235,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) */ tinst = (riscv_cpu_xlen(env) == 32) ? 0x00002000 : 0x00003000; - if (cause == RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT) { + if (env->two_stage_indirect_write) { tinst |= 0x20; } } else { @@ -2256,6 +2258,9 @@ void riscv_cpu_do_interrupt(CPUState *cs) * doing VS-stage page table walk. */ tinst = (riscv_cpu_xlen(env) == 32) ? 0x00002000 : 0x00003000; + if (env->two_stage_indirect_write) { + tinst |= 0x20; + } } break; case RISCV_EXCP_ILLEGAL_INST: @@ -2506,6 +2511,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->two_stage_lookup = false; env->two_stage_indirect_lookup = false; + env->two_stage_indirect_write = false; } #endif /* !CONFIG_USER_ONLY */ -- 2.54.0 (Apple Git-157)
