xref: /openbmc/linux/arch/arm64/kvm/debug.c (revision 263d6287)
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)
82*263d6287SAlexandru Elisei  */
83*263d6287SAlexandru Elisei static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
84*263d6287SAlexandru Elisei {
85*263d6287SAlexandru Elisei 	/*
86*263d6287SAlexandru Elisei 	 * This also clears MDCR_EL2_E2PB_MASK to disable guest access
87*263d6287SAlexandru Elisei 	 * to the profiling buffer.
88*263d6287SAlexandru Elisei 	 */
89*263d6287SAlexandru Elisei 	vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
90*263d6287SAlexandru Elisei 	vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
91*263d6287SAlexandru Elisei 				MDCR_EL2_TPMS |
92*263d6287SAlexandru Elisei 				MDCR_EL2_TTRF |
93*263d6287SAlexandru Elisei 				MDCR_EL2_TPMCR |
94*263d6287SAlexandru Elisei 				MDCR_EL2_TDRA |
95*263d6287SAlexandru Elisei 				MDCR_EL2_TDOSA);
96*263d6287SAlexandru Elisei 
97*263d6287SAlexandru Elisei 	/* Is the VM being debugged by userspace? */
98*263d6287SAlexandru Elisei 	if (vcpu->guest_debug)
99*263d6287SAlexandru Elisei 		/* Route all software debug exceptions to EL2 */
100*263d6287SAlexandru Elisei 		vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
101*263d6287SAlexandru Elisei 
102*263d6287SAlexandru Elisei 	/*
103*263d6287SAlexandru Elisei 	 * Trap debug register access when one of the following is true:
104*263d6287SAlexandru Elisei 	 *  - Userspace is using the hardware to debug the guest
105*263d6287SAlexandru Elisei 	 *  (KVM_GUESTDBG_USE_HW is set).
106*263d6287SAlexandru Elisei 	 *  - The guest is not using debug (KVM_ARM64_DEBUG_DIRTY is clear).
107*263d6287SAlexandru Elisei 	 */
108*263d6287SAlexandru Elisei 	if ((vcpu->guest_debug & KVM_GUESTDBG_USE_HW) ||
109*263d6287SAlexandru Elisei 	    !(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
110*263d6287SAlexandru Elisei 		vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
111*263d6287SAlexandru Elisei 
112*263d6287SAlexandru Elisei 	trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2);
113*263d6287SAlexandru Elisei }
114*263d6287SAlexandru Elisei 
115*263d6287SAlexandru Elisei /**
116*263d6287SAlexandru Elisei  * kvm_arm_vcpu_init_debug - setup vcpu debug traps
117*263d6287SAlexandru Elisei  *
118*263d6287SAlexandru Elisei  * @vcpu:	the vcpu pointer
119*263d6287SAlexandru Elisei  *
120*263d6287SAlexandru Elisei  * Set vcpu initial mdcr_el2 value.
121*263d6287SAlexandru Elisei  */
122*263d6287SAlexandru Elisei void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu)
123*263d6287SAlexandru Elisei {
124*263d6287SAlexandru Elisei 	preempt_disable();
125*263d6287SAlexandru Elisei 	kvm_arm_setup_mdcr_el2(vcpu);
126*263d6287SAlexandru Elisei 	preempt_enable();
127*263d6287SAlexandru Elisei }
128*263d6287SAlexandru Elisei 
129*263d6287SAlexandru Elisei /**
13084e690bfSAlex Bennée  * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state
13184e690bfSAlex Bennée  */
13284e690bfSAlex Bennée 
13384e690bfSAlex Bennée void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu)
13484e690bfSAlex Bennée {
13584e690bfSAlex Bennée 	vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state;
13684e690bfSAlex Bennée }
13784e690bfSAlex Bennée 
13884e690bfSAlex Bennée /**
13956c7f5e7SAlex Bennée  * kvm_arm_setup_debug - set up debug related stuff
14056c7f5e7SAlex Bennée  *
14156c7f5e7SAlex Bennée  * @vcpu:	the vcpu pointer
14256c7f5e7SAlex Bennée  *
14356c7f5e7SAlex Bennée  * This is called before each entry into the hypervisor to setup any
144*263d6287SAlexandru Elisei  * debug related registers.
14556c7f5e7SAlex Bennée  *
14656c7f5e7SAlex Bennée  * Additionally, KVM only traps guest accesses to the debug registers if
14756c7f5e7SAlex Bennée  * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
148fa89d31cSDave Martin  * flag on vcpu->arch.flags).  Since the guest must not interfere
14956c7f5e7SAlex Bennée  * with the hardware state when debugging the guest, we must ensure that
15056c7f5e7SAlex Bennée  * trapping is enabled whenever we are debugging the guest using the
15156c7f5e7SAlex Bennée  * debug registers.
15256c7f5e7SAlex Bennée  */
15356c7f5e7SAlex Bennée 
15456c7f5e7SAlex Bennée void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
15556c7f5e7SAlex Bennée {
1564942dc66SAndrew Murray 	unsigned long mdscr, orig_mdcr_el2 = vcpu->arch.mdcr_el2;
15756c7f5e7SAlex Bennée 
158eef8c85aSAlex Bennée 	trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug);
159eef8c85aSAlex Bennée 
160*263d6287SAlexandru Elisei 	kvm_arm_setup_mdcr_el2(vcpu);
16156c7f5e7SAlex Bennée 
162337b99bfSAlex Bennée 	/* Is Guest debugging in effect? */
163337b99bfSAlex Bennée 	if (vcpu->guest_debug) {
164337b99bfSAlex Bennée 		/* Save guest debug state */
165337b99bfSAlex Bennée 		save_guest_debug_regs(vcpu);
166337b99bfSAlex Bennée 
167337b99bfSAlex Bennée 		/*
168337b99bfSAlex Bennée 		 * Single Step (ARM ARM D2.12.3 The software step state
169337b99bfSAlex Bennée 		 * machine)
170337b99bfSAlex Bennée 		 *
171337b99bfSAlex Bennée 		 * If we are doing Single Step we need to manipulate
172337b99bfSAlex Bennée 		 * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
173337b99bfSAlex Bennée 		 * step has occurred the hypervisor will trap the
174337b99bfSAlex Bennée 		 * debug exception and we return to userspace.
175337b99bfSAlex Bennée 		 *
176337b99bfSAlex Bennée 		 * If the guest attempts to single step its userspace
177337b99bfSAlex Bennée 		 * we would have to deal with a trapped exception
178337b99bfSAlex Bennée 		 * while in the guest kernel. Because this would be
179337b99bfSAlex Bennée 		 * hard to unwind we suppress the guest's ability to
180337b99bfSAlex Bennée 		 * do so by masking MDSCR_EL.SS.
181337b99bfSAlex Bennée 		 *
182337b99bfSAlex Bennée 		 * This confuses guest debuggers which use
183337b99bfSAlex Bennée 		 * single-step behind the scenes but everything
184337b99bfSAlex Bennée 		 * returns to normal once the host is no longer
185337b99bfSAlex Bennée 		 * debugging the system.
186337b99bfSAlex Bennée 		 */
187337b99bfSAlex Bennée 		if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
188337b99bfSAlex Bennée 			*vcpu_cpsr(vcpu) |=  DBG_SPSR_SS;
1898d404c4cSChristoffer Dall 			mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
1908d404c4cSChristoffer Dall 			mdscr |= DBG_MDSCR_SS;
1918d404c4cSChristoffer Dall 			vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
192337b99bfSAlex Bennée 		} else {
1938d404c4cSChristoffer Dall 			mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
1948d404c4cSChristoffer Dall 			mdscr &= ~DBG_MDSCR_SS;
1958d404c4cSChristoffer Dall 			vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
196337b99bfSAlex Bennée 		}
197834bf887SAlex Bennée 
198eef8c85aSAlex Bennée 		trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu));
199eef8c85aSAlex Bennée 
200834bf887SAlex Bennée 		/*
201834bf887SAlex Bennée 		 * HW Breakpoints and watchpoints
202834bf887SAlex Bennée 		 *
203834bf887SAlex Bennée 		 * We simply switch the debug_ptr to point to our new
204834bf887SAlex Bennée 		 * external_debug_state which has been populated by the
205834bf887SAlex Bennée 		 * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
206834bf887SAlex Bennée 		 * mechanism ensures the registers are updated on the
207834bf887SAlex Bennée 		 * world switch.
208834bf887SAlex Bennée 		 */
209834bf887SAlex Bennée 		if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
210834bf887SAlex Bennée 			/* Enable breakpoints/watchpoints */
2118d404c4cSChristoffer Dall 			mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
2128d404c4cSChristoffer Dall 			mdscr |= DBG_MDSCR_MDE;
2138d404c4cSChristoffer Dall 			vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
214834bf887SAlex Bennée 
215834bf887SAlex Bennée 			vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
216fa89d31cSDave Martin 			vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
217eef8c85aSAlex Bennée 
218eef8c85aSAlex Bennée 			trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
219eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_bcr[0],
220eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_bvr[0]);
221eef8c85aSAlex Bennée 
222eef8c85aSAlex Bennée 			trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
223eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_wcr[0],
224eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_wvr[0]);
225337b99bfSAlex Bennée 		}
22656c7f5e7SAlex Bennée 	}
22756c7f5e7SAlex Bennée 
228834bf887SAlex Bennée 	BUG_ON(!vcpu->guest_debug &&
229834bf887SAlex Bennée 		vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
230834bf887SAlex Bennée 
23154ceb1bcSChristoffer Dall 	/* If KDE or MDE are set, perform a full save/restore cycle. */
2328d404c4cSChristoffer Dall 	if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE))
233fa89d31cSDave Martin 		vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
23454ceb1bcSChristoffer Dall 
2354942dc66SAndrew Murray 	/* Write mdcr_el2 changes since vcpu_load on VHE systems */
2364942dc66SAndrew Murray 	if (has_vhe() && orig_mdcr_el2 != vcpu->arch.mdcr_el2)
2374942dc66SAndrew Murray 		write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
2384942dc66SAndrew Murray 
2398d404c4cSChristoffer Dall 	trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1));
240834bf887SAlex Bennée }
241834bf887SAlex Bennée 
24256c7f5e7SAlex Bennée void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
24356c7f5e7SAlex Bennée {
244eef8c85aSAlex Bennée 	trace_kvm_arm_clear_debug(vcpu->guest_debug);
245eef8c85aSAlex Bennée 
246834bf887SAlex Bennée 	if (vcpu->guest_debug) {
247337b99bfSAlex Bennée 		restore_guest_debug_regs(vcpu);
248834bf887SAlex Bennée 
249834bf887SAlex Bennée 		/*
250834bf887SAlex Bennée 		 * If we were using HW debug we need to restore the
251834bf887SAlex Bennée 		 * debug_ptr to the guest debug state.
252834bf887SAlex Bennée 		 */
253eef8c85aSAlex Bennée 		if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
254834bf887SAlex Bennée 			kvm_arm_reset_debug_ptr(vcpu);
255834bf887SAlex Bennée 
256eef8c85aSAlex Bennée 			trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
257eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_bcr[0],
258eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_bvr[0]);
259eef8c85aSAlex Bennée 
260eef8c85aSAlex Bennée 			trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
261eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_wcr[0],
262eef8c85aSAlex Bennée 						&vcpu->arch.debug_ptr->dbg_wvr[0]);
263eef8c85aSAlex Bennée 		}
264834bf887SAlex Bennée 	}
26556c7f5e7SAlex Bennée }
266