• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = &current->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 	vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
41 
42 	return 0;
43 }
44 
45 /*
46  * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
47  * The actual loading is done by the FPSIMD access trap taken to hyp.
48  *
49  * Here, we just set the correct metadata to indicate that the FPSIMD
50  * state in the cpu regs (if any) belongs to current on the host.
51  */
kvm_arch_vcpu_load_fp(struct kvm_vcpu * vcpu)52 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
53 {
54 	BUG_ON(!current->mm);
55 	BUG_ON(test_thread_flag(TIF_SVE));
56 
57 	if (!system_supports_fpsimd())
58 		return;
59 
60 	vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
61 
62 	vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
63 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
64 		vcpu_set_flag(vcpu, HOST_SVE_ENABLED);
65 
66 	/*
67 	 * We don't currently support SME guests but if we leave
68 	 * things in streaming mode then when the guest starts running
69 	 * FPSIMD or SVE code it may generate SME traps so as a
70 	 * special case if we are in streaming mode we force the host
71 	 * state to be saved now and exit streaming mode so that we
72 	 * don't have to handle any SME traps for valid guest
73 	 * operations. Do this for ZA as well for now for simplicity.
74 	 */
75 	if (system_supports_sme()) {
76 		vcpu_clear_flag(vcpu, HOST_SME_ENABLED);
77 		if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
78 			vcpu_set_flag(vcpu, HOST_SME_ENABLED);
79 
80 		if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
81 			vcpu->arch.fp_state = FP_STATE_FREE;
82 			fpsimd_save_and_flush_cpu_state();
83 		}
84 	}
85 }
86 
87 /*
88  * Called just before entering the guest once we are no longer preemptable
89  * and interrupts are disabled. If we have managed to run anything using
90  * FP while we were preemptible (such as off the back of an interrupt),
91  * then neither the host nor the guest own the FP hardware (and it was the
92  * responsibility of the code that used FP to save the existing state).
93  */
kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu * vcpu)94 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
95 {
96 	if (test_thread_flag(TIF_FOREIGN_FPSTATE))
97 		vcpu->arch.fp_state = FP_STATE_FREE;
98 }
99 
100 /*
101  * Called just after exiting the guest. If the guest FPSIMD state
102  * was loaded, update the host's context tracking data mark the CPU
103  * FPSIMD regs as dirty and belonging to vcpu so that they will be
104  * written back if the kernel clobbers them due to kernel-mode NEON
105  * before re-entry into the guest.
106  */
kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu * vcpu)107 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
108 {
109 	WARN_ON_ONCE(!irqs_disabled());
110 
111 	if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
112 		/*
113 		 * Currently we do not support SME guests so SVCR is
114 		 * always 0 and we just need a variable to point to.
115 		 */
116 		fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
117 					 vcpu->arch.sve_state,
118 					 vcpu->arch.sve_max_vl,
119 					 NULL, 0, &vcpu->arch.svcr);
120 
121 		clear_thread_flag(TIF_FOREIGN_FPSTATE);
122 		update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
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 	/*
139 	 * If we have VHE then the Hyp code will reset CPACR_EL1 to
140 	 * CPACR_EL1_DEFAULT and we need to reenable SME.
141 	 */
142 	if (has_vhe() && system_supports_sme()) {
143 		/* Also restore EL0 state seen on entry */
144 		if (vcpu_get_flag(vcpu, HOST_SME_ENABLED))
145 			sysreg_clear_set(CPACR_EL1, 0,
146 					 CPACR_EL1_SMEN_EL0EN |
147 					 CPACR_EL1_SMEN_EL1EN);
148 		else
149 			sysreg_clear_set(CPACR_EL1,
150 					 CPACR_EL1_SMEN_EL0EN,
151 					 CPACR_EL1_SMEN_EL1EN);
152 	}
153 
154 	if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
155 		if (vcpu_has_sve(vcpu)) {
156 			__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
157 
158 			/*
159 			 * Restore the VL that was saved when bound to the CPU,
160 			 * which is the maximum VL for the guest. Because
161 			 * the layout of the data when saving the sve state
162 			 * depends on the VL, we need to use a consistent VL.
163 			 * Note that this means that at guest exit ZCR_EL1 is
164 			 * not necessarily the same as on guest entry.
165 			 *
166 			 * Flushing the cpu state sets the TIF_FOREIGN_FPSTATE
167 			 * bit for the context, which lets the kernel restore
168 			 * the sve state, including ZCR_EL1 later.
169 			 */
170 			if (!has_vhe())
171 				sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
172 						       SYS_ZCR_EL1);
173 		}
174 
175 		fpsimd_save_and_flush_cpu_state();
176 	} else if (has_vhe() && system_supports_sve()) {
177 		/*
178 		 * The FPSIMD/SVE state in the CPU has not been touched, and we
179 		 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
180 		 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
181 		 * for EL0.  To avoid spurious traps, restore the trap state
182 		 * seen by kvm_arch_vcpu_load_fp():
183 		 */
184 		if (vcpu_get_flag(vcpu, HOST_SVE_ENABLED))
185 			sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
186 		else
187 			sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
188 	}
189 
190 	update_thread_flag(TIF_SVE, 0);
191 
192 	local_irq_restore(flags);
193 }
194