The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: Colton Lewis <coltonlewis@google.com>
Cc: Oliver Upton <oupton@kernel.org>, Joey Gouly <joey.gouly@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Zenghui Yu <yuzenghui@huawei.com>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1
Date: Tue, 17 Nov 2026 13:04:57 +0000	[thread overview]
Message-ID: <86fqwz1udi.wl-maz@kernel.org> (raw)
In-Reply-To: <20261116200418.3535619-3-coltonlewis@google.com>

On Mon, 16 Nov 2026 20:04:18 +0000,
Colton Lewis <coltonlewis@google.com> 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
> 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.
>
> Signed-off-by: Colton Lewis <coltonlewis@google.com>
> ---
>  tools/testing/selftests/kvm/arm64/hello_el2.c | 34 ++++++++++++++++---
>  1 file changed, 29 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/arm64/hello_el2.c b/tools/testing/selftests/kvm/arm64/hello_el2.c
> index bbe6862c6ab16..46b0f63e199f0 100644
> --- a/tools/testing/selftests/kvm/arm64/hello_el2.c
> +++ b/tools/testing/selftests/kvm/arm64/hello_el2.c
> @@ -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));

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.

>  	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");
> +

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

>  	kvm_vm_free(vm);
>  	return 0;
>  }

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.

      reply	other threads:[~2026-11-17 13:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 20:04 [PATCH v1 0/2] KVM: arm64: Fix inverted E2H0 check for HCR_EL2.NV1 Colton Lewis
2026-11-16 20:04 ` [PATCH v1 1/2] " Colton Lewis
2026-11-16 20:04 ` [PATCH v1 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1 Colton Lewis
2026-11-17 13:04   ` Marc Zyngier [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86fqwz1udi.wl-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=coltonlewis@google.com \
    --cc=fuad.tabba@linux.dev \
    --cc=joey.gouly@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oupton@kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=yuzenghui@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox