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 #ifndef __ARM64_KVM_HYP_SWITCH_H__
8 #define __ARM64_KVM_HYP_SWITCH_H__
9
10 #include <hyp/adjust_pc.h>
11 #include <hyp/fault.h>
12
13 #include <linux/arm-smccc.h>
14 #include <linux/kvm_host.h>
15 #include <linux/types.h>
16 #include <linux/jump_label.h>
17 #include <uapi/linux/psci.h>
18
19 #include <kvm/arm_psci.h>
20
21 #include <asm/barrier.h>
22 #include <asm/cpufeature.h>
23 #include <asm/extable.h>
24 #include <asm/kprobes.h>
25 #include <asm/kvm_asm.h>
26 #include <asm/kvm_emulate.h>
27 #include <asm/kvm_hyp.h>
28 #include <asm/kvm_mmu.h>
29 #include <asm/kvm_nested.h>
30 #include <asm/fpsimd.h>
31 #include <asm/debug-monitors.h>
32 #include <asm/processor.h>
33 #include <asm/traps.h>
34
35 struct kvm_exception_table_entry {
36 int insn, fixup;
37 };
38
39 extern struct kvm_exception_table_entry __start___kvm_ex_table;
40 extern struct kvm_exception_table_entry __stop___kvm_ex_table;
41
42 /* Save the 32-bit only FPSIMD system register state */
__fpsimd_save_fpexc32(struct kvm_vcpu * vcpu)43 static inline void __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
44 {
45 if (!vcpu_el1_is_32bit(vcpu))
46 return;
47
48 __vcpu_sys_reg(vcpu, FPEXC32_EL2) = read_sysreg(fpexc32_el2);
49 }
50
__activate_traps_fpsimd32(struct kvm_vcpu * vcpu)51 static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
52 {
53 /*
54 * We are about to set CPTR_EL2.TFP to trap all floating point
55 * register accesses to EL2, however, the ARM ARM clearly states that
56 * traps are only taken to EL2 if the operation would not otherwise
57 * trap to EL1. Therefore, always make sure that for 32-bit guests,
58 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
59 * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
60 * it will cause an exception.
61 */
62 if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) {
63 write_sysreg(1 << 30, fpexc32_el2);
64 isb();
65 }
66 }
67
68 #define compute_clr_set(vcpu, reg, clr, set) \
69 do { \
70 u64 hfg; \
71 hfg = __vcpu_sys_reg(vcpu, reg) & ~__ ## reg ## _RES0; \
72 set |= hfg & __ ## reg ## _MASK; \
73 clr |= ~hfg & __ ## reg ## _nMASK; \
74 } while(0)
75
76 #define reg_to_fgt_group_id(reg) \
77 ({ \
78 enum fgt_group_id id; \
79 switch(reg) { \
80 case HFGRTR_EL2: \
81 case HFGWTR_EL2: \
82 id = HFGxTR_GROUP; \
83 break; \
84 case HFGITR_EL2: \
85 id = HFGITR_GROUP; \
86 break; \
87 case HDFGRTR_EL2: \
88 case HDFGWTR_EL2: \
89 id = HDFGRTR_GROUP; \
90 break; \
91 case HAFGRTR_EL2: \
92 id = HAFGRTR_GROUP; \
93 break; \
94 default: \
95 BUILD_BUG_ON(1); \
96 } \
97 \
98 id; \
99 })
100
101 #define compute_undef_clr_set(vcpu, kvm, reg, clr, set) \
102 do { \
103 u64 hfg = kvm->arch.fgu[reg_to_fgt_group_id(reg)]; \
104 set |= hfg & __ ## reg ## _MASK; \
105 clr |= hfg & __ ## reg ## _nMASK; \
106 } while(0)
107
108 #define update_fgt_traps_cs(hctxt, vcpu, kvm, reg, clr, set) \
109 do { \
110 u64 c = 0, s = 0; \
111 \
112 ctxt_sys_reg(hctxt, reg) = read_sysreg_s(SYS_ ## reg); \
113 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) \
114 compute_clr_set(vcpu, reg, c, s); \
115 \
116 compute_undef_clr_set(vcpu, kvm, reg, c, s); \
117 \
118 s |= set; \
119 c |= clr; \
120 if (c || s) { \
121 u64 val = __ ## reg ## _nMASK; \
122 val |= s; \
123 val &= ~c; \
124 write_sysreg_s(val, SYS_ ## reg); \
125 } \
126 } while(0)
127
128 #define update_fgt_traps(hctxt, vcpu, kvm, reg) \
129 update_fgt_traps_cs(hctxt, vcpu, kvm, reg, 0, 0)
130
131 /*
132 * Validate the fine grain trap masks.
133 * Check that the masks do not overlap and that all bits are accounted for.
134 */
135 #define CHECK_FGT_MASKS(reg) \
136 do { \
137 BUILD_BUG_ON((__ ## reg ## _MASK) & (__ ## reg ## _nMASK)); \
138 BUILD_BUG_ON(~((__ ## reg ## _RES0) ^ (__ ## reg ## _MASK) ^ \
139 (__ ## reg ## _nMASK))); \
140 } while(0)
141
cpu_has_amu(void)142 static inline bool cpu_has_amu(void)
143 {
144 u64 pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1);
145
146 return cpuid_feature_extract_unsigned_field(pfr0,
147 ID_AA64PFR0_EL1_AMU_SHIFT);
148 }
149
__activate_traps_hfgxtr(struct kvm_vcpu * vcpu)150 static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
151 {
152 struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
153 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
154
155 CHECK_FGT_MASKS(HFGRTR_EL2);
156 CHECK_FGT_MASKS(HFGWTR_EL2);
157 CHECK_FGT_MASKS(HFGITR_EL2);
158 CHECK_FGT_MASKS(HDFGRTR_EL2);
159 CHECK_FGT_MASKS(HDFGWTR_EL2);
160 CHECK_FGT_MASKS(HAFGRTR_EL2);
161 CHECK_FGT_MASKS(HCRX_EL2);
162
163 if (!cpus_have_final_cap(ARM64_HAS_FGT))
164 return;
165
166 update_fgt_traps(hctxt, vcpu, kvm, HFGRTR_EL2);
167 update_fgt_traps_cs(hctxt, vcpu, kvm, HFGWTR_EL2, 0,
168 cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38) ?
169 HFGxTR_EL2_TCR_EL1_MASK : 0);
170 update_fgt_traps(hctxt, vcpu, kvm, HFGITR_EL2);
171 update_fgt_traps(hctxt, vcpu, kvm, HDFGRTR_EL2);
172 update_fgt_traps(hctxt, vcpu, kvm, HDFGWTR_EL2);
173
174 if (cpu_has_amu())
175 update_fgt_traps(hctxt, vcpu, kvm, HAFGRTR_EL2);
176 }
177
178 #define __deactivate_fgt(htcxt, vcpu, kvm, reg) \
179 do { \
180 if ((vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) || \
181 kvm->arch.fgu[reg_to_fgt_group_id(reg)]) \
182 write_sysreg_s(ctxt_sys_reg(hctxt, reg), \
183 SYS_ ## reg); \
184 } while(0)
185
__deactivate_traps_hfgxtr(struct kvm_vcpu * vcpu)186 static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
187 {
188 struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
189 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
190
191 if (!cpus_have_final_cap(ARM64_HAS_FGT))
192 return;
193
194 __deactivate_fgt(hctxt, vcpu, kvm, HFGRTR_EL2);
195 if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38))
196 write_sysreg_s(ctxt_sys_reg(hctxt, HFGWTR_EL2), SYS_HFGWTR_EL2);
197 else
198 __deactivate_fgt(hctxt, vcpu, kvm, HFGWTR_EL2);
199 __deactivate_fgt(hctxt, vcpu, kvm, HFGITR_EL2);
200 __deactivate_fgt(hctxt, vcpu, kvm, HDFGRTR_EL2);
201 __deactivate_fgt(hctxt, vcpu, kvm, HDFGWTR_EL2);
202
203 if (cpu_has_amu())
204 __deactivate_fgt(hctxt, vcpu, kvm, HAFGRTR_EL2);
205 }
206
__activate_traps_mpam(struct kvm_vcpu * vcpu)207 static inline void __activate_traps_mpam(struct kvm_vcpu *vcpu)
208 {
209 u64 r = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
210
211 if (!system_supports_mpam())
212 return;
213
214 /* trap guest access to MPAMIDR_EL1 */
215 if (system_supports_mpam_hcr()) {
216 write_sysreg_s(MPAMHCR_EL2_TRAP_MPAMIDR_EL1, SYS_MPAMHCR_EL2);
217 } else {
218 /* From v1.1 TIDR can trap MPAMIDR, set it unconditionally */
219 r |= MPAM2_EL2_TIDR;
220 }
221
222 write_sysreg_s(r, SYS_MPAM2_EL2);
223 }
224
__deactivate_traps_mpam(void)225 static inline void __deactivate_traps_mpam(void)
226 {
227 if (!system_supports_mpam())
228 return;
229
230 write_sysreg_s(MPAM2_HOST_FLAGS, SYS_MPAM2_EL2);
231
232 if (system_supports_mpam_hcr())
233 write_sysreg_s(MPAMHCR_HOST_FLAGS, SYS_MPAMHCR_EL2);
234 }
235
__activate_traps_common(struct kvm_vcpu * vcpu)236 static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
237 {
238 /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
239 write_sysreg(1 << 15, hstr_el2);
240
241 /*
242 * Make sure we trap PMU access from EL0 to EL2. Also sanitize
243 * PMSELR_EL0 to make sure it never contains the cycle
244 * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
245 * EL1 instead of being trapped to EL2.
246 */
247 if (kvm_arm_support_pmu_v3()) {
248 struct kvm_cpu_context *hctxt;
249
250 write_sysreg(0, pmselr_el0);
251
252 hctxt = host_data_ptr(host_ctxt);
253 ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0);
254 write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
255 vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
256 }
257
258 *host_data_ptr(host_debug_state.mdcr_el2) = read_sysreg(mdcr_el2);
259 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
260 }
261
__activate_traps_hcrx(struct kvm_vcpu * vcpu)262 static inline void __activate_traps_hcrx(struct kvm_vcpu *vcpu)
263 {
264 u64 hcrx = vcpu->arch.hcrx_el2;
265
266 if (!cpus_have_final_cap(ARM64_HAS_HCX))
267 return;
268
269 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
270 u64 clr = 0, set = 0;
271
272 compute_clr_set(vcpu, HCRX_EL2, clr, set);
273
274 hcrx |= set;
275 hcrx &= ~clr;
276 }
277
278 write_sysreg_s(hcrx, SYS_HCRX_EL2);
279 __activate_traps_mpam(vcpu);
280 }
281
__deactivate_traps_common(struct kvm_vcpu * vcpu)282 static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
283 {
284 write_sysreg(*host_data_ptr(host_debug_state.mdcr_el2), mdcr_el2);
285
286 write_sysreg(0, hstr_el2);
287 if (kvm_arm_support_pmu_v3()) {
288 struct kvm_cpu_context *hctxt;
289
290 hctxt = host_data_ptr(host_ctxt);
291 write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0);
292 vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU);
293 }
294
295 if (cpus_have_final_cap(ARM64_HAS_HCX))
296 write_sysreg_s(HCRX_HOST_FLAGS, SYS_HCRX_EL2);
297 __deactivate_traps_mpam();
298 }
299
___activate_traps(struct kvm_vcpu * vcpu,u64 hcr)300 static inline void ___activate_traps(struct kvm_vcpu *vcpu, u64 hcr)
301 {
302 if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM))
303 hcr |= HCR_TVM;
304
305 write_sysreg(hcr, hcr_el2);
306
307 if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE))
308 write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
309 }
310
___deactivate_traps(struct kvm_vcpu * vcpu)311 static inline void ___deactivate_traps(struct kvm_vcpu *vcpu)
312 {
313 /*
314 * If we pended a virtual abort, preserve it until it gets
315 * cleared. See D1.14.3 (Virtual Interrupts) for details, but
316 * the crucial bit is "On taking a vSError interrupt,
317 * HCR_EL2.VSE is cleared to 0."
318 */
319 if (vcpu->arch.hcr_el2 & HCR_VSE) {
320 vcpu->arch.hcr_el2 &= ~HCR_VSE;
321 vcpu->arch.hcr_el2 |= read_sysreg(hcr_el2) & HCR_VSE;
322 }
323 }
324
__populate_fault_info(struct kvm_vcpu * vcpu)325 static inline bool __populate_fault_info(struct kvm_vcpu *vcpu)
326 {
327 return __get_fault_info(vcpu->arch.fault.esr_el2, &vcpu->arch.fault);
328 }
329
kvm_hyp_handle_mops(struct kvm_vcpu * vcpu,u64 * exit_code)330 static inline bool kvm_hyp_handle_mops(struct kvm_vcpu *vcpu, u64 *exit_code)
331 {
332 *vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
333 arm64_mops_reset_regs(vcpu_gp_regs(vcpu), vcpu->arch.fault.esr_el2);
334 write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
335
336 /*
337 * Finish potential single step before executing the prologue
338 * instruction.
339 */
340 *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
341 write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
342
343 return true;
344 }
345
__hyp_sve_restore_guest(struct kvm_vcpu * vcpu)346 static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu)
347 {
348 /*
349 * The vCPU's saved SVE state layout always matches the max VL of the
350 * vCPU. Start off with the max VL so we can load the SVE state.
351 */
352 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL2);
353 __sve_restore_state(vcpu_sve_pffr(vcpu),
354 &vcpu->arch.ctxt.fp_regs.fpsr,
355 true);
356
357 /*
358 * The effective VL for a VM could differ from the max VL when running a
359 * nested guest, as the guest hypervisor could select a smaller VL. Slap
360 * that into hardware before wrapping up.
361 */
362 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))
363 sve_cond_update_zcr_vq(__vcpu_sys_reg(vcpu, ZCR_EL2), SYS_ZCR_EL2);
364
365 write_sysreg_el1(__vcpu_sys_reg(vcpu, vcpu_sve_zcr_elx(vcpu)), SYS_ZCR);
366 }
367
__hyp_sve_save_host(void)368 static inline void __hyp_sve_save_host(void)
369 {
370 struct cpu_sve_state *sve_state = *host_data_ptr(sve_state);
371
372 sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR);
373 write_sysreg_s(sve_vq_from_vl(kvm_host_sve_max_vl) - 1, SYS_ZCR_EL2);
374 __sve_save_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl),
375 &sve_state->fpsr,
376 true);
377 }
378
fpsimd_lazy_switch_to_guest(struct kvm_vcpu * vcpu)379 static inline void fpsimd_lazy_switch_to_guest(struct kvm_vcpu *vcpu)
380 {
381 u64 zcr_el1, zcr_el2;
382
383 if (!guest_owns_fp_regs())
384 return;
385
386 if (vcpu_has_sve(vcpu)) {
387 /* A guest hypervisor may restrict the effective max VL. */
388 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))
389 zcr_el2 = __vcpu_sys_reg(vcpu, ZCR_EL2);
390 else
391 zcr_el2 = vcpu_sve_max_vq(vcpu) - 1;
392
393 write_sysreg_el2(zcr_el2, SYS_ZCR);
394
395 zcr_el1 = __vcpu_sys_reg(vcpu, vcpu_sve_zcr_elx(vcpu));
396 write_sysreg_el1(zcr_el1, SYS_ZCR);
397 }
398 }
399
fpsimd_lazy_switch_to_host(struct kvm_vcpu * vcpu)400 static inline void fpsimd_lazy_switch_to_host(struct kvm_vcpu *vcpu)
401 {
402 u64 zcr_el1, zcr_el2;
403
404 if (!guest_owns_fp_regs())
405 return;
406
407 /*
408 * When the guest owns the FP regs, we know that guest+hyp traps for
409 * any FPSIMD/SVE/SME features exposed to the guest have been disabled
410 * by either fpsimd_lazy_switch_to_guest() or kvm_hyp_handle_fpsimd()
411 * prior to __guest_entry(). As __guest_entry() guarantees a context
412 * synchronization event, we don't need an ISB here to avoid taking
413 * traps for anything that was exposed to the guest.
414 */
415 if (vcpu_has_sve(vcpu)) {
416 zcr_el1 = read_sysreg_el1(SYS_ZCR);
417 __vcpu_sys_reg(vcpu, vcpu_sve_zcr_elx(vcpu)) = zcr_el1;
418
419 /*
420 * The guest's state is always saved using the guest's max VL.
421 * Ensure that the host has the guest's max VL active such that
422 * the host can save the guest's state lazily, but don't
423 * artificially restrict the host to the guest's max VL.
424 */
425 if (has_vhe()) {
426 zcr_el2 = vcpu_sve_max_vq(vcpu) - 1;
427 write_sysreg_el2(zcr_el2, SYS_ZCR);
428 } else {
429 zcr_el2 = sve_vq_from_vl(kvm_host_sve_max_vl) - 1;
430 write_sysreg_el2(zcr_el2, SYS_ZCR);
431
432 zcr_el1 = vcpu_sve_max_vq(vcpu) - 1;
433 write_sysreg_el1(zcr_el1, SYS_ZCR);
434 }
435 }
436 }
437
kvm_hyp_save_fpsimd_host(struct kvm_vcpu * vcpu)438 static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
439 {
440 /*
441 * Non-protected kvm relies on the host restoring its sve state.
442 * Protected kvm restores the host's sve state as not to reveal that
443 * fpsimd was used by a guest nor leak upper sve bits.
444 */
445 if (system_supports_sve()) {
446 __hyp_sve_save_host();
447
448 /* Re-enable SVE traps if not supported for the guest vcpu. */
449 if (!vcpu_has_sve(vcpu))
450 cpacr_clear_set(CPACR_ELx_ZEN, 0);
451
452 } else {
453 __fpsimd_save_state(host_data_ptr(host_ctxt.fp_regs));
454 }
455
456 if (kvm_has_fpmr(kern_hyp_va(vcpu->kvm)))
457 *host_data_ptr(fpmr) = read_sysreg_s(SYS_FPMR);
458 }
459
460 /*
461 * We trap the first access to the FP/SIMD to save the host context and
462 * restore the guest context lazily.
463 * If FP/SIMD is not implemented, handle the trap and inject an undefined
464 * instruction exception to the guest. Similarly for trapped SVE accesses.
465 */
kvm_hyp_handle_fpsimd(struct kvm_vcpu * vcpu,u64 * exit_code)466 static inline bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
467 {
468 bool sve_guest;
469 u8 esr_ec;
470
471 if (!system_supports_fpsimd())
472 return false;
473
474 sve_guest = vcpu_has_sve(vcpu);
475 esr_ec = kvm_vcpu_trap_get_class(vcpu);
476
477 /* Only handle traps the vCPU can support here: */
478 switch (esr_ec) {
479 case ESR_ELx_EC_FP_ASIMD:
480 /* Forward traps to the guest hypervisor as required */
481 if (guest_hyp_fpsimd_traps_enabled(vcpu))
482 return false;
483 break;
484 case ESR_ELx_EC_SYS64:
485 if (WARN_ON_ONCE(!is_hyp_ctxt(vcpu)))
486 return false;
487 fallthrough;
488 case ESR_ELx_EC_SVE:
489 if (!sve_guest)
490 return false;
491 if (guest_hyp_sve_traps_enabled(vcpu))
492 return false;
493 break;
494 default:
495 return false;
496 }
497
498 /* Valid trap. Switch the context: */
499
500 /* First disable enough traps to allow us to update the registers */
501 if (sve_guest || (is_protected_kvm_enabled() && system_supports_sve()))
502 cpacr_clear_set(0, CPACR_ELx_FPEN | CPACR_ELx_ZEN);
503 else
504 cpacr_clear_set(0, CPACR_ELx_FPEN);
505 isb();
506
507 /* Write out the host state if it's in the registers */
508 if (is_protected_kvm_enabled() && host_owns_fp_regs())
509 kvm_hyp_save_fpsimd_host(vcpu);
510
511 /* Restore the guest state */
512 if (sve_guest)
513 __hyp_sve_restore_guest(vcpu);
514 else
515 __fpsimd_restore_state(&vcpu->arch.ctxt.fp_regs);
516
517 if (kvm_has_fpmr(kern_hyp_va(vcpu->kvm)))
518 write_sysreg_s(__vcpu_sys_reg(vcpu, FPMR), SYS_FPMR);
519
520 /* Skip restoring fpexc32 for AArch64 guests */
521 if (!(read_sysreg(hcr_el2) & HCR_RW))
522 write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);
523
524 *host_data_ptr(fp_owner) = FP_STATE_GUEST_OWNED;
525
526 return true;
527 }
528
handle_tx2_tvm(struct kvm_vcpu * vcpu)529 static inline bool handle_tx2_tvm(struct kvm_vcpu *vcpu)
530 {
531 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
532 int rt = kvm_vcpu_sys_get_rt(vcpu);
533 u64 val = vcpu_get_reg(vcpu, rt);
534
535 /*
536 * The normal sysreg handling code expects to see the traps,
537 * let's not do anything here.
538 */
539 if (vcpu->arch.hcr_el2 & HCR_TVM)
540 return false;
541
542 switch (sysreg) {
543 case SYS_SCTLR_EL1:
544 write_sysreg_el1(val, SYS_SCTLR);
545 break;
546 case SYS_TTBR0_EL1:
547 write_sysreg_el1(val, SYS_TTBR0);
548 break;
549 case SYS_TTBR1_EL1:
550 write_sysreg_el1(val, SYS_TTBR1);
551 break;
552 case SYS_TCR_EL1:
553 write_sysreg_el1(val, SYS_TCR);
554 break;
555 case SYS_ESR_EL1:
556 write_sysreg_el1(val, SYS_ESR);
557 break;
558 case SYS_FAR_EL1:
559 write_sysreg_el1(val, SYS_FAR);
560 break;
561 case SYS_AFSR0_EL1:
562 write_sysreg_el1(val, SYS_AFSR0);
563 break;
564 case SYS_AFSR1_EL1:
565 write_sysreg_el1(val, SYS_AFSR1);
566 break;
567 case SYS_MAIR_EL1:
568 write_sysreg_el1(val, SYS_MAIR);
569 break;
570 case SYS_AMAIR_EL1:
571 write_sysreg_el1(val, SYS_AMAIR);
572 break;
573 case SYS_CONTEXTIDR_EL1:
574 write_sysreg_el1(val, SYS_CONTEXTIDR);
575 break;
576 default:
577 return false;
578 }
579
580 __kvm_skip_instr(vcpu);
581 return true;
582 }
583
kvm_hyp_handle_cntpct(struct kvm_vcpu * vcpu)584 static bool kvm_hyp_handle_cntpct(struct kvm_vcpu *vcpu)
585 {
586 struct arch_timer_context *ctxt;
587 u32 sysreg;
588 u64 val;
589
590 /*
591 * We only get here for 64bit guests, 32bit guests will hit
592 * the long and winding road all the way to the standard
593 * handling. Yes, it sucks to be irrelevant.
594 */
595 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
596
597 switch (sysreg) {
598 case SYS_CNTPCT_EL0:
599 case SYS_CNTPCTSS_EL0:
600 if (vcpu_has_nv(vcpu)) {
601 if (is_hyp_ctxt(vcpu)) {
602 ctxt = vcpu_hptimer(vcpu);
603 break;
604 }
605
606 /* Check for guest hypervisor trapping */
607 val = __vcpu_sys_reg(vcpu, CNTHCTL_EL2);
608 if (!vcpu_el2_e2h_is_set(vcpu))
609 val = (val & CNTHCTL_EL1PCTEN) << 10;
610
611 if (!(val & (CNTHCTL_EL1PCTEN << 10)))
612 return false;
613 }
614
615 ctxt = vcpu_ptimer(vcpu);
616 break;
617 default:
618 return false;
619 }
620
621 val = arch_timer_read_cntpct_el0();
622
623 if (ctxt->offset.vm_offset)
624 val -= *kern_hyp_va(ctxt->offset.vm_offset);
625 if (ctxt->offset.vcpu_offset)
626 val -= *kern_hyp_va(ctxt->offset.vcpu_offset);
627
628 vcpu_set_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu), val);
629 __kvm_skip_instr(vcpu);
630 return true;
631 }
632
handle_ampere1_tcr(struct kvm_vcpu * vcpu)633 static bool handle_ampere1_tcr(struct kvm_vcpu *vcpu)
634 {
635 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
636 int rt = kvm_vcpu_sys_get_rt(vcpu);
637 u64 val = vcpu_get_reg(vcpu, rt);
638
639 if (sysreg != SYS_TCR_EL1)
640 return false;
641
642 /*
643 * Affected parts do not advertise support for hardware Access Flag /
644 * Dirty state management in ID_AA64MMFR1_EL1.HAFDBS, but the underlying
645 * control bits are still functional. The architecture requires these be
646 * RES0 on systems that do not implement FEAT_HAFDBS.
647 *
648 * Uphold the requirements of the architecture by masking guest writes
649 * to TCR_EL1.{HA,HD} here.
650 */
651 val &= ~(TCR_HD | TCR_HA);
652 write_sysreg_el1(val, SYS_TCR);
653 __kvm_skip_instr(vcpu);
654 return true;
655 }
656
kvm_hyp_handle_sysreg(struct kvm_vcpu * vcpu,u64 * exit_code)657 static inline bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)
658 {
659 if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) &&
660 handle_tx2_tvm(vcpu))
661 return true;
662
663 if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38) &&
664 handle_ampere1_tcr(vcpu))
665 return true;
666
667 if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
668 __vgic_v3_perform_cpuif_access(vcpu) == 1)
669 return true;
670
671 if (kvm_hyp_handle_cntpct(vcpu))
672 return true;
673
674 return false;
675 }
676
kvm_hyp_handle_cp15_32(struct kvm_vcpu * vcpu,u64 * exit_code)677 static inline bool kvm_hyp_handle_cp15_32(struct kvm_vcpu *vcpu, u64 *exit_code)
678 {
679 if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
680 __vgic_v3_perform_cpuif_access(vcpu) == 1)
681 return true;
682
683 return false;
684 }
685
kvm_hyp_handle_memory_fault(struct kvm_vcpu * vcpu,u64 * exit_code)686 static inline bool kvm_hyp_handle_memory_fault(struct kvm_vcpu *vcpu,
687 u64 *exit_code)
688 {
689 if (!__populate_fault_info(vcpu))
690 return true;
691
692 return false;
693 }
694 #define kvm_hyp_handle_iabt_low kvm_hyp_handle_memory_fault
695 #define kvm_hyp_handle_watchpt_low kvm_hyp_handle_memory_fault
696
kvm_hyp_handle_dabt_low(struct kvm_vcpu * vcpu,u64 * exit_code)697 static inline bool kvm_hyp_handle_dabt_low(struct kvm_vcpu *vcpu, u64 *exit_code)
698 {
699 if (kvm_hyp_handle_memory_fault(vcpu, exit_code))
700 return true;
701
702 if (static_branch_unlikely(&vgic_v2_cpuif_trap)) {
703 bool valid;
704
705 valid = kvm_vcpu_trap_is_translation_fault(vcpu) &&
706 kvm_vcpu_dabt_isvalid(vcpu) &&
707 !kvm_vcpu_abt_issea(vcpu) &&
708 !kvm_vcpu_abt_iss1tw(vcpu);
709
710 if (valid) {
711 int ret = __vgic_v2_perform_cpuif_access(vcpu);
712
713 if (ret == 1)
714 return true;
715
716 /* Promote an illegal access to an SError.*/
717 if (ret == -1)
718 *exit_code = ARM_EXCEPTION_EL1_SERROR;
719 }
720 }
721
722 return false;
723 }
724
725 typedef bool (*exit_handler_fn)(struct kvm_vcpu *, u64 *);
726
727 /*
728 * Allow the hypervisor to handle the exit with an exit handler if it has one.
729 *
730 * Returns true if the hypervisor handled the exit, and control should go back
731 * to the guest, or false if it hasn't.
732 */
kvm_hyp_handle_exit(struct kvm_vcpu * vcpu,u64 * exit_code,const exit_handler_fn * handlers)733 static inline bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu, u64 *exit_code,
734 const exit_handler_fn *handlers)
735 {
736 exit_handler_fn fn = handlers[kvm_vcpu_trap_get_class(vcpu)];
737 if (fn)
738 return fn(vcpu, exit_code);
739
740 return false;
741 }
742
synchronize_vcpu_pstate(struct kvm_vcpu * vcpu,u64 * exit_code)743 static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu, u64 *exit_code)
744 {
745 /*
746 * Check for the conditions of Cortex-A510's #2077057. When these occur
747 * SPSR_EL2 can't be trusted, but isn't needed either as it is
748 * unchanged from the value in vcpu_gp_regs(vcpu)->pstate.
749 * Are we single-stepping the guest, and took a PAC exception from the
750 * active-not-pending state?
751 */
752 if (cpus_have_final_cap(ARM64_WORKAROUND_2077057) &&
753 vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
754 *vcpu_cpsr(vcpu) & DBG_SPSR_SS &&
755 ESR_ELx_EC(read_sysreg_el2(SYS_ESR)) == ESR_ELx_EC_PAC)
756 write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
757
758 vcpu->arch.ctxt.regs.pstate = read_sysreg_el2(SYS_SPSR);
759 }
760
761 /*
762 * Return true when we were able to fixup the guest exit and should return to
763 * the guest, false when we should restore the host state and return to the
764 * main run loop.
765 */
__fixup_guest_exit(struct kvm_vcpu * vcpu,u64 * exit_code,const exit_handler_fn * handlers)766 static inline bool __fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code,
767 const exit_handler_fn *handlers)
768 {
769 if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ)
770 vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR);
771
772 if (ARM_SERROR_PENDING(*exit_code) &&
773 ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ) {
774 u8 esr_ec = kvm_vcpu_trap_get_class(vcpu);
775
776 /*
777 * HVC already have an adjusted PC, which we need to
778 * correct in order to return to after having injected
779 * the SError.
780 *
781 * SMC, on the other hand, is *trapped*, meaning its
782 * preferred return address is the SMC itself.
783 */
784 if (esr_ec == ESR_ELx_EC_HVC32 || esr_ec == ESR_ELx_EC_HVC64)
785 write_sysreg_el2(read_sysreg_el2(SYS_ELR) - 4, SYS_ELR);
786 }
787
788 /*
789 * We're using the raw exception code in order to only process
790 * the trap if no SError is pending. We will come back to the
791 * same PC once the SError has been injected, and replay the
792 * trapping instruction.
793 */
794 if (*exit_code != ARM_EXCEPTION_TRAP)
795 goto exit;
796
797 /* Check if there's an exit handler and allow it to handle the exit. */
798 if (kvm_hyp_handle_exit(vcpu, exit_code, handlers))
799 goto guest;
800 exit:
801 /* Return to the host kernel and handle the exit */
802 return false;
803
804 guest:
805 /* Re-enter the guest */
806 asm(ALTERNATIVE("nop", "dmb sy", ARM64_WORKAROUND_1508412));
807 return true;
808 }
809
__kvm_unexpected_el2_exception(void)810 static inline void __kvm_unexpected_el2_exception(void)
811 {
812 extern char __guest_exit_restore_elr_and_panic[];
813 unsigned long addr, fixup;
814 struct kvm_exception_table_entry *entry, *end;
815 unsigned long elr_el2 = read_sysreg(elr_el2);
816
817 entry = &__start___kvm_ex_table;
818 end = &__stop___kvm_ex_table;
819
820 while (entry < end) {
821 addr = (unsigned long)&entry->insn + entry->insn;
822 fixup = (unsigned long)&entry->fixup + entry->fixup;
823
824 if (addr != elr_el2) {
825 entry++;
826 continue;
827 }
828
829 write_sysreg(fixup, elr_el2);
830 return;
831 }
832
833 /* Trigger a panic after restoring the hyp context. */
834 this_cpu_ptr(&kvm_hyp_ctxt)->sys_regs[ELR_EL2] = elr_el2;
835 write_sysreg(__guest_exit_restore_elr_and_panic, elr_el2);
836 }
837
838 #endif /* __ARM64_KVM_HYP_SWITCH_H__ */
839