xref: /linux/tools/testing/selftests/kvm/arm64/hello_el2.c (revision 8a7c601e14576a22c2bbf7f67455ccf3f3d2737f)
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 mmfr0 = read_sysreg_s(SYS_ID_AA64MMFR0_EL1);
17 	u64 mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
18 	u64 mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);
19 	u8 e2h0 = SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4);
20 
21 	GUEST_ASSERT_EQ(get_current_el(), 2);
22 	GUEST_ASSERT(read_sysreg(hcr_el2) & HCR_EL2_E2H);
23 	GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR1_EL1, VH, mmfr1),
24 			ID_AA64MMFR1_EL1_VH_IMP);
25 
26 	/*
27 	 * Traps of the complete ID register space are IMPDEF without FEAT_FGT,
28 	 * which is really annoying to deal with in KVM describing E2H as RES1.
29 	 *
30 	 * If the implementation doesn't honor the trap then expect the register
31 	 * to return all zeros.
32 	 */
33 	if (e2h0 == ID_AA64MMFR4_EL1_E2H0_IMP)
34 		GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR0_EL1, FGT, mmfr0),
35 				ID_AA64MMFR0_EL1_FGT_NI);
36 	else
37 		GUEST_ASSERT_EQ(e2h0, ID_AA64MMFR4_EL1_E2H0_NI_NV1);
38 
39 	GUEST_DONE();
40 }
41 
42 int main(void)
43 {
44 	struct kvm_vcpu_init init;
45 	struct kvm_vcpu *vcpu;
46 	struct kvm_vm *vm;
47 	struct ucall uc;
48 
49 	TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
50 
51 	vm = vm_create(1);
52 
53 	kvm_get_default_vcpu_target(vm, &init);
54 	init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
55 	vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);
56 	kvm_arch_vm_finalize_vcpus(vm);
57 
58 	vcpu_run(vcpu);
59 	switch (get_ucall(vcpu, &uc)) {
60 	case UCALL_DONE:
61 		break;
62 	case UCALL_ABORT:
63 		REPORT_GUEST_ASSERT(uc);
64 		break;
65 	default:
66 		TEST_FAIL("Unhandled ucall: %ld\n", uc.cmd);
67 	}
68 
69 	kvm_vm_free(vm);
70 	return 0;
71 }
72