2 * ARM implementation of KVM hooks
4 * Copyright Christoffer Dall 2009-2010
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
16 #include <linux/kvm.h>
18 #include "qemu-common.h"
19 #include "qemu/timer.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/kvm.h"
24 #include "internals.h"
25 #include "hw/arm/arm.h"
27 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
31 static bool cap_has_mp_state
;
33 int kvm_arm_vcpu_init(CPUState
*cs
)
35 ARMCPU
*cpu
= ARM_CPU(cs
);
36 struct kvm_vcpu_init init
;
38 init
.target
= cpu
->kvm_target
;
39 memcpy(init
.features
, cpu
->kvm_init_features
, sizeof(init
.features
));
41 return kvm_vcpu_ioctl(cs
, KVM_ARM_VCPU_INIT
, &init
);
44 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try
,
46 struct kvm_vcpu_init
*init
)
48 int ret
, kvmfd
= -1, vmfd
= -1, cpufd
= -1;
50 kvmfd
= qemu_open("/dev/kvm", O_RDWR
);
54 vmfd
= ioctl(kvmfd
, KVM_CREATE_VM
, 0);
58 cpufd
= ioctl(vmfd
, KVM_CREATE_VCPU
, 0);
63 ret
= ioctl(vmfd
, KVM_ARM_PREFERRED_TARGET
, init
);
65 ret
= ioctl(cpufd
, KVM_ARM_VCPU_INIT
, init
);
70 /* Old kernel which doesn't know about the
71 * PREFERRED_TARGET ioctl: we know it will only support
72 * creating one kind of guest CPU which is its preferred
75 while (*cpus_to_try
!= QEMU_KVM_ARM_TARGET_NONE
) {
76 init
->target
= *cpus_to_try
++;
77 memset(init
->features
, 0, sizeof(init
->features
));
78 ret
= ioctl(cpufd
, KVM_ARM_VCPU_INIT
, init
);
108 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray
)
112 for (i
= 2; i
>= 0; i
--) {
117 static void kvm_arm_host_cpu_class_init(ObjectClass
*oc
, void *data
)
119 ARMHostCPUClass
*ahcc
= ARM_HOST_CPU_CLASS(oc
);
121 /* All we really need to set up for the 'host' CPU
122 * is the feature bits -- we rely on the fact that the
123 * various ID register values in ARMCPU are only used for
126 if (!kvm_arm_get_host_cpu_features(ahcc
)) {
127 fprintf(stderr
, "Failed to retrieve host CPU features!\n");
132 static void kvm_arm_host_cpu_initfn(Object
*obj
)
134 ARMHostCPUClass
*ahcc
= ARM_HOST_CPU_GET_CLASS(obj
);
135 ARMCPU
*cpu
= ARM_CPU(obj
);
136 CPUARMState
*env
= &cpu
->env
;
138 cpu
->kvm_target
= ahcc
->target
;
139 cpu
->dtb_compatible
= ahcc
->dtb_compatible
;
140 env
->features
= ahcc
->features
;
143 static const TypeInfo host_arm_cpu_type_info
= {
144 .name
= TYPE_ARM_HOST_CPU
,
145 #ifdef TARGET_AARCH64
146 .parent
= TYPE_AARCH64_CPU
,
148 .parent
= TYPE_ARM_CPU
,
150 .instance_init
= kvm_arm_host_cpu_initfn
,
151 .class_init
= kvm_arm_host_cpu_class_init
,
152 .class_size
= sizeof(ARMHostCPUClass
),
155 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
157 /* For ARM interrupt delivery is always asynchronous,
158 * whether we are using an in-kernel VGIC or not.
160 kvm_async_interrupts_allowed
= true;
162 cap_has_mp_state
= kvm_check_extension(s
, KVM_CAP_MP_STATE
);
164 type_register_static(&host_arm_cpu_type_info
);
169 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
171 return cpu
->cpu_index
;
174 /* We track all the KVM devices which need their memory addresses
175 * passing to the kernel in a list of these structures.
176 * When board init is complete we run through the list and
177 * tell the kernel the base addresses of the memory regions.
178 * We use a MemoryListener to track mapping and unmapping of
179 * the regions during board creation, so the board models don't
180 * need to do anything special for the KVM case.
182 typedef struct KVMDevice
{
183 struct kvm_arm_device_addr kda
;
184 struct kvm_device_attr kdattr
;
186 QSLIST_ENTRY(KVMDevice
) entries
;
190 static QSLIST_HEAD(kvm_devices_head
, KVMDevice
) kvm_devices_head
;
192 static void kvm_arm_devlistener_add(MemoryListener
*listener
,
193 MemoryRegionSection
*section
)
197 QSLIST_FOREACH(kd
, &kvm_devices_head
, entries
) {
198 if (section
->mr
== kd
->mr
) {
199 kd
->kda
.addr
= section
->offset_within_address_space
;
204 static void kvm_arm_devlistener_del(MemoryListener
*listener
,
205 MemoryRegionSection
*section
)
209 QSLIST_FOREACH(kd
, &kvm_devices_head
, entries
) {
210 if (section
->mr
== kd
->mr
) {
216 static MemoryListener devlistener
= {
217 .region_add
= kvm_arm_devlistener_add
,
218 .region_del
= kvm_arm_devlistener_del
,
221 static void kvm_arm_set_device_addr(KVMDevice
*kd
)
223 struct kvm_device_attr
*attr
= &kd
->kdattr
;
226 /* If the device control API is available and we have a device fd on the
227 * KVMDevice struct, let's use the newer API
229 if (kd
->dev_fd
>= 0) {
230 uint64_t addr
= kd
->kda
.addr
;
231 attr
->addr
= (uintptr_t)&addr
;
232 ret
= kvm_device_ioctl(kd
->dev_fd
, KVM_SET_DEVICE_ATTR
, attr
);
234 ret
= kvm_vm_ioctl(kvm_state
, KVM_ARM_SET_DEVICE_ADDR
, &kd
->kda
);
238 fprintf(stderr
, "Failed to set device address: %s\n",
244 static void kvm_arm_machine_init_done(Notifier
*notifier
, void *data
)
248 memory_listener_unregister(&devlistener
);
249 QSLIST_FOREACH_SAFE(kd
, &kvm_devices_head
, entries
, tkd
) {
250 if (kd
->kda
.addr
!= -1) {
251 kvm_arm_set_device_addr(kd
);
253 memory_region_unref(kd
->mr
);
258 static Notifier notify
= {
259 .notify
= kvm_arm_machine_init_done
,
262 void kvm_arm_register_device(MemoryRegion
*mr
, uint64_t devid
, uint64_t group
,
263 uint64_t attr
, int dev_fd
)
267 if (!kvm_irqchip_in_kernel()) {
271 if (QSLIST_EMPTY(&kvm_devices_head
)) {
272 memory_listener_register(&devlistener
, NULL
);
273 qemu_add_machine_init_done_notifier(¬ify
);
275 kd
= g_new0(KVMDevice
, 1);
279 kd
->kdattr
.flags
= 0;
280 kd
->kdattr
.group
= group
;
281 kd
->kdattr
.attr
= attr
;
283 QSLIST_INSERT_HEAD(&kvm_devices_head
, kd
, entries
);
284 memory_region_ref(kd
->mr
);
287 static int compare_u64(const void *a
, const void *b
)
289 if (*(uint64_t *)a
> *(uint64_t *)b
) {
292 if (*(uint64_t *)a
< *(uint64_t *)b
) {
298 /* Initialize the CPUState's cpreg list according to the kernel's
299 * definition of what CPU registers it knows about (and throw away
300 * the previous TCG-created cpreg list).
302 int kvm_arm_init_cpreg_list(ARMCPU
*cpu
)
304 struct kvm_reg_list rl
;
305 struct kvm_reg_list
*rlp
;
306 int i
, ret
, arraylen
;
307 CPUState
*cs
= CPU(cpu
);
310 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REG_LIST
, &rl
);
314 rlp
= g_malloc(sizeof(struct kvm_reg_list
) + rl
.n
* sizeof(uint64_t));
316 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REG_LIST
, rlp
);
320 /* Sort the list we get back from the kernel, since cpreg_tuples
321 * must be in strictly ascending order.
323 qsort(&rlp
->reg
, rlp
->n
, sizeof(rlp
->reg
[0]), compare_u64
);
325 for (i
= 0, arraylen
= 0; i
< rlp
->n
; i
++) {
326 if (!kvm_arm_reg_syncs_via_cpreg_list(rlp
->reg
[i
])) {
329 switch (rlp
->reg
[i
] & KVM_REG_SIZE_MASK
) {
330 case KVM_REG_SIZE_U32
:
331 case KVM_REG_SIZE_U64
:
334 fprintf(stderr
, "Can't handle size of register in kernel list\n");
342 cpu
->cpreg_indexes
= g_renew(uint64_t, cpu
->cpreg_indexes
, arraylen
);
343 cpu
->cpreg_values
= g_renew(uint64_t, cpu
->cpreg_values
, arraylen
);
344 cpu
->cpreg_vmstate_indexes
= g_renew(uint64_t, cpu
->cpreg_vmstate_indexes
,
346 cpu
->cpreg_vmstate_values
= g_renew(uint64_t, cpu
->cpreg_vmstate_values
,
348 cpu
->cpreg_array_len
= arraylen
;
349 cpu
->cpreg_vmstate_array_len
= arraylen
;
351 for (i
= 0, arraylen
= 0; i
< rlp
->n
; i
++) {
352 uint64_t regidx
= rlp
->reg
[i
];
353 if (!kvm_arm_reg_syncs_via_cpreg_list(regidx
)) {
356 cpu
->cpreg_indexes
[arraylen
] = regidx
;
359 assert(cpu
->cpreg_array_len
== arraylen
);
361 if (!write_kvmstate_to_list(cpu
)) {
362 /* Shouldn't happen unless kernel is inconsistent about
363 * what registers exist.
365 fprintf(stderr
, "Initial read of kernel register state failed\n");
375 bool write_kvmstate_to_list(ARMCPU
*cpu
)
377 CPUState
*cs
= CPU(cpu
);
381 for (i
= 0; i
< cpu
->cpreg_array_len
; i
++) {
382 struct kvm_one_reg r
;
383 uint64_t regidx
= cpu
->cpreg_indexes
[i
];
389 switch (regidx
& KVM_REG_SIZE_MASK
) {
390 case KVM_REG_SIZE_U32
:
391 r
.addr
= (uintptr_t)&v32
;
392 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &r
);
394 cpu
->cpreg_values
[i
] = v32
;
397 case KVM_REG_SIZE_U64
:
398 r
.addr
= (uintptr_t)(cpu
->cpreg_values
+ i
);
399 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &r
);
411 bool write_list_to_kvmstate(ARMCPU
*cpu
)
413 CPUState
*cs
= CPU(cpu
);
417 for (i
= 0; i
< cpu
->cpreg_array_len
; i
++) {
418 struct kvm_one_reg r
;
419 uint64_t regidx
= cpu
->cpreg_indexes
[i
];
424 switch (regidx
& KVM_REG_SIZE_MASK
) {
425 case KVM_REG_SIZE_U32
:
426 v32
= cpu
->cpreg_values
[i
];
427 r
.addr
= (uintptr_t)&v32
;
429 case KVM_REG_SIZE_U64
:
430 r
.addr
= (uintptr_t)(cpu
->cpreg_values
+ i
);
435 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &r
);
437 /* We might fail for "unknown register" and also for
438 * "you tried to set a register which is constant with
439 * a different value from what it actually contains".
447 void kvm_arm_reset_vcpu(ARMCPU
*cpu
)
451 /* Re-init VCPU so that all registers are set to
452 * their respective reset values.
454 ret
= kvm_arm_vcpu_init(CPU(cpu
));
456 fprintf(stderr
, "kvm_arm_vcpu_init failed: %s\n", strerror(-ret
));
459 if (!write_kvmstate_to_list(cpu
)) {
460 fprintf(stderr
, "write_kvmstate_to_list failed\n");
466 * Update KVM's MP_STATE based on what QEMU thinks it is
468 int kvm_arm_sync_mpstate_to_kvm(ARMCPU
*cpu
)
470 if (cap_has_mp_state
) {
471 struct kvm_mp_state mp_state
= {
473 cpu
->powered_off
? KVM_MP_STATE_STOPPED
: KVM_MP_STATE_RUNNABLE
475 int ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
477 fprintf(stderr
, "%s: failed to set MP_STATE %d/%s\n",
478 __func__
, ret
, strerror(-ret
));
487 * Sync the KVM MP_STATE into QEMU
489 int kvm_arm_sync_mpstate_to_qemu(ARMCPU
*cpu
)
491 if (cap_has_mp_state
) {
492 struct kvm_mp_state mp_state
;
493 int ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MP_STATE
, &mp_state
);
495 fprintf(stderr
, "%s: failed to get MP_STATE %d/%s\n",
496 __func__
, ret
, strerror(-ret
));
499 cpu
->powered_off
= (mp_state
.mp_state
== KVM_MP_STATE_STOPPED
);
505 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
509 void kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
513 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
518 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
523 int kvm_arch_process_async_events(CPUState
*cs
)
528 int kvm_arch_on_sigbus_vcpu(CPUState
*cs
, int code
, void *addr
)
533 int kvm_arch_on_sigbus(int code
, void *addr
)
538 void kvm_arch_update_guest_debug(CPUState
*cs
, struct kvm_guest_debug
*dbg
)
540 qemu_log_mask(LOG_UNIMP
, "%s: not implemented\n", __func__
);
543 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
,
544 struct kvm_sw_breakpoint
*bp
)
546 qemu_log_mask(LOG_UNIMP
, "%s: not implemented\n", __func__
);
550 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
551 target_ulong len
, int type
)
553 qemu_log_mask(LOG_UNIMP
, "%s: not implemented\n", __func__
);
557 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
558 target_ulong len
, int type
)
560 qemu_log_mask(LOG_UNIMP
, "%s: not implemented\n", __func__
);
564 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
,
565 struct kvm_sw_breakpoint
*bp
)
567 qemu_log_mask(LOG_UNIMP
, "%s: not implemented\n", __func__
);
571 void kvm_arch_remove_all_hw_breakpoints(void)
573 qemu_log_mask(LOG_UNIMP
, "%s: not implemented\n", __func__
);
576 void kvm_arch_init_irq_routing(KVMState
*s
)
580 int kvm_arch_irqchip_create(KVMState
*s
)
584 /* If we can create the VGIC using the newer device control API, we
585 * let the device do this when it initializes itself, otherwise we
586 * fall back to the old API */
588 ret
= kvm_create_device(s
, KVM_DEV_TYPE_ARM_VGIC_V2
, true);
596 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
597 uint64_t address
, uint32_t data
)