Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / arm64 / kvm / guest.c
blobd7e3299a773460fcd3b39930864e078e72453475
1 /*
2 * Copyright (C) 2012,2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
5 * Derived from arch/arm/kvm/guest.c:
6 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
7 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/errno.h>
23 #include <linux/err.h>
24 #include <linux/kvm_host.h>
25 #include <linux/module.h>
26 #include <linux/vmalloc.h>
27 #include <linux/fs.h>
28 #include <asm/cputype.h>
29 #include <linux/uaccess.h>
30 #include <asm/kvm.h>
31 #include <asm/kvm_emulate.h>
32 #include <asm/kvm_coproc.h>
34 #include "trace.h"
36 #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
37 #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
39 struct kvm_stats_debugfs_item debugfs_entries[] = {
40 VCPU_STAT(hvc_exit_stat),
41 VCPU_STAT(wfe_exit_stat),
42 VCPU_STAT(wfi_exit_stat),
43 VCPU_STAT(mmio_exit_user),
44 VCPU_STAT(mmio_exit_kernel),
45 VCPU_STAT(exits),
46 { NULL }
49 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
51 return 0;
54 static u64 core_reg_offset_from_id(u64 id)
56 return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
59 static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
62 * Because the kvm_regs structure is a mix of 32, 64 and
63 * 128bit fields, we index it as if it was a 32bit
64 * array. Hence below, nr_regs is the number of entries, and
65 * off the index in the "array".
67 __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
68 struct kvm_regs *regs = vcpu_gp_regs(vcpu);
69 int nr_regs = sizeof(*regs) / sizeof(__u32);
70 u32 off;
72 /* Our ID is an index into the kvm_regs struct. */
73 off = core_reg_offset_from_id(reg->id);
74 if (off >= nr_regs ||
75 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
76 return -ENOENT;
78 if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id)))
79 return -EFAULT;
81 return 0;
84 static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
86 __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
87 struct kvm_regs *regs = vcpu_gp_regs(vcpu);
88 int nr_regs = sizeof(*regs) / sizeof(__u32);
89 __uint128_t tmp;
90 void *valp = &tmp;
91 u64 off;
92 int err = 0;
94 /* Our ID is an index into the kvm_regs struct. */
95 off = core_reg_offset_from_id(reg->id);
96 if (off >= nr_regs ||
97 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
98 return -ENOENT;
100 if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
101 return -EINVAL;
103 if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
104 err = -EFAULT;
105 goto out;
108 if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
109 u32 mode = (*(u32 *)valp) & COMPAT_PSR_MODE_MASK;
110 switch (mode) {
111 case COMPAT_PSR_MODE_USR:
112 case COMPAT_PSR_MODE_FIQ:
113 case COMPAT_PSR_MODE_IRQ:
114 case COMPAT_PSR_MODE_SVC:
115 case COMPAT_PSR_MODE_ABT:
116 case COMPAT_PSR_MODE_UND:
117 case PSR_MODE_EL0t:
118 case PSR_MODE_EL1t:
119 case PSR_MODE_EL1h:
120 break;
121 default:
122 err = -EINVAL;
123 goto out;
127 memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
128 out:
129 return err;
132 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
134 return -EINVAL;
137 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
139 return -EINVAL;
142 static unsigned long num_core_regs(void)
144 return sizeof(struct kvm_regs) / sizeof(__u32);
148 * ARM64 versions of the TIMER registers, always available on arm64
151 #define NUM_TIMER_REGS 3
153 static bool is_timer_reg(u64 index)
155 switch (index) {
156 case KVM_REG_ARM_TIMER_CTL:
157 case KVM_REG_ARM_TIMER_CNT:
158 case KVM_REG_ARM_TIMER_CVAL:
159 return true;
161 return false;
164 static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
166 if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
167 return -EFAULT;
168 uindices++;
169 if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
170 return -EFAULT;
171 uindices++;
172 if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
173 return -EFAULT;
175 return 0;
178 static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
180 void __user *uaddr = (void __user *)(long)reg->addr;
181 u64 val;
182 int ret;
184 ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
185 if (ret != 0)
186 return -EFAULT;
188 return kvm_arm_timer_set_reg(vcpu, reg->id, val);
191 static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
193 void __user *uaddr = (void __user *)(long)reg->addr;
194 u64 val;
196 val = kvm_arm_timer_get_reg(vcpu, reg->id);
197 return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)) ? -EFAULT : 0;
201 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
203 * This is for all registers.
205 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
207 return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu)
208 + NUM_TIMER_REGS;
212 * kvm_arm_copy_reg_indices - get indices of all registers.
214 * We do core registers right here, then we append system regs.
216 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
218 unsigned int i;
219 const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
220 int ret;
222 for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
223 if (put_user(core_reg | i, uindices))
224 return -EFAULT;
225 uindices++;
228 ret = copy_timer_indices(vcpu, uindices);
229 if (ret)
230 return ret;
231 uindices += NUM_TIMER_REGS;
233 return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
236 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
238 /* We currently use nothing arch-specific in upper 32 bits */
239 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
240 return -EINVAL;
242 /* Register group 16 means we want a core register. */
243 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
244 return get_core_reg(vcpu, reg);
246 if (is_timer_reg(reg->id))
247 return get_timer_reg(vcpu, reg);
249 return kvm_arm_sys_reg_get_reg(vcpu, reg);
252 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
254 /* We currently use nothing arch-specific in upper 32 bits */
255 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
256 return -EINVAL;
258 /* Register group 16 means we set a core register. */
259 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
260 return set_core_reg(vcpu, reg);
262 if (is_timer_reg(reg->id))
263 return set_timer_reg(vcpu, reg);
265 return kvm_arm_sys_reg_set_reg(vcpu, reg);
268 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
269 struct kvm_sregs *sregs)
271 return -EINVAL;
274 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
275 struct kvm_sregs *sregs)
277 return -EINVAL;
280 int __attribute_const__ kvm_target_cpu(void)
282 unsigned long implementor = read_cpuid_implementor();
283 unsigned long part_number = read_cpuid_part_number();
285 switch (implementor) {
286 case ARM_CPU_IMP_ARM:
287 switch (part_number) {
288 case ARM_CPU_PART_AEM_V8:
289 return KVM_ARM_TARGET_AEM_V8;
290 case ARM_CPU_PART_FOUNDATION:
291 return KVM_ARM_TARGET_FOUNDATION_V8;
292 case ARM_CPU_PART_CORTEX_A53:
293 return KVM_ARM_TARGET_CORTEX_A53;
294 case ARM_CPU_PART_CORTEX_A57:
295 return KVM_ARM_TARGET_CORTEX_A57;
297 break;
298 case ARM_CPU_IMP_APM:
299 switch (part_number) {
300 case APM_CPU_PART_POTENZA:
301 return KVM_ARM_TARGET_XGENE_POTENZA;
303 break;
306 /* Return a default generic target */
307 return KVM_ARM_TARGET_GENERIC_V8;
310 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
312 int target = kvm_target_cpu();
314 if (target < 0)
315 return -ENODEV;
317 memset(init, 0, sizeof(*init));
320 * For now, we don't return any features.
321 * In future, we might use features to return target
322 * specific features available for the preferred
323 * target type.
325 init->target = (__u32)target;
327 return 0;
330 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
332 return -EINVAL;
335 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
337 return -EINVAL;
340 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
341 struct kvm_translation *tr)
343 return -EINVAL;
346 #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
347 KVM_GUESTDBG_USE_SW_BP | \
348 KVM_GUESTDBG_USE_HW | \
349 KVM_GUESTDBG_SINGLESTEP)
352 * kvm_arch_vcpu_ioctl_set_guest_debug - set up guest debugging
353 * @kvm: pointer to the KVM struct
354 * @kvm_guest_debug: the ioctl data buffer
356 * This sets up and enables the VM for guest debugging. Userspace
357 * passes in a control flag to enable different debug types and
358 * potentially other architecture specific information in the rest of
359 * the structure.
361 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
362 struct kvm_guest_debug *dbg)
364 int ret = 0;
366 vcpu_load(vcpu);
368 trace_kvm_set_guest_debug(vcpu, dbg->control);
370 if (dbg->control & ~KVM_GUESTDBG_VALID_MASK) {
371 ret = -EINVAL;
372 goto out;
375 if (dbg->control & KVM_GUESTDBG_ENABLE) {
376 vcpu->guest_debug = dbg->control;
378 /* Hardware assisted Break and Watch points */
379 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
380 vcpu->arch.external_debug_state = dbg->arch;
383 } else {
384 /* If not enabled clear all flags */
385 vcpu->guest_debug = 0;
388 out:
389 vcpu_put(vcpu);
390 return ret;
393 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
394 struct kvm_device_attr *attr)
396 int ret;
398 switch (attr->group) {
399 case KVM_ARM_VCPU_PMU_V3_CTRL:
400 ret = kvm_arm_pmu_v3_set_attr(vcpu, attr);
401 break;
402 case KVM_ARM_VCPU_TIMER_CTRL:
403 ret = kvm_arm_timer_set_attr(vcpu, attr);
404 break;
405 default:
406 ret = -ENXIO;
407 break;
410 return ret;
413 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
414 struct kvm_device_attr *attr)
416 int ret;
418 switch (attr->group) {
419 case KVM_ARM_VCPU_PMU_V3_CTRL:
420 ret = kvm_arm_pmu_v3_get_attr(vcpu, attr);
421 break;
422 case KVM_ARM_VCPU_TIMER_CTRL:
423 ret = kvm_arm_timer_get_attr(vcpu, attr);
424 break;
425 default:
426 ret = -ENXIO;
427 break;
430 return ret;
433 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
434 struct kvm_device_attr *attr)
436 int ret;
438 switch (attr->group) {
439 case KVM_ARM_VCPU_PMU_V3_CTRL:
440 ret = kvm_arm_pmu_v3_has_attr(vcpu, attr);
441 break;
442 case KVM_ARM_VCPU_TIMER_CTRL:
443 ret = kvm_arm_timer_has_attr(vcpu, attr);
444 break;
445 default:
446 ret = -ENXIO;
447 break;
450 return ret;