arm64: KVM: Enable access to TRBE support for host
[linux-2.6-microblaze.git] / arch / arm64 / kvm / hyp / nvhe / 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 <hyp/adjust_pc.h>
8 #include <hyp/switch.h>
9 #include <hyp/sysreg-sr.h>
10
11 #include <linux/arm-smccc.h>
12 #include <linux/kvm_host.h>
13 #include <linux/types.h>
14 #include <linux/jump_label.h>
15 #include <uapi/linux/psci.h>
16
17 #include <kvm/arm_psci.h>
18
19 #include <asm/barrier.h>
20 #include <asm/cpufeature.h>
21 #include <asm/kprobes.h>
22 #include <asm/kvm_asm.h>
23 #include <asm/kvm_emulate.h>
24 #include <asm/kvm_hyp.h>
25 #include <asm/kvm_mmu.h>
26 #include <asm/fpsimd.h>
27 #include <asm/debug-monitors.h>
28 #include <asm/processor.h>
29 #include <asm/thread_info.h>
30
31 /* Non-VHE specific context */
32 DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
33 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
34 DEFINE_PER_CPU(unsigned long, kvm_hyp_vector);
35
36 static void __activate_traps(struct kvm_vcpu *vcpu)
37 {
38         u64 val;
39
40         ___activate_traps(vcpu);
41         __activate_traps_common(vcpu);
42
43         val = CPTR_EL2_DEFAULT;
44         val |= CPTR_EL2_TTA | CPTR_EL2_TZ | CPTR_EL2_TAM;
45         if (!update_fp_enabled(vcpu)) {
46                 val |= CPTR_EL2_TFP;
47                 __activate_traps_fpsimd32(vcpu);
48         }
49
50         write_sysreg(val, cptr_el2);
51         write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
52
53         if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
54                 struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt;
55
56                 isb();
57                 /*
58                  * At this stage, and thanks to the above isb(), S2 is
59                  * configured and enabled. We can now restore the guest's S1
60                  * configuration: SCTLR, and only then TCR.
61                  */
62                 write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1), SYS_SCTLR);
63                 isb();
64                 write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1),   SYS_TCR);
65         }
66 }
67
68 static void __deactivate_traps(struct kvm_vcpu *vcpu)
69 {
70         extern char __kvm_hyp_host_vector[];
71         u64 mdcr_el2;
72
73         ___deactivate_traps(vcpu);
74
75         mdcr_el2 = read_sysreg(mdcr_el2);
76
77         if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
78                 u64 val;
79
80                 /*
81                  * Set the TCR and SCTLR registers in the exact opposite
82                  * sequence as __activate_traps (first prevent walks,
83                  * then force the MMU on). A generous sprinkling of isb()
84                  * ensure that things happen in this exact order.
85                  */
86                 val = read_sysreg_el1(SYS_TCR);
87                 write_sysreg_el1(val | TCR_EPD1_MASK | TCR_EPD0_MASK, SYS_TCR);
88                 isb();
89                 val = read_sysreg_el1(SYS_SCTLR);
90                 write_sysreg_el1(val | SCTLR_ELx_M, SYS_SCTLR);
91                 isb();
92         }
93
94         __deactivate_traps_common();
95
96         mdcr_el2 &= MDCR_EL2_HPMN_MASK;
97         mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
98         mdcr_el2 |= MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT;
99
100         write_sysreg(mdcr_el2, mdcr_el2);
101         if (is_protected_kvm_enabled())
102                 write_sysreg(HCR_HOST_NVHE_PROTECTED_FLAGS, hcr_el2);
103         else
104                 write_sysreg(HCR_HOST_NVHE_FLAGS, hcr_el2);
105         write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
106         write_sysreg(__kvm_hyp_host_vector, vbar_el2);
107 }
108
109 static void __load_host_stage2(void)
110 {
111         write_sysreg(0, vttbr_el2);
112 }
113
114 /* Save VGICv3 state on non-VHE systems */
115 static void __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
116 {
117         if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
118                 __vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
119                 __vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
120         }
121 }
122
123 /* Restore VGICv3 state on non_VEH systems */
124 static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
125 {
126         if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
127                 __vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
128                 __vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
129         }
130 }
131
132 /**
133  * Disable host events, enable guest events
134  */
135 static bool __pmu_switch_to_guest(struct kvm_cpu_context *host_ctxt)
136 {
137         struct kvm_host_data *host;
138         struct kvm_pmu_events *pmu;
139
140         host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
141         pmu = &host->pmu_events;
142
143         if (pmu->events_host)
144                 write_sysreg(pmu->events_host, pmcntenclr_el0);
145
146         if (pmu->events_guest)
147                 write_sysreg(pmu->events_guest, pmcntenset_el0);
148
149         return (pmu->events_host || pmu->events_guest);
150 }
151
152 /**
153  * Disable guest events, enable host events
154  */
155 static void __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt)
156 {
157         struct kvm_host_data *host;
158         struct kvm_pmu_events *pmu;
159
160         host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
161         pmu = &host->pmu_events;
162
163         if (pmu->events_guest)
164                 write_sysreg(pmu->events_guest, pmcntenclr_el0);
165
166         if (pmu->events_host)
167                 write_sysreg(pmu->events_host, pmcntenset_el0);
168 }
169
170 /* Switch to the guest for legacy non-VHE systems */
171 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
172 {
173         struct kvm_cpu_context *host_ctxt;
174         struct kvm_cpu_context *guest_ctxt;
175         bool pmu_switch_needed;
176         u64 exit_code;
177
178         /*
179          * Having IRQs masked via PMR when entering the guest means the GIC
180          * will not signal the CPU of interrupts of lower priority, and the
181          * only way to get out will be via guest exceptions.
182          * Naturally, we want to avoid this.
183          */
184         if (system_uses_irq_prio_masking()) {
185                 gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
186                 pmr_sync();
187         }
188
189         host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
190         host_ctxt->__hyp_running_vcpu = vcpu;
191         guest_ctxt = &vcpu->arch.ctxt;
192
193         pmu_switch_needed = __pmu_switch_to_guest(host_ctxt);
194
195         __sysreg_save_state_nvhe(host_ctxt);
196         /*
197          * We must flush and disable the SPE buffer for nVHE, as
198          * the translation regime(EL1&0) is going to be loaded with
199          * that of the guest. And we must do this before we change the
200          * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
201          * before we load guest Stage1.
202          */
203         __debug_save_host_buffers_nvhe(vcpu);
204
205         __adjust_pc(vcpu);
206
207         /*
208          * We must restore the 32-bit state before the sysregs, thanks
209          * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
210          *
211          * Also, and in order to be able to deal with erratum #1319537 (A57)
212          * and #1319367 (A72), we must ensure that all VM-related sysreg are
213          * restored before we enable S2 translation.
214          */
215         __sysreg32_restore_state(vcpu);
216         __sysreg_restore_state_nvhe(guest_ctxt);
217
218         __load_guest_stage2(kern_hyp_va(vcpu->arch.hw_mmu));
219         __activate_traps(vcpu);
220
221         __hyp_vgic_restore_state(vcpu);
222         __timer_enable_traps(vcpu);
223
224         __debug_switch_to_guest(vcpu);
225
226         do {
227                 /* Jump in the fire! */
228                 exit_code = __guest_enter(vcpu);
229
230                 /* And we're baaack! */
231         } while (fixup_guest_exit(vcpu, &exit_code));
232
233         __sysreg_save_state_nvhe(guest_ctxt);
234         __sysreg32_save_state(vcpu);
235         __timer_disable_traps(vcpu);
236         __hyp_vgic_save_state(vcpu);
237
238         __deactivate_traps(vcpu);
239         __load_host_stage2();
240
241         __sysreg_restore_state_nvhe(host_ctxt);
242
243         if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED)
244                 __fpsimd_save_fpexc32(vcpu);
245
246         __debug_switch_to_host(vcpu);
247         /*
248          * This must come after restoring the host sysregs, since a non-VHE
249          * system may enable SPE here and make use of the TTBRs.
250          */
251         __debug_restore_host_buffers_nvhe(vcpu);
252
253         if (pmu_switch_needed)
254                 __pmu_switch_to_host(host_ctxt);
255
256         /* Returning to host will clear PSR.I, remask PMR if needed */
257         if (system_uses_irq_prio_masking())
258                 gic_write_pmr(GIC_PRIO_IRQOFF);
259
260         host_ctxt->__hyp_running_vcpu = NULL;
261
262         return exit_code;
263 }
264
265 void __noreturn hyp_panic(void)
266 {
267         u64 spsr = read_sysreg_el2(SYS_SPSR);
268         u64 elr = read_sysreg_el2(SYS_ELR);
269         u64 par = read_sysreg_par();
270         struct kvm_cpu_context *host_ctxt;
271         struct kvm_vcpu *vcpu;
272
273         host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
274         vcpu = host_ctxt->__hyp_running_vcpu;
275
276         if (vcpu) {
277                 __timer_disable_traps(vcpu);
278                 __deactivate_traps(vcpu);
279                 __load_host_stage2();
280                 __sysreg_restore_state_nvhe(host_ctxt);
281         }
282
283         __hyp_do_panic(host_ctxt, spsr, elr, par);
284         unreachable();
285 }
286
287 asmlinkage void kvm_unexpected_el2_exception(void)
288 {
289         return __kvm_unexpected_el2_exception();
290 }