xref: /openbmc/linux/arch/arm64/kvm/debug.c (revision fbb31e5f)
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 /**
72*263d6287SAlexandru Elisei  * kvm_arm_setup_mdcr_el2 - configure vcpu mdcr_el2 value
73*263d6287SAlexandru Elisei  *
74*263d6287SAlexandru Elisei  * @vcpu:	the vcpu pointer
75*263d6287SAlexandru Elisei  *
76*263d6287SAlexandru Elisei  * This ensures we will trap access to:
77*263d6287SAlexandru Elisei  *  - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
78*263d6287SAlexandru Elisei  *  - Debug ROM Address (MDCR_EL2_TDRA)
79*263d6287SAlexandru Elisei  *  - OS related registers (MDCR_EL2_TDOSA)
80*263d6287SAlexandru Elisei  *  - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB)
81*263d6287SAlexandru Elisei  *  - Self-hosted Trace Filter controls (MDCR_EL2_TTRF)
82fbb31e5fSMarc Zyngier  *  - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB)
83*263d6287SAlexandru Elisei  */
84*263d6287SAlexandru Elisei static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
85*263d6287SAlexandru Elisei {
86*263d6287SAlexandru 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
89*263d6287SAlexandru Elisei 	 */
90*263d6287SAlexandru Elisei 	vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
91*263d6287SAlexandru Elisei 	vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
92*263d6287SAlexandru Elisei 				MDCR_EL2_TPMS |
93*263d6287SAlexandru Elisei 				MDCR_EL2_TTRF |
94*263d6287SAlexandru Elisei 				MDCR_EL2_TPMCR |
95*263d6287SAlexandru Elisei 				MDCR_EL2_TDRA |
96*263d6287SAlexandru Elisei 				MDCR_EL2_TDOSA);
97*263d6287SAlexandru Elisei 
98*263d6287SAlexandru Elisei 	/* Is the VM being debugged by userspace? */
99*263d6287SAlexandru Elisei 	if (vcpu->guest_debug)
100*263d6287SAlexandru Elisei 		/* Route all software debug exceptions to EL2 */
101*263d6287SAlexandru Elisei 		vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
102*263d6287SAlexandru Elisei 
103*263d6287SAlexandru Elisei 	/*
104*263d6287SAlexandru Elisei 	 * Trap debug register access when one of the following is true:
105*263d6287SAlexandru Elisei 	 *  - Userspace is using the hardware to debug the guest
106*263d6287SAlexandru Elisei 	 *  (KVM_GUESTDBG_USE_HW is set).
107*263d6287SAlexandru Elisei 	 *  - The guest is not using debug (KVM_ARM64_DEBUG_DIRTY is clear).
108*263d6287SAlexandru Elisei 	 */
109*263d6287SAlexandru Elisei 	if ((vcpu->guest_debug & KVM_GUESTDBG_USE_HW) ||
110*263d6287SAlexandru Elisei 	    !(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
111*263d6287SAlexandru Elisei 		vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
112*263d6287SAlexandru Elisei 
113*263d6287SAlexandru Elisei 	trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2);
114*263d6287SAlexandru Elisei }
115*263d6287SAlexandru Elisei 
116*263d6287SAlexandru Elisei /**
117*263d6287SAlexandru Elisei  * kvm_arm_vcpu_init_debug - setup vcpu debug traps
118*263d6287SAlexandru Elisei  *
119*263d6287SAlexandru Elisei  * @vcpu:	the vcpu pointer
120*263d6287SAlexandru Elisei  *
121*263d6287SAlexandru Elisei  * Set vcpu initial mdcr_el2 value.
122*263d6287SAlexandru Elisei  */
123*263d6287SAlexandru Elisei void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu)
124*263d6287SAlexandru Elisei {
125*263d6287SAlexandru Elisei 	preempt_disable();
126*263d6287SAlexandru Elisei 	kvm_arm_setup_mdcr_el2(vcpu);
127*263d6287SAlexandru Elisei 	preempt_enable();
128*263d6287SAlexandru Elisei }
129*263d6287SAlexandru Elisei 
130*263d6287SAlexandru Elisei /**
13184e690bfSAlex Bennée  * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state
13284e690bfSAlex Bennée  */
13384e690bfSAlex Bennée 
13484e690bfSAlex Bennée void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu)
13584e690bfSAlex Bennée {
13684e690bfSAlex Bennée 	vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state;
13784e690bfSAlex Bennée }
13884e690bfSAlex Bennée 
13984e690bfSAlex Bennée /**
14056c7f5e7SAlex Bennée  * kvm_arm_setup_debug - set up debug related stuff
14156c7f5e7SAlex Bennée  *
14256c7f5e7SAlex Bennée  * @vcpu:	the vcpu pointer
14356c7f5e7SAlex Bennée  *
14456c7f5e7SAlex Bennée  * This is called before each entry into the hypervisor to setup any
145*263d6287SAlexandru Elisei  * debug related registers.
14656c7f5e7SAlex Bennée  *
14756c7f5e7SAlex Bennée  * Additionally, KVM only traps guest accesses to the debug registers if
14856c7f5e7SAlex Bennée  * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
149fa89d31cSDave Martin  * flag on vcpu->arch.flags).  Since the guest must not interfere
15056c7f5e7SAlex Bennée  * with the hardware state when debugging the guest, we must ensure that
15156c7f5e7SAlex Bennée  * trapping is enabled whenever we are debugging the guest using the
15256c7f5e7SAlex Bennée  * debug registers.
15356c7f5e7SAlex Bennée  */
15456c7f5e7SAlex Bennée 
15556c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
15656c7f5e7SAlex Bennée {
1574942dc66SAndrew Murray 	unsigned long mdscr, orig_mdcr_el2 = vcpu->arch.mdcr_el2;
15856c7f5e7SAlex Bennée 
159eef8c85aSAlex Bennée 	trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug);
160eef8c85aSAlex Bennée 
161*263d6287SAlexandru Elisei 	kvm_arm_setup_mdcr_el2(vcpu);
16256c7f5e7SAlex Bennée 
163337b99bfSAlex Bennée 	/* Is Guest debugging in effect? */
164337b99bfSAlex Bennée 	if (vcpu->guest_debug) {
165337b99bfSAlex Bennée 		/* Save guest debug state */
166337b99bfSAlex Bennée 		save_guest_debug_regs(vcpu);
167337b99bfSAlex Bennée 
168337b99bfSAlex Bennée 		/*
169337b99bfSAlex Bennée 		 * Single Step (ARM ARM D2.12.3 The software step state
170337b99bfSAlex Bennée 		 * machine)
171337b99bfSAlex Bennée 		 *
172337b99bfSAlex Bennée 		 * If we are doing Single Step we need to manipulate
173337b99bfSAlex Bennée 		 * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
174337b99bfSAlex Bennée 		 * step has occurred the hypervisor will trap the
175337b99bfSAlex Bennée 		 * debug exception and we return to userspace.
176337b99bfSAlex Bennée 		 *
177337b99bfSAlex Bennée 		 * If the guest attempts to single step its userspace
178337b99bfSAlex Bennée 		 * we would have to deal with a trapped exception
179337b99bfSAlex Bennée 		 * while in the guest kernel. Because this would be
180337b99bfSAlex Bennée 		 * hard to unwind we suppress the guest's ability to
181337b99bfSAlex Bennée 		 * do so by masking MDSCR_EL.SS.
182337b99bfSAlex Bennée 		 *
183337b99bfSAlex Bennée 		 * This confuses guest debuggers which use
184337b99bfSAlex Bennée 		 * single-step behind the scenes but everything
185337b99bfSAlex Bennée 		 * returns to normal once the host is no longer
186337b99bfSAlex Bennée 		 * debugging the system.
187337b99bfSAlex Bennée 		 */
188337b99bfSAlex Bennée 		if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
189337b99bfSAlex Bennée 			*vcpu_cpsr(vcpu) |=  DBG_SPSR_SS;
1908d404c4cSChristoffer Dall 			mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
1918d404c4cSChristoffer Dall 			mdscr |= DBG_MDSCR_SS;
1928d404c4cSChristoffer Dall 			vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
193337b99bfSAlex Bennée 		} else {
1948d404c4cSChristoffer Dall 			mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
1958d404c4cSChristoffer Dall 			mdscr &= ~DBG_MDSCR_SS;
1968d404c4cSChristoffer Dall 			vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
197337b99bfSAlex Bennée 		}
198834bf887SAlex Bennée 
199eef8c85aSAlex Bennée 		trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu));
200eef8c85aSAlex Bennée 
201834bf887SAlex Bennée 		/*
202834bf887SAlex Bennée 		 * HW Breakpoints and watchpoints
203834bf887SAlex Bennée 		 *
204834bf887SAlex Bennée 		 * We simply switch the debug_ptr to point to our new
205834bf887SAlex Bennée 		 * external_debug_state which has been populated by the
206834bf887SAlex Bennée 		 * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
207834bf887SAlex Bennée 		 * mechanism ensures the registers are updated on the
208834bf887SAlex Bennée 		 * world switch.
209834bf887SAlex Bennée 		 */
210834bf887SAlex Bennée 		if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
211834bf887SAlex Bennée 			/* Enable breakpoints/watchpoints */
2128d404c4cSChristoffer Dall 			mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
2138d404c4cSChristoffer Dall 			mdscr |= DBG_MDSCR_MDE;
2148d404c4cSChristoffer Dall 			vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
215834bf887SAlex Bennée 
216834bf887SAlex Bennée 			vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
217fa89d31cSDave Martin 			vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
218eef8c85aSAlex 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]);
226337b99bfSAlex Bennée 		}
22756c7f5e7SAlex Bennée 	}
22856c7f5e7SAlex Bennée 
229834bf887SAlex Bennée 	BUG_ON(!vcpu->guest_debug &&
230834bf887SAlex Bennée 		vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
231834bf887SAlex Bennée 
23254ceb1bcSChristoffer Dall 	/* If KDE or MDE are set, perform a full save/restore cycle. */
2338d404c4cSChristoffer Dall 	if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE))
234fa89d31cSDave Martin 		vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
23554ceb1bcSChristoffer Dall 
2364942dc66SAndrew Murray 	/* Write mdcr_el2 changes since vcpu_load on VHE systems */
2374942dc66SAndrew Murray 	if (has_vhe() && orig_mdcr_el2 != vcpu->arch.mdcr_el2)
2384942dc66SAndrew Murray 		write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
2394942dc66SAndrew Murray 
2408d404c4cSChristoffer Dall 	trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1));
241834bf887SAlex Bennée }
242834bf887SAlex Bennée 
24356c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
24456c7f5e7SAlex Bennée {
245eef8c85aSAlex Bennée 	trace_kvm_arm_clear_debug(vcpu->guest_debug);
246eef8c85aSAlex Bennée 
247834bf887SAlex Bennée 	if (vcpu->guest_debug) {
248337b99bfSAlex Bennée 		restore_guest_debug_regs(vcpu);
249834bf887SAlex Bennée 
250834bf887SAlex Bennée 		/*
251834bf887SAlex Bennée 		 * If we were using HW debug we need to restore the
252834bf887SAlex Bennée 		 * debug_ptr to the guest debug state.
253834bf887SAlex Bennée 		 */
254eef8c85aSAlex Bennée 		if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
255834bf887SAlex Bennée 			kvm_arm_reset_debug_ptr(vcpu);
256834bf887SAlex Bennée 
257eef8c85aSAlex Bennée 			trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
258eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_bcr[0],
259eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_bvr[0]);
260eef8c85aSAlex Bennée 
261eef8c85aSAlex Bennée 			trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
262eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_wcr[0],
263eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_wvr[0]);
264eef8c85aSAlex Bennée 		}
265834bf887SAlex Bennée 	}
26656c7f5e7SAlex Bennée }
267d2602bb4SSuzuki K Poulose 
268d2602bb4SSuzuki K Poulose void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu)
269d2602bb4SSuzuki K Poulose {
270d2602bb4SSuzuki K Poulose 	u64 dfr0;
271d2602bb4SSuzuki K Poulose 
272d2602bb4SSuzuki K Poulose 	/* For VHE, there is nothing to do */
273d2602bb4SSuzuki K Poulose 	if (has_vhe())
274d2602bb4SSuzuki K Poulose 		return;
275d2602bb4SSuzuki K Poulose 
276d2602bb4SSuzuki K Poulose 	dfr0 = read_sysreg(id_aa64dfr0_el1);
277d2602bb4SSuzuki K Poulose 	/*
278d2602bb4SSuzuki K Poulose 	 * If SPE is present on this CPU and is available at current EL,
279d2602bb4SSuzuki K Poulose 	 * we may need to check if the host state needs to be saved.
280d2602bb4SSuzuki K Poulose 	 */
281d2602bb4SSuzuki K Poulose 	if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_PMSVER_SHIFT) &&
282d2602bb4SSuzuki K Poulose 	    !(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(SYS_PMBIDR_EL1_P_SHIFT)))
283d2602bb4SSuzuki K Poulose 		vcpu->arch.flags |= KVM_ARM64_DEBUG_STATE_SAVE_SPE;
284a1319260SSuzuki K Poulose 
285a1319260SSuzuki K Poulose 	/* Check if we have TRBE implemented and available at the host */
286a1319260SSuzuki K Poulose 	if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_TRBE_SHIFT) &&
287a1319260SSuzuki K Poulose 	    !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_PROG))
288a1319260SSuzuki K Poulose 		vcpu->arch.flags |= KVM_ARM64_DEBUG_STATE_SAVE_TRBE;
289d2602bb4SSuzuki K Poulose }
290d2602bb4SSuzuki K Poulose 
291d2602bb4SSuzuki K Poulose void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
292d2602bb4SSuzuki K Poulose {
293a1319260SSuzuki K Poulose 	vcpu->arch.flags &= ~(KVM_ARM64_DEBUG_STATE_SAVE_SPE |
294a1319260SSuzuki K Poulose 			      KVM_ARM64_DEBUG_STATE_SAVE_TRBE);
295d2602bb4SSuzuki K Poulose }
296