Skip to content

Commit 7cd4af9

Browse files
jsmattsonjrgregkh
authored andcommitted
KVM: selftests: Fix nested SVM tests when built with clang
[ Upstream commit ed290e1 ] Though gcc conveniently compiles a simple memset to "rep stos," clang prefers to call the libc version of memset. If a test is dynamically linked, the libc memset isn't available in L1 (nor is the PLT or the GOT, for that matter). Even if the test is statically linked, the libc memset may choose to use some CPU features, like AVX, which may not be enabled in L1. Note that __builtin_memset doesn't solve the problem, because (a) the compiler is free to call memset anyway, and (b) __builtin_memset may also choose to use features like AVX, which may not be available in L1. To avoid a myriad of problems, use an explicit "rep stos" to clear the VMCB in generic_svm_setup(), which is called both from L0 and L1. Reported-by: Ricardo Koller <ricarkol@google.com> Signed-off-by: Jim Mattson <jmattson@google.com> Fixes: 20ba262 ("selftests: KVM: AMD Nested test infrastructure") Message-Id: <20210930003649.4026553-1-jmattson@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 293fa72 commit 7cd4af9

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

  • tools/testing/selftests/kvm/lib/x86_64

‎tools/testing/selftests/kvm/lib/x86_64/svm.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
5757
seg->base = base;
5858
}
5959

60+
/*
61+
* Avoid using memset to clear the vmcb, since libc may not be
62+
* available in L1 (and, even if it is, features that libc memset may
63+
* want to use, like AVX, may not be enabled).
64+
*/
65+
static void clear_vmcb(struct vmcb *vmcb)
66+
{
67+
int n = sizeof(*vmcb) / sizeof(u32);
68+
69+
asm volatile ("rep stosl" : "+c"(n), "+D"(vmcb) : "a"(0) : "memory");
70+
}
71+
6072
void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rsp)
6173
{
6274
struct vmcb *vmcb = svm->vmcb;
@@ -73,7 +85,7 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
7385
wrmsr(MSR_EFER, efer | EFER_SVME);
7486
wrmsr(MSR_VM_HSAVE_PA, svm->save_area_gpa);
7587

76-
memset(vmcb, 0, sizeof(*vmcb));
88+
clear_vmcb(vmcb);
7789
asm volatile ("vmsave %0\n\t" : : "a" (vmcb_gpa) : "memory");
7890
vmcb_set_seg(&save->es, get_es(), 0, -1U, data_seg_attr);
7991
vmcb_set_seg(&save->cs, get_cs(), 0, -1U, code_seg_attr);

0 commit comments

Comments
 (0)