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 24d6c850ddSFuad Tabba static DEFINE_PER_CPU(u64, 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 /** 72263d6287SAlexandru Elisei * kvm_arm_setup_mdcr_el2 - configure vcpu mdcr_el2 value 73263d6287SAlexandru Elisei * 74263d6287SAlexandru Elisei * @vcpu: the vcpu pointer 75263d6287SAlexandru Elisei * 76263d6287SAlexandru Elisei * This ensures we will trap access to: 77263d6287SAlexandru Elisei * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR) 78263d6287SAlexandru Elisei * - Debug ROM Address (MDCR_EL2_TDRA) 79263d6287SAlexandru Elisei * - OS related registers (MDCR_EL2_TDOSA) 80263d6287SAlexandru Elisei * - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB) 81263d6287SAlexandru Elisei * - Self-hosted Trace Filter controls (MDCR_EL2_TTRF) 82fbb31e5fSMarc Zyngier * - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB) 83263d6287SAlexandru Elisei */ 84263d6287SAlexandru Elisei static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu) 85263d6287SAlexandru Elisei { 86263d6287SAlexandru Elisei /* 87fbb31e5fSMarc Zyngier * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK 88fbb31e5fSMarc Zyngier * to disable guest access to the profiling and trace buffers 89263d6287SAlexandru Elisei */ 90263d6287SAlexandru Elisei vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK; 91263d6287SAlexandru Elisei vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM | 92263d6287SAlexandru Elisei MDCR_EL2_TPMS | 93263d6287SAlexandru Elisei MDCR_EL2_TTRF | 94263d6287SAlexandru Elisei MDCR_EL2_TPMCR | 95263d6287SAlexandru Elisei MDCR_EL2_TDRA | 96263d6287SAlexandru Elisei MDCR_EL2_TDOSA); 97263d6287SAlexandru Elisei 98263d6287SAlexandru Elisei /* Is the VM being debugged by userspace? */ 99263d6287SAlexandru Elisei if (vcpu->guest_debug) 100263d6287SAlexandru Elisei /* Route all software debug exceptions to EL2 */ 101263d6287SAlexandru Elisei vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE; 102263d6287SAlexandru Elisei 103263d6287SAlexandru Elisei /* 104263d6287SAlexandru Elisei * Trap debug register access when one of the following is true: 105263d6287SAlexandru Elisei * - Userspace is using the hardware to debug the guest 106263d6287SAlexandru Elisei * (KVM_GUESTDBG_USE_HW is set). 107b1da4908SMarc Zyngier * - The guest is not using debug (DEBUG_DIRTY clear). 1087dabf02fSOliver Upton * - The guest has enabled the OS Lock (debug exceptions are blocked). 109263d6287SAlexandru Elisei */ 110263d6287SAlexandru Elisei if ((vcpu->guest_debug & KVM_GUESTDBG_USE_HW) || 111b1da4908SMarc Zyngier !vcpu_get_flag(vcpu, DEBUG_DIRTY) || 1127dabf02fSOliver Upton kvm_vcpu_os_lock_enabled(vcpu)) 113263d6287SAlexandru Elisei vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA; 114263d6287SAlexandru Elisei 115263d6287SAlexandru Elisei trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2); 116263d6287SAlexandru Elisei } 117263d6287SAlexandru Elisei 118263d6287SAlexandru Elisei /** 119263d6287SAlexandru Elisei * kvm_arm_vcpu_init_debug - setup vcpu debug traps 120263d6287SAlexandru Elisei * 121263d6287SAlexandru Elisei * @vcpu: the vcpu pointer 122263d6287SAlexandru Elisei * 123263d6287SAlexandru Elisei * Set vcpu initial mdcr_el2 value. 124263d6287SAlexandru Elisei */ 125263d6287SAlexandru Elisei void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu) 126263d6287SAlexandru Elisei { 127263d6287SAlexandru Elisei preempt_disable(); 128263d6287SAlexandru Elisei kvm_arm_setup_mdcr_el2(vcpu); 129263d6287SAlexandru Elisei preempt_enable(); 130263d6287SAlexandru Elisei } 131263d6287SAlexandru Elisei 132263d6287SAlexandru Elisei /** 13384e690bfSAlex Bennée * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state 13484e690bfSAlex Bennée */ 13584e690bfSAlex Bennée 13684e690bfSAlex Bennée void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) 13784e690bfSAlex Bennée { 13884e690bfSAlex Bennée vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state; 13984e690bfSAlex Bennée } 14084e690bfSAlex Bennée 14184e690bfSAlex Bennée /** 14256c7f5e7SAlex Bennée * kvm_arm_setup_debug - set up debug related stuff 14356c7f5e7SAlex Bennée * 14456c7f5e7SAlex Bennée * @vcpu: the vcpu pointer 14556c7f5e7SAlex Bennée * 14656c7f5e7SAlex Bennée * This is called before each entry into the hypervisor to setup any 147263d6287SAlexandru Elisei * debug related registers. 14856c7f5e7SAlex Bennée * 14956c7f5e7SAlex Bennée * Additionally, KVM only traps guest accesses to the debug registers if 150b1da4908SMarc Zyngier * the guest is not actively using them (see the DEBUG_DIRTY 151b1da4908SMarc Zyngier * flag on vcpu->arch.iflags). Since the guest must not interfere 15256c7f5e7SAlex Bennée * with the hardware state when debugging the guest, we must ensure that 15356c7f5e7SAlex Bennée * trapping is enabled whenever we are debugging the guest using the 15456c7f5e7SAlex Bennée * debug registers. 15556c7f5e7SAlex Bennée */ 15656c7f5e7SAlex Bennée 15756c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) 15856c7f5e7SAlex Bennée { 1594942dc66SAndrew Murray unsigned long mdscr, orig_mdcr_el2 = vcpu->arch.mdcr_el2; 16056c7f5e7SAlex Bennée 161eef8c85aSAlex Bennée trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug); 162eef8c85aSAlex Bennée 163263d6287SAlexandru Elisei kvm_arm_setup_mdcr_el2(vcpu); 16456c7f5e7SAlex Bennée 1657dabf02fSOliver Upton /* Check if we need to use the debug registers. */ 1667dabf02fSOliver Upton if (vcpu->guest_debug || kvm_vcpu_os_lock_enabled(vcpu)) { 167337b99bfSAlex Bennée /* Save guest debug state */ 168337b99bfSAlex Bennée save_guest_debug_regs(vcpu); 169337b99bfSAlex Bennée 170337b99bfSAlex Bennée /* 171337b99bfSAlex Bennée * Single Step (ARM ARM D2.12.3 The software step state 172337b99bfSAlex Bennée * machine) 173337b99bfSAlex Bennée * 174337b99bfSAlex Bennée * If we are doing Single Step we need to manipulate 175337b99bfSAlex Bennée * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the 176337b99bfSAlex Bennée * step has occurred the hypervisor will trap the 177337b99bfSAlex Bennée * debug exception and we return to userspace. 178337b99bfSAlex Bennée * 179337b99bfSAlex Bennée * If the guest attempts to single step its userspace 180337b99bfSAlex Bennée * we would have to deal with a trapped exception 181337b99bfSAlex Bennée * while in the guest kernel. Because this would be 182337b99bfSAlex Bennée * hard to unwind we suppress the guest's ability to 183337b99bfSAlex Bennée * do so by masking MDSCR_EL.SS. 184337b99bfSAlex Bennée * 185337b99bfSAlex Bennée * This confuses guest debuggers which use 186337b99bfSAlex Bennée * single-step behind the scenes but everything 187337b99bfSAlex Bennée * returns to normal once the host is no longer 188337b99bfSAlex Bennée * debugging the system. 189337b99bfSAlex Bennée */ 190337b99bfSAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { 191337b99bfSAlex Bennée *vcpu_cpsr(vcpu) |= DBG_SPSR_SS; 1928d404c4cSChristoffer Dall mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 1938d404c4cSChristoffer Dall mdscr |= DBG_MDSCR_SS; 1948d404c4cSChristoffer Dall vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); 195337b99bfSAlex Bennée } else { 1968d404c4cSChristoffer Dall mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 1978d404c4cSChristoffer Dall mdscr &= ~DBG_MDSCR_SS; 1988d404c4cSChristoffer Dall vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); 199337b99bfSAlex Bennée } 200834bf887SAlex Bennée 201eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu)); 202eef8c85aSAlex Bennée 203834bf887SAlex Bennée /* 204834bf887SAlex Bennée * HW Breakpoints and watchpoints 205834bf887SAlex Bennée * 206834bf887SAlex Bennée * We simply switch the debug_ptr to point to our new 207834bf887SAlex Bennée * external_debug_state which has been populated by the 208b1da4908SMarc Zyngier * debug ioctl. The existing DEBUG_DIRTY mechanism ensures 209b1da4908SMarc Zyngier * the registers are updated on the world switch. 210834bf887SAlex Bennée */ 211834bf887SAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) { 212834bf887SAlex Bennée /* Enable breakpoints/watchpoints */ 2138d404c4cSChristoffer Dall mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 2148d404c4cSChristoffer Dall mdscr |= DBG_MDSCR_MDE; 2158d404c4cSChristoffer Dall vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); 216834bf887SAlex Bennée 217834bf887SAlex Bennée vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state; 218b1da4908SMarc Zyngier vcpu_set_flag(vcpu, DEBUG_DIRTY); 219eef8c85aSAlex Bennée 220eef8c85aSAlex Bennée trace_kvm_arm_set_regset("BKPTS", get_num_brps(), 221eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bcr[0], 222eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bvr[0]); 223eef8c85aSAlex Bennée 224eef8c85aSAlex Bennée trace_kvm_arm_set_regset("WAPTS", get_num_wrps(), 225eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wcr[0], 226eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wvr[0]); 2277dabf02fSOliver Upton 2287dabf02fSOliver Upton /* 2297dabf02fSOliver Upton * The OS Lock blocks debug exceptions in all ELs when it is 2307dabf02fSOliver Upton * enabled. If the guest has enabled the OS Lock, constrain its 2317dabf02fSOliver Upton * effects to the guest. Emulate the behavior by clearing 2327dabf02fSOliver Upton * MDSCR_EL1.MDE. In so doing, we ensure that host debug 2337dabf02fSOliver Upton * exceptions are unaffected by guest configuration of the OS 2347dabf02fSOliver Upton * Lock. 2357dabf02fSOliver Upton */ 2367dabf02fSOliver Upton } else if (kvm_vcpu_os_lock_enabled(vcpu)) { 2377dabf02fSOliver Upton mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1); 2387dabf02fSOliver Upton mdscr &= ~DBG_MDSCR_MDE; 2397dabf02fSOliver Upton vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1); 240337b99bfSAlex Bennée } 24156c7f5e7SAlex Bennée } 24256c7f5e7SAlex Bennée 243834bf887SAlex Bennée BUG_ON(!vcpu->guest_debug && 244834bf887SAlex Bennée vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state); 245834bf887SAlex Bennée 24654ceb1bcSChristoffer Dall /* If KDE or MDE are set, perform a full save/restore cycle. */ 2478d404c4cSChristoffer Dall if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE)) 248b1da4908SMarc Zyngier vcpu_set_flag(vcpu, DEBUG_DIRTY); 24954ceb1bcSChristoffer Dall 2504942dc66SAndrew Murray /* Write mdcr_el2 changes since vcpu_load on VHE systems */ 2514942dc66SAndrew Murray if (has_vhe() && orig_mdcr_el2 != vcpu->arch.mdcr_el2) 2524942dc66SAndrew Murray write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2); 2534942dc66SAndrew Murray 2548d404c4cSChristoffer Dall trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1)); 255834bf887SAlex Bennée } 256834bf887SAlex Bennée 25756c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) 25856c7f5e7SAlex Bennée { 259eef8c85aSAlex Bennée trace_kvm_arm_clear_debug(vcpu->guest_debug); 260eef8c85aSAlex Bennée 2617dabf02fSOliver Upton /* 2627dabf02fSOliver Upton * Restore the guest's debug registers if we were using them. 2637dabf02fSOliver Upton */ 2647dabf02fSOliver Upton if (vcpu->guest_debug || kvm_vcpu_os_lock_enabled(vcpu)) { 265337b99bfSAlex Bennée restore_guest_debug_regs(vcpu); 266834bf887SAlex Bennée 267834bf887SAlex Bennée /* 268834bf887SAlex Bennée * If we were using HW debug we need to restore the 269834bf887SAlex Bennée * debug_ptr to the guest debug state. 270834bf887SAlex Bennée */ 271eef8c85aSAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) { 272834bf887SAlex Bennée kvm_arm_reset_debug_ptr(vcpu); 273834bf887SAlex Bennée 274eef8c85aSAlex Bennée trace_kvm_arm_set_regset("BKPTS", get_num_brps(), 275eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bcr[0], 276eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bvr[0]); 277eef8c85aSAlex Bennée 278eef8c85aSAlex Bennée trace_kvm_arm_set_regset("WAPTS", get_num_wrps(), 279eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wcr[0], 280eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wvr[0]); 281eef8c85aSAlex Bennée } 282834bf887SAlex Bennée } 28356c7f5e7SAlex Bennée } 284d2602bb4SSuzuki K Poulose 285d2602bb4SSuzuki K Poulose void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) 286d2602bb4SSuzuki K Poulose { 287d2602bb4SSuzuki K Poulose u64 dfr0; 288d2602bb4SSuzuki K Poulose 289d2602bb4SSuzuki K Poulose /* For VHE, there is nothing to do */ 290d2602bb4SSuzuki K Poulose if (has_vhe()) 291d2602bb4SSuzuki K Poulose return; 292d2602bb4SSuzuki K Poulose 293d2602bb4SSuzuki K Poulose dfr0 = read_sysreg(id_aa64dfr0_el1); 294d2602bb4SSuzuki K Poulose /* 295d2602bb4SSuzuki K Poulose * If SPE is present on this CPU and is available at current EL, 296d2602bb4SSuzuki K Poulose * we may need to check if the host state needs to be saved. 297d2602bb4SSuzuki K Poulose */ 298*fcf37b38SMark Brown if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_PMSVer_SHIFT) && 299d2602bb4SSuzuki K Poulose !(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(SYS_PMBIDR_EL1_P_SHIFT))) 300b1da4908SMarc Zyngier vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_SPE); 301a1319260SSuzuki K Poulose 302a1319260SSuzuki K Poulose /* Check if we have TRBE implemented and available at the host */ 303*fcf37b38SMark Brown if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) && 304a1319260SSuzuki K Poulose !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_PROG)) 305b1da4908SMarc Zyngier vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRBE); 306d2602bb4SSuzuki K Poulose } 307d2602bb4SSuzuki K Poulose 308d2602bb4SSuzuki K Poulose void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) 309d2602bb4SSuzuki K Poulose { 310b1da4908SMarc Zyngier vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE); 311b1da4908SMarc Zyngier vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE); 312d2602bb4SSuzuki K Poulose } 313