Skip to content

Commit 87ccb7d

Browse files
committed
kvm: selftests: actually use all of lib/vmx.c
The allocation of the VMXON and VMCS is currently done twice, in lib/vmx.c and in vmx_tsc_adjust_test.c. Reorganize the code to provide a cleaner and easier to use API to the tests. lib/vmx.c now does the complete setup of the VMX data structures, but does not create the VM or set CPUID. This has to be done by the caller. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 2305339 commit 87ccb7d

File tree

3 files changed

+72
-89
lines changed

3 files changed

+72
-89
lines changed

tools/testing/selftests/kvm/include/vmx.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,22 @@ static inline uint32_t vmcs_revision(void)
486486
return rdmsr(MSR_IA32_VMX_BASIC);
487487
}
488488

489-
void prepare_for_vmx_operation(void);
490-
void prepare_vmcs(void *guest_rip, void *guest_rsp);
491-
struct kvm_vm *vm_create_default_vmx(uint32_t vcpuid,
492-
vmx_guest_code_t guest_code);
489+
struct vmx_pages {
490+
void *vmxon_hva;
491+
uint64_t vmxon_gpa;
492+
void *vmxon;
493+
494+
void *vmcs_hva;
495+
uint64_t vmcs_gpa;
496+
void *vmcs;
497+
498+
void *msr_hva;
499+
uint64_t msr_gpa;
500+
void *msr;
501+
};
502+
503+
struct vmx_pages *vcpu_alloc_vmx(struct kvm_vm *vm, vm_vaddr_t *p_vmx_gva);
504+
bool prepare_for_vmx_operation(struct vmx_pages *vmx);
505+
void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp);
493506

494507
#endif /* !SELFTEST_KVM_VMX_H */

tools/testing/selftests/kvm/lib/vmx.c

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,43 @@
1313
#include "x86.h"
1414
#include "vmx.h"
1515

16-
/* Create a default VM for VMX tests.
16+
/* Allocate memory regions for nested VMX tests.
1717
*
1818
* Input Args:
19-
* vcpuid - The id of the single VCPU to add to the VM.
20-
* guest_code - The vCPU's entry point
19+
* vm - The VM to allocate guest-virtual addresses in.
2120
*
22-
* Output Args: None
21+
* Output Args:
22+
* p_vmx_gva - The guest virtual address for the struct vmx_pages.
2323
*
2424
* Return:
25-
* Pointer to opaque structure that describes the created VM.
25+
* Pointer to structure with the addresses of the VMX areas.
2626
*/
27-
struct kvm_vm *
28-
vm_create_default_vmx(uint32_t vcpuid, vmx_guest_code_t guest_code)
27+
struct vmx_pages *
28+
vcpu_alloc_vmx(struct kvm_vm *vm, vm_vaddr_t *p_vmx_gva)
2929
{
30-
struct kvm_cpuid2 *cpuid;
31-
struct kvm_vm *vm;
32-
vm_vaddr_t vmxon_vaddr;
33-
vm_paddr_t vmxon_paddr;
34-
vm_vaddr_t vmcs_vaddr;
35-
vm_paddr_t vmcs_paddr;
36-
37-
vm = vm_create_default(vcpuid, (void *) guest_code);
38-
39-
/* Enable nesting in CPUID */
40-
vcpu_set_cpuid(vm, vcpuid, kvm_get_supported_cpuid());
30+
vm_vaddr_t vmx_gva = vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
31+
struct vmx_pages *vmx = addr_gva2hva(vm, vmx_gva);
4132

4233
/* Setup of a region of guest memory for the vmxon region. */
43-
vmxon_vaddr = vm_vaddr_alloc(vm, getpagesize(), 0, 0, 0);
44-
vmxon_paddr = addr_gva2gpa(vm, vmxon_vaddr);
34+
vmx->vmxon = (void *)vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
35+
vmx->vmxon_hva = addr_gva2hva(vm, (uintptr_t)vmx->vmxon);
36+
vmx->vmxon_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->vmxon);
4537

4638
/* Setup of a region of guest memory for a vmcs. */
47-
vmcs_vaddr = vm_vaddr_alloc(vm, getpagesize(), 0, 0, 0);
48-
vmcs_paddr = addr_gva2gpa(vm, vmcs_vaddr);
39+
vmx->vmcs = (void *)vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
40+
vmx->vmcs_hva = addr_gva2hva(vm, (uintptr_t)vmx->vmcs);
41+
vmx->vmcs_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->vmcs);
4942

50-
vcpu_args_set(vm, vcpuid, 4, vmxon_vaddr, vmxon_paddr, vmcs_vaddr,
51-
vmcs_paddr);
43+
/* Setup of a region of guest memory for the MSR bitmap. */
44+
vmx->msr = (void *)vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
45+
vmx->msr_hva = addr_gva2hva(vm, (uintptr_t)vmx->msr);
46+
vmx->msr_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->msr);
5247

53-
return vm;
48+
*p_vmx_gva = vmx_gva;
49+
return vmx;
5450
}
5551

56-
void prepare_for_vmx_operation(void)
52+
bool prepare_for_vmx_operation(struct vmx_pages *vmx)
5753
{
5854
uint64_t feature_control;
5955
uint64_t required;
@@ -88,12 +84,27 @@ void prepare_for_vmx_operation(void)
8884
feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
8985
if ((feature_control & required) != required)
9086
wrmsr(MSR_IA32_FEATURE_CONTROL, feature_control | required);
87+
88+
/* Enter VMX root operation. */
89+
*(uint32_t *)(vmx->vmxon) = vmcs_revision();
90+
if (vmxon(vmx->vmxon_gpa))
91+
return false;
92+
93+
/* Load a VMCS. */
94+
*(uint32_t *)(vmx->vmcs) = vmcs_revision();
95+
if (vmclear(vmx->vmcs_gpa))
96+
return false;
97+
98+
if (vmptrld(vmx->vmcs_gpa))
99+
return false;
100+
101+
return true;
91102
}
92103

93104
/*
94105
* Initialize the control fields to the most basic settings possible.
95106
*/
96-
static inline void init_vmcs_control_fields(void)
107+
static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
97108
{
98109
vmwrite(VIRTUAL_PROCESSOR_ID, 0);
99110
vmwrite(POSTED_INTR_NV, 0);
@@ -119,6 +130,8 @@ static inline void init_vmcs_control_fields(void)
119130
vmwrite(CR4_GUEST_HOST_MASK, 0);
120131
vmwrite(CR0_READ_SHADOW, get_cr0());
121132
vmwrite(CR4_READ_SHADOW, get_cr4());
133+
134+
vmwrite(MSR_BITMAP, vmx->msr_gpa);
122135
}
123136

124137
/*
@@ -235,9 +248,9 @@ static inline void init_vmcs_guest_state(void *rip, void *rsp)
235248
vmwrite(GUEST_SYSENTER_EIP, vmreadz(HOST_IA32_SYSENTER_EIP));
236249
}
237250

238-
void prepare_vmcs(void *guest_rip, void *guest_rsp)
251+
void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp)
239252
{
240-
init_vmcs_control_fields();
253+
init_vmcs_control_fields(vmx);
241254
init_vmcs_host_state();
242255
init_vmcs_guest_state(guest_rip, guest_rsp);
243256
}

tools/testing/selftests/kvm/vmx_tsc_adjust_test.c

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ enum {
4646
PORT_DONE,
4747
};
4848

49-
struct vmx_page {
50-
vm_vaddr_t virt;
51-
vm_paddr_t phys;
52-
};
53-
5449
enum {
5550
VMXON_PAGE = 0,
5651
VMCS_PAGE,
@@ -67,9 +62,6 @@ struct kvm_single_msr {
6762
/* The virtual machine object. */
6863
static struct kvm_vm *vm;
6964

70-
/* Array of vmx_page descriptors that is shared with the guest. */
71-
struct vmx_page *vmx_pages;
72-
7365
#define exit_to_l0(_port, _arg) do_exit_to_l0(_port, (unsigned long) (_arg))
7466
static void do_exit_to_l0(uint16_t port, unsigned long arg)
7567
{
@@ -105,7 +97,7 @@ static void l2_guest_code(void)
10597
__asm__ __volatile__("vmcall");
10698
}
10799

108-
static void l1_guest_code(struct vmx_page *vmx_pages)
100+
static void l1_guest_code(struct vmx_pages *vmx_pages)
109101
{
110102
#define L2_GUEST_STACK_SIZE 64
111103
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
@@ -116,23 +108,14 @@ static void l1_guest_code(struct vmx_page *vmx_pages)
116108
wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE);
117109
check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
118110

119-
prepare_for_vmx_operation();
120-
121-
/* Enter VMX root operation. */
122-
*(uint32_t *)vmx_pages[VMXON_PAGE].virt = vmcs_revision();
123-
GUEST_ASSERT(!vmxon(vmx_pages[VMXON_PAGE].phys));
124-
125-
/* Load a VMCS. */
126-
*(uint32_t *)vmx_pages[VMCS_PAGE].virt = vmcs_revision();
127-
GUEST_ASSERT(!vmclear(vmx_pages[VMCS_PAGE].phys));
128-
GUEST_ASSERT(!vmptrld(vmx_pages[VMCS_PAGE].phys));
111+
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
129112

130113
/* Prepare the VMCS for L2 execution. */
131-
prepare_vmcs(l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
114+
prepare_vmcs(vmx_pages, l2_guest_code,
115+
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
132116
control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
133117
control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETING;
134118
vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
135-
vmwrite(MSR_BITMAP, vmx_pages[MSR_BITMAP_PAGE].phys);
136119
vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
137120

138121
/* Jump into L2. First, test failure to load guest CR3. */
@@ -152,33 +135,6 @@ static void l1_guest_code(struct vmx_page *vmx_pages)
152135
exit_to_l0(PORT_DONE, 0);
153136
}
154137

155-
static void allocate_vmx_page(struct vmx_page *page)
156-
{
157-
vm_vaddr_t virt;
158-
159-
virt = vm_vaddr_alloc(vm, PAGE_SIZE, 0, 0, 0);
160-
memset(addr_gva2hva(vm, virt), 0, PAGE_SIZE);
161-
162-
page->virt = virt;
163-
page->phys = addr_gva2gpa(vm, virt);
164-
}
165-
166-
static vm_vaddr_t allocate_vmx_pages(void)
167-
{
168-
vm_vaddr_t vmx_pages_vaddr;
169-
int i;
170-
171-
vmx_pages_vaddr = vm_vaddr_alloc(
172-
vm, sizeof(struct vmx_page) * NUM_VMX_PAGES, 0, 0, 0);
173-
174-
vmx_pages = (void *) addr_gva2hva(vm, vmx_pages_vaddr);
175-
176-
for (i = 0; i < NUM_VMX_PAGES; i++)
177-
allocate_vmx_page(&vmx_pages[i]);
178-
179-
return vmx_pages_vaddr;
180-
}
181-
182138
void report(int64_t val)
183139
{
184140
printf("IA32_TSC_ADJUST is %ld (%lld * TSC_ADJUST_VALUE + %lld).\n",
@@ -187,31 +143,32 @@ void report(int64_t val)
187143

188144
int main(int argc, char *argv[])
189145
{
190-
vm_vaddr_t vmx_pages_vaddr;
146+
struct vmx_pages *vmx_pages;
147+
vm_vaddr_t vmx_pages_gva;
191148
struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
192149

193150
if (!(entry->ecx & CPUID_VMX)) {
194151
fprintf(stderr, "nested VMX not enabled, skipping test\n");
195152
exit(KSFT_SKIP);
196153
}
197154

198-
vm = vm_create_default_vmx(VCPU_ID, (void *) l1_guest_code);
155+
vm = vm_create_default(VCPU_ID, (void *) l1_guest_code);
156+
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
199157

200158
/* Allocate VMX pages and shared descriptors (vmx_pages). */
201-
vmx_pages_vaddr = allocate_vmx_pages();
202-
vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_vaddr);
159+
vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
160+
vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
203161

204162
for (;;) {
205163
volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
206164
struct kvm_regs regs;
207165

208166
vcpu_run(vm, VCPU_ID);
167+
vcpu_regs_get(vm, VCPU_ID, &regs);
209168
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
210-
"Got exit_reason other than KVM_EXIT_IO: %u (%s),\n",
169+
"Got exit_reason other than KVM_EXIT_IO: %u (%s), rip=%lx\n",
211170
run->exit_reason,
212-
exit_reason_str(run->exit_reason));
213-
214-
vcpu_regs_get(vm, VCPU_ID, &regs);
171+
exit_reason_str(run->exit_reason), regs.rip);
215172

216173
switch (run->io.port) {
217174
case PORT_ABORT:

0 commit comments

Comments
 (0)