1 // SPDX-License-Identifier: GPL-2.0
3 * CR4 and CPUID sync test
5 * Copyright 2018, Red Hat, Inc. and/or its affiliates.
8 * Wei Huang <wei@redhat.com>
15 #include <sys/ioctl.h>
17 #include "test_util.h"
20 #include "processor.h"
22 static inline bool cr4_cpuid_is_sync(void)
24 uint64_t cr4 = get_cr4();
26 return (this_cpu_has(X86_FEATURE_OSXSAVE) == !!(cr4 & X86_CR4_OSXSAVE));
29 static void guest_code(void)
33 /* turn on CR4.OSXSAVE */
35 cr4 |= X86_CR4_OSXSAVE;
38 /* verify CR4.OSXSAVE == CPUID.OSXSAVE */
39 GUEST_ASSERT(cr4_cpuid_is_sync());
41 /* notify hypervisor to change CR4 */
45 GUEST_ASSERT(cr4_cpuid_is_sync());
50 int main(int argc, char *argv[])
52 struct kvm_vcpu *vcpu;
54 struct kvm_sregs sregs;
57 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE));
59 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
63 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
65 switch (get_ucall(vcpu, &uc)) {
67 /* emulate hypervisor clearing CR4.OSXSAVE */
68 vcpu_sregs_get(vcpu, &sregs);
69 sregs.cr4 &= ~X86_CR4_OSXSAVE;
70 vcpu_sregs_set(vcpu, &sregs);
73 REPORT_GUEST_ASSERT(uc);
78 TEST_FAIL("Unknown ucall %lu", uc.cmd);