qemu-log: fix x86 and user logging
[qemu/opensuse.git] / kvm-all.c
blob1016ca49b19c609a31199a1140af5ac39c8d0908
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 "qemu-option.h"
26 #include "qemu-config.h"
27 #include "sysemu.h"
28 #include "hw/hw.h"
29 #include "hw/msi.h"
30 #include "gdbstub.h"
31 #include "kvm.h"
32 #include "bswap.h"
33 #include "memory.h"
34 #include "exec-memory.h"
36 /* This check must be after config-host.h is included */
37 #ifdef CONFIG_EVENTFD
38 #include <sys/eventfd.h>
39 #endif
41 /* KVM uses PAGE_SIZE in its definition of COALESCED_MMIO_MAX */
42 #define PAGE_SIZE TARGET_PAGE_SIZE
44 //#define DEBUG_KVM
46 #ifdef DEBUG_KVM
47 #define DPRINTF(fmt, ...) \
48 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
49 #else
50 #define DPRINTF(fmt, ...) \
51 do { } while (0)
52 #endif
54 #define KVM_MSI_HASHTAB_SIZE 256
56 typedef struct KVMSlot
58 target_phys_addr_t start_addr;
59 ram_addr_t memory_size;
60 void *ram;
61 int slot;
62 int flags;
63 } KVMSlot;
65 typedef struct kvm_dirty_log KVMDirtyLog;
67 struct KVMState
69 KVMSlot slots[32];
70 int fd;
71 int vmfd;
72 int coalesced_mmio;
73 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
74 bool coalesced_flush_in_progress;
75 int broken_set_mem_region;
76 int migration_log;
77 int vcpu_events;
78 int robust_singlestep;
79 int debugregs;
80 #ifdef KVM_CAP_SET_GUEST_DEBUG
81 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
82 #endif
83 int pit_state2;
84 int xsave, xcrs;
85 int many_ioeventfds;
86 /* The man page (and posix) say ioctl numbers are signed int, but
87 * they're not. Linux, glibc and *BSD all treat ioctl numbers as
88 * unsigned, and treating them as signed here can break things */
89 unsigned irqchip_inject_ioctl;
90 #ifdef KVM_CAP_IRQ_ROUTING
91 struct kvm_irq_routing *irq_routes;
92 int nr_allocated_irq_routes;
93 uint32_t *used_gsi_bitmap;
94 unsigned int gsi_count;
95 QTAILQ_HEAD(msi_hashtab, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE];
96 bool direct_msi;
97 #endif
100 KVMState *kvm_state;
101 bool kvm_kernel_irqchip;
103 static const KVMCapabilityInfo kvm_required_capabilites[] = {
104 KVM_CAP_INFO(USER_MEMORY),
105 KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
106 KVM_CAP_LAST_INFO
109 static KVMSlot *kvm_alloc_slot(KVMState *s)
111 int i;
113 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
114 if (s->slots[i].memory_size == 0) {
115 return &s->slots[i];
119 fprintf(stderr, "%s: no free slot available\n", __func__);
120 abort();
123 static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
124 target_phys_addr_t start_addr,
125 target_phys_addr_t end_addr)
127 int i;
129 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
130 KVMSlot *mem = &s->slots[i];
132 if (start_addr == mem->start_addr &&
133 end_addr == mem->start_addr + mem->memory_size) {
134 return mem;
138 return NULL;
142 * Find overlapping slot with lowest start address
144 static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
145 target_phys_addr_t start_addr,
146 target_phys_addr_t end_addr)
148 KVMSlot *found = NULL;
149 int i;
151 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
152 KVMSlot *mem = &s->slots[i];
154 if (mem->memory_size == 0 ||
155 (found && found->start_addr < mem->start_addr)) {
156 continue;
159 if (end_addr > mem->start_addr &&
160 start_addr < mem->start_addr + mem->memory_size) {
161 found = mem;
165 return found;
168 int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
169 target_phys_addr_t *phys_addr)
171 int i;
173 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
174 KVMSlot *mem = &s->slots[i];
176 if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
177 *phys_addr = mem->start_addr + (ram - mem->ram);
178 return 1;
182 return 0;
185 static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
187 struct kvm_userspace_memory_region mem;
189 mem.slot = slot->slot;
190 mem.guest_phys_addr = slot->start_addr;
191 mem.memory_size = slot->memory_size;
192 mem.userspace_addr = (unsigned long)slot->ram;
193 mem.flags = slot->flags;
194 if (s->migration_log) {
195 mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
197 return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
200 static void kvm_reset_vcpu(void *opaque)
202 CPUArchState *env = opaque;
204 kvm_arch_reset_vcpu(env);
207 int kvm_init_vcpu(CPUArchState *env)
209 KVMState *s = kvm_state;
210 long mmap_size;
211 int ret;
213 DPRINTF("kvm_init_vcpu\n");
215 ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index);
216 if (ret < 0) {
217 DPRINTF("kvm_create_vcpu failed\n");
218 goto err;
221 env->kvm_fd = ret;
222 env->kvm_state = s;
223 env->kvm_vcpu_dirty = 1;
225 mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
226 if (mmap_size < 0) {
227 ret = mmap_size;
228 DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n");
229 goto err;
232 env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
233 env->kvm_fd, 0);
234 if (env->kvm_run == MAP_FAILED) {
235 ret = -errno;
236 DPRINTF("mmap'ing vcpu state failed\n");
237 goto err;
240 if (s->coalesced_mmio && !s->coalesced_mmio_ring) {
241 s->coalesced_mmio_ring =
242 (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
245 ret = kvm_arch_init_vcpu(env);
246 if (ret == 0) {
247 qemu_register_reset(kvm_reset_vcpu, env);
248 kvm_arch_reset_vcpu(env);
250 err:
251 return ret;
255 * dirty pages logging control
258 static int kvm_mem_flags(KVMState *s, bool log_dirty)
260 return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
263 static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
265 KVMState *s = kvm_state;
266 int flags, mask = KVM_MEM_LOG_DIRTY_PAGES;
267 int old_flags;
269 old_flags = mem->flags;
271 flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
272 mem->flags = flags;
274 /* If nothing changed effectively, no need to issue ioctl */
275 if (s->migration_log) {
276 flags |= KVM_MEM_LOG_DIRTY_PAGES;
279 if (flags == old_flags) {
280 return 0;
283 return kvm_set_user_memory_region(s, mem);
286 static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr,
287 ram_addr_t size, bool log_dirty)
289 KVMState *s = kvm_state;
290 KVMSlot *mem = kvm_lookup_matching_slot(s, phys_addr, phys_addr + size);
292 if (mem == NULL) {
293 fprintf(stderr, "BUG: %s: invalid parameters " TARGET_FMT_plx "-"
294 TARGET_FMT_plx "\n", __func__, phys_addr,
295 (target_phys_addr_t)(phys_addr + size - 1));
296 return -EINVAL;
298 return kvm_slot_dirty_pages_log_change(mem, log_dirty);
301 static void kvm_log_start(MemoryListener *listener,
302 MemoryRegionSection *section)
304 int r;
306 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
307 section->size, true);
308 if (r < 0) {
309 abort();
313 static void kvm_log_stop(MemoryListener *listener,
314 MemoryRegionSection *section)
316 int r;
318 r = kvm_dirty_pages_log_change(section->offset_within_address_space,
319 section->size, false);
320 if (r < 0) {
321 abort();
325 static int kvm_set_migration_log(int enable)
327 KVMState *s = kvm_state;
328 KVMSlot *mem;
329 int i, err;
331 s->migration_log = enable;
333 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
334 mem = &s->slots[i];
336 if (!mem->memory_size) {
337 continue;
339 if (!!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) == enable) {
340 continue;
342 err = kvm_set_user_memory_region(s, mem);
343 if (err) {
344 return err;
347 return 0;
350 /* get kvm's dirty pages bitmap and update qemu's */
351 static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
352 unsigned long *bitmap)
354 unsigned int i, j;
355 unsigned long page_number, c;
356 target_phys_addr_t addr, addr1;
357 unsigned int len = ((section->size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
358 unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
361 * bitmap-traveling is faster than memory-traveling (for addr...)
362 * especially when most of the memory is not dirty.
364 for (i = 0; i < len; i++) {
365 if (bitmap[i] != 0) {
366 c = leul_to_cpu(bitmap[i]);
367 do {
368 j = ffsl(c) - 1;
369 c &= ~(1ul << j);
370 page_number = (i * HOST_LONG_BITS + j) * hpratio;
371 addr1 = page_number * TARGET_PAGE_SIZE;
372 addr = section->offset_within_region + addr1;
373 memory_region_set_dirty(section->mr, addr,
374 TARGET_PAGE_SIZE * hpratio);
375 } while (c != 0);
378 return 0;
381 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
384 * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
385 * This function updates qemu's dirty bitmap using
386 * memory_region_set_dirty(). This means all bits are set
387 * to dirty.
389 * @start_add: start of logged region.
390 * @end_addr: end of logged region.
392 static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
394 KVMState *s = kvm_state;
395 unsigned long size, allocated_size = 0;
396 KVMDirtyLog d;
397 KVMSlot *mem;
398 int ret = 0;
399 target_phys_addr_t start_addr = section->offset_within_address_space;
400 target_phys_addr_t end_addr = start_addr + section->size;
402 d.dirty_bitmap = NULL;
403 while (start_addr < end_addr) {
404 mem = kvm_lookup_overlapping_slot(s, start_addr, end_addr);
405 if (mem == NULL) {
406 break;
409 /* XXX bad kernel interface alert
410 * For dirty bitmap, kernel allocates array of size aligned to
411 * bits-per-long. But for case when the kernel is 64bits and
412 * the userspace is 32bits, userspace can't align to the same
413 * bits-per-long, since sizeof(long) is different between kernel
414 * and user space. This way, userspace will provide buffer which
415 * may be 4 bytes less than the kernel will use, resulting in
416 * userspace memory corruption (which is not detectable by valgrind
417 * too, in most cases).
418 * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
419 * a hope that sizeof(long) wont become >8 any time soon.
421 size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
422 /*HOST_LONG_BITS*/ 64) / 8;
423 if (!d.dirty_bitmap) {
424 d.dirty_bitmap = g_malloc(size);
425 } else if (size > allocated_size) {
426 d.dirty_bitmap = g_realloc(d.dirty_bitmap, size);
428 allocated_size = size;
429 memset(d.dirty_bitmap, 0, allocated_size);
431 d.slot = mem->slot;
433 if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
434 DPRINTF("ioctl failed %d\n", errno);
435 ret = -1;
436 break;
439 kvm_get_dirty_pages_log_range(section, d.dirty_bitmap);
440 start_addr = mem->start_addr + mem->memory_size;
442 g_free(d.dirty_bitmap);
444 return ret;
447 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
449 int ret = -ENOSYS;
450 KVMState *s = kvm_state;
452 if (s->coalesced_mmio) {
453 struct kvm_coalesced_mmio_zone zone;
455 zone.addr = start;
456 zone.size = size;
457 zone.pad = 0;
459 ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone);
462 return ret;
465 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
467 int ret = -ENOSYS;
468 KVMState *s = kvm_state;
470 if (s->coalesced_mmio) {
471 struct kvm_coalesced_mmio_zone zone;
473 zone.addr = start;
474 zone.size = size;
475 zone.pad = 0;
477 ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone);
480 return ret;
483 int kvm_check_extension(KVMState *s, unsigned int extension)
485 int ret;
487 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);
488 if (ret < 0) {
489 ret = 0;
492 return ret;
495 static int kvm_check_many_ioeventfds(void)
497 /* Userspace can use ioeventfd for io notification. This requires a host
498 * that supports eventfd(2) and an I/O thread; since eventfd does not
499 * support SIGIO it cannot interrupt the vcpu.
501 * Older kernels have a 6 device limit on the KVM io bus. Find out so we
502 * can avoid creating too many ioeventfds.
504 #if defined(CONFIG_EVENTFD)
505 int ioeventfds[7];
506 int i, ret = 0;
507 for (i = 0; i < ARRAY_SIZE(ioeventfds); i++) {
508 ioeventfds[i] = eventfd(0, EFD_CLOEXEC);
509 if (ioeventfds[i] < 0) {
510 break;
512 ret = kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, true);
513 if (ret < 0) {
514 close(ioeventfds[i]);
515 break;
519 /* Decide whether many devices are supported or not */
520 ret = i == ARRAY_SIZE(ioeventfds);
522 while (i-- > 0) {
523 kvm_set_ioeventfd_pio_word(ioeventfds[i], 0, i, false);
524 close(ioeventfds[i]);
526 return ret;
527 #else
528 return 0;
529 #endif
532 static const KVMCapabilityInfo *
533 kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
535 while (list->name) {
536 if (!kvm_check_extension(s, list->value)) {
537 return list;
539 list++;
541 return NULL;
544 static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
546 KVMState *s = kvm_state;
547 KVMSlot *mem, old;
548 int err;
549 MemoryRegion *mr = section->mr;
550 bool log_dirty = memory_region_is_logging(mr);
551 target_phys_addr_t start_addr = section->offset_within_address_space;
552 ram_addr_t size = section->size;
553 void *ram = NULL;
554 unsigned delta;
556 /* kvm works in page size chunks, but the function may be called
557 with sub-page size and unaligned start address. */
558 delta = TARGET_PAGE_ALIGN(size) - size;
559 if (delta > size) {
560 return;
562 start_addr += delta;
563 size -= delta;
564 size &= TARGET_PAGE_MASK;
565 if (!size || (start_addr & ~TARGET_PAGE_MASK)) {
566 return;
569 if (!memory_region_is_ram(mr)) {
570 return;
573 ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
575 while (1) {
576 mem = kvm_lookup_overlapping_slot(s, start_addr, start_addr + size);
577 if (!mem) {
578 break;
581 if (add && start_addr >= mem->start_addr &&
582 (start_addr + size <= mem->start_addr + mem->memory_size) &&
583 (ram - start_addr == mem->ram - mem->start_addr)) {
584 /* The new slot fits into the existing one and comes with
585 * identical parameters - update flags and done. */
586 kvm_slot_dirty_pages_log_change(mem, log_dirty);
587 return;
590 old = *mem;
592 if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
593 kvm_physical_sync_dirty_bitmap(section);
596 /* unregister the overlapping slot */
597 mem->memory_size = 0;
598 err = kvm_set_user_memory_region(s, mem);
599 if (err) {
600 fprintf(stderr, "%s: error unregistering overlapping slot: %s\n",
601 __func__, strerror(-err));
602 abort();
605 /* Workaround for older KVM versions: we can't join slots, even not by
606 * unregistering the previous ones and then registering the larger
607 * slot. We have to maintain the existing fragmentation. Sigh.
609 * This workaround assumes that the new slot starts at the same
610 * address as the first existing one. If not or if some overlapping
611 * slot comes around later, we will fail (not seen in practice so far)
612 * - and actually require a recent KVM version. */
613 if (s->broken_set_mem_region &&
614 old.start_addr == start_addr && old.memory_size < size && add) {
615 mem = kvm_alloc_slot(s);
616 mem->memory_size = old.memory_size;
617 mem->start_addr = old.start_addr;
618 mem->ram = old.ram;
619 mem->flags = kvm_mem_flags(s, log_dirty);
621 err = kvm_set_user_memory_region(s, mem);
622 if (err) {
623 fprintf(stderr, "%s: error updating slot: %s\n", __func__,
624 strerror(-err));
625 abort();
628 start_addr += old.memory_size;
629 ram += old.memory_size;
630 size -= old.memory_size;
631 continue;
634 /* register prefix slot */
635 if (old.start_addr < start_addr) {
636 mem = kvm_alloc_slot(s);
637 mem->memory_size = start_addr - old.start_addr;
638 mem->start_addr = old.start_addr;
639 mem->ram = old.ram;
640 mem->flags = kvm_mem_flags(s, log_dirty);
642 err = kvm_set_user_memory_region(s, mem);
643 if (err) {
644 fprintf(stderr, "%s: error registering prefix slot: %s\n",
645 __func__, strerror(-err));
646 #ifdef TARGET_PPC
647 fprintf(stderr, "%s: This is probably because your kernel's " \
648 "PAGE_SIZE is too big. Please try to use 4k " \
649 "PAGE_SIZE!\n", __func__);
650 #endif
651 abort();
655 /* register suffix slot */
656 if (old.start_addr + old.memory_size > start_addr + size) {
657 ram_addr_t size_delta;
659 mem = kvm_alloc_slot(s);
660 mem->start_addr = start_addr + size;
661 size_delta = mem->start_addr - old.start_addr;
662 mem->memory_size = old.memory_size - size_delta;
663 mem->ram = old.ram + size_delta;
664 mem->flags = kvm_mem_flags(s, log_dirty);
666 err = kvm_set_user_memory_region(s, mem);
667 if (err) {
668 fprintf(stderr, "%s: error registering suffix slot: %s\n",
669 __func__, strerror(-err));
670 abort();
675 /* in case the KVM bug workaround already "consumed" the new slot */
676 if (!size) {
677 return;
679 if (!add) {
680 return;
682 mem = kvm_alloc_slot(s);
683 mem->memory_size = size;
684 mem->start_addr = start_addr;
685 mem->ram = ram;
686 mem->flags = kvm_mem_flags(s, log_dirty);
688 err = kvm_set_user_memory_region(s, mem);
689 if (err) {
690 fprintf(stderr, "%s: error registering slot: %s\n", __func__,
691 strerror(-err));
692 abort();
696 static void kvm_begin(MemoryListener *listener)
700 static void kvm_commit(MemoryListener *listener)
704 static void kvm_region_add(MemoryListener *listener,
705 MemoryRegionSection *section)
707 kvm_set_phys_mem(section, true);
710 static void kvm_region_del(MemoryListener *listener,
711 MemoryRegionSection *section)
713 kvm_set_phys_mem(section, false);
716 static void kvm_region_nop(MemoryListener *listener,
717 MemoryRegionSection *section)
721 static void kvm_log_sync(MemoryListener *listener,
722 MemoryRegionSection *section)
724 int r;
726 r = kvm_physical_sync_dirty_bitmap(section);
727 if (r < 0) {
728 abort();
732 static void kvm_log_global_start(struct MemoryListener *listener)
734 int r;
736 r = kvm_set_migration_log(1);
737 assert(r >= 0);
740 static void kvm_log_global_stop(struct MemoryListener *listener)
742 int r;
744 r = kvm_set_migration_log(0);
745 assert(r >= 0);
748 static void kvm_mem_ioeventfd_add(MemoryRegionSection *section,
749 bool match_data, uint64_t data, int fd)
751 int r;
753 assert(match_data && section->size <= 8);
755 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
756 data, true, section->size);
757 if (r < 0) {
758 abort();
762 static void kvm_mem_ioeventfd_del(MemoryRegionSection *section,
763 bool match_data, uint64_t data, int fd)
765 int r;
767 r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
768 data, false, section->size);
769 if (r < 0) {
770 abort();
774 static void kvm_io_ioeventfd_add(MemoryRegionSection *section,
775 bool match_data, uint64_t data, int fd)
777 int r;
779 assert(match_data && section->size == 2);
781 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
782 data, true);
783 if (r < 0) {
784 abort();
788 static void kvm_io_ioeventfd_del(MemoryRegionSection *section,
789 bool match_data, uint64_t data, int fd)
792 int r;
794 r = kvm_set_ioeventfd_pio_word(fd, section->offset_within_address_space,
795 data, false);
796 if (r < 0) {
797 abort();
801 static void kvm_eventfd_add(MemoryListener *listener,
802 MemoryRegionSection *section,
803 bool match_data, uint64_t data, int fd)
805 if (section->address_space == get_system_memory()) {
806 kvm_mem_ioeventfd_add(section, match_data, data, fd);
807 } else {
808 kvm_io_ioeventfd_add(section, match_data, data, fd);
812 static void kvm_eventfd_del(MemoryListener *listener,
813 MemoryRegionSection *section,
814 bool match_data, uint64_t data, int fd)
816 if (section->address_space == get_system_memory()) {
817 kvm_mem_ioeventfd_del(section, match_data, data, fd);
818 } else {
819 kvm_io_ioeventfd_del(section, match_data, data, fd);
823 static MemoryListener kvm_memory_listener = {
824 .begin = kvm_begin,
825 .commit = kvm_commit,
826 .region_add = kvm_region_add,
827 .region_del = kvm_region_del,
828 .region_nop = kvm_region_nop,
829 .log_start = kvm_log_start,
830 .log_stop = kvm_log_stop,
831 .log_sync = kvm_log_sync,
832 .log_global_start = kvm_log_global_start,
833 .log_global_stop = kvm_log_global_stop,
834 .eventfd_add = kvm_eventfd_add,
835 .eventfd_del = kvm_eventfd_del,
836 .priority = 10,
839 static void kvm_handle_interrupt(CPUArchState *env, int mask)
841 env->interrupt_request |= mask;
843 if (!qemu_cpu_is_self(env)) {
844 qemu_cpu_kick(env);
848 int kvm_irqchip_set_irq(KVMState *s, int irq, int level)
850 struct kvm_irq_level event;
851 int ret;
853 assert(kvm_irqchip_in_kernel());
855 event.level = level;
856 event.irq = irq;
857 ret = kvm_vm_ioctl(s, s->irqchip_inject_ioctl, &event);
858 if (ret < 0) {
859 perror("kvm_set_irqchip_line");
860 abort();
863 return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
866 #ifdef KVM_CAP_IRQ_ROUTING
867 typedef struct KVMMSIRoute {
868 struct kvm_irq_routing_entry kroute;
869 QTAILQ_ENTRY(KVMMSIRoute) entry;
870 } KVMMSIRoute;
872 static void set_gsi(KVMState *s, unsigned int gsi)
874 s->used_gsi_bitmap[gsi / 32] |= 1U << (gsi % 32);
877 static void clear_gsi(KVMState *s, unsigned int gsi)
879 s->used_gsi_bitmap[gsi / 32] &= ~(1U << (gsi % 32));
882 static void kvm_init_irq_routing(KVMState *s)
884 int gsi_count, i;
886 gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
887 if (gsi_count > 0) {
888 unsigned int gsi_bits, i;
890 /* Round up so we can search ints using ffs */
891 gsi_bits = ALIGN(gsi_count, 32);
892 s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
893 s->gsi_count = gsi_count;
895 /* Mark any over-allocated bits as already in use */
896 for (i = gsi_count; i < gsi_bits; i++) {
897 set_gsi(s, i);
901 s->irq_routes = g_malloc0(sizeof(*s->irq_routes));
902 s->nr_allocated_irq_routes = 0;
904 if (!s->direct_msi) {
905 for (i = 0; i < KVM_MSI_HASHTAB_SIZE; i++) {
906 QTAILQ_INIT(&s->msi_hashtab[i]);
910 kvm_arch_init_irq_routing(s);
913 static void kvm_irqchip_commit_routes(KVMState *s)
915 int ret;
917 s->irq_routes->flags = 0;
918 ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
919 assert(ret == 0);
922 static void kvm_add_routing_entry(KVMState *s,
923 struct kvm_irq_routing_entry *entry)
925 struct kvm_irq_routing_entry *new;
926 int n, size;
928 if (s->irq_routes->nr == s->nr_allocated_irq_routes) {
929 n = s->nr_allocated_irq_routes * 2;
930 if (n < 64) {
931 n = 64;
933 size = sizeof(struct kvm_irq_routing);
934 size += n * sizeof(*new);
935 s->irq_routes = g_realloc(s->irq_routes, size);
936 s->nr_allocated_irq_routes = n;
938 n = s->irq_routes->nr++;
939 new = &s->irq_routes->entries[n];
940 memset(new, 0, sizeof(*new));
941 new->gsi = entry->gsi;
942 new->type = entry->type;
943 new->flags = entry->flags;
944 new->u = entry->u;
946 set_gsi(s, entry->gsi);
948 kvm_irqchip_commit_routes(s);
951 void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin)
953 struct kvm_irq_routing_entry e;
955 assert(pin < s->gsi_count);
957 e.gsi = irq;
958 e.type = KVM_IRQ_ROUTING_IRQCHIP;
959 e.flags = 0;
960 e.u.irqchip.irqchip = irqchip;
961 e.u.irqchip.pin = pin;
962 kvm_add_routing_entry(s, &e);
965 void kvm_irqchip_release_virq(KVMState *s, int virq)
967 struct kvm_irq_routing_entry *e;
968 int i;
970 for (i = 0; i < s->irq_routes->nr; i++) {
971 e = &s->irq_routes->entries[i];
972 if (e->gsi == virq) {
973 s->irq_routes->nr--;
974 *e = s->irq_routes->entries[s->irq_routes->nr];
977 clear_gsi(s, virq);
979 kvm_irqchip_commit_routes(s);
982 static unsigned int kvm_hash_msi(uint32_t data)
984 /* This is optimized for IA32 MSI layout. However, no other arch shall
985 * repeat the mistake of not providing a direct MSI injection API. */
986 return data & 0xff;
989 static void kvm_flush_dynamic_msi_routes(KVMState *s)
991 KVMMSIRoute *route, *next;
992 unsigned int hash;
994 for (hash = 0; hash < KVM_MSI_HASHTAB_SIZE; hash++) {
995 QTAILQ_FOREACH_SAFE(route, &s->msi_hashtab[hash], entry, next) {
996 kvm_irqchip_release_virq(s, route->kroute.gsi);
997 QTAILQ_REMOVE(&s->msi_hashtab[hash], route, entry);
998 g_free(route);
1003 static int kvm_irqchip_get_virq(KVMState *s)
1005 uint32_t *word = s->used_gsi_bitmap;
1006 int max_words = ALIGN(s->gsi_count, 32) / 32;
1007 int i, bit;
1008 bool retry = true;
1010 again:
1011 /* Return the lowest unused GSI in the bitmap */
1012 for (i = 0; i < max_words; i++) {
1013 bit = ffs(~word[i]);
1014 if (!bit) {
1015 continue;
1018 return bit - 1 + i * 32;
1020 if (!s->direct_msi && retry) {
1021 retry = false;
1022 kvm_flush_dynamic_msi_routes(s);
1023 goto again;
1025 return -ENOSPC;
1029 static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
1031 unsigned int hash = kvm_hash_msi(msg.data);
1032 KVMMSIRoute *route;
1034 QTAILQ_FOREACH(route, &s->msi_hashtab[hash], entry) {
1035 if (route->kroute.u.msi.address_lo == (uint32_t)msg.address &&
1036 route->kroute.u.msi.address_hi == (msg.address >> 32) &&
1037 route->kroute.u.msi.data == msg.data) {
1038 return route;
1041 return NULL;
1044 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1046 struct kvm_msi msi;
1047 KVMMSIRoute *route;
1049 if (s->direct_msi) {
1050 msi.address_lo = (uint32_t)msg.address;
1051 msi.address_hi = msg.address >> 32;
1052 msi.data = msg.data;
1053 msi.flags = 0;
1054 memset(msi.pad, 0, sizeof(msi.pad));
1056 return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
1059 route = kvm_lookup_msi_route(s, msg);
1060 if (!route) {
1061 int virq;
1063 virq = kvm_irqchip_get_virq(s);
1064 if (virq < 0) {
1065 return virq;
1068 route = g_malloc(sizeof(KVMMSIRoute));
1069 route->kroute.gsi = virq;
1070 route->kroute.type = KVM_IRQ_ROUTING_MSI;
1071 route->kroute.flags = 0;
1072 route->kroute.u.msi.address_lo = (uint32_t)msg.address;
1073 route->kroute.u.msi.address_hi = msg.address >> 32;
1074 route->kroute.u.msi.data = msg.data;
1076 kvm_add_routing_entry(s, &route->kroute);
1078 QTAILQ_INSERT_TAIL(&s->msi_hashtab[kvm_hash_msi(msg.data)], route,
1079 entry);
1082 assert(route->kroute.type == KVM_IRQ_ROUTING_MSI);
1084 return kvm_irqchip_set_irq(s, route->kroute.gsi, 1);
1087 int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1089 struct kvm_irq_routing_entry kroute;
1090 int virq;
1092 if (!kvm_irqchip_in_kernel()) {
1093 return -ENOSYS;
1096 virq = kvm_irqchip_get_virq(s);
1097 if (virq < 0) {
1098 return virq;
1101 kroute.gsi = virq;
1102 kroute.type = KVM_IRQ_ROUTING_MSI;
1103 kroute.flags = 0;
1104 kroute.u.msi.address_lo = (uint32_t)msg.address;
1105 kroute.u.msi.address_hi = msg.address >> 32;
1106 kroute.u.msi.data = msg.data;
1108 kvm_add_routing_entry(s, &kroute);
1110 return virq;
1113 static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1115 struct kvm_irqfd irqfd = {
1116 .fd = fd,
1117 .gsi = virq,
1118 .flags = assign ? 0 : KVM_IRQFD_FLAG_DEASSIGN,
1121 if (!kvm_irqchip_in_kernel()) {
1122 return -ENOSYS;
1125 return kvm_vm_ioctl(s, KVM_IRQFD, &irqfd);
1128 #else /* !KVM_CAP_IRQ_ROUTING */
1130 static void kvm_init_irq_routing(KVMState *s)
1134 void kvm_irqchip_release_virq(KVMState *s, int virq)
1138 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
1140 abort();
1143 int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg)
1145 abort();
1148 static int kvm_irqchip_assign_irqfd(KVMState *s, int fd, int virq, bool assign)
1150 abort();
1152 #endif /* !KVM_CAP_IRQ_ROUTING */
1154 int kvm_irqchip_add_irqfd(KVMState *s, int fd, int virq)
1156 return kvm_irqchip_assign_irqfd(s, fd, virq, true);
1159 int kvm_irqchip_remove_irqfd(KVMState *s, int fd, int virq)
1161 return kvm_irqchip_assign_irqfd(s, fd, virq, false);
1164 static int kvm_irqchip_create(KVMState *s)
1166 QemuOptsList *list = qemu_find_opts("machine");
1167 int ret;
1169 if (QTAILQ_EMPTY(&list->head) ||
1170 !qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
1171 "kernel_irqchip", true) ||
1172 !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
1173 return 0;
1176 ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
1177 if (ret < 0) {
1178 fprintf(stderr, "Create kernel irqchip failed\n");
1179 return ret;
1182 s->irqchip_inject_ioctl = KVM_IRQ_LINE;
1183 if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
1184 s->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
1186 kvm_kernel_irqchip = true;
1188 kvm_init_irq_routing(s);
1190 return 0;
1193 int kvm_init(void)
1195 static const char upgrade_note[] =
1196 "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
1197 "(see http://sourceforge.net/projects/kvm).\n";
1198 KVMState *s;
1199 const KVMCapabilityInfo *missing_cap;
1200 int ret;
1201 int i;
1203 s = g_malloc0(sizeof(KVMState));
1206 * On systems where the kernel can support different base page
1207 * sizes, host page size may be different from TARGET_PAGE_SIZE,
1208 * even with KVM. TARGET_PAGE_SIZE is assumed to be the minimum
1209 * page size for the system though.
1211 assert(TARGET_PAGE_SIZE <= getpagesize());
1213 #ifdef KVM_CAP_SET_GUEST_DEBUG
1214 QTAILQ_INIT(&s->kvm_sw_breakpoints);
1215 #endif
1216 for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
1217 s->slots[i].slot = i;
1219 s->vmfd = -1;
1220 s->fd = qemu_open("/dev/kvm", O_RDWR);
1221 if (s->fd == -1) {
1222 fprintf(stderr, "Could not access KVM kernel module: %m\n");
1223 ret = -errno;
1224 goto err;
1227 ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
1228 if (ret < KVM_API_VERSION) {
1229 if (ret > 0) {
1230 ret = -EINVAL;
1232 fprintf(stderr, "kvm version too old\n");
1233 goto err;
1236 if (ret > KVM_API_VERSION) {
1237 ret = -EINVAL;
1238 fprintf(stderr, "kvm version not supported\n");
1239 goto err;
1242 s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
1243 if (s->vmfd < 0) {
1244 #ifdef TARGET_S390X
1245 fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "
1246 "your host kernel command line\n");
1247 #endif
1248 ret = s->vmfd;
1249 goto err;
1252 missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);
1253 if (!missing_cap) {
1254 missing_cap =
1255 kvm_check_extension_list(s, kvm_arch_required_capabilities);
1257 if (missing_cap) {
1258 ret = -EINVAL;
1259 fprintf(stderr, "kvm does not support %s\n%s",
1260 missing_cap->name, upgrade_note);
1261 goto err;
1264 s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);
1266 s->broken_set_mem_region = 1;
1267 ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);
1268 if (ret > 0) {
1269 s->broken_set_mem_region = 0;
1272 #ifdef KVM_CAP_VCPU_EVENTS
1273 s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);
1274 #endif
1276 s->robust_singlestep =
1277 kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);
1279 #ifdef KVM_CAP_DEBUGREGS
1280 s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);
1281 #endif
1283 #ifdef KVM_CAP_XSAVE
1284 s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1285 #endif
1287 #ifdef KVM_CAP_XCRS
1288 s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1289 #endif
1291 #ifdef KVM_CAP_PIT_STATE2
1292 s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1293 #endif
1295 #ifdef KVM_CAP_IRQ_ROUTING
1296 s->direct_msi = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0);
1297 #endif
1299 ret = kvm_arch_init(s);
1300 if (ret < 0) {
1301 goto err;
1304 ret = kvm_irqchip_create(s);
1305 if (ret < 0) {
1306 goto err;
1309 kvm_state = s;
1310 memory_listener_register(&kvm_memory_listener, NULL);
1312 s->many_ioeventfds = kvm_check_many_ioeventfds();
1314 cpu_interrupt_handler = kvm_handle_interrupt;
1316 return 0;
1318 err:
1319 if (s) {
1320 if (s->vmfd >= 0) {
1321 close(s->vmfd);
1323 if (s->fd != -1) {
1324 close(s->fd);
1327 g_free(s);
1329 return ret;
1332 static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
1333 uint32_t count)
1335 int i;
1336 uint8_t *ptr = data;
1338 for (i = 0; i < count; i++) {
1339 if (direction == KVM_EXIT_IO_IN) {
1340 switch (size) {
1341 case 1:
1342 stb_p(ptr, cpu_inb(port));
1343 break;
1344 case 2:
1345 stw_p(ptr, cpu_inw(port));
1346 break;
1347 case 4:
1348 stl_p(ptr, cpu_inl(port));
1349 break;
1351 } else {
1352 switch (size) {
1353 case 1:
1354 cpu_outb(port, ldub_p(ptr));
1355 break;
1356 case 2:
1357 cpu_outw(port, lduw_p(ptr));
1358 break;
1359 case 4:
1360 cpu_outl(port, ldl_p(ptr));
1361 break;
1365 ptr += size;
1369 static int kvm_handle_internal_error(CPUArchState *env, struct kvm_run *run)
1371 fprintf(stderr, "KVM internal error.");
1372 if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
1373 int i;
1375 fprintf(stderr, " Suberror: %d\n", run->internal.suberror);
1376 for (i = 0; i < run->internal.ndata; ++i) {
1377 fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
1378 i, (uint64_t)run->internal.data[i]);
1380 } else {
1381 fprintf(stderr, "\n");
1383 if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION) {
1384 fprintf(stderr, "emulation failure\n");
1385 if (!kvm_arch_stop_on_emulation_error(env)) {
1386 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1387 return EXCP_INTERRUPT;
1390 /* FIXME: Should trigger a qmp message to let management know
1391 * something went wrong.
1393 return -1;
1396 void kvm_flush_coalesced_mmio_buffer(void)
1398 KVMState *s = kvm_state;
1400 if (s->coalesced_flush_in_progress) {
1401 return;
1404 s->coalesced_flush_in_progress = true;
1406 if (s->coalesced_mmio_ring) {
1407 struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
1408 while (ring->first != ring->last) {
1409 struct kvm_coalesced_mmio *ent;
1411 ent = &ring->coalesced_mmio[ring->first];
1413 cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len);
1414 smp_wmb();
1415 ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
1419 s->coalesced_flush_in_progress = false;
1422 static void do_kvm_cpu_synchronize_state(void *_env)
1424 CPUArchState *env = _env;
1426 if (!env->kvm_vcpu_dirty) {
1427 kvm_arch_get_registers(env);
1428 env->kvm_vcpu_dirty = 1;
1432 void kvm_cpu_synchronize_state(CPUArchState *env)
1434 if (!env->kvm_vcpu_dirty) {
1435 run_on_cpu(env, do_kvm_cpu_synchronize_state, env);
1439 void kvm_cpu_synchronize_post_reset(CPUArchState *env)
1441 kvm_arch_put_registers(env, KVM_PUT_RESET_STATE);
1442 env->kvm_vcpu_dirty = 0;
1445 void kvm_cpu_synchronize_post_init(CPUArchState *env)
1447 kvm_arch_put_registers(env, KVM_PUT_FULL_STATE);
1448 env->kvm_vcpu_dirty = 0;
1451 int kvm_cpu_exec(CPUArchState *env)
1453 struct kvm_run *run = env->kvm_run;
1454 int ret, run_ret;
1456 DPRINTF("kvm_cpu_exec()\n");
1458 if (kvm_arch_process_async_events(env)) {
1459 env->exit_request = 0;
1460 return EXCP_HLT;
1463 do {
1464 if (env->kvm_vcpu_dirty) {
1465 kvm_arch_put_registers(env, KVM_PUT_RUNTIME_STATE);
1466 env->kvm_vcpu_dirty = 0;
1469 kvm_arch_pre_run(env, run);
1470 if (env->exit_request) {
1471 DPRINTF("interrupt exit requested\n");
1473 * KVM requires us to reenter the kernel after IO exits to complete
1474 * instruction emulation. This self-signal will ensure that we
1475 * leave ASAP again.
1477 qemu_cpu_kick_self();
1479 qemu_mutex_unlock_iothread();
1481 run_ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
1483 qemu_mutex_lock_iothread();
1484 kvm_arch_post_run(env, run);
1486 kvm_flush_coalesced_mmio_buffer();
1488 if (run_ret < 0) {
1489 if (run_ret == -EINTR || run_ret == -EAGAIN) {
1490 DPRINTF("io window exit\n");
1491 ret = EXCP_INTERRUPT;
1492 break;
1494 fprintf(stderr, "error: kvm run failed %s\n",
1495 strerror(-run_ret));
1496 abort();
1499 switch (run->exit_reason) {
1500 case KVM_EXIT_IO:
1501 DPRINTF("handle_io\n");
1502 kvm_handle_io(run->io.port,
1503 (uint8_t *)run + run->io.data_offset,
1504 run->io.direction,
1505 run->io.size,
1506 run->io.count);
1507 ret = 0;
1508 break;
1509 case KVM_EXIT_MMIO:
1510 DPRINTF("handle_mmio\n");
1511 cpu_physical_memory_rw(run->mmio.phys_addr,
1512 run->mmio.data,
1513 run->mmio.len,
1514 run->mmio.is_write);
1515 ret = 0;
1516 break;
1517 case KVM_EXIT_IRQ_WINDOW_OPEN:
1518 DPRINTF("irq_window_open\n");
1519 ret = EXCP_INTERRUPT;
1520 break;
1521 case KVM_EXIT_SHUTDOWN:
1522 DPRINTF("shutdown\n");
1523 qemu_system_reset_request();
1524 ret = EXCP_INTERRUPT;
1525 break;
1526 case KVM_EXIT_UNKNOWN:
1527 fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
1528 (uint64_t)run->hw.hardware_exit_reason);
1529 ret = -1;
1530 break;
1531 case KVM_EXIT_INTERNAL_ERROR:
1532 ret = kvm_handle_internal_error(env, run);
1533 break;
1534 default:
1535 DPRINTF("kvm_arch_handle_exit\n");
1536 ret = kvm_arch_handle_exit(env, run);
1537 break;
1539 } while (ret == 0);
1541 if (ret < 0) {
1542 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
1543 vm_stop(RUN_STATE_INTERNAL_ERROR);
1546 env->exit_request = 0;
1547 return ret;
1550 int kvm_ioctl(KVMState *s, int type, ...)
1552 int ret;
1553 void *arg;
1554 va_list ap;
1556 va_start(ap, type);
1557 arg = va_arg(ap, void *);
1558 va_end(ap);
1560 ret = ioctl(s->fd, type, arg);
1561 if (ret == -1) {
1562 ret = -errno;
1564 return ret;
1567 int kvm_vm_ioctl(KVMState *s, int type, ...)
1569 int ret;
1570 void *arg;
1571 va_list ap;
1573 va_start(ap, type);
1574 arg = va_arg(ap, void *);
1575 va_end(ap);
1577 ret = ioctl(s->vmfd, type, arg);
1578 if (ret == -1) {
1579 ret = -errno;
1581 return ret;
1584 int kvm_vcpu_ioctl(CPUArchState *env, int type, ...)
1586 int ret;
1587 void *arg;
1588 va_list ap;
1590 va_start(ap, type);
1591 arg = va_arg(ap, void *);
1592 va_end(ap);
1594 ret = ioctl(env->kvm_fd, type, arg);
1595 if (ret == -1) {
1596 ret = -errno;
1598 return ret;
1601 int kvm_has_sync_mmu(void)
1603 return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU);
1606 int kvm_has_vcpu_events(void)
1608 return kvm_state->vcpu_events;
1611 int kvm_has_robust_singlestep(void)
1613 return kvm_state->robust_singlestep;
1616 int kvm_has_debugregs(void)
1618 return kvm_state->debugregs;
1621 int kvm_has_xsave(void)
1623 return kvm_state->xsave;
1626 int kvm_has_xcrs(void)
1628 return kvm_state->xcrs;
1631 int kvm_has_pit_state2(void)
1633 return kvm_state->pit_state2;
1636 int kvm_has_many_ioeventfds(void)
1638 if (!kvm_enabled()) {
1639 return 0;
1641 return kvm_state->many_ioeventfds;
1644 int kvm_has_gsi_routing(void)
1646 #ifdef KVM_CAP_IRQ_ROUTING
1647 return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
1648 #else
1649 return false;
1650 #endif
1653 int kvm_allows_irq0_override(void)
1655 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
1658 void *kvm_vmalloc(ram_addr_t size)
1660 #ifdef TARGET_S390X
1661 void *mem;
1663 mem = kvm_arch_vmalloc(size);
1664 if (mem) {
1665 return mem;
1667 #endif
1668 return qemu_vmalloc(size);
1671 void kvm_setup_guest_memory(void *start, size_t size)
1673 if (!kvm_has_sync_mmu()) {
1674 int ret = qemu_madvise(start, size, QEMU_MADV_DONTFORK);
1676 if (ret) {
1677 perror("qemu_madvise");
1678 fprintf(stderr,
1679 "Need MADV_DONTFORK in absence of synchronous KVM MMU\n");
1680 exit(1);
1685 #ifdef KVM_CAP_SET_GUEST_DEBUG
1686 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
1687 target_ulong pc)
1689 struct kvm_sw_breakpoint *bp;
1691 QTAILQ_FOREACH(bp, &env->kvm_state->kvm_sw_breakpoints, entry) {
1692 if (bp->pc == pc) {
1693 return bp;
1696 return NULL;
1699 int kvm_sw_breakpoints_active(CPUArchState *env)
1701 return !QTAILQ_EMPTY(&env->kvm_state->kvm_sw_breakpoints);
1704 struct kvm_set_guest_debug_data {
1705 struct kvm_guest_debug dbg;
1706 CPUArchState *env;
1707 int err;
1710 static void kvm_invoke_set_guest_debug(void *data)
1712 struct kvm_set_guest_debug_data *dbg_data = data;
1713 CPUArchState *env = dbg_data->env;
1715 dbg_data->err = kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, &dbg_data->dbg);
1718 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1720 struct kvm_set_guest_debug_data data;
1722 data.dbg.control = reinject_trap;
1724 if (env->singlestep_enabled) {
1725 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
1727 kvm_arch_update_guest_debug(env, &data.dbg);
1728 data.env = env;
1730 run_on_cpu(env, kvm_invoke_set_guest_debug, &data);
1731 return data.err;
1734 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1735 target_ulong len, int type)
1737 struct kvm_sw_breakpoint *bp;
1738 CPUArchState *env;
1739 int err;
1741 if (type == GDB_BREAKPOINT_SW) {
1742 bp = kvm_find_sw_breakpoint(current_env, addr);
1743 if (bp) {
1744 bp->use_count++;
1745 return 0;
1748 bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
1749 if (!bp) {
1750 return -ENOMEM;
1753 bp->pc = addr;
1754 bp->use_count = 1;
1755 err = kvm_arch_insert_sw_breakpoint(current_env, bp);
1756 if (err) {
1757 g_free(bp);
1758 return err;
1761 QTAILQ_INSERT_HEAD(&current_env->kvm_state->kvm_sw_breakpoints,
1762 bp, entry);
1763 } else {
1764 err = kvm_arch_insert_hw_breakpoint(addr, len, type);
1765 if (err) {
1766 return err;
1770 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1771 err = kvm_update_guest_debug(env, 0);
1772 if (err) {
1773 return err;
1776 return 0;
1779 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1780 target_ulong len, int type)
1782 struct kvm_sw_breakpoint *bp;
1783 CPUArchState *env;
1784 int err;
1786 if (type == GDB_BREAKPOINT_SW) {
1787 bp = kvm_find_sw_breakpoint(current_env, addr);
1788 if (!bp) {
1789 return -ENOENT;
1792 if (bp->use_count > 1) {
1793 bp->use_count--;
1794 return 0;
1797 err = kvm_arch_remove_sw_breakpoint(current_env, bp);
1798 if (err) {
1799 return err;
1802 QTAILQ_REMOVE(&current_env->kvm_state->kvm_sw_breakpoints, bp, entry);
1803 g_free(bp);
1804 } else {
1805 err = kvm_arch_remove_hw_breakpoint(addr, len, type);
1806 if (err) {
1807 return err;
1811 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1812 err = kvm_update_guest_debug(env, 0);
1813 if (err) {
1814 return err;
1817 return 0;
1820 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1822 struct kvm_sw_breakpoint *bp, *next;
1823 KVMState *s = current_env->kvm_state;
1824 CPUArchState *env;
1826 QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {
1827 if (kvm_arch_remove_sw_breakpoint(current_env, bp) != 0) {
1828 /* Try harder to find a CPU that currently sees the breakpoint. */
1829 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1830 if (kvm_arch_remove_sw_breakpoint(env, bp) == 0) {
1831 break;
1836 kvm_arch_remove_all_hw_breakpoints();
1838 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1839 kvm_update_guest_debug(env, 0);
1843 #else /* !KVM_CAP_SET_GUEST_DEBUG */
1845 int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap)
1847 return -EINVAL;
1850 int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1851 target_ulong len, int type)
1853 return -EINVAL;
1856 int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1857 target_ulong len, int type)
1859 return -EINVAL;
1862 void kvm_remove_all_breakpoints(CPUArchState *current_env)
1865 #endif /* !KVM_CAP_SET_GUEST_DEBUG */
1867 int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset)
1869 struct kvm_signal_mask *sigmask;
1870 int r;
1872 if (!sigset) {
1873 return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
1876 sigmask = g_malloc(sizeof(*sigmask) + sizeof(*sigset));
1878 sigmask->len = 8;
1879 memcpy(sigmask->sigset, sigset, sizeof(*sigset));
1880 r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
1881 g_free(sigmask);
1883 return r;
1886 int kvm_set_ioeventfd_mmio(int fd, uint32_t addr, uint32_t val, bool assign,
1887 uint32_t size)
1889 int ret;
1890 struct kvm_ioeventfd iofd;
1892 iofd.datamatch = val;
1893 iofd.addr = addr;
1894 iofd.len = size;
1895 iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
1896 iofd.fd = fd;
1898 if (!kvm_enabled()) {
1899 return -ENOSYS;
1902 if (!assign) {
1903 iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1906 ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
1908 if (ret < 0) {
1909 return -errno;
1912 return 0;
1915 int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
1917 struct kvm_ioeventfd kick = {
1918 .datamatch = val,
1919 .addr = addr,
1920 .len = 2,
1921 .flags = KVM_IOEVENTFD_FLAG_DATAMATCH | KVM_IOEVENTFD_FLAG_PIO,
1922 .fd = fd,
1924 int r;
1925 if (!kvm_enabled()) {
1926 return -ENOSYS;
1928 if (!assign) {
1929 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1931 r = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1932 if (r < 0) {
1933 return r;
1935 return 0;
1938 int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr)
1940 return kvm_arch_on_sigbus_vcpu(env, code, addr);
1943 int kvm_on_sigbus(int code, void *addr)
1945 return kvm_arch_on_sigbus(code, addr);