Path: blob/master/tools/testing/selftests/kvm/arm64/hello_el2.c
38237 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* hello_el2 - Basic KVM selftest for VM running at EL2 with E2H=RES13*4* Copyright 2025 Google LLC5*/6#include "kvm_util.h"7#include "processor.h"8#include "test_util.h"9#include "ucall.h"1011#include <asm/sysreg.h>1213static void guest_code(void)14{15u64 mmfr0 = read_sysreg_s(SYS_ID_AA64MMFR0_EL1);16u64 mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);17u64 mmfr4 = read_sysreg_s(SYS_ID_AA64MMFR4_EL1);18u8 e2h0 = SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, mmfr4);1920GUEST_ASSERT_EQ(get_current_el(), 2);21GUEST_ASSERT(read_sysreg(hcr_el2) & HCR_EL2_E2H);22GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR1_EL1, VH, mmfr1),23ID_AA64MMFR1_EL1_VH_IMP);2425/*26* Traps of the complete ID register space are IMPDEF without FEAT_FGT,27* which is really annoying to deal with in KVM describing E2H as RES1.28*29* If the implementation doesn't honor the trap then expect the register30* to return all zeros.31*/32if (e2h0 == ID_AA64MMFR4_EL1_E2H0_IMP)33GUEST_ASSERT_EQ(SYS_FIELD_GET(ID_AA64MMFR0_EL1, FGT, mmfr0),34ID_AA64MMFR0_EL1_FGT_NI);35else36GUEST_ASSERT_EQ(e2h0, ID_AA64MMFR4_EL1_E2H0_NI_NV1);3738GUEST_DONE();39}4041int main(void)42{43struct kvm_vcpu_init init;44struct kvm_vcpu *vcpu;45struct kvm_vm *vm;46struct ucall uc;4748TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));4950vm = vm_create(1);5152kvm_get_default_vcpu_target(vm, &init);53init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);54vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);55kvm_arch_vm_finalize_vcpus(vm);5657vcpu_run(vcpu);58switch (get_ucall(vcpu, &uc)) {59case UCALL_DONE:60break;61case UCALL_ABORT:62REPORT_GUEST_ASSERT(uc);63break;64default:65TEST_FAIL("Unhandled ucall: %ld\n", uc.cmd);66}6768kvm_vm_free(vm);69return 0;70}717273