Re: [PATCH v8 20/20] KVM: x86: selftests: Add APX state handling and XCR0 sanity checks

From: Jim Mattson

Date: Fri Nov 06 2026 - 00:36:34 EST


On Wed, Sep 30, 2026 at 2:40 PM Chang S. Bae <chang.seok.bae@xxxxxxxxx> wrote:
>
> Now that KVM exposes the APX feature to guests, extend some existing
> selftests to validate XCR0 configuration and state management.
>
> Since APX repurposes the XSAVE area previously used by MPX in the
> non-compacted format, add a check to ensure that MPX state is not set
> when APX is enabled.
>
> Also, load non-init APX state data in the guest so that XSTATE_BV[APX] is
> set, allowing validation of APX state testing.
>
> Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
> ---
> tools/testing/selftests/kvm/x86/state_test.c | 3 +++
> .../selftests/kvm/x86/xcr0_cpuid_test.c | 19 +++++++++++++++++++
> 2 files changed, 22 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/x86/state_test.c b/tools/testing/selftests/kvm/x86/state_test.c
> index 4a1056a6cb8d..9727dbb2b981 100644
> --- a/tools/testing/selftests/kvm/x86/state_test.c
> +++ b/tools/testing/selftests/kvm/x86/state_test.c
> @@ -164,6 +164,9 @@ static void __attribute__((__flatten__)) guest_code(void *arg)
> asm volatile ("vmovupd %0, %%zmm16" :: "m" (buffer));
> }
>
> + if (supported_xcr0 & XFEATURE_MASK_APX)
> + write_egpr(16, 0xcccccccc);
> +

Nit: to match MPX and PKU below:

if (this_cpu_has(X86_FEATURE_APX)) {
GUEST_ASSERT(supported_xcr0 & XFEATURE_MASK_APX);
write_egpr(16, 0xcccccccc);
}

[ ... ]

> +#define ASSERT_XFEATURE_CONFLICT(supported_xcr0, xfeatures, conflicts) \
> +do { \
> + uint64_t __supported = (supported_xcr0) & ((xfeatures) | (conflicts)); \

Nit: u64

Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>