]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: arm64: selftests: Add basic test for running in VHE EL2
authorOliver Upton <oliver.upton@linux.dev>
Wed, 17 Sep 2025 21:20:43 +0000 (14:20 -0700)
committerMarc Zyngier <maz@kernel.org>
Wed, 24 Sep 2025 18:23:32 +0000 (19:23 +0100)
Add an embarrassingly simple selftest for sanity checking KVM's VHE EL2
and test that the ID register bits are consistent with HCR_EL2.E2H being
RES1.

Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Marc Zyngier <maz@kernel.org>
tools/testing/selftests/kvm/Makefile.kvm
tools/testing/selftests/kvm/arm64/hello_el2.c [new file with mode: 0644]

index 41b40c676d7f38a7726851510d0c38c97d36d79e..5d59267d4089a78d8dd3fea1168ec4db6b98ac32 100644 (file)
@@ -156,6 +156,7 @@ TEST_GEN_PROGS_arm64 = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_arm64 += arm64/aarch32_id_regs
 TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases
 TEST_GEN_PROGS_arm64 += arm64/debug-exceptions
+TEST_GEN_PROGS_arm64 += arm64/hello_el2
 TEST_GEN_PROGS_arm64 += arm64/host_sve
 TEST_GEN_PROGS_arm64 += arm64/hypercalls
 TEST_GEN_PROGS_arm64 += arm64/external_aborts
diff --git a/tools/testing/selftests/kvm/arm64/hello_el2.c b/tools/testing/selftests/kvm/arm64/hello_el2.c
new file mode 100644 (file)
index 0000000..3c1f44e
--- /dev/null
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * hello_el2 - Basic KVM selftest for VM running at EL2 with E2H=RES1
+ *
+ * Copyright 2025 Google LLC
+ */
+#include "kvm_util.h"
+#include "processor.h"
+#include "test_util.h"
+#include "ucall.h"
+
+#include <asm/sysreg.h>
+
+static void guest_code(void)
+{
+       u64 mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
+       u64 mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);
+
+       GUEST_ASSERT_EQ(get_current_el(), 2);
+       GUEST_ASSERT(read_sysreg(hcr_el2) & HCR_EL2_E2H);
+       GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR1_EL1, VH, mmfr1),
+                       ID_AA64MMFR1_EL1_VH_IMP);
+       GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4),
+                       ID_AA64MMFR4_EL1_E2H0_NI_NV1);
+
+       GUEST_DONE();
+}
+
+int main(void)
+{
+       struct kvm_vcpu_init init;
+       struct kvm_vcpu *vcpu;
+       struct kvm_vm *vm;
+       struct ucall uc;
+
+       TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
+
+       vm = vm_create(1);
+
+       kvm_get_default_vcpu_target(vm, &init);
+       init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
+       vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);
+       kvm_arch_vm_finalize_vcpus(vm);
+
+       vcpu_run(vcpu);
+       switch (get_ucall(vcpu, &uc)) {
+       case UCALL_DONE:
+               break;
+       case UCALL_ABORT:
+               REPORT_GUEST_ASSERT(uc);
+               break;
+       default:
+               TEST_FAIL("Unhandled ucall: %ld\n", uc.cmd);
+       }
+
+       kvm_vm_free(vm);
+       return 0;
+}