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> 2156c7f5e7SAlex Bennée 22*337b99bfSAlex Bennée #include <asm/debug-monitors.h> 23*337b99bfSAlex Bennée #include <asm/kvm_asm.h> 2456c7f5e7SAlex Bennée #include <asm/kvm_arm.h> 25*337b99bfSAlex Bennée #include <asm/kvm_emulate.h> 26*337b99bfSAlex Bennée 27*337b99bfSAlex Bennée /* These are the bits of MDSCR_EL1 we may manipulate */ 28*337b99bfSAlex Bennée #define MDSCR_EL1_DEBUG_MASK (DBG_MDSCR_SS | \ 29*337b99bfSAlex Bennée DBG_MDSCR_KDE | \ 30*337b99bfSAlex Bennée DBG_MDSCR_MDE) 3156c7f5e7SAlex Bennée 3256c7f5e7SAlex Bennée static DEFINE_PER_CPU(u32, mdcr_el2); 3356c7f5e7SAlex Bennée 3456c7f5e7SAlex Bennée /** 35*337b99bfSAlex Bennée * save/restore_guest_debug_regs 36*337b99bfSAlex Bennée * 37*337b99bfSAlex Bennée * For some debug operations we need to tweak some guest registers. As 38*337b99bfSAlex Bennée * a result we need to save the state of those registers before we 39*337b99bfSAlex Bennée * make those modifications. 40*337b99bfSAlex Bennée * 41*337b99bfSAlex Bennée * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled 42*337b99bfSAlex Bennée * after we have restored the preserved value to the main context. 43*337b99bfSAlex Bennée */ 44*337b99bfSAlex Bennée static void save_guest_debug_regs(struct kvm_vcpu *vcpu) 45*337b99bfSAlex Bennée { 46*337b99bfSAlex Bennée vcpu->arch.guest_debug_preserved.mdscr_el1 = vcpu_sys_reg(vcpu, MDSCR_EL1); 47*337b99bfSAlex Bennée } 48*337b99bfSAlex Bennée 49*337b99bfSAlex Bennée static void restore_guest_debug_regs(struct kvm_vcpu *vcpu) 50*337b99bfSAlex Bennée { 51*337b99bfSAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1) = vcpu->arch.guest_debug_preserved.mdscr_el1; 52*337b99bfSAlex Bennée } 53*337b99bfSAlex Bennée 54*337b99bfSAlex Bennée /** 5556c7f5e7SAlex Bennée * kvm_arm_init_debug - grab what we need for debug 5656c7f5e7SAlex Bennée * 5756c7f5e7SAlex Bennée * Currently the sole task of this function is to retrieve the initial 5856c7f5e7SAlex Bennée * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has 5956c7f5e7SAlex Bennée * presumably been set-up by some knowledgeable bootcode. 6056c7f5e7SAlex Bennée * 6156c7f5e7SAlex Bennée * It is called once per-cpu during CPU hyp initialisation. 6256c7f5e7SAlex Bennée */ 6356c7f5e7SAlex Bennée 6456c7f5e7SAlex Bennée void kvm_arm_init_debug(void) 6556c7f5e7SAlex Bennée { 6656c7f5e7SAlex Bennée __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2)); 6756c7f5e7SAlex Bennée } 6856c7f5e7SAlex Bennée 6956c7f5e7SAlex Bennée /** 7056c7f5e7SAlex Bennée * kvm_arm_setup_debug - set up debug related stuff 7156c7f5e7SAlex Bennée * 7256c7f5e7SAlex Bennée * @vcpu: the vcpu pointer 7356c7f5e7SAlex Bennée * 7456c7f5e7SAlex Bennée * This is called before each entry into the hypervisor to setup any 7556c7f5e7SAlex Bennée * debug related registers. Currently this just ensures we will trap 7656c7f5e7SAlex Bennée * access to: 7756c7f5e7SAlex Bennée * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR) 7856c7f5e7SAlex Bennée * - Debug ROM Address (MDCR_EL2_TDRA) 7956c7f5e7SAlex Bennée * - OS related registers (MDCR_EL2_TDOSA) 8056c7f5e7SAlex Bennée * 8156c7f5e7SAlex Bennée * Additionally, KVM only traps guest accesses to the debug registers if 8256c7f5e7SAlex Bennée * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY 8356c7f5e7SAlex Bennée * flag on vcpu->arch.debug_flags). Since the guest must not interfere 8456c7f5e7SAlex Bennée * with the hardware state when debugging the guest, we must ensure that 8556c7f5e7SAlex Bennée * trapping is enabled whenever we are debugging the guest using the 8656c7f5e7SAlex Bennée * debug registers. 8756c7f5e7SAlex Bennée */ 8856c7f5e7SAlex Bennée 8956c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) 9056c7f5e7SAlex Bennée { 9156c7f5e7SAlex Bennée bool trap_debug = !(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY); 9256c7f5e7SAlex Bennée 9356c7f5e7SAlex Bennée vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK; 9456c7f5e7SAlex Bennée vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM | 9556c7f5e7SAlex Bennée MDCR_EL2_TPMCR | 9656c7f5e7SAlex Bennée MDCR_EL2_TDRA | 9756c7f5e7SAlex Bennée MDCR_EL2_TDOSA); 9856c7f5e7SAlex Bennée 9956c7f5e7SAlex Bennée /* Trap on access to debug registers? */ 10056c7f5e7SAlex Bennée if (trap_debug) 10156c7f5e7SAlex Bennée vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA; 10256c7f5e7SAlex Bennée 103*337b99bfSAlex Bennée /* Is Guest debugging in effect? */ 104*337b99bfSAlex Bennée if (vcpu->guest_debug) { 105*337b99bfSAlex Bennée /* Route all software debug exceptions to EL2 */ 1064bd611caSAlex Bennée vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE; 107*337b99bfSAlex Bennée 108*337b99bfSAlex Bennée /* Save guest debug state */ 109*337b99bfSAlex Bennée save_guest_debug_regs(vcpu); 110*337b99bfSAlex Bennée 111*337b99bfSAlex Bennée /* 112*337b99bfSAlex Bennée * Single Step (ARM ARM D2.12.3 The software step state 113*337b99bfSAlex Bennée * machine) 114*337b99bfSAlex Bennée * 115*337b99bfSAlex Bennée * If we are doing Single Step we need to manipulate 116*337b99bfSAlex Bennée * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the 117*337b99bfSAlex Bennée * step has occurred the hypervisor will trap the 118*337b99bfSAlex Bennée * debug exception and we return to userspace. 119*337b99bfSAlex Bennée * 120*337b99bfSAlex Bennée * If the guest attempts to single step its userspace 121*337b99bfSAlex Bennée * we would have to deal with a trapped exception 122*337b99bfSAlex Bennée * while in the guest kernel. Because this would be 123*337b99bfSAlex Bennée * hard to unwind we suppress the guest's ability to 124*337b99bfSAlex Bennée * do so by masking MDSCR_EL.SS. 125*337b99bfSAlex Bennée * 126*337b99bfSAlex Bennée * This confuses guest debuggers which use 127*337b99bfSAlex Bennée * single-step behind the scenes but everything 128*337b99bfSAlex Bennée * returns to normal once the host is no longer 129*337b99bfSAlex Bennée * debugging the system. 130*337b99bfSAlex Bennée */ 131*337b99bfSAlex Bennée if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { 132*337b99bfSAlex Bennée *vcpu_cpsr(vcpu) |= DBG_SPSR_SS; 133*337b99bfSAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_SS; 134*337b99bfSAlex Bennée } else { 135*337b99bfSAlex Bennée vcpu_sys_reg(vcpu, MDSCR_EL1) &= ~DBG_MDSCR_SS; 136*337b99bfSAlex Bennée } 137*337b99bfSAlex Bennée } 13856c7f5e7SAlex Bennée } 13956c7f5e7SAlex Bennée 14056c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) 14156c7f5e7SAlex Bennée { 142*337b99bfSAlex Bennée if (vcpu->guest_debug) 143*337b99bfSAlex Bennée restore_guest_debug_regs(vcpu); 14456c7f5e7SAlex Bennée } 145