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>
28 #include <kvm/arm_psci.h>
29 #include <asm/cputype.h>
30 #include <linux/uaccess.h>
32 #include <asm/kvm_emulate.h>
33 #include <asm/kvm_coproc.h>
37 #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
38 #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
40 struct kvm_stats_debugfs_item debugfs_entries
[] = {
41 VCPU_STAT(hvc_exit_stat
),
42 VCPU_STAT(wfe_exit_stat
),
43 VCPU_STAT(wfi_exit_stat
),
44 VCPU_STAT(mmio_exit_user
),
45 VCPU_STAT(mmio_exit_kernel
),
50 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
55 static u64
core_reg_offset_from_id(u64 id
)
57 return id
& ~(KVM_REG_ARCH_MASK
| KVM_REG_SIZE_MASK
| KVM_REG_ARM_CORE
);
60 static int get_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
63 * Because the kvm_regs structure is a mix of 32, 64 and
64 * 128bit fields, we index it as if it was a 32bit
65 * array. Hence below, nr_regs is the number of entries, and
66 * off the index in the "array".
68 __u32 __user
*uaddr
= (__u32 __user
*)(unsigned long)reg
->addr
;
69 struct kvm_regs
*regs
= vcpu_gp_regs(vcpu
);
70 int nr_regs
= sizeof(*regs
) / sizeof(__u32
);
73 /* Our ID is an index into the kvm_regs struct. */
74 off
= core_reg_offset_from_id(reg
->id
);
76 (off
+ (KVM_REG_SIZE(reg
->id
) / sizeof(__u32
))) >= nr_regs
)
79 if (copy_to_user(uaddr
, ((u32
*)regs
) + off
, KVM_REG_SIZE(reg
->id
)))
85 static int set_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
87 __u32 __user
*uaddr
= (__u32 __user
*)(unsigned long)reg
->addr
;
88 struct kvm_regs
*regs
= vcpu_gp_regs(vcpu
);
89 int nr_regs
= sizeof(*regs
) / sizeof(__u32
);
95 /* Our ID is an index into the kvm_regs struct. */
96 off
= core_reg_offset_from_id(reg
->id
);
98 (off
+ (KVM_REG_SIZE(reg
->id
) / sizeof(__u32
))) >= nr_regs
)
101 if (KVM_REG_SIZE(reg
->id
) > sizeof(tmp
))
104 if (copy_from_user(valp
, uaddr
, KVM_REG_SIZE(reg
->id
))) {
109 if (off
== KVM_REG_ARM_CORE_REG(regs
.pstate
)) {
110 u32 mode
= (*(u32
*)valp
) & COMPAT_PSR_MODE_MASK
;
112 case COMPAT_PSR_MODE_USR
:
113 case COMPAT_PSR_MODE_FIQ
:
114 case COMPAT_PSR_MODE_IRQ
:
115 case COMPAT_PSR_MODE_SVC
:
116 case COMPAT_PSR_MODE_ABT
:
117 case COMPAT_PSR_MODE_UND
:
128 memcpy((u32
*)regs
+ off
, valp
, KVM_REG_SIZE(reg
->id
));
133 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
138 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
143 static unsigned long num_core_regs(void)
145 return sizeof(struct kvm_regs
) / sizeof(__u32
);
149 * ARM64 versions of the TIMER registers, always available on arm64
152 #define NUM_TIMER_REGS 3
154 static bool is_timer_reg(u64 index
)
157 case KVM_REG_ARM_TIMER_CTL
:
158 case KVM_REG_ARM_TIMER_CNT
:
159 case KVM_REG_ARM_TIMER_CVAL
:
165 static int copy_timer_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
167 if (put_user(KVM_REG_ARM_TIMER_CTL
, uindices
))
170 if (put_user(KVM_REG_ARM_TIMER_CNT
, uindices
))
173 if (put_user(KVM_REG_ARM_TIMER_CVAL
, uindices
))
179 static int set_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
181 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
185 ret
= copy_from_user(&val
, uaddr
, KVM_REG_SIZE(reg
->id
));
189 return kvm_arm_timer_set_reg(vcpu
, reg
->id
, val
);
192 static int get_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
194 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
197 val
= kvm_arm_timer_get_reg(vcpu
, reg
->id
);
198 return copy_to_user(uaddr
, &val
, KVM_REG_SIZE(reg
->id
)) ? -EFAULT
: 0;
202 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
204 * This is for all registers.
206 unsigned long kvm_arm_num_regs(struct kvm_vcpu
*vcpu
)
208 return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu
)
209 + kvm_arm_get_fw_num_regs(vcpu
) + NUM_TIMER_REGS
;
213 * kvm_arm_copy_reg_indices - get indices of all registers.
215 * We do core registers right here, then we append system regs.
217 int kvm_arm_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
220 const u64 core_reg
= KVM_REG_ARM64
| KVM_REG_SIZE_U64
| KVM_REG_ARM_CORE
;
223 for (i
= 0; i
< sizeof(struct kvm_regs
) / sizeof(__u32
); i
++) {
224 if (put_user(core_reg
| i
, uindices
))
229 ret
= kvm_arm_copy_fw_reg_indices(vcpu
, uindices
);
232 uindices
+= kvm_arm_get_fw_num_regs(vcpu
);
234 ret
= copy_timer_indices(vcpu
, uindices
);
237 uindices
+= NUM_TIMER_REGS
;
239 return kvm_arm_copy_sys_reg_indices(vcpu
, uindices
);
242 int kvm_arm_get_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
244 /* We currently use nothing arch-specific in upper 32 bits */
245 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM64
>> 32)
248 /* Register group 16 means we want a core register. */
249 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
250 return get_core_reg(vcpu
, reg
);
252 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_FW
)
253 return kvm_arm_get_fw_reg(vcpu
, reg
);
255 if (is_timer_reg(reg
->id
))
256 return get_timer_reg(vcpu
, reg
);
258 return kvm_arm_sys_reg_get_reg(vcpu
, reg
);
261 int kvm_arm_set_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
263 /* We currently use nothing arch-specific in upper 32 bits */
264 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM64
>> 32)
267 /* Register group 16 means we set a core register. */
268 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
269 return set_core_reg(vcpu
, reg
);
271 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_FW
)
272 return kvm_arm_set_fw_reg(vcpu
, reg
);
274 if (is_timer_reg(reg
->id
))
275 return set_timer_reg(vcpu
, reg
);
277 return kvm_arm_sys_reg_set_reg(vcpu
, reg
);
280 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
281 struct kvm_sregs
*sregs
)
286 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
287 struct kvm_sregs
*sregs
)
292 int __attribute_const__
kvm_target_cpu(void)
294 unsigned long implementor
= read_cpuid_implementor();
295 unsigned long part_number
= read_cpuid_part_number();
297 switch (implementor
) {
298 case ARM_CPU_IMP_ARM
:
299 switch (part_number
) {
300 case ARM_CPU_PART_AEM_V8
:
301 return KVM_ARM_TARGET_AEM_V8
;
302 case ARM_CPU_PART_FOUNDATION
:
303 return KVM_ARM_TARGET_FOUNDATION_V8
;
304 case ARM_CPU_PART_CORTEX_A53
:
305 return KVM_ARM_TARGET_CORTEX_A53
;
306 case ARM_CPU_PART_CORTEX_A57
:
307 return KVM_ARM_TARGET_CORTEX_A57
;
310 case ARM_CPU_IMP_APM
:
311 switch (part_number
) {
312 case APM_CPU_PART_POTENZA
:
313 return KVM_ARM_TARGET_XGENE_POTENZA
;
318 /* Return a default generic target */
319 return KVM_ARM_TARGET_GENERIC_V8
;
322 int kvm_vcpu_preferred_target(struct kvm_vcpu_init
*init
)
324 int target
= kvm_target_cpu();
329 memset(init
, 0, sizeof(*init
));
332 * For now, we don't return any features.
333 * In future, we might use features to return target
334 * specific features available for the preferred
337 init
->target
= (__u32
)target
;
342 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
347 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
352 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
353 struct kvm_translation
*tr
)
358 #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
359 KVM_GUESTDBG_USE_SW_BP | \
360 KVM_GUESTDBG_USE_HW | \
361 KVM_GUESTDBG_SINGLESTEP)
364 * kvm_arch_vcpu_ioctl_set_guest_debug - set up guest debugging
365 * @kvm: pointer to the KVM struct
366 * @kvm_guest_debug: the ioctl data buffer
368 * This sets up and enables the VM for guest debugging. Userspace
369 * passes in a control flag to enable different debug types and
370 * potentially other architecture specific information in the rest of
373 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
374 struct kvm_guest_debug
*dbg
)
378 trace_kvm_set_guest_debug(vcpu
, dbg
->control
);
380 if (dbg
->control
& ~KVM_GUESTDBG_VALID_MASK
) {
385 if (dbg
->control
& KVM_GUESTDBG_ENABLE
) {
386 vcpu
->guest_debug
= dbg
->control
;
388 /* Hardware assisted Break and Watch points */
389 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW
) {
390 vcpu
->arch
.external_debug_state
= dbg
->arch
;
394 /* If not enabled clear all flags */
395 vcpu
->guest_debug
= 0;
402 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu
*vcpu
,
403 struct kvm_device_attr
*attr
)
407 switch (attr
->group
) {
408 case KVM_ARM_VCPU_PMU_V3_CTRL
:
409 ret
= kvm_arm_pmu_v3_set_attr(vcpu
, attr
);
411 case KVM_ARM_VCPU_TIMER_CTRL
:
412 ret
= kvm_arm_timer_set_attr(vcpu
, attr
);
422 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu
*vcpu
,
423 struct kvm_device_attr
*attr
)
427 switch (attr
->group
) {
428 case KVM_ARM_VCPU_PMU_V3_CTRL
:
429 ret
= kvm_arm_pmu_v3_get_attr(vcpu
, attr
);
431 case KVM_ARM_VCPU_TIMER_CTRL
:
432 ret
= kvm_arm_timer_get_attr(vcpu
, attr
);
442 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu
*vcpu
,
443 struct kvm_device_attr
*attr
)
447 switch (attr
->group
) {
448 case KVM_ARM_VCPU_PMU_V3_CTRL
:
449 ret
= kvm_arm_pmu_v3_has_attr(vcpu
, attr
);
451 case KVM_ARM_VCPU_TIMER_CTRL
:
452 ret
= kvm_arm_timer_has_attr(vcpu
, attr
);