8a1e81a400e0f0257c3e6a45775a7b2c16b3b15b
[linux-2.6-microblaze.git] / arch / arm64 / kvm / hyp / switch.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6
7 #include <linux/arm-smccc.h>
8 #include <linux/kvm_host.h>
9 #include <linux/types.h>
10 #include <linux/jump_label.h>
11 #include <uapi/linux/psci.h>
12
13 #include <kvm/arm_psci.h>
14
15 #include <asm/barrier.h>
16 #include <asm/cpufeature.h>
17 #include <asm/kprobes.h>
18 #include <asm/kvm_asm.h>
19 #include <asm/kvm_emulate.h>
20 #include <asm/kvm_hyp.h>
21 #include <asm/kvm_mmu.h>
22 #include <asm/fpsimd.h>
23 #include <asm/debug-monitors.h>
24 #include <asm/processor.h>
25 #include <asm/thread_info.h>
26
27 /* Check whether the FP regs were dirtied while in the host-side run loop: */
28 static bool __hyp_text update_fp_enabled(struct kvm_vcpu *vcpu)
29 {
30         /*
31          * When the system doesn't support FP/SIMD, we cannot rely on
32          * the _TIF_FOREIGN_FPSTATE flag. However, we always inject an
33          * abort on the very first access to FP and thus we should never
34          * see KVM_ARM64_FP_ENABLED. For added safety, make sure we always
35          * trap the accesses.
36          */
37         if (!system_supports_fpsimd() ||
38             vcpu->arch.host_thread_info->flags & _TIF_FOREIGN_FPSTATE)
39                 vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
40                                       KVM_ARM64_FP_HOST);
41
42         return !!(vcpu->arch.flags & KVM_ARM64_FP_ENABLED);
43 }
44
45 /* Save the 32-bit only FPSIMD system register state */
46 static void __hyp_text __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
47 {
48         if (!vcpu_el1_is_32bit(vcpu))
49                 return;
50
51         vcpu->arch.ctxt.sys_regs[FPEXC32_EL2] = read_sysreg(fpexc32_el2);
52 }
53
54 static void __hyp_text __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
55 {
56         /*
57          * We are about to set CPTR_EL2.TFP to trap all floating point
58          * register accesses to EL2, however, the ARM ARM clearly states that
59          * traps are only taken to EL2 if the operation would not otherwise
60          * trap to EL1.  Therefore, always make sure that for 32-bit guests,
61          * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
62          * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
63          * it will cause an exception.
64          */
65         if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) {
66                 write_sysreg(1 << 30, fpexc32_el2);
67                 isb();
68         }
69 }
70
71 static void __hyp_text __activate_traps_common(struct kvm_vcpu *vcpu)
72 {
73         /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
74         write_sysreg(1 << 15, hstr_el2);
75
76         /*
77          * Make sure we trap PMU access from EL0 to EL2. Also sanitize
78          * PMSELR_EL0 to make sure it never contains the cycle
79          * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
80          * EL1 instead of being trapped to EL2.
81          */
82         write_sysreg(0, pmselr_el0);
83         write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
84         write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
85 }
86
87 static void __hyp_text __deactivate_traps_common(void)
88 {
89         write_sysreg(0, hstr_el2);
90         write_sysreg(0, pmuserenr_el0);
91 }
92
93 static void activate_traps_vhe(struct kvm_vcpu *vcpu)
94 {
95         u64 val;
96
97         val = read_sysreg(cpacr_el1);
98         val |= CPACR_EL1_TTA;
99         val &= ~CPACR_EL1_ZEN;
100
101         /*
102          * With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to
103          * CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2,
104          * except for some missing controls, such as TAM.
105          * In this case, CPTR_EL2.TAM has the same position with or without
106          * VHE (HCR.E2H == 1) which allows us to use here the CPTR_EL2.TAM
107          * shift value for trapping the AMU accesses.
108          */
109
110         val |= CPTR_EL2_TAM;
111
112         if (update_fp_enabled(vcpu)) {
113                 if (vcpu_has_sve(vcpu))
114                         val |= CPACR_EL1_ZEN;
115         } else {
116                 val &= ~CPACR_EL1_FPEN;
117                 __activate_traps_fpsimd32(vcpu);
118         }
119
120         write_sysreg(val, cpacr_el1);
121
122         write_sysreg(kvm_get_hyp_vector(), vbar_el1);
123 }
124 NOKPROBE_SYMBOL(activate_traps_vhe);
125
126 static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu)
127 {
128         u64 val;
129
130         __activate_traps_common(vcpu);
131
132         val = CPTR_EL2_DEFAULT;
133         val |= CPTR_EL2_TTA | CPTR_EL2_TZ | CPTR_EL2_TAM;
134         if (!update_fp_enabled(vcpu)) {
135                 val |= CPTR_EL2_TFP;
136                 __activate_traps_fpsimd32(vcpu);
137         }
138
139         write_sysreg(val, cptr_el2);
140
141         if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT_NVHE)) {
142                 struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt;
143
144                 isb();
145                 /*
146                  * At this stage, and thanks to the above isb(), S2 is
147                  * configured and enabled. We can now restore the guest's S1
148                  * configuration: SCTLR, and only then TCR.
149                  */
150                 write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1],     SYS_SCTLR);
151                 isb();
152                 write_sysreg_el1(ctxt->sys_regs[TCR_EL1],       SYS_TCR);
153         }
154 }
155
156 static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
157 {
158         u64 hcr = vcpu->arch.hcr_el2;
159
160         if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM))
161                 hcr |= HCR_TVM;
162
163         write_sysreg(hcr, hcr_el2);
164
165         if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE))
166                 write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
167
168         if (has_vhe())
169                 activate_traps_vhe(vcpu);
170         else
171                 __activate_traps_nvhe(vcpu);
172 }
173
174 static void deactivate_traps_vhe(void)
175 {
176         extern char vectors[];  /* kernel exception vectors */
177         write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
178
179         /*
180          * ARM errata 1165522 and 1530923 require the actual execution of the
181          * above before we can switch to the EL2/EL0 translation regime used by
182          * the host.
183          */
184         asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT_VHE));
185
186         write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
187         write_sysreg(vectors, vbar_el1);
188 }
189 NOKPROBE_SYMBOL(deactivate_traps_vhe);
190
191 static void __hyp_text __deactivate_traps_nvhe(void)
192 {
193         u64 mdcr_el2 = read_sysreg(mdcr_el2);
194
195         if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT_NVHE)) {
196                 u64 val;
197
198                 /*
199                  * Set the TCR and SCTLR registers in the exact opposite
200                  * sequence as __activate_traps_nvhe (first prevent walks,
201                  * then force the MMU on). A generous sprinkling of isb()
202                  * ensure that things happen in this exact order.
203                  */
204                 val = read_sysreg_el1(SYS_TCR);
205                 write_sysreg_el1(val | TCR_EPD1_MASK | TCR_EPD0_MASK, SYS_TCR);
206                 isb();
207                 val = read_sysreg_el1(SYS_SCTLR);
208                 write_sysreg_el1(val | SCTLR_ELx_M, SYS_SCTLR);
209                 isb();
210         }
211
212         __deactivate_traps_common();
213
214         mdcr_el2 &= MDCR_EL2_HPMN_MASK;
215         mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
216
217         write_sysreg(mdcr_el2, mdcr_el2);
218         write_sysreg(HCR_HOST_NVHE_FLAGS, hcr_el2);
219         write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
220 }
221
222 static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
223 {
224         /*
225          * If we pended a virtual abort, preserve it until it gets
226          * cleared. See D1.14.3 (Virtual Interrupts) for details, but
227          * the crucial bit is "On taking a vSError interrupt,
228          * HCR_EL2.VSE is cleared to 0."
229          */
230         if (vcpu->arch.hcr_el2 & HCR_VSE) {
231                 vcpu->arch.hcr_el2 &= ~HCR_VSE;
232                 vcpu->arch.hcr_el2 |= read_sysreg(hcr_el2) & HCR_VSE;
233         }
234
235         if (has_vhe())
236                 deactivate_traps_vhe();
237         else
238                 __deactivate_traps_nvhe();
239 }
240
241 void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
242 {
243         __activate_traps_common(vcpu);
244 }
245
246 void deactivate_traps_vhe_put(void)
247 {
248         u64 mdcr_el2 = read_sysreg(mdcr_el2);
249
250         mdcr_el2 &= MDCR_EL2_HPMN_MASK |
251                     MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
252                     MDCR_EL2_TPMS;
253
254         write_sysreg(mdcr_el2, mdcr_el2);
255
256         __deactivate_traps_common();
257 }
258
259 static void __hyp_text __activate_vm(struct kvm *kvm)
260 {
261         __load_guest_stage2(kvm);
262 }
263
264 static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
265 {
266         write_sysreg(0, vttbr_el2);
267 }
268
269 /* Save VGICv3 state on non-VHE systems */
270 static void __hyp_text __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
271 {
272         if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
273                 __vgic_v3_save_state(vcpu);
274                 __vgic_v3_deactivate_traps(vcpu);
275         }
276 }
277
278 /* Restore VGICv3 state on non_VEH systems */
279 static void __hyp_text __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
280 {
281         if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
282                 __vgic_v3_activate_traps(vcpu);
283                 __vgic_v3_restore_state(vcpu);
284         }
285 }
286
287 static bool __hyp_text __translate_far_to_hpfar(u64 far, u64 *hpfar)
288 {
289         u64 par, tmp;
290
291         /*
292          * Resolve the IPA the hard way using the guest VA.
293          *
294          * Stage-1 translation already validated the memory access
295          * rights. As such, we can use the EL1 translation regime, and
296          * don't have to distinguish between EL0 and EL1 access.
297          *
298          * We do need to save/restore PAR_EL1 though, as we haven't
299          * saved the guest context yet, and we may return early...
300          */
301         par = read_sysreg(par_el1);
302         asm volatile("at s1e1r, %0" : : "r" (far));
303         isb();
304
305         tmp = read_sysreg(par_el1);
306         write_sysreg(par, par_el1);
307
308         if (unlikely(tmp & SYS_PAR_EL1_F))
309                 return false; /* Translation failed, back to guest */
310
311         /* Convert PAR to HPFAR format */
312         *hpfar = PAR_TO_HPFAR(tmp);
313         return true;
314 }
315
316 static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu)
317 {
318         u8 ec;
319         u64 esr;
320         u64 hpfar, far;
321
322         esr = vcpu->arch.fault.esr_el2;
323         ec = ESR_ELx_EC(esr);
324
325         if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW)
326                 return true;
327
328         far = read_sysreg_el2(SYS_FAR);
329
330         /*
331          * The HPFAR can be invalid if the stage 2 fault did not
332          * happen during a stage 1 page table walk (the ESR_EL2.S1PTW
333          * bit is clear) and one of the two following cases are true:
334          *   1. The fault was due to a permission fault
335          *   2. The processor carries errata 834220
336          *
337          * Therefore, for all non S1PTW faults where we either have a
338          * permission fault or the errata workaround is enabled, we
339          * resolve the IPA using the AT instruction.
340          */
341         if (!(esr & ESR_ELx_S1PTW) &&
342             (cpus_have_final_cap(ARM64_WORKAROUND_834220) ||
343              (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) {
344                 if (!__translate_far_to_hpfar(far, &hpfar))
345                         return false;
346         } else {
347                 hpfar = read_sysreg(hpfar_el2);
348         }
349
350         vcpu->arch.fault.far_el2 = far;
351         vcpu->arch.fault.hpfar_el2 = hpfar;
352         return true;
353 }
354
355 /* Check for an FPSIMD/SVE trap and handle as appropriate */
356 static bool __hyp_text __hyp_handle_fpsimd(struct kvm_vcpu *vcpu)
357 {
358         bool vhe, sve_guest, sve_host;
359         u8 hsr_ec;
360
361         if (!system_supports_fpsimd())
362                 return false;
363
364         if (system_supports_sve()) {
365                 sve_guest = vcpu_has_sve(vcpu);
366                 sve_host = vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE;
367                 vhe = true;
368         } else {
369                 sve_guest = false;
370                 sve_host = false;
371                 vhe = has_vhe();
372         }
373
374         hsr_ec = kvm_vcpu_trap_get_class(vcpu);
375         if (hsr_ec != ESR_ELx_EC_FP_ASIMD &&
376             hsr_ec != ESR_ELx_EC_SVE)
377                 return false;
378
379         /* Don't handle SVE traps for non-SVE vcpus here: */
380         if (!sve_guest)
381                 if (hsr_ec != ESR_ELx_EC_FP_ASIMD)
382                         return false;
383
384         /* Valid trap.  Switch the context: */
385
386         if (vhe) {
387                 u64 reg = read_sysreg(cpacr_el1) | CPACR_EL1_FPEN;
388
389                 if (sve_guest)
390                         reg |= CPACR_EL1_ZEN;
391
392                 write_sysreg(reg, cpacr_el1);
393         } else {
394                 write_sysreg(read_sysreg(cptr_el2) & ~(u64)CPTR_EL2_TFP,
395                              cptr_el2);
396         }
397
398         isb();
399
400         if (vcpu->arch.flags & KVM_ARM64_FP_HOST) {
401                 /*
402                  * In the SVE case, VHE is assumed: it is enforced by
403                  * Kconfig and kvm_arch_init().
404                  */
405                 if (sve_host) {
406                         struct thread_struct *thread = container_of(
407                                 vcpu->arch.host_fpsimd_state,
408                                 struct thread_struct, uw.fpsimd_state);
409
410                         sve_save_state(sve_pffr(thread),
411                                        &vcpu->arch.host_fpsimd_state->fpsr);
412                 } else {
413                         __fpsimd_save_state(vcpu->arch.host_fpsimd_state);
414                 }
415
416                 vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
417         }
418
419         if (sve_guest) {
420                 sve_load_state(vcpu_sve_pffr(vcpu),
421                                &vcpu->arch.ctxt.gp_regs.fp_regs.fpsr,
422                                sve_vq_from_vl(vcpu->arch.sve_max_vl) - 1);
423                 write_sysreg_s(vcpu->arch.ctxt.sys_regs[ZCR_EL1], SYS_ZCR_EL12);
424         } else {
425                 __fpsimd_restore_state(&vcpu->arch.ctxt.gp_regs.fp_regs);
426         }
427
428         /* Skip restoring fpexc32 for AArch64 guests */
429         if (!(read_sysreg(hcr_el2) & HCR_RW))
430                 write_sysreg(vcpu->arch.ctxt.sys_regs[FPEXC32_EL2],
431                              fpexc32_el2);
432
433         vcpu->arch.flags |= KVM_ARM64_FP_ENABLED;
434
435         return true;
436 }
437
438 static bool __hyp_text handle_tx2_tvm(struct kvm_vcpu *vcpu)
439 {
440         u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_hsr(vcpu));
441         int rt = kvm_vcpu_sys_get_rt(vcpu);
442         u64 val = vcpu_get_reg(vcpu, rt);
443
444         /*
445          * The normal sysreg handling code expects to see the traps,
446          * let's not do anything here.
447          */
448         if (vcpu->arch.hcr_el2 & HCR_TVM)
449                 return false;
450
451         switch (sysreg) {
452         case SYS_SCTLR_EL1:
453                 write_sysreg_el1(val, SYS_SCTLR);
454                 break;
455         case SYS_TTBR0_EL1:
456                 write_sysreg_el1(val, SYS_TTBR0);
457                 break;
458         case SYS_TTBR1_EL1:
459                 write_sysreg_el1(val, SYS_TTBR1);
460                 break;
461         case SYS_TCR_EL1:
462                 write_sysreg_el1(val, SYS_TCR);
463                 break;
464         case SYS_ESR_EL1:
465                 write_sysreg_el1(val, SYS_ESR);
466                 break;
467         case SYS_FAR_EL1:
468                 write_sysreg_el1(val, SYS_FAR);
469                 break;
470         case SYS_AFSR0_EL1:
471                 write_sysreg_el1(val, SYS_AFSR0);
472                 break;
473         case SYS_AFSR1_EL1:
474                 write_sysreg_el1(val, SYS_AFSR1);
475                 break;
476         case SYS_MAIR_EL1:
477                 write_sysreg_el1(val, SYS_MAIR);
478                 break;
479         case SYS_AMAIR_EL1:
480                 write_sysreg_el1(val, SYS_AMAIR);
481                 break;
482         case SYS_CONTEXTIDR_EL1:
483                 write_sysreg_el1(val, SYS_CONTEXTIDR);
484                 break;
485         default:
486                 return false;
487         }
488
489         __kvm_skip_instr(vcpu);
490         return true;
491 }
492
493 /*
494  * Return true when we were able to fixup the guest exit and should return to
495  * the guest, false when we should restore the host state and return to the
496  * main run loop.
497  */
498 static bool __hyp_text fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
499 {
500         if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ)
501                 vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR);
502
503         /*
504          * We're using the raw exception code in order to only process
505          * the trap if no SError is pending. We will come back to the
506          * same PC once the SError has been injected, and replay the
507          * trapping instruction.
508          */
509         if (*exit_code != ARM_EXCEPTION_TRAP)
510                 goto exit;
511
512         if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) &&
513             kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 &&
514             handle_tx2_tvm(vcpu))
515                 return true;
516
517         /*
518          * We trap the first access to the FP/SIMD to save the host context
519          * and restore the guest context lazily.
520          * If FP/SIMD is not implemented, handle the trap and inject an
521          * undefined instruction exception to the guest.
522          * Similarly for trapped SVE accesses.
523          */
524         if (__hyp_handle_fpsimd(vcpu))
525                 return true;
526
527         if (!__populate_fault_info(vcpu))
528                 return true;
529
530         if (static_branch_unlikely(&vgic_v2_cpuif_trap)) {
531                 bool valid;
532
533                 valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
534                         kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
535                         kvm_vcpu_dabt_isvalid(vcpu) &&
536                         !kvm_vcpu_dabt_isextabt(vcpu) &&
537                         !kvm_vcpu_dabt_iss1tw(vcpu);
538
539                 if (valid) {
540                         int ret = __vgic_v2_perform_cpuif_access(vcpu);
541
542                         if (ret == 1)
543                                 return true;
544
545                         /* Promote an illegal access to an SError.*/
546                         if (ret == -1)
547                                 *exit_code = ARM_EXCEPTION_EL1_SERROR;
548
549                         goto exit;
550                 }
551         }
552
553         if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
554             (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 ||
555              kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) {
556                 int ret = __vgic_v3_perform_cpuif_access(vcpu);
557
558                 if (ret == 1)
559                         return true;
560         }
561
562 exit:
563         /* Return to the host kernel and handle the exit */
564         return false;
565 }
566
567 static inline bool __hyp_text __needs_ssbd_off(struct kvm_vcpu *vcpu)
568 {
569         if (!cpus_have_final_cap(ARM64_SSBD))
570                 return false;
571
572         return !(vcpu->arch.workaround_flags & VCPU_WORKAROUND_2_FLAG);
573 }
574
575 static void __hyp_text __set_guest_arch_workaround_state(struct kvm_vcpu *vcpu)
576 {
577 #ifdef CONFIG_ARM64_SSBD
578         /*
579          * The host runs with the workaround always present. If the
580          * guest wants it disabled, so be it...
581          */
582         if (__needs_ssbd_off(vcpu) &&
583             __hyp_this_cpu_read(arm64_ssbd_callback_required))
584                 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 0, NULL);
585 #endif
586 }
587
588 static void __hyp_text __set_host_arch_workaround_state(struct kvm_vcpu *vcpu)
589 {
590 #ifdef CONFIG_ARM64_SSBD
591         /*
592          * If the guest has disabled the workaround, bring it back on.
593          */
594         if (__needs_ssbd_off(vcpu) &&
595             __hyp_this_cpu_read(arm64_ssbd_callback_required))
596                 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 1, NULL);
597 #endif
598 }
599
600 /**
601  * Disable host events, enable guest events
602  */
603 static bool __hyp_text __pmu_switch_to_guest(struct kvm_cpu_context *host_ctxt)
604 {
605         struct kvm_host_data *host;
606         struct kvm_pmu_events *pmu;
607
608         host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
609         pmu = &host->pmu_events;
610
611         if (pmu->events_host)
612                 write_sysreg(pmu->events_host, pmcntenclr_el0);
613
614         if (pmu->events_guest)
615                 write_sysreg(pmu->events_guest, pmcntenset_el0);
616
617         return (pmu->events_host || pmu->events_guest);
618 }
619
620 /**
621  * Disable guest events, enable host events
622  */
623 static void __hyp_text __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt)
624 {
625         struct kvm_host_data *host;
626         struct kvm_pmu_events *pmu;
627
628         host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
629         pmu = &host->pmu_events;
630
631         if (pmu->events_guest)
632                 write_sysreg(pmu->events_guest, pmcntenclr_el0);
633
634         if (pmu->events_host)
635                 write_sysreg(pmu->events_host, pmcntenset_el0);
636 }
637
638 /* Switch to the guest for VHE systems running in EL2 */
639 static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
640 {
641         struct kvm_cpu_context *host_ctxt;
642         struct kvm_cpu_context *guest_ctxt;
643         u64 exit_code;
644
645         host_ctxt = vcpu->arch.host_cpu_context;
646         host_ctxt->__hyp_running_vcpu = vcpu;
647         guest_ctxt = &vcpu->arch.ctxt;
648
649         sysreg_save_host_state_vhe(host_ctxt);
650
651         /*
652          * ARM erratum 1165522 requires us to configure both stage 1 and
653          * stage 2 translation for the guest context before we clear
654          * HCR_EL2.TGE.
655          *
656          * We have already configured the guest's stage 1 translation in
657          * kvm_vcpu_load_sysregs above.  We must now call __activate_vm
658          * before __activate_traps, because __activate_vm configures
659          * stage 2 translation, and __activate_traps clear HCR_EL2.TGE
660          * (among other things).
661          */
662         __activate_vm(vcpu->kvm);
663         __activate_traps(vcpu);
664
665         sysreg_restore_guest_state_vhe(guest_ctxt);
666         __debug_switch_to_guest(vcpu);
667
668         __set_guest_arch_workaround_state(vcpu);
669
670         do {
671                 /* Jump in the fire! */
672                 exit_code = __guest_enter(vcpu, host_ctxt);
673
674                 /* And we're baaack! */
675         } while (fixup_guest_exit(vcpu, &exit_code));
676
677         __set_host_arch_workaround_state(vcpu);
678
679         sysreg_save_guest_state_vhe(guest_ctxt);
680
681         __deactivate_traps(vcpu);
682
683         sysreg_restore_host_state_vhe(host_ctxt);
684
685         if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
686                 __fpsimd_save_fpexc32(vcpu);
687
688         __debug_switch_to_host(vcpu);
689
690         return exit_code;
691 }
692 NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe);
693
694 int kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
695 {
696         int ret;
697
698         local_daif_mask();
699
700         /*
701          * Having IRQs masked via PMR when entering the guest means the GIC
702          * will not signal the CPU of interrupts of lower priority, and the
703          * only way to get out will be via guest exceptions.
704          * Naturally, we want to avoid this.
705          *
706          * local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a
707          * dsb to ensure the redistributor is forwards EL2 IRQs to the CPU.
708          */
709         pmr_sync();
710
711         ret = __kvm_vcpu_run_vhe(vcpu);
712
713         /*
714          * local_daif_restore() takes care to properly restore PSTATE.DAIF
715          * and the GIC PMR if the host is using IRQ priorities.
716          */
717         local_daif_restore(DAIF_PROCCTX_NOIRQ);
718
719         /*
720          * When we exit from the guest we change a number of CPU configuration
721          * parameters, such as traps.  Make sure these changes take effect
722          * before running the host or additional guests.
723          */
724         isb();
725
726         return ret;
727 }
728
729 /* Switch to the guest for legacy non-VHE systems */
730 int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu)
731 {
732         struct kvm_cpu_context *host_ctxt;
733         struct kvm_cpu_context *guest_ctxt;
734         bool pmu_switch_needed;
735         u64 exit_code;
736
737         /*
738          * Having IRQs masked via PMR when entering the guest means the GIC
739          * will not signal the CPU of interrupts of lower priority, and the
740          * only way to get out will be via guest exceptions.
741          * Naturally, we want to avoid this.
742          */
743         if (system_uses_irq_prio_masking()) {
744                 gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
745                 pmr_sync();
746         }
747
748         vcpu = kern_hyp_va(vcpu);
749
750         host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
751         host_ctxt->__hyp_running_vcpu = vcpu;
752         guest_ctxt = &vcpu->arch.ctxt;
753
754         pmu_switch_needed = __pmu_switch_to_guest(host_ctxt);
755
756         __sysreg_save_state_nvhe(host_ctxt);
757
758         /*
759          * We must restore the 32-bit state before the sysregs, thanks
760          * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
761          *
762          * Also, and in order to be able to deal with erratum #1319537 (A57)
763          * and #1319367 (A72), we must ensure that all VM-related sysreg are
764          * restored before we enable S2 translation.
765          */
766         __sysreg32_restore_state(vcpu);
767         __sysreg_restore_state_nvhe(guest_ctxt);
768
769         __activate_vm(kern_hyp_va(vcpu->kvm));
770         __activate_traps(vcpu);
771
772         __hyp_vgic_restore_state(vcpu);
773         __timer_enable_traps(vcpu);
774
775         __debug_switch_to_guest(vcpu);
776
777         __set_guest_arch_workaround_state(vcpu);
778
779         do {
780                 /* Jump in the fire! */
781                 exit_code = __guest_enter(vcpu, host_ctxt);
782
783                 /* And we're baaack! */
784         } while (fixup_guest_exit(vcpu, &exit_code));
785
786         __set_host_arch_workaround_state(vcpu);
787
788         __sysreg_save_state_nvhe(guest_ctxt);
789         __sysreg32_save_state(vcpu);
790         __timer_disable_traps(vcpu);
791         __hyp_vgic_save_state(vcpu);
792
793         __deactivate_traps(vcpu);
794         __deactivate_vm(vcpu);
795
796         __sysreg_restore_state_nvhe(host_ctxt);
797
798         if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
799                 __fpsimd_save_fpexc32(vcpu);
800
801         /*
802          * This must come after restoring the host sysregs, since a non-VHE
803          * system may enable SPE here and make use of the TTBRs.
804          */
805         __debug_switch_to_host(vcpu);
806
807         if (pmu_switch_needed)
808                 __pmu_switch_to_host(host_ctxt);
809
810         /* Returning to host will clear PSR.I, remask PMR if needed */
811         if (system_uses_irq_prio_masking())
812                 gic_write_pmr(GIC_PRIO_IRQOFF);
813
814         return exit_code;
815 }
816
817 static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
818
819 static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par,
820                                              struct kvm_cpu_context *__host_ctxt)
821 {
822         struct kvm_vcpu *vcpu;
823         unsigned long str_va;
824
825         vcpu = __host_ctxt->__hyp_running_vcpu;
826
827         if (read_sysreg(vttbr_el2)) {
828                 __timer_disable_traps(vcpu);
829                 __deactivate_traps(vcpu);
830                 __deactivate_vm(vcpu);
831                 __sysreg_restore_state_nvhe(__host_ctxt);
832         }
833
834         /*
835          * Force the panic string to be loaded from the literal pool,
836          * making sure it is a kernel address and not a PC-relative
837          * reference.
838          */
839         asm volatile("ldr %0, =__hyp_panic_string" : "=r" (str_va));
840
841         __hyp_do_panic(str_va,
842                        spsr, elr,
843                        read_sysreg(esr_el2), read_sysreg_el2(SYS_FAR),
844                        read_sysreg(hpfar_el2), par, vcpu);
845 }
846
847 static void __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par,
848                                  struct kvm_cpu_context *host_ctxt)
849 {
850         struct kvm_vcpu *vcpu;
851         vcpu = host_ctxt->__hyp_running_vcpu;
852
853         __deactivate_traps(vcpu);
854         sysreg_restore_host_state_vhe(host_ctxt);
855
856         panic(__hyp_panic_string,
857               spsr,  elr,
858               read_sysreg_el2(SYS_ESR),   read_sysreg_el2(SYS_FAR),
859               read_sysreg(hpfar_el2), par, vcpu);
860 }
861 NOKPROBE_SYMBOL(__hyp_call_panic_vhe);
862
863 void __hyp_text __noreturn hyp_panic(struct kvm_cpu_context *host_ctxt)
864 {
865         u64 spsr = read_sysreg_el2(SYS_SPSR);
866         u64 elr = read_sysreg_el2(SYS_ELR);
867         u64 par = read_sysreg(par_el1);
868
869         if (!has_vhe())
870                 __hyp_call_panic_nvhe(spsr, elr, par, host_ctxt);
871         else
872                 __hyp_call_panic_vhe(spsr, elr, par, host_ctxt);
873
874         unreachable();
875 }