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>
35 struct kvm_stats_debugfs_item debugfs_entries
[] = {
39 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
44 static u64
core_reg_offset_from_id(u64 id
)
46 return id
& ~(KVM_REG_ARCH_MASK
| KVM_REG_SIZE_MASK
| KVM_REG_ARM_CORE
);
49 static int get_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
52 * Because the kvm_regs structure is a mix of 32, 64 and
53 * 128bit fields, we index it as if it was a 32bit
54 * array. Hence below, nr_regs is the number of entries, and
55 * off the index in the "array".
57 __u32 __user
*uaddr
= (__u32 __user
*)(unsigned long)reg
->addr
;
58 struct kvm_regs
*regs
= vcpu_gp_regs(vcpu
);
59 int nr_regs
= sizeof(*regs
) / sizeof(__u32
);
62 /* Our ID is an index into the kvm_regs struct. */
63 off
= core_reg_offset_from_id(reg
->id
);
65 (off
+ (KVM_REG_SIZE(reg
->id
) / sizeof(__u32
))) >= nr_regs
)
68 if (copy_to_user(uaddr
, ((u32
*)regs
) + off
, KVM_REG_SIZE(reg
->id
)))
74 static int set_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
76 __u32 __user
*uaddr
= (__u32 __user
*)(unsigned long)reg
->addr
;
77 struct kvm_regs
*regs
= vcpu_gp_regs(vcpu
);
78 int nr_regs
= sizeof(*regs
) / sizeof(__u32
);
84 /* Our ID is an index into the kvm_regs struct. */
85 off
= core_reg_offset_from_id(reg
->id
);
87 (off
+ (KVM_REG_SIZE(reg
->id
) / sizeof(__u32
))) >= nr_regs
)
90 if (KVM_REG_SIZE(reg
->id
) > sizeof(tmp
))
93 if (copy_from_user(valp
, uaddr
, KVM_REG_SIZE(reg
->id
))) {
98 if (off
== KVM_REG_ARM_CORE_REG(regs
.pstate
)) {
99 u32 mode
= (*(u32
*)valp
) & COMPAT_PSR_MODE_MASK
;
101 case COMPAT_PSR_MODE_USR
:
102 case COMPAT_PSR_MODE_FIQ
:
103 case COMPAT_PSR_MODE_IRQ
:
104 case COMPAT_PSR_MODE_SVC
:
105 case COMPAT_PSR_MODE_ABT
:
106 case COMPAT_PSR_MODE_UND
:
117 memcpy((u32
*)regs
+ off
, valp
, KVM_REG_SIZE(reg
->id
));
122 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
127 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
132 static unsigned long num_core_regs(void)
134 return sizeof(struct kvm_regs
) / sizeof(__u32
);
138 * ARM64 versions of the TIMER registers, always available on arm64
141 #define NUM_TIMER_REGS 3
143 static bool is_timer_reg(u64 index
)
146 case KVM_REG_ARM_TIMER_CTL
:
147 case KVM_REG_ARM_TIMER_CNT
:
148 case KVM_REG_ARM_TIMER_CVAL
:
154 static int copy_timer_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
156 if (put_user(KVM_REG_ARM_TIMER_CTL
, uindices
))
159 if (put_user(KVM_REG_ARM_TIMER_CNT
, uindices
))
162 if (put_user(KVM_REG_ARM_TIMER_CVAL
, uindices
))
168 static int set_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
170 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
174 ret
= copy_from_user(&val
, uaddr
, KVM_REG_SIZE(reg
->id
));
178 return kvm_arm_timer_set_reg(vcpu
, reg
->id
, val
);
181 static int get_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
183 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
186 val
= kvm_arm_timer_get_reg(vcpu
, reg
->id
);
187 return copy_to_user(uaddr
, &val
, KVM_REG_SIZE(reg
->id
));
191 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
193 * This is for all registers.
195 unsigned long kvm_arm_num_regs(struct kvm_vcpu
*vcpu
)
197 return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu
)
202 * kvm_arm_copy_reg_indices - get indices of all registers.
204 * We do core registers right here, then we apppend system regs.
206 int kvm_arm_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
209 const u64 core_reg
= KVM_REG_ARM64
| KVM_REG_SIZE_U64
| KVM_REG_ARM_CORE
;
212 for (i
= 0; i
< sizeof(struct kvm_regs
) / sizeof(__u32
); i
++) {
213 if (put_user(core_reg
| i
, uindices
))
218 ret
= copy_timer_indices(vcpu
, uindices
);
221 uindices
+= NUM_TIMER_REGS
;
223 return kvm_arm_copy_sys_reg_indices(vcpu
, uindices
);
226 int kvm_arm_get_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
228 /* We currently use nothing arch-specific in upper 32 bits */
229 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM64
>> 32)
232 /* Register group 16 means we want a core register. */
233 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
234 return get_core_reg(vcpu
, reg
);
236 if (is_timer_reg(reg
->id
))
237 return get_timer_reg(vcpu
, reg
);
239 return kvm_arm_sys_reg_get_reg(vcpu
, reg
);
242 int kvm_arm_set_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 set a core register. */
249 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
250 return set_core_reg(vcpu
, reg
);
252 if (is_timer_reg(reg
->id
))
253 return set_timer_reg(vcpu
, reg
);
255 return kvm_arm_sys_reg_set_reg(vcpu
, reg
);
258 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
259 struct kvm_sregs
*sregs
)
264 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
265 struct kvm_sregs
*sregs
)
270 int __attribute_const__
kvm_target_cpu(void)
272 unsigned long implementor
= read_cpuid_implementor();
273 unsigned long part_number
= read_cpuid_part_number();
275 switch (implementor
) {
276 case ARM_CPU_IMP_ARM
:
277 switch (part_number
) {
278 case ARM_CPU_PART_AEM_V8
:
279 return KVM_ARM_TARGET_AEM_V8
;
280 case ARM_CPU_PART_FOUNDATION
:
281 return KVM_ARM_TARGET_FOUNDATION_V8
;
282 case ARM_CPU_PART_CORTEX_A53
:
283 return KVM_ARM_TARGET_CORTEX_A53
;
284 case ARM_CPU_PART_CORTEX_A57
:
285 return KVM_ARM_TARGET_CORTEX_A57
;
288 case ARM_CPU_IMP_APM
:
289 switch (part_number
) {
290 case APM_CPU_PART_POTENZA
:
291 return KVM_ARM_TARGET_XGENE_POTENZA
;
299 int kvm_vcpu_preferred_target(struct kvm_vcpu_init
*init
)
301 int target
= kvm_target_cpu();
306 memset(init
, 0, sizeof(*init
));
309 * For now, we don't return any features.
310 * In future, we might use features to return target
311 * specific features available for the preferred
314 init
->target
= (__u32
)target
;
319 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
324 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
329 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
330 struct kvm_translation
*tr
)