-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests: kvm: add get_msr_index_features
Test the KVM_GET_MSR_FEATURE_INDEX_LIST and KVM_GET_MSR_INDEX_LIST ioctls. Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com> Message-Id: <20210318145629.486450-1-eesposit@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
- Loading branch information
Emanuele Giuseppe Esposito
authored and
Paolo Bonzini
committed
Mar 18, 2021
1 parent
2c7f76b
commit 77a3aa2
Showing
5 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
#include "sparsebit.h" | ||
|
||
#define KVM_DEV_PATH "/dev/kvm" | ||
#define KVM_MAX_VCPUS 512 | ||
|
||
/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
tools/testing/selftests/kvm/x86_64/get_msr_index_features.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* | ||
* Test that KVM_GET_MSR_INDEX_LIST and | ||
* KVM_GET_MSR_FEATURE_INDEX_LIST work as intended | ||
* | ||
* Copyright (C) 2020, Red Hat, Inc. | ||
*/ | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/ioctl.h> | ||
|
||
#include "test_util.h" | ||
#include "kvm_util.h" | ||
#include "processor.h" | ||
|
||
static int kvm_num_index_msrs(int kvm_fd, int nmsrs) | ||
{ | ||
struct kvm_msr_list *list; | ||
int r; | ||
|
||
list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0])); | ||
list->nmsrs = nmsrs; | ||
r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list); | ||
TEST_ASSERT(r == -1 && errno == E2BIG, | ||
"Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i", | ||
r); | ||
|
||
r = list->nmsrs; | ||
free(list); | ||
return r; | ||
} | ||
|
||
static void test_get_msr_index(void) | ||
{ | ||
int old_res, res, kvm_fd, r; | ||
struct kvm_msr_list *list; | ||
|
||
kvm_fd = open(KVM_DEV_PATH, O_RDONLY); | ||
if (kvm_fd < 0) | ||
exit(KSFT_SKIP); | ||
|
||
old_res = kvm_num_index_msrs(kvm_fd, 0); | ||
TEST_ASSERT(old_res != 0, "Expecting nmsrs to be > 0"); | ||
|
||
if (old_res != 1) { | ||
res = kvm_num_index_msrs(kvm_fd, 1); | ||
TEST_ASSERT(res > 1, "Expecting nmsrs to be > 1"); | ||
TEST_ASSERT(res == old_res, "Expecting nmsrs to be identical"); | ||
} | ||
|
||
list = malloc(sizeof(*list) + old_res * sizeof(list->indices[0])); | ||
list->nmsrs = old_res; | ||
r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list); | ||
|
||
TEST_ASSERT(r == 0, | ||
"Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST, r: %i", | ||
r); | ||
TEST_ASSERT(list->nmsrs == old_res, "Expecting nmsrs to be identical"); | ||
free(list); | ||
|
||
close(kvm_fd); | ||
} | ||
|
||
static int kvm_num_feature_msrs(int kvm_fd, int nmsrs) | ||
{ | ||
struct kvm_msr_list *list; | ||
int r; | ||
|
||
list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0])); | ||
list->nmsrs = nmsrs; | ||
r = ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list); | ||
TEST_ASSERT(r == -1 && errno == E2BIG, | ||
"Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST probe, r: %i", | ||
r); | ||
|
||
r = list->nmsrs; | ||
free(list); | ||
return r; | ||
} | ||
|
||
struct kvm_msr_list *kvm_get_msr_feature_list(int kvm_fd, int nmsrs) | ||
{ | ||
struct kvm_msr_list *list; | ||
int r; | ||
|
||
list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0])); | ||
list->nmsrs = nmsrs; | ||
r = ioctl(kvm_fd, KVM_GET_MSR_FEATURE_INDEX_LIST, list); | ||
|
||
TEST_ASSERT(r == 0, | ||
"Unexpected result from KVM_GET_MSR_FEATURE_INDEX_LIST, r: %i", | ||
r); | ||
|
||
return list; | ||
} | ||
|
||
static void test_get_msr_feature(void) | ||
{ | ||
int res, old_res, i, kvm_fd; | ||
struct kvm_msr_list *feature_list; | ||
|
||
kvm_fd = open(KVM_DEV_PATH, O_RDONLY); | ||
if (kvm_fd < 0) | ||
exit(KSFT_SKIP); | ||
|
||
old_res = kvm_num_feature_msrs(kvm_fd, 0); | ||
TEST_ASSERT(old_res != 0, "Expecting nmsrs to be > 0"); | ||
|
||
if (old_res != 1) { | ||
res = kvm_num_feature_msrs(kvm_fd, 1); | ||
TEST_ASSERT(res > 1, "Expecting nmsrs to be > 1"); | ||
TEST_ASSERT(res == old_res, "Expecting nmsrs to be identical"); | ||
} | ||
|
||
feature_list = kvm_get_msr_feature_list(kvm_fd, old_res); | ||
TEST_ASSERT(old_res == feature_list->nmsrs, | ||
"Unmatching number of msr indexes"); | ||
|
||
for (i = 0; i < feature_list->nmsrs; i++) | ||
kvm_get_feature_msr(feature_list->indices[i]); | ||
|
||
free(feature_list); | ||
close(kvm_fd); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (kvm_check_cap(KVM_CAP_GET_MSR_FEATURES)) | ||
test_get_msr_feature(); | ||
|
||
test_get_msr_index(); | ||
} |