Commit 65efa61d authored by Paolo Bonzini's avatar Paolo Bonzini

selftests: kvm: provide common function to enable eVMCS

There are two tests already enabling eVMCS and a third is coming.
Add a function that enables the capability and tests the result.
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 92cd0f0b
...@@ -220,6 +220,8 @@ struct hv_enlightened_vmcs { ...@@ -220,6 +220,8 @@ struct hv_enlightened_vmcs {
struct hv_enlightened_vmcs *current_evmcs; struct hv_enlightened_vmcs *current_evmcs;
struct hv_vp_assist_page *current_vp_assist; struct hv_vp_assist_page *current_vp_assist;
int vcpu_enable_evmcs(struct kvm_vm *vm, int vcpu_id);
static inline int enable_vp_assist(uint64_t vp_assist_pa, void *vp_assist) static inline int enable_vp_assist(uint64_t vp_assist_pa, void *vp_assist)
{ {
u64 val = (vp_assist_pa & HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_MASK) | u64 val = (vp_assist_pa & HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_MASK) |
......
...@@ -12,6 +12,26 @@ ...@@ -12,6 +12,26 @@
bool enable_evmcs; bool enable_evmcs;
int vcpu_enable_evmcs(struct kvm_vm *vm, int vcpu_id)
{
uint16_t evmcs_ver;
struct kvm_enable_cap enable_evmcs_cap = {
.cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
.args[0] = (unsigned long)&evmcs_ver
};
vcpu_ioctl(vm, vcpu_id, KVM_ENABLE_CAP, &enable_evmcs_cap);
/* KVM should return supported EVMCS version range */
TEST_ASSERT(((evmcs_ver >> 8) >= (evmcs_ver & 0xff)) &&
(evmcs_ver & 0xff) > 0,
"Incorrect EVMCS version range: %x:%x\n",
evmcs_ver & 0xff, evmcs_ver >> 8);
return evmcs_ver;
}
/* Allocate memory regions for nested VMX tests. /* Allocate memory regions for nested VMX tests.
* *
* Input Args: * Input Args:
......
...@@ -79,11 +79,6 @@ int main(int argc, char *argv[]) ...@@ -79,11 +79,6 @@ int main(int argc, char *argv[])
struct kvm_x86_state *state; struct kvm_x86_state *state;
struct ucall uc; struct ucall uc;
int stage; int stage;
uint16_t evmcs_ver;
struct kvm_enable_cap enable_evmcs_cap = {
.cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
.args[0] = (unsigned long)&evmcs_ver
};
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); vm = vm_create_default(VCPU_ID, 0, guest_code);
...@@ -96,13 +91,7 @@ int main(int argc, char *argv[]) ...@@ -96,13 +91,7 @@ int main(int argc, char *argv[])
exit(KSFT_SKIP); exit(KSFT_SKIP);
} }
vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap); vcpu_enable_evmcs(vm, VCPU_ID);
/* KVM should return supported EVMCS version range */
TEST_ASSERT(((evmcs_ver >> 8) >= (evmcs_ver & 0xff)) &&
(evmcs_ver & 0xff) > 0,
"Incorrect EVMCS version range: %x:%x\n",
evmcs_ver & 0xff, evmcs_ver >> 8);
run = vcpu_state(vm, VCPU_ID); run = vcpu_state(vm, VCPU_ID);
...@@ -146,7 +135,7 @@ int main(int argc, char *argv[]) ...@@ -146,7 +135,7 @@ int main(int argc, char *argv[])
kvm_vm_restart(vm, O_RDWR); kvm_vm_restart(vm, O_RDWR);
vm_vcpu_add(vm, VCPU_ID); vm_vcpu_add(vm, VCPU_ID);
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid()); vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap); vcpu_enable_evmcs(vm, VCPU_ID);
vcpu_load_state(vm, VCPU_ID, state); vcpu_load_state(vm, VCPU_ID, state);
run = vcpu_state(vm, VCPU_ID); run = vcpu_state(vm, VCPU_ID);
free(state); free(state);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "test_util.h" #include "test_util.h"
#include "kvm_util.h" #include "kvm_util.h"
#include "processor.h" #include "processor.h"
#include "vmx.h"
#define VCPU_ID 0 #define VCPU_ID 0
...@@ -106,12 +107,7 @@ int main(int argc, char *argv[]) ...@@ -106,12 +107,7 @@ int main(int argc, char *argv[])
{ {
struct kvm_vm *vm; struct kvm_vm *vm;
int rv; int rv;
uint16_t evmcs_ver;
struct kvm_cpuid2 *hv_cpuid_entries; struct kvm_cpuid2 *hv_cpuid_entries;
struct kvm_enable_cap enable_evmcs_cap = {
.cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
.args[0] = (unsigned long)&evmcs_ver
};
/* Tell stdout not to buffer its content */ /* Tell stdout not to buffer its content */
setbuf(stdout, NULL); setbuf(stdout, NULL);
...@@ -136,14 +132,14 @@ int main(int argc, char *argv[]) ...@@ -136,14 +132,14 @@ int main(int argc, char *argv[])
free(hv_cpuid_entries); free(hv_cpuid_entries);
rv = _vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap); if (!kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
if (rv) {
fprintf(stderr, fprintf(stderr,
"Enlightened VMCS is unsupported, skip related test\n"); "Enlightened VMCS is unsupported, skip related test\n");
goto vm_free; goto vm_free;
} }
vcpu_enable_evmcs(vm, VCPU_ID);
hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm); hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
if (!hv_cpuid_entries) if (!hv_cpuid_entries)
return 1; return 1;
......
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