Skip to content

Commit 7770982

Browse files
sean-jcbonzini
authored andcommitted
KVM: selftests: Load XSAVE state into untouched vCPU during state test
Expand x86's state test to load XSAVE state into a "dummy" vCPU prior to KVM_SET_CPUID2, and again with an empty guest CPUID model. Except for off-by-default features, i.e. AMX, KVM's ABI for KVM_SET_XSAVE is that userspace is allowed to load xfeatures so long as they are supported by the host. This is a regression test for a combination of KVM bugs where the state saved by KVM_GET_XSAVE{2} could not be loaded via KVM_SET_XSAVE if the saved xstate_bv would load guest-unsupported xfeatures. Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 60d351f commit 7770982

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ static void __attribute__((__flatten__)) guest_code(void *arg)
231231
int main(int argc, char *argv[])
232232
{
233233
vm_vaddr_t nested_gva = 0;
234-
234+
struct kvm_cpuid2 empty_cpuid = {};
235235
struct kvm_regs regs1, regs2;
236-
struct kvm_vcpu *vcpu;
236+
struct kvm_vcpu *vcpu, *vcpuN;
237237
struct kvm_vm *vm;
238238
struct kvm_x86_state *state;
239239
struct ucall uc;
@@ -286,6 +286,21 @@ int main(int argc, char *argv[])
286286
/* Restore state in a new VM. */
287287
vcpu = vm_recreate_with_one_vcpu(vm);
288288
vcpu_load_state(vcpu, state);
289+
290+
/*
291+
* Restore XSAVE state in a dummy vCPU, first without doing
292+
* KVM_SET_CPUID2, and then with an empty guest CPUID. Except
293+
* for off-by-default xfeatures, e.g. AMX, KVM is supposed to
294+
* allow KVM_SET_XSAVE regardless of guest CPUID. Manually
295+
* load only XSAVE state, MSRs in particular have a much more
296+
* convoluted ABI.
297+
*/
298+
vcpuN = __vm_vcpu_add(vm, vcpu->id + 1);
299+
vcpu_xsave_set(vcpuN, state->xsave);
300+
301+
vcpu_init_cpuid(vcpuN, &empty_cpuid);
302+
vcpu_xsave_set(vcpuN, state->xsave);
303+
289304
kvm_x86_state_cleanup(state);
290305

291306
memset(&regs2, 0, sizeof(regs2));

0 commit comments

Comments
 (0)