1 // SPDX-License-Identifier: GPL-2.0
3 * Test that KVM_SET_BOOT_CPU_ID works as intended
5 * Copyright (C) 2020, Red Hat, Inc.
11 #include <sys/ioctl.h>
13 #include "test_util.h"
15 #include "processor.h"
18 static void guest_bsp_vcpu(void *arg
)
22 GUEST_ASSERT_NE(get_bsp_flag(), 0);
27 static void guest_not_bsp_vcpu(void *arg
)
31 GUEST_ASSERT_EQ(get_bsp_flag(), 0);
36 static void test_set_invalid_bsp(struct kvm_vm
*vm
)
38 unsigned long max_vcpu_id
= vm_check_cap(vm
, KVM_CAP_MAX_VCPU_ID
);
42 r
= __vm_ioctl(vm
, KVM_SET_BOOT_CPU_ID
, (void *)(max_vcpu_id
+ 1));
43 TEST_ASSERT(r
== -1 && errno
== EINVAL
, "BSP with ID > MAX should fail");
46 r
= __vm_ioctl(vm
, KVM_SET_BOOT_CPU_ID
, (void *)(1L << 32));
47 TEST_ASSERT(r
== -1 && errno
== EINVAL
, "BSP with ID[63:32]!=0 should fail");
50 static void test_set_bsp_busy(struct kvm_vcpu
*vcpu
, const char *msg
)
52 int r
= __vm_ioctl(vcpu
->vm
, KVM_SET_BOOT_CPU_ID
,
53 (void *)(unsigned long)vcpu
->id
);
55 TEST_ASSERT(r
== -1 && errno
== EBUSY
, "KVM_SET_BOOT_CPU_ID set %s", msg
);
58 static void run_vcpu(struct kvm_vcpu
*vcpu
)
63 for (stage
= 0; stage
< 2; stage
++) {
67 switch (get_ucall(vcpu
, &uc
)) {
69 TEST_ASSERT(!strcmp((const char *)uc
.args
[0], "hello") &&
70 uc
.args
[1] == stage
+ 1,
71 "Stage %d: Unexpected register values vmexit, got %lx",
72 stage
+ 1, (ulong
)uc
.args
[1]);
73 test_set_bsp_busy(vcpu
, "while running vm");
76 TEST_ASSERT(stage
== 1,
77 "Expected GUEST_DONE in stage 2, got stage %d",
81 REPORT_GUEST_ASSERT(uc
);
83 TEST_ASSERT(false, "Unexpected exit: %s",
84 exit_reason_str(vcpu
->run
->exit_reason
));
89 static struct kvm_vm
*create_vm(uint32_t nr_vcpus
, uint32_t bsp_vcpu_id
,
90 struct kvm_vcpu
*vcpus
[])
95 vm
= vm_create(nr_vcpus
);
97 test_set_invalid_bsp(vm
);
99 vm_ioctl(vm
, KVM_SET_BOOT_CPU_ID
, (void *)(unsigned long)bsp_vcpu_id
);
101 for (i
= 0; i
< nr_vcpus
; i
++)
102 vcpus
[i
] = vm_vcpu_add(vm
, i
, i
== bsp_vcpu_id
? guest_bsp_vcpu
:
107 static void run_vm_bsp(uint32_t bsp_vcpu_id
)
109 struct kvm_vcpu
*vcpus
[2];
112 vm
= create_vm(ARRAY_SIZE(vcpus
), bsp_vcpu_id
, vcpus
);
120 static void check_set_bsp_busy(void)
122 struct kvm_vcpu
*vcpus
[2];
125 vm
= create_vm(ARRAY_SIZE(vcpus
), 0, vcpus
);
127 test_set_bsp_busy(vcpus
[1], "after adding vcpu");
132 test_set_bsp_busy(vcpus
[1], "to a terminated vcpu");
137 int main(int argc
, char *argv
[])
139 TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_BOOT_CPU_ID
));
145 check_set_bsp_busy();