* [PATCH v3 0/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally
@ 2026-11-18 20:36 Colton Lewis
2026-11-18 20:36 ` [PATCH v3 1/2] " Colton Lewis
` (2 more replies)
0 siblings, 3 replies; 6+ 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
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) 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 E2H0 = NI_NV1.
KVM never supports a configuration where HCR_EL2.NV1 is non-RES0
(limit_nv_id_reg() exposes either VHE-only EL2 with E2H0 = NI_NV1 where
E2H0 is not userspace-writable, or FEAT_E2H0 without FEAT_VHE or
FEAT_NV).
Patch 1 drops feat_nv2_e2h0_ni() and marks HCR_EL2.NV1 unconditionally
RES0 using NEEDS_FEAT_FLAG(HCR_EL2_NV1, FORCE_RESx).
Patch 2 extends the arm64 hello_el2 selftest to verify via
KVM_SET_ONE_REG and KVM_GET_ONE_REG that HCR_EL2.E2H remains RES1 and
HCR_EL2.NV1 remains RES0 when E2H0 is NI_NV1.
Changes in v3:
- Drop feat_nv2_nv1_not_res0() and force HCR_EL2.NV1 to RES0
unconditionally via NEEDS_FEAT_FLAG(HCR_EL2_NV1, FORCE_RESx) (Marc).
- Add Suggested-by: Marc Zyngier <maz@kernel.org> on patch 1.
- Add a comment in hello_el2.c explaining why RESx enforcement is
checked from userspace rather than inside the guest (Oliver).
- Link to v2: https://lore.kernel.org/r/20261117204535.4036191-1-coltonlewis@google.com
Tested under QEMU 11.1.2 TCG (arm64, kvm-arm.mode=nested): hello_el2
fails with "HCR_EL2.NV1 should be RES0" on unpatched v7.4-rc3 and passes
with this series applied, with no check_feature_map() warnings at boot.
Changes in v2:
- Rename feat_nv2_e2h0_ni() to feat_nv2_nv1_not_res0() (Leonardo).
- Drop the guest EL2 MSR/MRS HCR_EL2 check in hello_el2 (Marc).
- Link to v1: https://lore.kernel.org/r/20261116200418.3535619-1-coltonlewis@google.com
Colton Lewis (2):
KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally
KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1
arch/arm64/kvm/config.c | 7 +------
tools/testing/selftests/kvm/arm64/hello_el2.c | 15 +++++++++++++++
2 files changed, 16 insertions(+), 6 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [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-19 12:09 ` Leonardo Bras 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 2026-11-19 12:43 ` [PATCH v3 0/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally Marc Zyngier 2 siblings, 1 reply; 6+ 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 related [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally 2026-11-18 20:36 ` [PATCH v3 1/2] " Colton Lewis @ 2026-11-19 12:09 ` Leonardo Bras 0 siblings, 0 replies; 6+ messages in thread From: Leonardo Bras @ 2026-11-19 12:09 UTC (permalink / raw) To: Colton Lewis Cc: Leonardo Bras, Marc Zyngier, Oliver Upton, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba, kvmarm, linux-arm-kernel, kvm, linux-kernel On Wed, Nov 18, 2026 at 08:36:24PM +0000, Colton Lewis wrote: > 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 > Right, KVM nested requires VHE, which uses E2H, which conflicts with NV1. FWIW: Reviewed-by: Leonardo Bras <leo.bras@arm.com> Thanks! Leo ^ permalink raw reply [flat|nested] 6+ 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 2026-11-19 12:43 ` Leonardo Bras 2026-11-19 12:43 ` [PATCH v3 0/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally Marc Zyngier 2 siblings, 1 reply; 6+ 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 related [flat|nested] 6+ messages in thread
* Re: [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 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1 Colton Lewis @ 2026-11-19 12:43 ` Leonardo Bras 0 siblings, 0 replies; 6+ messages in thread From: Leonardo Bras @ 2026-11-19 12:43 UTC (permalink / raw) To: Colton Lewis Cc: Leonardo Bras, Marc Zyngier, Oliver Upton, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba, kvmarm, linux-arm-kernel, kvm, linux-kernel On Wed, Nov 18, 2026 at 08:36:25PM +0000, Colton Lewis wrote: > 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 > The VMM tries to set NV1 and clean E2H, and it should not work. Current kernel should pass this test, as vcpu_set_reg() will call a KVM_SET_ONE_REG ioctl, going down to __vcpu_assign_sys_reg() which will, given vcpu_has_nv(), will apply RES0 to NV1 (patch #1) and RES1 to E2H, as per this line: NEEDS_FEAT_FLAG(HCR_EL2_E2H, RES1_WHEN_E2H1 | FORCE_RESx) FWIW: Reviewed-by: Leonardo Bras <leo.bras@arm.com> Thanks! Leo ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 0/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 ` [PATCH v3 1/2] " 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 @ 2026-11-19 12:43 ` Marc Zyngier 2 siblings, 0 replies; 6+ messages in thread From: Marc Zyngier @ 2026-11-19 12:43 UTC (permalink / raw) To: Oliver Upton, Colton Lewis Cc: Leonardo Bras, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba, kvmarm, linux-arm-kernel, kvm, linux-kernel On Wed, 18 Nov 2026 20:36:23 +0000, Colton Lewis wrote: > 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) 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 E2H0 = NI_NV1. > > [...] Applied to fixes, thanks! [1/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally commit: 0f1bdabb82be5d1b5401364cd828caf0985dad48 [2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1 commit: accc93b77267b739b48ab34cb5bcaffd1fbb5db9 Cheers, M. -- Without deviation from the norm, progress is not possible. ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-19 12:43 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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-19 12:09 ` Leonardo Bras 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 2026-11-19 12:43 ` Leonardo Bras 2026-11-19 12:43 ` [PATCH v3 0/2] KVM: arm64: Force HCR_EL2.NV1 to RES0 unconditionally Marc Zyngier
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.