• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012,2013 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  *
6  * Derived from arch/arm/kvm/reset.c
7  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
8  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
9  */
10 
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/kvm_host.h>
14 #include <linux/kvm.h>
15 #include <linux/hw_breakpoint.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 
20 #include <kvm/arm_arch_timer.h>
21 
22 #include <asm/cpufeature.h>
23 #include <asm/cputype.h>
24 #include <asm/fpsimd.h>
25 #include <asm/ptrace.h>
26 #include <asm/kvm_arm.h>
27 #include <asm/kvm_asm.h>
28 #include <asm/kvm_emulate.h>
29 #include <asm/kvm_mmu.h>
30 #include <asm/kvm_nested.h>
31 #include <asm/virt.h>
32 
33 /* Maximum phys_shift supported for any VM on this host */
34 static u32 __ro_after_init kvm_ipa_limit;
35 unsigned int __ro_after_init kvm_host_sve_max_vl;
36 
37 unsigned int __ro_after_init kvm_sve_max_vl;
38 
kvm_arm_init_sve(void)39 int __init kvm_arm_init_sve(void)
40 {
41 	if (system_supports_sve()) {
42 		kvm_sve_max_vl = sve_max_virtualisable_vl();
43 		kvm_nvhe_sym(kvm_sve_max_vl) = kvm_sve_max_vl;
44 		kvm_host_sve_max_vl = sve_max_vl();
45 		kvm_nvhe_sym(kvm_host_sve_max_vl) = kvm_host_sve_max_vl;
46 
47 		/*
48 		 * The get_sve_reg()/set_sve_reg() ioctl interface will need
49 		 * to be extended with multiple register slice support in
50 		 * order to support vector lengths greater than
51 		 * VL_ARCH_MAX:
52 		 */
53 		if (WARN_ON(kvm_sve_max_vl > VL_ARCH_MAX))
54 			kvm_sve_max_vl = VL_ARCH_MAX;
55 
56 		/*
57 		 * Don't even try to make use of vector lengths that
58 		 * aren't available on all CPUs, for now:
59 		 */
60 		if (kvm_sve_max_vl < sve_max_vl())
61 			pr_warn("KVM: SVE vector length for guests limited to %u bytes\n",
62 				kvm_sve_max_vl);
63 	}
64 
65 	return 0;
66 }
67 
kvm_vcpu_enable_sve(struct kvm_vcpu * vcpu)68 static void kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
69 {
70 	vcpu->arch.sve_max_vl = kvm_sve_max_vl;
71 
72 	/*
73 	 * Userspace can still customize the vector lengths by writing
74 	 * KVM_REG_ARM64_SVE_VLS.  Allocation is deferred until
75 	 * kvm_arm_vcpu_finalize(), which freezes the configuration.
76 	 */
77 	set_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &vcpu->kvm->arch.flags);
78 }
79 
alloc_sve_state(struct kvm_vcpu * vcpu)80 static int alloc_sve_state(struct kvm_vcpu *vcpu)
81 {
82 	size_t reg_sz = PAGE_ALIGN(vcpu_sve_state_size(vcpu));
83 	void *buf;
84 	int ret;
85 
86 	if (kvm_vm_is_protected(vcpu->kvm))
87 		return 0;
88 
89 	buf = alloc_pages_exact(reg_sz, GFP_KERNEL_ACCOUNT);
90 	if (!buf)
91 		return -ENOMEM;
92 
93 	ret = kvm_share_hyp(buf, buf + reg_sz);
94 	if (ret) {
95 		kfree(buf);
96 		return ret;
97 	}
98 
99 	vcpu->arch.sve_state = buf;
100 
101 	return 0;
102 }
103 
104 /*
105  * Finalize vcpu's maximum SVE vector length, allocating
106  * vcpu->arch.sve_state as necessary.
107  */
kvm_vcpu_finalize_sve(struct kvm_vcpu * vcpu)108 static int kvm_vcpu_finalize_sve(struct kvm_vcpu *vcpu)
109 {
110 	unsigned int vl;
111 	int ret;
112 
113 	vl = vcpu->arch.sve_max_vl;
114 
115 	/*
116 	 * Responsibility for these properties is shared between
117 	 * kvm_arm_init_sve(), kvm_vcpu_enable_sve() and
118 	 * set_sve_vls().  Double-check here just to be sure:
119 	 */
120 	if (WARN_ON(!sve_vl_valid(vl) || vl > sve_max_virtualisable_vl() ||
121 		    vl > VL_ARCH_MAX))
122 		return -EIO;
123 
124 	ret = alloc_sve_state(vcpu);
125 	if (ret)
126 		return ret;
127 
128 	vcpu_set_flag(vcpu, VCPU_SVE_FINALIZED);
129 	return 0;
130 }
131 
kvm_arm_vcpu_finalize(struct kvm_vcpu * vcpu,int feature)132 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
133 {
134 	switch (feature) {
135 	case KVM_ARM_VCPU_SVE:
136 		if (!vcpu_has_sve(vcpu))
137 			return -EINVAL;
138 
139 		if (kvm_arm_vcpu_sve_finalized(vcpu))
140 			return -EPERM;
141 
142 		return kvm_vcpu_finalize_sve(vcpu);
143 	}
144 
145 	return -EINVAL;
146 }
147 
kvm_arm_vcpu_is_finalized(struct kvm_vcpu * vcpu)148 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
149 {
150 	if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
151 		return false;
152 
153 	return true;
154 }
155 
kvm_arm_vcpu_destroy(struct kvm_vcpu * vcpu)156 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
157 {
158 	void *sve_state = vcpu->arch.sve_state;
159 
160 	kvm_unshare_hyp(vcpu, vcpu + 1);
161 
162 	if (sve_state) {
163 		size_t reg_sz = PAGE_ALIGN(vcpu_sve_state_size(vcpu));
164 
165 		/* sve_allocate within the hypervisor when protected */
166 		BUG_ON(kvm_vm_is_protected(vcpu->kvm));
167 
168 		kvm_unshare_hyp(sve_state, sve_state + reg_sz);
169 		free_pages_exact(sve_state, reg_sz);
170 	}
171 
172 	kfree(vcpu->arch.ccsidr);
173 }
174 
kvm_vcpu_reset_sve(struct kvm_vcpu * vcpu)175 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
176 {
177 	if (!kvm_vm_is_protected(vcpu->kvm) && vcpu_has_sve(vcpu))
178 		memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
179 }
180 
181 /**
182  * kvm_reset_vcpu - sets core registers and sys_regs to reset value
183  * @vcpu: The VCPU pointer
184  *
185  * This function sets the registers on the virtual CPU struct to their
186  * architecturally defined reset values, except for registers whose reset is
187  * deferred until kvm_arm_vcpu_finalize().
188  *
189  * Note: This function can be called from two paths: The KVM_ARM_VCPU_INIT
190  * ioctl or as part of handling a request issued by another VCPU in the PSCI
191  * handling code.  In the first case, the VCPU will not be loaded, and in the
192  * second case the VCPU will be loaded.  Because this function operates purely
193  * on the memory-backed values of system registers, we want to do a full put if
194  * we were loaded (handling a request) and load the values back at the end of
195  * the function.  Otherwise we leave the state alone.  In both cases, we
196  * disable preemption around the vcpu reset as we would otherwise race with
197  * preempt notifiers which also call put/load.
198  */
kvm_reset_vcpu(struct kvm_vcpu * vcpu)199 void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
200 {
201 	struct vcpu_reset_state reset_state;
202 	bool loaded;
203 
204 	spin_lock(&vcpu->arch.mp_state_lock);
205 	reset_state = vcpu->arch.reset_state;
206 	vcpu->arch.reset_state.reset = false;
207 	spin_unlock(&vcpu->arch.mp_state_lock);
208 
209 	/* Reset PMU outside of the non-preemptible section */
210 	kvm_pmu_vcpu_reset(vcpu);
211 
212 	preempt_disable();
213 	loaded = (vcpu->cpu != -1);
214 	if (loaded)
215 		kvm_arch_vcpu_put(vcpu);
216 
217 	if (!kvm_arm_vcpu_sve_finalized(vcpu)) {
218 		if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
219 			kvm_vcpu_enable_sve(vcpu);
220 	} else {
221 		kvm_vcpu_reset_sve(vcpu);
222 	}
223 
224 	/* Reset core registers */
225 	kvm_reset_vcpu_core(vcpu);
226 
227 	/* Reset system registers */
228 	kvm_reset_sys_regs(vcpu);
229 
230 	/*
231 	 * Additional reset state handling that PSCI may have imposed on us.
232 	 * Must be done after all the sys_reg reset.
233 	 */
234 	if (reset_state.reset)
235 		kvm_reset_vcpu_psci(vcpu, &reset_state);
236 
237 	/* Reset timer */
238 	kvm_timer_vcpu_reset(vcpu);
239 
240 	if (loaded)
241 		kvm_arch_vcpu_load(vcpu, smp_processor_id());
242 	preempt_enable();
243 }
244 
kvm_get_pa_bits(struct kvm * kvm)245 u32 kvm_get_pa_bits(struct kvm *kvm)
246 {
247 	/* Fixed limit until we can configure ID_AA64MMFR0.PARange */
248 	return kvm_ipa_limit;
249 }
250 
get_kvm_ipa_limit(void)251 u32 get_kvm_ipa_limit(void)
252 {
253 	return kvm_ipa_limit;
254 }
255 
kvm_set_ipa_limit(void)256 int __init kvm_set_ipa_limit(void)
257 {
258 	unsigned int parange;
259 	u64 mmfr0;
260 
261 	mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
262 	parange = cpuid_feature_extract_unsigned_field(mmfr0,
263 				ID_AA64MMFR0_EL1_PARANGE_SHIFT);
264 	/*
265 	 * IPA size beyond 48 bits for 4K and 16K page size is only supported
266 	 * when LPA2 is available. So if we have LPA2, enable it, else cap to 48
267 	 * bits, in case it's reported as larger on the system.
268 	 */
269 	if (!kvm_lpa2_is_enabled() && PAGE_SIZE != SZ_64K)
270 		parange = min(parange, (unsigned int)ID_AA64MMFR0_EL1_PARANGE_48);
271 
272 	/*
273 	 * Check with ARMv8.5-GTG that our PAGE_SIZE is supported at
274 	 * Stage-2. If not, things will stop very quickly.
275 	 */
276 	switch (cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_TGRAN_2_SHIFT)) {
277 	case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_NONE:
278 		kvm_err("PAGE_SIZE not supported at Stage-2, giving up\n");
279 		return -EINVAL;
280 	case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_DEFAULT:
281 		kvm_debug("PAGE_SIZE supported at Stage-2 (default)\n");
282 		break;
283 	case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MIN ... ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MAX:
284 		kvm_debug("PAGE_SIZE supported at Stage-2 (advertised)\n");
285 		break;
286 	default:
287 		kvm_err("Unsupported value for TGRAN_2, giving up\n");
288 		return -EINVAL;
289 	}
290 
291 	kvm_ipa_limit = id_aa64mmfr0_parange_to_phys_shift(parange);
292 	kvm_info("IPA Size Limit: %d bits%s\n", kvm_ipa_limit,
293 		 ((kvm_ipa_limit < KVM_PHYS_SHIFT) ?
294 		  " (Reduced IPA size, limited VM/VMM compatibility)" : ""));
295 
296 	return 0;
297 }
298