Skip to contentSkip to conversation

KVM: arm64: Fix inverted E2H0 check for HCR_EL2.NV1

PATCH Superseded Colton Lewis 3 participants 8 messages Nov 16 – Nov 18

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover KVM: arm64: Fix inverted E2H0 check for HCR_EL2.NV1

When commit b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits") migrated HCR_EL2 fixed-bit masking to hcr_feat_map, feat_nv2_e2h0_ni() was written as !kvm_has_feat(kvm, FEAT_E2H0). Because ID_AA64MMFR4_EL1.E2H0 is a signed 4-bit field where IMP is 0, NI is -1, and NI_NV1 is -2, !kvm_has_feat(kvm, FEAT_E2H0) tests E2H0 < 0 and returns true for NI_NV1 (-2), leaving HCR_EL2.NV1 writable instead of RES0 for VHE-only nested guests (and conversely treating HCR_EL2.NV1 as RES0 when E2H0 is IMP).

Patch 1 restores the check in feat_nv2_e2h0_ni() to kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI) (E2H0 >= -1) so that HCR_EL2.NV1 is implemented for IMP and NI and forced to RES0 for NI_NV1. Patch 2 extends the arm64 hello_el2 selftest to verify that HCR_EL2.E2H remains RES1 and HCR_EL2.NV1 remains RES0 across both guest EL2 writes and userspace KVM_SET_ONE_REG writes when E2H0 is NI_NV1.

Colton Lewis (2):
KVM: arm64: Fix inverted E2H0 check for HCR_EL2.NV1
KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1

 arch/arm64/kvm/config.c                       |  3 +-
 tools/testing/selftests/kvm/arm64/hello_el2.c | 34 ++++++++++++++++---
 2 files changed, 31 insertions(+), 6 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#1 lore raw

PATCH 1/2 KVM: arm64: Fix inverted E2H0 check for HCR_EL2.NV1

ID_AA64MMFR4_EL1.E2H0 is a signed 4-bit field where:
- 0b0000 (IMP, 0): FEAT_E2H0 is implemented.
- 0b1111 (NI, -1): FEAT_E2H0 is not implemented, except HCR_EL2.NV1 is implemented.
- 0b1110 (NI_NV1, -2): Neither HCR_EL2.E2H == 0 nor HCR_EL2.NV1 is implemented, and HCR_EL2.NV1 is RES0.

Prior to commit b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits"), kvm_init_nv_sysregs() marked HCR_EL2.NV1 as RES0 when !kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI), i.e., when the signed E2H0 field is less than NI (-1), which matches NI_NV1 (-2).

When converting HCR_EL2 to the feature map, feat_nv2_e2h0_ni() was written as !kvm_has_feat(kvm, FEAT_E2H0), where FEAT_E2H0 checks E2H0 >= IMP (0). This inverted the condition: feat_nv2_e2h0_ni() evaluates to true when E2H0 < 0 (including NI_NV1), leaving HCR_EL2.NV1 writable instead of RES0 for VHE-only nested guests where KVM advertises ID_AA64MMFR4_EL1.E2H0 = NI_NV1.

Check kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI) in feat_nv2_e2h0_ni() so that HCR_EL2.NV1 is treated as implemented only when E2H0 is IMP or NI, and forced to RES0 when E2H0 is NI_NV1.

Fixes b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits")
---
 arch/arm64/kvm/config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/config.c +2 -1
index adaf26ab93b85..b4f555d39f54a 100644
@@ -258,7 +258,8 @@ static bool feat_nv2(struct kvm *kvm)
 
 static bool feat_nv2_e2h0_ni(struct kvm *kvm)
 {
-	return feat_nv2(kvm) && !kvm_has_feat(kvm, FEAT_E2H0);
+	return feat_nv2(kvm) &&
+	       kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI);
 }
 
 static bool feat_rasv1p1(struct kvm *kvm)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#2 lore raw
Quoted 46 lines · Colton Lewis

ID_AA64MMFR4_EL1.E2H0 is a signed 4-bit field where:
- 0b0000 (IMP, 0): FEAT_E2H0 is implemented.
- 0b1111 (NI, -1): FEAT_E2H0 is not implemented, except HCR_EL2.NV1 is implemented.
- 0b1110 (NI_NV1, -2): Neither HCR_EL2.E2H == 0 nor HCR_EL2.NV1 is implemented, and HCR_EL2.NV1 is RES0.

Prior to commit b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits"), kvm_init_nv_sysregs() marked HCR_EL2.NV1 as RES0 when !kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI), i.e., when the signed E2H0 field is less than NI (-1), which matches NI_NV1 (-2).

When converting HCR_EL2 to the feature map, feat_nv2_e2h0_ni() was written as !kvm_has_feat(kvm, FEAT_E2H0), where FEAT_E2H0 checks E2H0 >= IMP (0). This inverted the condition: feat_nv2_e2h0_ni() evaluates to true when E2H0 < 0 (including NI_NV1), leaving HCR_EL2.NV1 writable instead of RES0 for VHE-only nested guests where KVM advertises ID_AA64MMFR4_EL1.E2H0 = NI_NV1.

Check kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI) in feat_nv2_e2h0_ni() so that HCR_EL2.NV1 is treated as implemented only when E2H0 is IMP or NI, and forced to RES0 when E2H0 is NI_NV1.

Fixes b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits")
---
 arch/arm64/kvm/config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/config.c +2 -1
index adaf26ab93b85..b4f555d39f54a 100644
@@ -258,7 +258,8 @@ static bool feat_nv2(struct kvm *kvm)
 
 static bool feat_nv2_e2h0_ni(struct kvm *kvm)
 {
-	return feat_nv2(kvm) && !kvm_has_feat(kvm, FEAT_E2H0);
+	return feat_nv2(kvm) &&
+	       kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI);
 }
 
 static bool feat_rasv1p1(struct kvm *kvm)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog

Hi Colton,

I just read the code that uses feat_nv2_e2h0_ni(). IIUC if it returns false, we get HCR_EL2.NV1 to be RES0. Otherwise it's writable.

The change you suggest is returning (ID_AA64MMFR4_EL1.E2H0 >= -1), which would return false in the case of NI_NV1 (-2), and make the field RES0 only in that case.

I think it does make sense, but maybe we should also change the name of the helper from feat_nv2_e2h0_ni() to something that reflects what is being tested now. feat_nv2_e2h0_imp_or_ni() would be confuse, and maybe feat_nv2_e2h0_not_ni_nv1() would be as well.

Maybe feat_nv2_e2h0_not_res0(). What do you think?

Thanks!
Leo

#3 lore raw
Quoted 1 line · Leonardo Bras

Maybe feat_nv2_e2h0_not_res0(). What do you think?

Agreed on renaming it. Since HCR_EL2.NV1 is the bit marked RES0 (while ID_AA64MMFR4_EL1.E2H0 is the signed ID field controlling it), I renamed the helper to feat_nv2_nv1_not_res0() in v2.

-- Colton

#4 lore raw
Quoted 9 lines · Colton Lewis
Quoted 1 line · Leonardo Bras

Maybe feat_nv2_e2h0_not_res0(). What do you think?

Agreed on renaming it. Since HCR_EL2.NV1 is the bit marked RES0 (while ID_AA64MMFR4_EL1.E2H0 is the signed ID field controlling it), I renamed the helper to feat_nv2_nv1_not_res0() in v2.

-- Colton

Awesome! Thanks!
Leo

#5 lore raw

PATCH 2/2 KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1

In reply to Colton Lewis

In a VHE-only EL2 guest (KVM_ARM_VCPU_HAS_EL2 without KVM_ARM_VCPU_HAS_EL2_E2H0), KVM advertises ID_AA64MMFR4_EL1.E2H0 as NI_NV1, making HCR_EL2.E2H RES1 and HCR_EL2.NV1 RES0.

Extend hello_el2 to attempt clearing HCR_EL2.E2H and setting HCR_EL2.NV1 both from guest EL2 (across a trapped ID register exit) and from userspace via KVM_SET_ONE_REG, verifying that HCR_EL2.E2H remains RES1 and HCR_EL2.NV1 is masked to RES0.

---
 tools/testing/selftests/kvm/arm64/hello_el2.c | 34 ++++++++++++++++---
 1 file changed, 29 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/arm64/hello_el2.c +29 -5
index bbe6862c6ab16..46b0f63e199f0 100644
@@ -13,13 +13,29 @@
 
 static void guest_code(void)
 {
-	u64 mmfr0 = read_sysreg_s(SYS_ID_AA64MMFR0_EL1);
-	u64 mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
-	u64 mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);
-	u8 e2h0 = SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4);
+	u64 mmfr0, mmfr1, mmfr4, hcr;
+	u8 e2h0;
 
 	GUEST_ASSERT_EQ(get_current_el(), 2);
-	GUEST_ASSERT(read_sysreg(hcr_el2) & HCR_EL2_E2H);
+
+	hcr = read_sysreg(hcr_el2);
+	GUEST_ASSERT(hcr & HCR_EL2_E2H);
+
+	/*
+	 * Attempt to clear HCR_EL2.E2H (RES1) and set HCR_EL2.NV1 (RES0).
+	 * KVM sanitises VNCR-backed HCR_EL2 across exits such as the
+	 * trapped ID register reads below.
+	 */
+	write_sysreg((hcr & ~HCR_EL2_E2H) | HCR_EL2_NV1, hcr_el2);
+
+	mmfr0 = read_sysreg_s(SYS_ID_AA64MMFR0_EL1);
+	mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
+	mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);
+	e2h0 = SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4);
+
+	hcr = read_sysreg(hcr_el2);
+	GUEST_ASSERT(hcr & HCR_EL2_E2H);
+	GUEST_ASSERT(!(hcr & HCR_EL2_NV1));
 	GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR1_EL1, VH, mmfr1),
 			ID_AA64MMFR1_EL1_VH_IMP);
 
@@ -45,6 +61,7 @@ int main(void)
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 	struct ucall uc;
+	u64 hcr;
 
 	TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
 
@@ -66,6 +83,13 @@ int main(void)
 		TEST_FAIL("Unhandled ucall: %ld\n", uc.cmd);
 	}
 
+	hcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2),
+		     (hcr & ~HCR_EL2_E2H) | HCR_EL2_NV1);
+	hcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2));
+	TEST_ASSERT(hcr & HCR_EL2_E2H, "HCR_EL2.E2H should be RES1");
+	TEST_ASSERT(!(hcr & HCR_EL2_NV1), "HCR_EL2.NV1 should be RES0");
+
 	kvm_vm_free(vm);
 	return 0;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
maintainer
#6 lore raw

Re: [PATCH v1 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1

Quoted 41 lines · Colton Lewis

In a VHE-only EL2 guest (KVM_ARM_VCPU_HAS_EL2 without KVM_ARM_VCPU_HAS_EL2_E2H0), KVM advertises ID_AA64MMFR4_EL1.E2H0 as NI_NV1, making HCR_EL2.E2H RES1 and HCR_EL2.NV1 RES0.

Extend hello_el2 to attempt clearing HCR_EL2.E2H and setting HCR_EL2.NV1 both from guest EL2 (across a trapped ID register exit) and from userspace via KVM_SET_ONE_REG, verifying that HCR_EL2.E2H remains RES1 and HCR_EL2.NV1 is masked to RES0.

---
 tools/testing/selftests/kvm/arm64/hello_el2.c | 34 ++++++++++++++++---
 1 file changed, 29 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/arm64/hello_el2.c +11 -5
index bbe6862c6ab16..46b0f63e199f0 100644
@@ -13,13 +13,29 @@
 
 static void guest_code(void)
 {
-	u64 mmfr0 = read_sysreg_s(SYS_ID_AA64MMFR0_EL1);
-	u64 mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
-	u64 mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);
-	u8 e2h0 = SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4);
+	u64 mmfr0, mmfr1, mmfr4, hcr;
+	u8 e2h0;
 
 	GUEST_ASSERT_EQ(get_current_el(), 2);
-	GUEST_ASSERT(read_sysreg(hcr_el2) & HCR_EL2_E2H);
+
+	hcr = read_sysreg(hcr_el2);
+	GUEST_ASSERT(hcr & HCR_EL2_E2H);
+
+	/*
+	 * Attempt to clear HCR_EL2.E2H (RES1) and set HCR_EL2.NV1 (RES0).
+	 * KVM sanitises VNCR-backed HCR_EL2 across exits such as the
+	 * trapped ID register reads below.
+	 */

tools/testing/selftests/kvm/arm64/hello_el2.c line 38

Quoted 10 lines
tools/testing/selftests/kvm/arm64/hello_el2.c +10 -0
+	write_sysreg((hcr & ~HCR_EL2_E2H) | HCR_EL2_NV1, hcr_el2);
+
+	mmfr0 = read_sysreg_s(SYS_ID_AA64MMFR0_EL1);
+	mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
+	mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);
+	e2h0 = SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4);
+
+	hcr = read_sysreg(hcr_el2);
+	GUEST_ASSERT(hcr & HCR_EL2_E2H);
+	GUEST_ASSERT(!(hcr & HCR_EL2_NV1));

That's relying on a KVM-specific behaviour, and not something the architecture guarantees. You should test that you either get the original written value, or the effective value.

Also, ID_AA64MMFR4_EL1 doesn't necessarily trap, since this relies on FGT being implemented. So on an older code (Neoverse V2, for example), this is unlikely to give you the correct results.

Quoted 12 lines
tools/testing/selftests/kvm/arm64/hello_el2.c +1 -0
 	GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR1_EL1, VH, mmfr1),
 			ID_AA64MMFR1_EL1_VH_IMP);
 
@@ -45,6 +61,7 @@ int main(void)
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 	struct ucall uc;
+	u64 hcr;
 
 	TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
 

tools/testing/selftests/kvm/arm64/hello_el2.c int main(void) · line 92

Quoted 10 lines
tools/testing/selftests/kvm/arm64/hello_el2.c +7 -0
@@ -66,6 +83,13 @@ int main(void)
 		TEST_FAIL("Unhandled ucall: %ld\n", uc.cmd);
 	}
 
+	hcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2),
+		     (hcr & ~HCR_EL2_E2H) | HCR_EL2_NV1);
+	hcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2));
+	TEST_ASSERT(hcr & HCR_EL2_E2H, "HCR_EL2.E2H should be RES1");
+	TEST_ASSERT(!(hcr & HCR_EL2_NV1), "HCR_EL2.NV1 should be RES0");
+

I think this is the only reliable test you can implement.

tools/testing/selftests/kvm/arm64/hello_el2.c int main(void) · line 95

Quoted 3 lines
tools/testing/selftests/kvm/arm64/hello_el2.c +0 -0
 	kvm_vm_free(vm);
 	return 0;
 }

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#7 lore raw

Re: [PATCH v1 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1

Quoted 1 line · Marc Zyngier

I think this is the only reliable test you can implement.

Thanks, I dropped the guest-side HCR_EL2 check and kept only the KVM_SET_ONE_REG/KVM_GET_ONE_REG test in v2.

-- Colton