Skip to content

Commit 2b9a126

Browse files
committed
KVM: selftests: Rework OSXSAVE CR4=>CPUID test to play nice with AVX insns
Rework the CR4/CPUID sync test to clear CR4.OSXSAVE, do CPUID, and restore CR4.OSXSAVE in assembly, so that there is zero chance of AVX instructions being executed while CR4.OSXSAVE is disabled. This will allow enabling CR4.OSXSAVE by default for selftests vCPUs as a general means of playing nice with AVX instructions. Reviewed-by: Vitaly Kuznetsov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 164cea3 commit 2b9a126

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

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

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
#include "kvm_util.h"
2020
#include "processor.h"
2121

22-
static inline bool cr4_cpuid_is_sync(void)
23-
{
24-
uint64_t cr4 = get_cr4();
25-
26-
return (this_cpu_has(X86_FEATURE_OSXSAVE) == !!(cr4 & X86_CR4_OSXSAVE));
27-
}
22+
#define MAGIC_HYPERCALL_PORT 0x80
2823

2924
static void guest_code(void)
3025
{
26+
u32 regs[4] = {
27+
[KVM_CPUID_EAX] = X86_FEATURE_OSXSAVE.function,
28+
[KVM_CPUID_ECX] = X86_FEATURE_OSXSAVE.index,
29+
};
3130
uint64_t cr4;
3231

3332
/* turn on CR4.OSXSAVE */
@@ -36,13 +35,29 @@ static void guest_code(void)
3635
set_cr4(cr4);
3736

3837
/* verify CR4.OSXSAVE == CPUID.OSXSAVE */
39-
GUEST_ASSERT(cr4_cpuid_is_sync());
40-
41-
/* notify hypervisor to change CR4 */
42-
GUEST_SYNC(0);
43-
44-
/* check again */
45-
GUEST_ASSERT(cr4_cpuid_is_sync());
38+
GUEST_ASSERT(this_cpu_has(X86_FEATURE_OSXSAVE));
39+
40+
/*
41+
* Notify hypervisor to clear CR4.0SXSAVE, do CPUID and save output,
42+
* and then restore CR4. Do this all in assembly to ensure no AVX
43+
* instructions are executed while OSXSAVE=0.
44+
*/
45+
asm volatile (
46+
"out %%al, $" __stringify(MAGIC_HYPERCALL_PORT) "\n\t"
47+
"cpuid\n\t"
48+
"mov %%rdi, %%cr4\n\t"
49+
: "+a" (regs[KVM_CPUID_EAX]),
50+
"=b" (regs[KVM_CPUID_EBX]),
51+
"+c" (regs[KVM_CPUID_ECX]),
52+
"=d" (regs[KVM_CPUID_EDX])
53+
: "D" (get_cr4())
54+
);
55+
56+
/* Verify KVM cleared OSXSAVE in CPUID when it was cleared in CR4. */
57+
GUEST_ASSERT(!(regs[X86_FEATURE_OSXSAVE.reg] & BIT(X86_FEATURE_OSXSAVE.bit)));
58+
59+
/* Verify restoring CR4 also restored OSXSAVE in CPUID. */
60+
GUEST_ASSERT(this_cpu_has(X86_FEATURE_OSXSAVE));
4661

4762
GUEST_DONE();
4863
}
@@ -62,13 +77,16 @@ int main(int argc, char *argv[])
6277
vcpu_run(vcpu);
6378
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
6479

65-
switch (get_ucall(vcpu, &uc)) {
66-
case UCALL_SYNC:
80+
if (vcpu->run->io.port == MAGIC_HYPERCALL_PORT &&
81+
vcpu->run->io.direction == KVM_EXIT_IO_OUT) {
6782
/* emulate hypervisor clearing CR4.OSXSAVE */
6883
vcpu_sregs_get(vcpu, &sregs);
6984
sregs.cr4 &= ~X86_CR4_OSXSAVE;
7085
vcpu_sregs_set(vcpu, &sregs);
71-
break;
86+
continue;
87+
}
88+
89+
switch (get_ucall(vcpu, &uc)) {
7290
case UCALL_ABORT:
7391
REPORT_GUEST_ASSERT(uc);
7492
break;

0 commit comments

Comments
 (0)