* [PATCH v3 1/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally
2026-11-18 20:36 [PATCH v3 0/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally Colton Lewis
@ 2026-11-18 20:36 ` Colton Lewis
2026-11-18 20:36 ` [PATCH v3 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1 Colton Lewis
1 sibling, 0 replies; 3+ messages in thread
From: Colton Lewis @ 2026-11-18 20:36 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: Leonardo Bras, Joey Gouly, Suzuki K Poulose, Zenghui Yu,
Fuad Tabba, kvmarm, linux-arm-kernel, kvm, linux-kernel,
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.
When commit b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive
fixed-value bits") converted HCR_EL2 to hcr_feat_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.
In practice, KVM cannot handle HCR_EL2.NV1 being non-RES0 in any
configuration: limit_nv_id_reg() exposes either VHE-only EL2 with
ID_AA64MMFR4_EL1.E2H0 = NI_NV1 (and E2H0 is not userspace-writable), or
FEAT_E2H0 without FEAT_VHE or FEAT_NV. Checking E2H0 in a feature helper
is therefore unnecessary.
Drop feat_nv2_e2h0_ni() and mark HCR_EL2_NV1 as an unconditional RES0 bit
using NEEDS_FEAT_FLAG(HCR_EL2_NV1, FORCE_RESx) in hcr_feat_map[] (shared
by HCR_EL2 and NVHCR_EL2). Because HCR_EL2_NV1 is not part of the
architectural HCR_EL2_RES0 mask, check_feat_map() accounts for it as an
implementation-defined FORCE_RESx bit.
Fixes: b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits")
Suggested-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Colton Lewis <coltonlewis@google.com>
---
arch/arm64/kvm/config.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index adaf26ab93b85..2e81f45dc9921 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -256,11 +256,6 @@ 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)
-{
- return feat_nv2(kvm) && !kvm_has_feat(kvm, FEAT_E2H0);
-}
-
static bool feat_rasv1p1(struct kvm *kvm)
{
return (kvm_has_feat(kvm, ID_AA64PFR0_EL1, RAS, V1P1) ||
@@ -1034,7 +1029,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_FLAG(HCR_EL2_NV1, FORCE_RESx), /* Missing from JSON, RES0 as an IMPDEF choice */
NEEDS_FEAT(HCR_EL2_API |
HCR_EL2_APK,
feat_pauth),
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v3 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1
2026-11-18 20:36 [PATCH v3 0/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally Colton Lewis
2026-11-18 20:36 ` [PATCH v3 1/2] " Colton Lewis
@ 2026-11-18 20:36 ` Colton Lewis
1 sibling, 0 replies; 3+ messages in thread
From: Colton Lewis @ 2026-11-18 20:36 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: Leonardo Bras, Joey Gouly, Suzuki K Poulose, Zenghui Yu,
Fuad Tabba, kvmarm, linux-arm-kernel, kvm, linux-kernel,
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
from userspace via KVM_SET_ONE_REG, verifying that HCR_EL2.E2H remains
RES1 and HCR_EL2.NV1 is masked to RES0. Add a comment explaining why the
check is performed from userspace rather than inside the guest.
Signed-off-by: Colton Lewis <coltonlewis@google.com>
---
tools/testing/selftests/kvm/arm64/hello_el2.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/tools/testing/selftests/kvm/arm64/hello_el2.c b/tools/testing/selftests/kvm/arm64/hello_el2.c
index bbe6862c6ab16..3bd3596f5fb1e 100644
--- a/tools/testing/selftests/kvm/arm64/hello_el2.c
+++ b/tools/testing/selftests/kvm/arm64/hello_el2.c
@@ -45,6 +45,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 +67,20 @@ int main(void)
TEST_FAIL("Unhandled ucall: %ld\n", uc.cmd);
}
+ /*
+ * Verify RES0/RES1 enforcement from userspace rather than inside
+ * the guest. From the guest's perspective RESx bits may be
+ * stateful (reading back as written) even though their value is
+ * ignored, whereas KVM_SET_ONE_REG applies the effective masks
+ * directly.
+ */
+ 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;
}
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 3+ messages in thread