1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2019, Google LLC.
7 * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID.
10 #define _GNU_SOURCE /* for program_invocation_short_name */
16 #include "test_util.h"
20 #include "linux/kvm.h"
22 void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
27 pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
28 num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
30 vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
32 for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
33 /* This asserts that the vCPU was created. */
39 int main(int argc, char *argv[])
41 int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
42 int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
44 pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
45 pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
48 * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
49 * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
53 kvm_max_vcpu_id = kvm_max_vcpus;
55 TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
56 "KVM_MAX_VCPU_ID (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
57 kvm_max_vcpu_id, kvm_max_vcpus);
59 test_vcpu_creation(0, kvm_max_vcpus);
61 if (kvm_max_vcpu_id > kvm_max_vcpus)
63 kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);