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

From: Colton Lewis

Date: Tue Nov 17 2026 - 15:45:50 EST


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) 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. Rename the helper from feat_nv2_e2h0_ni() to
feat_nv2_nv1_not_res0() to clearly reflect what it tests.

Fixes: b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits")
Signed-off-by: Colton Lewis <coltonlewis@xxxxxxxxxx>
---
arch/arm64/kvm/config.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index adaf26ab93b85..a975ac73ed876 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -256,9 +256,10 @@ static bool feat_nv2(struct kvm *kvm)
kvm_has_feat(kvm, ID_AA64MMFR2_EL1, NV, NV2));
}

-static bool feat_nv2_e2h0_ni(struct kvm *kvm)
+static bool feat_nv2_nv1_not_res0(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)
@@ -1034,7 +1035,7 @@ static const struct reg_bits_to_feat_map hcr_feat_map[] = {
HCR_EL2_NV2 |
HCR_EL2_NV,
feat_nv2),
- NEEDS_FEAT(HCR_EL2_NV1, feat_nv2_e2h0_ni), /* Missing from JSON */
+ NEEDS_FEAT(HCR_EL2_NV1, feat_nv2_nv1_not_res0), /* Missing from JSON */
NEEDS_FEAT(HCR_EL2_API |
HCR_EL2_APK,
feat_pauth),
--
2.56.1.750.gf70dc682cf-goog