2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <linux/errno.h>
20 #include <linux/err.h>
21 #include <linux/kvm_host.h>
22 #include <linux/module.h>
23 #include <linux/vmalloc.h>
25 #include <asm/cputype.h>
26 #include <asm/uaccess.h>
28 #include <asm/kvm_asm.h>
29 #include <asm/kvm_emulate.h>
30 #include <asm/kvm_coproc.h>
32 #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
33 #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
35 struct kvm_stats_debugfs_item debugfs_entries
[] = {
39 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
41 vcpu
->arch
.hcr
= HCR_GUEST_MASK
;
45 static u64
core_reg_offset_from_id(u64 id
)
47 return id
& ~(KVM_REG_ARCH_MASK
| KVM_REG_SIZE_MASK
| KVM_REG_ARM_CORE
);
50 static int get_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
52 u32 __user
*uaddr
= (u32 __user
*)(long)reg
->addr
;
53 struct kvm_regs
*regs
= &vcpu
->arch
.regs
;
56 if (KVM_REG_SIZE(reg
->id
) != 4)
59 /* Our ID is an index into the kvm_regs struct. */
60 off
= core_reg_offset_from_id(reg
->id
);
61 if (off
>= sizeof(*regs
) / KVM_REG_SIZE(reg
->id
))
64 return put_user(((u32
*)regs
)[off
], uaddr
);
67 static int set_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
69 u32 __user
*uaddr
= (u32 __user
*)(long)reg
->addr
;
70 struct kvm_regs
*regs
= &vcpu
->arch
.regs
;
73 if (KVM_REG_SIZE(reg
->id
) != 4)
76 /* Our ID is an index into the kvm_regs struct. */
77 off
= core_reg_offset_from_id(reg
->id
);
78 if (off
>= sizeof(*regs
) / KVM_REG_SIZE(reg
->id
))
81 if (get_user(val
, uaddr
) != 0)
84 if (off
== KVM_REG_ARM_CORE_REG(usr_regs
.ARM_cpsr
)) {
85 unsigned long mode
= val
& MODE_MASK
;
99 ((u32
*)regs
)[off
] = val
;
103 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
108 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
113 #ifndef CONFIG_KVM_ARM_TIMER
115 #define NUM_TIMER_REGS 0
117 static int copy_timer_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
122 static bool is_timer_reg(u64 index
)
129 #define NUM_TIMER_REGS 3
131 static bool is_timer_reg(u64 index
)
134 case KVM_REG_ARM_TIMER_CTL
:
135 case KVM_REG_ARM_TIMER_CNT
:
136 case KVM_REG_ARM_TIMER_CVAL
:
142 static int copy_timer_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
144 if (put_user(KVM_REG_ARM_TIMER_CTL
, uindices
))
147 if (put_user(KVM_REG_ARM_TIMER_CNT
, uindices
))
150 if (put_user(KVM_REG_ARM_TIMER_CVAL
, uindices
))
158 static int set_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
160 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
164 ret
= copy_from_user(&val
, uaddr
, KVM_REG_SIZE(reg
->id
));
168 return kvm_arm_timer_set_reg(vcpu
, reg
->id
, val
);
171 static int get_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
173 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
176 val
= kvm_arm_timer_get_reg(vcpu
, reg
->id
);
177 return copy_to_user(uaddr
, &val
, KVM_REG_SIZE(reg
->id
));
180 static unsigned long num_core_regs(void)
182 return sizeof(struct kvm_regs
) / sizeof(u32
);
186 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
188 * This is for all registers.
190 unsigned long kvm_arm_num_regs(struct kvm_vcpu
*vcpu
)
192 return num_core_regs() + kvm_arm_num_coproc_regs(vcpu
)
197 * kvm_arm_copy_reg_indices - get indices of all registers.
199 * We do core registers right here, then we apppend coproc regs.
201 int kvm_arm_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
204 const u64 core_reg
= KVM_REG_ARM
| KVM_REG_SIZE_U32
| KVM_REG_ARM_CORE
;
207 for (i
= 0; i
< sizeof(struct kvm_regs
)/sizeof(u32
); i
++) {
208 if (put_user(core_reg
| i
, uindices
))
213 ret
= copy_timer_indices(vcpu
, uindices
);
216 uindices
+= NUM_TIMER_REGS
;
218 return kvm_arm_copy_coproc_indices(vcpu
, uindices
);
221 int kvm_arm_get_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
223 /* We currently use nothing arch-specific in upper 32 bits */
224 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM
>> 32)
227 /* Register group 16 means we want a core register. */
228 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
229 return get_core_reg(vcpu
, reg
);
231 if (is_timer_reg(reg
->id
))
232 return get_timer_reg(vcpu
, reg
);
234 return kvm_arm_coproc_get_reg(vcpu
, reg
);
237 int kvm_arm_set_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
239 /* We currently use nothing arch-specific in upper 32 bits */
240 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM
>> 32)
243 /* Register group 16 means we set a core register. */
244 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
245 return set_core_reg(vcpu
, reg
);
247 if (is_timer_reg(reg
->id
))
248 return set_timer_reg(vcpu
, reg
);
250 return kvm_arm_coproc_set_reg(vcpu
, reg
);
253 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
254 struct kvm_sregs
*sregs
)
259 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
260 struct kvm_sregs
*sregs
)
265 int __attribute_const__
kvm_target_cpu(void)
267 switch (read_cpuid_part()) {
268 case ARM_CPU_PART_CORTEX_A7
:
269 return KVM_ARM_TARGET_CORTEX_A7
;
270 case ARM_CPU_PART_CORTEX_A15
:
271 return KVM_ARM_TARGET_CORTEX_A15
;
277 int kvm_vcpu_set_target(struct kvm_vcpu
*vcpu
,
278 const struct kvm_vcpu_init
*init
)
282 /* We can only cope with guest==host and only on A15/A7 (for now). */
283 if (init
->target
!= kvm_target_cpu())
286 vcpu
->arch
.target
= init
->target
;
287 bitmap_zero(vcpu
->arch
.features
, KVM_VCPU_MAX_FEATURES
);
289 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
290 for (i
= 0; i
< sizeof(init
->features
) * 8; i
++) {
291 if (test_bit(i
, (void *)init
->features
)) {
292 if (i
>= KVM_VCPU_MAX_FEATURES
)
294 set_bit(i
, vcpu
->arch
.features
);
298 /* Now we know what it is, we can reset it. */
299 return kvm_reset_vcpu(vcpu
);
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
)