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 #define X86_FEATURE_XSAVE (1<<26)
23 #define X86_FEATURE_OSXSAVE (1<<27)
26 static inline bool cr4_cpuid_is_sync(void)
29 uint32_t eax
, ebx
, ecx
, edx
;
34 __asm__
__volatile__("cpuid"
35 : "=a"(eax
), "=b"(ebx
), "=c"(ecx
), "=d"(edx
)
36 : "a"(func
), "c"(subfunc
));
40 return (!!(ecx
& X86_FEATURE_OSXSAVE
)) == (!!(cr4
& X86_CR4_OSXSAVE
));
43 static void guest_code(void)
47 /* turn on CR4.OSXSAVE */
49 cr4
|= X86_CR4_OSXSAVE
;
52 /* verify CR4.OSXSAVE == CPUID.OSXSAVE */
53 GUEST_ASSERT(cr4_cpuid_is_sync());
55 /* notify hypervisor to change CR4 */
59 GUEST_ASSERT(cr4_cpuid_is_sync());
64 int main(int argc
, char *argv
[])
68 struct kvm_sregs sregs
;
69 struct kvm_cpuid_entry2
*entry
;
73 entry
= kvm_get_supported_cpuid_entry(1);
74 if (!(entry
->ecx
& X86_FEATURE_XSAVE
)) {
75 print_skip("XSAVE feature not supported");
79 /* Tell stdout not to buffer its content */
83 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
84 run
= vcpu_state(vm
, VCPU_ID
);
87 rc
= _vcpu_run(vm
, VCPU_ID
);
89 TEST_ASSERT(rc
== 0, "vcpu_run failed: %d\n", rc
);
90 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
91 "Unexpected exit reason: %u (%s),\n",
93 exit_reason_str(run
->exit_reason
));
95 switch (get_ucall(vm
, VCPU_ID
, &uc
)) {
97 /* emulate hypervisor clearing CR4.OSXSAVE */
98 vcpu_sregs_get(vm
, VCPU_ID
, &sregs
);
99 sregs
.cr4
&= ~X86_CR4_OSXSAVE
;
100 vcpu_sregs_set(vm
, VCPU_ID
, &sregs
);
103 TEST_FAIL("Guest CR4 bit (OSXSAVE) unsynchronized with CPUID bit.");
108 TEST_FAIL("Unknown ucall %lu", uc
.cmd
);