GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arm64 / kvm / debug.c
1 /*
2  * Debug and Guest Debug support
3  *
4  * Copyright (C) 2015 - Linaro Ltd
5  * Author: Alex BennĂ©e <alex.bennee@linaro.org>
6  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <linux/kvm_host.h>
21 #include <linux/hw_breakpoint.h>
22
23 #include <asm/debug-monitors.h>
24 #include <asm/kvm_asm.h>
25 #include <asm/kvm_arm.h>
26 #include <asm/kvm_emulate.h>
27
28 #include "trace.h"
29
30 /* These are the bits of MDSCR_EL1 we may manipulate */
31 #define MDSCR_EL1_DEBUG_MASK    (DBG_MDSCR_SS | \
32                                 DBG_MDSCR_KDE | \
33                                 DBG_MDSCR_MDE)
34
35 static DEFINE_PER_CPU(u32, mdcr_el2);
36
37 /**
38  * save/restore_guest_debug_regs
39  *
40  * For some debug operations we need to tweak some guest registers. As
41  * a result we need to save the state of those registers before we
42  * make those modifications.
43  *
44  * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled
45  * after we have restored the preserved value to the main context.
46  */
47 static void save_guest_debug_regs(struct kvm_vcpu *vcpu)
48 {
49         u64 val = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
50
51         vcpu->arch.guest_debug_preserved.mdscr_el1 = val;
52
53         trace_kvm_arm_set_dreg32("Saved MDSCR_EL1",
54                                 vcpu->arch.guest_debug_preserved.mdscr_el1);
55 }
56
57 static void restore_guest_debug_regs(struct kvm_vcpu *vcpu)
58 {
59         u64 val = vcpu->arch.guest_debug_preserved.mdscr_el1;
60
61         vcpu_write_sys_reg(vcpu, val, MDSCR_EL1);
62
63         trace_kvm_arm_set_dreg32("Restored MDSCR_EL1",
64                                 vcpu_read_sys_reg(vcpu, MDSCR_EL1));
65 }
66
67 /**
68  * kvm_arm_init_debug - grab what we need for debug
69  *
70  * Currently the sole task of this function is to retrieve the initial
71  * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has
72  * presumably been set-up by some knowledgeable bootcode.
73  *
74  * It is called once per-cpu during CPU hyp initialisation.
75  */
76
77 void kvm_arm_init_debug(void)
78 {
79         __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2));
80 }
81
82 /**
83  * kvm_arm_setup_mdcr_el2 - configure vcpu mdcr_el2 value
84  *
85  * @vcpu:       the vcpu pointer
86  *
87  * This ensures we will trap access to:
88  *  - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
89  *  - Debug ROM Address (MDCR_EL2_TDRA)
90  *  - OS related registers (MDCR_EL2_TDOSA)
91  *  - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB)
92  *  - Self-hosted Trace Filter controls (MDCR_EL2_TTRF)
93  */
94 static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
95 {
96         /*
97          * This also clears MDCR_EL2_E2PB_MASK to disable guest access
98          * to the profiling buffer.
99          */
100         vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
101         vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
102                                 MDCR_EL2_TPMS |
103                                 MDCR_EL2_TTRF |
104                                 MDCR_EL2_TPMCR |
105                                 MDCR_EL2_TDRA |
106                                 MDCR_EL2_TDOSA);
107
108         /* Is the VM being debugged by userspace? */
109         if (vcpu->guest_debug)
110                 /* Route all software debug exceptions to EL2 */
111                 vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
112
113         /*
114          * Trap debug register access when one of the following is true:
115          *  - Userspace is using the hardware to debug the guest
116          *  (KVM_GUESTDBG_USE_HW is set).
117          *  - The guest is not using debug (KVM_ARM64_DEBUG_DIRTY is clear).
118          */
119         if ((vcpu->guest_debug & KVM_GUESTDBG_USE_HW) ||
120             !(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
121                 vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
122
123         trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2);
124 }
125
126 /**
127  * kvm_arm_vcpu_init_debug - setup vcpu debug traps
128  *
129  * @vcpu:       the vcpu pointer
130  *
131  * Set vcpu initial mdcr_el2 value.
132  */
133 void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu)
134 {
135         preempt_disable();
136         kvm_arm_setup_mdcr_el2(vcpu);
137         preempt_enable();
138 }
139
140 /**
141  * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state
142  */
143
144 void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu)
145 {
146         vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state;
147 }
148
149 /**
150  * kvm_arm_setup_debug - set up debug related stuff
151  *
152  * @vcpu:       the vcpu pointer
153  *
154  * This is called before each entry into the hypervisor to setup any
155  * debug related registers.
156  *
157  * Additionally, KVM only traps guest accesses to the debug registers if
158  * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
159  * flag on vcpu->arch.flags).  Since the guest must not interfere
160  * with the hardware state when debugging the guest, we must ensure that
161  * trapping is enabled whenever we are debugging the guest using the
162  * debug registers.
163  */
164
165 void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
166 {
167         unsigned long mdscr, orig_mdcr_el2 = vcpu->arch.mdcr_el2;
168
169         trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug);
170
171         kvm_arm_setup_mdcr_el2(vcpu);
172
173         /* Is Guest debugging in effect? */
174         if (vcpu->guest_debug) {
175                 /* Save guest debug state */
176                 save_guest_debug_regs(vcpu);
177
178                 /*
179                  * Single Step (ARM ARM D2.12.3 The software step state
180                  * machine)
181                  *
182                  * If we are doing Single Step we need to manipulate
183                  * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
184                  * step has occurred the hypervisor will trap the
185                  * debug exception and we return to userspace.
186                  *
187                  * If the guest attempts to single step its userspace
188                  * we would have to deal with a trapped exception
189                  * while in the guest kernel. Because this would be
190                  * hard to unwind we suppress the guest's ability to
191                  * do so by masking MDSCR_EL.SS.
192                  *
193                  * This confuses guest debuggers which use
194                  * single-step behind the scenes but everything
195                  * returns to normal once the host is no longer
196                  * debugging the system.
197                  */
198                 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
199                         *vcpu_cpsr(vcpu) |=  DBG_SPSR_SS;
200                         mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
201                         mdscr |= DBG_MDSCR_SS;
202                         vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
203                 } else {
204                         mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
205                         mdscr &= ~DBG_MDSCR_SS;
206                         vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
207                 }
208
209                 trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu));
210
211                 /*
212                  * HW Breakpoints and watchpoints
213                  *
214                  * We simply switch the debug_ptr to point to our new
215                  * external_debug_state which has been populated by the
216                  * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
217                  * mechanism ensures the registers are updated on the
218                  * world switch.
219                  */
220                 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
221                         /* Enable breakpoints/watchpoints */
222                         mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
223                         mdscr |= DBG_MDSCR_MDE;
224                         vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
225
226                         vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
227                         vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
228
229                         trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
230                                                 &vcpu->arch.debug_ptr->dbg_bcr[0],
231                                                 &vcpu->arch.debug_ptr->dbg_bvr[0]);
232
233                         trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
234                                                 &vcpu->arch.debug_ptr->dbg_wcr[0],
235                                                 &vcpu->arch.debug_ptr->dbg_wvr[0]);
236                 }
237         }
238
239         BUG_ON(!vcpu->guest_debug &&
240                 vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
241
242         /* If KDE or MDE are set, perform a full save/restore cycle. */
243         if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE))
244                 vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
245
246         /* Write mdcr_el2 changes since vcpu_load on VHE systems */
247         if (has_vhe() && orig_mdcr_el2 != vcpu->arch.mdcr_el2)
248                 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
249
250         trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1));
251 }
252
253 void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
254 {
255         trace_kvm_arm_clear_debug(vcpu->guest_debug);
256
257         if (vcpu->guest_debug) {
258                 restore_guest_debug_regs(vcpu);
259
260                 /*
261                  * If we were using HW debug we need to restore the
262                  * debug_ptr to the guest debug state.
263                  */
264                 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
265                         kvm_arm_reset_debug_ptr(vcpu);
266
267                         trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
268                                                 &vcpu->arch.debug_ptr->dbg_bcr[0],
269                                                 &vcpu->arch.debug_ptr->dbg_bvr[0]);
270
271                         trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
272                                                 &vcpu->arch.debug_ptr->dbg_wcr[0],
273                                                 &vcpu->arch.debug_ptr->dbg_wvr[0]);
274                 }
275         }
276 }
277
278
279 /*
280  * After successfully emulating an instruction, we might want to
281  * return to user space with a KVM_EXIT_DEBUG. We can only do this
282  * once the emulation is complete, though, so for userspace emulations
283  * we have to wait until we have re-entered KVM before calling this
284  * helper.
285  *
286  * Return true (and set exit_reason) to return to userspace or false
287  * if no further action is required.
288  */
289 bool kvm_arm_handle_step_debug(struct kvm_vcpu *vcpu, struct kvm_run *run)
290 {
291         if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
292                 run->exit_reason = KVM_EXIT_DEBUG;
293                 run->debug.arch.hsr = ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT;
294                 return true;
295         }
296         return false;
297 }