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
36 /*
37 * ARMv8 Reset Values
38 */
39 #define VCPU_RESET_PSTATE_EL1 (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \
40 PSR_F_BIT | PSR_D_BIT)
41
42 #define VCPU_RESET_PSTATE_EL2 (PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \
43 PSR_F_BIT | PSR_D_BIT)
44
45 #define VCPU_RESET_PSTATE_SVC (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \
46 PSR_AA32_I_BIT | PSR_AA32_F_BIT)
47
48 unsigned int __ro_after_init kvm_sve_max_vl;
49
kvm_arm_init_sve(void)50 int __init kvm_arm_init_sve(void)
51 {
52 if (system_supports_sve()) {
53 kvm_sve_max_vl = sve_max_virtualisable_vl();
54
55 /*
56 * The get_sve_reg()/set_sve_reg() ioctl interface will need
57 * to be extended with multiple register slice support in
58 * order to support vector lengths greater than
59 * VL_ARCH_MAX:
60 */
61 if (WARN_ON(kvm_sve_max_vl > VL_ARCH_MAX))
62 kvm_sve_max_vl = VL_ARCH_MAX;
63
64 /*
65 * Don't even try to make use of vector lengths that
66 * aren't available on all CPUs, for now:
67 */
68 if (kvm_sve_max_vl < sve_max_vl())
69 pr_warn("KVM: SVE vector length for guests limited to %u bytes\n",
70 kvm_sve_max_vl);
71 }
72
73 return 0;
74 }
75
kvm_vcpu_enable_sve(struct kvm_vcpu * vcpu)76 static int kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
77 {
78 if (!system_supports_sve())
79 return -EINVAL;
80
81 vcpu->arch.sve_max_vl = kvm_sve_max_vl;
82
83 /*
84 * Userspace can still customize the vector lengths by writing
85 * KVM_REG_ARM64_SVE_VLS. Allocation is deferred until
86 * kvm_arm_vcpu_finalize(), which freezes the configuration.
87 */
88 vcpu_set_flag(vcpu, GUEST_HAS_SVE);
89
90 return 0;
91 }
92
93 /*
94 * Finalize vcpu's maximum SVE vector length, allocating
95 * vcpu->arch.sve_state as necessary.
96 */
kvm_vcpu_finalize_sve(struct kvm_vcpu * vcpu)97 static int kvm_vcpu_finalize_sve(struct kvm_vcpu *vcpu)
98 {
99 void *buf;
100 unsigned int vl;
101 size_t reg_sz;
102 int ret;
103
104 vl = vcpu->arch.sve_max_vl;
105
106 /*
107 * Responsibility for these properties is shared between
108 * kvm_arm_init_sve(), kvm_vcpu_enable_sve() and
109 * set_sve_vls(). Double-check here just to be sure:
110 */
111 if (WARN_ON(!sve_vl_valid(vl) || vl > sve_max_virtualisable_vl() ||
112 vl > VL_ARCH_MAX))
113 return -EIO;
114
115 reg_sz = vcpu_sve_state_size(vcpu);
116 buf = kzalloc(reg_sz, GFP_KERNEL_ACCOUNT);
117 if (!buf)
118 return -ENOMEM;
119
120 ret = kvm_share_hyp(buf, buf + reg_sz);
121 if (ret) {
122 kfree(buf);
123 return ret;
124 }
125
126 vcpu->arch.sve_state = buf;
127 vcpu_set_flag(vcpu, VCPU_SVE_FINALIZED);
128 return 0;
129 }
130
kvm_arm_vcpu_finalize(struct kvm_vcpu * vcpu,int feature)131 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
132 {
133 switch (feature) {
134 case KVM_ARM_VCPU_SVE:
135 if (!vcpu_has_sve(vcpu))
136 return -EINVAL;
137
138 if (kvm_arm_vcpu_sve_finalized(vcpu))
139 return -EPERM;
140
141 return kvm_vcpu_finalize_sve(vcpu);
142 }
143
144 return -EINVAL;
145 }
146
kvm_arm_vcpu_is_finalized(struct kvm_vcpu * vcpu)147 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
148 {
149 if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
150 return false;
151
152 return true;
153 }
154
kvm_arm_vcpu_destroy(struct kvm_vcpu * vcpu)155 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
156 {
157 void *sve_state = vcpu->arch.sve_state;
158
159 kvm_vcpu_unshare_task_fp(vcpu);
160 kvm_unshare_hyp(vcpu, vcpu + 1);
161 if (sve_state)
162 kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
163 kfree(sve_state);
164 kfree(vcpu->arch.ccsidr);
165 }
166
kvm_vcpu_reset_sve(struct kvm_vcpu * vcpu)167 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
168 {
169 if (vcpu_has_sve(vcpu))
170 memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
171 }
172
kvm_vcpu_enable_ptrauth(struct kvm_vcpu * vcpu)173 static int kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
174 {
175 /*
176 * For now make sure that both address/generic pointer authentication
177 * features are requested by the userspace together and the system
178 * supports these capabilities.
179 */
180 if (!test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, vcpu->arch.features) ||
181 !test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, vcpu->arch.features) ||
182 !system_has_full_ptr_auth())
183 return -EINVAL;
184
185 vcpu_set_flag(vcpu, GUEST_HAS_PTRAUTH);
186 return 0;
187 }
188
189 /**
190 * kvm_set_vm_width() - set the register width for the guest
191 * @vcpu: Pointer to the vcpu being configured
192 *
193 * Set both KVM_ARCH_FLAG_EL1_32BIT and KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED
194 * in the VM flags based on the vcpu's requested register width, the HW
195 * capabilities and other options (such as MTE).
196 * When REG_WIDTH_CONFIGURED is already set, the vcpu settings must be
197 * consistent with the value of the FLAG_EL1_32BIT bit in the flags.
198 *
199 * Return: 0 on success, negative error code on failure.
200 */
kvm_set_vm_width(struct kvm_vcpu * vcpu)201 static int kvm_set_vm_width(struct kvm_vcpu *vcpu)
202 {
203 struct kvm *kvm = vcpu->kvm;
204 bool is32bit;
205
206 is32bit = vcpu_has_feature(vcpu, KVM_ARM_VCPU_EL1_32BIT);
207
208 lockdep_assert_held(&kvm->lock);
209
210 if (test_bit(KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED, &kvm->arch.flags)) {
211 /*
212 * The guest's register width is already configured.
213 * Make sure that the vcpu is consistent with it.
214 */
215 if (is32bit == test_bit(KVM_ARCH_FLAG_EL1_32BIT, &kvm->arch.flags))
216 return 0;
217
218 return -EINVAL;
219 }
220
221 if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1) && is32bit)
222 return -EINVAL;
223
224 /* MTE is incompatible with AArch32 */
225 if (kvm_has_mte(kvm) && is32bit)
226 return -EINVAL;
227
228 /* NV is incompatible with AArch32 */
229 if (vcpu_has_nv(vcpu) && is32bit)
230 return -EINVAL;
231
232 if (is32bit)
233 set_bit(KVM_ARCH_FLAG_EL1_32BIT, &kvm->arch.flags);
234
235 set_bit(KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED, &kvm->arch.flags);
236
237 return 0;
238 }
239
240 /**
241 * kvm_reset_vcpu - sets core registers and sys_regs to reset value
242 * @vcpu: The VCPU pointer
243 *
244 * This function sets the registers on the virtual CPU struct to their
245 * architecturally defined reset values, except for registers whose reset is
246 * deferred until kvm_arm_vcpu_finalize().
247 *
248 * Note: This function can be called from two paths: The KVM_ARM_VCPU_INIT
249 * ioctl or as part of handling a request issued by another VCPU in the PSCI
250 * handling code. In the first case, the VCPU will not be loaded, and in the
251 * second case the VCPU will be loaded. Because this function operates purely
252 * on the memory-backed values of system registers, we want to do a full put if
253 * we were loaded (handling a request) and load the values back at the end of
254 * the function. Otherwise we leave the state alone. In both cases, we
255 * disable preemption around the vcpu reset as we would otherwise race with
256 * preempt notifiers which also call put/load.
257 */
kvm_reset_vcpu(struct kvm_vcpu * vcpu)258 int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
259 {
260 struct vcpu_reset_state reset_state;
261 int ret;
262 bool loaded;
263 u32 pstate;
264
265 mutex_lock(&vcpu->kvm->lock);
266 ret = kvm_set_vm_width(vcpu);
267 if (!ret) {
268 reset_state = vcpu->arch.reset_state;
269 WRITE_ONCE(vcpu->arch.reset_state.reset, false);
270 }
271 mutex_unlock(&vcpu->kvm->lock);
272
273 if (ret)
274 return ret;
275
276 /* Reset PMU outside of the non-preemptible section */
277 kvm_pmu_vcpu_reset(vcpu);
278
279 preempt_disable();
280 loaded = (vcpu->cpu != -1);
281 if (loaded)
282 kvm_arch_vcpu_put(vcpu);
283
284 /* Disallow NV+SVE for the time being */
285 if (vcpu_has_nv(vcpu) && vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE)) {
286 ret = -EINVAL;
287 goto out;
288 }
289
290 if (!kvm_arm_vcpu_sve_finalized(vcpu)) {
291 if (test_bit(KVM_ARM_VCPU_SVE, vcpu->arch.features)) {
292 ret = kvm_vcpu_enable_sve(vcpu);
293 if (ret)
294 goto out;
295 }
296 } else {
297 kvm_vcpu_reset_sve(vcpu);
298 }
299
300 if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, vcpu->arch.features) ||
301 test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, vcpu->arch.features)) {
302 if (kvm_vcpu_enable_ptrauth(vcpu)) {
303 ret = -EINVAL;
304 goto out;
305 }
306 }
307
308 switch (vcpu->arch.target) {
309 default:
310 if (vcpu_el1_is_32bit(vcpu)) {
311 pstate = VCPU_RESET_PSTATE_SVC;
312 } else if (vcpu_has_nv(vcpu)) {
313 pstate = VCPU_RESET_PSTATE_EL2;
314 } else {
315 pstate = VCPU_RESET_PSTATE_EL1;
316 }
317
318 if (kvm_vcpu_has_pmu(vcpu) && !kvm_arm_support_pmu_v3()) {
319 ret = -EINVAL;
320 goto out;
321 }
322 break;
323 }
324
325 /* Reset core registers */
326 memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu)));
327 memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
328 vcpu->arch.ctxt.spsr_abt = 0;
329 vcpu->arch.ctxt.spsr_und = 0;
330 vcpu->arch.ctxt.spsr_irq = 0;
331 vcpu->arch.ctxt.spsr_fiq = 0;
332 vcpu_gp_regs(vcpu)->pstate = pstate;
333
334 /* Reset system registers */
335 kvm_reset_sys_regs(vcpu);
336
337 /*
338 * Additional reset state handling that PSCI may have imposed on us.
339 * Must be done after all the sys_reg reset.
340 */
341 if (reset_state.reset) {
342 unsigned long target_pc = reset_state.pc;
343
344 /* Gracefully handle Thumb2 entry point */
345 if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
346 target_pc &= ~1UL;
347 vcpu_set_thumb(vcpu);
348 }
349
350 /* Propagate caller endianness */
351 if (reset_state.be)
352 kvm_vcpu_set_be(vcpu);
353
354 *vcpu_pc(vcpu) = target_pc;
355 vcpu_set_reg(vcpu, 0, reset_state.r0);
356 }
357
358 /* Reset timer */
359 ret = kvm_timer_vcpu_reset(vcpu);
360 out:
361 if (loaded)
362 kvm_arch_vcpu_load(vcpu, smp_processor_id());
363 preempt_enable();
364 return ret;
365 }
366
get_kvm_ipa_limit(void)367 u32 get_kvm_ipa_limit(void)
368 {
369 return kvm_ipa_limit;
370 }
371
kvm_set_ipa_limit(void)372 int __init kvm_set_ipa_limit(void)
373 {
374 unsigned int parange;
375 u64 mmfr0;
376
377 mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
378 parange = cpuid_feature_extract_unsigned_field(mmfr0,
379 ID_AA64MMFR0_EL1_PARANGE_SHIFT);
380 /*
381 * IPA size beyond 48 bits could not be supported
382 * on either 4K or 16K page size. Hence let's cap
383 * it to 48 bits, in case it's reported as larger
384 * on the system.
385 */
386 if (PAGE_SIZE != SZ_64K)
387 parange = min(parange, (unsigned int)ID_AA64MMFR0_EL1_PARANGE_48);
388
389 /*
390 * Check with ARMv8.5-GTG that our PAGE_SIZE is supported at
391 * Stage-2. If not, things will stop very quickly.
392 */
393 switch (cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_TGRAN_2_SHIFT)) {
394 case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_NONE:
395 kvm_err("PAGE_SIZE not supported at Stage-2, giving up\n");
396 return -EINVAL;
397 case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_DEFAULT:
398 kvm_debug("PAGE_SIZE supported at Stage-2 (default)\n");
399 break;
400 case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MIN ... ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MAX:
401 kvm_debug("PAGE_SIZE supported at Stage-2 (advertised)\n");
402 break;
403 default:
404 kvm_err("Unsupported value for TGRAN_2, giving up\n");
405 return -EINVAL;
406 }
407
408 kvm_ipa_limit = id_aa64mmfr0_parange_to_phys_shift(parange);
409 kvm_info("IPA Size Limit: %d bits%s\n", kvm_ipa_limit,
410 ((kvm_ipa_limit < KVM_PHYS_SHIFT) ?
411 " (Reduced IPA size, limited VM/VMM compatibility)" : ""));
412
413 return 0;
414 }
415