root/tools/testing/selftests/kvm/kvm_create_max_vcpus.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_vcpu_creation
  2. main

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * kvm_create_max_vcpus
   4  *
   5  * Copyright (C) 2019, Google LLC.
   6  *
   7  * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID.
   8  */
   9 
  10 #define _GNU_SOURCE /* for program_invocation_short_name */
  11 #include <fcntl.h>
  12 #include <stdio.h>
  13 #include <stdlib.h>
  14 #include <string.h>
  15 
  16 #include "test_util.h"
  17 
  18 #include "kvm_util.h"
  19 #include "asm/kvm.h"
  20 #include "linux/kvm.h"
  21 
  22 void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
  23 {
  24         struct kvm_vm *vm;
  25         int i;
  26 
  27         printf("Testing creating %d vCPUs, with IDs %d...%d.\n",
  28                num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
  29 
  30         vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
  31 
  32         for (i = 0; i < num_vcpus; i++) {
  33                 int vcpu_id = first_vcpu_id + i;
  34 
  35                 /* This asserts that the vCPU was created. */
  36                 vm_vcpu_add(vm, vcpu_id);
  37         }
  38 
  39         kvm_vm_free(vm);
  40 }
  41 
  42 int main(int argc, char *argv[])
  43 {
  44         int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
  45         int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
  46 
  47         printf("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
  48         printf("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
  49 
  50         /*
  51          * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
  52          * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
  53          * in this case.
  54          */
  55         if (!kvm_max_vcpu_id)
  56                 kvm_max_vcpu_id = kvm_max_vcpus;
  57 
  58         TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
  59                     "KVM_MAX_VCPU_ID (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
  60                     kvm_max_vcpu_id, kvm_max_vcpus);
  61 
  62         test_vcpu_creation(0, kvm_max_vcpus);
  63 
  64         if (kvm_max_vcpu_id > kvm_max_vcpus)
  65                 test_vcpu_creation(
  66                         kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
  67 
  68         return 0;
  69 }

/* [<][>][^][v][top][bottom][index][help] */