blob: 14607fac7ca38364f88ea052545a475366f755d8 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6
7#include <linux/arm-smccc.h>
David Brazdil0f672f62019-12-10 10:32:29 +00008#include <linux/kvm_host.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009#include <linux/types.h>
10#include <linux/jump_label.h>
11#include <uapi/linux/psci.h>
12
13#include <kvm/arm_psci.h>
14
David Brazdil0f672f62019-12-10 10:32:29 +000015#include <asm/arch_gicv3.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <asm/cpufeature.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020017#include <asm/extable.h>
David Brazdil0f672f62019-12-10 10:32:29 +000018#include <asm/kprobes.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include <asm/kvm_asm.h>
20#include <asm/kvm_emulate.h>
21#include <asm/kvm_host.h>
22#include <asm/kvm_hyp.h>
23#include <asm/kvm_mmu.h>
24#include <asm/fpsimd.h>
25#include <asm/debug-monitors.h>
26#include <asm/processor.h>
27#include <asm/thread_info.h>
28
Olivier Deprez0e641232021-09-23 10:07:05 +020029extern struct exception_table_entry __start___kvm_ex_table;
30extern struct exception_table_entry __stop___kvm_ex_table;
31
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032/* Check whether the FP regs were dirtied while in the host-side run loop: */
33static bool __hyp_text update_fp_enabled(struct kvm_vcpu *vcpu)
34{
Olivier Deprez0e641232021-09-23 10:07:05 +020035 /*
36 * When the system doesn't support FP/SIMD, we cannot rely on
37 * the _TIF_FOREIGN_FPSTATE flag. However, we always inject an
38 * abort on the very first access to FP and thus we should never
39 * see KVM_ARM64_FP_ENABLED. For added safety, make sure we always
40 * trap the accesses.
41 */
42 if (!system_supports_fpsimd() ||
43 vcpu->arch.host_thread_info->flags & _TIF_FOREIGN_FPSTATE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
45 KVM_ARM64_FP_HOST);
46
47 return !!(vcpu->arch.flags & KVM_ARM64_FP_ENABLED);
48}
49
50/* Save the 32-bit only FPSIMD system register state */
51static void __hyp_text __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
52{
53 if (!vcpu_el1_is_32bit(vcpu))
54 return;
55
56 vcpu->arch.ctxt.sys_regs[FPEXC32_EL2] = read_sysreg(fpexc32_el2);
57}
58
59static void __hyp_text __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
60{
61 /*
62 * We are about to set CPTR_EL2.TFP to trap all floating point
63 * register accesses to EL2, however, the ARM ARM clearly states that
64 * traps are only taken to EL2 if the operation would not otherwise
65 * trap to EL1. Therefore, always make sure that for 32-bit guests,
66 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
67 * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
68 * it will cause an exception.
69 */
70 if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) {
71 write_sysreg(1 << 30, fpexc32_el2);
72 isb();
73 }
74}
75
76static void __hyp_text __activate_traps_common(struct kvm_vcpu *vcpu)
77{
78 /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
79 write_sysreg(1 << 15, hstr_el2);
80
81 /*
82 * Make sure we trap PMU access from EL0 to EL2. Also sanitize
83 * PMSELR_EL0 to make sure it never contains the cycle
84 * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
85 * EL1 instead of being trapped to EL2.
86 */
87 write_sysreg(0, pmselr_el0);
88 write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
89 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
90}
91
92static void __hyp_text __deactivate_traps_common(void)
93{
94 write_sysreg(0, hstr_el2);
95 write_sysreg(0, pmuserenr_el0);
96}
97
98static void activate_traps_vhe(struct kvm_vcpu *vcpu)
99{
100 u64 val;
101
102 val = read_sysreg(cpacr_el1);
103 val |= CPACR_EL1_TTA;
104 val &= ~CPACR_EL1_ZEN;
David Brazdil0f672f62019-12-10 10:32:29 +0000105 if (update_fp_enabled(vcpu)) {
106 if (vcpu_has_sve(vcpu))
107 val |= CPACR_EL1_ZEN;
108 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109 val &= ~CPACR_EL1_FPEN;
110 __activate_traps_fpsimd32(vcpu);
111 }
112
113 write_sysreg(val, cpacr_el1);
114
115 write_sysreg(kvm_get_hyp_vector(), vbar_el1);
116}
David Brazdil0f672f62019-12-10 10:32:29 +0000117NOKPROBE_SYMBOL(activate_traps_vhe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118
119static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu)
120{
121 u64 val;
122
123 __activate_traps_common(vcpu);
124
125 val = CPTR_EL2_DEFAULT;
126 val |= CPTR_EL2_TTA | CPTR_EL2_TZ;
127 if (!update_fp_enabled(vcpu)) {
128 val |= CPTR_EL2_TFP;
129 __activate_traps_fpsimd32(vcpu);
130 }
131
132 write_sysreg(val, cptr_el2);
133}
134
135static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
136{
137 u64 hcr = vcpu->arch.hcr_el2;
138
David Brazdil0f672f62019-12-10 10:32:29 +0000139 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM))
140 hcr |= HCR_TVM;
141
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000142 write_sysreg(hcr, hcr_el2);
143
144 if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE))
145 write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
146
147 if (has_vhe())
148 activate_traps_vhe(vcpu);
149 else
150 __activate_traps_nvhe(vcpu);
151}
152
153static void deactivate_traps_vhe(void)
154{
155 extern char vectors[]; /* kernel exception vectors */
156 write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
David Brazdil0f672f62019-12-10 10:32:29 +0000157
158 /*
159 * ARM erratum 1165522 requires the actual execution of the above
160 * before we can switch to the EL2/EL0 translation regime used by
161 * the host.
162 */
163 asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_1165522));
164
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165 write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
166 write_sysreg(vectors, vbar_el1);
167}
David Brazdil0f672f62019-12-10 10:32:29 +0000168NOKPROBE_SYMBOL(deactivate_traps_vhe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169
170static void __hyp_text __deactivate_traps_nvhe(void)
171{
172 u64 mdcr_el2 = read_sysreg(mdcr_el2);
173
174 __deactivate_traps_common();
175
176 mdcr_el2 &= MDCR_EL2_HPMN_MASK;
177 mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
178
179 write_sysreg(mdcr_el2, mdcr_el2);
David Brazdil0f672f62019-12-10 10:32:29 +0000180 write_sysreg(HCR_HOST_NVHE_FLAGS, hcr_el2);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
182}
183
184static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
185{
186 /*
187 * If we pended a virtual abort, preserve it until it gets
188 * cleared. See D1.14.3 (Virtual Interrupts) for details, but
189 * the crucial bit is "On taking a vSError interrupt,
190 * HCR_EL2.VSE is cleared to 0."
191 */
David Brazdil0f672f62019-12-10 10:32:29 +0000192 if (vcpu->arch.hcr_el2 & HCR_VSE) {
193 vcpu->arch.hcr_el2 &= ~HCR_VSE;
194 vcpu->arch.hcr_el2 |= read_sysreg(hcr_el2) & HCR_VSE;
195 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000196
197 if (has_vhe())
198 deactivate_traps_vhe();
199 else
200 __deactivate_traps_nvhe();
201}
202
203void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
204{
205 __activate_traps_common(vcpu);
206}
207
208void deactivate_traps_vhe_put(void)
209{
210 u64 mdcr_el2 = read_sysreg(mdcr_el2);
211
212 mdcr_el2 &= MDCR_EL2_HPMN_MASK |
213 MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
214 MDCR_EL2_TPMS;
215
216 write_sysreg(mdcr_el2, mdcr_el2);
217
218 __deactivate_traps_common();
219}
220
221static void __hyp_text __activate_vm(struct kvm *kvm)
222{
David Brazdil0f672f62019-12-10 10:32:29 +0000223 __load_guest_stage2(kvm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224}
225
226static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
227{
228 write_sysreg(0, vttbr_el2);
229}
230
231/* Save VGICv3 state on non-VHE systems */
232static void __hyp_text __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
233{
234 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
235 __vgic_v3_save_state(vcpu);
236 __vgic_v3_deactivate_traps(vcpu);
237 }
238}
239
240/* Restore VGICv3 state on non_VEH systems */
241static void __hyp_text __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
242{
243 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
244 __vgic_v3_activate_traps(vcpu);
245 __vgic_v3_restore_state(vcpu);
246 }
247}
248
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000249static bool __hyp_text __translate_far_to_hpfar(u64 far, u64 *hpfar)
250{
251 u64 par, tmp;
252
253 /*
254 * Resolve the IPA the hard way using the guest VA.
255 *
256 * Stage-1 translation already validated the memory access
257 * rights. As such, we can use the EL1 translation regime, and
258 * don't have to distinguish between EL0 and EL1 access.
259 *
260 * We do need to save/restore PAR_EL1 though, as we haven't
261 * saved the guest context yet, and we may return early...
262 */
263 par = read_sysreg(par_el1);
Olivier Deprez0e641232021-09-23 10:07:05 +0200264 if (!__kvm_at("s1e1r", far))
265 tmp = read_sysreg(par_el1);
266 else
267 tmp = SYS_PAR_EL1_F; /* back to the guest */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268 write_sysreg(par, par_el1);
269
David Brazdil0f672f62019-12-10 10:32:29 +0000270 if (unlikely(tmp & SYS_PAR_EL1_F))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271 return false; /* Translation failed, back to guest */
272
273 /* Convert PAR to HPFAR format */
David Brazdil0f672f62019-12-10 10:32:29 +0000274 *hpfar = PAR_TO_HPFAR(tmp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275 return true;
276}
277
278static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu)
279{
280 u8 ec;
281 u64 esr;
282 u64 hpfar, far;
283
284 esr = vcpu->arch.fault.esr_el2;
285 ec = ESR_ELx_EC(esr);
286
287 if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW)
288 return true;
289
David Brazdil0f672f62019-12-10 10:32:29 +0000290 far = read_sysreg_el2(SYS_FAR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291
292 /*
293 * The HPFAR can be invalid if the stage 2 fault did not
294 * happen during a stage 1 page table walk (the ESR_EL2.S1PTW
295 * bit is clear) and one of the two following cases are true:
296 * 1. The fault was due to a permission fault
297 * 2. The processor carries errata 834220
298 *
299 * Therefore, for all non S1PTW faults where we either have a
300 * permission fault or the errata workaround is enabled, we
301 * resolve the IPA using the AT instruction.
302 */
303 if (!(esr & ESR_ELx_S1PTW) &&
David Brazdil0f672f62019-12-10 10:32:29 +0000304 (cpus_have_const_cap(ARM64_WORKAROUND_834220) ||
305 (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306 if (!__translate_far_to_hpfar(far, &hpfar))
307 return false;
308 } else {
309 hpfar = read_sysreg(hpfar_el2);
310 }
311
312 vcpu->arch.fault.far_el2 = far;
313 vcpu->arch.fault.hpfar_el2 = hpfar;
314 return true;
315}
316
David Brazdil0f672f62019-12-10 10:32:29 +0000317/* Check for an FPSIMD/SVE trap and handle as appropriate */
318static bool __hyp_text __hyp_handle_fpsimd(struct kvm_vcpu *vcpu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000319{
David Brazdil0f672f62019-12-10 10:32:29 +0000320 bool vhe, sve_guest, sve_host;
321 u8 hsr_ec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322
David Brazdil0f672f62019-12-10 10:32:29 +0000323 if (!system_supports_fpsimd())
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324 return false;
David Brazdil0f672f62019-12-10 10:32:29 +0000325
326 if (system_supports_sve()) {
327 sve_guest = vcpu_has_sve(vcpu);
328 sve_host = vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE;
329 vhe = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000331 sve_guest = false;
332 sve_host = false;
333 vhe = has_vhe();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000334 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335
David Brazdil0f672f62019-12-10 10:32:29 +0000336 hsr_ec = kvm_vcpu_trap_get_class(vcpu);
337 if (hsr_ec != ESR_ELx_EC_FP_ASIMD &&
338 hsr_ec != ESR_ELx_EC_SVE)
339 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340
David Brazdil0f672f62019-12-10 10:32:29 +0000341 /* Don't handle SVE traps for non-SVE vcpus here: */
342 if (!sve_guest)
343 if (hsr_ec != ESR_ELx_EC_FP_ASIMD)
344 return false;
345
346 /* Valid trap. Switch the context: */
347
348 if (vhe) {
349 u64 reg = read_sysreg(cpacr_el1) | CPACR_EL1_FPEN;
350
351 if (sve_guest)
352 reg |= CPACR_EL1_ZEN;
353
354 write_sysreg(reg, cpacr_el1);
355 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356 write_sysreg(read_sysreg(cptr_el2) & ~(u64)CPTR_EL2_TFP,
357 cptr_el2);
David Brazdil0f672f62019-12-10 10:32:29 +0000358 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359
360 isb();
361
362 if (vcpu->arch.flags & KVM_ARM64_FP_HOST) {
363 /*
364 * In the SVE case, VHE is assumed: it is enforced by
365 * Kconfig and kvm_arch_init().
366 */
David Brazdil0f672f62019-12-10 10:32:29 +0000367 if (sve_host) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368 struct thread_struct *thread = container_of(
David Brazdil0f672f62019-12-10 10:32:29 +0000369 vcpu->arch.host_fpsimd_state,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370 struct thread_struct, uw.fpsimd_state);
371
David Brazdil0f672f62019-12-10 10:32:29 +0000372 sve_save_state(sve_pffr(thread),
373 &vcpu->arch.host_fpsimd_state->fpsr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000375 __fpsimd_save_state(vcpu->arch.host_fpsimd_state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000376 }
377
378 vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
379 }
380
David Brazdil0f672f62019-12-10 10:32:29 +0000381 if (sve_guest) {
382 sve_load_state(vcpu_sve_pffr(vcpu),
383 &vcpu->arch.ctxt.gp_regs.fp_regs.fpsr,
384 sve_vq_from_vl(vcpu->arch.sve_max_vl) - 1);
385 write_sysreg_s(vcpu->arch.ctxt.sys_regs[ZCR_EL1], SYS_ZCR_EL12);
386 } else {
387 __fpsimd_restore_state(&vcpu->arch.ctxt.gp_regs.fp_regs);
388 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389
390 /* Skip restoring fpexc32 for AArch64 guests */
391 if (!(read_sysreg(hcr_el2) & HCR_RW))
392 write_sysreg(vcpu->arch.ctxt.sys_regs[FPEXC32_EL2],
393 fpexc32_el2);
394
395 vcpu->arch.flags |= KVM_ARM64_FP_ENABLED;
396
397 return true;
398}
399
David Brazdil0f672f62019-12-10 10:32:29 +0000400static bool __hyp_text handle_tx2_tvm(struct kvm_vcpu *vcpu)
401{
402 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_hsr(vcpu));
403 int rt = kvm_vcpu_sys_get_rt(vcpu);
404 u64 val = vcpu_get_reg(vcpu, rt);
405
406 /*
407 * The normal sysreg handling code expects to see the traps,
408 * let's not do anything here.
409 */
410 if (vcpu->arch.hcr_el2 & HCR_TVM)
411 return false;
412
413 switch (sysreg) {
414 case SYS_SCTLR_EL1:
415 write_sysreg_el1(val, SYS_SCTLR);
416 break;
417 case SYS_TTBR0_EL1:
418 write_sysreg_el1(val, SYS_TTBR0);
419 break;
420 case SYS_TTBR1_EL1:
421 write_sysreg_el1(val, SYS_TTBR1);
422 break;
423 case SYS_TCR_EL1:
424 write_sysreg_el1(val, SYS_TCR);
425 break;
426 case SYS_ESR_EL1:
427 write_sysreg_el1(val, SYS_ESR);
428 break;
429 case SYS_FAR_EL1:
430 write_sysreg_el1(val, SYS_FAR);
431 break;
432 case SYS_AFSR0_EL1:
433 write_sysreg_el1(val, SYS_AFSR0);
434 break;
435 case SYS_AFSR1_EL1:
436 write_sysreg_el1(val, SYS_AFSR1);
437 break;
438 case SYS_MAIR_EL1:
439 write_sysreg_el1(val, SYS_MAIR);
440 break;
441 case SYS_AMAIR_EL1:
442 write_sysreg_el1(val, SYS_AMAIR);
443 break;
444 case SYS_CONTEXTIDR_EL1:
445 write_sysreg_el1(val, SYS_CONTEXTIDR);
446 break;
447 default:
448 return false;
449 }
450
451 __kvm_skip_instr(vcpu);
452 return true;
453}
454
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000455/*
456 * Return true when we were able to fixup the guest exit and should return to
457 * the guest, false when we should restore the host state and return to the
458 * main run loop.
459 */
460static bool __hyp_text fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
461{
462 if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ)
David Brazdil0f672f62019-12-10 10:32:29 +0000463 vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464
465 /*
466 * We're using the raw exception code in order to only process
467 * the trap if no SError is pending. We will come back to the
468 * same PC once the SError has been injected, and replay the
469 * trapping instruction.
470 */
471 if (*exit_code != ARM_EXCEPTION_TRAP)
472 goto exit;
473
David Brazdil0f672f62019-12-10 10:32:29 +0000474 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) &&
475 kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 &&
476 handle_tx2_tvm(vcpu))
477 return true;
478
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479 /*
480 * We trap the first access to the FP/SIMD to save the host context
481 * and restore the guest context lazily.
482 * If FP/SIMD is not implemented, handle the trap and inject an
483 * undefined instruction exception to the guest.
David Brazdil0f672f62019-12-10 10:32:29 +0000484 * Similarly for trapped SVE accesses.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485 */
David Brazdil0f672f62019-12-10 10:32:29 +0000486 if (__hyp_handle_fpsimd(vcpu))
487 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000488
489 if (!__populate_fault_info(vcpu))
490 return true;
491
492 if (static_branch_unlikely(&vgic_v2_cpuif_trap)) {
493 bool valid;
494
495 valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
496 kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
497 kvm_vcpu_dabt_isvalid(vcpu) &&
498 !kvm_vcpu_dabt_isextabt(vcpu) &&
Olivier Deprez0e641232021-09-23 10:07:05 +0200499 !kvm_vcpu_abt_iss1tw(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500
501 if (valid) {
502 int ret = __vgic_v2_perform_cpuif_access(vcpu);
503
David Brazdil0f672f62019-12-10 10:32:29 +0000504 if (ret == 1)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505 return true;
506
David Brazdil0f672f62019-12-10 10:32:29 +0000507 /* Promote an illegal access to an SError.*/
508 if (ret == -1)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000509 *exit_code = ARM_EXCEPTION_EL1_SERROR;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510
511 goto exit;
512 }
513 }
514
515 if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
516 (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 ||
517 kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) {
518 int ret = __vgic_v3_perform_cpuif_access(vcpu);
519
David Brazdil0f672f62019-12-10 10:32:29 +0000520 if (ret == 1)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000521 return true;
522 }
523
524exit:
525 /* Return to the host kernel and handle the exit */
526 return false;
527}
528
529static inline bool __hyp_text __needs_ssbd_off(struct kvm_vcpu *vcpu)
530{
531 if (!cpus_have_const_cap(ARM64_SSBD))
532 return false;
533
534 return !(vcpu->arch.workaround_flags & VCPU_WORKAROUND_2_FLAG);
535}
536
537static void __hyp_text __set_guest_arch_workaround_state(struct kvm_vcpu *vcpu)
538{
539#ifdef CONFIG_ARM64_SSBD
540 /*
541 * The host runs with the workaround always present. If the
542 * guest wants it disabled, so be it...
543 */
544 if (__needs_ssbd_off(vcpu) &&
545 __hyp_this_cpu_read(arm64_ssbd_callback_required))
546 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 0, NULL);
547#endif
548}
549
550static void __hyp_text __set_host_arch_workaround_state(struct kvm_vcpu *vcpu)
551{
552#ifdef CONFIG_ARM64_SSBD
553 /*
554 * If the guest has disabled the workaround, bring it back on.
555 */
556 if (__needs_ssbd_off(vcpu) &&
557 __hyp_this_cpu_read(arm64_ssbd_callback_required))
558 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 1, NULL);
559#endif
560}
561
David Brazdil0f672f62019-12-10 10:32:29 +0000562/**
563 * Disable host events, enable guest events
564 */
565static bool __hyp_text __pmu_switch_to_guest(struct kvm_cpu_context *host_ctxt)
566{
567 struct kvm_host_data *host;
568 struct kvm_pmu_events *pmu;
569
570 host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
571 pmu = &host->pmu_events;
572
573 if (pmu->events_host)
574 write_sysreg(pmu->events_host, pmcntenclr_el0);
575
576 if (pmu->events_guest)
577 write_sysreg(pmu->events_guest, pmcntenset_el0);
578
579 return (pmu->events_host || pmu->events_guest);
580}
581
582/**
583 * Disable guest events, enable host events
584 */
585static void __hyp_text __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt)
586{
587 struct kvm_host_data *host;
588 struct kvm_pmu_events *pmu;
589
590 host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
591 pmu = &host->pmu_events;
592
593 if (pmu->events_guest)
594 write_sysreg(pmu->events_guest, pmcntenclr_el0);
595
596 if (pmu->events_host)
597 write_sysreg(pmu->events_host, pmcntenset_el0);
598}
599
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000600/* Switch to the guest for VHE systems running in EL2 */
601int kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
602{
603 struct kvm_cpu_context *host_ctxt;
604 struct kvm_cpu_context *guest_ctxt;
605 u64 exit_code;
606
607 host_ctxt = vcpu->arch.host_cpu_context;
608 host_ctxt->__hyp_running_vcpu = vcpu;
609 guest_ctxt = &vcpu->arch.ctxt;
610
611 sysreg_save_host_state_vhe(host_ctxt);
612
David Brazdil0f672f62019-12-10 10:32:29 +0000613 /*
614 * ARM erratum 1165522 requires us to configure both stage 1 and
615 * stage 2 translation for the guest context before we clear
616 * HCR_EL2.TGE.
617 *
618 * We have already configured the guest's stage 1 translation in
619 * kvm_vcpu_load_sysregs above. We must now call __activate_vm
620 * before __activate_traps, because __activate_vm configures
621 * stage 2 translation, and __activate_traps clear HCR_EL2.TGE
622 * (among other things).
623 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000624 __activate_vm(vcpu->kvm);
David Brazdil0f672f62019-12-10 10:32:29 +0000625 __activate_traps(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000626
627 sysreg_restore_guest_state_vhe(guest_ctxt);
628 __debug_switch_to_guest(vcpu);
629
630 __set_guest_arch_workaround_state(vcpu);
631
632 do {
633 /* Jump in the fire! */
634 exit_code = __guest_enter(vcpu, host_ctxt);
635
636 /* And we're baaack! */
637 } while (fixup_guest_exit(vcpu, &exit_code));
638
639 __set_host_arch_workaround_state(vcpu);
640
641 sysreg_save_guest_state_vhe(guest_ctxt);
642
643 __deactivate_traps(vcpu);
644
645 sysreg_restore_host_state_vhe(host_ctxt);
646
647 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
648 __fpsimd_save_fpexc32(vcpu);
649
650 __debug_switch_to_host(vcpu);
651
652 return exit_code;
653}
David Brazdil0f672f62019-12-10 10:32:29 +0000654NOKPROBE_SYMBOL(kvm_vcpu_run_vhe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000655
656/* Switch to the guest for legacy non-VHE systems */
657int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu)
658{
659 struct kvm_cpu_context *host_ctxt;
660 struct kvm_cpu_context *guest_ctxt;
David Brazdil0f672f62019-12-10 10:32:29 +0000661 bool pmu_switch_needed;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000662 u64 exit_code;
663
David Brazdil0f672f62019-12-10 10:32:29 +0000664 /*
665 * Having IRQs masked via PMR when entering the guest means the GIC
666 * will not signal the CPU of interrupts of lower priority, and the
667 * only way to get out will be via guest exceptions.
668 * Naturally, we want to avoid this.
669 */
670 if (system_uses_irq_prio_masking()) {
671 gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
672 dsb(sy);
673 }
674
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675 vcpu = kern_hyp_va(vcpu);
676
677 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
678 host_ctxt->__hyp_running_vcpu = vcpu;
679 guest_ctxt = &vcpu->arch.ctxt;
680
David Brazdil0f672f62019-12-10 10:32:29 +0000681 pmu_switch_needed = __pmu_switch_to_guest(host_ctxt);
682
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000683 __sysreg_save_state_nvhe(host_ctxt);
684
Olivier Deprez0e641232021-09-23 10:07:05 +0200685 /*
686 * We must flush and disable the SPE buffer for nVHE, as
687 * the translation regime(EL1&0) is going to be loaded with
688 * that of the guest. And we must do this before we change the
689 * translation regime to EL2 (via MDCR_EL2_EPB == 0) and
690 * before we load guest Stage1.
691 */
692 __debug_save_host_buffers_nvhe(vcpu);
693
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000694 __activate_vm(kern_hyp_va(vcpu->kvm));
David Brazdil0f672f62019-12-10 10:32:29 +0000695 __activate_traps(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000696
697 __hyp_vgic_restore_state(vcpu);
698 __timer_enable_traps(vcpu);
699
700 /*
701 * We must restore the 32-bit state before the sysregs, thanks
702 * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
703 */
704 __sysreg32_restore_state(vcpu);
705 __sysreg_restore_state_nvhe(guest_ctxt);
706 __debug_switch_to_guest(vcpu);
707
708 __set_guest_arch_workaround_state(vcpu);
709
710 do {
711 /* Jump in the fire! */
712 exit_code = __guest_enter(vcpu, host_ctxt);
713
714 /* And we're baaack! */
715 } while (fixup_guest_exit(vcpu, &exit_code));
716
717 __set_host_arch_workaround_state(vcpu);
718
719 __sysreg_save_state_nvhe(guest_ctxt);
720 __sysreg32_save_state(vcpu);
721 __timer_disable_traps(vcpu);
722 __hyp_vgic_save_state(vcpu);
723
724 __deactivate_traps(vcpu);
725 __deactivate_vm(vcpu);
726
727 __sysreg_restore_state_nvhe(host_ctxt);
728
729 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
730 __fpsimd_save_fpexc32(vcpu);
731
Olivier Deprez0e641232021-09-23 10:07:05 +0200732 __debug_switch_to_host(vcpu);
733
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 /*
735 * This must come after restoring the host sysregs, since a non-VHE
736 * system may enable SPE here and make use of the TTBRs.
737 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200738 __debug_restore_host_buffers_nvhe(vcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739
David Brazdil0f672f62019-12-10 10:32:29 +0000740 if (pmu_switch_needed)
741 __pmu_switch_to_host(host_ctxt);
742
743 /* Returning to host will clear PSR.I, remask PMR if needed */
744 if (system_uses_irq_prio_masking())
745 gic_write_pmr(GIC_PRIO_IRQOFF);
746
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000747 return exit_code;
748}
749
750static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
751
752static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par,
753 struct kvm_cpu_context *__host_ctxt)
754{
755 struct kvm_vcpu *vcpu;
756 unsigned long str_va;
757
758 vcpu = __host_ctxt->__hyp_running_vcpu;
759
760 if (read_sysreg(vttbr_el2)) {
761 __timer_disable_traps(vcpu);
762 __deactivate_traps(vcpu);
763 __deactivate_vm(vcpu);
764 __sysreg_restore_state_nvhe(__host_ctxt);
765 }
766
767 /*
768 * Force the panic string to be loaded from the literal pool,
769 * making sure it is a kernel address and not a PC-relative
770 * reference.
771 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200772 asm volatile("ldr %0, =%1" : "=r" (str_va) : "S" (__hyp_panic_string));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000773
774 __hyp_do_panic(str_va,
David Brazdil0f672f62019-12-10 10:32:29 +0000775 spsr, elr,
776 read_sysreg(esr_el2), read_sysreg_el2(SYS_FAR),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000777 read_sysreg(hpfar_el2), par, vcpu);
778}
779
780static void __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par,
781 struct kvm_cpu_context *host_ctxt)
782{
783 struct kvm_vcpu *vcpu;
784 vcpu = host_ctxt->__hyp_running_vcpu;
785
786 __deactivate_traps(vcpu);
787 sysreg_restore_host_state_vhe(host_ctxt);
788
789 panic(__hyp_panic_string,
790 spsr, elr,
David Brazdil0f672f62019-12-10 10:32:29 +0000791 read_sysreg_el2(SYS_ESR), read_sysreg_el2(SYS_FAR),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000792 read_sysreg(hpfar_el2), par, vcpu);
793}
David Brazdil0f672f62019-12-10 10:32:29 +0000794NOKPROBE_SYMBOL(__hyp_call_panic_vhe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795
796void __hyp_text __noreturn hyp_panic(struct kvm_cpu_context *host_ctxt)
797{
David Brazdil0f672f62019-12-10 10:32:29 +0000798 u64 spsr = read_sysreg_el2(SYS_SPSR);
799 u64 elr = read_sysreg_el2(SYS_ELR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000800 u64 par = read_sysreg(par_el1);
801
802 if (!has_vhe())
803 __hyp_call_panic_nvhe(spsr, elr, par, host_ctxt);
804 else
805 __hyp_call_panic_vhe(spsr, elr, par, host_ctxt);
806
807 unreachable();
808}
Olivier Deprez0e641232021-09-23 10:07:05 +0200809
810asmlinkage void __hyp_text kvm_unexpected_el2_exception(void)
811{
812 unsigned long addr, fixup;
813 struct kvm_cpu_context *host_ctxt;
814 struct exception_table_entry *entry, *end;
815 unsigned long elr_el2 = read_sysreg(elr_el2);
816
817 entry = hyp_symbol_addr(__start___kvm_ex_table);
818 end = hyp_symbol_addr(__stop___kvm_ex_table);
819 host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
820
821 while (entry < end) {
822 addr = (unsigned long)&entry->insn + entry->insn;
823 fixup = (unsigned long)&entry->fixup + entry->fixup;
824
825 if (addr != elr_el2) {
826 entry++;
827 continue;
828 }
829
830 write_sysreg(fixup, elr_el2);
831 return;
832 }
833
834 hyp_panic(host_ctxt);
835}