blob: 4a99e54d7f3db735a939d544be49067b4da52a45 [file] [log] [blame]
Alex Bennée56c7f5e2015-07-07 17:29:56 +01001/*
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
Alex Bennée337b99b2015-07-07 17:29:58 +010022#include <asm/debug-monitors.h>
23#include <asm/kvm_asm.h>
Alex Bennée56c7f5e2015-07-07 17:29:56 +010024#include <asm/kvm_arm.h>
Alex Bennée337b99b2015-07-07 17:29:58 +010025#include <asm/kvm_emulate.h>
26
27/* These are the bits of MDSCR_EL1 we may manipulate */
28#define MDSCR_EL1_DEBUG_MASK (DBG_MDSCR_SS | \
29 DBG_MDSCR_KDE | \
30 DBG_MDSCR_MDE)
Alex Bennée56c7f5e2015-07-07 17:29:56 +010031
32static DEFINE_PER_CPU(u32, mdcr_el2);
33
34/**
Alex Bennée337b99b2015-07-07 17:29:58 +010035 * save/restore_guest_debug_regs
36 *
37 * For some debug operations we need to tweak some guest registers. As
38 * a result we need to save the state of those registers before we
39 * make those modifications.
40 *
41 * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled
42 * after we have restored the preserved value to the main context.
43 */
44static void save_guest_debug_regs(struct kvm_vcpu *vcpu)
45{
46 vcpu->arch.guest_debug_preserved.mdscr_el1 = vcpu_sys_reg(vcpu, MDSCR_EL1);
47}
48
49static void restore_guest_debug_regs(struct kvm_vcpu *vcpu)
50{
51 vcpu_sys_reg(vcpu, MDSCR_EL1) = vcpu->arch.guest_debug_preserved.mdscr_el1;
52}
53
54/**
Alex Bennée56c7f5e2015-07-07 17:29:56 +010055 * kvm_arm_init_debug - grab what we need for debug
56 *
57 * Currently the sole task of this function is to retrieve the initial
58 * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has
59 * presumably been set-up by some knowledgeable bootcode.
60 *
61 * It is called once per-cpu during CPU hyp initialisation.
62 */
63
64void kvm_arm_init_debug(void)
65{
66 __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2));
67}
68
Alex Bennée56c7f5e2015-07-07 17:29:56 +010069/**
Alex Bennée84e690b2015-07-07 17:30:00 +010070 * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state
71 */
72
73void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu)
74{
75 vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state;
76}
77
78/**
Alex Bennée56c7f5e2015-07-07 17:29:56 +010079 * kvm_arm_setup_debug - set up debug related stuff
80 *
81 * @vcpu: the vcpu pointer
82 *
83 * This is called before each entry into the hypervisor to setup any
84 * debug related registers. Currently this just ensures we will trap
85 * access to:
86 * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
87 * - Debug ROM Address (MDCR_EL2_TDRA)
88 * - OS related registers (MDCR_EL2_TDOSA)
89 *
90 * Additionally, KVM only traps guest accesses to the debug registers if
91 * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
92 * flag on vcpu->arch.debug_flags). Since the guest must not interfere
93 * with the hardware state when debugging the guest, we must ensure that
94 * trapping is enabled whenever we are debugging the guest using the
95 * debug registers.
96 */
97
98void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
99{
100 bool trap_debug = !(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY);
101
102 vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
103 vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
104 MDCR_EL2_TPMCR |
105 MDCR_EL2_TDRA |
106 MDCR_EL2_TDOSA);
107
Alex Bennée337b99b2015-07-07 17:29:58 +0100108 /* Is Guest debugging in effect? */
109 if (vcpu->guest_debug) {
110 /* Route all software debug exceptions to EL2 */
Alex Bennée4bd611c2015-07-07 17:29:57 +0100111 vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
Alex Bennée337b99b2015-07-07 17:29:58 +0100112
113 /* Save guest debug state */
114 save_guest_debug_regs(vcpu);
115
116 /*
117 * Single Step (ARM ARM D2.12.3 The software step state
118 * machine)
119 *
120 * If we are doing Single Step we need to manipulate
121 * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
122 * step has occurred the hypervisor will trap the
123 * debug exception and we return to userspace.
124 *
125 * If the guest attempts to single step its userspace
126 * we would have to deal with a trapped exception
127 * while in the guest kernel. Because this would be
128 * hard to unwind we suppress the guest's ability to
129 * do so by masking MDSCR_EL.SS.
130 *
131 * This confuses guest debuggers which use
132 * single-step behind the scenes but everything
133 * returns to normal once the host is no longer
134 * debugging the system.
135 */
136 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
137 *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
138 vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_SS;
139 } else {
140 vcpu_sys_reg(vcpu, MDSCR_EL1) &= ~DBG_MDSCR_SS;
141 }
Alex Bennée834bf882015-07-07 17:30:02 +0100142
143 /*
144 * HW Breakpoints and watchpoints
145 *
146 * We simply switch the debug_ptr to point to our new
147 * external_debug_state which has been populated by the
148 * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
149 * mechanism ensures the registers are updated on the
150 * world switch.
151 */
152 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
153 /* Enable breakpoints/watchpoints */
154 vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_MDE;
155
156 vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
157 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
158 trap_debug = true;
159 }
Alex Bennée337b99b2015-07-07 17:29:58 +0100160 }
Alex Bennée834bf882015-07-07 17:30:02 +0100161
162 BUG_ON(!vcpu->guest_debug &&
163 vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
164
165 /* Trap debug register access */
166 if (trap_debug)
167 vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100168}
169
170void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
171{
Alex Bennée834bf882015-07-07 17:30:02 +0100172 if (vcpu->guest_debug) {
Alex Bennée337b99b2015-07-07 17:29:58 +0100173 restore_guest_debug_regs(vcpu);
Alex Bennée834bf882015-07-07 17:30:02 +0100174
175 /*
176 * If we were using HW debug we need to restore the
177 * debug_ptr to the guest debug state.
178 */
179 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW)
180 kvm_arm_reset_debug_ptr(vcpu);
181
182 }
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100183}