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 <asm/cputype.h>
29 #include <asm/uaccess.h>
31 #include <asm/kvm_asm.h>
32 #include <asm/kvm_emulate.h>
33 #include <asm/kvm_coproc.h>
37 struct kvm_stats_debugfs_item debugfs_entries
[] = {
41 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
46 static u64
core_reg_offset_from_id(u64 id
)
48 return id
& ~(KVM_REG_ARCH_MASK
| KVM_REG_SIZE_MASK
| KVM_REG_ARM_CORE
);
51 static int get_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
54 * Because the kvm_regs structure is a mix of 32, 64 and
55 * 128bit fields, we index it as if it was a 32bit
56 * array. Hence below, nr_regs is the number of entries, and
57 * off the index in the "array".
59 __u32 __user
*uaddr
= (__u32 __user
*)(unsigned long)reg
->addr
;
60 struct kvm_regs
*regs
= vcpu_gp_regs(vcpu
);
61 int nr_regs
= sizeof(*regs
) / sizeof(__u32
);
64 /* Our ID is an index into the kvm_regs struct. */
65 off
= core_reg_offset_from_id(reg
->id
);
67 (off
+ (KVM_REG_SIZE(reg
->id
) / sizeof(__u32
))) >= nr_regs
)
70 if (copy_to_user(uaddr
, ((u32
*)regs
) + off
, KVM_REG_SIZE(reg
->id
)))
76 static int set_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
78 __u32 __user
*uaddr
= (__u32 __user
*)(unsigned long)reg
->addr
;
79 struct kvm_regs
*regs
= vcpu_gp_regs(vcpu
);
80 int nr_regs
= sizeof(*regs
) / sizeof(__u32
);
86 /* Our ID is an index into the kvm_regs struct. */
87 off
= core_reg_offset_from_id(reg
->id
);
89 (off
+ (KVM_REG_SIZE(reg
->id
) / sizeof(__u32
))) >= nr_regs
)
92 if (KVM_REG_SIZE(reg
->id
) > sizeof(tmp
))
95 if (copy_from_user(valp
, uaddr
, KVM_REG_SIZE(reg
->id
))) {
100 if (off
== KVM_REG_ARM_CORE_REG(regs
.pstate
)) {
101 u32 mode
= (*(u32
*)valp
) & COMPAT_PSR_MODE_MASK
;
103 case COMPAT_PSR_MODE_USR
:
104 case COMPAT_PSR_MODE_FIQ
:
105 case COMPAT_PSR_MODE_IRQ
:
106 case COMPAT_PSR_MODE_SVC
:
107 case COMPAT_PSR_MODE_ABT
:
108 case COMPAT_PSR_MODE_UND
:
119 memcpy((u32
*)regs
+ off
, valp
, KVM_REG_SIZE(reg
->id
));
124 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
129 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
134 static unsigned long num_core_regs(void)
136 return sizeof(struct kvm_regs
) / sizeof(__u32
);
140 * ARM64 versions of the TIMER registers, always available on arm64
143 #define NUM_TIMER_REGS 3
145 static bool is_timer_reg(u64 index
)
148 case KVM_REG_ARM_TIMER_CTL
:
149 case KVM_REG_ARM_TIMER_CNT
:
150 case KVM_REG_ARM_TIMER_CVAL
:
156 static int copy_timer_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
158 if (put_user(KVM_REG_ARM_TIMER_CTL
, uindices
))
161 if (put_user(KVM_REG_ARM_TIMER_CNT
, uindices
))
164 if (put_user(KVM_REG_ARM_TIMER_CVAL
, uindices
))
170 static int set_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
172 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
176 ret
= copy_from_user(&val
, uaddr
, KVM_REG_SIZE(reg
->id
));
180 return kvm_arm_timer_set_reg(vcpu
, reg
->id
, val
);
183 static int get_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
185 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
188 val
= kvm_arm_timer_get_reg(vcpu
, reg
->id
);
189 return copy_to_user(uaddr
, &val
, KVM_REG_SIZE(reg
->id
));
193 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
195 * This is for all registers.
197 unsigned long kvm_arm_num_regs(struct kvm_vcpu
*vcpu
)
199 return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu
)
204 * kvm_arm_copy_reg_indices - get indices of all registers.
206 * We do core registers right here, then we apppend system regs.
208 int kvm_arm_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
211 const u64 core_reg
= KVM_REG_ARM64
| KVM_REG_SIZE_U64
| KVM_REG_ARM_CORE
;
214 for (i
= 0; i
< sizeof(struct kvm_regs
) / sizeof(__u32
); i
++) {
215 if (put_user(core_reg
| i
, uindices
))
220 ret
= copy_timer_indices(vcpu
, uindices
);
223 uindices
+= NUM_TIMER_REGS
;
225 return kvm_arm_copy_sys_reg_indices(vcpu
, uindices
);
228 int kvm_arm_get_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
230 /* We currently use nothing arch-specific in upper 32 bits */
231 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM64
>> 32)
234 /* Register group 16 means we want a core register. */
235 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
236 return get_core_reg(vcpu
, reg
);
238 if (is_timer_reg(reg
->id
))
239 return get_timer_reg(vcpu
, reg
);
241 return kvm_arm_sys_reg_get_reg(vcpu
, reg
);
244 int kvm_arm_set_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
246 /* We currently use nothing arch-specific in upper 32 bits */
247 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM64
>> 32)
250 /* Register group 16 means we set a core register. */
251 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
252 return set_core_reg(vcpu
, reg
);
254 if (is_timer_reg(reg
->id
))
255 return set_timer_reg(vcpu
, reg
);
257 return kvm_arm_sys_reg_set_reg(vcpu
, reg
);
260 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
261 struct kvm_sregs
*sregs
)
266 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
267 struct kvm_sregs
*sregs
)
272 int __attribute_const__
kvm_target_cpu(void)
274 unsigned long implementor
= read_cpuid_implementor();
275 unsigned long part_number
= read_cpuid_part_number();
277 switch (implementor
) {
278 case ARM_CPU_IMP_ARM
:
279 switch (part_number
) {
280 case ARM_CPU_PART_AEM_V8
:
281 return KVM_ARM_TARGET_AEM_V8
;
282 case ARM_CPU_PART_FOUNDATION
:
283 return KVM_ARM_TARGET_FOUNDATION_V8
;
284 case ARM_CPU_PART_CORTEX_A53
:
285 return KVM_ARM_TARGET_CORTEX_A53
;
286 case ARM_CPU_PART_CORTEX_A57
:
287 return KVM_ARM_TARGET_CORTEX_A57
;
290 case ARM_CPU_IMP_APM
:
291 switch (part_number
) {
292 case APM_CPU_PART_POTENZA
:
293 return KVM_ARM_TARGET_XGENE_POTENZA
;
298 /* Return a default generic target */
299 return KVM_ARM_TARGET_GENERIC_V8
;
302 int kvm_vcpu_preferred_target(struct kvm_vcpu_init
*init
)
304 int target
= kvm_target_cpu();
309 memset(init
, 0, sizeof(*init
));
312 * For now, we don't return any features.
313 * In future, we might use features to return target
314 * specific features available for the preferred
317 init
->target
= (__u32
)target
;
322 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
327 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
332 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
333 struct kvm_translation
*tr
)
338 #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
339 KVM_GUESTDBG_USE_SW_BP | \
340 KVM_GUESTDBG_USE_HW | \
341 KVM_GUESTDBG_SINGLESTEP)
344 * kvm_arch_vcpu_ioctl_set_guest_debug - set up guest debugging
345 * @kvm: pointer to the KVM struct
346 * @kvm_guest_debug: the ioctl data buffer
348 * This sets up and enables the VM for guest debugging. Userspace
349 * passes in a control flag to enable different debug types and
350 * potentially other architecture specific information in the rest of
353 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
354 struct kvm_guest_debug
*dbg
)
356 trace_kvm_set_guest_debug(vcpu
, dbg
->control
);
358 if (dbg
->control
& ~KVM_GUESTDBG_VALID_MASK
)
361 if (dbg
->control
& KVM_GUESTDBG_ENABLE
) {
362 vcpu
->guest_debug
= dbg
->control
;
364 /* Hardware assisted Break and Watch points */
365 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW
) {
366 vcpu
->arch
.external_debug_state
= dbg
->arch
;
370 /* If not enabled clear all flags */
371 vcpu
->guest_debug
= 0;