1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
4 *
5 * Copyright 2018 Arm Limited
6 * Author: Dave Martin <Dave.Martin@arm.com>
7 */
8 #include <linux/irqflags.h>
9 #include <linux/sched.h>
10 #include <linux/kvm_host.h>
11 #include <asm/fpsimd.h>
12 #include <asm/kvm_asm.h>
13 #include <asm/kvm_hyp.h>
14 #include <asm/kvm_mmu.h>
15 #include <asm/sysreg.h>
16
17 /*
18 * Called on entry to KVM_RUN unless this vcpu previously ran at least
19 * once and the most recent prior KVM_RUN for this vcpu was called from
20 * the same task as current (highly likely).
21 *
22 * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
23 * such that on entering hyp the relevant parts of current are already
24 * mapped.
25 */
kvm_arch_vcpu_run_map_fp(struct kvm_vcpu * vcpu)26 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
27 {
28 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
29 int ret;
30
31 /* pKVM has its own tracking of the host fpsimd state. */
32 if (is_protected_kvm_enabled())
33 return 0;
34
35 /* Make sure the host task fpsimd state is visible to hyp: */
36 ret = kvm_share_hyp(fpsimd, fpsimd + 1);
37 if (ret)
38 return ret;
39
40 return 0;
41 }
42
43 /*
44 * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
45 * The actual loading is done by the FPSIMD access trap taken to hyp.
46 *
47 * Here, we just set the correct metadata to indicate that the FPSIMD
48 * state in the cpu regs (if any) belongs to current on the host.
49 */
kvm_arch_vcpu_load_fp(struct kvm_vcpu * vcpu)50 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
51 {
52 BUG_ON(!current->mm);
53
54 if (!system_supports_fpsimd())
55 return;
56
57 /*
58 * Ensure that any host FPSIMD/SVE/SME state is saved and unbound such
59 * that the host kernel is responsible for restoring this state upon
60 * return to userspace, and the hyp code doesn't need to save anything.
61 *
62 * When the host may use SME, fpsimd_save_and_flush_cpu_state() ensures
63 * that PSTATE.{SM,ZA} == {0,0}.
64 */
65 fpsimd_save_and_flush_cpu_state();
66 *host_data_ptr(fp_owner) = FP_STATE_FREE;
67
68 /*
69 * If normal guests gain SME support, maintain this behavior for pKVM
70 * guests, which don't support SME.
71 */
72 WARN_ON(is_protected_kvm_enabled() && system_supports_sme() &&
73 read_sysreg_s(SYS_SVCR));
74 }
75
76 /*
77 * Called just before entering the guest once we are no longer preemptible
78 * and interrupts are disabled. If we have managed to run anything using
79 * FP while we were preemptible (such as off the back of an interrupt),
80 * then neither the host nor the guest own the FP hardware (and it was the
81 * responsibility of the code that used FP to save the existing state).
82 */
kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu * vcpu)83 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
84 {
85 if (test_thread_flag(TIF_FOREIGN_FPSTATE))
86 *host_data_ptr(fp_owner) = FP_STATE_FREE;
87 }
88
89 /*
90 * Called just after exiting the guest. If the guest FPSIMD state
91 * was loaded, update the host's context tracking data mark the CPU
92 * FPSIMD regs as dirty and belonging to vcpu so that they will be
93 * written back if the kernel clobbers them due to kernel-mode NEON
94 * before re-entry into the guest.
95 */
kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu * vcpu)96 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
97 {
98 struct cpu_fp_state fp_state;
99
100 WARN_ON_ONCE(!irqs_disabled());
101
102 if (guest_owns_fp_regs()) {
103 /*
104 * Currently we do not support SME guests so SVCR is
105 * always 0 and we just need a variable to point to.
106 */
107 fp_state.st = &vcpu->arch.ctxt.fp_regs;
108 fp_state.sve_state = vcpu->arch.sve_state;
109 fp_state.sve_vl = vcpu->arch.sve_max_vl;
110 fp_state.sme_state = NULL;
111 fp_state.svcr = &__vcpu_sys_reg(vcpu, SVCR);
112 fp_state.fpmr = &__vcpu_sys_reg(vcpu, FPMR);
113 fp_state.fp_type = &vcpu->arch.fp_type;
114
115 if (vcpu_has_sve(vcpu))
116 fp_state.to_save = FP_STATE_SVE;
117 else
118 fp_state.to_save = FP_STATE_FPSIMD;
119
120 fpsimd_bind_state_to_cpu(&fp_state);
121
122 clear_thread_flag(TIF_FOREIGN_FPSTATE);
123 }
124 }
125
126 /*
127 * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
128 * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
129 * disappears and another task or vcpu appears that recycles the same
130 * struct fpsimd_state.
131 */
kvm_arch_vcpu_put_fp(struct kvm_vcpu * vcpu)132 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
133 {
134 unsigned long flags;
135
136 local_irq_save(flags);
137
138 if (guest_owns_fp_regs()) {
139 /*
140 * Flush (save and invalidate) the fpsimd/sve state so that if
141 * the host tries to use fpsimd/sve, it's not using stale data
142 * from the guest.
143 *
144 * Flushing the state sets the TIF_FOREIGN_FPSTATE bit for the
145 * context unconditionally, in both nVHE and VHE. This allows
146 * the kernel to restore the fpsimd/sve state, including ZCR_EL1
147 * when needed.
148 */
149 fpsimd_save_and_flush_cpu_state();
150 }
151
152 local_irq_restore(flags);
153 }
154