cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

kvm_create_max_vcpus.c (2502B)


      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#include <sys/resource.h>
     16
     17#include "test_util.h"
     18
     19#include "kvm_util.h"
     20#include "asm/kvm.h"
     21#include "linux/kvm.h"
     22
     23void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
     24{
     25	struct kvm_vm *vm;
     26	int i;
     27
     28	pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
     29		num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
     30
     31	vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
     32
     33	for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
     34		/* This asserts that the vCPU was created. */
     35		vm_vcpu_add(vm, i);
     36
     37	kvm_vm_free(vm);
     38}
     39
     40int main(int argc, char *argv[])
     41{
     42	int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
     43	int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
     44	/*
     45	 * Number of file descriptors reqired, KVM_CAP_MAX_VCPUS for vCPU fds +
     46	 * an arbitrary number for everything else.
     47	 */
     48	int nr_fds_wanted = kvm_max_vcpus + 100;
     49	struct rlimit rl;
     50
     51	pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
     52	pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
     53
     54	/*
     55	 * Check that we're allowed to open nr_fds_wanted file descriptors and
     56	 * try raising the limits if needed.
     57	 */
     58	TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
     59
     60	if (rl.rlim_cur < nr_fds_wanted) {
     61		rl.rlim_cur = nr_fds_wanted;
     62		if (rl.rlim_max < nr_fds_wanted) {
     63			int old_rlim_max = rl.rlim_max;
     64			rl.rlim_max = nr_fds_wanted;
     65
     66			int r = setrlimit(RLIMIT_NOFILE, &rl);
     67			if (r < 0) {
     68				printf("RLIMIT_NOFILE hard limit is too low (%d, wanted %d)\n",
     69				       old_rlim_max, nr_fds_wanted);
     70				exit(KSFT_SKIP);
     71			}
     72		} else {
     73			TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
     74		}
     75	}
     76
     77	/*
     78	 * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
     79	 * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
     80	 * in this case.
     81	 */
     82	if (!kvm_max_vcpu_id)
     83		kvm_max_vcpu_id = kvm_max_vcpus;
     84
     85	TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
     86		    "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
     87		    kvm_max_vcpu_id, kvm_max_vcpus);
     88
     89	test_vcpu_creation(0, kvm_max_vcpus);
     90
     91	if (kvm_max_vcpu_id > kvm_max_vcpus)
     92		test_vcpu_creation(
     93			kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
     94
     95	return 0;
     96}