156c7f5e7SAlex Bennée /* 256c7f5e7SAlex Bennée * Debug and Guest Debug support 356c7f5e7SAlex Bennée * 456c7f5e7SAlex Bennée * Copyright (C) 2015 - Linaro Ltd 556c7f5e7SAlex Bennée * Author: Alex Bennée <alex.bennee@linaro.org> 656c7f5e7SAlex Bennée * 756c7f5e7SAlex Bennée * This program is free software; you can redistribute it and/or modify 856c7f5e7SAlex Bennée * it under the terms of the GNU General Public License version 2 as 956c7f5e7SAlex Bennée * published by the Free Software Foundation. 1056c7f5e7SAlex Bennée * 1156c7f5e7SAlex Bennée * This program is distributed in the hope that it will be useful, 1256c7f5e7SAlex Bennée * but WITHOUT ANY WARRANTY; without even the implied warranty of 1356c7f5e7SAlex Bennée * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1456c7f5e7SAlex Bennée * GNU General Public License for more details. 1556c7f5e7SAlex Bennée * 1656c7f5e7SAlex Bennée * You should have received a copy of the GNU General Public License 1756c7f5e7SAlex Bennée * along with this program. If not, see <http://www.gnu.org/licenses/>. 1856c7f5e7SAlex Bennée */ 1956c7f5e7SAlex Bennée 2056c7f5e7SAlex Bennée #include <linux/kvm_host.h> 21eef8c85aSAlex Bennée #include <linux/hw_breakpoint.h> 2256c7f5e7SAlex Bennée 23337b99bfSAlex Bennée #include <asm/debug-monitors.h> 24337b99bfSAlex Bennée #include <asm/kvm_asm.h> 2556c7f5e7SAlex Bennée #include <asm/kvm_arm.h> 26337b99bfSAlex Bennée #include <asm/kvm_emulate.h> 27337b99bfSAlex Bennée 28eef8c85aSAlex Bennée #include "trace.h" 29eef8c85aSAlex Bennée 30337b99bfSAlex Bennée /* These are the bits of MDSCR_EL1 we may manipulate */ 31337b99bfSAlex Bennée #define MDSCR_EL1_DEBUG_MASK (DBG_MDSCR_SS | \ 32337b99bfSAlex Bennée DBG_MDSCR_KDE | \ 33337b99bfSAlex Bennée DBG_MDSCR_MDE) 3456c7f5e7SAlex Bennée 3556c7f5e7SAlex Bennée static DEFINE_PER_CPU(u32, mdcr_el2); 3656c7f5e7SAlex Bennée 3756c7f5e7SAlex Bennée /** 38337b99bfSAlex Bennée * save/restore_guest_debug_regs 39337b99bfSAlex Bennée * 40337b99bfSAlex Bennée * For some debug operations we need to tweak some guest registers. As 41337b99bfSAlex Bennée * a result we need to save the state of those registers before we 42337b99bfSAlex Bennée * make those modifications. 43337b99bfSAlex Bennée * 44337b99bfSAlex Bennée * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled 45337b99bfSAlex Bennée * after we have restored the preserved value to the main context. 46337b99bfSAlex Bennée */ 47337b99bfSAlex Bennée static void save_guest_debug_regs(struct kvm_vcpu *vcpu) 48337b99bfSAlex Bennée { 49337b99bfSAlex Bennée vcpu->arch.guest_debug_preserved.mdscr_el1 = vcpu_sys_reg(vcpu, MDSCR_EL1); 50eef8c85aSAlex Bennée 51eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("Saved MDSCR_EL1", 52eef8c85aSAlex Bennée vcpu->arch.guest_debug_preserved.mdscr_el1); 53337b99bfSAlex Bennée } 54337b99bfSAlex Bennée 55337b99bfSAlex Bennée static void restore_guest_debug_regs(struct kvm_vcpu *vcpu) 56337b99bfSAlex Bennée { 57337b99bfSAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1) = vcpu->arch.guest_debug_preserved.mdscr_el1; 58eef8c85aSAlex Bennée 59eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("Restored MDSCR_EL1", 60eef8c85aSAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1)); 61337b99bfSAlex Bennée } 62337b99bfSAlex Bennée 63337b99bfSAlex Bennée /** 6456c7f5e7SAlex Bennée * kvm_arm_init_debug - grab what we need for debug 6556c7f5e7SAlex Bennée * 6656c7f5e7SAlex Bennée * Currently the sole task of this function is to retrieve the initial 6756c7f5e7SAlex Bennée * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has 6856c7f5e7SAlex Bennée * presumably been set-up by some knowledgeable bootcode. 6956c7f5e7SAlex Bennée * 7056c7f5e7SAlex Bennée * It is called once per-cpu during CPU hyp initialisation. 7156c7f5e7SAlex Bennée */ 7256c7f5e7SAlex Bennée 7356c7f5e7SAlex Bennée void kvm_arm_init_debug(void) 7456c7f5e7SAlex Bennée { 7556c7f5e7SAlex Bennée __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2)); 7656c7f5e7SAlex Bennée } 7756c7f5e7SAlex Bennée 7856c7f5e7SAlex Bennée /** 7984e690bfSAlex Bennée * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state 8084e690bfSAlex Bennée */ 8184e690bfSAlex Bennée 8284e690bfSAlex Bennée void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) 8384e690bfSAlex Bennée { 8484e690bfSAlex Bennée vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state; 8584e690bfSAlex Bennée } 8684e690bfSAlex Bennée 8784e690bfSAlex Bennée /** 8856c7f5e7SAlex Bennée * kvm_arm_setup_debug - set up debug related stuff 8956c7f5e7SAlex Bennée * 9056c7f5e7SAlex Bennée * @vcpu: the vcpu pointer 9156c7f5e7SAlex Bennée * 9256c7f5e7SAlex Bennée * This is called before each entry into the hypervisor to setup any 9356c7f5e7SAlex Bennée * debug related registers. Currently this just ensures we will trap 9456c7f5e7SAlex Bennée * access to: 9556c7f5e7SAlex Bennée * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR) 9656c7f5e7SAlex Bennée * - Debug ROM Address (MDCR_EL2_TDRA) 9756c7f5e7SAlex Bennée * - OS related registers (MDCR_EL2_TDOSA) 98f85279b4SWill Deacon * - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB) 9956c7f5e7SAlex Bennée * 10056c7f5e7SAlex Bennée * Additionally, KVM only traps guest accesses to the debug registers if 10156c7f5e7SAlex Bennée * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY 10256c7f5e7SAlex Bennée * flag on vcpu->arch.debug_flags). Since the guest must not interfere 10356c7f5e7SAlex Bennée * with the hardware state when debugging the guest, we must ensure that 10456c7f5e7SAlex Bennée * trapping is enabled whenever we are debugging the guest using the 10556c7f5e7SAlex Bennée * debug registers. 10656c7f5e7SAlex Bennée */ 10756c7f5e7SAlex Bennée 10856c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) 10956c7f5e7SAlex Bennée { 11056c7f5e7SAlex Bennée bool trap_debug = !(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY); 11156c7f5e7SAlex Bennée 112eef8c85aSAlex Bennée trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug); 113eef8c85aSAlex Bennée 114f85279b4SWill Deacon /* 115f85279b4SWill Deacon * This also clears MDCR_EL2_E2PB_MASK to disable guest access 116f85279b4SWill Deacon * to the profiling buffer. 117f85279b4SWill Deacon */ 11856c7f5e7SAlex Bennée vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK; 11956c7f5e7SAlex Bennée vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM | 120f85279b4SWill Deacon MDCR_EL2_TPMS | 12156c7f5e7SAlex Bennée MDCR_EL2_TPMCR | 12256c7f5e7SAlex Bennée MDCR_EL2_TDRA | 12356c7f5e7SAlex Bennée MDCR_EL2_TDOSA); 12456c7f5e7SAlex Bennée 125337b99bfSAlex Bennée /* Is Guest debugging in effect? */ 126337b99bfSAlex Bennée if (vcpu->guest_debug) { 127337b99bfSAlex Bennée /* Route all software debug exceptions to EL2 */ 1284bd611caSAlex Bennée vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE; 129337b99bfSAlex Bennée 130337b99bfSAlex Bennée /* Save guest debug state */ 131337b99bfSAlex Bennée save_guest_debug_regs(vcpu); 132337b99bfSAlex Bennée 133337b99bfSAlex Bennée /* 134337b99bfSAlex Bennée * Single Step (ARM ARM D2.12.3 The software step state 135337b99bfSAlex Bennée * machine) 136337b99bfSAlex Bennée * 137337b99bfSAlex Bennée * If we are doing Single Step we need to manipulate 138337b99bfSAlex Bennée * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the 139337b99bfSAlex Bennée * step has occurred the hypervisor will trap the 140337b99bfSAlex Bennée * debug exception and we return to userspace. 141337b99bfSAlex Bennée * 142337b99bfSAlex Bennée * If the guest attempts to single step its userspace 143337b99bfSAlex Bennée * we would have to deal with a trapped exception 144337b99bfSAlex Bennée * while in the guest kernel. Because this would be 145337b99bfSAlex Bennée * hard to unwind we suppress the guest's ability to 146337b99bfSAlex Bennée * do so by masking MDSCR_EL.SS. 147337b99bfSAlex Bennée * 148337b99bfSAlex Bennée * This confuses guest debuggers which use 149337b99bfSAlex Bennée * single-step behind the scenes but everything 150337b99bfSAlex Bennée * returns to normal once the host is no longer 151337b99bfSAlex Bennée * debugging the system. 152337b99bfSAlex Bennée */ 153337b99bfSAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { 154337b99bfSAlex Bennée *vcpu_cpsr(vcpu) |= DBG_SPSR_SS; 155337b99bfSAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_SS; 156337b99bfSAlex Bennée } else { 157337b99bfSAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1) &= ~DBG_MDSCR_SS; 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 */ 173834bf887SAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_MDE; 174834bf887SAlex Bennée 175834bf887SAlex Bennée vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state; 176834bf887SAlex Bennée vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY; 177834bf887SAlex Bennée trap_debug = true; 178eef8c85aSAlex Bennée 179eef8c85aSAlex Bennée trace_kvm_arm_set_regset("BKPTS", get_num_brps(), 180eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bcr[0], 181eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bvr[0]); 182eef8c85aSAlex Bennée 183eef8c85aSAlex Bennée trace_kvm_arm_set_regset("WAPTS", get_num_wrps(), 184eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wcr[0], 185eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wvr[0]); 186337b99bfSAlex Bennée } 18756c7f5e7SAlex Bennée } 18856c7f5e7SAlex Bennée 189834bf887SAlex Bennée BUG_ON(!vcpu->guest_debug && 190834bf887SAlex Bennée vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state); 191834bf887SAlex Bennée 192834bf887SAlex Bennée /* Trap debug register access */ 193834bf887SAlex Bennée if (trap_debug) 194834bf887SAlex Bennée vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA; 195eef8c85aSAlex Bennée 19654ceb1bcSChristoffer Dall /* If KDE or MDE are set, perform a full save/restore cycle. */ 19754ceb1bcSChristoffer Dall if ((vcpu_sys_reg(vcpu, MDSCR_EL1) & DBG_MDSCR_KDE) || 19854ceb1bcSChristoffer Dall (vcpu_sys_reg(vcpu, MDSCR_EL1) & DBG_MDSCR_MDE)) 19954ceb1bcSChristoffer Dall vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY; 20054ceb1bcSChristoffer Dall 201eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2); 202eef8c85aSAlex Bennée trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_sys_reg(vcpu, MDSCR_EL1)); 203834bf887SAlex Bennée } 204834bf887SAlex Bennée 20556c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) 20656c7f5e7SAlex Bennée { 207eef8c85aSAlex Bennée trace_kvm_arm_clear_debug(vcpu->guest_debug); 208eef8c85aSAlex Bennée 209834bf887SAlex Bennée if (vcpu->guest_debug) { 210337b99bfSAlex Bennée restore_guest_debug_regs(vcpu); 211834bf887SAlex Bennée 212834bf887SAlex Bennée /* 213834bf887SAlex Bennée * If we were using HW debug we need to restore the 214834bf887SAlex Bennée * debug_ptr to the guest debug state. 215834bf887SAlex Bennée */ 216eef8c85aSAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) { 217834bf887SAlex Bennée kvm_arm_reset_debug_ptr(vcpu); 218834bf887SAlex Bennée 219eef8c85aSAlex Bennée trace_kvm_arm_set_regset("BKPTS", get_num_brps(), 220eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bcr[0], 221eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_bvr[0]); 222eef8c85aSAlex Bennée 223eef8c85aSAlex Bennée trace_kvm_arm_set_regset("WAPTS", get_num_wrps(), 224eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wcr[0], 225eef8c85aSAlex Bennée &vcpu->arch.debug_ptr->dbg_wvr[0]); 226eef8c85aSAlex Bennée } 227834bf887SAlex Bennée } 22856c7f5e7SAlex Bennée } 229696673d1SAlex Bennée 230696673d1SAlex Bennée 231696673d1SAlex Bennée /* 232696673d1SAlex Bennée * After successfully emulating an instruction, we might want to 233696673d1SAlex Bennée * return to user space with a KVM_EXIT_DEBUG. We can only do this 234696673d1SAlex Bennée * once the emulation is complete, though, so for userspace emulations 235696673d1SAlex Bennée * we have to wait until we have re-entered KVM before calling this 236696673d1SAlex Bennée * helper. 237696673d1SAlex Bennée * 238696673d1SAlex Bennée * Return true (and set exit_reason) to return to userspace or false 239696673d1SAlex Bennée * if no further action is required. 240696673d1SAlex Bennée */ 241696673d1SAlex Bennée bool kvm_arm_handle_step_debug(struct kvm_vcpu *vcpu, struct kvm_run *run) 242696673d1SAlex Bennée { 243696673d1SAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { 244696673d1SAlex Bennée run->exit_reason = KVM_EXIT_DEBUG; 245696673d1SAlex Bennée run->debug.arch.hsr = ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT; 246696673d1SAlex Bennée return true; 247696673d1SAlex Bennée } 248696673d1SAlex Bennée return false; 249696673d1SAlex Bennée } 250