• 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/guest.c:
7  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
8  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
9  */
10 
11 #include <linux/bits.h>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/nospec.h>
15 #include <linux/kvm_host.h>
16 #include <linux/module.h>
17 #include <linux/stddef.h>
18 #include <linux/string.h>
19 #include <linux/vmalloc.h>
20 #include <linux/fs.h>
21 #include <kvm/arm_hypercalls.h>
22 #include <asm/cputype.h>
23 #include <linux/uaccess.h>
24 #include <asm/fpsimd.h>
25 #include <asm/kvm.h>
26 #include <asm/kvm_emulate.h>
27 #include <asm/sigcontext.h>
28 
29 #include "trace.h"
30 
31 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
32 	KVM_GENERIC_VM_STATS(),
33 	STATS_DESC_ICOUNTER(VM, protected_hyp_mem),
34 	STATS_DESC_ICOUNTER(VM, protected_shared_mem),
35 };
36 
37 const struct kvm_stats_header kvm_vm_stats_header = {
38 	.name_size = KVM_STATS_NAME_SIZE,
39 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
40 	.id_offset =  sizeof(struct kvm_stats_header),
41 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
42 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
43 		       sizeof(kvm_vm_stats_desc),
44 };
45 
46 const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
47 	KVM_GENERIC_VCPU_STATS(),
48 	STATS_DESC_COUNTER(VCPU, hvc_exit_stat),
49 	STATS_DESC_COUNTER(VCPU, wfe_exit_stat),
50 	STATS_DESC_COUNTER(VCPU, wfi_exit_stat),
51 	STATS_DESC_COUNTER(VCPU, mmio_exit_user),
52 	STATS_DESC_COUNTER(VCPU, mmio_exit_kernel),
53 	STATS_DESC_COUNTER(VCPU, signal_exits),
54 	STATS_DESC_COUNTER(VCPU, exits)
55 };
56 
57 const struct kvm_stats_header kvm_vcpu_stats_header = {
58 	.name_size = KVM_STATS_NAME_SIZE,
59 	.num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
60 	.id_offset = sizeof(struct kvm_stats_header),
61 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
62 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
63 		       sizeof(kvm_vcpu_stats_desc),
64 };
65 
core_reg_offset_is_vreg(u64 off)66 static bool core_reg_offset_is_vreg(u64 off)
67 {
68 	return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
69 		off < KVM_REG_ARM_CORE_REG(fp_regs.fpsr);
70 }
71 
core_reg_offset_from_id(u64 id)72 static u64 core_reg_offset_from_id(u64 id)
73 {
74 	return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
75 }
76 
core_reg_size_from_offset(const struct kvm_vcpu * vcpu,u64 off)77 static int core_reg_size_from_offset(const struct kvm_vcpu *vcpu, u64 off)
78 {
79 	int size;
80 
81 	switch (off) {
82 	case KVM_REG_ARM_CORE_REG(regs.regs[0]) ...
83 	     KVM_REG_ARM_CORE_REG(regs.regs[30]):
84 	case KVM_REG_ARM_CORE_REG(regs.sp):
85 	case KVM_REG_ARM_CORE_REG(regs.pc):
86 	case KVM_REG_ARM_CORE_REG(regs.pstate):
87 	case KVM_REG_ARM_CORE_REG(sp_el1):
88 	case KVM_REG_ARM_CORE_REG(elr_el1):
89 	case KVM_REG_ARM_CORE_REG(spsr[0]) ...
90 	     KVM_REG_ARM_CORE_REG(spsr[KVM_NR_SPSR - 1]):
91 		size = sizeof(__u64);
92 		break;
93 
94 	case KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]) ...
95 	     KVM_REG_ARM_CORE_REG(fp_regs.vregs[31]):
96 		size = sizeof(__uint128_t);
97 		break;
98 
99 	case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
100 	case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
101 		size = sizeof(__u32);
102 		break;
103 
104 	default:
105 		return -EINVAL;
106 	}
107 
108 	if (!IS_ALIGNED(off, size / sizeof(__u32)))
109 		return -EINVAL;
110 
111 	/*
112 	 * The KVM_REG_ARM64_SVE regs must be used instead of
113 	 * KVM_REG_ARM_CORE for accessing the FPSIMD V-registers on
114 	 * SVE-enabled vcpus:
115 	 */
116 	if (vcpu_has_sve(vcpu) && core_reg_offset_is_vreg(off))
117 		return -EINVAL;
118 
119 	return size;
120 }
121 
core_reg_addr(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)122 static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
123 {
124 	u64 off = core_reg_offset_from_id(reg->id);
125 	int size = core_reg_size_from_offset(vcpu, off);
126 
127 	if (size < 0)
128 		return NULL;
129 
130 	if (KVM_REG_SIZE(reg->id) != size)
131 		return NULL;
132 
133 	switch (off) {
134 	case KVM_REG_ARM_CORE_REG(regs.regs[0]) ...
135 	     KVM_REG_ARM_CORE_REG(regs.regs[30]):
136 		off -= KVM_REG_ARM_CORE_REG(regs.regs[0]);
137 		off /= 2;
138 		return &vcpu->arch.ctxt.regs.regs[off];
139 
140 	case KVM_REG_ARM_CORE_REG(regs.sp):
141 		return &vcpu->arch.ctxt.regs.sp;
142 
143 	case KVM_REG_ARM_CORE_REG(regs.pc):
144 		return &vcpu->arch.ctxt.regs.pc;
145 
146 	case KVM_REG_ARM_CORE_REG(regs.pstate):
147 		return &vcpu->arch.ctxt.regs.pstate;
148 
149 	case KVM_REG_ARM_CORE_REG(sp_el1):
150 		return __ctxt_sys_reg(&vcpu->arch.ctxt, SP_EL1);
151 
152 	case KVM_REG_ARM_CORE_REG(elr_el1):
153 		return __ctxt_sys_reg(&vcpu->arch.ctxt, ELR_EL1);
154 
155 	case KVM_REG_ARM_CORE_REG(spsr[KVM_SPSR_EL1]):
156 		return __ctxt_sys_reg(&vcpu->arch.ctxt, SPSR_EL1);
157 
158 	case KVM_REG_ARM_CORE_REG(spsr[KVM_SPSR_ABT]):
159 		return &vcpu->arch.ctxt.spsr_abt;
160 
161 	case KVM_REG_ARM_CORE_REG(spsr[KVM_SPSR_UND]):
162 		return &vcpu->arch.ctxt.spsr_und;
163 
164 	case KVM_REG_ARM_CORE_REG(spsr[KVM_SPSR_IRQ]):
165 		return &vcpu->arch.ctxt.spsr_irq;
166 
167 	case KVM_REG_ARM_CORE_REG(spsr[KVM_SPSR_FIQ]):
168 		return &vcpu->arch.ctxt.spsr_fiq;
169 
170 	case KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]) ...
171 	     KVM_REG_ARM_CORE_REG(fp_regs.vregs[31]):
172 		off -= KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]);
173 		off /= 4;
174 		return &vcpu->arch.ctxt.fp_regs.vregs[off];
175 
176 	case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
177 		return &vcpu->arch.ctxt.fp_regs.fpsr;
178 
179 	case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
180 		return &vcpu->arch.ctxt.fp_regs.fpcr;
181 
182 	default:
183 		return NULL;
184 	}
185 }
186 
get_core_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)187 static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
188 {
189 	/*
190 	 * Because the kvm_regs structure is a mix of 32, 64 and
191 	 * 128bit fields, we index it as if it was a 32bit
192 	 * array. Hence below, nr_regs is the number of entries, and
193 	 * off the index in the "array".
194 	 */
195 	__u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
196 	int nr_regs = sizeof(struct kvm_regs) / sizeof(__u32);
197 	void *addr;
198 	u32 off;
199 
200 	/* Our ID is an index into the kvm_regs struct. */
201 	off = core_reg_offset_from_id(reg->id);
202 	if (off >= nr_regs ||
203 	    (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
204 		return -ENOENT;
205 
206 	addr = core_reg_addr(vcpu, reg);
207 	if (!addr)
208 		return -EINVAL;
209 
210 	if (copy_to_user(uaddr, addr, KVM_REG_SIZE(reg->id)))
211 		return -EFAULT;
212 
213 	return 0;
214 }
215 
set_core_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)216 static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
217 {
218 	__u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
219 	int nr_regs = sizeof(struct kvm_regs) / sizeof(__u32);
220 	__uint128_t tmp;
221 	void *valp = &tmp, *addr;
222 	u64 off;
223 	int err = 0;
224 
225 	/* Our ID is an index into the kvm_regs struct. */
226 	off = core_reg_offset_from_id(reg->id);
227 	if (off >= nr_regs ||
228 	    (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
229 		return -ENOENT;
230 
231 	addr = core_reg_addr(vcpu, reg);
232 	if (!addr)
233 		return -EINVAL;
234 
235 	if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
236 		return -EINVAL;
237 
238 	if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
239 		err = -EFAULT;
240 		goto out;
241 	}
242 
243 	if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
244 		u64 mode = (*(u64 *)valp) & PSR_AA32_MODE_MASK;
245 		switch (mode) {
246 		case PSR_AA32_MODE_USR:
247 			if (!kvm_supports_32bit_el0())
248 				return -EINVAL;
249 			break;
250 		case PSR_AA32_MODE_FIQ:
251 		case PSR_AA32_MODE_IRQ:
252 		case PSR_AA32_MODE_SVC:
253 		case PSR_AA32_MODE_ABT:
254 		case PSR_AA32_MODE_UND:
255 			if (!vcpu_el1_is_32bit(vcpu))
256 				return -EINVAL;
257 			break;
258 		case PSR_MODE_EL0t:
259 		case PSR_MODE_EL1t:
260 		case PSR_MODE_EL1h:
261 			if (vcpu_el1_is_32bit(vcpu))
262 				return -EINVAL;
263 			break;
264 		default:
265 			err = -EINVAL;
266 			goto out;
267 		}
268 	}
269 
270 	memcpy(addr, valp, KVM_REG_SIZE(reg->id));
271 
272 	if (*vcpu_cpsr(vcpu) & PSR_MODE32_BIT) {
273 		int i, nr_reg;
274 
275 		switch (*vcpu_cpsr(vcpu)) {
276 		/*
277 		 * Either we are dealing with user mode, and only the
278 		 * first 15 registers (+ PC) must be narrowed to 32bit.
279 		 * AArch32 r0-r14 conveniently map to AArch64 x0-x14.
280 		 */
281 		case PSR_AA32_MODE_USR:
282 		case PSR_AA32_MODE_SYS:
283 			nr_reg = 15;
284 			break;
285 
286 		/*
287 		 * Otherwise, this is a privileged mode, and *all* the
288 		 * registers must be narrowed to 32bit.
289 		 */
290 		default:
291 			nr_reg = 31;
292 			break;
293 		}
294 
295 		for (i = 0; i < nr_reg; i++)
296 			vcpu_set_reg(vcpu, i, (u32)vcpu_get_reg(vcpu, i));
297 
298 		*vcpu_pc(vcpu) = (u32)*vcpu_pc(vcpu);
299 	}
300 out:
301 	return err;
302 }
303 
304 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
305 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
306 #define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
307 
get_sve_vls(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)308 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
309 {
310 	unsigned int max_vq, vq;
311 	u64 vqs[KVM_ARM64_SVE_VLS_WORDS];
312 
313 	if (!vcpu_has_sve(vcpu))
314 		return -ENOENT;
315 
316 	if (WARN_ON(!sve_vl_valid(vcpu->arch.sve_max_vl)))
317 		return -EINVAL;
318 
319 	memset(vqs, 0, sizeof(vqs));
320 
321 	max_vq = vcpu_sve_max_vq(vcpu);
322 	for (vq = SVE_VQ_MIN; vq <= max_vq; ++vq)
323 		if (sve_vq_available(vq))
324 			vqs[vq_word(vq)] |= vq_mask(vq);
325 
326 	if (copy_to_user((void __user *)reg->addr, vqs, sizeof(vqs)))
327 		return -EFAULT;
328 
329 	return 0;
330 }
331 
set_sve_vls(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)332 static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
333 {
334 	unsigned int max_vq, vq;
335 	u64 vqs[KVM_ARM64_SVE_VLS_WORDS];
336 
337 	if (!vcpu_has_sve(vcpu))
338 		return -ENOENT;
339 
340 	if (kvm_arm_vcpu_sve_finalized(vcpu))
341 		return -EPERM; /* too late! */
342 
343 	if (WARN_ON(vcpu->arch.sve_state))
344 		return -EINVAL;
345 
346 	if (copy_from_user(vqs, (const void __user *)reg->addr, sizeof(vqs)))
347 		return -EFAULT;
348 
349 	max_vq = 0;
350 	for (vq = SVE_VQ_MIN; vq <= SVE_VQ_MAX; ++vq)
351 		if (vq_present(vqs, vq))
352 			max_vq = vq;
353 
354 	if (max_vq > sve_vq_from_vl(kvm_sve_max_vl))
355 		return -EINVAL;
356 
357 	/*
358 	 * Vector lengths supported by the host can't currently be
359 	 * hidden from the guest individually: instead we can only set a
360 	 * maximum via ZCR_EL2.LEN.  So, make sure the available vector
361 	 * lengths match the set requested exactly up to the requested
362 	 * maximum:
363 	 */
364 	for (vq = SVE_VQ_MIN; vq <= max_vq; ++vq)
365 		if (vq_present(vqs, vq) != sve_vq_available(vq))
366 			return -EINVAL;
367 
368 	/* Can't run with no vector lengths at all: */
369 	if (max_vq < SVE_VQ_MIN)
370 		return -EINVAL;
371 
372 	/* vcpu->arch.sve_state will be alloc'd by kvm_vcpu_finalize_sve() */
373 	vcpu->arch.sve_max_vl = sve_vl_from_vq(max_vq);
374 
375 	return 0;
376 }
377 
378 #define SVE_REG_SLICE_SHIFT	0
379 #define SVE_REG_SLICE_BITS	5
380 #define SVE_REG_ID_SHIFT	(SVE_REG_SLICE_SHIFT + SVE_REG_SLICE_BITS)
381 #define SVE_REG_ID_BITS		5
382 
383 #define SVE_REG_SLICE_MASK					\
384 	GENMASK(SVE_REG_SLICE_SHIFT + SVE_REG_SLICE_BITS - 1,	\
385 		SVE_REG_SLICE_SHIFT)
386 #define SVE_REG_ID_MASK							\
387 	GENMASK(SVE_REG_ID_SHIFT + SVE_REG_ID_BITS - 1, SVE_REG_ID_SHIFT)
388 
389 #define SVE_NUM_SLICES (1 << SVE_REG_SLICE_BITS)
390 
391 #define KVM_SVE_ZREG_SIZE KVM_REG_SIZE(KVM_REG_ARM64_SVE_ZREG(0, 0))
392 #define KVM_SVE_PREG_SIZE KVM_REG_SIZE(KVM_REG_ARM64_SVE_PREG(0, 0))
393 
394 /*
395  * Number of register slices required to cover each whole SVE register.
396  * NOTE: Only the first slice every exists, for now.
397  * If you are tempted to modify this, you must also rework sve_reg_to_region()
398  * to match:
399  */
400 #define vcpu_sve_slices(vcpu) 1
401 
402 /* Bounds of a single SVE register slice within vcpu->arch.sve_state */
403 struct sve_state_reg_region {
404 	unsigned int koffset;	/* offset into sve_state in kernel memory */
405 	unsigned int klen;	/* length in kernel memory */
406 	unsigned int upad;	/* extra trailing padding in user memory */
407 };
408 
409 /*
410  * Validate SVE register ID and get sanitised bounds for user/kernel SVE
411  * register copy
412  */
sve_reg_to_region(struct sve_state_reg_region * region,struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)413 static int sve_reg_to_region(struct sve_state_reg_region *region,
414 			     struct kvm_vcpu *vcpu,
415 			     const struct kvm_one_reg *reg)
416 {
417 	/* reg ID ranges for Z- registers */
418 	const u64 zreg_id_min = KVM_REG_ARM64_SVE_ZREG(0, 0);
419 	const u64 zreg_id_max = KVM_REG_ARM64_SVE_ZREG(SVE_NUM_ZREGS - 1,
420 						       SVE_NUM_SLICES - 1);
421 
422 	/* reg ID ranges for P- registers and FFR (which are contiguous) */
423 	const u64 preg_id_min = KVM_REG_ARM64_SVE_PREG(0, 0);
424 	const u64 preg_id_max = KVM_REG_ARM64_SVE_FFR(SVE_NUM_SLICES - 1);
425 
426 	unsigned int vq;
427 	unsigned int reg_num;
428 
429 	unsigned int reqoffset, reqlen; /* User-requested offset and length */
430 	unsigned int maxlen; /* Maximum permitted length */
431 
432 	size_t sve_state_size;
433 
434 	const u64 last_preg_id = KVM_REG_ARM64_SVE_PREG(SVE_NUM_PREGS - 1,
435 							SVE_NUM_SLICES - 1);
436 
437 	/* Verify that the P-regs and FFR really do have contiguous IDs: */
438 	BUILD_BUG_ON(KVM_REG_ARM64_SVE_FFR(0) != last_preg_id + 1);
439 
440 	/* Verify that we match the UAPI header: */
441 	BUILD_BUG_ON(SVE_NUM_SLICES != KVM_ARM64_SVE_MAX_SLICES);
442 
443 	reg_num = (reg->id & SVE_REG_ID_MASK) >> SVE_REG_ID_SHIFT;
444 
445 	if (reg->id >= zreg_id_min && reg->id <= zreg_id_max) {
446 		if (!vcpu_has_sve(vcpu) || (reg->id & SVE_REG_SLICE_MASK) > 0)
447 			return -ENOENT;
448 
449 		vq = vcpu_sve_max_vq(vcpu);
450 
451 		reqoffset = SVE_SIG_ZREG_OFFSET(vq, reg_num) -
452 				SVE_SIG_REGS_OFFSET;
453 		reqlen = KVM_SVE_ZREG_SIZE;
454 		maxlen = SVE_SIG_ZREG_SIZE(vq);
455 	} else if (reg->id >= preg_id_min && reg->id <= preg_id_max) {
456 		if (!vcpu_has_sve(vcpu) || (reg->id & SVE_REG_SLICE_MASK) > 0)
457 			return -ENOENT;
458 
459 		vq = vcpu_sve_max_vq(vcpu);
460 
461 		reqoffset = SVE_SIG_PREG_OFFSET(vq, reg_num) -
462 				SVE_SIG_REGS_OFFSET;
463 		reqlen = KVM_SVE_PREG_SIZE;
464 		maxlen = SVE_SIG_PREG_SIZE(vq);
465 	} else {
466 		return -EINVAL;
467 	}
468 
469 	sve_state_size = vcpu_sve_state_size(vcpu);
470 	if (WARN_ON(!sve_state_size))
471 		return -EINVAL;
472 
473 	region->koffset = array_index_nospec(reqoffset, sve_state_size);
474 	region->klen = min(maxlen, reqlen);
475 	region->upad = reqlen - region->klen;
476 
477 	return 0;
478 }
479 
get_sve_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)480 static int get_sve_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
481 {
482 	int ret;
483 	struct sve_state_reg_region region;
484 	char __user *uptr = (char __user *)reg->addr;
485 
486 	/* Handle the KVM_REG_ARM64_SVE_VLS pseudo-reg as a special case: */
487 	if (reg->id == KVM_REG_ARM64_SVE_VLS)
488 		return get_sve_vls(vcpu, reg);
489 
490 	/* Try to interpret reg ID as an architectural SVE register... */
491 	ret = sve_reg_to_region(&region, vcpu, reg);
492 	if (ret)
493 		return ret;
494 
495 	if (!kvm_arm_vcpu_sve_finalized(vcpu))
496 		return -EPERM;
497 
498 	if (copy_to_user(uptr, vcpu->arch.sve_state + region.koffset,
499 			 region.klen) ||
500 	    clear_user(uptr + region.klen, region.upad))
501 		return -EFAULT;
502 
503 	return 0;
504 }
505 
set_sve_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)506 static int set_sve_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
507 {
508 	int ret;
509 	struct sve_state_reg_region region;
510 	const char __user *uptr = (const char __user *)reg->addr;
511 
512 	/* Handle the KVM_REG_ARM64_SVE_VLS pseudo-reg as a special case: */
513 	if (reg->id == KVM_REG_ARM64_SVE_VLS)
514 		return set_sve_vls(vcpu, reg);
515 
516 	/* Try to interpret reg ID as an architectural SVE register... */
517 	ret = sve_reg_to_region(&region, vcpu, reg);
518 	if (ret)
519 		return ret;
520 
521 	if (!kvm_arm_vcpu_sve_finalized(vcpu))
522 		return -EPERM;
523 
524 	if (copy_from_user(vcpu->arch.sve_state + region.koffset, uptr,
525 			   region.klen))
526 		return -EFAULT;
527 
528 	return 0;
529 }
530 
kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)531 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
532 {
533 	return -EINVAL;
534 }
535 
kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)536 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
537 {
538 	return -EINVAL;
539 }
540 
copy_core_reg_indices(const struct kvm_vcpu * vcpu,u64 __user * uindices)541 static int copy_core_reg_indices(const struct kvm_vcpu *vcpu,
542 				 u64 __user *uindices)
543 {
544 	unsigned int i;
545 	int n = 0;
546 
547 	for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
548 		u64 reg = KVM_REG_ARM64 | KVM_REG_ARM_CORE | i;
549 		int size = core_reg_size_from_offset(vcpu, i);
550 
551 		if (size < 0)
552 			continue;
553 
554 		switch (size) {
555 		case sizeof(__u32):
556 			reg |= KVM_REG_SIZE_U32;
557 			break;
558 
559 		case sizeof(__u64):
560 			reg |= KVM_REG_SIZE_U64;
561 			break;
562 
563 		case sizeof(__uint128_t):
564 			reg |= KVM_REG_SIZE_U128;
565 			break;
566 
567 		default:
568 			WARN_ON(1);
569 			continue;
570 		}
571 
572 		if (uindices) {
573 			if (put_user(reg, uindices))
574 				return -EFAULT;
575 			uindices++;
576 		}
577 
578 		n++;
579 	}
580 
581 	return n;
582 }
583 
num_core_regs(const struct kvm_vcpu * vcpu)584 static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)
585 {
586 	return copy_core_reg_indices(vcpu, NULL);
587 }
588 
589 /**
590  * ARM64 versions of the TIMER registers, always available on arm64
591  */
592 
593 #define NUM_TIMER_REGS 3
594 
is_timer_reg(u64 index)595 static bool is_timer_reg(u64 index)
596 {
597 	switch (index) {
598 	case KVM_REG_ARM_TIMER_CTL:
599 	case KVM_REG_ARM_TIMER_CNT:
600 	case KVM_REG_ARM_TIMER_CVAL:
601 		return true;
602 	}
603 	return false;
604 }
605 
copy_timer_indices(struct kvm_vcpu * vcpu,u64 __user * uindices)606 static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
607 {
608 	if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
609 		return -EFAULT;
610 	uindices++;
611 	if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
612 		return -EFAULT;
613 	uindices++;
614 	if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
615 		return -EFAULT;
616 
617 	return 0;
618 }
619 
set_timer_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)620 static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
621 {
622 	void __user *uaddr = (void __user *)(long)reg->addr;
623 	u64 val;
624 	int ret;
625 
626 	ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
627 	if (ret != 0)
628 		return -EFAULT;
629 
630 	return kvm_arm_timer_set_reg(vcpu, reg->id, val);
631 }
632 
get_timer_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)633 static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
634 {
635 	void __user *uaddr = (void __user *)(long)reg->addr;
636 	u64 val;
637 
638 	val = kvm_arm_timer_get_reg(vcpu, reg->id);
639 	return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)) ? -EFAULT : 0;
640 }
641 
num_sve_regs(const struct kvm_vcpu * vcpu)642 static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
643 {
644 	const unsigned int slices = vcpu_sve_slices(vcpu);
645 
646 	if (!vcpu_has_sve(vcpu))
647 		return 0;
648 
649 	/* Policed by KVM_GET_REG_LIST: */
650 	WARN_ON(!kvm_arm_vcpu_sve_finalized(vcpu));
651 
652 	return slices * (SVE_NUM_PREGS + SVE_NUM_ZREGS + 1 /* FFR */)
653 		+ 1; /* KVM_REG_ARM64_SVE_VLS */
654 }
655 
copy_sve_reg_indices(const struct kvm_vcpu * vcpu,u64 __user * uindices)656 static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,
657 				u64 __user *uindices)
658 {
659 	const unsigned int slices = vcpu_sve_slices(vcpu);
660 	u64 reg;
661 	unsigned int i, n;
662 	int num_regs = 0;
663 
664 	if (!vcpu_has_sve(vcpu))
665 		return 0;
666 
667 	/* Policed by KVM_GET_REG_LIST: */
668 	WARN_ON(!kvm_arm_vcpu_sve_finalized(vcpu));
669 
670 	/*
671 	 * Enumerate this first, so that userspace can save/restore in
672 	 * the order reported by KVM_GET_REG_LIST:
673 	 */
674 	reg = KVM_REG_ARM64_SVE_VLS;
675 	if (put_user(reg, uindices++))
676 		return -EFAULT;
677 	++num_regs;
678 
679 	for (i = 0; i < slices; i++) {
680 		for (n = 0; n < SVE_NUM_ZREGS; n++) {
681 			reg = KVM_REG_ARM64_SVE_ZREG(n, i);
682 			if (put_user(reg, uindices++))
683 				return -EFAULT;
684 			num_regs++;
685 		}
686 
687 		for (n = 0; n < SVE_NUM_PREGS; n++) {
688 			reg = KVM_REG_ARM64_SVE_PREG(n, i);
689 			if (put_user(reg, uindices++))
690 				return -EFAULT;
691 			num_regs++;
692 		}
693 
694 		reg = KVM_REG_ARM64_SVE_FFR(i);
695 		if (put_user(reg, uindices++))
696 			return -EFAULT;
697 		num_regs++;
698 	}
699 
700 	return num_regs;
701 }
702 
703 /**
704  * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
705  *
706  * This is for all registers.
707  */
kvm_arm_num_regs(struct kvm_vcpu * vcpu)708 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
709 {
710 	unsigned long res = 0;
711 
712 	res += num_core_regs(vcpu);
713 	res += num_sve_regs(vcpu);
714 	res += kvm_arm_num_sys_reg_descs(vcpu);
715 	res += kvm_arm_get_fw_num_regs(vcpu);
716 	res += NUM_TIMER_REGS;
717 
718 	return res;
719 }
720 
721 /**
722  * kvm_arm_copy_reg_indices - get indices of all registers.
723  *
724  * We do core registers right here, then we append system regs.
725  */
kvm_arm_copy_reg_indices(struct kvm_vcpu * vcpu,u64 __user * uindices)726 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
727 {
728 	int ret;
729 
730 	ret = copy_core_reg_indices(vcpu, uindices);
731 	if (ret < 0)
732 		return ret;
733 	uindices += ret;
734 
735 	ret = copy_sve_reg_indices(vcpu, uindices);
736 	if (ret < 0)
737 		return ret;
738 	uindices += ret;
739 
740 	ret = kvm_arm_copy_fw_reg_indices(vcpu, uindices);
741 	if (ret < 0)
742 		return ret;
743 	uindices += kvm_arm_get_fw_num_regs(vcpu);
744 
745 	ret = copy_timer_indices(vcpu, uindices);
746 	if (ret < 0)
747 		return ret;
748 	uindices += NUM_TIMER_REGS;
749 
750 	return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
751 }
752 
kvm_arm_get_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)753 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
754 {
755 	/* We currently use nothing arch-specific in upper 32 bits */
756 	if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
757 		return -EINVAL;
758 
759 	switch (reg->id & KVM_REG_ARM_COPROC_MASK) {
760 	case KVM_REG_ARM_CORE:	return get_core_reg(vcpu, reg);
761 	case KVM_REG_ARM_FW:
762 	case KVM_REG_ARM_FW_FEAT_BMAP:
763 		return kvm_arm_get_fw_reg(vcpu, reg);
764 	case KVM_REG_ARM64_SVE:	return get_sve_reg(vcpu, reg);
765 	}
766 
767 	if (is_timer_reg(reg->id))
768 		return get_timer_reg(vcpu, reg);
769 
770 	return kvm_arm_sys_reg_get_reg(vcpu, reg);
771 }
772 
kvm_arm_set_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)773 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
774 {
775 	/* We currently use nothing arch-specific in upper 32 bits */
776 	if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
777 		return -EINVAL;
778 
779 	switch (reg->id & KVM_REG_ARM_COPROC_MASK) {
780 	case KVM_REG_ARM_CORE:	return set_core_reg(vcpu, reg);
781 	case KVM_REG_ARM_FW:
782 	case KVM_REG_ARM_FW_FEAT_BMAP:
783 		return kvm_arm_set_fw_reg(vcpu, reg);
784 	case KVM_REG_ARM64_SVE:	return set_sve_reg(vcpu, reg);
785 	}
786 
787 	if (is_timer_reg(reg->id))
788 		return set_timer_reg(vcpu, reg);
789 
790 	return kvm_arm_sys_reg_set_reg(vcpu, reg);
791 }
792 
kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)793 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
794 				  struct kvm_sregs *sregs)
795 {
796 	return -EINVAL;
797 }
798 
kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)799 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
800 				  struct kvm_sregs *sregs)
801 {
802 	return -EINVAL;
803 }
804 
__kvm_arm_vcpu_get_events(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)805 int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
806 			      struct kvm_vcpu_events *events)
807 {
808 	events->exception.serror_pending = !!(vcpu->arch.hcr_el2 & HCR_VSE);
809 	events->exception.serror_has_esr = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
810 
811 	if (events->exception.serror_pending && events->exception.serror_has_esr)
812 		events->exception.serror_esr = vcpu_get_vsesr(vcpu);
813 
814 	/*
815 	 * We never return a pending ext_dabt here because we deliver it to
816 	 * the virtual CPU directly when setting the event and it's no longer
817 	 * 'pending' at this point.
818 	 */
819 
820 	return 0;
821 }
822 
__kvm_arm_vcpu_set_events(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)823 int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
824 			      struct kvm_vcpu_events *events)
825 {
826 	bool serror_pending = events->exception.serror_pending;
827 	bool has_esr = events->exception.serror_has_esr;
828 	bool ext_dabt_pending = events->exception.ext_dabt_pending;
829 
830 	if (serror_pending && has_esr) {
831 		if (!cpus_have_const_cap(ARM64_HAS_RAS_EXTN))
832 			return -EINVAL;
833 
834 		if (!((events->exception.serror_esr) & ~ESR_ELx_ISS_MASK))
835 			kvm_set_sei_esr(vcpu, events->exception.serror_esr);
836 		else
837 			return -EINVAL;
838 	} else if (serror_pending) {
839 		kvm_inject_vabt(vcpu);
840 	}
841 
842 	if (ext_dabt_pending)
843 		kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
844 
845 	return 0;
846 }
847 
kvm_target_cpu(void)848 u32 __attribute_const__ kvm_target_cpu(void)
849 {
850 	unsigned long implementor = read_cpuid_implementor();
851 	unsigned long part_number = read_cpuid_part_number();
852 
853 	switch (implementor) {
854 	case ARM_CPU_IMP_ARM:
855 		switch (part_number) {
856 		case ARM_CPU_PART_AEM_V8:
857 			return KVM_ARM_TARGET_AEM_V8;
858 		case ARM_CPU_PART_FOUNDATION:
859 			return KVM_ARM_TARGET_FOUNDATION_V8;
860 		case ARM_CPU_PART_CORTEX_A53:
861 			return KVM_ARM_TARGET_CORTEX_A53;
862 		case ARM_CPU_PART_CORTEX_A57:
863 			return KVM_ARM_TARGET_CORTEX_A57;
864 		}
865 		break;
866 	case ARM_CPU_IMP_APM:
867 		switch (part_number) {
868 		case APM_CPU_PART_POTENZA:
869 			return KVM_ARM_TARGET_XGENE_POTENZA;
870 		}
871 		break;
872 	}
873 
874 	/* Return a default generic target */
875 	return KVM_ARM_TARGET_GENERIC_V8;
876 }
877 
kvm_vcpu_preferred_target(struct kvm_vcpu_init * init)878 void kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
879 {
880 	u32 target = kvm_target_cpu();
881 
882 	memset(init, 0, sizeof(*init));
883 
884 	/*
885 	 * For now, we don't return any features.
886 	 * In future, we might use features to return target
887 	 * specific features available for the preferred
888 	 * target type.
889 	 */
890 	init->target = (__u32)target;
891 }
892 
kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)893 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
894 {
895 	return -EINVAL;
896 }
897 
kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)898 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
899 {
900 	return -EINVAL;
901 }
902 
kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu * vcpu,struct kvm_translation * tr)903 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
904 				  struct kvm_translation *tr)
905 {
906 	return -EINVAL;
907 }
908 
909 /**
910  * kvm_arch_vcpu_ioctl_set_guest_debug - set up guest debugging
911  * @kvm:	pointer to the KVM struct
912  * @kvm_guest_debug: the ioctl data buffer
913  *
914  * This sets up and enables the VM for guest debugging. Userspace
915  * passes in a control flag to enable different debug types and
916  * potentially other architecture specific information in the rest of
917  * the structure.
918  */
kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu * vcpu,struct kvm_guest_debug * dbg)919 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
920 					struct kvm_guest_debug *dbg)
921 {
922 	int ret = 0;
923 
924 	trace_kvm_set_guest_debug(vcpu, dbg->control);
925 
926 	if (dbg->control & ~KVM_GUESTDBG_VALID_MASK) {
927 		ret = -EINVAL;
928 		goto out;
929 	}
930 
931 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
932 		vcpu->guest_debug = dbg->control;
933 
934 		/* Hardware assisted Break and Watch points */
935 		if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
936 			vcpu->arch.external_debug_state = dbg->arch;
937 		}
938 
939 	} else {
940 		/* If not enabled clear all flags */
941 		vcpu->guest_debug = 0;
942 		vcpu_clear_flag(vcpu, DBG_SS_ACTIVE_PENDING);
943 	}
944 
945 out:
946 	return ret;
947 }
948 
kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)949 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
950 			       struct kvm_device_attr *attr)
951 {
952 	int ret;
953 
954 	switch (attr->group) {
955 	case KVM_ARM_VCPU_PMU_V3_CTRL:
956 		mutex_lock(&vcpu->kvm->arch.config_lock);
957 		ret = kvm_arm_pmu_v3_set_attr(vcpu, attr);
958 		mutex_unlock(&vcpu->kvm->arch.config_lock);
959 		break;
960 	case KVM_ARM_VCPU_TIMER_CTRL:
961 		ret = kvm_arm_timer_set_attr(vcpu, attr);
962 		break;
963 	case KVM_ARM_VCPU_PVTIME_CTRL:
964 		ret = kvm_arm_pvtime_set_attr(vcpu, attr);
965 		break;
966 	default:
967 		ret = -ENXIO;
968 		break;
969 	}
970 
971 	return ret;
972 }
973 
kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)974 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
975 			       struct kvm_device_attr *attr)
976 {
977 	int ret;
978 
979 	switch (attr->group) {
980 	case KVM_ARM_VCPU_PMU_V3_CTRL:
981 		ret = kvm_arm_pmu_v3_get_attr(vcpu, attr);
982 		break;
983 	case KVM_ARM_VCPU_TIMER_CTRL:
984 		ret = kvm_arm_timer_get_attr(vcpu, attr);
985 		break;
986 	case KVM_ARM_VCPU_PVTIME_CTRL:
987 		ret = kvm_arm_pvtime_get_attr(vcpu, attr);
988 		break;
989 	default:
990 		ret = -ENXIO;
991 		break;
992 	}
993 
994 	return ret;
995 }
996 
kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)997 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
998 			       struct kvm_device_attr *attr)
999 {
1000 	int ret;
1001 
1002 	switch (attr->group) {
1003 	case KVM_ARM_VCPU_PMU_V3_CTRL:
1004 		ret = kvm_arm_pmu_v3_has_attr(vcpu, attr);
1005 		break;
1006 	case KVM_ARM_VCPU_TIMER_CTRL:
1007 		ret = kvm_arm_timer_has_attr(vcpu, attr);
1008 		break;
1009 	case KVM_ARM_VCPU_PVTIME_CTRL:
1010 		ret = kvm_arm_pvtime_has_attr(vcpu, attr);
1011 		break;
1012 	default:
1013 		ret = -ENXIO;
1014 		break;
1015 	}
1016 
1017 	return ret;
1018 }
1019 
kvm_vm_ioctl_mte_copy_tags(struct kvm * kvm,struct kvm_arm_copy_mte_tags * copy_tags)1020 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
1021 				struct kvm_arm_copy_mte_tags *copy_tags)
1022 {
1023 	gpa_t guest_ipa = copy_tags->guest_ipa;
1024 	size_t length = copy_tags->length;
1025 	void __user *tags = copy_tags->addr;
1026 	gpa_t gfn;
1027 	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
1028 	int ret = 0;
1029 
1030 	if (!kvm_has_mte(kvm))
1031 		return -EINVAL;
1032 
1033 	if (copy_tags->reserved[0] || copy_tags->reserved[1])
1034 		return -EINVAL;
1035 
1036 	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
1037 		return -EINVAL;
1038 
1039 	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
1040 		return -EINVAL;
1041 
1042 	gfn = gpa_to_gfn(guest_ipa);
1043 
1044 	mutex_lock(&kvm->slots_lock);
1045 
1046 	while (length > 0) {
1047 		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
1048 		void *maddr;
1049 		unsigned long num_tags;
1050 		struct page *page;
1051 
1052 		if (is_error_noslot_pfn(pfn)) {
1053 			ret = -EFAULT;
1054 			goto out;
1055 		}
1056 
1057 		page = pfn_to_online_page(pfn);
1058 		if (!page) {
1059 			/* Reject ZONE_DEVICE memory */
1060 			ret = -EFAULT;
1061 			goto out;
1062 		}
1063 		maddr = page_address(page);
1064 
1065 		if (!write) {
1066 			if (page_mte_tagged(page))
1067 				num_tags = mte_copy_tags_to_user(tags, maddr,
1068 							MTE_GRANULES_PER_PAGE);
1069 			else
1070 				/* No tags in memory, so write zeros */
1071 				num_tags = MTE_GRANULES_PER_PAGE -
1072 					clear_user(tags, MTE_GRANULES_PER_PAGE);
1073 			kvm_release_pfn_clean(pfn);
1074 		} else {
1075 			num_tags = mte_copy_tags_from_user(maddr, tags,
1076 							MTE_GRANULES_PER_PAGE);
1077 
1078 			/*
1079 			 * Set the flag after checking the write
1080 			 * completed fully
1081 			 */
1082 			if (num_tags == MTE_GRANULES_PER_PAGE)
1083 				set_page_mte_tagged(page);
1084 
1085 			kvm_release_pfn_dirty(pfn);
1086 		}
1087 
1088 		if (num_tags != MTE_GRANULES_PER_PAGE) {
1089 			ret = -EFAULT;
1090 			goto out;
1091 		}
1092 
1093 		gfn++;
1094 		tags += num_tags;
1095 		length -= PAGE_SIZE;
1096 	}
1097 
1098 out:
1099 	mutex_unlock(&kvm->slots_lock);
1100 	/* If some data has been copied report the number of bytes copied */
1101 	if (length != copy_tags->length)
1102 		return copy_tags->length - length;
1103 	return ret;
1104 }
1105