2 * Debug and Guest Debug support
4 * Copyright (C) 2015 - Linaro Ltd
5 * Author: Alex Bennée <alex.bennee@linaro.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/kvm_host.h>
22 #include <asm/kvm_arm.h>
24 static DEFINE_PER_CPU(u32, mdcr_el2);
27 * kvm_arm_init_debug - grab what we need for debug
29 * Currently the sole task of this function is to retrieve the initial
30 * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has
31 * presumably been set-up by some knowledgeable bootcode.
33 * It is called once per-cpu during CPU hyp initialisation.
36 void kvm_arm_init_debug(void)
38 __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2));
43 * kvm_arm_setup_debug - set up debug related stuff
45 * @vcpu: the vcpu pointer
47 * This is called before each entry into the hypervisor to setup any
48 * debug related registers. Currently this just ensures we will trap
50 * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
51 * - Debug ROM Address (MDCR_EL2_TDRA)
52 * - OS related registers (MDCR_EL2_TDOSA)
54 * Additionally, KVM only traps guest accesses to the debug registers if
55 * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
56 * flag on vcpu->arch.debug_flags). Since the guest must not interfere
57 * with the hardware state when debugging the guest, we must ensure that
58 * trapping is enabled whenever we are debugging the guest using the
62 void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
64 bool trap_debug = !(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY);
66 vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
67 vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
72 /* Trap on access to debug registers? */
74 vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
78 void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
80 /* Nothing to do yet */