Skip to contentSkip to conversation

KVM: arm64: Mask SErrors in a protected vCPU's host copy at first run

PATCH In review Fuad Tabba 2 participants 3 messages Oct 6

Subsystems (2)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

PATCH v2 1/1 KVM: arm64: Mask SErrors in a protected vCPU's host copy at first run

The host's copy of a protected vCPU has SErrors masked from reset, so a host-injected SError is pended through HCR_EL2.VSE and the guest takes it when it unmasks SErrors. However, the VMM can unmask SErrors in that copy before the first run, through PSTATE.A or SCTLR2_EL1.NMEA. KVM then emulates the SError's entry on the host copy, and the guest never takes it as an SError. Exits that copy PSTATE out set PSTATE.A again, but nothing clears NMEA. With NMEA set, an SError injected after a KVM_RUN that completes an MMIO access but returns before entering the guest also trips WARN_ON(INCREMENT_PC).

Mask SErrors in the host copy when the first run creates the hyp vCPU, after which KVM_SET_ONE_REG is rejected. An SError injected before then is still emulated, but only writes registers the VMM can set itself.

Fixes 872383bd12e11 ("KVM: arm64: Add per-EC entry/exit state marshalling for protected guests")

---
v2:
- Set PSTATE.A and clear SCTLR2_EL1.NMEA in the host copy when the hyp vCPU is created, instead of testing vcpu_is_protected() in kvm_inject_serror_esr() (Marc).

Applies on kvmarm/next. A follow-up to "KVM: arm64: Confine protected VM vCPU state to EL2" [1], from Sashiko's review of its v4 patch 12.

v1: https://lore.kernel.org/r/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/

 arch/arm64/kvm/pkvm.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/kvm/pkvm.c +6 -0
index d4822d8bb16b8..49973c689789f 100644
@@ -191,12 +191,18 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
 	/*
 	 * Mirror EL2's seeding of power_state from mp_state. The hyp vCPU is
 	 * published, so take mp_state_lock against kvm_psci_vcpu_on().
+	 *
+	 * EL2 never reads the VMM's PSTATE or SCTLR2_EL1: undo any unmasking
+	 * so that a host SError is pended through HCR_EL2.VSE.
 	 */
 	if (vcpu_is_protected(vcpu)) {
 		spin_lock(&vcpu->arch.mp_state_lock);
 		if (kvm_arm_vcpu_stopped(vcpu))
 			WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_UNINITIALIZED);
 		spin_unlock(&vcpu->arch.mp_state_lock);
+
+		*vcpu_cpsr(vcpu) |= PSR_A_BIT;
+		__vcpu_rmw_sys_reg(vcpu, SCTLR2_EL1, &=, ~SCTLR2_EL1_NMEA);
 	}
 
 	vcpu_set_flag(vcpu, VCPU_PKVM_FINALIZED);

base-commit: fa22cd9947fc245d71bc40482f7f78eb0d5a4af0

Signature · 1 line
2.39.5
maintainer
#1 lore raw

Hi Fuad,

Quoted 44 lines · Fuad Tabba

The host's copy of a protected vCPU has SErrors masked from reset, so a host-injected SError is pended through HCR_EL2.VSE and the guest takes it when it unmasks SErrors. However, the VMM can unmask SErrors in that copy before the first run, through PSTATE.A or SCTLR2_EL1.NMEA. KVM then emulates the SError's entry on the host copy, and the guest never takes it as an SError. Exits that copy PSTATE out set PSTATE.A again, but nothing clears NMEA. With NMEA set, an SError injected after a KVM_RUN that completes an MMIO access but returns before entering the guest also trips WARN_ON(INCREMENT_PC).

Mask SErrors in the host copy when the first run creates the hyp vCPU, after which KVM_SET_ONE_REG is rejected. An SError injected before then is still emulated, but only writes registers the VMM can set itself.

Fixes 872383bd12e11 ("KVM: arm64: Add per-EC entry/exit state marshalling for protected guests")

---
v2:
- Set PSTATE.A and clear SCTLR2_EL1.NMEA in the host copy when the hyp vCPU is created, instead of testing vcpu_is_protected() in kvm_inject_serror_esr() (Marc).

Applies on kvmarm/next. A follow-up to "KVM: arm64: Confine protected VM vCPU state to EL2" [1], from Sashiko's review of its v4 patch 12.

v1: https://lore.kernel.org/r/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/

 arch/arm64/kvm/pkvm.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/kvm/pkvm.c +3 -0
index d4822d8bb16b8..49973c689789f 100644
@@ -191,12 +191,18 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
 	/*
 	 * Mirror EL2's seeding of power_state from mp_state. The hyp vCPU is
 	 * published, so take mp_state_lock against kvm_psci_vcpu_on().
+	 *
+	 * EL2 never reads the VMM's PSTATE or SCTLR2_EL1: undo any unmasking
+	 * so that a host SError is pended through HCR_EL2.VSE.

arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu) · line 206

Quoted 10 lines
arch/arm64/kvm/pkvm.c +3 -0
 	 */
 	if (vcpu_is_protected(vcpu)) {
 		spin_lock(&vcpu->arch.mp_state_lock);
 		if (kvm_arm_vcpu_stopped(vcpu))
 			WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_UNINITIALIZED);
 		spin_unlock(&vcpu->arch.mp_state_lock);
+
+		*vcpu_cpsr(vcpu) |= PSR_A_BIT;
+		__vcpu_rmw_sys_reg(vcpu, SCTLR2_EL1, &=, ~SCTLR2_EL1_NMEA);
 	}

Urgh, I hadn't realized that PSTATE.A is still modifiable by userspace prior to KVM_RUN. In that case, I would prefer the vcpu_has_nv() approach that I had recommended as a backup.

FWIW, since pVMs do not implement FEAT_SCTLR2 it should not be able to write to the register at all.

Thanks,
Oliver

author reviewer
#2 lore raw

Hi Oliver,

On Tue, 6 Oct 2026 at 15:15, Oliver Upton <[email protected]> wrote:
[...]

Quoted 8 lines

arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu) · line 206

Quoted 4 lines
arch/arm64/kvm/pkvm.c +3 -0
+
+             *vcpu_cpsr(vcpu) |= PSR_A_BIT;
+             __vcpu_rmw_sys_reg(vcpu, SCTLR2_EL1, &=, ~SCTLR2_EL1_NMEA);
      }

Urgh, I hadn't realized that PSTATE.A is still modifiable by userspace prior to KVM_RUN. In that case, I would prefer the vcpu_has_nv() approach that I had recommended as a backup.

Sure. I'll respin with that.

Cheers,
/fuad

Quoted 5 lines

FWIW, since pVMs do not implement FEAT_SCTLR2 it should not be able to write to the register at all.

Thanks,
Oliver