kvm: selftests: Add KVM_CAP_MAX_VCPU_ID cap test

commit 753dcf7a86 upstream.

Basic test coverage of KVM_CAP_MAX_VCPU_ID cap.

This capability can be enabled before vCPU creation and only allowed
to set once. if assigned vcpu id is beyond KVM_CAP_MAX_VCPU_ID
capability, vCPU creation will fail.

Signed-off-by: Zeng Guang <guang.zeng@intel.com>
Message-Id: <20220422134456.26655-1-guang.zeng@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Chen Zhuo <sagazchen@tencent.com>
Signed-off-by: Xinghui Li <korantli@tencent.com>
This commit is contained in:
Zhuo Chen 2022-08-16 11:24:33 +08:00 committed by Jianping Liu
parent 2abb5f6526
commit 15139c8538
3 changed files with 56 additions and 0 deletions

View File

@ -3,6 +3,7 @@
/x86_64/cr4_cpuid_sync_test
/x86_64/evmcs_test
/x86_64/hyperv_cpuid
/x86_64/max_vcpuid_cap_test
/x86_64/mmio_warning_test
/x86_64/platform_info_test
/x86_64/set_sregs_test

View File

@ -26,6 +26,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test
TEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test
TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test
TEST_GEN_PROGS_x86_64 += x86_64/amx_test
TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test
TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
TEST_GEN_PROGS_x86_64 += dirty_log_test
TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus

View File

@ -0,0 +1,54 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* maximum APIC ID capability tests
*
* Copyright (C) 2022, Intel, Inc.
*
* Tests for getting/setting maximum APIC ID capability
*/
#include "kvm_util.h"
#include "../lib/kvm_util_internal.h"
#define MAX_VCPU_ID 2
int main(int argc, char *argv[])
{
struct kvm_vm *vm;
struct kvm_enable_cap cap = { 0 };
int ret;
vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);
/* Get KVM_CAP_MAX_VCPU_ID cap supported in KVM */
ret = vm_check_cap(vm, KVM_CAP_MAX_VCPU_ID);
/* Try to set KVM_CAP_MAX_VCPU_ID beyond KVM cap */
cap.cap = KVM_CAP_MAX_VCPU_ID;
cap.args[0] = ret + 1;
ret = ioctl(vm->fd, KVM_ENABLE_CAP, &cap);
TEST_ASSERT(ret < 0,
"Unexpected success to enable KVM_CAP_MAX_VCPU_ID"
"beyond KVM cap!\n");
/* Set KVM_CAP_MAX_VCPU_ID */
cap.cap = KVM_CAP_MAX_VCPU_ID;
cap.args[0] = MAX_VCPU_ID;
ret = ioctl(vm->fd, KVM_ENABLE_CAP, &cap);
TEST_ASSERT(ret == 0,
"Unexpected failure to enable KVM_CAP_MAX_VCPU_ID!\n");
/* Try to set KVM_CAP_MAX_VCPU_ID again */
cap.args[0] = MAX_VCPU_ID + 1;
ret = ioctl(vm->fd, KVM_ENABLE_CAP, &cap);
TEST_ASSERT(ret < 0,
"Unexpected success to enable KVM_CAP_MAX_VCPU_ID again\n");
/* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap*/
ret = ioctl(vm->fd, KVM_CREATE_VCPU, MAX_VCPU_ID);
TEST_ASSERT(ret < 0,
"Unexpected success in creating a vCPU with VCPU ID out of range\n");
kvm_vm_free(vm);
return 0;
}