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 <linux/uaccess.h>
28 #include <asm/kvm_emulate.h>
29 #include <asm/kvm_coproc.h>
31 #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
32 #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
34 struct kvm_stats_debugfs_item debugfs_entries
[] = {
35 VCPU_STAT(hvc_exit_stat
),
36 VCPU_STAT(wfe_exit_stat
),
37 VCPU_STAT(wfi_exit_stat
),
38 VCPU_STAT(mmio_exit_user
),
39 VCPU_STAT(mmio_exit_kernel
),
44 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
49 static u64
core_reg_offset_from_id(u64 id
)
51 return id
& ~(KVM_REG_ARCH_MASK
| KVM_REG_SIZE_MASK
| KVM_REG_ARM_CORE
);
54 static int get_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
56 u32 __user
*uaddr
= (u32 __user
*)(long)reg
->addr
;
57 struct kvm_regs
*regs
= &vcpu
->arch
.ctxt
.gp_regs
;
60 if (KVM_REG_SIZE(reg
->id
) != 4)
63 /* Our ID is an index into the kvm_regs struct. */
64 off
= core_reg_offset_from_id(reg
->id
);
65 if (off
>= sizeof(*regs
) / KVM_REG_SIZE(reg
->id
))
68 return put_user(((u32
*)regs
)[off
], uaddr
);
71 static int set_core_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
73 u32 __user
*uaddr
= (u32 __user
*)(long)reg
->addr
;
74 struct kvm_regs
*regs
= &vcpu
->arch
.ctxt
.gp_regs
;
77 if (KVM_REG_SIZE(reg
->id
) != 4)
80 /* Our ID is an index into the kvm_regs struct. */
81 off
= core_reg_offset_from_id(reg
->id
);
82 if (off
>= sizeof(*regs
) / KVM_REG_SIZE(reg
->id
))
85 if (get_user(val
, uaddr
) != 0)
88 if (off
== KVM_REG_ARM_CORE_REG(usr_regs
.ARM_cpsr
)) {
89 unsigned long mode
= val
& MODE_MASK
;
103 ((u32
*)regs
)[off
] = val
;
107 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
112 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
117 #define NUM_TIMER_REGS 3
119 static bool is_timer_reg(u64 index
)
122 case KVM_REG_ARM_TIMER_CTL
:
123 case KVM_REG_ARM_TIMER_CNT
:
124 case KVM_REG_ARM_TIMER_CVAL
:
130 static int copy_timer_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
132 if (put_user(KVM_REG_ARM_TIMER_CTL
, uindices
))
135 if (put_user(KVM_REG_ARM_TIMER_CNT
, uindices
))
138 if (put_user(KVM_REG_ARM_TIMER_CVAL
, uindices
))
144 static int set_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
146 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
150 ret
= copy_from_user(&val
, uaddr
, KVM_REG_SIZE(reg
->id
));
154 return kvm_arm_timer_set_reg(vcpu
, reg
->id
, val
);
157 static int get_timer_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
159 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
162 val
= kvm_arm_timer_get_reg(vcpu
, reg
->id
);
163 return copy_to_user(uaddr
, &val
, KVM_REG_SIZE(reg
->id
)) ? -EFAULT
: 0;
166 static unsigned long num_core_regs(void)
168 return sizeof(struct kvm_regs
) / sizeof(u32
);
172 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
174 * This is for all registers.
176 unsigned long kvm_arm_num_regs(struct kvm_vcpu
*vcpu
)
178 return num_core_regs() + kvm_arm_num_coproc_regs(vcpu
)
183 * kvm_arm_copy_reg_indices - get indices of all registers.
185 * We do core registers right here, then we append coproc regs.
187 int kvm_arm_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
190 const u64 core_reg
= KVM_REG_ARM
| KVM_REG_SIZE_U32
| KVM_REG_ARM_CORE
;
193 for (i
= 0; i
< sizeof(struct kvm_regs
)/sizeof(u32
); i
++) {
194 if (put_user(core_reg
| i
, uindices
))
199 ret
= copy_timer_indices(vcpu
, uindices
);
202 uindices
+= NUM_TIMER_REGS
;
204 return kvm_arm_copy_coproc_indices(vcpu
, uindices
);
207 int kvm_arm_get_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
209 /* We currently use nothing arch-specific in upper 32 bits */
210 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM
>> 32)
213 /* Register group 16 means we want a core register. */
214 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
215 return get_core_reg(vcpu
, reg
);
217 if (is_timer_reg(reg
->id
))
218 return get_timer_reg(vcpu
, reg
);
220 return kvm_arm_coproc_get_reg(vcpu
, reg
);
223 int kvm_arm_set_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
225 /* We currently use nothing arch-specific in upper 32 bits */
226 if ((reg
->id
& ~KVM_REG_SIZE_MASK
) >> 32 != KVM_REG_ARM
>> 32)
229 /* Register group 16 means we set a core register. */
230 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_CORE
)
231 return set_core_reg(vcpu
, reg
);
233 if (is_timer_reg(reg
->id
))
234 return set_timer_reg(vcpu
, reg
);
236 return kvm_arm_coproc_set_reg(vcpu
, reg
);
239 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
240 struct kvm_sregs
*sregs
)
245 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
246 struct kvm_sregs
*sregs
)
251 int __attribute_const__
kvm_target_cpu(void)
253 switch (read_cpuid_part()) {
254 case ARM_CPU_PART_CORTEX_A7
:
255 return KVM_ARM_TARGET_CORTEX_A7
;
256 case ARM_CPU_PART_CORTEX_A15
:
257 return KVM_ARM_TARGET_CORTEX_A15
;
263 int kvm_vcpu_preferred_target(struct kvm_vcpu_init
*init
)
265 int target
= kvm_target_cpu();
270 memset(init
, 0, sizeof(*init
));
273 * For now, we don't return any features.
274 * In future, we might use features to return target
275 * specific features available for the preferred
278 init
->target
= (__u32
)target
;
283 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
288 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
293 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
294 struct kvm_translation
*tr
)
299 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
300 struct kvm_guest_debug
*dbg
)
305 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu
*vcpu
,
306 struct kvm_device_attr
*attr
)
310 switch (attr
->group
) {
311 case KVM_ARM_VCPU_TIMER_CTRL
:
312 ret
= kvm_arm_timer_set_attr(vcpu
, attr
);
322 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu
*vcpu
,
323 struct kvm_device_attr
*attr
)
327 switch (attr
->group
) {
328 case KVM_ARM_VCPU_TIMER_CTRL
:
329 ret
= kvm_arm_timer_get_attr(vcpu
, attr
);
339 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu
*vcpu
,
340 struct kvm_device_attr
*attr
)
344 switch (attr
->group
) {
345 case KVM_ARM_VCPU_TIMER_CTRL
:
346 ret
= kvm_arm_timer_has_attr(vcpu
, attr
);