aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/kvm/lib/kvm_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/testing/selftests/kvm/lib/kvm_util.c')
-rw-r--r--tools/testing/selftests/kvm/lib/kvm_util.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index e213d513dc61..2cedfda181d4 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -378,7 +378,7 @@ int kvm_memcmp_hva_gva(void *hva,
378 * complicated. This function uses a reasonable default length for 378 * complicated. This function uses a reasonable default length for
379 * the array and performs the appropriate allocation. 379 * the array and performs the appropriate allocation.
380 */ 380 */
381struct kvm_cpuid2 *allocate_kvm_cpuid2(void) 381static struct kvm_cpuid2 *allocate_kvm_cpuid2(void)
382{ 382{
383 struct kvm_cpuid2 *cpuid; 383 struct kvm_cpuid2 *cpuid;
384 int nent = 100; 384 int nent = 100;
@@ -402,17 +402,21 @@ struct kvm_cpuid2 *allocate_kvm_cpuid2(void)
402 * Input Args: None 402 * Input Args: None
403 * 403 *
404 * Output Args: 404 * Output Args:
405 * cpuid - The supported KVM CPUID
406 * 405 *
407 * Return: void 406 * Return: The supported KVM CPUID
408 * 407 *
409 * Get the guest CPUID supported by KVM. 408 * Get the guest CPUID supported by KVM.
410 */ 409 */
411void kvm_get_supported_cpuid(struct kvm_cpuid2 *cpuid) 410struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
412{ 411{
412 static struct kvm_cpuid2 *cpuid;
413 int ret; 413 int ret;
414 int kvm_fd; 414 int kvm_fd;
415 415
416 if (cpuid)
417 return cpuid;
418
419 cpuid = allocate_kvm_cpuid2();
416 kvm_fd = open(KVM_DEV_PATH, O_RDONLY); 420 kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
417 TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i", 421 TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i",
418 KVM_DEV_PATH, kvm_fd, errno); 422 KVM_DEV_PATH, kvm_fd, errno);
@@ -422,6 +426,7 @@ void kvm_get_supported_cpuid(struct kvm_cpuid2 *cpuid)
422 ret, errno); 426 ret, errno);
423 427
424 close(kvm_fd); 428 close(kvm_fd);
429 return cpuid;
425} 430}
426 431
427/* Locate a cpuid entry. 432/* Locate a cpuid entry.
@@ -435,12 +440,13 @@ void kvm_get_supported_cpuid(struct kvm_cpuid2 *cpuid)
435 * Return: A pointer to the cpuid entry. Never returns NULL. 440 * Return: A pointer to the cpuid entry. Never returns NULL.
436 */ 441 */
437struct kvm_cpuid_entry2 * 442struct kvm_cpuid_entry2 *
438find_cpuid_index_entry(struct kvm_cpuid2 *cpuid, uint32_t function, 443kvm_get_supported_cpuid_index(uint32_t function, uint32_t index)
439 uint32_t index)
440{ 444{
445 struct kvm_cpuid2 *cpuid;
441 struct kvm_cpuid_entry2 *entry = NULL; 446 struct kvm_cpuid_entry2 *entry = NULL;
442 int i; 447 int i;
443 448
449 cpuid = kvm_get_supported_cpuid();
444 for (i = 0; i < cpuid->nent; i++) { 450 for (i = 0; i < cpuid->nent; i++) {
445 if (cpuid->entries[i].function == function && 451 if (cpuid->entries[i].function == function &&
446 cpuid->entries[i].index == index) { 452 cpuid->entries[i].index == index) {