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