1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Debug and Guest Debug support
4 *
5 * Copyright (C) 2015 - Linaro Ltd
6 * Authors: Alex Bennée <alex.bennee@linaro.org>
7 * Oliver Upton <oliver.upton@linux.dev>
8 */
9
10 #include <linux/kvm_host.h>
11 #include <linux/hw_breakpoint.h>
12
13 #include <asm/debug-monitors.h>
14 #include <asm/kvm_asm.h>
15 #include <asm/kvm_arm.h>
16 #include <asm/kvm_emulate.h>
17
18 /**
19 * kvm_arm_setup_mdcr_el2 - configure vcpu mdcr_el2 value
20 *
21 * @vcpu: the vcpu pointer
22 *
23 * This ensures we will trap access to:
24 * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
25 * - Debug ROM Address (MDCR_EL2_TDRA)
26 * - OS related registers (MDCR_EL2_TDOSA)
27 * - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB)
28 * - Self-hosted Trace Filter controls (MDCR_EL2_TTRF)
29 * - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB)
30 */
kvm_arm_setup_mdcr_el2(struct kvm_vcpu * vcpu)31 static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
32 {
33 preempt_disable();
34
35 /*
36 * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK
37 * to disable guest access to the profiling and trace buffers
38 */
39 vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN,
40 *host_data_ptr(nr_event_counters));
41 vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
42 MDCR_EL2_TPMS |
43 MDCR_EL2_TTRF |
44 MDCR_EL2_TPMCR |
45 MDCR_EL2_TDRA |
46 MDCR_EL2_TDOSA);
47
48 /* Is the VM being debugged by userspace? */
49 if (vcpu->guest_debug)
50 /* Route all software debug exceptions to EL2 */
51 vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
52
53 /*
54 * Trap debug registers if the guest doesn't have ownership of them.
55 */
56 if (!kvm_guest_owns_debug_regs(vcpu))
57 vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
58
59 if (vcpu_has_nv(vcpu))
60 kvm_nested_setup_mdcr_el2(vcpu);
61
62 /* Write MDCR_EL2 directly if we're already at EL2 */
63 if (has_vhe())
64 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
65
66 preempt_enable();
67 }
68
kvm_init_host_debug_data(void)69 void kvm_init_host_debug_data(void)
70 {
71 u64 dfr0 = read_sysreg(id_aa64dfr0_el1);
72
73 if (cpuid_feature_extract_signed_field(dfr0, ID_AA64DFR0_EL1_PMUVer_SHIFT) > 0)
74 *host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
75 read_sysreg(pmcr_el0));
76
77 *host_data_ptr(debug_brps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, BRPs, dfr0);
78 *host_data_ptr(debug_wrps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, WRPs, dfr0);
79
80 if (has_vhe())
81 return;
82
83 if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_PMSVer_SHIFT) &&
84 !(read_sysreg_s(SYS_PMBIDR_EL1) & PMBIDR_EL1_P))
85 host_data_set_flag(HAS_SPE);
86
87 /* Check if we have BRBE implemented and available at the host */
88 if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_BRBE_SHIFT))
89 host_data_set_flag(HAS_BRBE);
90
91 if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT)) {
92 /* Force disable trace in protected mode in case of no TRBE */
93 if (is_protected_kvm_enabled())
94 host_data_set_flag(EL1_TRACING_CONFIGURED);
95
96 if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) &&
97 !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P))
98 host_data_set_flag(HAS_TRBE);
99 }
100 }
101
kvm_debug_init_vhe(void)102 void kvm_debug_init_vhe(void)
103 {
104 /* Clear PMSCR_EL1.E{0,1}SPE which reset to UNKNOWN values. */
105 if (SYS_FIELD_GET(ID_AA64DFR0_EL1, PMSVer, read_sysreg(id_aa64dfr0_el1)))
106 write_sysreg_el1(0, SYS_PMSCR);
107 }
108
109 /*
110 * Configures the 'external' MDSCR_EL1 value for the guest, i.e. when the host
111 * has taken over MDSCR_EL1.
112 *
113 * - Userspace is single-stepping the guest, and MDSCR_EL1.SS is forced to 1.
114 *
115 * - Userspace is using the breakpoint/watchpoint registers to debug the
116 * guest, and MDSCR_EL1.MDE is forced to 1.
117 *
118 * - The guest has enabled the OS Lock, and KVM is forcing MDSCR_EL1.MDE to 0,
119 * masking all debug exceptions affected by the OS Lock.
120 */
setup_external_mdscr(struct kvm_vcpu * vcpu)121 static void setup_external_mdscr(struct kvm_vcpu *vcpu)
122 {
123 /*
124 * Use the guest's MDSCR_EL1 as a starting point, since there are
125 * several other features controlled by MDSCR_EL1 that are not relevant
126 * to the host.
127 *
128 * Clear the bits that KVM may use which also satisfies emulation of
129 * the OS Lock as MDSCR_EL1.MDE is cleared.
130 */
131 u64 mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1) & ~(MDSCR_EL1_SS |
132 MDSCR_EL1_MDE |
133 MDSCR_EL1_KDE);
134
135 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
136 mdscr |= MDSCR_EL1_SS;
137
138 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW)
139 mdscr |= MDSCR_EL1_MDE | MDSCR_EL1_KDE;
140
141 vcpu->arch.external_mdscr_el1 = mdscr;
142 }
143
kvm_vcpu_load_debug(struct kvm_vcpu * vcpu)144 void kvm_vcpu_load_debug(struct kvm_vcpu *vcpu)
145 {
146 u64 mdscr;
147
148 /* Must be called before kvm_vcpu_load_vhe() */
149 KVM_BUG_ON(vcpu_get_flag(vcpu, SYSREGS_ON_CPU), vcpu->kvm);
150
151 if (has_vhe())
152 *host_data_ptr(host_debug_state.mdcr_el2) = read_sysreg(mdcr_el2);
153
154 /*
155 * Determine which of the possible debug states we're in:
156 *
157 * - VCPU_DEBUG_HOST_OWNED: KVM has taken ownership of the guest's
158 * breakpoint/watchpoint registers, or needs to use MDSCR_EL1 to do
159 * software step or emulate the effects of the OS Lock being enabled.
160 *
161 * - VCPU_DEBUG_GUEST_OWNED: The guest has debug exceptions enabled, and
162 * the breakpoint/watchpoint registers need to be loaded eagerly.
163 *
164 * - VCPU_DEBUG_FREE: Neither of the above apply, no breakpoint/watchpoint
165 * context needs to be loaded on the CPU.
166 */
167 if (vcpu->guest_debug || kvm_vcpu_os_lock_enabled(vcpu)) {
168 vcpu->arch.debug_owner = VCPU_DEBUG_HOST_OWNED;
169 setup_external_mdscr(vcpu);
170
171 /*
172 * Steal the guest's single-step state machine if userspace wants
173 * single-step the guest.
174 */
175 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
176 if (*vcpu_cpsr(vcpu) & DBG_SPSR_SS)
177 vcpu_clear_flag(vcpu, GUEST_SS_ACTIVE_PENDING);
178 else
179 vcpu_set_flag(vcpu, GUEST_SS_ACTIVE_PENDING);
180
181 if (!vcpu_get_flag(vcpu, HOST_SS_ACTIVE_PENDING))
182 *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
183 else
184 *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
185 }
186 } else {
187 mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
188
189 if (mdscr & (MDSCR_EL1_KDE | MDSCR_EL1_MDE))
190 vcpu->arch.debug_owner = VCPU_DEBUG_GUEST_OWNED;
191 else
192 vcpu->arch.debug_owner = VCPU_DEBUG_FREE;
193 }
194
195 kvm_arm_setup_mdcr_el2(vcpu);
196 }
197
kvm_vcpu_put_debug(struct kvm_vcpu * vcpu)198 void kvm_vcpu_put_debug(struct kvm_vcpu *vcpu)
199 {
200 if (has_vhe())
201 write_sysreg(*host_data_ptr(host_debug_state.mdcr_el2), mdcr_el2);
202
203 if (likely(!(vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
204 return;
205
206 /*
207 * Save the host's software step state and restore the guest's before
208 * potentially returning to userspace.
209 */
210 if (!(*vcpu_cpsr(vcpu) & DBG_SPSR_SS))
211 vcpu_set_flag(vcpu, HOST_SS_ACTIVE_PENDING);
212 else
213 vcpu_clear_flag(vcpu, HOST_SS_ACTIVE_PENDING);
214
215 if (vcpu_get_flag(vcpu, GUEST_SS_ACTIVE_PENDING))
216 *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
217 else
218 *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
219 }
220
221 /*
222 * Updates ownership of the debug registers after a trapped guest access to a
223 * breakpoint/watchpoint register. Host ownership of the debug registers is of
224 * strictly higher priority, and it is the responsibility of the VMM to emulate
225 * guest debug exceptions in this configuration.
226 */
kvm_debug_set_guest_ownership(struct kvm_vcpu * vcpu)227 void kvm_debug_set_guest_ownership(struct kvm_vcpu *vcpu)
228 {
229 if (kvm_host_owns_debug_regs(vcpu))
230 return;
231
232 vcpu->arch.debug_owner = VCPU_DEBUG_GUEST_OWNED;
233 kvm_arm_setup_mdcr_el2(vcpu);
234 }
235
kvm_debug_handle_oslar(struct kvm_vcpu * vcpu,u64 val)236 void kvm_debug_handle_oslar(struct kvm_vcpu *vcpu, u64 val)
237 {
238 if (val & OSLAR_EL1_OSLK)
239 __vcpu_rmw_sys_reg(vcpu, OSLSR_EL1, |=, OSLSR_EL1_OSLK);
240 else
241 __vcpu_rmw_sys_reg(vcpu, OSLSR_EL1, &=, ~OSLSR_EL1_OSLK);
242
243 preempt_disable();
244 kvm_arch_vcpu_put(vcpu);
245 kvm_arch_vcpu_load(vcpu, smp_processor_id());
246 preempt_enable();
247 }
248
skip_trbe_access(bool skip_condition)249 static bool skip_trbe_access(bool skip_condition)
250 {
251 return (WARN_ON_ONCE(preemptible()) || skip_condition ||
252 is_protected_kvm_enabled() || !is_kvm_arm_initialised());
253 }
254
kvm_enable_trbe(void)255 void kvm_enable_trbe(void)
256 {
257 if (!skip_trbe_access(has_vhe()))
258 host_data_set_flag(TRBE_ENABLED);
259 }
260 EXPORT_SYMBOL_GPL(kvm_enable_trbe);
261
kvm_disable_trbe(void)262 void kvm_disable_trbe(void)
263 {
264 if (!skip_trbe_access(has_vhe()))
265 host_data_clear_flag(TRBE_ENABLED);
266 }
267 EXPORT_SYMBOL_GPL(kvm_disable_trbe);
268
kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest)269 void kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest)
270 {
271 if (skip_trbe_access(false))
272 return;
273
274 if (has_vhe()) {
275 write_sysreg_s(trfcr_while_in_guest, SYS_TRFCR_EL12);
276 return;
277 }
278
279 *host_data_ptr(trfcr_while_in_guest) = trfcr_while_in_guest;
280 if (read_sysreg_s(SYS_TRFCR_EL1) != trfcr_while_in_guest)
281 host_data_set_flag(EL1_TRACING_CONFIGURED);
282 else
283 host_data_clear_flag(EL1_TRACING_CONFIGURED);
284 }
285 EXPORT_SYMBOL_GPL(kvm_tracing_set_el1_configuration);
286