1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * maximum APIC ID capability tests
4 *
5 * Copyright (C) 2022, Intel, Inc.
6 *
7 * Tests for getting/setting maximum APIC ID capability
8 */
9
10 #include "kvm_util.h"
11
12 #define MAX_VCPU_ID 2
13
main(int argc,char * argv[])14 int main(int argc, char *argv[])
15 {
16 struct kvm_vm *vm;
17 int ret;
18
19 vm = vm_create_barebones();
20
21 /* Get KVM_CAP_MAX_VCPU_ID cap supported in KVM */
22 ret = vm_check_cap(vm, KVM_CAP_MAX_VCPU_ID);
23
24 /* Try to set KVM_CAP_MAX_VCPU_ID beyond KVM cap */
25 ret = __vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, ret + 1);
26 TEST_ASSERT(ret < 0,
27 "Setting KVM_CAP_MAX_VCPU_ID beyond KVM cap should fail");
28
29 /* Test BOOT_CPU_ID interaction (MAX_VCPU_ID cannot be lower) */
30 if (kvm_has_cap(KVM_CAP_SET_BOOT_CPU_ID)) {
31 vm_ioctl(vm, KVM_SET_BOOT_CPU_ID, (void *)MAX_VCPU_ID);
32
33 /* Try setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID */
34 ret = __vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID - 1);
35 TEST_ASSERT(ret < 0,
36 "Setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID should fail");
37 }
38
39 /* Set KVM_CAP_MAX_VCPU_ID */
40 vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID);
41
42 /* Try to set KVM_CAP_MAX_VCPU_ID again */
43 ret = __vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID + 1);
44 TEST_ASSERT(ret < 0,
45 "Setting KVM_CAP_MAX_VCPU_ID multiple times should fail");
46
47 /* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap */
48 ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)MAX_VCPU_ID);
49 TEST_ASSERT(ret < 0, "Creating vCPU with ID > MAX_VCPU_ID should fail");
50
51 /* Create vCPU with bits 63:32 != 0, but an otherwise valid id */
52 ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(1L << 32));
53 TEST_ASSERT(ret < 0, "Creating vCPU with ID[63:32] != 0 should fail");
54
55 /* Create vCPU with id within bounds */
56 ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)0);
57 TEST_ASSERT(ret >= 0, "Creating vCPU with ID 0 should succeed");
58
59 close(ret);
60 kvm_vm_free(vm);
61 return 0;
62 }
63