qom: Push error reporting to object_property_find()
[qemu-kvm.git] / kvm-all.c
blob4ea7d85fe29dd14b3a77b66d9f2d2b9a575ea91c
1 /*
2 * QEMU KVM support
4 * Copyright IBM, Corp. 2008
5 * Red Hat, Inc. 2008
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Glauber Costa <gcosta@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
19 #include <stdarg.h>
21 #include <linux/kvm.h>
23 #include "qemu-common.h"
24 #include "qemu-barrier.h"
25 #include "sysemu.h"
26 #include "hw/hw.h"
27 #include "hw/msi.h"
28 #include "gdbstub.h"
29 #include "kvm.h"
30 #include "bswap.h"
31 #include "memory.h"
32 #include "exec-memory.h"
34 /* This check must be after config-host.h is included */
35 #ifdef CONFIG_EVENTFD
36 #include <sys/eventfd.h>
37 #endif
39 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
40 #define PAGE_SIZE TARGET_PAGE_SIZE
42 //#define DEBUG_KVM
44 #ifdef DEBUG_KVM
45 #define DPRINTF(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #else
48 #define DPRINTF(fmt, ...) \
49 do { } while (0)
50 #endif
52 #define KVM_MSI_HASHTAB_SIZE 256
54 typedef struct KVMSlot
56 target_phys_addr_t start_addr;
57 ram_addr_t memory_size;
58 void *ram;
59 int slot;
60 int flags;
61 } KVMSlot;
63 typedef struct kvm_dirty_log KVMDirtyLog;
65 struct KVMState
67 KVMSlot slots[32];
68 int fd;
69 int vmfd;
70 int coalesced_mmio;
71 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
72 bool coalesced_flush_in_progress;
73 int broken_set_mem_region;
74 int migration_log;
75 int vcpu_events;
76 int robust_singlestep;
77 int debugregs;
78 #ifdef KVM_CAP_SET_GUEST_DEBUG
79 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
80 #endif
81 int pit_state2;
82 int xsave, xcrs;
83 int many_ioeventfds;
84 /* The man page (and posix) say ioctl numbers are signed int, but
85 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
86 * unsigned, and treating them as signed here can break things */
87 unsigned irqchip_inject_ioctl;
88 #ifdef KVM_CAP_IRQ_ROUTING
89 struct kvm_irq_routing *irq_routes;
90 int nr_allocated_irq_routes;
91 uint32_t *used_gsi_bitmap;
92 unsigned int gsi_count;
93 QTAILQ_HEAD(msi_hashtab, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE];
94 bool direct_msi;
95 #endif
98 KVMState *kvm_state;
99 bool kvm_kernel_irqchip;
101 static const KVMCapabilityInfo kvm_required_capabilites[] = {
102 KVM_CAP_INFO(USER_MEMORY),
103 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
104 KVM_CAP_LAST_INFO
107 static KVMSlot *kvm_alloc_slot(KVMState *s)
109 int i;
111 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
112 if (s->slots[i].memory_size == 0) {
113 return &s->slots[i];
117 fprintf(stderr, "%s: no free slot available\n", __func__);
118 abort();
121 static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
122 target_phys_addr_t start_addr,
123 target_phys_addr_t end_addr)
125 int i;
127 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
128 KVMSlot *mem = &s->slots[i];
130 if (start_addr == mem->start_addr &&
131 end_addr == mem->start_addr + mem->memory_size) {
132 return mem;
136 return NULL;
140 * Find overlapping slot with lowest start address
142 static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
143 target_phys_addr_t start_addr,
144 target_phys_addr_t end_addr)
146 KVMSlot *found = NULL;
147 int i;
149 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
150 KVMSlot *mem = &s->slots[i];
152 if (mem->memory_size == 0 ||
153 (found && found->start_addr < mem->start_addr)) {
154 continue;
157 if (end_addr > mem->start_addr &&
158 start_addr < mem->start_addr + mem->memory_size) {
159 found = mem;
163 return found;
166 int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
167 target_phys_addr_t *phys_addr)
169 int i;
171 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
172 KVMSlot *mem = &s->slots[i];
174 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
175 *phys_addr = mem->start_addr + (ram - mem->ram);
176 return 1;
180 return 0;
183 static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
185 struct kvm_userspace_memory_region mem;
187 mem.slot = slot->slot;
188 mem.guest_phys_addr = slot->start_addr;
189 mem.memory_size = slot->memory_size;
190 mem.userspace_addr = (unsigned long)slot->ram;
191 mem.flags = slot->flags;
192 if (s->migration_log) {
193 mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
195 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
198 static void kvm_reset_vcpu(void *opaque)
200 CPUArchState *env = opaque;
202 kvm_arch_reset_vcpu(env);
205 int kvm_init_vcpu(CPUArchState *env)
207 KVMState *s = kvm_state;
208 long mmap_size;
209 int ret;
211 DPRINTF("kvm_init_vcpu\n");
213 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
214 if (ret < 0) {
215 DPRINTF("kvm_create_vcpu failed\n");
216 goto err;
219 env->kvm_fd = ret;
220 env->kvm_state = s;
221 env->kvm_vcpu_dirty = 1;
223 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
224 if (mmap_size < 0) {
225 ret = mmap_size;
226 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
227 goto err;
230 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
231 env->kvm_fd, 0);
232 if (env->kvm_run == MAP_FAILED) {
233 ret = -errno;
234 DPRINTF("mmap'ing vcpu state failed\n");
235 goto err;
238 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
239 s->coalesced_mmio_ring =
240 (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
243 ret = kvm_arch_init_vcpu(env);
244 if (ret == 0) {
245 qemu_register_reset(kvm_reset_vcpu, env);
246 kvm_arch_reset_vcpu(env);
248 err:
249 return ret;
253 * dirty pages logging control
256 static int kvm_mem_flags(KVMState *s, bool log_dirty)
258 return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
261 static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
263 KVMState *s = kvm_state;
264 int flags, mask = KVM_MEM_LOG_DIRTY_PAGES;
265 int old_flags;
267 old_flags = mem->flags;
269 flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
270 mem->flags = flags;
272 /* If nothing changed effectively, no need to issue ioctl */
273 if (s->migration_log) {
274 flags |= KVM_MEM_LOG_DIRTY_PAGES;
277 if (flags == old_flags) {
278 return 0;
281 return kvm_set_user_memory_region(s, mem);
284 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
285 ram_addr_t size, bool log_dirty)
287 KVMState *s = kvm_state;
288 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
290 if (mem == NULL) {
291 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
292 TARGET_FMT_plx "\n", __func__, phys_addr,
293 (target_phys_addr_t)(phys_addr + size - 1));
294 return -EINVAL;
296 return kvm_slot_dirty_pages_log_change(mem, log_dirty);
299 static void kvm_log_start(MemoryListener *listener,
300 MemoryRegionSection *section)
302 int r;
304 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
305 section->size, true);
306 if (r < 0) {
307 abort();
311 static void kvm_log_stop(MemoryListener *listener,
312 MemoryRegionSection *section)
314 int r;
316 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
317 section->size, false);
318 if (r < 0) {
319 abort();
323 static int kvm_set_migration_log(int enable)
325 KVMState *s = kvm_state;
326 KVMSlot *mem;
327 int i, err;
329 s->migration_log = enable;
331 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
332 mem = &s->slots[i];
334 if (!mem->memory_size) {
335 continue;
337 if (!!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) == enable) {
338 continue;
340 err = kvm_set_user_memory_region(s, mem);
341 if (err) {
342 return err;
345 return 0;
348 /* get kvm's dirty pages bitmap and update qemu's */
349 static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
350 unsigned long *bitmap)
352 unsigned int i, j;
353 unsigned long page_number, c;
354 target_phys_addr_t addr, addr1;
355 unsigned int len = ((section->size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
356 unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
359 * bitmap-traveling is faster than memory-traveling (for addr...)
360 * especially when most of the memory is not dirty.
362 for (i = 0; i < len; i++) {
363 if (bitmap[i] != 0) {
364 c = leul_to_cpu(bitmap[i]);
365 do {
366 j = ffsl(c) - 1;
367 c &= ~(1ul << j);
368 page_number = (i * HOST_LONG_BITS + j) * hpratio;
369 addr1 = page_number * TARGET_PAGE_SIZE;
370 addr = section->offset_within_region + addr1;
371 memory_region_set_dirty(section->mr, addr,
372 TARGET_PAGE_SIZE * hpratio);
373 } while (c != 0);
376 return 0;
379 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
382 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
383 * This function updates qemu's dirty bitmap using
384 * memory_region_set_dirty(). This means all bits are set
385 * to dirty.
387 * @start_add: start of logged region.
388 * @end_addr: end of logged region.
390 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
392 KVMState *s = kvm_state;
393 unsigned long size, allocated_size = 0;
394 KVMDirtyLog d;
395 KVMSlot *mem;
396 int ret = 0;
397 target_phys_addr_t start_addr = section->offset_within_address_space;
398 target_phys_addr_t end_addr = start_addr + section->size;
400 d.dirty_bitmap = NULL;
401 while (start_addr < end_addr) {
402 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
403 if (mem == NULL) {
404 break;
407 /* XXX bad kernel interface alert
408 * For dirty bitmap, kernel allocates array of size aligned to
409 * bits-per-long. But for case when the kernel is 64bits and
410 * the userspace is 32bits, userspace can't align to the same
411 * bits-per-long, since sizeof(long) is different between kernel
412 * and user space. This way, userspace will provide buffer which
413 * may be 4 bytes less than the kernel will use, resulting in
414 * userspace memory corruption (which is not detectable by valgrind
415 * too, in most cases).
416 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
417 * a hope that sizeof(long) wont become >8 any time soon.
419 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
420 /*HOST_LONG_BITS*/ 64) / 8;
421 if (!d.dirty_bitmap) {
422 d.dirty_bitmap = g_malloc(size);
423 } else if (size > allocated_size) {
424 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
426 allocated_size = size;
427 memset(d.dirty_bitmap, 0, allocated_size);
429 d.slot = mem->slot;
431 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
432 DPRINTF("ioctl failed %d\n", errno);
433 ret = -1;
434 break;
437 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
438 start_addr = mem->start_addr + mem->memory_size;
440 g_free(d.dirty_bitmap);
442 return ret;
445 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
447 int ret = -ENOSYS;
448 KVMState *s = kvm_state;
450 if (s->coalesced_mmio) {
451 struct kvm_coalesced_mmio_zone zone;
453 zone.addr = start;
454 zone.size = size;
455 zone.pad = 0;
457 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
460 return ret;
463 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
465 int ret = -ENOSYS;
466 KVMState *s = kvm_state;
468 if (s->coalesced_mmio) {
469 struct kvm_coalesced_mmio_zone zone;
471 zone.addr = start;
472 zone.size = size;
473 zone.pad = 0;
475 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
478 return ret;
481 int kvm_check_extension(KVMState *s, unsigned int extension)
483 int ret;
485 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
486 if (ret < 0) {
487 ret = 0;
490 return ret;
493 static int kvm_check_many_ioeventfds(void)
495 /* Userspace can use ioeventfd for io notification. This requires a host
496 * that supports eventfd(2) and an I/O thread; since eventfd does not
497 * support SIGIO it cannot interrupt the vcpu.
499 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
500 * can avoid creating too many ioeventfds.
502 #if defined(CONFIG_EVENTFD)
503 int ioeventfds[7];
504 int i, ret = 0;
505 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
506 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
507 if (ioeventfds[i] < 0) {
508 break;
510 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
511 if (ret < 0) {
512 close(ioeventfds[i]);
513 break;
517 /* Decide whether many devices are supported or not */
518 ret = i == ARRAY_SIZE(ioeventfds);
520 while (i-- > 0) {
521 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
522 close(ioeventfds[i]);
524 return ret;
525 #else
526 return 0;
527 #endif
530 static const KVMCapabilityInfo *
531 kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
533 while (list->name) {
534 if (!kvm_check_extension(s, list->value)) {
535 return list;
537 list++;
539 return NULL;
542 static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
544 KVMState *s = kvm_state;
545 KVMSlot *mem, old;
546 int err;
547 MemoryRegion *mr = section->mr;
548 bool log_dirty = memory_region_is_logging(mr);
549 target_phys_addr_t start_addr = section->offset_within_address_space;
550 ram_addr_t size = section->size;
551 void *ram = NULL;
552 unsigned delta;
554 /* kvm works in page size chunks, but the function may be called
555 with sub-page size and unaligned start address. */
556 delta = TARGET_PAGE_ALIGN(size) - size;
557 if (delta > size) {
558 return;
560 start_addr += delta;
561 size -= delta;
562 size &= TARGET_PAGE_MASK;
563 if (!size || (start_addr & ~TARGET_PAGE_MASK)) {
564 return;
567 if (!memory_region_is_ram(mr)) {
568 return;
571 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
573 while (1) {
574 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
575 if (!mem) {
576 break;
579 if (add && start_addr >= mem->start_addr &&
580 (start_addr + size <= mem->start_addr + mem->memory_size) &&
581 (ram - start_addr == mem->ram - mem->start_addr)) {
582 /* The new slot fits into the existing one and comes with
583 * identical parameters - update flags and done. */
584 kvm_slot_dirty_pages_log_change(mem, log_dirty);
585 return;
588 old = *mem;
590 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
591 kvm_physical_sync_dirty_bitmap(section);
594 /* unregister the overlapping slot */
595 mem->memory_size = 0;
596 err = kvm_set_user_memory_region(s, mem);
597 if (err) {
598 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
599 __func__, strerror(-err));
600 abort();
603 /* Workaround for older KVM versions: we can't join slots, even not by
604 * unregistering the previous ones and then registering the larger
605 * slot. We have to maintain the existing fragmentation. Sigh.
607 * This workaround assumes that the new slot starts at the same
608 * address as the first existing one. If not or if some overlapping
609 * slot comes around later, we will fail (not seen in practice so far)
610 * - and actually require a recent KVM version. */
611 if (s->broken_set_mem_region &&
612 old.start_addr == start_addr && old.memory_size < size && add) {
613 mem = kvm_alloc_slot(s);
614 mem->memory_size = old.memory_size;
615 mem->start_addr = old.start_addr;
616 mem->ram = old.ram;
617 mem->flags = kvm_mem_flags(s, log_dirty);
619 err = kvm_set_user_memory_region(s, mem);
620 if (err) {
621 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
622 strerror(-err));
623 abort();
626 start_addr += old.memory_size;
627 ram += old.memory_size;
628 size -= old.memory_size;
629 continue;
632 /* register prefix slot */
633 if (old.start_addr < start_addr) {
634 mem = kvm_alloc_slot(s);
635 mem->memory_size = start_addr - old.start_addr;
636 mem->start_addr = old.start_addr;
637 mem->ram = old.ram;
638 mem->flags = kvm_mem_flags(s, log_dirty);
640 err = kvm_set_user_memory_region(s, mem);
641 if (err) {
642 fprintf(stderr, "%s: error registering prefix slot: %s\n",
643 __func__, strerror(-err));
644 #ifdef TARGET_PPC
645 fprintf(stderr, "%s: This is probably because your kernel's " \
646 "PAGE_SIZE is too big. Please try to use 4k " \
647 "PAGE_SIZE!\n", __func__);
648 #endif
649 abort();
653 /* register suffix slot */
654 if (old.start_addr + old.memory_size > start_addr + size) {
655 ram_addr_t size_delta;
657 mem = kvm_alloc_slot(s);
658 mem->start_addr = start_addr + size;
659 size_delta = mem->start_addr - old.start_addr;
660 mem->memory_size = old.memory_size - size_delta;
661 mem->ram = old.ram + size_delta;
662 mem->flags = kvm_mem_flags(s, log_dirty);
664 err = kvm_set_user_memory_region(s, mem);
665 if (err) {
666 fprintf(stderr, "%s: error registering suffix slot: %s\n",
667 __func__, strerror(-err));
668 abort();
673 /* in case the KVM bug workaround already "consumed" the new slot */
674 if (!size) {
675 return;
677 if (!add) {
678 return;
680 mem = kvm_alloc_slot(s);
681 mem->memory_size = size;
682 mem->start_addr = start_addr;
683 mem->ram = ram;
684 mem->flags = kvm_mem_flags(s, log_dirty);
686 err = kvm_set_user_memory_region(s, mem);
687 if (err) {
688 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
689 strerror(-err));
690 abort();
694 static void kvm_begin(MemoryListener *listener)
698 static void kvm_commit(MemoryListener *listener)
702 static void kvm_region_add(MemoryListener *listener,
703 MemoryRegionSection *section)
705 kvm_set_phys_mem(section, true);
708 static void kvm_region_del(MemoryListener *listener,
709 MemoryRegionSection *section)
711 kvm_set_phys_mem(section, false);
714 static void kvm_region_nop(MemoryListener *listener,
715 MemoryRegionSection *section)
719 static void kvm_log_sync(MemoryListener *listener,
720 MemoryRegionSection *section)
722 int r;
724 r = kvm_physical_sync_dirty_bitmap(section);
725 if (r < 0) {
726 abort();
730 static void kvm_log_global_start(struct MemoryListener *listener)
732 int r;
734 r = kvm_set_migration_log(1);
735 assert(r >= 0);
738 static void kvm_log_global_stop(struct MemoryListener *listener)
740 int r;
742 r = kvm_set_migration_log(0);
743 assert(r >= 0);
746 static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
747 bool match_data, uint64_t data, int fd)
749 int r;
751 assert(match_data && section->size <= 8);
753 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
754 data, true, section->size);
755 if (r < 0) {
756 abort();
760 static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
761 bool match_data, uint64_t data, int fd)
763 int r;
765 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
766 data, false, section->size);
767 if (r < 0) {
768 abort();
772 static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
773 bool match_data, uint64_t data, int fd)
775 int r;
777 assert(match_data && section->size == 2);
779 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
780 data, true);
781 if (r < 0) {
782 abort();
786 static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
787 bool match_data, uint64_t data, int fd)
790 int r;
792 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
793 data, false);
794 if (r < 0) {
795 abort();
799 static void kvm_eventfd_add(MemoryListener *listener,
800 MemoryRegionSection *section,
801 bool match_data, uint64_t data, int fd)
803 if (section->address_space == get_system_memory()) {
804 kvm_mem_ioeventfd_add(section, match_data, data, fd);
805 } else {
806 kvm_io_ioeventfd_add(section, match_data, data, fd);
810 static void kvm_eventfd_del(MemoryListener *listener,
811 MemoryRegionSection *section,
812 bool match_data, uint64_t data, int fd)
814 if (section->address_space == get_system_memory()) {
815 kvm_mem_ioeventfd_del(section, match_data, data, fd);
816 } else {
817 kvm_io_ioeventfd_del(section, match_data, data, fd);
821 static MemoryListener kvm_memory_listener = {
822 .begin = kvm_begin,
823 .commit = kvm_commit,
824 .region_add = kvm_region_add,
825 .region_del = kvm_region_del,
826 .region_nop = kvm_region_nop,
827 .log_start = kvm_log_start,
828 .log_stop = kvm_log_stop,
829 .log_sync = kvm_log_sync,
830 .log_global_start = kvm_log_global_start,
831 .log_global_stop = kvm_log_global_stop,
832 .eventfd_add = kvm_eventfd_add,
833 .eventfd_del = kvm_eventfd_del,
834 .priority = 10,
837 static void kvm_handle_interrupt(CPUArchState *env, int mask)
839 env->interrupt_request |= mask;
841 if (!qemu_cpu_is_self(env)) {
842 qemu_cpu_kick(env);
846 int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
848 struct kvm_irq_level event;
849 int ret;
851 assert(kvm_irqchip_in_kernel());
853 event.level = level;
854 event.irq = irq;
855 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
856 if (ret < 0) {
857 perror("kvm_set_irqchip_line");
858 abort();
861 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
864 #ifdef KVM_CAP_IRQ_ROUTING
865 typedef struct KVMMSIRoute {
866 struct kvm_irq_routing_entry kroute;
867 QTAILQ_ENTRY(KVMMSIRoute) entry;
868 } KVMMSIRoute;
870 static void set_gsi(KVMState *s, unsigned int gsi)
872 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
875 static void clear_gsi(KVMState *s, unsigned int gsi)
877 s->used_gsi_bitmap[gsi / 32] &= ~(1U << (gsi % 32));
880 static void kvm_init_irq_routing(KVMState *s)
882 int gsi_count, i;
884 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
885 if (gsi_count > 0) {
886 unsigned int gsi_bits, i;
888 /* Round up so we can search ints using ffs */
889 gsi_bits = ALIGN(gsi_count, 32);
890 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
891 s->gsi_count = gsi_count;
893 /* Mark any over-allocated bits as already in use */
894 for (i = gsi_count; i < gsi_bits; i++) {
895 set_gsi(s, i);
899 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
900 s->nr_allocated_irq_routes = 0;
902 if (!s->direct_msi) {
903 for (i = 0; i < KVM_MSI_HASHTAB_SIZE; i++) {
904 QTAILQ_INIT(&s->msi_hashtab[i]);
908 kvm_arch_init_irq_routing(s);
911 static void kvm_irqchip_commit_routes(KVMState *s)
913 int ret;
915 s->irq_routes->flags = 0;
916 ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
917 assert(ret == 0);
920 static void kvm_add_routing_entry(KVMState *s,
921 struct kvm_irq_routing_entry *entry)
923 struct kvm_irq_routing_entry *new;
924 int n, size;
926 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
927 n = s->nr_allocated_irq_routes * 2;
928 if (n < 64) {
929 n = 64;
931 size = sizeof(struct kvm_irq_routing);
932 size += n * sizeof(*new);
933 s->irq_routes = g_realloc(s->irq_routes, size);
934 s->nr_allocated_irq_routes = n;
936 n = s->irq_routes->nr++;
937 new = &s->irq_routes->entries[n];
938 memset(new, 0, sizeof(*new));
939 new->gsi = entry->gsi;
940 new->type = entry->type;
941 new->flags = entry->flags;
942 new->u = entry->u;
944 set_gsi(s, entry->gsi);
946 kvm_irqchip_commit_routes(s);
949 void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin)
951 struct kvm_irq_routing_entry e;
953 assert(pin < s->gsi_count);
955 e.gsi = irq;
956 e.type = KVM_IRQ_ROUTING_IRQCHIP;
957 e.flags = 0;
958 e.u.irqchip.irqchip = irqchip;
959 e.u.irqchip.pin = pin;
960 kvm_add_routing_entry(s, &e);
963 void kvm_irqchip_release_virq(KVMState *s, int virq)
965 struct kvm_irq_routing_entry *e;
966 int i;
968 for (i = 0; i < s->irq_routes->nr; i++) {
969 e = &s->irq_routes->entries[i];
970 if (e->gsi == virq) {
971 s->irq_routes->nr--;
972 *e = s->irq_routes->entries[s->irq_routes->nr];
975 clear_gsi(s, virq);
977 kvm_irqchip_commit_routes(s);
980 static unsigned int kvm_hash_msi(uint32_t data)
982 /* This is optimized for IA32 MSI layout. However, no other arch shall
983 * repeat the mistake of not providing a direct MSI injection API. */
984 return data & 0xff;
987 static void kvm_flush_dynamic_msi_routes(KVMState *s)
989 KVMMSIRoute *route, *next;
990 unsigned int hash;
992 for (hash = 0; hash < KVM_MSI_HASHTAB_SIZE; hash++) {
993 QTAILQ_FOREACH_SAFE(route, &s->msi_hashtab[hash], entry, next) {
994 kvm_irqchip_release_virq(s, route->kroute.gsi);
995 QTAILQ_REMOVE(&s->msi_hashtab[hash], route, entry);
996 g_free(route);
1001 static int kvm_irqchip_get_virq(KVMState *s)
1003 uint32_t *word = s->used_gsi_bitmap;
1004 int max_words = ALIGN(s->gsi_count, 32) / 32;
1005 int i, bit;
1006 bool retry = true;
1008 again:
1009 /* Return the lowest unused GSI in the bitmap */
1010 for (i = 0; i < max_words; i++) {
1011 bit = ffs(~word[i]);
1012 if (!bit) {
1013 continue;
1016 return bit - 1 + i * 32;
1018 if (!s->direct_msi && retry) {
1019 retry = false;
1020 kvm_flush_dynamic_msi_routes(s);
1021 goto again;
1023 return -ENOSPC;
1027 static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
1029 unsigned int hash = kvm_hash_msi(msg.data);
1030 KVMMSIRoute *route;
1032 QTAILQ_FOREACH(route, &s->msi_hashtab[hash], entry) {
1033 if (route->kroute.u.msi.address_lo == (uint32_t)msg.address &&
1034 route->kroute.u.msi.address_hi == (msg.address >> 32) &&
1035 route->kroute.u.msi.data == msg.data) {
1036 return route;
1039 return NULL;
1042 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1044 struct kvm_msi msi;
1045 KVMMSIRoute *route;
1047 if (s->direct_msi) {
1048 msi.address_lo = (uint32_t)msg.address;
1049 msi.address_hi = msg.address >> 32;
1050 msi.data = msg.data;
1051 msi.flags = 0;
1052 memset(msi.pad, 0, sizeof(msi.pad));
1054 return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
1057 route = kvm_lookup_msi_route(s, msg);
1058 if (!route) {
1059 int virq;
1061 virq = kvm_irqchip_get_virq(s);
1062 if (virq < 0) {
1063 return virq;
1066 route = g_malloc(sizeof(KVMMSIRoute));
1067 route->kroute.gsi = virq;
1068 route->kroute.type = KVM_IRQ_ROUTING_MSI;
1069 route->kroute.flags = 0;
1070 route->kroute.u.msi.address_lo = (uint32_t)msg.address;
1071 route->kroute.u.msi.address_hi = msg.address >> 32;
1072 route->kroute.u.msi.data = msg.data;
1074 kvm_add_routing_entry(s, &route->kroute);
1076 QTAILQ_INSERT_TAIL(&s->msi_hashtab[kvm_hash_msi(msg.data)], route,
1077 entry);
1080 assert(route->kroute.type == KVM_IRQ_ROUTING_MSI);
1082 return kvm_irqchip_set_irq(s, route->kroute.gsi, 1);
1085 int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1087 struct kvm_irq_routing_entry kroute;
1088 int virq;
1090 if (!kvm_irqchip_in_kernel()) {
1091 return -ENOSYS;
1094 virq = kvm_irqchip_get_virq(s);
1095 if (virq < 0) {
1096 return virq;
1099 kroute.gsi = virq;
1100 kroute.type = KVM_IRQ_ROUTING_MSI;
1101 kroute.flags = 0;
1102 kroute.u.msi.address_lo = (uint32_t)msg.address;
1103 kroute.u.msi.address_hi = msg.address >> 32;
1104 kroute.u.msi.data = msg.data;
1106 kvm_add_routing_entry(s, &kroute);
1108 return virq;
1111 static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1113 struct kvm_irqfd irqfd = {
1114 .fd = fd,
1115 .gsi = virq,
1116 .flags = assign ? 0 : KVM_IRQFD_FLAG_DEASSIGN,
1119 if (!kvm_irqchip_in_kernel()) {
1120 return -ENOSYS;
1123 return kvm_vm_ioctl(s, KVM_IRQFD, &irqfd);
1126 #else /* !KVM_CAP_IRQ_ROUTING */
1128 static void kvm_init_irq_routing(KVMState *s)
1132 void kvm_irqchip_release_virq(KVMState *s, int virq)
1136 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1138 abort();
1141 int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1143 abort();
1146 static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1148 abort();
1150 #endif /* !KVM_CAP_IRQ_ROUTING */
1152 int kvm_irqchip_add_irqfd(KVMState *s, int fd, int virq)
1154 return kvm_irqchip_assign_irqfd(s, fd, virq, true);
1157 int kvm_irqchip_remove_irqfd(KVMState *s, int fd, int virq)
1159 return kvm_irqchip_assign_irqfd(s, fd, virq, false);
1162 static int kvm_irqchip_create(KVMState *s)
1164 QemuOptsList *list = qemu_find_opts("machine");
1165 int ret;
1167 if (QTAILQ_EMPTY(&list->head) ||
1168 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
1169 "kernel_irqchip", true) ||
1170 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
1171 return 0;
1174 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
1175 if (ret < 0) {
1176 fprintf(stderr, "Create kernel irqchip failed\n");
1177 return ret;
1180 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
1181 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
1182 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
1184 kvm_kernel_irqchip = true;
1186 kvm_init_irq_routing(s);
1188 return 0;
1191 int kvm_init(void)
1193 static const char upgrade_note[] =
1194 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1195 "(see http://sourceforge.net/projects/kvm).\n";
1196 KVMState *s;
1197 const KVMCapabilityInfo *missing_cap;
1198 int ret;
1199 int i;
1201 s = g_malloc0(sizeof(KVMState));
1204 * On systems where the kernel can support different base page
1205 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1206 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1207 * page size for the system though.
1209 assert(TARGET_PAGE_SIZE <= getpagesize());
1211 #ifdef KVM_CAP_SET_GUEST_DEBUG
1212 QTAILQ_INIT(&s->kvm_sw_breakpoints);
1213 #endif
1214 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
1215 s->slots[i].slot = i;
1217 s->vmfd = -1;
1218 s->fd = qemu_open("/dev/kvm", O_RDWR);
1219 if (s->fd == -1) {
1220 fprintf(stderr, "Could not access KVM kernel module: %m\n");
1221 ret = -errno;
1222 goto err;
1225 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
1226 if (ret < KVM_API_VERSION) {
1227 if (ret > 0) {
1228 ret = -EINVAL;
1230 fprintf(stderr, "kvm version too old\n");
1231 goto err;
1234 if (ret > KVM_API_VERSION) {
1235 ret = -EINVAL;
1236 fprintf(stderr, "kvm version not supported\n");
1237 goto err;
1240 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
1241 if (s->vmfd < 0) {
1242 #ifdef TARGET_S390X
1243 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1244 "your host kernel command line\n");
1245 #endif
1246 ret = s->vmfd;
1247 goto err;
1250 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1251 if (!missing_cap) {
1252 missing_cap =
1253 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1255 if (missing_cap) {
1256 ret = -EINVAL;
1257 fprintf(stderr, "kvm does not support %s\n%s",
1258 missing_cap->name, upgrade_note);
1259 goto err;
1262 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
1264 s->broken_set_mem_region = 1;
1265 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
1266 if (ret > 0) {
1267 s->broken_set_mem_region = 0;
1270 #ifdef KVM_CAP_VCPU_EVENTS
1271 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1272 #endif
1274 s->robust_singlestep =
1275 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
1277 #ifdef KVM_CAP_DEBUGREGS
1278 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1279 #endif
1281 #ifdef KVM_CAP_XSAVE
1282 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1283 #endif
1285 #ifdef KVM_CAP_XCRS
1286 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1287 #endif
1289 #ifdef KVM_CAP_PIT_STATE2
1290 s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1291 #endif
1293 #ifdef KVM_CAP_IRQ_ROUTING
1294 s->direct_msi = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0);
1295 #endif
1297 ret = kvm_arch_init(s);
1298 if (ret < 0) {
1299 goto err;
1302 ret = kvm_irqchip_create(s);
1303 if (ret < 0) {
1304 goto err;
1307 kvm_state = s;
1308 memory_listener_register(&kvm_memory_listener, NULL);
1310 s->many_ioeventfds = kvm_check_many_ioeventfds();
1312 cpu_interrupt_handler = kvm_handle_interrupt;
1314 return 0;
1316 err:
1317 if (s) {
1318 if (s->vmfd >= 0) {
1319 close(s->vmfd);
1321 if (s->fd != -1) {
1322 close(s->fd);
1325 g_free(s);
1327 return ret;
1330 static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1331 uint32_t count)
1333 int i;
1334 uint8_t *ptr = data;
1336 for (i = 0; i < count; i++) {
1337 if (direction == KVM_EXIT_IO_IN) {
1338 switch (size) {
1339 case 1:
1340 stb_p(ptr, cpu_inb(port));
1341 break;
1342 case 2:
1343 stw_p(ptr, cpu_inw(port));
1344 break;
1345 case 4:
1346 stl_p(ptr, cpu_inl(port));
1347 break;
1349 } else {
1350 switch (size) {
1351 case 1:
1352 cpu_outb(port, ldub_p(ptr));
1353 break;
1354 case 2:
1355 cpu_outw(port, lduw_p(ptr));
1356 break;
1357 case 4:
1358 cpu_outl(port, ldl_p(ptr));
1359 break;
1363 ptr += size;
1367 static int kvm_handle_internal_error(CPUArchState *env, struct kvm_run *run)
1369 fprintf(stderr, "KVM internal error.");
1370 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1371 int i;
1373 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
1374 for (i = 0; i < run->internal.ndata; ++i) {
1375 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1376 i, (uint64_t)run->internal.data[i]);
1378 } else {
1379 fprintf(stderr, "\n");
1381 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1382 fprintf(stderr, "emulation failure\n");
1383 if (!kvm_arch_stop_on_emulation_error(env)) {
1384 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1385 return EXCP_INTERRUPT;
1388 /* FIXME: Should trigger a qmp message to let management know
1389 * something went wrong.
1391 return -1;
1394 void kvm_flush_coalesced_mmio_buffer(void)
1396 KVMState *s = kvm_state;
1398 if (s->coalesced_flush_in_progress) {
1399 return;
1402 s->coalesced_flush_in_progress = true;
1404 if (s->coalesced_mmio_ring) {
1405 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
1406 while (ring->first != ring->last) {
1407 struct kvm_coalesced_mmio *ent;
1409 ent = &ring->coalesced_mmio[ring->first];
1411 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
1412 smp_wmb();
1413 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1417 s->coalesced_flush_in_progress = false;
1420 static void do_kvm_cpu_synchronize_state(void *_env)
1422 CPUArchState *env = _env;
1424 if (!env->kvm_vcpu_dirty) {
1425 kvm_arch_get_registers(env);
1426 env->kvm_vcpu_dirty = 1;
1430 void kvm_cpu_synchronize_state(CPUArchState *env)
1432 if (!env->kvm_vcpu_dirty) {
1433 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
1437 void kvm_cpu_synchronize_post_reset(CPUArchState *env)
1439 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1440 env->kvm_vcpu_dirty = 0;
1443 void kvm_cpu_synchronize_post_init(CPUArchState *env)
1445 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1446 env->kvm_vcpu_dirty = 0;
1449 int kvm_cpu_exec(CPUArchState *env)
1451 struct kvm_run *run = env->kvm_run;
1452 int ret, run_ret;
1454 DPRINTF("kvm_cpu_exec()\n");
1456 if (kvm_arch_process_async_events(env)) {
1457 env->exit_request = 0;
1458 return EXCP_HLT;
1461 do {
1462 if (env->kvm_vcpu_dirty) {
1463 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
1464 env->kvm_vcpu_dirty = 0;
1467 kvm_arch_pre_run(env, run);
1468 if (env->exit_request) {
1469 DPRINTF("interrupt exit requested\n");
1471 * KVM requires us to reenter the kernel after IO exits to complete
1472 * instruction emulation. This self-signal will ensure that we
1473 * leave ASAP again.
1475 qemu_cpu_kick_self();
1477 qemu_mutex_unlock_iothread();
1479 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
1481 qemu_mutex_lock_iothread();
1482 kvm_arch_post_run(env, run);
1484 kvm_flush_coalesced_mmio_buffer();
1486 if (run_ret < 0) {
1487 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1488 DPRINTF("io window exit\n");
1489 ret = EXCP_INTERRUPT;
1490 break;
1492 fprintf(stderr, "error: kvm run failed %s\n",
1493 strerror(-run_ret));
1494 abort();
1497 switch (run->exit_reason) {
1498 case KVM_EXIT_IO:
1499 DPRINTF("handle_io\n");
1500 kvm_handle_io(run->io.port,
1501 (uint8_t *)run + run->io.data_offset,
1502 run->io.direction,
1503 run->io.size,
1504 run->io.count);
1505 ret = 0;
1506 break;
1507 case KVM_EXIT_MMIO:
1508 DPRINTF("handle_mmio\n");
1509 cpu_physical_memory_rw(run->mmio.phys_addr,
1510 run->mmio.data,
1511 run->mmio.len,
1512 run->mmio.is_write);
1513 ret = 0;
1514 break;
1515 case KVM_EXIT_IRQ_WINDOW_OPEN:
1516 DPRINTF("irq_window_open\n");
1517 ret = EXCP_INTERRUPT;
1518 break;
1519 case KVM_EXIT_SHUTDOWN:
1520 DPRINTF("shutdown\n");
1521 qemu_system_reset_request();
1522 ret = EXCP_INTERRUPT;
1523 break;
1524 case KVM_EXIT_UNKNOWN:
1525 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1526 (uint64_t)run->hw.hardware_exit_reason);
1527 ret = -1;
1528 break;
1529 case KVM_EXIT_INTERNAL_ERROR:
1530 ret = kvm_handle_internal_error(env, run);
1531 break;
1532 default:
1533 DPRINTF("kvm_arch_handle_exit\n");
1534 ret = kvm_arch_handle_exit(env, run);
1535 break;
1537 } while (ret == 0);
1539 if (ret < 0) {
1540 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1541 vm_stop(RUN_STATE_INTERNAL_ERROR);
1544 env->exit_request = 0;
1545 return ret;
1548 int kvm_ioctl(KVMState *s, int type, ...)
1550 int ret;
1551 void *arg;
1552 va_list ap;
1554 va_start(ap, type);
1555 arg = va_arg(ap, void *);
1556 va_end(ap);
1558 ret = ioctl(s->fd, type, arg);
1559 if (ret == -1) {
1560 ret = -errno;
1562 return ret;
1565 int kvm_vm_ioctl(KVMState *s, int type, ...)
1567 int ret;
1568 void *arg;
1569 va_list ap;
1571 va_start(ap, type);
1572 arg = va_arg(ap, void *);
1573 va_end(ap);
1575 ret = ioctl(s->vmfd, type, arg);
1576 if (ret == -1) {
1577 ret = -errno;
1579 return ret;
1582 int kvm_vcpu_ioctl(CPUArchState *env, int type, ...)
1584 int ret;
1585 void *arg;
1586 va_list ap;
1588 va_start(ap, type);
1589 arg = va_arg(ap, void *);
1590 va_end(ap);
1592 ret = ioctl(env->kvm_fd, type, arg);
1593 if (ret == -1) {
1594 ret = -errno;
1596 return ret;
1599 int kvm_has_sync_mmu(void)
1601 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
1604 int kvm_has_vcpu_events(void)
1606 return kvm_state->vcpu_events;
1609 int kvm_has_robust_singlestep(void)
1611 return kvm_state->robust_singlestep;
1614 int kvm_has_debugregs(void)
1616 return kvm_state->debugregs;
1619 int kvm_has_xsave(void)
1621 return kvm_state->xsave;
1624 int kvm_has_xcrs(void)
1626 return kvm_state->xcrs;
1629 int kvm_has_pit_state2(void)
1631 return kvm_state->pit_state2;
1634 int kvm_has_many_ioeventfds(void)
1636 if (!kvm_enabled()) {
1637 return 0;
1639 return kvm_state->many_ioeventfds;
1642 int kvm_has_gsi_routing(void)
1644 #ifdef KVM_CAP_IRQ_ROUTING
1645 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
1646 #else
1647 return false;
1648 #endif
1651 int kvm_allows_irq0_override(void)
1653 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1656 void kvm_setup_guest_memory(void *start, size_t size)
1658 if (!kvm_has_sync_mmu()) {
1659 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
1661 if (ret) {
1662 perror("qemu_madvise");
1663 fprintf(stderr,
1664 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1665 exit(1);
1670 #ifdef KVM_CAP_SET_GUEST_DEBUG
1671 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
1672 target_ulong pc)
1674 struct kvm_sw_breakpoint *bp;
1676 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
1677 if (bp->pc == pc) {
1678 return bp;
1681 return NULL;
1684 int kvm_sw_breakpoints_active(CPUArchState *env)
1686 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
1689 struct kvm_set_guest_debug_data {
1690 struct kvm_guest_debug dbg;
1691 CPUArchState *env;
1692 int err;
1695 static void kvm_invoke_set_guest_debug(void *data)
1697 struct kvm_set_guest_debug_data *dbg_data = data;
1698 CPUArchState *env = dbg_data->env;
1700 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
1703 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1705 struct kvm_set_guest_debug_data data;
1707 data.dbg.control = reinject_trap;
1709 if (env->singlestep_enabled) {
1710 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1712 kvm_arch_update_guest_debug(env, &data.dbg);
1713 data.env = env;
1715 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
1716 return data.err;
1719 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1720 target_ulong len, int type)
1722 struct kvm_sw_breakpoint *bp;
1723 CPUArchState *env;
1724 int err;
1726 if (type == GDB_BREAKPOINT_SW) {
1727 bp = kvm_find_sw_breakpoint(current_env, addr);
1728 if (bp) {
1729 bp->use_count++;
1730 return 0;
1733 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
1734 if (!bp) {
1735 return -ENOMEM;
1738 bp->pc = addr;
1739 bp->use_count = 1;
1740 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1741 if (err) {
1742 g_free(bp);
1743 return err;
1746 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
1747 bp, entry);
1748 } else {
1749 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1750 if (err) {
1751 return err;
1755 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1756 err = kvm_update_guest_debug(env, 0);
1757 if (err) {
1758 return err;
1761 return 0;
1764 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1765 target_ulong len, int type)
1767 struct kvm_sw_breakpoint *bp;
1768 CPUArchState *env;
1769 int err;
1771 if (type == GDB_BREAKPOINT_SW) {
1772 bp = kvm_find_sw_breakpoint(current_env, addr);
1773 if (!bp) {
1774 return -ENOENT;
1777 if (bp->use_count > 1) {
1778 bp->use_count--;
1779 return 0;
1782 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1783 if (err) {
1784 return err;
1787 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
1788 g_free(bp);
1789 } else {
1790 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1791 if (err) {
1792 return err;
1796 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1797 err = kvm_update_guest_debug(env, 0);
1798 if (err) {
1799 return err;
1802 return 0;
1805 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1807 struct kvm_sw_breakpoint *bp, *next;
1808 KVMState *s = current_env->kvm_state;
1809 CPUArchState *env;
1811 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
1812 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1813 /* Try harder to find a CPU that currently sees the breakpoint. */
1814 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1815 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
1816 break;
1821 kvm_arch_remove_all_hw_breakpoints();
1823 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1824 kvm_update_guest_debug(env, 0);
1828 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1830 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1832 return -EINVAL;
1835 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1836 target_ulong len, int type)
1838 return -EINVAL;
1841 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1842 target_ulong len, int type)
1844 return -EINVAL;
1847 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1850 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1852 int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset)
1854 struct kvm_signal_mask *sigmask;
1855 int r;
1857 if (!sigset) {
1858 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
1861 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
1863 sigmask->len = 8;
1864 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1865 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
1866 g_free(sigmask);
1868 return r;
1871 int kvm_set_ioeventfd_mmio(int fd, uint32_t addr, uint32_t val, bool assign,
1872 uint32_t size)
1874 int ret;
1875 struct kvm_ioeventfd iofd;
1877 iofd.datamatch = val;
1878 iofd.addr = addr;
1879 iofd.len = size;
1880 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1881 iofd.fd = fd;
1883 if (!kvm_enabled()) {
1884 return -ENOSYS;
1887 if (!assign) {
1888 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1891 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1893 if (ret < 0) {
1894 return -errno;
1897 return 0;
1900 int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1902 struct kvm_ioeventfd kick = {
1903 .datamatch = val,
1904 .addr = addr,
1905 .len = 2,
1906 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1907 .fd = fd,
1909 int r;
1910 if (!kvm_enabled()) {
1911 return -ENOSYS;
1913 if (!assign) {
1914 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1916 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1917 if (r < 0) {
1918 return r;
1920 return 0;
1923 int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr)
1925 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1928 int kvm_on_sigbus(int code, void *addr)
1930 return kvm_arch_on_sigbus(code, addr);