[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20201025185334.389061-2-peterx@redhat.com>
Date: Sun, 25 Oct 2020 14:53:33 -0400
From: Peter Xu <peterx@...hat.com>
To: linux-kernel@...r.kernel.org, kvm@...r.kernel.org
Cc: Vitaly Kuznetsov <vkuznets@...hat.com>, peterx@...hat.com,
Sean Christopherson <sean.j.christopherson@...el.com>,
Paolo Bonzini <pbonzini@...hat.com>
Subject: [PATCH 1/2] KVM: selftests: Add get featured msrs test case
Try to fetch any supported featured msr. Currently it won't fail, so at least
we can check against valid ones (which should be >0).
This reproduces [1] too by trying to fetch one invalid msr there.
[1] https://bugzilla.kernel.org/show_bug.cgi?id=209845
Signed-off-by: Peter Xu <peterx@...hat.com>
---
.../testing/selftests/kvm/include/kvm_util.h | 3 +
tools/testing/selftests/kvm/lib/kvm_util.c | 14 +++++
.../testing/selftests/kvm/x86_64/state_test.c | 58 +++++++++++++++++++
3 files changed, 75 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 919e161dd289..e34cf263b20a 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -66,6 +66,9 @@ int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);
struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm);
struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm);
+void kvm_vm_get_msr_feature_index_list(struct kvm_vm *vm,
+ struct kvm_msr_list *list);
+int kvm_vm_get_feature_msrs(struct kvm_vm *vm, struct kvm_msrs *msrs);
void kvm_vm_free(struct kvm_vm *vmp);
void kvm_vm_restart(struct kvm_vm *vmp, int perm);
void kvm_vm_release(struct kvm_vm *vmp);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 74776ee228f2..3c16fa044335 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -132,6 +132,20 @@ static const struct vm_guest_mode_params vm_guest_mode_params[] = {
_Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params) == NUM_VM_MODES,
"Missing new mode params?");
+void kvm_vm_get_msr_feature_index_list(struct kvm_vm *vm,
+ struct kvm_msr_list *list)
+{
+ int r = ioctl(vm->kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list);
+
+ TEST_ASSERT(r == 0, "KVM_GET_MSR_FEATURE_INDEX_LIST failed: %d\n",
+ -errno);
+}
+
+int kvm_vm_get_feature_msrs(struct kvm_vm *vm, struct kvm_msrs *msrs)
+{
+ return ioctl(vm->kvm_fd, KVM_GET_MSRS, msrs);
+}
+
/*
* VM Create
*
diff --git a/tools/testing/selftests/kvm/x86_64/state_test.c b/tools/testing/selftests/kvm/x86_64/state_test.c
index f6c8b9042f8a..7ce9920e526a 100644
--- a/tools/testing/selftests/kvm/x86_64/state_test.c
+++ b/tools/testing/selftests/kvm/x86_64/state_test.c
@@ -152,6 +152,61 @@ static void __attribute__((__flatten__)) guest_code(void *arg)
GUEST_DONE();
}
+#define KVM_MSR_FEATURE_N 64
+
+static int test_kvm_get_feature_msr_one(struct kvm_vm *vm, __u32 index,
+ struct kvm_msrs *msrs)
+{
+ msrs->nmsrs = 1;
+ msrs->entries[0].index = index;
+ return kvm_vm_get_feature_msrs(vm, msrs);
+}
+
+static void test_kvm_get_msr_features(struct kvm_vm *vm)
+{
+ struct kvm_msr_list *msr_list;
+ struct kvm_msrs *msrs;
+ int i, ret, sum;
+
+ if (!kvm_check_cap(KVM_CAP_GET_MSR_FEATURES)) {
+ pr_info("skipping kvm get msr features test\n");
+ return;
+ }
+
+ msr_list = calloc(1, sizeof(struct kvm_msr_list) +
+ sizeof(__u32) * KVM_MSR_FEATURE_N);
+ msr_list->nmsrs = KVM_MSR_FEATURE_N;
+
+ TEST_ASSERT(msr_list, "msr_list allocation failed\n");
+
+ kvm_vm_get_msr_feature_index_list(vm, msr_list);
+
+ msrs = calloc(1, sizeof(struct kvm_msrs) +
+ sizeof(struct kvm_msr_entry));
+
+ TEST_ASSERT(msrs, "msr entries allocation failed\n");
+
+ sum = 0;
+ for (i = 0; i < msr_list->nmsrs; i++) {
+ ret = test_kvm_get_feature_msr_one(vm, msr_list->indices[i],
+ msrs);
+ TEST_ASSERT(ret >= 0, "KVM_GET_MSR failed: %d\n", ret);
+ sum += ret;
+ }
+ TEST_ASSERT(sum > 0, "KVM_GET_MSR has no feature msr\n");
+
+ /*
+ * Test invalid msr. Note the retcode can be either 0 or 1 depending
+ * on kvm.ignore_msrs
+ */
+ ret = test_kvm_get_feature_msr_one(vm, (__u32)-1, msrs);
+ TEST_ASSERT(ret >= 0 && ret <= 1,
+ "KVM_GET_MSR on invalid msr error: %d\n", ret);
+
+ free(msrs);
+ free(msr_list);
+}
+
int main(int argc, char *argv[])
{
vm_vaddr_t nested_gva = 0;
@@ -168,6 +223,9 @@ int main(int argc, char *argv[])
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
run = vcpu_state(vm, VCPU_ID);
+ /* Test KVM_GET_MSR for VM */
+ test_kvm_get_msr_features(vm);
+
vcpu_regs_get(vm, VCPU_ID, ®s1);
if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
--
2.26.2
Powered by blists - more mailing lists