KVM: arm64: Fix splat/misbehavior for MMIO SEA injection
Subsystems (5)
v2 cover KVM: arm64: Fix splat/misbehavior for MMIO SEA injection
v1 -> v2:
- Make it clear that the series is referring to synchronous external aborts (i.e. SEAs)
- Move helper out of header
- Pull esr + friends into tools, for use in KVM selftests
v2: https://lore.kernel.org/kvmarm/[email protected]/
Oliver Upton (4):
KVM: arm64: Don't retire aborted MMIO instruction
tools: arm64: Grab a copy of esr.h from kernel
KVM: arm64: selftests: Convert to kernel's ESR terminology
KVM: arm64: selftests: Add tests for MMIO external abort injection
arch/arm64/kvm/mmio.c | 32 +- tools/arch/arm64/include/asm/brk-imm.h | 42 ++ tools/arch/arm64/include/asm/esr.h | 455 ++++++++++++++++++ tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/debug-exceptions.c | 10 +- .../selftests/kvm/aarch64/mmio_abort.c | 159 ++++++ .../selftests/kvm/aarch64/no-vgic-v3.c | 2 +- .../selftests/kvm/aarch64/page_fault_test.c | 4 +- .../kvm/aarch64/vpmu_counter_access.c | 12 +- .../selftests/kvm/include/aarch64/processor.h | 15 +- .../selftests/kvm/lib/aarch64/processor.c | 6 +- 11 files changed, 706 insertions(+), 32 deletions(-) create mode 100644 tools/arch/arm64/include/asm/brk-imm.h create mode 100644 tools/arch/arm64/include/asm/esr.h create mode 100644 tools/testing/selftests/kvm/aarch64/mmio_abort.c
base-commit: 8e929cb546ee42c9a61d24fae60605e9e3192354
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 1/4 KVM: arm64: Don't retire aborted MMIO instruction
Returning an abort to the guest for an unsupported MMIO access is a documented feature of the KVM UAPI. Nevertheless, it's clear that this plumbing has seen limited testing, since userspace can trivially cause a WARN in the MMIO return:
WARNING: CPU: 0 PID: 30558 at arch/arm64/include/asm/kvm_emulate.h:536 kvm_handle_mmio_return+0x46c/0x5c4 arch/arm64/include/asm/kvm_emulate.h:536 Call trace: kvm_handle_mmio_return+0x46c/0x5c4 arch/arm64/include/asm/kvm_emulate.h:536 kvm_arch_vcpu_ioctl_run+0x98/0x15b4 arch/arm64/kvm/arm.c:1133 kvm_vcpu_ioctl+0x75c/0xa78 virt/kvm/kvm_main.c:4487 __do_sys_ioctl fs/ioctl.c:51 [inline] __se_sys_ioctl fs/ioctl.c:893 [inline] __arm64_sys_ioctl+0x14c/0x1c8 fs/ioctl.c:893 __invoke_syscall arch/arm64/kernel/syscall.c:35 [inline] invoke_syscall+0x98/0x2b8 arch/arm64/kernel/syscall.c:49 el0_svc_common+0x1e0/0x23c arch/arm64/kernel/syscall.c:132 do_el0_svc+0x48/0x58 arch/arm64/kernel/syscall.c:151 el0_svc+0x38/0x68 arch/arm64/kernel/entry-common.c:712 el0t_64_sync_handler+0x90/0xfc arch/arm64/kernel/entry-common.c:730 el0t_64_sync+0x190/0x194 arch/arm64/kernel/entry.S:598
The splat is complaining that KVM is advancing PC while an exception is pending, i.e. that KVM is retiring the MMIO instruction despite a pending synchronous external abort. Womp womp.
Fix the glaring UAPI bug by skipping over all the MMIO emulation in case there is a pending synchronous exception. Note that while userspace is capable of pending an asynchronous exception (SError, IRQ, or FIQ), it is still safe to retire the MMIO instruction in this case as (1) they are by definition asynchronous, and (2) KVM relies on hardware support for pending/delivering these exceptions instead of the software state machine for advancing PC.
--- arch/arm64/kvm/mmio.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmio.c +30 -2
@@ -72,6 +72,31 @@ unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len)return data; } +static bool kvm_pending_sync_exception (struct kvm_vcpu *vcpu) +{ +if (!vcpu_get_flag (vcpu,PENDING_EXCEPTION )) +return false; + +if (vcpu_el1_is_32bit (vcpu)) { +switch (vcpu_get_flag (vcpu,EXCEPT_MASK )) { +case unpack_vcpu_flag (EXCEPT_AA32_UND ): +case unpack_vcpu_flag (EXCEPT_AA32_IABT ): +case unpack_vcpu_flag (EXCEPT_AA32_DABT ): +return true; +default : +return false; + } + }else { +switch (vcpu_get_flag (vcpu,EXCEPT_MASK )) { +case unpack_vcpu_flag (EXCEPT_AA64_EL1_SYNC ): +case unpack_vcpu_flag (EXCEPT_AA64_EL2_SYNC ): +return true; +default : +return false; + } + } +} +/** * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation * or in-kernel IO emulation
@@ -84,8 +109,11 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)unsigned int len;int mask; -/* Detect an already handled MMIO return */ -if (unlikely (!vcpu->mmio_needed)) +/* +* Detect if the MMIO return was already handled or if userspace aborted +* the MMIO access. +*/ +if (unlikely (!vcpu->mmio_needed ||kvm_pending_sync_exception (vcpu)))return 1 ; vcpu->mmio_needed =0 ;
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 2/4 tools: arm64: Grab a copy of esr.h from kernel
In reply to Oliver UptonGrab esr.h and brk-imm.h for subsequent use in KVM selftests.
--- tools/arch/arm64/include/asm/brk-imm.h | 42 +++ tools/arch/arm64/include/asm/esr.h | 455 +++++++++++++++++++++++++ 2 files changed, 497 insertions(+) create mode 100644 tools/arch/arm64/include/asm/brk-imm.h create mode 100644 tools/arch/arm64/include/asm/esr.h
tools/arch/arm64/include/asm/brk-imm.h new file +42 -0
@@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2012 ARM Ltd. +*/ + +#ifndef __ASM_BRK_IMM_H +#define __ASM_BRK_IMM_H + +/* +* #imm16 values used for BRK instruction generation +* 0x004: for installing kprobes +* 0x005: for installing uprobes +* 0x006: for kprobe software single-step +* 0x007: for kretprobe return +* Allowed values for kgdb are 0x400 - 0x7ff +* 0x100: for triggering a fault on purpose (reserved) +* 0x400: for dynamic BRK instruction +* 0x401: for compile time BRK instruction +* 0x800: kernel-mode BUG() and WARN() traps +* 0x9xx: tag-based KASAN trap (allowed values 0x900 - 0x9ff) +* 0x55xx: Undefined Behavior Sanitizer traps ('U' << 8) +* 0x8xxx: Control-Flow Integrity traps +*/ +#define KPROBES_BRK_IMM 0x004 +#define UPROBES_BRK_IMM 0x005 +#define KPROBES_BRK_SS_IMM 0x006 +#define KRETPROBES_BRK_IMM 0x007 +#define FAULT_BRK_IMM 0x100 +#define KGDB_DYN_DBG_BRK_IMM 0x400 +#define KGDB_COMPILED_DBG_BRK_IMM 0x401 +#define BUG_BRK_IMM 0x800 +#define KASAN_BRK_IMM 0x900 +#define KASAN_BRK_MASK 0x0ff +#define UBSAN_BRK_IMM 0x5500 +#define UBSAN_BRK_MASK 0x00ff + +#define CFI_BRK_IMM_TARGET GENMASK(4, 0) +#define CFI_BRK_IMM_TYPE GENMASK(9, 5) +#define CFI_BRK_IMM_BASE 0x8000 +#define CFI_BRK_IMM_MASK (CFI_BRK_IMM_TARGET | CFI_BRK_IMM_TYPE) + +#endif
tools/arch/arm64/include/asm/esr.h new file +455 -0
455 lines not on this page · raw message
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 3/4 KVM: arm64: selftests: Convert to kernel's ESR terminology
In reply to Oliver UptonDrop the KVM selftests specific flavoring of ESR in favor of the kernel header.
--- .../selftests/kvm/aarch64/debug-exceptions.c | 10 +++++----- tools/testing/selftests/kvm/aarch64/no-vgic-v3.c | 2 +- .../selftests/kvm/aarch64/page_fault_test.c | 4 ++-- .../selftests/kvm/aarch64/vpmu_counter_access.c | 12 ++++++------ .../selftests/kvm/include/aarch64/processor.h | 15 ++------------- .../testing/selftests/kvm/lib/aarch64/processor.c | 6 +++--- 6 files changed, 19 insertions(+), 30 deletions(-)
tools/testing/selftests/kvm/aarch64/debug-exceptions.c +5 -5
@@ -433,15 +433,15 @@ static void test_guest_debug_exceptions(uint8_t bpn, uint8_t wpn, uint8_t ctx_bpvcpu_init_descriptor_tables (vcpu);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_BRK_INS , guest_sw_bp_handler); + ESR_ELx_EC_BRK64, guest_sw_bp_handler);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_HW_BP_CURRENT , guest_hw_bp_handler); + ESR_ELx_EC_BREAKPT_CUR, guest_hw_bp_handler);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_WP_CURRENT , guest_wp_handler); + ESR_ELx_EC_WATCHPT_CUR, guest_wp_handler);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_SSTEP_CURRENT , guest_ss_handler); + ESR_ELx_EC_SOFTSTP_CUR, guest_ss_handler);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_SVC64 , guest_svc_handler); + ESR_ELx_EC_SVC64, guest_svc_handler);/* Specify bpn/wpn/ctx_bpn to be tested */ vcpu_args_set (vcpu,3 , bpn, wpn, ctx_bpn);
tools/testing/selftests/kvm/aarch64/no-vgic-v3.c +1 -1
@@ -150,7 +150,7 @@ static void test_guest_no_gicv3(void)vcpu_init_descriptor_tables (vcpu);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_UNKNOWN , guest_undef_handler); + ESR_ELx_EC_UNKNOWN, guest_undef_handler);test_run_vcpu (vcpu);
tools/testing/selftests/kvm/aarch64/page_fault_test.c +2 -2
@@ -544,9 +544,9 @@ static void setup_abort_handlers(struct kvm_vm *vm, struct kvm_vcpu *vcpu,vcpu_init_descriptor_tables (vcpu);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_DABT , no_dabt_handler); + ESR_ELx_EC_DABT_CUR, no_dabt_handler);vm_install_sync_handler (vm,VECTOR_SYNC_CURRENT , -ESR_EC_IABT , no_iabt_handler); + ESR_ELx_EC_IABT_CUR, no_iabt_handler); }static void setup_gva_maps (struct kvm_vm *vm)
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c +6 -6
@@ -300,7 +300,7 @@ static void guest_sync_handler(struct ex_regs *regs)uint64_t esr, ec; esr =read_sysreg (esr_el1); - ec = (esr >>ESR_EC_SHIFT ) &ESR_EC_MASK ; + ec =ESR_ELx_EC (esr); __GUEST_ASSERT(expected_ec == ec, "PC: 0x%lx; ESR: 0x%lx; EC: 0x%lx; EC expected: 0x%lx",
@@ -338,10 +338,10 @@ static void test_access_invalid_pmc_regs(struct pmc_accessor *acc, int pmc_idx)* Reading/writing the event count/type registers should cause * an UNDEFINED exception. */ -TEST_EXCEPTION (ESR_EC_UNKNOWN , acc->read_cntr (pmc_idx)); -TEST_EXCEPTION (ESR_EC_UNKNOWN , acc->write_cntr (pmc_idx,0 )); -TEST_EXCEPTION (ESR_EC_UNKNOWN , acc->read_typer (pmc_idx)); -TEST_EXCEPTION (ESR_EC_UNKNOWN , acc->write_typer (pmc_idx,0 )); +TEST_EXCEPTION (ESR_ELx_EC_UNKNOWN, acc->read_cntr (pmc_idx)); +TEST_EXCEPTION (ESR_ELx_EC_UNKNOWN, acc->write_cntr (pmc_idx,0 )); +TEST_EXCEPTION (ESR_ELx_EC_UNKNOWN, acc->read_typer (pmc_idx)); +TEST_EXCEPTION (ESR_ELx_EC_UNKNOWN, acc->write_typer (pmc_idx,0 ));/* * The bit corresponding to the (unimplemented) counter in * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers should be RAZ.
@@ -425,7 +425,7 @@ static void create_vpmu_vm(void *guest_code) vpmu_vm.vm =vm_create (1 );vm_init_descriptor_tables (vpmu_vm.vm); -for (ec =0 ; ec <ESR_EC_NUM ; ec++) { +for (ec =0 ; ec < ESR_ELx_EC_MAX +1 ; ec++) {vm_install_sync_handler (vpmu_vm.vm,VECTOR_SYNC_CURRENT , ec, guest_sync_handler); }
tools/testing/selftests/kvm/include/aarch64/processor.h +2 -13
@@ -12,6 +12,8 @@#include <linux/stringify.h> #include <linux/types.h> +#include <asm/brk-imm.h> +#include <asm/esr.h> #include <asm/sysreg.h>
@@ -100,19 +102,6 @@ enum { (v) == VECTOR_SYNC_LOWER_64 || \ (v) == VECTOR_SYNC_LOWER_32) -#define ESR_EC_NUM 64 -#define ESR_EC_SHIFT 26 -#define ESR_EC_MASK (ESR_EC_NUM - 1) - -#define ESR_EC_UNKNOWN 0x0 -#define ESR_EC_SVC64 0x15 -#define ESR_EC_IABT 0x21 -#define ESR_EC_DABT 0x25 -#define ESR_EC_HW_BP_CURRENT 0x31 -#define ESR_EC_SSTEP_CURRENT 0x33 -#define ESR_EC_WP_CURRENT 0x35 -#define ESR_EC_BRK_INS 0x3c -/* Access flag */ #define PTE_AF (1ULL << 10)
tools/testing/selftests/kvm/lib/aarch64/processor.c +3 -3
@@ -450,7 +450,7 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu) }struct handlers { -handler_fn exception_handlers[VECTOR_NUM ][ESR_EC_NUM ]; +handler_fn exception_handlers[VECTOR_NUM ][ESR_ELx_EC_MAX +1 ]; };void vcpu_init_descriptor_tables (struct kvm_vcpu *vcpu)
@@ -469,7 +469,7 @@ void route_exception(struct ex_regs *regs, int vector)switch (vector) {case VECTOR_SYNC_CURRENT :case VECTOR_SYNC_LOWER_64 : - ec = (read_sysreg (esr_el1) >>ESR_EC_SHIFT ) &ESR_EC_MASK ; + ec =ESR_ELx_EC (read_sysreg (esr_el1)); valid_ec = true;break ;case VECTOR_IRQ_CURRENT :
@@ -508,7 +508,7 @@ void vm_install_sync_handler(struct kvm_vm *vm, int vector, int ec,assert (VECTOR_IS_SYNC (vector));assert (vector <VECTOR_NUM ); -assert (ec <ESR_EC_NUM ); +assert (ec <= ESR_ELx_EC_MAX); handlers->exception_handlers[vector][ec] = handler; }
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 4/4 KVM: arm64: selftests: Add tests for MMIO external abort injection
In reply to Oliver UptonTest that the plumbing exposed to userspace for injecting aborts in response to unexpected MMIO works as intended in two different flavors:
- A 'normal' MMIO instruction (i.e. ESR_ELx.ISV=1)
- An ISV=0 MMIO instruction with/without KVM_CAP_ARM_NISV_TO_USER enabled
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/mmio_abort.c | 159 ++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/mmio_abort.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -156,6 +156,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/aarch32_id_regs TEST_GEN_PROGS_aarch64 += aarch64/arch_timer_edge_cases TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/hypercalls +TEST_GEN_PROGS_aarch64 += aarch64/mmio_abort TEST_GEN_PROGS_aarch64 += aarch64/page_fault_test TEST_GEN_PROGS_aarch64 += aarch64/psci_test TEST_GEN_PROGS_aarch64 += aarch64/set_id_regs
tools/testing/selftests/kvm/aarch64/mmio_abort.c new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
Quoted 7 lines · Oliver Upton
v1 -> v2:
- Make it clear that the series is referring to synchronous external aborts (i.e. SEAs)
- Move helper out of header
- Pull esr + friends into tools, for use in KVM selftests
Quoted 8 lines
v2: https://lore.kernel.org/kvmarm/[email protected]/
Oliver Upton (4):
KVM: arm64: Don't retire aborted MMIO instruction
tools: arm64: Grab a copy ofesr.hfrom kernel
KVM: arm64: selftests: Convert to kernel's ESR terminology
KVM: arm64: selftests: Add tests for MMIO external abort injection
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Oliver Upton
AppliedQuoted 9 lines · Oliver Upton
v1 -> v2:
- Make it clear that the series is referring to synchronous external aborts (i.e. SEAs)
- Move helper out of header
- Pull esr + friends into tools, for use in KVM selftestsv2: https://lore.kernel.org/kvmarm/[email protected]/
[...]
Applied to kvmarm/next, thanks!
[1/4] KVM: arm64: Don't retire aborted MMIO instruction
https://git.kernel.org/kvmarm/kvmarm/c/e735a5da6442 [2/4] tools: arm64: Grab a copy of esr.h from kernel https://git.kernel.org/kvmarm/kvmarm/c/9fb8e9178b25 [3/4] KVM: arm64: selftests: Convert to kernel's ESR terminology https://git.kernel.org/kvmarm/kvmarm/c/c660d334b3a5 [4/4] KVM: arm64: selftests: Add tests for MMIO external abort injection https://git.kernel.org/kvmarm/kvmarm/c/3eb09a3e028e
--
Best,
Oliver