Skip to contentSkip to conversation

RISC-V: KVM: Redirect software-check exceptions without henvcfg.LPE/SSE

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: Redirect software-check exceptions without henvcfg.LPE/SSE

Landing pads for VU-mode are enabled by the guest's senvcfg.LPE. henvcfg.LPE and henvcfg.SSE only cover VS-mode, yet KVM redirects software-check exceptions to the guest only when one of them is set. When a guest enables landing pads for its processes but not for its kernel, which a Linux guest does on a vCPU with Zicfilp but without Zicfiss, hedeleg does not delegate the exception, a landing-pad violation in any guest process is taken in HS-mode, the EXC_SOFTWARE_CHECK case leaves ret at -EFAULT, and KVM_RUN fails. An unprivileged process in the guest can thus stop the whole VM.

kvm_riscv_vcpu_trap_redirect() has the same condition on forwarding SPELP, so a trap redirected from VU-mode loses the ELP state of such a guest.

Redirect software-check exceptions from the guest unconditionally and always forward SPELP. Without Zicfilp, SPELP reads as zero.

Fixes 864260d71d82 ("RISC-V: KVM: Handle software-check exits for VCPU")

---
Tested in a nested setup: QEMU 11.1.91 (virt, aia=aplic-imsic, cpu max with zicfilp=true, zicfiss=false) as the machine, this tree as the L1 host with KVM, kvmtool, and the same kernel as the guest. Without Zicfiss the guest enables landing pads only for its processes. A guest process enables them with prctl(PR_SET_CFI) and jumps through a6 to a target that is not an lpad.

Before, KVM_RUN fails and kvmtool exits:

kvm [66]: VCPU exit error -14
kvm [66]: SEPC=0x1075c SSTATUS=0x8000000200806620 HSTATUS=0x200201080 kvm [66]: SCAUSE=0x12 STVAL=0x2 HTVAL=0x0 HTINST=0x0

After, the guest kernel gets the exception, the process is killed with SIGSEGV (si_code SEGV_CPERR), and the VM keeps running.

The SPELP part is not covered by this test. Not tested on hardware; the test program is available on request.

Assisted-by Claude:claude-opus-5-5
 arch/riscv/kvm/vcpu_exit.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
arch/riscv/kvm/vcpu_exit.c +9 -8
index cab46eb32e9d..e1c440c7d441 100644
@@ -175,12 +175,14 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
 	/* Clear Guest SSTATUS.SIE bit */
 	vsstatus &= ~SR_SIE;
 
-	/* Change Guest SSTATUS.SPELP bit */
-	if (vcpu->arch.cfg.henvcfg & ENVCFG_LPE) {
-		vsstatus &= ~SR_SPELP;
-		vsstatus |= vcpu->arch.guest_context.sstatus & SR_SPELP;
-		vcpu->arch.guest_context.sstatus &= ~SR_SPELP;
-	}
+	/*
+	 * Change Guest SSTATUS.SPELP bit. Landing pads for VU-mode are
+	 * enabled by the guest's senvcfg.LPE, so this cannot depend on
+	 * henvcfg.LPE. Without Zicfilp, SPELP reads as zero.
+	 */
+	vsstatus &= ~SR_SPELP;
+	vsstatus |= vcpu->arch.guest_context.sstatus & SR_SPELP;
+	vcpu->arch.guest_context.sstatus &= ~SR_SPELP;
 
 	/* Update Guest SSTATUS */
 	ncsr_write(CSR_VSSTATUS, vsstatus);
@@ -275,8 +277,7 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
 		ret = 0;
 		break;
 	case EXC_SOFTWARE_CHECK:
-		if (vcpu->arch.cfg.henvcfg & (ENVCFG_LPE | ENVCFG_SSE))
-			ret = vcpu_redirect(vcpu, trap);
+		ret = vcpu_redirect(vcpu, trap);
 		break;
 	default:
 		break;

base-commit: bd68956997dddcc3b1a7dd2fadadf584529027a1

Signature · 1 line
2.43.0