blob: b3a66c50d12b98970b23533e9237633ae6acea0c [file] [log] [blame]
Marc Zyngierbe901e92015-10-21 09:57:10 +01001/*
2 * Copyright (C) 2015 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Marc Zyngier5f05a722015-10-28 15:06:47 +000018#include <linux/types.h>
Marc Zyngier68908bf2015-01-29 15:47:55 +000019#include <asm/kvm_asm.h>
Marc Zyngierfb5ee362016-09-06 09:28:45 +010020#include <asm/kvm_emulate.h>
Marc Zyngier13720a52016-01-28 13:44:07 +000021#include <asm/kvm_hyp.h>
Marc Zyngierbe901e92015-10-21 09:57:10 +010022
Marc Zyngier32876222015-10-28 14:15:45 +000023static bool __hyp_text __fpsimd_enabled_nvhe(void)
24{
25 return !(read_sysreg(cptr_el2) & CPTR_EL2_TFP);
26}
27
28static bool __hyp_text __fpsimd_enabled_vhe(void)
29{
30 return !!(read_sysreg(cpacr_el1) & CPACR_EL1_FPEN);
31}
32
33static hyp_alternate_select(__fpsimd_is_enabled,
34 __fpsimd_enabled_nvhe, __fpsimd_enabled_vhe,
35 ARM64_HAS_VIRT_HOST_EXTN);
36
37bool __hyp_text __fpsimd_enabled(void)
38{
39 return __fpsimd_is_enabled()();
40}
41
Marc Zyngier68908bf2015-01-29 15:47:55 +000042static void __hyp_text __activate_traps_vhe(void)
43{
44 u64 val;
45
46 val = read_sysreg(cpacr_el1);
47 val |= CPACR_EL1_TTA;
48 val &= ~CPACR_EL1_FPEN;
49 write_sysreg(val, cpacr_el1);
50
51 write_sysreg(__kvm_hyp_vector, vbar_el1);
52}
53
54static void __hyp_text __activate_traps_nvhe(void)
55{
56 u64 val;
57
58 val = CPTR_EL2_DEFAULT;
59 val |= CPTR_EL2_TTA | CPTR_EL2_TFP;
60 write_sysreg(val, cptr_el2);
61}
62
63static hyp_alternate_select(__activate_traps_arch,
64 __activate_traps_nvhe, __activate_traps_vhe,
65 ARM64_HAS_VIRT_HOST_EXTN);
66
Marc Zyngierbe901e92015-10-21 09:57:10 +010067static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
68{
69 u64 val;
70
71 /*
72 * We are about to set CPTR_EL2.TFP to trap all floating point
73 * register accesses to EL2, however, the ARM ARM clearly states that
74 * traps are only taken to EL2 if the operation would not otherwise
75 * trap to EL1. Therefore, always make sure that for 32-bit guests,
76 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
77 */
78 val = vcpu->arch.hcr_el2;
79 if (!(val & HCR_RW)) {
80 write_sysreg(1 << 30, fpexc32_el2);
81 isb();
82 }
83 write_sysreg(val, hcr_el2);
84 /* Trap on AArch32 cp15 c15 accesses (EL1 or EL0) */
85 write_sysreg(1 << 15, hstr_el2);
Shannon Zhaod692b8a2015-09-08 15:15:56 +080086 /* Make sure we trap PMU access from EL0 to EL2 */
87 write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
Marc Zyngierbe901e92015-10-21 09:57:10 +010088 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
Marc Zyngier68908bf2015-01-29 15:47:55 +000089 __activate_traps_arch()();
Marc Zyngierbe901e92015-10-21 09:57:10 +010090}
91
Marc Zyngier68908bf2015-01-29 15:47:55 +000092static void __hyp_text __deactivate_traps_vhe(void)
93{
94 extern char vectors[]; /* kernel exception vectors */
95
96 write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
97 write_sysreg(CPACR_EL1_FPEN, cpacr_el1);
98 write_sysreg(vectors, vbar_el1);
99}
100
101static void __hyp_text __deactivate_traps_nvhe(void)
102{
103 write_sysreg(HCR_RW, hcr_el2);
104 write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
105}
106
107static hyp_alternate_select(__deactivate_traps_arch,
108 __deactivate_traps_nvhe, __deactivate_traps_vhe,
109 ARM64_HAS_VIRT_HOST_EXTN);
110
Marc Zyngierbe901e92015-10-21 09:57:10 +0100111static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
112{
Marc Zyngier68908bf2015-01-29 15:47:55 +0000113 __deactivate_traps_arch()();
Marc Zyngierbe901e92015-10-21 09:57:10 +0100114 write_sysreg(0, hstr_el2);
115 write_sysreg(read_sysreg(mdcr_el2) & MDCR_EL2_HPMN_MASK, mdcr_el2);
Shannon Zhaod692b8a2015-09-08 15:15:56 +0800116 write_sysreg(0, pmuserenr_el0);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100117}
118
119static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu)
120{
121 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
122 write_sysreg(kvm->arch.vttbr, vttbr_el2);
123}
124
125static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
126{
127 write_sysreg(0, vttbr_el2);
128}
129
130static hyp_alternate_select(__vgic_call_save_state,
131 __vgic_v2_save_state, __vgic_v3_save_state,
132 ARM64_HAS_SYSREG_GIC_CPUIF);
133
134static hyp_alternate_select(__vgic_call_restore_state,
135 __vgic_v2_restore_state, __vgic_v3_restore_state,
136 ARM64_HAS_SYSREG_GIC_CPUIF);
137
138static void __hyp_text __vgic_save_state(struct kvm_vcpu *vcpu)
139{
140 __vgic_call_save_state()(vcpu);
141 write_sysreg(read_sysreg(hcr_el2) & ~HCR_INT_OVERRIDE, hcr_el2);
142}
143
144static void __hyp_text __vgic_restore_state(struct kvm_vcpu *vcpu)
145{
146 u64 val;
147
148 val = read_sysreg(hcr_el2);
149 val |= HCR_INT_OVERRIDE;
150 val |= vcpu->arch.irq_lines;
151 write_sysreg(val, hcr_el2);
152
153 __vgic_call_restore_state()(vcpu);
154}
155
Marc Zyngier5f05a722015-10-28 15:06:47 +0000156static bool __hyp_text __true_value(void)
157{
158 return true;
159}
160
161static bool __hyp_text __false_value(void)
162{
163 return false;
164}
165
166static hyp_alternate_select(__check_arm_834220,
167 __false_value, __true_value,
168 ARM64_WORKAROUND_834220);
169
170static bool __hyp_text __translate_far_to_hpfar(u64 far, u64 *hpfar)
171{
172 u64 par, tmp;
173
174 /*
175 * Resolve the IPA the hard way using the guest VA.
176 *
177 * Stage-1 translation already validated the memory access
178 * rights. As such, we can use the EL1 translation regime, and
179 * don't have to distinguish between EL0 and EL1 access.
180 *
181 * We do need to save/restore PAR_EL1 though, as we haven't
182 * saved the guest context yet, and we may return early...
183 */
184 par = read_sysreg(par_el1);
185 asm volatile("at s1e1r, %0" : : "r" (far));
186 isb();
187
188 tmp = read_sysreg(par_el1);
189 write_sysreg(par, par_el1);
190
191 if (unlikely(tmp & 1))
192 return false; /* Translation failed, back to guest */
193
194 /* Convert PAR to HPFAR format */
195 *hpfar = ((tmp >> 12) & ((1UL << 36) - 1)) << 4;
196 return true;
197}
198
199static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu)
200{
201 u64 esr = read_sysreg_el2(esr);
Mark Rutland561454e2016-05-31 12:33:02 +0100202 u8 ec = ESR_ELx_EC(esr);
Marc Zyngier5f05a722015-10-28 15:06:47 +0000203 u64 hpfar, far;
204
205 vcpu->arch.fault.esr_el2 = esr;
206
207 if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW)
208 return true;
209
210 far = read_sysreg_el2(far);
211
212 /*
213 * The HPFAR can be invalid if the stage 2 fault did not
214 * happen during a stage 1 page table walk (the ESR_EL2.S1PTW
215 * bit is clear) and one of the two following cases are true:
216 * 1. The fault was due to a permission fault
217 * 2. The processor carries errata 834220
218 *
219 * Therefore, for all non S1PTW faults where we either have a
220 * permission fault or the errata workaround is enabled, we
221 * resolve the IPA using the AT instruction.
222 */
223 if (!(esr & ESR_ELx_S1PTW) &&
224 (__check_arm_834220()() || (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) {
225 if (!__translate_far_to_hpfar(far, &hpfar))
226 return false;
227 } else {
228 hpfar = read_sysreg(hpfar_el2);
229 }
230
231 vcpu->arch.fault.far_el2 = far;
232 vcpu->arch.fault.hpfar_el2 = hpfar;
233 return true;
234}
235
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100236static void __hyp_text __skip_instr(struct kvm_vcpu *vcpu)
237{
238 *vcpu_pc(vcpu) = read_sysreg_el2(elr);
239
240 if (vcpu_mode_is_32bit(vcpu)) {
241 vcpu->arch.ctxt.gp_regs.regs.pstate = read_sysreg_el2(spsr);
242 kvm_skip_instr32(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
243 write_sysreg_el2(vcpu->arch.ctxt.gp_regs.regs.pstate, spsr);
244 } else {
245 *vcpu_pc(vcpu) += 4;
246 }
247
248 write_sysreg_el2(*vcpu_pc(vcpu), elr);
249}
250
Christoffer Dallcf0ba182016-09-01 13:16:03 +0200251int __hyp_text __kvm_vcpu_run(struct kvm_vcpu *vcpu)
Marc Zyngierbe901e92015-10-21 09:57:10 +0100252{
253 struct kvm_cpu_context *host_ctxt;
254 struct kvm_cpu_context *guest_ctxt;
Marc Zyngierc13d1682015-10-26 08:34:09 +0000255 bool fp_enabled;
Marc Zyngierbe901e92015-10-21 09:57:10 +0100256 u64 exit_code;
257
258 vcpu = kern_hyp_va(vcpu);
259 write_sysreg(vcpu, tpidr_el2);
260
261 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
262 guest_ctxt = &vcpu->arch.ctxt;
263
Marc Zyngieredef5282015-10-28 12:17:35 +0000264 __sysreg_save_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100265 __debug_cond_save_host_state(vcpu);
266
267 __activate_traps(vcpu);
268 __activate_vm(vcpu);
269
270 __vgic_restore_state(vcpu);
271 __timer_restore_state(vcpu);
272
273 /*
274 * We must restore the 32-bit state before the sysregs, thanks
Marc Zyngier674e7012016-08-16 15:03:01 +0100275 * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
Marc Zyngierbe901e92015-10-21 09:57:10 +0100276 */
277 __sysreg32_restore_state(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000278 __sysreg_restore_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100279 __debug_restore_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
280
281 /* Jump in the fire! */
Marc Zyngier5f05a722015-10-28 15:06:47 +0000282again:
Marc Zyngierbe901e92015-10-21 09:57:10 +0100283 exit_code = __guest_enter(vcpu, host_ctxt);
284 /* And we're baaack! */
285
Marc Zyngier5f05a722015-10-28 15:06:47 +0000286 if (exit_code == ARM_EXCEPTION_TRAP && !__populate_fault_info(vcpu))
287 goto again;
288
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100289 if (static_branch_unlikely(&vgic_v2_cpuif_trap) &&
290 exit_code == ARM_EXCEPTION_TRAP) {
291 bool valid;
292
293 valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
294 kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
295 kvm_vcpu_dabt_isvalid(vcpu) &&
296 !kvm_vcpu_dabt_isextabt(vcpu) &&
297 !kvm_vcpu_dabt_iss1tw(vcpu);
298
299 if (valid && __vgic_v2_perform_cpuif_access(vcpu)) {
300 __skip_instr(vcpu);
301 goto again;
302 }
303 }
304
Marc Zyngierc13d1682015-10-26 08:34:09 +0000305 fp_enabled = __fpsimd_enabled();
306
Marc Zyngieredef5282015-10-28 12:17:35 +0000307 __sysreg_save_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100308 __sysreg32_save_state(vcpu);
309 __timer_save_state(vcpu);
310 __vgic_save_state(vcpu);
311
312 __deactivate_traps(vcpu);
313 __deactivate_vm(vcpu);
314
Marc Zyngieredef5282015-10-28 12:17:35 +0000315 __sysreg_restore_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100316
Marc Zyngierc13d1682015-10-26 08:34:09 +0000317 if (fp_enabled) {
318 __fpsimd_save_state(&guest_ctxt->gp_regs.fp_regs);
319 __fpsimd_restore_state(&host_ctxt->gp_regs.fp_regs);
320 }
321
Marc Zyngierbe901e92015-10-21 09:57:10 +0100322 __debug_save_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
323 __debug_cond_restore_host_state(vcpu);
324
325 return exit_code;
326}
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000327
328static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
329
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000330static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par)
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000331{
Marc Zyngiercf7df132016-06-30 18:40:35 +0100332 unsigned long str_va;
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000333
Marc Zyngiercf7df132016-06-30 18:40:35 +0100334 /*
335 * Force the panic string to be loaded from the literal pool,
336 * making sure it is a kernel address and not a PC-relative
337 * reference.
338 */
339 asm volatile("ldr %0, =__hyp_panic_string" : "=r" (str_va));
340
341 __hyp_do_panic(str_va,
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000342 spsr, elr,
343 read_sysreg(esr_el2), read_sysreg_el2(far),
344 read_sysreg(hpfar_el2), par,
345 (void *)read_sysreg(tpidr_el2));
346}
347
348static void __hyp_text __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par)
349{
350 panic(__hyp_panic_string,
351 spsr, elr,
352 read_sysreg_el2(esr), read_sysreg_el2(far),
353 read_sysreg(hpfar_el2), par,
354 (void *)read_sysreg(tpidr_el2));
355}
356
357static hyp_alternate_select(__hyp_call_panic,
358 __hyp_call_panic_nvhe, __hyp_call_panic_vhe,
359 ARM64_HAS_VIRT_HOST_EXTN);
360
361void __hyp_text __noreturn __hyp_panic(void)
362{
363 u64 spsr = read_sysreg_el2(spsr);
364 u64 elr = read_sysreg_el2(elr);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000365 u64 par = read_sysreg(par_el1);
366
367 if (read_sysreg(vttbr_el2)) {
368 struct kvm_vcpu *vcpu;
369 struct kvm_cpu_context *host_ctxt;
370
371 vcpu = (struct kvm_vcpu *)read_sysreg(tpidr_el2);
372 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
373 __deactivate_traps(vcpu);
374 __deactivate_vm(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000375 __sysreg_restore_host_state(host_ctxt);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000376 }
377
378 /* Call panic for real */
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000379 __hyp_call_panic()(spsr, elr, par);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000380
381 unreachable();
382}