Re: [PATCH v8 19/20] KVM: x86: selftests: Add APX state and ABI test

From: Jim Mattson

Date: Fri Nov 06 2026 - 00:31:38 EST


On Wed, Sep 30, 2026 at 5:07 PM Chang S. Bae <chang.seok.bae@xxxxxxxxx> wrote:
>
> Test APX-specific behavior and ABI interactions as these are unique
> comparing to other state components:
>
> * EGPR state is saved on VM entry assembly (unlike other components).
> * The saved state is retained even if the guest disables APX.
> * EGPR state is exposed via the XSAVE ABI while not stored in the
> kernel XSAVE buffer.
>
> Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
> ---
> tools/testing/selftests/kvm/Makefile.kvm | 1 +
> .../selftests/kvm/include/x86/processor.h | 120 +++++++++++
> tools/testing/selftests/kvm/x86/apx_test.c | 191 ++++++++++++++++++
> 3 files changed, 312 insertions(+)
> create mode 100644 tools/testing/selftests/kvm/x86/apx_test.c
>
> diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
> index 96bab7002d39..2bd5c77047a8 100644
> --- a/tools/testing/selftests/kvm/Makefile.kvm
> +++ b/tools/testing/selftests/kvm/Makefile.kvm
> @@ -166,6 +166,7 @@ TEST_GEN_PROGS_x86 += rseq_test
> TEST_GEN_PROGS_x86 += steal_time
> TEST_GEN_PROGS_x86 += system_counter_offset_test
> TEST_GEN_PROGS_x86 += pre_fault_memory_test
> +TEST_GEN_PROGS_x86 += x86/apx_test

Nit: please group x86/apx_test with the other x86/ tests above (e.g. next
to x86/amx_test), rather than after the common tests.

[ ... ]

> +static inline void xstatebv_set(void *xsave, uint64_t mask)
> +{
> + *(uint64_t *)(xsave + XSAVE_HDR_OFFSET) |= mask;

Nit: u64.

[ ... ]

> + case UCALL_SYNC: {
> + stage = uc.args[1];
> + vcpu_xsave2_get(vcpu, xsave);
> + if (stage == USERSPACE_UPDATE) {
> + write_xsave_egprs(xsave, stage);
> + } else {
> + TEST_ASSERT(validate_xsave_egprs(xsave, stage),
> + "EGPR state mismatch in userspace XSAVE buffer");
> + }
> + vcpu_xsave_set(vcpu, xsave);

vcpu_xsave_set() should only be called when stage == USERSPACE_UPDATE.
Calling it unconditionally on every UCALL_SYNC overwrites the vCPU's XSAVE
state from userspace during GUEST_UPDATE and GUEST_APXOFF, which defeats
the purpose of verifying that KVM preserves the guest's EGPRs across
VM-exit/VM-entry and across XCR0.APX transitions on its own.

Also, please add a stage where userspace clears XFEATURE_MASK_APX in
XSTATE_BV (while leaving non-zero values in the XSAVE APX payload) and
calls vcpu_xsave_set(), then verifies that R16-R31 have been reset to 0 in
the guest (per patch 3).