Skip to content

Commit 87e3ca0

Browse files
sean-jcbonzini
authored andcommitted
KVM: selftests: Force load all supported XSAVE state in state test
Extend x86's state to forcefully load *all* host-supported xfeatures by modifying xstate_bv in the saved state. Stuffing xstate_bv ensures that the selftest is verifying KVM's full ABI regardless of whether or not the guest code is successful in getting various xfeatures out of their INIT state, e.g. see the disaster that is/was MPX. Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 7770982 commit 87e3ca0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,15 @@ static inline bool kvm_pmu_has(struct kvm_x86_pmu_feature feature)
922922
!kvm_cpu_has(feature.anti_feature);
923923
}
924924

925+
static __always_inline uint64_t kvm_cpu_supported_xcr0(void)
926+
{
927+
if (!kvm_cpu_has_p(X86_PROPERTY_SUPPORTED_XCR0_LO))
928+
return 0;
929+
930+
return kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_LO) |
931+
((uint64_t)kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_HI) << 32);
932+
}
933+
925934
static inline size_t kvm_cpuid2_size(int nr_entries)
926935
{
927936
return sizeof(struct kvm_cpuid2) +

tools/testing/selftests/kvm/x86_64/state_test.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static void __attribute__((__flatten__)) guest_code(void *arg)
230230

231231
int main(int argc, char *argv[])
232232
{
233+
uint64_t *xstate_bv, saved_xstate_bv;
233234
vm_vaddr_t nested_gva = 0;
234235
struct kvm_cpuid2 empty_cpuid = {};
235236
struct kvm_regs regs1, regs2;
@@ -294,12 +295,25 @@ int main(int argc, char *argv[])
294295
* allow KVM_SET_XSAVE regardless of guest CPUID. Manually
295296
* load only XSAVE state, MSRs in particular have a much more
296297
* convoluted ABI.
298+
*
299+
* Load two versions of XSAVE state: one with the actual guest
300+
* XSAVE state, and one with all supported features forced "on"
301+
* in xstate_bv, e.g. to ensure that KVM allows loading all
302+
* supported features, even if something goes awry in saving
303+
* the original snapshot.
297304
*/
305+
xstate_bv = (void *)&((uint8_t *)state->xsave->region)[512];
306+
saved_xstate_bv = *xstate_bv;
307+
298308
vcpuN = __vm_vcpu_add(vm, vcpu->id + 1);
299309
vcpu_xsave_set(vcpuN, state->xsave);
310+
*xstate_bv = kvm_cpu_supported_xcr0();
311+
vcpu_xsave_set(vcpuN, state->xsave);
300312

301313
vcpu_init_cpuid(vcpuN, &empty_cpuid);
302314
vcpu_xsave_set(vcpuN, state->xsave);
315+
*xstate_bv = saved_xstate_bv;
316+
vcpu_xsave_set(vcpuN, state->xsave);
303317

304318
kvm_x86_state_cleanup(state);
305319

0 commit comments

Comments
 (0)