Commit 87ccb7db authored by Paolo Bonzini's avatar Paolo Bonzini

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: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 2305339e
...@@ -486,9 +486,22 @@ static inline uint32_t vmcs_revision(void) ...@@ -486,9 +486,22 @@ static inline uint32_t vmcs_revision(void)
return rdmsr(MSR_IA32_VMX_BASIC); return rdmsr(MSR_IA32_VMX_BASIC);
} }
void prepare_for_vmx_operation(void); struct vmx_pages {
void prepare_vmcs(void *guest_rip, void *guest_rsp); void *vmxon_hva;
struct kvm_vm *vm_create_default_vmx(uint32_t vcpuid, uint64_t vmxon_gpa;
vmx_guest_code_t guest_code); void *vmxon;
void *vmcs_hva;
uint64_t vmcs_gpa;
void *vmcs;
void *msr_hva;
uint64_t msr_gpa;
void *msr;
};
struct vmx_pages *vcpu_alloc_vmx(struct kvm_vm *vm, vm_vaddr_t *p_vmx_gva);
bool prepare_for_vmx_operation(struct vmx_pages *vmx);
void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp);
#endif /* !SELFTEST_KVM_VMX_H */ #endif /* !SELFTEST_KVM_VMX_H */
...@@ -13,47 +13,43 @@ ...@@ -13,47 +13,43 @@
#include "x86.h" #include "x86.h"
#include "vmx.h" #include "vmx.h"
/* Create a default VM for VMX tests. /* Allocate memory regions for nested VMX tests.
* *
* Input Args: * Input Args:
* vcpuid - The id of the single VCPU to add to the VM. * vm - The VM to allocate guest-virtual addresses in.
* guest_code - The vCPU's entry point
* *
* Output Args: None * Output Args:
* p_vmx_gva - The guest virtual address for the struct vmx_pages.
* *
* Return: * Return:
* Pointer to opaque structure that describes the created VM. * Pointer to structure with the addresses of the VMX areas.
*/ */
struct kvm_vm * struct vmx_pages *
vm_create_default_vmx(uint32_t vcpuid, vmx_guest_code_t guest_code) vcpu_alloc_vmx(struct kvm_vm *vm, vm_vaddr_t *p_vmx_gva)
{ {
struct kvm_cpuid2 *cpuid; vm_vaddr_t vmx_gva = vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
struct kvm_vm *vm; struct vmx_pages *vmx = addr_gva2hva(vm, vmx_gva);
vm_vaddr_t vmxon_vaddr;
vm_paddr_t vmxon_paddr;
vm_vaddr_t vmcs_vaddr;
vm_paddr_t vmcs_paddr;
vm = vm_create_default(vcpuid, (void *) guest_code);
/* Enable nesting in CPUID */
vcpu_set_cpuid(vm, vcpuid, kvm_get_supported_cpuid());
/* Setup of a region of guest memory for the vmxon region. */ /* Setup of a region of guest memory for the vmxon region. */
vmxon_vaddr = vm_vaddr_alloc(vm, getpagesize(), 0, 0, 0); vmx->vmxon = (void *)vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
vmxon_paddr = addr_gva2gpa(vm, vmxon_vaddr); vmx->vmxon_hva = addr_gva2hva(vm, (uintptr_t)vmx->vmxon);
vmx->vmxon_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->vmxon);
/* Setup of a region of guest memory for a vmcs. */ /* Setup of a region of guest memory for a vmcs. */
vmcs_vaddr = vm_vaddr_alloc(vm, getpagesize(), 0, 0, 0); vmx->vmcs = (void *)vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
vmcs_paddr = addr_gva2gpa(vm, vmcs_vaddr); vmx->vmcs_hva = addr_gva2hva(vm, (uintptr_t)vmx->vmcs);
vmx->vmcs_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->vmcs);
vcpu_args_set(vm, vcpuid, 4, vmxon_vaddr, vmxon_paddr, vmcs_vaddr, /* Setup of a region of guest memory for the MSR bitmap. */
vmcs_paddr); vmx->msr = (void *)vm_vaddr_alloc(vm, getpagesize(), 0x10000, 0, 0);
vmx->msr_hva = addr_gva2hva(vm, (uintptr_t)vmx->msr);
vmx->msr_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->msr);
return vm; *p_vmx_gva = vmx_gva;
return vmx;
} }
void prepare_for_vmx_operation(void) bool prepare_for_vmx_operation(struct vmx_pages *vmx)
{ {
uint64_t feature_control; uint64_t feature_control;
uint64_t required; uint64_t required;
...@@ -88,12 +84,27 @@ void prepare_for_vmx_operation(void) ...@@ -88,12 +84,27 @@ void prepare_for_vmx_operation(void)
feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL); feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
if ((feature_control & required) != required) if ((feature_control & required) != required)
wrmsr(MSR_IA32_FEATURE_CONTROL, feature_control | required); wrmsr(MSR_IA32_FEATURE_CONTROL, feature_control | required);
/* Enter VMX root operation. */
*(uint32_t *)(vmx->vmxon) = vmcs_revision();
if (vmxon(vmx->vmxon_gpa))
return false;
/* Load a VMCS. */
*(uint32_t *)(vmx->vmcs) = vmcs_revision();
if (vmclear(vmx->vmcs_gpa))
return false;
if (vmptrld(vmx->vmcs_gpa))
return false;
return true;
} }
/* /*
* Initialize the control fields to the most basic settings possible. * Initialize the control fields to the most basic settings possible.
*/ */
static inline void init_vmcs_control_fields(void) static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
{ {
vmwrite(VIRTUAL_PROCESSOR_ID, 0); vmwrite(VIRTUAL_PROCESSOR_ID, 0);
vmwrite(POSTED_INTR_NV, 0); vmwrite(POSTED_INTR_NV, 0);
...@@ -119,6 +130,8 @@ static inline void init_vmcs_control_fields(void) ...@@ -119,6 +130,8 @@ static inline void init_vmcs_control_fields(void)
vmwrite(CR4_GUEST_HOST_MASK, 0); vmwrite(CR4_GUEST_HOST_MASK, 0);
vmwrite(CR0_READ_SHADOW, get_cr0()); vmwrite(CR0_READ_SHADOW, get_cr0());
vmwrite(CR4_READ_SHADOW, get_cr4()); vmwrite(CR4_READ_SHADOW, get_cr4());
vmwrite(MSR_BITMAP, vmx->msr_gpa);
} }
/* /*
...@@ -235,9 +248,9 @@ static inline void init_vmcs_guest_state(void *rip, void *rsp) ...@@ -235,9 +248,9 @@ static inline void init_vmcs_guest_state(void *rip, void *rsp)
vmwrite(GUEST_SYSENTER_EIP, vmreadz(HOST_IA32_SYSENTER_EIP)); vmwrite(GUEST_SYSENTER_EIP, vmreadz(HOST_IA32_SYSENTER_EIP));
} }
void prepare_vmcs(void *guest_rip, void *guest_rsp) void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp)
{ {
init_vmcs_control_fields(); init_vmcs_control_fields(vmx);
init_vmcs_host_state(); init_vmcs_host_state();
init_vmcs_guest_state(guest_rip, guest_rsp); init_vmcs_guest_state(guest_rip, guest_rsp);
} }
...@@ -46,11 +46,6 @@ enum { ...@@ -46,11 +46,6 @@ enum {
PORT_DONE, PORT_DONE,
}; };
struct vmx_page {
vm_vaddr_t virt;
vm_paddr_t phys;
};
enum { enum {
VMXON_PAGE = 0, VMXON_PAGE = 0,
VMCS_PAGE, VMCS_PAGE,
...@@ -67,9 +62,6 @@ struct kvm_single_msr { ...@@ -67,9 +62,6 @@ struct kvm_single_msr {
/* The virtual machine object. */ /* The virtual machine object. */
static struct kvm_vm *vm; static struct kvm_vm *vm;
/* Array of vmx_page descriptors that is shared with the guest. */
struct vmx_page *vmx_pages;
#define exit_to_l0(_port, _arg) do_exit_to_l0(_port, (unsigned long) (_arg)) #define exit_to_l0(_port, _arg) do_exit_to_l0(_port, (unsigned long) (_arg))
static void do_exit_to_l0(uint16_t port, unsigned long arg) static void do_exit_to_l0(uint16_t port, unsigned long arg)
{ {
...@@ -105,7 +97,7 @@ static void l2_guest_code(void) ...@@ -105,7 +97,7 @@ static void l2_guest_code(void)
__asm__ __volatile__("vmcall"); __asm__ __volatile__("vmcall");
} }
static void l1_guest_code(struct vmx_page *vmx_pages) static void l1_guest_code(struct vmx_pages *vmx_pages)
{ {
#define L2_GUEST_STACK_SIZE 64 #define L2_GUEST_STACK_SIZE 64
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
...@@ -116,23 +108,14 @@ static void l1_guest_code(struct vmx_page *vmx_pages) ...@@ -116,23 +108,14 @@ static void l1_guest_code(struct vmx_page *vmx_pages)
wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE); wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE);
check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE); check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
prepare_for_vmx_operation(); GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
/* Enter VMX root operation. */
*(uint32_t *)vmx_pages[VMXON_PAGE].virt = vmcs_revision();
GUEST_ASSERT(!vmxon(vmx_pages[VMXON_PAGE].phys));
/* Load a VMCS. */
*(uint32_t *)vmx_pages[VMCS_PAGE].virt = vmcs_revision();
GUEST_ASSERT(!vmclear(vmx_pages[VMCS_PAGE].phys));
GUEST_ASSERT(!vmptrld(vmx_pages[VMCS_PAGE].phys));
/* Prepare the VMCS for L2 execution. */ /* Prepare the VMCS for L2 execution. */
prepare_vmcs(l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]); prepare_vmcs(vmx_pages, l2_guest_code,
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
control = vmreadz(CPU_BASED_VM_EXEC_CONTROL); control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETING; control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETING;
vmwrite(CPU_BASED_VM_EXEC_CONTROL, control); vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
vmwrite(MSR_BITMAP, vmx_pages[MSR_BITMAP_PAGE].phys);
vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE); vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
/* Jump into L2. First, test failure to load guest CR3. */ /* Jump into L2. First, test failure to load guest CR3. */
...@@ -152,33 +135,6 @@ static void l1_guest_code(struct vmx_page *vmx_pages) ...@@ -152,33 +135,6 @@ static void l1_guest_code(struct vmx_page *vmx_pages)
exit_to_l0(PORT_DONE, 0); exit_to_l0(PORT_DONE, 0);
} }
static void allocate_vmx_page(struct vmx_page *page)
{
vm_vaddr_t virt;
virt = vm_vaddr_alloc(vm, PAGE_SIZE, 0, 0, 0);
memset(addr_gva2hva(vm, virt), 0, PAGE_SIZE);
page->virt = virt;
page->phys = addr_gva2gpa(vm, virt);
}
static vm_vaddr_t allocate_vmx_pages(void)
{
vm_vaddr_t vmx_pages_vaddr;
int i;
vmx_pages_vaddr = vm_vaddr_alloc(
vm, sizeof(struct vmx_page) * NUM_VMX_PAGES, 0, 0, 0);
vmx_pages = (void *) addr_gva2hva(vm, vmx_pages_vaddr);
for (i = 0; i < NUM_VMX_PAGES; i++)
allocate_vmx_page(&vmx_pages[i]);
return vmx_pages_vaddr;
}
void report(int64_t val) void report(int64_t val)
{ {
printf("IA32_TSC_ADJUST is %ld (%lld * TSC_ADJUST_VALUE + %lld).\n", printf("IA32_TSC_ADJUST is %ld (%lld * TSC_ADJUST_VALUE + %lld).\n",
...@@ -187,7 +143,8 @@ void report(int64_t val) ...@@ -187,7 +143,8 @@ void report(int64_t val)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
vm_vaddr_t vmx_pages_vaddr; struct vmx_pages *vmx_pages;
vm_vaddr_t vmx_pages_gva;
struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1); struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
if (!(entry->ecx & CPUID_VMX)) { if (!(entry->ecx & CPUID_VMX)) {
...@@ -195,23 +152,23 @@ int main(int argc, char *argv[]) ...@@ -195,23 +152,23 @@ int main(int argc, char *argv[])
exit(KSFT_SKIP); exit(KSFT_SKIP);
} }
vm = vm_create_default_vmx(VCPU_ID, (void *) l1_guest_code); vm = vm_create_default(VCPU_ID, (void *) l1_guest_code);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
/* Allocate VMX pages and shared descriptors (vmx_pages). */ /* Allocate VMX pages and shared descriptors (vmx_pages). */
vmx_pages_vaddr = allocate_vmx_pages(); vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_vaddr); vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
for (;;) { for (;;) {
volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID); volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
struct kvm_regs regs; struct kvm_regs regs;
vcpu_run(vm, VCPU_ID); vcpu_run(vm, VCPU_ID);
vcpu_regs_get(vm, VCPU_ID, &regs);
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
"Got exit_reason other than KVM_EXIT_IO: %u (%s),\n", "Got exit_reason other than KVM_EXIT_IO: %u (%s), rip=%lx\n",
run->exit_reason, run->exit_reason,
exit_reason_str(run->exit_reason)); exit_reason_str(run->exit_reason), regs.rip);
vcpu_regs_get(vm, VCPU_ID, &regs);
switch (run->io.port) { switch (run->io.port) {
case PORT_ABORT: case PORT_ABORT:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment