Skip to contentSkip to conversation

RISC-V: KVM: Do not emulate CSR accesses from VU-mode as VS-mode

PATCH New Zihui Guo 1 participant 1 message Dec 1

Subsystems (3)
lore
Share & tools
lore

Zihui Guo

author
#0 lore raw

PATCH 1/1 RISC-V: KVM: Do not emulate CSR accesses from VU-mode as VS-mode

A virtual-instruction trap taken from VU-mode is emulated as if it had come from VS-mode. kvm_riscv_vcpu_exit() only checks hstatus.SPV, and the CSR emulation never looks at hstatus.SPVP. Two groups of emulated CSRs are reachable from a guest process this way:

- sireg and stopei. "csrrw x0, stopei, x0" in guest user mode claims and drops the top pending guest external interrupt, and sireg reads or writes whichever IMSIC or iprio register the guest kernel last selected in siselect.

- cycle, instret and hpmcounter*. hcounteren only has TM set, so these always trap, and a guest process gets a counter value even when the guest kernel has cleared the bit in scounteren.

Both accesses are illegal in U-mode, and the privileged spec says that a hypervisor not emulating the trapping instruction should convert the virtual-instruction trap into an illegal-instruction exception for the guest. Do that for accesses from VU-mode to CSRs that are not user-level, and to counters that the guest's scounteren does not enable.

Fixes a9ac6c37521f ("RISC-V: KVM: Implement trap & emulate for hpmcounters")
Fixes 2f4d58f7635a ("RISC-V: KVM: Virtualize per-HART AIA CSRs")

---
Tested in a nested setup: QEMU 11.1.91 (virt, aia=aplic-imsic, cpu max) as the machine, this tree as the L1 host with KVM, kvmtool, and the same kernel as the guest. A static program in the guest, run as a normal process with kernel.perf_user_access=0:

                                       before          after
  csrr cycle   (cycles event open)     counter value   SIGILL
  csrr instret (instructions event)    counter value   SIGILL
  csrr sireg                           0x0, no trap    SIGILL
  csrr stopei                          0x0, no trap    SIGILL

Without an open perf event, cycle and instret already gave SIGILL before the patch because KVM had no counter to read. stopei was only read, so no interrupt was claimed by the test. Not tested on hardware; the test program is available on request.

WFI and WRS from VU-mode have the same problem. "RISC-V: KVM: Redirect nested WFI and WRS traps" in the nested series already handles them, so they are left out here.

Assisted-by Claude:claude-opus-5-5
 arch/riscv/kvm/vcpu_insn.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/riscv/kvm/vcpu_insn.c +23 -0
index af752275c210..12c450143858 100644
@@ -145,6 +145,24 @@ int kvm_riscv_vcpu_csr_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
 	return 0;
 }
 
+/*
+ * A virtual-instruction trap from VU-mode must not be emulated with the
+ * privilege of VS-mode. VU-mode can only access user-level CSRs, and the
+ * counters only when the guest has enabled them in scounteren.
+ */
+static bool csr_allowed_from_vu(struct kvm_vcpu *vcpu, unsigned int csr_num)
+{
+	/* Bits [9:8] of the CSR number hold the lowest privilege level */
+	if (csr_num & 0x300)
+		return false;
+
+	if ((csr_num >= CSR_CYCLE && csr_num <= CSR_HPMCOUNTER31) ||
+	    (csr_num >= CSR_CYCLEH && csr_num <= CSR_HPMCOUNTER31H))
+		return vcpu->arch.guest_csr.scounteren & BIT(csr_num & 0x1f);
+
+	return true;
+}
+
 static int csr_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn)
 {
 	int i, rc = KVM_INSN_ILLEGAL_TRAP;
@@ -184,6 +202,11 @@ static int csr_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn)
 		return rc;
 	}
 
+	/* Accesses from VU-mode get the privilege of VU-mode */
+	if (!(vcpu->arch.guest_context.hstatus & HSTATUS_SPVP) &&
+	    !csr_allowed_from_vu(vcpu, csr_num))
+		return KVM_INSN_ILLEGAL_TRAP;
+
 	/* Save instruction decode info */
 	vcpu->arch.csr_decode.insn = insn;
 	vcpu->arch.csr_decode.return_handled = 0;

base-commit: bd68956997dddcc3b1a7dd2fadadf584529027a1

Signature · 1 line
2.43.0