Skip to content

Commit f677b0e

Browse files
ouptonMarc Zyngier
authored andcommitted
KVM: arm64: selftests: Add basic test for running in VHE EL2
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>
1 parent 2de21fb commit f677b0e

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

tools/testing/selftests/kvm/Makefile.kvm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ TEST_GEN_PROGS_arm64 = $(TEST_GEN_PROGS_COMMON)
156156
TEST_GEN_PROGS_arm64 += arm64/aarch32_id_regs
157157
TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases
158158
TEST_GEN_PROGS_arm64 += arm64/debug-exceptions
159+
TEST_GEN_PROGS_arm64 += arm64/hello_el2
159160
TEST_GEN_PROGS_arm64 += arm64/host_sve
160161
TEST_GEN_PROGS_arm64 += arm64/hypercalls
161162
TEST_GEN_PROGS_arm64 += arm64/external_aborts
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* hello_el2 - Basic KVM selftest for VM running at EL2 with E2H=RES1
4+
*
5+
* Copyright 2025 Google LLC
6+
*/
7+
#include "kvm_util.h"
8+
#include "processor.h"
9+
#include "test_util.h"
10+
#include "ucall.h"
11+
12+
#include <asm/sysreg.h>
13+
14+
static void guest_code(void)
15+
{
16+
u64 mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
17+
u64 mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);
18+
19+
GUEST_ASSERT_EQ(get_current_el(), 2);
20+
GUEST_ASSERT(read_sysreg(hcr_el2) & HCR_EL2_E2H);
21+
GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR1_EL1, VH, mmfr1),
22+
ID_AA64MMFR1_EL1_VH_IMP);
23+
GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4),
24+
ID_AA64MMFR4_EL1_E2H0_NI_NV1);
25+
26+
GUEST_DONE();
27+
}
28+
29+
int main(void)
30+
{
31+
struct kvm_vcpu_init init;
32+
struct kvm_vcpu *vcpu;
33+
struct kvm_vm *vm;
34+
struct ucall uc;
35+
36+
TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
37+
38+
vm = vm_create(1);
39+
40+
kvm_get_default_vcpu_target(vm, &init);
41+
init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
42+
vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);
43+
kvm_arch_vm_finalize_vcpus(vm);
44+
45+
vcpu_run(vcpu);
46+
switch (get_ucall(vcpu, &uc)) {
47+
case UCALL_DONE:
48+
break;
49+
case UCALL_ABORT:
50+
REPORT_GUEST_ASSERT(uc);
51+
break;
52+
default:
53+
TEST_FAIL("Unhandled ucall: %ld\n", uc.cmd);
54+
}
55+
56+
kvm_vm_free(vm);
57+
return 0;
58+
}

0 commit comments

Comments
 (0)