2 * Copyright (C) 2012,2013 - ARM Ltd
5 * Derived from arch/arm/kvm/guest.c:
6 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
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>
28 #include <asm/cputype.h>
29 #include <linux/uaccess.h>
31 #include <asm/kvm_emulate.h>
32 #include <asm/kvm_coproc.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),
49 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
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);
72 /* Our ID is an index into the kvm_regs struct. */
73 off = core_reg_offset_from_id(reg->id);
75 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
78 if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id)))
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);
94 /* Our ID is an index into the kvm_regs struct. */
95 off = core_reg_offset_from_id(reg->id);
97 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
100 if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
103 if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
108 if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
109 u32 mode = (*(u32 *)valp) & COMPAT_PSR_MODE_MASK;
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:
127 memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
132 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
137 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
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)
156 case KVM_REG_ARM_TIMER_CTL:
157 case KVM_REG_ARM_TIMER_CNT:
158 case KVM_REG_ARM_TIMER_CVAL:
164 static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
166 if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
169 if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
172 if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
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;
184 ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
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;
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)
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)
219 const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
222 for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
223 if (put_user(core_reg | i, uindices))
228 ret = copy_timer_indices(vcpu, uindices);
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)
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)
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)
274 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
275 struct kvm_sregs *sregs)
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;
298 case ARM_CPU_IMP_APM:
299 switch (part_number) {
300 case APM_CPU_PART_POTENZA:
301 return KVM_ARM_TARGET_XGENE_POTENZA;
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();
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
325 init->target = (__u32)target;
330 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
335 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
340 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
341 struct kvm_translation *tr)
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
361 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
362 struct kvm_guest_debug *dbg)
364 trace_kvm_set_guest_debug(vcpu, dbg->control);
366 if (dbg->control & ~KVM_GUESTDBG_VALID_MASK)
369 if (dbg->control & KVM_GUESTDBG_ENABLE) {
370 vcpu->guest_debug = dbg->control;
372 /* Hardware assisted Break and Watch points */
373 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
374 vcpu->arch.external_debug_state = dbg->arch;
378 /* If not enabled clear all flags */
379 vcpu->guest_debug = 0;
384 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
385 struct kvm_device_attr *attr)
389 switch (attr->group) {
390 case KVM_ARM_VCPU_PMU_V3_CTRL:
391 ret = kvm_arm_pmu_v3_set_attr(vcpu, attr);
393 case KVM_ARM_VCPU_TIMER_CTRL:
394 ret = kvm_arm_timer_set_attr(vcpu, attr);
404 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
405 struct kvm_device_attr *attr)
409 switch (attr->group) {
410 case KVM_ARM_VCPU_PMU_V3_CTRL:
411 ret = kvm_arm_pmu_v3_get_attr(vcpu, attr);
413 case KVM_ARM_VCPU_TIMER_CTRL:
414 ret = kvm_arm_timer_get_attr(vcpu, attr);
424 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
425 struct kvm_device_attr *attr)
429 switch (attr->group) {
430 case KVM_ARM_VCPU_PMU_V3_CTRL:
431 ret = kvm_arm_pmu_v3_has_attr(vcpu, attr);
433 case KVM_ARM_VCPU_TIMER_CTRL:
434 ret = kvm_arm_timer_has_attr(vcpu, attr);