Skip to contentSkip to conversation

selftests/kvm: add set_boot_cpu_id test

PATCH In review Emanuele Giuseppe Esposito 2 participants 2 messages Mar 17, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/kvm: add set_boot_cpu_id test

Test for the KVM_SET_BOOT_CPU_ID ioctl.
Check that it correctly allows to change the BSP vcpu.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/x86_64/set_boot_cpu_id.c    | 151 ++++++++++++++++++
 3 files changed, 153 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c
tools/testing/selftests/kvm/.gitignore +1 -0
index 32b87cc77c8e..43b8aa82aefe 100644
@@ -5,6 +5,7 @@
 /s390x/resets
 /s390x/sync_regs_test
 /x86_64/cr4_cpuid_sync_test
+/x86_64/set_boot_cpu_id
 /x86_64/debug_regs
 /x86_64/evmcs_test
 /x86_64/get_cpuid_test
tools/testing/selftests/kvm/Makefile +1 -0
index a6d61f451f88..e7b62666e06e 100644
@@ -39,6 +39,7 @@ LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c
 LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c
 
 TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test
+TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id
 TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test
 TEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c new file +151 -0

151 lines not on this page · raw message

Signature · 1 line
2.29.2
maintainer
#1 lore raw
Quoted 4 lines · Emanuele Giuseppe Esposito
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c new file +4 -0
+	printf("vcpu executing...\n");
+	vcpu_run(vm, vcpuid);
+	printf("vcpu executed\n");
+

tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c line 64

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c new file +10 -0
+	switch (get_ucall(vm, vcpuid, &uc)) {
+	case UCALL_SYNC:
+		printf("stage %d sync %ld\n", stage, uc.args[1]);
+		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
+			    uc.args[1] == stage + 1,
+			    "Stage %d: Unexpected register values vmexit, got %lx",
+			    stage + 1, (ulong)uc.args[1]);
+		return;
+	case UCALL_DONE:
+		printf("got done\n");

You can remove the printfs here.

Quoted 31 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c new file +31 -0
+		return;
+	case UCALL_ABORT:
+		TEST_ASSERT(false, "%s at %s:%ld\n\tvalues: %#lx, %#lx", (const char *)uc.args[0],
+			    __FILE__, uc.args[1], uc.args[2], uc.args[3]);
+	default:
+		TEST_ASSERT(false, "Unexpected exit: %s",
+			    exit_reason_str(vcpu_state(vm, vcpuid)->exit_reason));
+	}
+}
+
+static void add_x86_vcpu(struct kvm_vm *vm, uint32_t vcpuid, void *code)
+{
+	vm_vcpu_add_default(vm, vcpuid, code);
+	vcpu_set_cpuid(vm, vcpuid, kvm_get_supported_cpuid());
+}
+
+static void run_vm_bsp(uint32_t bsp_vcpu)
+{
+	struct kvm_vm *vm;
+	int stage;
+	void *vcpu0_code, *vcpu1_code;
+
+	vm = create_vm();
+
+	vcpu0_code = guest_bsp_vcpu;
+	vcpu1_code = guest_not_bsp_vcpu;
+
+	if (bsp_vcpu == VCPU_ID1) {
+		vcpu0_code = guest_not_bsp_vcpu;
+		vcpu1_code = guest_bsp_vcpu;
+

tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c line 134

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c new file +10 -0
+		vm_ioctl(vm, KVM_SET_BOOT_CPU_ID, (void *) VCPU_ID1);
+	}
+
+	add_x86_vcpu(vm, VCPU_ID0, vcpu0_code);
+	add_x86_vcpu(vm, VCPU_ID1, vcpu1_code);
+
+	for (stage = 0; stage < 2; stage++) {
+		run_vcpu(vm, VCPU_ID0, stage);
+		run_vcpu(vm, VCPU_ID1, stage);
+	}

These are just stylistic nits, but:

1) you could pass a bool to add_x86_vcpu, like

	add_x86_vcpu(vm, VCPU_ID0, bsp_vcpu == VCPU_ID0);
	add_x86_vcpu(vm, VCPU_ID1, bsp_vcpu == VCPU_ID1);

instead of having the vcpu0_code and vcpu1_code pointers.

2) you could move the for loop inside run_vcpu. This way you can assert that you get UCALL_DONE when stage == 2.

tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c line 148

Quoted 13 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c new file +13 -0
+	kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+	if (!kvm_check_cap(KVM_CAP_SET_BOOT_CPU_ID)) {
+		print_skip("set_boot_cpu_id not available");
+		return 0;
+	}
+
+	run_vm_bsp(VCPU_ID0);
+	run_vm_bsp(VCPU_ID1);
+	run_vm_bsp(VCPU_ID0);

Looks good! Just another small change: since this third test is not adding much, you could instead invoke KVM_SET_BOOT_CPU_ID(VCPU_ID1) *after* the creation of the vCPUs instead of before. This should fail with -EBUSY, and vcpu 0 will remain the BSP.

Paolo