Path: blob/master/tools/testing/selftests/kvm/arm64/external_aborts.c
38237 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* external_abort - Tests for userspace external abort injection3*4* Copyright (c) 2024 Google LLC5*/6#include "processor.h"7#include "test_util.h"89#define MMIO_ADDR 0x8000000ULL10#define EXPECTED_SERROR_ISS (ESR_ELx_ISV | 0x1d1ed)1112static u64 expected_abort_pc;1314static void expect_sea_handler(struct ex_regs *regs)15{16u64 esr = read_sysreg(esr_el1);1718GUEST_ASSERT_EQ(regs->pc, expected_abort_pc);19GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_DABT_CUR);20GUEST_ASSERT_EQ(esr & ESR_ELx_FSC_TYPE, ESR_ELx_FSC_EXTABT);2122GUEST_DONE();23}2425static void unexpected_dabt_handler(struct ex_regs *regs)26{27GUEST_FAIL("Unexpected data abort at PC: %lx\n", regs->pc);28}2930static struct kvm_vm *vm_create_with_dabt_handler(struct kvm_vcpu **vcpu, void *guest_code,31handler_fn dabt_handler)32{33struct kvm_vm *vm = vm_create_with_one_vcpu(vcpu, guest_code);3435vm_init_descriptor_tables(vm);36vcpu_init_descriptor_tables(*vcpu);37vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT, ESR_ELx_EC_DABT_CUR, dabt_handler);3839virt_map(vm, MMIO_ADDR, MMIO_ADDR, 1);4041return vm;42}4344static void vcpu_inject_sea(struct kvm_vcpu *vcpu)45{46struct kvm_vcpu_events events = {};4748events.exception.ext_dabt_pending = true;49vcpu_events_set(vcpu, &events);50}5152static bool vcpu_has_ras(struct kvm_vcpu *vcpu)53{54u64 pfr0 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64PFR0_EL1));5556return SYS_FIELD_GET(ID_AA64PFR0_EL1, RAS, pfr0);57}5859static bool guest_has_ras(void)60{61return SYS_FIELD_GET(ID_AA64PFR0_EL1, RAS, read_sysreg(id_aa64pfr0_el1));62}6364static void vcpu_inject_serror(struct kvm_vcpu *vcpu)65{66struct kvm_vcpu_events events = {};6768events.exception.serror_pending = true;69if (vcpu_has_ras(vcpu)) {70events.exception.serror_has_esr = true;71events.exception.serror_esr = EXPECTED_SERROR_ISS;72}7374vcpu_events_set(vcpu, &events);75}7677static void __vcpu_run_expect(struct kvm_vcpu *vcpu, unsigned int cmd)78{79struct ucall uc;8081vcpu_run(vcpu);82switch (get_ucall(vcpu, &uc)) {83case UCALL_ABORT:84REPORT_GUEST_ASSERT(uc);85break;86default:87if (uc.cmd == cmd)88return;8990TEST_FAIL("Unexpected ucall: %lu", uc.cmd);91}92}9394static void vcpu_run_expect_done(struct kvm_vcpu *vcpu)95{96__vcpu_run_expect(vcpu, UCALL_DONE);97}9899static void vcpu_run_expect_sync(struct kvm_vcpu *vcpu)100{101__vcpu_run_expect(vcpu, UCALL_SYNC);102}103104extern char test_mmio_abort_insn;105106static noinline void test_mmio_abort_guest(void)107{108WRITE_ONCE(expected_abort_pc, (u64)&test_mmio_abort_insn);109110asm volatile("test_mmio_abort_insn:\n\t"111"ldr x0, [%0]\n\t"112: : "r" (MMIO_ADDR) : "x0", "memory");113114GUEST_FAIL("MMIO instruction should not retire");115}116117/*118* Test that KVM doesn't complete MMIO emulation when userspace has made an119* external abort pending for the instruction.120*/121static void test_mmio_abort(void)122{123struct kvm_vcpu *vcpu;124struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_mmio_abort_guest,125expect_sea_handler);126struct kvm_run *run = vcpu->run;127128vcpu_run(vcpu);129TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);130TEST_ASSERT_EQ(run->mmio.phys_addr, MMIO_ADDR);131TEST_ASSERT_EQ(run->mmio.len, sizeof(unsigned long));132TEST_ASSERT(!run->mmio.is_write, "Expected MMIO read");133134vcpu_inject_sea(vcpu);135vcpu_run_expect_done(vcpu);136kvm_vm_free(vm);137}138139extern char test_mmio_nisv_insn;140141static void test_mmio_nisv_guest(void)142{143WRITE_ONCE(expected_abort_pc, (u64)&test_mmio_nisv_insn);144145asm volatile("test_mmio_nisv_insn:\n\t"146"ldr x0, [%0], #8\n\t"147: : "r" (MMIO_ADDR) : "x0", "memory");148149GUEST_FAIL("MMIO instruction should not retire");150}151152/*153* Test that the KVM_RUN ioctl fails for ESR_EL2.ISV=0 MMIO aborts if userspace154* hasn't enabled KVM_CAP_ARM_NISV_TO_USER.155*/156static void test_mmio_nisv(void)157{158struct kvm_vcpu *vcpu;159struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_mmio_nisv_guest,160unexpected_dabt_handler);161162TEST_ASSERT(_vcpu_run(vcpu), "Expected nonzero return code from KVM_RUN");163TEST_ASSERT_EQ(errno, ENOSYS);164165kvm_vm_free(vm);166}167168/*169* Test that ESR_EL2.ISV=0 MMIO aborts reach userspace and that an injected SEA170* reaches the guest.171*/172static void test_mmio_nisv_abort(void)173{174struct kvm_vcpu *vcpu;175struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_mmio_nisv_guest,176expect_sea_handler);177struct kvm_run *run = vcpu->run;178179vm_enable_cap(vm, KVM_CAP_ARM_NISV_TO_USER, 1);180181vcpu_run(vcpu);182TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_ARM_NISV);183TEST_ASSERT_EQ(run->arm_nisv.fault_ipa, MMIO_ADDR);184185vcpu_inject_sea(vcpu);186vcpu_run_expect_done(vcpu);187kvm_vm_free(vm);188}189190static void unexpected_serror_handler(struct ex_regs *regs)191{192GUEST_FAIL("Took unexpected SError exception");193}194195static void test_serror_masked_guest(void)196{197GUEST_ASSERT(read_sysreg(isr_el1) & ISR_EL1_A);198199isb();200201GUEST_DONE();202}203204static void test_serror_masked(void)205{206struct kvm_vcpu *vcpu;207struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_serror_masked_guest,208unexpected_dabt_handler);209210vm_install_exception_handler(vm, VECTOR_ERROR_CURRENT, unexpected_serror_handler);211212vcpu_inject_serror(vcpu);213vcpu_run_expect_done(vcpu);214kvm_vm_free(vm);215}216217static void expect_serror_handler(struct ex_regs *regs)218{219u64 esr = read_sysreg(esr_el1);220221GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_SERROR);222if (guest_has_ras())223GUEST_ASSERT_EQ(ESR_ELx_ISS(esr), EXPECTED_SERROR_ISS);224225GUEST_DONE();226}227228static void test_serror_guest(void)229{230GUEST_ASSERT(read_sysreg(isr_el1) & ISR_EL1_A);231232local_serror_enable();233isb();234local_serror_disable();235236GUEST_FAIL("Should've taken pending SError exception");237}238239static void test_serror(void)240{241struct kvm_vcpu *vcpu;242struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_serror_guest,243unexpected_dabt_handler);244245vm_install_exception_handler(vm, VECTOR_ERROR_CURRENT, expect_serror_handler);246247vcpu_inject_serror(vcpu);248vcpu_run_expect_done(vcpu);249kvm_vm_free(vm);250}251252static void expect_sea_s1ptw_handler(struct ex_regs *regs)253{254u64 esr = read_sysreg(esr_el1);255256GUEST_ASSERT_EQ(regs->pc, expected_abort_pc);257GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_DABT_CUR);258GUEST_ASSERT_EQ((esr & ESR_ELx_FSC), ESR_ELx_FSC_SEA_TTW(3));259260GUEST_DONE();261}262263static noinline void test_s1ptw_abort_guest(void)264{265extern char test_s1ptw_abort_insn;266267WRITE_ONCE(expected_abort_pc, (u64)&test_s1ptw_abort_insn);268269asm volatile("test_s1ptw_abort_insn:\n\t"270"ldr x0, [%0]\n\t"271: : "r" (MMIO_ADDR) : "x0", "memory");272273GUEST_FAIL("Load on S1PTW abort should not retire");274}275276static void test_s1ptw_abort(void)277{278struct kvm_vcpu *vcpu;279u64 *ptep, bad_pa;280struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_s1ptw_abort_guest,281expect_sea_s1ptw_handler);282283ptep = virt_get_pte_hva_at_level(vm, MMIO_ADDR, 2);284bad_pa = BIT(vm->pa_bits) - vm->page_size;285286*ptep &= ~GENMASK(47, 12);287*ptep |= bad_pa;288289vcpu_run_expect_done(vcpu);290kvm_vm_free(vm);291}292293static void test_serror_emulated_guest(void)294{295GUEST_ASSERT(!(read_sysreg(isr_el1) & ISR_EL1_A));296297local_serror_enable();298GUEST_SYNC(0);299local_serror_disable();300301GUEST_FAIL("Should've taken unmasked SError exception");302}303304static void test_serror_emulated(void)305{306struct kvm_vcpu *vcpu;307struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_serror_emulated_guest,308unexpected_dabt_handler);309310vm_install_exception_handler(vm, VECTOR_ERROR_CURRENT, expect_serror_handler);311312vcpu_run_expect_sync(vcpu);313vcpu_inject_serror(vcpu);314vcpu_run_expect_done(vcpu);315kvm_vm_free(vm);316}317318static void test_mmio_ease_guest(void)319{320sysreg_clear_set_s(SYS_SCTLR2_EL1, 0, SCTLR2_EL1_EASE);321isb();322323test_mmio_abort_guest();324}325326/*327* Test that KVM doesn't complete MMIO emulation when userspace has made an328* external abort pending for the instruction.329*/330static void test_mmio_ease(void)331{332struct kvm_vcpu *vcpu;333struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_mmio_ease_guest,334unexpected_dabt_handler);335struct kvm_run *run = vcpu->run;336u64 pfr1;337338pfr1 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64PFR1_EL1));339if (!SYS_FIELD_GET(ID_AA64PFR1_EL1, DF2, pfr1)) {340pr_debug("Skipping %s\n", __func__);341return;342}343344/*345* SCTLR2_ELx.EASE changes the exception vector to the SError vector but346* doesn't further modify the exception context (e.g. ESR_ELx, FAR_ELx).347*/348vm_install_exception_handler(vm, VECTOR_ERROR_CURRENT, expect_sea_handler);349350vcpu_run(vcpu);351TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);352TEST_ASSERT_EQ(run->mmio.phys_addr, MMIO_ADDR);353TEST_ASSERT_EQ(run->mmio.len, sizeof(unsigned long));354TEST_ASSERT(!run->mmio.is_write, "Expected MMIO read");355356vcpu_inject_sea(vcpu);357vcpu_run_expect_done(vcpu);358kvm_vm_free(vm);359}360361static void test_serror_amo_guest(void)362{363/*364* The ISB is entirely unnecessary (and highlights how FEAT_NV2 is borked)365* since the write is redirected to memory. But don't write (intentionally)366* broken code!367*/368sysreg_clear_set(hcr_el2, HCR_EL2_AMO | HCR_EL2_TGE, 0);369isb();370371GUEST_SYNC(0);372GUEST_ASSERT(read_sysreg(isr_el1) & ISR_EL1_A);373374/*375* KVM treats the effective value of AMO as 1 when376* HCR_EL2.{E2H,TGE} = {1, 0}, meaning the SError will be taken when377* unmasked.378*/379local_serror_enable();380isb();381local_serror_disable();382383GUEST_FAIL("Should've taken pending SError exception");384}385386static void test_serror_amo(void)387{388struct kvm_vcpu *vcpu;389struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_serror_amo_guest,390unexpected_dabt_handler);391392vm_install_exception_handler(vm, VECTOR_ERROR_CURRENT, expect_serror_handler);393vcpu_run_expect_sync(vcpu);394vcpu_inject_serror(vcpu);395vcpu_run_expect_done(vcpu);396kvm_vm_free(vm);397}398399int main(void)400{401test_mmio_abort();402test_mmio_nisv();403test_mmio_nisv_abort();404test_serror();405test_serror_masked();406test_serror_emulated();407test_mmio_ease();408test_s1ptw_abort();409410if (!test_supports_el2())411return 0;412413test_serror_amo();414}415416417