1 // SPDX-License-Identifier: GPL-2.0
3 * Test that KVM_GET_MSR_INDEX_LIST and
4 * KVM_GET_MSR_FEATURE_INDEX_LIST work as intended
6 * Copyright (C) 2020, Red Hat, Inc.
12 #include <sys/ioctl.h>
14 #include "test_util.h"
16 #include "processor.h"
18 static int kvm_num_index_msrs(int kvm_fd, int nmsrs)
20 struct kvm_msr_list *list;
23 list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
25 r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
26 TEST_ASSERT(r == -1 && errno == E2BIG,
27 "Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i",
35 static void test_get_msr_index(void)
37 int old_res, res, kvm_fd, r;
38 struct kvm_msr_list *list;
40 kvm_fd = open_kvm_dev_path_or_exit();
42 old_res = kvm_num_index_msrs(kvm_fd, 0);
43 TEST_ASSERT(old_res != 0, "Expecting nmsrs to be > 0");
46 res = kvm_num_index_msrs(kvm_fd, 1);
47 TEST_ASSERT(res > 1, "Expecting nmsrs to be > 1");
48 TEST_ASSERT(res == old_res, "Expecting nmsrs to be identical");
51 list = malloc(sizeof(*list) + old_res * sizeof(list->indices[0]));
52 list->nmsrs = old_res;
53 r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
56 "Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST, r: %i",
58 TEST_ASSERT(list->nmsrs == old_res, "Expecting nmsrs to be identical");
64 static int kvm_num_feature_msrs(int kvm_fd, int nmsrs)
66 struct kvm_msr_list *list;
69 list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
71 r = ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list);
72 TEST_ASSERT(r == -1 && errno == E2BIG,
73 "Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST probe, r: %i",
81 struct kvm_msr_list *kvm_get_msr_feature_list(int kvm_fd, int nmsrs)
83 struct kvm_msr_list *list;
86 list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
88 r = ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list);
91 "Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST, r: %i",
97 static void test_get_msr_feature(void)
99 int res, old_res, i, kvm_fd;
100 struct kvm_msr_list *feature_list;
102 kvm_fd = open_kvm_dev_path_or_exit();
104 old_res = kvm_num_feature_msrs(kvm_fd, 0);
105 TEST_ASSERT(old_res != 0, "Expecting nmsrs to be > 0");
108 res = kvm_num_feature_msrs(kvm_fd, 1);
109 TEST_ASSERT(res > 1, "Expecting nmsrs to be > 1");
110 TEST_ASSERT(res == old_res, "Expecting nmsrs to be identical");
113 feature_list = kvm_get_msr_feature_list(kvm_fd, old_res);
114 TEST_ASSERT(old_res == feature_list->nmsrs,
115 "Unmatching number of msr indexes");
117 for (i = 0; i < feature_list->nmsrs; i++)
118 kvm_get_feature_msr(feature_list->indices[i]);
124 int main(int argc, char *argv[])
126 if (kvm_check_cap(KVM_CAP_GET_MSR_FEATURES))
127 test_get_msr_feature();
129 test_get_msr_index();