1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 256c7f5e7SAlex Bennée /* 356c7f5e7SAlex Bennée * Debug and Guest Debug support 456c7f5e7SAlex Bennée * 556c7f5e7SAlex Bennée * Copyright (C) 2015 - Linaro Ltd 656c7f5e7SAlex Bennée * Author: Alex Bennée <alex.bennee@linaro.org> 756c7f5e7SAlex Bennée */ 856c7f5e7SAlex Bennée 956c7f5e7SAlex Bennée #include <linux/kvm_host.h> 10eef8c85aSAlex Bennée #include <linux/hw_breakpoint.h> 1156c7f5e7SAlex Bennée 12337b99bfSAlex Bennée #include <asm/debug-monitors.h> 13337b99bfSAlex Bennée #include <asm/kvm_asm.h> 1456c7f5e7SAlex Bennée #include <asm/kvm_arm.h> 15337b99bfSAlex Bennée #include <asm/kvm_emulate.h> 16337b99bfSAlex Bennée 17eef8c85aSAlex Bennée #include "trace.h" 18eef8c85aSAlex Bennée 19337b99bfSAlex Bennée /* These are the bits of MDSCR_EL1 we may manipulate */ 20337b99bfSAlex Bennée #define MDSCR_EL1_DEBUG_MASK (DBG_MDSCR_SS | \ 21337b99bfSAlex Bennée DBG_MDSCR_KDE | \ 22337b99bfSAlex Bennée DBG_MDSCR_MDE) 2356c7f5e7SAlex Bennée 2456c7f5e7SAlex Bennée static DEFINE_PER_CPU(u32, mdcr_el2); 2556c7f5e7SAlex Bennée 2656c7f5e7SAlex Bennée /** 27337b99bfSAlex Bennée * save/restore_guest_debug_regs 28337b99bfSAlex Bennée * 29337b99bfSAlex Bennée * For some debug operations we need to tweak some guest registers. As 30337b99bfSAlex Bennée * a result we need to save the state of those registers before we 31337b99bfSAlex Bennée * make those modifications. 32337b99bfSAlex Bennée * 33337b99bfSAlex Bennée * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled 34337b99bfSAlex Bennée * after we have restored the preserved value to the main context. 35337b99bfSAlex Bennée */ 36337b99bfSAlex Bennée static void save_guest_debug_regs(struct kvm_vcpu *vcpu) 37337b99bfSAlex Bennée { 388d404c4cSChristoffer Dall u64 val = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 398d404c4cSChristoffer Dall 408d404c4cSChristoffer Dall vcpu->arch.guest_debug_preserved.mdscr_el1 = val; 41eef8c85aSAlex Bennée 42eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("Saved MDSCR_EL1", 43eef8c85aSAlex Bennée vcpu->arch.guest_debug_preserved.mdscr_el1); 44337b99bfSAlex Bennée } 45337b99bfSAlex Bennée 46337b99bfSAlex Bennée static void restore_guest_debug_regs(struct kvm_vcpu *vcpu) 47337b99bfSAlex Bennée { 488d404c4cSChristoffer Dall u64 val = vcpu->arch.guest_debug_preserved.mdscr_el1; 498d404c4cSChristoffer Dall 508d404c4cSChristoffer Dall vcpu_write_sys_reg(vcpu, val, MDSCR_EL1); 51eef8c85aSAlex Bennée 52eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("Restored MDSCR_EL1", 538d404c4cSChristoffer Dall vcpu_read_sys_reg(vcpu, MDSCR_EL1)); 54337b99bfSAlex Bennée } 55337b99bfSAlex Bennée 56337b99bfSAlex Bennée /** 5756c7f5e7SAlex Bennée * kvm_arm_init_debug - grab what we need for debug 5856c7f5e7SAlex Bennée * 5956c7f5e7SAlex Bennée * Currently the sole task of this function is to retrieve the initial 6056c7f5e7SAlex Bennée * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has 6156c7f5e7SAlex Bennée * presumably been set-up by some knowledgeable bootcode. 6256c7f5e7SAlex Bennée * 6356c7f5e7SAlex Bennée * It is called once per-cpu during CPU hyp initialisation. 6456c7f5e7SAlex Bennée */ 6556c7f5e7SAlex Bennée 6656c7f5e7SAlex Bennée void kvm_arm_init_debug(void) 6756c7f5e7SAlex Bennée { 687aa8d146SMarc Zyngier __this_cpu_write(mdcr_el2, kvm_call_hyp_ret(__kvm_get_mdcr_el2)); 6956c7f5e7SAlex Bennée } 7056c7f5e7SAlex Bennée 7156c7f5e7SAlex Bennée /** 7284e690bfSAlex Bennée * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state 7384e690bfSAlex Bennée */ 7484e690bfSAlex Bennée 7584e690bfSAlex Bennée void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) 7684e690bfSAlex Bennée { 7784e690bfSAlex Bennée vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state; 7884e690bfSAlex Bennée } 7984e690bfSAlex Bennée 8084e690bfSAlex Bennée /** 8156c7f5e7SAlex Bennée * kvm_arm_setup_debug - set up debug related stuff 8256c7f5e7SAlex Bennée * 8356c7f5e7SAlex Bennée * @vcpu: the vcpu pointer 8456c7f5e7SAlex Bennée * 8556c7f5e7SAlex Bennée * This is called before each entry into the hypervisor to setup any 8656c7f5e7SAlex Bennée * debug related registers. Currently this just ensures we will trap 8756c7f5e7SAlex Bennée * access to: 8856c7f5e7SAlex Bennée * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR) 8956c7f5e7SAlex Bennée * - Debug ROM Address (MDCR_EL2_TDRA) 9056c7f5e7SAlex Bennée * - OS related registers (MDCR_EL2_TDOSA) 91f85279b4SWill Deacon * - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB) 92*a1319260SSuzuki K Poulose * - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB) 9356c7f5e7SAlex Bennée * 9456c7f5e7SAlex Bennée * Additionally, KVM only traps guest accesses to the debug registers if 9556c7f5e7SAlex Bennée * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY 96fa89d31cSDave Martin * flag on vcpu->arch.flags). Since the guest must not interfere 9756c7f5e7SAlex Bennée * with the hardware state when debugging the guest, we must ensure that 9856c7f5e7SAlex Bennée * trapping is enabled whenever we are debugging the guest using the 9956c7f5e7SAlex Bennée * debug registers. 10056c7f5e7SAlex Bennée */ 10156c7f5e7SAlex Bennée 10256c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) 10356c7f5e7SAlex Bennée { 104fa89d31cSDave Martin bool trap_debug = !(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY); 1054942dc66SAndrew Murray unsigned long mdscr, orig_mdcr_el2 = vcpu->arch.mdcr_el2; 10656c7f5e7SAlex Bennée 107eef8c85aSAlex Bennée trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug); 108eef8c85aSAlex Bennée 109f85279b4SWill Deacon /* 110*a1319260SSuzuki K Poulose * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK 111*a1319260SSuzuki K Poulose * to disable guest access to the profiling and trace buffers 112f85279b4SWill Deacon */ 11356c7f5e7SAlex Bennée vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK; 11456c7f5e7SAlex Bennée vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM | 115f85279b4SWill Deacon MDCR_EL2_TPMS | 116a354a64dSSuzuki K Poulose MDCR_EL2_TTRF | 11756c7f5e7SAlex Bennée MDCR_EL2_TPMCR | 11856c7f5e7SAlex Bennée MDCR_EL2_TDRA | 11956c7f5e7SAlex Bennée MDCR_EL2_TDOSA); 12056c7f5e7SAlex Bennée 121337b99bfSAlex Bennée /* Is Guest debugging in effect? */ 122337b99bfSAlex Bennée if (vcpu->guest_debug) { 123337b99bfSAlex Bennée /* Route all software debug exceptions to EL2 */ 1244bd611caSAlex Bennée vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE; 125337b99bfSAlex Bennée 126337b99bfSAlex Bennée /* Save guest debug state */ 127337b99bfSAlex Bennée save_guest_debug_regs(vcpu); 128337b99bfSAlex Bennée 129337b99bfSAlex Bennée /* 130337b99bfSAlex Bennée * Single Step (ARM ARM D2.12.3 The software step state 131337b99bfSAlex Bennée * machine) 132337b99bfSAlex Bennée * 133337b99bfSAlex Bennée * If we are doing Single Step we need to manipulate 134337b99bfSAlex Bennée * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the 135337b99bfSAlex Bennée * step has occurred the hypervisor will trap the 136337b99bfSAlex Bennée * debug exception and we return to userspace. 137337b99bfSAlex Bennée * 138337b99bfSAlex Bennée * If the guest attempts to single step its userspace 139337b99bfSAlex Bennée * we would have to deal with a trapped exception 140337b99bfSAlex Bennée * while in the guest kernel. Because this would be 141337b99bfSAlex Bennée * hard to unwind we suppress the guest's ability to 142337b99bfSAlex Bennée * do so by masking MDSCR_EL.SS. 143337b99bfSAlex Bennée * 144337b99bfSAlex Bennée * This confuses guest debuggers which use 145337b99bfSAlex Bennée * single-step behind the scenes but everything 146337b99bfSAlex Bennée * returns to normal once the host is no longer 147337b99bfSAlex Bennée * debugging the system. 148337b99bfSAlex Bennée */ 149337b99bfSAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { 150337b99bfSAlex Bennée *vcpu_cpsr(vcpu) |= DBG_SPSR_SS; 1518d404c4cSChristoffer Dall mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 1528d404c4cSChristoffer Dall mdscr |= DBG_MDSCR_SS; 1538d404c4cSChristoffer Dall vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); 154337b99bfSAlex Bennée } else { 1558d404c4cSChristoffer Dall mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 1568d404c4cSChristoffer Dall mdscr &= ~DBG_MDSCR_SS; 1578d404c4cSChristoffer Dall vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); 158337b99bfSAlex Bennée } 159834bf887SAlex Bennée 160eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu)); 161eef8c85aSAlex Bennée 162834bf887SAlex Bennée /* 163834bf887SAlex Bennée * HW Breakpoints and watchpoints 164834bf887SAlex Bennée * 165834bf887SAlex Bennée * We simply switch the debug_ptr to point to our new 166834bf887SAlex Bennée * external_debug_state which has been populated by the 167834bf887SAlex Bennée * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY 168834bf887SAlex Bennée * mechanism ensures the registers are updated on the 169834bf887SAlex Bennée * world switch. 170834bf887SAlex Bennée */ 171834bf887SAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) { 172834bf887SAlex Bennée /* Enable breakpoints/watchpoints */ 1738d404c4cSChristoffer Dall mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 1748d404c4cSChristoffer Dall mdscr |= DBG_MDSCR_MDE; 1758d404c4cSChristoffer Dall vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); 176834bf887SAlex Bennée 177834bf887SAlex Bennée vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state; 178fa89d31cSDave Martin vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY; 179834bf887SAlex Bennée trap_debug = true; 180eef8c85aSAlex Bennée 181eef8c85aSAlex Bennée trace_kvm_arm_set_regset("BKPTS", get_num_brps(), 182eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bcr[0], 183eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bvr[0]); 184eef8c85aSAlex Bennée 185eef8c85aSAlex Bennée trace_kvm_arm_set_regset("WAPTS", get_num_wrps(), 186eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wcr[0], 187eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wvr[0]); 188337b99bfSAlex Bennée } 18956c7f5e7SAlex Bennée } 19056c7f5e7SAlex Bennée 191834bf887SAlex Bennée BUG_ON(!vcpu->guest_debug && 192834bf887SAlex Bennée vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state); 193834bf887SAlex Bennée 194834bf887SAlex Bennée /* Trap debug register access */ 195834bf887SAlex Bennée if (trap_debug) 196834bf887SAlex Bennée vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA; 197eef8c85aSAlex Bennée 19854ceb1bcSChristoffer Dall /* If KDE or MDE are set, perform a full save/restore cycle. */ 1998d404c4cSChristoffer Dall if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE)) 200fa89d31cSDave Martin vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY; 20154ceb1bcSChristoffer Dall 2024942dc66SAndrew Murray /* Write mdcr_el2 changes since vcpu_load on VHE systems */ 2034942dc66SAndrew Murray if (has_vhe() && orig_mdcr_el2 != vcpu->arch.mdcr_el2) 2044942dc66SAndrew Murray write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2); 2054942dc66SAndrew Murray 206eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2); 2078d404c4cSChristoffer Dall trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1)); 208834bf887SAlex Bennée } 209834bf887SAlex Bennée 21056c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) 21156c7f5e7SAlex Bennée { 212eef8c85aSAlex Bennée trace_kvm_arm_clear_debug(vcpu->guest_debug); 213eef8c85aSAlex Bennée 214834bf887SAlex Bennée if (vcpu->guest_debug) { 215337b99bfSAlex Bennée restore_guest_debug_regs(vcpu); 216834bf887SAlex Bennée 217834bf887SAlex Bennée /* 218834bf887SAlex Bennée * If we were using HW debug we need to restore the 219834bf887SAlex Bennée * debug_ptr to the guest debug state. 220834bf887SAlex Bennée */ 221eef8c85aSAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) { 222834bf887SAlex Bennée kvm_arm_reset_debug_ptr(vcpu); 223834bf887SAlex Bennée 224eef8c85aSAlex Bennée trace_kvm_arm_set_regset("BKPTS", get_num_brps(), 225eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bcr[0], 226eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bvr[0]); 227eef8c85aSAlex Bennée 228eef8c85aSAlex Bennée trace_kvm_arm_set_regset("WAPTS", get_num_wrps(), 229eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wcr[0], 230eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wvr[0]); 231eef8c85aSAlex Bennée } 232834bf887SAlex Bennée } 23356c7f5e7SAlex Bennée } 234d2602bb4SSuzuki K Poulose 235d2602bb4SSuzuki K Poulose void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) 236d2602bb4SSuzuki K Poulose { 237d2602bb4SSuzuki K Poulose u64 dfr0; 238d2602bb4SSuzuki K Poulose 239d2602bb4SSuzuki K Poulose /* For VHE, there is nothing to do */ 240d2602bb4SSuzuki K Poulose if (has_vhe()) 241d2602bb4SSuzuki K Poulose return; 242d2602bb4SSuzuki K Poulose 243d2602bb4SSuzuki K Poulose dfr0 = read_sysreg(id_aa64dfr0_el1); 244d2602bb4SSuzuki K Poulose /* 245d2602bb4SSuzuki K Poulose * If SPE is present on this CPU and is available at current EL, 246d2602bb4SSuzuki K Poulose * we may need to check if the host state needs to be saved. 247d2602bb4SSuzuki K Poulose */ 248d2602bb4SSuzuki K Poulose if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_PMSVER_SHIFT) && 249d2602bb4SSuzuki K Poulose !(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(SYS_PMBIDR_EL1_P_SHIFT))) 250d2602bb4SSuzuki K Poulose vcpu->arch.flags |= KVM_ARM64_DEBUG_STATE_SAVE_SPE; 251*a1319260SSuzuki K Poulose 252*a1319260SSuzuki K Poulose /* Check if we have TRBE implemented and available at the host */ 253*a1319260SSuzuki K Poulose if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_TRBE_SHIFT) && 254*a1319260SSuzuki K Poulose !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_PROG)) 255*a1319260SSuzuki K Poulose vcpu->arch.flags |= KVM_ARM64_DEBUG_STATE_SAVE_TRBE; 256d2602bb4SSuzuki K Poulose } 257d2602bb4SSuzuki K Poulose 258d2602bb4SSuzuki K Poulose void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) 259d2602bb4SSuzuki K Poulose { 260*a1319260SSuzuki K Poulose vcpu->arch.flags &= ~(KVM_ARM64_DEBUG_STATE_SAVE_SPE | 261*a1319260SSuzuki K Poulose KVM_ARM64_DEBUG_STATE_SAVE_TRBE); 262d2602bb4SSuzuki K Poulose } 263