Skip to contentSkip to conversation

KVM: arm64: selftest: Add standalone test checking for KVM's own UUID

PATCH Superseded Marc Zyngier 3 participants 5 messages Jul 21, 2025 – Aug 6, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 KVM: arm64: selftest: Add standalone test checking for KVM's own UUID

Tinkering with UUIDs is a perilious task, and the KVM UUID gets broken at times. In order to spot this early enough, add a selftest that will shout if the expected value isn't found.

---
 tools/testing/selftests/kvm/Makefile.kvm     |  1 +
 tools/testing/selftests/kvm/arm64/kvm-uuid.c | 67 ++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/kvm-uuid.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index ce817a975e50a..e1eb1ba238a2a 100644
@@ -167,6 +167,7 @@ TEST_GEN_PROGS_arm64 += arm64/vgic_irq
 TEST_GEN_PROGS_arm64 += arm64/vgic_lpi_stress
 TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access
 TEST_GEN_PROGS_arm64 += arm64/no-vgic-v3
+TEST_GEN_PROGS_arm64 += arm64/kvm-uuid
 TEST_GEN_PROGS_arm64 += access_tracking_perf_test
 TEST_GEN_PROGS_arm64 += arch_timer
 TEST_GEN_PROGS_arm64 += coalesced_io_test
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.39.2
#1 lore raw

Hi Marc,

Quoted 46 lines · Marc Zyngier

Tinkering with UUIDs is a perilious task, and the KVM UUID gets broken at times. In order to spot this early enough, add a selftest that will shout if the expected value isn't found.

---
 tools/testing/selftests/kvm/Makefile.kvm     |  1 +
 tools/testing/selftests/kvm/arm64/kvm-uuid.c | 67 ++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/kvm-uuid.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index ce817a975e50a..e1eb1ba238a2a 100644
@@ -167,6 +167,7 @@ TEST_GEN_PROGS_arm64 += arm64/vgic_irq
 TEST_GEN_PROGS_arm64 += arm64/vgic_lpi_stress
 TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access
 TEST_GEN_PROGS_arm64 += arm64/no-vgic-v3
+TEST_GEN_PROGS_arm64 += arm64/kvm-uuid
 TEST_GEN_PROGS_arm64 += access_tracking_perf_test
 TEST_GEN_PROGS_arm64 += arch_timer
 TEST_GEN_PROGS_arm64 += coalesced_io_test
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +16 -0
new file mode 100644
index 0000000000000..89d9c8b182ae5
@@ -0,0 +1,67 @@
+#include <errno.h>
+#include <linux/arm-smccc.h>
+#include <asm/kvm.h>
+#include <kvm_util.h>
+
+#include "processor.h"
+
+/*
+ * Do NOT redefine these constants, or try to replace them with some
+ * "common" version. They are hardcoded here to detect any potential
+ * breakage happening in the rest of the kernel.
+ *
+ * KVM UID value: 28b46fb6-2ec5-11e9-a9ca-4b564d003a74
+ */
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0	0xb66fb428U
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1	0xe911c52eU

tools/testing/selftests/kvm/arm64/kvm-uuid.c line 26

Quoted 10 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +10 -0
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2	0x564bcaa9U
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3	0x743a004dU
+
+static void guest_code(void)
+{
+	struct arm_smccc_res res = {};
+
+	smccc_hvc(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, 0, 0, 0, 0, 0, 0, 0, &res);
+
+	__GUEST_ASSERT(res.a0 != SMCCC_RET_NOT_SUPPORTED, "a0 = %lx\n", res.a0);

Should this check res.a0 == SMCCC_RET_SUCCESS instead?

Quoted 26 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +26 -0
+	__GUEST_ASSERT(res.a0 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 &&
+		       res.a1 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 &&
+		       res.a2 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 &&
+		       res.a3 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3,
+		       "Unexpected KVM-specific UID %lx %lx %lx %lx\n", res.a0, res.a1, res.a2, res.a3);
+	GUEST_DONE();
+}
+
+int main (int argc, char *argv[])
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	bool guest_done = false;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+	while (!guest_done) {
+		vcpu_run(vcpu);
+
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_SYNC:
+			break;
+		case UCALL_DONE:
+			guest_done = true;
+			break;

tools/testing/selftests/kvm/arm64/kvm-uuid.c line 62

Quoted 10 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +10 -0
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+			break;
+		case UCALL_PRINTF:
+			printf("%s", uc.buffer);
+			break;
+		default:
+			TEST_FAIL("Unexpected guest exit");
+		}
+	}

This is becoming a very common and useful pattern. I wonder if it's time for a ucall helper

static void ucall_vcpu_run(struct kvm_vcpu *vcpu,
void (*sync_func)(struct kvm_vcpu *, void *), void *sync_data)

{
        bool guest_done = false;
        struct ucall uc;

        while (!guest_done) {
                vcpu_run(vcpu);

                switch (get_ucall(vcpu, &uc)) {
                case UCALL_SYNC:
                        if (sync_func)
                                sync_func(vcpu, sync_data);
                        break;
                case UCALL_DONE:
                        guest_done = true;
                        break;
                case UCALL_ABORT:
                        REPORT_GUEST_ASSERT(uc);
                        break;
                case UCALL_PRINTF:
                        printf("%s", uc.buffer);
                        break;
                default:
                        TEST_FAIL("Unexpected guest exit");
                }
        }
}

Thanks,
drew

Quoted 8 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +5 -0
+
+	kvm_vm_free(vm);
+
+	return 0;
+}
Signature · 1 line
2.39.2
author maintainer
#2 lore raw
In reply to Andrew Jones
Quoted 54 lines · Andrew Jones

Hi Marc,

Quoted 50 lines · Marc Zyngier

Tinkering with UUIDs is a perilious task, and the KVM UUID gets broken at times. In order to spot this early enough, add a selftest that will shout if the expected value isn't found.

---
 tools/testing/selftests/kvm/Makefile.kvm     |  1 +
 tools/testing/selftests/kvm/arm64/kvm-uuid.c | 67 ++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/kvm-uuid.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index ce817a975e50a..e1eb1ba238a2a 100644
@@ -167,6 +167,7 @@ TEST_GEN_PROGS_arm64 += arm64/vgic_irq
 TEST_GEN_PROGS_arm64 += arm64/vgic_lpi_stress
 TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access
 TEST_GEN_PROGS_arm64 += arm64/no-vgic-v3
+TEST_GEN_PROGS_arm64 += arm64/kvm-uuid
 TEST_GEN_PROGS_arm64 += access_tracking_perf_test
 TEST_GEN_PROGS_arm64 += arch_timer
 TEST_GEN_PROGS_arm64 += coalesced_io_test
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +20 -0
new file mode 100644
index 0000000000000..89d9c8b182ae5
@@ -0,0 +1,67 @@
+#include <errno.h>
+#include <linux/arm-smccc.h>
+#include <asm/kvm.h>
+#include <kvm_util.h>
+
+#include "processor.h"
+
+/*
+ * Do NOT redefine these constants, or try to replace them with some
+ * "common" version. They are hardcoded here to detect any potential
+ * breakage happening in the rest of the kernel.
+ *
+ * KVM UID value: 28b46fb6-2ec5-11e9-a9ca-4b564d003a74
+ */
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0	0xb66fb428U
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1	0xe911c52eU
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2	0x564bcaa9U
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3	0x743a004dU
+
+static void guest_code(void)
Quoted 8 lines

tools/testing/selftests/kvm/arm64/kvm-uuid.c line 26

Quoted 6 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +6 -0
+{
+	struct arm_smccc_res res = {};
+
+	smccc_hvc(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, 0, 0, 0, 0, 0, 0, 0, &res);
+
+	__GUEST_ASSERT(res.a0 != SMCCC_RET_NOT_SUPPORTED, "a0 = %lx\n", res.a0);

Should this check res.a0 == SMCCC_RET_SUCCESS instead?

Yeah, probably.

Quoted 62 lines
Quoted 27 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +26 -0
+	__GUEST_ASSERT(res.a0 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 &&
+		       res.a1 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 &&
+		       res.a2 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 &&
+		       res.a3 == ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3,
+		       "Unexpected KVM-specific UID %lx %lx %lx %lx\n", res.a0, res.a1, res.a2, res.a3);
+	GUEST_DONE();
+}
+
+int main (int argc, char *argv[])
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	bool guest_done = false;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+	while (!guest_done) {
+		vcpu_run(vcpu);
+
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_SYNC:
+			break;
+		case UCALL_DONE:
+			guest_done = true;
+			break;

tools/testing/selftests/kvm/arm64/kvm-uuid.c line 62

Quoted 10 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +10 -0
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+			break;
+		case UCALL_PRINTF:
+			printf("%s", uc.buffer);
+			break;
+		default:
+			TEST_FAIL("Unexpected guest exit");
+		}
+	}

This is becoming a very common and useful pattern. I wonder if it's time for a ucall helper

static void ucall_vcpu_run(struct kvm_vcpu *vcpu,
void (*sync_func)(struct kvm_vcpu *, void *), void *sync_data)

{
        bool guest_done = false;
        struct ucall uc;

        while (!guest_done) {
                vcpu_run(vcpu);

                switch (get_ucall(vcpu, &uc)) {
                case UCALL_SYNC:
                        if (sync_func)
                                sync_func(vcpu, sync_data);
                        break;
                case UCALL_DONE:
                        guest_done = true;
                        break;
                case UCALL_ABORT:
                        REPORT_GUEST_ASSERT(uc);
                        break;
Quoted 8 lines
                case UCALL_PRINTF:
                        printf("%s", uc.buffer);
                        break;
                default:
                        TEST_FAIL("Unexpected guest exit");
                }
        }
}

Honestly, I don't know. My understanding is that the common kvm selftest code is now mostly a pile of x86-specific stuff, and I've made it a goal not to touch any of it.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author maintainer
#3 lore raw
In reply to Andrew Jones
Quoted 54 lines · Andrew Jones

Hi Marc,

Quoted 50 lines · Marc Zyngier

Tinkering with UUIDs is a perilious task, and the KVM UUID gets broken at times. In order to spot this early enough, add a selftest that will shout if the expected value isn't found.

---
 tools/testing/selftests/kvm/Makefile.kvm     |  1 +
 tools/testing/selftests/kvm/arm64/kvm-uuid.c | 67 ++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/kvm-uuid.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index ce817a975e50a..e1eb1ba238a2a 100644
@@ -167,6 +167,7 @@ TEST_GEN_PROGS_arm64 += arm64/vgic_irq
 TEST_GEN_PROGS_arm64 += arm64/vgic_lpi_stress
 TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access
 TEST_GEN_PROGS_arm64 += arm64/no-vgic-v3
+TEST_GEN_PROGS_arm64 += arm64/kvm-uuid
 TEST_GEN_PROGS_arm64 += access_tracking_perf_test
 TEST_GEN_PROGS_arm64 += arch_timer
 TEST_GEN_PROGS_arm64 += coalesced_io_test
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +20 -0
new file mode 100644
index 0000000000000..89d9c8b182ae5
@@ -0,0 +1,67 @@
+#include <errno.h>
+#include <linux/arm-smccc.h>
+#include <asm/kvm.h>
+#include <kvm_util.h>
+
+#include "processor.h"
+
+/*
+ * Do NOT redefine these constants, or try to replace them with some
+ * "common" version. They are hardcoded here to detect any potential
+ * breakage happening in the rest of the kernel.
+ *
+ * KVM UID value: 28b46fb6-2ec5-11e9-a9ca-4b564d003a74
+ */
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0	0xb66fb428U
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1	0xe911c52eU
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2	0x564bcaa9U
+#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3	0x743a004dU
+
+static void guest_code(void)
Quoted 8 lines

tools/testing/selftests/kvm/arm64/kvm-uuid.c line 26

Quoted 6 lines
tools/testing/selftests/kvm/arm64/kvm-uuid.c new file +6 -0
+{
+	struct arm_smccc_res res = {};
+
+	smccc_hvc(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, 0, 0, 0, 0, 0, 0, 0, &res);
+
+	__GUEST_ASSERT(res.a0 != SMCCC_RET_NOT_SUPPORTED, "a0 = %lx\n", res.a0);

Should this check res.a0 == SMCCC_RET_SUCCESS instead?

Sorry for the delay, but now that I'm awake, it is obvious this doesn't work at all. A0-A3 contain the full UID.

While checking for A0 != NOT_SUPPORTED is valid, checking for A0 == SUCCESS cannot work on KVM (we'd get 0xb66fb428 instead of 0).

So actually, this assertion is meaningless, and I'm dropping it.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.